#include <bits/stdc++.h>
using namespace std;
int arr[200200];
void solve(int tc) {
  int n, ans = 0;
  cin >> n;
  for (int i = 0; i < (n); i++) cin >> arr[i];
  for (int i = 1; i < n; i++) {
    if (arr[i - 1] > arr[i]) {
      ans += arr[i - 1] - arr[i];
      arr[i] = arr[i - 1];
    }
  }
  cout << ans << endl;
}
signed main() {
  int T = 1, tc;
  for (tc = 1; tc <= T; tc++) solve(tc);
}
