【cf439B】Devu, the Dumb Guy

描述 Description

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.

输入格式 InputFormat

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).

输出格式 OutputFormat

Output a single integer representing the answer to the problem.

样例输入 SampleInput

4 2
5 1 2 1

样例输出 SampleOutput

10


Codeforces 439B


代码 Code

忘加强制转换结果计算过程爆 int 了。。。

#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int a[100005];
int i,j,t,n,m,l,r,k,z,y,x;
long long ans;
int main()
{
    scanf("%d%d",&n,&x);
    for (i=1;i<=n;i++) scanf("%d",&a[i]);
    sort(a+1,a+n+1);
    ans=0;
    for (i=1;i<=n;i++)
    {
        ans+=(long long)a[i]*x;
        if (x>1) x--;
    }
    printf("%I64d",ans);
    return 0;
}