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)$.

Source: codeforces | Difficulty: HARD
Tags: Sorting

Greedy algorithms

Complete search

Dynamic programming

Polynomials and generating functions


Solutions:
import sys

class Q:

	def __init__(self, ll, i):
		self.ll = ll
		self.i = i

class Main:

	def __init__(self):
		self.buff = None
		self.index = 0

	def next(self):
		if self.buff is None or self.index == len(self.buff):
			self.buff = sys.stdin.readline().split()
			self.index = 0
		val = self.buff[self.index]
		self.index += 1
		return val

	def next_int(self):
		return int(self.next())

	def solve(self):
		n = self.next_int()
		ss = 0
		ans = 0
		for _ in range(0, n):
			d = self.next_int()
			re = ss - 3 * ans
			ss += d
			if d >= re * 2:
				ans += re + (d - re * 2) // 3
			else:
				ans += d // 2
		print(ans)
Main().solve()


n = int(input())
l = [*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)


def li():
	return list(map(int, input().split(' ')))
input()
ans = lo = 0
for i in li():
	k = min(lo, i // 2)
	i -= 2 * k
	lo -= k
	ans += k
	ans += i // 3
	lo += i % 3
print(ans)


n = int(input())
l1 = list(map(int, input().split()))
i = 0
j = 0
ct2 = 0
while i < n:
	ct2 += l1[i] // 3
	l1[i] %= 3
	if l1[i] >= 1:
		l1[i] -= 1
		while j < n:
			if l1[j] >= 2:
				ct2 += 1
				l1[j] -= 2
				break
			else:
				j += 1
	else:
		i += 1
print(ct2)


from sys import stdin
n = int(stdin.readline().strip())
s = list(map(int, stdin.readline().strip().split()))
x = 0
ans = 0
for i in range(n):
	y = s[i] // 2
	y1 = min(y, x)
	s[i] -= y1 * 2
	x -= y1
	ans += y1
	y = s[i] // 3
	x += s[i] - y * 3
	s[i] -= y * 3
	ans += y
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())
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)


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())


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 = 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)


n = int(input())
a = list(map(int, input().split()))
curr = 0
two = 0
tri = 0
while curr < n:
	tri += a[curr] // 3
	a[curr] %= 3
	while two < n - 1 and (a[two] < 2 or two <= curr):
		two += 1
	if a[curr] == 1:
		if a[two] >= 2 and two > curr:
			a[curr] -= 1
			a[two] -= 2
			tri += 1
	elif a[curr] == 2:
		if a[two] >= 2 and two > curr:
			a[curr] -= 1
			a[two] -= 2
			tri += 1
			while two < n - 1 and (a[two] < 2 or two <= curr):
				two += 1
			if a[two] >= 2 and two > curr:
				a[curr] -= 1
				a[two] -= 2
				tri += 1
	curr += 1
print(tri)


import math
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
import sys
mod = 10 ** 9 + 7
mod1 = 998244353
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class TreeNode:

	def __init__(self, k, v):
		self.key = k
		self.value = v
		self.left = None
		self.right = None
		self.parent = None
		self.height = 1
		self.num_left = 1
		self.num_total = 1

