#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int height[n];
  for (int i = 0; i < n; i++) {
    cin >> height[i];
  }
  int step[n];
  for (int i = 0; i < n; i++) step[i] = 0;
  for (int i = 1; i < n; i++) {
    if (height[i - 1] + step[i - 1] > height[i])
      step[i] += height[i - 1] + step[i - 1] - height[i];
  }
  int ans = 0;
  for (int i = 0; i < n; i++) ans += step[i];
  cout << ans << endl;
  return 0;
}
