谈判(贪心算法)

题目

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();
	}
}
相关推荐
芯岭技术郦7 小时前
MS32C001‑C:极致成本 32 位 MCU
c语言·开发语言·单片机
程序员小羊!7 小时前
17 Maven
java·maven
C+-C资深大佬7 小时前
C++ 数字与字符串互转
java·c++·算法
nexustech7 小时前
simplejson:Python JSON 处理的备用引擎
开发语言·python·其他·json
陈猪的杰咪7 小时前
DeepSeek V4 中转方案全解析 | Flash 成本仅为 GPT 的 1/90
java·人工智能·gpt·spring
zlpzlpzyd7 小时前
spring boot 4.1发布
java·数据库·spring boot
雷工笔记7 小时前
MES系列48-MES 系统「质量管理」完整设计与实施方案
开发语言·javascript·ecmascript
无籽西瓜a7 小时前
Plan-and-Execute 里的 DAG 是怎么工作的
java·后端·ai·agent·dag
ch.ju7 小时前
Java Programming Chapter 4——The difference between overloading and overwriting.
java·开发语言
我命由我123457 小时前
Android 开发问题:View 的 getWidth、getHeight 方法返回的值都为 0
android·java·java-ee·android studio·android jetpack·android-studio·android runtime