Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
n = int(input())
xs = [int(c) for c in input().split()]
ys = [int(c) for c in input().split()]

def dp(x, y, mods):
  table = [False] * (x + 1)
  table[x] = True
  mods.sort(reverse=True)
  for mod in mods:
    for i in range(x + 1):
      table[i % mod] = table[i % mod] or table[i]
  return table[y]

def ok(mods):
  return all(dp(x, y, list(mods)) for x, y in zip(xs, ys))

if not ok(range(1, 51)):
  print(-1)
  quit(0)

mods = []
for mod in range(50, 0, -1):
  if not ok(mods + list(range(1, mod))):
    mods.append(mod)

print(sum(2 ** mod for mod in mods))

Fixed submission:
n = int(input())
xs = [int(c) for c in input().split()]
ys = [int(c) for c in input().split()]

def dp(x, y, mods):
  if y > x:
    return False
  table = [False] * (x + 1)
  table[x] = True
  mods.sort(reverse=True)
  for mod in mods:
    for i in range(x + 1):
      table[i % mod] = table[i % mod] or table[i]
  return table[y]

def ok(mods):
  return all(dp(x, y, list(mods)) for x, y in zip(xs, ys))

if not ok(range(1, 51)):
  print(-1)
  quit(0)

mods = []
for mod in range(50, 0, -1):
  if not ok(mods + list(range(1, mod))):
    mods.append(mod)

print(sum(2 ** mod for mod in mods))
Buggy python submission (verdict: Runtime Error):
s = input().strip()
n = len(s)

ans = 0
for i, c in enumerate(s):
  ld, rd = i, n - i - 1
  if c == '1':
  	ans = min(ans, max(lr, rd))
print(ans)

Fixed submission:
s = input().strip()
n = len(s)

ansone, anszero = n, n
for i, c in enumerate(s):
    ld, rd = i, n - i - 1
    if c == '1':
        ansone = min(ansone, max(ld, rd))
    else:
        anszero = min(anszero, max(ld, rd))
print(max(ansone, anszero))
Buggy python submission (verdict: Time Limit Exceeded):
from itertools import permutations

def main():
    n, m, r = map(int, input().strip().split())
    rs = list(map(int, input().strip().split()))
    dist = [[10 ** 10 for _ in range(n)] for _ in range(n)]
    for i in range(n):
        dist[i][i] = 0
    for _ in range(m):
        a, b, c = map(int, input().strip().split())
        dist[a-1][b-1] = c
        dist[b-1][a-1] = c
    for k in range(n):
        for i in range(n):
            for j in range(n):
                dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
    ans = 10 ** 10
    for trs in permutations(rs):
        tans = 0
        for i in range(len(trs) - 1):
            tans += dist[trs[i]-1][trs[i+1]-1]
        ans = min(ans, tans)
    print(ans)

if __name__ == '__main__':
    main()

Fixed submission:
from itertools import permutations

def main():
    n, m, r = map(int, input().strip().split())
    rs = list(map(int, input().strip().split()))
    dist = [[10 ** 10 for _ in range(n)] for _ in range(n)]
    for i in range(n):
        dist[i][i] = 0
    for _ in range(m):
        a, b, c = map(int, input().strip().split())
        dist[a-1][b-1] = c
        dist[b-1][a-1] = c
    for k in range(n):
        for i in range(n):
            for j in range(n):
                dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
    ans = 10 ** 10
    for trs in permutations(rs):
        tans = 0
        for i in range(len(trs) - 1):
            tans += dist[trs[i]-1][trs[i+1]-1]
        ans = min(ans, tans)
    print(ans)

if __name__ == '__main__':
    main()
Buggy python submission (verdict: Runtime Error):
from collections import Counter

n = int(input())
xs = [int(c) for c in input().split()]

mod = (10 ** 9) + 7

c = Counter(xs)

for k, v in c.items():
    if v == 2:
        dup = k

dup_idx = [i for i, v in enumerate(xs) if v == dup]

facts = [1]
for i in range(1, n + 3):
    facts.append(facts[-1] * i % mod)

def inv(x):
    return pow(x, mod - 2, mod)

