Fence Repair
题目
解析
看一下题目,突然发现这题就是合并果子的原题
那是不是就直接提交了呢?不,本题不保证答案在int范围内,记得开longlong
code:
#include<cstdio>
#define int long long
using namespace std;
inline void swap(int &x,int &y){x^=y^=x^=y;}
inline bool idigit(char x){return (x<'0'|x>'9')?0:1;}
inline int read()
{
int num=0,f=1;
char c=0;
while(!idigit(c=getchar())){if(c=='-')f=-1;}
while(idigit(c))num=(num<<1)+(num<<3)+(c&15),c=getchar();
return num*f;
}
inline void write(int x)
{
int F[20];
int tmp=x>0?x:-x;
if(x<0)putchar('-');
int cnt=0;
while(tmp>0){F[cnt++]=tmp%10+'0';tmp/=10;}
while(cnt>0)putchar(F[--cnt]);
if(x==0)putchar('0');
}
struct heap
{
int tot,a[10010];
inline void clean(){tot=0;}
inline void up(int x){while((x>>1)&&(a[x]<a[x>>1]))swap(a[x],a[x>>1]),x>>=1;}
inline void down(int x){int r;while((x<<1)<=tot){r=((x<<1|1)<=tot&&a[x<<1|1]<a[x<<1])?(x<<1|1):(x<<1);if(a[x]>a[r])swap(a[x],a[r]),x=r;else break;}}
inline int size(){return tot;}
inline int top(){return a[1];}
inline void pop(){swap(a[1],a[tot--]),down(1);}
inline void push(int x){a[++tot]=x,up(tot);}
}a;
int n,t,q,ans;
signed main()
{
n=read(),a.clean();
for(register int i=1;i<=n;++i)a.push(read());
//--n的循环次数为n-1,刚好堆中还剩下一个
while(--n)t=a.top(),a.pop(),q=a.top(),a.pop(),a.push(t+q),ans+=t+q;
write(ans);
return 0;
}