#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  int n, cnt = 0;
  cin >> n;
  ll a[200005];
  ll maxx;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++) {
    maxx = max(maxx, a[i]);
    if (i != 0 && maxx > a[i]) {
      cnt += (maxx - a[i]);
    }
  }
  cout << cnt << endl;
  return 0;
}
