#include <bits/stdc++.h>
using namespace std;
int main() {
  int A, N, Max = 0, goukei = 0;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A;
    goukei += max(Max - A, 0);
    Max = max(A, Max);
  }
  cout << goukei << endl;
}
