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