#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  int N;
  cin >> N;
  int A[N];
  for (int i = 0; i < (int)(N); i++) {
    int a;
    cin >> a;
    A[i] = a;
  }
  int sum_step = 0;
  for (int i = 1; i < N; i++) {
    int b = A[i - 1] - A[i];
    if (b > 0) {
      A[i] += b;
      sum_step += b;
    }
  }
  cout << sum_step << endl;
  return 0;
}
