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