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