B. Devu, the Dumb Guy
来源:互联网 发布:网络技术员 编辑:程序博客网 时间:2024/06/04 18:26
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 in x 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.
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 a single integer representing the answer to the problem.
2 34 1
11
4 25 1 2 1
10
3 31 1 1
6
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 take 3 × 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.
解题说明:此题就从小到大排序,这样就能保证最少时间,但是要注意用long Long,用int不知道为何第12个测试用例一直过不去。
#include<iostream>#include<cstdio>#include<cmath>#include<cstdlib>#include <algorithm>#include<cstring>#include<string>using namespace std;long long a[100005];int main(){long long n, x;long long i;long long sum;cin >> n >> x;for (i = 0; i < n; i++){cin >> a[i];}sort(a, a + n);sum = 0;for (i = 0; i < n; i++){sum += a[i] * x;if (x != 1){x--;}}cout << sum << endl;return 0;}
- B. Devu, the Dumb Guy
- B. Devu, the Dumb Guy
- Codeforces 439B Devu, the Dumb Guy
- Codeforces 439 B. Devu, the Dumb Guy
- CodeForces 439B Devu, the Dumb Guy
- CodeForces 439B Devu, the Dumb Guy
- Codeforces 439B Devu, the Dumb Guy
- Devu, the Dumb Guy
- Codeforces 439B Devu, the Dumb Guy(贪心)
- Codeforces Round #251 (Div. 2) 439B Devu, the Dumb Guy(贪心)
- Codeforces Round #251(Div. 2) 439B. Devu, the Dumb Guy 贪心
- The first guy
- I'm the new guy here
- A. I Wanna Be the Guy
- 469A - I Wanna Be the Guy
- 【CodeForces】A. I Wanna Be the Guy
- Make Anonymous be the lucky guy
- Codeforces I Wanna Be the Guy
- iOS 基本编码格式转化
- unity 插件iTween 官方例子学习心得
- 学生信息管理系统(1)————全局关系
- Android 仿微信对话列表滑动删除效果
- poj2449Remmarguts' Date
- B. Devu, the Dumb Guy
- 对“仅通过崩溃地址找出源代码的出错行”一文的补充与改进
- Sql 同表同列一条sql显示所有组合
- mssql 实现排序上移下移
- Android视觉规范-间距规范与文字规范单位换算(dip、sp与px)
- 真正统治世界的十大算法
- crm2011创建DateTime类型的字段
- 正则表达式巧妙实现字符串去重
- 计科1111-1114班第二次实验作业(算法基本功 与 综合思考)