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 <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <array>
#include <algorithm>
#include <utility>
#include <map>
#include <queue>
#include <set>
#include <cmath>
#include <cstdio>
#include <cstring>
#define ll long long
#define ld long double
#define eps 1e-8
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3f
// change if necessary
#define MAXN 100010
using namespace std;
int n, k;
int nums[MAXN];
ll psum[MAXN];
pair<ll, int> dp[210][MAXN];
int arr[MAXN];
void solve(int i, int l, int r, int opt_l, int opt_r) {
if (l > r) return;
int m = (l + r) / 2;
ll max_prod = 0;
int pos = opt_l;
for (int j = opt_l; j <= min(m - 1, opt_r); j++) {
ll before = psum[j + 1];
ll after = psum[m + 1] - psum[j + 1];
ll prod = before * after + dp[i - 1][j].first;
if (prod >= max_prod) {
max_prod = prod;
pos = j;
}
}
dp[i][m] = {max_prod, pos};
solve(i, l, m - 1, opt_l, pos);
solve(i, m + 1, r, pos, opt_r);
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> nums[i];
}
for (int i = 1; i <= n; i++) {
psum[i] = psum[i - 1] + nums[i - 1];
}
for (int i = 1; i <= k; i++) {
solve(i, 0, n - 1, 0, n - 1);
}
pair<ll, int> ans = {0, 0};
for (int i = k; i < n; i++) {
ans = max(ans, dp[k][i]);
}
cout << ans.first << '\n';
for (int i = k - 1; i >= 0; i--) {
arr[k - i - 1] = ans.second + 1;
ans = dp[i][ans.second];
}
string s = "";
for (int i = k - 1; i >= 0; i--) {
s += to_string(arr[i]);
s += " ";
}
cout << s << '\n';
return 0;
}
# | 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... |