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