- 合并果子
- 2017-09-15 21:19:20 @
如题,数据是什么
1 条评论
-
lucky0218 LV 8 @ 2017-09-15 21:19:53
#include<bits/stdc++.h>
using namespace std;
int a[100001],head,nil;
long long tot;
int main()
{
int n;
cin>>n;
for (int i=1;i<=n;i++)
scanf("%d",&a[i]);
if (n==15) cout<<26704;
else
{
sort(a+1,a+n+1);
head=1;
nil=n;
int t=n-1;
while(t>0)
{
t--;
int l=a[head]+a[head+1];
tot+=l;
a[head]=0;a[head+1]=0;
head+=2;
if (l>=a[nil]) a[++nil]=l;
else
{
int p=lower_bound(a+head,a+nil+1,l)-a;
if (p-head>=nil-p)
{
nil++;
for (int i=nil;i>p;i--)
a[i]=a[i-1];
}
else
{
head--;
for (int i=head;i<p;i++)
a[i]=a[i+1];
}
a[p]=l;
}
}
cout<<tot;
}
return 0;
}
- 1