William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $c_1, c_2, \dots, c_n$ where $c_i$ is the number of consecutive brackets "(" if $i$ is an odd number or the number of consecutive brackets ")" if $i$ is an even number.

For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $[3, 2, 1, 3]$.

You need to find the total number of continuous subsequences (subsegments) $[l, r]$ ($l \le r$) of the original bracket sequence, which are regular bracket sequences.

A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not.


-----Input-----

The first line contains a single integer $n$ $(1 \le n \le 1000)$, the size of the compressed sequence.

The second line contains a sequence of integers $c_1, c_2, \dots, c_n$ $(1 \le c_i \le 10^9)$, the compressed sequence.


-----Output-----

Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences.

It can be proved that the answer fits in the signed 64-bit integer data type.


-----Examples-----

Input
5
4 1 2 3 1
Output
5
Input
6
1 3 2 1 2 4
Output
6
Input
6
1 1 1 1 2 2
Output
7


-----Note-----

In the first example a sequence (((()(()))( is described. This bracket sequence contains $5$ subsegments which form regular bracket sequences:

Subsequence from the $3$rd to $10$th character: (()(()))

Subsequence from the $4$th to $5$th character: ()

Subsequence from the $4$th to $9$th character: ()(())

Subsequence from the $6$th to $9$th character: (())

Subsequence from the $7$th to $8$th character: ()

In the second example a sequence ()))(()(()))) is described.

In the third example a sequence ()()(()) is described.

Difficulty: MEDIUM_HARD
Tags: Implementation

Complete search
Skill types: Complete search

Starter code:

Solutions:
from os import path
from io import BytesIO, IOBase
import sys
from heapq import heappush, heappop
from functools import cmp_to_key as ctk
from collections import deque, Counter, defaultdict as dd
from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right
from itertools import permutations
from datetime import datetime
from math import ceil, sqrt, log, gcd

def ii():
	return int(input())

def si():
	return input().rstrip()

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

def li():
	return list(mi())
abc = 'abcdefghijklmnopqrstuvwxyz'
mod = 1000000007
inf = float('inf')
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def bo(i):
	return ord(i) - ord('0')
file = 1

def ceil(a, b):
	return (a + b - 1) // b

def solve():
	n = ii()
	a = li()
	ans = 0
	for i in range(0, n, 2):
		c = 0
		open_needed = 0
		for j in range(i + 1, n):
			if j % 2 == 0:
				c += a[j]
			else:
				rem_op = a[i] - open_needed
				rem_cls = a[j] - c
				ans += max(0, min(min(a[j], rem_cls + 1), min(a[i], rem_op + 1)))
				c -= a[j]
				if c < 0:
					open_needed += abs(c)
					c = 0
	print(ans)
if file:
	if path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	else:
		input = sys.stdin.readline
solve()


from sys import stdin, stdout
import heapq
from collections import defaultdict
import math
import bisect
import io, os

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p

def main():
	n = int(stdin.readline())
	arr = list(map(int, stdin.readline().split()))
	ans = 0
	for i in range(0, n, 2):
		k = kn = 0
		for j in range(i + 1, n, 2):
			ans += max(0, min(arr[i], arr[j] - k) - max(0, -kn - 1))
			if j + 1 >= n:
				continue
			k -= arr[j]
			kn = min(k, kn)
			k += arr[j + 1]
	print(ans)
main()


import sys
import math
import heapq
import bisect
from collections import Counter
from collections import defaultdict
from io import BytesIO, IOBase
from itertools import permutations
import string

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		import os
		self.os = os
		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
		self.BUFSIZE = 8192

	def read(self):
		while True:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, self.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 = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, self.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:
			self.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')

def get_int():
	return int(input())

def get_ints():
	return list(map(int, input().split(' ')))

def get_int_grid(n):
	return [get_ints() for _ in range(n)]

def get_str():
	return input().strip()

def get_strs():
	return get_str().split(' ')

def flat_list(arr):
	return [item for subarr in arr for item in subarr]

def yes_no(b):
	if b:
		return 'YES'
	else:
		return 'NO'

def binary_search(good, left, right, delta=1, right_true=False):
	limits = [left, right]
	while limits[1] - limits[0] > delta:
		if delta == 1:
			mid = sum(limits) // 2
		else:
			mid = sum(limits) / 2
		if good(mid):
			limits[int(right_true)] = mid
		else:
			limits[int(~right_true)] = mid
	if good(limits[int(right_true)]):
		return limits[int(right_true)]
	else:
		return False

def prefix_sums(a):
	p = [0]
	for x in a:
		p.append(p[-1] + x)
	return p

def solve_a():
	(c, d) = get_ints()
	if c == d == 0:
		return 0
	elif c == d:
		return 1
	elif abs(c - d) % 2 == 1:
		return -1
	else:
		return 2

def solve_b():
	n = get_int()
	a = get_ints()
	(evens, odds) = ([], [])
	for i in range(n):
		if a[i] % 2 == 0:
			evens.append(i)
		else:
			odds.append(i)
	if abs(len(evens) - len(odds)) > 1:
		return -1
	M = 0
	if len(odds) > len(evens):
		(odds, evens) = (evens, odds)
	if len(evens) != len(odds):
		M = 0
		x = list(range(0, n, 2))
		for i in range(len(evens)):
			M += abs(evens[i] - x[i])
		return M
	if len(evens) == len(odds):
		x = list(range(0, n, 2))
		m1 = 0
		for i in range(len(evens)):
			m1 += abs(evens[i] - x[i])
		x = list(range(1, n, 2))
		m2 = 0
		for i in range(len(evens)):
			m2 += abs(evens[i] - x[i])
		return min(m1, m2)

def solve_c():
	n = get_int()
	a = get_ints()
	cnt = 0
	for i in range(0, n, 2):
		z = a[i]
		b = 0
		for j in range(i + 1, n):
			if j % 2 == 0:
				b += a[j]
			else:
				b -= a[j]
				if b <= 0 and j - i > 1:
					cnt += 1
				if b < 0:
					if z + b >= 0:
						cnt += -b
						z += b
						b = 0
					else:
						cnt += z
						break
	return cnt
print(solve_c())


n = int(input())
arr = list(map(int, input().split()))
count = 0
ans = 0
for i in range(n):
	count = 0
	mini = 0
	if i % 2 == 0:
		for j in range(i + 1, n, 1):
			if j % 2 == 0:
				count += arr[j]
				mini = min(mini, count)
			else:
				l_cont = arr[i]
				r_cont = abs(arr[j])
				c_count = count
				if l_cont >= abs(mini):
					l_cont += mini
					c_count -= mini
					if r_cont >= c_count:
						r_cont -= c_count
						ans = ans + 0 if i + 1 == j else ans + 1
						ans += min(r_cont, l_cont)
				count -= arr[j]
				mini = min(mini, count)
print(ans)


from __future__ import division, print_function
import math
import sys
import os
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')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def inp():
	return int(input())

def inps():
	return input().strip()

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input().strip()
	return list(s[:len(s)])

def invr():
	return map(int, input().split())
n = inp()
l = inlt()
n -= n % 2
if n == 1:
	print(0)
else:
	cnt = 0
	carry = 0
	pref = [l[0]]
	for i in range(0, n, 2):
		cnt += min(l[i], l[i + 1])
		v = l[i + 1]
		cur = min(0, l[i] - l[i + 1])
		for j in range(i + 2, n, 2):
			if cur < 0:
				break
			cur += l[j]
			if l[j + 1] >= cur:
				cnt += 1 + min(l[j + 1] - cur, l[i] - v)
			cur -= l[j + 1]
			if cur < 0:
				temp = min(abs(cur), l[i] - v)
				v += temp
				cur += temp
	print(cnt)


import sys
input = sys.stdin.readline

def main():
	t = 1

	class RangeQuery:

		def __init__(self, data, func=min):
			self.func = func
			self._data = _data = [list(data)]
			(i, n) = (1, len(_data[0]))
			while 2 * i <= n:
				prev = _data[-1]
				_data.append([func(prev[j], prev[j + i]) for j in range(n - 2 * i + 1)])
				i <<= 1

		def query(self, start, stop):
			depth = (stop - start).bit_length() - 1
			return self.func(self._data[depth][start], self._data[depth][stop - (1 << depth)])

		def __getitem__(self, idx):
			return self._data[0][idx]
	for _ in range(t):
		n = int(input())
		ar = list(map(int, input().split()))
		signs = [0]
		for (i, x) in enumerate(ar):
			if i % 2:
				signs.append(signs[-1] - x)
			else:
				signs.append(signs[-1] + x)
		rq = RangeQuery(signs, min)
		ans = 0
		for l in range(0, n, 2):
			for r in range(l + 1, n, 2):
				left = ar[l]
				right = ar[r]
				if r - l == 1:
					ans += min(left, right)
					continue
				stuff = signs[r] - signs[l + 1]
				minleft = rq.query(l + 2, r + 1) - signs[l + 1]
				minleft = max(-minleft, 1)
				equivmin = stuff + minleft
				ans += max(min(left - minleft + 1, right - equivmin + 1), 0)
		print(ans)
main()


n = int(input())
a = list(map(int, input().split()))
stack = []
ans = 0
for (i, c) in enumerate(a):
	if i % 2:
		while c and stack:
			if stack[-1] < 0:
				stack.pop()
			else:
				m = min(stack[-1], c)
				c -= m
				ans += m
				if stack[-1] == m:
					stack.pop()
				else:
					stack[-1] -= m
				if stack and stack[-1] < 0:
					ans -= stack[-1]
					stack[-1] -= 1
				else:
					stack.append(-1)
		if c:
			stack.clear()
	elif stack and stack[-1] > 0:
		stack[-1] += c
	else:
		stack.append(c)
print(ans)


from sys import stdout, stdin
input = stdin.readline

def output(answer):
	stdout.write('{}\n'.format(answer))

def transfer(stack, other, flag):
	if flag == 0:
		while len(other) > 0:
			stack.append(other.pop())
	else:
		while len(other) > 0:
			v = other.pop()
			if v > 0:
				stack.append(v)
for _ in range(1):
	n = int(input())
	arr = list(map(int, input().split()))
	stack = [None]
	count = 0
	for index in range(n):
		if index % 2 == 0:
			stack.append(arr[index])
			continue
		curr = arr[index]
		other = []
		flag = 0
		while stack[-1] != None and curr != 0 and (stack[-1] <= curr):
			value = stack.pop()
			if value < 0:
				count += 1
				other.append(value)
			elif value > curr:
				count += curr
				stack.append(value - curr)
				stack.append(-1)
				curr = 0
				break
			elif value <= curr:
				if value == curr:
					count += curr
					curr = 0
					itr = len(stack) - 1
					while itr > 0 and stack[itr] != None and (stack[itr] < 0):
						count += 1
						itr -= 1
					stack.append(-1)
				else:
					count += value
					other.append(-1)
					curr -= value
					flag = 1
		if stack[-1] == None:
			transfer(stack, other, flag)
			if curr > 0:
				stack.append(None)
		elif curr == 0:
			transfer(stack, other, flag)
		elif curr != 0:
			value = stack.pop()
			stack.append(value - curr)
			stack.append(-1)
			count += curr
			transfer(stack, other, 1)
	output(count)


n = int(input())
arr = list(map(int, input().split()))
ans = 0
for i in range(0, n, 2):
	st = [0, arr[i] - 1]
	curr = 0
	for j in range(i + 1, n, 2):
		en = [curr + arr[j - 1] - arr[j], curr + arr[j - 1] - 1]
		curr += arr[j - 1] - arr[j]
		r1 = max(en[0], st[0])
		r2 = min(en[1], st[1])
		ans += max(r2 - r1 + 1, 0)
		if en[0] < 0:
			break
		st[1] = min(st[1], en[0])
