#include <bits/stdc++.h>
using namespace std;
int ar[1000000];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> ar[i];
  int ans = 0;
  int ma = ar[0];
  for (int i = 0; i < n; i++) {
    ma = max(ma, ar[i]);
    if ((ar[i]) - ma < 0) {
      ans += abs(ma - ar[i]);
    }
  }
  cout << ans << endl;
  return 0;
}
