This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#define endl '\n'
#define pb push_back
#define fr first
#define sc second
#define ll long long int
#define ld long double
#define bit(idx) idx&(-idx)
#define bin(x) bitset<32>(x)
#define all(A) A.begin(), A.end()
#define de(x) cout << #x << " = " << x << endl;
#define row vector<ll>
#define row_matrix vector<ll>
#define matrix vector<row_matrix>
#define ordered_set_T int
using namespace std;
using namespace __gnu_pbds;
typedef tree<ordered_set_T, null_type, less<ordered_set_T>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
/// find_by_order(x) -> x-th element in the set
/// order_of_key(x) -> how many elements are smaller than x
/// insert(x) -> inserts x into the set
const int MAXN = 1e5 + 5;
const int MAXK = 256;
int n, k, prefix[MAXN], A[MAXN], dp[MAXK][MAXN], opt[MAXK][MAXN];
int f(ll l, ll r){
return (l == 0 ? 0 : prefix[l - 1]) * (prefix[r] - (l == 0 ? 0 : prefix[l - 1]));
}
void solve(ll i, ll l, ll r, ll tl, ll tr){
if(l > r){
return;
}
ll m = (l + r) / 2;
for(ll _t = max(tl, i - 1); _t <= min(tr, m); _t ++){
if(dp[i - 1][_t - 1] + f(_t, m) > dp[i][m]){
opt[i][m] = _t;
dp[i][m] = dp[i - 1][_t - 1] + f(_t, m);
}
}
solve(i, l, m - 1, tl, opt[i][m]);
solve(i, m + 1, r, opt[i][m], tr);
}
int main(){
/// ios_base::sync_with_stdio(false); cin.tie(NULL);
cin >> n >> k;
for(int i = 0; i < n; i ++) cin >> A[i];
prefix[0] = A[0]; for(int i = 1; i < n; i ++) prefix[i] = prefix[i - 1] + A[i];
for(int _k = 2; _k <= k + 1; _k ++){
solve(_k, 0, n - 1, 0, n - 1);
}
cout << dp[k + 1][n - 1] << endl;
row v;
ll tmp = opt[k + 1][n - 1], r = k;
while(tmp && r){
v.pb(tmp);
tmp = opt[r --][tmp - 1];
}
reverse(all(v));
for(auto x : v) cout << x << " "; cout << endl;
}
/**
*/
Compilation message (stderr)
sequence.cpp: In function 'int main()':
sequence.cpp:70:2: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
70 | for(auto x : v) cout << x << " "; cout << endl;
| ^~~
sequence.cpp:70:36: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
70 | for(auto x : v) cout << x << " "; cout << endl;
| ^~~~
# | 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... |