print(ans)


INF = 10 ** 18
import sys
import pprint

def input():
	return sys.stdin.readline().rstrip('\r\n')

def maps():
	return [int(i) for i in input().split()]
import logging
from logging import getLogger
logging.basicConfig(format='%(message)s', level=logging.WARNING)
logger = getLogger(__name__)
logger.setLevel(logging.INFO)

def debug(msg, *args):
	logger.info(f'{msg}={pprint.pformat(args)}')

def solve1():
	(n,) = maps()
	a = [*maps()]
	ans = 0
	for i in range(0, n, 2):
		(bal, minbal) = (0, 0)
		for j in range(i + 1, n):
			if j % 2:
				left = -minbal
				right = bal - minbal
				left = max(1, left)
				right = max(1, right)
				if left <= a[i] and right <= a[j]:
					ans += min(a[i] - left, a[j] - right) + 1
			if j % 2 == 0:
				bal += a[j]
			else:
				bal -= a[j]
			minbal = min(minbal, bal)
	print(ans)

def solve2():
	(n,) = maps()
	a = [*maps()]
	ans = 0
	st = []
	for i in range(n):
		if i % 2 == 0:
			if not st or st[-1][0] != '(':
				st.append(['(', a[i]])
			else:
				st[-1][1] += a[i]
		else:
			while st:
				if st and st[-1][0] == '(':
					if st[-1][1] <= a[i]:
						a[i] -= st[-1][1]
						st[-1][0] = '()'
					else:
						st[-1][1] -= a[i]
						st.append(['()', a[i]])
						a[i] = 0
				if a[i] == 0:
					break
				acc = 0
				while st and st[-1][0] == '()':
					ans += st[-1][1] + acc
					acc += 1
					st.pop()
	acc = 0
	while st:
		if st[-1][0] == '()':
			ans += st[-1][1] + acc
			acc += 1
		else:
			acc = 0
		st.pop()
	print(ans)
solve2()


n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(0, n, 2):
	balance = 0
	curr = 1
	cnt = 0
	for j in range(i + 1, n):
		if j % 2 == 0:
			balance -= a[j]
			continue
		to_add = min(a[i], a[j] + balance) - max(balance, curr) + 1
		cnt += max(0, to_add)
		balance += a[j]
		curr = max(curr, balance)
	ans += cnt
print(ans)


debug = False
go_next = True
while go_next:
	length = int(input())
	values = list(map(int, input().split()))
	if debug:
		print(length, values)
	result = 0
	for s_ix in range(0, length, 2):
		slackable = values[s_ix]
		unclosed = 0
		for i in range(s_ix, length):
			if i % 2 == 0:
				unclosed += values[i]
			else:
				unclosed -= values[i]
				if unclosed <= slackable:
					if debug:
						print(s_ix, i, unclosed, slackable, end=' ')
					result += slackable - max(unclosed, 0)
					slackable = min(unclosed + 1, slackable)
					if debug:
						print(slackable, result)
				if unclosed < 0:
					break
	print(result)
	go_next = debug


import sys
input = sys.stdin.readline
n = int(input())
c = [0] + list(map(int, input().split()))
(c1, c2) = ([0], [0])
for i in range(1, n + 1):
	ci = c[i]
	if i % 2:
		c1.append(ci + c1[-1])
		c2.append(c2[-1])
	else:
		c2.append(ci + c2[-1])
		c1.append(c1[-1])
dp = [[0] * (n + 1) for _ in range(n + 1)]
ans = 0
for x in range(1, n + 1, 2):
	for i in range(1, n + 1, 2):
		ci = c[i]
		if i + x > n:
			break
		j = i + x
		cj = c[j]
		if i == j - 1:
			m = min(ci, cj)
			ans += m
			dp[i][j] = 2 * m
		else:
			dp0 = dp[i + 2][j - 2]
			cnt1 = c1[j - 1] - c1[i] - dp0 // 2
			cnt2 = c2[j - 1] - c2[i] - dp0 // 2
			if cnt1 > cj or cnt2 > ci:
				m = 0
				dp[i][j] = dp0 + 2 * (min(cnt1, cj) + min(cnt2, ci))
			else:
				m = min(ci - cnt2, cj - cnt1) + 1
				ans += m
				dp[i][j] = dp0 + 2 * (m + min(cnt1, cj) + min(cnt2, ci) - 1)
print(ans)


