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