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