#include <bits/stdc++.h>
constexpr int MOD = 1e9 + 7;
const int MAX = 1e5 + 5;
using namespace std;
long long int gcd(int a, int b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
void solve() {
  int n;
  cin >> n;
  int ans = 0, mx = -1, x;
  for (long long int i = 0; i < n; i++) {
    cin >> x;
    if (mx == -1)
      mx = x;
    else {
      if (x < mx) ans += (mx - x);
    }
    mx = max(mx, x);
  }
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int q = 1;
  while (q--) {
    solve();
  }
}
