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