#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) x.begin(), x.end()
#define pb push_back
#define ar array
#define nl '\n'
template <typename A, typename B>
bool chmin(A &a, const B &b) {
if(a > b) {
return a = b, true;
}
return false;
}
template <typename A, typename B>
bool chmax(A &a, const B &b) {
if(a < b) {
return a = b, true;
}
return false;
}
const int inf = 1e18;
void solve() {
int n, k; cin >> n >> k;
vector<int> a(n);
for(auto &x : a) cin >> x;
vector<int> pref(n + 1);
for(int i = 0; i < n; i++) {
pref[i + 1] = pref[i] + a[i];
}
vector<int> dp(n + 1);
vector<vector<int>> p(k + 1, vector<int>(n + 1, -1));
for(int i = 0; i <= k; i++) {
vector<int> ndp(n + 1, -inf);
for(int j = 1; j <= n; j++) {
for(int l = 0; l < j; l++) {
if(chmax(ndp[j], dp[l] + (pref[j] - pref[l]) * (pref[n] - pref[j]))) {
p[i][j] = l;
}
}
}
swap(dp, ndp);
}
cout << dp[n] << nl;
for(int i = k, cur = n; i >= 1; i--) {
cout << p[i][cur] << ' ';
cur = p[i][cur];
}
cout << nl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
}
# | 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... |