#include <bits/stdc++.h>
using namespace std;
const int64_t mod = 1000000007;
const int64_t tmpMod = 998244353;
int main() {
  int64_t N, m, ans = 0;
  cin >> N;
  vector<int64_t> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    m = max(A[i], m);
    if (m > A[i]) ans += m - A[i];
  }
  cout << ans << endl;
}
