#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  int ans = 0;
  int x = v[0];
  vector<int> v1 = v;
  for (int i = 1; i < n; i++) {
    x = max(x, v[i]);
    if (v[i] - x < 0) {
      ans += abs(x - v[i]);
    }
  }
  cout << ans << endl;
}
