#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  int previous = 0;
  int box_sum = 0;
  for (int i = 0; i < N; i++) {
    int height;
    cin >> height;
    if (i == 0) {
      previous = height;
      continue;
    }
    if (height < previous) {
      int diff = previous - height;
      box_sum += diff;
    } else {
      previous = height;
    }
  }
  cout << box_sum << endl;
}