def comb(p, q):
    if (not 0 <= q <= p) or p < 0:
        return 0
    return facts[p] * inv(facts[q]) % mod * inv(facts[p - q]) % mod

tl = n + 1
for l in range(1, n + 2):
    use_no = comb(tl - 2, l)
    use_both = comb(tl - 2, l - 2)
    use_one = comb(tl - 2, l - 1) * 2 - comb(min(dup_idx) + (tl - 1 - max(dup_idx)), l - 1)
    print((use_no + use_both + use_one) % mod)
    # print(use_no,use_both ,use_one)

Fixed submission:
n = input()
xs = [int(c) for c in input().split()]

p = xs[::2]
q = xs[1::2]

if len(xs) & 1:
    a = list(reversed(p)) + q
else:
    a = list(reversed(q)) + p

print(' '.join(map(str, a)))
Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter

n = int(input())
xs = [int(c) for c in input().split()]

mod = (10 ** 9) + 7

c = Counter(xs)

for k, v in c.items():
    if v == 2:
        dup = k

dup_idx = [i for i, v in enumerate(xs) if v == dup]

facts = [1]
for i in range(1, n + 3):
    facts.append(facts[-1] * i % mod)

def inv(x):
    return pow(x, mod - 2, mod)

def comb(p, q):
    if (not 0 <= q <= p) or p < 0:
        return 0
    return facts[p] * inv(facts[q]) % mod * inv(facts[p - q]) % mod

tl = n + 1
for l in range(1, n + 2):
    use_no = comb(tl - 2, l)
    use_both = comb(tl - 2, l - 2)
    use_one = comb(tl - 2, l - 1) * 2 - comb(min(dup_idx) + (tl - 1 - max(dup_idx)), l - 1)
    print((use_no + use_both + use_one) % mod)
    # print(use_no,use_both ,use_one)

Fixed submission:
from collections import Counter

n = int(input())
xs = [int(c) for c in input().split()]

mod = (10 ** 9) + 7

c = Counter(xs)

for k, v in c.items():
    if v == 2:
        dup = k

dup_idx = [i for i, v in enumerate(xs) if v == dup]

facts = [1]
for i in range(1, n + 3):
    facts.append(facts[-1] * i % mod)

def inv(x):
    return pow(x, mod - 2, mod)

def comb(p, q):
    if (not 0 <= q <= p) or p < 0:
        return 0
    return facts[p] * inv(facts[q]) % mod * inv(facts[p - q]) % mod

tl = n + 1
for l in range(1, n + 2):
    use_no = comb(tl - 2, l)
    use_both = comb(tl - 2, l - 2)
    use_one = comb(tl - 2, l - 1) * 2 - comb(min(dup_idx) + (tl - 1 - max(dup_idx)), l - 1)
    print((use_no + use_both + use_one) % mod)
    # print(use_no,use_both ,use_one)
Buggy python submission (verdict: Runtime Error):
def main():
    n = int(input())
    xs = [int(c) for c in input().split()]
    l = int(input())

    xs.append(10 ** 17)
    nexts = list(range(n))
    for fr in range(n):
        p, q = fr, n
        while q - p > 1:
            m = (p + q) // 2
            if xs[m] - xs[fr] <= l:
                p = m
            else:
                q = m
        nexts[fr] = p

    doubling = [nexts]
    for _ in range(20):
        base = doubling[-1]
        nexts = [base[base_next] for base_next in base]
        doubling.append(nexts)

    def query(fr, to):
        if fr == to:
            return 0
        p, q = 0, 20
        while q - p > 1:
            m = (p + q) // 2
            if doubling[m][fr] > to:
                q = m
            else:
                p = m

        return query(doubling[p][fr], to) + 2 ** p

    q_count = int(input())
    for _ in range(q_count):
        a, b = map(int, input().split())
        if a > b:
            a, b = b, a
        a -= 1
        b -= 1

        print(query(a, b))


if __name__ == '__main__':
    main()

