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