#include <bits/stdc++.h>
using namespace std;
const double pi = 3.1415926535;
const long long mod = 1000000007;
int main() {
  int n;
  cin >> n;
  vector<int> v(n);
  {
    for (int i = 0; i < n; i++) cin >> v[i];
  }
  int ans = 0;
  int ma = 0;
  for (int i = 0; i < n; i++) {
    ma = max(ma, v[i]);
    ans += ma - v[i];
  }
  cout << ans << endl;
}