Fixed submission:
def main():
    n = int(input())
    xs = [int(c) for c in input().split()]
    l = int(input())

    xs.append(10 ** 17)
    nexts = list(range(n))
    for fr in range(n):
        p, q = fr, n
        while q - p > 1:
            m = (p + q) // 2
            if xs[m] - xs[fr] <= l:
                p = m
            else:
                q = m
        nexts[fr] = p

    doubling = [nexts]
    for _ in range(20):
        base = doubling[-1]
        nexts = [base[base_next] for base_next in base]
        doubling.append(nexts)

    def query(fr, to):
        if fr >= to:
            return 0
        p, q = 0, 20
        while q - p > 1:
            m = (p + q) // 2
            if doubling[m][fr] >= to:
                q = m
            else:
                p = m
        return query(doubling[p][fr], to) + 2 ** p

    q_count = int(input())
    for _ in range(q_count):
        a, b = map(int, input().split())
        if a > b:
            a, b = b, a
        a -= 1
        b -= 1

        print(query(a, b))


if __name__ == '__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
import sys

readline = sys.stdin.readline
readall = sys.stdin.read
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
prn = lambda x: print(*x, sep='\n')

def solve():
    mod = 998244353
    a, b, c, d = nm()
    n, m = c-a, d-b
    dp = [[0]*(m+1) for _ in range(n+1)]
    dp[0][0] = 1
    for i in range(n):
        dp[i+1][0] = dp[i][0] * b % mod
    for i in range(m):
        dp[0][i+1] = dp[0][i] * a % mod
    for i in range(1, n+1):
        for j in range(1, m+1):
            dp[i][j] = (dp[i][j-1] * (a + i) + dp[i-1][j] * (b + j) - dp[i-1][j-1] * (a + i - 1) * (b + j - 1)) % mod
    # if a < 10:
    #     prn(dp)
    print(dp[n][m])

    return

solve()


Fixed submission:
import sys

readline = sys.stdin.readline
readall = sys.stdin.read
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
prn = lambda x: print(*x, sep='\n')

def solve():
    mod = 998244353
    a, b, c, d = nm()
    n, m = c-a, d-b
    dp = [[0]*(m+1) for _ in range(n+1)]
    dp[0][0] = 1
    for i in range(n):
        dp[i+1][0] = dp[i][0] * b % mod
    for i in range(m):
        dp[0][i+1] = dp[0][i] * a % mod
    for i in range(1, n+1):
        for j in range(1, m+1):
            dp[i][j] = (dp[i][j-1] * (a + i) + dp[i-1][j] * (b + j) - dp[i-1][j-1] * (a + i - 1) * (b + j - 1)) % mod
    # if a < 10:
    #     prn(dp)
    print(dp[n][m])

    return

solve()
Buggy python submission (verdict: Runtime Error):
x, n = map(int, input().split())
a = set(map(int, input().split()))
print(min(i for i in range(102) if i not in a, key=lambda y:abs(x-y)))


Fixed submission:
x, n = map(int, input().split())
a = set(map(int, input().split()))
print(min([i for i in range(102) if i not in a], key=lambda y:abs(x-y)))
Buggy python submission (verdict: Runtime Error):
import sys

stdin = sys.stdin

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

n = ni()
a = nl()
lis = [(e, i) for i, e in enumerate(a)]
lis.sort()
dp = [[-1]*n for _ in range(n)]

def solve(l, r):
    if l > r:
        return 0
    if dp[l][r] >= 0:
        return dp[l][r]
    idx = r - l
    e, i = lis[idx]
    dp[l][r] = max(abs(i-l)*e + solve(l+1, r), abs(r-i)*e + solve(l, r-1))
    return dp[l][r]

print(solve(0, n-1))


Fixed submission:
import sys

stdin = sys.stdin
sys.setrecursionlimit(10**6)
ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

n = ni()
a = nl()
lis = [(e, i) for i, e in enumerate(a)]
lis.sort()
dp = [[-1]*n for _ in range(n)]

def solve(l, r):
    if l > r:
        return 0
    if dp[l][r] >= 0:
        return dp[l][r]
    idx = r - l
    e, i = lis[idx]
    dp[l][r] = max(abs(i-l)*e + solve(l+1, r), abs(r-i)*e + solve(l, r-1))
    return dp[l][r]

print(solve(0, n-1))
Buggy python submission (verdict: Time Limit Exceeded):
import sys

