Devu, the Dumb Guy

CodeForces - 439B

Devu is a dumb guy, his learning curve is very slow. You are supposed to teach him nsubjects, 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 nx (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.

sol:较显然的贪心,从小到大排序后先学小的再学大的

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=;
int n;
long long X,A[N];
int main()
{
int i;
ll ans=;
R(n); R(X);
for(i=;i<=n;i++) R(A[i]);
sort(A+,A+n+);
for(i=;i<=n;i++)
{
ans+=1ll*X*A[i];
if(X>) X--;
}
Wl(ans);
return ;
}
/*
input
2 3
4 1
output
11 input
4 2
5 1 2 1
output
10 input
3 3
1 1 1
output
6
*/

codeforces439B的更多相关文章

随机推荐

  1. 迭代和JDB调试

    迭代和JDB调试 题目要求 1 使用C(n,m)=C(n-1,m-1)+C(n-1,m)公式进行递归编程实现求组合数C(m,n)的功能 2 m,n 要通过命令行传入 3 提交测试运行截图(至少三张:正 ...

  2. Android学习之六种事件响应方法汇总

    java源码如下: 1.MainActivity.java源码 package com.example.responsetest; import android.app.Activity; impor ...

  3. Android2.3系统 自定义的PopupWindow在实例化时报空指针异常

    情况:是这样的,前段时间做了一个自定义的PopupWindow,就是写一个类,然后继承PopupWindow,别的什么操作都没有,但是在实例化的时候,在2.3系统中直接就报空指针异常(4.0及以上系统 ...

  4. c语言学习5

    break 和 continue之间的区别: 在1000人中,募捐100000元,当达到10万元后结束   break 跳出当前循环,即  是终止循环,continue结束本次循环,不终止循环 #in ...

  5. Luogu P2522 [HAOI2011]Problem b

    如果你做过[Luogu P3455 POI2007]ZAP-Queries就很好办了,我们发现那一题求的是\(\sum_{i=1}^a\sum_{j=1}^b[\gcd(i,j)=d]\),就是这道题 ...

  6. 浅谈java反射机制

    目录 什么是反射 初探 初始化 类 构造函数 属性 方法 总结 思考 什么是反射 JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意 ...

  7. ExtJS框架基础:事件模型及其常用功能

    前言 工作中用ExtJS有一段时间了,Ext丰富的UI组件大大的提高了开发B/S应用的效率.虽然近期工作中天天都用到ExtJS,但很少对ExtJS框架原理性的东西进行过深入学习,这两天花了些时间学习了 ...

  8. 解决 webpack-dev-server 不能自动刷新的问题

    原文发表于我的技术博客 此文主要帮助大家解决 webpack-dev-server 启动后修改源文件浏览器不能自动刷新的问题. 原文发表于我的技术博客 1. webpack 不能热加载的问题 主要的问 ...

  9. Nginx入门【转】

    原文地址:http://blog.csdn.net/u012486840/article/details/53098890 1.静态HTTP服务器 首先,Nginx是一个HTTP服务器,可以将服务器上 ...

  10. python基础学习笔记(十二)

    模块 前面有简单介绍如何使用import从外部模块获取函数并且为自己的程序所用: >>> import math >>> math.sin(0) #sin为正弦函数 ...