Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
n,m = map(int,input().split())

inf = float("inf")
ans = [inf]*n**2
for _ in range(m):
    a,b = map(int,input().split())
    C = list(map(int,input().split()))
    res = 0
    for c in C:
        res |= 2**(c-1)
    ans[res] = a

aa = []
for i,a in enumerate(ans):
    if a == inf:continue
    aa.append([i,a])

for i,a in aa:
    for j,b in aa:
        ans[i|j] = min(ans[i|j],a+b)
print(ans[-1])

Fixed submission:
n,m = map(int,input().split())
vals = []
inf = float("inf")
res = [inf]*2**n

for i in range(m):
	a,b = map(int,input().split())
	C = list(map(int,input().split()))
	cur = 0
	for c in C:
		cur |= 2**(c-1)
	res[cur] = min(res[cur],a)

ans = []
for i,r in enumerate(res):
	if res[i] == inf:
		continue
	ans.append([i,r])


for i,a in ans:
	for j in range(2**n):
		res[i|j] = min(res[i|j],res[i]+res[j])

if res[-1] == inf:
	print(-1)
else:
	print(res[-1])

Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
vals = []
inf = float("inf")
ans = [inf]*2**n

for i in range(m):
	a,b = map(int,input().split())
	C = list(map(int,input().split()))
	cur = 0
	for c in C:
		cur += 2**(c-1)
	ans[cur] = min(ans[cur],a)

for i,a in enumerate(ans):
	if a == inf: continue
	for j,b in enumerate(ans):
		if b == inf: continue
		ans[i|j] = min(ans[i|j],ans[i]+ans[j])

if ans[-1] == inf:
	print(-1)
else:
	print(ans[-1])

Fixed submission:
n,m = map(int,input().split())
vals = []
inf = float("inf")
res = [inf]*2**n

for i in range(m):
	a,b = map(int,input().split())
	C = list(map(int,input().split()))
	cur = 0
	for c in C:
		cur |= 2**(c-1)
	res[cur] = min(res[cur],a)

ans = []
for i,r in enumerate(res):
	if res[i] == inf:
		continue
	ans.append([i,r])


for i,a in ans:
	for j in range(2**n):
		res[i|j] = min(res[i|j],res[i]+res[j])

if res[-1] == inf:
	print(-1)
else:
	print(res[-1])

Buggy python submission (verdict: Time Limit Exceeded):
from itertools import combinations,permutations

n,m = map(int,input().split())
prices,keys = [],[]
for i in range(m):
	a,b = map(int,input().split())
	C = list(map(int,input().split()))

	prices.append(a)
	cur_key = 0
	for c in C:
		cur_key += 2**(c-1)
	keys.append(cur_key)
ans_max = 2**n-1
cnt = 0
ans = float("inf")
for i in range(1,m+1):
	for comb in combinations(range(0,m),r=i):
		# print(comb)
		price = 0
		cur_key = 0
		for c in comb:
			price += prices[c]
			cur_key |= keys[c]
		# print(price,cur_key)
		if cur_key == ans_max and price < ans:
			ans = price
if ans == float("inf"):
	ans = -1
print(ans)

Fixed submission:
n,m = map(int,input().split())
vals = []
inf = float("inf")
res = [inf]*2**n

for i in range(m):
	a,b = map(int,input().split())
	C = list(map(int,input().split()))
	cur = 0
	for c in C:
		cur |= 2**(c-1)
	res[cur] = min(res[cur],a)

ans = []
for i,r in enumerate(res):
	if res[i] == inf:
		continue
	ans.append([i,r])


for i,a in ans:
	for j in range(2**n):
		res[i|j] = min(res[i|j],res[i]+res[j])

if res[-1] == inf:
	print(-1)
else:
	print(res[-1])
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
ans = n%1
for i in range(2,n+1):
    ans += (i-1)%i
    # print(i-1,i,ans)
print(ans)

