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 ll long long
#define pb push_back
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define sz(v) (int)v.size()
#define x first
#define y second
#define nl "\n"
using namespace std;
const int N = (int)1e5 + 7;
const int M = (int)2e6 + 5e5 + 7;
const ll MOD = (ll)1e9;
const int inf = (int)1e9 + 7;
const int B = (int)390;
const ll INF = (ll)1e18 + 7;
pair<int, int> dir[] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
bool bit(int mask, int i) {
return (mask >> i & 1);
}
int sum(int x, int y) {
x += y;
if(x >= MOD) x -= MOD;
return x;
}
int mult(int x, int y) {
return 1ll * x * y % MOD;
}
int n, k, cr, a[N], p[N][201];
ll dp[2][N], suf[N];
void calc(int l, int r, int tl, int tr) {
if(l > r)
return;
int mid = (l + r) >> 1, x = l;
dp[1][mid] = -INF;
for(int i = tl; i <= min(mid, tr); ++i) {
ll cur = dp[0][i - 1] + (suf[i] - suf[mid + 1]) * suf[mid + 1];
if(cur > dp[1][mid]) {
dp[1][mid] = cur;
p[mid][cr] = i - 1;
x = i;
}
}
calc(l, mid - 1, tl, x);
calc(mid + 1, r, x, tr);
}
void solve() {
cin >> n >> k;
for(int i = 1; i <= n; ++i) cin >> a[i];
for(int i = n; i >= 1; --i) suf[i] = suf[i + 1] + a[i];
ll ans = -INF;
int lst = -1;
for(int i = 0; i < 2; ++i) {
for(int j = 0; j <= n; ++j) {
dp[i][j] = -INF;
}
}
dp[0][0] = 0;
for(int i = 1; i <= k; ++i) {
cr = i;
calc(1, n, 1, n);
if(i == k) {
for(int j = 1; j <= n; ++j) {
if(dp[1][j] > ans) {
ans = dp[1][j];
lst = j;
}
}
}
for(int j = 0; j <= n; ++j) {
dp[0][j] = dp[1][j];
}
}
assert(lst != -1);
vector<int> v;
while(lst > 0) {
v.pb(lst);
lst = p[lst][k--];
}
reverse(all(v));
cout << ans << nl;
for(auto x : v) cout << x << ' ';
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
//freopen("sequence.in", "r", stdin);
//freopen("sequence.out", "w", stdout);
int test = 1;
//cin >> test;
for(int i = 1; i <= test; ++i) {
//cout << "Case " << i << ":\n";
solve();
}
return 0;
}
/*
possible causes of error :
* array bounds
* int overflow
* special case (n == 1?)
* haven't clean something
* wrong N | M | inf | INF | MOD
* try to write brute force to find test
* don't waste time
* don't write bullshit
* solution is easy
*/
# | 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... |