Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

import bisect
import math
N = int(input())
A = list(map(int, input().split()))
T = [2 ** i for i in range(int(math.log2(max(A))) + 1, -1, -1)]

A.sort()

def is_in(t):
    i = bisect.bisect_left(A, t)
    if i < len(A) and t == A[i]:
        return i
    return -1

res = 0
while A:
    a = A.pop()
    T2 = [t - a for t in T if t > a]
    for t in T2:
        i = is_in(t)
        if i != -1:
            res += 1
            A.pop(i)
            break

print(res)



### FIXED CODE ###

import bisect
import math
N = int(input())
A = list(map(int, input().split()))
T = [2 ** i for i in range(31)]
A.sort()
already = set()


def to_dic(A):
    d = {}
    for a in A:
        d[a] = d.setdefault(a, 0) + 1

    return d

def is_in(t, num):
    i = D.setdefault(t, 0)
    if i == 0:
        return 0
    s = min(i, num)
    D[t] -= s
    return s


res = 0
D = to_dic(A)
A2 = list(D.keys())
A2.sort()

for n in range(len(A2) - 1, -1, -1):
    a = A2[n]

    if a in T:
        res += D[a] // 2
        D[a] = 0

        if D[a] % 2 == 0:
            continue

    num = D[a]
    t = 1 << (int(math.log2(a)) + 1)

    while t > a:
        s = is_in(t - a, num)
        res += s
        num -= s
        if num == 0:
            break

        t >>= 1

print(res)
### BUGGY CODE ###

import math
n=int(input())

for p in range(1, n+1):
  right=math.floor(p*0.5)
  cnt=0
  for i in range(right):
    for j in range(i+1):
      for k in range(j+1):
        if i**2+j**2+k**2+i*j+j*k+k*i==p and i>0 and j>0 and k>0:
          if i==j and j==k:
            cnt+=1
          elif i==j and j!=k:
            cnt+=3
          elif j==k and k!=i:
            cnt+=3
          elif k==i and i!=j:
            cnt+=3
          elif i!=j and j!=k:
            cnt+=6
  print(cnt)

### FIXED CODE ###

n=int(input())
lst=[0]*10000000
for i in range(1, 101):
  for j in range(1, i+1):
    for k in range(1, j+1):
      temp=i**2+j**2+k**2+i*j+j*k+k*i
      if i==j and j==k:
        lst[temp+1]+=1
      elif i==j and j!=k:
         lst[temp+1]+=3
      elif j==k and k!=i:
         lst[temp+1]+=3
      elif k==i and i!=j:
         lst[temp+1]+=3
      elif i!=j and j!=k and k!=i:
         lst[temp+1]+=6
for p in range(1, n+1):
  print(lst[p+1])
### BUGGY CODE ###

N=int(input())
S=list(input())

R=[]
G=[]
B=[]

# リストを作成
for i in range(0,N):
  if S[i]=="R":
    R += [i]
  elif S[i]=="G":
    G += [i]
  else:
    B += [i]

def search(ind, col):
  n=0
  useable_col = ["R","G","B"]
  useable_col.remove(col)
  for i,j in [[0,1],[1,0]]:
    l_col=useable_col[i]
    r_col=useable_col[j]
    if l_col=="R":
      l_col=R
    elif l_col=="G":
      l_col=G
    else:
      l_col=B
    if r_col=="R":
      r_col=R
    elif r_col=="G":
      r_col=G
    else:
      r_col=B
    
    l_useable = [] 
    r_useable = [] 
    for ii in l_col:
      if ii<ind:
        l_useable += [ii]
      else:
        break
    if l_useable == []:
      continue
    for ii in reversed(r_col):
      if ii>ind:
        r_useable += [ii]
      else:
        break
    if r_useable == []:
      continue

    # print(l_useable)
    # print(r_useable)
    l = min(ind, N-1-ind)
    for m in range(1,l+1):
      if ind-m in l_useable and ind+m in r_useable:
        n -= 1
    n += len(l_useable) * len(r_useable)
    # print(len(l_useable))
    # print(len(r_useable))
  return n

