#include <bits/stdc++.h>
using namespace std;
int n, x, t, ans = 0;
const int N = 2e5 + 5;
int a[N];
int mx = 0;
signed main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    if (i) {
      ans += max(0, mx - a[i]);
    }
    mx = max(mx, a[i]);
  }
  cout << ans;
  return 0;
}