Fixed submission:
n = int(input())
print(n*(n-1)//2)
Buggy python submission (verdict: Time Limit Exceeded):
n,q = map(int,input().split())
ab = [list(map(int,input().split())) for i in range(n-1)]
px = [list(map(int,input().split())) for i in range(q)]

child = [[] for _ in range(n)]
for a,b in ab:
    a -= 1
    b -= 1
    child[a].append(b)
    child[b].append(a)
# print(child)

p_val = [0] * n
for p,x in px:
    p_val[p-1] += x

ans = [p_val[0]]+[0]*(n-1)
stack = [0]
visited = [0] * n
while stack:
    s = stack.pop()
    visited[s] = 1
    for c in child[s]:
        if visited[c]:
            continue
        stack+=[c]
        ans[c] = ans[s]+p_val[c]
print(*ans)



Fixed submission:
n,q = map(int,input().split())
ab = [list(map(int,input().split())) for i in range(n-1)]
px = [list(map(int,input().split())) for i in range(q)]

child = [[] for _ in range(n)]
for a,b in ab:
    a -= 1
    b -= 1
    child[a].append(b)
    child[b].append(a)
# print(child)

p_val = [0] * n
for p,x in px:
    p_val[p-1] += x

ans = [p_val[0]]+[0]*(n-1)
stack = [0]
visited = [0] * n
while stack:
    s = stack.pop()
    visited[s] = 1
    for c in child[s]:
        if visited[c]:
            continue
        stack+=[c]
        ans[c] = ans[s]+p_val[c]
print(*ans)
Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter

n = int(input())
S = [input() for _ in range(n)]

char_list = []

for s in S:
	char_list.append(Counter(s))

ans = 0
for i in range(n-1):
	for j in range(i+1,n):
		if char_list[i] == char_list[j]:
			ans += 1
print(ans)

Fixed submission:
n = int(input())
S = [input() for i in range(n)]

ans = 0
cnt = {}

for s in S:
    s = "".join(sorted(s))
    if s in cnt:
        ans += cnt[s]
        cnt[s] += 1
    else:
        cnt[s] = 1

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
from heapq import heappush,heappop

n,m = map(int,input().split())
ab = []
for i in range(n):
    a,b = map(int,input().split())
    if a <= m:
        ab.append([a,b])
ab.sort()

stock = []
ans = 0
for i in range(1,m+1):
    while ab and ab[0][0] <= i:
        a = ab.pop(0)
        heappush(stock,-a[1])
    if stock:
        ans += -heappop(stock)

print(ans)

Fixed submission:
import sys
from heapq import heappush,heappop

n,m = map(int,input().split())
ab = []
for line in sys.stdin:
    a,b = map(int,line.split())
    if a <= m:
        ab.append([a,b])
ab.sort()

stock = []
ans = 0
for i in range(1,m+1):
    while ab and ab[0][0] <= i:
        a = ab.pop(0)
        heappush(stock,-a[1])
    if stock:
        ans += -heappop(stock)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from heapq import heappush,heappop

n,m = map(int,input().split())
ab = [list(map(int,input().split())) for i in range(n)]
ab.sort()

stock = []
ans = 0
for i in range(1,m+1):
    while ab and ab[0][0] <= i:
        a = ab.pop(0)
        heappush(stock,-a[1])
    if stock:
        ans += -heappop(stock)

print(ans)

Fixed submission:
import sys
from heapq import heappush,heappop

n,m = map(int,input().split())
ab = []
for line in sys.stdin:
    a,b = map(int,line.split())
    if a <= m:
        ab.append([a,b])
ab.sort()

stock = []
ans = 0
for i in range(1,m+1):
    while ab and ab[0][0] <= i:
        a = ab.pop(0)
        heappush(stock,-a[1])
    if stock:
        ans += -heappop(stock)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from heapq import heappush,heappop

n,m = map(int,input().split())
ab = [list(map(int,input().split())) for i in range(n)]
ab.sort()

stock = []
ans = 0
for i in range(1,m+1):
    while ab and ab[0][0] <= i:
        a = ab.pop(0)
        heappush(stock,-a[1])
    if stock:
        ans += -heappop(stock)

print(ans)

Fixed submission:
import sys
from heapq import heappush,heappop

n,m = map(int,input().split())
ab = []
for line in sys.stdin:
    a,b = map(int,line.split())
    if a <= m:
        ab.append([a,b])
ab.sort()

stock = []
ans = 0
for i in range(1,m+1):
    while ab and ab[0][0] <= i:
        a = ab.pop(0)
        heappush(stock,-a[1])
    if stock:
        ans += -heappop(stock)

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

n = int(input())
A = list(map(int,input().split()))
S = sum(A)
ans = []
for i in range(1,n+1):
    aaa = A[i:]+A[:i]
    # print(aaa[:-1:2])
    x = S-(sum(aaa[:-1:2]))*2
    ans.append(x)

[print(a) for a in ans]

Fixed submission:
import numpy as np

n = int(input())
A = list(map(int,input().split()))
S = sum(A)

x = S-sum(A[1:-1:2])*2
print(x)
for a in A[:-1]:
    x = (a-x//2)*2
    print(x)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int,input().split()))

