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