#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int& a : A) {
    cin >> a;
  }
  int ans = 0;
  for (int i = 0; i < N; i++) {
    auto it_max = max_element(A.begin(), A.begin() + i + 1);
    int d = *it_max - A[i];
    if (d > 0) ans += d;
  }
  cout << ans << endl;
  return 0;
}
