#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (auto& a : A) {
    cin >> a;
  }
  vector<int> buf(N);
  int tmp = 0;
  for (int i = 0; i < N; ++i) {
    buf[i] = tmp;
    tmp = max(tmp, A[i]);
  }
  int ans = 0;
  for (int i = 0; i < N; ++i) {
    ans += (A[i] > buf[i] ? 0 : buf[i] - A[i]);
  }
  cout << ans << endl;
  return 0;
}
