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