#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  vector<int> A(N);
  for (int i = 0; i < N; ++i) cin >> A[i];
  int max_height = 0;
  long long ans = 0;
  for (int i = 0; i < N; ++i) {
    if (max_height > A[i]) {
      ans += max_height - A[i];
    } else {
      max_height = A[i];
    }
  }
  cout << ans << endl;
  return 0;
}
