#include <bits/stdc++.h>
using namespace std;
const double EPS = 0.0000000001;
const long long mod1 = 998244353;
const long long mod2 = 1000000007;
const long long mod3 = 1000000009;
const long long inf = 1000000000000000000;
void print(vector<long long> v) {
  for (auto it : v) cout << it << " ";
  cout << endl;
}
signed main() {
  long long n;
  cin >> n;
  long long mass[n];
  for (long long i = 0; i < n; i++) cin >> mass[i];
  long long c = 0;
  long long s = 0;
  for (long long i = 1; i < n; i++) {
    mass[i] += c;
    if (mass[i] < mass[i - 1]) {
      s += mass[i - 1] - mass[i];
      c += mass[i - 1] - mass[i];
      mass[i] = mass[i - 1];
    }
  }
  cout << s;
}
