Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
def scoring():
    score = 0
    last = [0] * 26
    for d in range(len(out)):
        last[out[d]] = d + 1
        score += S[d][out[d]]
        score -= sum((d + 1 - l) * c for l, c in zip(last, C))
    return score

def local_search(d, q, score):
    # old = out[d]
    out[d] = q
    tmp = scoring()
    # if tmp > score:
        # score = tmp
    # else:
        # out[d] = old
    return tmp

D = int(input())
C = list(map(int, input().split()))
S = []
for _ in range(D):
    s = list(map(int, input().split()))
    S.append(s)
out = [int(input()) - 1 for _ in range(D)]
res = scoring()
M = int(input())
for _ in range(M):
    d, q = map(int, input().split())
    res = local_search(d-1, q-1, res)
    print(res)


Fixed submission:
import sys
input = sys.stdin.readline

def scoring(sc):
    last = [0] * 26
    res = 0
    penalty = 0
    for i in range(D):
        t = out[i]
        res += S[i][t]
        penalty += sc - (i + 1 - last[t]) * C[t]
        res -= penalty
        last[t] = i + 1
    return res

D = int(input())
C = list(map(int, input().split()))
S = [list(map(int, input().split())) for _ in range(D)]
out = [int(input()) - 1 for _ in range(D)]
sc = sum(C)
M = int(input())
for _ in range(M):
    d, q = map(int, input().split())
    out[d-1] = q - 1
    print(scoring(sc))

Buggy python submission (verdict: Time Limit Exceeded):
def scoring():
    score = 0
    last = [0] * 26
    for d in range(len(out)):
        last[out[d]] = d + 1
        score += S[d][out[d]]
        score -= sum((d + 1 - l) * c for l, c in zip(last, C))
    return score

def local_search(d, q, score):
    # old = out[d]
    out[d] = q
    tmp = scoring()
    # if tmp > score:
        # score = tmp
    # else:
        # out[d] = old
    return tmp

D = int(input())
C = list(map(int, input().split()))
S = []
for _ in range(D):
    s = list(map(int, input().split()))
    S.append(s)
out = [int(input()) - 1 for _ in range(D)]
res = scoring()
M = int(input())
for _ in range(M):
    d, q = map(int, input().split())
    res = local_search(d-1, q-1, res)
    print(res)

Fixed submission:
import sys
input = sys.stdin.readline

def scoring(sc):
    last = [0] * 26
    res = 0
    penalty = 0
    for i in range(D):
        t = out[i]
        res += S[i][t]
        penalty += sc - (i + 1 - last[t]) * C[t]
        res -= penalty
        last[t] = i + 1
    return res

D = int(input())
C = list(map(int, input().split()))
S = [list(map(int, input().split())) for _ in range(D)]
out = [int(input()) - 1 for _ in range(D)]
sc = sum(C)
M = int(input())
for _ in range(M):
    d, q = map(int, input().split())
    out[d-1] = q - 1
    print(scoring(sc))
Buggy python submission (verdict: Runtime Error):
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
k = 0
idx1 = 0
idx2 = 0
while True:
    if idx1 >= N and idx2 >= M:
        break
    if idx1 >= N:
        if B[idx2] <= K:
            K -= B[idx2]
            idx2 += 1
            continue
        else:
            break
    elif idx2 >= N:
        if A[idx1] <= K:
            K -= A[idx1]
            idx1 += 1
        else:
            break
    if A[idx1] <= B[idx2]:
        if A[idx1] <= K:
            K -= A[idx1]
            idx1 += 1
            continue
        else:
            break
    else:
        if B[idx2] <= K:
            K -= B[idx2]
            idx2 += 1
            continue
        else:
            break
print(idx1 + idx2)


Fixed submission:
from itertools import accumulate
from bisect import bisect_left

N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
acc_A = [0] + list(accumulate(A))
acc_B = [0] + list(accumulate(B))
res = 0
for i, a in enumerate(acc_A):
    if a > K:
        break
    idx = bisect_left(acc_B, K - a)
    idx = min(idx, M)
    if acc_B[idx] > K - a:
        idx -= 1
    res = max(res, i + idx)
