P1090 [NOIP2004 提高组] 合并果子 / [USACO06NOV] Fence Repair G
一.题目描述:
二.代码实现
import java.util.Arrays;
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
Arrays.sort(a);
int temp=0,flag=0,result=0;
for(int i=0;i<n-1;i++)
{
temp=a[i]+a[i+1];
result=result+temp;
flag=i+1;
while(flag<n&&a[flag]<temp)
{
a[flag-1]=a[flag];
flag++;
}
a[flag-1]=temp;
}
System.out.println(result);
}
}