比赛 20161114 评测结果 AAAAAAAAAA
题目名称 社长的qwa 最终得分 100
用户昵称 KZNS 运行时间 0.059 s
代码语言 C++ 内存使用 0.95 MiB
提交时间 2016-11-14 11:12:42
显示代码纯文本
//KZNS
#include <cstdio>
#include <algorithm>
using namespace std;
#define Nmax 100005
typedef long long LL;
int N, K;
LL ls[Nmax];
int main() {
    freopen("qwa.in", "r", stdin);
    freopen("qwa.out", "w", stdout);
    scanf("%d %d", &N, &K);
    for (int i = 0; i < N; i++)
        scanf("%lld", ls+i);
    sort(ls, ls+N);
    LL ans = 0x7fffffffffffffffll;
    LL sm = 0;
    LL lss = ls[0];
    for (int i = 1; i < K; i++) {
        sm += ls[i] * i - lss;
        lss += ls[i];
    }
    ans = min(ans, sm);
    for (int i = K; i < N; i++) {
        sm -= lss - ls[i - K] * K;
        lss -= ls[i - K];
        sm += ls[i] * (K-1) - lss;
        lss += ls[i];
        ans = min(ans, sm);
    }
    printf("%lld\n", ans);
    return 0;
}
//UBWH