Harryxun/stf_run huggingface dataset:
from collections import defaultdict, deque
import sys
#input = sys.stdin.readline
def inpl(): return list(map(int, input().split()))
N = int(input())
A = sorted(inpl())

print(sum(map(abs, A[1:])) - A[0])
for i in range(N-2, 0, -1):
  a = A[i]
  if a > 0:
    print(A[0], a)
    A[0] -= a
    else:
      print(A[-1], a)
      A[-1] -= a
      print(A[-1], A[0])

### FIXED CODE ###

from itertools import accumulate
from bisect import bisect, bisect_left
from functools import reduce
from itertools import accumulate, product
from operator import itemgetter, xor, mul
from math import sqrt
from random import randint, random
from heapq import heappop, heappush

from fractions import gcd
from collections import Counter, deque, defaultdict

import sys
input = sys.stdin.readline
from sys import setrecursionlimit
setrecursionlimit(10**9)

def inpl(): return list(map(int, input().split()))
N = int(input())
A = sorted(inpl())
l = bisect_left(A, 0)
if l == 0:
    ans = []
    for i in range(N-2, 0, -1):
        ans.append("{} {}".format(str(A[0]), str(A[i])))
        A[0] -= A[i]
    ans.append("{} {}".format(str(A[-1]), str(A[0])))
    score = A[-1] - A[0]

elif l == N:
    ans = []
    for i in range(N-1):
        ans.append("{} {}".format(str(A[-1]), str(A[i])))
        A[-1] -= A[i]
    score = A[-1]

else:
    ans = []
    for i in range(l, N-1):
        ans.append("{} {}".format(str(A[0]), str(A[i])))
        A[0] -= A[i]
    for i in range(l):
        ans.append("{} {}".format(str(A[-1]), str(A[i])))
        A[-1] -= A[i]
    score = A[-1]
print(score)
print(*ans, sep="\n")
a,b=map(str,input().split())
ab=int(a+b)
if (ab**.5).is_integer() = "True":
  print("Yes")
else:
  print("No")

### FIXED CODE ###

a,b=map(str,input().split())
ab=int(a+b)
if (ab**.5).is_integer():
  print("Yes")
else:
  print("No")
from itertools import accumulate
N, M = map(int, input().split())
 
AB = []
 
for i in range(N):
    a, b = map(int, input().split())
    AB.append([a, b])

AB.sort(key=lambda x: x[0])
whole = [0]*sum([i[1] for i in AB])
cnt = 0
 
for ab in AB:
    for b in range(ab[1]):
        whole[cnt] = ab[0]
        cnt += 1

ans = list(accumulate(whole))

print(ans[M-1])

### FIXED CODE ###

N, M = map(int, input().split())

AB = []

for i in range(N):
    a, b = map(int, input().split())
    AB.append([a, b])

AB.sort()

ans = 0
cnt = 0

for ab in AB:
    for b in range(ab[1]):
        ans += ab[0]
        cnt += 1
        if cnt==M:
            break
    if cnt==M:
        break

print(ans)
a = list(input())
b = []
for i in a:
    if a.count(i) >= len(a)/2:
        b = [j for j ,x in enumerate(a) if x == i]
    else:
        pass
if not b:
  print(-1,1)
else:
  print(b[0],b[1])

### FIXED CODE ###

a = int(input())
b = 0
for i in range(a+1):
    b += int(i)
print(b)
def cal(n):
  ans = 0
  while n != 0:
    ans += n % 10
    n /= 10
  return ans

n = int(raw_input())
if n % cal(n) == 0:
  print "Yes"
else:
  print "No"


### FIXED CODE ###

s = raw_input()
n = 0
for c in s:
  if c == '+':
    n += 1
  else:
    n -= 1
print n
from collections import deque

w = collections.deque()

### FIXED CODE ###

from collections import deque

def isPa(w,k,p):
    tr = 0
    c = 0
    d = deque(w)
    while(d):
        tmp = d.popleft()
        if(tmp > p):
            return False
        
        if( tr + tmp > p):
            tr = tmp
            c += 1
        else:
            tr += tmp
        if c+1>k:
            return False
    return True

