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

Fixed submission:
from collections import Counter
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()

N, Q = tuple(li())
S = ns()
ops = []
for i in range(Q):
    ops.append(tuple(ls()))

head, tail = 0, N-1
while(ops):
    cur = ops.pop()
    if cur[1] == 'L' and head <N:
        if S[head] == cur[0]:
            head += 1
        if tail <= N-2 and S[tail + 1] == cur[0]:
            tail = min(tail + 1, N-1)
    elif cur[1] == 'R' and tail >= 0:
        if S[tail] == cur[0]:
            tail -= 1
        if head >= 1 and S[head - 1] == cur[0]:
            head = max(0, head-1)
alive = tail - head + 1
# print("tail:{}, head:{}".format(tail, head))
print(max(alive, 0))

Buggy submission (verdict: Runtime Error):
from collections import Counter
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()

N, Q = tuple(li())
S = ns()
ops = []
for i in range(Q):
    ops.append(tuple(ls()))

head, tail = 0, N-1
while(ops):
    cur = ops.pop()
    if cur[1] == 'L':
        if S[head] == cur[0]:
            head += 1
    elif cur[1] == 'R':
        if S[tail] == cur[0]:
            tail -= 1
alive = tail - head + 1

print(max(alive, 0))

Buggy submission (verdict: Runtime Error):
from collections import Counter
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()

N, Q = tuple(li())
S = ns()
ops = []
for i in range(Q):
    ops.append(tuple(ls()))

head, tail = 0, N-1
while(ops):
    cur = ops.pop()
    if cur[1] == 'L':
        if S[head] == cur[0]:
            head += 1
    elif cur[1] == 'R':
        if S[tail] == cur[0]:
            tail -= 1
alive = tail - head + 1
print("tail:{}, head:{}".format(tail, head))
print(max(alive, 0))
Problem p03103 (python)

Fixed submission:
import sys
import bisect
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())

N, M = tuple(li())

categories = [ tuple(li()) for _ in range(N) ]
categories.sort()

total_cost = [(0, 0)]
for A, B in categories:
    total_cost.append((B + total_cost[-1][0], A * B + total_cost[-1][1]))
    idx = bisect.bisect_right( total_cost, (M, 0) )
total = total_cost[idx][1] + (M - total_cost[idx][0]) * categories[idx - 1][0]
print(total)

Buggy submission (verdict: Runtime Error):
import sys
stdin = sys.stdin
 

 
def li(): return map(int, stdin.readline().split())

N, M = tuple(li())

categories = [ tuple(li()) for _ in range(N) ]
categories.sort()

total = 0
def buy_M(cate, M, cost):
    if M <= 0:
        return cost
    if cate[0][1] >= M:
        cur_cost = cate[0][0] * M
        return cur_cost + cost
    else:
        rest = M - cate[0][1]
        cur_cost = cate[0][1] * cate[0][0]
        return buy_M(cate[1:], rest, cost + cur_cost)
print(buy_M(categories, M, 0))
Problem p03125 (python)

Fixed submission:
import sys
stdin = sys.stdin
def li(): return map(int, stdin.readline().split())
a, b = tuple(li())
div, mod = divmod(b, a)
if mod == 0:
    print(a + b)
else:
    print(b - a)

Buggy submission (verdict: Runtime Error):
import sys
stdin = sys.stdin
def li(): return map(int, stdin.readline().split())
a, b = tuple(li())
div, mod = divmod(b, a)
if mod == 0:
    return a + b
else:
    return b - a

Buggy submission (verdict: Runtime Error):
import sys
stdin = sys.stdin
def li(): return map(int, stdin.readline().split())
a, b = tuple(li())
div, mod = divmod(b, a)
if mod == 0:
	return a + b
else:
	return b - a

Buggy submission (verdict: Runtime Error):
import sys
stdin = sys.stdin
def li(): return map(int, stdin.readline().split())
a, b = tuple(li())
div, mod = divmod(b, a)
if mod == 0:
	return a + b
else:
    return b - a
Problem p03145 (python)

