#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, t, a, b, c, d, e, p, q, cnt = 0;
  map<long long int, long long int> mp;
  vector<long long int> vec;
  string s, s1, s2;
  long long int ac = 0, tle = 0, wa = 0, re = 0;
  cin >> n;
  long long int arr[n + 5];
  for (long long int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  for (long long int i = 0; i < n; i++) {
    if (arr[i] > arr[i + 1]) {
      long long int p = arr[i] - arr[i + 1];
      cnt += p;
      arr[i + 1] += p;
    }
  }
  cout << cnt << endl;
}
