#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);
  long long n, lr = 0, rl = 0;
  cin >> n;
  vector<long long> v(n);
  for (long long i = 0; i < n; i++) {
    cin >> v[i];
  }
  for (long long i = 1; i < n; i++) {
    if (v[i] > v[i - 1]) {
      lr += v[i] - v[i - 1];
    }
  }
  for (long long i = (n - 1) - 1; i >= 0; i--) {
    if (v[i] < v[i + 1]) {
      rl += v[i + 1] - v[i];
      v[i] = v[i + 1];
    }
  }
  cout << rl << '\n';
}
