#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
void solve() {
  int N;
  cin >> N;
  vector<int> v(N);
  for (auto &x : v) {
    cin >> x;
  }
  int answer = 0;
  int mx = 0;
  for (int i = 0; i < N; i++) {
    answer += max(0, mx - v[i]);
    mx = max(mx, v[i]);
  }
  cout << answer << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  int testcases = 1;
  for (int i = 1; i <= testcases; i++) {
    solve();
  }
  return 0;
}
