#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n = 0;
  cin >> n;
  int sum = 0;
  int maenoA = 0;
  int a;
  for (int i = 0; i < n; i++) {
    cin >> a;
    if (maenoA > a) {
      sum += maenoA - a;
      a = maenoA;
    }
    maenoA = a;
  }
  cout << sum << endl;
}
