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