/*huypheu
6 5
5 4 3 2 1 4
*/
#include <bits/stdc++.h>
#define int long long
#define oo (LLONG_MAX/2)
using namespace std;
bool pok[100007];
int a[100007],f[100007][107],mask[100007][107];
signed main()
{
int n,k;
cin >> n >> k;
for(int i=1;i<=n;i++)
{
cin >> a[i];
}
for(int i=0;i<=n;i++)
{
for(int j=0;j<=k;j++)
{
f[i][j]=oo;
mask[i][j]=oo;
}
}
f[0][0]=0;
stack <int> st;
for(int i=n;i>=1;i--)
{
while(!st.empty() && a[st.top()]<=a[i]) st.pop();
if(st.empty()) pok[i]=1;
st.push(i);
}
for(int j=1;j<=k;j++)
{
while(!st.empty()) st.pop();
for(int i=1+j-1;i<=n;i++)
{
while(!st.empty() && a[st.top()]<=a[i])
{
mask[i][j]=min(mask[i][j],mask[st.top()][j]);
st.pop();
}
if(st.empty()) mask[i][j]=min(mask[i][j],f[1+j-2][j-1]); else mask[i][j]=min(mask[i][j],f[st.top()][j-1]);
f[i][j]=min(f[i][j],mask[i][j]+a[i]);
if(!st.empty()) f[i][j]=min(f[i][j],f[st.top()][j]);
mask[i][j]=min(mask[i][j],f[i][j-1]);
st.push(i);
}
}
int mi=oo;
// for(int j=0;j<=k;j++)
// {
// for(int i=1;i<=n;i++)
// {
// cout << mask[i][j] /*<< "/" << f[i][j]*/ << " ";
// }
// cout << endl;
// }
// for(int j=0;j<=k;j++)
// {
// for(int i=1;i<=n;i++)
// {
// cout << f[i][j] /*<< "/" << f[i][j]*/ << " ";
// }
// cout << endl;
// }
for(int i=1;i<=n;i++)
{
if(pok[i]) mi=min(mi,f[i][k]);
}
cout << mi << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
170092 KB |
Output is correct |
2 |
Correct |
0 ms |
170092 KB |
Output is correct |
3 |
Correct |
0 ms |
170092 KB |
Output is correct |
4 |
Correct |
0 ms |
170092 KB |
Output is correct |
5 |
Correct |
0 ms |
170092 KB |
Output is correct |
6 |
Correct |
0 ms |
170092 KB |
Output is correct |
7 |
Correct |
0 ms |
170092 KB |
Output is correct |
8 |
Correct |
0 ms |
170092 KB |
Output is correct |
9 |
Correct |
0 ms |
170092 KB |
Output is correct |
10 |
Correct |
0 ms |
170092 KB |
Output is correct |
11 |
Correct |
0 ms |
170092 KB |
Output is correct |
12 |
Correct |
0 ms |
170092 KB |
Output is correct |
13 |
Correct |
0 ms |
170092 KB |
Output is correct |
14 |
Correct |
0 ms |
170092 KB |
Output is correct |
15 |
Correct |
0 ms |
170092 KB |
Output is correct |
16 |
Correct |
0 ms |
170092 KB |
Output is correct |
17 |
Correct |
0 ms |
170092 KB |
Output is correct |
18 |
Correct |
0 ms |
170092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
170092 KB |
Output is correct |
2 |
Correct |
0 ms |
170092 KB |
Output is correct |
3 |
Correct |
0 ms |
170092 KB |
Output is correct |
4 |
Correct |
0 ms |
170092 KB |
Output is correct |
5 |
Correct |
0 ms |
170092 KB |
Output is correct |
6 |
Correct |
0 ms |
170092 KB |
Output is correct |
7 |
Correct |
0 ms |
170092 KB |
Output is correct |
8 |
Correct |
0 ms |
170092 KB |
Output is correct |
9 |
Correct |
0 ms |
170092 KB |
Output is correct |
10 |
Correct |
0 ms |
170092 KB |
Output is correct |
11 |
Correct |
0 ms |
170092 KB |
Output is correct |
12 |
Correct |
0 ms |
170092 KB |
Output is correct |
13 |
Correct |
0 ms |
170092 KB |
Output is correct |
14 |
Correct |
0 ms |
170092 KB |
Output is correct |
15 |
Correct |
0 ms |
170092 KB |
Output is correct |
16 |
Correct |
0 ms |
170092 KB |
Output is correct |
17 |
Correct |
0 ms |
170092 KB |
Output is correct |
18 |
Correct |
0 ms |
170092 KB |
Output is correct |
19 |
Correct |
0 ms |
170092 KB |
Output is correct |
20 |
Correct |
0 ms |
170092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
170092 KB |
Output is correct |
2 |
Correct |
0 ms |
170092 KB |
Output is correct |
3 |
Correct |
0 ms |
170092 KB |
Output is correct |
4 |
Correct |
0 ms |
170092 KB |
Output is correct |
5 |
Correct |
0 ms |
170092 KB |
Output is correct |
6 |
Correct |
0 ms |
170092 KB |
Output is correct |
7 |
Correct |
0 ms |
170092 KB |
Output is correct |
8 |
Correct |
0 ms |
170092 KB |
Output is correct |
9 |
Correct |
0 ms |
170092 KB |
Output is correct |
10 |
Correct |
0 ms |
170092 KB |
Output is correct |
11 |
Correct |
0 ms |
170092 KB |
Output is correct |
12 |
Correct |
0 ms |
170092 KB |
Output is correct |
13 |
Correct |
0 ms |
170092 KB |
Output is correct |
14 |
Correct |
0 ms |
170092 KB |
Output is correct |
15 |
Correct |
0 ms |
170092 KB |
Output is correct |
16 |
Correct |
0 ms |
170092 KB |
Output is correct |
17 |
Correct |
0 ms |
170092 KB |
Output is correct |
18 |
Correct |
0 ms |
170092 KB |
Output is correct |
19 |
Correct |
0 ms |
170092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
170092 KB |
Output is correct |
2 |
Correct |
19 ms |
170224 KB |
Output is correct |
3 |
Correct |
49 ms |
170224 KB |
Output is correct |
4 |
Correct |
156 ms |
170224 KB |
Output is correct |
5 |
Correct |
863 ms |
170352 KB |
Output is correct |
6 |
Correct |
86 ms |
170352 KB |
Output is correct |
7 |
Correct |
373 ms |
170352 KB |
Output is correct |
8 |
Correct |
0 ms |
170092 KB |
Output is correct |
9 |
Correct |
0 ms |
170092 KB |
Output is correct |
10 |
Correct |
0 ms |
170092 KB |
Output is correct |
11 |
Correct |
0 ms |
170092 KB |
Output is correct |
12 |
Correct |
0 ms |
170092 KB |
Output is correct |
13 |
Correct |
0 ms |
170092 KB |
Output is correct |
14 |
Correct |
43 ms |
170092 KB |
Output is correct |
15 |
Correct |
6 ms |
170092 KB |
Output is correct |
16 |
Correct |
13 ms |
170092 KB |
Output is correct |
17 |
Correct |
26 ms |
170092 KB |
Output is correct |
18 |
Correct |
46 ms |
170092 KB |
Output is correct |
19 |
Correct |
166 ms |
170092 KB |
Output is correct |
20 |
Correct |
813 ms |
170092 KB |
Output is correct |
21 |
Correct |
73 ms |
170092 KB |
Output is correct |
22 |
Correct |
353 ms |
170092 KB |
Output is correct |
23 |
Correct |
49 ms |
170092 KB |
Output is correct |
24 |
Correct |
133 ms |
170092 KB |
Output is correct |
25 |
Correct |
869 ms |
170092 KB |
Output is correct |
26 |
Correct |
0 ms |
170092 KB |
Output is correct |
27 |
Correct |
0 ms |
170092 KB |
Output is correct |
28 |
Correct |
43 ms |
170092 KB |
Output is correct |
29 |
Correct |
9 ms |
170092 KB |
Output is correct |
30 |
Correct |
13 ms |
170092 KB |
Output is correct |
31 |
Correct |
26 ms |
170224 KB |
Output is correct |
32 |
Correct |
36 ms |
170224 KB |
Output is correct |
33 |
Correct |
193 ms |
170224 KB |
Output is correct |
34 |
Correct |
836 ms |
170352 KB |
Output is correct |
35 |
Correct |
66 ms |
170352 KB |
Output is correct |
36 |
Correct |
439 ms |
170352 KB |
Output is correct |
37 |
Correct |
9 ms |
170092 KB |
Output is correct |
38 |
Correct |
39 ms |
170092 KB |
Output is correct |
39 |
Correct |
0 ms |
170092 KB |
Output is correct |
40 |
Correct |
0 ms |
170092 KB |
Output is correct |
41 |
Correct |
0 ms |
170092 KB |
Output is correct |
42 |
Correct |
0 ms |
170092 KB |
Output is correct |