#include <bits/stdc++.h>
using namespace std;
const int INF = 2e9 + 3;
const int N = 1e5 + 3;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;
const double PI = acos(-1);
void solve() {
  int n;
  cin >> n;
  int a[n];
  for (int &x : a) cin >> x;
  int ans = 0, mx = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] < mx) ans += mx - a[i];
    mx = max(mx, a[i]);
  }
  cout << ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  int t;
  t = 1;
  while (t--) {
    solve();
  }
}
