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