#include "bits/stdc++.h"
using namespace std;
#define ll long long
#define ln '\n'
const int N = 1e5 + 5;
const int LG = 20;
const int INF = 2e9 + 5;
const int MOD = 998244353;
ll a[N];
priority_queue<pair<ll, ll>> pq;
vector<int> vec;
ll ans = 0LL;
void solve(){
int n, k; cin >> n >> k;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 1; i < n; i++){
pq.push({a[i] - a[i-1], i-1});
}
vec.push_back(-1);
int t = k;
while (--t && !pq.empty()){
// cout << pq.top().first << ' ' << pq.top().second << ln;
vec.push_back(pq.top().second);
pq.pop();
}
sort(vec.begin(), vec.end());
int x = vec.size();
// for (int i = 0; i < x; i++) cout << vec[i] << ' ';
// cout << ln;
for (int i = 1; i < x; i++){
// cout << a[vec[i]] << ' ' << a[vec[i-1]+1] << ln;
ans += a[vec[i]]- a[vec[i-1]+1];
}
ans += a[n-1] - a[vec[x-1]+1];
cout << ans + k << ln;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
// freopen("mooyomooyo.in", "r", stdin);
// freopen("mooyomooyo.out", "w", stdout);
// ll T; cin >> T;
// while (T--){
// solve();
// }
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
328 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
328 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
496 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
328 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
496 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
13 ms |
4212 KB |
Output is correct |
17 |
Correct |
12 ms |
4200 KB |
Output is correct |
18 |
Correct |
11 ms |
4176 KB |
Output is correct |
19 |
Correct |
18 ms |
4184 KB |
Output is correct |
20 |
Correct |
20 ms |
4176 KB |
Output is correct |
21 |
Correct |
27 ms |
4496 KB |
Output is correct |
22 |
Correct |
34 ms |
4560 KB |
Output is correct |
23 |
Correct |
29 ms |
4508 KB |
Output is correct |
24 |
Correct |
32 ms |
4512 KB |
Output is correct |