print(res)
Buggy python submission (verdict: Time Limit Exceeded):
import functools
import operator
prod = functools.partial(functools.reduce, operator.mul)

def sieve(n=10**7):
    srn = int(n ** 0.5) + 1
    f = [True] * (srn + 1)
    res = []
    for i in range(2, srn+1):
        if not f[i]:
            continue
        res.append(i)
        for j in range(i * i, srn+1, i):
            f[j] = False
    return res

def trial_division(n):
    fn = dict()
    for p in pn:
        if p > n:
            break
        while n % p == 0:
            fn[p] = fn.get(p, 0) + 1
            n //= p
    if n > 1:
        fn[n] = fn.get(n, 0) + 1
    return prod([f + 1 for f in fn.values()])

N = int(input())
pn = sieve(N)
res = sum(i * trial_division(i) for i in range(2, N+1))
print(res + 1)

Fixed submission:
N = int(input())
res = 0
for i in range(1, N+1):
    j = N // i
    res += j * (j + 1) // 2 * i
print(res)

Buggy python submission (verdict: Time Limit Exceeded):
import functools
import operator
prod = functools.partial(functools.reduce, operator.mul)

def sieve(n=10**7):
    srn = int(n ** 0.5) + 1
    f = [True] * (srn + 1)
    res = []
    for i in range(2, srn+1):
        if not f[i]:
            continue
        res.append(i)
        for j in range(i * i, srn+1, i):
            f[j] = False
    return res

def trial_division(n):
    num = dict()
    for p in pn:
        while n % p == 0:
            num[p] = num.get(p, 0) + 1
            n //= p
    if n > 1:
        num[n] = 1
    res = 1
    for v in num.values():
        res *= (v + 1)
    return res

N = int(input())
pn = sieve(N)
res = sum(i * trial_division(i) for i in range(1, N+1))
print(res)


Fixed submission:
N = int(input())
res = 0
for i in range(1, N+1):
    j = N // i
    res += j * (j + 1) // 2 * i
print(res)
Buggy python submission (verdict: Time Limit Exceeded):
MAX = 10 ** 6 + 5
N = int(input())
A = list(map(int, input().split()))
cnt = [0] * MAX
for a in A:
    cnt[a] += 1
for i in range(N):
    a = A[i]
    if cnt[a] == 0:
        continue
    for j in range(2 * a, MAX, a):
        cnt[j] = 0
res = 0
for a in A:
    if cnt[a] == 1:
        res += 1
print(res)

Fixed submission:
MAX = 10 ** 6 + 1
N = int(input())
A = list(map(int, input().split()))
cnt = [0] * MAX
f = [True] * MAX
for a in A:
    cnt[a] += 1
    if not f[a] or cnt[a] > 1:
        continue
    for j in range(2 * a, MAX, a):
        f[j] = False
res = 0
for a in A:
    if f[a] and cnt[a] == 1:
        res += 1
print(res)

Buggy python submission (verdict: Time Limit Exceeded):
MAX = 10 ** 6 + 5
N = int(input())
A = list(map(int, input().split()))
cnt = [0] * MAX
for a in A:
    cnt[a] += 1
for i in range(N):
    a = A[i]
    if cnt[a] == 0:
        continue
    for j in range(2 * a, MAX, a):
        cnt[j] = 0
res = 0
for a in A:
    if cnt[a] == 1:
        res += 1
print(res)

Fixed submission:
MAX = 10 ** 6 + 1
N = int(input())
A = list(map(int, input().split()))
cnt = [0] * MAX
f = [True] * MAX
for a in A:
    cnt[a] += 1
    if not f[a] or cnt[a] > 1:
        continue
    for j in range(2 * a, MAX, a):
        f[j] = False
res = 0
for a in A:
    if f[a] and cnt[a] == 1:
        res += 1
