#include <bits/stdc++.h>
using namespace std;
int N, k, arr[200005], res;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> N;
  for (int i = 0; i < N; i++) cin >> arr[i];
  for (int i = 1; i < N; i++) {
    if (arr[i] < arr[i - 1]) {
      res += arr[i - 1] - arr[i];
      arr[i] = arr[i - 1];
    }
  }
  cout << res;
}
