#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int N = 1e5 + 5;
void solve() {
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++) cin >> a[i];
  int x = a[0];
  int ans = 0;
  for (int i = 1; i <= n - 1; i = i + 1) {
    x = max(x, a[i]);
    ans = ans + (x - a[i]);
  }
  cout << ans;
}
int main() {
  std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t = 1;
  while (t--) {
    solve();
    cout << endl;
  }
}
