题解:P10334 [UESTCPC 2024] 饮料
无解
容易发现前
即存在
求解
注意到第
正确性显然,时间复杂度
CODE
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, t[N], a[N], stk[N], tp;
long long ans;
int main() {
scanf("%d", &n);
bool as = 0;
for(int i = 1; i <= n; ++i) {
scanf("%d", t + i);
if(t[i] < i) as = 1;
}
for(int i = 1; i <= n; ++i) scanf("%d", a + i);
if(as) puts("-1");
else {
int now = t[n];
for(int i = n; i; --i) {
while(tp && now > t[i]) --now, ans += stk[tp--];
stk[tp] = max(stk[tp++], a[i]);
now = t[i];
}
while(tp) ans += stk[tp--];
printf("%lld", ans);
}
return 0;
}