#include <bits/stdc++.h>
using namespace std;
long long a[200010];
int n;
long long ans = 0;
void sol(int right, int mi) {
  if (right <= 0) return;
  for (int i = mi; i < right; i++) ans += a[mi] - a[i];
  right = mi;
  mi = 0;
  for (int i = 0; i < right; i++)
    if (a[i] > a[mi]) mi = i;
  sol(right, mi);
}
int main() {
  int mi = 0;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 1; i < n; i++)
    if (a[i] > a[mi]) mi = i;
  sol(n, mi);
  cout << ans << endl;
  return 0;
}
