Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
l = []
for i in range(n):
    l.append([a[i],i])
l.sort()
ans = 0
for i in range(n-1):
    for j in range(i+1,n):
        if l[i][0]+l[j][0] >= n:
            break
        if l[i][0]+l[j][0] == abs(l[i][1]-l[j][1]):
            ans+=1
print(ans)


Fixed submission:
n = int(input())
a = list(map(int, input().split()))
l1 = []
l2 = []
for i in range(n):
    l1.append(a[i]+i)
    l2.append(-a[i]+i)
j = {}
ans = 0
for i in l1:
    if i in j:
        j[i]+= 1
    else:
        j[i] = 1
for i in l2:
    if i in j:
        ans+=j[i]
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
s = input()
r = []
g = []
b = []
for i in range(n):
    if s[i] == "R":
        r.append(i)
    elif s[i] == "G":
        g.append(i)
    else:
        b.append(i)
ans = len(r)*len(g)*len(b)
for i in r:
    for j in g:
        a = max(i,j)
        u = min(i,j)
        if a*2-u >= 0 and a*2-u < n :
            if s[a*2-u] == 'B':
                ans -= 1
        if 2*u-a >= 0 and 2*u-a < n:
            if s[2*u-a] == 'B':
                ans -= 1
        if (a-u)%2 == 0 and u+(a-u)/2 >= 0 and u+(a-u)/2 < n:
            if s[u+(a-u)//2] == 'B':
                ans -= 1
print(ans)


Fixed submission:
n = int(input())
s = input()
r = []
g = []
b = []
for i in range(n):
    if s[i] == "R":
        r.append(i)
    elif s[i] == "G":
        g.append(i)
    else:
        b.append(i)
ans = len(r)*len(g)*len(b)
for i in b:
    for j in g:
        a = max(i,j)
        u = min(i,j)
        if a*2-u >= 0 and a*2-u < n :
            if s[a*2-u] == 'R':
                ans -= 1
        if 2*u-a >= 0 and 2*u-a < n:
            if s[2*u-a] == 'R':
                ans -= 1
        if (a-u)%2 == 0 and u+(a-u)/2 >= 0 and u+(a-u)/2 < n:
            if s[u+(a-u)//2] == 'R':
                ans -= 1
print(ans)
Buggy python submission (verdict: Runtime Error):
a = list(map(int,input().split()))
sort(a)
if (a[0] == a[1] and a[1] != a[2]) or (a[0] != a[1] and a[1] == a[2]):
  print("Yes")
else:
  print("No")

Fixed submission:
a = list(map(int,input().split()))
a.sort()
if (a[0] == a[1] and a[1] != a[2]) or (a[0] != a[1] and a[1] == a[2]):
  print("Yes")
else:
  print("No")
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
l = [int(input()) for i in range(n)]
a = [0]
ma = 0
for i in range(len(l)):
    if l[i] > ma:
        ma = l[i]
    a.append(ma)
a.append(0)
b = [0]
ma = 0
for i in range(len(l)):
    if l[-i-1] > ma:
        ma = l[-i-1]
    b.insert(0,ma)
b.insert(0,0)
#print(a,b)
for i in range(n):
    print(max(a[i],b[i+2]))


Fixed submission:
n = int(input())
l = [int(input()) for i in range(n)]
a = [0]
ma = 0
for i,j in enumerate(l):
    if j > ma:
        ma = j
    a.append(ma)
a.append(0)
b = [0]
ma = 0
for i,j in enumerate(reversed(l)):
    if j > ma:
        ma = j
    b.append(ma)
b.append(0)
#print(a,b)
for i in range(n):
    print(max(a[i],b[-i-3]))
Buggy python submission (verdict: Runtime Error):
n,k = map(int,input().split())
l = [1,1]
for i in range(2,2000):
    l.append(l[i-1]*i)
def f(i):
    global n,k,l
    if i == 1:
        return n-k+1
    else:
        tiri = l[k-1]//l[i-1]*l[k-i]
        aka = n - k
        aka = aka - i + 1
        akatiri = l[i + aka ] // (l[aka] * l[i])
        #print(tiri,akatiri)
        return tiri * akatiri
for i in range(1,k+1):
    print(f(i)%(10**9 + 7))


Fixed submission:
n,k = map(int,input().split())
l = [1,1]
for i in range(2,2000):
    l.append(l[i-1]*i)
def f(i):
    global n,k,l
    if i == 1:
        return n-k+1
    elif i - 1 > n - k :
        return 0
    else:
        tiri = l[k-1]//(l[i-1]*l[k-i])
        aka = n - k
        aka = aka - i + 1
        akatiri = l[i + aka ] // (l[aka] * l[i])
        #print(tiri,akatiri)
        return tiri * akatiri
for i in range(1,k+1):
    print(f(i)%(10**9 + 7))
Buggy python submission (verdict: Runtime Error):
n, k= map(int,input().split())
if k > (n-1)+(n-2):
    print(-1)
elif n == 2:
    if k == 0:
        print(1,2)
    else:
        print(-1)
else:
    for i in range(2,n+1):
        print(1,i)
    ima = (n-1)*(n-2) / 2
    #print(ima)
    hajime = 2
    owari = 3
    while(ima > k):
        print(hajime ,owari)
        ima -= 1
        if(owari < n):
            owari += 1
        else:
            hajime += 1
            owari = hajime + 1


Fixed submission:
#n,q= map(int,input().split())
s = input()
a = 0
for i in range(len(s)-1):
    if s[i] == s[i+1] :
        print("Bad")
        break
else:
    print("Good")
Buggy python submission (verdict: Runtime Error):
import math
a,b,c,d= map(int,input().split())
count = 0
kou = (c*d) // math.gcd(c,d)
#print(kou)
if a % kou == 0:
    hajime = a
else :
    hajime = (kou - a % kou) + a
owari = b - (b % kou)
if a % c == 0:
    chajime = a
else :
    chajime = (c - a % c) + a
cowari = b - (b % c)
if a % d == 0:
    dhajime = a
else :
    dhajime = (d - a % d) + a
dowari = b - (b % d)


kousuu = ((cowari - chajime) // c + 1) + ((dowari - dhajime) // d + 1) - ((owari - hajime) // kou + 1)
sa = b - a + 1
print(sa - (kousuu))
#print(hajime,owari,chajime,cowari,dhajime,dowari,)


Fixed submission:
import fractions
a,b,c,d= map(int,input().split())
count = 0
kou = (c*d) // fractions.gcd(c,d)
#print(kou)
if a % kou == 0:
    hajime = a
else :
    hajime = (kou - a % kou) + a
owari = b - (b % kou)
if a % c == 0:
    chajime = a
else :
    chajime = (c - a % c) + a
cowari = b - (b % c)
if a % d == 0:
    dhajime = a
else :
    dhajime = (d - a % d) + a
dowari = b - (b % d)


kousuu = ((cowari - chajime) // c + 1) + ((dowari - dhajime) // d + 1) - ((owari - hajime) // kou + 1)
sa = b - a + 1
print(sa - (kousuu))
#print(hajime,owari,chajime,cowari,dhajime,dowari,)
Buggy python submission (verdict: Runtime Error):
n,m = map(int, input().split())
l = []
for i in range(n):
    a = input()
    a.split()
    l.append(a)
def f(x,y):
    global l ,n,m
    ans = -3
    if l[x][y] == "#":
        return 0
    else :
        for i in range(x,n):
            if l[i][y] == ".":
                ans += 1
            else :
                break
        for i in range(x+1):
            if l[x-i][y] == ".":
                ans += 1
            else :
                break
        for i in range(y,m):
            if l[x][i] == ".":
                ans += 1
            else :
                break
        for i in range(y+1):
            if l[x][y-i] == ".":
                ans += 1
            else:
                break
        return ans
k = 1
for i in range(n):
    for j in range(m):
        k = max(k,f(i,j))
        if k == n + m - 1:
            break
print(k)


Fixed submission:
n,m = map(int, input().split())
u = [0]*(n+1)
for i in range(m):
    u[int(input())] = 1
ll = [0]*(n+2)
ll[1] = 1
for i in range(2,n+2):
    if not u[i-1]:
        ll[i] = ll[i-1] + ll[i - 2]
print(ll[-1] % 1000000007)

Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int, input().split())
l = [int(input()) for i in range(m)]
ll = [0]*(n+2)
ll[1] = 1
for i in range(2,n+2):
    if not i-1 in l:
        ll[i] = ll[i-1] + ll[i - 2]
print(ll[-1] % 1000000007)


Fixed submission:
n,m = map(int, input().split())
u = [0]*(n+1)
for i in range(m):
    u[int(input())] = 1
ll = [0]*(n+2)
ll[1] = 1
for i in range(2,n+2):
    if not u[i-1]:
        ll[i] = ll[i-1] + ll[i - 2]
print(ll[-1] % 1000000007)
Buggy python submission (verdict: Runtime Error):
n = int(input())
s = input()
siro = s.count(".")
ren = 0
i = 1
while s[-i] == "#":
    ren += 1
    i += 1
print(min(siro,n-ren-siro))


Fixed submission:
n = int(input())
s = input()
l = [0]*(n+1)
count = 0
for i in range(len(s)) :
    if s[i] == "." :
        count += 1
    l[i+1] = count
siro = l[-1]
ans = 1000000000000000000000
for i in range(n+1):
    ans = min(ans,i-2*l[i]+siro)
print(ans)
Buggy python submission (verdict: Runtime Error):
n = int(input())
f = 0
k = 0
for i in range(n) :
    a = input().split("")[1] == "JPY"
    if a[1] == "JPY" :
        f +=  int(a[0])
    else :
        k += int(a[0])
print(f + k *380000 )


Fixed submission:
n = int(input())
f = 0
k = 0
a = []
for i in range(n) :
    a = input().split()
    if a[1] == "JPY" :
        f +=  float(a[0])
    else :
        k += float(a[0])
print(f + k *380000 )
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
a.sort()
while len(a) >= 2 :
    k = a[1] % a[0]
    if k == 0 :
        a.pop(1)
    else :
        a[1] = k
    a.sort()
    if a[0] == 1 :
        break
print(a[0])


Fixed submission:
n = int(input())
a = list(map(int, input().split()))
a.sort()
while len(a) >= 2 :
    k = a[1] % a[0]
    if k == 0 :
        a.pop(1)
    else :
        a[1] = k
        a[0], a[1] = a[1], a[0]
print(a[0])

Buggy python submission (verdict: Runtime Error):
n = int(input())
a = list(map(int, input().split()))
a.sort()
while len(a) >= 2 :
    k = a[1] % a[0]
    if k == 0 :
        a.pop(1)
    else :
        a[1] = k
    a.sort()
    print(a)
    if a[0] == 1 :
        break
print(a[0])


Fixed submission:
n = int(input())
a = list(map(int, input().split()))
a.sort()
while len(a) >= 2 :
    k = a[1] % a[0]
    if k == 0 :
        a.pop(1)
    else :
        a[1] = k
        a[0], a[1] = a[1], a[0]
print(a[0])

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
a.sort()
while len(a) >= 2 :
    k = a[1] % a[0]
    if k == 0 :
        a.pop(1)
    else :
        a[1] = k
    a.sort()
print(a[0])


Fixed submission:
n = int(input())
a = list(map(int, input().split()))
a.sort()
while len(a) >= 2 :
    k = a[1] % a[0]
    if k == 0 :
        a.pop(1)
    else :
        a[1] = k
        a[0], a[1] = a[1], a[0]
print(a[0])
Buggy python submission (verdict: Runtime Error):
n = int(input())
l = list(map(int, input().split()))
def memo(n) :
    me = [-1]*(n+1)
    global l
    def f(n) :
        if n == 0 :
            return 0
        elif n == 1 :
            return abs(l[0] - l[1])
        else :
            if me[n] != -1 :
                return me[n]
            else :
                me[n] = min([f(n - 2) + abs(l[n - 2] - l[n]),f(n - 1) + abs(l[n - 1] - l[n])])
                return me[n]
    return f(n)
print(memo(n - 1))


Fixed submission:
n = int(input())
l = list(map(int, input().split()))
def memo(n) :
    me = [0]*(n)
    global l
    me[0] = 0
    me[1] = abs(l[0] - l[1])
    def f(n) :
        for i in range(2, n) :
            me[i] = min([me[i - 2] + abs(l[i - 2] - l[i]),me[i - 1] + abs(l[i - 1] - l[i])])
        return me[n-1]
    return f(n)
print(memo(n))

Buggy python submission (verdict: Runtime Error):
n = int(input())
l = list(map(int, input().split()))
def memo(n) :
    me = [0]*(n+1)
    global l
    def f(n) :
        if n == 0 :
            return 0
        elif n == 1 :
            return abs(l[0] - l[1])
        else :
            if me[n] != 0 :
                return me[n]
            else :
                me[n] = min([f(n - 2) + abs(l[n - 2] - l[n]),f(n - 1) + abs(l[n - 1] - l[n])])
                return me[n]
    return f(n)
print(memo(n - 1))


Fixed submission:
n = int(input())
l = list(map(int, input().split()))
def memo(n) :
    me = [0]*(n)
    global l
    me[0] = 0
    me[1] = abs(l[0] - l[1])
    def f(n) :
        for i in range(2, n) :
            me[i] = min([me[i - 2] + abs(l[i - 2] - l[i]),me[i - 1] + abs(l[i - 1] - l[i])])
        return me[n-1]
    return f(n)
print(memo(n))
Buggy python submission (verdict: Time Limit Exceeded):
if __name__ == "__main__":
	N = int(input())
	counter = 0

	c = 1
	while N - c >= 1:
		i = 1
		while i <= N:
			j = 1
			while i * j <= N:
				if i * j + c== N:
					counter += 1
				j += 1
			i += 1
		c += 1
	print(counter)

Fixed submission:
N = int(input())

ans = 0
for i in range(1,N):
	ans += int((N - 1) / i)
print(ans)
Buggy python submission (verdict: Runtime Error):
def myfunc(N):
	if(N  == 1):
		return 1
	return 2 * myfunc(N/2) + 1

if __name__ == "__main__":
	N = int(input())
	print(myfunc(N))

Fixed submission:
def myfunc(N):
	if(N  <= 1):
		return 1
	return 2 * myfunc(int(N/2)) + 1

if __name__ == "__main__":
	N = int(input())
	print(myfunc(N))
Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())


L_list = []
R_list = []

for i in range(M):
    A, B = map(int, input().split())
    L_list.append(A)
    R_list.append(B)

counter = 0
for j in range(min(L_list),(max(R_list) + 1)):
    if j >= max(L_list) and j <= min(R_list):
        counter += 1

print(counter)

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

L_list = []
R_list = []

for i in range(M):
    A, B = map(int, input().split())
    L_list.append(A)
    R_list.append(B)

if max(L_list) > min(R_list):
    print(0)
else:
    print(min(R_list) - max(L_list) + 1)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())

l = [0] * N

for i in range(M):
    A, B = map(int, input().split())
    for j in range(A-1,B):
        l[j] += 1

ans = [k for k in l if k >= M]
print(len(ans))

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

L_list = []
R_list = []

for i in range(M):
    A, B = map(int, input().split())
    L_list.append(A)
    R_list.append(B)

if max(L_list) > min(R_list):
    print(0)
else:
    print(min(R_list) - max(L_list) + 1)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
a.sort()

total = 0
flag = 0

for i in range(n):
  if i != n-1:
    if a[i] == a[i + 1]:
      continue
  for j in range(i):
    if i == j:
      continue
    
    if int(a[i]/a[j]) == a[i]/a[j]:
      flag = 1
      break
    
  if flag == 0:
    total += 1
  else:
    flag = 0
    
print(total)

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

max_a = max(a)
l = [0] * max_a

for i in range(n):
  j = 1
  while a[i] * j - 1 < max_a:
    l[a[i] * j - 1] += 1
    j += 1

count = 0

for i in range(n):
  if l[a[i] - 1] == 1:
    count += 1

print(count)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
a.sort()

total = 0
flag = 0

for i in range(n):
  for j in range(i):
    if i == j:
      continue
    
    if int(a[i]/a[j]) == a[i]/a[j]:
      flag = 1
      break
  
  if i != n - 1:
    if a[i] == a[i + 1]:
      flag = 1
    
  if flag == 0:
    total += 1
  else:
    flag = 0
    
print(total)

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

max_a = max(a)
l = [0] * max_a

for i in range(n):
  j = 1
  while a[i] * j - 1 < max_a:
    l[a[i] * j - 1] += 1
    j += 1

count = 0

for i in range(n):
  if l[a[i] - 1] == 1:
    count += 1

print(count)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))

