CF1165F2.Microtransactions (hard version)
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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 ki 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 (dj,tj) means that microtransactions of the tj -th type are on sale during the dj -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.
输入格式
The first line of the input contains two integers n and m ( 1≤n,m≤2⋅105 ) — 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 k1,k2,…,kn ( 0≤ki≤2⋅105 ), where ki is the number of copies of microtransaction of the i -th type Ivan has to order. It is guaranteed that sum of all ki is not less than 1 and not greater than 2⋅105 .
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 (dj,tj) ( 1≤dj≤2⋅105,1≤tj≤n ) and means that microtransactions of the tj -th type are on sale during the dj -th day.
输出格式
Print one integer — the minimum day when Ivan can order all microtransactions he wants and actually start playing.
输入输出样例
输入#1
5 6 1 2 0 2 0 2 4 3 3 1 5 1 2 1 5 2 3
输出#1
8
输入#2
5 3 4 2 1 3 2 3 5 4 2 2 5
输出#2
20