Fixed submission:
a, b, c = map(int, input().split())
print(a*b//2)

Buggy submission (verdict: Runtime Error):
from math import pow






def compute_score(target_collection):
    keys = {neta_point[0] for neta_point in target_collection}
    points = [neta_point[1] for neta_point in target_collection]
    cardinality = len(keys)
    score = pow(cardinality, 2) + sum(points)
    return score

# exchange one record and compare
def optimize(full_collection, target_collection, target_collection_len, idx):
    while True:
        ex_record = full_collection[idx]
        # check on the idx-th record
        idx += 1
        # bump up for return value
        if ex_record in target_collection:
            continue # cardinality score will not increase
        else:
            for i in range(target_collection_len-1):
                if target_collection[i][0] == target_collection[i-1][0]:
                    target_collection[i] = ex_record
                    new_score = compute_score(target_collection)
                    return new_score, target_collection, idx
        # default
        return 0, target_collection, idx



def cal_score(N, K, records):
    sorted_recs = sorted(records, reverse=True, key=lambda x: x[1])
    target_collection = sorted_recs[0:K]
    score =  compute_score(target_collection)

    # optimzie at most K times
    idx = K
    for i in range(K):
        new_score, new_target_collection, idx = optimize(sorted_recs, target_collection, K, idx)
        if new_score <= score: # no optimization occured
            break
        else:
            score = new_score
    return score
  
N, K = map(int, input().split())
records = [tuple(map(int, input().split())) for i in range(N)] 
score =cal_score(N, K, records)
print(score)

Buggy submission (verdict: Runtime Error):
A = input()
B = input()
C = input()
print(A*B/2)
Problem p03147 (python)

Fixed submission:
num = int(input())
targets = map(int, input().split())
targets = list(targets)


global water_cnt
water_cnt = 0
def exe_water(init_state):
    global water_cnt
    # break condition
    if sum(init_state) == 0:
        return
    temp_water_cnt = min(init_state)
    water_cnt += temp_water_cnt

    lowest = temp_water_cnt
    # init sub_state
    sub_state = []
    for h in init_state:
        substracted_h = h - lowest
        if substracted_h != 0:
            sub_state.append(substracted_h)
        else:
            exe_water(sub_state)
            # reinit for next sequence
            sub_state = []
    # end case where no trailing 0
    exe_water(sub_state)

exe_water(targets)
print(water_cnt)

Buggy submission (verdict: Runtime Error):
num = int(input())
targets = map(int, input().split())
targets = list(targets)


water_cnt = 0
def exe_water(init_state):
    # break condition
    if sum(init_state) == 0:
        return
    temp_water_cnt = min(init_state)
    water_cnt += temp_water_cnt

    lowest = temp_water_cnt
    # init sub_state
    sub_state = []
    for h in init_state:
        substracted_h = h - lowest
        if substracted_h != 0:
            sub_state.append(substracted_h)
        else:
            exe_water(sub_state)
            # reinit for next sequence
            sub_state = []

exe_water(targets)
print(water_cnt)
Problem p02602 (python)

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

a = list(map(int, input().split()))

for i in range(k, n):
  if a[i-k] < a[i]:
    print("Yes")
  else:
    print("No")

Buggy submission (verdict: Runtime Error):
    n, k = map(int, input().split())
    a = list(map(int, input().split())) 
     
    for i in range(k, n):
        if a[i-k] < a[i]:
            print('Yes')
        else:
            print('No')
Problem p02608 (python)

Fixed submission:
n = int(input())
count = 0
a = [0]*(10**5)
for i in range(1, 100):
  for j in range(1, 100):
    for k in range(1, 100):
      a[(i+j+k)**2-(i*j+j*k+i*k)] += 1


for i in range(n):
  print(a[i+1])

Buggy submission (verdict: Runtime Error):
n = int(input())
count = 0
a = [0]*(10**4)
for i in range(1, 41):
  for j in range(1, 41):
    for k in range(1, 41):
      a[(i+j+k)**2-(i*j+j*k+i*k)] += 1


for i in range(1, n+1):
  print(a[i])

Buggy submission (verdict: Runtime Error):
n = int(input())
count = 0
a = [0]*(10**4)
for i in range(1, 41):
  for j in range(1, 41):
    for k in range(1, 41):
      a[(i+j+k)**2-(i*j+j*k+i*k)] += 1


for i in range(1, n+1):
  print(a[i])
Problem p02640 (python)

Fixed submission:
X, Y = map(int,input().split())
XX = X +1
for i in range(XX) :
    if Y -(4*i) == (X-i)*2 :
        print("Yes")
        exit()

print("No")

Buggy submission (verdict: Runtime Error):
X, Y = map(int,input().split())

for i in range(X) :
    if Y -(4*((i+1) == (X-(i+1))*2 :
        print("Yes")
        exit()
    else :
        if Y==2 or Y==4 :
            print("Yes")
            exit()
print("No")
Problem p03240 (python)

Fixed submission:
# -*- coding: utf-8 -*-

def getH(x, cx, y, cy, h):
    return h + abs(x-cx) + abs(y-cy)

N = int(input())
conditions = []
for i in range(N):
    x,y,h = map(int, input().split())
    if h > 0:
        conditions.append([x,y,h])

num_cnd = len(conditions)
if num_cnd==1:
    print(conditions[0][0],conditions[0][1],conditions[0][2])
    exit(0)

for cx in range(101):
    for cy in range(101):
        cnt = 0
        h = 0
        for i,cnd in enumerate(conditions):
            h_tmp = getH(cnd[0], cx, cnd[1], cy, cnd[2])
            if h_tmp >= 1:
                if i==0:
                    h = h_tmp
                    cnt += 1
                elif h == h_tmp:
                    cnt += 1
                    if cnt == num_cnd:
                        print(cx,cy,h)
                        exit(0)
                else:
                    break
            else:
                break
        

print('No')

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-

N = int(input())
conditions = []
for i in range(N):
    x,y,h = map(int, input().split())
    conditions.append([x,y,h])


for cx in range(101):
    for cy in range(101):
        H = -1
        for _,cond in enumerate(conditions):
            if cond[2] > 0:
                H_tmp = cond[2] + abs(cond[0]-cx) + abs(cond[1]-cy)
                if H == -1:
                    H = H_tmp
                else:
                    if H_tmp != H:
                        H = -2
                        break

        if H == -2:
            continue

        for _,cond in enumerate(conditions):
            if cond[2] == 0:
                if H > abs(cond[0]-cx) + abs(cond[1]-cy):
                    H = -2
                    break
        
        if H == -2:
            continue
        
        print(cx,cy,H)
        exit(1)

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-

def getH(x, cx, y, cy, h):
    return h + abs(x-cx) + abs(y-cy)

N = int(input())
conditions = []
for i in range(N):
    x,y,h = map(int, input().split())
    if h > 0:
        conditions.append([x,y,h])

num_cnd = len(conditions)
if num_cnd==1:
    print(conditions[0],conditions[1],conditions[2])
    exit(0)

for cx in range(101):
    for cy in range(101):
        cnt = 0
        h = 0
        for i,cnd in enumerate(conditions):
            h_tmp = getH(cnd[0], cx, cnd[1], cy, cnd[2])
            if h_tmp >= 1:
                if i==0:
                    h = h_tmp
                    cnt += 1
                elif h == h_tmp:
                    cnt += 1
                    if cnt == num_cnd:
                        print(cx,cy,h)
                        exit(0)
                else:
                    break
            else:
                break
        

print('No')
Problem p03331 (python)

Fixed submission:
# -*- coding: utf-8 -*-

def get_digits_sum(val_str):
    digits_sum = 0
    for i in range(len(val_str)):
        digits_sum += int(val_str[i])

    return digits_sum

N = int(input())

if N==2:
    sum_min = 2

for i in range(2, N, 1):
    A = i
    B = N - i
    a_sum = get_digits_sum(str(A))
    b_sum = get_digits_sum(str(B))
    if i==2:
        sum_min = a_sum + b_sum
    elif sum_min > a_sum + b_sum:
        sum_min = a_sum + b_sum
        
print(sum_min)

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-

def get_digits_sum(val_str):
    digits_sum = 0
    for i in range(len(val_str)):
        digits_sum += int(val_str[i])

    return digits_sum

N = int(input())


for i in range(2, N, 1):
    A = i
    B = N - i
    a_sum = get_digits_sum(str(A))
    b_sum = get_digits_sum(str(B))
    if i==2:
        sum_min = a_sum + b_sum
    elif sum_min > a_sum + b_sum:
        sum_min = a_sum + b_sum
        
print(sum_min)
Problem p03574 (python)

Fixed submission:
# -*- coding: utf-8 -*-

def get_count(S, H, W, i, j):
    num = 0
    start_i = start_j = -1
    end_i = end_j = 2
    if W == 1:
        start_i = 0 
        end_i = 1
    else:
        if i == 0:
            start_i = 0
        elif i == W-1:
            end_i = 1
    if H == 1:
        start_j = 0
        end_j = 1
    else:
        if j == 0:
            start_j = 0
        elif j == H-1:
            end_j = 1

    for y in range(start_j, end_j, 1):
        for x in range(start_i, end_i, 1):
            if S[j+y][i+x] == "#":
                num += 1

    return num


H,W = map(int, input().split())
S = []
for i in range(H):
    S.append(list(input()))

for y in range(H):
    for x in range(W):
        if S[y][x] == ".":
            num = get_count(S, H, W, x, y)
            S[y][x] = str(num)

for y in range(H):
    print("".join(S[y]))

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-

def get_count(S, H, W, i, j):
    num = 0
    start_i = start_j = -1
    end_i = end_j = 2
    if i == 0:
        start_i = 0
    elif i == W-1:
        end_i = 1
    if j == 0:
        start_j = 0
    elif j == H-1:
        end_j = 1

    for y in range(start_j, end_j, 1):
        for x in range(start_i, end_i, 1):
            if S[j+y][i+x] == "#":
                num += 1

    return num


H,W = map(int, input().split())
S = []
for i in range(H):
    S.append(list(input()))

for y in range(H):
    for x in range(W):
        if S[y][x] == ".":
            num = get_count(S, H, W, x, y)
            S[y][x] = str(num)

for y in range(H):
    print("".join(S[y]))
Problem p03730 (python)

Fixed submission:
# -*- coding: utf-8 -*-

A,B,C = map(int, input().split())

mod = []
for i in range(max(B,C)):
    res = A * i % B
    if res == C:
        ans = "YES"
        break
    if res in mod:
        ans = "NO"
        break
    mod.append(res)

print(ans)

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-

A,B,C = map(int, input().split())

ans = "NO"
for i in range(B+1)):
    res = A * i % B
    if res == C:
        ans = "YES"
        break

print(ans)
Problem p02603 (python)

Fixed submission:
#D
N=int(input())
A=list(map(int,input().split()))
B=[]

for i in range(N-1):
    if A[i+1]>A[i]:
        B.append(["up",i])
    elif A[i+1]<A[i]:
        B.append(["down",i])
        
C=[]
for i in range(len(B)-1):
    if B[i][0]==B[i+1][0]:
        continue
    C.append(B[i])
if B!=[]:
    C.append(B[-1])

while True:
    if C==[]:
        break
    if C[-1][0]=="down":
        C.pop(-1)
    else:
        break
        
kabu=0
money=1000
if C!=[]:
    if C[0][0]=="up":
        kabu+=money//A[0]
        money%=A[0]
for act in C:
    if act[0]=="up":
        money+=A[act[1]+1]*kabu
        kabu=0
    else:
        kabu+=money//A[act[1]+1]
        money%=A[act[1]+1]
print(money)

Buggy submission (verdict: Runtime Error):
#D
N=int(input())
A=list(map(int,input().split()))
B=[]

for i in range(N-1):
    if A[i+1]>A[i]:
        B.append(["up",i])
    elif A[i+1]<A[i]:
        B.append(["down",i])
        
C=[]
for i in range(len(B)-1):
    if B[i][0]==B[i+1][0]:
        continue
    C.append(B[i])
C.append(B[-1])

while True:
    if C==[]:
        break
    if C[-1][0]=="down":
        C.pop(-1)
    else:
        break
        
kabu=0
money=1000
if C!=[]:
    if C[0][0]=="up":
        kabu+=money//A[0]
        money%=A[0]
for act in C:
    if act[0]=="up":
        money+=A[act[1]+1]*kabu
        kabu=0
    else:
        kabu+=money//A[act[1]+1]
        money%=A[act[1]+1]
print(money)
Problem p02609 (python)

Fixed submission:
#D
N=int(input())
X=str(input())
   
CNT=0
for i in range(N):
    if X[i]=="1":
        CNT+=1
NUM=int(X,2)

to0_cnt=[0 for i in range(N)]
for i in range(1,N):
    ans=0
    num=i
    while True:
        cnt=0
        for j in bin(num)[2:]:
            if j=="1":
                cnt+=1
        if cnt==0:
            break
        r=num%cnt
        ans+=1
        if r==0:
            break
        num=r
    to0_cnt[i]=ans
                
R=[NUM%(CNT+1),NUM%(CNT-1) if CNT!=1 else 0]
for i in range(N):
    if X[i]=="0":
        cnt=CNT+1
        r=(R[0]+pow(2,N-i-1,cnt))%cnt
    else:
        cnt=CNT-1
        if NUM-pow(2,N-i-1)==0:
            print(0)
            continue
        r=(R[1]-pow(2,N-i-1,cnt))%cnt
    
    ans=1+to0_cnt[r]
    print(ans)

Buggy submission (verdict: Runtime Error):
#D
N=int(input())
X=str(input())
   
CNT=0
for i in range(N):
    if X[i]=="1":
        CNT+=1
NUM=int(X,2)

to0_cnt=[0 for i in range(N)]
for i in range(1,N):
    ans=0
    num=i
    while True:
        cnt=0
        for j in bin(num)[2:]:
            if j=="1":
                cnt+=1
        if cnt==0:
            break
        r=num%cnt
        ans+=1
        if r==0:
            break
        num=r
    to0_cnt[i]=ans
                

R=[NUM%(CNT+1),NUM%(CNT-1)]
for i in range(N):
    if X[i]=="0":
        cnt=CNT+1
        r=(R[0]+2**(N-i-1))%cnt
    else:
        cnt=CNT-1
        r=(R[1]-2**(N-i-1))%cnt
    
    ans=1+to0_cnt[r]
    print(ans)

Buggy submission (verdict: Runtime Error):
#D
N=int(input())
X=str(input())
   
CNT=0
for i in range(N):
    if X[i]=="1":
        CNT+=1
NUM=int(X,2)

R=[NUM%(CNT+1),NUM%(CNT-1)]
for i in range(N):
    if X[i]=="0":
        cnt=CNT+1
        r=(R[0]+2**(N-i-1))%cnt
    else:
        cnt=CNT-1
        r=(R[1]-2**(N-i-1))%cnt
    
    ans=1
    while True:
        if r==0:
            print(ans)
            break

        num=r
        cnt=0
        for i in bin(num)[2:]:
            if i=="1":
                cnt+=1
        r=num%cnt
        ans+=1
Problem p02744 (python)

Fixed submission:
N=int(input())
S="a"
bottom=""
alpha="abcdefghijklmnopqrstuvwxyz"

for i in range(N):
    if i>25:
        bottom=bottom+"z"
    else:
        bottom=bottom+alpha[i]
    S=S+"a"

C=0
S=""
def F(S,loop,N,C):
    for i in range(0,loop):
        if N==C+1:
            print(S+bottom[i])
        else:
            if loop==i+1:
                loop+=1
            F(S+bottom[i],loop,N,C+1)
    return None
            
F(S,1,N,C)

Buggy submission (verdict: Runtime Error):
N=int(input())
S="a"
bottom=""
alpha="abcdefghijklmnopqrstuvwxyz"

for i in range(N):
    if i>25:
        bottom=bottom+"z"
    else:
        bottom=bottom+alpha[i]
    S=S+"a"
print(bottom)

C=0
S=""
def F(S,loop,N,C):
    for i in range(0,loop):
        if N==C+1:
            print(S+bottom[i])
        else:
            F(S+bottom[i],i+2,L,C+1)
    return None
            
F(S,1,L,C)
Problem p02802 (python)

Fixed submission:
#C
N,M=map(int,input().split())
re=[0 for i in range(N+1)]
P=[0 for i in range(N+1)]
x=0
y=0
for i in range(M):
    p,S=input().split()
    p=int(p)
    if re[p]=="AC":
        continue
    elif S=="WA":
        P[p]+=1
    elif S=="AC":
        re[p]="AC"
        y+=1
for i in range(1,N+1):
    if re[i]=="AC":
        x+=P[i]
print(y,x)

Buggy submission (verdict: Runtime Error):
#C
N,M=map(int,input().split())
re=[0 for i in range(N+1)]
P=[0 for i in range(N+1)]
x=0
y=0
for i in range(M):
    p,S=input().split()
    p=int(p)
    if re[p]=="AC":
        continue
    elif S=="WA":
        P[p]+=1
    elif S=="AC":
        re[p]="AC"
        y+=1
for i in range(1,N+1):
    if re[p]=="AC":
        x+=P[p]
print(y,x)
Problem p02803 (python)

Fixed submission:
#D
import queue
H,W=map(int,input().split())
P=[0 for i in range(H)]
for i in range(H):
    P[i]=str(input())
ans=0
for i in range(H*W):
    s=(i//W,i%W)
    if P[s[0]][s[1]]=="#":
        continue
    st=queue.Queue()
    st.put(s)
    log=[[-1 for i in range(W)] for j in range(H)]
    log[s[0]][s[1]]=0
    while not st.empty():
        y,x=st.get()
        for z,w in [(y+1,x),(y-1,x),(y,x+1),(y,x-1)]:
            if 0>z or 0>w:
                continue
            elif z>=H or w>=W:
                continue
            elif P[z][w]=="." and log[z][w]==-1:
                st.put((z,w))
                log[z][w]=log[y][x]+1
                ans=max(ans,log[z][w])
print(ans)

Buggy submission (verdict: Runtime Error):
#D
import queue
H,W=map(int,input().split())
P=[0 for i in range(H)]
for i in range(H):
    P[i]=str(input())

ans=0
for i in range(H*W):
    s=(i%W,i//W)
    if P[s[0]][s[1]]=="#":
        continue
    st=queue.Queue()
    st.put(s)
    log=[[-1 for i in range(W)] for j in range(H)]
    log[s[0]][s[1]]=0
    while not st.empty():
        x,y=st.get()
        for z,w in [(x+1,y),(x-1,y),(x,y+1),(x,y-1)]:
            if not (0<=z and 0<=w):
                continue
            elif not (z<W and w<H):
                continue
            elif P[z][w]=="." and log[z][w]==-1:
                st.put((z,w))
                log[z][w]=log[x][y]+1
                ans=max(ans,log[z][w])
print(ans)
Problem p02821 (python)

Fixed submission:
#E
from bisect import bisect_left
from itertools import accumulate

N,M=map(int,input().split())
A=list(map(int,input().split()))
A=sorted(A)
A_r=list(reversed(A))
B=[0]+list(accumulate(A_r))

def f(x):
    count=0
    for i in A:
        idx=bisect_left(A,x-i)
        count+=N-idx
    if count>=M:
        return True
    else:
        return False
    
MIN=0
MAX=2*10**5+1
while MAX-MIN>1:
    MID=(MIN+MAX)//2
    if f(MID):
        MIN=MID
    else:
        MAX=MID

ans=0
count=0
for i in A_r:
    idx=bisect_left(A,MIN-i)
    ans+=i*(N-idx)+B[N-idx]
    count+=N-idx
print(ans-(count-M)*MIN)

Buggy submission (verdict: Runtime Error):
N,M=map(int,input().split())
A=list(map(int,input().split()))
A=sorted(A,reverse=True)

Pair=[0 for i in range(N)]
for i in range(N-1):
    Pair[i]=A[i]+A[i+1]

print(A)

ans=0
cnt=0
mode=0
Next=[]
i=0
while i<M:
    if mode==0:
        ans+=2*A[cnt]
        mode=1
        Next.append([cnt,cnt+1])
        cnt+=1
        i+=1
    else:
        Max=0
        for j in Next:
            if Max<=A[j[0]]+A[j[1]]:
                Max=A[j[0]]+A[j[1]]
                p_cnt=j
        ans+=A[p_cnt[0]]+A[p_cnt[1]]
        i+=1
        
        if i<=M-1:
            ans+=A[p_cnt[0]]+A[p_cnt[1]]
            i+=1
        Next.remove(p_cnt)
        if p_cnt[1]+1<N:
            p_cnt[1]+=1
            Next.append(p_cnt)
            
        Max=max(Max,A[p_cnt[0]]+A[p_cnt[1]])
        if Max<=A[cnt]*2:
            mode=0
        
print(ans)
Problem p03161 (python)

Fixed submission:
#2
def chmin(a,b):
    if a>b:
        return b
    else:
        return a

N,K=map(int,input().split())
H=[int(x) for x in 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]=chmin(abs(H[i+j]-H[i])+dp[i],dp[i+j])
        else:
            break
print(dp[-1])

Buggy submission (verdict: Runtime Error):
#2
N,K=map(int,input().split())
H=[int(x) for x in input().split()]

diff=[0]*(K+1)
dp=[0]*N


for i in range(1,N):
    
    for k in range(1,K+1):
        if i-k>-1:
            diff[k]=abs(H[i]-H[i-k])
        else:
            break
    
    Min=diff[1]
    for k in range(2,min(i+1,K+1)):
        if diff[k]<Min:
            Min=diff[k]
            Min_p=k

    dp[i]=dp[i-Min_p]+Min
print(dp[-1])

Buggy submission (verdict: Runtime Error):
#2
N,K=map(int,input().split())
H=[int(x) for x in input().split()]

diff=[0]*(K+1)
dp=[0]*N


for i in range(1,N):
    
    for k in range(1,K+1):
        if i-k>-1:
            diff[k]=abs(H[i]-H[i-k])
        else:
            break
    
    Min=diff[1]
    for k in range(2,min(i+1,K+1)):
        if diff[k]<Min:
            Min=diff[k]
            Min_p=k

    dp[i]=dp[i-Min_p]+Min
print(dp[-1])