#include <bits/stdc++.h>
using namespace std;
const int N = 332748118;
const int M = 5e5 + 10;
int main() {
  std::ios::sync_with_stdio(0);
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int cnt = 0;
  int maxn = a[0];
  for (int i = 0; i < n; i++) {
    if (a[i] < maxn) {
      cnt += maxn - a[i];
      maxn = max(maxn, a[i]);
    } else {
      maxn = max(maxn, a[i]);
    }
  }
  cout << cnt << endl;
}
