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