Maximum Candies | Codechef

You are given a matrix A with N rows (numbered 1 through N) and M columns (numbered 1 through M).
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 0 and X 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 Y. 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 T denoting the number of test cases. The description of T test cases follows.
  • The first and only line of each test case contains four space-separated integers NMX and Y.

Output

For each test case, print a single line containing one integer ― the maximum number of candies.

Constraints

  • 1T100
  • 1N,M100
  • 1X,Y104

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

Popular posts from this blog

Boxes through a Tunnel | HackerRank

Array Manipulation | HackerRank

String Validators | HackerRank