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 fi first
#define se second
const int N = 1e5 + 5;
const int K = 200;
const int mod = 1e9 + 7;
const int inf = 1e18 + 7;
#define all(v) (v).begin(), (v).end()
#define pii pair<int, int>
using namespace std;
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
int n, k, a[N], dp[K][N], f[N];
int cost(int i, int j){
return (f[n] - f[j]) * (f[j] - f[i - 1]);
}
void solve(int l, int r, int u, int v, int j){
if(l > r) return;
int mid = l + r >> 1;
pii best = {-inf, -1};
for(int pos = u; pos <= min(mid, v); pos++){
best = max(best, {dp[j - 1][pos - 1] + cost(pos, mid), pos});
}
dp[j][mid] = best.fi;
if(best.se == -1) return;
solve(l, mid - 1, u, best.se, j);
solve(mid + 1, r, best.se, v, j);
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
if(ifstream("sequence.in")){
freopen("sequence.in", "r", stdin);
freopen("sequence.out", "w", stdout);
}
cin >> n >> k;
for(int i = 1; i <= n; i++){
cin >> a[i];
f[i] = f[i - 1] + a[i];
dp[0][i] = -inf;
}
for(int j = 1; j <= k; j++){
for(int i = 0; i <= n; i++) dp[j][i] = -inf;
solve(1, n - 1, 1, n - 1, j);
}
int res = 0, pos = 0;
for(int i = 1; i < n; i++){
if(dp[k][i] > res){
res = dp[k][i];
pos = i;
}
}
cout << res << '\n';
stack<int> split;
for(; k; k--){
split.push(pos);
for(int i = 1; i <= pos; i++){
if(dp[k - 1][i - 1] + cost(i, pos) == dp[k][pos]){
pos = i - 1;
break;
}
}
}
while(split.size()){
cout << split.top() << ' ';
split.pop();
}
return 0;
}
// tuntun
Compilation message (stderr)
sequence.cpp: In function 'void solve(long long int, long long int, long long int, long long int, long long int)':
sequence.cpp:27:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
27 | int mid = l + r >> 1;
| ~~^~~
sequence.cpp: In function 'int main()':
sequence.cpp:43:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
43 | freopen("sequence.in", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:44:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
44 | freopen("sequence.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... |