#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, A;
  int max = 0, sum = 0;
  cin >> N;
  for (int j = 0; j < N; j++) {
    cin >> A;
    if (max < A) {
      max = A;
    } else {
      sum = sum + (max - A);
    }
  }
  cout << sum << endl;
  return 0;
}
