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>
using namespace std;
typedef long long ll;
typedef long double ld;
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.setf(ios::fixed); cout.precision(20);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int n, k;
cin >> n >> k;
vector <int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
vector <vector <ll>> dp(n + 1, vector <ll> (k + 1));
vector <ll> pref(n + 1);
for (int i = 0; i < n; ++i) {
pref[i + 1] = pref[i] + a[i];
}
vector <vector <int>> who(n + 1, vector <int> (k + 1, -1));
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= k; ++j) {
for (int t = 0; t < n; ++t) {
if ((pref[i] - pref[t]) * (pref[n] - pref[i]) + dp[t][j - 1] > dp[i][j]) {
dp[i][j] = (pref[i] - pref[t]) * (pref[n] - pref[i]) + dp[t][j - 1];
who[i][j] = t;
}
}
}
}
ll ans = 0;
int id = -1;
for (int i = 0; i <= n; ++i) {
if (dp[i][k] > ans) {
ans = dp[i][k];
id = i;
}
}
vector <int> where;
for (int cr = k; cr >= 1; --cr) {
where.push_back(id);
id = who[id][cr];
}
reverse(where.begin(), where.end());
cout << ans << '\n';
for (auto x : where) {
cout << x << ' ';
}
}
# | 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... |