class AvlTree:

	def __init__(self):
		self._tree = None

	def add(self, k, v):
		if not self._tree:
			self._tree = TreeNode(k, v)
			return
		node = self._add(k, v)
		if node:
			self._rebalance(node)

	def _add(self, k, v):
		node = self._tree
		while node:
			if k < node.key:
				if node.left:
					node = node.left
				else:
					node.left = TreeNode(k, v)
					node.left.parent = node
					return node.left
			elif node.key < k:
				if node.right:
					node = node.right
				else:
					node.right = TreeNode(k, v)
					node.right.parent = node
					return node.right
			else:
				node.value = v
				return

	@staticmethod
	def get_height(x):
		return x.height if x else 0

	@staticmethod
	def get_num_total(x):
		return x.num_total if x else 0

	def _rebalance(self, node):
		n = node
		while n:
			lh = self.get_height(n.left)
			rh = self.get_height(n.right)
			n.height = max(lh, rh) + 1
			balance_factor = lh - rh
			n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right)
			n.num_left = 1 + self.get_num_total(n.left)
			if balance_factor > 1:
				if self.get_height(n.left.left) < self.get_height(n.left.right):
					self._rotate_left(n.left)
				self._rotate_right(n)
			elif balance_factor < -1:
				if self.get_height(n.right.right) < self.get_height(n.right.left):
					self._rotate_right(n.right)
				self._rotate_left(n)
			else:
				n = n.parent

	def _remove_one(self, node):
		replacement = node.left or node.right
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = replacement
			else:
				node.parent.right = replacement
			replacement.parent = node.parent
			node.parent = None
		else:
			self._tree = replacement
			replacement.parent = None
		node.left = None
		node.right = None
		node.parent = None
		self._rebalance(replacement)

	def _remove_leaf(self, node):
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = None
			else:
				node.parent.right = None
			self._rebalance(node.parent)
		else:
			self._tree = None
		node.parent = None
		node.left = None
		node.right = None

	def remove(self, k):
		node = self._get_node(k)
		if not node:
			return
		if AvlTree._is_leaf(node):
			self._remove_leaf(node)
			return
		if node.left and node.right:
			nxt = AvlTree._get_next(node)
			node.key = nxt.key
			node.value = nxt.value
			if self._is_leaf(nxt):
				self._remove_leaf(nxt)
			else:
				self._remove_one(nxt)
			self._rebalance(node)
		else:
			self._remove_one(node)

	def get(self, k):
		node = self._get_node(k)
		return node.value if node else -1

	def _get_node(self, k):
		if not self._tree:
			return None
		node = self._tree
		while node:
			if k < node.key:
				node = node.left
			elif node.key < k:
				node = node.right
			else:
				return node
		return None

	def get_at(self, pos):
		x = pos + 1
		node = self._tree
		while node:
			if x < node.num_left:
				node = node.left
			elif node.num_left < x:
				x -= node.num_left
				node = node.right
			else:
				return (node.key, node.value)
		raise IndexError('Out of ranges')

	@staticmethod
	def _is_left(node):
		return node.parent.left and node.parent.left == node

	@staticmethod
	def _is_leaf(node):
		return node.left is None and node.right is None

	def _rotate_right(self, node):
		if not node.parent:
			self._tree = node.left
			node.left.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.left
			node.left.parent = node.parent
		else:
			node.parent.right = node.left
			node.left.parent = node.parent
		bk = node.left.right
		node.left.right = node
		node.parent = node.left
		node.left = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	def _rotate_left(self, node):
		if not node.parent:
			self._tree = node.right
			node.right.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.right
			node.right.parent = node.parent
		else:
			node.parent.right = node.right
			node.right.parent = node.parent
		bk = node.right.left
		node.right.left = node
		node.parent = node.right
		node.right = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	@staticmethod
	def _get_next(node):
		if not node.right:
			return node.parent
		n = node.right
		while n.left:
			n = n.left
		return n

