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