College Life 4 Codechef Solution

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 respectively.

However, the canteen is about to run out of some ingredients. In particular, they only have EE eggs and HH 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.

Input

  • The first line of the input contains a single integer T denoting the number of test cases. The description of TT test cases follows.
  • The first and only line of each test case contains six space-separated integers N, E, H, A, B and C.

Output

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.

Constraints

  • 1≤T≤2⋅1051≤T≤2⋅105
  • 1≤N≤1061≤N≤106
  • 0≤E,H≤1060≤E,H≤106
  • 1≤A,B,C≤1061≤A,B,C≤106
  • the sum of NN over all test cases does not exceed 106106

Sample Input 1 

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

Sample Output 1 

-1
7
4

College Life 4   – CodeChef Solution in JAVA

import java.util.*;
import java.lang.*;
import java.io.*;
class Solution{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while(t-- > 0){
            int items = sc.nextInt();
            int eggs = sc.nextInt();
            int chocs = sc.nextInt();
            int A = sc.nextInt();
            int B = sc.nextInt();
            int C = sc.nextInt();
            long ans = Long.MAX_VALUE;
            for(int c = 0;c<=items;c++){
                if(eggs<c || chocs<c) break;
                int maxO = (eggs-c)/2;
                int maxM = (chocs-c)/3;
                int reqO=0;
                int reqM=0;
                if(c+maxO+maxM < items) continue;
                if(A<B){
                    reqO = Math.min(maxO, items-c);
                    reqM = (items-c-reqO);
                }
                else{
                    reqM = Math.min(maxM, items-c);
                    reqO = (items-c-reqM);
                }
                long cost = (long)((long)A*(long)reqO) + (long)((long)B*(long)reqM) + (long)((long)C*(long)c);
                ans = Math.min(ans, cost);
            }
            if(ans == Long.MAX_VALUE){
                System.out.println(-1);
            }
            else System.out.println(ans);
        }
    }
}

College Life 4  – CodeChef Solution in CPP

#include"bits/stdc++.h"
typedef long long ll;
using namespace std;
int main()
{
    ll t;
    cin>>t;
    while(t>0)
    {
        ll n,a,b,c,e,h;
        cin>>n>>e>>h>>a>>b>>c;
        ll ans=1e18;
       for(ll cakes=0;cakes<=n;cakes++)
       {
           if(e<cakes||h<cakes)
           break;
           
           ll maxo=(e-cakes)/2;
           ll maxc=(h-cakes)/3;
           if((cakes+maxo+maxc)<n)
           continue;
           ll requiredo=0,requiredc=0;
           if(a<b)
           {
               requiredo=min(n-cakes,maxo);
             requiredc=n-cakes-requiredo;
           }
           else
           {
                 requiredc=min(n-cakes,maxc);
                requiredo=n-cakes-requiredc;
           }
           
           ll price=(c*cakes)+(a*requiredo)+b*requiredc;
           ans=min(price,ans);
       }
       if(ans==1e18)
       ans=-1;
       
       cout<<ans<<endl;
       
        t--;
    }
}

College Life 4 -CodeChef Solution in Python

for _ in range(int(input())):
    n,e,h,a,b,c = map(int,input().split())
    l2 = []
    ans = 1e18
    for numc in range(n+1):
        if numc > e or numc > h:
            break
        maxo = (e - numc)//2
        maxm = (h - numc)//3
        if n > numc + maxo + maxm :
            continue
        if a<b :
            reqo = min(n-numc,maxo)
            reqm = n-numc-reqo
        else :
            reqm = min(n-numc,maxm)
            reqo = n - numc- reqm
        price = numc*c + reqo*a + reqm*b 
        ans =min(ans,price)
        #print(price)
        #print(numc,reqo,reqm)
    if ans == 1e18:
        print(-1)
    else :
        print(ans)

Disclaimer: The above Problem (College Life 4) is generated by CodeChef but the solution is provided by Codeworld19.This tutorial is only for Educational and Learning purpose.

Leave a Reply

Your email address will not be published. Required fields are marked *