#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int n, box[200001], i, MAX, sum;
  cin >> n;
  sum = 0;
  MAX = -1;
  for (i = 0; i < n; i++) {
    cin >> box[i];
    if (MAX > box[i]) {
      sum += MAX - box[i];
      box[i] += MAX - box[i];
    }
    MAX = max(MAX, box[i]);
  }
  cout << sum << endl;
  return 0;
}
