Colorful knapsack

Can anyone help me to figure it out the mistake, it gives wa.

https://pastebin.com/xU1JtE1T

correct link https://pastebin.com/8Z8VMD92

hello @knakul853
pls explain ur approach

1 Like

@knakul853
image

here it should be the weight,ur color array is containing indices

1 Like

Thanks, it worked in some of the cases…

the approach was to group all the same color weights together and then use classical knapsack. but here I am not sure how to establish 1/1 condition instead of 0/1

pls share ur updated code with me.

https://pastebin.com/SPabLCbK

@knakul853

image

this can return -inf , handle that case

1 Like

thanks, it yet failed on the last test case https://pastebin.com/1rQgnZjz
but I eventually tried the editorial method and it worked.

ok … . … . …:+1::+1:

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.