#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, i, j = INT_MIN, temp;
  cin >> n;
  int sum = 0;
  vector<int> vect(n);
  for (i = 0; i < n; i++) {
    cin >> temp;
    vect[i] = temp;
    j = max(temp, j);
    sum += j - temp;
  }
  j = INT_MIN;
  temp = 0;
  for (i = vect.size() - 1; i >= 0; i--) {
    j = max(vect[i], j);
    temp += j - vect[i];
  }
  sum = min(sum, temp);
  cout << sum << endl;
  return 0;
}
