#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int n, i, t;
int a[100001];
void inp() {
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] < a[i - 1]) {
      t += a[i - 1] - a[i];
      a[i] = a[i - 1];
    }
  }
}
void process() { cout << t; }
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  inp();
  process();
  return 0;
}
