#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> arr;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  arr.resize(n);
  for (int i = 0; i < n; i++) cin >> arr[i];
  int ans = 0, mx = 0;
  for (int i = 0; i < arr.size(); i++) {
    mx = max(mx, arr[i]);
    ans += (mx - arr[i]);
  }
  cout << ans << "\n";
  return 0;
}
