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