print(res)
Buggy python submission (verdict: Time Limit Exceeded):
MOD = 998244353
N, S = map(int, input().split())
A = list(map(int, input().split()))
dp = [[0] * (S + 1) for _ in range(N+1)]
dp[0][0] = 1
for i in range(N):
    a = A[i]
    for j in range(S+1):
        dp[i+1][j] += dp[i][j] * 2
        dp[i+1][j] %= MOD
        if j + a <= S:
            dp[i+1][j+a] += dp[i][j]
            dp[i+1][j+a] %= MOD
print(dp[N][S])


Fixed submission:
MOD = 998244353
N, S = map(int, input().split())
A = list(map(int, input().split()))
dp = [[0] * (S + 1) for _ in range(N+1)]
dp[0][0] = 1
for i in range(N):
    a = A[i]
    for j in range(S+1):
        dp[i+1][j] += dp[i][j] * 2
        dp[i+1][j] %= MOD
        if j + a <= S:
            dp[i+1][j+a] += dp[i][j]
            dp[i+1][j+a] %= MOD
print(dp[N][S])
Buggy python submission (verdict: Time Limit Exceeded):
H, N = map(int, input().split())
M = [tuple(map(int, input().split())) for _ in range(N)]
dp = [float('inf')] * (H + 1)
dp[0] = 0
for h in range(H):
    if dp[h] == float('inf'):
        continue
    for a, b in M:
        idx = min(H, h + a)
        dp[idx] = min(dp[idx], dp[h] + b)
print(dp[H])


Fixed submission:
H, N = map(int, input().split())
M = [tuple(map(int, input().split())) for _ in range(N)]
dp = [float('inf')] * (H + 1)
dp[0] = 0
for h in range(H):
    if dp[h] == float('inf'):
        continue
    for a, b in M:
        idx = min(H, h + a)
        dp[idx] = min(dp[idx], dp[h] + b)
print(dp[H])
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = input()

mod = 10 ** 9 + 7

if S[0] == "W" or S[-1] == "W":
    print(0)
    exit()

ans = 1
T = [True] * 2 * N
prev = S[0]
t_sum = 1
f_sum = 0
for i in range(1, 2*N-1):
    if S[i] == prev:
        T[i] = not T[i-1]
    else:
        T[i] = T[i-1]
    prev = S[i]

    if T[i]:
        t_sum += 1
    else:
        ans *= (t_sum - f_sum)
        f_sum += 1

if t_sum != N:
    ans = 0

for i in range(1, N+1):
    ans *= i
    ans %= mod

print(ans)

Fixed submission:
N = int(input())
S = input()

mod = 10 ** 9 + 7

if S[0] == "W" or S[-1] == "W":
    print(0)
    exit()

ans = 1
prev_s = S[0]
prev_t = True
t_sum = 1
f_sum = 0
for i in range(1, 2*N-1):
    if S[i] == prev_s:
        t = not prev_t
    else:
        t = prev_t

    if t:
        t_sum += 1
    else:
        ans *= (t_sum - f_sum)
        ans %= mod
        f_sum += 1
    prev_s = S[i]
    prev_t = t

if t_sum != N:
    ans = 0

for i in range(1, N+1):
    ans *= i
    ans %= mod

print(ans)

Buggy python submission (verdict: Runtime Error):
def factorial(N):
    if N == 1:
        return N
    return N * factorial(N-1)

N = int(input())
S = input()

mod = 10 ** 9 + 7

if S[0] == "W" or S[-1] == "W":
    print(0)
    exit()

T = [True] * 2 * N
prev = S[0]
for i in range(1, 2*N-1):
    if S[i] == prev:
        T[i] = not T[i-1]
    else:
        T[i] = T[i-1]
    prev = S[i]
T[-1] = False

ans = 1
t_sum = 0
f_sum = 0
for t in T:
    if t:
        t_sum += 1
    else:
        ans *= (t_sum - f_sum)
        f_sum += 1

if t_sum != f_sum:
    ans = 0

ans %= mod
ans *= factorial(N)
print(ans % mod)

