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 java.io.*;
  import java.util.*;
  public class Solution {
  	static class SegmentTree { /* Author: Po-Chun Chiu */
  		int size;
  		private long[] max;
  		private long[] min;
   
   
  		public SegmentTree(long[] array){
  			size = array.length;
  			max = new long[array.length*4];
  			min = new long[array.length*4];
  			buildTree(array, 1, 0, size - 1);
  		}
   
  		private void buildTree(long[] array, int position, int front, int end){
  			//Basis case for returning the elements itself
  			if(front == end){
  				max[position] = array[front];
  				min[position] = array[front];
  				return;
  			}
  			//Building trees by dividing into two subtrees
  			buildTree(array, position * 2, front, (front+end)/2);
  			buildTree(array, position * 2 + 1, (front+end)/2 + 1, end);
   
  			recalc(position, front, end);
  		}
   
  		private void recalc(int position, int front, int end) {
  			max[position] = Math.max(getMax(position * 2), getMax(position * 2 + 1));
  			min[position] = Math.min(getMin(position * 2), getMin(position * 2 + 1));
  		}
   
  		private long getMax(int position){
  			return max[position] ;
  		}
   
  		private long getMin(int position){
  			return min[position] ;
  		}
   
   
  		public long queryMax(int queryFront, int queryEnd) {
  			return queryMax(1, 0, size - 1, queryFront, queryEnd);
  		}
   
  		private long queryMax(int position, int front, int end, int queryFront, int queryEnd){
  			//Case for entirely inclusive
  			if(front >= queryFront && queryEnd >= end){
  				return getMax(position);
  			}
  			//Case for entirely exclusive
  			if(end < queryFront || queryEnd < front){
  				return Long.MIN_VALUE;
  			}
  			long leftAns = queryMax(position*2, front,(front+end)/2,queryFront,queryEnd);
  			long rightAns = queryMax(position*2+1, (front+end)/2+1,end,queryFront,queryEnd);
  			return Math.max(leftAns, rightAns);
  		}
   
  		public long queryMin(int queryFront, int queryEnd) {
  			return queryMin(1,0,size-1,queryFront,queryEnd);
  		}
   
  		private long queryMin(int position,int front,int end, int queryFront, int queryEnd){
  			//Case for entirely inclusive
  			if(front>=queryFront&&queryEnd>=end){
  				return getMin(position);
  			}
  			//Case for entirely exclusive
  			if(end<queryFront||queryEnd<front){
  				return Long.MAX_VALUE;
  			}
  			long leftAns=queryMin(position*2,front,(front+end)/2,queryFront,queryEnd);
  			long rightAns=queryMin(position*2+1,(front+end)/2+1,end,queryFront,queryEnd);
  			return Math.min(leftAns, rightAns);
  		}
  	}
  	public static void main(String[] args) throws IOException 
  	{ 
  		FastScanner f= new FastScanner(); 
  		int ttt=1;
  //		ttt=f.nextInt();
  		PrintWriter out=new PrintWriter(System.out);
  		outer: for(int tt=0;tt<ttt;tt++) {
  			int n=f.nextInt();
  			int[] l=f.readArray(n);
  			long ans=0;
  			long[] sum=new long[n+1];
  			for(int i=1;i<=n;i++) {
  				if(i%2==0) l[i-1]=-l[i-1];
  				sum[i]=sum[i-1]+l[i-1];
  			}
  //			System.out.println(Arrays.toString(sum));
  			SegmentTree tree=new SegmentTree(sum);
  			for(int i=1;i<n;i+=2) {
  				for(int j=i+1;j<n;j+=2) {
  					long sumf=sum[j+1]-sum[i];
  					long min=tree.queryMin(i+1, j+1)-sum[i];
  //					System.out.println(min);
  					if(sumf>=0) {
  						if(j+1<n && i-1>-1) {
  							long left=l[i-1];
  							long right=-l[j+1];
  //							System.out.println("left " +left+" right "+right);
  							right-=sumf;					
  							if(right>=0) {
  								if(min<0) {
  									left+=min;
  									right+=min;
  									if(left>=0 && right>=0) {
  										ans+=Math.min(left, right)+1;
  									}
  								}
  								else {
  									ans+=Math.min(left, right)+1;
  								}
  							}
  						}
  					}
  					else {
  						if(j+1<n && i-1>-1) {
  							long left=l[i-1];
  							long right=-l[j+1];
  //							System.out.println("left " +left+" right "+right);
  							left+=sumf;
  							min-=sumf;
  							if(left>=0) {
  								if(min<0) {
  									left+=min;
  									right+=min;
  									if(left>=0 && right>=0) {
  										ans+=Math.min(left, right)+1;
  									}
  								}
  								else {
  									ans+=Math.min(left, right)+1;
  								}
  							}
  						}
  					}
  //					System.out.println("i "+i+" j "+j+" ans "+ans+" sum"+sumf+" min"+min);
  				}
  			}
  			for(int i=0;i<n;i+=2) {
  				if(i+1<n) ans+=Math.min(l[i], -l[i+1]);
  			}
  			System.out.println(ans);
  		}
  		out.close();
  	} 
  	static void sort(int[] p) {
          ArrayList<Integer> q = new ArrayList<>();
          for (int i: p) q.add( i);
          Collections.sort(q);
          for (int i = 0; i < p.length; i++) p[i] = q.get(i);
      }
  	static class FastScanner {
  		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  		StringTokenizer st=new StringTokenizer("");
  		String next() {
  			while (!st.hasMoreTokens())
  				try {
  					st=new StringTokenizer(br.readLine());
  				} catch (IOException e) {
  					e.printStackTrace();
  				}
  			return st.nextToken();
  		}
  		
  		int nextInt() {
  			return Integer.parseInt(next());
  		}
  		int[] readArray(int n) {
  			int[] a=new int[n];
  			for (int i=0; i<n; i++) a[i]=nextInt();
  			return a;
  		}
  		long nextLong() {
  			return Long.parseLong(next());
  		}
  		double nextDouble() {
  			return Double.parseDouble(next());
  		}
  		long[] readLongArray(int n) {
  			long[] a=new long[n];
  			for (int i=0; i<n; i++) a[i]=nextLong();
  			return a;
  		}
  	}
  }