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