INTEGERS -- SUM

M adjacent integers M1,…,MN are inserted in a circle

the following operation can be done M-1 times

pick 2 adjacent nos. and sum them up and store it in a variable.
delete picked numbers and replace it with that single variable number in the circle.
What is the minimum value of that variable?


example: 3
10 8 9

sum up 8 & 9 it gives 17..

Now the circle contains 10 and 17 ..

summing up gives 27+17=44..

that is the answer.

example 2:
3
10 10 1
ans: 32.

Please explain with code. (preferably c++)
Last edited on
example 2:
3
10 10 1
ans: 22.
Why is it 22 and not 21?

Isn't the answer always equal to the sum of all the numbers?
1. You have a sequence of numbers.
2. If the sequence has less than two elements, you're done.
3. Otherwise, remove any two of its elements, add them, and put back the result of the addition.
4. Go to step 2.
Following this algorithm, regardless of which two numbers are picked each time on step 3, the final result will necessarily be either an empty sequence or a sequence with a single element that's the sum of all the elements of the original sequence.
hey ... I updated the correct example now ...

we get 10+1 =11 ..
store it in variable ..

then we are left with 11 and 10 ...

sum it up and add it to that add it to that variable:

11+21=32..

that's why it is 32..

so this is not direct sum of all elements.. and at last we need the minimum value of that variable ....

please help me with code and thanks for responding.
Last edited on
You have three numbers: 10 8 9

One "operation":
* pick 2 adjacent nos. and sum them up and store it in a variable
variable = 10+8. variable == 18
* delete picked numbers and replace it with that single variable number in the circle
circle: 18 9

Second "operation":
* pick 2 adjacent nos. and sum them up and store it in a variable
variable = 18 + 9. variable == 27
* delete picked numbers and replace it with that single variable number in the circle
circle: 27

Result: 27

The result would be different, if the instructions would say that the variable is 0 at start and you add pairwise sum to the variable.


Furthermore: "operation can be done M-1 times". "Can", not "have to". Okay, I'll do the operation 0 times. Nothing is stored into variable. That must be the hands down minimum (unless the circle contains negative values).
@minimum poor lad tried to express an ongoing contest question in his words and it seems like he isn't good at this too.
https://www.codechef.com/JULY19A/problems/CIRMERGE
@keskiverto, @minimum, didn't write the problem correctly as @lazybot told. I think In this problem, you have to do the "operations" till you have only 1 number left in the array. That's why here we have to do these operations.
Also, you wrote the second operation wrong. it's 18+(18+9).i.e 18+27=45. which is not the minimum value(which is 44).
@theKlaw --- you are right bro...
Yes, will anyone explain with code... I am also not able to understand ...
No. No code. No ideas.
Mikeyboy wrote:
If this is a Codechef problem, you should know that the Codechef adjudicators are aware of this forum, and that people use it to cheat on their contests. If you use this forum to try and cheat, you run the risk of being disqualified.


Niccolo commented these problems: http://www.cplusplus.com/forum/general/256899/#msg1123086


theKlaw wrote:
Also, you wrote the second operation wrong.

No. What I wrote was valid according to the OP instructions. One must be exact with computers. They do not guess your intentions. They do exactly what they are told.
Last edited on
Topic archived. No new replies allowed.