#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  long long mx = 0, x = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    long long y;
    cin >> y;
    y += mx;
    mx += max(0LL, x - y), x = max(y, x);
  }
  cout << mx << endl;
  return 0;
}
