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