#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;
    if (Max <= A) {
      Max = A;
    }
    goukei += (Max - A);
  }
  cout << goukei << endl;
}
