#include <bits/stdc++.h>
#define N 100010
#define K 110
#define inf 200000000000000000LL
using namespace std;
typedef long long ll;
ll n, k, v[N], pref[N], dp[N][K], best[N][K];
int main()
{
ios::sync_with_stdio(false); cin.tie(0);
cin>>n>>k;
for(int i = 1; i <= n; i++) cin>>v[i];
for(int i = 0; i < N; i++)
for(int j = 0; j < K; j++)
dp[i][j] = inf;
dp[0][1] = 0;
for(int i = 1; i <= n; i++)
{
dp[i][1] = max(dp[i - 1][1], v[i]);
best[i][1] = dp[i][1];
pref[i] = pref[i - 1] + v[i];
}
for(int q = 2; q <= k; q++)
{
for(int i = 1; i <= n; i++)
{
if(i < q) dp[i][q] = inf;
if(i == q)
{
dp[i][q] = pref[i];
best[i][q] = v[i];
continue;
}
ll A = dp[i - 1][q] + max(v[i] - best[i - 1][q], 0LL);
ll B = dp[i - 1][q - 1] + v[i];
if(A <= B) best[i][q] = max(v[i], best[i - 1][q]);
else best[i][q] = v[i];
dp[i][q] = min(A, B);
}
}
cout<<dp[n][k]<<'\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
76 ms |
86520 KB |
Output is correct |
2 |
Correct |
63 ms |
86520 KB |
Output is correct |
3 |
Correct |
61 ms |
86632 KB |
Output is correct |
4 |
Correct |
64 ms |
86764 KB |
Output is correct |
5 |
Correct |
64 ms |
86796 KB |
Output is correct |
6 |
Correct |
76 ms |
86796 KB |
Output is correct |
7 |
Correct |
68 ms |
86796 KB |
Output is correct |
8 |
Correct |
68 ms |
86796 KB |
Output is correct |
9 |
Correct |
62 ms |
86796 KB |
Output is correct |
10 |
Correct |
70 ms |
86796 KB |
Output is correct |
11 |
Incorrect |
75 ms |
86796 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
86796 KB |
Output is correct |
2 |
Correct |
59 ms |
86796 KB |
Output is correct |
3 |
Correct |
61 ms |
86796 KB |
Output is correct |
4 |
Correct |
71 ms |
86796 KB |
Output is correct |
5 |
Correct |
61 ms |
86796 KB |
Output is correct |
6 |
Correct |
62 ms |
86796 KB |
Output is correct |
7 |
Correct |
61 ms |
86796 KB |
Output is correct |
8 |
Correct |
72 ms |
86796 KB |
Output is correct |
9 |
Correct |
69 ms |
86796 KB |
Output is correct |
10 |
Correct |
68 ms |
86796 KB |
Output is correct |
11 |
Incorrect |
68 ms |
86796 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
70 ms |
86796 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
74 ms |
95412 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |