#include <bits/stdc++.h>
#define fi first
#define se second
#define fin(s) freopen( s, "r", stdin );
#define fout(s) freopen( s, "w", stdout );
const long long N = 100100;
const long long Q = N * 30;
const long long mod = 998244353;
const long long MAGIC = 30;
using namespace std;
int n;
int k;
int a[N];
vector < int > v;
void solve()
{
cin >> n >> k;
long long ans = 0;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
sort(a + 1, a + n + 1);
for(int i = 2; i <= n; i++){
ans += a[i] - a[i - 1];
v.push_back(a[i] - a[i - 1]);
}
sort(v.begin(), v.end());
while(v.size() > n - k){
ans -= v.back();
v.pop_back();
}
cout << ans + k << "\n";
}
bool mtest = false; int main()
{
//fin("input.txt");
//fout("output.txt");
//fin("island.in");
//fout("island.out");
ios_base::sync_with_stdio(0);
int TE = 1;
if(mtest)
cin >> TE;
while(TE--)
solve();
return 0;
}
Compilation message
stove.cpp: In function 'void solve()':
stove.cpp:33:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(v.size() > n - k){
~~~~~~~~~^~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
248 KB |
Output is correct |
2 |
Correct |
3 ms |
360 KB |
Output is correct |
3 |
Correct |
2 ms |
400 KB |
Output is correct |
4 |
Correct |
2 ms |
516 KB |
Output is correct |
5 |
Correct |
3 ms |
516 KB |
Output is correct |
6 |
Correct |
2 ms |
520 KB |
Output is correct |
7 |
Correct |
3 ms |
540 KB |
Output is correct |
8 |
Correct |
2 ms |
544 KB |
Output is correct |
9 |
Correct |
2 ms |
548 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
248 KB |
Output is correct |
2 |
Correct |
3 ms |
360 KB |
Output is correct |
3 |
Correct |
2 ms |
400 KB |
Output is correct |
4 |
Correct |
2 ms |
516 KB |
Output is correct |
5 |
Correct |
3 ms |
516 KB |
Output is correct |
6 |
Correct |
2 ms |
520 KB |
Output is correct |
7 |
Correct |
3 ms |
540 KB |
Output is correct |
8 |
Correct |
2 ms |
544 KB |
Output is correct |
9 |
Correct |
2 ms |
548 KB |
Output is correct |
10 |
Correct |
5 ms |
552 KB |
Output is correct |
11 |
Correct |
4 ms |
584 KB |
Output is correct |
12 |
Correct |
6 ms |
712 KB |
Output is correct |
13 |
Correct |
4 ms |
712 KB |
Output is correct |
14 |
Correct |
4 ms |
748 KB |
Output is correct |
15 |
Correct |
3 ms |
756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
248 KB |
Output is correct |
2 |
Correct |
3 ms |
360 KB |
Output is correct |
3 |
Correct |
2 ms |
400 KB |
Output is correct |
4 |
Correct |
2 ms |
516 KB |
Output is correct |
5 |
Correct |
3 ms |
516 KB |
Output is correct |
6 |
Correct |
2 ms |
520 KB |
Output is correct |
7 |
Correct |
3 ms |
540 KB |
Output is correct |
8 |
Correct |
2 ms |
544 KB |
Output is correct |
9 |
Correct |
2 ms |
548 KB |
Output is correct |
10 |
Correct |
5 ms |
552 KB |
Output is correct |
11 |
Correct |
4 ms |
584 KB |
Output is correct |
12 |
Correct |
6 ms |
712 KB |
Output is correct |
13 |
Correct |
4 ms |
712 KB |
Output is correct |
14 |
Correct |
4 ms |
748 KB |
Output is correct |
15 |
Correct |
3 ms |
756 KB |
Output is correct |
16 |
Correct |
37 ms |
2708 KB |
Output is correct |
17 |
Correct |
32 ms |
3800 KB |
Output is correct |
18 |
Correct |
28 ms |
4644 KB |
Output is correct |
19 |
Correct |
35 ms |
5612 KB |
Output is correct |
20 |
Correct |
31 ms |
6704 KB |
Output is correct |
21 |
Correct |
33 ms |
7568 KB |
Output is correct |
22 |
Correct |
43 ms |
8540 KB |
Output is correct |
23 |
Correct |
38 ms |
9512 KB |
Output is correct |
24 |
Correct |
34 ms |
10476 KB |
Output is correct |