#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, x, dai = 0;
  cin >> n;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> x;
    dai = max(x, dai);
    if (x <= dai) ans += dai - x;
  }
  cout << ans << endl;
  return 0;
}
