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