n=0
for pivot in range(1,N-1):
  if S[pivot] =="R":
    n += search(pivot, "R")
  elif S[pivot] =="G":
    n += search(pivot, "G")
  else:
    n += search(pivot, "B")

print(n)


### FIXED CODE ###

N=int(input())
S=list(input())

r=0
g=0
b=0

for i in range(N):
  if S[i]=="R":
    r+=1
  elif S[i]=="G":
    g+=1
  else:
    b+=1
    
n=r*g*b

for i in range(N):
  for j in range(i+1,N):
    if S[i]==S[j]:
      continue
    k = 2*j-i
    if k >= N:
      break
    if S[i]==S[k] or S[j]==S[k]:
      continue
    n -= 1
      
print(n)
### BUGGY CODE ###

N = int(input())
A = [] 
for i in range (N):
    A.append(input())
A = list(map(int,A))
Max = A[1]-A[0]
for i in range(N-1):
    for j in range(i+1,N):
        if (A[j]-A[i]) > Max :
            Max = A[j]-A[i]
print(Max)


### FIXED CODE ###

n = int(input())
numbers = []

minv = int(input())
num = int(input())

max_r = num - minv

if(minv > num):
    minv = num

for i in range(2,n):
    num = int(input())
    if(max_r <(num - minv)):
        max_r = num - minv

    if(minv > num):
        minv = num

print(max_r)
### BUGGY CODE ###

N = int(input())
S = input()

ans = []
for i in range(N):
    ans.append(S[:i].count('W') + S[i+1:].count('E'))

print(min(ans))

### FIXED CODE ###

N = int(input())
S = str(input())

Wsum = [0]
for i in range(N):
    if S[i] == 'W':
        Wsum.append(Wsum[i] + 1)
    else:
        Wsum.append(Wsum[i])
del Wsum[0]

S = S[::-1]
Esum = [0]
for i in range(N):
    if S[i] == 'E':
        Esum.append(Esum[i] + 1)
    else:
        Esum.append(Esum[i])
del Esum[0]
Esum = Esum[::-1]

Sum = []
for i in range(0,N):
    Sum.append(Wsum[i] + Esum[i]-1)

print(min(Sum))
### BUGGY CODE ###

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

if A > K:
  for k in range(K):
    count += 1
elif A + B >= K:
  for a in range(A):
    count += 1
elif A + B < K  and A >= K - A - B:
   for ac in range(A + A + B - K):
    count += 1
else:
  for ca in range(K - A - A - B):
    count -= 1

print(count)

### FIXED CODE ###

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

Xa = min(K,A)
K2 = K - Xa
Xb = min(K2,B)
Xc = K2 - Xb

print(Xa - Xc)
### BUGGY CODE ###

from collections import defaultdict
class Unionfindtree:
    def __init__(self, number):
        self.par = [i for i in range(number)]
        self.rank = [0] * (number)

    def find(self, x):  # 親を探す
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def union(self, x, y):  # x,yを繋げる
        px = self.find(x)
        py = self.find(y)
        if px == py:
            return
        if self.rank[px] < self.rank[py]:
            self.par[px] = py
        else:
            self.par[py] = px
        if self.rank[px] == self.rank[py]:
            self.rank[px] += 1

    def connect(self, x, y):  # 親が同じかみる
        return self.find(x) == self.find(y)

N,M=map(int,input().split())
X=[int(i) for i in input().split()]
num=0
x=sum(X)
table=[]
for i in range(M):
    a,b,c=map(int,input().split())
    a,b=a-1,b-1
    if c<=x:
        table.append((c,a,b))
        num+=1
test=sorted(table)
while True:
    #print(1)
    flag=True
    T=Unionfindtree(N)
    dd=defaultdict(int)
    for c,a,b in test:
        T.union(a,b)
    for i in range(N):
        dd[T.find(i)]+=X[i]
    ntest=[]
    for c,a,b in test:
        if dd[T.find(a)]<c:
            flag=False
        else:
            ntest.append((c,a,b))
    if flag:
        break
    test=ntest
print(M-len(test))