total = 0
flag = 0

for i in range(n):
  for j in range(n):
    if i == j:
      continue
    
    if int(a[i]/a[j]) == a[i]/a[j]:
      flag = 1
      break
    
  if flag == 0:
    total += 1
  else:
    flag = 0
    
print(total)

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

max_a = max(a)
l = [0] * max_a

for i in range(n):
  j = 1
  while a[i] * j - 1 < max_a:
    l[a[i] * j - 1] += 1
    j += 1

count = 0

for i in range(n):
  if l[a[i] - 1] == 1:
    count += 1

print(count)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
flag = 0
b = 1

for i in range(n):
  b *= a[i]

if b > 10**18:
  print(-1)
else:
  print(b)

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
flag = 0
b = 1

if 0 in a:
  print(0)
else:
  for i in range(n):
    b *= a[i]
    if b > 10**18:
      flag = 1
      break
  if b > 10**18:
    print(-1)
  else:
    print(b)
Buggy python submission (verdict: Runtime Error):
If input() == 'ABC':
  print('ARC')
else:
  print('ABC')

Fixed submission:
if input() == 'ABC':
  print('ARC')
else:
  print('ABC')
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))

count = 0
for i in range(n-1):
  for j in range(i+1,n):
    if abs(i - j) == a[i]+a[j]:
      count += 1
      
