#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  int a[t], aaa = 0;
  for (int i = 0; i < t; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < t; i++) {
    int zzz = abs(a[i] - a[i - 1]);
    if (a[i] < a[i - 1] && (i != 0)) {
      a[i] = a[i] + zzz;
      aaa = aaa + zzz;
    }
  }
  cout << aaa << "\n";
}
