#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1e9 + 7;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t = 1;
  while (t--) {
    int n;
    cin >> n;
    vector<int> a(n);
    for (int &x : a) cin >> x;
    int st = 0;
    int maxm = a[0];
    for (int i = 1; i < n; ++i) {
      if (a[i] < maxm) {
        st += (maxm - a[i]);
      }
      maxm = max(maxm, a[i]);
    }
    cout << st;
  }
  return 0;
}
