#include <bits/stdc++.h>
using namespace std;
#define s second
#define f first
typedef long long ll;
const int N = 100005, M = 105;
int n, k, a[N], dp[N][M], ch[N][M];
int main()
{
cin >> n >> k;
for(int i=1; i<=n; i++) cin >> a[i];
memset(dp, 0x3f3f3f3f, sizeof dp);
dp[0][0] = 0;
for(int i=1; i<=k; i++)
{
stack<int> s;
for(int j=1; j<=n; j++)
{
dp[i][j] = dp[i-1][j-1]+ a[j], ch[i][j] = a[j];
while(!s.empty())
{
int u = s.top();
if(a[u] <= a[j])
{
if(dp[i][j] > dp[i][u] - ch[i][u] + max(ch[i][u], a[j]))
dp[i][j] = dp[i][u] - ch[i][u] + max(ch[i][u], a[j]), ch[i][j] = max(ch[i][u], a[j]);
}
else break;
s.pop();
}
if(!s.empty())
{
int u = s.top();
dp[i][j] = dp[i][u];
ch[i][j] = ch[i][u];
}
s.push(i);
}
}
cout << dp[k][n] << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
41464 KB |
Output is correct |
2 |
Correct |
38 ms |
41464 KB |
Output is correct |
3 |
Correct |
35 ms |
41468 KB |
Output is correct |
4 |
Correct |
36 ms |
41452 KB |
Output is correct |
5 |
Correct |
41 ms |
41452 KB |
Output is correct |
6 |
Correct |
41 ms |
41428 KB |
Output is correct |
7 |
Correct |
41 ms |
41464 KB |
Output is correct |
8 |
Correct |
35 ms |
41464 KB |
Output is correct |
9 |
Incorrect |
38 ms |
41464 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
41436 KB |
Output is correct |
2 |
Correct |
38 ms |
41464 KB |
Output is correct |
3 |
Correct |
41 ms |
41436 KB |
Output is correct |
4 |
Correct |
39 ms |
41464 KB |
Output is correct |
5 |
Correct |
35 ms |
41464 KB |
Output is correct |
6 |
Correct |
31 ms |
41340 KB |
Output is correct |
7 |
Correct |
34 ms |
41440 KB |
Output is correct |
8 |
Correct |
35 ms |
41464 KB |
Output is correct |
9 |
Incorrect |
40 ms |
41464 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
44 ms |
41448 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
51 ms |
41620 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |