#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;
pair < long long, int > d[N];
pair < long long, int > get(long long f)
{
pair < long long, int > mn = {- a[1], 0};
for(int i = 1; i <= n; i++){
d[i] = {mn.fi + a[i] + f, mn.se + 1};
mn = min(mn, {d[i].fi - a[i + 1], d[i].se});
}
return d[n];
}
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);
long long l = 0, r = 1e12;
while(l < r){
long long m = (l + r) / 2;
if(get(m).se > k){
l = m + 1;
}
else{
r = m;
}
}
cout << get(l).fi - k * l + 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:34:19: warning: unused variable 'ans' [-Wunused-variable]
long long ans = 0;
^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
452 KB |
Output is correct |
3 |
Correct |
3 ms |
452 KB |
Output is correct |
4 |
Correct |
3 ms |
488 KB |
Output is correct |
5 |
Correct |
4 ms |
564 KB |
Output is correct |
6 |
Correct |
2 ms |
564 KB |
Output is correct |
7 |
Correct |
2 ms |
620 KB |
Output is correct |
8 |
Correct |
3 ms |
620 KB |
Output is correct |
9 |
Correct |
3 ms |
620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
452 KB |
Output is correct |
3 |
Correct |
3 ms |
452 KB |
Output is correct |
4 |
Correct |
3 ms |
488 KB |
Output is correct |
5 |
Correct |
4 ms |
564 KB |
Output is correct |
6 |
Correct |
2 ms |
564 KB |
Output is correct |
7 |
Correct |
2 ms |
620 KB |
Output is correct |
8 |
Correct |
3 ms |
620 KB |
Output is correct |
9 |
Correct |
3 ms |
620 KB |
Output is correct |
10 |
Correct |
4 ms |
620 KB |
Output is correct |
11 |
Correct |
5 ms |
620 KB |
Output is correct |
12 |
Correct |
4 ms |
620 KB |
Output is correct |
13 |
Correct |
5 ms |
668 KB |
Output is correct |
14 |
Correct |
4 ms |
668 KB |
Output is correct |
15 |
Correct |
4 ms |
668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
452 KB |
Output is correct |
3 |
Correct |
3 ms |
452 KB |
Output is correct |
4 |
Correct |
3 ms |
488 KB |
Output is correct |
5 |
Correct |
4 ms |
564 KB |
Output is correct |
6 |
Correct |
2 ms |
564 KB |
Output is correct |
7 |
Correct |
2 ms |
620 KB |
Output is correct |
8 |
Correct |
3 ms |
620 KB |
Output is correct |
9 |
Correct |
3 ms |
620 KB |
Output is correct |
10 |
Correct |
4 ms |
620 KB |
Output is correct |
11 |
Correct |
5 ms |
620 KB |
Output is correct |
12 |
Correct |
4 ms |
620 KB |
Output is correct |
13 |
Correct |
5 ms |
668 KB |
Output is correct |
14 |
Correct |
4 ms |
668 KB |
Output is correct |
15 |
Correct |
4 ms |
668 KB |
Output is correct |
16 |
Correct |
53 ms |
2796 KB |
Output is correct |
17 |
Correct |
48 ms |
2796 KB |
Output is correct |
18 |
Correct |
43 ms |
2796 KB |
Output is correct |
19 |
Correct |
48 ms |
2796 KB |
Output is correct |
20 |
Correct |
57 ms |
2796 KB |
Output is correct |
21 |
Correct |
48 ms |
2796 KB |
Output is correct |
22 |
Correct |
50 ms |
2796 KB |
Output is correct |
23 |
Correct |
51 ms |
2796 KB |
Output is correct |
24 |
Correct |
52 ms |
2796 KB |
Output is correct |