#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n;
  cin >> n;
  long long a[n];
  for (long long i = 0; i < n; i++) cin >> a[i];
  long long ans = 0, dp[n];
  dp[0] = a[0];
  for (long long i = 1; i < n; i++) dp[i] = max(dp[i - 1], a[i]);
  for (long long i = 0; i < n; i++)
    if (dp[i] > a[i]) ans += max(ans, dp[i] - a[i]);
  cout << ans << "\n";
  return 0;
}
