#include <bits/stdc++.h>
using namespace std;
void test_case() {
  int n;
  cin >> n;
  int tallest;
  int total = 0;
  cin >> tallest;
  for (int i = 1; i < n; i++) {
    int a;
    cin >> a;
    total += max(0, tallest - a);
    tallest = max(tallest, a);
  }
  cout << total << endl;
}
int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t = 1;
  while (t--) {
    test_case();
  }
  return 0;
}
