이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define TASK ""
#define all(x) x.begin(), x.end()
using namespace std;
const int N = 1e5 + 7;
const int MOD = 1e9 + 7; // 998244353;
const int INF = 1e9 + 7;
const long long INFLL = 1e18 + 7;
typedef long long ll;
template <class X, class Y> bool minimize(X &a, Y b) {
if (a > b) return a = b, true;
return false;
}
template <class X, class Y> bool maximize(X &a, Y b) {
if (a < b) return a = b, true;
return false;
}
int n, k;
int pre[N];
ll dp[2][N];
int trace[222][N];
void compute(int l, int r, int li, int ri, int level) {
if (l > r) return;
int mid = (l + r) >> 1;
pair<ll, int> best(-INF, 0);
for (int i = li; i < min(mid, ri + 1); i ++) {
ll tmp = 1LL * (pre[mid] - pre[i]) * (pre[n] - pre[mid]);
maximize(best, mp(tmp + dp[!(level & 1)][i], i));
}
dp[level & 1][mid] = best.fi;
trace[level][mid] = best.se;
compute(l, mid - 1, li, best.se, level);
compute(mid + 1, r, best.se, ri, level);
}
void solve(void) {
cin >> n >> k;
for (int i = 1; i <= n; i ++) {
int x; cin >> x;
pre[i] = pre[i - 1] + x;
}
for (int i = 1; i <= k; i ++) {
compute(i, n - 1, 0, n - 1, i);
// for (int j = 1; j <= n; j ++) cout << dp[i & 1][j] << ' ';
// cout << '\n';
}
pair<ll, int> res(-INF, 0);
for (int i = 1; i < n; i ++) maximize(res, mp(dp[k & 1][i], i));
cout << res.fi << '\n';
for (int cur = res.se; k; cur = trace[k][cur], k --)
cout << cur << " ";
}
int main(void) {
cin.tie(0)->sync_with_stdio(0);
int test = 1;
// cin >> test;
while (test --) {
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... |