#include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; const ll MOD = 1000000007; struct Node { int idx; int v; Node(int idx = -1, int v = -1) { this->idx = idx; this->v = v; } bool operator>(const Node &n) const { return v < n.v; } }; int main() { int N; cin >> N; vector A(N); priority_queue , greater> pque; for (int i = 0; i < N; ++i) { cin >> A[i]; if (1 <= i && i <= N - 2) { pque.push(Node(i, A[i])); } } int ans = 0; while (not pque.empty()) { Node node = pque.top(); pque.pop(); int left = node.idx - 1; int right = node.idx + 1; // fprintf(stderr, "(%d, %d, %d)\n", A[left], A[node.idx], A[right]); if (A[left] < A[node.idx] && A[node.idx] > A[right]) { int nv = min(A[left], A[right]) - 1; A[node.idx] = nv; ++ans; pque.push(Node(node.idx, nv)); } } cout << ans << endl; return 0; }