#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
int main() {
  int n;
  cin >> n;
  int a[MAXN] = {0};
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] < a[i - 1]) {
      cnt += a[i - 1] - a[i];
      a[i] = a[i - 1];
    }
  }
  cout << cnt;
  return 0;
}
