贪心:排序不等式

排队打水:

cpp 复制代码
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 100010;
typedef long long LL;

int n;
int t[N];//总时间

int main()
{
    scanf("%d",&n);
    for (int i = 0; i < n; i ++ )scanf("%d",&t[i]);
    
    sort(t, t+n);//从小到大排序

    LL res = 0;//答案
    
    for (int i = 0; i < n; i ++ )
    {
        res += t[i]*(n-1-i);//计算公式
    }
    printf("%lld\n",res);

    return 0;
}
相关推荐
phltxy5 小时前
C语言操作符详解
java·c语言·算法
RuoZoe6 小时前
从 2026 年 3 月 1 日开源,到 26.10.9:Jalium UI 半年时间到底走了多远?
c语言·c++
aqiu1111116 小时前
【LeetCode 902】最大为 N 的数字组合 - 详细题解与数位组合思路
数据结构·算法·leetcode·蓝桥杯·数位dp
辰烨chenye7 小时前
LeetCode Hot 100 题解 · 哈希篇
算法·leetcode·哈希算法
罗西的思考7 小时前
DreamZero 与 DreamDojo:世界模型与策略的分层协同综合分析与对比
人工智能·算法·机器学习
鱼子星_7 小时前
【C++】继承和多态(上)
c++·笔记
玖玥拾8 小时前
LeetCode 219 存在重复元素 II
算法·leetcode·哈希算法·散列表
知无不研9 小时前
c语言中循环的介绍与简单应用
c语言·开发语言·算法·循环·for·while
郝学胜-神的一滴9 小时前
Qt 高级编程 045:坐标体系深度实战
开发语言·c++·windows·python·qt·程序人生