Chef and new recipe Codechef Solution

Chef and new recipe Codechef Solution: Rupsa recently started to intern under Chef. He gave her N type of ingredients of varying quantity A1, A2, …, AN respectively to store it. But as she is lazy to arrange them she puts them all in a storage box.

Chef comes up with a new recipe and decides to prepare it. He asks Rupsa to get two units of each type ingredient for the dish. But when she went to retrieve the ingredients, she realizes that she can only pick one item at a time from the box and can know its type only after she has picked it out. The picked item is not put back in the bag.

She, being lazy, wants to know the maximum number of times she would need to pick items from the box in the worst case so that it is guaranteed that she gets at least two units of each type of ingredient. If it is impossible to pick items in such a way, print -1.


  • The first line of the input contains an integer T denoting the number of test cases.
  • The first line of each test case contains a single integer N denoting the number of different type of ingredients.
  • The second line contains N space-separated integers A1, A2, …, AN denoting the quantity of each ingredient.


  • For each test case, output a single line containing an integer denoting the answer corresponding to that test case.


  • 1 ≤ T ≤ 10
  • 1 ≤ N ≤ 105
  • 1 ≤ Ai ≤ 104

Sub tasks

  • Subtask #1: 1 ≤ N ≤ 1000 (30 points)
  • Subtask #2: original constraints (70 points)

Sample Input 1 

2 2

Sample Output 1 



  • In Example 1, she need to pick up all items.
  • In Example 2, since there is only one type of ingredient, picking two items is enough.

Chef and new recipe CodeChef Solution in JAVA

class ChefRP {
    public static void main(String[] args) throws java.lang.Exception{
        BufferedReader buff = new BufferedReader(new InputStreamReader(, 100000);
        int test_cases = Integer.parseInt(buff.readLine().trim());
        while(test_cases-- > 0){
            int min, num, flag=0;
            int num_ingredients, sum=0;
            String[] nums;
            num_ingredients = Integer.parseInt(buff.readLine().trim());
            nums = buff.readLine().trim().split(" ");
            num = Integer.parseInt(nums[0]);
            if(num < 2)
                flag = 1;
            min = num;
            sum += num;
            for(int i = 1; i< num_ingredients; i++){
                num = Integer.parseInt(nums[i]);
                sum += num;
                if(num < 2)
                    flag = 1;
                if(num < min)
                    min = num;
            if(flag == 1)
                System.out.println((sum - (min - 2)));

Chef and new recipe CodeChef Solution in CPP

#include <bits/stdc++.h> // Include every standard library
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
typedef pair<string, string> pss;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef vector<LL> vl;
typedef vector<vl> vvl;
double EPS = 1e-9;
int INF = 1000000005;
long long INFF = 1000000000000000005LL;
double PI = acos(-1);
int dirx[8] = { -1, 0, 0, 1, -1, -1, 1, 1 };
int diry[8] = { 0, 1, -1, 0, -1, 1, -1, 1 };
#define FOR(a, b, c) for (int(a) = (b); (a) < (c); ++(a))
#define FORN(a, b, c) for (int(a) = (b); (a) <= (c); ++(a))
#define FORD(a, b, c) for (int(a) = (b); (a) >= (c); --(a))
#define FORSQ(a, b, c) for (int(a) = (b); (a) * (a) <= (c); ++(a))
#define FORC(a, b, c) for (char(a) = (b); (a) <= (c); ++(a))
#define FOREACH(a, b) for (auto&(a) : (b))
#define REP(i, n) FOR(i, 0, n)
#define REPN(i, n) FORN(i, 1, n)
#define MAX(a, b) a = max(a, b)
#define MIN(a, b) a = min(a, b)
#define SQR(x) ((LL)(x) * (x))
#define RESET(a, b) memset(a, b, sizeof(a))
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ALL(v) v.begin(), v.end()
#define ALLA(arr, sz) arr, arr + sz
#define SIZE(v) (int)v.size()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define SORTA(arr, sz) sort(ALLA(arr, sz))
#define REVERSEA(arr, sz) reverse(ALLA(arr, sz))
#define PERMUTE next_permutation
#define TC(t) while (t--)
string IntToString(LL a)
    char x[100];
    sprintf(x, "%lld", a);
    string s = x;
    return s;
LL StringToInt(string a)
    char x[100];
    LL res;
    strcpy(x, a.c_str());
    sscanf(x, "%lld", &res);
    return res;
string GetString(void)
    char x[1000005];
    scanf("%s", x);
    string s = x;
    return s;
string uppercase(string s)
    int n = SIZE(s);
    REP(i, n)
    if (s[i] >= 'a' && s[i] <= 'z')
        s[i] = s[i] - 'a' + 'A';
    return s;
string lowercase(string s)
    int n = SIZE(s);
    REP(i, n)
    if (s[i] >= 'A' && s[i] <= 'Z')
        s[i] = s[i] - 'A' + 'a';
    return s;
void solve()
    int n;
    int arr[n];
    int mi = INT_MAX;
    LL sum = 0;
    for(int i=  0; i<n; i++) {cin>>arr[i]; if(arr[i]<mi) mi =arr[i]; sum+=arr[i];}
    if(mi<2) cout<<-1<<"\n";
int main()
    int t;
    while(t--) solve();
    return 0;

Chef and new recipe CodeChef Solution in Python

    for i in range(0,n):
        if len(l)==1:
        elif min(l)==1:
except EOFError as e:

Disclaimer: The above Problem (Chef and new recipe) is generated by CodeChef but the solution is provided by Chase2learn. This tutorial is only for Educational and Learning purpose.

Sharing Is Caring