#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  int a[n];
  int dp[n];
  int h[n];
  int i;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    h[i] = a[i];
  }
  dp[0] = 0;
  for (i = 1; i < n; i++) {
    if (a[i] < h[i - 1]) {
      h[i] = a[i] + abs(a[i] - h[i - 1]);
      dp[i] = dp[i - 1] + abs(a[i] - h[i - 1]);
    } else {
      dp[i] = dp[i - 1];
    }
  }
  cout << dp[n - 1] << endl;
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  long long t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
