Feedback for MOONSOON problem

Problem Link: MOONSOON Problem - CodeChef
include
include
include
include
using namespace std;

int main() {
// your code goes here
int t;
cin>>t;
while(t–){
int n,m,h;
cin>>n>>m>>h;
vector cap(n);
for(int i=0;i<n;++i)cin>>cap[i];
sort(cap.begin(),cap.end());
vector p(m);
for(int i=0;i<m;++i)cin>>p[i];
sort(p.begin(),p.end());
int i=n-1,j=m-1;
long long ans=0;
while(i>=0 && j>=0){
long long power=h*p[j];
ans+=min((long long)cap[i],power);
–i;
–j;
}
cout<<ans<<endl;
}
return 0;
}
my code was similar to other’s solution, but still giving wrong answer in one tc. How the hell is background algorithm of codechef work??

Feedback

1 Like

Same error it does not work for 1TC.Cannot understand where it goes wrong.Can anyone help please.
/* package codechef; // don’t place package name! */

import java.util.;
import java.lang.
;
import java.io.*;

/* Name of the class has to be “Main” only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t–>0)
{
int n_e=sc.nextInt();
int n_otl=sc.nextInt();
int hr=sc.nextInt();

	    int[] e=new int[n_e];
	    for(int i=0;i<n_e;i++)
	        e[i]=sc.nextInt();
	    int[] otl=new int[n_otl];
	   for(int i=0;i<n_otl;i++)
	        otl[i]=sc.nextInt();
	    
	    Arrays.sort(e);
	    Arrays.sort(otl);
	    
	    int j=n_e-1;
	    long s=0;
	    for(int i=otl.length-1;i>=0;i--)
	    {
	        long store=0;
	        long cur=otl[i]*hr;
	        
	        if(cur>=e[j])
	            store=e[j];
	        else
	            {store=cur ; }
	       j--;
	       s=s+store;
	       if(j<0)
	        break;
	    }
	    System.out.println(s);
	}
}

}

Your code has one line where it produces a wrong number. It’s this line:

Although you declare “cur” to be a long, after the equal sign there are two integer values being multiplied. For some odd reason you have to tell the compiler, that also the number behind the “=” sign to be a long number. There are two possible ways to do this: Either

long cur = (long)otl[i]*hr;

or

long cur = 1l*otl[i]*hr;

The 1l or 1L at the beginning tells the compiler, that it must allocate a long space for the result.
I’ve tried your code like this and now it works.
The original poster uses C++ which I don’t know enough, but might have a similar solution.

1 Like