CodeForces 439B Devu, the Dumb Guy
2016-03-31 21:49
239 查看
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 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.
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.
Examples
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 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.
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 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.
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.
Examples
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 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.
#include<iostream>
#include<algorithm>
#define K 111111using namespace std;
int main(){
long long int n,x,i,t,sum;
long long int num[K];
while(cin>>n>>x){
sum=0;
t=x;
for(i=0;i<n;i++){
cin>>num[i];
}
sort(num,num+n);
for(i=0;i<n;i++){
if(t>1){
sum+=(num[i]*t);
t--;
}
else{
sum+=num[i];
}
}
cout<<sum<<endl;
}
return 0;
}
相关文章推荐
- codeforces 659B Qualifying Contest
- JFusionChartFree
- Webservice之schema
- codeforces 638B - Making Genome in Berland (STL&&DFS)
- Java设计模式小问题
- Linux命令(3):date命令
- 免费IT技术视频网站
- atomic和unatomic区别
- 23中设计模式
- 第五周学习总结
- Dom解析xml文件(3)
- Spring MVC之文件上传与下载
- 从控制台录入一个文件路径,将其拷贝到当前目录
- java包生成API遇乱码问题解决方法
- hdu 1025 Constructing Roads In JGShining's Kingdom
- 索引值匹配图片切换
- ubuntu 15.04 为eclipse创建快捷方式
- 神经网络:卷积神经网络CNN
- 构造函数的理解和应用场景
- JavaWeb学习笔记——XML解析