谈判(贪心算法)

题目

java 复制代码
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {		
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();

		sc.nextLine();
		
    List<Integer> list = new ArrayList<>();
		for(int i=0;i<n;i++) {
			int x = sc.nextInt();
			list.add(x);
		}
		
    Collections.sort(list);//1 3 5 9
		
    int sum = 0;
		while(list.size()!=1) {
			int a = list.get(0);
			int b = list.get(1);
			int c = a+b;
			sum+=c;
			list.remove(0);//3 5 9
			list.remove(0);//5 9
			list.add(c);//5 9 4
			Collections.sort(list);//4 5 9
		}
		System.out.println(sum);
		sc.close();
	}
}
相关推荐
胚芽鞘68114 分钟前
查询依赖冲突工具maven Helper
java·数据库·maven
Charlie__ZS20 分钟前
若依框架去掉Redis
java·redis·mybatis
screenCui31 分钟前
macOS运行python程序遇libiomp5.dylib库冲突错误解决方案
开发语言·python·macos
linux kernel1 小时前
第七讲:C++中的string类
开发语言·c++
咖啡啡不加糖1 小时前
RabbitMQ 消息队列:从入门到Spring Boot实战
java·spring boot·rabbitmq
玩代码1 小时前
Java线程池原理概述
java·开发语言·线程池
NE_STOP1 小时前
SpringBoot--如何给项目添加配置属性及读取属性
java
水果里面有苹果1 小时前
20-C#构造函数--虚方法
java·前端·c#
%d%d21 小时前
python 在运行时没有加载修改后的版本
java·服务器·python
金銀銅鐵1 小时前
[Kotlin] 单例对象是如何实现的?
java·kotlin