### FIXED CODE ###

import sys
input = sys.stdin.readline
from collections import deque
class Unionfindtree:
    def __init__(self, number,L):
        self.par = [i for i in range(number)]
        self.rank = [0] * (number)
        self.count = L

    def find(self, x):  # 親を探す
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def union(self, x, y):  # x,yを繋げる
        px = self.find(x)
        py = self.find(y)
        if px == py:
            return
        if self.rank[px] < self.rank[py]:
            self.par[px] = py
            self.count[py]+=self.count[px]
        else:
            self.par[py] = px
            self.count[px]+=self.count[py]
        if self.rank[px] == self.rank[py]:
            self.rank[px] += 1

    def connect(self, x, y):  # 親が同じかみる
        return self.find(x) == self.find(y)



N,M=map(int,input().split())
X=[int(i) for i in input().split()]
que=[]
table=[[] for i in range(N)]
for i in range(M):
    a,b,c=map(int,input().split())
    a,b=a-1,b-1
    que.append((c,a,b))

que.sort()
T=Unionfindtree(N,X)
ok=deque()
for i in range(M):
    c,a,b=que[i]
    T.union(a,b)
    if T.count[T.find(a)]>=c:
        ok.append(i)

for i in range(M):
    c,a,b=que[i]
    table[a].append((b,c,i))
    table[b].append((a,c,i))

used=[0]*M
def dfs(i,lim):
    used[i]=1
    c,a,b=que[i]
    H=deque()
    H.append(b)
    H.append(a)

    while H:
        x=H.popleft()
        for y,c,i in table[x]:
            if used[i]==1:
                continue

            if c>lim:
                continue
            H.append(y)
            used[i]=1
    return

while ok:
    i=ok.pop()
    if used[i]==0:

        c,a,b=que[i]
        dfs(i,c)

print(M-sum(used))
### BUGGY CODE ###


from collections import defaultdict
import math

def kari():
    map(int, input().split())
    list(map(int, input().split()))


def main():
    high, width = map(int, input().split())
    data = [list(input().replace('#', '0')) for i in range(high)]

    count = '1'
    check_count = '0'
    while 1:
        flg = 0
        for i in range(high):
            for j in range(width):
                if data[i][j] == check_count:
                    kari_i1, kari_i2 = i - 1, i + 1
                    kari_j1, kari_j2 = j - 1, j + 1

                    if kari_i1 >= 0:
                        if data[kari_i1][j] == '.':
                            data[kari_i1][j] = count
                            flg = 1
                    if kari_i2 <= high - 1:
                        if data[kari_i2][j] == '.':
                            data[kari_i2][j] = count
                            flg = 1
                    if kari_j1 >= 0:
                        if data[i][kari_j1] == '.':
                            data[i][kari_j1] = count
                            flg = 1
                    if kari_j2 <= width - 1:
                        if data[i][kari_j2] == '.':
                            data[i][kari_j2] = count
                            flg = 1

        if flg == 0:
            break

        check_count = count
        count = str(int(count) + 1)

    #     print('------')
    #     for i in range(high):
    #         print(''.join(data[i]))
    #
    # print('------')
    # for i in range(high):
    #     print(''.join(data[i]))

    print(check_count)




if __name__ == '__main__':
    main()

### FIXED CODE ###

def main():
    high, width = map(int, input().split())
    data = [list(input()) for i in range(high)]

    now_list = []
    next_list = []

    for i in range(high):
        for j in range(width):
            if data[i][j] == '#':
                now_list.append([i, j])

    count = 0

    while 1:
        for now_data in now_list:
            i, j = now_data

            kari_i1, kari_i2 = i - 1, i + 1
            kari_j1, kari_j2 = j - 1, j + 1

            if kari_i1 >= 0:
                a, b = kari_i1, j
                if data[a][b] == '.':
                    next_list.append([a, b])
                    data[a][b] = '#'

            if kari_i2 <= high - 1:
                a, b = kari_i2, j
                if data[a][b] == '.':
                    next_list.append([a, b])
                    data[a][b] = '#'

            if kari_j1 >= 0:
                a, b = i, kari_j1
                if data[a][b] == '.':
                    next_list.append([a, b])
                    data[a][b] = '#'

            if kari_j2 <= width - 1:
                a, b = i, kari_j2
                if data[a][b] == '.':
                    next_list.append([a, b])
                    data[a][b] = '#'

        now_list = next_list
        next_list = []


        if len(now_list) == 0:
            break
        else:
            count += 1


    print(count)



