#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
    ll n, k;
    cin >> n;
    ll arr[n] = {0};
    for (int x = 0; x < n; x++)
    {
        cin >> arr[x];
    }
    ll sum = 0;
    ll mx = 0;
    for (int x = 0; x < n; x++)
    {
        if (arr[x] < mx)
        {
            sum = sum + (mx - arr[x]);
        }
        else
        {
            mx = arr[x];
        }
    }
    cout << sum << "\n";
}