#include <bits/stdc++.h>
using namespace std;
int main() {
  int total_people;
  cin >> total_people;
  int answer = 0;
  int people_list[total_people];
  for (int i = 0; i < total_people; i++) {
    int height;
    cin >> height;
    people_list[i] = height;
    if (i != 0) {
      if (people_list[i - 1] > people_list[i]) {
        answer += people_list[i - 1] - people_list[i];
      }
    }
  }
  cout << answer << "\n";
  return 0;
}
