Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
N=int(input())
S=[input() for c in range(N)]
ans=0
xa=0;bx=0;bxa=0;
for s in S:
  ans+=s.count("AB")
  xa+=(s[0]!="B" and s[-1]=="A")
  bx+=(s[0]=="B" and s[-1]!="A")
  bxa+=(s[0]=="B" and s[-1]=="A")
  
print(ans+(bxa-1)+(xa>0)+(bx>0) +min((xa-1,bx-1),0))



Fixed submission:
N=int(input())
S=[input() for i in range(N)]
BXA=0;XA=0;BX=0;ans=0;
for s in S:
  ans+=s.count("AB")
  if(s[0]=='B' and s[-1]=='A'):
    BXA+=1
  elif(s[0]=='B'):
    BX+=1
  elif(s[-1]=='A'):
    XA+=1
#print(ans,min([XA,BX]))
if min([XA,BX])>0:
  ans+=min([XA,BX])+BXA
elif max([XA,BX])>0:
  ans+=BXA
else:
  ans+=max([BXA-1,0])
print(ans)

Buggy python submission (verdict: Runtime Error):
N=int(input())
S=[input() for c in range(N)]
ans=0
xa=0;bx=0;bxa=0;
for s in S:
  ans+=s.count("AB")
  xa+=(s[0]!="B"&&S[-1]=="A")
  bx+=(s[0]=="B"&&S[-1]!="A")
  bxa+=(s[0]=="B"&&S[-1]=="A")
print(ans+(bxa-1)+min(xa,bx)+(xa!=bx&&bxa>0))




Fixed submission:
N=int(input())
S=[input() for i in range(N)]
BXA=0;XA=0;BX=0;ans=0;
for s in S:
  ans+=s.count("AB")
  if(s[0]=='B' and s[-1]=='A'):
    BXA+=1
  elif(s[0]=='B'):
    BX+=1
  elif(s[-1]=='A'):
    XA+=1
#print(ans,min([XA,BX]))
if min([XA,BX])>0:
  ans+=min([XA,BX])+BXA
elif max([XA,BX])>0:
  ans+=BXA
else:
  ans+=max([BXA-1,0])
print(ans)

Buggy python submission (verdict: Runtime Error):



R,G,B,N=[int(c) for c in input().split()]

C=sorted([R,G,B])




if(C==[1,1,1]):
	input()
    print(4504501)
	exit()
if(C==[1,1,2]):
	print(2253001)
	exit()






