Chef and the Feast CodeChef Solution

Problem -Chef and the Feast CodeChef Solution

This website is dedicated for CodeChef solution where we will publish right solution of all your favourite CodeChef problems along with detailed explanatory of different competitive programming concepts and languages.

Chef and the Feast CodeChef Solution in C++14


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define Time cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl;
#define pb push_back
#define mp make_pair
#define line cout << endl;
#define ff first
#define ss second
#define vi vector<int>
#define no cout << "NO" << endl;
#define yes cout << "YES" << endl;
#define printv(v)                      \
  for (int i = 0; i < (v.size()); i++) \
  {                                    \
    cout << v[i] << " ";               \
  }                                    \
  line;
#define onesbits(x) __builtin_popcountll(x)
#define zerobits(x) __builtin_ctzll(x)
#define sp(x, y) fixed << setprecision(y) << x
#define w(x) \
  int x;     \
  cin >> x;  \
  while (x--)
#define tk(x) \
  int x;      \
  cin >> x;
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
#ifndef ONLINE_JUDGE
#define debug(x)     \
  cerr << #x << " "; \
  _print(x);         \
  cerr << endl;
#else
#define debug(x)
#endif
template <class T>
void _print(T t)
{
  cerr << t;
}

template <class T, class V>
void _print(pair<T, V> p)
{
  cerr << "{";
  _print(p.ff);
  cerr << ",";
  _print(p.ss);
  cerr << "}";
}

template <class T>
void _print(vector<T> v)
{
  cerr << "[ ";
  for (T i : v)
  {
    _print(i);
    cerr << " ";
  }
  cerr << "]";
}

template <class T>
void _print(vector<vector<T>> v)
{
  cerr << "[\n";
  for (int l = 0; l < v.size(); l++)
  {
    {
      for (int k = 0; k < v[l].size(); k++)
        cerr << v[l][k] << " ";
    }
    cerr << "\n";
  }
  cerr << "]";
}

template <class T, class V>
void _print(map<T, V> v)
{
  cerr << "[ ";
  for (auto i : v)
  {
    _print(i);
    cerr << " ";
  }
  cerr << "]";
}

template <class T>
void _print(set<T> v)
{
  cerr << "[ ";
  for (T i : v)
  {
    _print(i);
    cerr << " ";
  }
  cerr << "]";
}

const long long inf = 1e18;
const int MOD = 1e9 + 7;
const int MAX = 1e6;

int numbit(int x)
{
    int ans = 0;
    while (x > 0)
    {
        x = x >> 1;
        ans++;
    }
    return ans;
}

int setbit(int x)
{
    int ans = 0;
    while (x > 0)
    {
        if (x & 1)
        {
            ans++;
        }
        x = x >> 1;
    }
    return ans;
}

bool isValid(string s)
{
  int len = s.size();
  for (int i = 0; i < len / 2; i++)
  {
    if (s[i] != s[len - 1 - i])
      return false;
  }
  return true;
}

void rotateMatrix(vector<vector<int>> &v, int n)
{
  for (int i = 0; i < n / 2; i++)
  {
    for (int j = i; j < n - i - 1; j++)
    {
      int ptr = v[i][j];
      v[i][j] = v[n - 1 - j][i];
      v[n - 1 - j][i] = v[n - 1 - i][n - 1 - j];
      v[n - 1 - i][n - 1 - j] = v[j][n - 1 - i];
      v[j][n - 1 - i] = ptr;
    }
  }
}

vector<bool> is_prime(10001, 1);
vector<int> primes;

void seive()
{
  is_prime[0] = 0;
  is_prime[1] = 0;
  for (int i = 2; i < 10001; i++)
  {
    if (is_prime[i])
    {
      primes.push_back(i);
      for (int j = i + i; j < 10001; j += i)
      {
        is_prime[j] = 0;
      }
    }
  }
}



int32_t main() {

  ll t; cin >> t;
  // seive();
  while (t--) {

    ll n;
	    cin>>n;
	    ll arr[n], sumn=0, sump=0, cnt=0;
	    vector<ll> v;
	    for (ll i=0; i<n; i++){
	        cin>>arr[i];
	        if (arr[i]>=0){
	            sump+=arr[i];
	            cnt+=1;
	        }
	        else
	            v.push_back(arr[i]);
	    }
	    sort(v.begin(), v.end());
	    for (ll i=v.size()-1; i>=0; i--){
	            if ((sump+v[i])*(cnt+1)>sump*cnt){
	                sump+=v[i];
	                cnt+=1;
	            }
	            else
	                sumn+=v[i];
	       
	            
	    }
	    cout<<sumn+sump*cnt<<endl;

    
    
      
  }
  return 0;
}

// 1b 4d

Chef and the Feast CodeChef Solution in PYTH 3

# cook your dish here
t = int(input())

for i in range(t):
    n = int(input())
    a = sorted(map(int, input().split()), reverse = True)
    ans = s = c = 0
    for j in range(n):
	    s += a[j]
	    if s * (j + 1) > ans:
		    ans = s * (j + 1)
		    c = j + 1
	    else:
		    break
    print(ans + sum(a[c:]))

Chef and the Feast CodeChef Solution in C

