Maximum Candies | Codechef
You are given a matrix with rows (numbered through ) and columns (numbered through ).
You need to fill the matrix with candies in such a way that:
- For each cell, the number of candies in this cell is an integer between and inclusive.
- For each pair of adjacent cells, the number of candies in one cell plus the number of candies in the other cell does not exceed . Two cells are considered adjacent if they share a side.
Find the maximum number of candies you can place in the given matrix.
Input
- The first line of the input contains a single integer denoting the number of test cases. The description of test cases follows.
- The first and only line of each test case contains four space-separated integers , , and .
Output
For each test case, print a single line containing one integer ― the maximum number of candies.
Constraints
Subtasks
Subtask #1 (100 points): original constraints
Example Input
2
4 4 4 6
3 4 4 5
Example Output
48
30
Solution(C++):
#include
#define ll long long int
using namespace std;
int main()
{
ll tc;
cin>>tc;
while(tc--)
{
ll n,m,x,y,sum=0;
cin>>n>>m>>x>>y;
if(n==1 && m==1)
sum+=x;
else if((n*m)%2==0)
{
if(y>=x && y-x<=x)
sum+=((n*m)/2)*(x+(y-x));
else if(y>=x && y-x>x)
sum+=((n*m)/2)*(x+x);
else
sum+=((n*m)/2)*y;
}
else if((n*m)%2!=0)
{
if(y>=x && y-x<=x)
sum+=(floor(((n*m)/2)+1)*x)+floor((n*m)/2)*(y-x);
else if(y>=x && y-x>x)
sum+=(floor(((n*m)/2)+1)*x)+floor((n*m)/2)*(x);
else
sum+=(floor(((n*m)/2)+1)*y);
}
cout<
}
return 0;
}
Comments
Post a Comment