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