Fixed submission:
N = int(input())
S = input()

mod = 10 ** 9 + 7

if S[0] == "W" or S[-1] == "W":
    print(0)
    exit()

ans = 1
prev_s = S[0]
prev_t = True
t_sum = 1
f_sum = 0
for i in range(1, 2*N-1):
    if S[i] == prev_s:
        t = not prev_t
    else:
        t = prev_t

    if t:
        t_sum += 1
    else:
        ans *= (t_sum - f_sum)
        ans %= mod
        f_sum += 1
    prev_s = S[i]
    prev_t = t

if t_sum != N:
    ans = 0

for i in range(1, N+1):
    ans *= i
    ans %= mod

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
S = input()
T = input()

d = [[] for _ in range(27)]
alpha2num = lambda c: ord(c) - ord("a") + 1
ans = 0

for i, s in enumerate(S):
    d[alpha2num(s)].append(i)

idx = -1
for t in T:
    dl = d[alpha2num(t)]
    if not(dl):
        ans = -1
        break
    for i in dl:
        if i > idx:
            ans += i - idx
            idx = i
            break
    else:
        ans += (len(S) - idx + dl[0])
        idx = dl[0]

print(ans)

Fixed submission:
import bisect

S = input()
T = input()

d = [[] for _ in range(27)]
alpha2num = lambda c: ord(c) - ord("a") + 1

for i, s in enumerate(S):
    d[alpha2num(s)].append(i)

idx = -1
l = 0
ans = 0
for t in T:
    dl = d[alpha2num(t)]
    if not dl:
        ans = -1
        break
    insert_idx = bisect.bisect_right(dl, idx)
    if insert_idx == len(dl):
        l += 1
        idx = dl[0]
    else:
        idx = dl[insert_idx]
    ans = l * len(S) + idx + 1

print(ans)

Buggy python submission (verdict: Runtime Error):
S = input()
T = input()

d = [[] for _ in range(26)]
alpha2num = lambda c: ord(c) - ord("a") + 1
ans = 0

for i, s in enumerate(S):
    d[alpha2num(s)].append(i)

idx = -1
for t in T:
    dl = d[alpha2num(t)]
    if not(dl):
        ans = -1
        break
    for i in dl:
        if i > idx:
            ans += i - idx
            idx = i
            break
    else:
        ans += (len(S) - idx + dl[0])
        idx = dl[0]

print(ans)

Fixed submission:
import bisect

S = input()
T = input()

d = [[] for _ in range(27)]
alpha2num = lambda c: ord(c) - ord("a") + 1

for i, s in enumerate(S):
    d[alpha2num(s)].append(i)

idx = -1
l = 0
ans = 0
for t in T:
    dl = d[alpha2num(t)]
    if not dl:
        ans = -1
        break
    insert_idx = bisect.bisect_right(dl, idx)
    if insert_idx == len(dl):
        l += 1
        idx = dl[0]
    else:
        idx = dl[insert_idx]
    ans = l * len(S) + idx + 1

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
S = input()

N = len(S)
dp = [[0 for _ in range(13)] for _ in range(10 ** 5 + 1)]
mod = 10 ** 9 + 7
dp[0][0] = 1

for i in range(N):
    s = S[i]
    for j in range(10):
        if s != "?" and s != str(j):
            continue
        for r in range(13):
            dp[i+1][(r * 10 + j) % 13] += dp[i][r]
    for j in range(13):
        dp[i+1][j] %= mod

print(dp[N][5])


Fixed submission:
S = input()

mod = 10 ** 9 + 7
N = len(S)
dp = [[0 for _ in range(13)] for _ in range(10 ** 5 + 5)]
dp[0][0] = 1

for i in range(N):
    c = 0
    if S[i] == "?":
        c = -1
    else:
        c = int(S[i])
    for j in range(10):
        if c != -1 and c != j:
            continue
        for k in range(13):
            dp[i+1][(k * 10 + j) % 13] += dp[i][k]
    for j in range(13):
        dp[i+1][j] %= mod

