#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int64_t> A;
  int64_t tmp;
  int64_t judge = 0;
  int result = 0;
  for (int64_t i = 0; i < N; i++) {
    cin >> tmp;
    A.push_back(tmp);
    if (i == 0) {
      judge = A.at(0);
      continue;
    }
    if (judge <= tmp)
      judge = tmp;
    else
      result += (judge - tmp);
  }
  cout << result << endl;
  return 0;
}
