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-----
5
2 1 5 4 3

-----Sample Output-----
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.
[{"code": "N = int(input())\nA = list(map(int,input().split()))\nT = 0\nfor i in range(len(A)-1):\n    if  (A[i] > A[i + 1]):\n        T += A[i] - A[i +1]\n        A[i+1] = A[i] \nprint(T)", "passed": true, "time": 0.15, "memory": 14148.0, "status": "done"}, {"code": "n=int(input())\na=list(map(int, input().split()))\nans=0\nfor i in range(1,n):\n\tif a[i] < a[i-1]:\n\t\tans += a[i-1] - a[i]\n\t\ta[i] = a[i-1]\nprint(ans)", "passed": true, "time": 0.19, "memory": 14380.0, "status": "done"}, {"code": "n = int(input())\na = input().split()\na = [ int(i) for i in a ]\nnum = 0\nmaxa = a[0]\nfor i in range(1, len(a) ):\n  if maxa < a[i]:\n    maxa = a[i]\n  elif maxa > a[i]:\n    num += maxa - a[i]\n  else:\n    pass\nprint(num)\n  \n    \n    \n\n", "passed": true, "time": 1.62, "memory": 14056.0, "status": "done"}, {"code": "a=int(input())\nlis=list(map(int,input().split()))\nans=0\nfor i in range(a-1):\n  if lis[i]>lis[i+1]:\n    j=lis[i]-lis[i+1]\n    ans+=j\n    lis[i+1]=lis[i]\nprint(ans)", "passed": true, "time": 0.14, "memory": 14080.0, "status": "done"}, {"code": "from typing import List\n\n\ndef answer(n: int, a: List[int]) -> int:\n    height_sum = 0\n    current_tallest = 0\n\n    for i in a:\n        if i < current_tallest:\n            height_sum += current_tallest - i\n        elif current_tallest < i:\n            current_tallest = i\n\n    return height_sum\n\n\ndef main():\n    n = int(input())\n    a = list(map(int, input().split()))\n    print((answer(n, a)))\n\n\ndef __starting_point():\n    main()\n\n__starting_point()", "passed": true, "time": 0.3, "memory": 15240.0, "status": "done"}, {"code": "n = int(input())\na = list(map(int, input().split()))\ntmp = a[0]\nans = 0\nfor i in range(1,n):\n    if tmp > a[i]:\n        ans += tmp - a[i]\n    else:\n        tmp = a[i]\nprint(ans)", "passed": true, "time": 0.14, "memory": 14052.0, "status": "done"}, {"code": "N = int(input())\nA = list(map(int, input().split()))\nA_sub = A[1:]\na_before = A[0]\ncount = 0\nfor a in A_sub:\n    if a < a_before:\n        count += a_before - a\n    else:\n        a_before = a\nprint(count)", "passed": true, "time": 1.62, "memory": 14132.0, "status": "done"}, {"code": "n = int(input())\na = list(map(int,input().split()))\ntall = 0\nans = 0\nfor an in a:\n  if an < tall:\n    ans += tall - an\n  elif an > tall:\n    tall = an\nprint(ans)", "passed": true, "time": 0.14, "memory": 14284.0, "status": "done"}, {"code": "s=int(input())\nx=list(map(int,input().split()))\nsum=0\nmaxi=x[0]\nfor i in x:\n  maxi=max(maxi,i)\n  sum+=(maxi-i)\nprint(sum)", "passed": true, "time": 0.18, "memory": 14120.0, "status": "done"}, {"code": "N = int(input())\nA = list(map(int, input().split()))\ncount = 0\n\nfor i in range(N-1):\n    if A[i] > A[i+1]:\n        count += A[i] - A[i+1]\n        A[i+1] = A[i]\n        \nprint(count)", "passed": true, "time": 0.2, "memory": 14292.0, "status": "done"}, {"code": "# -*- coding utf-8 -*-\n\nMOD = 10 ** 9 + 7\n\nN = int(input())\nAN = list(map(int, input().split()))\n\nans = 0\nm = AN[0]\nfor i in range(1, N):\n    if m > AN[i]:\n        ans += m - AN[i]\n    m = max(m, AN[i])\n\nprint(ans)\n", "passed": true, "time": 0.15, "memory": 14104.0, "status": "done"}, {"code": "N = int(input())\nA = list(map(int, input().split()))\n\nans = 0\nfor i in range(1, len(A)):\n    height = max(0, A[i-1] - A[i])\n    A[i] += height\n    ans += height\n\nprint(ans)\n", "passed": true, "time": 0.19, "memory": 14340.0, "status": "done"}, {"code": "N=int(input())\nA=list(map(int,input().split()))\nresult=0\nminimum=0\nfor a in A:\n  if a>minimum:\n    minimum=a\n  elif a<minimum:\n    result+=minimum-a\nprint(result)", "passed": true, "time": 0.21, "memory": 14244.0, "status": "done"}, {"code": "n = int(input())\na = list(map(int, input().split()))\nmax = a[0]\nans = 0\n\nfor i in range(1, n):\n    if max < a[i]:\n        max = a[i]\n\n    if a[i] < max:\n        ans += max - a[i]\n\nprint(ans)", "passed": true, "time": 0.2, "memory": 14236.0, "status": "done"}, {"code": "n = int(input())\na = [int(s) for s in input().split()]\nallsum = sum(a)\nfor i in range(1,n):\n    if a[i] < a[i-1]:\n        a[i] = a[i-1]\nprint(sum(a) - allsum)", "passed": true, "time": 0.14, "memory": 14176.0, "status": "done"}, {"code": "N = int(input())\nA = list(map(int,input().split()))\n\nmax_n = A[0]\nans = 0\nfor i in range(N):\n    tmp = max_n - A[i]\n    if tmp > 0:\n        ans += tmp\n    if max_n < A[i]:\n        max_n = A[i]\nprint(ans)\n    \n", "passed": true, "time": 0.14, "memory": 14168.0, "status": "done"}, {"code": "# -*- coding: utf-8 -*-\n\n\n# \u5165\u529b\u3092\u6574\u6570\u306b\u5909\u63db\u3057\u3066\u53d7\u3051\u53d6\u308b\ndef input_int():\n    return int(input())\n\n\n# \u30de\u30a4\u30ca\u30b91\u3057\u305f\u5024\u3092\u8fd4\u5374\ndef int1(x):\n    return int(x) - 1\n\n\n# \u534a\u89d2\u30b9\u30da\u30fc\u30b9\u533a\u5207\u308a\u5165\u529b\u3092Int\u306b\u5909\u63db\u3057\u3066Map\u3067\u53d7\u3051\u53d6\u308b\ndef input_to_int_map():\n    return list(map(int, input().split()))\n\n\n# \u534a\u89d2\u30b9\u30da\u30fc\u30b9\u533a\u5207\u308a\u5165\u529b\u3092Int\u306b\u5909\u63db\u3057\u3066\u53d7\u3051\u53d6\u308b\ndef input_to_int_tuple():\n    return tuple(map(int, input().split()))\n\n\n# \u534a\u89d2\u30b9\u30da\u30fc\u30b9\u533a\u5207\u308a\u5165\u529b\u3092Int\u306b\u5909\u63db\u3057\u3066\u30de\u30a4\u30ca\u30b91\u3057\u305f\u5024\u3092\u53d7\u3051\u53d6\u308b\ndef input_to_int_tuple_minus1():\n    return tuple(map(int1, input().split()))\n\n\ndef main():\n    N = input_int()\n    A = input_to_int_tuple()\n\n    result = 0\n    max_a = 0\n    for a in A:\n        if max_a > a:\n            result += max_a - a\n        max_a = max(a, max_a)\n\n    return result\n\n\ndef __starting_point():\n    print((main()))\n\n__starting_point()", "passed": true, "time": 0.15, "memory": 14284.0, "status": "done"}, {"code": "n = input()\nx = [int(i) for i in input().split()]\n\ntmp = x[0]\ntot = 0\n\n\nfor i in x:\n  if i <= tmp:\n    tot += tmp - i\n  else:\n    tmp = i\n\n\nprint(f\"{tot}\")", "passed": true, "time": 0.14, "memory": 14340.0, "status": "done"}, {"code": "def c176(n, alist):\n\n    ans = 0\n    tmp = alist[0]\n\n    for i in alist:\n        if tmp <= i:\n            tmp = i\n            continue\n        ans += tmp - i\n\n    return ans\n\ndef main():\n    n = int(input())\n    alist = list(map(int, input().split()))\n    print(c176(n, alist))\n\ndef __starting_point():\n    main()\n__starting_point()", "passed": true, "time": 0.14, "memory": 14224.0, "status": "done"}, {"code": "N = int(input())\na = list(map(int, input().split()))\n\nmax_height = -1\ns = 0\n\nfor i in a:\n    max_height = max(max_height, i)\n    d = max_height - i\n    s += d\n\nprint(s)", "passed": true, "time": 0.16, "memory": 14336.0, "status": "done"}, {"code": "N = int(input())\nA = list(map(int, input().split()))\nans = 0\nfor i in range(1, N):\n    if A[i - 1] > A[i]:\n        ans += A[i - 1] - A[i]\n        A[i] = A[i - 1]\nprint(ans)", "passed": true, "time": 0.15, "memory": 14248.0, "status": "done"}, {"code": "n = int(input())\nA = list(map(int,input().split()))\nans = 0\nprev_h = 0\nfor i,h in enumerate(A):\n    if i == 0:\n        prev_h = h\n    else:\n        if h < prev_h:\n            ans += prev_h - h\n        else:\n            prev_h = h\n\nprint (ans)\n", "passed": true, "time": 0.14, "memory": 14236.0, "status": "done"}, {"code": "n=int(input())\nA=list(map(int,input().split()))\nans=0\nfor i in range(n-1):\n  ans+=max(0,A[i]-A[i+1])\n  A[i+1]=max(A[i+1],A[i])\nprint(ans)", "passed": true, "time": 0.14, "memory": 14264.0, "status": "done"}, {"code": "n = int(input())\na = list(map(int,input().split()))\nm = 0\nans = 0\n\nfor i in range(n-1):\n    if a[i] > m:\n        m = a[i]\n    if a[i+1] < m:\n        ans += m-a[i+1]\n        a[i+1] = m\n\nprint(ans)\n", "passed": true, "time": 0.14, "memory": 14224.0, "status": "done"}, {"code": "n = int(input())\n\na = list(map(int, input().split()))\n\nh = 0\nans = 0\n\nfor i in range(n):\n    h = max(h, a[i])\n    ans += h - a[i]\n\n\nprint(ans)\n", "passed": true, "time": 0.14, "memory": 14128.0, "status": "done"}, {"code": "n=int(input())\nA=list(map(int,input().split()))\nb=sum(A)\nfor i in range(n-1):\n\tif A[i+1]<A[i]:\n\t\tA[i+1]=A[i]\nprint(sum(A)-b)", "passed": true, "time": 0.14, "memory": 14164.0, "status": "done"}, {"code": "N = int(input())\nA = list(map(int,input().split()))\nstep = 0\nsum = 0\nfor i in range(N-1):\n  step = A[i]-A[i+1]\n  if(step >= 1):\n    sum = sum + step\n    A[i+1] = A[i]\n  else:continue\nprint(sum)", "passed": true, "time": 0.14, "memory": 14196.0, "status": "done"}, {"code": "n = int(input())\nlst = list(map(int, input().split()))\ntall = lst[0]\nans = 0\nfor i in range(1, n):\n    if tall > lst[i]:\n        ans += tall - lst[i]\n    else:\n        tall = lst[i]\nprint(ans)\n", "passed": true, "time": 0.14, "memory": 14024.0, "status": "done"}, {"code": "n = int(input())\na = list(map(int, input().split()))\n\nans = 0\nh = 0\nfor x in a:\n\tans += max(0, h-x)\n\th = max(h, x)\n\nprint(ans)\n", "passed": true, "time": 0.14, "memory": 14284.0, "status": "done"}, {"code": "N = int(input())\nA = list(map(int, input().split()))\n\nans = 0\nmax_num = 0\n\nfor i in range(N):\n    max_num = max(max_num, A[i])\n    bi = max_num\n    ans += bi - A[i]\n\nprint(ans)\n", "passed": true, "time": 0.23, "memory": 14112.0, "status": "done"}, {"code": "# -*- coding: utf-8 -*-\nn=int(input())\nalist=list(map(int,input().split()))\nans=0\nb=0\nc=0\nk=0\nfor i in range(n):\n  b+=alist[i]\n  c+=alist[0+k]\n  if b<c:\n    ans+=c-b\n    b=c\n  elif b>c:\n    k=i\n    c=b\nprint(ans)", "passed": true, "time": 0.14, "memory": 14024.0, "status": "done"}, {"code": "n=int(input())\ns=0\nx=list(map(int,input().split()))\nz=x[0]\nfor i in range(len(x)):\n    if(z>x[i]):\n        s+=z-x[i]\n    else:\n        z=x[i]\nprint(s)", "passed": true, "time": 0.23, "memory": 14052.0, "status": "done"}, {"code": "def solve(n, a):\n    res, th = 0, 0\n    for x in a:\n        th = max(th, x)\n        res += th - x\n    return res\n\nn = int(input())\na = list(map(int, input().split()))\nprint(solve(n, a))", "passed": true, "time": 0.14, "memory": 14224.0, "status": "done"}, {"code": "import sys\n\n\ndef input():\n    return sys.stdin.readline().strip()\n\n\nsys.setrecursionlimit(20000000)\n\nMOD = 10 ** 9 + 7\nINF = float(\"inf\")\n\n\ndef main():\n    N = int(input())\n    A = list(map(int, input().split()))\n    answer = 0\n    last = A[0]\n    for i in range(1, N):\n        if A[i] >= last:\n            last = A[i]\n        else:\n            answer += last - A[i]\n    print(answer)\n\n\ndef __starting_point():\n    main()\n\n__starting_point()", "passed": true, "time": 0.14, "memory": 14348.0, "status": "done"}, {"code": "n=int(input())\na=list(map(int,input().split()))\ncnt=0\nfor i in range(n-1):\n    if a[i+1]<a[i]:\n        cnt += a[i]-a[i+1]\n        a[i+1] += a[i]-a[i+1]\nprint(cnt)", "passed": true, "time": 0.15, "memory": 14116.0, "status": "done"}, {"code": "\n# 1\u884c\u76ee\u306e\u5165\u529b\u306f\u4f7f\u7528\u3057\u306a\u3044\ninput()\n# 2\u884c\u76ee\u306e\u5165\u529b\u306f\u6574\u6570\u306e\u914d\u5217\u3068\u3057\u3066\u53d6\u5f97\nA = list(map(int, input().split()))\n\n# \u6700\u5927\u306eAi\u3092\u8a2d\u5b9a\nmax_A = 0\n# output\nsum_A = 0\n\nfor Ai in A:\n    max_A = max(Ai, max_A)\n    if max_A > Ai:\n        sum_A += max_A - Ai\n\nprint(sum_A)", "passed": true, "time": 0.23, "memory": 14148.0, "status": "done"}, {"code": "N = int(input())\nA = list(map(int, input().split()))\nmaxhigh = 0\nans = 0\nfor i in A:\n    if maxhigh> i:\n        ans += maxhigh - i\n    else:\n        maxhigh = i\nprint(ans)", "passed": true, "time": 0.14, "memory": 14324.0, "status": "done"}, {"code": "import sys\nfrom sys import stdin\ndef I():\n    return stdin.readline().rstrip()\ndef MI():\n    return map(int,stdin.readline().rstrip().split())\ndef LI():\n    return list(map(int,stdin.readline().rstrip().split()))\n#main part\nn=int(I())\na=LI()\nans=0\nfor i in range(n-1):\n    if a[i]>a[i+1]:\n        ans+=a[i]-a[i+1]\n        a[i+1]=a[i]\nprint(ans)", "passed": true, "time": 0.14, "memory": 14344.0, "status": "done"}, {"code": "n=int(input())\na=list(map(int,input().split()))\ns=0\nfor i in range(1,n):\n  if a[i-1] > a[i]:\n    s+=a[i-1]-a[i]\n    a[i] = a[i-1]\n    \nprint(s)\n", "passed": true, "time": 0.24, "memory": 14248.0, "status": "done"}, {"code": "n= int(input())\na = list(map(int,input().split()))\nli = [a[0]]\nfor i in range(n-1):\n    if li[i] < a[i+1]:\n        li.append(a[i+1])\n    else:\n        li.append(li[i])\n        \nans = 0\nfor j in range(n):\n    ans += li[j] - a[j]\n    \nprint(ans)", "passed": true, "time": 0.14, "memory": 14312.0, "status": "done"}, {"code": "N = int(input())\nAn = list(map(int, input().split()))\nm = 0\nans = 0\nfor a in An:\n  if a < m:\n    ans += m - a\n  else:\n    m = a\nprint(ans)", "passed": true, "time": 0.14, "memory": 14228.0, "status": "done"}, {"code": "n = int(input())\na = list(map(int, input().split()))\nasum = sum(a)\nfor i in range(1, n):\n    if a[i] < a[i - 1]:\n        a[i] = a[i - 1]\nprint((sum(a)-asum))\n", "passed": true, "time": 0.14, "memory": 14168.0, "status": "done"}, {"code": "def main():\n    N = int(input())\n    a = list(map(int, input().split()))\n    \n    DP = [0] * N\n    max_i = 0\n    \n    for i in range(1, N):\n        if a[max_i] > a[i]:\n            DP[i] += (a[max_i]-a[i])\n        elif a[max_i] < a[i]:\n            max_i = i\n            \n    \n    print(sum(DP))\n\ndef __starting_point():\n    main()\n__starting_point()", "passed": true, "time": 0.14, "memory": 14328.0, "status": "done"}, {"code": "N = int(input())\nnums = [int(i) for i in input().split()]\n\nbefore = 0\nans = 0\nfor i in range(N):\n    if i == 0:\n        before =nums[i]\n        continue\n    if before > nums[i]:\n        ans +=before - nums[i]\n    else:\n        before = nums[i]\n\nprint(ans)\n", "passed": true, "time": 0.23, "memory": 14328.0, "status": "done"}, {"code": "N = int(input())\nA = list(map(int, input().split()))\nh = 0\nfor i in range(N-1):\n    if A[i+1] >= A[i]:\n        continue\n    else:\n        tmp = A[i] - A[i+1]\n        A[i+1] += tmp\n        h += tmp\nprint(h)\n", "passed": true, "time": 0.14, "memory": 14296.0, "status": "done"}, {"code": "n = int(input())\na = list(map(int,input().split()))\nans = 0\nfor i in range(len(a) - 1):\n         if a[i+1] < a[i]:\n                  ans += a[i] - a[i+1]\n                  a[i+1] = a[i]\nprint(ans)\n\n", "passed": true, "time": 0.14, "memory": 14280.0, "status": "done"}, {"code": "n = int(input())\na = list(map(int, input().split()))\n\nheight = 0\nfor i in range(n - 1):\n  if a[i] > a[i + 1]:\n    height += a[i] - a[i + 1]\n    a[i + 1] = a[i]\nprint(height)", "passed": true, "time": 0.14, "memory": 14272.0, "status": "done"}, {"code": "n=int(input())\nx=list(map(int,input().split()))\nc=0\ntot=0\nfor i in range(n-1):\n    if x[i]>x[i+1]:\n        c=x[i]-x[i+1]\n        tot=tot+c\n        x[i+1]=x[i+1]+c\nprint(tot)\n", "passed": true, "time": 0.14, "memory": 14100.0, "status": "done"}, {"code": "N = int(input())\nA = list(map(int,input().split()))\ncount = 0\nmh = A[0]\nfor i in range(1,len(A)):\n    if mh > A[i]:\n        count += mh-A[i]\n    else:\n        mh = A[i]\nprint(count)", "passed": true, "time": 0.14, "memory": 14056.0, "status": "done"}, {"code": "n = int(input())\na = list(map(int,input().split()))\nans = 0\nif a == sorted(a):\n    print(ans)\n    return\nb = a.index(max(a))\nfor i in range(0,b):\n    if a[i] > a[i+1]:\n        ans = ans + a[i]-a[i+1]\n        a[i+1] = a[i]\nans = ans + max(a)*len(a[b+1:])-sum(a[b+1:])\nprint(ans)", "passed": true, "time": 0.14, "memory": 14248.0, "status": "done"}, {"code": "N=int(input())\nA=list(map(int,input().split()))\na=0\ns=[]\nfor i in A:\n  if i<a:\n    s.append(a-i)\n    \n  else:\n    a=i\nprint((sum(s)))\n", "passed": true, "time": 0.23, "memory": 14232.0, "status": "done"}, {"code": "import sys\n\nN = int(input())\nA = list(map(int, input().split()))\n\nm = A[0]\ncount = 0\nfor a in A:\n    if m > a:\n        count += m-a\n    else:\n        m = a\nprint(count)\n", "passed": true, "time": 0.14, "memory": 14060.0, "status": "done"}, {"code": "n = int(input())\nA = list(map(int, input().split()))\n\nans = 0\n\nfor i in range(n-1):\n  if A[i+1] >= A[i]:\n    pass\n  elif A[i+1] < A[i]:\n    ans += A[i] - A[i+1]\n    A[i+1] += A[i] - A[i+1]\n\nprint(ans)", "passed": true, "time": 0.19, "memory": 14240.0, "status": "done"}, {"code": "n = int(input())\naaa = list(map(int, input().split()))\nans = 0\nfor i in range(1, n):\n    diff = aaa[i] - aaa[i - 1]\n    if diff < 0:\n        aaa[i] -= diff\n        ans -= diff\nprint(ans)", "passed": true, "time": 0.14, "memory": 14164.0, "status": "done"}, {"code": "N=int(input())\nA=list(map(int,input().split()))\ncount=0\nfor i in range(1,N):\n  if A[i-1]>A[i]:\n    count+=A[i-1]-A[i]\n    A[i]=A[i-1]\nprint(count)", "passed": true, "time": 0.43, "memory": 14384.0, "status": "done"}, {"code": "N = int(input())\nA_list = list(map(int, input().split()))\n\nA_max = A_list[0]\nans = 0\n\nfor i in(A_list):\n  if i > A_max:\n    A_max = i\n  elif i < A_max:\n    ans += A_max - i\n\nprint(ans)", "passed": true, "time": 0.43, "memory": 14068.0, "status": "done"}, {"code": "#abc176c\nn=int(input())\na=list(map(int,input().split()))\nres=0\nfor i in range(1,n):\n d=max(a[i-1]-a[i],0)\n res+=d\n a[i]+=d\nprint(res)\n", "passed": true, "time": 0.31, "memory": 14128.0, "status": "done"}, {"code": "N = int(input())\nA = list(map(int, input().split()))\nans = 0\nfor i in range(N-1):\n    if A[i+1]<A[i]:\n        ans += A[i]-A[i+1]\n        A[i+1] += A[i]-A[i+1]\n\nprint(ans)\n", "passed": true, "time": 0.43, "memory": 14292.0, "status": "done"}, {"code": "n = int(input())\na = list(map(int, input().split(\" \")))\na_max = 0\nstep = 0\n\nfor i in range(n):\n    if a_max < a[i]:\n        a_max = a[i]\n        a_num = i\n    else:\n        step += a_max - a[i]\n\nprint(step)", "passed": true, "time": 0.14, "memory": 14080.0, "status": "done"}, {"code": "n = int(input())\nl = list(map(int,input().split()))\nal = []\n \nfor i in range(0,n-1):\n  if(l[i]<=l[i+1]):\n    al.append(0)\n  else:\n    al.append(l[i]-l[i+1])\n    l[i+1] = l[i]\n\nprint(sum(al))", "passed": true, "time": 0.16, "memory": 14308.0, "status": "done"}, {"code": "N = int(input())\nh = list(map(int, input().split()))\nstep=0\nsteps=0\nfor i in range(N-1):\n    if h[i+1] < h[i]+step:\n        step=h[i]-h[i+1] +step\n        steps+=step\n    else:\n        step=0\nprint(steps)", "passed": true, "time": 0.15, "memory": 14228.0, "status": "done"}, {"code": "N = int(input())\nA = list(map(int, input().split()))\ntot = 0\nfor i, a in enumerate(A):\n    if i == 0:\n        forward = a\n        continue\n    if forward >= a:\n        tot += forward - a\n    else:\n        forward = a\nprint(tot)", "passed": true, "time": 0.15, "memory": 14172.0, "status": "done"}, {"code": "N = int(input())\nnum_list = list(map(int, input().split()))\n\nANS = 0\ntall = 0\npre = 0\n\nfor i in range(N):\n  if tall > num_list[i]:\n    pre = (tall - num_list[i])\n    ANS += pre\n    tall = num_list[i] + pre\n  else:\n    tall = num_list[i]\n\nprint(ANS)\n", "passed": true, "time": 0.22, "memory": 14088.0, "status": "done"}, {"code": "n = int(input())\na = list(map(int, input().split()))\nans = 0\nh = a[0]\nfor i in range(n):\n    ans += max(a[i],h) - a[i]\n    h = max(a[i],h)\nprint(ans)", "passed": true, "time": 0.19, "memory": 14212.0, "status": "done"}, {"code": "N = int(input())\nA = list(map(int, input().split()))\n\ncnt = 0\n\nfor i in range(N-1):\n    if A[i+1] >= A[i]:\n        pass\n    else:\n        cnt += A[i] - A[i+1]\n        A[i+1] = A[i]\n\nprint(cnt)", "passed": true, "time": 0.14, "memory": 13996.0, "status": "done"}, {"code": "n = int(input())\na = [int(i) for i in input().split()]\nmx = a[0]\nans = 0\nfor i in a:\n    if mx > i:\n        ans += mx - i\n    elif mx < i:\n        mx = i\nprint(ans)\n", "passed": true, "time": 0.2, "memory": 14144.0, "status": "done"}, {"code": "def main():\n    N = int(input())\n    A = list(map(int, input().split()))\n\n    dp = [0] * N\n    for i in range(1, N):\n        h = A[i - 1] - A[i]\n        if h > 0:\n            A[i] += h\n            dp[i] = dp[i-1] + h\n        else:\n            dp[i] = dp[i-1]\n\n    print((dp[-1]))\n    return\n\n\ndef __starting_point():\n    main()\n\n__starting_point()", "passed": true, "time": 0.14, "memory": 14216.0, "status": "done"}, {"code": "n = int(input())\na = list(map(int,input().split()))\nans = 0\nif a == sorted(a):\n    print(ans)\n    return\nfor i in range(n-1):\n    if a[i] > a[i+1]:\n        ans = ans + a[i]-a[i+1]\n        a[i+1] = a[i]        \nprint(ans)", "passed": true, "time": 0.14, "memory": 14268.0, "status": "done"}, {"code": "from sys import stdin\nN = int(stdin.readline().rstrip())\nA = [int(_) for _ in stdin.readline().rstrip().split()]\nans, tmp_max = 0, 0\nfor a in A:\n    if a > tmp_max:\n        tmp_max = a\n    else:\n        ans += (tmp_max - a)\nprint(ans)", "passed": true, "time": 0.14, "memory": 14080.0, "status": "done"}, {"code": "N = int(input())\nA = list(map(int, input().split()))\n\ncount = 0\nmax_value = A[0]\nfor i in range(N-1):\n    #pdb.set_trace()\n    if(max_value > A[i+1]):\n        count += max_value-A[i+1]\n    if (max_value < A[i+1]):\n        max_value = A[i+1]\nprint(count)", "passed": true, "time": 0.14, "memory": 14096.0, "status": "done"}, {"code": "n = int(input())\na = list(map(int,input().split()))\n\nsum = 0\nmax = a[0]\n\nfor i in range(n):\n\tif(max > a[i]):\n\t\tsum += (max - a[i])\n\telse:\n\t\tmax = a[i]\nprint(sum)", "passed": true, "time": 0.14, "memory": 14292.0, "status": "done"}, {"code": "N = int(input())\nA = list(map(int, input().split()))\n\nans = 0\n\nfor i in range(N-1):\n    if A[i+1] <= A[i]:\n        ans += A[i] - A[i+1]\n        A[i+1] = A[i]\n    else:\n        pass\n\nprint(ans)\n", "passed": true, "time": 0.14, "memory": 14308.0, "status": "done"}, {"code": "n = int(input())\na = list(map(int,input().split()))\nans = 0\nfor i in range(1,n):\n  d = max(a[i-1]-a[i],0)\n  ans += d\n  a[i] += d\nprint(ans)\n", "passed": true, "time": 0.14, "memory": 14336.0, "status": "done"}, {"code": "import sys\ndef rs(): return sys.stdin.readline().rstrip()\ndef ri(): return int(rs())\ndef rs_(): return [_ for _ in rs().split()]\ndef ri_(): return [int(_) for _ in rs().split()]\n\nN = ri()\nA = ri_()\nans = 0\ntmp = A[0]\nfor i in range(1, N):\n    if tmp > A[i]:\n        ans += tmp - A[i]\n    else:\n        tmp = A[i]\nprint(ans)", "passed": true, "time": 0.14, "memory": 14224.0, "status": "done"}, {"code": "\"\"\"\n1 2 3 4 5 6 ..... N(\u4eba)\n        i = \u8eab\u9577:Ai(cm)\n\nif A[i] > A[i+1]:\n    A[i] == A[i+1] + \u8e0f\u307f\u53f0\n\"\"\"\ndef step():\n    # \u5165\u529b\n    N = int(input())\n    A = list(map(int, input().split()))\n    # \u51e6\u7406\n    step_height_sum = 0\n    for i in range(len(A)-1):\n        if A[i] > A[i+1]:\n            step_height_sum += A[i]-A[i+1]\n            A[i+1] = A[i]\n    return step_height_sum\n    \nresult = step()\nprint(result)", "passed": true, "time": 0.14, "memory": 14300.0, "status": "done"}, {"code": "n = int(input())\na = list(map(int,input().split()))\nans = 0\nx = a[0]\nfor i in range(n):\n    x = max(x,a[i])\n    if a[i]<x:\n        ans+=x-a[i]\nprint(ans)", "passed": true, "time": 0.14, "memory": 14168.0, "status": "done"}, {"code": "n=int(input())\nlst=list(map(int,input().split()))\n\nnow=lst[0]\nsm=0\n\nfor i in range(1,n):\n    if lst[i]>=now :\n        now=lst[i]\n        continue\n    sm+=now-lst[i]\n\nprint(sm)", "passed": true, "time": 0.19, "memory": 14272.0, "status": "done"}, {"code": "n = int(input())\n\npeople = list(map(int, input().split()))\n\ncount = 0\n\nfor i in range(n):\n    if i == 0 or people[i-1] <= people[i]:\n        continue\n    else:\n        count += people[i-1] - people[i]\n        people[i] += people[i-1] - people[i]\nprint(count)", "passed": true, "time": 0.14, "memory": 14044.0, "status": "done"}, {"code": "n=int(input())\na=list(map(int, input().split()))\nans=0\nfor i in range(n-1):\n  if a[i]>a[i+1]:\n    ans+=a[i]-a[i+1]\n    a[i+1]=a[i]\nprint(ans)", "passed": true, "time": 0.43, "memory": 14244.0, "status": "done"}, {"code": "\nN = int(input())\nA = input()\n\nAa = A.split()\n\ni = 0\n\ntotal = 0\n\nfor i in range(N-1):\n    left = int(Aa[i])\n    right = int(Aa[i+1])\n\n    diff = 0\n\n    if left>right:\n        diff = abs(right-left)\n        total = total + diff\n        Aa[i+1] = right + diff\n    else:\n        total = total + 0\n    \n\nprint(total)\n", "passed": true, "time": 0.45, "memory": 14088.0, "status": "done"}, {"code": "n = int(input())\na = list(map(int,input().split()))\nans = 0\nfor i in range(1,n):\n  if a[i] < a[i-1]:\n    ans += a[i-1]-a[i]\n    a[i] += a[i-1]-a[i]\nprint(ans)", "passed": true, "time": 0.31, "memory": 14348.0, "status": "done"}, {"code": "\ndef main(A):\n    ans = 0\n    max_num = A[0]\n    for a in A:\n        max_num = max(max_num, a)\n        ans += max_num - a\n\n    return ans\n\ndef __starting_point():\n    N = int(input())\n    A = list(map(int, input().split()))\n    ans = main(A)\n    print(ans)\n\n\n__starting_point()", "passed": true, "time": 0.31, "memory": 14220.0, "status": "done"}, {"code": "N = int(input())\nAs = list(map(int, input().split()))\n\nans_step_sum = 0\ncurrent_max_height = As[0]\n\nfor Ai in As[1:]:\n  diff = current_max_height - Ai\n  if diff > 0: ans_step_sum += diff\n  else: current_max_height = Ai\nprint(ans_step_sum)", "passed": true, "time": 0.29, "memory": 14320.0, "status": "done"}, {"code": "n=int(input())\nx=list(map(int,input().split()))\n\na=x[0]\nc=[]\n\nfor i in range(1,n):\n    if x[i]<a:\n        c.append(a-x[i])\n    if x[i]>a:\n        a=x[i]\nprint(sum(c))", "passed": true, "time": 0.14, "memory": 14108.0, "status": "done"}, {"code": "n =int(input())\nl = list(map(int, input().split()))\nans=0\nfor i in range(1,n):\n    if l[i-1] > l[i]:\n        ans +=l[i-1] - l[i]\n        l[i] =l[i-1]\nprint(ans)", "passed": true, "time": 0.17, "memory": 14352.0, "status": "done"}, {"code": "n = int(input())\na = [int(i) for i in input().split()]\n\nans = 0\nmax_a = a[0]\n\nfor i in range(1, n):\n    if max_a > a[i]:\n        ans += max_a - a[i]\n    elif max_a == a[i]:\n        pass\n    else:\n        max_a = a[i]\nprint(ans)", "passed": true, "time": 0.14, "memory": 14196.0, "status": "done"}, {"code": "# coding: utf-8\n# Your code here!\n\nclass Person:\n    def __init__(self, num, height):\n        self.num = num\n        self.height = height\n    \n    @staticmethod\n    def static():\n        print(\"static\")\n\n\ndef main():\n    number = int(input())\n    person_list = list(map(int, input().split()))\n    \n    ans = 0\n    for num, person in enumerate(person_list):\n        if num > 0:\n            if person < person_list[num-1]:\n                person_list[num] = person_list[num-1]\n                ans += person_list[num-1] - person\n        \n    \n    print(ans)\n\ndef __starting_point():\n    main()\n    \n\n__starting_point()", "passed": true, "time": 0.21, "memory": 14284.0, "status": "done"}, {"code": "N = int(input())\nA_l = map(int, input().split())\nM = 0\nB = 0\nfor i in A_l:\n    if i >= M:\n        B += 0\n        M = i\n    else:\n        B += M-i\nprint(B)", "passed": true, "time": 0.15, "memory": 14076.0, "status": "done"}, {"code": "N = int(input())\nAs = list(map(int, input().split()))\ntlest, total = As[0], 0\nfor i in range(1, N):\n\ttlest = max(tlest, As[i])\n\tif tlest - As[i]:\n\t\ttotal += tlest - As[i]\nprint(total)\n", "passed": true, "time": 0.15, "memory": 14016.0, "status": "done"}, {"code": "n=int(input())\na=list(map(int,input().split()))\nres=0\nans=-1\nfor i in range(n):\n    if(a[i]>=ans):\n        ans=a[i]\n    elif(a[i]<ans):\n        res+=ans-a[i]\nprint(res)\n", "passed": true, "time": 0.21, "memory": 14108.0, "status": "done"}, {"code": "n=int(input())\na=list(map(int, input().split()))\nmax=a[0]\ndai=0\nfor i in range(n):\n    #print(f\"{i}\u56de\u76ee\")\n    if max>a[i]:\n        dai+=max-a[i]\n       \n    else:\n        max=a[i]\n    #print(max,dai)\nprint(dai)", "passed": true, "time": 0.14, "memory": 14308.0, "status": "done"}, {"code": "def steps():\n    biggerone=0\n    ans=0\n    list_a=[]\n    cnt=int(input())\n    a=input()\n    list_a=[int(x) for x in a.split()]\n    if len(list_a)!=cnt:\n        print('Wrong input')\n\n        if ans==0:\n            ans=None\n\n        steps()\n\n    else:\n\n        for i in range(0,len(list_a)-1):\n            if i ==0:\n                biggerone=list_a[i]\n\n            if biggerone>list_a[i+1]:\n                ans+=biggerone-list_a[i+1]\n            else:\n                biggerone=list_a[i+1]\n\n    print(ans)\n\nsteps()\n", "passed": true, "time": 0.14, "memory": 14196.0, "status": "done"}, {"code": "n = int(input())\n\nheights=list(map(int, input().split()))\nhumidais=[]\n\nfor i in range(n):\n  if i >= 1:\n    if heights[i-1] > heights[i]:\n      humidai = heights[i-1] - heights[i]\n      humidais.append(humidai)\n      heights[i] = heights[i] + humidai\n\nans = 0\nfor i in range(len(humidais)):\n  ans += humidais[i]\n  \nprint(ans)", "passed": true, "time": 0.14, "memory": 14064.0, "status": "done"}, {"code": "N = int(input())\nA = list(map(int,input().split()))\nmaximum = A[0]\nans = 0\n\nfor i in range(N):\n    if maximum >= A[i]:\n        ans += (maximum - A[i])\n    else:\n        maximum = max(maximum, A[i])\n\nprint(ans)", "passed": true, "time": 0.15, "memory": 14200.0, "status": "done"}, {"code": "import math\n\n\ndef cin():\n    return list(map(int, input().split()))\n\n\nN = int(input())\ndata = cin()\ntotal = 0\n\n\nfor i in range(N - 1):\n    if data[i + 1] < data[i]:\n        total += data[i] - data[i + 1]\n        data[i + 1] = data[i]\n\nprint(total)\n", "passed": true, "time": 0.14, "memory": 14296.0, "status": "done"}, {"code": "N = int(input())\nA = [int(d) for d in input().split()]\n\nS = []\nX = 0\nfor i in range(N):\n    if X < A[i]:\n        X = A[i]\n    else:\n        pass\n    S.append( X - A[i] )\n\nprint(sum(S))", "passed": true, "time": 0.14, "memory": 14240.0, "status": "done"}, {"code": "N = int(input())\nA = list(map(int, input().split()))\n\nresult = 0\nfor i in range(1, N):\n    if A[i] < A[i - 1]:\n        result += A[i - 1] - A[i]\n        A[i] = A[i - 1]\nprint(result)\n", "passed": true, "time": 0.14, "memory": 14132.0, "status": "done"}, {"code": "n = int(input())\narr = [int(x) for x in input().split()]\n\nheights = 0\nfor i in range(1,len(arr)):\n    if arr[i-1] > arr[i]:\n        heights += arr[i-1] - arr[i]\n        arr[i] = arr[i-1]\n\nprint(heights)\n", "passed": true, "time": 0.14, "memory": 14056.0, "status": "done"}]