이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "bits/stdc++.h"
using namespace std;
#define ln "\n"
#define dbg(x) cout << #x << " = " << x << ln
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define inf 2e18
#define fast_cin() \
ios_base::sync_with_stdio(false); \
cin.tie(NULL)
#define out(file) freopen(file, "w", stdout)
#define in(file) freopen(file, "r", stdin)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define int long long
int MOD = 1e9 + 7;
const int N = 1e5 + 7;
int n, k, a[N], ps[N];
vector<int> pre, cur;
vector<vector<int>> trace;
void calc(int l, int r, int optl, int optr, int k) {
if(l > r) return;
pair<int, int> best = {-inf, -1};
int mid = (l + r) >> 1;
for(int i = optl; i <= min(mid-1, optr); i++) {
best = max(best, {pre[i] + ps[i] * (ps[mid]-ps[i]), i});
}
trace[mid][k] = best.se;
cur[mid] = best.fi;
calc(l, mid-1, optl, best.se, k);
calc(mid+1, r, best.se, optr, k);
}
signed main() {
fast_cin();
cin >> n >> k;
for(int i = 1; i <= n; i++) cin >> a[i], ps[i] = ps[i-1] + a[i];
pre.resize(n+5, 0);
cur.resize(n+5, 0);
trace.resize(n+5, vector<int>(k+5, 0));
for(int i = 1; i <= k; i++) {
calc(i+1, n, i-1, n, i);
pre = cur;
}
// for(int i = 1; i <= n; i++) {
// for(int j = 1; j <= k; j++) {
// cout << trace[i][j] << ' ';
// }
// cout << ln;
// }
cout << cur[n] << ln;
vector<int> ans;
int cur = n;
for(int i = k; i > 0; i--) {
assert(cur > 0);
ans.pb(trace[cur][i]);
cur = trace[cur][i];
}
reverse(all(ans));
for(int x : ans) cout << x << ' ';
cerr << "\nTime: " << clock() * 1000 / CLOCKS_PER_SEC;
}
# | 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... |