#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e5 + 5;
const int MAX_L = 20;
const long long MOD = 1e9 + 7;
const long long INF = 1e9 + 7;
void solve() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) cin >> a[i];
  int ans = 0;
  int mx = a[0];
  for (auto &i : a) {
    mx = max(mx, i);
    ans += mx - i;
  }
  cout << ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
  cerr << "\ntime taken : " << (float)clock() / CLOCKS_PER_SEC << " secs"
       << endl;
  return 0;
}
