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