304 North Cardinal St.
Dorchester Center, MA 02124

# Healthy dinner party CodeChef Solution

## Healthy dinner party CodeChef Solution in C++17

``````#include <bits/stdc++.h>

#pragma optimization_level 3
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#pragma GCC optimize("Ofast")//Comment optimisations for interactive problems (use endl)
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")

using namespace std;

struct PairHash {inline std::size_t operator()(const std::pair<int, int> &v) const { return v.first * 31 + v.second; }};

// speed
#define Code ios_base::sync_with_stdio(false);
#define By ios::sync_with_stdio(0);
#define Sumfi cout.tie(NULL);

// alias
using ll = long long;
using ld = long double;
using ull = unsigned long long;

// constants
const ld PI = 3.14159265358979323846;  /* pi */
const ll INF = 1e18;
const ld EPS = 1e-9;
const ll MAX_N = 202020;
const ll mod = 1e9 + 7;

// typedef
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef array<ll,3> all3;
typedef array<ll,5> all5;
typedef vector<all3> vall3;
typedef vector<all5> vall5;
typedef vector<ld> vld;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<int> vi;
typedef deque<ll> dqll;
typedef deque<pll> dqpll;
typedef pair<string, string> pss;
typedef vector<pss> vpss;
typedef vector<string> vs;
typedef vector<vs> vvs;
typedef unordered_set<ll> usll;
typedef unordered_set<pll, PairHash> uspll;
typedef unordered_map<ll, ll> umll;
typedef unordered_map<pll, ll, PairHash> umpll;

// macros
#define rep(i,m,n) for(ll i=m;i<n;i++)
#define rrep(i,m,n) for(ll i=n;i>=m;i--)
#define all(a) begin(a), end(a)
#define rall(a) rbegin(a), rend(a)
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
#define INF(a) memset(a,0x3f3f3f3f3f3f3f3fLL,sizeof(a))
#define ASCEND(a) iota(all(a),0)
#define sz(x) ll((x).size())
#define BIT(a,i) (a & (1ll<<i))
#define BITSHIFT(a,i,n) (((a<<i) & ((1ll<<n) - 1)) | (a>>(n-i)))
#define pyes cout<<"Yes\n";
#define pno cout<<"No\n";
#define endl "\n"
#define pneg1 cout<<"-1\n";
#define ppossible cout<<"Possible\n";
#define pimpossible cout<<"Impossible\n";
#define TC(x) cout<<"Case #"<<x<<": ";
#define X first
#define Y second

// utility functions
template <typename T>
void print(T &&t)  { cout << t << "\n"; }
template<typename T>
void printv(vector<T>v){ll n=v.size();rep(i,0,n){cout<<v[i];if(i+1!=n)cout<<' ';}cout<<endl;}
template<typename T>
void printvln(vector<T>v){ll n=v.size();rep(i,0,n)cout<<v[i]<<endl;}
void fileIO(string in = "input.txt", string out = "output.txt") {freopen(in.c_str(),"r",stdin); freopen(out.c_str(),"w",stdout);}
template<typename T>
template<typename T, typename U>
template<typename T, typename U>

struct Combination {
vll fac, inv;
ll n, MOD;

ll modpow(ll n, ll x, ll MOD = mod) { if(!x) return 1; ll res = modpow(n,x>>1,MOD); res = (res * res) % MOD; if(x&1) res = (res * n) % MOD; return res; }

Combination(ll _n, ll MOD = mod): n(_n + 1), MOD(MOD) {
inv = fac = vll(n,1);
rep(i,1,n) fac[i] = fac[i-1] * i % MOD;
inv[n - 1] = modpow(fac[n - 1], MOD - 2, MOD);
rrep(i,1,n - 2) inv[i] = inv[i + 1] * (i + 1) % MOD;
}

ll fact(ll n) {return fac[n];}
ll nCr(ll n, ll r) {
if(n < r or n < 0 or r < 0) return 0;
return fac[n] * inv[r] % MOD * inv[n-r] % MOD;
}
};

