Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
a, b, x = map(int, input().split())
if a  x or a+b < x:
    print('NO')
else:
    print('YES')

Fixed submission:
a, b, x = map(int, input().split())
if a > x or a+b < x:
    print('NO')
else:
    print('YES')
Buggy python submission (verdict: Runtime Error):
N = int(input())
X = [int(i) for i in input().split()]

for i in range(N):
    tmp = []
    once = True
    delnum = X[i]
    for j in range(len(X)):
        if X[j] == delnum and once:
            continue  
        else:
            tmp.append(X[j])
            once = False
    tmp = sorted(tmp)
    print(tmp[int((len(tmp)+1)/2)])

Fixed submission:
import copy

N = int(input())
X = [int(i) for i in input().split()]
tmp = copy.deepcopy(X)

X = sorted(X)
for i in range(N):
    ans1 = X[int(len(X)/2)]
    ans2 = X[int(len(X)/2)-1]
    if tmp[i] < ans1:
        print(ans1)
    else:
        print(ans2)

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

N = int(input())
X = [int(i) for i in input().split()]

for i in range(N):
    tmp = copy.deepcopy(X)
    del tmp[i]
    tmp = sorted(tmp)
    print(tmp[int((len(tmp)+1)/2)-1])

Fixed submission:
import copy

N = int(input())
X = [int(i) for i in input().split()]
tmp = copy.deepcopy(X)

X = sorted(X)
for i in range(N):
    ans1 = X[int(len(X)/2)]
    ans2 = X[int(len(X)/2)-1]
    if tmp[i] < ans1:
        print(ans1)
    else:
        print(ans2)
Buggy python submission (verdict: Runtime Error):
n = int(input())
s = input().split()

if 'Y' in s:
    print('Four')
else:
    print('Three')

Fixed submission:
import math

n = int(input())
d = n / 3

if d == 0:
    print('0')
else:
    print(math.floor(d))
Buggy python submission (verdict: Runtime Error):
A,B,C,D = map(int, input().split())
from math import gcd
cd = C*D//gcd(D,C)
f = lambda x: B//x - ((A-1)//x)
print(B - A + 1 - (f(C) + f(D) - f(cd)))

Fixed submission:
A,B,C,D = map(int, input().split())
from fractions import gcd

cd = C*D//gcd(D,C)
f = lambda x: B//x - ((A-1)//x)
print(B - A + 1 - (f(C) + f(D) - f(cd)))

Buggy python submission (verdict: Runtime Error):
a,b,c,d = input()
count = 0
for i in range(a,b+1):
  if i % c == 0 or i % d == 0:
    continue
  count += 1

print(count)

Fixed submission:
A,B,C,D = map(int, input().split())
from fractions import gcd

cd = C*D//gcd(D,C)
f = lambda x: B//x - ((A-1)//x)
print(B - A + 1 - (f(C) + f(D) - f(cd)))
Buggy python submission (verdict: Runtime Error):
N = int(input())
edg = [list(map(int,input().split())) for _ in range(N-1)]
vtx = [[] for _ in range(N)]
for e in edg:
    v1,v2,w = e
    vtx[v1-1].append([v2-1,w])
    vtx[v2-1].append([v1-1,w])

colors = [1 for _ in range(N)]

def dfs(vtx,i=0,p=-1,c=1): # vertex and its parent
    colors[i] = c
    for v,w in vtx[i]:
        if v == p:
            continue
        dfs(vtx,v,i,1-c if w%2 != 0 else c)
dfs(vtx)

for v in colors:
    print(v)

Fixed submission:
import sys
sys.setrecursionlimit(10**6)
N = int(input())
edg = [list(map(int,input().split())) for _ in range(N-1)]
vtx = [[] for _ in range(N)]
for e in edg:
    v1,v2,w = e
    vtx[v1-1].append([v2-1,w])
    vtx[v2-1].append([v1-1,w])

colors = [1 for _ in range(N)]

