#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, i, j, k;
  cin >> n;
  int a[n];
  stack<int> s;
  int mx = INT_MIN;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] > mx) mx = a[i];
    s.push(mx);
  }
  int sum = 0;
  for (i = n - 1; i >= 0; i--) {
    sum += s.top() - a[i];
    s.pop();
  }
  cout << sum << endl;
}
