Codeforces Round #251(Div. 2) 439B. Devu, the Dumb Guy 贪心

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
2 3
4 1
output
11
input
4 2
5 1 2 1
output
10
input
3 3
1 1 1
output
6
题意:
要教n门课,第i门课又分为Ci小节,每小节的教学用时相同,都是x,另外,如果在教了第i门课之后,第i+1门课的每一小节会比第i门课的每一小节的用时少1,但是至少要为1,问这么安排教课的顺序,使得时间最短。

题解:
贪心,先教Ci小的。
可以通过相邻交换法来证明。


/****************
*PID:439b div2
*Auth:Jonariguez
*****************
*/
#define lson k*2,l,m
#define rson k*2+1,m+1,r
#define rep(i,s,e) for(i=(s);i<=(e);i++)
#define For(j,s,e) For(j=(s);j<(e);j++)
#define sc(x) scanf("%d",&x)
#define In(x) scanf("%I64d",&x)
#define pf(x) printf("%d",x)
#define pfn(x) printf("%d\n",(x))
#define Pf(x) printf("%I64d",(x))
#define Pfn(x) printf("%I64d\n",(x))
#define Pc printf(" ")
#define PY puts("YES")
#define PN puts("NO")
#include <stdio.h>
#include <string.h>
#include <string>
#include <math.h>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef int Ll;
Ll quick_pow(Ll a,Ll b,Ll MOD){a%=MOD;Ll res=1;while(b){if(b&1)res=(res*a)%MOD;b/=2;a=(a*a)%MOD;}return res;}

const int maxn=100000+10;
int a[maxn];

int main()
{
    int i,j,n,x;
    while(scanf("%d%d",&n,&x)!=EOF){
        for(i=0;i<n;i++) sc(a[i]);
        sort(a,a+n);
        LL res=0;
        for(i=0;i<n;i++){
            res+=(LL)x*a[i];
            x--;
            x=max(x,1);
        }
        Pfn(res);
    }
    return 0;
}


你可能感兴趣的:(codeforces)