#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n;
  cin >> n;
  vector<long long> a(n);
  int cnt = 0;
  int max_num = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    int s = a[i];
    if (s < max_num) {
      cnt += max_num - s;
    }
    max_num = max(max_num, s);
  }
  cout << cnt << endl;
  return 0;
}
