#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> data(n);
  for (int i = 0; i < (n); ++i) {
    cin >> data[i];
  }
  int bf = 0;
  int sum = 0;
  for (int i = 0; i < (n); ++i) {
    int current = data[i];
    int step = 0;
    if (bf > current) {
      step = bf - current;
    }
    bf = current + step;
    sum += step;
  }
  cout << sum;
  return 0;
}
