#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, count = 0, current_hight = 0, previous_hight = 0, fa;
  cin >> N >> fa;
  previous_hight = fa;
  for (int i = 1; i < N; i++) {
    cin >> current_hight;
    if (current_hight < previous_hight)
      count += previous_hight - current_hight;
    else
      previous_hight = current_hight;
  }
  cout << count << endl;
}
