#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int& a : A) {
    cin >> a;
  }
  int ans = 0;
  int max = A[0];
  for (int i = 0; i < N; i++) {
    int d = max - A[i];
    if (d > 0) ans += d;
    if (max < A[i]) max = A[i];
  }
  cout << ans << endl;
  return 0;
}