def MaxInP(w,k,p):
    maxinp = 0
    tmp = 0
    d = deque(w)
    while(d):
        tmp += d.popleft();
        if(tmp > p):
            if(tmp > maxinp):
                maxinp = tmp
            tmp = 0
    return maxinp

n,k = [int(x) for x in input().split()]
w = deque()
for i in range(n):
    w.append(int(input()))
    
mean = int(sum(w)/k)
maxinp = MaxInP(w,k,mean)

if k == 1:
    print(mean)
else:
    minP = mean
    maxP = maxinp
    while(True):
        m = int ((minP + maxP)/2)
        if ( isPa(w,k,m) ):
            maxP = m
        else:
            minP = m
        if (minP+1 == maxP or minP == maxP):
            print(maxP)
            break
import copy
def main():
    [N,W]=input().split(" ")'''read from keyboard and put all the item to box'''
    N=int(N)
    W=int(W)
    box=[]
    for i in range(N):
        [vi,wi]=input().split(" ")
        box.append([float(vi),float(wi)])
        
    assemblebox=[]'''creat a set of full permutation of input. using recursion'''
    assemblebox=permutations(N,box,assemblebox,[])

    maximum=0'''optimization'''
    for i in range(len(assemblebox)):
        weight=0
        value=0
        for j in range(N):
            weight=weight+assemblebox[i][j][1]
            if(weight>W):
                break
            value=value+assemblebox[i][j][0]
        if(maximum<value):
            maximum=value
            
    print(maximum)'''output'''
        

    return 0

def permutations(N,box,assemblebox,record):
    for i in range(N):
        Np=N
        boxp=copy.deepcopy(box)
        recordp=copy.deepcopy(record)
        recordp.append(box[i])
        boxp.pop(i)
        Np=Np-1
        if(Np==0):
            assemblebox.append(recordp)
        else:
            result=permutations(Np,boxp,assemblebox,recordp)
    return assemblebox

main()

### FIXED CODE ###

def main():
    [N,W]=input().split(" ")
    N=int(N)
    W=int(W)
    inputset=[]
    for i in range(N):
        [vi,wi]=input().split(" ")
        inputset.append([int(vi),int(wi)])

    graph=[]
    for i in range(N+1):
        graph.append([])
        for j in range(W+1):
            graph[i].append(0)

    for i in range(N):
        for j in range(W+1):
            if(inputset[i][1]>j):
                graph[i+1][j]=graph[i][j]
            else:
                rest=j-inputset[i][1]
                choosevalue=graph[i][rest]+inputset[i][0]
                nonchoosevalue=graph[i][j]
                if(choosevalue>nonchoosevalue):
                    graph[i+1][j]=choosevalue
                else:
                    graph[i+1][j]=nonchoosevalue

    print(graph[N][W])

main()
from collections import defaultdict


def rec(node):
    if dp[node]:
        return dp[node]

    res = 0
    for next_node in edge[node]:
        res = max(res, rec(next_node) + 1)

    dp[node] = res
    return res


n, m = map(int, input().split())
dp = [0] * (n + 1)
edge = defaultdict(list)

for i in range(m):
    x, y = map(int, input().split())
    edge[x].append(y)

ans = 0
for i in range(1, n + 1):
    ans = max(ans, rec(i))

print(ans)


### FIXED CODE ###

from collections import defaultdict
import sys

sys.setrecursionlimit(10 ** 6)


def rec(node):
    if memo[node]:
        return memo[node]

    res = 0
    for next_node in edge[node]:
        res = max(res, rec(next_node) + 1)

    memo[node] = res
    return res


n, m = map(int, input().split())
memo = [0] * (n + 1)
edge = defaultdict(list)

for i in range(m):
    x, y = map(int, input().split())
    edge[x].append(y)

ans = 0
for i in range(1, n + 1):
    ans = max(ans, rec(i))

print(ans)
i = int(input())
if i >= 1 and 100 >= i and i % 2 == 0:  
  s = input()
    if i == len(s) and s.isalpha() and s.islower():  
      t_0 = s[0]
      t_1 = s[i/2]
	  for j in range(1, i/2):
	    t_0 += s[j]
      for k in range(i/2 + 1, i):
        t_1 += s[k]
      if t_0 == t_1:
        print("Yes")
      else:
        print("No")


