#include <bits/stdc++.h>
using namespace std;
int main() {
  uint32_t N;
  cin >> N;
  vector<uint32_t> vec(N);
  for (uint32_t i = 0; i < N; ++i) {
    cin >> vec.at(i);
  }
  uint32_t max = 0;
  uint32_t ans = 0;
  for (const auto &x : vec) {
    if (x <= max) {
      ans += max - x;
    }
    if (x > max) {
      max = x;
    }
  }
  cout << ans << endl;
}