print(count)

Fixed submission:
import collections

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

l = []
r = []
for i in range(n):
  l.append(i+a[i])
  r.append(i-a[i])

count = collections.Counter(r)
ans = 0
for i in l:
  ans += count.get(i,0)
      
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import math
import itertools

k = int(input())
k_list = []
for i in range(1, k+1):
  k_list.append(i)

total = 0
for i in list(itertools.product(k_list,repeat=3)):
  total += math.gcd(i[0],math.gcd(i[1],i[2]))
  
print(total)

Fixed submission:
import math

k = int(input())

total = 0
for a in range(1, k+1):
  for b in range(1, k+1):
    if math.gcd(a,b) == 1:
      total += k
    else:
      for c in range(1, k+1):
        total += math.gcd(a,math.gcd(b,c))
    
print(total)

Buggy python submission (verdict: Time Limit Exceeded):
import math

k = int(input())

total = 0
for a in range(1, k+1):
  for b in range(1, k+1):
    for c in range(1, k+1):
      total += math.gcd(a,math.gcd(b,c))
      
print(total)

Fixed submission:
import math

k = int(input())

total = 0
for a in range(1, k+1):
  for b in range(1, k+1):
    if math.gcd(a,b) == 1:
      total += k
    else:
      for c in range(1, k+1):
        total += math.gcd(a,math.gcd(b,c))
    
print(total)
Buggy python submission (verdict: Runtime Error):
x = int(input())
l = [1]

total = 100
rate = 1

year = 0

while total < x :
    total = total + total // 100
   year += 1

print(year)

Fixed submission:
x = int(input())
l = [1]

total = 100
rate = 1

year = 0

while total < x :
    total = total + total // 100
    year += 1

print(year)
Buggy python submission (verdict: Time Limit Exceeded):
ht, st, ha, sa = map(int, raw_input().split())
turn = 0
while st > 0 and sa > 0:
  if turn == 0:
    ha -= st
  else:
    ht -= sa
    
  turn = 1 - turn
  
if st > 0:
  print 'Yes'
else:
  print 'No'

Fixed submission:
ht, st, ha, sa = map(int, raw_input().split())
turn = 0
while ha > 0 and ht > 0:
  if turn == 0:
    ha -= st
  else:
    ht -= sa
    
  turn = 1 - turn
  
if ht > 0:
  print 'Yes'
else:
  print 'No'
Buggy python submission (verdict: Runtime Error):
n,k = map(int, input().split())
from fractions import gcd

if n % k== 0:
  print 0
else:
  mini = n
  s = set()
  while True:
    if n > k:
      n = n % k
    else:
      n = abs(n-k)
    mini = min(n, mini)
    if n in s:
      print mini
      break
    s.add(n)
    

