#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  vector<int> a;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    a.push_back(tmp);
  }
  int total = 0;
  int mae = a[0];
  for (int i = 1; i < n; i++) {
    int fumidai = 0;
    if (mae <= a[i]) {
      fumidai = 0;
      mae = a[i];
    } else {
      fumidai = mae - a[i];
      mae = a[i] + fumidai;
    }
    total += fumidai;
  }
  cout << total << endl;
  return 0;
}
