#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, ans = 0;
  cin >> N;
  long long max;
  cin >> max;
  for (int i = 1; i < N; i++) {
    int a;
    cin >> a;
    int diff = max - a;
    if (diff > 0) {
      ans += diff;
    } else {
      max = a;
    }
  }
  cout << ans << endl;
  return 0;
}
