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