Chef and feast june 17 challenge

https://www.codechef.com/viewsolution/14263219

i dont understand why do i get wa in second case ?

3 Likes

try this test case :

1
5
-1 2 0 1 3

your code is giving answer : 23

but actual answer is 25

what you’re doing is that you are considering all the positive numbers together and negative numbers separately .
Actually if you closely look at the test case that I’ve provided,you will see that there are 2 possibilities.


possibility 1 :

take all positive numbers at together and negative numbers separately

so,

positive sum = 1+0+2+3 = 6

happy_pos = 6 * 4 = 24

negative sum = -1

happy_neg = -1 * 1 = -1

total_happy = 24 + (-1) = 23 (which is the answer according your code)

possibility 2 :

take all positive numbers together and then add negative number one at a time into the positive sum.

why we are doing this ?

because there might be a possibility that by adding a negative number into the positive sum that we considered our total happiness might increase.

so,

positive sum = 1+0+2+3 = 6

add negative number to this = 6 + (-1) = 5


total_happy = 5 * 5 = 25

which is the correct answer.

if negative number would have been -3 rather than -1 then,

 positive_sum = 6

 happy_pos = 6 * 4 = 24

 new_pos_sum = 6 + (-3) = 3

new_happy_pos = 3 * 5 = 15

since here our overall happiness is decreasing thus in this case we consider the negative number separately thus,

 happy_pos = 24

 happy_neg = -3 * 1 = -3

 total_happy = 24 + (-3) = 21

hope this helps you. :slight_smile:

2 Likes

Thanks @de_crypto now i got it

Here is my solution, it might help.
https://www.codechef.com/viewsolution/14047753

1 Like

thanks for the answer it actually helped me sorry for late reply

You shouldnt use award points feature dear. It subtracts your karma :frowning: . Upvoting so you can continue to ask questions.

thanks for upvote man i didnt know karma is subtracted

thanks for upvote man i didnt know karma is subtracted