print(dp[N][5])

Buggy python submission (verdict: Time Limit Exceeded):
S = input()

mod = 10 ** 9 + 7
N = len(S)
dp = [[0 for _ in range(13)] for _ in range(10 ** 5 + 5)]
dp[0][0] = 1

for i in range(N):
    c = 0
    if S[i] == "?":
        c = -1
    else:
        c = int(S[i])
    for j in range(10):
        if c != -1 and c != j:
            continue
        for k in range(13):
            dp[i+1][(k * 10 + j) % 13] += dp[i][k]
    for j in range(13):
        dp[i+1][j] %= mod

print(dp[N][5])


Fixed submission:
S = input()

mod = 10 ** 9 + 7
N = len(S)
dp = [[0 for _ in range(13)] for _ in range(10 ** 5 + 5)]
dp[0][0] = 1

for i in range(N):
    c = 0
    if S[i] == "?":
        c = -1
    else:
        c = int(S[i])
    for j in range(10):
        if c != -1 and c != j:
            continue
        for k in range(13):
            dp[i+1][(k * 10 + j) % 13] += dp[i][k]
    for j in range(13):
        dp[i+1][j] %= mod

print(dp[N][5])
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = [int(input()) for _ in range(N)]

for n in range(N):
    print(max(A[:n] + A[n+1:]))

Fixed submission:
N = int(input())
A = [int(input()) for _ in range(N)]
max_a = max(A)
count = A.count(max_a)

if count > 1:
    for n in range(N):
        print(max_a)
else:
    for n in range(N):
        if A[n] == max_a:
            print(max(A[:n] + A[n+1:]))
            continue
        print(max_a)
Buggy python submission (verdict: Time Limit Exceeded):
from bisect import bisect_right
N = int(input())
A = [int(input()) for _ in range(N)][::-1]
res = [A[0]]
for a in A[1:]:
    idx = bisect_right(res, a)
    if idx == len(res):
        res.append(a)
    else:
        if res[idx] == a:
            res.append(a)
        else:
            res[idx] = a
        res.sort()
print(len(res))
    


Fixed submission:
from bisect import bisect_right, insort_right
N = int(input())
A = [int(input()) for _ in range(N)][::-1]
res = [A[0]]
for a in A[1:]:
    idx = bisect_right(res, a)
    if idx == len(res):
        res.append(a)
    else:
        if res[idx] == a:
            insort_right(res, a)   
        else:
            res[idx] = a
print(len(res))

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
a = int(input())
A = [a]
C = [a]

for _ in range(N-1):
    a = (int(input()))
    for i, c in enumerate(C):
        if c < a:
            C[i] = a
            break
    else:
        C.append(a)
print(len(C))




Fixed submission:
from bisect import bisect_right, insort_right
N = int(input())
A = [int(input()) for _ in range(N)][::-1]
res = [A[0]]
for a in A[1:]:
    idx = bisect_right(res, a)
    if idx == len(res):
        res.append(a)
    else:
        if res[idx] == a:
            insort_right(res, a)   
        else:
            res[idx] = a
print(len(res))
Buggy python submission (verdict: Runtime Error):
L, R = map(int, input().split())
mod = 2019

l = L // mod
r = R // mod

if l != r:
    print(0)
else:
    l_ = L % mod
    r_ = R % mod
    ans = 2019
    for i in range(l_, r_):
        for j in range(i+1, r_+1):
            a = i * j % mod
            ans = min(ans, a)
            if ans == 0:
                break
        if ans == 0:
            break
    print(ans)


Fixed submission:
N, A, B = map(int, input().split())
print(min(A*N, B))
Buggy python submission (verdict: Runtime Error):
L, R = map(int, input().split())
mod = 2019

l = L // mod
r = R // mod

if l != r:
    print(0)
else:
   l_ = L % mod
   r_ = R % mod
   ans = 2019
   for i in range(l_, r_):
       for j in range(i+1, r_+1):
           a = i * j % mod
           ans = min(ans, a)
           if ans == 1:
               break
        if ans == 1:
            break
   print(ans)


