#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++) cin >> a[i];
  int mx = -1;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    mx = max(a[i], mx);
    ans += mx - a[i];
  }
  cout << ans;
}
int main() {
  int t = 1;
  while (t--) {
    solve();
  }
}
