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++ (GCC 9.2.1) (judge verdict: Wrong Answer)

#include<bits/stdc++.h>
using namespace std;
#define _ios ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define    common(a,b)      SORT(a), SORT(b), a.erase(set_intersection(all(a),all(b),a.begin()),a.end())
#define    uncommon(a,b)    SORT(a), SORT(b), a.erase(set_symmetric_difference(all(a),all(b),a.begin()),a.end())
#define    minv(v)         *min_element(all(v))
#define    maxv(v)         *max_element(all(v))
#define    SORT(v)          sort(all(v))
#define    REV(x)           reverse(x.begin(),x.end())
#define    all(x)           x.begin(), x.end()
#define    rev(v)           reverse(all(v))
#define    unq(v)           sort(all(v)),v.erase(unique(all(v)),v.end())
#define    fr(i,x,n)        for(int i=x;i<n;i++)
#define    trav(a,x)        for (auto &a: x)
#define    sz(x)            (ll)(x).size()
#define    ll               long long int
#define    pb               push_back
#define    in               insert
#define    gcd(a, b)        __gcd(a, b)
#define    lcm(a, b)        ((a)*((b)/gcd(a,b)))
#define    w(t)              cin>>t;while(t--)
const ll mod = 1000000007;
const double pi = acos(-1.0);
/*bool isprime(int x)
{
    if(x==0 or x==1)return false;
    for(int i=2; i*i<=x; i++)
    {
        if(x%i==0)
            return false;
    }
    return true;
}*/
/*ll power(ll n,ll b,ll mod)
{
    ll res=1;
    while(b)
    {
        if(b%2)res=((n%mod)*(res%mod))%mod,b--;
        else n=((n%mod)*(n%mod))%mod,b/=2;
    }
    return res;
}*/
string str[]= {"YES","NO"};
int main()
{
    _ios
    //ll z;
    //w(z)
    //{
        ll n,a,b,c,d,e,x,y,mul=1,cn=0,cn1=0,cn2=0,sm=0,sm1=0,sm2=0,mx=INT_MIN,mx1=INT_MIN,mn=INT_MAX,mn1=INT_MAX,i,j,k=0,temp;
        string s,ss,sss,str="";
        set<ll>st;
        map<ll,ll>mp;
        bool f=true,ff=true;
         cin>>a;
         ll ar[a];
         fr(i,0,a){cin>>ar[i];if(ar[i]>mx)mx=ar[i],cn=i;}
         fr(i,1,cn)
         {
             cn1+=abs(ar[i]-ar[0]);
         }
         fr(i,cn,a)
         {
             cn1+=abs(ar[i]-mx);
         }
         //fr(i,0,a){sm+=abs(ar[i]-ar[0]);sm1+=abs(ar[i]-mx);}
         fr(i,1,a)
         {
             if(ar[i]>=ar[i-1])continue;
             else f=false;
         }
         if(!f)cout<<cn1<<endl;
         else cout<<0<<endl;
            /*if(a%2==0 and sqrt(a)*sqrt(a)!=
                 a)
               cout<<min(a%2,a/2)<<endl;
              else
                  cout<<max(a%2,a/2+1)<<endl;*/
       // }
    return 0;
}