#include <bits/stdc++.h>
using namespace std;
int n;
int nowm;
int ans;
int u;
int main() {
  cin >> n;
  while (n--) {
    cin >> u;
    nowm = max(nowm, u);
    ans += nowm - u;
  }
  cout << ans;
  return 0;
}
