Codeforces Round #560 (Div. 3)F1&&F2 - Microtransactions【二分】

F2. Microtransactions (hard version)

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 nn types of microtransactions in the game. Each microtransaction costs 22 burles usually and 11 burle if it is on sale. Ivan has to order exactly kiki microtransactions of the ii-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 11 burle and otherwise he can buy it for 22 burles.

There are also mm special offers in the game shop. The jj-th offer (dj,tj)(dj,tj) means that microtransactions of the tjtj-th type are on sale during the djdj-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 nn and mm (1≤n,m≤2⋅1051≤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 nn integers k1,k2,…,knk1,k2,…,kn (0≤ki≤2⋅1050≤ki≤2⋅105), where kiki is the number of copies of microtransaction of the ii-th type Ivan has to order. It is guaranteed that sum of all kiki is not less than 11 and not greater than 2⋅1052⋅105.

The next mm lines contain special offers. The jj-th of these lines contains the jj-th special offer. It is given as a pair of integers (dj,tj)(dj,tj) (1≤dj≤2⋅105,1≤tj≤n1≤dj≤2⋅105,1≤tj≤n) and means that microtransactions of the tjtj-th type are on sale during the djdj-th day.

Output

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

Examples

input

Copy

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

output

Copy

8

input

Copy

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

output

Copy

20

 

 

分析:每张碟的价格都是一样的,那么普通价格的不管我在什么时候买都是一样的,所以可以默认全部都在最后一天买。

在考虑打折的碟。如果某张碟子打了多次折,那么我可以留到最后一次打折再买。

然后就可以二分了,check的时候从mid天往前倒着推,遇到打折的就直接买够,最后判断一下不打折的就可以了。

 

#include "bits/stdc++.h"

using namespace std;
int a[200004];
vector<int> b[400004];
int c[400004];
int n, m, sum;

bool che(int mid) {
    int num = sum;
    for (int i = 1; i <= n; ++i) {
        c[i] = a[i];
    }
    int left = mid;
    int x = 0;
    for (int i = mid; i >= 1 && left; --i) {
        for (int j = 0; j < b[i].size(); ++j) {
            if (!c[b[i][j]])continue;
            int temp = min(c[b[i][j]], left);
            c[b[i][j]] -= temp;
            num -= temp;
            left -= temp;
        }
        if (left > i - 1) {
            x += left - i + 1;
            left = i - 1;
        }
    }
    return x >= num * 2;
}

int main() {

    cin >> n >> m;
    for (int i = 1; i <= n; ++i) {
        scanf("%d", &a[i]);
        sum += a[i];
    }
    int d, t;
    for (int i = 0; i < m; ++i) {
        scanf("%d%d", &d, &t);
        b[d].push_back(t);
    }
    int l = 1, r = sum * 2;
    int ans = -1;
    while (l <= r) {
        int mid = (l + r) >> 1;
        if (che(mid)) {
            ans = mid;
            r = mid - 1;
        } else l = mid + 1;
    }
    cout << ans << endl;
}

 

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值