Harryxun/GPT-100-sft-dataset huggingface dataset:
Problem p02681 (python)

Fixed submission:
import sys
sys.setrecursionlimit(100000000)
MOD = 10 ** 9 + 7
INF = 10 ** 15

def main():
    s = input()
    t = input()
    if s == t[:-1] and len(t) == len(s) + 1:
        print('Yes')
    else:
        print('No')
         
if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(100000000)
MOD = 10 ** 9 + 7
INF = 10 ** 15

def main():  
    N,K = map(int,input().split())
    A = list(map(int,input().split()))
    A = [a - 1 for a in A]
    X = K.bit_length()
    to = [[-1] * X for _ in range(N)]
    for i,a in enumerate(A):
        to[i][0] = a
    for i in range(X - 1):
        for j in range(N):
            to[j][i + 1] = to[to[j][i]][i]
    now = 0
    i = 0
    while K > 0:
        if K&1:
            now = to[now][i]
        i += 1
        K >>= 1
    print(now + 1)
if __name__ == '__main__':
    main()
Problem p02734 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10000000)
MOD = 998244353
INF = 10 ** 15

def main():
    N,S = map(int,input().split())
    A = list(map(int,input().split()))

    before = [0] * (1 + S)
    before[0] = 1
    ans = 0
    for a in A:
        now = [0] * (1 + S)
        for i in range(S + 1):
            now[i] += before[i]
            if i + a <= S:
                now[i + a] += before[i]
                now[i + a] %= MOD
        now[0] += 1
        ans += now[S]
        ans %= MOD
        before = now
    print(ans)
if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys 
MOD = 10**9 +7
INF = 10**5

def main():
    n,s = map(int,input().split())
    a = list(map(int,input().split()))

    dp = [[0] * (s + 1) for _ in range(n + 1)]
    for i in range(n):
        num = a[i]
        for j in range(1,s + 1):
            dp[i + 1][j] = dp[i][j] + dp[i][j - a[i]]
    
    print(dp[-1][-1])
if __name__=='__main__':
    main()
Problem p02838 (python)

Fixed submission:
def main():
  n=int(input())
  num=[int(i) for i in input().split()]
  mod=10**9+7

  import math
  if max(num)==0:
    print(0)
    exit()
  l=math.ceil(math.log(max(num),2))

  ans=[0]*l
  tmp1,tmp2,tmp3=0,0,0
  for i in range(l):
    ans[i]=tmp3
    cnt=0
    for j in range(n):
      if (num[j]>>i)&1:
        cnt+=1
    tmp1=cnt*(n-cnt)
    tmp2=(ans[i]+tmp1)%2
    tmp3=(ans[i]+tmp1)//2
    ans[i]=tmp2

  a_append=ans.append
  while tmp3>0:
    a_append(tmp3%2)
    #print(tmp3)
    tmp3//=2
  #print(ans)
  ans=int(''.join([str(i) for i in ans[::-1]]),2)%mod
  print(ans)
  
if __name__=='__main__':
  main()

Buggy submission (verdict: Runtime Error):
ef main():
  n=int(input())
  num=[int(i) for i in input().split()]
  mod=10**9+7

  import math
  if max(num)==0:
    print(0)
    exit()
  l=math.ceil(math.log(max(num),2))

  ans=[0]*l
  tmp1,tmp2,tmp3=0,0,0
  for i in range(l):
    ans[i]=tmp3
    cnt=0
    for j in range(n):
      if (num[j]>>i)&1:
        cnt+=1
    tmp1=cnt*(n-cnt)
    tmp2=(ans[i]+tmp1)%2
    tmp3=(ans[i]+tmp1)//2
    ans[i]=tmp2

  a_append=ans.append
  while tmp3>0:
    a_append(tmp3%2)
    #print(tmp3)
    tmp3//=2
  #print(ans)
  ans=int(''.join([str(i) for i in ans[::-1]]),2)%mod
  print(ans)
  
if __name__=='__main__':
  main()

Buggy submission (verdict: Runtime Error):
n=int(input())
num=[int(i) for i in input().split()]
mod=10**9+7

