Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int a[300000], n, k;
- int main()
- {
- ll ans, s = 0, f = 0;
- cin >> n >> k;
- for (int i = 0; i < n; i++)
- scanf("%d", &a[i]);
- sort(a, a + n);
- for (int i = 0; i < k; i++)
- {
- f += i * 1ll * a[i] - s;
- s += a[i];
- }
- ans = f;
- for (int i = k; i < n; i++)
- {
- s -= a[i - k];
- f -= -(k - 1) * 1ll * a[i - k] + s;
- f += (k - 1) * 1ll * a[i] - s;
- s += a[i];
- ans = min(ans, f);
- }
- cout << ans;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement