#include <bits/stdc++.h>
using namespace std;
int main() {
  int n = 0;
  cin >> n;
  int* s = new int[n];
  int* t = new int[n];
  for (int i = 0; i < n; i++) cin >> s[i];
  t[0] = 0;
  for (int i = 0; i < n - 1; i++) {
    if (s[i] <= s[i + 1])
      t[i + 1] = 0;
    else {
      t[i + 1] = (s[i] - s[i + 1]);
      s[i + 1] += t[i + 1];
    }
  }
  int sum = 0;
  for (int i = 0; i < n; i++) sum += t[i];
}
