#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int a[N];
int n, m;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  int s = 0;
  for (int i = 1; i < n; i++) {
    int x = a[i + 1] - a[i];
    if (x < 0) a[i + 1] += -x;
    s += x < 0 ? -x : 0;
  }
  cout << s << endl;
  return 0;
}