import math
l=math.ceil(math.log(max(num),2))

ans=[0]*l
tmp1,tmp2,tmp3=0,0,0
for i in range(l):
  ans[i]=tmp3
  cnt=0
  for j in range(n):
    if (num[j]>>i)&1:
      cnt+=1
  tmp1=cnt*(n-cnt)
  tmp2=(ans[i]+tmp1)%2
  tmp3=(ans[i]+tmp1)//2
  ans[i]=tmp2

while tmp3>0:
  ans.append(tmp3%2)
  #print(tmp3)
  tmp3//=2
#print(ans)
ans=int(''.join([str(i) for i in ans[::-1]]),2)%mod
print(ans)
Problem p02855 (python)

Fixed submission:
def main():
    h,w,k=map(int,input().split())
    s=[list(input()) for _ in range(h)]
    
    ans=[[0]*w for _ in range(h)]
    for idx in range(h):
        if '#' in s[idx]:
            i=idx
            break
            
    start,j,cnt,first=i,0,1,True

    while i<h:
        if j==w:
            if first:
                ans[i]=ans[i-1]
                i+=1
                j=0
                continue
            first=True
            i+=1
            j=0
            cnt+=1
            continue

        if s[i][j]=='#':
            if first:
                first=False
                ans[i][j]=cnt
                j+=1
                continue
            cnt+=1
            ans[i][j]=cnt
        else:
            ans[i][j]=cnt

        j+=1
    
    for i in range(start):
        ans[i]=ans[start]
    
    for i in range(h):
        print(*ans[i]) 
    
if __name__=='__main__':
    main()

Buggy submission (verdict: Runtime Error):
def main():
    h,w,k=map(int,input().split())
    s=[list(input()) for _ in range(h)]
    
    ans=[[0]*w for _ in range(h)]
    for idx in range(h):
        if '#' in s[idx]:
            i=idx
            break
            
    start,j,cnt,first,itigo=i,0,1,True,True
    while i<h:
        if s[i][j]=='#':
            if first:
                first=False
                itigo=False
                ans[i][j]=cnt
                j+=1
                continue
            cnt+=1
            ans[i][j]=cnt
        else:
            ans[i][j]=cnt

        j+=1
        if j==w:
            first=True
            if itigo:
                ans[i]=ans[i-1]
                i+=1
                j=0
                continue
            itigo=True
            i+=1
            j=0
            cnt+=1
    
    for i in range(start):
        ans[i]=ans[start]
    
    for i in range(h):
        print(*ans[i]) 
    
if __name__=='__main__':
    main()
Problem p02866 (python)

Fixed submission:
n=int(input())
ds=[int(i) for i in input().split()]
counts={}

if ds[0]!=0:
  ans=0

else:  
    for i in range(1,n):
      if ds[i] not in counts:
        counts[ds[i]]=1
      else:
        counts[ds[i]]+=1
    c=0
    for i in range(1,len(counts)+1):
      if i in counts:
        c+=1
    if c==len(counts):
      ans=1
      for i in range(1,len(counts)):
          x=(counts[i]**counts[i+1])%998244353
          ans*=x
          ans%998244353
    else:
      ans=0

print(ans%998244353)

Buggy submission (verdict: Runtime Error):
n=int(input())
ds=[int(i) for i in input().split()]
ans=0
counts={}

if ds[0]!=0:
  ans=0
  
else:  
    for i in range(1,n):
      if ds[i] not in counts:
        counts[ds[i]]=1
      else:
        counts[ds[i]]+=1
    ans=1
    for i in range(1,len(counts)):
      x=(counts[i]**counts[i+1])%998244353
      ans*=x
      ans%998244353

print(ans%998244353)

Buggy submission (verdict: Runtime Error):
n=int(input())
ds=[int(i) for i in input().split()]
ans=0
counts={}
if ds[0]!=0:
  ans=0
  
else:  
    for i in range(1,n):
      if ds[i] not in counts:
        counts[ds[i]]=1
      else:
        counts[ds[i]]+=1
    ans=1
    for i in range(1,len(counts)):
      ans*=counts[i]**counts[i+1]

