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