The only difference between easy and hard versions is constraints.

Ivan plays a computer game that contains some microtransactions to make characters look cooler. Since Ivan wants his character to be really cool, he wants to use some of these microtransactions — and he won't start playing until he gets all of them.

Each day (during the morning) Ivan earns exactly one burle.

There are n types of microtransactions in the game. Each microtransaction costs 2 burles usually and 1 burle if it is on sale. Ivan has to order exactly k_i microtransactions of the i-th type (he orders microtransactions during the evening).

Ivan can order any (possibly zero) number of microtransactions of any types during any day (of course, if he has enough money to do it). If the microtransaction he wants to order is on sale then he can buy it for 1 burle and otherwise he can buy it for 2 burles.

There are also m special offers in the game shop. The j-th offer (d_j, t_j) means that microtransactions of the t_j-th type are on sale during the d_j-th day.

Ivan wants to order all microtransactions as soon as possible. Your task is to calculate the minimum day when he can buy all microtransactions he want and actually start playing.

Input

The first line of the input contains two integers n and m (1 ≤ n, m ≤ 1000) — the number of types of microtransactions and the number of special offers in the game shop.

The second line of the input contains n integers k_1, k_2, ..., k_n (0 ≤ k_i ≤ 1000), where k_i is the number of copies of microtransaction of the i-th type Ivan has to order. It is guaranteed that sum of all k_i is not less than 1 and not greater than 1000.

The next m lines contain special offers. The j-th of these lines contains the j-th special offer. It is given as a pair of integers (d_j, t_j) (1 ≤ d_j ≤ 1000, 1 ≤ t_j ≤ n) and means that microtransactions of the t_j-th type are on sale during the d_j-th day.

Output

Print one integer — the minimum day when Ivan can order all microtransactions he wants and actually start playing.

Examples

Input


5 6
1 2 0 2 0
2 4
3 3
1 5
1 2
1 5
2 3


Output


8


Input


5 3
4 2 1 3 2
3 5
4 2
2 5


Output


20

import java.awt.Point;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.Map.Entry;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
import java.util.Set;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;


public class N560 {
	static PrintWriter out;
	static Scanner sc;
	static int[][] q;
	static ArrayList<Integer>w,primes;
	static ArrayList<Integer>adj[];
	static HashSet<Integer>primesH;
	static boolean prime[];
	//static ArrayList<Integer>a;
	static HashSet<Long>h,tmp;
	static boolean[]v;
	static int[]a,b,c,d;
	static int dp[][][];
	static int A,B,n,m,k,mt[][];
	static int oo=Integer.MIN_VALUE;
	static String s,tt;
	public static void main(String[]args) throws IOException {
		sc=new Scanner(System.in);
		out=new PrintWriter(System.out);
		//A();
    	//B();
		//primes=new ArrayList<Integer>();
		//primesH=new HashSet<Integer>();
	//	sieveOfEratosthenes(50);
		//C();
		//D();
		//E();
		//minimum for subarrays of fixed length
		F();
		out.close();
	}
	  private static void A() throws IOException {
		  int t=1;
			while(t-->0) {
				int n=ni(),x=ni(),y=ni();
				String s=ns();
				int cnt=0;
				for(int i=n-1;i>=n-x;i--) {
					if(i==n-y-1) {
						if(s.charAt(i)=='0')cnt++;
					}else {
						if(s.charAt(i)=='1')cnt++;
					}
				}
				ol(cnt);
			}
		
	   }
		static void B() throws IOException {
			int t=1;
			while(t-->0) {
					int n=ni();
					Integer [] a=new Integer[n];
					for(int i=0;i<n;i++) {
						a[i]=ni();
					}
					TreeMap<Integer,Integer>tr=new TreeMap<Integer,Integer>();
					for(int i=0;i<n;i++) {
						tr.put(a[i],tr.getOrDefault(a[i], 0)+1);
					}
					ArrayList<Integer>x=new ArrayList<Integer>();
					for(Integer y:tr.keySet())x.add(y);
					Collections.sort(x);
					int cnt=0,cur=0;
					for(int i=0;i<x.size();i++) {
						if(x.get(i)<i)break;
						int tk=Math.min(tr.get(x.get(i)),x.get(i)-cur );
						cur+=tk;
					}
					ol(cur);
					
			}
		}
	  static void C() throws IOException {
		  int t=1;
		  while(t-->0) {
			   int n=ni();
			   String s=ns();
			   int tk=0;
			   boolean rem[]=new boolean[n];
			   for(int i=0;i<n;) {
				   int cnt=0;
				   char cur=s.charAt(i);
				   int x=i;
				   while(i<n&&cur==s.charAt(i)) {
					   rem[i]=true;
					   i++;
					   cnt++;
					   
				   }
				   int c=1;
				  
				   rem[x]=false;
				   if(cnt>=2&&tk%2==1) {
					   rem[x+1]=false;tk++;
				   }
				   tk++;
				  // i--;
				   
			   }
			   if(tk%2==1)tk--;
			   StringBuilder sb=new StringBuilder();
			   for(int i=0,c=0;i<n&&c<tk;i++) {
				   if(!rem[i]) {
					   c++;
					   sb.append(s.charAt(i));
				   }
			   }
			   ol(n-tk);
			   ol(sb.toString());
		  }
		  
		}



