#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  int arr[n];
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  int ans = 0;
  int max = arr[0];
  for (int i = 0; i < n; i++) {
    if (arr[i] > max) max = arr[i];
    if (max > arr[i]) {
      ans += max - arr[i];
    }
  }
  cout << ans << "\n";
}
int main() {
  ios::sync_with_stdio(0);
  cout.tie(0);
  cin.tie(0);
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
