Skip to content

Commit b626f86

Browse files
author
sundereshwar
committed
Hacktoberfest2019 SunderEshwar
1 parent e2ba3a7 commit b626f86

1 file changed

Lines changed: 135 additions & 0 deletions

File tree

cirmerg.cpp

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
unsigned long int small(unsigned long int array[], unsigned long int size)
5+
{
6+
unsigned long int index = 0;
7+
for(unsigned long int i = 1; i < size; i++)
8+
{
9+
if(array[i] < array[index])
10+
index = i;
11+
}
12+
return index;
13+
}
14+
15+
int testcase()
16+
{
17+
unsigned long int i,n,s,ans=0,m;
18+
cin>>n;
19+
unsigned long int a[n];
20+
for(i=0;i<n;i++)
21+
{
22+
cin>>a[i];
23+
}
24+
s=n;
25+
if(n>1)
26+
{
27+
do{
28+
m=small(a,s);
29+
//cout<<m<<" "<<s<<endl;
30+
if(m==0)
31+
{
32+
if(a[s]<=a[m+1])
33+
{
34+
a[s]+=a[m];
35+
ans+=a[s];
36+
for(i=m;i<s;i++)
37+
{
38+
a[i]=a[i+1];
39+
}
40+
s--;
41+
}
42+
else
43+
{
44+
a[m]+=a[m+1];
45+
ans+=a[m];
46+
for(i=m+1;i<s;i++)
47+
{
48+
a[i]=a[i+1];
49+
}
50+
s--;
51+
}
52+
}
53+
else if(m==s-1)
54+
{
55+
if(a[m-1]<=a[0])
56+
{
57+
a[m-1]+=a[m];
58+
ans+=a[m-1];
59+
for(i=m;i<s;i++)
60+
{
61+
a[i]=a[i+1];
62+
}
63+
s--;
64+
}
65+
else
66+
{
67+
a[m]+=a[0];
68+
ans+=a[m];
69+
for(i=0;i<s;i++)
70+
{
71+
a[i]=a[i+1];
72+
}
73+
s--;
74+
}
75+
}
76+
else
77+
{
78+
if(a[m-1]<=a[m+1])
79+
{
80+
a[m-1]+=a[m];
81+
ans+=a[m-1];
82+
for(i=m;i<s;i++)
83+
{
84+
a[i]=a[i+1];
85+
}
86+
s--;
87+
}
88+
else
89+
{
90+
a[m]+=a[m+1];
91+
ans+=a[m];
92+
for(i=m+1;i<s;i++)
93+
{
94+
a[i]=a[i+1];
95+
}
96+
s--;
97+
}
98+
}
99+
/* for(i=0;i<s;i++)
100+
{
101+
cout<<"a["<<i<<"]="<<a[i]<<" ";
102+
}
103+
cout<<endl;*/
104+
}while(s>1);
105+
cout<<ans;
106+
}
107+
else{
108+
if(n<=0)
109+
{
110+
cout<<0;
111+
}
112+
else
113+
{
114+
cout<<a[0];
115+
}
116+
}
117+
/*for(i=0;i<s;i++)
118+
{
119+
cout<<a[i]<<" ";
120+
}
121+
cout<<endl<<" "<<s<<" "<<ans;*/
122+
return 0;
123+
}
124+
125+
int main()
126+
{
127+
int t,i;
128+
cin>>t;
129+
for(i=0;i<t;i++)
130+
{
131+
testcase();
132+
cout<<endl;
133+
}
134+
return 0;
135+
}

0 commit comments

Comments
 (0)