This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
#define all(x) (x).begin(), (x).end()
ll n, k, c;
vector<ll> v;
pair<ll, ll> solve() {
pair<ll, ll> dp[n];
dp[0] = {3 + c, 1};
for (int i = 1; i < n; i++) {
ll opt1 = dp[i - 1].first + c + 2;
ll opt2 = dp[i - 1].first + v[i] - v[i - 1] - 2;
if (opt1 < opt2) dp[i] = {opt1, dp[i - 1].second + 1};
else dp[i] = {opt2, dp[i - 1].second};
}
return dp[n - 1];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
v.resize(n);
for (int i = 0; i < n; i++) cin >> v[i];
sort(all(v));
ll lb = -1e14, rb = 1e14, res = 1e14;
while (lb + 1 < rb) {
c = (lb + rb) / 2;
pair<ll, ll> ans = solve();
if (ans.second > k) lb = c;
else {
res = min(res, ans.first - ans.second * c);
rb = c;
}
}
cout << res;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |