比赛 20161114 评测结果 WAAWWAWWWW
题目名称 社长的qwa 最终得分 30
用户昵称 iortheir 运行时间 0.046 s
代码语言 C++ 内存使用 0.75 MiB
提交时间 2016-11-14 11:16:55
显示代码纯文本
#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 += A[i] * i - A[i] - sum;
//		cout<<cnt<<endl;
		sum += A[i];
//		cout<<sum<<endl;
	}
	for(int i = k + 1;i <= n;i ++)
	{
		ans = min(ans,cnt);
		cnt -= sum - k * A[i - k];
//		cout<<sum<<endl;
//		cout<<cnt<<endl;
		sum += A[i] - A[i - k];
		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
*/