#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  int a[200000];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int old = 0;
  int dif;
  int addif = 0;
  for (int i = 0; i < n; i++) {
    if (old > a[i]) {
      dif = abs(old - a[i]);
      a[i] += dif;
      addif += dif;
    }
    old = a[i];
  }
  cout << addif << endl;
  return 0;
}
