Pavel has several sticks with lengths equal to powers of two.

He has $a_0$ sticks of length $2^0 = 1$, $a_1$ sticks of length $2^1 = 2$, ..., $a_{n-1}$ sticks of length $2^{n-1}$. 

Pavel wants to make the maximum possible number of triangles using these sticks. The triangles should have strictly positive area, each stick can be used in at most one triangle.

It is forbidden to break sticks, and each triangle should consist of exactly three sticks.

Find the maximum possible number of triangles.


-----Input-----

The first line contains a single integer $n$ ($1 \leq n \leq 300\,000$) — the number of different lengths of sticks.

The second line contains $n$ integers $a_0$, $a_1$, ..., $a_{n-1}$ ($1 \leq a_i \leq 10^9$), where $a_i$ is the number of sticks with the length equal to $2^i$.


-----Output-----

Print a single integer — the maximum possible number of non-degenerate triangles that Pavel can make.


-----Examples-----
Input
5
1 2 2 2 2

Output
3

Input
3
1 1 1

Output
0

Input
3
3 3 3

Output
3



-----Note-----

In the first example, Pavel can, for example, make this set of triangles (the lengths of the sides of the triangles are listed): $(2^0, 2^4, 2^4)$, $(2^1, 2^3, 2^3)$, $(2^1, 2^2, 2^2)$.

In the second example, Pavel cannot make a single triangle.

In the third example, Pavel can, for example, create this set of triangles (the lengths of the sides of the triangles are listed): $(2^0, 2^0, 2^0)$, $(2^1, 2^1, 2^1)$, $(2^2, 2^2, 2^2)$.

Difficulty: interview

Starter code:

Solutions:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [tuple(map(int, l.split())) for l in sys.stdin]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    n = I()
    a = LI()

    r = 0
    ai = 0
    for i in range(n):
        if a[i] < 2:
            continue
        while ai < i:
            if a[ai] < 1:
                ai += 1
                continue
            if a[i] < 2:
                break
            r += 1
            a[ai] -= 1
            a[i] -= 2
        r += a[i] // 3
        a[i] %= 3

    return r

print(main())




