#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  int n;
  cin >> n;
  int sum = 0, temp = 0;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    if (i > 0) {
      if (temp - a > 0) {
        sum += temp - a;
        a += (temp - a);
      }
    }
    temp = a;
  }
  cout << sum << endl;
  return 0;
}