struct Matrix {
ll r,c;
vvll matrix;
Matrix(ll r, ll c, ll v = 0): r(r), c(c), matrix(vvll(r,vll(c,v))) {}

Matrix operator*(const Matrix& B) const {
Matrix res(r, B.c);
rep(i,0,r) rep(j,0,B.c) rep(k,0,B.r) {
res.matrix[i][j] = (res.matrix[i][j] + matrix[i][k] * B.matrix[k][j] % mod) % mod;
}
return res;
}

Matrix copy() {
Matrix copy(r,c);
copy.matrix = matrix;
return copy;
}

Matrix pow(ll n) {
assert(r == c);
Matrix res(r,r);
Matrix now = copy();
rep(i,0,r) res.matrix[i][i] = 1;
while(n) {
if(n & 1) res = res * now;
now = now * now;
n /= 2;
}
return res;
}
};

// geometry data structures
template <typename T>
struct Point {
T y,x;
Point(T y, T x) : y(y), x(x) {}
Point(pair<T,T> p) : y(p.first), x(p.second) {}
Point() {}
void input() {cin>>y>>x;}
friend ostream& operator<<(ostream& os, const Point<T>& p) { os<<p.y<<' '<<p.x<<'\n'; return os;}
Point<T> operator+(Point<T>& p) {return Point<T>(y + p.y, x + p.x);}
Point<T> operator-(Point<T>& p) {return Point<T>(y - p.y, x - p.x);}
Point<T> operator*(ll n) {return Point<T>(y*n,x*n); }
Point<T> operator/(ll n) {return Point<T>(y/n,x/n); }
bool operator<(const Point &other) const {if (x == other.x) return y < other.y;return x < other.x;}
Point<T> rotate(Point<T> center, ld angle) {
ld si = sin(angle * PI / 180.), co = cos(angle * PI / 180.);
ld y = this->y - center.y;
ld x = this->x - center.x;

return Point<T>(y * co - x * si + center.y, y * si + x * co + center.x);
}
ld distance(Point<T> other) {
T dy = abs(this->y - other.y);
T dx = abs(this->x - other.x);
return sqrt(dy * dy + dx * dx);
}

T norm() { return x * x + y * y; }
};

template<typename T>
struct Line {
Point<T> A, B;
Line(Point<T> A, Point<T> B) : A(A), B(B) {}
Line() {}

void input() {
A = Point<T>();
B = Point<T>();
A.input();
B.input();
}

T ccw(Point<T> &a, Point<T> &b, Point<T> &c) {
T res = a.x * b.y + b.x * c.y + c.x * a.y;
res -= (a.x * c.y + b.x * a.y + c.x * b.y);
return res;
}

bool isIntersect(Line<T> o) {
T p1p2 = ccw(A,B,o.A) * ccw(A,B,o.B);
T p3p4 = ccw(o.A,o.B,A) * ccw(o.A,o.B,B);
if (p1p2 == 0 && p3p4 == 0) {
pair<T,T> p1(A.y, A.x), p2(B.y,B.x), p3(o.A.y, o.A.x), p4(o.B.y, o.B.x);
if (p1 > p2) swap(p2, p1);
if (p3 > p4) swap(p3, p4);
return p3 <= p2 && p1 <= p4;
}
return p1p2 <= 0 && p3p4 <= 0;
}

pair<bool,Point<ld>> intersection(Line<T> o) {
if(!this->intersection(o)) return {false, {}};
ld det = 1. * (o.B.y-o.A.y)*(B.x-A.x) - 1.*(o.B.x-o.A.x)*(B.y-A.y);
ld t = ((o.B.x-o.A.x)*(A.y-o.A.y) - (o.B.y-o.A.y)*(A.x-o.A.x)) / det;
return {true, {A.y + 1. * t * (B.y - A.y), B.x + 1. * t * (B.x - A.x)}};
}

//@formula for : y = ax + b
//@return {a,b};
pair<ld, ld> formula() {
T y1 = A.y, y2 = B.y;
T x1 = A.x, x2 = B.x;
if(y1 == y2) return {1e9, 0};
if(x1 == x2) return {0, 1e9};
ld a = 1. * (y2 - y1) / (x2 - x1);
ld b = -x1 * a + y1;
return {a, b};
}
};