Fixed submission:
n,k = map(int, raw_input().split())
from fractions import gcd

if n % k== 0:
  print 0
else:
  mini = n
  s = set()
  while True:
    if n > k:
      n = n % k
    else:
      n = abs(n-k)
    mini = min(n, mini)
    if n in s:
      print mini
      break
    s.add(n)

Buggy python submission (verdict: Runtime Error):
n,k = map(int, input().split())
from fractions import gcd

if n % k== 0:
  print 0
else:
  print gcd(n,k)

Fixed submission:
n,k = map(int, raw_input().split())
from fractions import gcd

if n % k== 0:
  print 0
else:
  mini = n
  s = set()
  while True:
    if n > k:
      n = n % k
    else:
      n = abs(n-k)
    mini = min(n, mini)
    if n in s:
      print mini
      break
    s.add(n)

Buggy python submission (verdict: Runtime Error):
n,k = map(int, input().split())
from fractions import gcd

if n % k== 0:
  print 0
else:
  print gcd(n,k)

Fixed submission:
n,k = map(int, raw_input().split())
from fractions import gcd

if n % k== 0:
  print 0
else:
  mini = n
  s = set()
  while True:
    if n > k:
      n = n % k
    else:
      n = abs(n-k)
    mini = min(n, mini)
    if n in s:
      print mini
      break
    s.add(n)