print(ans)
Problem p02874 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10000000)
MOD = 10 ** 9 + 7
INF = 10 ** 15

def main():
    N = int(input())
    prob = [tuple(map(int,input().split())) for _ in range(N)]
    
    if N == 2:
        ans = sum(r - l + 1 for l,r in prob)
        print(ans)
        return 

    #min_Rとmax_Lが同じグループにいる時
    max_L = 0
    min_R = INF
    cnt_max = 0 #L最大を満たすのが何個あるか
    cnt_min = 0 #R最小を満たすのが何個あるか
    for l,r in prob:
        if l == max_L:
            cnt_max += 1
        elif l > max_L:
            cnt_max = 1
            max_L = l
        if r == min_R:
            cnt_min += 1
        elif r < min_R:
            cnt_min = 1
            min_R = r
    
    if cnt_min > 1 and cnt_max > 1:
        max_diff = max(r - l for l,r in prob) + 1
    elif cnt_min > 1:
        max_diff = max(r - l for l,r in prob if l != max_L) + 1
    elif cnt_max > 1:
        max_diff = max(r - l for l,r in prob if r != min_R) + 1
    else:
        max_diff = max(r - l for l,r in prob if (r != min_R) and (l != max_L)) + 1
    
    ans1 = max_diff + max(min_R - max_L + 1,0)
    
    #min_Rとmax_Lが違うグループにいる時
    prob.sort(key = lambda x:(x[1],x[0]))
    cummax_L = prob[0][0]
    ans2 = 0
    for l,r in prob[1:]:
        ans2 = max(ans2,max(r - max_L + 1,0) + max(min_R - cummax_L + 1,0))
        cummax_L = max(cummax_L,l)

    print(max(ans1,ans2))
if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10000000)
MOD = 10 ** 9 + 7
INF = 10 ** 15

def main():
    N = int(input())
    prob = [tuple(map(int,input().split())) for _ in range(N)]
    
    #min_Rとmax_Lが同じグループにいる時
    max_L = 0
    min_R = INF
    cnt_max = 0 #L最大を満たすのが何個あるか
    cnt_min = 0 #R最小を満たすのが何個あるか
    for i,(l,r) in enumerate(prob):
        if l == max_L:
            cnt_max += 1
        elif l > max_L:
            cnt_max = 1
            max_L = l
        if r == min_R:
            cnt_min += 1
        elif r < min_R:
            cnt_min = 1
            min_R = r
    
    if cnt_min > 1 and cnt_max > 1:
        max_diff = max(r - l for l,r in prob)
    elif cnt_min > 1:
        max_diff = max(r - l for l,r in prob if l != max_L)
    elif cnt_max > 1:
        max_diff = max(r - l for l,r in prob if r != min_R)
    else:
        max_diff = max(r - l for l,r in prob if (r != min_R) and (l != max_L))
    
    ans1 = max_diff + max(min_R - max_L + 1,0) + 1
    
    #min_Rとmax_Lが違うグループにいる時
    prob.sort(key = lambda x:x[1])
    diff = prob[0][1] - prob[0][0]
    cummax_L = prob[0][0]
    for l,r in prob[1:]:
        diff = max(diff,r - cummax_L)
        cummax_L = max(cummax_L,l)
    
    ans2 = min_R - max_L + diff + 2

    print(max(ans1,ans2,0))
if __name__ == '__main__':
    main()
Problem p02883 (python)

Fixed submission:
import numpy as np
N, K = map(int, input().split())
A = np.array(input().split(), dtype=np.int64)
F = np.array(input().split(), dtype=np.int64)
A = np.sort(A)
F = np.sort(F)[::-1]