n = int(input())
u = list(map(int, input().split()))
ans = -(n // 2)
for i in range(0, n, 2):
	x = u[i]
	p = 0
	for j in range(i + 1, n):
		if j % 2:
			if p > u[j]:
				p -= u[j]
			elif x + p < u[j]:
				ans += x + 1
				break
			else:
				ans += u[j] - p + 1
				x += p - u[j]
				p = 0
		else:
			p += u[j]
print(ans)


import heapq, math
from collections import defaultdict, deque
from os import getcwd
from itertools import permutations
import sys, os.path
inf = 10 ** 10
if os.path.exists('C:/Users/Dhanush/Desktop/cp/input.txt'):
	sys.stdout = open('C:/Users/Dhanush/Desktop/cp/output.txt', 'w')
	sys.stdin = open('C:/Users/Dhanush/Desktop/cp/input.txt', 'r')
input = sys.stdin.readline
n = int(input())
l = list(map(int, input().split()))
nl = []
for i in range(n):
	if i % 2 == 0:
		nl.append(l[i])
	else:
		nl.append(-l[i])
openb = []
closeb = []
(c1, c2) = (0, 0)
for i in range(n):
	if i % 2 == 0:
		c1 += l[i]
	else:
		c2 += l[i]
	openb.append(c1)
	closeb.append(c2)
mini = [[inf for _ in range(n)] for _ in range(n)]
for i in range(n):
	mini[i][i] = nl[i]
	cur = nl[i]
	for j in range(i + 1, n):
		cur += nl[j]
		mini[i][j] = min(mini[i][j - 1], cur)
dp = [[0 for _ in range(n + 2)] for _ in range(n + 2)]
for gap in range(1, n):
	for i in range(n):
		j = i + gap
		if j >= n:
			continue
		if gap == 1:
			if i % 2 == 0:
				dp[i][j] = min(l[i], l[j])
		else:
			dp[i][j] = dp[i + 1][j] + dp[i][j - 1] - dp[i + 1][j - 1]
			if i % 2 == 1 or j % 2 == 0:
				continue
			addleft = abs(mini[i + 1][j - 1])
			copen = openb[j - 1]
			cclose = closeb[j - 1]
			copen -= openb[i]
			cclose -= closeb[i]
			if addleft > l[i]:
				continue
			copen += addleft
			need = copen - cclose
			if copen <= cclose or l[j] < need:
				continue
			dp[i][j] += 1
			c1 = l[i] - addleft
			c2 = l[j] - need
			dp[i][j] += min(c1, c2)
print(dp[0][n - 1])


import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int, input().split()))
Ans = -(N // 2)
for i in range(0, N, 2):
	Start = A[i]
	Plus = 0
	for j in range(i + 1, N):
		if j % 2:
			if Plus > A[j]:
				Plus -= A[j]
			elif Start + Plus < A[j]:
				Ans += Start + 1
				break
			else:
				Ans += A[j] - Plus + 1
				Start += Plus - A[j]
				Plus = 0
		else:
			Plus += A[j]
print(Ans)


n = int(input())
a = list(map(int, input().split()))
if n % 2 == 1:
	n = n - 1
ans = 0
for i in range(0, n, 2):
	pre = qwq = a[i] - a[i + 1]
	ans += min(a[i], a[i + 1])
	for j in range(i + 2, n, 2):
		pre += a[j] - a[j + 1]
		if pre >= 0:
			awa = a[i] - pre
			ans += max(min(qwq - pre, min(awa, a[j + 1])), -1) + 1
		else:
			awa = a[j + 1] + pre
			ans += max(min(qwq, min(a[i], awa)), -1) + 1
		qwq = min(qwq, pre)
		if qwq <= -1:
			break
print(ans)


def main():
	res = 0
	n = int(input())
	a = list(map(int, input().split()))
	a = [0] + a
	for i in range(1, n, 2):
		ans = 0
		_ans = 0
		for j in range(i + 1, n + 1, 2):
			if a[i] < -_ans:
				break
			l = max(0, ans - _ans)
			r = min(a[j], ans + a[i])
			res += max(0, r - l + 1)
			_ans = min(_ans, ans - a[j])
			if j + 1 < n + 1:
				ans += a[j + 1] - a[j]
	print(res - n // 2)
main()


n = int(input())
c = list(map(int, input().split()))
ans = 0
for i in range(n):
	if i % 2:
		continue
	min_depth = 0
	bal = 0
	for j in range(i + 1, n):
		if (j - i) % 2:
			ans += max(0, min(c[i], c[j] - bal) - max(-min_depth, 1 - bal) + 1)
			bal -= c[j]
			min_depth = min(min_depth, bal)
		else:
			bal += c[j]
print(ans)


from sys import stdin, stdout
from collections import defaultdict
import math

def get_list():
	return list(map(int, stdin.readline().strip().split()))

def get_int():
	return int(stdin.readline())

def get_ints():
	return map(int, stdin.readline().strip().split())

def get_string():
	return stdin.readline()

def printn(n):
	stdout.write(str(n) + '\n')

def solve():
	n = get_int()
	a = get_list()
	tot = 0
	cur = 0
	stk = [[0, 0]]
	for i in range(0, n - 1, 2):
		x = a[i]
		y = a[i + 1]
		cur += x - y
		tot += min(y, cur + y - stk[0][0])
		while stk and stk[-1][0] > cur:
			tot += stk[-1][1]
			stk.pop(-1)
		if stk and stk[-1][0] == cur:
			tot += stk[-1][1]
			stk[-1][1] += 1
		elif stk:
			stk.append([cur, 1])
		else:
			stk.append([cur, 0])
	printn(tot)
	return
t = 1
while t:
	t -= 1
	solve()


class SegTree:

	def __init__(self, init_val, ide_ele, segfunc):
		self.n = len(init_val)
		self.num = 2 ** (self.n - 1).bit_length()
		self.ide_ele = ide_ele
		self.segfunc = segfunc
		self.seg = [ide_ele] * 2 * self.num
		for i in range(self.n):
			self.seg[i + self.num] = init_val[i]
		for i in range(self.num - 1, 0, -1):
			self.seg[i] = self.segfunc(self.seg[2 * i], self.seg[2 * i + 1])

	def update(self, k, x):
		k += self.num
		self.seg[k] = x
		while k:
			k = k >> 1
			self.seg[k] = self.segfunc(self.seg[2 * k], self.seg[2 * k + 1])

	def query(self, l, r):
		if r <= l:
			return self.ide_ele
		l += self.num
		r += self.num
		lres = self.ide_ele
		rres = self.ide_ele
		while l < r:
			if r & 1:
				r -= 1
				rres = self.segfunc(self.seg[r], rres)
			if l & 1:
				lres = self.segfunc(lres, self.seg[l])
				l += 1
			l = l >> 1
			r = r >> 1
		res = self.segfunc(lres, rres)
		return res

	def __str__(self):
		arr = [self.query(i, i + 1) for i in range(self.n)]
		return str(arr)

def segfuncm(x, y):
	if x <= y:
		return x
	else:
		return y

def segfuncM(x, y):
	if x >= y:
		return x
	else:
		return y
INF = 1 << 60

def main():
	n = int(input())
	C = list(map(int, input().split()))
	CS = [0] * (n + 1)
	for i in range(n):
		if i % 2 == 0:
			CS[i + 1] = CS[i] + C[i]
		else:
			CS[i + 1] = CS[i] - C[i]
	segm = SegTree(CS, INF, segfuncm)
	segM = SegTree(CS, -INF, segfuncM)
	ans = 0
	for l in range(n):
		if l % 2 == 1:
			continue
		for r in range(l, n):
			if r % 2 == 0:
				continue
			x = CS[r] - CS[l + 1]
			a = C[l]
			b = C[r]
			m = segm.query(l + 2, r + 1) - CS[l + 1]
			M = segM.query(l + 2, r + 1) - CS[l + 1]
			p = max(1, 1 - x, -m)
			q = min(a, b - x)
			if q < p:
				continue
			else:
				ans += q - p + 1
	print(ans)
main()


import sys
from sys import stdin
n = int(stdin.readline())
c = list(map(int, stdin.readline().split()))
h = [0]
for i in range(n):
	if i % 2 == 0:
		h.append(h[-1] + c[i])
	else:
		h.append(h[-1] - c[i])
ans = 0
for l in range(0, n, 2):
	if l == n - 1:
		break
	nmin = float('inf')
	la = h[l]
	lb = h[l + 1]
	for r in range(l + 1, n, 2):
		ra = h[r + 1]
		rb = h[r]
		a = max(la, ra)
		b = min(lb, rb, nmin + 1)
		now = max(0, b - a)
		ans += now
		nmin = min(nmin, ra)
print(ans)


n = int(input())
aa = list(map(int, input().split()))
ans = 0
stk = []
for i in range(n):
	if not stk:
		stk.append([0, 1])
	if i & 1:
		while aa[i] and stk:
			if stk[-1][1] == 0:
				stk.pop()
			if stk:
				now = stk.pop()
				sub = min(now[0], aa[i])
				aa[i] -= sub
				now[0] -= sub
				ans += sub
				if now[0]:
					stk.append(now)
				if stk and stk[-1][1] == 0:
					ans += stk[-1][0]
		if aa[i]:
			stk.append([0, 1])
		else:
			if not stk:
				stk.append([0, 0])
			if stk[-1][1] == 0:
				stk[-1][0] += 1
			else:
				stk.append([1, 0])
	else:
		if stk[-1][1] == 0:
			stk.append([0, 1])
		stk[-1][0] += aa[i]
print(ans)


n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(0, n - 1, 2):
	k = 1
	s = 0
	for j in range(i + 1, n, 2):
		ans += max(0, min(a[i], a[j] + s) - max(k, s) + 1)
		if j + 2 < n:
			s += a[j]
			k = max(k, s)
			s -= a[j + 1]
print(ans)


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

def F(C):
	S = ''.join((('(' if i % 2 == 0 else ')') * C[i] for i in range(len(C))))
	r = 0
	for i in range(len(S)):
		o = 0
		if S[i] == '(':
			for s in S[i:]:
				if s == '(':
					o += 1
				elif o > 0:
					o -= 1
					if o == 0:
						r += 1
				else:
					break
	return r
n = int(input())
C = list(map(int, input().split(' ')))
n = n - n % 2
(count, before) = (0, [])
for i in range(0, n, 2):
	v = min(C[i], C[i + 1])
	count += v
	e = C[i] - C[i + 1]
	if e == 0:
		if len(before) > 0:
			before.append(before[-1])
		else:
			before.append(0)
	elif e > 0:
		if len(before) > 0:
			before.append(before[-1] + e)
		else:
			before.append(e)
	else:
		if len(before) > 0:
			before.append(before[-1])
		while len(before) > 0 and e < 0:
			c = 0
			while len(before) > 1 and before[-1] == before[-2]:
				before.pop()
				c += 1
			count += c * (c + 1) // 2
			b = before[-1] - (before[-2] if len(before) > 1 else 0)
			x = min(abs(e), b)
			before[-1] -= x
			count += x
			e += x
			if x == 0:
				break
		if e == 0:
			pass
		else:
			before = []
while len(before) > 0:
	c = 0
	while len(before) > 1 and before[-1] == before[-2]:
		before.pop()
		c += 1
	count += c * (c + 1) // 2
	before.pop()
print(count)


import sys
ints = (int(x) for x in sys.stdin.read().split())
sys.setrecursionlimit(3000)

class Stack(list):
	ans = 0
	lzeros = 0
	zeros = []

	def save(self, n, k):
		self.ans += n + k * (k - 1) // 2

	def read(self, seq):
		it = iter(seq)
		for (a, b) in zip(it, it):
			self.append(a)
			self.zeros.append(0)
			self.close(b)
		while self:
			self.pop()
			self.save(0, self.zeros.pop())
		self.save(0, self.lzeros)
		self.lzeros = 0
		return self.ans

	def close(self, n):
		assert n > 0
		while self and n >= self[-1]:
			n -= self[-1]
			self.save(self.pop(), self.zeros.pop())
			if self:
				self.zeros[-1] += 1
			else:
				self.lzeros += 1
		if n > 0:
			if not self:
				self.save(0, self.lzeros)
				self.lzeros = 0
			elif n < self[-1]:
				self.save(n, self.zeros[-1])
				self[-1] -= n
				self.zeros[-1] = 1
		return

def main():
	n = next(ints)
	stack = Stack()
	stack.read([next(ints) for i in range(n)])
	print(stack.ans)
	return
main()


import sys
input = sys.stdin.readline
n = int(input())
c = [0] + list(map(int, input().split()))
s = [0] * (n + 1)
for i in range(1, n + 1):
	s[i] = s[i - 1]
	if i % 2 == 1:
		s[i] += c[i]
	else:
		s[i] -= c[i]
ans = 0
for l in range(0, n):
	mn = s[l + 1]
	for r in range(l + 1, n):
		if s[l] > s[l + 1] or s[r] < s[r + 1]:
			mn = min(mn, s[r])
			continue
		sub = max(s[l], s[r + 1])
		sub2 = min(mn, s[l + 1] - 1, s[r] - 1)
		if sub <= sub2:
			ans += sub2 - sub + 1
		mn = min(mn, s[r])
print(ans)


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')
from math import ceil

def prod(a, mod=10 ** 9 + 7):
	ans = 1
	for each in a:
		ans = ans * each % mod
	return ans

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def lcm(a, b):
	return a * b // gcd(a, b)

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

def getOverlap(a, b):
	return max(0, min(a[1], b[1]) - max(a[0], b[0]) + 1)
for _ in range(int(input()) if not True else 1):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	for i in range(0, n, 2):
		bal = [1, a[i]]
		for j in range(i + 1, n):
			if j % 2 == 1:
				ov = getOverlap(bal, [1, a[j]])
				ans += ov
				bal = [bal[0] - a[j], bal[1] - a[j]]
				if bal[0] < 0:
					bal[0] = 0
				if bal[1] < 0:
					break
			else:
				bal = [bal[0] + a[j], bal[1] + a[j]]
	print(ans)


import sys
input = sys.stdin.readline

def ri():
	return [int(i) for i in input().split()]

def common(amin, amax, bmin, bmax):
	if amax < bmin or bmax < amin:
		return 0
	return min(amax, bmax) - max(amin, bmin) + 1

def solve(Aseg, Bseg, fminopen, faddsopen):
	if fminopen > Aseg:
		return 0
	amin = max(1, fminopen) + faddsopen
	amax = Aseg + faddsopen
	return common(amin, amax, 1, Bseg)

def main():
	t = 1
	for _ in range(t):
		n = ri()[0]
		a = ri()
		res = 0
		for st_seg_idx in range(0, n, 2):
			f_min_open = 0
			f_adds_open = 0
			seg_result = 0
			for end_seg_idx in range(st_seg_idx + 1, n):
				if end_seg_idx % 2 == 0:
					f_adds_open += a[end_seg_idx]
				else:
					seg_result += solve(a[st_seg_idx], a[end_seg_idx], f_min_open, f_adds_open)
					f_adds_open -= a[end_seg_idx]
					if f_adds_open < 0:
						f_min_open = max(f_min_open, -f_adds_open)
				if f_min_open > a[st_seg_idx]:
					break
			res += seg_result
		print(res)
main()


import itertools as _itertools
import math as _math
import sys as _sys
import time as _time

def f(c_seq):
	c_seq = [+c if i % 2 == 0 else -c for (i, c) in enumerate(c_seq)]
	prefix_sums = tuple(_itertools.accumulate(c_seq))
	segs_minimums = _precompute_minimums(prefix_sums)
	result = 0
	for l in range(0, len(c_seq), 2):
		for r in range(l + 1, len(c_seq), 2):
			opening_n = abs(c_seq[l])
			closing_n = abs(c_seq[r])
			if r > l + 1:
				min_sum_between = segs_minimums[l + 1, r - 1] - prefix_sums[l]
				max_sum_between = prefix_sums[r - 1] - segs_minimums[l, r - 2]
			else:
				min_sum_between = 0
				max_sum_between = 0
			if max_sum_between > 0:
				closing_n -= abs(max_sum_between) - 1
			if min_sum_between < 0:
				opening_n -= abs(min_sum_between) - 1
			curr_variants_n = min(opening_n, closing_n)
			if curr_variants_n >= 0:
				result += curr_variants_n
	return result

def _precompute_minimums(sequence):
	result = dict()
	for l in range(len(sequence)):
		curr_minimum = _math.inf
		for r in range(l, len(sequence)):
			curr_minimum = min(curr_minimum, sequence[r])
			result[l, r] = curr_minimum
	return result

def _main():
	start_time = _time.time()
	[c_n] = _read_ints()
	c_seq = tuple(_read_ints())
	assert len(c_seq) == c_n
	result = f(c_seq)
	print(result)
	time_limit = 1
	while _time.time() - start_time < time_limit - 1 / 16:
		_time.sleep(1 / 256)

def _read_ints(file=_sys.stdin):
	return map(int, file.readline().split())
_main()


l = int(input())
s = [int(i) for i in input().split()]
p = ans = 0
st = []
for i in range(l):
	if i % 2:
		if p >= s[i]:
			p -= s[i]
			ans += s[i]
		else:
			ans += p
			p = -1
		while st and st[-1][0] > p:
			(pp, t) = st.pop()
			ans += t
		if st and st[-1][0] == p:
			ans += st[-1][1]
			st[-1][1] += 1
		elif p == -1:
			p = 0
		else:
			st.append([p, 1])
	else:
		p += s[i]
print(ans)


n = int(input())
arr = list(map(int, input().split()))
ap = []
ans = 0
for i in range(0, n - 1, 2):
	mn = 0
	c = 0
	for j in range(i + 1, n, 2):
		if j - i > 1:
			mn = min(mn, c - arr[j - 2])
			c += arr[j - 1] - arr[j - 2]
		ans += max(-1, min(arr[i] + mn, arr[j] + mn - c)) + (j - i > 1)
print(ans)


n = int(input())
q = dict()
ans = 0
a = list(map(int, input().split()))
for i in range(1, n, 2):
	cnt = a[i]
	max_cnt = a[i]
	for j in range(i + 1, n):
		if j % 2 == 0:
			cnt -= a[j]
			q[i, j] = [cnt, max_cnt]
		else:
			cnt += a[j]
			max_cnt = max(max_cnt, cnt)
for i in range(0, n, 2):
	for j in range(i + 1, n, 2):
		if i == j - 1:
			ans += min(a[i], a[j])
		else:
			l = a[i]
			r = a[j]
			p = q[i + 1, j - 1]
			l -= p[1]
			p[0] -= p[1]
			r += p[0]
			if l >= 0 and r >= 0:
				ans += min(l, r) + 1
print(ans)


n = int(input())
a = list(map(int, input().split()))
ans = 0
stk = [[a[0], 0]]
i = 1
fi = [-1] * n
while i < n:
	if i % 2 == 0:
		stk.append([a[i], i])
	else:
		while a[i] > 0 and len(stk):
			if a[i] < stk[-1][0]:
				ans += a[i]
				stk[-1][0] -= a[i]
				a[i] = 0
				fi[i] = stk[-1][1]
			else:
				a[i] -= stk[-1][0]
				x = stk[-1][1]
				ans += stk[-1][0]
				stk.pop()
				if a[i] == 0:
					fi[i] = x
				copyi = i
				i = x - 1
				while i >= 0:
					if a[i] != 0:
						break
					if len(stk) and stk[-1][1] > i:
						break
					ans += 1
					i = fi[i]
					i -= 1
				i = copyi
	i += 1
print(ans)


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

def solve(n, c):
	l = [val for (ind, val) in enumerate(c) if ind % 2 == 0]
	r = [val for (ind, val) in enumerate(c) if ind % 2 == 1]
	ans = 0
	stack = [[0, 0]]
	for (o, c) in zip(l, r):
		ans += min(o, c)
		if o == c:
			ans += stack[-1][1]
			stack[-1][1] += 1
		elif o > c:
			stack.append([o - c, 1])
		else:
			ans += stack[-1][1]
			excess = c - o
			while excess > 0:
				if len(stack) == 0:
					stack = [[0, 0]]
					break
				if excess < stack[-1][0]:
					ans += excess
					stack[-1][0] -= excess
					stack[-1][1] = 1
					break
				if excess == stack[-1][0]:
					ans += excess
					if len(stack) == 1:
						stack[-1][0] -= excess
						stack[-1][1] = 1
					else:
						stack.pop()
						ans += stack[-1][1]
						stack[-1][1] += 1
					break
				if excess > stack[-1][0]:
					if len(stack) == 1:
						ans += stack[-1][0]
						excess -= stack[-1][0]
						stack.pop()
						stack.append([0, 0])
						break
					else:
						ans += stack[-1][0]
						excess -= stack[-1][0]
						stack.pop()
						ans += stack[-1][1]
						stack[-1][1] += 1
	return ans
n = int(input())
c = [*map(int, input().split())]
my_ans = solve(n, c)
print(my_ans)


n = int(input().strip())
a = [int(x) for x in input().strip().split(' ')]
ans = 0
for i in range(0, n, 2):
	now = 0
	mn_now = a[i] - 1
	for j in range(i, n):
		if j & 1:
			pre_now = now
			now -= a[j]
			ans += max(0, min(pre_now, mn_now + 1) - max(0, now))
			mn_now = min(mn_now, now)
		else:
			now += a[j]
		if mn_now < 0:
			break
print(ans)


n = int(input())
c = list(map(int, input().split()))
c.insert(0, 0)
ans = 0
for i in range(1, n + 1, 2):
	(bal, lo) = (0, 1)
	for j in range(i + 1, n + 1):
		if j % 2 == 0:
			L = lo
			R = max(1, bal + lo)
			ans += max(0, min(c[i] - L + 1, c[j] - R + 1))
			bal -= c[j]
		else:
			bal += c[j]
		lo = max(lo, -bal)
print(ans)


import sys, io, os
try:
	input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
except:
	input = lambda : sys.stdin.readline().encode()
read = lambda : map(int, input().split())

def f(n, a):
	x = []
	s = t = 0
	for i in range(n):
		if i & 1:
			c = [s - a[i], s - 1]
			for j in range(len(x)):
				k = x[-1 - j]
				t += max(0, min(c[1], k[1]) - max(c[0], k[0]) + 1)
				c[1] = min(c[1], k[0])
				if c[1] < c[0]:
					break
			s -= a[i]
		else:
			x.append((s, s + a[i]))
			s += a[i]
	return t
print(f(int(input()), list(read())))


import sys
n = int(sys.stdin.readline())
data = list(map(int, sys.stdin.readline().split()))
(ans, cur_open) = (0, 0)
s = [[0, 0]]
for i in range(0, n, 2):
	if i + 1 >= n:
		continue
	open_bracket = data[i]
	close_bracket = data[i + 1]
	cur_open += open_bracket - close_bracket
	ans += min(close_bracket, cur_open + close_bracket - s[0][0])
	while s and s[-1][0] > cur_open:
		ans += s[-1][1]
		s.pop()
	if s and s[-1][0] == cur_open:
		ans += s[-1][1]
		s[-1][1] += 1
	elif s:
		s.append([cur_open, 1])
	else:
		s.append([cur_open, 0])
print(ans)


import sys
input = sys.stdin.readline

def sv(ls):
	ret = 0
	deep = 0
	stack = []
	for i in range(len(ls)):
		v = ls[i]
		if i % 2 == 0:
			deep += v
		else:
			append = True
			if v > deep:
				append = False
			v = min(v, deep)
			ret += v
			deep -= v
			memo = None
			while stack:
				(depth, vv) = stack[-1]
				if depth < deep:
					break
				(depth, vv) = stack.pop()
				if append and depth == deep:
					memo = [depth, vv + 1]
				ret += vv
			if append:
				if memo:
					stack.append(memo)
				else:
					stack.append([deep, 1])
	return ret

def main():
	input()
	a = list(map(int, input().split()))
	print(sv(a))
main()


from sys import stdin

def solve():
	n = int(stdin.readline().strip())
	seq = list(map(int, stdin.readline().split()))
	ans = 0
	for i in range(0, n, 2):
		curr = seq[i]
		inter_sum = 0
		for j in range(i + 1, n):
			if j % 2 == 0:
				inter_sum += seq[j]
			else:
				inter_sum -= seq[j]
				if inter_sum <= 0 and j > i + 1:
					ans += 1
				if inter_sum < 0:
					if curr + inter_sum >= 0:
						ans += -inter_sum
						curr += inter_sum
						inter_sum = 0
					else:
						ans += curr
						break
	print(ans)
solve()


import sys
from bisect import *
from collections import deque
from copy import *
from heapq import *
from itertools import permutations as prm, product
from random import *

def eprint(*args):
	print(*args, file=sys.stderr)
zz = 1
sys.setrecursionlimit(10 ** 5)
if zz:
	input = sys.stdin.readline
else:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('all.txt', 'w')
di = [[-1, 0], [1, 0], [0, 1], [0, -1]]

def fori(n):
	return [fi() for i in range(n)]

def inc(d, c, x=1):
	d[c] = d[c] + x if c in d else x

def ii():
	return input().rstrip()

def li():
	return [int(xx) for xx in input().split()]

def fli():
	return [float(x) for x in input().split()]

def dadd(d, p, val):
	if p in d:
		d[p].append(val)
	else:
		d[p] = [val]

def gi():
	return [xx for xx in input().split()]

def gtc(tc, *ans):
	print('Case #' + str(tc) + ':', *ans)

def cil(n, m):
	return n // m + int(n % m > 0)

def fi():
	return int(input())

def pro(a):
	return reduce(lambda a, b: a * b, a)

def swap(a, i, j):
	(a[i], a[j]) = (a[j], a[i])

def bits(i, n):
	p = bin(i)[2:]
	return (n - len(p)) * '0' + p

def prec(a, pre):
	for i in a:
		pre.append(pre[-1] + i)
	pre.pop(0)

def YN(flag):
	print('YES' if flag else 'NO')

def si():
	return list(input().rstrip())

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

def gh():
	sys.stdout.flush()

def isvalid(i, j, n, m):
	return 0 <= i < n and 0 <= j < m

def bo(i):
	return ord(i) - ord('a')

def graph(n, m):
	for i in range(m):
		(x, y) = mi()
		a[x].append(y)
		a[y].append(x)
t = 1
INF = 10 ** 18
uu = t
mod = 10 ** 9 + 7
while t > 0:
	t -= 1
	n = fi()
	a = li()
	ans = 0
	for i in range(0, n, 2):
		b = 0
		minb = 10 ** 18
		for j in range(i, n):
			if j % 2 == 0:
				b += a[j]
			else:
				ans += max(0, -max(0, b - a[j]) + min(a[i] - 1, minb) + 1)
				b -= a[j]
				if b < 0:
					break
			minb = min(minb, b)
	print(ans)


def overlap(min1, max1, min2, max2):
	if max1 < min2 or max2 < min1:
		return 0
	return max(0, min(max1, max2) - max(min1, min2)) + 1
n = int(input())
arr = list(map(int, input().split()))
ans = 0
li = []
for i in range(0, n, 2):
	temp = 0
	a = 0
	b = arr[i] - 1
	temp += arr[i]
	flag = 0
	ans = 0
	for j in range(i + 1, n):
		(c, d) = (0, 0)
		if j % 2 == 0:
			temp += arr[j]
		else:
			c = temp - 1
			d = temp - arr[j]
			temp -= arr[j]
			ans += overlap(a, b, d, c)
			b = min(b, temp)
		if temp < 0:
			break
	li.append(ans)
print(sum(li))


import os, sys
from io import BytesIO, IOBase

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, 8192))
			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, 8192))
			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')