template<typename T>
struct Circle {
Point<T> center;
Circle() {}

void input() {
center = Point<T>();
center.input();
}

bool circumference(Point<T> p) {
return (center.x - p.x) * (center.x - p.x) + (center.y - p.y) * (center.y - p.y) == radius * radius;
}

bool intersect(Circle<T> c) {
T d = (center.x - c.center.x) * (center.x - c.center.x) + (center.y - c.center.y) * (center.y - c.center.y);
}

bool include(Circle<T> c) {
T d = (center.x - c.center.x) * (center.x - c.center.x) + (center.y - c.center.y) * (center.y - c.center.y);
}
};

ll __gcd(ll x, ll y) { return !y ? x : __gcd(y, x % y); }
all3 __exgcd(ll x, ll y) { if(!y) return {x,1,0}; auto [g,x1,y1] = __exgcd(y, x % y); return {g, y1, x1 - (x/y) * y1}; }
ll __lcm(ll x, ll y) { return x / __gcd(x,y) * y; }
ll modpow(ll n, ll x, ll MOD = mod) { n%=MOD; if(!x) return 1; ll res = modpow(n,x>>1,MOD); res = (res * res) % MOD; if(x&1) res = (res * n) % MOD; return res; }

string solve(vll& A, string s, ll k) {
vs dp(k + 1);
rep(i,0,sz(s)) {
ll n = sz(dp);
ll point = A[s[i]-'a'];
rrep(j,point,k) {
if(sz(dp[j-point]) == 0) continue;
string now = dp[j-point];
now.push_back(s[i]);
if(sz(dp[j]) == 0) dp[j] = now;
else dp[j] = min(dp[j], now);
}
if(point <= k) {
if(sz(dp[point]) == 0) dp[point] = string(1,s[i]);
else dp[point] = min(dp[point], string(1,s[i]));
}
}
if(sz(dp[k])) return dp[k];
return "IMPOSSIBLE";
}
int main() {
Code By Sumfi
cout.precision(12);
ll tc = 1;
cin>>tc;
rep(i,1,tc+1) {
ll n,k;
cin>>n;
vll A(n);
string s;
cin>>s;
cin>>k;
print(solve(A,s,k));
}
return 0;
}``````

## Healthy dinner party CodeChef Solution in C++14

``````#include<iostream>
#include<stdio.h>
#include<assert.h>
#include<string.h>
#include<time.h>
#include<stdlib.h>
#include<math.h>
#include<string>
#include<sstream>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define LL long long
#define bit __builtin_popcountll
#define sqr(x) (x) * (x)
using namespace std;
typedef pair<int, int> pii;
const double eps = 1e-9;
const double pi = acos(-1.0);
char s[1005];
int w[26];
bool dp[1005][505];
void solve() {
int k; cin >> k;
for (int i = 0; i < k; i++) {
cin >> w[i];
}
int W; cin >> s >> W;
int n = strlen(s);
for (int i = 1; i <= W; i++) {
dp[n][i] = false;
}
dp[n][0] = true;
for (int i = n - 1; i >= 0; i--) {
for (int j = 0; j < w[s[i] - 'a']; j++) {
dp[i][j] = dp[i + 1][j];
}
for (int j = w[s[i] - 'a']; j <= W; j++) {
dp[i][j] = dp[i + 1][j] | dp[i + 1][j - w[s[i] - 'a']];
}
}
if (!dp[0][W]) {
cout << "IMPOSSIBLE" << endl;
return;
}
int p = 0;
while(W > 0) {
for (char ch = 'a'; ch <= 'a' + k - 1; ch++) {
if (W < w[ch - 'a']) continue;
bool ok = false;
for (int i = p; i < n; i++) {
if (ch != s[i] || !dp[i + 1][W - w[ch - 'a']]) continue;
p = i + 1;
ok = true;
break;
}
if (ok) {
W -= w[ch - 'a'];
putchar(ch);
break;
}
}
}
puts("");
}
int main()
{
#ifndef ONLINE_JUDGE
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
#endif
int T; cin >> T;
while(T--) {
solve();
}
return 0;
} ``````

## Healthy dinner party CodeChef Solution in PYTH 3

