#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  int a[n];
  for (int& u : a) {
    cin >> u;
  }
  int ans = 0;
  long long maxHeight = -1;
  for (int i = 0; i < n; i++) {
    maxHeight = max(maxHeight, 1ll * a[i]);
    ans += maxHeight - a[i];
  }
  cout << ans << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t = 1;
  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}
