#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, c, ans = 0, m = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> c;
    m = max(m, c);
    ans += m - c;
  }
  cout << ans << "\n";
  return 0;
}