``````# cook your dish here

for _ in range(int(input())):
val=list(map(int,input().split()))
val.pop(0)
thing=input()
req=int(input())
sol=[""]*501
for i in range(0,len(thing)):
th=val[(ord(thing[i])-ord('a'))]
for m in range(req - th,-1 ,-1):
if sol [m] == "":
if m != 0:
continue

thi = sol[m] + thing[i]
if sol[m + th] == "":
sol [m + th] = thi

elif thi<sol[m + th]:
sol [m + th] = thi
if sol[req] == "":
print("IMPOSSIBLE")
else:
print(sol[req])

``````

## Healthy dinner party CodeChef Solution in C

``````#include<stdio.h>
#include<string.h>

void sort(char string[]);
void printPath(int value[][501],int i,int j,char string[],int protien[]);

int main(){
int protien[26];
int k,i,j,T,S,len;
char string[1001];
int dynamic[2][501];
int value[1001][501];
scanf("%d",&T);

while(T--){
for(i=0;i<501;i++){
dynamic[0][i]=0;
}
dynamic[0][0]=1;
scanf("%d",&k);
for(i=0;i<k;i++){
scanf("%d",&protien[i]);   //protien content
}
for(i=0;i<501;i++){
value[0][i]=-1;
}
scanf("%s",string);
scanf("%d",&S);
sort(string);
len=strlen(string);
// DYNAMIC PROGRAMMING

for(i=1;i<=len;i++){
for(j=0;j<=S;j++){
if(protien[string[i-1]-'a']<=j){
if(dynamic[0][j-protien[string[i-1]-'a']]==1){
if(value[i-1][j]==-1){
dynamic[1][j]=1;
value[i][j]=i-1;
}
else if(string[value[i-1][j]]>=string[i-1]){
dynamic[1][j]=1;
value[i][j]=i-1;
}
else{
dynamic[1][j]=dynamic[0][j];
value[i][j]=value[i-1][j];
}
}
else{
dynamic[1][j]=dynamic[0][j];
value[i][j]=value[i-1][j];
}
}
else{
dynamic[1][j]=dynamic[0][j];
value[i][j]=value[i-1][j];
}
}
for(k=0;k<=S;k++){
dynamic[0][k]=dynamic[1][k];
}
}

//************
/*for(i=1;i<=len;i++){
for(j=0;j<=S;j++){
printf("%d    ",value[i][j]);
}
printf("\n");
}*/
if(dynamic[1][S]==0){
printf("IMPOSSIBLE\n");
}
else{
printPath(value,len,S,string,protien);
printf("\n");
}
}

return 0;
}

void sort(char string[]){
int len,i,j;
char temp;
len=strlen(string);
for(i=0;i<len/2;i++){
temp=string[i];
string[i]=string[len-i-1];
string[len-i-1]=temp;
}
}

void printPath(int value[][501],int i,int j,char string[],int protien[]){
if(value[i][j]!=-1){
printf("%c",string[value[i][j]]);
printPath(value,value[i][j],j-protien[string[value[i][j]]-'a'],string,protien);
}
}``````

## Healthy dinner party CodeChef Solution in JAVA

``````import java.util.Scanner;

public class Main {
static int[] value;
static char[] str;
static int k;
static int S;
static String[][] f;

public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
for (int T = cin.nextInt(); T > 0; T--) {
k = cin.nextInt();
value = new int[k];
for (int i = 0; i < k; i++)
value[i] = cin.nextInt();
str = cin.next().toCharArray();
S = cin.nextInt();

f = new String[str.length + 1][S + 1];
f[str.length][0] = "";
for (int i = str.length - 1; i >= 0; i--) {
for (int j = 0; j <= S; j++) {
f[i][j] = f[i + 1][j];
if (value[str[i] - 'a'] <= j && f[i + 1][j - value[str[i] - 'a']] != null) {
if (f[i][j] == null || f[i][j].compareTo(str[i] + f[i + 1][j - value[str[i] - 'a']]) > 0) {
f[i][j] = str[i] + f[i + 1][j - value[str[i] - 'a']];
}
}
}
}
if (f[0][S] == null)
System.out.println("IMPOSSIBLE");
else
System.out.println(f[0][S]);
}
cin.close();
}
}``````

