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