#include <bits/stdc++.h>
using namespace std;
void max_self(int &a, int b) { a = max(a, b); }
void min_self(int &a, int b) { a = min(a, b); }
void solve() {
  int n;
  cin >> n;
  int prev, x, ans = 0;
  cin >> prev;
  for (int i = 1; i < n; i++) {
    cin >> x;
    if (x < prev) {
      ans += prev - x;
    } else {
      prev = x;
    }
  }
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}
