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