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