#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pint = pair<int, int>;

int main()
{
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
  {
    cin >> a[i];
  }

  ll ans = 0;
  int ma = a[0];
  if (int i = 1; i < n; i++)
  {
    if (a[i] < ma)
    {
      ans += ma - a[i];
      ma = max(ma, a[i]);
    }
  }

  cout << ans << endl;

  return 0;
}
