#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = 1001001001;
const long double PI = (acos(-1));
const int mod = 1e9 + 7;
const int vx[4] = {0, 1, 0, -1};
const int vy[4] = {1, 0, -1, 0};
ll gcd(ll x, ll y) {
  x = x % y;
  return x == 0 ? y : gcd(y, x);
}
ll lcm(ll x, ll y) { return x * y / gcd(x, y); }
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (ll i = 0; i < n; ++i) cin >> a[i];
  int now = a[0];
  ll ans = 0;
  for (ll i = 0; i < n; ++i) {
    if (i == 0) continue;
    if (now <= a[i]) {
      now = a[i];
    } else {
      ans += now - a[i];
      now = a[i - 1];
    }
  }
  cout << ans << endl;
  return 0;
}
