#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, i;
  unsigned long long bef;
  unsigned long long A;
  unsigned long long step;
  unsigned long long sum;
  cin >> N;
  cin >> bef;
  for (i = 1; i < N; i++) {
    cin >> A;
    if (bef > A) {
      step = bef - A;
    } else {
      step = 0;
    }
    bef = A + step;
    sum += step;
  }
  cout << sum << endl;
  return 0;
}
