#include <bits/stdc++.h>
using namespace std;
int a[200001];
void solve() {
  int n;
  cin >> n;
  for (int i = 0; i < (n); i++) cin >> a[i];
  int lar = a[0];
  int sum = 0;
  for (int i = 1; i < (n); i++) {
    if (a[i] >= lar)
      lar = a[i];
    else
      sum += lar - a[i];
  }
  cout << sum;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int test = 1;
  while (test--) {
    solve();
    cout << "\n";
  }
  return 0;
}
