#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  vector<int> test(N);
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> test.at(i);
  }
  long long ans = 0;
  for (int i = 0; i < N; i++) {
    if (i == 0) {
      continue;
    }
    if (test.at(i - 1) > test.at(i)) {
      ans = ans + test.at(i - 1) - test.at(i);
      test.at(i) = test.at(i - 1);
    }
  }
  cout << ans << endl;
}
