Problem:


Score : 300 points

Problem Statement

N persons are standing in a row. The height of the i-th person from the front is A_i.

We want to have each person stand on a stool of some heights - at least zero - so that the following condition is satisfied for every person:

Condition: Nobody in front of the person is taller than the person. Here, the height of a person includes the stool.

Find the minimum total height of the stools needed to meet this goal.

Constraints

1 \leq N \leq 2\times 10^5

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 \ldots A_N

Output

Print the minimum total height of the stools needed to meet the goal.

Sample Input 1

5
2 1 5 4 3

Sample Output 1

4

If the persons stand on stools of heights 0, 1, 0, 1, and 2, respectively, their heights will be 2, 2, 5, 5, and 5, satisfying the condition.

We cannot meet the goal with a smaller total height of the stools.

Sample Input 2

5
3 3 3 3 3

Sample Output 2

0

Giving a stool of height 0 to everyone will work.



Sample input:
5
2 1 5 4 3


Sample output:
4

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
import copy
b = copy.copy(a)
tall = a[0]
for i in range(n):
    if a[i]<tall:
        a[i] = tall
        cnt +=tall-a[i]
    elif a[i]>tall:
        tall = a[i]
print(sum(a)-sum(b))

Submission status: Runtime Error

Solution code:
N = int(input())
A = [map(int, input().split())]
d = 0
for n in range(0, N-1):
    if A[n+1] < A[n]:
        d += A[n]-A[n+1]
        A[n+1] = A[n]
print(d)

Submission status: Runtime Error

Solution code:
# coding: utf-8
import math

num = int(input())
total = 0

tall = list(map(int, input().split()))
for i in range(num):
    if i != num-1:
    	num1 = tall[i]
    	num2 = tall[i+1]
    if num1 > num2:
        total += num2 - num1

print(abs(total))

Submission status: Runtime Error

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

h =[]

if a[0] > a[1]:
    h.append(a[0] - a[1])
    a[1] += (a[0]- a[1]) 
else:
    h.append(0)
    
for i in range(2,n):
    if a[i-1] > a[i]:
        h.append(a[i-1]- a[i])
        a[i] += (a[i-1]- a[i]) 
    else:
        h.append(0)
        
print(sum(h))

Submission status: Runtime Error

Solution code:
n = int(input())
l = list(map(int,input().split()))
m = []
for i in range(1,n+1):
  if l[i-1]>l[i]:
     m.append(l[i-1]-l[i])
    l[i] = l[i-1]

print(sum(m))

Submission status: Runtime Error

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

cnt = 0
for i in range(1,5):
    #print(cnt)
    if a[i] < a[i-1]:
        cnt = cnt + (a[i-1] - a[i])
        a[i] = a[i-1]
        #print(cnt)
print(cnt)

Submission status: Runtime Error

Solution code:
n=int(input())

a=list(map(int,input().split()))
  
b=[]
for j in range (n-1):
  c=a[j+1]-a[j]
  b.append(c)

if max(b)>-1:
  print(max(b))
else:
  print(0)

Submission status: Runtime Error

Solution code:
n = int(input())
l = list(map(int,input().split()))
m = []
for i in range(n):
  if l[i]>l[i+1]:
     m.append(l[i]-l[i+1])
    l[i+1] = l[i]

print(sum(m))

Submission status: Runtime Error

Solution code:
N = int(input())
L = list(map(int, input().split()))
if N == 1:
  print(0)
else:
  x = 0
  for i in range(0,len(L)-1):
    if L[i] > L[i + 1]:
      x = x + (L[i] - L[i + 1])
      L[i + 1] = L[i + 1] + (L[i] - L[i + 1])
print(x)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(input().split()))
count=0
for i in range(N):
  try:
    a=A[i]
    b=A[i+1]
    while a>b:
      b+=1
      count+=1
  except:
    pass
print(count)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split(' ')))

cnt = 0

for i in range(1,len(A)):
  if A[i] < A[i-1]:
	cnt += A[i-1] - A[i]
    A[i] = A[i-1]
    
print(cnt)

Submission status: Runtime Error

Solution code:
n=int(input())

