#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ll n, def, sum;
  int A[n];
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    if (i != 0) {
      if (A[i - 1] <= A[i])
        continue;
      else {
        def = A[i - 1] - A[i];
        A[i - 1] = def + A[i];
        sum += def;
      }
    }
  }
  cout << sum << endl;
  return 0;
}
