#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, x;
  vector<long long> v;
  for (long long i = 0; i < n; i++) {
    cin >> x;
    v.push_back(x);
  }
  long long d, h = 0;
  for (long long i = 1; i < n; i++) {
    if (v[i - 1] > v[i]) {
      d = v[i - 1] - v[i];
      h = h + d;
      v[i] = v[i] + d;
    }
  }
  cout << h;
  return (0);
}
