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