#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 100;
long long n, k, a[N], sum[N], c[N];
long long dp[5020][5020];
void sub2()
{
sort(a + 1, a + 1 + n);
dp[0][0] = 0;
for (int i = 1; i <= n; i++)
dp[i][0] = 1e18;
for (int j = 1; j <= k; j++)
{
long long ok = a[1] - dp[0][j - 1];
for (int i = 1; i <= n; i++)
{
dp[i][j] = (a[i] + 1) - ok;
ok = max(ok, a[i + 1] - dp[i][j - 1]);
}
}
cout << dp[n][k];
}
void sub()
{
sort(a + 1, a + 1 + n);
int m = 0;
for (int i = 1; i <= n; i++)
if (m == 0 || c[m] != a[i])
{
m++;
c[m] = a[i];
}
vector <long long> b;
for (int i = 2; i <= m; i++)
b.push_back(c[i] - c[i - 1] - 1);
sort(b.begin(), b.end());
long long ans = a[n] + 1 - a[1];
for (int i = b.size() - 1; i >= 0; i--)
{
if (k == 1)
break;
ans = ans - b[i];
k--;
}
cout << ans;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
cin >> n >> k;
for (int i = 1; i <= n; i++)
cin >> a[i];
if (n <= 5000)
sub2();
else
sub();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
6 ms |
12792 KB |
Output is correct |
11 |
Correct |
10 ms |
14932 KB |
Output is correct |
12 |
Correct |
45 ms |
36044 KB |
Output is correct |
13 |
Correct |
80 ms |
59468 KB |
Output is correct |
14 |
Correct |
114 ms |
80568 KB |
Output is correct |
15 |
Correct |
117 ms |
82612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
6 ms |
12792 KB |
Output is correct |
11 |
Correct |
10 ms |
14932 KB |
Output is correct |
12 |
Correct |
45 ms |
36044 KB |
Output is correct |
13 |
Correct |
80 ms |
59468 KB |
Output is correct |
14 |
Correct |
114 ms |
80568 KB |
Output is correct |
15 |
Correct |
117 ms |
82612 KB |
Output is correct |
16 |
Correct |
19 ms |
3916 KB |
Output is correct |
17 |
Correct |
19 ms |
4036 KB |
Output is correct |
18 |
Correct |
18 ms |
3924 KB |
Output is correct |
19 |
Correct |
18 ms |
3924 KB |
Output is correct |
20 |
Correct |
19 ms |
4044 KB |
Output is correct |
21 |
Correct |
19 ms |
4016 KB |
Output is correct |
22 |
Correct |
19 ms |
3924 KB |
Output is correct |
23 |
Correct |
19 ms |
4048 KB |
Output is correct |
24 |
Correct |
19 ms |
4028 KB |
Output is correct |