이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define dbg(x) cerr << #x << ": " << x << endl;
const int MAX_N = 1e5 + 1;
const int MAX_K = 200 + 1;
ll dp[MAX_N][MAX_K];
int p[MAX_N][MAX_K];
ll a[MAX_N];
ll pref[MAX_N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
cin >> n >> k;
ll total_sum = 0;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
pref[i] = pref[i - 1] + a[i];
total_sum += a[i];
}
for (int j = 1; j <= k; ++j) {
for (int i = 1; i <= n; ++i) {
for (int d = 0; d < i; ++d) {
ll s = total_sum - pref[d];
ll x = pref[i] - pref[d];
ll curr_cost = dp[d][j - 1] + x * (s - x);
if (curr_cost >= dp[i][j]) {
dp[i][j] = curr_cost;
p[i][j] = d;
}
}
}
}
ll ans = 0;
int opt = -1;
for (int i = 1; i <= n; ++i) {
if (dp[i][k] >= ans) {
ans = dp[i][k];
opt = i;
}
}
vector<int> splits;
int curr_i = opt;
int curr_k = k;
while (curr_k > 0) {
splits.push_back(curr_i);
curr_i = p[curr_i][curr_k];
--curr_k;
}
reverse(splits.begin(), splits.end());
cout << ans << '\n';
for (auto x : splits) {
cout << x << ' ';
}
cout << '\n';
return 0;
}
/*
7 3
4 1 3 4 0 2 3
*/
# | 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... |