Problem:
Score : 400 points

Problem Statement

Snuke has a large collection of cards. Each card has an integer between 1 and N, inclusive, written on it.
He has A_i cards with an integer i.

Two cards can form a pair if the absolute value of the difference of the integers written on them is at most 1.

Snuke wants to create the maximum number of pairs from his cards, on the condition that no card should be used in multiple pairs. Find the maximum number of pairs that he can create.

Constraints

1 ≦ N ≦ 10^5

0 ≦ A_i ≦ 10^9 (1 ≦ i ≦ N)

All input values are integers.

Input

The input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

Print the maximum number of pairs that Snuke can create.

Sample Input 1

4
4
0
3
2

Sample Output 1

4

For example, Snuke can create the following four pairs: (1,1),(1,1),(3,4),(3,4).

Sample Input 2

8
2
0
1
6
0
8
2
1

Sample Output 2

9

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

    import java.util.Scanner;
     
    public class Main {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
     
            int n = sc.nextInt();
     
            long[] cards = new long[n];
            for (int i = 0; i < n; i++) {
                cards[i] = sc.nextInt();
            }
     
            int pairCount = 0;
            for (int i = 0; i < n; i++) {
                long prevMin = 0, nextMin = 0;
                long prevPair = 0, prevCurrentPair = 0, nextCurrentPair = 0, nextPair = 0;
                if (i > 0) {
                    prevMin = Math.min(cards[i - 1], cards[i]);
                    prevMin = prevMin > 0 ? prevMin : 0;
                    prevPair = (cards[i - 1] - prevMin) / 2;
                    prevCurrentPair = (cards[i] - prevMin) / 2;
                } else if(i < n - 1) {
                    nextMin = Math.min(cards[i], cards[i + 1]);
                    nextMin = nextMin > 0 ? nextMin : 0;
                    nextPair = (cards[i + 1] - nextMin) / 2;
                    nextCurrentPair = (cards[i] - nextMin) / 2;
                }
     
                long prev = prevMin + prevPair + prevCurrentPair;
                long next = nextMin + nextPair + nextCurrentPair;
     
                if (prev >= next) {
                    cards[i - 1] -= prevMin + prevPair * 2;
                    cards[i] -= prevMin + prevCurrentPair * 2;
                    pairCount += prev;
                } else if (next > prev) {
                    cards[i] -= nextMin + nextCurrentPair * 2;
                    cards[i + 1] -= nextMin + nextPair * 2;
                    pairCount += next;
                }
            }
     
            System.out.println(pairCount);
        }
    }