#include <bits/stdc++.h>
using namespace std;
int main() {
  int tam, ans = 0;
  cin >> tam;
  int arr[tam + 2];
  arr[0] = 0;
  arr[tam + 1] = 0;
  for (int i = 1; i <= tam; i++) cin >> arr[i];
  for (int j = 1; j < tam + 1; j++) {
    if (arr[j] < arr[j - 1]) {
      ans += arr[j - 1] - arr[j];
      arr[j] += arr[j - 1] - arr[j];
    }
  }
  cout << ans << endl;
}