count=0
for i in range(N//C[2]+1):
	for j in range(N//C[1]+1):
		if(N-C[2]*i-C[1]*j>=0 and (N-C[2]*i-C[1]*j)%C[0]==0):
			count+=1
print(count)


Fixed submission:
N=int(input())
S=[input() for i in range(N)]
BXA=0;XA=0;BX=0;ans=0;
for s in S:
  ans+=s.count("AB")
  if(s[0]=='B' and s[-1]=='A'):
    BXA+=1
  elif(s[0]=='B'):
    BX+=1
  elif(s[-1]=='A'):
    XA+=1
#print(ans,min([XA,BX]))
if min([XA,BX])>0:
  ans+=min([XA,BX])+BXA
elif max([XA,BX])>0:
  ans+=BXA
else:
  ans+=max([BXA-1,0])
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

N=int(input())
A=[int(c) for c in input().split()]

A=np.array(A)





for i in range(sorted(A)[1],1,-1):
	if(np.sum(A%i==0)>=N-1):
		print(i)
		exit()
print(1)





Fixed submission:
#import numpy as np

N=int(input())
A=[int(c) for c in input().split()]

#A=np.array(A)






#大きい方から
for i in range(sorted(A)[1],1,-1):
#	if(np.sum(A%i==0)>=N-1):
	
	#check
	b=0
	j=0
	while(b<2):
		if(j==N):
			print(i)
			exit()
		if(A[j]%i!=0):
			b+=1
		j+=1
print(1)

Buggy python submission (verdict: Runtime Error):
for i in range(sorted(A)[1],1,-1):
	if(np.sum(A%i==0)>=N-1):
		print(i)
		exit()
print(1)



Fixed submission:
#import numpy as np

N=int(input())
A=[int(c) for c in input().split()]

#A=np.array(A)






#大きい方から
for i in range(sorted(A)[1],1,-1):
#	if(np.sum(A%i==0)>=N-1):
	
	#check
	b=0
	j=0
	while(b<2):
		if(j==N):
			print(i)
			exit()
		if(A[j]%i!=0):
			b+=1
		j+=1
print(1)
Buggy python submission (verdict: Runtime Error):
import numpy as np
N=int(input())
A=np.array([int(c) for c in input().split()])
AP=A[A>=0]
AN=A[A<0]
if(len(AN)%2==1):
  print(max(-sum(AN)+2*max(AN)+sum(AP),-sum(AN)-2*min(AP)+sum(AP)))
else:
  print(-sum(AN)+sum(AP))
  

Fixed submission:
N=int(input())
A=[int(c) for c in input().split()]

#どうするか
#-1 -1  1
# 1 -1 -1
#-1  1 -1

#偶数個の数値を自由に反転できるとして見ていい

positive=[]
negative=[]
for a in A:
	if(a>=0):
		positive.append(a)
	else:
		negative.append(a)
if(len(negative)%2==0):#偶数個なら全部ひっくり返せばいい
	print(sum(positive)-sum(negative))
else:#奇数個なら, もっとも大きなもの以外をひっくり返す
	#あるいは, 最後にpositive最小とnegative最大をひっくり返すこともありうる	
	if(len(positive)==0):
		print(-sum(negative)+2*max(negative))
	
	else:
		max_nega=max(negative)
		min_posi=min(positive)
		if(-1*max_nega>min_posi):
			print(sum(positive)-sum(negative)-2*min_posi)
		else:	
			print(sum(positive)-sum(negative)+2*max_nega)

Buggy python submission (verdict: Runtime Error):
N=int(input())
A=[int(c) for c in input().split()]

#どうするか
#-1 -1  1
# 1 -1 -1
#-1  1 -1

#偶数個の数値を自由に反転できるとして見ていい

positive=[]
negative=[]
for a in A:
	if(a>=0):
		positive.append(a)
	else:
		negative.append(a)
if(len(negative)%2==0):#偶数個なら全部ひっくり返せばいい
	print(sum(positive)-sum(negative))
else:#奇数個なら, もっとも大きなもの以外をひっくり返す
	#あるいは, 最後にpositive最小とnegative最大をひっくり返すこともありうる	
	max_nega=max(negative)
	min_posi=min(positive)
	if(-1*max_nega>min_posi):
		print(sum(positive)-sum(negative)-2*min_posi)
	else:
		
		print(sum(positive)-sum(negative)+2*max_nega)








Fixed submission:
N=int(input())
A=[int(c) for c in input().split()]

#どうするか
#-1 -1  1
# 1 -1 -1
#-1  1 -1

#偶数個の数値を自由に反転できるとして見ていい

positive=[]
negative=[]
for a in A:
	if(a>=0):
		positive.append(a)
	else:
		negative.append(a)
if(len(negative)%2==0):#偶数個なら全部ひっくり返せばいい
	print(sum(positive)-sum(negative))
else:#奇数個なら, もっとも大きなもの以外をひっくり返す
	#あるいは, 最後にpositive最小とnegative最大をひっくり返すこともありうる	
	if(len(positive)==0):
		print(-sum(negative)+2*max(negative))
	
	else:
		max_nega=max(negative)
		min_posi=min(positive)
		if(-1*max_nega>min_posi):
			print(sum(positive)-sum(negative)-2*min_posi)
		else:	
			print(sum(positive)-sum(negative)+2*max_nega)
Buggy python submission (verdict: Runtime Error):
def up(x):
	return x+10*(x%10!=0)-x%10



A=int(input())
B=int(input())
C=int(input())
D=int(input())
E=int(input())

l=[A,B,C,D,E]

m=np.array([i%10+10*(i%10==0) for i in l])
a=np.argmin(m)
out=0
for i in range(5):
	if(i==a):
		out+=l[i]
	else:
		out+=up(l[i])	

print(out)

Fixed submission:
import numpy as np







def up(x):
	return x+10*(x%10!=0)-x%10



A=int(input())
B=int(input())
C=int(input())
D=int(input())
E=int(input())

l=[A,B,C,D,E]

m=np.array([i%10+10*(i%10==0) for i in l])
a=np.argmin(m)
out=0
for i in range(5):
	if(i==a):
		out+=l[i]
	else:
		out+=up(l[i])	

print(out)
Buggy python submission (verdict: Time Limit Exceeded):



import numpy as np
def XOR(X):
	X=np.array(X)
	out=0
	i=0
	while(max(X)>0):
		out+=(np.sum(X%2)%2)*(2**i)
		X=X//2
		i+=1

	return out



A,B=input().split(" ")
A=int(A)
B=int(B)


X=[i for i in range(A,B+1)]
print(XOR(X))



Fixed submission:
A,B=input().split(" ")
A=int(A)
B=int(B)



if(A%2==1):
	A_=A+1
else:
	A_=A
if(B%2==0):
	B_=B-1
else:
	B_=B

out=((B_-A_+1)//2)%2
#import numpy as np
if(A%2==1):
	out^=A
if(B%2==0):
	out^=B
print(out)

Buggy python submission (verdict: Runtime Error):



import numpy as np
def XOR(X):
	X=np.array(X)
	out=0
	i=0
	while(max(X)>0):
		out+=(np.sum(X%2)%2)*(2**i)
		X=X//2
		i+=1

	return out



A,B=input().split(" ")
A=int(A)
B=int(B)


X=[for i in range(A,B+1)]
print(XOR(X))



Fixed submission:
A,B=input().split(" ")
A=int(A)
B=int(B)



if(A%2==1):
	A_=A+1
else:
	A_=A
if(B%2==0):
	B_=B-1
else:
	B_=B

out=((B_-A_+1)//2)%2
#import numpy as np
if(A%2==1):
	out^=A
if(B%2==0):
	out^=B
print(out)

Buggy python submission (verdict: Runtime Error):




import numpy as np
def XOR(X):
	X=np.array(X)
	out=0
	i=0
	while(max(X)>0):
		out+=(np.sum(X%2)%2)*(2**i)
		X=X//2
		i+=1

	return out



A,B=input().split(" ")
A=int(A)
B=int(B)


X=range(A,B+1)
print(XOR(X))







Fixed submission:
A,B=input().split(" ")
A=int(A)
B=int(B)



if(A%2==1):
	A_=A+1
else:
	A_=A
if(B%2==0):
	B_=B-1
else:
	B_=B

out=((B_-A_+1)//2)%2
#import numpy as np
if(A%2==1):
	out^=A
if(B%2==0):
	out^=B
print(out)
Buggy python submission (verdict: Runtime Error):
S=input();print(min(S.count("1"),S.count(0))*2)

Fixed submission:
S=input();print(min(S.count("1"),S.count("0"))*2)

Buggy python submission (verdict: Time Limit Exceeded):
S=input()


S=[s for s in S]

count=0
i=0
while(i<len(S)-1):
#	print(i,S)
	if(int(S[i])+int(S[i+1])==1):
		count+=2
		if(i<=len(S)-3):
			S=S[:i]+S[i+2:]
		elif(i==len(S)-2):
			break
				
		if(i>0):
			i-=1
	else:
		i+=1

print(count)

Fixed submission:
S=input();print(min(S.count("1"),S.count("0"))*2)
Buggy python submission (verdict: Runtime Error):

N=int(input())
A=input().split()
A=[int(a) for a in A]

import math
a=math.gcd(A[0],A[1])
for i in range(2,len(A)):
	a=math.gcd(a,A[i])
print(a)



Fixed submission:
def gcd(x,y):
  return y if x%y==0 else gcd(y,x%y)
N=input();A=list(map(int,input().split()));
g=A[0];
for a in A:
  g=gcd(g,a)
print(g)
Buggy python submission (verdict: Runtime Error):
A,B,K=input().split()
A=int(A)
B=int(B)
K=int(K)
turn=0
while(1):
	if(turn==K):
		break
	if(turn%2==0):#高橋くん
		if(A%2==1):
			A-=1
	else:#青木くん
		if(B%2==1):
			B-=1
	turn+=1
print(A+" "+B)
 

Fixed submission:

A,B,K=input().split()
A=int(A)
B=int(B)
K=int(K)
turn=0
while(1):
	if(turn==K):
		break
	if(turn%2==0):#高橋くん
		if(A%2==1):
			A-=1
		B+=A//2
		A-=A//2
	else:#青木くん
		if(B%2==1):
			B-=1
			
		A+=B//2
		B-=B//2
	turn+=1
print(str(A)+" "+str(B))

Buggy python submission (verdict: Runtime Error):

A,B=input().split()
A=int(A)
B=int(B)
 
turn=0
while(1):
	if(turn==K):
		break
	if(turn%2==0):#高橋くん
		if(A%2==1):
			A-=1
	else:#青木くん
		if(B%2==1):
			B-=1
	turn+=1
print(A+" "+B)
 

Fixed submission:

A,B,K=input().split()
A=int(A)
B=int(B)
K=int(K)
turn=0
while(1):
	if(turn==K):
		break
	if(turn%2==0):#高橋くん
		if(A%2==1):
			A-=1
		B+=A//2
		A-=A//2
	else:#青木くん
		if(B%2==1):
			B-=1
			
		A+=B//2
		B-=B//2
	turn+=1
print(str(A)+" "+str(B))

Buggy python submission (verdict: Runtime Error):
A,B=input()
A=input(A)
B=input(B)
 
turn=0
while(1):
	if(turn==K):
		break
	if(turn%2==0):#高橋くん
		if(A%2==1):
			A-=1
	else:#青木くん
		if(B%2==1):
			B-=1
	turn+=1
print(A+" "+B)

Fixed submission:

A,B,K=input().split()
A=int(A)
B=int(B)
K=int(K)
turn=0
while(1):
	if(turn==K):
		break
	if(turn%2==0):#高橋くん
		if(A%2==1):
			A-=1
		B+=A//2
		A-=A//2
	else:#青木くん
		if(B%2==1):
			B-=1
			
		A+=B//2
		B-=B//2
	turn+=1
print(str(A)+" "+str(B))

Buggy python submission (verdict: Runtime Error):
 A,B=input()
 A=input(A)
 B=input(B)
 
 turn=0
 while(1):
 	if(turn==K):
 		break
 	if(turn%2==0):#高橋くん
 		if(A%2==1):
 			A-=1
	else:#青木くん
		if(B%2==1):
			B-=1
	turn+=1
 print(A+" "+B)
 
 
 
 
 

Fixed submission:

A,B,K=input().split()
A=int(A)
B=int(B)
K=int(K)
turn=0
while(1):
	if(turn==K):
		break
	if(turn%2==0):#高橋くん
		if(A%2==1):
			A-=1
		B+=A//2
		A-=A//2
	else:#青木くん
		if(B%2==1):
			B-=1
			
		A+=B//2
		B-=B//2
	turn+=1
print(str(A)+" "+str(B))
Buggy python submission (verdict: Runtime Error):
 
 


A,B,K=input().split()
A=int(A)
B=int(B)
K=int(K)
turn=0
while(1):
	if(turn==K):
		break
	if(turn%2==0):#高橋くん
		if(A%2==1):
			A-=1
	else:#青木くん
		if(B%2==1):
			B-=1
	turn+=1
print(str(A)+" "+str(B))

Fixed submission:

N=input()
N=int(N)

As=[]
for i in range(N):
	As.append(int(input()))


As.sort()

l=[]
l.append(As[N//2])
#if(N%2==0):
s=0
b=0
for i in range(N-1):
	if(i%2==1):
		l.append(As[-b-1])
		b+=1
	else:
		l.append(As[s])
		s+=1
ans=0
for i in range(1,N):
	ans+=abs(l[i]-l[i-1])
#print(l)
#print(ans)

l2=[]
l2.append(As[(N-1)//2])
#if(N%2==0):
s2=0
b2=0
for i in range(N-1):
	if(i%2==0):
		l2.append(As[-b2-1])
		b2+=1
	else:
		l2.append(As[s2])
		s2+=1
ans2=0
for i in range(1,N):
	ans2+=abs(l2[i]-l2[i-1])
#print(l2)
#print(ans2)
print(max(ans,ans2))
Buggy python submission (verdict: Runtime Error):
N=int(input())
A=list(map(int,input().split()))
D={}
for a in A:
  if(a in D):
    D[a]+=1
  else:
    D[a]=1
    
if(len(D)>2):
  print("No")
  exit()
  
if(len(D)==2):
  B=max(D)
  if(B-D[B-1]>0 and D[B]>=2*(B-D[B-1])):
    print("Yes")
  else:
    print("No")
  
else:
  B=max(D) # 常に同じ色の人がいる
  if(N>=2*B):
    print("Yes")
    exit()
  
  B=max(D)+1 # 全員違う色
  if(B==N):
    print("Yes")
    exit()
  print("No")
  

Fixed submission:

N=int(input())
A=list(map(int,input().split()))
D={}
for a in A:
  if(a in D):
    D[a]+=1
  else:
    D[a]=1
    
if(max(D)-min(D)>1):
  print("No")
  exit()
  
if(len(D)==2):
  B=max(D)
  if(B-D[B-1]>0 and D[B]>=2*(B-D[B-1])):
    print("Yes")
  else:
    print("No")
  
else:
  B=max(D) # 常に同じ色の人がいる
  if(N>=2*B):
    print("Yes")
    exit()
  
  B=max(D)+1 # 全員違う色
  if(B==N):
    print("Yes")
    exit()
  print("No")

Buggy python submission (verdict: Runtime Error):
N=int(input())
A=list(map(int,input().split()))
D={}
for a in A:
  if(a in D):
    D[a]+=1
  else:
    D[a]=1
    
if(len(D)>2):
  print("No")
  exit()
  
if(len(D)==2):
  B=max(D)
  if(B-D[B-1]>0 and D[B]>=2*(B-D[B-1])):
    print("Yes")
  else:
    print("No")
  
else:
  B=max(D) # 常に同じ色の人がいる
  if(N>=2*B):
    print("Yes")
    exit()
  
  B=max(D)+1 # 全員違う色
  if(B==N):
    print("Yes")
    exit()
  print("No")
  

Fixed submission:

N=int(input())
A=list(map(int,input().split()))
D={}
for a in A:
  if(a in D):
    D[a]+=1
  else:
    D[a]=1
    
if(max(D)-min(D)>1):
  print("No")
  exit()
  
if(len(D)==2):
  B=max(D)
  if(B-D[B-1]>0 and D[B]>=2*(B-D[B-1])):
    print("Yes")
  else:
    print("No")
  
else:
  B=max(D) # 常に同じ色の人がいる
  if(N>=2*B):
    print("Yes")
    exit()
  
  B=max(D)+1 # 全員違う色
  if(B==N):
    print("Yes")
    exit()
  print("No")
Buggy python submission (verdict: Time Limit Exceeded):
# input here
_INPUT = """\
4
141421356 17320508 22360679 244949
"""
"""
K = int(input())
H, W, K = map(int, input().split())
a = list(map(int, input().split()))
xy = [list(map(int, input().split())) for i in range(N)]
p = tuple(map(int,input().split()))
"""
def main():
    n = int(input())
    a = list(map(int, input().split()))

    sum1 = 0
    for i in range(len(a)):
        mult = a[i] * sum(a[i+1:])
        sum1 += mult

    print((sum1 % (10**9 + 7)))


if __name__ == '__main__':
    import io
    import sys
    import math
    import itertools
    from collections import deque

    # sys.stdin = io.StringIO(_INPUT)
    main()

Fixed submission:
# input here
_INPUT = """\
4
141421356 17320508 22360679 244949
"""
"""
K = int(input())
H, W, K = map(int, input().split())
a = list(map(int, input().split()))
xy = [list(map(int, input().split())) for i in range(N)]
p = tuple(map(int,input().split()))
"""
def main():
    n = int(input())
    a = list(map(int, input().split()))
    cumsum = list(itertools.accumulate(a))

    sum1 = 0
    for i in range(len(a)):
        mult = a[i] * (cumsum[n-1] - cumsum[i])
        sum1 += mult

    print((sum1 % (10**9 + 7)))


if __name__ == '__main__':
    import io
    import sys
    import math
    import itertools
    from collections import deque

    # sys.stdin = io.StringIO(_INPUT)
    main()

Buggy python submission (verdict: Time Limit Exceeded):
# input here
_INPUT = """\
4
141421356 17320508 22360679 244949
"""
"""
K = int(input())
H, W, K = map(int, input().split())
a = list(map(int, input().split()))
xy = [list(map(int, input().split())) for i in range(N)]
p = tuple(map(int,input().split()))
"""
def main():
    n = int(input())
    a = list(map(int, input().split()))
    comb = list(itertools.combinations(a, 2))
    sum = 0
    for i in range(len(comb)):
        mult = comb[i][0] * comb[i][1]
        sum += mult

    print((sum % (10**9 + 7)))


if __name__ == '__main__':
    import io
    import sys
    import math
    import itertools
    from collections import deque

    # sys.stdin = io.StringIO(_INPUT)
    main()

Fixed submission:
# input here
_INPUT = """\
4
141421356 17320508 22360679 244949
"""
"""
K = int(input())
H, W, K = map(int, input().split())
a = list(map(int, input().split()))
xy = [list(map(int, input().split())) for i in range(N)]
p = tuple(map(int,input().split()))
"""
def main():
    n = int(input())
    a = list(map(int, input().split()))
    cumsum = list(itertools.accumulate(a))

    sum1 = 0
    for i in range(len(a)):
        mult = a[i] * (cumsum[n-1] - cumsum[i])
        sum1 += mult

    print((sum1 % (10**9 + 7)))


if __name__ == '__main__':
    import io
    import sys
    import math
    import itertools
    from collections import deque

    # sys.stdin = io.StringIO(_INPUT)
    main()
Buggy python submission (verdict: Time Limit Exceeded):
import io
import sys
import math
import itertools
N = int(input())
A = list(map(int, input().split()))

Springboard = []
for i in range(1,len(A)):
  # 自分より前に自分より身長が高い人がいないとき
  if max(A[:i+1]) == A[i]:
    Springboard.append(0)
    # 自分より前に自分より身長が高い人がいるとき
  else:
    Springboard.append(max(A[:i]) - A[i])

print(sum(Springboard))

Fixed submission:
# input here
_INPUT = """\
5
3 3 3 3 3
"""

def main():
    N = int(input())
    A = list(map(int, input().split()))
    
    Springboard = []
    max = A[0]
    for i in range(1,len(A)):
    # 自分より前に自分より身長が高い人がいないとき
        if max < A[i]:
            max = A[i]
            Springboard.append(0)
     # 自分より前に自分より身長が高い人がいるとき
        else:
            Springboard.append(max - A[i])

    print(sum(Springboard))

if __name__ == '__main__':
    import io
    import sys
    import math
    import itertools

    # sys.stdin = io.StringIO(_INPUT)
    main()

Buggy python submission (verdict: Runtime Error):
import io
import sys
import math
import itertools
N = int(input())
A = list(map(int, input().split()))

Springboard = []
for i in range(1,len(A)):
  # 自分より前に自分より身長が高い人がいないとき
  if max(A[:i+1]) == A[i]:
    Springboard.append(0)
    # 自分より前に自分より身長が高い人がいるとき
    else:
      Springboard.append(max(A[:i]) - A[i])

print(sum(Springboard))



Fixed submission:
# input here
_INPUT = """\
5
3 3 3 3 3
"""

def main():
    N = int(input())
    A = list(map(int, input().split()))
    
    Springboard = []
    max = A[0]
    for i in range(1,len(A)):
    # 自分より前に自分より身長が高い人がいないとき
        if max < A[i]:
            max = A[i]
            Springboard.append(0)
     # 自分より前に自分より身長が高い人がいるとき
        else:
            Springboard.append(max - A[i])

    print(sum(Springboard))

if __name__ == '__main__':
    import io
    import sys
    import math
    import itertools

    # sys.stdin = io.StringIO(_INPUT)
    main()

Buggy python submission (verdict: Time Limit Exceeded):
# input here
_INPUT = """\
5
3 3 3 3 3
"""

def main():
    N = int(input())
    A = list(map(int, input().split()))
    
    Springboard = []
    for i in range(1,len(A)):
    # 自分より前に自分より身長が高い人がいないとき
        if max(A[:i+1]) == A[i]:
            Springboard.append(0)
     # 自分より前に自分より身長が高い人がいるとき
        else:
            Springboard.append(max(A[:i]) - A[i])

    print(sum(Springboard))

if __name__ == '__main__':
    import io
    import sys
    import math
    import itertools

    # sys.stdin = io.StringIO(_INPUT)
    main()

Fixed submission:
# input here
_INPUT = """\
5
3 3 3 3 3
"""

def main():
    N = int(input())
    A = list(map(int, input().split()))
    
    Springboard = []
    max = A[0]
    for i in range(1,len(A)):
    # 自分より前に自分より身長が高い人がいないとき
        if max < A[i]:
            max = A[i]
            Springboard.append(0)
     # 自分より前に自分より身長が高い人がいるとき
        else:
            Springboard.append(max - A[i])

    print(sum(Springboard))

if __name__ == '__main__':
    import io
    import sys
    import math
    import itertools

    # sys.stdin = io.StringIO(_INPUT)
    main()
Buggy python submission (verdict: Runtime Error):
def popcount(x):
    return bin(x)[2:].count("1")

def f(x):
    cnt=0
    while(x>0):
        x%=popcount(x)
        cnt+=1
    return cnt

n=int(input())
s=input()
d=s.count("1")
s=s[::-1]
toaddm=1
toaddM=1
m=0
M=0
for i in range(n):
    if s[i]=="1":
        m+=toaddm
        M+=toaddM
        m%=d-1
        M%=d+1
    toaddm*=2
    toaddm%=d-1
    toaddM*=2
    toaddM%=d+1
toaddm=-1
toaddM=1
res=[None for i in range(n)]
for i in range(n):
    if s[i]=="1":
        res[i]=f((m+toaddm)%(d-1))+1
    else:
        res[i]=f((M+toaddM)%(d+1))+1
    toaddm*=2
    toaddm%=d-1
    toaddM*=2
    toaddM%=d+1
for i in res[::-1]:
    print(i)


Fixed submission:
def popcount(x):
    return bin(x)[2:].count("1")

def f(x):
    cnt=0
    while(x>0):
        x%=popcount(x)
        cnt+=1
    return cnt

n=int(input())
s=input()
d=s.count("1")
if d==0:
    for i in range(n):
        print(1)
elif d==1:
    if s[-1]=="1":
        for i in range(n-1):
            print(2)
        else:
            print(0)
    else:
        for i in range(n-1):
            if s[i]=="1":
                print(0)
            else:
                print(1)
        print(2)
else:
    s=s[::-1]
    toaddm=1
    toaddM=1
    m=0
    M=0
    for i in range(n):
        if s[i]=="1":
            m+=toaddm
            M+=toaddM
            m%=d-1
            M%=d+1
        toaddm*=2
        toaddm%=d-1
        toaddM*=2
        toaddM%=d+1
    toaddm=-1
    toaddM=1
    res=[None for i in range(n)]
    for i in range(n):
        if s[i]=="1":
            res[i]=f((m+toaddm)%(d-1))+1
        else:
            res[i]=f((M+toaddM)%(d+1))+1
        toaddm*=2
        toaddm%=d-1
        toaddM*=2
        toaddM%=d+1
    for i in res[::-1]:
        print(i)
Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
if n==1:
    print(1)
else:
    l=[int(i) for i in input().split()]
    l.sort()
    d=dict()
    for i in l:
        d[i]=True
    res=0
    for i in range(n):
        if d[l[i]]:
            if i==n-1 or (i<n-1 and l[i]!=l[i+1]):
                res+=1
            for j in range(i+1,n):
                if not l[j]%l[i]:
                    d[l[j]]=False
    print(res)



Fixed submission:
n=int(input())
if n==1:
    print(1)
else:
    oklist=[True for i in range(1000001)]
    l=[int(i) for i in input().split()]
    l.sort()
    res=0
    for i in range(n):
        if oklist[l[i]]:
            if i==n-1 or (i<n-1 and l[i]!=l[i+1]):
                res+=1
            for j in range(1,1000001):
                if l[i]*j<=1000000:
                    oklist[l[i]*j]=False
                else:
                  break
    print(res)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
if n==1:
    print(1)
else:
    oklist=[True for i in range(1000001)]
    l=[int(i) for i in input().split()]
    l.sort()
    res=0
    for i in range(n):
        if oklist[l[i]]:
            if i==n-1 or (i<n-1 and l[i]!=l[i+1]):
                res+=1
            for j in range(1,1000001):
                if l[i]*j<=1000000:
                    oklist[l[i]*j]=False
    print(res)



Fixed submission:
n=int(input())
if n==1:
    print(1)
else:
    oklist=[True for i in range(1000001)]
    l=[int(i) for i in input().split()]
    l.sort()
    res=0
    for i in range(n):
        if oklist[l[i]]:
            if i==n-1 or (i<n-1 and l[i]!=l[i+1]):
                res+=1
            for j in range(1,1000001):
                if l[i]*j<=1000000:
                    oklist[l[i]*j]=False
                else:
                  break
    print(res)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
if n==1:
    print(1)
else:
    l=[int(i) for i in input().split()]
    l.sort()
    res=0
    ll=[]
    for i in range(n):
        flag=True
        for j in ll:
            if not l[i]%j:
                flag=False
                break
        if flag:
            ll.append(l[i])
            if i==n-1 or (i<n-1 and l[i]!=l[i+1]):
                res+=1
    print(res)



Fixed submission:
n=int(input())
if n==1:
    print(1)
else:
    oklist=[True for i in range(1000001)]
    l=[int(i) for i in input().split()]
    l.sort()
    res=0
    for i in range(n):
        if oklist[l[i]]:
            if i==n-1 or (i<n-1 and l[i]!=l[i+1]):
                res+=1
            for j in range(1,1000001):
                if l[i]*j<=1000000:
                    oklist[l[i]*j]=False
                else:
                  break
    print(res)
Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
edg=[[] for i in range(n)]
for i in range(m):
    a,b=map(int,input().split())
    a-=1;b-=1
    edg[a].append(b)
    edg[b].append(a)
visited=[False for i in range(n)]
visited[0]=True
parent=[None for i in range(n)]
st=[0]
while st!=[]:
    now=st[0]
    del(st[0])
    for i in edg[now]:
        if not visited[i]:
            st.append(i)
            visited[i]=True
            parent[i]=now
print("Yes")
for i in parent[1:]:
    print(i+1)


Fixed submission:
from collections import deque
n,m=map(int,input().split())
edg=[[] for i in range(n)]
for i in range(m):
    a,b=map(int,input().split())
    a-=1;b-=1
    edg[a].append(b)
    edg[b].append(a)
visited=[False for i in range(n)]
visited[0]=True
parent=[None for i in range(n)]
q=deque([0])
while len(q)>0:
    now=q.popleft()
    for i in edg[now]:
        if not visited[i]:
            q.append(i)
            visited[i]=True
            parent[i]=now
print("Yes")
for i in parent[1:]:
    print(i+1)