#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
using namespace std;
void fastio() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
long long dp[101][1001], ans1 = 1e10, dp1[1000001];
long long num[10];
signed main() {
  fastio();
  long long t = 1;
  while (t--) {
    long long n, xx = 0, ans = 0;
    cin >> n;
    long long a[n + 2];
    for (long long i = 0; i < n; i++) cin >> a[i];
    long long mm = -1;
    for (long long i = 0; i < n - 1; i++) {
      if (a[i] > mm) mm = a[i];
      if (a[i + 1] < a[i]) ans += (mm - a[i + 1]);
    }
    cout << ans;
  }
}
