Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
n = int(input())
A = list(map(int,input().split()))
B  =list(map(int,input().split()))
f = True
for i in range(n):
    if A[i]==B[i]:
        f = False
        break
if f:
    print('Yes')
    ans =[str(i) for i in B]
    print(' '.join(ans))
    exit()
if A[-1]<B[0]:
    print('Yes')
    ans =[str(i) for i in B]
    print(' '.join(ans))
    exit()

B=B[::-1]
f = True
for i in range(n-1,-1,-1):
    if A[i]!=B[i]:
        continue
    else:
        p = A[i]
        l = A.count(A[i])
        m = B.count(B[i])
        idx = i
        f = False
        break
l ,m=0,0
for i in range(idx+1):
    if A[i]==p:l+=1
    if B[i]==p:m+=1
if f:
    print('Yes')
    ans =[str(i) for i in B]
    print(' '.join(ans))
    exit()

if idx+1-m==0:
    if n-idx<=l:
        print('No')
        exit()
    else:
        print('Yes')
        tmp = B[:idx-l+1]+B[idx+1:] + B[idx-l+1:idx+1]
        ans =[str(i) for i in tmp]
        print(' '.join(ans))
        exit()

if idx+1-m>=l:
    print('Yes')
    tmp = B[idx+1-m:idx+1]+B[:idx+1-m]+B[idx+1:]
    ans =[str(i) for i in tmp]
    print(' '.join(ans))
    exit()
else:
    print('No')
    exit()


Fixed submission:
def get_ints():
      return list(map(int,input().split()))

n = int(input())
a = get_ints()
b = get_ints()

cnt = [0]*(n+1)
for x in a+b:
  cnt[x] += 1

q = [set() for i in range(n+1)]
for i in range(1,n+1):
  if cnt[i] > n:
    print('No')
    exit(0)
  q[cnt[i]].add(i)
print('Yes')

def make_cnt(a):
  cnt_ = {}
  for i in range(n):
    if a[i] not in cnt_:
      cnt_[a[i]] = [i]
    else:
      cnt_[a[i]].append(i)
  return cnt_

cnt_a = make_cnt(a)
cnt_b = make_cnt(b)

def get_any(cnt_, x):
  for key in cnt_.keys():
    if key != x:
      return key
  assert False

ans = [-1]*n

for i in range(n,0,-1):
  s = list(q[i])
  x,y = 0,0
  if len(s) == 0:
    x = get_any(cnt_a, -1)
    y = get_any(cnt_b, x)
  elif len(s) == 1:
    x = s[0]
    if x not in cnt_a:
      y = x
      x = get_any(cnt_a, y)
    else:
      y = get_any(cnt_b, x)
  else:
    x,y = s
    if x not in cnt_a:
      x,y = y,x

  ans[cnt_a[x][-1]] = y
  def update_cnt(cnt_, x):
    cnt_[x].pop()
    if len(cnt_[x]) == 0:
      del cnt_[x]
  def use(x):
    q[cnt[x]].remove(x)
    cnt[x] -= 1
    q[cnt[x]].add(x)
  update_cnt(cnt_a, x)
  update_cnt(cnt_b, y)
  use(x)
  use(y)

print(' '.join(map(str,ans)))
Buggy python submission (verdict: Runtime Error):
n = int(input())
x = input()
original_pop_count = x.count('1')
one_pop_count = original_pop_count-1
zero_pop_count = original_pop_count+1

one_mod = 0
zero_mod= 0
for b in x:
    one_mod=(one_mod*2+int(b))%one_pop_count
    zero_mod=(zero_mod*2+int(b))%zero_pop_count
