#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  long long H = 0;
  long long B = 0;
  for (int X = 0; X < N; X++) {
    long long A;
    cin >> A;
    if (X != 0) {
      if (B > A) H += B - A;
    }
    B = A;
  }
  cout << H << endl;
}
