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