#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;
  for (int i = 0; i < (n); ++i) {
    cin >> a;
    (a > mx) ? mx = a : ans += mx - a;
  }
  cout << ans << endl;
  return 0;
}
