#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, ar[200000];
  cin >> n;
  for (int i = 0; i < n; i++) cin >> ar[i];
  int sum = 0;
  int maxx = ar[0];
  for (int i = 1; i < n; i++) {
    if (ar[i] >= maxx)
      maxx = ar[i];
    else
      sum += (maxx - ar[i]);
  }
  cout << sum << endl;
  return 0;
}