Buggy python submission (verdict: Runtime Error):
print raw_input()[0]+raw_input()[0]+raw_input()[0]

Fixed submission:
s = raw_input().upper().split()
print s[0][0]+s[1][0]+s[2][0]
Buggy python submission (verdict: Runtime Error):
a, b  = input(), input()
if a>b:
    print 'GREATER'
else if a<b:
    print 'LESS'
else:
    print 'EQUAL'


Fixed submission:
a, b  = input(), input()
if a>b:
    print 'GREATER'
elif a<b:
    print 'LESS'
else:
    print 'EQUAL'
Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
l=list(map(int,input().split()))
s=0
for i in range(n-1):
    for j in range(i+1,n):
        s+=l[i]*l[j]
print(s%(10**9+7))

Fixed submission:
n=int(input())
l=list(map(int,input().split()))
sum=sum(l)
s=0
for i in range(n):
    s+=l[i]*(sum-l[i])
print(s//2%(10**9+7))
Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
a=list(map(int,input().split()))
s=0
if sorted(a) == a:
    print(s)
    exit()
while len(a)>0:
    x=max(a)
    k=a.index(x)
    s=s+x*(len(a)-1-k)-sum(a[k+1:])
    a=a[:k]
print(s)





Fixed submission:
n=int(input())
a=list(map(int,input().split()))
ans=0
mx=a[0]
for x in a:
    if mx>x:ans+=mx-x
    else:mx=x
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
a=list(map(int,input().split()))
s=0
while len(a)>0:
    if sorted(a) == a:
        print(s)
        exit()
    else:
        x=max(a)
        k=a.index(x)
        s=s+x*(len(a)-1-k)-sum(a[k+1:])
        #for i in range(k+1,len(a)):s+=x-a[i]
        a=a[:k]
print(s)





Fixed submission:
n=int(input())
a=list(map(int,input().split()))
ans=0
mx=a[0]
for x in a:
    if mx>x:ans+=mx-x
    else:mx=x
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
a=list(map(int,input().split()))
s=0

while len(a)>0:
    if sorted(a) == a:
        print(s)
        exit()
    else:
        x=max(a)
        k=a.index(x)
        for i in range(k+1,len(a)):s+=x-a[i]
        a=a[:k]
print(s)





Fixed submission:
n=int(input())
a=list(map(int,input().split()))
ans=0
mx=a[0]
for x in a:
    if mx>x:ans+=mx-x
    else:mx=x
print(ans)
Buggy python submission (verdict: Runtime Error):
n,k=map(int,input().split())
a=list(map(str,input().split()))
a=[0]+a

s="0"
t=1
flag1=0
flag2=0
for i in range(1,n+1):
    if a[t] not in s:
        s+=a[t]
        t=int(a[t])
    else:
        flag1=s.find(a[t])
        flag2=i
        break

if flag1==0:print(s[(k-1)%n+1])
else:
    if k<=flag2-1-1+1:print(s[k])
    else:
        s='0'+s[flag1:flag2]  #上下保持一致，左边加上0
        #print(s)
        print(s[(k-(flag1-1))%(flag2-1-flag1+1)])  #去除非循环节部分后求余











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

n,k=MAP()
a=LIST()
a=[0]+a
s=[0]
loca=1
begin=end=0
temp=[-1]*(2*10**5+1)
for i in range(1,n+1):
    s.append(a[loca])
    loca=a[loca]
    if k==i:
        print(s[i])
        exit()
    if temp[loca]==-1:
        temp[loca]=i
    else:
        begin=temp[loca]
        end=i-1
        break

if begin==0:print(s[(k-1)%n+1])
else:
    k=k-(begin-1)
    s=[0]+s[begin:]
    print(s[(k-1)%(end-begin+1)+1])

Buggy python submission (verdict: Runtime Error):
n,k=map(int,input().split())
a=list(map(str,input().split()))
a=[0]+a

s="0"
t=1
flag1=0
flag2=0
for i in range(1,n+1):
    if a[t] not in s:
        s+=a[t]
        t=int(a[t])
    else:
        flag1=s.find(a[t])
        flag2=i
        break

if flag1==0:print(s[(k-1)%n+1])
else:
    if k<=n:print(s[k])
    else:
        s='0'+s[flag1:flag2]  #上下保持一致，左边加上0
        #print(s)
        print(s[(k-(flag1-1))%(flag2-1-flag1+1)])  #去除非循环节部分后求余











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

n,k=MAP()
a=LIST()
a=[0]+a
s=[0]
loca=1
begin=end=0
temp=[-1]*(2*10**5+1)
for i in range(1,n+1):
    s.append(a[loca])
    loca=a[loca]
    if k==i:
        print(s[i])
        exit()
    if temp[loca]==-1:
        temp[loca]=i
    else:
        begin=temp[loca]
        end=i-1
        break

if begin==0:print(s[(k-1)%n+1])
else:
    k=k-(begin-1)
    s=[0]+s[begin:]
    print(s[(k-1)%(end-begin+1)+1])