思路:
(用较低的复杂度来维护删除当前最大值是关键),我们只需要在前i(i>=k)中选出k个b就行了。
那么我们从1到遍历过去,顺便维护当前的选出k个的最小值
Accode:
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e5+10;
int n,m,q,a[N],b[N],k,pre[N];
void solve() {
cin>>n>>q;
for(int i=1; i<=n; i++) {
cin>>a[i];
pre[i]=pre[i-1]+a[i];
}
for(int i=1; i<=n; i++) cin>>b[i];
while(q--) {
cin>>k;
priority_queue<int>st;
int res=0,ans=2e18;
for(int i=1; i<=n; i++) {
res+=b[i];
st.push(b[i]);
if(st.size()>k) {
res-=st.top();
st.pop();
}
if(st.size()==k) {
ans=min(ans,pre[i]+res);
}
}
cout<<ans<<"\n";
}
}
signed main() {
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
int tt=1;
// cin>>tt;
while(tt--) {
solve();
}
return 0;
}
over