def dfs(vtx,i=0,p=-1,c=1): # vertex and its parent
    colors[i] = c
    for v,w in vtx[i]:
        if v == p:
            continue
        dfs(vtx,v,i,1-c if w%2 != 0 else c)
dfs(vtx)

for v in colors:
    print(v)

Buggy python submission (verdict: Runtime Error):
N = int(input())
edg = [list(map(int,input().split())) for _ in range(N-1)]
vtx = [[] for _ in range(N)]
for e in edg:
    v1,v2,w = e
    vtx[v1-1].append([v2-1,w])
    vtx[v2-1].append([v1-1,w])

colors = [1 for _ in range(N)]
def dfs(vtx,i=0,p=-1): # vertex and its parent
    for c,w in vtx[i]:
        if c == p:
            continue
        colors[c] = 1 - colors[i] if w%2 != 0 else colors[i]
        dfs(vtx,c,i)
dfs(vtx)

for v in colors:
    print(v)

Fixed submission:
import sys
sys.setrecursionlimit(10**6)
N = int(input())
edg = [list(map(int,input().split())) for _ in range(N-1)]
vtx = [[] for _ in range(N)]
for e in edg:
    v1,v2,w = e
    vtx[v1-1].append([v2-1,w])
    vtx[v2-1].append([v1-1,w])

colors = [1 for _ in range(N)]

def dfs(vtx,i=0,p=-1,c=1): # vertex and its parent
    colors[i] = c
    for v,w in vtx[i]:
        if v == p:
            continue
        dfs(vtx,v,i,1-c if w%2 != 0 else c)
dfs(vtx)

for v in colors:
    print(v)
Buggy python submission (verdict: Runtime Error):
print("{}{}{}".format(s[0],len(s)-2,s[-1]))

Fixed submission:
s = input()
print("{}{}{}".format(s[0],len(s)-2,s[-1]))

