题目
思路
显然,k个b类任务至少要完成k个a类任务。但我们可以完成k个以上的a类任务,从中选择b类任务来完成。也就是说,我们只需要在每次询问时维护一个前k小的b类任务时间就可以解决这个问题。
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
#define pll pair<ll,ll>
#define endl "\n"
const int Inf =1e18;
const int inf = 0x3f3f3f3f;
const int N = 2e5 + 10;
inline ll read();
int a[N],b[N];
int ti[N];
bool vis[N];
void solve() {
int n=read(),q=read();
for(int i=1;i<=n;i++){
a[i]=read();
}
for(int i=1;i<=n;i++){
b[i]=read();
}
while(q--){
int x=read();
priority_queue<int>que;
int now=0,ans=0,sum=0;
for(int i=1;i<=x;i++){
now+=a[i];
que.push(b[i]);
sum+=b[i];
}
ans=now+sum;
for(int i=x+1;i<=n;i++){
now+=a[i];
que.push(b[i]);
sum=sum+b[i]-que.top();
que.pop();
ans=min(ans,now+sum);
}
cout<<ans<<endl;
}
}
signed main() {
// ios::sync_with_stdio(false);
// cin.tie(nullptr),cout.tie(nullptr);
int tt = 1;
//tt=read();
while (tt--)
solve();
}
inline ll read() {
ll x = 0, ch = getchar(), f = 1;
while (!isdigit(ch)) {
if (ch == '-')
f = -f;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}