	static void D() throws IOException {
		int t=ni();
		a1:while(t-->0) {
			int n=ni();
			long l[]=nal(n);
			Arrays.sort(l);
			ArrayList<Long>ans=new ArrayList<Long>();
			long num=l[0]*l[n-1]*1l;
			for(long i=2;i*i<=num;i++) {
				if(num%i==0) {
					ans.add(i);
					if(i*i*1l!=num) {
						ans.add(num/i);
					}
				}
			}
			Collections.sort(ans);
			if(ans.size()!=n) {
				ol(-1);
				continue a1;
			}
			for(int i=0;i<n;i++) {
				if(l[i]!=ans.get(i)) {
					ol(-1);
					continue a1;
				}
			}
			ol(""+num);
		}
		
	}
	private static long pow(Integer x, Integer p) {
		if(p==0)return 1l;
		long sq=pow(x,p/2);
		sq*=sq;
		if(p%2==1)sq*=x*1l;
		return sq;
	}
	private static int sum(char[] ss) {
		int sum=0;
		for(int i=0;i<ss.length;i++) {
			sum+=(ss[i]-'0');
		}
		return sum;
	}
	static long gcd (long a, long b) {
	    return b==0?a:gcd (b, a % b);
	}
	static long lcm (long a, long b) {
	    return a / gcd(a, b) * b;
	}
	static void E() throws IOException {
		int t=1;
		while(t-->0) {
			int n=ni();
			Long a[]=new Long[n];
			Long b[]=new Long[n];
			for(int i=0;i<n;i++)a[i]=nl();
			for(int i=0;i<n;i++)b[i]=nl();
			//Arrays.sort(a);
			Arrays.sort(b);
			
			long oo=998244353l;
			long sum=0;
			for(int i=0,j=n-1;i<=j;i++,j--) {
				long cur=((long)(i+1)*(long)(n-i));
				a[i]=a[i]*cur*1l;
				if(i!=j) {
					a[j]=a[j]*cur*1l;
				}
			}
			Arrays.sort(a);
			for(int i=0;i<n;i++) {
				a[i]%=oo;
				long cur=(a[i]*b[n-i-1])%oo;
				sum=(sum%oo+cur)%oo;
			}
			ol(""+sum);
		}
	}
	static final Random random=new Random();
	static void ruffleSort(int[] a) {
		int n=a.length;//shuffle, then sort 
		for (int i=0; i<n; i++) {
			int oi=random.nextInt(n), temp=a[oi];
			a[oi]=a[i]; a[i]=temp;
		}
		Arrays.sort(a);
	}
	static void F() throws IOException{
		int t=1;
		while(t-->0) {
			n=ni();
			m=ni();
			a=nai(n);
			q=new int[m][2];
			for(int i=0;i<m;i++) {
				q[i][0]=ni()-1;
				q[i][1]=ni()-1;
			}
			int lo=0,hi=(int)4e5;
			while(lo<hi) {
				int mid=(lo+hi)/2;
				if(suff(mid)) {
					hi=mid;
				}else{
					lo=mid+1;
				}
			}
//			for(int i=lo;i<=hi;i++) {
//				if(suff(i)) {
//					ol(i+1);return;
//				}
//			}
			ol(lo+1);
		}
		
	}

