#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  int input[N];
  for (int i = 0; i < N; ++i) {
    cin >> input[i];
  }
  int sum = 0;
  int highest = 0;
  for (int i = 0; i < N; ++i) {
    if (highest <= input[i]) {
      highest = input[i];
    }
    sum += highest - input[i];
  }
  cout << sum << endl;
}
