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