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