#include<stdio.h>
#include<stdlib.h>
long long int comparetor(const void *a,const void *b)
{
    return(*(long long int *)b-*(long long int *)a);
}
int main()
{
	int t,i;
	scanf("%d",&t);
	for(i=0;i<t;i++)
	{
		long long int n,k=0,*arr,*narr,j,psum=0,nsum=0,ppos=0;
		scanf("%lld",&n);
		arr=(long long int *)malloc(n*sizeof(long long int));
		narr=(long long int *)malloc(n*sizeof(long long int));
		for(j=0;j<n;j++)
		{
			scanf("%lld",&arr[j]);
			if(arr[j]>=0)
			{
				psum+=arr[j];
				ppos++;
			}
			else
			{
				narr[k]=arr[j];
				k++;
			}
		}
        	qsort(narr,k,sizeof(long long int),comparetor);
        	for(j=0;j<k;j++)
        	{
        	    if((narr[j]+psum)*(ppos+1)>ppos*psum)
                {
                    psum+=narr[j];
                    ppos++;
                }
                else
                {
                    nsum+=narr[j];
                }
        	}
        	printf("%lld\n",(psum*ppos)+nsum);
	}
	return 0;
}

Chef and the Feast CodeChef Solution in JAVA

/* package codechef; // don't place package name! */

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

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		Scanner scn = new Scanner(System.in);
		int t = scn.nextInt();
		while(t-->0){
		    int n = scn.nextInt();
		    Integer [] arr = new Integer[n];
		    for(int i=0;i<n;i++){
		        arr[i]=scn.nextInt();
		    }
		    Arrays.sort(arr, Collections.reverseOrder());
		    long ans=0,sum=0;
		    int i=0;
		    for(i=0;i<n;i++){
		        if(ans>(sum+arr[i])*(i+1))break;
		        sum+=arr[i];
		        ans=sum*(i+1);
		    }
		    for(;i<n;i++){
		        ans+=arr[i];
		    }
		    System.out.println(ans);
		}
	}
}

Chef and the Feast CodeChef Solution in PYPY 3

t = int(input())

for i in range(t):
    n = int(input())
    a = sorted(map(int, input().split()), reverse = True)
    ans = s = c = 0
    for j in range(n):
	    s += a[j]
	    if s * (j + 1) > ans:
		    ans = s * (j + 1)
		    c = j + 1
	    else:
		    break
    print(ans + sum(a[c:]))

Chef and the Feast CodeChef Solution in PYTH

t = int(raw_input())
for i in range(t):
	N = int(raw_input())
	st = raw_input().split()
	cnt = 0
	sp = 0
	sm = 0
	L = []
	for x in st:
		n = int(x)
		if n < 0:
			sm += n
			L.append(n)
		else:
			sp += n
			cnt += 1
		# endif
	# endfor x
	L.sort()
	L.reverse()
	sz = len(L)
	L.append(-10**9)
	r = sp*cnt + sm
	p = 0
	sp += L[p]
	sm -= L[p]
	cnt += 1
	nr = sp*cnt + sm
	while nr > r:
		r = nr
		p += 1
		sp += L[p]
		sm -= L[p]
		cnt += 1
		nr = sp*cnt + sm
	# endwhile
	print r
# endfor i

Chef and the Feast CodeChef Solution in C#

using System;
using System.Collections.Generic;
using System.Text;

namespace June_17
{
    class NEO01
    {
        public static void Main(string[] args)
        {
            int t = int.Parse(Console.ReadLine());
            while(t-- !=0)
            {
                int n = int.Parse(Console.ReadLine());
                string[] str = Console.ReadLine().Split(' ');

                List<int> list = new List<int>();
                long sum = 0, sumNegative = 0;
                long size = 0;
                int negativecount=0;
                for(int i=0; i< str.Length; i++)
                {
                    int k = int.Parse(str[i]);
                    if(k >= 0)
                    {
                        sum = sum + k;
                        size++;
                    }
                    else
                    {
                        list.Add(k);
                        sumNegative = sumNegative + k;
                    }                      
                }
                str = null;
                long total = sum * size + sumNegative;
                list.Sort();
                long ans = total;

                for (int kk = list.Count - 1; kk >=0; kk--)
                {
                    total = (sum + list[kk]) * (size + 1) + sumNegative - list[kk];
                    sum = sum + list[kk];
                    sumNegative = sumNegative - list[kk];
                    size++;
                    if( total > ans)
                    {
                        ans = total;
                    }
                }

                Console.WriteLine(ans);
            }
            
        }
    }
}

Chef and the Feast CodeChef Solution in GO

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

var in = bufio.NewScanner(os.Stdin)

func readInt() int {
	if !in.Scan() {
		panic(in.Err())
	}
	res := 0
	mul := 1
	for _, b := range in.Bytes() {
		if b == '-' {
			mul = -1
			continue
		}
		res = res*10 + int(b-'0')
	}
	return mul * res
}

func main() {
	in.Split(bufio.ScanWords)
	for t := readInt(); t > 0; t-- {
		fmt.Println(solve())
	}
}

func solve() int64 {
	a := make([]int, readInt())
	for i := range a {
		a[i] = readInt()
	}
	sort.Sort(sort.Reverse(sort.IntSlice(a)))

	sum := int64(a[0])
	i := int64(1)
	for ; int(i) < len(a); i++ {
		ai := int64(a[i])
		if sum*i+ai <= (sum+ai)*(i+1) {
			sum += ai
		} else {
			break
		}
	}
	res := sum * i
	for _, ai := range a[i:] {
		res += int64(ai)
	}
	return res
}
Chef and the Feast CodeChef Solution Review:

In our experience, we suggest you solve this Chef and the Feast CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Chef and the Feast CodeChef Solution.

Find on CodeChef

Conclusion:

I hope this Chef and the Feast CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Programming Language in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

Cognitive Class Answer

CodeChef Solution

Microsoft Learn

Leave a Reply

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