#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using st = string;
using P = pair<int, int>;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) cin >> a.at(i);
  int ans = 0;
  int maxval = 0;
  for (int i = 0; i < n; i++) {
    maxval = max(maxval, a.at(i));
    ans += maxval - a.at(i);
  }
  cout << ans << endl;
}
