College Life 4 - CodeChef Solution in Java | AskTheCode

CodeChef March Long Challenge Solution | College Life 4 solution in Java | AskTheCode


Chef and N−1 more of his friends go to the night canteen. The canteen serves only three items (well, they serve more, but only these three are edible!), which are omelette, chocolate milkshake, and chocolate cake. Their prices are A, B and C


However, the canteen is about to run out of some ingredients. In particular, they only have E

eggs and H chocolate bars left. They need:

  • 2 eggs to make an omelette

  • 3 chocolate bars for a chocolate milkshake

  • 1 egg and 1 chocolate bar for a chocolate cake

Each of the N friends wants to order one item. They can only place an order if the canteen has enough ingredients to prepare all the ordered items. Find the smallest possible total price they have to pay or determine that it is impossible to prepare N items.


  • 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 six space-separated integers N, E, H, A, B and C.


For each test case, print a single line containing one integer ― the minimum cost of N items, or −1 if it is impossible to prepare N items.

Example Input:

3 5 4 4 2 2 2 4 5 5 1 2 3 4 5 5 3 2 1

Example Output:

-1 7 4


Example case 1: The maximum number of items that can be produced using 4 eggs and 4 chocolates is 4, so the answer is −1.

Example case 2: In the optimal solution, the friends should order 2

omelettes, 1 chocolate milkshake and 1 chocolate cake, with cost 1⋅2+2⋅1+3⋅1=7.

Example case 3: In the optimal solution, the friends should order 4 chocolate cakes, with cost 1⋅4=4.


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

/* Name of the class has to be "Main" only if the class is public. */
class Codechef{
	public static long solve(long n, long e, long h, long a, long  b, long c){
        //long ans =  Integer.MAX_VALUE;
        long ans = (long)1e15;
            return 0;
        if(n<=e && n<=h){
    		ans = Math.min(ans,n*c);
    	if(2*n <= e){
        	ans = Math.min(ans,n*a);
    	if(3*n <= h ){
        	ans  = Math.min(ans, n*b);
    	if((h-n)/2 >= 1 && (h-n)/2 >= n-e){
            	long x = Math.min(n-1,(h-n)/2);
            	ans  = Math.min(ans,(b-c)*x+n*c);
           		long x = Math.max(1,(n-e));
            	ans = Math.min(ans,(b-c)*x+n*c);

    	if((e-n)>=1 && (e-n)>=n-h){
            	long x = Math.min(n-1,(e-n));
            	ans = Math.min(ans,(a-c)*x+n*c);
            	long x = Math.max(1,(n-h));
            	ans = Math.min(ans,(a-c)*x+n*c);

    	if(e/2 >= 1 && e/2>=(3*n-h+2)/3){
            	long x = Math.min(n-1,(e/2));
            	ans = Math.min(ans,(a-b)*x+n*b);
            	long x = Math.max(1,((3*n-h+2)/3));
            	ans = Math.min(ans,(a-b)*x+n*b);

    	if(e>=3 && h>=4 && n>=3){
        	ans = Math.min(ans,a+b+c+solve(n-3,e-3,h-4,a,b,c));
    	return ans;

	public static void main (String[] args) throws java.lang.Exception{
		Scanner  s = new Scanner(;
		int t = s.nextInt();
		    long n = s.nextLong();
		    long e = s.nextLong();
		    long h = s.nextLong();
		    long a = s.nextLong();
		    long b = s.nextLong();
		    long c = s.nextLong();
		    long result = solve(n,e,h,a,b,c);
		    if(result ==  (long)1e15){
22 views0 comments

Recent Posts

See All

Golf CodeChef Solution in Java - AskTheCode

CodeChef May Long Challenge Solution | Golf (LKDNGOLF) solution | AskTheCode Golf (LKDNGOLF) solution Problem: It's a lockdown. You’re bored in your house and are playing golf in the hallway. The hall

Solubility - CodeChef Solution in Java and C++| AskTheCode

CodeChef May Long Challenge Solution | Solubility (SOLBLTY) solution | AskTheCode Solubility (SOLBLTY) solution Problem: Suppose for a unit rise in temperature, the solubility of sugar in water increa

Valid Paths CodeChef Solution in C++ | AskTheCode

CodeChef May Long Challenge Solution | Valid Paths (VPATH) solution | AskTheCode Valid Paths (VPATH) CodeChef solution Problem: You are given a tree with N nodes numbered from 1 to N. A set S of nodes