要点
- 二分答案,内部喜闻乐见的拖延策略:对于某个打折玩具,就选最晚的打折时间买,答案并不会变劣,只是购买时间的平移。
- 注意最晚时间不是预处理的东西,是二分内部的、在mid以内的最晚时间。
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn = 2e5 + 5;
int n, m, k[maxn], d[maxn], t[maxn];
int Late[maxn], sum;
bool ok(int mid) {
for (int i = 1; i <= n; i++) {
Late[i] = 0;
}
vector<int> day[maxn];
for (int i = 1; i <= m; i++) {
if (d[i] <= mid)
Late[t[i]] = max(Late[t[i]], d[i]);
}
for (int i = 1; i <= n; i++) {
if (k[i] && Late[i])
day[Late[i]].push_back(i);
}
int s = sum, cur = 0;
for (int i = 1; i <= mid; i++) {
cur++;
if (i < maxn && day[i].size()) {
for (int t : day[i]) {
if (cur >= k[t]) {
cur -= k[t];
s -= k[t];
} else {
s -= cur;
cur = 0;
break;
}
}
}
}
return cur >= s * 2;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
scanf("%d", &k[i]), sum += k[i];
for (int i = 1; i <= m; i++) {
scanf("%d%d", &d[i], &t[i]);
}
int l = 1, r = 4e5 + 5, ans;
while (l <= r) {
int mid = (l + r) >> 1;
if (ok(mid)) ans = mid, r = mid - 1;
else l = mid + 1;
}
return !printf("%d\n", ans);
}