#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }
  int key = 0;
  vector<int> B(N);
  for (int i = 1; i < N; ++i) {
    key = A[i - 1] + B[i - 1];
    if (key >= A[i])
      B[i] = key - A[i];
    else
      B[i] = 0;
  }
  cout << accumulate(B.begin(), B.end(), 0);
  return 0;
}