class SegmentTree1:

	def __init__(self, data, default=0, func=lambda a, b: max(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class SegmentTree:

	def __init__(self, data, default=0, func=lambda a, b: a + b):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD

def powm(a, n, m):
	if a == 1 or n == 0:
		return 1
	if n % 2 == 0:
		s = powm(a, n // 2, m)
		return s * s % m
	else:
		return a * powm(a, n - 1, m) % m

def sort_list(list1, list2):
	zipped_pairs = zip(list2, list1)
	z = [x for (_, x) in sorted(zipped_pairs)]
	return z

def product(l):
	por = 1
	for i in range(len(l)):
		por *= l[i]
	return por

def binarySearchCount(arr, n, key):
	left = 0
	right = n - 1
	count = 0
	while left <= right:
		mid = int((right + left) / 2)
		if arr[mid] < key:
			count = mid + 1
			left = mid + 1
		else:
			right = mid - 1
	return count

def countdig(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def binary(x, length):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y

def countGreater(arr, n, k):
	l = 0
	r = n - 1
	leftGreater = n
	while l <= r:
		m = int(l + (r - l) / 2)
		if arr[m] >= k:
			leftGreater = m
			r = m - 1
		else:
			l = m + 1
	return n - leftGreater
n = int(input())
l = list(map(int, input().split()))
cur = 0
ans = 0
for i in range(n):
	ans += min(cur, l[i] // 2)
	t = min(cur, l[i] // 2)
	cur -= t
	l[i] -= 2 * t
	ans += l[i] // 3
	l[i] = l[i] % 3
	cur += l[i]
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)


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)


n = int(input())
l = list(map(int, input().split()))
ost = 0
res = 0
for x in l:
	ost += x % 2
	x //= 2
	if x < ost:
		res += x
		ost -= x
	else:
		res += ost + (x - ost) * 2 // 3
		ost = 2 * (x - ost) % 3
print(res)


n = int(input())
a = [int(i) for i in input().split()]
count = 0
pairs = 0
for i in range(len(a) - 1, -1, -1):
	pairs += a[i] // 2
	if a[i] % 2 == 1 and pairs > 0:
		count += 1
		pairs -= 1
count += pairs * 2 // 3
print(count)


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)


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())
A = [int(a) for a in input().split()]
s = sum(A)
r = 0
k = N

def nextk(f=1):
	global 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 os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
import collections
from itertools import permutations
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
n = int(input())
a = list(map(int, input().split()))
count = 0
un = 0
two = 0
for i in range(n):
	two = a[i] // 2
	d = min(two, un)
	un -= d
	a[i] -= 2 * d
	count += d + a[i] // 3
	un += a[i] % 3
print(count)


from collections import Counter
n = int(input())
d = [int(x) for x in input().split()]
c = 0
ans = 0
whats_left = 0
for i in range(n):
	freq = d[i] // 2
	sub = min(freq, whats_left)
	ans += sub
	whats_left -= sub
	d[i] -= sub * 2
	ans += d[i] // 3
	whats_left += d[i] % 3
print(ans)


import sys
input = sys.stdin.readline
from heapq import *
n = int(input())
a = list(map(int, input().split()))
h = []
triangles = 0
remainder = 0
for x in a[::-1]:
	if x == 3:
		triangles += 1
	elif x == 1:
		try:
			y = -heappop(h)
			triangles += 1
			remainder -= 2
			y -= 2
			if y > 1 and y != 3:
				heappush(h, -y)
		except:
			pass
	else:
		heappush(h, -x)
		remainder += x
print(triangles + remainder // 3)


def main():
	print = out.append
	n = get_int()
	li = get_list()
	pairs = ans = 0
	for i in reversed(li):
		pairs += i // 2
		if i % 2 != 0 and pairs > 0:
			pairs -= 1
			ans += 1
	ans += 2 * (pairs // 3)
	ans += 1 if pairs % 3 == 2 else 0
	print(ans)
import sys
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
out = []
get_int = lambda : int(input())
get_list = lambda : list(map(int, input().split()))
main()
print(*out, sep='\n')


n = int(input())
ans = 0
kol = 0
for c in list(map(int, input().split())):
	x = c // 2
	if x <= kol:
		ans += x
		kol -= x
		if c % 2 == 1:
			kol += 1
	else:
		ans += kol
		ans += (c - kol * 2) // 3
		kol = (c - kol * 2) % 3
print(ans)


n = int(input())
A = list(map(int, input().split()))
ans = 0
cnt = 0
A.reverse()
for x in A:
	cnt += x // 2
	if x & 1:
		if cnt:
			ans += 1
			cnt -= 1
f = 2 * (cnt // 3)
if cnt % 3 == 2:
	f += 1
ans += f
print(ans)


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])


import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
from collections import defaultdict
n = int(input())
a = [int(i) for i in input().split()]
pairs = 0
singles = 0
ans = 0
for i in a:
	new = min(i // 2, singles)
	ans += new
	i -= 2 * new
	singles -= new
	ans += i // 3
	singles += i % 3
print(ans)


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
frnxt = 0
for i in l:
	rem = min(frnxt, i // 2)
	frnxt -= rem
	ans += rem
	i -= 2 * rem
	ans += i // 3
	i %= 3
	frnxt += i
print(ans)


n = int(input())
a = list(map(int, input().split()))
u_d = [0] * n
u_r = [0] * n
d = [0] * (n + 1)
for i in range(n):
	u_d[i] = a[i] // 2
	u_r[i] = a[i] % 2
count = 0
for i in range(n):
	if u_d[i] >= d[i]:
		count += d[i]
		u_d[i] -= d[i]
		count += (2 * u_d[i] + u_r[i]) // 3
		d[i + 1] = (2 * u_d[i] + u_r[i]) % 3
	else:
		count += u_d[i]
		d[i] -= u_d[i]
		d[i + 1] = d[i] + u_r[i]
print(count)


import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
r = 0
ans = 0
for i in range(n):
	x = a[i] // 2
	if x > 0:
		if x >= r:
			ans += r
			a[i] -= 2 * r
			r = 0
		else:
			ans += x
			a[i] -= 2 * x
			r -= x
	z = a[i] // 3
	ans += z
	a[i] -= z * 3
	r += a[i]
print(ans)


n = int(input())
d = [int(x) for x in input().split()]
c = 0
ans = 0
x = 0
for i in range(n):
	f = d[i] // 2
	s = min(f, x)
	ans += s
	x -= s
	d[i] -= s * 2
	ans += d[i] // 3
	x += d[i] % 3
print(ans)


n = int(input())
length = [int(g) for g in input().split()]
counter = 0
answer = 0
for i in range(n - 1, -1, -1):
	counter += length[i] // 2
	if length[i] % 2 == 1 and counter > 0:
		answer += 1
		counter -= 1
answer += counter * 2 // 3
print(answer)


from collections import defaultdict as dd
import math

def nn():
	return int(input())

def li():
	return list(input())

def mi():
	return map(int, input().split())

def lm():
	return list(map(int, input().split()))
n = nn()
tris = lm()
answer = 0
extra = 0
for i in range(n):
	answer += tris[i] // 3
	extra += tris[i] % 3
	if i < n - 1:
		while extra >= 1 and tris[i + 1] >= 2:
			answer += 1
			extra -= 1
			tris[i + 1] -= 2
print(answer)


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()))
ans = prv = 0
for x in a:
	match = min(prv, x // 2)
	ans += match
	prv -= match
	x -= match * 2
	ans += x // 3
	prv += x % 3
print(ans)


def ria():
	return [int(i) for i in input().split()]
n = ria()[0]
ar = ria()
trios = 0
trash = 0
for i in ar:
	cr = i
	dttrash = min(cr // 2, trash)
	cr -= dttrash * 2
	trash -= dttrash
	trios += dttrash
	trios += cr // 3
	trash += cr % 3
print(trios)


def main():
	print = out.append
	n = get_int()
	li = get_list()
	pairs = ans = 0
	for i in reversed(li):
		pairs += i // 2
		if i % 2 != 0 and pairs > 0:
			pairs -= 1
			ans += 1
	ans += 2 * (pairs // 3)
	ans += 1 if pairs % 3 == 2 else 0
	print(ans)
import sys
out = []
get_int = lambda : int(input())
get_list = lambda : list(map(int, input().split()))
main()
print(*out, sep='\n')


n = int(input())
a = list(map(int, input().split()))
c = 0
ans = 0
for x in a:
	d = x // 2
	add = min(d, c)
	c -= add
	ans += add
	x -= add * 2
	ans += x // 3
	c += x % 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)


rest = 0
ans = 0
n = int(input())
a = list(map(int, input().split()))
for i in range(n):
	k = rest
	p = a[i] // 3
	c = a[i] % 3
	for j in range(1, rest + 1):
		while c < j * 2:
			c += 3
			p -= 1
			if p < 0:
				break
		if p < 0:
			p = 0
			c -= 3
			break
		k = min(rest - j + (c - j * 2) % 3, k)
	ans += p + (rest - k) + (c - (rest - k) * 2) // 3
	rest = k + (c - (rest - k) * 2) % 3
print(ans)


import sys
import math
from collections import defaultdict, deque
import heapq
mod = 998244353
n = int(sys.stdin.readline())
arr = list(map(int, sys.stdin.readline().split()))
ans = 0
rem = 0
for i in range(n):
	num = arr[i]
	x = min(arr[i] // 2, rem)
	y = arr[i] - 2 * x
	ans += y // 3
	ans += x
	rem1 = rem - x
	rem = rem1 + y % 3
print(ans)


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


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


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())
a = list(map(int, input().split()))
(l, r, c) = (0, 0, 0)
while r < n:
	if l < r and a[r] > 1:
		x = min(a[l], a[r] // 2)
		c += x
		a[l] -= x
		a[r] -= 2 * x
	else:
		x = a[r] // 3
		c += x
		a[r] -= 3 * x
		r += 1
	while r < n and a[r] == 0:
		r += 1
	while l < r and a[l] == 0:
		l += 1
print(c)


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)


n = int(input())
l = [0] + list(map(int, input().split()))
cnt = 0
trash = 0
for i in range(1, n + 1):
	if not trash:
		cnt += l[i] // 3
		trash += l[i] % 3
	else:
		trash += l[i] % 2
		l[i] -= l[i] % 2
		use = min(l[i], trash * 2)
		cnt += use // 2
		trash -= use // 2
		l[i] -= use
		cnt += l[i] // 3
		trash += l[i] % 3
print(cnt)


n = int(input())
ans = 0
y = 0
a = []
cnt = 0
str_in = input()
a = list(map(int, str_in.strip().split()))
for i in range(len(a)):
	t = min(int(a[i] / 2), y)
	ans += t
	a[i] -= int(t * 2)
	y -= t
	ans += a[i] // 3
	y += a[i] % 3
print(int(ans))


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 = [int(x) for x in input().split()]
ans = 0
cnt = 0
for x in a:
	cur = x // 2
	if cur <= cnt:
		ans += cur
		cnt -= cur
		if x % 2 == 1:
			cnt += 1
	else:
		ans += cnt
		ans += (x - 2 * cnt) // 3
		cnt = (x - 2 * cnt) % 3
print(ans)