#include <algorithm>
#include <cassert>
#include <ios>
#include <iostream>
#include <map>
#include <vector>
#define ll long long
#define ull unsigned ll
#define ld long double
#define ff first
#define ss second
#define ln "\n"
#define pll pair<ll, ll>
using namespace std;
vector<ll> dep(100001, 0), wr(201, 0), pref(100001), vals(100001);
vector<vector<ll>> link(100001, vector<ll>(201));
void get(ll l, ll r, ll optl, ll optr, ll n, ll k){
if (l>r) return;
ll m = (l+r)/2;
pll optm = {-1, -1};
for (ll i=optl; i<=m; i++){
optm = max(optm, {dep[i]+(pref[m]-pref[i])*(pref[n]-pref[m]), i});
}
wr[m]=optm.ff;
link[m][k]=optm.ss;
get(l, m-1, optl, optm.ss, n, k);
get(m+1, r, optm.ss, optr, n, k);
}
void solve(){
ll n, k; cin >> n >> k;
for (ll i=1; i<=n; i++){
cin >> vals[i];
pref[i]+=pref[i-1]+vals[i];
}
for (ll i=1; i<=k; i++){
get(1, n, 0, n, n, i);
dep.swap(wr);
}
pair<ll, ll> mx={-1, -1};
for (ll i=0; i<=n; i++) mx=max(mx, {dep[i], i});
cout << mx.ff << ln;
vector<ll> ans;
for (ll i=k; i>=1; i--){
ans.emplace_back(mx.ss);
mx.ss=link[mx.ss][i];
}
for (ll i=k-1; i>=0; i--){
cout << ans[i] << " ";
}
cout << ln;
}
// dp[i][k] = maxj(dp[j][k-1]+(pref[i]-pref[j])*(pref[n]-pref[i]));
// (dp[j][k-1]-pref[j]*pref[n])+pref[j]*pref[i];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(nullptr);
ll t=1;
// cin >> t;
while (t--)solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
90 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
90 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
89 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
88 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
94 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
91 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |