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