def gcd(a,b):
    if b == 0:
        return a    
    return gcd(b,a%b)

ans = 0
B = []
for i in range(N):
    b = [A[i],A[i-1]]
    B.append(gcd(max(b),min(b)))
for i in range(-1,-N-1,-1):
    if N == 2:
        ans = max(A)
        break
    while 1:
        C = B.copy()
        C.pop(i)
        C.pop(i-(-1))
        c = []
        for j in range(len(C)):
            c.append(gcd(C[j],C[j-1]))
        if c == C:
            ans = max(ans,max(c))
            break
        C = c
print(ans)

Fixed submission:
N = int(input())
A = list(map(int,input().split()))

def gcd(a,b):
    if b == 0:
        return a
    return gcd(b,a%b)

L,R = [A[0]],[A[-1]]
Lg = A[0]
Rg = A[-1]
for i in range(N-1):
    Lg = gcd(Lg,A[i+1])
    L.append(Lg)
for i in range(N-1,0,-1):
    Rg = gcd(Rg,A[i-1])
    R.append(Rg)
L.append(A[-1])
R.append(A[0])

res = 0
for i in range(N):
    if i == 0:
        res = max(res,R[-3])
    elif i == N-1:
        res = max(res,L[-3])
    else:
        res = max(res,gcd(L[i-1],R[-i-3]))
        
print(res)

Buggy python submission (verdict: Runtime Error):
import math
import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int,input().split()))
L = [math.gcd(A[-1],A[-2])]
flag = 0
for i in range(N-2,-1,-1):
    L.append(math.gcd(A[i],A[i-1]))
    flag += 1 if L[-1]%L[-2] != 0 else 0
if flag >= 2:
    L2 = []
    for j in range(len(L)-1,-1,-1):
        L2.append(math.gcd(L[i],L[i-1]))
    L = L2
print(max(L))

Fixed submission:
N = int(input())
A = list(map(int,input().split()))

def gcd(a,b):
    if b == 0:
        return a
    return gcd(b,a%b)

L,R = [A[0]],[A[-1]]
Lg = A[0]
Rg = A[-1]
for i in range(N-1):
    Lg = gcd(Lg,A[i+1])
    L.append(Lg)
for i in range(N-1,0,-1):
    Rg = gcd(Rg,A[i-1])
    R.append(Rg)
L.append(A[-1])
R.append(A[0])

res = 0
for i in range(N):
    if i == 0:
        res = max(res,R[-3])
    elif i == N-1:
        res = max(res,L[-3])
    else:
        res = max(res,gcd(L[i-1],R[-i-3]))
        
print(res)

Buggy python submission (verdict: Runtime Error):
import math
import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int,input().split()))
L = [0]*(N-3)
for i in range(N-1,-1,-1):
    L.append(math.gcd(A[i],A[i-1]))
if L.count(min(L)) == 2 and len(L) > 2:
    L.sort()
    ans = L[2]
else:
    ans = L[0]
print(ans)

Fixed submission:
N = int(input())
A = list(map(int,input().split()))

def gcd(a,b):
    if b == 0:
        return a
    return gcd(b,a%b)

L,R = [A[0]],[A[-1]]
Lg = A[0]
Rg = A[-1]
for i in range(N-1):
    Lg = gcd(Lg,A[i+1])
    L.append(Lg)
