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