#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, max, a, tmp;
  int sum = 0;
  cin >> N;
  cin >> max;
  for (int i = 0; i < N - 1; i++) {
    cin >> a;
    tmp = max - a;
    if (0 > tmp) {
      max = a;
    } else {
      sum += tmp;
    }
  }
  cout << sum << endl;
  return 0;
}
