#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  while (t--) {
    int n;
    cin >> n;
    int sum = 0;
    int arr[n];
    for (auto &i : arr) cin >> i;
    ;
    for (int i = 1; i < n; i++) {
      if (arr[i] < arr[i - 1])
        sum += arr[i - 1] - arr[i], arr[i] += arr[i - 1] - arr[i];
    }
    cout << sum;
  }
}
