#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, s = 0, a, cur = INT_MIN;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a;
    if (cur < a) {
      cur = a;
    } else {
      s += cur - a;
    }
  }
  cout << s << '\n';
  return 0;
}
