#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; ++i) {
    cin >> A.at(i);
  }
  int res = 0;
  int temp = 0;
  int now = 0;
  for (int i = 0; i < N - 1; ++i) {
    if (temp != 0) {
      now = A.at(i) + temp;
    } else {
      now = A.at(i);
    }
    if (now >= A.at(i + 1)) {
      res += now - A.at(i + 1);
      temp = now - A.at(i + 1);
    } else {
      temp = 0;
    }
  }
  cout << res << endl;
}