n = int(input())
A = list(map(int, input().split()))
dp = [A[0] // 3]
cnt = A[0]
for i in range(1, n):
    ans = 0
    x = cnt - 3 * dp[-1]
    y = A[i] // 2
    if y <= x:
        dp.append(dp[-1] + y)
    else:
        s = A[i] - 2 * x
        dp.append(dp[-1] + s // 3 + x)
    cnt += A[i]
print(dp[-1])

def main():
    n = int(input())
    arr = list(map(int, input().split()))
    ans = 0
    kek = 0
    for i in range(n):
        if kek * 2 >= arr[i]:
            ans += arr[i] >> 1
            kek -= arr[i] >> 1
            kek += arr[i] & 1
        else:
            ans += kek
            arr[i] -= kek << 1
            ans += arr[i] // 3
            arr[i] %= 3
            kek = arr[i]
    print(ans)
    return 0
main()

n = int(input())

A = list(map(int, input().split()))

num1 = 0

ans = 0

for a in A:
    d = a // 2
    deduct = min(num1, d)
    num1 -= deduct
    ans += deduct    
    a -= deduct * 2
    ans += a // 3
    
    num1 += a % 3
print(ans)
    
    


n = int(input())
c = list(map(int, input().split()))
ans = 0
osn = 0
for i in range(n):
    if c[i] <= osn * 2:
        ans += c[i] // 2
        osn += c[i] % 2 - c[i] // 2
    else:
        osn += c[i]
        ans += osn // 3
        osn %= 3
print(ans)

N = int(input())
A = [int(a) for a in input().split()]
s = sum(A)
r = 0
k = N

def nextk(f = 1):
    nonlocal k
    if f and k >= 0:
        A[k] = 0
    k -= 1
    while k >= 0 and A[k] != 1:
        k -= 1
    
def ct(n):
    if n % 2 == 0:
        return n//2
    return (n-3)//2

nextk(0)
for i in range(N)[::-1]:
    if k < 0:
        break
    
    j = 0
    if A[i] == 1:
        r += 1
        if k == i:
            nextk(0)
    else:
        j = ct(A[i])
        
        while j > 0:
            nextk()
            j -= 1
print((s-r)//3)


import sys

n = int(sys.stdin.readline().strip())
s = list(map(int, sys.stdin.readline().strip().split()))
t = 0
i = 0
j2 = 0
while i < n:
    x = s[i] // 3
    t = t + x
    s[i] = s[i] - 3 * x
    if s[i] != 0:
        j = max([i + 1, j2])
        while j < n:
            j2 = j
            if s[j] >= 2:
                t = t + 1
                s[j] = s[j] - 2
                s[i] = s[i] - 1
                if s[i] == 0:
                    j = n
            else:
                j = j + 1
    if s[i] != 0:
        i = n
    else:
        i = i + 1

print(t)


input()
cur = 0
res = 0
for a in map(int, input().split()):
    cur += a % 2
    a //= 2
    if a < cur:
        cur -= a
        res += a
    else:
        res += cur
        cur = (a - cur) * 2
        res += cur // 3
        cur %= 3
print(res)


n = int(input())
a = list(map(int, input().split()))

ans = 0
spare = 0

for i in range(n):
    can_build_2 = min(spare, a[i] // 2)
    ans += can_build_2
    spare -= can_build_2
    a[i] -= can_build_2 * 2

    ans += a[i] // 3
    spare += a[i] % 3

print(ans)


n = int(input())
size___nr = [int(x) for x in input().split()]

one_idxes = [i for i, x in enumerate(size___nr) if x == 1]

curr_max = n - 1
ans = 0
while True:
	if curr_max == 0 or len(one_idxes) == 0:
		break
	if size___nr[curr_max] == 0:
		curr_max -= 1
		continue
	if size___nr[curr_max] == 3:
		size___nr[curr_max] = 0
		ans += 1
		curr_max -= 1
		continue
	elif size___nr[curr_max] == 1:
		size___nr[curr_max] -= 1
		one_idxes.pop()
		continue
	elif len(one_idxes) > 0:
		x = one_idxes.pop()
		size___nr[curr_max] -= 2
		size___nr[x] -= 1
		ans += 1
		if size___nr[curr_max] == 1:
			size___nr[curr_max] -= 1
		continue

ans += sum(size___nr) // 3
print(ans)


n, ans, last = int(input()), 0, 0
a = list(map(int, input().split()))
for i in range(len(a)):
    while last > 0 and a[i] >= 2:
        ans += 1
        a[i] -= 2
        last -= 1
    ans += a[i] // 3
    last += a[i] % 3
print(ans)

N = int(input())
nums = [int(i) for i in input().split(' ')]


from collections import defaultdict


def helper(nums):
    tot, carry = 0, 0
    for i in range(len(nums)):
        v = min(nums[i]//2, carry)
        tot += v
        carry -= v
        nums[i] -= 2 * v

        tot += nums[i] // 3
        nums[i] %= 3

        carry += nums[i]
    print(tot)




helper(nums)

n=int(input())
l=list(map(int,input().split()))
ans,mod=0,0
for i in range(n):
         if(mod>0):
                  a=min(l[i]//2,mod)
                  ans+=a
                  mod-=a
                  l[i]-=2*a
         ans+=l[i]//3
         mod+=l[i]%3
print(ans)

n = int(input())
l = [*list(map(int ,input().split()))]
ans = 0
p = 0
for i in range(n - 1 , -1, -1):
     p += l[i] // 2
     if(l[i] % 2 and p > 0):
          ans += 1
          p -= 1
ans += 2 * p // 3
print(ans)


import math
import bisect
import heapq

from collections import defaultdict

def egcd(a, b):
    if a == 0:
        return (b, 0, 1)
    else:
        g, x, y = egcd(b % a, a)
        return (g, y - (b // a) * x, x)



def mulinv(b, n):
    g, x, _ = egcd(b, n)
    if g == 1:
        return x % n


primes = []

def isprime(n):
    for d in range(2, int(math.sqrt(n))+1):
        if n%d==0:
            return False
    return True


def argsort(ls):
    return sorted(range(len(ls)), key=ls.__getitem__)

def f(p=0):
    if p==1:
        return map(int, input().split())
    elif p==2:
        return list(map(int, input().split()))
    else:
        return int(input())



n = f()
cl = f(2)

count = 0
sm = 0

for i in range(n):
    a = sm
    b = cl[i]//2
    t = min(a, b)
    count+=t
    sm += cl[i]
    cl[i]-=t*2
    sm-=t*3
    c = cl[i] // 3
    sm-=c*3
    count+=c

print(count)

n,l,ans,p = int(input()),[*map(int ,input().split())],0,0
for i in range(n-1,-1,-1):
 p+=l[i]//2
 if(l[i]%2and p>0):ans+=1;p-=1
print(ans+2*p//3)

n,l,ans,p=int(input()),[*map(int,input().split())],0,0
for i in range(n-1,-1,-1):
 p+=l[i]//2
 if(l[i]%2)&(p>0):ans+=1;p-=1
print(ans+2*p//3)

n,l,ans,p=int(input()),[*map(int,input().split())][::-1],0,0
for i in range(n):
 p+=l[i]//2
 if(l[i]%2)&(p>0):ans+=1;p-=1
print(ans+2*p//3)

num=int(input())
ns=[*map(int,input().split())]
tri,edge=0,0
for n in ns:
    newTri=min(edge,n//2)
    tri+=newTri
    edge-=newTri
    n-=newTri*2
    tri+=n//3
    n=n%3
    edge+=n
print(tri)

x = int(input())
a = [int(i) for i in input().split()]
a.append(0)
ans = 0
sum12 = {1:0, 2:0}
for i in range(x+1):
	for j in range(1, 2):
		if i>1 and  sum12[j] != 0:
			t = a[i] // (3-j)
			t = min(sum12[j], t)
			a[i] -= t * (3-j)
			ans += t
			sum12[j] -= t

	ans += a[i] // 3
	a[i] %= 3
	if a[i]!=0:
		sum12[1] += a[i]

print(ans)


#8/4/19
#1119E
#BlueyNeilo

n=int(input())
a=list(map(int,input().split()))
r=list(map(lambda x: x%3, a))

memo = [0]*n
t = 0
for i in range(n):
    if i>0:
        if memo[i-1]*2<=a[i]:
            memo[i]=(a[i]-memo[i-1]*2)%3
        else:
            memo[i]=(memo[i-1] - a[i]//2) + (a[i]%2)
    else:
        memo[i]=r[i]
        
print((sum(a)-memo[n-1])//3)