이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "bits/stdc++.h"
using namespace std;
#ifdef Nero
#include "Deb.h"
#else
#define deb(...)
#endif
const int K = 202;
const int N = 1e5 + 5;
struct Line {
int m, b;
Line() {}
Line(int m_, int b_) : m(m_), b(b_) {}
long long val(int x) {
return (long long) m * x + b;
}
};
int a[N];
int path[N][K];
long long dp[N][K];
int pref[N], suf[N];
Line seg[N * 15];
int idd, lx[N * 15], rx[N * 15];
//void upd(int& nd, int l, int
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
cin >> n >> k;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
for (int i = 1; i <= n; ++i) {
pref[i] = pref[i - 1] + a[i];
}
for (int i = n; i >= 1; --i) {
suf[i] = suf[i + 1] + a[i];
}
int ind = 0;
long long ans = -LLONG_MAX;
for (int j = k - 1; j >= 0; --j) {
for (int i = 1; i <= n; ++i) {
long long c = suf[i + 1] * pref[i];
for (int l = i - 1; l >= 0; --l) {
long long cl = dp[l][j + 1] - (long long) suf[i + 1] * pref[l];
if (c + cl > dp[i][j]) {
dp[i][j] = c + cl;
path[i][j] = l;
}
dp[i][j] = max(dp[i][j], c + cl);
if (dp[i][j] > ans) {
ans = dp[i][j];
ind = i;
}
}
}
}
int cnt = 0;
vector<int> p;
while (ind) {
p.push_back(ind);
ind = path[ind][cnt++];
}
reverse(p.begin(), p.end());
cout << ans << '\n';
for (int i : p) {
cout << i << ' ';
}
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... |