#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  scanf("%d", &n);
  vector<long long> vec;
  for (int i = 0; i < n; i++) {
    long long x;
    cin >> x;
    vec.push_back(x);
  }
  long long val = 0;
  for (int i = 1; i < n; i++) {
    val += max(vec[i] - vec[i - 1], 0ll);
    vec[i] += max(vec[i] - vec[i - 1], 0ll);
  }
  cout << val << endl;
  return 0;
}
