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