#include <bits/stdc++.h>
using namespace std;
const long double pie = 3.1415926535897932384626;
int main() {
  long long n;
  long long arr[n];
  for (long long i = 0; i < n; i++) {
    cin >> arr[i];
  }
  long long ans = 0;
  for (long long i = 1; i < n; i++) {
    if (arr[i] < arr[i - 1]) {
      ans += (arr[i - 1] - arr[i]);
      arr[i] = arr[i - 1];
    }
  }
  cout << ans;
  return 0;
}
