#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const int INF = 1e9;
int main() {
  int n;
  const int max_n = 2 * 1e5;
  int a[max_n + 1];
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];
  int ans = 0;
  int current_min = a[0];
  for (int i = 1; i < n; i++) {
    if (a[i] > current_min) {
      current_min = a[i];
    } else {
      ans += current_min - a[i];
    }
  }
  cout << ans << endl;
  return 0;
}
