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