// __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];
ll dp[maxn];
ll ndp[maxn];
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 = max(st[l][k],st[r-(1<<k)+1][k]);
if(test){
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;
}
void reshi(ll l,ll r,ll optl,ll optr){
if(l>r) return;
ll mid = (l+r)/2;
ndp[mid] = dp[mid-1] + a[mid];
ll opt = mid-1;
for(ll i = optl;i<=min(mid-1,optr);i++){
if(dp[i] + get(i+1,mid) <= ndp[mid]){
opt = i;
ndp[mid] = dp[i] + get(i+1,mid);
}
}
if(test) cerr<<"i: "<<mid<< " "<<optl<< " "<<optr<<" "<<opt<<endl;
//cerr<<l<< " "<<r<< " "<<optl<< " "<<optr<<" "<<mid<< " "<<opt<<endl;
reshi(l,mid-1,opt,optr);
reshi(mid+1,r,optl,opt);
}
ll brut(){
vector<vector<ll> > v(n+1,vector<ll>(k+1,0));
vector<ll> opt(n+1);
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;
for(ll f = i-1;f>=0;f--){
ll mx = get(f+1,i);
if(v[f][j-1]+mx<=v[i][j]){
opt[i] = f;
}
v[i][j] = min(v[i][j],v[f][j-1] + mx);
}
}
for(ll i = 0;i<=n;i++) cerr<<v[i][j]<< " ";
cerr<<endl;
cerr<<"opt: ";
for(ll i = 1;i<=n;i++) cerr<<opt[i]<< " ";
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 = 0;
int main(){
test = 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{
n = 7;
k = 4;
for(ll i = 1;i<=n;i++) a[i] = rnd(1,30);
cerr<<n<< " "<<k<<endl;
for(ll i = 1;i<=n;i++) cerr<<a[i]<< " ";
cerr<<endl;
}
for(ll i = 1;i<=n;i++) st[i][0] = a[i];
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];
}
fill(dp,dp+n+1,llinf);
dp[0] = 0;
for(ll ii = 1;ii<=k;ii++){
fill(ndp,ndp+n+1,llinf);
//if(test) here;
reshi(1,n,0,n);
//cerr<<"dp: ";
for(ll i = 1;i<=n;i++) dp[i] = ndp[i];
for(ll i = 0;i<ii;i++) dp[i] = llinf;
if(test) ceri(ndp,0,n);
}
if(test) here;
if(test)cerr<<"brut: "<<brut()<<endl;
cout<<dp[n]<<endl;
return 0;
}
/*
5 2
1 2 3 4 5
out: 6
7 4
10 30 5 9 16 13 26
out: 70
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
328 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 |
328 KB |
Output is correct |
16 |
Correct |
1 ms |
328 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
340 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 |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
324 KB |
Output is correct |
12 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
328 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 |
328 KB |
Output is correct |
16 |
Correct |
1 ms |
328 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 |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
328 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
324 KB |
Output is correct |
30 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
328 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 |
328 KB |
Output is correct |
16 |
Correct |
1 ms |
328 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 |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
328 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
324 KB |
Output is correct |
30 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |