#include <bits/stdc++.h>
using namespace std;
int INF = 1e9 + 7;
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(0);
  int n, ans = 0;
  cin >> n;
  vector<int> v(n);
  for (int i = 0; i < n; i++) cin >> v[i];
  int maximum = v[0];
  for (int i = 1; i < n; i++) {
    maximum = max(maximum, v[i]);
    ans += maximum - v[i];
  }
  cout << ans;
}
