#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.at(i);
  }
  vector<int> B(N, 0);
  B.at(0) = 0;
  int total = 0;
  for (int i = 0; i < N - 1; i++) {
    int b = A.at(i) - A.at(i + 1) + B.at(i);
    if (b < 0) b = 0;
    for (int j = b; j < 1000000000; j++) {
      B.at(i + 1) = j;
      if ((A.at(i) + B.at(i)) <= (A.at(i + 1) + B.at(i + 1))) {
        break;
      }
    }
    total = total + B.at(i + 1);
  }
  cout << total << endl;
}
