#include<bits/stdc++.h>
using namespace std;
using lol=long long int;
#define endl "\n"
const lol mod1=1e9+7,mod2=998244353;
const lol inf=1e18+8;
const double eps=1e-12;
const int N=2*1e5+5;
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int lg2[N];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
lg2[1]=0;
for(int i=2;i<N;i++) lg2[i]=lg2[i/2]+1;
int _=1;
//cin>>_;
while(_--)
{
int n,k;
cin>>n>>k;
vector<vector<lol> > dp(n,vector<lol>(k+1,inf));
vector<lol> v(n);
for(int i=0;i<n;i++)
{
cin>>v[i];
}
vector<int> psg(n,-1);
stack<lol> st;
for(int i=0;i<n;i++)
{
while(!st.empty() && v[st.top()]<v[i]) st.pop();
if(!st.empty()) psg[i]=st.top();
st.push(i);
}
lol mx=0;
for(int i=0;i<n;i++)
{
mx=max(mx,v[i]);
dp[i][1]=mx;
}
for(int l=2;l<=k;l++)
{
lol spt[n][25];
for(int i=0;i<n;i++)
{
spt[i][0]=dp[i][l-1];
}
for(int j=1;j<25;j++)
{
for(int i=0;i+(1<<j)<=n;i++)
{
spt[i][j]=min(spt[i][j-1],spt[i+(1<<(j-1))][j-1]);
}
}
auto getmin=[&](int L,int R)
{
if(L>R) return inf;
int j=lg2[R-L+1];
return min(spt[L][j],spt[R-(1<<j)+1][j]);
};
for(int i=0;i<n;i++)
{
int p=psg[i];
if(p==-1)
{
dp[i][l]=v[i]+getmin(0,i-1);
}else
{
lol mn=v[i]+getmin(p,i-1);
mn=min(mn,dp[p][l]);
dp[i][l]=min(mn,inf);
}
}
}
/*for(int i=1;i<=k;i++)
{
for(int j=0;j<n;j++)
{
cout<<dp[j][i]<<" ";
}
cout<<endl;
}*/
cout<<dp[n-1][k];
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1100 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
1100 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
1100 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
1 ms |
972 KB |
Output is correct |
11 |
Correct |
1 ms |
1040 KB |
Output is correct |
12 |
Correct |
1 ms |
1100 KB |
Output is correct |
13 |
Correct |
1 ms |
1100 KB |
Output is correct |
14 |
Correct |
1 ms |
1100 KB |
Output is correct |
15 |
Correct |
1 ms |
1100 KB |
Output is correct |
16 |
Correct |
1 ms |
1100 KB |
Output is correct |
17 |
Correct |
1 ms |
1100 KB |
Output is correct |
18 |
Correct |
1 ms |
1100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1100 KB |
Output is correct |
2 |
Correct |
1 ms |
1100 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
1100 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
972 KB |
Output is correct |
11 |
Correct |
1 ms |
972 KB |
Output is correct |
12 |
Correct |
1 ms |
972 KB |
Output is correct |
13 |
Correct |
1 ms |
972 KB |
Output is correct |
14 |
Correct |
1 ms |
972 KB |
Output is correct |
15 |
Correct |
1 ms |
1100 KB |
Output is correct |
16 |
Correct |
1 ms |
972 KB |
Output is correct |
17 |
Correct |
1 ms |
1100 KB |
Output is correct |
18 |
Correct |
1 ms |
1100 KB |
Output is correct |
19 |
Correct |
1 ms |
1100 KB |
Output is correct |
20 |
Correct |
1 ms |
972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1100 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
1100 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
1100 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
1 ms |
972 KB |
Output is correct |
11 |
Correct |
1 ms |
1040 KB |
Output is correct |
12 |
Correct |
1 ms |
1100 KB |
Output is correct |
13 |
Correct |
1 ms |
1100 KB |
Output is correct |
14 |
Correct |
1 ms |
1100 KB |
Output is correct |
15 |
Correct |
1 ms |
1100 KB |
Output is correct |
16 |
Correct |
1 ms |
1100 KB |
Output is correct |
17 |
Correct |
1 ms |
1100 KB |
Output is correct |
18 |
Correct |
1 ms |
1100 KB |
Output is correct |
19 |
Correct |
1 ms |
1100 KB |
Output is correct |
20 |
Correct |
1 ms |
1100 KB |
Output is correct |
21 |
Correct |
1 ms |
972 KB |
Output is correct |
22 |
Correct |
1 ms |
1100 KB |
Output is correct |
23 |
Correct |
1 ms |
972 KB |
Output is correct |
24 |
Correct |
1 ms |
972 KB |
Output is correct |
25 |
Correct |
1 ms |
972 KB |
Output is correct |
26 |
Correct |
1 ms |
972 KB |
Output is correct |
27 |
Correct |
1 ms |
972 KB |
Output is correct |
28 |
Correct |
1 ms |
972 KB |
Output is correct |
29 |
Correct |
1 ms |
972 KB |
Output is correct |
30 |
Correct |
1 ms |
972 KB |
Output is correct |
31 |
Correct |
1 ms |
972 KB |
Output is correct |
32 |
Correct |
1 ms |
972 KB |
Output is correct |
33 |
Correct |
1 ms |
1100 KB |
Output is correct |
34 |
Correct |
1 ms |
972 KB |
Output is correct |
35 |
Correct |
1 ms |
1100 KB |
Output is correct |
36 |
Correct |
1 ms |
1100 KB |
Output is correct |
37 |
Correct |
1 ms |
1100 KB |
Output is correct |
38 |
Correct |
1 ms |
972 KB |
Output is correct |
39 |
Correct |
1 ms |
1100 KB |
Output is correct |
40 |
Correct |
1 ms |
972 KB |
Output is correct |
41 |
Correct |
1 ms |
972 KB |
Output is correct |
42 |
Correct |
1 ms |
972 KB |
Output is correct |
43 |
Correct |
1 ms |
972 KB |
Output is correct |
44 |
Correct |
1 ms |
972 KB |
Output is correct |
45 |
Correct |
1 ms |
972 KB |
Output is correct |
46 |
Correct |
1 ms |
972 KB |
Output is correct |
47 |
Correct |
1 ms |
972 KB |
Output is correct |
48 |
Correct |
1 ms |
972 KB |
Output is correct |
49 |
Correct |
1 ms |
972 KB |
Output is correct |
50 |
Correct |
1 ms |
972 KB |
Output is correct |
51 |
Correct |
1 ms |
1100 KB |
Output is correct |
52 |
Correct |
1 ms |
1100 KB |
Output is correct |
53 |
Correct |
1 ms |
1100 KB |
Output is correct |
54 |
Correct |
1 ms |
1100 KB |
Output is correct |
55 |
Correct |
1 ms |
1100 KB |
Output is correct |
56 |
Correct |
1 ms |
1100 KB |
Output is correct |
57 |
Correct |
1 ms |
1100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1100 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
1100 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
1100 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
1 ms |
972 KB |
Output is correct |
11 |
Correct |
1 ms |
1040 KB |
Output is correct |
12 |
Correct |
1 ms |
1100 KB |
Output is correct |
13 |
Correct |
1 ms |
1100 KB |
Output is correct |
14 |
Correct |
1 ms |
1100 KB |
Output is correct |
15 |
Correct |
1 ms |
1100 KB |
Output is correct |
16 |
Correct |
1 ms |
1100 KB |
Output is correct |
17 |
Correct |
1 ms |
1100 KB |
Output is correct |
18 |
Correct |
1 ms |
1100 KB |
Output is correct |
19 |
Correct |
1 ms |
1100 KB |
Output is correct |
20 |
Correct |
1 ms |
1100 KB |
Output is correct |
21 |
Correct |
1 ms |
972 KB |
Output is correct |
22 |
Correct |
1 ms |
1100 KB |
Output is correct |
23 |
Correct |
1 ms |
972 KB |
Output is correct |
24 |
Correct |
1 ms |
972 KB |
Output is correct |
25 |
Correct |
1 ms |
972 KB |
Output is correct |
26 |
Correct |
1 ms |
972 KB |
Output is correct |
27 |
Correct |
1 ms |
972 KB |
Output is correct |
28 |
Correct |
1 ms |
972 KB |
Output is correct |
29 |
Correct |
1 ms |
972 KB |
Output is correct |
30 |
Correct |
1 ms |
972 KB |
Output is correct |
31 |
Correct |
1 ms |
972 KB |
Output is correct |
32 |
Correct |
1 ms |
972 KB |
Output is correct |
33 |
Correct |
1 ms |
1100 KB |
Output is correct |
34 |
Correct |
1 ms |
972 KB |
Output is correct |
35 |
Correct |
1 ms |
1100 KB |
Output is correct |
36 |
Correct |
1 ms |
1100 KB |
Output is correct |
37 |
Correct |
1 ms |
1100 KB |
Output is correct |
38 |
Correct |
1 ms |
972 KB |
Output is correct |
39 |
Correct |
1 ms |
1100 KB |
Output is correct |
40 |
Correct |
1 ms |
972 KB |
Output is correct |
41 |
Correct |
1 ms |
972 KB |
Output is correct |
42 |
Correct |
1 ms |
972 KB |
Output is correct |
43 |
Correct |
1 ms |
972 KB |
Output is correct |
44 |
Correct |
1 ms |
972 KB |
Output is correct |
45 |
Correct |
1 ms |
972 KB |
Output is correct |
46 |
Correct |
1 ms |
972 KB |
Output is correct |
47 |
Correct |
1 ms |
972 KB |
Output is correct |
48 |
Correct |
1 ms |
972 KB |
Output is correct |
49 |
Correct |
1 ms |
972 KB |
Output is correct |
50 |
Correct |
1 ms |
972 KB |
Output is correct |
51 |
Correct |
1 ms |
1100 KB |
Output is correct |
52 |
Correct |
1 ms |
1100 KB |
Output is correct |
53 |
Correct |
1 ms |
1100 KB |
Output is correct |
54 |
Correct |
1 ms |
1100 KB |
Output is correct |
55 |
Correct |
1 ms |
1100 KB |
Output is correct |
56 |
Correct |
1 ms |
1100 KB |
Output is correct |
57 |
Correct |
1 ms |
1100 KB |
Output is correct |
58 |
Correct |
10 ms |
5580 KB |
Output is correct |
59 |
Correct |
7 ms |
4024 KB |
Output is correct |
60 |
Correct |
31 ms |
14796 KB |
Output is correct |
61 |
Correct |
261 ms |
38800 KB |
Output is correct |
62 |
Execution timed out |
1097 ms |
104664 KB |
Time limit exceeded |
63 |
Halted |
0 ms |
0 KB |
- |