이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "bits/stdc++.h"
using namespace std;
using i64 = long long;
using u64 = unsigned long long;
using i32 = int;
using u32 = unsigned;
using i16 = short;
using u16 = unsigned short;
using ld = long double;
using ii = pair<int, int>;
const int N = 1e5 + 1, K = 202;
const i64 inf = 1e18L;
i64 dp[K][N], pre[N];
int n, k, pa[K][N];
void rec(int k, int l, int r, int lo, int hi) {
int mid = (l + r) / 2;
auto ans = make_pair(-inf, 0);
int m = min(mid - 1, hi);
for(int i = lo; i <= m; ++i) {
i64 s = pre[mid] - pre[i];
ans = max(ans, make_pair(dp[k - 1][i] + (pre[n] - s) * s, i));
}
dp[k][mid] = ans.first;
pa[k][mid] = ans.second;
if(l < mid) rec(k, l, mid - 1, lo, ans.second);
if(r > mid) rec(k, mid + 1, r, ans.second, hi);
}
void solve() {
cin >> n >> k;
++k;
for(int i = 1; i <= n; ++i) {
cin >> pre[i];
pre[i] += pre[i - 1];
}
for(int i = 1; i <= n; ++i) dp[1][i] = (pre[n] - pre[i]) * pre[i];
for(int i = 2; i <= k; ++i) rec(i, i, n, i - 1, n);
cout << dp[k][n] / 2 << '\n';
vector<int> s;
for(int i = n; k > 1 && i > 0; --k) {
s.emplace_back(pa[k][i]);
i = pa[k][i];
}
for(int i = (int)s.size() - 1; i >= 0; --i) cout << s[i] << ' ';
cout << '\n';
}
int main() {
ios_base :: sync_with_stdio(false);
cin.tie(0);
int t = 1;
//cin >> t;
while(t--) solve();
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... |