stdin = sys.stdin

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

n = ni()
s = list(map(ord, ns()))
d = dict()
for x in map(ord, list('RGB')):
    d[x] = s.count(x)
tot = sum(map(ord, list('RGB')))
ans = 0
for i in range(n-1):
    for j in range(i):
        if s[i] != s[j]:
            t = tot - s[i] - s[j]
            ans += d[t] - ((2*i - j) < n and t == s[2*i - j])
    d[s[i]] -= 1
print(ans)


Fixed submission:
import sys

stdin = sys.stdin

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

n = ni()
s = list(map(ord, ns()))
d = dict()
for x in map(ord, list('RGB')):
    d[x] = s.count(x)
tot = sum(map(ord, list('RGB')))
ans = 0
for i in range(n-1):
    for j in range(i):
        if s[i] != s[j]:
            t = tot - s[i] - s[j]
            ans += d[t] - ((2*i - j) < n and t == s[2*i - j])
    d[s[i]] -= 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys

stdin = sys.stdin

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

n = ni()
s = ns()
d = dict()
for x in 'RGB':
    d[x] = s.count(x)
ans = 0
for i in range(n-1):
    for j in range(i):
        if s[i] != s[j]:
            t = list({'R', 'G', 'B'} - {s[i], s[j]})[0]
            ans += d[t] - ((2*i - j) < n and t == s[2*i - j])
    d[s[i]] -= 1
print(ans)


Fixed submission:
import sys

stdin = sys.stdin

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

n = ni()
s = list(map(ord, ns()))
d = dict()
for x in map(ord, list('RGB')):
    d[x] = s.count(x)
tot = sum(map(ord, list('RGB')))
ans = 0
for i in range(n-1):
    for j in range(i):
        if s[i] != s[j]:
            t = tot - s[i] - s[j]
            ans += d[t] - ((2*i - j) < n and t == s[2*i - j])
    d[s[i]] -= 1
print(ans)
Buggy python submission (verdict: Runtime Error):
#![allow(unused_imports, non_snake_case)]
use proconio::{
    input, fastout,
    marker::{Chars, Bytes, Usize1}
};

macro_rules! max {($ a : expr $ (, ) * ) => {{$ a } } ; ($ a : expr , $ b : expr $ (, ) * ) => {{std :: cmp :: max ($ a , $ b ) } } ; ($ a : expr , $ ($ rest : expr ) ,+ $ (, ) * ) => {{std :: cmp :: max ($ a , max ! ($ ($ rest ) ,+ ) ) } } ; }

#[fastout]
fn main() {
    input! {
        n: usize,
        a: [usize; n]
    }
    let mut dp = vec![vec![0; 3]; n+4];
    for i in 0..n{
        dp[i + 4][0] = dp[i + 2][0] + a[i];
        dp[i + 4][1] = max!(dp[i+2][1], dp[i+1][0]) + a[i];
        if n & 1 > 0 {
            dp[i + 4][2] = max!(dp[i + 2][2], dp[i + 1][1], dp[i][0]) + a[i];
        }
    }
    if n & 1 > 0 {
        println!("{}", max!(dp[n+1][0], dp[n+2][1], dp[n+3][2]));
    } else {
        println!("{}", max!(dp[n+2][0], dp[n+3][1]));
    }
}

Fixed submission:
import sys

stdin = sys.stdin

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

n = ni()
a = nl()
dp = [[0]*3 for _ in range(n+5)]
t = n%2
for i in range(n):
    dp[i][0] = dp[i-2][0] + a[i]
    dp[i][1] = max(dp[i-2][1], dp[i-3][0]) + a[i]
    if t:
        dp[i][2] = max(dp[i-2][2], dp[i-3][1], dp[i-4][0]) + a[i]
if t:
    print(max(dp[n-3][0], dp[n-2][1], dp[n-1][2]))
else:
    print(max(dp[n-2][0], dp[n-1][1]))
Buggy python submission (verdict: Time Limit Exceeded):
import sys

stdin = sys.stdin

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

