记录编号 348917 评测结果 AAAAAAAAAA
题目名称 社长的qwa 最终得分 100
用户昵称 GravatarJanis 是否通过 通过
代码语言 C++ 运行时间 0.016 s
提交时间 2016-11-14 17:25:41 内存使用 0.97 MiB
显示代码纯文本
/*
膜拜神牛Rapiz!
膜拜神牛KZNS!
膜拜神牛农场主!
(排名不分先后)(反正都是神牛)
*/

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
#define debug(x) cout << #x << ":" << x << "\n";
const int maxn = 100010;
const ll ool = (ll)1<<62;
int n,k;
ll a[maxn];
ll ans = ool;

int main(){
	#ifndef DEBUG
		string FileName="qwa";
		freopen((FileName+".in").c_str(),"r",stdin);
		freopen((FileName+".out").c_str(),"w",stdout);
	#endif
	scanf("%d%d",&n,&k);
	for(int i = 0; i < n; i++)
		scanf("%lld",&a[i]);
//		scanf("%I64d",&a[i]);
	sort(a,a+n);
	ll now = 0, jian = a[0];
	for(int i = 1; i < k; i++){
		now = now+a[i]*i;
		now = now-jian;
		jian = jian+a[i];
	}
	ans = min(ans, now);
	for(int i = k; i < n; i++){
		now = now-jian;
		now = now+a[i-k]*k;
		jian = jian-a[i-k];
		now = now+a[i]*(k-1)-jian;
		jian = jian+a[i];
		ans = min(ans, now);
	}
//	printf("%I64d",ans);
	printf("%lld",ans);
}