#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int arr[n];
  int ans = 0;
  int temp;
  for (int& i : arr) cin >> i;
  for (int i = 1; i < n; i++) {
    temp = arr[i] - arr[i - 1];
    if (temp < 0) {
      arr[i] += abs(temp);
      ans += abs(temp);
    }
  }
  cout << ans;
}
