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