谈判(贪心算法)

题目

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();
	}
}
相关推荐
java1234_小锋1 天前
SpringBoot为什么要禁止循环依赖?
java·数据库·spring boot
如竟没有火炬1 天前
用队列实现栈
开发语言·数据结构·python·算法·leetcode·深度优先
折哥的程序人生 · 物流技术专研1 天前
《Java 100 天进阶之路》第17篇:Java常用包装类与自动装箱拆箱深入
java·开发语言·后端·面试
RH2312111 天前
2026.5.12 Linux
java·linux·数据结构
C+++Python1 天前
C 语言 动态内存分配:malloc /calloc/realloc /free
c语言·开发语言
水木流年追梦1 天前
大模型入门-应用篇3-Agent智能体
开发语言·python·算法·leetcode·正则表达式
小新同学^O^1 天前
简单学习 --> WebSocket
java·websocket·网络协议·学习
凯瑟琳.奥古斯特1 天前
假脱机技术原理详解
开发语言·职场和发展
敲代码的瓦龙1 天前
Java?枚举!!!
java·开发语言
NiceCloud喜云1 天前
IntelliJ IDEA 保姆级安装 + ClaudeAPI 配置教程
java·开发语言·前端·ide·chrome·docker·intellij-idea