#include <bits/stdc++.h>
using namespace std;
int n;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  clock_t ck;
  ck = clock();
  cin >> n;
  int maxi = 0;
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    maxi = max(maxi, x);
    ans += maxi - x;
  }
  cout << ans << '\n';
  ck = clock() - ck;
  cerr << "It took " << 1.0 * ck / CLOCKS_PER_SEC << " sec\n";
  return 0;
}
