#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  int n;
  cin >> n;
  int a1 = 0, a2 = 0;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    cin >> a2;
    if (a2 <= a1) {
      cnt += a1 - a2;
    } else {
      a1 = a2;
    }
  }
  cout << cnt << endl;
  return 0;
}
