#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';
}
void solve2() {
int n, m;
priority_queue<int> pq;
cin >> n >> m;
int last, ans;
cin >> last;
ans = -last + 1;
for (int i = 1; i < n; i++) {
int x;
cin >> x;
pq.emplace(x - last - 1);
last = x;
}
ans += last;
int cnt = 1;
m = min(m, n);
while (cnt + 1 <= m) {
ans -= pq.top();
pq.pop();
cnt++;
}
cout << ans << '\n';
}
signed main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
solve2();
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 |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 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 |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 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 |
2 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 |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 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 |
2 ms |
332 KB |
Output is correct |
16 |
Correct |
14 ms |
976 KB |
Output is correct |
17 |
Correct |
14 ms |
988 KB |
Output is correct |
18 |
Correct |
18 ms |
976 KB |
Output is correct |
19 |
Correct |
16 ms |
944 KB |
Output is correct |
20 |
Correct |
20 ms |
976 KB |
Output is correct |
21 |
Correct |
24 ms |
908 KB |
Output is correct |
22 |
Correct |
25 ms |
976 KB |
Output is correct |
23 |
Correct |
25 ms |
916 KB |
Output is correct |
24 |
Correct |
26 ms |
960 KB |
Output is correct |