Why it's giving me wrong ans? Maximum Candies

/* 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)
		{
			long n=sc.nextLong();
			long m=sc.nextLong();
			long x=sc.nextLong();
			long y=sc.nextLong();
			
			System.out.println(find(n,m,x,y));
		}
	}

	private static long find(long n, long m, long x, long y) {
		// TODO Auto-generated method stub
		long sum=0;
            if(n==1&&m==1)
		return x;
		if(2*x>y)
		{
			if(x>y)
				x=y;
		}
		else
			y=2*x-1;
			
		if(m%2==0)
		{
			long div=m/2;
			sum=div*y*n;
			return sum;
		}	
		if(n%2==0)
		{
			long div=n/2;
			sum=div*y*m;
			return sum;
		}
		
		//n and m are odd
		long div=m/2;
		sum=div*y*n;
		//System.out.println(sum);
		long divn=n/2;
		sum+=divn*y;
		sum+=x;
		
		return sum;
	}
}

Why this code is not working for maximum candies? What am I missing here?

Hey @sdfsdfsdf,

The case when 2x<=y, y should be equal to 2x and not 2*x-1.

1 Like

if my x is 4 and y is 10 the i can fill my matrix like
4 3 4 3
3 4 3 4
right? So how can we take y as 8?

i misunderstood the question
thanks for help!

Hey @sdfsdfsdf, I think you misread the question.
Since the sum of any pair of adjacent cells should not exceed y, the matrix would fill like
4 4 4 4
4 4 4 4
considering x = 4 and y = 10

Also in the edge case of n=m=1, the answer would be x even if it is greater than y, since it doesn’t have any adjacent cells.