#include <bits/stdc++.h>
using namespace std;
int n, a[200001];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL), cout.tie(NULL);
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];
  int ans = 0, max_yet = a[0];
  for (int i = 1; i < n; i++) {
    if (a[i] < max_yet) ans += (max_yet - a[i]);
    max_yet = max(max_yet, a[i]);
  }
  cout << ans << "\n";
  return 0;
}
