#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
int arr[maxn];
int n, m;
pair<int, int> DP(int p) {
static int dp[maxn] = {};
static int cnt[maxn] = {};
int pre = 0;
for (int i = 1; i <= n; i++) {
dp[i] = dp[pre] + (arr[i] - arr[pre+1] + 1) + p;
cnt[i] = cnt[pre] + 1;
if (dp[i] - arr[i+1] < dp[pre] - arr[pre+1]) pre = i;
}
return {dp[n] - cnt[n] * p, cnt[n]};
}
void solve() {
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> arr[i];
m = min(m, n);
int l = 0, r = arr[n];
while (l <= r) {
int v, c;
int mid = (l+r)>>1;
tie(v, c) = DP(mid);
if (c <= m) r = mid-1;
else l = mid+1;
}
long long lv, lc, rv, rc;
tie(lv, lc) = DP(l);
tie(rv, rc) = DP(r);
if (lc == rc) cout << lv << '\n';
else cout << lv + (rv - lv) * (m - lc) / (rc - lc) << '\n';
}
signed main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
33 ms |
2436 KB |
Output is correct |
17 |
Correct |
31 ms |
2380 KB |
Output is correct |
18 |
Correct |
32 ms |
2460 KB |
Output is correct |
19 |
Correct |
33 ms |
2316 KB |
Output is correct |
20 |
Correct |
35 ms |
2400 KB |
Output is correct |
21 |
Correct |
31 ms |
2380 KB |
Output is correct |
22 |
Correct |
31 ms |
2456 KB |
Output is correct |
23 |
Correct |
31 ms |
2380 KB |
Output is correct |
24 |
Correct |
32 ms |
2552 KB |
Output is correct |