#include <bits/stdc++.h>
using namespace std;
long long step_num = 0;
void search(int n, long long *a) {
  if (n <= 0) return;
  long long max_h = a[0];
  int max_h_num = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] > max_h) {
      max_h = a[i];
      max_h_num = i;
    }
  }
  for (int i = n - 1; i >= 0; i--) {
    if (i > max_h_num)
      step_num += max_h - a[i];
    else if (i == max_h_num)
      break;
  }
  search(max_h_num, a);
  return;
}
int main() {
  int n;
  long long a[200000];
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];
  search(n, a);
  cout << step_num << endl;
  return 0;
}
