#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  int A[200000];
  int max = 0;
  int total = 0;
  cin >> N;
  for (int a = 0; a < N; a++) cin >> A[a];
  max = A[0];
  for (int i = 0; i < N; i++) {
    if (max < A[i]) max = A[i];
    total += max - A[i];
  }
  cout << total;
}
