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