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