#include <bits/stdc++.h>
using namespace std;
const long long INF = 63;
const int mxn = 0;
bool DEBUG = 0;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  int mx = 0, ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    ans += (mx > a[i] ? mx - a[i] : 0);
    mx = max(mx, a[i]);
  }
  cout << ans << "\n";
}
