### Re: 10954 - Add All

Posted:

**Tue Mar 19, 2013 11:37 pm**Use a priority_queue.

Page **5** of **6**

Posted: **Tue Mar 19, 2013 11:37 pm**

Use a priority_queue.

Posted: **Wed Mar 27, 2013 12:33 am**

i am getting time limit in 10954 . I have tried it with priority queue and vector both. Some suggestions are badly needed. Here are the codes:

Using Priority Queue:

Using Stack:

Using Priority Queue:

Code: Select all

```
#include<iostream>
#include<cstdio>
#include<stack>
#include<queue>
using namespace std;
int main()
{
long long i,n,x,y,sum1,sum2;
while(cin>>n && n)
{
priority_queue<long long>q;
stack<long long>s;
while(n--)
{
cin>>x;
q.push(x);
}
sum1=0;
sum2=0;
while(q.size()!=1)
{
if(q.size()>2)
{
s.push(q.top());
q.pop();
}
else
{
x=q.top();
q.pop();
y=q.top();
q.pop();
sum1=x+y;
sum2=sum2+sum1;
q.push(sum1);
while(!s.empty())
{
q.push(s.top());
s.pop();
}
}
}
cout<<sum2<<endl;
/*FILE* fp;
fp=fopen("G:\\g_output10954.txt","w");
fprintf(fp,"%lld\n",sum2);*/
}
return 0;
}
```

Using Stack:

Code: Select all

```
#include<iostream>
#include<cstdio>
#include<stack>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
long long i,n,x,y,sum1,sum2;
while(cin>>n && n)
{
vector<long long>v;
while(n--)
{
cin>>x;
v.push_back(x);
}
sum1=0;
sum2=0;
while(v.size()!=1)
{
sort(v.begin(),v.end());
x=v[0];
v.erase(v.begin()+0);
if(!v.empty())
{
y=v[0];
v.erase(v.begin()+0);
}
else
y=0;
sum1=x+y;
sum2=sum2+sum1;
v.push_back(sum1);
}
cout<<sum2<<endl;
}
return 0;
}
```

Posted: **Wed Mar 27, 2013 1:50 am**

Use just a priority_queue, you don't need a stack.

Posted: **Tue Sep 03, 2013 9:02 pm**

I just use a vector and at first got TLE. then i optimize the code and get AC. But it took 2.8 sec.Probably the worst runtime.

Posted: **Mon Feb 10, 2014 1:06 pm**

brianfry713,

Thanks for all your thoughts on this thread. It really helped me out a bunch.

Thanks for all your thoughts on this thread. It really helped me out a bunch.

Posted: **Wed Jul 23, 2014 10:08 am**

I am getting WA.I tested my output,but did not find any mistake.Please,anybody help me.Thanks in advance.Here is my code...

Code: Select all

```
got ac
```

Posted: **Wed Jul 23, 2014 12:09 pm**

I'd suggest you avoid using the "long" type in C/C++, since it behaves differently in different systems. In 32-bit systems it typically has 4-bytes, and in 64-bit systems it has 8. Consider if a 4-byte integer suffices to hold every possible answer for this problem.sampad74 wrote:I am getting WA.I tested my output,but did not find any mistake.Please,anybody help me.Thanks in advance.Here is my code...

Also, be careful with array overflows/underflows. Consider, for example, what happens when

Code: Select all

```
for (i = 2; i < n; i++)
a [i - 2] = a [i];
i = n - 3;
while (a [i] > key) // ....
```

Posted: **Wed Jul 23, 2014 12:59 pm**

lbv wrote--

Also, be careful with array overflows/underflows. Consider, for example, what happens when n=2 and this piece of code is executed:

for (i = 2; i < n; i++)

a= a;

i = n - 3;

while (a> key) // ....

i changed my code..

Code: Select all

I changed long to long long int ,but got WA again.

Posted: **Wed Jul 23, 2014 1:15 pm**

Please post the full, latest version of your code. For people wanting to help you, combining code from different posts to keep track of the changes is not exactly fun .sampad74 wrote:i changed my code.. (..)

but got WA again.

Posted: **Wed Jul 23, 2014 4:33 pm**

sorry...Here is my code.Thanks..

Code: Select all

```
got ac
```

Posted: **Wed Jul 23, 2014 7:55 pm**

You must check your program for input in this thread. It fails on this one
It must be
Don't forget to remove your code after getting accepted.

Change lineMartin Macko wrote:Not working for this one:Nazmul Quader Zinnuree wrote:OK, Noboby is giving me some I/O that i wanted in another mail.

Now, would you just plz run my code, and say where I'm wrong!!!My AC's output:Code: Select all

`8 40952 13521 6223 42589 93489 22376 78730 38792 0`

Code: Select all

`899661`

Code: Select all

`while (a [i] > key) {`

Code: Select all

`while (i > 0 && a[i] > key) {`

Posted: **Thu Jul 24, 2014 4:43 am**

My code gave right answer for the following test case before changing previous code at my computer ..i don't know why ...

Afterall,got AC.Thank you.8

40952 13521 6223 42589 93489 22376 78730 38792

0

My AC's output:

899661

Posted: **Tue Sep 30, 2014 11:15 am**

It gives exact output for sample input and some other critical inputs. I don't know why this code gets WA. . . .

Code: Select all

```
#include <stdio.h>
int main()
{
long long int n,sum,cost,i,a[99999],j,b;
while(scanf("%lld",&n)==1&&n!=0)
{
for(i=1;i<=n;i++)
scanf("%lld",&a[i]);
for(i=1;i<=n-1;i++)
{
for(j=1;j<=n-i;j++)
{
if(a[j]>a[j+1])
{
b=a[j];
a[j]=a[j+1];
a[j+1]=b;
}
}
}
sum=a[1],cost=0;
for(i=2;i<=n;i++)
{
sum=sum+a[i];
cost=cost+sum;
}
if(n==1)
cost=sum;
printf("%lld\n",cost);
}
return 0;
}
```

Posted: **Tue Sep 30, 2014 4:10 pm**

Input:Correct output 144

Code: Select all

```
6
9 9 9 9 9 9
0
```

Posted: **Wed Oct 01, 2014 7:38 am**

Please help me with sample input analysis:

For the 2nd sample input-

4

1 2 3 4

1+2=3, cost=3

3+3=6, cost=6

6+4=10, cost=10

Total= 19

So, for your input-

6

9 9 9 9 9 9

9 + 9=18, cost=18

18+9=27, cost=27

27+9=36, cost=36

36+9=45, cost=45

45+9=54, cost=54

Total=180

Shouldn't it be like that? How that can be 144?

For the 2nd sample input-

4

1 2 3 4

1+2=3, cost=3

3+3=6, cost=6

6+4=10, cost=10

Total= 19

So, for your input-

6

9 9 9 9 9 9

9 + 9=18, cost=18

18+9=27, cost=27

27+9=36, cost=36

36+9=45, cost=45

45+9=54, cost=54

Total=180

Shouldn't it be like that? How that can be 144?