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