#include <bits/stdc++.h>
using namespace std;
int main() {
  {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
  };
  int n;
  cin >> n;
  long long int a[n];
  long long int sum = 0, sum1 = 0;
  long long int ans = 0, t;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  ans = a[1];
  for (int i = 2; i <= n; i++) {
    if (ans > a[i]) {
      sum += (ans - a[i]);
    } else {
      ans = max(ans, a[i]);
    }
  }
  cout << sum << endl;
  return 0;
}
