# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
699068 | Ran | Stove (JOI18_stove) | C++17 | 2 ms | 340 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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()
{
freopen("Stove.inp", "r", stdin);
freopen("Stove.out", "w", stdout);
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;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |