description: <image>
  
  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, ..., 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 ≤ 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 ≤ n ≤ 1000), the size of the compressed sequence.
  
  The second line contains a sequence of integers c_1, c_2, ..., c_n (1 ≤ c_i ≤ 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:
  
    1. Subsequence from the 3rd to 10th character: (()(()))
    2. Subsequence from the 4th to 5th character: ()
    3. Subsequence from the 4th to 9th character: ()(())
    4. Subsequence from the 6th to 9th character: (())
    5. Subsequence from the 7th to 8th character: ()
  
  
  
  In the second example a sequence ()))(()(()))) is described.
  
  In the third example a sequence ()()(()) is described.
solution: import heapq,math
  from collections import defaultdict,deque
  from os import getcwd
  from itertools import permutations
  #from functools import cmp_to_key
  
  import sys, os.path
  #sys.setrecursionlimit(10000000)
  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])
  
  
  
  