Help me in solving MOONSOON problem ( what is task 1 in which my solution is failing)

My issue

My code

#include <bits/stdc++.h>

using namespace std;

int main() {
    int t; cin>>t;
    while(t--){
    int n, m, h; cin>>n>>m>>h;
 int a[n],b[m];
for (int i=0;i<n; ++i) cin>>a[i];
 sort(a,a+n,greater<int>());

for(int i=0;i<m;++i)cin>>b[i];
 sort (b,b+m,greater<int>());

int mx_index = max(n,m);
long long int res = 0; 
for (int i=0;i<mx_index; ++i) {
    if(i>=n || i>=m) continue;
res += min(h*b[i], a[i]);}
cout<<res<<endl;
       }
	// your code goes here
	return 0;
}

Problem Link: MOONSOON Problem - CodeChef

replace this line res += min(h*b[i], a[i]);
with this res += min(1ll * h *b[i], a[i]);

1 Like

also declare array a[n],b[n] with long long data type

what replace line you have written i am not able to get it please explain

when you will multiply h*b[i] this can exceed to intiger . whenever it will go beyond limit it will automatically converted into negative value and … your minum function will store that negative value in your result then you will get wrong answer.

thats why i said before multiplying use 1ll that means you converted first into long long then you are multiplying … so it will not overflow . and you will get correct answer.

#include <bits/stdc++.h>
using namespace std;

int main() {
    int t; cin>>t;
    while(t--){
    int n, m, h; cin>>n>>m>>h;
 int a[n],b[m];
for (int i=0;i<n; ++i) cin>>a[i];
 sort(a,a+n,greater<int>());

for(int i=0;i<m;++i)cin>>b[i];
 sort (b,b+m,greater<int>());

int mx_index = max(n,m);
long long int res = 0; 
for (int i=0;i<mx_index; ++i) {
    if(i>=n || i>=m) continue;
res += min(1ll*h*b[i], 1ll*a[i]);
    
}
cout<<res<<endl;
       }
}
	```
it will work

btw i did the same mistake , again & again i was getting 1 testcase wrong .

1 Like

Thanks for your help