1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| #include<bits/stdc++.h> #define N 100010 #define ll long long
using namespace std;
int n,k,q,m,id[N],pos[N]; ll a[N],b[N];
struct node{ int pos; ll val; bool operator < (const node &x) const{ return val>x.val; } }c[N];
ll sum=0,S=0; bool vh[N]; void work() { scanf("%d%d",&n,&k); for(int i=1;i<=n;i++) scanf("%lld",&a[i]); for(int i=1;i<=n;i++) sum+=a[i]; for(int i=1;i<=n;i++) scanf("%lld",&b[i]); for(int i=1;i<=n;i++) { c[i].val=b[i]-a[i]; c[i].pos=i; } sort(c+1,c+n+1); for(int i=1;i<=k;i++) S+=c[i].val; for(int i=1;i<=n;i++) pos[c[i].pos]=i; scanf("%d",&q); for(int i=1;i<=q;i++) { scanf("%d",&m); for(int j=1;j<=m;j++) scanf("%d",&id[j]); int cnt=0; ll tS=S; for(int j=1;j<=m;j++) { if(pos[id[j]]<=k) { cnt++; tS-=c[pos[id[j]]].val; } vh[id[j]]=true; } int now=0; for(int j=k+1;j<=n;j++) { if(now==cnt) break; if(!vh[c[j].pos]) { now++; tS+=c[j].val; } } printf("%lld\n",sum+tS); for(int j=1;j<=m;j++) vh[id[j]]=false; } }
int main() { work(); return 0; }
|