#include<bits/stdc++.h>
#define maxn 100005
#define INF (int)(1e9 + 77)
using namespace std;
int n,k;
int a[maxn];
int dp[maxn][102];
int lv[maxn];
int queries[maxn];
int ans[maxn];
int minn[maxn][22];
int niz[maxn];
stack<int>mss;
int p[maxn];
int par(int v)
{
if(v==p[v])return v;
return p[v] = par(p[v]);
}
void build(int ind)
{
for(int i=0;i<=n;i++)
{
p[i] = -1;
niz[i] = dp[i][ind];
}
while(!mss.empty())mss.pop();
/*cout<<endl<<endl;
cout<<ind<<endl;
for(int i=0;i<=n;i++)
{
cout<<niz[i]<<" ";
}
cout<<endl;*/
for(int i=0;i<n;i++)
{
while(!mss.empty())
{
int vrh = mss.top();
if(niz[vrh] > niz[i])
{
p[vrh] = i;
mss.pop();
}
else break;
}
mss.push(i);
p[i] = i;
ans[i] = niz[par(queries[i])];
//cout<<queries[i]<<","<<i<<","<<ans[i]<<endl;
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin>>n>>k;
for(int i=1;i<=n;i++)cin>>a[i];
stack<int>ms;
ms.push(0);
a[0]=INF;
for(int i=1;i<=n;i++)
{
while(!ms.empty())
{
int vrh=ms.top();
if(a[vrh]<=a[i])
{
ms.pop();
}
else break;
}
lv[i]=ms.top();
queries[i-1] = lv[i];
ms.push(i);
}
//for(int i=1;i<=n;i++)cout<<lv[i]<<" ";cout<<endl;
for(int i=0;i<=n;i++)
for(int j=0;j<=k;j++)dp[i][j]=INF;
dp[0][0]=0;
for(int i=1;i<=n;i++)dp[i][0]=INF;
for(int j=1;j<=k;j++)
{
build(j-1);
for(int i=1;i<=n;i++)
{
dp[i][j]=min( dp[ lv[i] ][j], ans[i-1] + a[i]);
}
}
cout<<dp[n][k];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
396 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
476 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
380 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
432 KB |
Output is correct |
10 |
Correct |
2 ms |
476 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
380 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
380 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
4 ms |
356 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
4 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
4572 KB |
Output is correct |
2 |
Correct |
23 ms |
19612 KB |
Output is correct |
3 |
Correct |
32 ms |
19576 KB |
Output is correct |
4 |
Correct |
131 ms |
19700 KB |
Output is correct |
5 |
Correct |
413 ms |
43916 KB |
Output is correct |
6 |
Correct |
56 ms |
43716 KB |
Output is correct |
7 |
Correct |
205 ms |
43900 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
3 ms |
888 KB |
Output is correct |
13 |
Correct |
3 ms |
760 KB |
Output is correct |
14 |
Correct |
45 ms |
4728 KB |
Output is correct |
15 |
Correct |
7 ms |
4720 KB |
Output is correct |
16 |
Correct |
15 ms |
4840 KB |
Output is correct |
17 |
Correct |
23 ms |
19576 KB |
Output is correct |
18 |
Correct |
30 ms |
19584 KB |
Output is correct |
19 |
Correct |
150 ms |
19704 KB |
Output is correct |
20 |
Correct |
454 ms |
43716 KB |
Output is correct |
21 |
Correct |
55 ms |
43716 KB |
Output is correct |
22 |
Correct |
227 ms |
43720 KB |
Output is correct |
23 |
Correct |
52 ms |
43640 KB |
Output is correct |
24 |
Correct |
87 ms |
43768 KB |
Output is correct |
25 |
Correct |
461 ms |
43716 KB |
Output is correct |
26 |
Correct |
2 ms |
760 KB |
Output is correct |
27 |
Correct |
3 ms |
760 KB |
Output is correct |
28 |
Correct |
39 ms |
4728 KB |
Output is correct |
29 |
Correct |
7 ms |
4728 KB |
Output is correct |
30 |
Correct |
15 ms |
4728 KB |
Output is correct |
31 |
Correct |
23 ms |
19704 KB |
Output is correct |
32 |
Correct |
32 ms |
19708 KB |
Output is correct |
33 |
Correct |
149 ms |
19764 KB |
Output is correct |
34 |
Correct |
536 ms |
43896 KB |
Output is correct |
35 |
Correct |
56 ms |
43896 KB |
Output is correct |
36 |
Correct |
355 ms |
43872 KB |
Output is correct |
37 |
Correct |
7 ms |
4728 KB |
Output is correct |
38 |
Correct |
38 ms |
4728 KB |
Output is correct |
39 |
Correct |
3 ms |
760 KB |
Output is correct |
40 |
Correct |
2 ms |
764 KB |
Output is correct |
41 |
Correct |
2 ms |
376 KB |
Output is correct |
42 |
Correct |
2 ms |
376 KB |
Output is correct |