if __name__ == '__main__':
    main()
### BUGGY CODE ###

S = list(input())
Q = int(input())
for _ in range(Q):
    query = list(map(str, input().split()))
    if query[0]=='1':
        S.reverse()
    else:
        if query[1]=='1':
            S = list(query[2]) + S
        else:
            S = S + list(query[2])

print(''.join(S))

### FIXED CODE ###

S = input()
Q = int(input())
hanten = 1
front = []
back = []
for _ in range(Q):
    q = list(map(str, input().split()))
    if q[0]=='1':
        hanten *= -1
    else:
        if hanten==1:
            if q[1]=='1':
                front.append(q[2])
            else:
                back.append(q[2])
        else:
            if q[1]=='1':
                back.append(q[2])
            else:
                front.append(q[2])


if hanten==1:
    print(''.join(list(reversed(front)) + list(S) + back))
else:
    print(''.join(list(reversed(list(S)+back))+front))
### BUGGY CODE ###

n = int(input())
ans = 10**12
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr

q = factorization(n)
ans_list = []
for i in range(len(q)):
    for j in range(q[i][1]):
        ans_list.append(q[i][0])

p = 1
for i in ans_list:
    p *= i
s = len(ans_list)
for i in range(2 ** s):
    bag = []
    for j in range(s):
        if ((i >> j) & 1): 
            bag.append(ans_list[j]) 
    a = 1
    for t in bag:
        a *= t
    b = p/a
    
    ans = min(ans,a+b-2)
    
print(int(ans))

### FIXED CODE ###

N=int(input())
a=1
for i in range(1,int(N**(1/2))+2):
    if N%i==0:
        a=max(a,i)
