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