#include <bits/stdc++.h>
using namespace std;
int getsum(int n[], int t) {
  int tool = 0;
  for (int i = 0; i < t - 1; i++) {
    int a = n[i + 1] - n[i];
    if (a < 0) {
      tool += -a;
      n[i + 1] += -a;
    }
  }
  return tool;
}
int main() {
  int n;
  cin >> n;
  int line[n];
  for (int i = 0; i < n; i++) {
    cin >> line[i];
  }
  int p = getsum(line, n);
  cout << p;
}
