#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 10;
int a[maxn], mx, n;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    mx = max(mx, a[i]);
    if (a[i] < mx) ans += mx - a[i];
  }
  cout << ans;
}
