#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
const long long inf = 1000000001;
const double PI = 3.14159265358979323846;
int main(void) {
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < (n); i++) cin >> a[i];
  int sum = 0;
  for (int i = 0; i < (n); i++) {
    if (a[i] > a[i + 1]) {
      int diff = a[i] - a[i + 1];
      a[i + 1] += diff;
      sum += diff;
    }
  }
  cout << sum << endl;
}
