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