a=list(map(int,input().split()))
  
b=[]
for j in range (n-1):
  c=a[j+1]-a[j]
  b.append(c)
  
print(max(b))

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
sum=0
for i in range(N-1):
  if A[i] < A[i+1]:
    continue
  else:
    sa = A[i]-A[i+1]+1
    A[i+1]=A[i]+1
    sum += sa
print(sa)

Submission status: Runtime Error

Solution code:
n = int(input())
l = list(map(int,input().split()))
ans = 0
for i in range(n):
  if l[i]>l[i+1]:
    ans = ans + l[i]-l[i+1]
    l[i+1] = l[i]

print(ans)

Submission status: Runtime Error

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

h =[]

if a[0] > a[1]:
    h.append(a[0] - a[1])
else:
    h.append(0)
    
for i in range(2,n):
    if a[i-1] > a[i]:
        h.append(a[i-1]- a[i])
        a[i] += (a[i-1]- a[i]) 
    else:
        h.append(0)
        
print(sum(h))

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
step=0
for i in range(n-1):
  if a[i]>a[i+1]:
    humi+=a[i]-a[i+1]
    a[i+1]=a[i]
print(step)

Submission status: Runtime Error

Solution code:
a = int(input())
b = list(map(int,input().split()))
bmax = 0
ans = 0
for i in range(a):
    if i == 0:
        bmax = a[i]
    else:
        if bmax < a[i]:
            bmax = a[i]
        elif bmax > a[i]:
            ans += bmax - a[i]
print(str(ans))

Submission status: Runtime Error

Solution code:
n = int(input())

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

n = max(a)

highest = 0
last = 0
for i in range(n):
    if a[i] >= highest:
        highest = a[i]
        last = i

shortest = highest
for j in range(0, i):
    if a[j] <= shortest:
        shortest = a[j]
        #print(shortest)

print(highest - shortest)

Submission status: Runtime Error

Solution code:
n = int(input())
arr = map(int, input().split())
cmp = arr[0]
ans = 0

for i in range(1,len(arr)):
  cmp = max(arr[i],cmp)
  add = cmp - arr[i]
  ans = ans + add

print(ans)

Submission status: Runtime Error

Solution code:
N=int(input())
A=list(map(int, input().split()))
B=[0 for _ in range(10001)]
C=A
for i in range(1,N):
    M=max(C[:i])
    if M>A[i]:
        B[i]=M-A[i]
        C[i]=M
print(sum(B))

Submission status: Runtime Error

Solution code:
5
3 3 3 3 3

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
#define ll long long
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t, n, ma = -1;
    ll ans = 0;
    cin >> n;
    while(n--){
        cin >> t;
        ma = max(ma, t);
        ans += (ma - t);
    }
    cout << ans;
    return 0;
}

Submission status: Runtime Error

Solution code:
N = int(input())
a = [int(s) for s in input().split()]
b = int(0)

for i in range(N):
  if a[i] <= a[int(i+1)]:
    pass
  else:
    b = b + (a[i] - a[int(i+1)])
    a[int(i+1)] = a[i]

print(b)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
M = A[0]
ans = 0
for i in range(1,N):
  if M > A[i]:
    ans += M-A[i]
  else:
    M = A[i]
print(ans)v

Submission status: Runtime Error

Solution code:
n = input()
A = list(map(int,input().split()))
ans = 0 
temp = 0
for i in range(n):
    if A[i]<temp:
        ans += temp-A[i]
    else:
        temp = max(temp,A[i])

print(ans)

Submission status: Runtime Error

Solution code:
N=int(input())
A=list(map(int,input().split()))
answer=0
for n in range(N-1):
    if A[n]>A[n+1]:
        sa=A[n]-A[n+1]
        answer+=sa
        A[n+1]=A[n]
if A[-2]>A[-1]:
    answer+=A[-2]-A[-1]
print(answer)

Submission status: Runtime Error

Solution code:
n = input()
A = list(map(int,input().split()))
ans = 0 
temp = 0
for i in range(n-1):
    if A[i]<temp:
        ans += temp-A[i]
    else:
        temp = max(temp,A[i])

print(ans)