#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, sum = 0, chukan = 0;
  cin >> N;
  vector<int> vec(N);
  for (int i = 0; i < N; i++) {
    cin >> vec[i];
  }
  for (int i = 0; i < N; i++) {
    chukan = max(chukan, vec[i]);
    sum += chukan - vec[i];
  }
  cout << sum;
  return 0;
}
