https://atcoder.jp/contests/abc176/tasks/abc176_c
#include <iostream>
#include <algorithm>
#include <set>
#include <stack>
#include <string>
using namespace std;
typedef long long ll;
const int MAXN=2e5;

ll a[MAXN];

void solve() {
    int N; ll total = 0;
    cin >> N;
    for (int x = 0; x < N; x++) 
        cin >> a[x];
    for (int x = 0; x < N-1; x++) 
        if (a[x] > a[x+1]) {
            total += a[x] - a[x+1];
            a[x+1] = a[x];
        }
    cout << total;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    solve();
    return 0;	
}