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