This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5, K = 201;
int a[N], par[N][K], lvl;
long long dp[N][2], C[N], ps[N];
long long Cost(int l, int r) {return C[r] - C[l - 1] - (ps[r] - ps[l - 1]) * ps[l - 1];}
void Solve(int s, int e, int l, int r) {
if (s == e) return ;
int mid = (s+e)>>1, pos = l;
dp[mid][lvl&1] = dp[l - 1][lvl&1^1] + Cost(l, mid);
for (int i = l + 1; i <= min(mid, r); i++)
if (dp[i - 1][lvl&1^1] + Cost(i, mid) <= dp[mid][lvl&1]) dp[mid][lvl&1] = dp[i - 1][lvl&1^1] + Cost(i, mid), pos = i;
par[mid][lvl] = pos - 1;
Solve(s, mid, l, pos), Solve(mid + 1, e, pos, r);
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n, k; cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i], dp[i][0] = C[i] = C[i - 1] + a[i] * ps[i - 1], ps[i] = ps[i - 1] + a[i];
for (lvl = 1; lvl <= k; lvl++) Solve(1, n + 1, 1, n);
cout << C[n] - dp[n][k&1] << "\n";
vector <int> ans;
while (par[n][k]) ans.push_back(n = par[n][k]), k--;
reverse(ans.begin(), ans.end());
for (int i : ans) cout << i << " ";
cout << "\n";
return 0;
}
Compilation message (stderr)
sequence.cpp: In function 'void Solve(int, int, int, int)':
sequence.cpp:12:35: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
dp[mid][lvl&1] = dp[l - 1][lvl&1^1] + Cost(l, mid);
~~~^~
sequence.cpp:14:26: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
if (dp[i - 1][lvl&1^1] + Cost(i, mid) <= dp[mid][lvl&1]) dp[mid][lvl&1] = dp[i - 1][lvl&1^1] + Cost(i, mid), pos = i;
~~~^~
sequence.cpp:14:96: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
if (dp[i - 1][lvl&1^1] + Cost(i, mid) <= dp[mid][lvl&1]) dp[mid][lvl&1] = dp[i - 1][lvl&1^1] + Cost(i, mid), pos = i;
~~~^~
# | 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... |