#include <bits/stdc++.h>
using namespace std;
int main() {
  int num;
  int sum = 0;
  int index = 0;
  bool flag = false;
  cin >> num;
  int* a = new int[num];
  for (int i = 0; i < num; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < num - 1; i++) {
    if (a[i + 1] < a[i]) {
      if (!flag) {
        index = i;
        sum += a[index] - a[i + 1];
        flag = true;
      } else {
        sum += a[index] - a[i + 1];
      }
    } else {
      flag = false;
    }
  }
  cout << sum;
}