print(a+N//a-2)
### BUGGY CODE ###

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

l = []
ans = 0
for s in S:
    st = "".join(sorted(list(s)))
    if st in l:
        ans += l.count(st)
    l.append(st)

print(ans)


### FIXED CODE ###

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

t = {}
ans = 0
for s in S:
    st = "".join(sorted(list(s)))
    if st in t.keys():
        t[st] += 1
        ans += t[st]
    else:
        t[st] = 0

print(ans)
### BUGGY CODE ###

s = input()
n = len(s)
cnt = 0
for length in range(4, n + 1):
    for start_index in range(n - length + 1):
        i = int(s[start_index:(start_index+length)])
        if i % 2019 == 0:
            cnt += 1
print(cnt)

### FIXED CODE ###

s = input()[::-1]
mod_cnt = [0] * 2019
mod_cnt[0] = 1
t = 0
d = 1
for c in s:
    t += int(c) * d
    t %= 2019
    mod_cnt[t] += 1
    d *= 10
    d %= 2019
print(sum([m * (m - 1) // 2 for m in mod_cnt]))
### BUGGY CODE ###

# 移動Aで探索する方向
d_a = [(-1,0),(0,-1),(1,0),(0,1)]
# 移動Bで探索する方向
d_b = list()
for i in range(-2, 3):
    for j in range(-2, 3):
        if (i,j) not in (d_a+[(0,0)]):
            d_b += [(i,j)]

h, w = list(map(int, input().split()))
si, sj = list(map(int, input().split()))
ti, tj = list(map(int, input().split()))
s = [None] * h
for i in range(h):
    s[i] = input()

# 距離情報を一番遠い値で初期化
#import numpy as np
INF = int(1e9)
#dist = np.full((h+4, w+4), INF)
dist = [[INF for j in range(w+4)] for i in range(h+4)]

# マップ境界を壁で囲む
S = ['##{}##'.format(s_row) for s_row in s]
S = ['##{}##'.format('#' * (w+4))]*2 + S
S = S + ['##{}##'.format('#' * (w+4))]*2

# 01-BFS探索用キュー
from collections import deque
q = deque()

# 開始位置、終了位置を調整
si, sj = si+1, sj+1
ti, tj = ti+1, tj+1
dist[si][sj] = 0
q.append((si,sj))

while (len(q)>0):
    i, j = q[0]
    d = dist[i][j]
    q.popleft()
    # 移動Aによるコスト
    for di, dj in d_a:
        di, dj = di+i, dj+j
        if (di, dj) == (ti, tj):
            print(d)
            exit(0)
        if (S[di][dj]=='#'):
            continue # 壁
        if (dist[di][dj]<=d):
            continue # 探索済みコスト以上
        dist[di][dj] = d
        q.appendleft((di, dj))
    # 移動Bによるコスト
    for di, dj in d_b:
        di, dj = di+i, dj+j
        if (S[di][dj]=='#'):
            continue # 壁
        if (dist[di][dj]<=d):
            continue # 探索済みコスト以上
        dist[di][dj] = d+1
        q.append((di, dj))

ans = dist[ti][tj]
if ans == INF:
    ans = -1
print(ans)


### FIXED CODE ###

h, w = list(map(int, input().split()))
si, sj = list(map(int, input().split()))
ti, tj = list(map(int, input().split()))
s = [None] * h
for i in range(h):
    s[i] = input()

# 移動Aで探索する方向
d_a = [(-1,0), (0,-1),(1,0),(0,1)]
# 移動Bで探索する方向
d_b = [ (i, j) for j in range(-2, 3) for i in range(-2, 3)]

# 距離情報を一番遠い値で初期化
#import numpy as np
INF = int(1e9)
#dist = np.full((h+2, w+2), INF)
dist = [[INF for j in range(w+2)] for i in range(h+2)]

# マップ境界と壁を距離0にして探索させない
for i in range(h+2):
    dist[i][0] = 0
    dist[i][-1] = 0
for j in range(w+2):
    dist[0][j] = 0
    dist[-1][j] = 0
for i in range(h):
    for j in range(w):
        if s[i][j]=='#': dist[i+1][j+1] = 0
#dist = [[INF for j in range(w)] for i in range(h)]

# 01-BFS探索用キュー
from collections import deque
q = deque()

# 開始位置
dist[si][sj] = 0
q.append((si,sj))

while (len(q)>0):
    i, j = q[0]
    d = dist[i][j]
    q.popleft()
    # 移動Aによるコスト
    for v in range(len(d_a)):
        di, dj = d_a[v][0]+i, d_a[v][1]+j
        if (di, dj) == (ti, tj):
            print(d)
            exit(0)
        if (dist[di][dj]<=d):
            continue # 探索済みコスト以上
        dist[di][dj] = d
        q.appendleft((di, dj))
    # 移動Bによるコスト
    for v in range(len(d_b)):
        # マップ境界で2以上移動したときに、反対の境界に向かうように剰余を取る
        di, dj = (d_b[v][0]+i)%(h+2), (d_b[v][1]+j)%(w+2)
        if (dist[di][dj]<=d):
            continue # 探索済みコスト以上
        dist[di][dj] = d+1
        q.append((di, dj))

ans = dist[ti][tj]
if ans == INF:
    ans = -1
print(ans)
### BUGGY CODE ###

import collections

n = int(input())
a = list(map(int,input().split()))
ma = max(a)
mi = min(a)

ac = collections.Counter(a)
ans = 0

for i in range(mi,ma+1):
    if ac[i] < i:
        ans += ac[i]
    else:
        ans += ac[i] - i

print(ans)

### FIXED CODE ###

import collections

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

ac = collections.Counter(a)
ans = 0

for i in s:
    if ac[i] < i:
        ans += ac[i]
    else:
        ans += ac[i] - i

print(ans)
### BUGGY CODE ###

import sys
input = sys.stdin.readline
from collections import deque

H,W=map(int,input().split())
sh,sw=map(int,input().split())
gh,gw=map(int,input().split())
MAP=[input().strip() for i in range(H)]

ANS=[[1<<30]*W for i in range(H)]

sh-=1
sw-=1
gh-=1
gw-=1

ANS[sh][sw]=0

Q=deque([(sh,sw,0)])

while Q:
    x,y,sc=Q.popleft()
    if ANS[x][y]<sc:
        continue

    for i,j in [(x-2,y-2),(x-2,y-1),(x-2,y),(x-2,y+1),(x-2,y+2),(x-1,y-2),(x-1,y-1),(x-1,y),(x-1,y+1),(x-1,y+2),(x,y-2),(x,y-1),(x,y+1),(x,y+2),(x+1,y-2),(x+1,y-1),(x+1,y),(x+1,y+1),(x+1,y+2),(x+2,y-2),(x+2,y-1),(x+2,y),(x+2,y+1),(x+2,y+2)]:
        if 0<=i<H and 0<=j<W:
            if MAP[i][j]=="." and abs(i-x)+abs(j-y)==1 and ANS[i][j]>ANS[x][y]:
                ANS[i][j]=ANS[x][y]
                Q.append((i,j,ANS[i][j]))
            elif MAP[i][j]=="." and abs(i-x)+abs(j-y)>1 and ANS[i][j]>ANS[x][y]+1:
                ANS[i][j]=ANS[x][y]+1
                Q.append((i,j,ANS[i][j]))

if ANS[gh][gw]==1<<30:
    print(-1)
else:
    print(ANS[gh][gw])



### FIXED CODE ###

import sys
input = sys.stdin.readline
import heapq

H,W=map(int,input().split())
sh,sw=map(int,input().split())
gh,gw=map(int,input().split())
MAP=[input().strip() for i in range(H)]

ANS=[[1<<30]*W for i in range(H)]

sh-=1
sw-=1
gh-=1
gw-=1

ANS[sh][sw]=0

def ca(sc,x,y):
    return sc*(10**8)+x*(10**4)+y

def inv(w):
    sc=w//(10**8)
    w%=(10**8)
    x=w//(10**4)
    y=w%(10**4)

    return sc,x,y

Q=[ca(0,sh,sw)]

while Q:
    sc,x,y=inv(heapq.heappop(Q))

    if sc>ANS[x][y]:
        continue

    for i,j in [(x-2,y-2),(x-2,y-1),(x-2,y),(x-2,y+1),(x-2,y+2),(x-1,y-2),(x-1,y-1),(x-1,y),(x-1,y+1),(x-1,y+2),(x,y-2),(x,y-1),(x,y+1),(x,y+2),(x+1,y-2),(x+1,y-1),(x+1,y),(x+1,y+1),(x+1,y+2),(x+2,y-2),(x+2,y-1),(x+2,y),(x+2,y+1),(x+2,y+2)]:
        if 0<=i<H and 0<=j<W:
            if MAP[i][j]=="." and abs(i-x)+abs(j-y)==1 and ANS[i][j]>sc:
                ANS[i][j]=sc
                heapq.heappush(Q,ca(ANS[i][j],i,j))
            elif MAP[i][j]=="." and abs(i-x)+abs(j-y)>1 and ANS[i][j]>sc+1:
                ANS[i][j]=sc+1
                heapq.heappush(Q,ca(ANS[i][j],i,j))

if ANS[gh][gw]==1<<30:
    print(-1)
else:
    print(ANS[gh][gw])
### BUGGY CODE ###

import fractions

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

gcd = fractions.gcd(A, B)


ans = 1

for p in range(2, gcd+1):
    if gcd % p == 0:
        ans += 1
        gcd = gcd / p
        while gcd % p == 0:
            gcd = gcd / p
    if p > gcd:
        break
print(ans)

### FIXED CODE ###

import fractions
import math

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

gcd = fractions.gcd(A, B)


ans = 1

max_N = int(math.sqrt(gcd))

for p in range(2, max_N+1):
    if gcd % p == 0:
        ans += 1
        gcd = gcd / p
        while gcd % p == 0:
            gcd = gcd / p
    if p > gcd:
        break
if gcd != 1:
    ans += 1
print(ans)
### BUGGY CODE ###

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

total=0
for i in range(len(a)):
    total=total+(max(a[0:i+1])-a[i])
print(total)

### FIXED CODE ###

def maxof(x, y):
    if x > y:
        return x
    return y

n=int(input())
a = list(map(int, input().strip().split()))
total=0
max_num=0
for i in range(len(a)):
    max_num=maxof(max_num,a[i])
    total=total+max_num-a[i]
print(total)
### BUGGY CODE ###

import math
def com(n,r):
    return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))

from collections import Counter
n=int(input())
a=list(map(int,input().split()))
cnt=Counter(a)
#print(cnt)

ans={}
for i in set(a):
    temp=cnt[i]-1
    ans[i]=0
    for x,y in cnt.items():
        if x!=i and y>=2:
            ans[i]+=(y-1)*y//2
        elif x==i and temp>=2:
            ans[i]+=(temp-1)*temp//2

for i in a:
    print(ans[i])

### FIXED CODE ###

import sys
input=sys.stdin.readline
from collections import Counter
n=int(input())
a=list(map(int,input().split()))
cnt=Counter(a)
#print(cnt)

ans=0
for i in cnt.values():
    ans=ans+i*(i-1)//2

for i in a:
    print(ans-cnt[i]+1)
### BUGGY CODE ###

n = int(input())
a = [int(x) for x in input().split()]

ans=0

a.sort()

while len(a)>0:
    k=a[0]
    kesu=[]
    tyouhuku=False
    for i in range(1,len(a)):
        if a[i]==k:
            tyouhuku=True
        if a[i]%k==0:
            kesu.append(a[i])
    
    for i in range(len(kesu)):
        a=[j for j in a if j !=kesu[i]]
    
    if not tyouhuku:
        a.pop(0)
        ans+=1



print(ans)

### FIXED CODE ###

n = int(input())

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

baisuu=[0]*(10**6+1)

for i in a:
	if baisuu[i]>=1:
		baisuu[i]=2
		continue

	for j in range(i,10**6+1,i):
		baisuu[j]+=1

ans=0

for i in a:
	if baisuu[i]==1:
		ans+=1

print(ans)
### BUGGY CODE ###

N,Q = map(int,input().split())
A = [list(map(int,input().split())) for i in range(N-1)]
B = [list(map(int,input().split())) for i in range(Q)]

G = {}
for i in range(1,N+1):
    G[i] = []

for i in range(N-1):
    G[A[i][0]].append(A[i][1])
    G[A[i][1]].append(A[i][0])

from collections import defaultdict,deque

H = defaultdict(int)

for i in range(Q):
    H[B[i][0]] += B[i][1]

#bfs

r = 0
P = [0]*(N+1)  #ポイント

D = [0]+[-1]*(N)
D[1] = 1
P[1] = H[1]
q = deque([1])

while q:
    n = q.pop()
    for d in G[n]:
        if D[d] != -1:
            continue
        else:
            D[d] = 1
            q.appendleft(d)
            P[d] = P[n] + H[d]
del P[0]

print(*P)

### FIXED CODE ###

N,Q = map(int,input().split())
A = [list(map(int,input().split())) for i in range(N-1)]
B = [list(map(int,input().split())) for i in range(Q)]

G = {}
for i in range(1,N+1):
    G[i] = []

for i in range(N-1):
    G[A[i][0]].append(A[i][1])
    G[A[i][1]].append(A[i][0])

from collections import deque

H = {}
for i in range(1,N+1):
    H[i] = 0

for i in range(Q):
    H[B[i][0]] += B[i][1]

#bfs

r = 0
P = [0]*(N+1)  #ポイント

D = [0]+[-1]*(N)
D[1] = 1
P[1] = H[1]
q = deque([1])

while q:
    n = q.pop()
    for d in G[n]:
        if D[d] != -1:
            continue
        else:
            D[d] = 1
            q.appendleft(d)
            P[d] = P[n] + H[d]
del P[0]

print(*P)