forked from gaurav03kr/hello-hacktoberfest-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hi.cpp
58 lines (52 loc) · 1023 Bytes
/
hi.cpp
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
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define debug(x); cout<<#x<<" "<<x<<endl;
ll hell=1e9+7;
void solve()
{
ll n;
cin>>n;
vector<ll> v(n),one,zero;
for(ll i=0;i<n;i++){
cin>>v[i];
}
for(ll i=0;i<n;i++){
ll x;
cin>>x;
if(v[i]){
one.push_back(x);
}
else{
zero.push_back(x);
}
}
sort(one.begin(),one.end());
sort(zero.begin(),zero.end());
reverse(one.begin(),one.end());
reverse(zero.begin(),zero.end());
ll x=min(one.size(),zero.size());
ll ans=0;
for(ll i=0;i<x;i++){
ans+=2*(one[i]+zero[i]);
}
for(ll i=x;i<one.size();i++){
ans+=one[i];
}
for(ll i=x;i<zero.size();i++){
ans+=zero[i];
}
if(one.size()==zero.size()){
ans-=min(one[one.size()-1],zero[zero.size()-1]);
}
cout<<ans<<endl;
return ;
}
int main()
{
int t;
cin>>t;
while(t--)
solve();
return 0;
}