谈判(贪心算法)

题目

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();
	}
}
相关推荐
点云SLAM1 分钟前
constexpr 和 explicit 在 C++ 中被提出的动机
开发语言·c++·explicit关键字·隐式转换·constexpr关键字·c++11/17/20
我命由我123451 分钟前
python-dotenv - python-dotenv 快速上手
服务器·开发语言·数据库·后端·python·学习·学习方法
黑客思维者1 分钟前
Python定时任务schedule/APScheduler/Crontab 原理与落地实践
开发语言·python·crontab·apscheduler
yaoxin5211237 分钟前
268. Java Stream API 入门指南
java·开发语言·python
ss27311 分钟前
ConcurrentLinkedQueue实战:电商秒杀系统的队列选型优化
java·开发语言·安全
繁星蓝雨14 分钟前
Qt优雅的组织项目结构三(使用CMakeLists进行模块化配置)——————附带详细示例代码
开发语言·数据库·qt
电饭叔21 分钟前
指定私有浮点数据域get/set方法等《python语言程序设计》2018版--第8章18题第1部分
开发语言·python
李绍熹37 分钟前
C语言基础语法示例
c语言·开发语言
BD_Marathon40 分钟前
【JavaWeb】Servlet_jar包导入和Content-Type问题
java·servlet·jar
kiki-bf41 分钟前
使用python把图片转为word
开发语言·python·word