Fixed submission:
L, R = map(int, input().split())
mod = 2019

l = L // mod
r = R // mod

if l != r:
    print(0)
else:
   l_ = L % mod
   r_ = R % mod
   ans = 2019
   for i in range(l_, r_):
       for j in range(i+1, r_+1):
           a = i * j % mod
           ans = min(ans, a)
   print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
S = list(input())
c = 0
flag = True

while flag:
    flag = False
    for i in range(len(S) - 2):
        if (S[i+2] == 'C') and (S[i+1] == 'B') and (S[i] == 'A'):
            S[i+2], S[i+1], S[i] = 'A', 'C', 'B'
            c += 1
            flag = True
print(c)

Fixed submission:
S = input()
S = S.replace("BC", "D")

c = 0
total = 0

for s in S:
    if s == "A":
        c += 1
    elif s == "D":
        total += c
    else:
        c = 0

print(total)
Buggy python submission (verdict: Runtime Error):
def dfs(d, v, p):
    if (d & 1):
        C[v] = 1
    for w, u in E[v]:
        if u == p:
            continue
        dfs(d ^ w, u, v)

N = int(input())
C = [0] * N
E = [[] for _ in range(N)]
for i in range(N-1):
    u, v, w = map(int, input().split())
    u -= 1
    v -= 1
    E[u].append((w % 2, v))
    E[v].append((w % 2, u))
dfs(0, 0, -1)
print(*C, sep='\n')

Fixed submission:
import sys
sys.setrecursionlimit(10 ** 6)

def dfs(v):
    check[v] = True
    for u, w in T[v]:
        if check[u]:
            continue
        dist[u] = dist[v] + w
        dfs(u)

N = int(input())
T = [[] for _ in range(N+1)]
for _ in range(N-1):
    u, v, w = map(int, input().split())
    T[u].append((v, w))
    T[v].append((u, w))

dist = [0] * (N + 1)
check = [False] * (N + 1)
dfs(1)

print(*[d % 2 for d in dist[1:]], sep="\n")

Buggy python submission (verdict: Runtime Error):
def dfs(v):
    check[v] = True
    for u, w in T[v]:
        if check[u]:
            continue
        dist[u] = dist[v] + w
        dfs(u)

N = int(input())
T = [[] for _ in range(N+1)]
for _ in range(N-1):
    u, v, w = map(int, input().split())
    T[u].append((v, w))
    T[v].append((u, w))

dist = [0] * (N + 1)
check = [False] * (N + 1)
dfs(1)

print(*[d % 2 for d in dist[1:]], sep="\n")

Fixed submission:
import sys
sys.setrecursionlimit(10 ** 6)

def dfs(v):
    check[v] = True
    for u, w in T[v]:
        if check[u]:
            continue
        dist[u] = dist[v] + w
        dfs(u)

N = int(input())
T = [[] for _ in range(N+1)]
for _ in range(N-1):
    u, v, w = map(int, input().split())
    T[u].append((v, w))
    T[v].append((u, w))

dist = [0] * (N + 1)
check = [False] * (N + 1)
dfs(1)

print(*[d % 2 for d in dist[1:]], sep="\n")
Buggy python submission (verdict: Runtime Error):
def dfs(i, j):
    visited[i][j] = True
    res_b = 0
    res_w = 0
    if N[i][j]:
        res_b += 1
    else:
        res_w += 1
    for di, dj in d:
        if i + di < 0 or i + di >= H or j + dj < 0 or j + dj >= W:
            continue
        if N[i][j] == N[i+di][j+dj]:
            continue
        if visited[i+di][j+dj]:
            continue
        tmp_b, tmp_w = dfs(i + di, j + dj)
        res_b += tmp_b
        res_w += tmp_w
    return res_b, res_w

H, W = map(int, input().split())
N = [[False] * W for _ in range(H)]
for i in range(H):
    s = input()
    for j in range(W):
        N[i][j] = (s[j] == "#")

