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