Under construction

Still being built. Things may change

Triangle Construction

Time limit2sMemory limit1024 MB

Summary
Given a regular polygon whose sides carry A_i equally spaced points, find the most non-crossing, non-degenerate triangles with disjoint corners.
Level

Medium7 of 10

Topics
Greedy, Math, Geometry
Solved
No attempts yet

Problem

You are given a regular NN-sided polygon. Label one arbitrary side as side 11, then label the next sides in clockwise order as side 2,3,...,N2, 3, ..., N. There are AiA_i special points on side ii. These points are positioned so that side ii is divided into Ai+1A_i + 1 segments of equal length.

For instance, suppose that you have a regular 44-sided polygon, i.e., a square. The following illustration shows how the special points are located within each side when A=[3,1,4,6]A = [3, 1, 4, 6]. The uppermost side is labelled as side 11.

You want to create as many non-degenerate triangles as possible while satisfying the following requirements. Each triangle consists of 33 distinct special points (not necessarily from different sides) as its corners. Each special point can be a corner of at most 11 triangle. All triangles must not intersect with each other.

Determine the maximum number of non-degenerate triangles that you can create.

A triangle is non-degenerate if it has a positive area.

Input

The first line contains an integer NN (3≤N≤200 0003 \le N \le 200\,000).

The following line contains NN integers AiA_i (1≤Ai≤2⋅1091 \le A_i \le 2 \cdot 10^9).

Output

Output a single integer representing the maximum number of non-degenerate triangles that you can create.

Examples3

  1. Example 1

    Input
    4
    3 1 4 6
    
    Expected output
    4
    
  2. Example 2

    Input
    6
    1 2 1 2 1 2
    
    Expected output
    3
    
  3. Example 3

    Input
    3
    1 1 1
    
    Expected output
    1