for i in range(N-1,0,-1):
    Rg = gcd(Rg,A[i-1])
    R.append(Rg)
L.append(A[-1])
R.append(A[0])

res = 0
for i in range(N):
    if i == 0:
        res = max(res,R[-3])
    elif i == N-1:
        res = max(res,L[-3])
    else:
        res = max(res,gcd(L[i-1],R[-i-3]))
        
print(res)
Buggy python submission (verdict: Runtime Error):
S = input()
def func():
    from collections import Counter as c
    ev,od = [],[]
    for i in range(len(S)):
        ev.append(S[i]) if i%2 == 0 else 0
        od.append(S[i]) if i%2 == 1 else 0
    ev = c(ev).most_common()
    od = c(od).most_common()
    if len(od) == 0:
        return 0
    if ev[0][0] != od[0][0]:
        return len(S)-ev[0][1]-od[0][1]
    else:
        if len(ev) == 1 or len(od) == 1:
            return len(S)//2
        else:
            return max(ans,len(S)-ev[0][1]-od[1][1],len(S)-ev[1][1]-od[0][1])
print(func())

Fixed submission:
S = input()
from collections import Counter as c

od,ev = [],[]#od=奇数 ev=偶数
for s,i in zip(S,range(1,len(S)+1)):
    if i%2 == 0:
        ev.append(s)
    else:
        od.append(s)
odd = c(od).most_common(2)
eve = c(ev).most_common(2)
N = len(S)
N,odd,eve
if len(odd) != 2:
    odd.append(('9',0))
if len(eve) != 2:
    eve.append(('9',0))
if N == 1:
    ans = 0
elif odd[0][0] != eve[0][0]:
    ans = N-odd[0][1]-eve[0][1]
else:
    if len(odd) == len(eve) == 1:
        ans = N//2
    else:
        ans = N-max(odd[0][1]+eve[1][1],odd[1][1]+eve[0][1])
print(ans)
Buggy python submission (verdict: Runtime Error):
a = [int(input()) for _ in range(5)]
ans = 0
for i in a:
    ans += i if i%10 == 0 else i+(10-i%10)
    mod = max(mod,i%10) if i%10 == 0 else max(mod,10-i%10)
print(ans-mod)

Fixed submission:
A = input()
B = input()
C = input()
D = input()
E = input()
ABC = [A,B,C,D,E]
sort_abc = []
time = 0
m = 9
for abc in ABC:
    if m < int(abc[-1]) or abc[-1] == '0':
        sort_abc.insert(0,abc)
    else:
        m = int(abc[-1])
        sort_abc.append(abc)
sort_abc = list(map(int,sort_abc))
for i in range(5):
    abc = sort_abc[i]
    if i == 4:
        time += abc
    else:
        if abc%10 == 0:
            time += abc
        elif abc%10 != 0 and abc >= 10:
            time += abc + (10-abc%10)
        else:
            time += 10       
print(time)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = int(input())
B = int(input())
C = int(input())
D = int(input())
E = int(input())
ABC = [N,0,0,0,0]
time = 0
flag = 0
i = 0
while ABC[4] > 0 or flag == 0:
    if i >= 4:
        if ABC[4] >= E:
            ABC[4]-=E
        else:
            ABC[4]=0
    if i >= 3:
        if ABC[3] >= D:
            ABC[3]-=D
            ABC[4]+=D
            flag = 1
        else:
            ABC[4]+=ABC[3]
            flag = 1
            ABC[3]=0
    if i >= 2:
        if ABC[2] >= C:
            ABC[2]-=C
            ABC[3]+=C
        else:
            ABC[3]+=ABC[2]
            ABC[2]=0
    if i >= 1:
        if ABC[1] >= B:
            ABC[1]-=B
            ABC[2]+=B
        else:
            ABC[2]+=ABC[1]
            ABC[1]=0
    if ABC[0] >= A:
        ABC[0]-=A
        ABC[1]+=A
    else:
        ABC[1]+=ABC[0]
        ABC[0]=0
        
        
    time += 1
    i += 1
