Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define ll long long
- ll maximum(ll x, ll y){
- if (x>y) return x;
- else return y;
- }
- int main()
- {
- ll t;
- cin>>t;
- while(t--){
- ll n;
- cin>>n;
- ll a[n],b[n];
- vector <int> v1,v2;
- for (ll i=0; i<n; i++) cin>>a[i];
- for (ll i=0; i<n; i++){
- cin>>b[i];
- if (a[i]==0) v1.push_back(b[i]);
- else v2.push_back(b[i]);
- }
- sort(v1.begin(),v1.end());
- sort(v2.begin(),v2.end());
- ll p=accumulate(v1.begin(),v1.end(),0), q=accumulate(v2.begin(),v2.end(),0);
- if (v1.size()==0 || v2.size()==0){
- if (v1.size()==0) cout<<q<<"\n";
- else cout<<p<<"\n";
- }
- else {
- if (v1.size()==v2.size()){
- ll r=maximum(2*(p+q)-v1[0],2*(p+q)-v2[0]);
- cout<<r<<"\n";
- }
- else {
- ll k=min(v1.size(),v2.size());
- sort(v1.begin(),v1.end(),greater<ll>());
- sort(v2.begin(),v2.end(),greater<ll>());
- ll sum=0;
- for (int i=0; i<k; i++){
- sum+=v1[i]+v2[i];
- }
- cout<<p+q+sum<<"\n";
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement