Harryxun/testdata huggingface dataset:
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:
import numpy as np
N,W = map(int,input().split())
l = []
for i in range(N):
  y,x =  map(int,input().split())
  l.append((x,y))
V = sum([x[0] for x in l])
dp = np.full((N+1,V+1), np.inf)
dp[0][0]=0
for i in range(N):
  v = l[i][0]
  w = l[i][1]
  dp[i+1]=dp[i]
  dp[i+1][v:] = np.minimum(dp[i][v:],dp[i][:V-v+1]+w)
i = V
while True:
    if dp[N][i] <= W:
      print(i)
      break
    i -= 1
Buggy python submission (verdict: Runtime Error):
n = int(input())
a = []
for i in range(n):
  x,y,z = map(int,input().split())
  a.append([x+y,x,y,z])
dp = [-1] * 2001
a.sort()
dp[0] = 0
for i in range(n):
  for j in range(2000,-1,-1):
    if dp[j] != -1 and a[i][2]>= j:
      dp[j+a[i][1]] = max(dp[j+a[i][1]],dp[j]+a[i][3])
print(max(dp)) 

Fixed submission:
n = int(input())
a = []
for i in range(n):
  x,y,z = map(int,input().split())
  a.append([x+y,x,y,z])
dp = [-1] * 20001
a.sort()
dp[0] = 0
for i in range(n):
  for j in range(20000,-1,-1):
    if dp[j] != -1 and a[i][2]>= j:
      dp[j+a[i][1]] = max(dp[j+a[i][1]],dp[j]+a[i][3])
print(max(dp))
Buggy python submission (verdict: Runtime Error):
n = int(input())
a = [int(input()) for i in range(n)]
for i in a:
  if i == 1:
    print("first")
    exit()
xor = a[0]
for i in range(1,n):
  xor ^= a[i]
if xor %2 == 0:
  assert 100<xor and xor < 1000 
  print("second")
else:
  print("first")


Fixed submission:
n,p = map(int,input().split())
import math
def factorization(n):
    def factor_sub(n, m):
        c = 0
        while n % m == 0:
            c += 1
            n //= m
        return c, n
    #
    buff = []
    c, m = factor_sub(n, 2)
    if c > 0: buff.append((2, c))
    c, m = factor_sub(m, 3)
    if c > 0: buff.append((3, c))
    x = 5
    while m >= x * x:
        c, m = factor_sub(m, x)
        if c > 0: buff.append((x, c))
        if x % 6 == 5:
            x += 2
        else:
            x += 4
    if m > 1: buff.append((m, 1))
    return buff
