#include <bits/stdc++.h>
using namespace std;
int a[200005], n, ma[200005], sum = 0;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];
  ma[0] = a[0];
  for (int i = 1; i < n; i++) {
    ma[i] = max(ma[i - 1], a[i]);
    sum += ma[i] - a[i];
  }
  cout << sum;
}