def divisor(n):
    ass = []
    for i in range(1, int(n**0.5)+1):
        if n%i == 0:
            ass.append(i)
            ass.append(n//i)
    return ass

n = ni()
p = divisor(n)
s = set()
for k in p:
    nk = n // k
    for t in divisor(nk - 1):
        s.add(t*k)
ans = 0
s.remove(1)
s.add(n)
for t in s:
    m = n
    while m % t == 0:
        m //= t
    if m % t == 1:
        ans += 1
print(ans)


Fixed submission:
import sys

stdin = sys.stdin

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

def divisor(n):
    ass = []
    for i in range(1, int(n**0.5)+1):
        if n%i == 0:
            ass.append(i)
            ass.append(n//i)
    return ass

n = ni()
p = divisor(n)
s = set()
for k in p:
    nk = n // k
    for t in divisor(nk - 1):
        s.add(t*k)
ans = 0
s.remove(1)
s.add(n)
for t in s:
    m = n
    while m % t == 0:
        m //= t
    if m % t == 1:
        ans += 1
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import sys

stdin = sys.stdin

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

h,w,k = nm()
s = [list(map(int, list(ns()))) for _ in range(h)]
ans = h*w
for bit in range(1<<(h-1)):
    d = dict()
    l = list()
    q = [0]
    for j in range(h-1):
        if bit & (1<<j):
            l.append(tuple(q))
            q = list()
        q.append(j+1)
    l.append(tuple(q))
    t = len(l)
    for i in range(t):
        for x in l[i]:
            d[x] = i
    cur = bin(bit).count('1')
    g = [0]*t
    for j in range(w):
        c = [0]*t
        for i in range(h):
            c[d[i]] += s[i][j]
        if max(c) > k:
            cur = h*w
            break
        for i in range(t):
            g[i] += c[i]
        if max(g) > k:
            cur += 1
            g = c[:]
    if ans > cur: ans = cur
print(ans)


Fixed submission:
import sys

stdin = sys.stdin

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

h,w,k = nm()
s = [list(map(int, list(ns()))) for _ in range(h)]
ans = h*w
for bit in range(1<<(h-1)):
    d = dict()
    l = list()
    q = [0]
    for j in range(h-1):
        if bit & (1<<j):
            l.append(tuple(q))
            q = list()
        q.append(j+1)
    l.append(tuple(q))
    t = len(l)
    for i in range(t):
        for x in l[i]:
            d[x] = i
    cur = bin(bit).count('1')
    g = [0]*t
    for j in range(w):
        c = [0]*t
        for i in range(h):
            c[d[i]] += s[i][j]
        if max(c) > k:
            cur = h*w
            break
        for i in range(t):
            g[i] += c[i]
        if max(g) > k:
            cur += 1
            g = c[:]
    if ans > cur: ans = cur
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import sys

stdin = sys.stdin

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

n,s = nm()
a = nl()
mod = 998244353
ans = 0
dp = [[0]*(s+1) for i in range(n+1)]
dp[0][0] = 1
for i,v in enumerate(a):
    dp[i+1][0] = 1
    for j in range(s+1):
        dp[i+1][j] += dp[i][j]
        if j >= v: dp[i+1][j] += dp[i][j-v]
        dp[i+1][j] %= mod
    ans += dp[i+1][s]
print(ans%mod)


Fixed submission:
import sys

stdin = sys.stdin

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

n,s = nm()
a = nl()
mod = 998244353
ans = 0
dp = [[0]*(s+1) for i in range(n+1)]
dp[0][0] = 1
for i,v in enumerate(a):
    dp[i+1][0] = 1
    for j in range(s+1):
        dp[i+1][j] += dp[i][j]
        if j >= v: dp[i+1][j] += dp[i][j-v]
        dp[i+1][j] %= mod
    ans += dp[i+1][s]
print(ans%mod)

Buggy python submission (verdict: Time Limit Exceeded):
import sys

stdin = sys.stdin

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

n,s = nm()
a = nl()
mod = 998244353
ans = 0
dp = [[0]*(s+1) for i in range(n+1)]
dp[0][0] = 1
for i,v in enumerate(a):
    dp[i+1][0] = 1
    for j in range(s+1):
        dp[i+1][j] += dp[i][j]
        if j >= v: dp[i+1][j] += dp[i][j-v]
        dp[i+1][j] %= mod
    ans += dp[i+1][s]
print(ans%mod)


Fixed submission:
import sys

stdin = sys.stdin

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

n,s = nm()
a = nl()
mod = 998244353
ans = 0
dp = [[0]*(s+1) for i in range(n+1)]
dp[0][0] = 1
for i,v in enumerate(a):
    dp[i+1][0] = 1
    for j in range(s+1):
        dp[i+1][j] += dp[i][j]
        if j >= v: dp[i+1][j] += dp[i][j-v]
        dp[i+1][j] %= mod
    ans += dp[i+1][s]
print(ans%mod)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import deque

stdin = sys.stdin

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

h,w = nm()
g = [list(map(lambda s:s=='#', list(input()))) for _ in range(h)]
dp = [[h*w]*w for _ in range(h)]
q = deque()
q.append((0,g[0][0]))
while q:
    v,d = q.popleft()
    if v == h*w-1: print(int(d)); break
    y,x = divmod(v,w)
    if dp[y][x] < d: continue
    dp[y][x] = d
    if y < h-1:
        if g[y+1][x] < g[y][x]:
            q.append((v+w, d+1))
        else:
            q.appendleft((v+w, d))
    if x < w-1:
        if g[y][x+1] < g[y][x]:
            q.append((v+1, d+1))
        else:
            q.appendleft((v+1, d))


Fixed submission:
import sys

stdinr = sys.stdin.readline

ns = lambda: stdinr().rstrip()
ni = lambda: int(stdinr().rstrip())
nm = lambda: map(int, stdinr().split())
nl = lambda: list(map(int, stdinr().split()))

h,w = nm()
g = [list(map(lambda s:s=='#', list(input()))) for _ in range(h)]
dp = [[h*w]*w for _ in range(h)]
dp[0][0] = g[0][0]
for i in range(h):
    for j in range(w):
        if i < h-1:
            d = dp[i][j] + (g[i][j] < g[i+1][j])
            if dp[i+1][j] > d: dp[i+1][j] = d
        if j < w-1:
            d = dp[i][j] + (g[i][j] < g[i][j+1])
            if dp[i][j+1] > d: dp[i][j+1] = d
print(dp[-1][-1])
Buggy python submission (verdict: Runtime Error):
n,p = map(int, input().split())
s = list(map(int, list(ns())))
if p in (2, 5):
  print(sum(i+1 for i in range(n) if s[i]%p == 0))
  exit(0)
   
ans = g = 0
d = [0]*p
d[0] = 1
t = 1
for i in range(n-1,-1,-1):
    g = (g + s[i]*t)%p
    ans += d[g]
    d[g] += 1
    t = (t*10)%p
print(ans)

Fixed submission:
import sys

stdin = sys.stdin

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

n,p = nm()
s = list(map(int, list(ns())))
ans = 0
if p == 2:
  for i in range(n):
    if s[i]%2 == 0:
      ans += i+1
  print(ans); exit(0)
if p == 5:
  for i in range(n):
    if s[i]%5 == 0:
      ans += i+1
  print(ans); exit(0)
   
g = [0]*(n+1)
d = [0]*p
d[0] = 1
t = 1
for i in range(n-1,-1,-1):
    g[i] = (g[i+1] + s[i]*t)%p
    ans += d[g[i]]
    d[g[i]] += 1
    t = (t*10)%p
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import sys

stdin = sys.stdin

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

h,n = nm()
v = [nl() for _ in range(n)]
dp = [1<<30]*(h+10020)
dp[0] = 0
for a,b in v:
    for i in range(h+1):
        if dp[i+a] > dp[i] + b:
            dp[i+a] = dp[i] + b
print(min(dp[h:]))


Fixed submission:
import sys

stdin = sys.stdin

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

h,n = nm()
v = [nl() for _ in range(n)]
dp = [1<<30]*(h+10020)
dp[0] = 0
for a,b in v:
    for i in range(h+1):
        if dp[i+a] > dp[i] + b:
            dp[i+a] = dp[i] + b
print(min(dp[h:]))