	private static boolean suff(int days) {
		int[]nd=new int[n];
		for(int i=0;i<n;i++)nd[i]=a[i];
		int[]lst=new int[n];
		Arrays.fill(lst, -1);
		ArrayList<Integer>idx[]=new ArrayList[(int)2e5 +1];
		for(int i=0;i<idx.length;i++)idx[i]=new ArrayList<Integer>();
		for(int []x:q) {
			if(x[0]<=days)
				lst[x[1]]=Math.max(lst[x[1]], x[0]);
		}
		for(int i=0;i<n;i++)if(lst[i]!=-1)idx[lst[i]].add(i);
		int cnt=0;
		for(int i=0;i<=days;i++) {
			cnt++;
			if(i>(int)2e5)continue;
			for(int id:idx[i]) {
				if(cnt>=nd[id]) {
					cnt-=nd[id];nd[id]=0;
				}else {
					nd[id]-=cnt;
					cnt=0;
					break;
				}
			}
		}
		int sum=0;for(int i=0;i<n;i++)sum+=nd[i];
		return sum*2<=cnt;
	}

	
	static void G() throws IOException{
		int t=1;
		while(t-->0) {

			
			
			
		}
		
	}
	private static void disp(int[] revl) {
		for(int i=0;i<revl.length;i++) {
			out.print(revl[i]+" ");
		}
		out.println();
	}

	static int ni() throws IOException {
		return sc.nextInt();
	}
	static double nd() throws IOException {
		return sc.nextDouble();
	}
	static long nl() throws IOException {
		return  sc.nextLong();
	}
	static String ns() throws IOException {
		return sc.next();
	}
	static int[] nai(int n) throws IOException {
		int[] a = new int[n];
		for (int i = 0; i < n; i++)
			a[i] = sc.nextInt();
		return a;
	}
	static long[] nal(int n) throws IOException {
		long[] a = new long[n];
		for (int i = 0; i < n; i++)
			a[i] = sc.nextLong();
		return a;
	}
	static int[][] nmi(int n,int m) throws IOException{
		int[][]a=new int[n][m];
		for(int i=0;i<n;i++) {
			for(int j=0;j<m;j++) {
				a[i][j]=sc.nextInt();
			}
		}
		return a;
	}

	static long[][] nml(int n,int m) throws IOException{
		long[][]a=new long[n][m];
		for(int i=0;i<n;i++) {
			for(int j=0;j<m;j++) {
				a[i][j]=sc.nextLong();
			}
		}
		return a;
	}
	static void o(String x) {
		out.print(x);
	}
	static void ol(String x) {
		out.println(x);
	}
	static void ol(int x) {
		out.println(x);
	}
	static void disp1(int []a) {
		for(int i=0;i<a.length;i++) {
			out.print(a[i]+" ");
		}
		out.println();
	}

	static class Scanner 
	{
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream s){	br = new BufferedReader(new InputStreamReader(s));}

		public String next() throws IOException 
		{
			while (st == null || !st.hasMoreTokens()) 
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}
		public boolean hasNext() {return st.hasMoreTokens();}
		public int nextInt() throws IOException {return Integer.parseInt(next());}
		
		public double nextDouble() throws IOException {return Double.parseDouble(next());}
		
		public long nextLong() throws IOException {return Long.parseLong(next());}

		public String nextLine() throws IOException {return br.readLine();}
			
		public boolean ready() throws IOException {return br.ready(); }
		

	}
}
