이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int const maxn = 1010;
int const maxk = 210;
ll const inf = 0x3f3f3f3f3f3f3f3f;
ll p[maxn];
ll dp[maxk][maxn], opt[maxk][maxn];
ll solve(int k, int n) {
if (dp[k][n] >= 0) return dp[k][n];
if (k == 0) return 0;
if (n == 1) return -inf;
ll& d = dp[k][n];
ll& op = opt[k][n];
d = -inf, op = 1;
for (int i = k; i < n; i++) {
ll cost = p[i] * (p[n]-p[i]) + solve(k-1, i);
if (cost > d) {
op = i;
d = cost;
}
}
return d;
}
vector<ll> recover(int k, int n) {
vector<ll> res;
while (k != 0) {
res.push_back(opt[k][n]);
n = opt[k][n];
k--;
}
reverse(res.begin(), res.end());
return res;
}
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
memset(dp, -1, sizeof(dp));
int n, k;
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> p[i];
p[i] += p[i-1];
}
ll ans = solve(k, n);
auto res = recover(k, n);
cout << ans << "\n";
for (ll i : res) cout << i << " ";
cout << "\n";
}
# | 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... |