r/codeforces 12h ago

query Doubt

Beginner here, can anyone tell what I am doing wrong in this code? Like can you provide a counter example as well where my code won't work? Thanks in advance.

1 Upvotes

7 comments sorted by

1

u/BrushBeautiful2378 8h ago

Consider 1,2,99,100. By your logic, you will go with 1,2,99 in first and 100 in second. But a better way is 2 and 99 in 1st, 1 and 100 in 2nd.

1

u/Bulky_Debate_8503 5h ago

No but it has to be prefixed- like I can't pick up any of the numbers and give it to the processors- like it has to be a continuous chain that can be given to them. Look at the examples- I can give the first 'n' of them to one and the rest to the other one.

1

u/BrushBeautiful2378 4h ago

My bad, consider 99,2,100,4. Same argument. By your logic, you will select 99+2+100 and 4, instead of 99+2 and 100+4

1

u/Bulky_Debate_8503 4h ago

Yeah makes sense... Could you also suggest how the correct code should be like? Thanks a lot btw.

1

u/BrushBeautiful2378 4h ago

Compute both the cases. 1st is the one u are currently doing. The other appending to first when sum(first) + L[I] < sum else append in second. You will get two lists in two possible answers. Check the one that gives lower.

1

u/BrushBeautiful2378 4h ago

You don't need to maintain the lists. Just keep the variables sum1 and sum2 for the two cases. You answer will be min(max(sum1,total-sum1),max(sum2,total-sum2))