#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);
  cin >> ques[0];
  for (int i = 1; i < N; i++) {
    cin >> ques[i];
    if (ques[i] > ques[i - 1]) {
      ans[i] = ques[i] - ques[i - 1];
      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;
}
