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