#include <bits/stdc++.h>
#pragma GCC optimize("Ofast", "unroll-loops", "omit-frame-pointer", "inline")
#pragma GCC option("arch=native", "tune=native", "no-zero-upper")
#pragma GCC target("avx2")
using namespace std;
const int MAXN = 1e5 + 5;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const long double PI = acos(-1);
const long double EPS = 1e-9;
pair<int, int> mkp(int a, int b) {
  if (a > b) swap(a, b);
  return make_pair(a, b);
}
int mod_pow(int a, int b) {
  if (!b) return 1;
  if (b & 1) return mod_pow(a, b - 1) * a % MOD;
  int power = mod_pow(a, b >> 1);
  return power * power % MOD;
}
int modular_inverse(int a) { return mod_pow(a, MOD - 2); }
vector<int> movy = {0, 0, 1, -1};
vector<int> movx = {1, -1, 0, 0};
void solve() {
  int n, ans = 0;
  cin >> n;
  vector<int> a(n + 1, 0);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i - 1] > a[i]) {
      ans += a[i - 1] - a[i];
      a[i] = a[i - 1];
    }
  }
  cout << ans << '\n';
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  solve();
  return 0;
}
