给出n个章节,每个章节需要的时间不同,每学一个章节,时间会减少1,但不会为0,问你最少时间是多少。
排序后加起来即可。
AC代码:
#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 5;
int main(int argc, char const *argv[])
{
int n;
ll x;
while(scanf("%d%lld", &n, &x) != EOF) {
ll c[maxn];
for(int i = 0; i < n; ++i)
scanf("%lld", &c[i]);
sort(c, c + n);
ll ans = 0;
for(int i = 0; i < n; ++i) {
if(x == 1) ans += c[i];
else {
ans += (c[i] * x);
x--;
}
}
printf("%lld\n", ans);
}
return 0;
}