This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
//#define int long long
#define ll long long
#define fi first
#define se second
#define pb push_back
#define all(lmao) lmao.begin(), lmao.end()
using namespace std;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tp;
const int N = 1e5 + 5;
const ll oo = -1e12;
const int K = 210;
const int mod = 998244353;
//const int base = 23;
ll n, k, a[N];
namespace sub3{
ll f[222][222], dp[222][222], s[N];
ll cal(int i,int j){
return 1ll * (s[j] - s[i]) * (s[n] - s[j]);
}
void sol(){
for(int i = 1; i <= n; i ++){
s[i] = s[i - 1] + a[i];
}
for(int i = 1; i <= n; i ++) dp[i][1] = s[i] * (s[n] - s[i]), f[i][1] = 0;
for(int j = 2; j <= k; j ++){
for(int i = j; i < n; i ++){
dp[i][j] = oo;
for(int c = j - 1; c < i; c ++){
ll val = dp[c][j - 1] + cal(c, i);
if(dp[i][j] < val){
dp[i][j] = val;
f[i][j] = c;
}
}
}
}
ll ans = oo;
int tmp = 0, cnt = k;
for(int i = 1; i <= n; i ++){
if(ans < dp[i][k]){
ans = dp[i][k];
tmp = i;
}
}
cout << ans << "\n";
vector<int> vr;
vr.pb(tmp);
while(f[tmp][cnt]){
vr.pb(f[tmp][cnt]);
tmp = f[tmp][cnt];
cnt--;
}
for(int i = vr.size() - 1; i >= 0; i --) cout << vr[i] << " ";
}
}
namespace AC{
ll dp[N][5], s[N], pre, nx;
int f[N][K];
ll cal(int i,int j){
return 1ll * (s[j] - s[i]) * (s[n] - s[j]);
}
void solve(int j,int l,int r,int pl,int pr){
if(l > r) return;
int mid = (l + r) / 2;
dp[mid][nx] = oo;
for(int i = pl; i <= min(mid - 1, pr); i ++){
ll val = dp[i][pre] + cal(i, mid);
if(val > dp[mid][nx]){
dp[mid][nx] = val;
f[mid][j] = i;
}
}
solve(j, l, mid - 1, pl, f[mid][j]);
solve(j, mid + 1, r, f[mid][j], pr);
}
void sol(){
for(int i = 1; i <= n; i ++) s[i] = s[i - 1] + a[i];
if(k == 0){
cout << 0;
return ;
}
for(int j = 0; j <= 1; j ++){
for(int i = 0; i <= n; i ++){
dp[i][j] = oo;
}
}
pre = 0, nx = 1;
for(int i = 1; i <= n; i ++) dp[i][0] = 1ll * s[i] * (s[n] - s[i]), f[i][0] = 0;
for(int j = 2; j <= k; j ++){
solve(j, j, n, 1, n - 1);
swap(pre, nx);
}
ll ans = oo;
int tmp = 0, cnt = k;
for(int i = 1; i <= n; i ++){
if(ans < dp[i][pre]){
ans = dp[i][pre];
tmp = i;
}
}
cout << ans << "\n";
vector<int> vr;
vr.pb(tmp);
while(f[tmp][cnt]){
vr.pb(f[tmp][cnt]);
tmp = f[tmp][cnt];
cnt--;
}
for(int i = vr.size() - 1; i >= 0; i --) cout << vr[i] << " ";
}
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define task "v"
if(fopen(task ".inp","r")){
freopen(task ".inp","r",stdin);
freopen(task ".out","w",stdout);
}
cin >> n >> k;
for(int i = 1; i <= n; i ++){
cin >> a[i];
}
if(n <= 200 && k <= 200) sub3 :: sol();
else
AC :: sol();
}
Compilation message (stderr)
sequence.cpp: In function 'int main()':
sequence.cpp:136:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
136 | freopen(task ".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:137:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
137 | freopen(task ".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |