#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int32_t main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int now = a[0];
  int answer = 0;
  for (int i = 1; i < n; i++) {
    now = max(now, a[i]);
    answer += now - a[i];
  }
  cout << answer << endl;
}