Buggy python submission (verdict: Runtime Error):
def main(str):
  print("{}{}{}".format(str[0],str(len(str)-2),str[len(str)-1])

Fixed submission:
s = input()
print("{}{}{}".format(s[0],len(s)-2,s[-1]))
Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import deque
from collections import defaultdict
import math
import copy
#input = sys.stdin.readline
sys.setrecursionlimit(20000000)
s = input()
n = len(s)
dp = [[0]*2019 for i in range(n+1)]
for i in range(n+1):
    dp[i][0] = 1
for i in range(n):
    for j in range(2019):
        dp[i+1][(j*10+int(s[i]))%2019] += dp[i][j]
ans = 0
for i in range(n+1):
    ans += dp[i][0]
print(ans-n-1)


Fixed submission:
import sys
from collections import deque
from collections import defaultdict
import math
import copy
#input = sys.stdin.readline
sys.setrecursionlimit(20000000)
s = input()
s = s[::-1]
n = len(s)
dp = [0]*(n+1)
k = 1
a = defaultdict(int)
a[0]+=1
for i in range(n):
  dp[i] = (dp[i-1]+int(s[i])*k)%2019
  k*=10
  k%=2019
  a[dp[i]] += 1
ans = 0
for i in a.keys():
  ans += a[i]*(a[i]-1)/2
print(int(ans))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import deque
from collections import defaultdict
import math
import copy
#input = sys.stdin.readline
sys.setrecursionlimit(20000000)
s = input()
n = len(s)
dp = [[0]*2019 for i in range(n+1)]
for i in range(n+1):
    dp[i][0] = 1
for i in range(n):
    for j in range(2019):
        dp[i+1][(j*10+int(s[i]))%2019] += dp[i][j]
ans = 0
for i in range(n+1):
    ans += dp[i][0]
print(ans-n-1)


Fixed submission:
import sys
from collections import deque
from collections import defaultdict
import math
import copy
#input = sys.stdin.readline
sys.setrecursionlimit(20000000)
s = input()
s = s[::-1]
n = len(s)
dp = [0]*(n+1)
k = 1
a = defaultdict(int)
a[0]+=1
for i in range(n):
  dp[i] = (dp[i-1]+int(s[i])*k)%2019
  k*=10
  k%=2019
  a[dp[i]] += 1
ans = 0
for i in a.keys():
  ans += a[i]*(a[i]-1)/2
print(int(ans))
Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
dp = [float("inf")]*(1<<n)
dp[0]=0
kagi = []
for i in range(m):
    a,b = map(int,input().split())
    c = list(map(int,input().split()))
    kagi.append([a,b,c])
for j in range(m):
    for i in range(1<<n):
        if dp[i] == -1:
            continue
        tmp = i+0
        for k in kagi[j][2]:
            tmp = tmp | 1<<(k-1)
        dp[tmp] = min(dp[tmp],dp[i]+kagi[j][0])
print(dp[(1<<n)-1] if dp[(1<<n)-1] != float("inf") else -1)

Fixed submission:
n,m = map(int,input().split())
dp = [float("inf")]*(1<<n)
dp[0]=0
kagi = []
for i in range(m):
    a,b = map(int,input().split())
    c = list(map(int,input().split()))
    kagi.append([a,b,c])
for j in range(m):
    for i in range(1<<n):
        tmp = i+0
        for k in kagi[j][2]:
            tmp = tmp | 1<<(k-1)
        dp[tmp] = min(dp[tmp],dp[i]+kagi[j][0])
print(dp[(1<<n)-1] if dp[(1<<n)-1] != float("inf") else -1)

Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
dp = [float("inf")]*(1<<n)
dp[0]=0
kagi = []
for i in range(m):
    a,b = map(int,input().split())
    c = list(map(int,input().split()))
    kagi.append([a,b,c])
for j in range(m):
    for i in range(1<<n):
        tmp = i+0
        for k in kagi[j][2]:
            tmp = tmp | 1<<(k-1)
        dp[tmp] = min(dp[tmp],dp[i]+kagi[j][0])
print(dp[(1<<n)-1] if dp[(1<<n)-1] != float("inf") else -1)



Fixed submission:
n,m = map(int,input().split())
dp = [float("inf")]*(1<<n)
dp[0]=0
kagi = []
for i in range(m):
    a,b = map(int,input().split())
    c = list(map(int,input().split()))
    kagi.append([a,b,c])
for j in range(m):
    for i in range(1<<n):
        tmp = i+0
        for k in kagi[j][2]:
            tmp = tmp | 1<<(k-1)
        dp[tmp] = min(dp[tmp],dp[i]+kagi[j][0])
print(dp[(1<<n)-1] if dp[(1<<n)-1] != float("inf") else -1)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import deque
from collections import defaultdict
import math
sys.setrecursionlimit(20000000)
input = sys.stdin.readline
q = int(input())
tei = 0
x = []
nnn = []
for i in range(q):
        nnn.append(input().rstrip())
for i in range(q):
        nn = nnn[i]
        if int(nn[0]) == 1:
                _,a,b = map(int,nn.split())
                x.append(a)
                tei += b
        else:
                x.sort()
                t = x[(len(x)-1)//2]
                ans = 0
                for j in x:
                        ans += abs(t-j)
                print(t,tei+ans)

Fixed submission:
import sys
from collections import deque
from collections import defaultdict
import math
sys.setrecursionlimit(20000000)
input = sys.stdin.readline
q = int(input())
tei = 0
x = []
nnn = []
import bisect
def press(a):
    n = len(a)
    b = set(a)
    b = list(b)
    b.sort()
    c = [0] * n
    for i in range(n):
        c[i] = bisect.bisect_left(b,a[i])
    return b,c
for i in range(q):
        nnn.append(input().rstrip())
for i in range(q):
        nn = nnn[i]
        if int(nn[0]) == 1:
                _,a,b = map(int,nn.split())
                x.append(a)
x.sort()
mo,za = press(x)
mo = x
d = dict()
for i in range(len(x)):
        d[mo[i]] = za[i]+1
dg = dict()
for i in range(len(x)):
        dg[za[i]+1] = mo[i]
class BIT:
    def __init__(self, node_size):
        self._node = node_size+1
        self.bit = [0]*self._node

    def add(self, index, add_val):
        while index < self._node:
            self.bit[index] += add_val
            index += index & -index

    def sum(self, index):
        res = 0
        while index > 0:
            res += self.bit[index]
            index -= index & -index
        return res
bit1 = BIT(len(x))
bit2 = BIT(len(x))
now = -1
def cheak(x,y):
  o = bit2.sum(x)
  if y >= o:
    return 0
  else:
    return 1
for i in range(q):
        nn = nnn[i]
        if int(nn[0]) == 1:
                _,a,b = map(int,nn.split())
                tei += b
                bit1.add(d[a],a)
                bit2.add(d[a],1)
                now += 1
        else:
                dai = len(x)
                syo = 1
                while True:
                        if dai-syo <= 1:
                                if cheak(syo,now//2) == 1:
                                        dai = syo
                                break
                        kon = (dai+syo)//2
                        c = cheak(kon,now//2)
                        if c == 1:
                                dai = kon
                        else:
                                syo = kon
                ans = 0
                ans -= bit1.sum(dai) - dg[dai]*bit2.sum(dai)
                ans += bit1.sum(len(x)) -bit1.sum(dai) -((bit2.sum(len(x))-bit2.sum(dai))*dg[dai])
                print(dg[dai],tei+ans)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
import random
import itertools
sys.setrecursionlimit(20000000)
#input = sys.stdin.readline
h, w = map(int,input().split())
a = [[j for j in input()] for i in range(h)]
def osen(x,y):
    global kuro2
    if x >0 and a[x-1][y] == ".":
        kuro2.append([x-1,y])
        a[x-1][y] = "#"
    if x < h-1 and a[x+1][y] == ".":
        kuro2.append([x+1,y])
        a[x+1][y] = "#"       

    if y >0 and a[x][y-1] == ".":
        kuro2.append([x,y-1])
        a[x][y-1] = "#"
    if y < w-1 and a[x][y+1] == ".":
        kuro2.append([x,y+1])
        a[x][y+1] = "#"      



kuro = []
for i in range(h):
    for j in range(w):
        if a[i][j] == "#":
            kuro.append([i,j])
ans = 0
while kuro:
    kuro2 = []
    while kuro:
        x,y = kuro.pop()
        osen(x,y)
    kuro = kuro2
    ans += 1

print(ans-1)

Fixed submission:
import sys
import random
import itertools
sys.setrecursionlimit(20000000)
#input = sys.stdin.readline
h, w = map(int,input().split())
a = [[j for j in input()] for i in range(h)]
def osen(x,y):
    global kuro2
    if x >0 and a[x-1][y] == ".":
        kuro2.append([x-1,y])
        a[x-1][y] = "#"
    if x < h-1 and a[x+1][y] == ".":
        kuro2.append([x+1,y])
        a[x+1][y] = "#"       

    if y >0 and a[x][y-1] == ".":
        kuro2.append([x,y-1])
        a[x][y-1] = "#"
    if y < w-1 and a[x][y+1] == ".":
        kuro2.append([x,y+1])
        a[x][y+1] = "#"      



kuro = []
for i in range(h):
    for j in range(w):
        if a[i][j] == "#":
            kuro.append([i,j])
ans = 0
while kuro:
    kuro2 = []
    while kuro:
        x,y = kuro.pop()
        osen(x,y)
    kuro = kuro2
    ans += 1

print(ans-1)
Buggy python submission (verdict: Runtime Error):
n = int(input())
a = list(map(int,input().split()))
mi = 0
for i in range(a):
  if a[i] < 0:
    mi+=1
  a[i]= abs(a[i])
if mi %2 == 1:
  print(sum(a)-min(a))
else:
  print(sum(a))

Fixed submission:
n = int(input())
a = list(map(int,input().split()))
mi = 0
for i in range(n):
  if a[i] < 0:
    mi+=1
  a[i]= abs(a[i])
if mi %2 == 1:
  print(sum(a)-2*min(a))
else:
  print(sum(a))
Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(20000000)
input = sys.stdin.readline
mod = 998244353
n = int(input())
a = [int(input()) for i in range(n)]
d = max(a)
dp = [[[0]*(d*n+1) for i in range(d*n+1)] for j in range(n+1)]
dp[0][0][0] = 1
for i in range(n):
    for j in range(d*n+1-d):
        for k in range(d*n+1-d):
            dp[i+1][j+a[i]][k] += dp[i][j][k]
            dp[i+1][j][k+a[i]] += dp[i][j][k]
            dp[i+1][j][k] += dp[i][j][k]
            dp[i+1][j+a[i]][k] %= mod
            dp[i+1][j][k+a[i]] %= mod
            dp[i+1][j][k] %= mod
go = sum(a)
ans = 0
for j in range(1,d*n+1):
    for k in range(1,d*n+1):
        if dp[n][j][k]:
            side1 = j
            side2 = k
            side3 = go-j-k
            if side3 == 0:
                continue
            if(side1+side2>side3)and(side2+side3>side1)and(side3+side1>side2):
                ans += dp[n][j][k]
                ans %= mod
print(ans)

Fixed submission:
import sys
sys.setrecursionlimit(20000000)
input = sys.stdin.readline
mod = 998244353
n = int(input())
a = [int(input()) for i in range(n)]
d = max(a)
go = sum(a)
dp = [[0]*(go+d+1) for j in range(n+1)]
dp[0][0] = 1
dp2 = [[0]*(go+d+1) for j in range(n+1)]
dp2[0][0] = 1
for i in range(n):
    for j in range(go):
            dp[i+1][j+a[i]] += dp[i][j]
            dp[i+1][j] += dp[i][j]*2
            dp[i+1][j+a[i]] %= mod
            dp[i+1][j] %= mod

            dp2[i+1][j+a[i]] += dp2[i][j]
            dp2[i+1][j] += dp2[i][j]
            dp2[i+1][j+a[i]] %= mod
            dp2[i+1][j] %= mod

zen = 3**n
zen %= mod
if go %2 == 1:
    print((zen - sum(dp[n][go//2+1:])*3)%mod)
else:
    print((zen - sum(dp[n][go//2:])*3 + dp2[n][go//2]*3)%mod)
Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(20000000)
input = sys.stdin.readline
n,x = map(int,input().split())
s = list(map(int,input().split()))
mod = 10**9 + 7
s.sort()
s = s[::-1]
c = max(max(s),x)
dp = [[0]*(c+1) for i in range(n+1)]
dp[0][x] = 1
for i in range(n):
    for j in range(c+1):
        dp[i+1][j%s[i]] += dp[i][j]
        dp[i+1][j%s[i]] %= mod
        dp[i+1][j] += dp[i][j]*(n-i-1)
        dp[i+1][j] %= mod
ans = 0
for i in range(c+1):
        ans += dp[n][i]*i
        ans %= mod
print(ans)
if n <= 10000:
  print(9)

Fixed submission:
import sys
sys.setrecursionlimit(20000000)
input = sys.stdin.readline
n,q = map(int,input().split())
s = input().strip()
a = [list(input().split()) for i in range(q)]
def cheak(x):
  for i in range(q):
      if s[x] == a[i][0]:
          if a[i][1] == "L":
              x-=1
          else:
              x+=1
      if x >= n :
          return 0
      elif x < 0:
          return 1
  return 0
def cheak2(x):
  for i in range(q):
      if s[x] == a[i][0]:
          if a[i][1] == "L":
              x-=1
          else:
              x+=1
      if x >= n :
          return 1
      elif x < 0:
          return 0
  return 0
dai = n-1
syo = 0
hi = cheak(syo)
mi = cheak2(dai)
if hi== 0 and mi==0:
    print(n)
elif hi and mi:
  while True:
    if dai-syo <= 1:
        if cheak(dai) != 1:
            dai = syo
        break
    kon = (dai+syo)//2
    c = cheak(kon)
    if c == 0:
        dai = kon
    else:
        syo = kon
  ab = dai
  dai = n-1
  syo = 0
  while True:
    if dai-syo <= 1:
        if cheak2(syo) == 1:
            dai = syo
        break
    kon = (dai+syo)//2
    c = cheak2(kon)
    if c == 1:
        dai = kon
    else:
        syo = kon
  if dai <= ab:
      print(0)
      exit()
  print(n-(ab+1+n-dai))
elif hi==1 and mi == 0:
  while True:
    if dai-syo <= 1:
        if cheak(dai) != 1:
            dai = syo
        break
    kon = (dai+syo)//2
    c = cheak(kon)
    if c == 0:
        dai = kon
    else:
        syo = kon
  print(n-1-dai)
else:
  while True:
    if dai-syo <= 1:
        if cheak2(syo) == 1:
            dai = syo
        break
    kon = (dai+syo)//2
    c = cheak2(kon)
    if c == 1:
        dai = kon
    else:
        syo = kon
  print(dai)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(20000000)
input = sys.stdin.readline
n = int(input())
a = list(map(int,input().split()))
for i in range(n):
    if i+1 < a[i]:
        print(-1)
        exit()
 
def dfs(x,ans,so):
        if x == n:
                if ans == a:
                        so = so[::-1]
                        for i in so:
                                print(i)
                        exit()
                return 0
        for i in range(len(ans)):
            if ans[i] == i+1:
                ans.pop(i)
                so.append(i+1)
                dfs(x+1,ans,so)
                ans.insert(i,i+1)
                so.pop()
        return -1
print(dfs(0,a,[]))      

Fixed submission:
import sys
sys.setrecursionlimit(20000000)
input = sys.stdin.readline
n = int(input())
a = list(map(int,input().split()))
for i in range(n):
    if i+1 < a[i]:
        print(-1)
        exit()
 
def dfs(x,ans,so):
        if x == n:
                if ans == a:
                        so = so[::-1]
                        for i in so:
                                print(i)
                        exit()
                return 0
        for i in range(len(ans)-1,-1,-1):
            if ans[i] == i+1:
                ans.pop(i)
                so.append(i+1)
                dfs(x+1,ans,so)
                ans.insert(i,i+1)
                so.pop()
        return -1
print(dfs(0,a,[]))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(20000000)
input = sys.stdin.readline
n = int(input())
a = list(map(int,input().split()))
for i in range(n):
    if i+1 < a[i]:
        print(-1)
        exit()

def dfs(x,ans,so):
        if x == n:
                if ans == a:
                        for i in so:
                                print(i)
                        exit()
                return 0
        for i in range(1,x+2):
                ans.insert(i-1,i)
                so.append(i)
                dfs(x+1,ans,so)
                ans.pop(i-1)
                so.pop()
        return -1
print(dfs(0,[],[]))      

Fixed submission:
import sys
sys.setrecursionlimit(20000000)
input = sys.stdin.readline
n = int(input())
a = list(map(int,input().split()))
for i in range(n):
    if i+1 < a[i]:
        print(-1)
        exit()
 
def dfs(x,ans,so):
        if x == n:
                if ans == a:
                        so = so[::-1]
                        for i in so:
                                print(i)
                        exit()
                return 0
        for i in range(len(ans)-1,-1,-1):
            if ans[i] == i+1:
                ans.pop(i)
                so.append(i+1)
                dfs(x+1,ans,so)
                ans.insert(i,i+1)
                so.pop()
        return -1
print(dfs(0,a,[]))
Buggy python submission (verdict: Runtime Error):
import numpy as np
n,m = map(int,input().split())
if b%a == 0:
    print(b+a)
else:
    print(b-a)

Fixed submission:
import numpy as np
a,b = map(int,input().split())
if b%a == 0:
    print(b+a)
else:
    print(b-a)
Buggy python submission (verdict: Runtime Error):
from collections import deque
import sys
sys.setrecursionlimit(20000)
#input = sys.stdin.readline
h,w = map(int,input().split())
a = [[j for j in input()] for i in range(h)]
used = [[0]*w for i in range(h)]
def dfs(x,y):
    if used[x][y]:
        return 0
    global go
    used[x][y] = 1
    if x-1 >=0 and a[x-1][y] != a[x][y]:
            go.add((x-1,y))
            dfs(x-1,y)
    if y-1 >=0 and a[x][y-1] != a[x][y]:
            go.add((x,y-1))
            dfs(x,y-1)
    if x+1 <h and a[x+1][y] != a[x][y]:
            go.add((x+1,y))      
            dfs(x+1,y)
    if y+1 <w and a[x][y+1] != a[x][y]:
            go.add((x,y+1))
            dfs(x,y+1)
ans = 0
for i in range(h):
    for j in range(w):
        go = set()
        if used[i][j]:
            continue
        dfs(i,j)
        kuro = 0
        siro = 0
        for k,l in go:
            if a[k][l] == "#":
                kuro += 1
            else:
                siro += 1
        ans += kuro*siro

print(ans)

Fixed submission:
from collections import deque
import sys
sys.setrecursionlimit(200000)
#input = sys.stdin.readline
h,w = map(int,input().split())
a = [[j for j in input()] for i in range(h)]
used = [[0]*w for i in range(h)]
def dfs(x,y):
    if used[x][y]:
        return 0
    global go
    used[x][y] = 1
    if x-1 >=0 and a[x-1][y] != a[x][y]:
            go.add((x-1,y))
            dfs(x-1,y)
    if y-1 >=0 and a[x][y-1] != a[x][y]:
            go.add((x,y-1))
            dfs(x,y-1)
    if x+1 <h and a[x+1][y] != a[x][y]:
            go.add((x+1,y))      
            dfs(x+1,y)
    if y+1 <w and a[x][y+1] != a[x][y]:
            go.add((x,y+1))
            dfs(x,y+1)
ans = 0
for i in range(h):
    for j in range(w):
        go = set()
        if used[i][j]:
            continue
        dfs(i,j)
        kuro = 0
        siro = 0
        for k,l in go:
            if a[k][l] == "#":
                kuro += 1
            else:
                siro += 1
        ans += kuro*siro

print(ans)
Buggy python submission (verdict: Runtime Error):
n,k = map(int,input().split())
h = list(map(int,input().split()))
dp = [float("inf")]*n
dp[0] = 0
for i in range(n):
  for j in range(1,k+1):
    if i+j < n:
      dp[i+j] = min(dp[i]+abs(h[i+j]-h[i]),dp[i+j])
print(dp[-1])

Fixed submission:
n = int(input())
h = list(map(int,input().split()))
dp = [float("inf")]*n
dp[0] = 0
for i in range(n):
  if i+1 < n:
    dp[i+1] = min(dp[i]+abs(h[i+1]-h[i]),dp[i+1])
  if i+2 < n:
    dp[i+2] = min(dp[i]+abs(h[i+2]-h[i]),dp[i+2])
print(dp[-1])
Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
 
N,W = map(int, input().split())
WV = []
for i in range(N):
    WV.append(list(map(int, input().split())))
 
dp = defaultdict(int)
 
dp[0] = 0
 
for w,v in WV:
    for nw, nv in list(dp.items()):
        if nw+w <= W:
            dp[nw+w] = max(dp[nw+w], nv+v)
            
print(max(dp.values()))

Fixed submission:
from collections import defaultdict
 
N,W = map(int, input().split())
WV = []
for i in range(N):
    WV.append(list(map(int, input().split())))
 
dp = defaultdict(int)
 
dp[0] = 0
 
for w,v in WV:
    for nw, nv in list(dp.items()):
        if nw+w <= W:
            dp[nw+w] = max(dp[nw+w], nv+v)
            
print(max(dp.values()))