### FIXED CODE ###

i = int(input()) 
s = input()
if i >= 1 and 100 >= i and i % 2 == 0 and i == len(s) and s.isalpha() and s.islower():
  n = (int)(i/2)
  t_0 = s[0]
  t_1 = s[n]
  for j in range(1, n):
    t_0 += s[j]
  for k in range(n + 1, i):
    t_1 += s[k]
  if t_0 == t_1:
    print("Yes")
  else:
    print("No")
else:
  print("No")
N,A,B = map(int,input().split())
color = ''
while True:
    if len(color) < N:
        color = color + 'b' * A + 'r' * B
    else:
        break
num = color[:N].count('b')
print(num)

### FIXED CODE ###

N, A, B = map(int, input().split())
num = N // (A + B) + 1
zan = num * (A + B) - N

if zan <= B:
    print(num * A)
else:
    print(num * A + B - zan)
n,a,b = [int(i) for i in input().split()]
if (a＊n < b) :
  print(a＊n)
else :
  print(b)

### FIXED CODE ###

n,a,b = [int(i) for i in input().split()]
if (a*n < b) :
  print(a*n)
else :
  print(b)
S, T = input().split()
cnt = 0
for i in range(len(S)):
  if S[i] != T[i]:
    cnt += 1
print(cnt)

### FIXED CODE ###

S = input()
T = input()
cnt = 0
for i in range(len(S)):
  if S[i] != T[i]:
    cnt += 1
print(cnt)
n, x = map(int, input().split())
l = list(map(int, input().split()))

count = 0
d = 0

while d < x and count <= n:
  d += l[count]
  count += 1
  if d == x:
    count += 1

print(count)

### FIXED CODE ###

n, x = map(int, input().split())
l = list(map(int, input().split()))

count = 0
d = 0

for i in range(n):
  d += l[count]
  count += 1
  if d >= x:
    if d == x:
      count += 1
    break

print(count) if d>=x else print(count+1)
N, M, X = map(int, input().split(" "))
A = map(int, raw_input().split(" "))
print(min([len([i for i in A if i >= X+1]), len([i for i in A if i <= X-1])]))

### FIXED CODE ###

N, M, X = map(int, input().split(" "))
A = map(int, input().split(" "))
left, right = 0,0
for ai in A:
  if ai < X: left+=1
  else: right+=1
print(min(right, left))
a = int(input())
s = input()

if a >= 3200:
  print(s)
elif a < 3200:
  print(red)
  

### FIXED CODE ###

a = int(input())
s = input()

if a >= 3200:
  print(s)
elif a < 3200:
  print("red")
import collections
import sys
from functools import lru_cache
sys.setrecursionlimit(20000)
n, m  = [int(i) for i in input().strip().split(' ')] 
edges = collections.defaultdict(list)
for i in range(m):
    s,d = [int(i) for i in input().strip().split(' ')]
    edges[s-1].append(d-1) 
# dp = [-1]*n
maxPath = 0 
@lru_cache(None)
def dfs(node): 
    # global dp
    ans = 0 
    for i in edges[node]:
        ans = max(ans,dfs(i)+1)
    # dp[node] = ans
    return ans
for i in range(n):
    maxPath = max(dfs(i), maxPath) 
print(maxPath)


### FIXED CODE ###

import collections
import sys
from functools import lru_cache
sys.setrecursionlimit(int(10**9))
n, m  = [int(i) for i in input().strip().split(' ')] 
edges = collections.defaultdict(list)
for i in range(m):
    s,d = [int(i) for i in input().strip().split(' ')]
    edges[s-1].append(d-1) 
# dp = [-1]*n
maxPath = 0 
@lru_cache(None)
def dfs(node): 
    ans = 0 
    for i in edges[node]:
        ans = max(ans,dfs(i)+1)
    return ans

for i in range(n):
    maxPath = max(dfs(i), maxPath) 
print(maxPath)
N, A, B = map(int, input().split())

if N * A > B:
  print(B)
else
  print(N * A)

### FIXED CODE ###

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

if N * A > B:
  print(B)
else:
  print(N * A)
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 14 09:20:52 2020

