#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 9;
void slv() {
  int n;
  cin >> n;
  int a[n + 3];
  for (int i = 0; i < n; i++) cin >> a[i];
  int mx = 0;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    mx = max(mx, a[i]);
    ans += (mx - a[i]);
  }
  cout << ans << endl;
}
int main() {
  int t;
  t = 1;
  for (int cs = 1; cs <= t; cs++) {
    slv();
  }
  return 0;
}