print(time)

Fixed submission:
N = int(input())
A = int(input())
B = int(input())
C = int(input())
D = int(input())
E = int(input())
if N%min(A,B,C,D,E) == 0:z = 4
else:z = 5
print(int(z+N/min(A,B,C,D,E)))

Buggy python submission (verdict: Runtime Error):
ABC = [N,0,0,0,0]
time = 0
flag = 0
i = 0
while ABC[4] > 0 or flag == 0:
    print(ABC)
    if i >= 4:
        if ABC[4] >= E:
            ABC[4]-=E
        else:
            ABC[4]=0
    if i >= 3:
        if ABC[3] >= D:
            ABC[3]-=D
            ABC[4]+=D
            flag = 1
        else:
            ABC[4]+=ABC[3]
            flag = 1
            ABC[3]=0
    if i >= 2:
        if ABC[2] >= C:
            ABC[2]-=C
            ABC[3]+=C
        else:
            ABC[3]+=ABC[2]
            ABC[2]=0
    if i >= 1:
        if ABC[1] >= B:
            ABC[1]-=B
            ABC[2]+=B
        else:
            ABC[2]+=ABC[1]
            ABC[1]=0
    if ABC[0] >= A:
        ABC[0]-=A
        ABC[1]+=A
    else:
        ABC[1]+=ABC[0]
        ABC[0]=0
        
        
    time += 1
    i += 1
print('end',ABC)
print(time)

Fixed submission:
N = int(input())
A = int(input())
B = int(input())
C = int(input())
D = int(input())
E = int(input())
if N%min(A,B,C,D,E) == 0:z = 4
else:z = 5
print(int(z+N/min(A,B,C,D,E)))
Buggy python submission (verdict: Time Limit Exceeded):
X,Y,Z,K = map(int,input().split())
abc= [list((map(int,input().split()))) for _ in range(3)]
from itertools import product as pro
ans = []
for a in pro(abc[0],abc[1],abc[2]):
    ans.append(int(a[0])+int(a[1])+int(a[2]))
ans.sort(reverse=1)
for i in range(K):
    print(ans[i])

Fixed submission:
*XYZ,K = map(int,input().split())
ABC = [list(map(int,input().split())) for i in range(3)]
for a in ABC:
    a.sort(reverse=1)
import heapq
ans = [0]*K
for a in ABC[0]:
    for b in ABC[1]:
        for c in ABC[2]:
            if a+b+c>ans[0]:
                heapq.heappush(ans,a+b+c)
                heapq.heappop(ans)
            else:
                break
ans.sort(reverse=1)
for i in range(K):
    print(ans[i])
Buggy python submission (verdict: Runtime Error):
import sys
N,Q = map(int,input().split())
if N > Q:
    max = N
else:
    max = Q
if 1 > max or max > 2*10^5:
    sys.exit()
s = input()
if s.islower() or len(s) != N:
    sys.exit()
mass_list,mond_list = [mass for mass in s],['1' for hoge in range(len(s))]
for i in range(Q):
    index_list = []
    index_num = 0
    t,d = input().split()
    if t.islower():
        sys.exit()
    if d == 'R':
        d = 1
    elif d == 'L':
        d = -1
    else:
        sys.exit()
    for mass in mass_list:
        if mass == t:
            index_list.append(index_num)
        index_num += 1
    if d == 1:
        index_list.reverse()
    for index_num in index_list:
        if mons_list[index_num] > 0:
            if 0 > index_num+d or index_num+d > N-1:
                next
            else:
                mons_list[index_num+d] += mons_list[index_num]
            mons_list[index_num] = 0
print(sum(mons_list))

Fixed submission:
N,Q = map(int,input().split())
S = '_'+input()+'_'
td = [input().split() for _ in range(Q)]
R = N+1
L = 0
for i in range(Q-1,-1,-1):
    t,d = td[i]
    if t == S[R-1] and d == 'R':
        R -= 1
    elif t == S[R] and d == 'L':
        R += 1
    if t == S[L+1] and d == 'L':
        L += 1
    elif t == S[L] and d == 'R':
        L -= 1
