记录编号 348635 评测结果 WAAWWAWWWW
题目名称 社长的qwa 最终得分 30
用户昵称 Gravatariortheir 是否通过 未通过
代码语言 C++ 运行时间 0.021 s
提交时间 2016-11-14 15:30:11 内存使用 0.97 MiB
显示代码纯文本
#include<iostream>
#include<cstdio>
#include<algorithm>

using namespace std;

typedef long long ll;

const int maxn = 1e5 + 10;

int n;
int k;

ll A[maxn];

ll ans = (ll)1<<(62);
ll cnt = 0;
ll sum = 0;

int main()
{
	freopen("qwa.in","r",stdin);
	freopen("qwa.out","w",stdout);
	scanf("%d%d",&n,&k);
	for(int i = 1;i <= n ;i ++)
	{
		scanf("%lld",&A[i]);
	}
	sort(A + 1,A + 1 + n);
/*
	for(int i = 2;i <= n ;i ++)
	{
		dff[1] = 0x7fffffff;
		dff[i] = A[i] - A[i - 1];
		cout<<dff[i];
	}
*/
	for(int i = 1;i <= k ;i ++)
	{
		cnt = cnt + (A[i] * i - A[i] - sum);
//		cout<<cnt<<endl;
		sum = sum + A[i];
//		cout<<sum<<endl;
	}
	for(int i = k + 1;i <= n;i ++)
	{
		ans = min(ans,cnt);
		cnt = cnt - (sum - k * A[i - k]);
//		cout<<sum<<endl;
//		cout<<cnt<<endl;
		sum = sum + (A[i] - A[i - k]);
		cnt = cnt + (k * A[i] - sum);
	}
	ans = min(ans,cnt);
	cout<<ans;
	return 0;
}
/*
4 3
1 2 3 8
*/
/*
10 3
1 2 3 8 8 1 2 3 8 8
*/