#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, i, j;
  long long localmin, stdec, enddec, sum;
  long long arr[100005];
  bool flag;
  while (cin >> n) {
    cin >> arr[0];
    flag = false;
    sum = 0;
    for (i = 1; i < n; i++) {
      cin >> arr[i];
      if (arr[i] < arr[i - 1]) sum += arr[i - 1] - arr[i];
    }
    cout << sum << endl;
  }
}
