// __builtin_popcount(x)
// __builtin_popcountll(x)
#define here cerr<<"===========================================\n"
#include <bits/stdc++.h>
#define ld double
#define ll long long
#define ull unsigned long long
#define llinf 100000000000000000LL // 10^17
#define iinf 2000000000 // 2*10^9
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define sz(a) int(a.size())
#define all(a) a.begin(),a.end()
#define ceri(a,l,r) {for(ll i_ = l;i_<=r;i_++) cerr<<a[i_]<< " ";cerr<<endl;}
using namespace std;
#define maxn 100005
#define maxk 105
#define lg 25
ll n,k;
ll a[maxn];
bool pisi = 1;
ll st[maxn][lg];
ll powi[maxn];
bool test = 0;
ll get(ll l,ll r){
ll len = (r-l+1);
ll k = powi[len];
ll ans = min(st[l][k],st[r-(1<<k)+1][k]);
if(0){
ll g =0 ;
for(ll i = l;i<=r;i++) g = max(g,a[i]);
if(g!=ans){
cerr<<"MIN: "<<l<< " "<<r<<endl;
exit(0);
}
}
return ans;
}
ll brut(){
vector<vector<ll> > v(n+1,vector<ll>(k+1,0));
v[0][0] = 0;
for(ll i = 1;i<=n;i++) v[i][0] = llinf;
for(ll j = 1;j<=k;j++){
v[0][j] = llinf;
for(ll i = 1;i<=n;i++){
v[i][j] = llinf;
ll mx = a[i];
for(ll f = i-1;f>=0;f--){
v[i][j] = min(v[i][j],v[f][j-1] + mx);
mx = max(mx,a[f]);
}
}
if(pisi){
for(ll i = 0;i<=n;i++) cerr<<v[i][j]<< " ";
cerr<<endl;
}
}
return v[n][k];
}
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll rnd(ll l,ll r){
return uniform_int_distribution<ll>(l,r)(rng);
}
bool gen = 1;
ll b[maxn];
ll dp[maxn][2];
ll dpmin[maxn][2];
ll opt[maxn];
void calc(ll i,ll id){
if(b[i]==0){
//if(pisi) cerr<<i<< " "<<get(0,i-1)<<" "<<a[i]<<endl;
opt[i] = get(0,i-1) + a[i];
return;
}
calc(b[i],id);
//cerr<<b[i]<< " "<<i-1<< " "<<get(b[i],i-1)<<endl;
opt[i] = min(opt[b[i]],get(b[i],i-1) + a[i]);
}
int main(){
test = 0;
gen = 0;
pisi = 0;
ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
if(gen==0){
cin >> n >> k;
for(ll i = 1;i<=n;i++) cin >> a[i];
}else{
lol:;
n = 5000;
k = 5;
for(ll i = 1;i<=n;i++) a[i] = rnd(1,1000000);
if(pisi){
cerr<<n<< " "<<k<<endl;
for(ll i = 1;i<=n;i++) cerr<<a[i]<< " ";
cerr<<endl;
}
}
cout<<brut()<<endl;
return 0;
st[0][0] = 0;
for(ll i = 1;i<=n;i++) st[i][0] = llinf;
for(ll j = 1;j<lg;j++){
for(ll i = 1;i+(1<<j)-1<=n;i++){
st[i][j] = max(st[i][j-1],st[i+(1<<(j-1))][j-1]);
}
}
for(ll j = 0;j<lg;j++){
if((1<<j)>n) break;
powi[(1<<j)] = j;
}
for(ll i = 1;i<=n;i++){
if(powi[i]==0) powi[i] = powi[i-1];
}
stack<ll> ste;
for(ll i =1;i<=n;i++){
while(sz(ste)&&a[i]>a[ste.top()]) ste.pop();
if(sz(ste)) b[i] = ste.top();
ste.push(i);
}
if(pisi) ceri(b,1,n);
for(ll i = 1;i<=n;i++) dp[i][0] = llinf;
dp[0][0] = 0;
for(ll i = 1;i<=n;i++) dpmin[i][0] = 0;
for(ll j = 1;j<=k;j++){
for(ll i = 1;i<=n;i++) opt[i] = llinf;
dp[0][j] = llinf;
for(ll i = 1;i<=n;i++){
calc(i,(j&1)^1);
dp[i][j&1] = opt[i];
}
if(pisi){
here;
for(ll i = 0;i<=n;i++) cerr<<dp[i][(j&1)]<<" ";
cerr<<endl;
for(ll i = 0;i<=n;i++) cerr<<dp[i][(j&1)^1]<< " ";
cerr<<endl;
}
for(ll i = 0;i<=n;i++) st[i][0] = dp[i][(j&1)];
for(ll j = 1;j<lg;j++){
for(ll i = 0;i+(1<<j)-1<=n;i++){
st[i][j] = min(st[i][j-1],st[i+(1<<(j-1))][j-1]);
}
}
for(ll i = 0;i<=n;i++) dp[i][(j&1)^1] = dp[i][j&1];
//for(ll i = 0;i<=n;i++) dp[i][(j&1)] = llinf;
}
//here;
here;
ll ans2 = brut();
if(test) cerr<<"brut: "<<ans2<<endl;
//if(ans2==dp[n][(k&1)^1]) goto lol;
cout<<dp[n][(k&1)^1]<<endl;
return 0;
}
/*
5 2
1 2 3 4 5
out: 6
5 4
3 1 3 5 1
out: 10
7 4
10 7 4 1 8 4 1
10 5
5 9 2 9 4 6 9 3 7 8
*/
Compilation message
blocks.cpp: In function 'int main()':
blocks.cpp:96:9: warning: label 'lol' defined but not used [-Wunused-label]
96 | lol:;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
336 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
340 KB |
Output is correct |
32 |
Correct |
0 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
212 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
0 ms |
340 KB |
Output is correct |
45 |
Correct |
0 ms |
336 KB |
Output is correct |
46 |
Correct |
0 ms |
332 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
1 ms |
340 KB |
Output is correct |
50 |
Correct |
1 ms |
212 KB |
Output is correct |
51 |
Correct |
1 ms |
332 KB |
Output is correct |
52 |
Correct |
1 ms |
340 KB |
Output is correct |
53 |
Correct |
2 ms |
340 KB |
Output is correct |
54 |
Correct |
1 ms |
336 KB |
Output is correct |
55 |
Correct |
1 ms |
340 KB |
Output is correct |
56 |
Correct |
2 ms |
340 KB |
Output is correct |
57 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
336 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
340 KB |
Output is correct |
32 |
Correct |
0 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
212 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
0 ms |
340 KB |
Output is correct |
45 |
Correct |
0 ms |
336 KB |
Output is correct |
46 |
Correct |
0 ms |
332 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
1 ms |
340 KB |
Output is correct |
50 |
Correct |
1 ms |
212 KB |
Output is correct |
51 |
Correct |
1 ms |
332 KB |
Output is correct |
52 |
Correct |
1 ms |
340 KB |
Output is correct |
53 |
Correct |
2 ms |
340 KB |
Output is correct |
54 |
Correct |
1 ms |
336 KB |
Output is correct |
55 |
Correct |
1 ms |
340 KB |
Output is correct |
56 |
Correct |
2 ms |
340 KB |
Output is correct |
57 |
Correct |
2 ms |
340 KB |
Output is correct |
58 |
Execution timed out |
1089 ms |
2900 KB |
Time limit exceeded |
59 |
Halted |
0 ms |
0 KB |
- |