d = [(-1, 0), (1, 0), (0, -1), (0, 1)]
visited = [[False] * W for _ in range(H)]
ans = 0
for i in range(H):
    for j in range(W):
        if not N[i][j]:
            continue
        if visited[i][j]:
            continue
        b, w = dfs(i, j)
        ans += (b * w)

print(ans)

Fixed submission:
import sys
sys.setrecursionlimit(10 ** 7)

def dfs(i, j):
    visited[i][j] = True
    res_b = 0
    res_w = 0
    if N[i][j]:
        res_b += 1
    else:
        res_w += 1
    for di, dj in d:
        if i + di < 0 or i + di >= H or j + dj < 0 or j + dj >= W:
            continue
        if N[i][j] == N[i+di][j+dj]:
            continue
        if visited[i+di][j+dj]:
            continue
        tmp_b, tmp_w = dfs(i + di, j + dj)
        res_b += tmp_b
        res_w += tmp_w
    return res_b, res_w

H, W = map(int, input().split())
N = [[False] * W for _ in range(H)]
for i in range(H):
    s = input()
    for j in range(W):
        N[i][j] = (s[j] == "#")

d = [(-1, 0), (1, 0), (0, -1), (0, 1)]
visited = [[False] * W for _ in range(H)]
ans = 0
for i in range(H):
    for j in range(W):
        if not N[i][j]:
            continue
        if visited[i][j]:
            continue
        b, w = dfs(i, j)
        ans += (b * w)

print(ans)
Buggy python submission (verdict: Runtime Error):
N = int(input())
H = list(map(int, input().split()))
INF = sum(H) + 1

dp = [INF] * N

def rec(i):
    if dp[i] < sum(H) + 1:
        return dp[i]
    if i == 0:
        return 0
    res = INF
    res = min(res, rec(i-1) + abs(H[i] - H[i-1]))
    if i > 1:
        res = min(res, rec(i-2) + abs(H[i] - H[i-2]))
    dp[i] = res
    return dp[i]

print(rec(N-1))

Fixed submission:
N = int(input())
H = list(map(int, input().split()))

dp = [sum(H) + 1] * N
dp[0] = 0

for i in range(1, N):
    if i - 2 >= 0:
        dp[i] = min(dp[i-2] + abs(H[i] - H[i-2]), dp[i])
    dp[i] = min(dp[i-1] + abs(H[i] - H[i-1]), dp[i])

print(dp[-1])

Buggy python submission (verdict: Runtime Error):
N = int(input())
H = list(map(int, input().split()))
INF = sum(H) + 1

dp = [INF] * N

def rec(i):
    if dp[i] < sum(H) + 1:
        return dp[i]
    if i == 0:
        return 0
    res = INF
    res = min(res, rec(i-1) + abs(H[i] - H[i-1]))
    if i > 1:
        res = min(res, rec(i-2) + abs(H[i] - H[i-2]))
    dp[i] = res
    return dp[i]

print(rec(N-1))

Fixed submission:
N = int(input())
H = list(map(int, input().split()))

dp = [sum(H) + 1] * N
dp[0] = 0

for i in range(1, N):
    if i - 2 >= 0:
        dp[i] = min(dp[i-2] + abs(H[i] - H[i-2]), dp[i])
    dp[i] = min(dp[i-1] + abs(H[i] - H[i-1]), dp[i])

print(dp[-1])
Buggy python submission (verdict: Time Limit Exceeded):
N, W = map(int, input().split())

dp = [0] * (W + 1)

for i in range(N):
    w, v = map(int, input().split())
    for j in range(W, w-1, -1):
        dp[j] = max(dp[j], dp[j-w] + v)

print(dp[W])

Fixed submission:
N, W = map(int, input().split())

dp = [0] * (W + 1)

for i in range(N):
    w, v = map(int, input().split())
    for j in range(W, w-1, -1):
        dp[j] = max(dp[j], dp[j-w] + v)

print(dp[W])