#include<bits/stdc++.h>
#include<vector>
#include<string>
#include<math.h>
#include<set>
#include<sstream>

using namespace std;

//-----------------------------------------------------------------------------------------------------------//

#define ll         long long int
#define ull        unsigned long long int
#define endl       '\n'
#define pb         push_back
#define mod        1000000007
#define vll        vector<ll>
#define vchar      vector<char>
#define vstr       vector<string>
#define all(v)     v.begin(),v.end()
#define f(i,x,n)   for(int i=x;i<n;i++)
#define fo(i,x,n)  for(int i=x;i>=n;i--)
#define take(a)    for(auto &i:a)cin>>i;
#define print(a)   for(auto i:a)cout<<i<<" ";
#define prec(n,x)  cout<<fixed<<setprecision(x)<<n<<endl
#define srt(x)     sort(x.begin(),x.end())
#define rev_srt(x) sort(x.begin(),x.end(),greater<ll>())
#define fast       ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

//-----------------------------------------------------------------------------------------------------------//

ll gcd(ll a, ll b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}

ll lcm(ll a, ll b)
{
    ll x = gcd(a, b);
    return (a * b / x);
}

ll to_int(string s)
{
    stringstream conv(s);
    ll x;
    conv >> x;
    return x;
}

string leftrotate(string s, ll d)
{
    reverse(s.begin(), s.begin() + d);
    reverse(s.begin() + d, s.end());
    reverse(s.begin(), s.end());
    return s;
}

string rightrotate(string s, ll d)
{
    d = s.length() - d;
    reverse(s.begin(), s.begin() + d);
    reverse(s.begin() + d, s.end());
    reverse(s.begin(), s.end());
    return s;
}

ll factorial(ll n)
{
    ll ans = 1;
    f(i, 1, n + 1)
    {
        ans = (ans * i) % mod;
    }
    return ans;
}

ll isprime(ll n)
{
    if (n == 1 || n == 3)
        return 0;
    if (n == 2)
        return 1;
    f(i, 2, sqrt(n) + 1)
    {
        if (n % i == 0)
            return 0;
    }
    return 1;
}

ll ispowerof2 (ll num )
{
    if ( ( num & ( num - 1 ) ) == 0 )
        return 1;
    return 0;
}

ll power(ll x, ll y)
{
    ll ans = 1;
    x = x % mod;
    while (y > 0)
    {
        if (y & 1)
        {
            ans = (ans * x) % mod;
        }
        x = (x * x) % mod;
        y = y >> 1;
    }
    return ans;
}

ll decimalToBinary(ll n)
{
    ll num = 0, cnt = 0;
    while (n != 0)
    {
        ll rem = n % 2;
        ll c = power(10, cnt);
        num += rem * c;
        n /= 2;
        cnt++;
    }
    return num;
}

vector<ll> subsetSums(vector<ll> arr, ll n)
{
    vector<ll> ans;
    long long total = 1 << n;

    for (long long i = 0; i < total; i++)
    {
        long long sum = 0, cnt = 0;

        for (int j = 0; j < n; j++)
            if (i & (1 << j))
            {
                sum += arr[j];
            }

        ans.pb(sum);
    }
    return ans;
}

string to_string(ll n)
{
    ostringstream str1;
    str1 << n;
    string s = str1.str();
    return s;
}

ll binarySearch(vll a, ll key)
{
    ll low = 0, high = a.size() - 1;
    while (low <= high)
    {
        ll mid = (low) + (high - low) / 2;
        if (a[mid] < key)
        {
            low = mid + 1;
        }
        else if (a[mid] > key)
        {
            high = mid - 1;
        }
        else
        {
            return mid;
        }
    }
    return -1;                //key not found
}

double to_double(string s)
{
    stringstream geek(s);
    double x = 0;
    geek >> x;
    return x;
}

int lcs(string x, string y, int i, int j)
{
    if (i == 0 || j == 0)
        return 0;
    if (x[i] == y[j])
        return 1 + lcs(x, y, i - 1, j - 1);
    else
    {
        return max(lcs(x, y, i - 1, j), lcs(x, y, i, j - 1));
    }
}


//************************************************************************************************************//

int main()
{

#ifndef ONLINE_JUDGE
    freopen("F:/Programming/Codes/input.txt", "r", stdin);
    freopen("F:/Programming/Codes/output.txt", "w", stdout);
#endif

    fast;

    ll t = 1;
    //cin >> t;

    while (t--)
    {
        ll n;
        cin >> n;

        vll v(n);
        take(v);

        ll ans = 0;
        {
            ll mx = v[0];
            f(i, 1, n)
            {
                if (v[i] <= mx)
                    ans += (mx - v[i]);
                else
                {
                    mx = v[i];
                }
            }
        }
        cout << ans << endl;
    }

    return 0;
}


Measured memory_bytes: 4768