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