#include <bits/stdc++.h>
#pragma GCC optimize("Ofast", "-funroll-loops", "-fdelete-null-pointer-checks")
#pragma GCC target("ssse3", "sse3", "sse2", "sse", "avx2", "avx", "popcnt")
#pragma GCC optimize(2)
#pragma GCC optimize(3)
using namespace std;
const long long MOD = 1e9 + 7;
void solve() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int ans = 0;
  for (int i = 1; i < n; i++) {
    int diff = a[i - 1] - a[i];
    if (diff >= 0) {
      ans += diff;
      a[i] += diff;
    }
  }
  cout << ans;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  long long t = 1;
  for (long long tt = 1; tt <= t; tt++) {
    solve();
  }
  return 0;
}
