#include <bits/stdc++.h>
using namespace std;
int N, sum = 0, back = 0, pre = 0;
int num = 0;
vector<int> A, tmp;
int getmax(int a, int b) {
  int max = a;
  if (a > b) {
    max = a;
  } else {
    max = b;
  }
  return max;
}
int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> num;
    A.push_back(num);
  }
  for (int i = 1; i < N; i++) {
    back = getmax(back, A[i - 1]);
    pre = A[i];
    if (back >= pre) {
      sum += (back - pre);
      A[i] = back;
    }
  }
  cout << sum;
  return 0;
}