print(max(0,R-L-1))
Buggy python submission (verdict: Runtime Error):
from collections import defaultdict

N,M = map(int,input().split())
AB =defaultdict(list)
total = 0
for _ in range(N):
    A,B = map(int,input().split())
    AB[A].append(B)
while M > 0:
    minAB = AB[min(AB)]
    if minAB[0] <= M:
        #print('あと{}'.format(M))
        M -= minAB[0]
        total += min(AB) * minAB[0]
        del AB[min(AB)]
    else:
        #print('残り{}'.format(M))
        total += min(AB) * M
        M -= M
print(total)

Fixed submission:
N,M = map(int,input().split())
AB = [list(map(int,input().split())) for _ in range(N)]
AB.sort()
ans = 0
while M > 0:
    ab = AB[0]
    if ab[1] <= M:
        M-= ab[1]
        ans += ab[0] * ab[1]
        AB.pop(0)
    else:
        ans += ab[0] * M
        M -= M
print(ans)
Buggy python submission (verdict: Runtime Error):
A,B = map(int,input().split())
start = time.time()
if 0 <= A <= B <= 10**12:
    #A,B = bin(A),bin(B)
    total = A
    a,b,c = A+1,B+1,1
    for i in range(a,b,c):
        total ^= i
    print(total)

Fixed submission:
def xor(x):
    res = [x,1,x+1,0]
    return res[x%4]

A,B = map(int,input().split())
print(xor(A-1)^xor(B))

Buggy python submission (verdict: Time Limit Exceeded):
A,B = map(int,input().split())
#A,B = bin(A),bin(B)
total = A
for i in range(A+1,B+1,1):
    total = total^i
print(total)

Fixed submission:
def xor(x):
    res = [x,1,x+1,0]
    return res[x%4]

A,B = map(int,input().split())
print(xor(A-1)^xor(B))
Buggy python submission (verdict: Runtime Error):
S = input()
ans = 0
while i < len(S):
    if any(['01' in S[i:i+2],'10' in S[i:i+2]]):
        S = S[:i]+S[i+2:]
        ans += 2
        i -= 1
    else:
        i += 1
print(ans)

Fixed submission:
S = input()
i = 0
total = 0
while i < len(S):
    if S[i:i+2] == '01'or S[i:i+2] == '10':
        Sa = S[:i]
        Sb = S[i+2:]
        S = Sa+Sb
        total += 1
        i -= 1
    else:
        i += 1
print(total*2)

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
ans = 0
while any(['01' in S,'10' in S]):
    ans += S.count('01')
    S = S.replace('01','')
    ans += S.count('10')
    S = S.replace('10','')
print(ans*2)

Fixed submission:
S = input()
i = 0
total = 0
while i < len(S):
    if S[i:i+2] == '01'or S[i:i+2] == '10':
        Sa = S[:i]
        Sb = S[i+2:]
        S = Sa+Sb
        total += 1
        i -= 1
    else:
        i += 1
print(total*2)

Buggy python submission (verdict: Runtime Error):
i = 0
total = 0
while i < len(S):
    if S[i:i+2] == '01'or S[i:i+2] == '10':
        Sa = S[:i]
        Sb = S[i+2:]
        S = Sa+Sb
        total += 1
        i -= 1
    else:
        i += 1
print(total*2)

Fixed submission:
S = input()
i = 0
total = 0
while i < len(S):
    if S[i:i+2] == '01'or S[i:i+2] == '10':
        Sa = S[:i]
        Sb = S[i+2:]
        S = Sa+Sb
        total += 1
        i -= 1
    else:
        i += 1
print(total*2)
Buggy python submission (verdict: Runtime Error):
N,M = map(int,input().split())
X = sorted(map(int,input().split()))
L = sorted([X[i+1]-X[i] for i in range(M-1)])
if M < L:print(0)
else:print(sum(L[:(M-N)]))

