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