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