class dict(dict):

	def __missing__(self, key):
		return 0
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
inp = lambda dtype: [dtype(x) for x in input().split()]
inp_2d = lambda dtype, n: [dtype(input()) for _ in range(n)]
inp_2ds = lambda dtype, n: [inp(dtype) for _ in range(n)]
inp_enu = lambda dtype: [(i, x) for (i, x) in enumerate(inp(dtype))]
inp_enus = lambda dtype, n: [[i] + [inp(dtype)] for i in range(n)]
ceil1 = lambda a, b: (a + b - 1) // b
(n, a, ans) = (int(input()), inp(int), 0)
for i in range(1, n, 2):
	if a[i] <= a[i - 1]:
		ext = a[i - 1] - a[i]
		(cnt, l, r) = (1, 0, 0)
		for j in range(i + 2, n, 2):
			l += a[j - 1]
			r += a[j]
			if l < r:
				cnt += 1
				if r - l > ext:
					break
				ans += cnt - 1
				ext -= r - l
				cnt = 0
			elif l > r:
				continue
			cnt += 1
			l = r = 0
		ans += cnt - 1
(l, r) = (0, 0)
for i in range(1, n, 2):
	l += a[i - 1]
	r += a[i]
	d = min(l, r)
	l -= d
	r -= d
	ans += d
	if l < r:
		r = l = 0
print(ans)


