#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Graph = vector<vector<int>>;
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < (N); i++) {
    cin >> A[i];
  }
  int ans = 0;
  int mx = A[0];
  for (int i = 1; i < N; i++) {
    if (A[i] < mx) {
      ans += mx - A[i];
    } else {
      mx = A[i];
    }
  }
  cout << ans << endl;
  return 0;
}
