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