#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  int cnt = 0;
  int mx = INT_MIN;
  while (n--) {
    int x;
    cin >> x;
    if (mx <= x)
      mx = x;
    else
      cnt += (mx - x);
  }
  cout << cnt;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  solve();
  return 0;
}
