#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
  int n;
  cin >> n;
  int ans = 0, mx = -1, a;
  while (cin >> a) (a > mx) ? mx = a : ans += mx - a;
  cout << ans << endl;
  return 0;
}
