14,699,489 members
1.00/5 (1 vote)
See more:
Statement

Krishna loves candies a lot, so whenever he gets them, he stores them so that he can eat them later whenever he wants to.

He has recently received N boxes of candies each containing Ci candies where Ci represents the total number of candies in the ith box. Krishna wants to store them in a single box. The only constraint is that he can choose any two boxes and store their joint contents in an empty box only. Assume that there are infinite number of empty boxes available.

At a time he can pick up any two boxes for transferring and if both the boxes say contain X and Y number of candies respectively, then it takes him exactly X+Y seconds of time. As he is to eager to collect all of them he has approached you to tell him the minimum time in which all the candies can be collected.

Input Format:

First line of input is number of test case T
Each test case is comprised of two inputs
First input of a test case is the number of boxes N
Second input is N integers delimited by whitespace denoting number of candies in each box

Output Format:

Print minimum time required, in seconds, for each of the test case. Print each output on a new line.

Constraints:

1 ≤T≤10
1 ≤N≤ 10000
1 ≤ [Candies in each box] ≤ 100009

Sample Input and Output

SNo. Input Output Explaination

1.

1
4
1 2 3 4

o/p:19

4 boxes, each containing 1, 2, 3 and 4 candies respectively.
Adding 1 + 2 in a new box takes 3 seconds
Adding 3 + 3 in a new box takes 6 seconds
Adding 4 + 6 in a new box takes 10 seconds
Hence total time taken is 19 seconds. There could be other combinations also, but overall time does not go below 19 seconds.

2.

1
5
1 2 3 4 5

o/p:33

5 boxes, each containing 1, 2, 3, 4 and 5 candies respectively.
Adding 1 + 2 in a new box takes 3 seconds
Adding 3 + 3 in a new box takes 6 seconds
Adding 4 + 5 in a new box takes 9 seconds
Adding 6 + 9 in a new box takes 15 seconds
Hence total time taken is 33 seconds. There could be other combinations also, but overall time does not go below 33 seconds.

What I have tried:

```#include <stdio.h>
int main(){
int i,t;
scanf("%d",&t);
for(i=0; i<t;i++)
scanf("%d",&n);

for(j=0; j<n;j++)
scanf("%d",&a[j]);
}
for(j=0;j<n;j++){
time=a[j]+a[j+1];
sum=time+sum;

}
printf("%d",sum);
}
return 0;
}```
Posted
Updated 26-Mar-20 20:47pm
v4
[no name] 20-Aug-16 3:38am

Waste of time posting here. Not a code service.
Member 12694317 20-Aug-16 3:40am

This is the problem which was asked yestrday in a competition and I could'nt do that...so just wanted to know how to solve it.
Richard MacCutchan 20-Aug-16 4:31am

This is a mathematics problem, so you need to figure that out first. Once you have done that then turning it into code is easy.

Solution 1

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!
So if that code doesn't do what is required, then use the debugger to find out why. Testing and fixing your code is as much a part of development as coding it in the first place!
Member 12694317 20-Aug-16 3:36am

I have a problem in looping. please give any suggestion or any link for reference
OriginalGriff 20-Aug-16 4:05am

What "problem in looping"?
Member 12694317 20-Aug-16 4:10am

#include <stdio.h>
int main(){
int i,t;
scanf("%d",&t);
for(i=0; i<t; i++){
="" int="" func();
="" }
="" return="" 0;
}

int="" func(){
="" i,="" a[10001],n,sum="0,sum1=0,sum2=0," f="1,r," p;
="" scanf("%d",&n);
="" for(i="0;" i<n;="" scanf("%d",&a[i]);
=""
="" &&="" r="a[i]+a[i+1];
" p="a[i+2]+a[i+3];
" *="" if(a[i+1]="=NULL){
" }*="" if((r+a[i+2])="">p){
sum1=sum1+p;
}
else{
sum2=sum2+r;
}
sum=sum1+sum2;

}
printf("%d",sum);
return sum;
}

I am trying to do something like this...but not getting the output
OriginalGriff 20-Aug-16 4:18am

So, have you looked at what output you are getting and compared it to the output you need? What are the differences? Why?
Member 12694317 20-Aug-16 3:38am

this is a problem which was asked yestrday in a competition and I could'nt do that...so just wanted to know how to solve it.

Solution 2

Quote:
I have a problem in looping. please give any suggestion or any link for reference
It is a good idea to state what is the problem you encounter.

Your problem is not a problem of looping, it is a problem of understanding what you need to do to solve the statement.

you have a list of boxes known by the number of candies inside. you have 1 operation: pick 2 boxes merge them and put back the resulting box.

Take an example with 1 10 5 check all possibilities
1 10 5 => 11 5 => 16 cost= 11 + 16 = 27
1 10 5 => 1 15 => 16 cost= 15 + 16 = 31
1 10 5 => 6 10 => 16 cost= 6 + 16 = 22

Your task is to understand why a solution is better than another. This understanding will tell you what the program must do in order to get the best score.