click below for solution

Showing posts with label Codesheff starter 23. Show all posts
Showing posts with label Codesheff starter 23. Show all posts

Wednesday, 26 January 2022

Maximise the Minimum solution|Codesheff starter 23 for div 3 (Rated)

You are given two arrays A and B, both of length N.

You would like to choose exactly K distinct indices i1,i2,,iK such that min(Ai1+Ai2++AiK,Bi1+Bi2++BiK) is maximized. Find this maximum value.

Input Format

  • The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
  • Each test case consists of 3 lines of input.
    • The first line of each test case contains two space-separated integers N and K.
    • The second line contains N space-separated integers A1,A2,,AN denoting the elements of A.
    • The third line contains N space-separated integers B1,B2,,BN denoting the elements of B.

Output Format

For each test case, output a new line containing one integer — the required maximum value.

Constraints

  • 1T20
  • 2N40
  • 1KN
  • 1Ai,Bi40

Sample Input 1 

3
5 3
4 2 3 1 4
3 2 5 5 1
4 2
1 2 3 4
4 3 2 1
6 3
8 10 3 6 7 2
4 8 4 1 1 6

Sample Output 1 

9
5
18

Explanation

Test Case 1: One optimal choice of indices is i1=1,i2=3,i3=5. This gives A1+A3+A5=11 and B1+B3+B5=9.

Test Case 2: One optimal choice of indices is i1=1,i2=4.

Test Case 3: One optimal choice of indices is i1=1,i2=2,i3=6.

Solution:

#include <iostream>

using namespace std;

int main()

{

   int t;

   cin>>t;

   while(t--){

       int n,k,max=0;

       cin>>n>>k;

       int a[n],b[n];

       for(int i=1;i<=n;i++){

           cin>>a[i];         

       }

       for(int i=1;i<=n;i++){

           cin>>b[i];

       }

       int sum1=0,sum2=0;

       for(int i=1;i<=n;i++){

           for(int j=1;j<=n;j++){

               max=(a[i],b[i]);

               sum1+=a[i];

               sum2+=b[i];

           }  

       }

       if(sum1>sum2){

           cout<<sum1<<endl;

       }else{

           cout<<sum2<<endl;

       }

   }

    return 0;

}