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