#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, sum = 0;
  cin >> N;
  vector<int> ans(N);
  ans[0] = 0;
  vector<int> ques(N);
  for (int i = 0; i < N; i++) {
    cin >> ques[i];
    if (i > 0) {
      if (ques[i] < ques[i - 1]) {
        ans[i] = ques[i - 1] - ques[i];
        ques[i] = ques[i] + ans[i];
      } else
        ans[i] = 0;
    }
  }
  for (int i = 0; i < N; i++) sum = sum + ans[i];
  cout << sum;
  return 0;
}
