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