#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 curr = v[0], ans = 0;
  for (int i = 1; i < n; i++) {
    ans += max(0, curr - v[i]);
    curr = max(curr, v[i]);
  }
  cout << ans << "\n";
}
