#include <bits/stdc++.h>
using namespace std;
int a[1000005];
int main() {
  int n, cnt = -1e9, sum = 0;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++) {
    cnt = max(cnt, a[i]);
    sum += cnt - a[i];
  }
  cout << sum;
  return 0;
}