so = factorization(p)
ans = 1
for i,j in so:
    ans *= i**(j//n)
print(ans)

Buggy python submission (verdict: Runtime Error):
n = int(input())
a = [int(input()) for i in range(n)]
for i in a:
  if i == 1:
    print("first")
    exit()
xor = a[0]
for i in range(1,n):
  xor ^= a[i]
if xor %2 == 0:
  assert n == 10**5 and xor == 0 and a[0] > 90000
  print("second")
else:
  print("first")

Fixed submission:
n,p = map(int,input().split())
import math
def factorization(n):
    def factor_sub(n, m):
        c = 0
        while n % m == 0:
            c += 1
            n //= m
        return c, n
    #
    buff = []
    c, m = factor_sub(n, 2)
    if c > 0: buff.append((2, c))
    c, m = factor_sub(m, 3)
    if c > 0: buff.append((3, c))
    x = 5
    while m >= x * x:
        c, m = factor_sub(m, x)
        if c > 0: buff.append((x, c))
        if x % 6 == 5:
            x += 2
        else:
            x += 4
    if m > 1: buff.append((m, 1))
    return buff
so = factorization(p)
ans = 1
for i,j in so:
    ans *= i**(j//n)
print(ans)

Buggy python submission (verdict: Runtime Error):
n = int(input())
a = [int(input()) for i in range(n)]
for i in a:
  if i == 1:
    print("first")
    exit()
xor = a[0]
for i in range(1,n):
  xor ^= a[i]
if xor %2 == 0:
  assert n == 10**5 and xor == 0 and a[0] > 50000
  print("second")
else:
  print("first")



Fixed submission:
n,p = map(int,input().split())
import math
def factorization(n):
    def factor_sub(n, m):
        c = 0
        while n % m == 0:
            c += 1
            n //= m
        return c, n
    #
    buff = []
    c, m = factor_sub(n, 2)
    if c > 0: buff.append((2, c))
    c, m = factor_sub(m, 3)
    if c > 0: buff.append((3, c))
    x = 5
    while m >= x * x:
        c, m = factor_sub(m, x)
        if c > 0: buff.append((x, c))
        if x % 6 == 5:
            x += 2
        else:
            x += 4
    if m > 1: buff.append((m, 1))
    return buff
so = factorization(p)
ans = 1
for i,j in so:
    ans *= i**(j//n)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
n = input()
a = [i for i in n]
ans = 0
n = len(n)
for j in range(20000):
    flag = 1
    for i in range(n-2,-1,-1):
        if a[i+1] == "W" and a[i] == "B":
            a[i] = "W"
            a[i+1] = "B"
            ans += 1
            flag = 0
            x = i+1
            while x<n-1:
                if a[x+1] == "W" and a[x] == "B":
                    a[x] = "W"
                    a[x+1] = "B"
                    ans += 1
                    x+=1
                else:
                    break
    if flag ==1:
        break
print(ans)

Fixed submission:
n = input()
a = [i for i in n]
ans = 0
n = len(n)
B=0
W = 0
for i in range(n):
    if a[i] == "W":
        ans += i-W
        W+=1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = input()
a = [i for i in n]
ans = 0
for j in range(20000):
    flag = 1
    for i in range(len(n)-2,-1,-1):
        if a[i+1] == "W" and a[i] == "B":
            a[i] = "W"
            a[i+1] = "B"
            ans += 1
            flag = 0
    if flag ==1:
        break
print(ans)

Fixed submission:
n = input()
a = [i for i in n]
ans = 0
n = len(n)
B=0
W = 0
for i in range(n):
    if a[i] == "W":
        ans += i-W
        W+=1
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
n = int(input())
a = list(map(int,input().split()))
a.sort(key=lambda x:-x)
d = defaultdict(int)
for i in a:
    d[i] += 1
beki = [2**i for i in range(60,0,-1)]
ans = 0
for i in a:
    if d[i] == 0:
        continue
    for j in beki:
        if j <= i:
            break
        ni = j - i
        if ni == i:
            if d[i] >= 2:
                ans +=1
                d[i]-=2
                break
        elif d[ni] > 0:
            ans += 1
            d[ni]-=1
            d[i]-=1
            break
print(ans)

Fixed submission:
from collections import defaultdict
n = int(input())
a = list(map(int,input().split()))
a.sort(key=lambda x:-x)
d = defaultdict(int)
for i in a:
    d[i] += 1
beki = [2**i for i in range(32,0,-1)]
ans = 0
for i in a:
    if d[i] == 0:
        continue
    for j in beki:
        if j <= i:
            break
        ni = j - i
        if ni == i:
            if d[i] >= 2:
                ans +=1
                d[i]-=2
                break
        elif d[ni] > 0:
            ans += 1
            d[ni]-=1
            d[i]-=1
            break
print(ans)
Buggy python submission (verdict: Runtime Error):
import math
n,m = map(int,input().split())
MAX = m//n
ans = 1
def factorization(n):
    def factor_sub(n, m):
        c = 0
        while n % m == 0:
            c += 1
            n /= m
        return c, n
    #
    buff = []
    c, m = factor_sub(n, 2)
    if c > 0: buff.append((2, c))
    c, m = factor_sub(m, 3)
    if c > 0: buff.append((3, c))
    x = 5
    while m >= x * x:
        c, m = factor_sub(m, x)
        if c > 0: buff.append((x, c))
        if x % 6 == 5:
            x += 2
        else:
            x += 4
    if m > 1: buff.append((m, 1))
    return buff
def divisor_sub(p, q):
    a = []
    for i in range(0, q + 1):
        a.append(p ** i)
    return a

def divisor(n):
    xs = factorization(n)
    ys = divisor_sub(xs[0][0], xs[0][1])
    for p, q in xs[1:]:
        ys = [x * y for x in divisor_sub(p, q) for y in ys]
    return sorted(ys)
a = divisor(m)
for i in a:
  if i <= MAX:
    ans = i
print(ans)

Fixed submission:
import math
n,m = map(int,input().split())
MAX = m//n
ans = 1
def factorization(n):
    def factor_sub(n, m):
        c = 0
        while n % m == 0:
            c += 1
            n /= m
        return c, n
    #
    buff = []
    c, m = factor_sub(n, 2)
    if c > 0: buff.append((2, c))
    c, m = factor_sub(m, 3)
    if c > 0: buff.append((3, c))
    x = 5
    while m >= x * x:
        c, m = factor_sub(m, x)
        if c > 0: buff.append((x, c))
        if x % 6 == 5:
            x += 2
        else:
            x += 4
    if m > 1: buff.append((m, 1))
    return buff
def divisor_sub(p, q):
    a = []
    for i in range(0, q + 1):
        a.append(p ** i)
    return a

def divisor(n):
    if n == 1:
      return([1])
    xs = factorization(n)
    ys = divisor_sub(xs[0][0], xs[0][1])
    for p, q in xs[1:]:
        ys = [x * y for x in divisor_sub(p, q) for y in ys]
    return sorted(ys)
a = divisor(m)
for i in a:
  if i <= MAX:
    ans = i
print(int(ans))
Buggy python submission (verdict: Runtime Error):
n,m,x,y = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
aMAX = max(a)
bMIN = min(b)
if (a<b and a+1>x and a+1<=y):
  print("No War")
else:
  print("War")

Fixed submission:
n,m,x,y = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a = max(a)
b = min(b)
if (a<b and a+1>x and a+1<=y):
  print("No War")
else:
  print("War")
Buggy python submission (verdict: Time Limit Exceeded):
import math
import sys
n,x= map(int,input().split())
a = list(map(int, sys.stdin.readline().split()))
ru = [0]*(n+1)
for i in range(1,n+1):
  ru[i] = ru[i-1]+a[i-1]
ans = float("inf")
for i in range(1,n+1):
  tmp = x*i
  l = math.ceil(n/i)
  for j in range(1,l+1):
    if ans <= tmp:
      break
    if j == 1:
        m = -i*(j+1)-1
        if m < -n:
          m = -n-1
        tmp += (ru[n]-ru[m])*5
    else:
        m = -i*(j+1)-1
        if m < -n:
          m = -n-1
        if -i*j-1 < -n:
          break
        tmp += (ru[-i*j-1]-ru[m])*(2*j+3)
  ans = min(ans,tmp)
print(ans+n*x)

Fixed submission:
import math
n,x= map(int,input().split())
a = [int(_) for _ in input().split()]
ru = [0]*(n+1)
for i in range(1,n+1):
  ru[i] = ru[i-1]+a[i-1]
ans = float("inf")
for i in range(1,n+1):
  tmp = 0
  l = math.ceil(n/i)
  for j in range(1,l+1):
    if j == 1:
        m = -i*(j+1)-1
        if m < -n:
          m = -n-1
        tmp += (ru[n]-ru[m])*5
    else:
        m = -i*(j+1)-1
        if m < -n:
          m = -n-1
        if -i*j-1 < -n:
          break
        tmp += (ru[-i*j-1]-ru[m])*(2*j+3)
  tmp += x*i
  ans = min(ans,tmp)
print(ans+n*x)

Buggy python submission (verdict: Time Limit Exceeded):
import math
n,x= map(int,input().split())
a = [int(_) for _ in input().split()]
ru = [0]*(n+1)
for i in range(1,n+1):
  ru[i] = ru[i-1]+a[i-1]
ans = float("inf")
for i in range(1,n+1):
  tmp = 0
  l = math.ceil(n/i)
  for j in range(1,l+1):
    if j == 1:
        m = -i*(j+1)-1
        if m < -n:
          m = -n-1
        tmp += (ru[n]-ru[m])*5
    else:
        m = -i*(j+1)-1
        if m < -n:
          m = -n-1
        if -i*j-1 < -n:
          break
        tmp += (ru[-i*j-1]-ru[m])*(2*j+3)
  tmp += x*i
  ans = min(ans,tmp)
print(ans+n*x)

Fixed submission:
import math
n,x= map(int,input().split())
a = [int(_) for _ in input().split()]
ru = [0]*(n+1)
for i in range(1,n+1):
  ru[i] = ru[i-1]+a[i-1]
ans = float("inf")
for i in range(1,n+1):
  tmp = 0
  l = math.ceil(n/i)
  for j in range(1,l+1):
    if j == 1:
        m = -i*(j+1)-1
        if m < -n:
          m = -n-1
        tmp += (ru[n]-ru[m])*5
    else:
        m = -i*(j+1)-1
        if m < -n:
          m = -n-1
        if -i*j-1 < -n:
          break
        tmp += (ru[-i*j-1]-ru[m])*(2*j+3)
  tmp += x*i
  ans = min(ans,tmp)
print(ans+n*x)
Buggy python submission (verdict: Runtime Error):
import collections
n = int(input())
a = list(map(int,input().split()))
mae = []
usi = []
for i in range(0,n,2):
  mae.append(a[i])
for i in range(1,n,2):
  usi.append(a[i])
mae = collections.Counter(mae)
usi = collections.Counter(usi)
mae = mae.most_common()
usi = usi.most_common()
if mae[0][0] != usi[0][0]:
  print(n-mae[0][1]-usi[0][1])
else:
  try:
    t = mae[0][1] + usi[1][1]
  except:
    t = 0
  try:
    s = mae[1][1] + usi[0][1]
  except:
    s = 0
  u = mae[0][1]
  print(n-max(t,s,u))

Fixed submission:
import sys
n,k = map(int, sys.stdin.readline().split())
ans = 0
a = n//k
ans += a*(a-1)*(a-2)+a*(a-1)*3+a
if k %2 == 0:
    a = n//k
    if n %k >= k/2:
        a+=1
    ans += a*(a-1)*(a-2)+a*(a-1)*3+a
print(ans)
Buggy python submission (verdict: Runtime Error):
n = int(input())
x = []
for i in range(n):
  x1,y1=[int(i) for i in input().split()]
  x.append([x1,y1])
ki = (abs(x[0][0]) + abs(x[0][1])) %2
for i in range(n):
  if ki != (abs(x[i][0]) + abs(x[i][1]))%2:
    print(-1)
    exit()
if ki == 1:
  m = 19
  d = [1]*19
else:
  m = 20
  d = [1]*20
ans = []
for i in range(n):
  tmp = ""
  if x[i][0] > 0:
    tmp += "R"*x[i][0]
  else:
    tmp += "L"*abs(x[i][0])
  if x[i][1] > 0:
    tmp += "U"*x[i][1]
  else:
    tmp += "D"*abs(x[i][1])
  while len(tmp) < m:
    tmp += "RL"
  ans.append(tmp)
print(m)
print(*d)
for i in ans:
  print(i)

Fixed submission:
k=int(input())
g = []
i = 0
while k >3:
  if k%2 == 1:
    g.append([1,20-i,(k-1)*2**i])
    k = k//2
    g.append([20-i-1,20-i,0])
    g.append([20-i-1,20-i,1*2**i])
    i+=1
  else:
    k = k//2
    g.append([20-i-1,20-i,0])
    g.append([20-i-1,20-i,1*2**i])
    i+=1
if k==3:
  g.append([1,20-i,(k-1)*2**i])
  k = k//2
  g.append([20-i-1,20-i,0])
  g.append([20-i-1,20-i,1*2**i])
  if 20-i-1 != 1:
    g.append([1,20-i-1,0])
elif k == 2:
  g.append([20-i-1,20-i,0])
  g.append([20-i-1,20-i,1*2**i])
  if 20-i-1 != 1:
    g.append([1,20-i-1,0])
print(20,len(g))
for x,y,z in g:
  print(x,y,z)
Buggy python submission (verdict: Runtime Error):
import sys
n,k = map(int, sys.stdin.readline().split())
a = list(map(int, sys.stdin.readline().split()))
ma = []
pu = []
for i in range(n):
    if a[i] < 0:
        ma.append(a[i])
    else:
        pu.append(a[i])
for i in range(len(ma)):
    ma[i] = -ma[i]
ma.sort()
ans = float("inf")
for i in range(len(ma)):
    kota = 0
    kota += ma[i]
    if k <= i+1:
        ans = min(kota,ans)
    else:
        kota += ma[i]
        if (len(pu) >= k- i-2 and len(pu) != 0):
            kota += pu[k-i-2]
            ans = min(kota,ans)
for i in range(len(pu)):
    kota = 0
    kota += pu[i]
    if k <= i+1:
        ans = min(kota,ans)
    else:
        kota += pu[i]
        if (len(ma) >= k- i-2 and len(ma) != 0):
            kota += ma[k-i-2]
            ans = min(kota,ans)
print(ans)

Fixed submission:
import sys
n,k = map(int, sys.stdin.readline().split())
a = list(map(int, sys.stdin.readline().split()))
ma = []
pu = []
for i in range(n):
    if a[i] < 0:
        ma.append(a[i])
    else:
        pu.append(a[i])
for i in range(len(ma)):
    ma[i] = -ma[i]
ma.sort()
ans = float("inf")
for i in range(len(ma)):
    kota = 0
    kota += ma[i]
    if k <= i+1:
        ans = min(kota,ans)
    else:
        kota += ma[i]
        if len(pu) >= k- i-1:
            kota += pu[k-i-2]
            ans = min(kota,ans)
for i in range(len(pu)):
    kota = 0
    kota += pu[i]
    if k <= i+1:
        ans = min(kota,ans)
    else:
        kota += pu[i]
        if len(ma) >= k- i-1:
            kota += ma[k-i-2]
            ans = min(kota,ans)
print(ans)
Buggy python submission (verdict: Runtime Error):
s = input()
if s[0] == "A":
    x = s.find("C")
    if 2<=x and x != len(s)-1:
        s[0]="a"
        s[x]="c"
        if s.islower():
            print("AC")
            exit()
print("WA")

Fixed submission:
s = input()
e = 0
if s[0] == "A":
  for i in range(2,len(s)-1):
    b = s[i]
    if b == "C":
      e = 1
      f = i
      break
  if e == 1:
    s = s[:f] + s[f+1:]
    s = s[1:]
    if s.islower():
      print("AC")
    else:
      print("WA")
  else:
    print("WA")
else:
  print("WA")
Buggy python submission (verdict: Time Limit Exceeded):
import math
from itertools import permutations
d,g = map(int,input().split())
x = []
for i in range(d):
    x1,y1=[int(i) for i in input().split()]
    x.append([x1,y1])
zy = [i for i in range(1,d+1)]
zyu = list(permutations(zy, d))
ans2 = 100000000000000000000000
gou = g+0
for i in zyu:
  ans = 0
  g = gou +0
  while g > 0:
    for j in i:
      if g <= 0:
        break
      dai = x[-j][0]
      if g > (d-j+1)*100*dai:
        g-= ((d-j+1)*100*dai +x[-j][1])
        ans += dai
        continue
      else:
        ans += math.ceil(g/((d-j+1)*100))
        g = 0
      g -= x[-j][1]
  ans2 = min(ans,ans2)
print(int(ans2))

Fixed submission:
import math
from itertools import combinations
d,g = map(int,input().split())
x = []
for i in range(d):
    x1,y1=[int(i) for i in input().split()]
    x.append([x1,y1])
zy = [i for i in range(1,d+1)]
zyu = []
for i in range(d+1):
  zyu.extend(list(combinations(zy, i)))
ans2 = 100000000000000000000000
gou = g+0
for i in zyu:
  ans = 0
  g = gou +0
  for j in i:
    g -= (x[j-1][0] * j*100 + x[j-1][1])
    ans += x[j-1][0]
  if g >0:
    y = d+0
    while y in i:
      y -= 1
    a = x[y-1][0]
    if g > a*y*100:
      continue
    ans += math.ceil(g/(y*100))
  ans2 = min(ans,ans2)
print(ans2)
Buggy python submission (verdict: Time Limit Exceeded):
s = input()
mod = 1000000007
a = 0
mae = 0
usi = s.count("?")
c = s.count("C")
ans = 0
for i in range(len(s)):
  if s[i] == "B":
    ans += (a*c*(3**(usi+mae)))%mod+(mae*c*(3**(mae-1+usi)))%mod+(usi*a*(3**(mae-1+usi)))%mod+(usi*mae*(3**(mae-2+usi)))%mod
  if s[i] == "?":
    usi -= 1
    ans += (a*c*(3**(usi+mae)))%mod+(mae*c*(3**(mae-1+usi)))%mod+(usi*a*(3**(mae-1+usi)))%mod+(usi*mae*(3**(mae-2+usi)))%mod
    mae +=1
  if s[i] == "A":
    a +=1
  if s[i] == "C":
    c -=1
print(int(ans%mod))

Fixed submission:
def power_func(a,n,p):
  bi=str(format(n,"b"))#2進表現に
  res=1
  for i in range(len(bi)):
    res=(res*res) %p
    if bi[i]=="1":
      res=(res*a) %p
  return res
s = input()
mod = 1000000007
a = 0
mae = 0
usi = s.count("?")
c = s.count("C")
ans = 0
for i in range(len(s)):
  if s[i] == "B":
    ans += (a*c*(power_func(3,usi+mae,mod)))%mod+(mae*c*(power_func(3,usi+mae-1,mod)))%mod+(usi*a*(power_func(3,usi+mae-1,mod)))%mod+(usi*mae*(power_func(3,usi+mae-2,mod)))%mod
  if s[i] == "?":
    usi -= 1
    ans += (a*c*(power_func(3,usi+mae,mod)))%mod+(mae*c*(power_func(3,usi+mae-1,mod)))%mod+(usi*a*(power_func(3,usi+mae-1,mod)))%mod+(usi*mae*(power_func(3,usi+mae-2,mod)))%mod
    mae +=1
  if s[i] == "A":
    a +=1
  if s[i] == "C":
    c -=1
print(int(ans%mod))
Buggy python submission (verdict: Runtime Error):
N = int(input())
a =[1,2,3,4,5,6,7,8,9]
for i in range(1,10000):
  b = 10**i - 1
  for j in range(1,10):
    a.append(b + j * 10 ** i)
  
for i in range(N):
  print(a[i])

Fixed submission:
N,K = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
c = a.index(1)
naga = len(a)-K
if naga <= 0:
  print(1)
else:
  d = c-0
  if d != 0:
    V = K-1
    ans += d // V
    if d % V != 0:
      ans += 1
  e = N-1-c
  if e != 0:
      V = K-1
      ans += e // V
      if e % V != 0:
        ans += 1
  ans1 = 0
  ans2 = 0
  if d <= K-1:
    ans1 += 1
    ex = e -(K-d-1)
    if ex >= 0:
      V = K-1
      ans1 += ex // V
      if ex % V != 0:
        ans1 += 1
  else:
    ans1 = 999999999999999999999999999999
  if e <= K-1:
    ans2 += 1
    dx = d -(K-e-1)
    if dx >= 0:
      V = K-1
      ans2 += dx // V
      if dx % V != 0:
        ans2 += 1
  else:
    ans2 = 999999999999999999999999999999
  ans3 = 0
  ans4 = 999999999999999999
  V = K-1
  for i in range(K):
    ans3 = 0
    dx = d- i
    ex = e - (K - i - 1)
    ans3 += 1
    if dx > 0:
      if ex > 0:
        ans3 += ex//V
        if ex % V != 0:
          ans3 += 1
        ans3 += dx//V
        if dx % V != 0:
          ans3 += 1
        ans4 = min(ans4,ans3)
  print(min(ans,ans1,ans2,ans4))
Buggy python submission (verdict: Time Limit Exceeded):
import itertools
N,C = map(int,input().split())
D = [input().split() for l in range(C)]
c = [input().split() for l in range(N)]
F = list(range(C))
B = list(itertools.permutations(F, 3))
ans = 0
kotae = float("inf")
for e,f,g in B:
    for i in range(N):
        for j in range(N):
            if (i+j+2) %3 == 1:
                    ans += int(D[int(c[i][j])-1][e])
            if (i+j+2) %3 == 2:
                    ans += int(D[int(c[i][j])-1][f])
            if (i+j+2) %3 == 0:
                    ans += int(D[int(c[i][j])-1][g])
    kotae = min(ans,kotae)
    ans = 0
print(kotae)


Fixed submission:
import itertools
N,C = map(int,input().split())
D = [input().split() for l in range(C)]
c = [input().split() for l in range(N)]
F = list(range(C))
B = list(itertools.permutations(F, 3))
ans = 0
kotae = float("inf")
moto1 = [0] * C 
moto2 = [0] * C 
moto3 = [0] * C 
for i in range(N):
    for j in range(N):
        if (i+j+2) %3 == 1:
                moto1[int(c[i][j])-1] += 1
        if (i+j+2) %3 == 2:
                moto2[int(c[i][j])-1] += 1
        if (i+j+2) %3 == 0:
                moto3[int(c[i][j])-1] += 1
for e,f,g in B:
    for i in range(C):
        ans += moto1[i] * int(D[i][e])
        ans += moto2[i] * int(D[i][f])
        ans += moto3[i] * int(D[i][g])
    kotae = min(ans,kotae)
    ans = 0
print(kotae)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = []
for i in range(n):
  x,y = map(int,input().split())
  a.append([x,y])
ans = 0
ima = 0
for j in range(n):
  ban = -1
  to = 0
  for i in range(len(a)):
    if a[i][0] <= ima and a[i][1]>= ima:
      continue
    toa = min(abs(ima-a[i][0]),abs(ima-a[i][1]))
    if to < toa:
      ban = i
      to = toa
  if ban ==-1:
    print(ans+abs(ima))
    break
  ans += to
  if abs(ima-a[ban][0])>abs(ima-a[ban][1]):
    ima = a[ban][1]
  else:
    ima = a[ban][0]
  a.pop(ban)
else:
  print(ans+abs(ima))
  

Fixed submission:
n = int(input())
a = []
for i in range(n):
  x,y = map(int,input().split())
  a.append([x,y])
l = sorted(a,key=lambda x: -x[0])
r = sorted(a,key=lambda x: x[1])
left = 0
hi = 0
mi = 0
for i in range(n):
  hi += l[i][0]
  left = max(left,(hi-mi)*2)
  mi += r[i][1]
  left = max(left,(hi-mi)*2)
right = 0
hi = 0
mi = 0
for i in range(n):
  mi += r[i][1]
  right = max(right,(hi-mi)*2)
  hi += l[i][0]
  right = max(right,(hi-mi)*2)
print(max(right,left))
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = input()
tei = 10000000000000000000000000000
for i in range(1,N-1):
  a = S[:(i-1)]
  b = S[(i+1):]
  kae = a.count("W") + b.count("E")
  tei = min(tei,kae)
a = S[1:]
b = S[:(N-1)]
sai = a.count("E")
go = b.count("W")
print(min(tei,sai,go))

Fixed submission:
N = int(input())
S = input()
a = S[1:]
hazime = a.count("E")
tei = a.count("E")
for i in range(N-1):
  a = S[i]
  b = S[i+1]
  if a == "E" and b == "E":
    hazime += -1
    tei = min(tei,hazime)
  elif a == "E" and b == "W":
        continue
        tei = min(tei,hazime)
  elif a == "W" and b == "E":
     continue
     tei = min(tei,hazime)
  elif a == "W" and b == "W":
       hazime += 1
       tei = min(tei,hazime)
b = S[:N-1]
go = b.count("W")
tei = min(tei,go)
print(tei)
Buggy python submission (verdict: Runtime Error):
N = int(input())
a = [int(input()) for i in range(N)]
c = 0
dai = a.index(2)
for i in range (1,N):
  ti = a.index(i)
  dai = a.index(i+1)
  if  ti <  dai:
      c += 1
  else:
     break
d = 0
for  i  in range (N,1,-1):
  dai  =  a.index(i)
  ti =  a.index(i-1)
  if   ti < dai:
    d += 1
  else:
    break
jo =  max(c,d)
ans = N - jo - 1
print(ans)

Fixed submission:
n = int(input())
t = [0]*n
for i in range(n):
    p = int(input())-1
    t[p] = i
maxi = 1
c = 1
for i in range(n-1):
  a = t[i]
  b = t[1+i]
  if a < b:
    c += 1
  else:
    maxi = max(c,maxi)
    c = 1
maxi = max(c,maxi)
print(n - maxi)