Codeforces Round #251 (Div. 2) 439B Devu, the Dumb Guy(贪心)
2015-08-29 07:38
525 查看
B. Devu, the Dumb Guy
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Devu is a dumb guy, his learning curve is very slow. You are supposed to teach him n subjects, the ith subject
has ci chapters.
When you teach him, you are supposed to teach all the chapters of a subject continuously.
Let us say that his initial per chapter learning power of a subject is x hours. In other words he can learn a chapter of a particular
subject inx hours.
Well Devu is not complete dumb, there is a good thing about him too. If you teach him a subject, then time required to teach any chapter of the next subject will require exactly 1 hour less than previously required (see the examples to understand it more clearly).
Note that his per chapter learning power can not be less than 1 hour.
You can teach him the n subjects in any possible order. Find out minimum amount of time (in hours) Devu will take to understand all
the subjects and you will be free to do some enjoying task rather than teaching a dumb guy.
Please be careful that answer might not fit in 32 bit data type.
Input
The first line will contain two space separated integers n, x (1 ≤ n, x ≤ 105).
The next line will contain n space separated integers:c1, c2, ..., cn (1 ≤ ci ≤ 105).
Output
Output a single integer representing the answer to the problem.
Sample test(s)
input
output
input
output
input
output
Note
Look at the first example. Consider the order of subjects: 1, 2.
When you teach Devu the first subject, it will take him 3 hours per chapter, so it will take 12 hours
to teach first subject. After teaching first subject, his per chapter learning time will be 2 hours. Now teaching him second subject will take 2 × 1 = 2 hours.
Hence you will need to spend 12 + 2 = 14 hours.
Consider the order of subjects: 2, 1.
When you teach Devu the second subject, then it will take him 3 hours per chapter, so it will take3 × 1 = 3 hours
to teach the second subject. After teaching the second subject, his per chapter learning time will be 2 hours. Now teaching him the first subject
will take 2 × 4 = 8 hours. Hence you will need to spend 11 hours.
So overall, minimum of both the cases is 11 hours.
Look at the third example. The order in this example doesn't matter. When you teach Devu the first subject, it will take him 3 hours per chapter.
When you teach Devu the second subject, it will take him 2 hours per chapter. When you teach Devu the third subject, it will take him 1 hours
per chapter. In total it takes 6 hours.
给出n个章节,每个章节需要的时间不同,每学一个章节,时间会减少1,但不会为0,问你最少时间是多少。
排序后加起来即可。
AC代码:
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Devu is a dumb guy, his learning curve is very slow. You are supposed to teach him n subjects, the ith subject
has ci chapters.
When you teach him, you are supposed to teach all the chapters of a subject continuously.
Let us say that his initial per chapter learning power of a subject is x hours. In other words he can learn a chapter of a particular
subject inx hours.
Well Devu is not complete dumb, there is a good thing about him too. If you teach him a subject, then time required to teach any chapter of the next subject will require exactly 1 hour less than previously required (see the examples to understand it more clearly).
Note that his per chapter learning power can not be less than 1 hour.
You can teach him the n subjects in any possible order. Find out minimum amount of time (in hours) Devu will take to understand all
the subjects and you will be free to do some enjoying task rather than teaching a dumb guy.
Please be careful that answer might not fit in 32 bit data type.
Input
The first line will contain two space separated integers n, x (1 ≤ n, x ≤ 105).
The next line will contain n space separated integers:c1, c2, ..., cn (1 ≤ ci ≤ 105).
Output
Output a single integer representing the answer to the problem.
Sample test(s)
input
2 3 4 1
output
11
input
4 2 5 1 2 1
output
10
input
3 3 1 1 1
output
6
Note
Look at the first example. Consider the order of subjects: 1, 2.
When you teach Devu the first subject, it will take him 3 hours per chapter, so it will take 12 hours
to teach first subject. After teaching first subject, his per chapter learning time will be 2 hours. Now teaching him second subject will take 2 × 1 = 2 hours.
Hence you will need to spend 12 + 2 = 14 hours.
Consider the order of subjects: 2, 1.
When you teach Devu the second subject, then it will take him 3 hours per chapter, so it will take3 × 1 = 3 hours
to teach the second subject. After teaching the second subject, his per chapter learning time will be 2 hours. Now teaching him the first subject
will take 2 × 4 = 8 hours. Hence you will need to spend 11 hours.
So overall, minimum of both the cases is 11 hours.
Look at the third example. The order in this example doesn't matter. When you teach Devu the first subject, it will take him 3 hours per chapter.
When you teach Devu the second subject, it will take him 2 hours per chapter. When you teach Devu the third subject, it will take him 1 hours
per chapter. In total it takes 6 hours.
给出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; }
相关文章推荐
- 分支01 —— 分支10
- svchost.exe大量占用CPU和内存的解决办法
- 三言两语来学C-壹
- Android项目结构搭建详解
- TC图形化的GRAPHICS的封装
- 极光推送Couldn't load jpush180 from loader dalvik.system.PathClassLoader解决方法
- 14个Xcode中常用的快捷键操作
- MySQL日志滚动
- 线段树 POJ2777 Count Color 解题报告
- django 1.8 官方文档翻译: 3-1-3 Django 的快捷函数
- MapReduce 初识
- LeetCode "Strobogrammatic Number III"
- 吸粉、品宣、转化在微信营销中的注意事项
- del语句
- OSChina 周六乱弹 —— 鬼节最怕碰见什么鬼?
- django 1.8 官方文档翻译: 3-1-1 URL调度器
- [CareerCup] 7.3 Line Intersection 直线相交
- 【LeetCode-面试算法经典-Java实现】【225-Implement Stack using Queues(用队列实现栈操作)】
- 【LeetCode-面试算法经典-Java实现】【223-Rectangle Area(矩形区域)】
- Some projects cannot be imported because they already exist in the workspace