Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int main() {
- int t;
- cin>>t;
- while(t--)
- {
- int N,M;
- long long sum=0;
- cin>>N>>M;
- vector<int> batsman(N);
- vector<int> bowler(M);
- for(int i=0;i<N;i++)
- {
- cin>>batsman[i];
- }
- for(int i=0;i<M;i++){
- cin>>bowler[i];
- }
- if(N<4 || M<4 || (N+M)<11)
- {
- cout<<"-1"<<endl;
- continue;
- }
- sort(batsman.rbegin(),batsman.rend());
- sort(bowler.rbegin(),bowler.rend());
- for(int i=0;i<4;i++)
- {
- sum+=batsman[i];
- sum+=bowler[i];
- }
- int count=0,i=4,j=4;
- while(count<3)
- {
- if(i<N && j<M)
- {
- if(batsman[i]>bowler[j])
- {
- sum+=batsman[i];
- i++;
- }
- else
- {
- sum+=bowler[j];
- j++;
- }
- }
- else if(i<N)
- {
- sum+=batsman[i];
- i++;
- }
- else{
- sum+=bowler[j];
- j++;
- }
- count++;
- }
- cout<<sum<<endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement