# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1123565 | hoainiem | Stove (JOI18_stove) | C++17 | 22 ms | 2632 KiB |
#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define task "file"
using namespace std;
const int inf = 1e15;
const int mx = 2e5 + 5;
int n, k;
long long a[mx];
pair<long long,int> dp[mx];
void read(){
cin >> n >> k;
for (int i = 1; i <= n; i++){
cin >> a[i];
}
sort(a + 1, a + n + 1);
}
pair<long long, int> choose(pair<long long, int> x, pair<long long, int> y) {
if (x.first < y.first) {
return x;
}
if (x.first > y.first) {
return y;
}
return (x.second < y.second) ? x : y;
}
void solve(){
long long l = 0, r = inf;
long long ans = -1;
while (l <= r) {
long long mid = (l + r) / 2;
dp[1] = {mid + 1, 1};
for (int i = 2; i <= n; i++){
dp[i] = choose(make_pair(dp[i - 1].first + 1 + mid, dp[i - 1].second + 1), make_pair(dp[i - 1].first + a[i] - a[i - 1], dp[i - 1].second));
}
if (dp[n].second <= k) {
ans = dp[n].first - mid * k;
r = mid - 1;
}
else {
l = mid + 1;
}
}
cout << ans;
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
if(fopen(task".inp", "r")){
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
read();
solve();
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |