#include <bits/stdc++.h>
using namespace std;
void c_p_c() {}
void solution() {
  int n;
  cin >> n;
  int arr[n];
  for (int i = 0; i < n; i++) cin >> arr[i];
  int ans = 0;
  int Max = arr[0];
  for (int i = 1; i < n; i++) {
    if (arr[i] < Max) {
      ans += Max - arr[i];
    } else {
      Max = arr[i];
    }
  }
  cout << ans << endl;
}
int32_t main() {
  c_p_c();
  solution();
  return 0;
}
