#include <bits/stdc++.h>
const double pi = 3.141592653589793238462643383279;
using namespace std;
const long double EPS = 1e-6, PI = acos((long double)-1);
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(4);
  int N;
  vector<long long> v(N);
  cin >> N;
  for (int i = 0; i < (int)N; ++i) cin >> v[i];
  long long ans = 0;
  long long mx = v[0];
  for (int i = 1; i < N; i++) {
    ans += max(0LL, mx - v[i]);
    mx = max(mx, v[i]);
  }
  cout << ans << endl;
  return 0;
}
