#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int N;
  cin >> N;
  int ans = 0;
  int maxnum = 0;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    if (i == 0) {
      maxnum = a;
      continue;
    }
    if (a < maxnum) {
      ans += maxnum - a;
    } else {
      maxnum = a;
    }
  }
  cout << ans << endl;
}
