#include <bits/stdc++.h>
using namespace std;
int main() {
  int n = 0, ans = 0, mx = 0, x = 0;
  cin >> n;
  cin >> x;
  n--;
  mx = x;
  while (n--) {
    cin >> x;
    if (x < mx) {
      ans += mx - x;
      x = mx;
    }
    mx = max(mx, x);
  }
  cout << ans;
  return 0;
}