Fixed submission:
N,M = map(int,input().split())
X = sorted(map(int,input().split()))
L = sorted([X[i+1]-X[i] for i in range(M-1)])
if M < N:print(0)
else:print(sum(L[:(M-N)]))
Buggy python submission (verdict: Runtime Error):
i = 0
ans = 0
while True:
    if i >= len(S):
        #print('終わり！')
        break
    for j in range(i+1,len(S)+1):
        #print(S[i:j])
        a = max(S[i:j])
        if j >= len(S):
            #print(1,S[i:j],i,j,ans)
            ans += max(S[i:j])
            i += j
            break
        elif a <= S[j]:
            #print(2,i,j,ans)
            continue
        else:
            #print(3,S[i:j],i,j,ans)
            ans += max(S[i:j])
            ans -= S[j]
            i = j
            break
print(ans)

Fixed submission:
input()
S = list(map(int,input().split()))
i = 0
ans = 0
while True:
    if i >= len(S):
        #print('終わり！')
        break
    for j in range(i+1,len(S)+1):
        #print(S[i:j])
        a = max(S[i:j])
        if j >= len(S):
            #print(1,S[i:j],i,j,ans)
            ans += max(S[i:j])
            i += j
            break
        elif a <= S[j]:
            #print(2,i,j,ans)
            continue
        else:
            #print(3,S[i:j],i,j,ans)
            ans += max(S[i:j])
            ans -= S[j]
            i = j
            break
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N,K = map(int,input().split())
H = [10**5]*K+list(map(int,input().split()))
L = [0]*(N+K)
for i in range(K+1,N+K):
    #print(i)
    res = abs(H[i]-H[i-1])+L[i-1]
    for j in range(i-1,i-K-1,-1):
        #print('j={}'.format(j))
        res = min(res,abs(H[j]-H[i])+L[j])
    L[i] = res
print(L[N+K-1])

Fixed submission:
N,K = map(int,input().split())
H = [10**5]*K+list(map(int,input().split()))
L = [0]*(N+K)
for i in range(K+1,N+K):
    #print(i)
    res = abs(H[i]-H[i-1])+L[i-1]
    for j in range(i-1,i-K-1,-1):
        #print('j={}'.format(j))
        res = min(res,abs(H[j]-H[i])+L[j])
    L[i] = res
print(L[N+K-1])

Buggy python submission (verdict: Time Limit Exceeded):
N,K = map(int,input().split())
H = [10**5]*K+list(map(int,input().split()))
L = [0]*(N+K)
for i in range(K+1,N+K):
    #print(i)
    res = abs(H[i]-H[i-1])+L[i-1]
    for j in range(i-1,i-K-1,-1):
        #print('j={}'.format(j))
        res = min(res,abs(H[j]-H[i])+L[j])
    L[i] = res
print(L[N+K-1])

Fixed submission:
N,K = map(int,input().split())
H = [10**5]*K+list(map(int,input().split()))
L = [0]*(N+K)
for i in range(K+1,N+K):
    #print(i)
    res = abs(H[i]-H[i-1])+L[i-1]
    for j in range(i-1,i-K-1,-1):
        #print('j={}'.format(j))
        res = min(res,abs(H[j]-H[i])+L[j])
    L[i] = res
print(L[N+K-1])
Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline()
N = int(input())
abc = [[0,0,0]]+[list(map(int,input().split())) for _ in range(N)]
dp = [[0,0,0] for _ in range(N+1)]

for i in range(1,N+1):
    for j in range(3):
        for k in range(3):
            if j == k:continue
            #print('i={},j={},k={}'.format(i,j,k))
            #print(dp[i][j],abc[i][j],abc[i-1][k])
            dp[i][j] = max(dp[i][j],abc[i][j]+dp[i-1][k])
print(max(dp[-1]))

Fixed submission:
import sys
input = sys.stdin.readline
N = int(input())
abc = [[0,0,0]]+[list(map(int,input().split())) for _ in range(N)]
dp = [[0,0,0] for _ in range(N+1)]

for i in range(1,N+1):
    for j in range(3):
        for k in range(3):
            if j == k:continue
            #print('i={},j={},k={}'.format(i,j,k))
            #print(dp[i][j],abc[i][j],abc[i-1][k])
            dp[i][j] = max(dp[i][j],abc[i][j]+dp[i-1][k])
print(max(dp[-1]))