## Healthy dinner party CodeChef Solution in PYTH

``````import sys

def create_meal_from_rawinput():
proteinsStr = temp.split()[1:]
proteins = [int(i) for i in proteinsStr]

return Meal(
proteins,
ingredients,
desiredProteins
)

class Meal(object):
def __init__(self, proteins, ingredients, desiredProteins):
self.proteins = proteins
self.ingredients = ingredients
self.desiredProteins = desiredProteins
self.bestMatch = ["#" for i in xrange(desiredProteins + 1)]
self.bestMatch[desiredProteins] = ""

def displayProperties(self):
print self.proteins
print self.ingredients
print self.desiredProteins
print self.bestMatch

def findBestIngredients(self):
for i in self.ingredients:
self.iterate(i)

if self.bestMatch[0] == "#":
return "IMPOSSIBLE"
else:
return self.bestMatch[0]

def iterate(self, currentIngredient):
currentProtein = self.proteins[ord(currentIngredient) - ord('a')]
for i in xrange(self.desiredProteins + 1):
if self.bestMatch[i] != "#":
temp = i - currentProtein

if temp >= 0:
newProteinString = self.bestMatch[i] + currentIngredient

if self.bestMatch[temp] == "#" or self.bestMatch[temp] > newProteinString:
self.bestMatch[temp] = newProteinString

for i in xrange(numberOfGuests):
meal = create_meal_from_rawinput()
print meal.findBestIngredients()``````

## Healthy dinner party CodeChef Solution in GO

``````package main

import (
"bufio"
"bytes"
"fmt"
"os"
)

func main() {

var buf bytes.Buffer
for tc > 0 {
tc--

var pos int
var K int
pos = readInt(line, 0, &K) + 1

V := make([]int, K)

for i := 0; i < K; i++ {
pos = readInt(line, pos, &V[i]) + 1
}

res := solve(K, V, L, S)

if len(res) == 0 {
res = "IMPOSSIBLE"
}

buf.WriteString(fmt.Sprintln(res))
}
fmt.Print(buf.String())
}

func readUint64(bytes []byte, from int, val *uint64) int {
i := from

var tmp uint64
for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
tmp = tmp*10 + uint64(bytes[i]-'0')
i++
}
*val = tmp

return i
}

func readInt(bytes []byte, from int, val *int) int {
i := from
sign := 1
if bytes[i] == '-' {
sign = -1
i++
}
tmp := 0
for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
tmp = tmp*10 + int(bytes[i]-'0')
i++
}
*val = tmp * sign
return i
}

for i := 0; i < len(s); i++ {
if s[i] == '\n' || s[i] == '\r' {
return s[:i]
}
}
return s
}

return
}

a, b = res[0], res[1]
return
}

a, b, c = res[0], res[1], res[2]
return
}

res := make([]int, n)
x := 0
for i := 0; i < n; i++ {
for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
x++
}
}
return res
}

func solve(K int, V []int, L string, S int) string {
n := len(L)
dp := make([][]bool, n+1)
for i := 0; i <= n; i++ {
dp[i] = make([]bool, S+1)
}
can := make([]bool, S+1)
can[0] = true
dp[n][0] = true

for i := n - 1; i >= 0; i-- {
x := int(L[i] - 'a')
v := V[x]
for j := S - v; j >= 0; j-- {
if can[j] {
can[j+v] = true
dp[i][j+v] = true
}
}
}

if !can[S] {
return ""
}

var buf bytes.Buffer
var p int
for S > 0 && p < n {
ii := -1
for i := p; i < n; i++ {
if dp[i][S] && (ii < 0 || L[i] < L[ii]) {
ii = i
}
}
buf.WriteByte(L[ii])
S -= V[int(L[ii]-'a')]
p = ii + 1
}

return buf.String()
}``````
##### Healthy dinner party CodeChef Solution Review:

In our experience, we suggest you solve this Healthy dinner party 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 Healthy dinner party CodeChef Solution.

Find on CodeChef

##### Conclusion:

I hope this Healthy dinner party 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!