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.

Solution in C# (.NET Core 3.1.201) (judge verdict: Accepted)

using System;
using System.Numerics;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace debug
{
    class main
    {
        static void Main(string[] args)
        {
            //問題クラスを展開
            ProgramC a = new ProgramC();
            a.main();//実行する

        }

    }
    //ABC175
    class ProgramA
    {
        public void main()
        {
            //入力
            string s = Console.ReadLine();

            //30以上ならYes、それ以外はNoを出力
            if (s == "RRR")
                Console.WriteLine(3);
            else if(s == "RRS" || s == "SRR")
                Console.WriteLine(2);
            else if (s == "SSS")
                Console.WriteLine(0);
            else
                Console.WriteLine(1);

        }
    }

    class ProgramB
    {
        public void main()
        {
            //入力

            int n = int.Parse(Console.ReadLine());
            string[] s = Console.ReadLine().Split(' ');

            long count = 0;
            //それぞれの距離を求めて、距離がD以下なら1足す
            for (int i = 0; i < n; i++)
                for (int j = i + 1; j < n; j++)
                    for (int k = j + 1; k < n; k++)
                    {
                        long a = long.Parse(s[i]);
                        long b = long.Parse(s[j]);
                        long c = long.Parse(s[k]);
                        if (a == b || b == c || c == a)
                            continue;

                        if (a + b> c  && b + c > a && c +a > b)
                            count++;
                    }

            //答え出力
            Console.WriteLine(count);

        }
    }

    class ProgramC
    {
        public void main()
        {

            //入力
            long n = long.Parse(Console.ReadLine());
            string[] s = Console.ReadLine().Split(' ');

            long count = 0;
            long min = long.Parse(s[0]);
            for(int i = 1;i < n;i++)
            {
                long temp = long.Parse(s[i]);
                if(temp < min)
                {
                    count += min - temp;
                }
                else
                {
                    min = temp;
                }

            }

            Console.WriteLine(count);


        }
    }

    class ProgramD
    {
        public void main()
        {

            //入力
            string[] s = Console.ReadLine().Split(' ');
            long n = long.Parse(s[0]);
            long k = long.Parse(s[1]);
            string[] p = Console.ReadLine().Split(' ');
            string[] c = Console.ReadLine().Split(' ');

            long[] flag = new long[n];
            long ans = 0;

            for (int i = 0; i < n; i++)
            {
                if (flag[i] != 0)
                    continue;

                List<long> list = new List<long>();
                int start = int.Parse(p[i]) - 1;
                while (true)
                {
                    flag[start]++;
                    list.Add(long.Parse(c[start]));
                    start = int.Parse(p[start])- 1;
                    if (start == i)
                    {
                        list.Add(long.Parse(c[start]));
                        break;
                    }
                
                }

                if(k > list.Count())
                {
                    long temp = 0;
                    for (int x = 0; x < list.Count(); x++)
                    {
                        temp += list[x];
                    }
                }

                for(int j = 0; j < list.Count();j++)
                {
                    if (k < j + 1)
                        continue;


                    long temp = 0;
                    for (int x = 0;x < j + 1; x++)
                    {
                        temp += list[x];
                    }

                    ans = Math.Max(ans, temp);

                    for(int y = j + 1; y < list.Count();y++)
                    {
                        temp -= list[y - j - 1];
                        temp += list[y];
                        ans = Math.Max(ans, temp);
                    }
                }


            }

            
            Console.WriteLine(ans);
            

        }

    }

    class ProgramE
    {

        public void main()
        {
            //入力
            string[] s = Console.ReadLine().Split(' ');
            long n = long.Parse(s[0]);
            long k = long.Parse(s[1]);

            long[] a = new long[n];

            string[] t = Console.ReadLine().Split(' ');

            for(int i = 0;i < n;i++)
                a[i] = long.Parse(t[i]);

            long min = 0;//最初を0にすると1に答えができる
            long max = 1000000000;//答候補の最大

            //二分探索（答えを決め打ちして、切る回数を数える。k回以下ならmax,そうでないならmin)
            //小さくした方が回数がかかるので、maxが満たす最小の丸太の長さ
            while(min + 1 != max)
            {
                long temp = (min + max) / 2;
                long cut = 0;
                //切る回数を数える
                for (int i = 0; i < n; i++)
                {
                    //tempが大きいなら切らない
                    if (a[i] < temp)
                        continue;
                    //割り切れるかどうかで回数は変わる
                    if(a[i] % temp == 0)
                        cut += a[i] / temp - 1;
                    else
                        cut += a[i] / temp;
                }

                //k回以下ならmax更新、そうでないならminを更新
                if (cut <= k)
                    max = temp;
                else
                    min = temp;

            }

            //答え出力
            Console.WriteLine(max);

        }


    }

}