#include <bits/stdc++.h>
const int MAX = 5e6 + 9;
const long long MOD = 1e9 + 7;
const int TOT_PRIMES = 19;
const int MAX_A = 70;
using namespace std;
int main() {
  int n;
  cin >> n;
  int arr[n + 1];
  for (int i = 1; i <= n; i++) cin >> arr[i];
  int ans = 0;
  for (int i = 2; i <= n; i++) {
    if (arr[i] < arr[i - 1]) {
      ans = ans + (arr[i - 1] - arr[i]);
      arr[i] = arr[i - 1];
    }
  }
  cout << ans;
}
