谈判(贪心算法)

题目

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();
	}
}
相关推荐
无声旅者24 分钟前
深度解析 IDEA 集成 Continue 插件:提升开发效率的全流程指南
java·ide·ai·intellij-idea·ai编程·continue·openapi
Blossom.11837 分钟前
使用Python实现简单的人工智能聊天机器人
开发语言·人工智能·python·低代码·数据挖掘·机器人·云计算
da-peng-song44 分钟前
ArcGIS Desktop使用入门(二)常用工具条——数据框工具(旋转视图)
开发语言·javascript·arcgis
galaxy_strive1 小时前
qtc++ qdebug日志生成
开发语言·c++·qt
Ryan-Joee1 小时前
Spring Boot三层架构设计模式
java·spring boot
TNTLWT1 小时前
Qt功能区:简介与安装
开发语言·qt
Hygge-star1 小时前
【数据结构】二分查找5.12
java·数据结构·程序人生·算法·学习方法
dkmilk1 小时前
Tomcat发布websocket
java·websocket·tomcat
工一木子1 小时前
【Java项目脚手架系列】第七篇:Spring Boot + Redis项目脚手架
java·spring boot·redis
哞哞不熬夜2 小时前
JavaEE--初识网络
java·网络·java-ee