#include <bits/stdc++.h>
using namespace std;

int main() {
  long N;
  cin >> N;
  vector<long> A(N);
  for (long i = 0; i < N; i++) cin >> A.at(i);
  long mx = A.at(0);
  long ans = 0;
  for (long i = 1; i < N; i++) {
    ans += max(0, mx - A.at(i));
    mx = max(mx, A.at(i));
  }
  cout << ans << "\n";
}