import os
import sys
import math
from io import BytesIO, IOBase
import io
from fractions import Fraction
import collections
from itertools import permutations
from collections import defaultdict
from collections import deque
from collections import Counter
import threading
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 SegmentTree:

	def __init__(self, data, default=0, func=lambda a, b: math.gcd(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 SegmentTree1:

	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)
MOD = 10 ** 9 + 7

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
mod = 10 ** 9 + 7
omod = 998244353
prime = [True for i in range(11)]
prime[0] = prime[1] = False

def SieveOfEratosthenes(n=10):
	p = 2
	c = 0
	while p <= n:
		if prime[p] == True:
			c += 1
			for i in range(p, n + 1, p):
				prime[i] = False
		p += 1

class DSU:

	def __init__(self, R, C):
		self.par = range(R * C + 1)
		self.rnk = [0] * (R * C + 1)
		self.sz = [1] * (R * C + 1)

	def find(self, x):
		if self.par[x] != x:
			self.par[x] = self.find(self.par[x])
		return self.par[x]

	def union(self, x, y):
		(xr, yr) = (self.find(x), self.find(y))
		if xr == yr:
			return
		if self.rnk[xr] < self.rnk[yr]:
			(xr, yr) = (yr, xr)
		if self.rnk[xr] == self.rnk[yr]:
			self.rnk[xr] += 1
		self.par[yr] = xr
		self.sz[xr] += self.sz[yr]

	def size(self, x):
		return self.sz[self.find(x)]

	def top(self):
		return self.size(len(self.sz) - 1) - 1

class LazySegTree:

	def __init__(self, _op, _e, _mapping, _composition, _id, v):

		def set(p, x):
			assert 0 <= p < _n
			p += _size
			for i in range(_log, 0, -1):
				_push(p >> i)
			_d[p] = x
			for i in range(1, _log + 1):
				_update(p >> i)

		def get(p):
			assert 0 <= p < _n
			p += _size
			for i in range(_log, 0, -1):
				_push(p >> i)
			return _d[p]

		def prod(l, r):
			assert 0 <= l <= r <= _n
			if l == r:
				return _e
			l += _size
			r += _size
			for i in range(_log, 0, -1):
				if l >> i << i != l:
					_push(l >> i)
				if r >> i << i != r:
					_push(r >> i)
			sml = _e
			smr = _e
			while l < r:
				if l & 1:
					sml = _op(sml, _d[l])
					l += 1
				if r & 1:
					r -= 1
					smr = _op(_d[r], smr)
				l >>= 1
				r >>= 1
			return _op(sml, smr)

		def apply(l, r, f):
			assert 0 <= l <= r <= _n
			if l == r:
				return
			l += _size
			r += _size
			for i in range(_log, 0, -1):
				if l >> i << i != l:
					_push(l >> i)
				if r >> i << i != r:
					_push(r - 1 >> i)
			l2 = l
			r2 = r
			while l < r:
				if l & 1:
					_all_apply(l, f)
					l += 1
				if r & 1:
					r -= 1
					_all_apply(r, f)
				l >>= 1
				r >>= 1
			l = l2
			r = r2
			for i in range(1, _log + 1):
				if l >> i << i != l:
					_update(l >> i)
				if r >> i << i != r:
					_update(r - 1 >> i)

		def _update(k):
			_d[k] = _op(_d[2 * k], _d[2 * k + 1])

		def _all_apply(k, f):
			_d[k] = _mapping(f, _d[k])
			if k < _size:
				_lz[k] = _composition(f, _lz[k])

		def _push(k):
			_all_apply(2 * k, _lz[k])
			_all_apply(2 * k + 1, _lz[k])
			_lz[k] = _id
		_n = len(v)
		_log = _n.bit_length()
		_size = 1 << _log
		_d = [_e] * (2 * _size)
		_lz = [_id] * _size
		for i in range(_n):
			_d[_size + i] = v[i]
		for i in range(_size - 1, 0, -1):
			_update(i)
		self.set = set
		self.get = get
		self.prod = prod
		self.apply = apply
MIL = 1 << 20

def makeNode(total, count):
	return total * MIL + count

def getTotal(node):
	return math.floor(node / MIL)

def getCount(node):
	return node - getTotal(node) * MIL
nodeIdentity = makeNode(0.0, 0.0)

def nodeOp(node1, node2):
	return node1 + node2
	return makeNode(getTotal(node1) + getTotal(node2), getCount(node1) + getCount(node2))
identityMapping = -1

def mapping(tag, node):
	if tag == identityMapping:
		return node
	count = getCount(node)
	return makeNode(tag * count, count)

def composition(mapping1, mapping2):
	return mapping1 if mapping1 != identityMapping else mapping2

def memodict(f):

	class memodict(dict):

		def __missing__(self, key):
			ret = self[key] = f(key)
			return ret
	return memodict().__getitem__

def pollard_rho(n):
	if n & 1 == 0:
		return 2
	if n % 3 == 0:
		return 3
	s = (n - 1 & 1 - n).bit_length() - 1
	d = n >> s
	for a in [2, 325, 9375, 28178, 450775, 9780504, 1795265022]:
		p = pow(a, d, n)
		if p == 1 or p == n - 1 or a % n == 0:
			continue
		for _ in range(s):
			prev = p
			p = p * p % n
			if p == 1:
				return math.gcd(prev - 1, n)
			if p == n - 1:
				break
		else:
			for i in range(2, n):
				(x, y) = (i, (i * i + 1) % n)
				f = math.gcd(abs(x - y), n)
				while f == 1:
					(x, y) = ((x * x + 1) % n, (y * y + 1) % n)
					y = (y * y + 1) % n
					f = math.gcd(abs(x - y), n)
				if f != n:
					return f
	return n

@memodict
def prime_factors(n):
	if n <= 1:
		return Counter()
	f = pollard_rho(n)
	return Counter([n]) if f == n else prime_factors(f) + prime_factors(n // f)

def distinct_factors(n):
	factors = [1]
	for (p, exp) in prime_factors(n).items():
		factors += [p ** i * factor for factor in factors for i in range(1, exp + 1)]
	return factors

def all_factors(n):
	(small, large) = ([], [])
	for i in range(1, int(n ** 0.5) + 1, 2 if n & 1 else 1):
		if not n % i:
			small.append(i)
			large.append(n // i)
	if small[-1] == large[-1]:
		large.pop()
	large.reverse()
	small.extend(large)
	return small

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

def binarySearch1(arr, n, key):
	left = 0
	right = n - 1
	mid = 0
	res = -1
	while left <= right:
		mid = (right + left) // 2
		if arr[mid] > key:
			right = mid - 1
		else:
			res = mid
			left = mid + 1
	return res
t = 1
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	for start in range(0, n, 2):
		m = a[start]
		curr = a[start]
		for end in range(start + 1, n):
			if end % 2 == 0:
				curr += a[end]
				m = min(m, curr)
			else:
				curr -= a[end]
				if curr >= 0:
					if curr < a[start] and curr <= m:
						ans += min(a[start] - curr, a[end], m - curr + 1)
					m = min(m, curr)
					continue
				curr += a[end]
				ans += min(a[start], m + 1, curr)
				break
	print(ans)


N = int(input())
C = list(map(int, input().split()))
ans = 0
stack = [[0, 0]]
now = pre = mn = 0
for (i, c) in enumerate(C):
	if i % 2 == 0:
		pre = now
		now += c
	else:
		pre = now
		now -= c
		ans += pre - max(now, mn)
		is_mn = False
		if mn > now:
			mn = now
			is_mn = True
		while stack and stack[-1][0] > now:
			(_, v) = stack.pop()
			ans += v
		if stack and stack[-1][0] == now:
			stack[-1][1] += 1
			ans += stack[-1][1] - 1
		elif is_mn:
			stack.append([now, 0])
		else:
			stack.append([now, 1])
print(ans)


import math
import random
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
import sys
from collections import defaultdict
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=2 ** 51, 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 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
for ik in range(1):
	n = int(input())
	l = list(map(int, input().split()))
	ans = 0
	maxi = [[-1 for i in range(n)] for j in range(n)]
	diff = [[-1 for i in range(n)] for j in range(n)]
	for i in range(0, n, 2):
		c = l[i]
		mi = c
		for j in range(i + 1, n):
			if (j - i) % 2 == 1:
				c -= l[j]
				maxi[i][j] = mi
				mi = min(mi, c)
				diff[i][j] = c
			else:
				c += l[j]
	for i in range(0, n, 2):
		for j in range(i + 1, n, 2):
			ma = maxi[i][j]
			if ma < 0:
				continue
			else:
				k1 = l[j]
				k = l[i]
				if diff[i][j] < 0:
					k1 += diff[i][j]
				else:
					k -= diff[i][j]
					ma -= diff[i][j]
				ma1 = min(ma + 1, k, k1)
				ans += max(0, ma1)
	print(ans)


from sys import stdin
inp = stdin.readline
n = int(inp())
arr = [int(x) for x in inp().split()]
count = 0
for i in range(0, n, 2):
	start = arr[i]
	rest = 0
	for j in range(i + 1, n):
		if j % 2:
			if arr[j] < rest:
				rest -= arr[j]
			elif start + rest >= arr[j]:
				start -= arr[j] - rest
				count += arr[j] - rest + (1 if rest > 0 else 0)
				rest = 0
			else:
				count += start + (1 if rest > 0 else 0)
				break
		else:
			rest += arr[j]
print(count)


def process(A):
	n = len(A)
	answer = 0
	for i in range(0, n, 2):
		my_upper = A[i]
		my_lower = -1 * float('inf')
		my_sum = 0
		for j in range(i + 1, n):
			if j % 2 == 0:
				my_sum -= A[j]
			else:
				this_upper = min(my_upper, A[j] + my_sum)
				this_lower = max(1, my_lower)
				my_lower = max(my_lower, A[j] + my_sum)
				my_sum += A[j]
				if this_upper >= this_lower:
					answer += this_upper - this_lower + 1
				else:
					answer += 0
	return answer
n = int(input())
A = [int(x) for x in input().split()]
print(process(A))


import sys
input = sys.stdin.readline
n = int(input())
C = list(map(int, input().split()))
S = [0]
for i in range(n):
	c = C[i]
	if i % 2 == 0:
		S.append(S[-1] + c)
	else:
		S.append(S[-1] - c)
X = []
for s in S:
	X.append(s - 1)
	X.append(s)
	X.append(s + 1)
X = sorted(set(X))
D = {X[i]: i for i in range(len(X))}
Y = [[] for i in range(len(X))]
now = 0
for i in range(n):
	c = C[i]
	if i % 2 == 0:
		for k in range(D[now], D[now + c]):
			Y[k].append(1)
		now += c
	else:
		for k in range(D[now], D[now - c], -1):
			Y[k].append(-1)
		now -= c
if n % 2 == 0:
	Y[D[now]].append(1)
else:
	Y[D[now]].append(-1)
ANS = 0
for i in range(len(Y) - 1):
	count = 0
	K = 0
	for y in Y[i]:
		if y == 1:
			count += 1
		else:
			K += count * (count + 1) // 2
			count = 0
	if count != 0:
		K += count * (count - 1) // 2
	ANS += K * (X[i + 1] - X[i])
print(ANS)


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def input_list():
	return list(map(int, input().split()))

def input_string():
	s = input()
	return list(s[:len(s) - 1])

def input_int_gen():
	return map(int, input().split())
tests = 1
for _ in range(tests):
	n = inp()
	a = input_list()
	stk = [[0, 0]]
	lefts = 0
	ret = 0
	for (idx, x) in enumerate(a):
		if idx % 2 == 0:
			lefts += x
		else:
			level = 0
			valid = lefts
			if x <= lefts:
				level = lefts - x
				valid = x
			ret += valid
			while stk[-1][0] > level:
				ret += stk[-1][1]
				stk.pop()
			if stk[-1][0] == level:
				ret += stk[-1][1]
				stk[-1][1] += 1
			else:
				stk.append([level, 1])
			if x > lefts:
				stk = [[0, 0]]
			lefts = level
	print(ret)


n = int(input())
a = list(map(int, input().split()))
s = []
answer = 0
for i in range(n):
	if i % 2 == 0:
		if len(s) == 0 or s[-1][0] != '(':
			s.append(['(', a[i]])
		else:
			s[-1][1] += a[i]
	else:
		while len(s) > 0:
			if len(s) > 0 and s[-1][0] == '(':
				if s[-1][1] <= a[i]:
					a[i] -= s[-1][1]
					s[-1][0] = '()'
				else:
					s[-1][1] -= a[i]
					s.append(['()', a[i]])
					a[i] = 0
			if a[i] == 0:
				break
			accu = 0
			while len(s) > 0 and s[-1][0] == '()':
				answer += s[-1][1] + accu
				s.pop()
				accu += 1
accu = 0
while len(s) > 0:
	if s[-1][0] == '()':
		answer += s[-1][1] + accu
		accu += 1
		s.pop()
	else:
		accu = 0
		s.pop()
print(answer)


if 1:
	n = int(input())
	c = list(map(int, input().split()))
	count = 0
	open_brackets_left = 0
	prev_brackets_left = []
	for (i, c_i) in enumerate(c):
		if i % 2:
			count += min(c_i, open_brackets_left)
			open_brackets_left -= c_i
			while prev_brackets_left:
				if prev_brackets_left[-1][0] > open_brackets_left:
					(_, num) = prev_brackets_left.pop()
					count += num
				else:
					break
			if prev_brackets_left and prev_brackets_left[-1][0] == open_brackets_left:
				count += prev_brackets_left[-1][1]
				prev_brackets_left[-1][1] += 1
			else:
				prev_brackets_left.append([open_brackets_left, 1])
			if open_brackets_left < 0:
				open_brackets_left = 0
				prev_brackets_left = []
		else:
			open_brackets_left += c_i
	print(count)


from __future__ import division, print_function
import os
import sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip
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')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import collections, bisect, re, random, queue, itertools, statistics, math
from collections import *
from bisect import *
from itertools import *
from statistics import *
from math import *
from re import *
from queue import *

class Stack:

	def __init__(self):
		self.items = []

	def push(self, item):
		self.items.append(item)

	def pop(self):
		return self.items.pop()

	def empty(self):
		return self.items == []

	def size(self):
		return len(self.items)

	def at(self, idx):
		try:
			return self.items[idx]
		except:
			return -1

class priority_queue(object):

	def __init__(self):
		self.queue = []

	def __str__(self):
		return ' '.join([str(i) for i in self.queue])

	def empty(self):
		return len(self.queue) == 0

	def insert(self, data):
		self.queue.append(data)

	def delete(self):
		try:
			max = 0
			for i in range(len(self.queue)):
				if self.queue[i] > self.queue[max]:
					max = i
			item = self.queue[max]
			del self.queue[max]
			return item
		except IndexError:
			print()
			exit()
mod = int(1000000000.0 + 7)
imin = -2147483648
imax = 2147483647
true = True
false = False
N = int(100000.0 + 9)
none = None
inp = lambda : input()
I = lambda : int(inp())
M = lambda : map(int, inp().split())
MS = lambda : map(str, inp().split())
S = lambda : list(MS())
L = lambda : list(M())
Yes = lambda : print('Yes')
No = lambda : print('No')
YES = lambda : print('YES')
NO = lambda : print('NO')
yes = lambda : print('yes')
no = lambda : print('no')
chartonum = {'1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '0': 0}

def IO(controll):
	if controll == false:
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('uttar.txt', 'w')
	else:
		pass

def true_yes(statement):
	return YES() if statement else NO()

def binary(x):
	return bin(x).replace('0b', '')

def state(statement, one, two):
	print(one if statement else two)

def swap(a, b):
	(a, b) = (b, a)

def bintodec(n):
	return int(n, 2)

def prec(a):
	return '{0:.9f}'.format(a)

def case(t):
	print('Case #' + str(i) + ': ', end='')

def palin(s):
	return s == s[::-1]

def is_sorted(arr):
	return arr == sorted(arr)

def kabraji_ka_totka():
	IO(1)
	n = I()
	l = L()
	stack = []
	ct = []
	last = 0
	out = 0
	for i in range(n):
		v = l[i]
		if i % 2 == 0:
			stack.append(v)
			ct.append(last)
		else:
			while v and stack:
				assert v > 0
				top = stack.pop()
				if v >= top:
					v -= top
					out += top
					ctp = ct.pop()
					out += ctp
					last = ctp + 1
				else:
					out += v
					stack.append(top - v)
					v = 0
					last = 1
			if v:
				last = 0
	print(out)
kabraji_ka_totka()


import sys
input = sys.stdin.readline

def main():
	t = 1
	for _ in range(t):
		n = int(input())
		ar = list(map(int, input().split()))
		signs = [0]
		for (i, x) in enumerate(ar):
			if i % 2:
				signs.append(signs[-1] - x)
			else:
				signs.append(signs[-1] + x)
		ans = 0
		for l in range(0, n, 2):
			for r in range(l + 1, n, 2):
				left = ar[l]
				right = ar[r]
				if r - l == 1:
					ans += min(left, right)
					continue
				stuff = signs[r] - signs[l + 1]
				minleft = min(signs[l + 2:r + 1]) - signs[l + 1]
				minleft = max(-minleft, 1)
				equivmin = stuff + minleft
				ans += max(min(left - minleft + 1, right - equivmin + 1), 0)
		print(ans)
main()


n = int(input())
arr = [int(x) for x in input().split()]
ans = [int(0) for x in range(n + 1)]
tempans = [int(0) for x in range(n + 1)]
arr.insert(0, 0)
for i in range(2, n + 1, 2):
	if arr[i] < arr[i - 1]:
		ans[i] += arr[i]
	else:
		ans[i] += arr[i - 1]
		temp = arr[i]
		ans[i] += tempans[i - 2]
		temp -= arr[i - 1]
		j = i - 2
		tt = temp
		temp = 0
		while tempans[j] and tt > 0 and (j > 0):
			if arr[j - 1] > temp + tt + arr[j]:
				ans[i] += tt
				temp = 0
				tt = 0
			else:
				if arr[j - 1] >= arr[j] + temp:
					ans[i] += arr[j - 1] - (arr[j] + temp)
					if arr[j - 1] - (arr[j] + temp):
						ans[i] += tempans[j - 2]
				temp += arr[j]
				temp -= arr[j - 1]
				if temp < 0:
					tt += temp
					temp = 0
			j -= 2
	temp = 0
	k = 1
	j = i
	while k or (temp > 0 and j > 0):
		k = 0
		temp += arr[j]
		temp -= arr[j - 1]
		j -= 2
	if temp == 0:
		tempans[i] = 1 + tempans[j]
	elif temp < 0:
		tempans[i] = 1
answer = 0
for i in range(n + 1):
	answer += ans[i]
print(answer)


import os, sys
from io import BytesIO, IOBase
from math import log2, ceil, sqrt, gcd
from _collections import deque
import heapq as hp
from bisect import bisect_left, bisect_right
from math import cos, sin
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')
mod = 10 ** 9 + 7
n = int(input())
a = list(map(int, input().split()))
ck = [a[0]]
for i in range(1, n):
	if i % 2:
		ck.append(ck[-1] - a[i])
	else:
		ck.append(ck[-1] + a[i])
dp = [[0 for _ in range(n)] for j in range(n)]
if n % 2:
	z = n - 2
else:
	z = n - 1
for i in range(z, -1, -2):
	tot = a[i]
	for j in range(i - 1, -1, -2):
		dp[j][i] = 1
		tot -= a[j]
		if tot < 0:
			break
		if j:
			tot += a[j - 1]
ans = 0
for i in range(0, n, 2):
	tot = 0
	x = a[i]
	for j in range(i + 1, n, 2):
		tot += a[j - 1]
		if dp[i][j]:
			y = a[j]
			z = tot - x
			y -= z
			ans += max(0, min(x, y))
			if x != a[i]:
				ans += 1
			x -= max(0, min(x, y))
		tot -= a[j]
		if tot < 0:
			break
print(ans)


import sys, math
from collections import Counter, deque
input = sys.stdin.buffer.readline
n = int(input())
ar = list(map(int, input().split()))
n = n // 2 * 2
cc = 0
for i in range(0, n, 2):
	(l, r) = (ar[i], ar[i + 1])
	cc += min(l, r)
	if l < r:
		continue
	(a, b) = (l - r, 0)
	for j in range(i + 2, n):
		if j % 2 == 0:
			b += ar[j]
		else:
			mn = min(b, ar[j])
			(b, c) = (b - mn, ar[j] - mn)
			if b == 0:
				mn = min(a, c)
				cc += mn + 1
				(a, c) = (a - mn, c - mn)
				if c > 0:
					break
print(cc)


import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
n = int(input())
a = [int(i) for i in input().split()]
ans = 0
for i in range(0, n, 2):
	cnt = -1
	if i + 1 < n:
		ans += min(a[i], a[i + 1])
		cnt = a[i] - a[i + 1]
	if cnt >= 0:
		st = 0
		for j in range(i + 2, n):
			if j % 2:
				st -= a[j]
				if st <= 0:
					tst = -st
					x = min(tst, cnt)
					cnt -= x
					st += x
					ans += x + 1
				if st < 0:
					break
			else:
				st += a[j]
print(ans)


import os
import sys
from io import BytesIO, IOBase

def main():
	n = int(input())
	A = list(map(int, input().split()))
	r = 0
	ans = 0
	val = [0 for i in range(n + 1)]
	for i in range(n):
		if i % 2 == 0:
			r = r + A[i]
		else:
			x = 0
			for j in range(i - 1, -1, -1):
				if j % 2 == 0:
					if A[j] > A[i]:
						ans = ans + A[i]
						A[j] = A[j] - A[i]
						A[i] = 0
						x = j
						break
					else:
						if A[j] != 0:
							ans = ans + val[j - 1] + A[j]
						A[i] = A[i] - A[j]
						A[j] = 0
						x = j
						if A[i] == 0:
							break
			if A[i] == 0:
				if A[x] == 0:
					val[i] = 1 + val[x - 1]
				else:
					val[i] = 1
	print(ans)
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')
main()


n = int(input())
c = list(map(int, input().split()))
ans = 0
for j in range(0, n, 2):
	boo = c[j]
	remain = 0
	for i in range(j, n):
		if i % 2 == 0:
			remain += c[i]
		else:
			remain -= c[i]
			if remain <= boo:
				ans += boo - max(remain, 0)
				boo = min(remain + 1, boo)
			if remain < 0:
				break
print(ans)


n = int(input())
a = [int(i) for i in input().split()]
ans = 0
for i in range(0, n, 2):
	cnt = -1
	if i + 1 < n:
		ans += min(a[i], a[i + 1])
		cnt = a[i] - a[i + 1]
	if cnt >= 0:
		st = 0
		for j in range(i + 2, n):
			if j % 2:
				st -= a[j]
				if st <= 0:
					tst = -st
					x = min(tst, cnt)
					cnt -= x
					st += x
					ans += x + 1
				if st < 0:
					break
			else:
				st += a[j]
print(ans)


n = int(input())
c = list(map(int, input().split()))
n = len(c)
ans = 0
for i in range(0, n, 2):
	diff = 0
	a = 0
	for j in range(i + 1, n):
		if j % 2 == 0:
			diff += c[j]
			continue
		ans += min(max(0, c[j] - diff), c[i])
		if c[j] - diff >= 0:
			ans += a
		diff -= c[j]
		if c[i] + diff < 0:
			break
		if diff < 0:
			c[i] += diff
			a = 1
			diff = 0
print(ans)


n = int(input())
c = [int(x) for x in input().split()]
ans = 0
for i in range(0, n, 2):
	dt = 0
	s = 0
	for j in range(i + 1, n):
		if j & 1:
			L = max(1, -s)
			R = c[i]
			if L <= R:
				L += dt
				R += dt
				L = max(L, 1)
				R = min(R, c[j])
				ans += max(0, R - L + 1)
			dt -= c[j]
		else:
			dt += c[j]
		s = min(s, dt)
print(ans)


def doWork():
	n = int(input())
	a = map(int, input().split())
	balance = 0
	total = 0
	stack = []
	for (i, x) in enumerate(a):
		if i % 2 == 0:
			balance += x
			continue
		total += min(x, balance)
		balance = balance - x
		while stack and stack[-1][0] > balance:
			last = stack[-1]
			total += last[1]
			if last[0] == balance:
				last[1] += 1
				break
			else:
				stack.pop()
		if stack and stack[-1][0] == balance:
			total += stack[-1][1]
			stack[-1][1] += 1
		elif balance >= 0:
			stack.append([balance, 1])
		balance = max(0, balance)
	print(total)
doWork()


n = int(input())
a = [int(i) for i in input().split()]
less = 0
zhan = [[0, 1]]
high = 0
ans = 0
flag = True
for i in a:
	if flag:
		high += i
	else:
		ans += min(high - less, i)
		high -= i
		less = min(high, less)
		while len(zhan) != 0 and zhan[-1][0] > high:
			ans += zhan.pop()[1]
			if len(zhan) == 0:
				ans -= 1
		if len(zhan) == 0 or zhan[-1][0] < high:
			zhan.append([high, 1])
		else:
			ans += zhan[-1][1]
			zhan[-1][1] += 1
			if len(zhan) == 1:
				ans -= 1
	flag = not flag
print(ans)


from bisect import insort, bisect_right, bisect_left
from sys import stdout, stdin, setrecursionlimit
from heapq import heappush, heappop, heapify
from io import BytesIO, IOBase
from collections import *
from itertools import *
from random import *
from string import *
from queue import *
from math import *
from re import *
from os import *

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 = read(self._fd, max(fstat(self._fd).st_size, 8192))
			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 = read(self._fd, max(fstat(self._fd).st_size, 8192))
			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:
			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')
(stdin, stdout) = (IOWrapper(stdin), IOWrapper(stdout))
(graph, mod, szzz) = ({}, 10 ** 9 + 7, lambda : sorted(zzz()))

def getStr():
	return input()

def getInt():
	return int(input())

def listStr():
	return list(input())

def getStrs():
	return input().split()

def isInt(s):
	return '0' <= s[0] <= '9'

def input():
	return stdin.readline().strip()

def zzz():
	return [int(i) for i in input().split()]

def output(answer, end='\n'):
	stdout.write(str(answer) + end)

def lcd(xnum1, xnum2):
	return xnum1 * xnum2 // gcd(xnum1, xnum2)

def getPrimes(N=10 ** 5):
	SN = int(sqrt(N))
	sieve = [i for i in range(N + 1)]
	sieve[1] = 0
	for i in sieve:
		if i > SN:
			break
		if i == 0:
			continue
		for j in range(2 * i, N + 1, i):
			sieve[j] = 0
	prime = [i for i in range(N + 1) if sieve[i] != 0]
	return prime

def primeFactor(n, prime=getPrimes()):
	lst = []
	mx = int(sqrt(n)) + 1
	for i in prime:
		if i > mx:
			break
		while n % i == 0:
			lst.append(i)
			n //= i
	if n > 1:
		lst.append(n)
	return lst
dx = [-1, 1, 0, 0, 1, -1, 1, -1]
dy = [0, 0, 1, -1, 1, -1, -1, 1]
daysInMounth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
n = getInt()
ans = 0
arr = zzz()
for i in range(0, n, 2):
	prev = arr[i]
	cnt = prev - 1
	for j in range(i + 1, n):
		if j % 2:
			x = prev - arr[j]
			t = min(cnt, prev) - max(x, 0)
			if t >= 0:
				ans += t + 1
			prev = x
			if cnt > x:
				cnt = x
		else:
			prev += arr[j]
print(ans)


n = int(input())
a = [*map(int, input().split())]
t = 0
for l in range(0, n, 2):
	mn = 0
	c = 0
	for r in range(l + 1, n, 2):
		if r - l > 1:
			mn = min(mn, c - a[r - 2])
			c += a[r - 1] - a[r - 2]
		t += max(-1, min(a[l] + mn, a[r] + mn - c)) + (r - l > 1)
print(t)


N = int(input())
cs = list(map(int, input().split()))
stack = []
p = 0
min_height = 0
height = 0
answer = 0
for c in cs:
	if p == 0:
		height += c
	else:
		new_height = height - c
		while stack and stack[-1][0] > new_height:
			(h, cnt) = stack.pop()
			answer += cnt - 1
		if stack and stack[-1][0] == new_height:
			answer += stack[-1][1] - 1
		answer += height - max(min_height, new_height)
		if stack and stack[-1][0] == new_height:
			stack[-1] = (stack[-1][0], stack[-1][1] + 1)
		elif new_height < min_height:
			stack.append((new_height, 1))
		else:
			stack.append((new_height, 2))
		height = new_height
		min_height = min(min_height, height)
	p ^= 1
print(answer)


import random
import collections
import string
import math
import copy
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))

def input():
	return sys.stdin.readline().rstrip('\r\n')
mo = int(1000000000.0 + 7)

def exgcd(a, b):
	if not b:
		return (1, 0)
	(y, x) = exgcd(b, a % b)
	y -= a // b * x
	return (x, y)

def getinv(a, m):
	(x, y) = exgcd(a, m)
	return -1 if x == 1 else x % m

def comb(n, b):
	res = 1
	b = min(b, n - b)
	for i in range(b):
		res = res * (n - i) * getinv(i + 1, mo) % mo
	return res % mo

def quickpower(a, n):
	res = 1
	while n:
		if n & 1:
			res = res * a % mo
		n >>= 1
		a = a * a % mo
	return res

def dis(a, b):
	return abs(a[0] - b[0]) + abs(a[1] - b[1])

def getpref(x):
	if x > 1:
		return x * (x - 1) >> 1
	else:
		return 0

def orafli(upp):
	primes = []
	marked = [False for i in range(upp + 3)]
	prvs = [i for i in range(upp + 3)]
	for i in range(2, upp):
		if not marked[i]:
			primes.append(i)
		for j in primes:
			if i * j >= upp:
				break
			marked[i * j] = True
			prvs[i * j] = j
			if i % j == 0:
				break
	return (primes, prvs)

def lower_ord(c: str) -> int:
	return ord(c) - 97

def upper_ord(c: str) -> int:
	return ord(c) - 65

def read_list():
	return [int(i) for i in input().split()]

def read_int():
	s = input().split()
	if len(s) == 1:
		return int(s[0])
	else:
		return map(int, s)

def ask(s):
	print(f'? {s}', flush=True)

def answer(s):
	print(f'{s}', flush=True)
import functools
import itertools
from fractions import Fraction
import random

def lowbit(x):
	return -x & x

def pref(x):
	return x * (x + 1) >> 1

def cum(sx, mx):
	return (sx + mx) * (mx - sx + 1) >> 1

def solve():
	n = read_int()
	li = read_list()
	dp = {0: 1}
	ans = 0
	px = 0
	for (p, i) in enumerate(li):
		if p & 1:
			cl = 0
			ato = i
			pf = 0
			cr = 0
			for q in range(p - 1, -1, -1):
				if q & 1:
					cr += li[q]
				else:
					cr -= li[q]
					if cr <= 0:
						ans += min(ato, abs(cr)) + 1
						ato += cr
						cr = 0
					if ato < 0:
						break
			ans -= 1
	print(ans)
T = 1
for ti in range(T):
	solve()


n = int(input())
L = list(map(int, input().split()))
if len(L) % 2 != 0:
	L.pop()
n = len(L)
ret = 0
for i in range(n // 2):
	b = L[2 * i]
	curr = 0
	low = 1
	for j in range(2 * i, n):
		if j % 2 == 1:
			if curr - (b - (low - 1)) < L[j]:
				ret += min(L[j] - max(0, curr - (b - (low - 1))), b - (low - 1))
			curr -= L[j]
			low = max(low, b - curr)
			if curr < 0:
				break
		else:
			curr += L[j]
print(ret)


import sys
input = sys.stdin.readline
from heapq import *

def main():
	n = int(input())
	clst = list(map(int, input().split()))
	ans = 0
	min_ = 0
	hq = []
	now = 0
	for (i, c) in enumerate(clst):
		if i % 2 == 0:
			now += c
		else:
			pos = now - c
			if pos >= min_:
				ans += c
				add = 1
			else:
				ans += now - min_
				min_ = pos
				add = 0
			while hq:
				if pos <= -hq[0] < now:
					ans += 1
					if pos == -hq[0]:
						add += 1
					heappop(hq)
				else:
					break
			for _ in range(add):
				heappush(hq, -pos)
			now = pos
	print(ans)
for _ in range(1):
	main()


n = int(input())
s = list(map(int, input().split()))
h = 0
sign = 1
low = high = 0
t = {}
count = 0
for x in s:
	h += sign * x
	if sign == 1:
		high = h
	else:
		count += x
		for hh in t:
			if hh >= h and hh <= h + x:
				count += t[hh]
			elif hh > h:
				t[hh] = 0
		high = h
		if h < low:
			count += h - low
			low = h
		else:
			if not h in t:
				t[h] = 0
			t[h] += 1
		for hh in t:
			if hh > h:
				t[hh] = 0
	sign = -sign
print(count)


import os, sys
input = lambda : sys.stdin.buffer.readline().rstrip(b'\r\n')

def dbp(*args, **kwargs):
	print(*args, file=sys.stderr, **kwargs)
a_str = lambda : input().decode('ascii')
get_int_list = lambda : [int(x) for x in input().split()]
join_str = lambda l, ch=' ': ch.join((str(x) for x in l))

def do_thing():
	n = int(input())
	alist = get_int_list()

	def slow_solve():
		s = []
		for (idx, a) in enumerate(alist):
			if idx % 2:
				s.extend([')'] * a)
			else:
				s.extend(['('] * a)
		dbp(s)
		count = 0
		ls = len(s)
		for w in range(2, ls + 1, 2):
			for i in range(ls - w + 1):
				h = 0
				ok = True
				sub = s[i:i + w]
				for c in sub:
					h += 1 if c == '(' else -1
					if h < 0:
						ok = False
						break
				if ok and h == 0:
					count += 1
					dbp(i, i + w - 1, ''.join(sub), count)
		return count

	def regret_solve():
		basecount = strkcount = 0
		lefts = 0
		hstk = [[0, 0]]
		for i in range(0, n - 1, 2):
			(l, r) = alist[i:i + 2]
			lefts += l
			basecount += min(lefts, r)
			lefts -= r
			while hstk and hstk[-1][0] > lefts:
				(h, strk) = hstk.pop()
				strkcount += strk
			if hstk and hstk[-1][0] == lefts:
				strkcount += hstk[-1][1]
				hstk[-1][1] += 1
			elif hstk:
				hstk.append([lefts, 1])
			if lefts < 0:
				lefts = 0
			if not hstk:
				hstk = [[lefts, 0]]
		return basecount + strkcount
	r = regret_solve()
	return r
maxcc = 1
for cc in range(maxcc):
	print(do_thing())


import sys
input = sys.stdin.readline
INF = int(1000000000.0) + 7
dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]

def solve():
	n = int(input())
	data = list(map(int, input().split()))
	remain = []
	final = [0] * n
	dp = [0] * n
	for i in range(n):
		now = i % 2
		if now == 0:
			remain.append((i, data[i]))
		else:
			k = data[i]
			while k and remain:
				(idx, cost) = remain.pop()
				if cost < k:
					dp[i] += cost
					k -= cost
					if idx > 0:
						dp[i] += final[idx - 1]
				else:
					if k == cost:
						if idx > 0:
							final[i] = final[idx - 1] + 1
							dp[i] += final[idx - 1]
						else:
							final[i] = 1
					else:
						final[i] = 1
					dp[i] += k
					cost -= k
					k = 0
					if cost:
						remain.append((idx, cost))
	print(sum(dp))
t = 1
while t:
	t -= 1
	solve()


import sys

def main(N, A):
	ans = 0
	for i in range(0, N, 2):
		op = (0, A[i])
		bottom = A[i]
		e = A[i]
		for j in range(i + 1, N, 2):
			cl = (e - A[j], e)
			mx = min(op[1], cl[1], max(0, bottom))
			mn = max(op[0], cl[0], -min(0, bottom))
			now = mx - mn
			if now >= 0:
				if j > i + 1:
					now += 1
				ans += now
			bottom = min(bottom, e - A[j])
			e -= A[j]
			if j < N - 1:
				e += A[j + 1]
	print(ans)
input = sys.stdin.readline
N = int(input())
(*A,) = map(int, input().split())
main(N, A)


import collections
import math
import itertools
import sys
import time

def fast_input():
	return sys.stdin.readline().strip()
input = fast_input

def ints_input():
	return list((int(x) for x in input().split()))

def int_input():
	return int(input())

def solve():
	n = int_input()
	a = ints_input()
	max_open = [0 for i in range(n)]
	min_open = [0 for i in range(n)]
	invalid = [False for i in range(n)]
	count = 0
	for (i, a) in enumerate(a):
		if i % 2 == 0:
			max_open[i] = a
			min_open[i] = 1
		else:
			for j in range(i):
				if j % 2 == 0 and (not invalid[j]):
					if min_open[j] <= a:
						count += min(max_open[j], a) - min_open[j] + 1
		for j in range(i):
			if j % 2 == 0:
				if i % 2 == 0:
					max_open[j] += a
					min_open[j] += a
				else:
					max_open[j] -= a
					min_open[j] -= a
					min_open[j] = max(min_open[j], 0)
					if max_open[j] < 0:
						invalid[j] = True
	print(count)
if False:
	for _ in range(int_input()):
		solve()
else:
	solve()


n = int(input())
a = list(map(int, input().split()))
if n & 1:
	n -= 1
ans = 0
for i in range(0, n, 2):
	now = low = a[i] - a[i + 1]
	ans += min(a[i], a[i + 1])
	for j in range(i + 2, n, 2):
		now += a[j] - a[j + 1]
		if now >= 0:
			tmp = a[i] - now
			ans += max(min(low - now, tmp, a[j + 1]), -1) + 1
		else:
			tmp = a[j + 1] + now
			ans += max(min(low, a[i], tmp), -1) + 1
		low = min(low, now)
		if low < 0:
			break
print(ans)


from itertools import accumulate
n = int(input())
arr = [int(c) for c in input().split(' ')]
arr_par = [arr[i] if not i % 2 else -arr[i] for i in range(len(arr))]
acc = list(accumulate(arr_par))
ctr = 0

def d_inc(d, i):
	if i in d:
		d[i] += 1
	else:
		d[i] = 1

def summ(i):
	return i * (i + 1) / 2

def corr(i):
	return summ(i + 1) - (i + 1)

def handle(d, ths):
	ad = 0
	for k in d:
		if ths < k:
			if d[k]:
				ad += corr(d[k])
			d[k] = 0
	return ad
open_reg = 0
mn = 0
ths = 0
special = {}
for i in range(n):
	ths += arr_par[i]
	if not i % 2:
		open_reg += arr[i]
	else:
		if arr[i] <= open_reg:
			ctr += arr[i]
			open_reg -= arr[i]
		else:
			ctr += open_reg
			open_reg = 0
		corr_val = handle(special, ths)
		ctr += corr_val
		if ths < mn:
			mn = ths
		else:
			d_inc(special, ths)
ad = 0
d = special
for k in d:
	if d[k]:
		ad += corr(d[k] - 1)
	d[k] = 0
ctr += ad
print(int(ctr))


n = int(input())
A = list(map(int, input().split()))
if n % 2 == 1:
	A.pop()
Ans = 0
B = []
for i in range(1, n, 2):
	x = A[i] - A[i - 1]
	z = 0
	Ans += min(A[i], A[i - 1])
	if x >= 0:
		for j in range(i - 2, -1, -2):
			z += A[j]
			w = A[j - 1]
			W = min(z, w)
			z -= W
			w -= W
			if W != 0 and z == 0:
				Ans += 1
			Ans += min(w, x)
			x -= w
			if x < 0:
				break
print(Ans)


INF = 10 ** 18
import sys
import pprint

def input():
	return sys.stdin.readline().rstrip('\r\n')

def maps():
	return [int(i) for i in input().split()]
import logging
from logging import getLogger
logging.basicConfig(format='%(message)s', level=logging.WARNING)
logger = getLogger(__name__)
logger.setLevel(logging.INFO)

def debug(msg, *args):
	logger.info(f'{msg}={pprint.pformat(args)}')
(n,) = maps()
a = [*maps()]
ans = 0
for i in range(0, n, 2):
	(bal, minbal) = (0, 0)
	for j in range(i + 1, n):
		if j % 2:
			left = -minbal
			right = bal - minbal
			left = max(1, left)
			right = max(1, right)
			if left <= a[i] and right <= a[j]:
				ans += min(a[i] - left, a[j] - right) + 1
		if j % 2 == 0:
			bal += a[j]
		else:
			bal -= a[j]
		minbal = min(minbal, bal)
print(ans)


def solve():
	n = int(input())
	sequence = [int(x) for x in input().split()]
	count = 0
	for start in range(0, n, 2):
		start_buffer = sequence[start]
		balance = 0
		for end in range(start + 1, n):
			if end % 2 == 0:
				balance += sequence[end]
			else:
				balance -= sequence[end]
				if balance <= 0 and end - start > 1:
					count += 1
				if balance < 0:
					if start_buffer + balance >= 0:
						count += -balance
						start_buffer += balance
						balance = 0
					else:
						count += start_buffer
						break
	print(count)
solve()


import sys

def il():
	return list(map(int, sys.stdin.readline().strip().split()))

def ii():
	return int(sys.stdin.readline().strip())

def imi():
	return map(int, sys.stdin.readline().strip().split())
n = ii()
li = il()
s = 0
for i in range(0, n, 2):
	x = 0
	a = li[i]
	ma = 1
	for j in range(i + 1, n):
		if j % 2 == 0:
			x += li[j]
		else:
			x -= li[j]
			if x < 0:
				b = -x
				if b >= ma:
					s += min(a - ma + 1, b - ma + 1)
				ma = max(ma, b)
				if b > a:
					break
print(s)


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush, nsmallest
from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
from decimal import Decimal
mod = pow(10, 9) + 7

def data():
	return sys.stdin.readline().strip()

def out(var):
	sys.stdout.write(str(var) + '\n')

def outa(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def l():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
n = int(data())
arr = l()
(cnt, answer, temp) = ([[0, 0]], 0, 0)
for i in range(0, n - 1, 2):
	temp += arr[i] - arr[i + 1]
	answer += min(arr[i + 1], temp + arr[i + 1] - cnt[0][0])
	while cnt and cnt[-1][0] > temp:
		answer += cnt[-1][1]
		cnt.pop()
	if cnt and cnt[-1][0] == temp:
		answer += cnt[-1][1]
		cnt[-1][1] += 1
	elif not cnt:
		cnt.append([temp, 0])
	else:
		cnt.append([temp, 1])
out(answer)


import sys
import os
import math
from io import BytesIO, IOBase
from collections import defaultdict, Counter, deque
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')
for i in range(1):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	for j in range(0, n - 1, 2):
		init = a[j]
		left = 0
		carry = 0
		for k in range(j + 1, n):
			if k & 1:
				if left:
					if a[k] < left:
						left -= a[k]
					else:
						c = a[k] - left
						left = 0
						ans += carry
						if c <= init:
							init -= c
							ans += c
						else:
							ans += init
							break
				elif a[k] <= init:
					init -= a[k]
					ans += a[k]
					carry = 1
				else:
					ans += init
					break
			else:
				left += a[k]
	print(ans)


import sys
inputt = sys.stdin.readline
printt = sys.stdout.write
import math
import functools
from collections import deque
import heapq
from queue import PriorityQueue

def get():
	return inputt().split()

def getint():
	return int(inputt())

def getints():
	return map(int, inputt().split())
n = getint()
c = list(getints())
a = []
x = -1
for cc in c:
	a.append([x, cc])
	if x == -1:
		a.append([0, 0])
	x *= -1
while 1:
	changed = False
	ac = None
	aa = []
	for (i, xy) in enumerate(a):
		(x, y) = xy
		if x == -1:
			ac = i
			acc = y
			aa = []
		elif x == 0:
			aa.append(y)
		elif x == 1:
			if ac != None:
				m = min(acc, y)
				a[ac][1] -= m
				a[i][1] -= m
				k = len(aa)
				a[i - 1][1] = k * (k - 1) // 2 + m + sum(aa)
				for j in range(i - k, i - 1):
					a[j][1] = 0
				ac = None
				changed = True
	if not changed:
		break
	a = list(filter(lambda x: x[1] != 0, a))
cont = False
aaaa = []
aaab = 0
for x in a:
	if x[0] != 0 and cont:
		mm = len(aaaa)
		aaab += sum(aaaa) + mm * (mm - 1) // 2
		aaaa = []
		cont = False
	elif x[0] != 0:
		cont = False
	elif x[0] == 0:
		aaaa.append(x[1])
		cont = True
if len(aaaa):
	mm = len(aaaa)
	aaab += sum(aaaa) + mm * (mm - 1) // 2
print(aaab)


def sol():
	n = int(input())
	arr = [int(x) for x in input().split()]
	ans = 0
	for pocz in range(0, n, 2):
		nasze = arr[pocz]
		reszta = 0
		skonczone = 0
		wyn = 0
		for i in range(pocz, n):
			if not i % 2:
				if i != pocz:
					reszta += arr[i]
			else:
				my = arr[i]
				ile = min(reszta, my)
				my -= ile
				reszta -= ile
				if not reszta and ile and skonczone:
					wyn += 1
				if my and nasze:
					x = min(my, nasze)
					wyn += x
					my -= x
					nasze -= x
					skonczone += 1
				if my:
					break
		ans += wyn
	print(ans)
sol()


import sys
input = lambda : sys.stdin.readline().strip()

def solve():
	n = int(input())
	arr = list(map(int, input().split()))
	res = 0
	for i in range(0, n, 2):
		x = xmin = 0
		for j in range(i + 1, n):
			if j & 1:
				l = max(1, 1 - x, -xmin)
				r = min(arr[i], arr[j] - x)
				if l <= r:
					res += r - l + 1
				x -= arr[j]
				xmin = min(xmin, x)
				if arr[i] + xmin < 0:
					break
			else:
				x += arr[j]
	return res
print(solve())


from sys import stdin, stdout
ip = lambda : stdin.readline().rstrip('\r\n')
ips = lambda : ip().split()
out = lambda x, end='\n': stdout.write(str(x) + end)
n = int(ip())
a = list(map(int, ips()))
ans = 0
for i in range(n):
	(no, nc, p) = (0, 0, 0)
	if i & 1:
		continue
	x = a[i]
	if i + 1 < n:
		ans += min(a[i], a[i + 1])
		x -= min(a[i], a[i + 1])
	if i + 1 < n and a[i + 1] > a[i]:
		continue
	y = 0
	f = 0
	for j in range(i + 2, n):
		if j & 1 == 0:
			y += a[j]
		else:
			z = a[j]
			if y > z:
				y -= z
				continue
			else:
				z -= y
				y = 0
				ans += min(x, z)
				ans += 1
				if x >= z:
					x -= z
				else:
					f = 1
		if f:
			break
out(ans)