#予めf[i]を計算する
f = [0]*220000
pop_count = [0]*220000
for i in range(1,220000):
    pop_count[i]= pop_count[i//2] + i%2
    f[i] = f[i%pop_count[i]]+1
one_pow2 = [1]*220000
zero_pow2 = [1]*220000
for i in range(1,220000):
    one_pow2[i] = one_pow2[i-1]*2%one_pop_count
    zero_pow2[i] = zero_pow2[i-1]*2%zero_pop_count


for i in range(n-1,-1,-1):
    if x[n-1-i]=='1':
        nxt = one_mod
        nxt-=one_pow2[i]
        nxt%=one_pop_count
        print(f[nxt]+1)
    if x[n-1-i]=='0':
        nxt = zero_mod
        nxt+=zero_pow2[i]
        nxt%=zero_pop_count
        print(f[nxt]+1)

Fixed submission:
n = int(input())
x = input()
original_pop_count = x.count('1')
one_pop_count = original_pop_count-1
zero_pop_count = original_pop_count+1

one_mod = 0
zero_mod= 0
for b in x:
    if one_pop_count!=0:
        one_mod=(one_mod*2+int(b))%one_pop_count
    zero_mod=(zero_mod*2+int(b))%zero_pop_count
#予めf[i]を計算する
f = [0]*220000
pop_count = [0]*220000
for i in range(1,220000):
    pop_count[i]= pop_count[i//2] + i%2
    f[i] = f[i%pop_count[i]]+1
one_pow2 = [1]*220000
zero_pow2 = [1]*220000
for i in range(1,220000):
    if one_pop_count!=0:
        one_pow2[i] = one_pow2[i-1]*2%one_pop_count
    zero_pow2[i] = zero_pow2[i-1]*2%zero_pop_count


for i in range(n-1,-1,-1):
    if x[n-1-i]=='1':
        if one_pop_count!=0:
            nxt = one_mod
            nxt-=one_pow2[i]
            nxt%=one_pop_count
            print(f[nxt]+1)
        else:
            print(0)
    if x[n-1-i]=='0':
        nxt = zero_mod
        nxt+=zero_pow2[i]
        nxt%=zero_pop_count
        print(f[nxt]+1)
Buggy python submission (verdict: Time Limit Exceeded):
import collections
N = int(input())
A = list(map(int,input().split()))
counter = collections.Counter(A)
B = list(set(A))
count = 0
for i in range(len(B)):
    if counter[B[i]]>1:
        continue
    if B[i]==1:
        count += 1
        continue
    else:
        check = 0
        for j in range(1,int(B[i]**0.5)+1):
            if (j != 1 and B[i]%j==0 and (j in counter or B[i]//j in counter)) or (j==1 and j in counter):
                check += 1
                break
        if check==0:
            count += 1
print(count)
    

Fixed submission:
import collections
N = int(input())
A = list(map(int,input().split()))
S = set()
D = set()
for a in A:
    if a in S:
        D.add(a)
    else:
        S.add(a)
M = max(S)
A = [0]*(M+1)
for s in S:
    if s in D:
        A[s] += 1
    for i in range(2*s,M+1,s):
        A[i]+=1
count = 0
for s in S:
    if A[s]==0:
        count += 1
print(count)
Buggy python submission (verdict: Time Limit Exceeded):
import heapq
H,W,K = map(int,input().split())
s1,e1,s2,e2 = map(int,input().split())
s1-=1
e1-=1
s2-=1
e2-=1
L = []
board = []
for _ in range(H):
    a = input()
    L.append(a)
for i in range(H):
    l = L[i]
    tmp = []
    for j in range(len(l)):
        tmp.append(l[j])
    board.append(tmp)

hq = []
heapq.heappush(hq,[0,s1,e1])
seen = [[False]*(W) for _ in range(H)]
destination = [(1,0),(-1,0),(0,1),(0,-1)]
while hq:
    cost,x,y = heapq.heappop(hq)
    seen[x][y] = True
    if x==s2 and y ==e2:
        print(cost);exit()
        break
    for dx,dy in destination:
        X,Y = x,y
        for i in range(K):
            X +=dx
            Y += dy
            if 0>X or X>=H or 0>Y or Y>=W: break
            else:
                if board[X][Y]=='@':
                    break
                elif not seen[X][Y]:
                    heapq.heappush(hq,[cost+1,X,Y])
                    seen[X][Y] = True

print(-1)

Fixed submission:
import collections
H,W,K = map(int,input().split())
s1,e1,s2,e2 = map(int,input().split())
s1-=1
e1-=1
s2-=1
e2-=1
L = []
board = []
for _ in range(H):
    a = input()
    L.append(a)
for i in range(H):
    l = L[i]
    tmp = []
    for j in range(len(l)):
        tmp.append(l[j])
    board.append(tmp)

queue = collections.deque()
queue.append([0,s1,e1])
seen = [[float('inf')]*(W) for _ in range(H)]
destination = [(1,0),(-1,0),(0,1),(0,-1)]
while queue:
    cost,x,y = queue.popleft()
    seen[x][y] = cost
    if x==s2 and y ==e2:
        print(cost);exit()
        break
    for dx,dy in destination:
        X,Y = x,y
        for i in range(1,K+1):
            NX = X +i*dx
            NY = Y + i*dy
            if 0>NX or NX>=H or 0>NY or NY>=W or seen[NX][NY] < seen[X][Y]+1: break
            else:
                if board[NX][NY]=='@':
                    break
                elif seen[NX][NY] ==float('inf'):
                    queue.append([cost+1,NX,NY])
                    seen[NX][NY] = cost+1

print(-1)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int,input().split()))
ans = 1
for i in range(N):
    ans *=A[i]
ans = -1 if ans >10**18 else ans
print(ans)

Fixed submission:
N = int(input())
A = list(map(int,input().split()))
ans = 1
b = 10**18
A.sort()
for i in range(N):
    ans *=A[i]
    if ans >b:
        print(-1);exit()
    if ans ==0:print(0);exit()
print(ans)
Buggy python submission (verdict: Runtime Error):
N,M,X = map(int,input().split())
A = []
cost = []
for i in range(N):
    a = list(map(int,input().split()))
    A.append(a[1:])
    cost.append(a[0])
ans = float('inf')
for i in range(2**N):
    I = bin(i)[2:]
    I = I.zfill(M)
    tmp = [0]*M
    buy = 0
    for j in range(len(I)):
        if I[j]=='1':
            for k in range(len(A[j])):
                tmp[k] += A[j][k]
            buy += cost[j]
    if all(tmp[i]>=X for i in range(len(tmp))):
        ans = min(ans,buy)
ans = -1 if ans ==float('inf') else ans
print(ans)

Fixed submission:
N,M,X = map(int,input().split())
A = []
cost = []
for i in range(N):
    a = list(map(int,input().split()))
    A.append(a[1:])
    cost.append(a[0])
ans = float('inf')
for i in range(2**N):
    I = bin(i)[2:]
    I = I.zfill(N)
    tmp = [0]*M
    buy = 0
    for j in range(len(I)):
        if I[j]=='1':
            for k in range(len(A[j])):
                tmp[k] += A[j][k]
            buy += cost[j]
    if all(tmp[i]>=X for i in range(len(tmp))):
        ans = min(ans,buy)
ans = -1 if ans ==float('inf') else ans
print(ans)
Buggy python submission (verdict: Runtime Error):
n,m,k = map(int,input().split())
f = [[] for _ in range(n)]
g = [[] for _ in range(n)]
for _ in range(m):
    a,b = map(int,input().split())
    a,b = a-1,b-1
    f[a].append(b)
    f[b].append(a)
for _ in range(k):
    c,d = map(int,input().split())
    c,d = c-1,d-1
    g[c].append(d)
    g[d].append(c)
d = {}
parent = [-1]*n
seen = [False]*n
from collections import deque
for i in range(n):
    if seen[i]: continue
    stack = deque()
    d[i] = set([i])
    stack = [i]
    while stack:
        v = stack.pop()
        seen[v] = True
        parent[v]=i
        for nv in f[v]:
            if seen[nv]: continue
            d[i].add(nv)
            stack.append(nv)
ans = [0]*n
for i in range(n):
    group = d[parent[i]]
    sum = len(d[i]) -len(f[i]) - len(set(g[i])&group) -1
    ans[i]=sum
print(*ans)

Fixed submission:
n,m,k = map(int,input().split())
f = [[] for _ in range(n)]
g = [[] for _ in range(n)]
for _ in range(m):
    a,b = map(int,input().split())
    a,b = a-1,b-1
    f[a].append(b)
    f[b].append(a)
for _ in range(k):
    c,d = map(int,input().split())
    c,d = c-1,d-1
    g[c].append(d)
    g[d].append(c)
d = {}
parent = [-1]*n
seen = [False]*n
from collections import deque
for i in range(n):
    if seen[i]: continue
    stack = deque()
    d[i] = set([i])
    stack = [i]
    while stack:
        v = stack.pop()
        parent[v] = i #同じ連結成分をまとめる
        seen[v] = True
        for nv in f[v]:
            if seen[nv]: continue
            d[i].add(nv)
            stack.append(nv)
ans = [0]*n
for i in range(n):
    group = d[parent[i]]
    #連結成分-直接の友達関係-ブロックしてるけど連結成分にいる人-自分
    sum = len(group)-len(f[i])-len(set(g[i])&group) -1
    ans[i] = sum
print(*ans)
Buggy python submission (verdict: Runtime Error):
mod = 10**9+7
n,a,b = map(int,input().split())
frac = [1]*(n+1)
finv = [1]*(n+1)
def combination(n, a):
    # nCk = n! / k! (n-k)! のうち、 n!/(n-k)! を計算する
    res = 1
    div = 1
    for i in range(a):
        res *= n-i
        res %= mod
        div *= a - i
        div %= mod
    res = (res * pow(div, mod-2, mod)) % mod
    return res
 
print((pow(2,n,mod)-1-combination(n,a)-combination(n,b))%mod)

Fixed submission:
mod = 10**9+7
n,a,b = map(int,input().split())
def combination(n, a):
    res = 1
    div = 1
    for i in range(a):
        res *= n-i
        res %= mod
        div *= a - i
        div %= mod
    res = (res * pow(div, mod-2, mod)) % mod
    return res
print((pow(2,n,mod)-1-combination(n,a)-combination(n,b))%mod)
Buggy python submission (verdict: Runtime Error):
n = int(input())
N = [int(i) for i in n]
dp = [[0,0] for _ in range(len(N))]

dp[0][0] = min(N[0],11-N[0])
dp[0][1] = min(N[0]+1,10-N[0])

for i in range(1,len(N)):
    dp[i + 1][0] = min(dp[i][0] + n, dp[i][1] + 10 - n)
    dp[i + 1][1] = min(dp[i][0] + n + 1, dp[i][1] + 10 - (n + 1))
print(dp[-1][0])

Fixed submission:
N = input()
dp = [[0] * 2 for _ in range(len(N) + 1)]
dp[0][1] = 1

for i in range(len(N)):
    n = int(N[i])
    dp[i + 1][0] = min(dp[i][0] + n, dp[i][1] + 10 - n)
    dp[i + 1][1] = min(dp[i][0] + n + 1, dp[i][1] + 10 - (n + 1))

print(dp[-1][0])
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
S = input()
cnt = 0
set_ = set()
for val in range(1000):
    val = str(val).zfill(3)
    a =val[0]
    b = val[1]
    c = val[2]
    flag = False
    for i in range(n):
        if S[i]==a and flag == False:
            for j in range(i+1,n):
                if S[j]==b and flag == False:
                    for k in range(j+1,n):
                        if S[k]==c and flag == False: 
                            cnt += 1
                            flag = True
print(cnt)

Fixed submission:
n = int(input())
S = input()
cnt = 0
set_ = set()
for val in range(1000):
    val = str(val).zfill(3)
    a =val[0]
    b = val[1]
    c = val[2]
    flag = False
    for i in range(n):
        if S[i]==a and flag == False:
            for j in range(i+1,n):
                if S[j]==b and flag == False:
                    for k in range(j+1,n):
                        if S[k]==c and flag == False: 
                            cnt += 1
                            flag = True
                            break
                    break
            break
print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
S = input()
cnt = 0
set_ = set()
for val in range(1000):
    val = str(val).zfill(3)
    a =val[0]
    b = val[1]
    c = val[2]
    for i in range(n):
        if S[i]==a:
            for j in range(i+1,n):
                if S[j]==b:
                    for k in range(j+1,n):
                        if S[k]==c and S[i]+S[j]+S[k] not in set_: 
                            cnt += 1
                            set_.add(S[i]+S[j]+S[k])
print(cnt)

Fixed submission:
n = int(input())
S = input()
cnt = 0
set_ = set()
for val in range(1000):
    val = str(val).zfill(3)
    a =val[0]
    b = val[1]
    c = val[2]
    flag = False
    for i in range(n):
        if S[i]==a and flag == False:
            for j in range(i+1,n):
                if S[j]==b and flag == False:
                    for k in range(j+1,n):
                        if S[k]==c and flag == False: 
                            cnt += 1
                            flag = True
                            break
                    break
            break
print(cnt)
Buggy python submission (verdict: Runtime Error):
n  =int(input())
D = list(map(int,input().split()))
ans = 0
for i in range(n):
    for j in range(i+1,n):
        ans += D[i]*D[j]
print(ans)

Fixed submission:
n = int(input())
s = input()
S = []
for i in s:
    S.append(i)
from collections import deque
q = deque(S)
ans = []
while q:
    c  =q.popleft()
    if not ans:
        ans.append(c)
    else:
        while ans and ans[-1]==c:
            ans.pop()
        ans.append(c)

print(len(ans))
Buggy python submission (verdict: Runtime Error):
n,q = map(int,input().split())
g = [[] for _ in range(n+1)]
d = [0]*(n+1)
for _ in range(n-1):
    a,b = map(int,input().split())
    g[a].append(b)
for _ in range(q):
    p,x = map(int,input().split())
    d[p] += x
ans = [0]*(n+1)
seen = [False]*(n+1)
def helper(s,cost):
    if not seen[s]:
        seen[s] = True
        ans[s] += cost
        for e in g[s]:
            if not seen[e]:
                helper(e,cost+d[e])
for i in range(1,n+1):
    helper(i,d[i])
res = []
for a in ans[1:]:
    res.append(str(a))

print(' '.join(res))

Fixed submission:
n,q = map(int,input().split())
g = [[] for _ in range(n+1)]
d = [0]*(n+1)
for _ in range(n-1):
    a,b = map(int,input().split())
    g[a].append(b)
    g[b].append(a)
for _ in range(q):
    p,x = map(int,input().split())
    d[p] += x
ans = [0]*(n+1)
seen = [False]*(n+1)
from collections import deque
q = deque([(1,d[1])])
while q:
    s,cost = q.popleft()
    seen[s] = True
    ans[s] += cost
    for e in g[s]:
        if not seen[e]:
            q.append((e,cost+d[e]))
# def helper(s,cost):
#     seen[s] = True
#     ans[s] += cost
#     for e in g[s]:
#         if not seen[e]:
#             helper(e,cost+d[e])
# for i in range(1,n+1):
#     if not seen[i]:
#         helper(i,d[i])
res = []
for a in ans[1:]:
    res.append(str(a))

print(' '.join(res))

Buggy python submission (verdict: Runtime Error):
n,q = map(int,input().split())
g = [[] for _ in range(n+1)]
d = [0]*(n+1)
for _ in range(n-1):
    a,b = map(int,input().split())
    g[a].append(b)
for _ in range(q):
    p,x = map(int,input().split())
    d[p] += x
ans = [0]*(n+1)
seen = [False]*(n+1)
def helper(s,cost):
    if not seen[s]:
        seen[s] = True
        ans[s] += cost
        for e in g[s]:
            if not seen[e]:
                helper(e,cost+d[e])
helper(1,d[1])
res = []
for a in ans[1:]:
    res.append(str(a))

print(' '.join(res))

Fixed submission:
n,q = map(int,input().split())
g = [[] for _ in range(n+1)]
d = [0]*(n+1)
for _ in range(n-1):
    a,b = map(int,input().split())
    g[a].append(b)
    g[b].append(a)
for _ in range(q):
    p,x = map(int,input().split())
    d[p] += x
ans = [0]*(n+1)
seen = [False]*(n+1)
from collections import deque
q = deque([(1,d[1])])
while q:
    s,cost = q.popleft()
    seen[s] = True
    ans[s] += cost
    for e in g[s]:
        if not seen[e]:
            q.append((e,cost+d[e]))
# def helper(s,cost):
#     seen[s] = True
#     ans[s] += cost
#     for e in g[s]:
#         if not seen[e]:
#             helper(e,cost+d[e])
# for i in range(1,n+1):
#     if not seen[i]:
#         helper(i,d[i])
res = []
for a in ans[1:]:
    res.append(str(a))

print(' '.join(res))
Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
n = int(input())
S = []
for _ in range(n):
    S.append(input())
cnt = 0
for i in range(1,n):
    d1 = Counter(S[i])
    for j in range(i):
        d2 = Counter(S[j])
        if d1 ==d2: cnt += 1
print(cnt)



Fixed submission:
import collections
n = int(input())
S = collections.defaultdict(int)
for _ in range(n):
    s = ''.join(sorted(input()))
    S[s] += 1
cnt = 0
for key,val in S.items():
    if val >1:
        cnt += val*(val-1)//2
print(cnt)
Buggy python submission (verdict: Runtime Error):
N = int(input())
seq = [int(input()) for _ in range(N)]
from collections import deque
LIS = deque([seq[0]])
for i in range(1,len(seq)):
    if seq[i] <= LIS[0]:
        LIS.appendleft(seq[i])
    else:
        LIS[bisect.bisect_left(LIS, seq[i])-1] = seq[i]
print(len(LIS))

Fixed submission:
N = int(input())
seq = [int(input()) for _ in range(N)]
from collections import deque
import bisect
LIS = deque([seq[0]])
for i in range(1,len(seq)):
    if seq[i] <= LIS[0]:
        LIS.appendleft(seq[i])
    else:
        LIS[bisect.bisect_left(LIS, seq[i])-1] = seq[i]

print(len(LIS))

Buggy python submission (verdict: Runtime Error):
N = int(input())
seq = [int(input()) for _ in range(N)]
LIS = deque([seq[0]])
for i in range(1,len(seq)):
    if seq[i] <= LIS[0]:
        LIS.appendleft(seq[i])
    else:
        LIS[bisect.bisect_left(LIS, seq[i])-1] = seq[i]
 
print(len(LIS))

Fixed submission:
N = int(input())
seq = [int(input()) for _ in range(N)]
from collections import deque
import bisect
LIS = deque([seq[0]])
for i in range(1,len(seq)):
    if seq[i] <= LIS[0]:
        LIS.appendleft(seq[i])
    else:
        LIS[bisect.bisect_left(LIS, seq[i])-1] = seq[i]

print(len(LIS))
Buggy python submission (verdict: Time Limit Exceeded):
N,M = map(int,input().split())
L=[]
for _ in range(N):
    a,b = map(int,input().split())
    L.append((a,b))
L.sort()
ans = 0
while M>0:
    a,b = L[0]
    del L[0]
    if M>=b:
        ans += a*b
    else:
        ans +=a*M
    M-=b
print(ans)

Fixed submission:
import collections
N,M = map(int,input().split())
l=[]
for _ in range(N):
    a,b = map(int,input().split())
    l.append((a,b))
l.sort()
L = collections.deque(l)
ans = 0
while M>0:
    a,b = L.popleft()
    if M>=b:
        ans += a*b
    else:
        ans +=a*M
    M-=b
print(ans)
Buggy python submission (verdict: Runtime Error):
N = int(input())
A = list(map(int,input().split()))
# def helper(A):
#     v = min(A)
#     ans = set()
#     for a in A:
#         if a%v!=0:
#             ans.add(a%v)
#     ans.add(v)
#     return ans

# while len(A)>1:
#     A = helper(A)
# print(min(A))
def gcd(x,y):
    if y==0: return x
    return gcd(y,x%y)
ans = A[0]
for a in A:
    ans = gcd(ans,i)
print(ans)

Fixed submission:
N = int(input())
A = set(map(int,input().split()))
def helper(A):
    v = min(A)
    ans = set()
    for a in A:
        if a%v!=0:
            ans.add(a%v)
    ans.add(v)
    return ans

while len(A)>1:
    A = helper(A)
print(min(A))
Buggy python submission (verdict: Runtime Error):
N,K = map(int,input().split())
dic = {}
A = []
for _ in range(N):
    t,d = map(int,input().split())
    A.append([t,d])
A.sort(key = lambda x:x[1], reverse = True)
val = 0
set_ = set()
duplicated = collections.deque([])
for t,d in A[:K]:
    val += d
    if t in set_:
        duplicated.append(d)
    else:
        set_.add(t)
now = val + len(set_)**2

nx = collections.deque(A[K:])
ans = now
c = len(set_)
while len(set_) < K and nx:
    i,j = nx.popleft()
    if i in set_:
        continue
    d = duplicated.pop()
    diff = j-d + 2*c+1
    set_.add(i)
    now += diff
    ans =max(ans,now)
    c += 1
print(ans)


Fixed submission:
import collections
N,K = map(int,input().split())
dic = {}
A = []
for _ in range(N):
    t,d = map(int,input().split())
    A.append([t,d])
A.sort(key = lambda x:x[1], reverse = True)
val = 0
set_ = set()
duplicated = collections.deque([])
for t,d in A[:K]:
    val += d
    if t in set_:
        duplicated.append(d)
    else:
        set_.add(t)
now = val + len(set_)**2

nx = collections.deque(A[K:])
ans = now
c = len(set_)
while len(set_) < K and nx:
    i,j = nx.popleft()
    if i in set_:
        continue
    d = duplicated.pop()
    diff = j-d + 2*c+1
    set_.add(i)
    now += diff
    ans =max(ans,now)
    c += 1
print(ans)

Buggy python submission (verdict: Runtime Error):
import collections
N,K = map(int,input().split())
dic = {}
A = []
for _ in range(N):
    t,d = map(int,input().split())
    A.append([t,d])
A.sort(key = lambda x:x[1],reverse = True)
val = 0
set_= set()
D = collections.deque([])
for t,d in A[:K]:
    val += d
    if t in set_:
        D.append(d)
    else:
        set_.add(t)
now = val + len(set_)**2
nx = collections.deque(A[K:])
ans = now
c = len(set_)
while len(set_)<K and nx:
    i,j = nx.popleft()
    if i in set_:
        continue
    d = D.pop()
    diff = j-d+2*c+1
    set_.add(j)
    now += diff
    ans = max(ans,now)
    c +=1
print(ans)

Fixed submission:
import collections
N,K = map(int,input().split())
dic = {}
A = []
for _ in range(N):
    t,d = map(int,input().split())
    A.append([t,d])
A.sort(key = lambda x:x[1], reverse = True)
val = 0
set_ = set()
duplicated = collections.deque([])
for t,d in A[:K]:
    val += d
    if t in set_:
        duplicated.append(d)
    else:
        set_.add(t)
now = val + len(set_)**2

nx = collections.deque(A[K:])
ans = now
c = len(set_)
while len(set_) < K and nx:
    i,j = nx.popleft()
    if i in set_:
        continue
    d = duplicated.pop()
    diff = j-d + 2*c+1
    set_.add(i)
    now += diff
    ans =max(ans,now)
    c += 1
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
s = input()
t = input()
S,T = len(s),len(t)
dp = [['']*(T+1) for _ in range(S+1)]
for i in range(S):
    for j in range(T):
        if s[i]==t[j]:
            dp[i+1][j+1] = dp[i][j] + s[i]
        else:
            if len(dp[i+1][j]) >= len(dp[i][j+1]):
                dp[i+1][j+1] = dp[i+1][j]
            else:
                dp[i+1][j+1] = dp[i][j+1]
print(dp[-1][-1])

Fixed submission:
s = input()
t = input()
S,T = len(s),len(t)
dp = [[0]*(T+1) for _ in range(S+1)]
for i in range(S):
    for j in range(T):
        if s[i]==t[j]:
            dp[i+1][j+1] = 1 + dp[i][j]
        else:
            dp[i+1][j+1] = max(dp[i][j+1], dp[i+1][j])
ans = ''
i,j = S,T
while dp[i][j]:
    if dp[i][j] ==dp[i][j-1]:
        j-=1
    elif dp[i][j] == dp[i-1][j]:
        i-=1
    else:
        ans = s[i-1] + ans
        i-=1
        j-=1
print(ans)