def C(x):
    train = np.maximum(0, A - x // F).sum()
    return train <= K


left = -1
right = 10 ** 12 + 1
while right > left + 1:
    mid = (right + left) // 2
    if C(mid):  # 食べきれる場合
        right = mid
    else:
        left = mid

ans = right
print(ans)

Buggy submission (verdict: Runtime Error):
import sys
read=sys.stdin.buffer.read
import numpy as np

n,k=map(int,input().split())
A=np.array(read().split(),np.int64)
F=np.array(read().split(),np.int64)
A.sort()
F.sort()
F=F[::-1]
    
def ok(x): #チームの成績をxとするような修行の仕方が存在するか
    B=A-x//F
    return sum(B[B>0])<=k
    
l=-1
r=10**18
while r-l>1:
    mid=(r+l)//2
    if ok(mid):
        r=mid
    else:
        l=mid
print(r)
Problem p02923 (python)

Fixed submission:
n=int(input())
H=[int(i) for i in input().split()]


cnt=0
ans=[]
for i in range(n-1):
    if H[i]>=H[i+1]:
        cnt+=1
        ans.append(cnt)
    else:
        cnt=0
        ans.append(cnt)

if ans:
  print(max(ans))

else:
  print(0)

Buggy submission (verdict: Runtime Error):
n=int(input())
heights=[int(i) for i in input().split()]

ans=[]
trans=0

for i in range(n-1):
  if heights[i]>=heights[i+1]:
    trans+=1
    ans.append(trans)
  else:
    trans=0
    ans.append(trans)
       
print(max(ans))

Buggy submission (verdict: Runtime Error):
n=int(input())
H=[int(i) for i in input().split()]

cnt=0
ans=[]
for i in range(n-1):
    if H[i]>=H[i+1]:
        cnt+=1
        ans.append(cnt)
    else:
        cnt=0
print(max(ans))
Problem p02936 (python)

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

n,q=map(int,input().split())
tree=[[] for i in range(n)]

for i in range(n-1):
  a,b=map(int,input().split())
  a,b=a-1,b-1
  tree[a].append(b)
  tree[b].append(a)

ans=[0]*n
for i in range(q):
  p,x=map(int,input().split())
  ans[p-1]+=x

from collections import deque
q=deque([0])
while q:
  i=deque.popleft(q)
  for leaf in tree[i]:
    q.append(leaf)
    ans[leaf]+=ans[i]
    tree[leaf].remove(i)

print(*ans)

Buggy submission (verdict: Runtime Error):
def main():
    import sys
    input=sys.stdin.readline
    
    n,q=map(int,input().split())
    tree=[[] for _ in range(n)]
    for _ in range(n-1):
        a,b=map(int,input().split())
        a-=1
        b-=1
        tree[a].append(b)
        tree[b].append(a)
    
    cnt=[0]*n
    for _ in range(q):
        p,x=map(int,input().split())
        p-=1
        cnt[p]+=x
    
    def dfs(i,before=-1):
        for e in tree[i]:
            if e!=before:
                cnt[e]+=cnt[i]
                dfs(e,i)
    dfs(0)   
    print(*cnt)

if __name__=='__main__':
    main()
Problem p03000 (python)

Fixed submission:
n,x=map(int,input().split())
Len=[int(i) for i in input().split()]

ans=n+1
test=0
for i in range(n):
  test+=Len[i]
  if test<=x:
    continue
  else:
    ans=i+1
    break
    
print(ans)

Buggy submission (verdict: Runtime Error):
n,x=map(int,input().split())
Len=[int(i) for i in input().split()]

test=0
for i in range(n):
  test+=Len[i]
  if test<=x:
    continue
  else:
    ans=i+1
    break
    
print(ans)
Problem p03006 (python)

Fixed submission:
def main():
    import sys
    input=sys.stdin.readline   
    sys.setrecursionlimit(100000000)

    n=int(input())
    point=[tuple(map(int,input().split())) for _ in range(n)]
    dic={i:j for i,j in zip(point,range(n))}
    
    if n==1:
        print(1)
        exit()
        
    ans=10**9
    for i in range(n-1):
        for j in range(i+1,n):
            p=point[j][0]-point[i][0]
            q=point[j][1]-point[i][1]
            cnt=0
            visited=[True]*n

            def dfs(x,y):
                if (x+p,y+q) in dic:
                    idx=dic[(x+p,y+q)]
                    if visited[idx]:
                        visited[idx]=False
                        dfs(x+p,y+q)
                if (x-p,y-q) in dic:
                    idx=dic[(x-p,y-q)]
                    if visited[idx]:
                        visited[idx]=False
                        dfs(x-p,y-q)

            for k in range(n):
                if visited[k]:
                    visited[k]=False
                    cnt+=1
                    dfs(point[k][0],point[k][1])      
        
            ans=min(ans,cnt)  
    print(ans)

if __name__=='__main__':
    main()

Buggy submission (verdict: Runtime Error):
def main():
    import sys
    input=sys.stdin.readline   
    sys.setrecursionlimit(100000000)

    n=int(input())
    point=[tuple(map(int,input().split())) for _ in range(n)]
    dic={i:j for i,j in zip(point,range(n))}
    

    ans=10**9
    for i in range(n-1):
        for j in range(i+1,n):
            p=point[j][0]-point[i][0]
            q=point[j][1]-point[i][1]
            cnt=0
            visited=[True]*n

            def dfs(x,y,p,q):
                if (x+p,y+q) in dic:
                    idx=dic[(x+p,y+q)]
                    if visited[idx]:
                        visited[idx]=False
                        dfs(x+p,y+q,p,q)
                if (x-p,y-q) in dic:
                    idx=dic[(x-p,y-q)]
                    if visited[idx]:
                        visited[idx]=False
                        dfs(x-p,y-q,p,q)

            for k in range(n):
                if visited[k]:
                    cnt+=1
                    dfs(point[k][0],point[k][1],p,q)      

            ans=min(ans,cnt)  
    print(cnt)

if __name__=='__main__':
    main()
Problem p03007 (python)

Fixed submission:
import sys
INF = 10 ** 11
MOD = 10 ** 9 + 7
from collections import deque
sys.setrecursionlimit(100000000)
from heapq import heapify,heappush,heappop   

def main():
    n = int(input())
    a = list(map(int,input().split()))
    m = []
    p = []
    z = 0
    for num in a:
        if num < 0:
            m.append(num)
        elif num > 0:
            p.append(num)
        else:
            z += 1
    
    ans = []
    
    if z == n:
        print(0)
        for i in range(n - 1):
            print(0,0)
        return

    if len(p) == 0:
        if z:
            num1 = 0
            z -= 1
        else:
            m.sort()
            num1 = m[-1]
            m.pop()
        for i in range(len(m)):
            ans.append((num1,m[i]))
            num1 -= m[i]
    
    elif len(m) == 0:
        if z:
            num1 = 0
            z -= 1
            idx = 0
        else:
            p.sort()
            num1 = p[0]
            idx = 1
        for i in range(idx,len(p) - 1):
            ans.append((num1,p[i]))
            num1 -= p[i]
        ans.append((p[-1],num1))
        num1 = p[-1] - num1
    
    elif len(p) * len(m) > 0:
        num1 = p[-1]
        p.pop()
        num2 = m[-1]
        m.pop()
        for i in range(len(m)):
            ans.append((num1,m[i]))
            num1 -= m[i]
        for i in range(len(p)):
            ans.append((num2,p[i]))
            num2 -= p[i]
        ans.append((num1,num2))
        num1 -= num2
    
    for i in range(z):
        ans.append((num1,0))
    print(num1)
    for i in range(n - 1):
        print(*ans[i])

if __name__=='__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
INF = 10 ** 11
MOD = 10 ** 9 + 7
from collections import deque
sys.setrecursionlimit(100000000)
from heapq import heapify,heappush,heappop   

def main():
    n = int(input())
    a = list(map(int,input().split()))
    m = []
    p = []
    z = 0
    for num in a:
        if num < 0:
            m.append(num)
        elif num > 0:
            p.append(num)
        else:
            z += 1
    
    ans = []
    if len(p) * len(m) > 0:
        num1 = p[-1]
        p.pop()
        num2 = m[-1]
        m.pop()
        for i in range(len(m)):
            ans.append((num1,m[i]))
            num1 -= m[i]
        for i in range(len(p)):
            ans.append((num2,p[i]))
            num2 -= p[i]
        ans.append((num1,num2))
        num1 -= num2
    
    elif len(p) == 0:
        m.sort()
        num1 = m[-1]
        m.pop()
        for i in range(len(m)):
            ans.append((num1,m[i]))
            num1 -= m[i]
    
    elif len(m) == 0:
        p.sort()
        num1 = p[0]
        for i in range(1,len(p) - 1):
            ans.append((num1,p[i]))
            num1 -= p[i]
        ans.append((p[-1],num1))
        num1 = p[-1] - num1
    
    for i in range(z):
        ans.append((num1,0))
    print(num1)
    for i in range(n - 1):
        print(*ans[i])

if __name__=='__main__':
    main()
Problem p03044 (python)

Fixed submission:
from collections import deque,defaultdict
n=int(input())
routes=[tuple(map(int,input().split())) for i in range(n-1)]

edges=defaultdict(list)
for v1,v2,dist in routes:
  edges[v1-1].append([v2-1,dist])
  edges[v2-1].append([v1-1,dist])
#print(edges)

ans=[0]*n
q=deque([0])
while q:
  v=q.popleft()
  #print(v)
  if ans[v]==0:
    color=[1,0]
  else:
    color=[0,1]
  #print(edges)
  for edge in edges[v]:
    v2=edge[0]
    dist=edge[1]
    edges[v2].remove([v,dist])
    q.append(v2)
    if dist%2==1:
      ans[v2]=color[0]
    else:
      ans[v2]=color[1]
      
print(*ans,sep='\n')

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
n=int(input())
routes=[tuple(map(int,input().split())) for i in range(n-1)]

edges=defaultdict(list)
for v1,v2,dist in routes:
  edges[v1-1].append([v2-1,dist])
  edges[v2-1].append([v1-1,dist])
#print(edges)

ans=[-1]*n
ans[0]=0

def dfs(i):
  if ans[i]!=-1:
    return ans[i]
  else:
    for edge in edges[i]:
      v=edge[0]
      dist=edge[1]
      ans[i]=dfs(v)+dist
      return ans[i]
for i in range(n):
  dfs(i)
  
print(*[ans[i]%2 for i in range(n)],sep='\n')
Problem p03074 (python)

Fixed submission:
n,k=map(int,input().split())
s=input()

num_cnt=[]
if s[0]=='1':
  num_cnt.append(0)

cnt=1
for i in range(1,n):
  if s[i]==s[i-1]:
    cnt+=1
  else:
    num_cnt.append(cnt)
    cnt=1
num_cnt.append(cnt)

leng=len(num_cnt)
if leng/2>k:
  ans=sum(num_cnt[:2*k])
  tot=ans
  
  if leng%2==0:
    limit=leng//2-k
  else:
    num_cnt.append(0)
    limit=leng//2-k+1
  
  #print(ans)
  #print(num_cnt)
  tmp2=num_cnt[2*k]+num_cnt[2*k+1]-num_cnt[0]
  tot+=tmp2
  if tot>ans:
    ans=tot
  
  for i in range(1,limit):
    #print(2*(i+k)+1)
    tmp=num_cnt[2*(i+k)]+num_cnt[2*(i+k)+1]-num_cnt[2*i]-num_cnt[2*i-1]
    #print(tot)
    tot+=tmp
    if tot>ans:
      ans=tot
else:
  ans=n
print(ans)

Buggy submission (verdict: Runtime Error):
n,k=map(int,input().split())
s=input()

num_cnt=[]
if s[0]=='1':
  num_cnt.append(0)

cnt=1
for i in range(1,n):
  if s[i]==s[i-1]:
    cnt+=1
  else:
    num_cnt.append(cnt)
    cnt=1
num_cnt.append(cnt)

if k<=n/2:
  ans=sum(num_cnt[:2*k])

  tmp=len(num_cnt)
  if tmp%2==0:
    limit=tmp//2-k
  else:
    num_cnt.append(0)
    limit=tmp//2-k+1
  
  #print(ans)
  #print(num_cnt)
  tmp=num_cnt[2*k]+num_cnt[2*k+1]-num_cnt[0]
  if tmp>0:
    ans+=tmp
  
  for i in range(1,limit):
    tmp=num_cnt[2*(i+k)]+num_cnt[2*(i+k)+1]-num_cnt[2*i]-num_cnt[2*i+1]
    #print(tmp)
    if tmp>0:
      ans+=tmp
else:
  ans=n
print(ans)

Buggy submission (verdict: Runtime Error):
n,k=map(int,input().split())
s=input()

num_cnt=[]
if s[0]=='1':
  num_cnt.append(0)

cnt=1
for i in range(1,n):
  if s[i]==s[i-1]:
    cnt+=1
  else:
    num_cnt.append(cnt)
    cnt=1
num_cnt.append(cnt)

if k<=n/2:
  ans=sum(num_cnt[:2*k])

  tmp=len(num_cnt)
  if tmp%2==0:
    limit=tmp//2-k-1
  else:
    num_cnt.append(0)
    limit=tmp//2-k
  
  #print(ans)
  #print(num_cnt)
  if k/2<=n:
    tmp=num_cnt[2*k]+num_cnt[2*k+1]-num_cnt[0]
    if tmp>0:
      ans+=tmp
  
  for i in range(1,limit):
    tmp=num_cnt[2*(i+k)]+num_cnt[2*(i+k)+1]-num_cnt[2*i]-num_cnt[2*i+1]
    #print(tmp)
    if tmp>0:
      ans+=tmp
else:
  ans=n
print(ans)
Problem p03148 (python)

Fixed submission:
import sys
sys.setrecursionlimit(100000000)

def main():
    N,K = map(int,input().split())
    sushi = [tuple(map(int,input().split())) for _ in range(N)]

    sushi.sort(key = lambda x:x[1],reverse = True)
    ans = 0
    dic = {}
    change = []
    for a,b in sushi[:K]:
        if a in dic:
            dic[a].append(b)
            change.append((a,b))
        else:
            dic[a] = [b]
        ans += b

    various = len(dic)
    ans += various ** 2
    ret = ans
    used_type = set(dic.keys())
    suggestion = [p for p in sushi[K:] if p[0] not in used_type]
    for a,b in suggestion:
        if len(change) == 0:
            break
        if a in used_type:
            continue
        dic[change[-1][0]].pop()
        ret -= change[-1][1]
        ret += b + 2*various + 1
        dic[a] = b
        change.pop()
        various += 1
        used_type.add(a)
        ans = max(ans,ret)
    print(ans)
if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(100000000)

def main():
    N,K = map(int,input().split())
    sushi = [tuple(map(int,input().split())) for _ in range(N)]

    sushi.sort(key = lambda x:x[1],reverse = True)
    ans = 0
    dic = {}
    change = []
    for a,b in sushi[:K]:
        if a in dic:
            dic[a].append(b)
            change.append((a,b))
        else:
            dic[a] = [b]
        ans += b

    various = len(dic)
    ans += various ** 2
    ret = ans
    used_type = {a for a in dic.keys()}
    suggestion = [p for p in sushi[K:] if p[0] not in used_type]
    for a,b in suggestion:
        if a in used_type:
            continue
        dic[change[-1][0]].pop()
        ret -= change[-1][1]
        ret += b + 2*various + 1
        dic[a] = b
        change.pop()
        various += 1
        used_type.add(a)
        ans = max(ans,ret)
    print(ans)
if __name__ == '__main__':
    main()
Problem p03160 (python)

Fixed submission:
n=int(input())
heights=[int(i) for i in input().split()]

dp=[0]*n
dp[1]=abs(heights[1]-heights[0])

for i in range(2,n):
  dp[i]=min(dp[i-2]+abs(heights[i]-heights[i-2]), dp[i-1]+abs(heights[i]-heights[i-1]))
print(dp[n-1])

Buggy submission (verdict: Runtime Error):
n,k=map(int,input().split())
heights=[int(i) for i in input().split()]

dp=[0]*n
dp[1]=abs(heights[1]-heights[0])

for i in range(2,n):
  step=[]
  for j in range(1,min(k+1,i+1)):
      step.append(dp[i-j]+abs(heights[i]-heights[i-j]))
    
  dp[i]=min(step)
print(dp[n-1])