#include <bits/stdc++.h>
using namespace std;
int dr[] = {0, 0, -1, +1, -1, -1, +1, +1};
int dc[] = {-1, +1, 0, 0, -1, +1, -1, +1};
int main() {
  int n;
  cin >> n;
  int arr[n];
  for (int &i : arr) cin >> i;
  int mx = arr[0];
  int ans = 0;
  for (int i = 1; i < n; i++) {
    if (arr[i] < mx) ans += mx - arr[i];
    mx = max(arr[i], mx);
  }
  cout << ans << '\n';
}
