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