#include <bits/stdc++.h>
using namespace std;
int main() {
  int sum = 0;
  int a[1000000];
  int b[1000000];
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int max = 0;
  for (int i = 0; i < n; i++) {
    if (max < a[i]) {
      max = a[i];
    } else {
      b[i] = max - a[i];
      sum += b[i];
    }
  }
  cout << sum << endl;
  return 0;
}
