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