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