#include<iostream>
#include<string>
#include<cmath>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cctype>
 
using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<char> vc;

const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
const int MAXN = 1e6 + 3;

#define sortA(v) sort(v.begin(), v.end())
#define fill(a) memset(a, 0, sizeof(a))

#define FOR(i, a, b) for(int i = a; i < b; i++)
#define FOR_REV(i, a ,b) for(int i = a; i >= b; i--)
#define REP(i, n) FOR(i, 0, n)


void test() {
    int n;
    cin >> n;
    vll a(n);
    REP(i, n) {
        cin >> a[i];
    }
    ll sum = 0;
    ll high = a[0];
    FOR(i, 1, n) {
        if(a[i] < high) {
            sum += high - a[i];
        }
        else {
            high = a[i];
        }
    }
    cout << sum << endl;
}   
 
int main ()     
{
    // int t;
    // cin >> t;
    // while(t--) 
        test();
}