@author: zkwbl7u
"""

def read_int(): return int(input())
N = read_int()
def read_str(): return input().strip()
S = list(read_str())

__DEBUG__ = False

def solution():
    count = 0    
    for i in range(N-2):
        if __DEBUG__: print(f"checking {S[i]} at index: {i}")
        if S[i] == 'A':
            if S[i+1] == 'B' and S[i+2] == 'C':
                count += 1
                # continue
    return count

    
if __name__ == '__main__':
    print(solution())
    


### FIXED CODE ###

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


count = 0    
for i in range(N-2):
  if S[i] == 'A':
    if S[i+1] == 'B' and S[i+2] == 'C':
      count += 1
    
print(count)
s = input()
if s[-2] == s[-1]:
    print(len(s)-1, len(s))
    exit()

for i in range(s-2):
    if s[i] == s[i+1] or s[i] == s[i+2]:
        print(i+1, i+3)
        exit()
print(-1 - 1)


### FIXED CODE ###

s = input()

if len(s) == 2 and s[0] == s[1]:
    print(1, 2)
    exit()

for i in range(len(s)-2):
    if len(set(s[i:i+3])) != 3:
        print(i+1, i+3)
        exit()
print(-1, -1)
a,b,c,d = map(int, input().split())
if a =b or a=c or a=d or b =c or c = d or b= d:
  print("Good")
else:
  print("Bad")

### FIXED CODE ###

n = int(input())
 
a = int(n/1000)
b = int(n/100) -a*10
c = int(n/10)-a*100 - b*10
d = n -a*1000-b*100-c*10
 
if a == b  or b ==c or c == d :
  print("Bad")
else:
  print("Good")
aaaaaaaaaaaaaaab
aaaaaaaaaaaaaaab

### FIXED CODE ###

import sys

s = list(input())
t = list(input())

for i in range(len(s)):    
    if s == t:
        break

    tmp = s.pop(len(s)-1)
    s.insert(0, tmp)

if s == t:
    print("Yes")
else:
    print("No")
N, M = map(int, input().split())
s = [list(map(int, input().spllit())) for i in range(M)]
p = list(map(int, input().split()))
total = 0
flag = 1
for i in range(2 ** N):
  now = format(i, "0{}b".format(n))
  for k in range(M):
    tmp = 0
    for l in range(s[k][0]):
      if now[s[k][l+1]-1] == '1':
        tmp += 1
    if tmp % 2 == p[k]:
      continue
    else:
      flag = 0
      break
  if flag == 1:
    total += 1
  flag = 1
print(total)


### FIXED CODE ###

n, m = map(int, input().split())
switch = []
for i in range(m):
    switch.append(list(map(int, input().split())))
p = list(map(int, input().split()))

ans = 0
for i in range(2**n):
    i = format(i, "0{}b".format(n))
    flag = 1
    for j in range(m):
        count = 0
        for k in range(switch[j][0]):
            if i[switch[j][k+1]-1] == "1":
                count += 1
        if count % 2 == p[j]:
            continue
        else:
            flag = 0
            break
    if flag == 1:
        ans += 1
print(ans)
n = int(input())
a = list(map(int, input().split()))
import sympy
from collections import Counter
ans = 0
for i in range(n):
    if len(set(a) & set(sympy.divisors(a[i])))-1 == 0:
        ans += 1
for i in Counter(a).values():
    if i > 1:
        ans -= i

print(ans)


### FIXED CODE ###

N = int(input())
A = list(map(int, input().split()))
M = max(A)+1
cnt = [0]*M
ans = 0
for a in A:
    if cnt[a] != 0:
        cnt[a] = 2
    else:
        for i in range(a, M, a):
            cnt[i] += 1
for a in A:
    if cnt[a] == 1:
        ans += 1
print(ans)
import math

a,b,x = map(int, input().split())


if a**2*b/2<=x:
    ans = 90-math.degrees(math.atan(a**3/2/(a**2*b-x)))
else:
    ans = 90-math.degrees(math.atan(2*x/a/b**2))
print(ans)

### FIXED CODE ###

import math

a,b,x = map(int, input().split())

if x==a**2*b:
    print(0)
    exit()

if a**2*b/2<=x:
    ans = 90-math.degrees(math.atan(a**3/2/(a**2*b-x)))
else:
    ans = 90-math.degrees(math.atan(2*x/a/b**2))
print(ans)
import itertools
import numpy as np

N = int(input())
lst = np.array(list(map(int,input().split())))

lst2 = np.array(list(itertools.combinations(lst,2)))
lst3 =[]
for i in range(len(lst2):
               dot = np.dot(lst2[i][0],lst2[i][1])
               lst3.append(dot)

print(sum(lst3))

### FIXED CODE ###

import itertools
import numpy as np

N = int(input())
lst = np.array(list(map(int,input().split())))
lst2 = np.array(list(itertools.combinations(lst,2)))
lst3 =[]

for i in range(len(lst2)):
               dot = np.dot(lst2[i][0],lst2[i][1])
               lst3.append(dot)

print(sum(lst3))
W, H, x, y = map(int, input().split())
if x == 0 and y == 0:
	print(0, 1)
	exit()

s1 = float(min(x*H, W*H-x*H))
s2 = float(min(y*W, W*H-y*W))
s3 = float(min((y/x*W * W * 0.5), H/((-1*(H-y)/x) + H) * H * 0.5))

b = 0
if s1 != s2 and s2 != s3 and s1 != s3:
	b = 0
else:
	b = 1
    
print(max(s1, s2, s3), b)

### FIXED CODE ###

W, H, x, y = [int(i) for i in input().split()]
print(W*H / 2, int(x*2 == W and y*2 == H))
a,b=map(int, input().split())

if a+b=<16:
  print('Yay!')
else:
  print(':(')

### FIXED CODE ###

a,b=map(int, input().split())
if a<=8 and b<=8:
  print('Yay!')
else:
  print(':(')
a, b = map(int,input().split())
if a + b = 15:
  print("+")
elif a * b = 15:
  print("*")
else:
  print("x")

### FIXED CODE ###

a, b = map(int,input().split())
if a + b == 15:
  print("+")
elif a * b == 15:
  print("*")
else:
  print("x")
A, B = map(int, input().split())

if A>B:
  print("GREATER")
elif A<B:
  print("LESS")
else:
  print("EQUAL")

### FIXED CODE ###

A = int(input())
B = int(input())

if A>B:
  print("GREATER")
elif A<B:
  print("LESS")
else:
  print("EQUAL")
S = input()
dp = [[0]*13 for _ in range(len(S))]
mod = 10**9+7
for i in range(13):
    if S[0] == "?":
        dp[0][i] = 1 if i <= 9 else 0
    else:
        dp[0][i] = 1 if int(S[0]) == i else 0
for i in range(1,len(S)):
    if S[i] == "?":
        for j in range(10):
            for k in range(13):
                dp[i][(k*10+j) % 13] = (dp[i][(k*10+j) % 13]+dp[i-1][k])%mod
    else:
        j = int(S[i])
        for k in range(13):
            dp[i][(k*10+j)%13] = (dp[i][(k*10+j)%13]+dp[i-1][k])%mod
print(dp[len(S)-1][5])

### FIXED CODE ###

A,B = map(int,input().split())
if A == B:
    print(0)
else:
    if (A+B) % 2 == 0:
        print((A+B)//2)
    else:
        print("IMPOSSIBLE")
a, b = (int(i) for i in input().split())  
sum=a+b
dif=a-b
mul=a*b


if sum > dif:
	if mul>sum:
    	print("{}".format(mul))
	else:
    	print("{}"format(sum))
else:
	if mul>dif:
    	print("{}".format(mul))
	else:
    	print("{}"format(dif))


### FIXED CODE ###

a, b = (int(i) for i in input().split())  
sum=a+b
dif=a-b
mul=a*b
 
 
if sum > dif:
    if mul>sum:
        print('{}'.format(mul))
    else:
        print('{}'.format(sum))
else:
    if mul>dif:
        print('{}'.format(mul))
    else:
        print('{}'.format(dif))
N = int(input())
H = list(map(int, input().split()))
counts = []

for i in range(N):
    if i == N-1:
        break
    count = 0
    for j in range(i+1, N):
        if H[i] < H[j]:
            break
        count += 1
    counts.append(count)

print(max(counts))

### FIXED CODE ###

N = int(input())
H = list(map(int, input().split()))
count = 0
counts = []

if N <= 1:
    counts.append(0)
else:
    for i in range(N-1):
        if H[i] >= H[i+1]:
            count += 1
        else:
            counts.append(count)
            count = 0
    else:
        counts.append(count)

print(max(counts))
w,h,x,y = map(double, input().split())
if  w/2 = x && h/2 = y:
	ans = 1
else:
	ans = 0
print (str(w*h/2) + " " + str(ans))

### FIXED CODE ###

w,h,x,y = map(int, input().split())
if  w/2 == x and h/2 == y:
	ans = 1
else:
	ans = 0
print (w*h/2.0,ans)
# input
A, B = map(int, input().split())

# check
ans = -1
for n in range(100 / 0.1, -1, -1):
    if int(n * 0.08) == A and int(n * 0.1) == B:
        ans = n

print(ans)

### FIXED CODE ###

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

# check
ans = -1
for n in range(1000, -1, -1):
    if int(n * 0.08) == A and int(n * 0.1) == B:
        ans = n

print(ans)
n = int(input())

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

s = 1
for i in a:
    s = s*i
if s > 10**18:
    print('-1')
else:
    print(s)

### FIXED CODE ###

n = int(input())

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

s = 1
for i in a:
    s = s*i
    s = min(s,10**18+1)
if s == 10**18+1:
    print('-1')
else:
    print(s)
x,n = map(int,input().split())
nump = list(map(int,input().split()))
if(n ==0):
    print(x)
else:
    list_io = [0]*100
    for i in range(n):
        list_io[nump[i]-1] = 1
    # print(list_io)
    for i in range(1,50):
        if(list_io[(x-1)-i] == 0):
            print(x-i)
            break
        if(list_io[(x-1)+i] == 0):
            print(x+i)
            break
    # print("?")

### FIXED CODE ###

x,n = map(int,input().split())
nump = list(map(int,input().split()))
nump.sort()
x_ind = -1
nump_io = [0]*103
for i in range(n):
    nump_io[nump[i]-1] = 1
    if(nump[i]==x):
        x_ind = i
if(x_ind == -1):
    print(x)
else:            
    for i in range(1,51):
        if(nump_io[(x-1)-i] == 0):
            print(x-i)
            break
        if(nump_io[(x-1)+i] == 0):
            print(x+i)
            break
# print(nump_io)
a,b,c=input()
if a=='7' or b=='7' or c=='7';
  print(Yes)
else;
  print(No)

### FIXED CODE ###

N=input()
if N[0]=='7' or N[1]=='7' or N[2]=='7':
    print('Yes')
else:
    print('No')
import math
from scipy.special import comb
X, Y = map(int,input().split())
a=(-X+2*Y)/3
b=(2*X-Y)/3
if a.is_integer()==False or b.is_integer()==False:
    print(0)
else:
    print(comb((int(a+b),int(a))))
    

### FIXED CODE ###

p = 1000000007

X, Y = map(int, input().split())

if (X + Y) % 3 != 0:
    print(0)
    exit()

a = (2 * Y - X) // 3
b = (2 * X - Y) // 3

if a < 0 or b < 0:
    print(0)
    exit()

n = a + b
fac = [0] * (n + 1)
fac[0] = 1
for i in range(n):
    fac[i + 1] = fac[i] * (i + 1) % p


def mcomb(n, k):
    if n == 0 and k == 0:
        return 1
    if n < k or k < 0:
        return 0
    return fac[n] * pow(fac[n - k], p - 2, p) * pow(fac[k], p - 2, p) % p


print(mcomb(n, a))
#他者の解答を参考にした
# coding:utf-8

N = int(input())
H = [int(hi) for hi in input().split()]

# 地点iへの最小コストを計算する関数
def dp(i):
    if i == 1:
        return 0
    elif i == 2:
        return abs(H[0]-H[1])
    else:
        dp_i_1 = dp(i-1)+abs(H[i-2]-H[i-1])
        dp_i_2 = dp(i-2)+abs(H[i-3]-H[i-1])
        return min(dp_i_1, dp_i_2)

cost = dp(N)
print(cost)

### FIXED CODE ###

# coding:utf-8

N = int(input())
H = [int(hi) for hi in input().split()]
dp = [0]*N
dp[1] = abs(H[0]-H[1])

for i in range(2, N):
    dp[i] = min(abs(H[i]-H[i-2])+dp[i-2],abs(H[i]-H[i-1])+dp[i-1])
    
print(dp[N-1])
A, B, N = map(int, input().split())

def f(x):
    return int(A*x/B)-A*int(x/B)

X = N
Y = min(B-1,N)

print(min(f(X),f(Y))

### FIXED CODE ###

a, b, n = map(int, input().split())
x = min(b-1, n)
ans = int((a*x)/b) - a*int(x/b)
print(ans)
a = list(input())
for i in range(3):
  if a[i] = "1":
    a[i] = "9"
  else:
    a[i] = "1"
print("".join(a))


### FIXED CODE ###

a = list(input())
for i in range(3):
  if a[i] == "1":
    a[i] = "9"
  else:
    a[i] = "1"
print("".join(a))
A,B=map(int,input().split())

if (A+B)%2=1:
  print("IMPOSSIBLE")

else:
  print((A+B)/2)

### FIXED CODE ###


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

if (A+B)%2==1:
  print("IMPOSSIBLE")

else:
  print(int((A+B)/2))
N,D=input().split(" ")
count=0
for n in range(int(N)):
  x,y=input().split(" ")
  if pow(pow(x,2)+pow(y,2),0.5)<=int(D):
    count+=1
print(count)


### FIXED CODE ###

N,D=input().split(" ")
count=0
for n in range(int(N)):
  x,y=input().split(" ")
  if pow(pow(int(x),2)+pow(int(y),2),0.5)<=int(D):
    count+=1
print(count)
#coding:utf-8
n = input()
n =  int(n)
j = 0
f = 0
li = []
while n > 0:
    n -= 1
    f = input()
    f = int(f)
    li.append(f)
mn = min(li)
mx = max(li)
sm = sum(li)
print(mn,mx,sm)

### FIXED CODE ###

n=int(input())
l=list(map(int,input().split()))
print(min(l),max(l),sum(l))
'''
Problem Link - https://atcoder.jp/contests/abc176/tasks/abc176_c
'''
from sys import stdin,stdout
from math import *
from collections import *

mod = 1000000007

def gcd(a,b):
	while b:
		a,b = b,a%b
	return a

def lcm(a,b): return (a*b) // gcd(a,b)

def set_bits(X):
	c = 0
	while X:
		X &= (X-1);c += 1
	return c

def compute_MOD(N,M): return (N%M + M) % M

def get_array(): return list(map(int, stdin.readline().split()))
 
def get_ints(): return map(int, stdin.readline().split())

def get_int(): return int(stdin.readline())

def get_input(): return stdin.readline().strip()

def main():
    tc = get_int()
    while(tc):
    	arr = get_array()
    	ans = 0
    	Max = arr[0]
    	for i in arr[1:]:
    		Max = max(Max,i)
    		ans += Max-i
    	stdout.write(str(ans)+'\n')
    	tc-=1

if __name__ == "__main__":
    main()

### FIXED CODE ###

'''
Problem Link - https://atcoder.jp/contests/abc176/tasks/abc176_c
'''
from sys import stdin,stdout
from math import *
from collections import *

mod = 1000000007

def gcd(a,b):
	while b:
		a,b = b,a%b
	return a

def lcm(a,b): return (a*b) // gcd(a,b)

def set_bits(X):
	c = 0
	while X:
		X &= (X-1);c += 1
	return c

def compute_MOD(N,M): return (N%M + M) % M

def get_array(): return list(map(int, stdin.readline().split()))
 
def get_ints(): return map(int, stdin.readline().split())

def get_int(): return int(stdin.readline())

def get_input(): return stdin.readline().strip()

def main():
    tc = get_int()
    arr = get_array()
    ans = 0
    Max = arr[0]
    for i in arr[1:]:
    	Max = max(Max,i)
    	ans += Max-i
    stdout.write(str(ans)+'\n')
if __name__ == "__main__":
    main()