This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
// is short or still long ???
hollwo_pelw's template(short)
// Note : -Dhollwo_pelw_local
*/
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/trie_policy.hpp>
// #include <ext/rope>
using namespace std;
// using namespace __gnu_pbds;
// using namespace __gnu_cxx;
void FAST_IO(string filein = "", string fileout = "", string fileerr = ""){
if (fopen(filein.c_str(), "r")){
freopen(filein.c_str(), "r", stdin);
freopen(fileout.c_str(), "w", stdout);
#ifdef hollwo_pelw_local
freopen(fileerr.c_str(), "w", stderr);
#endif
}
cin.tie(0), cout.tie(0) -> sync_with_stdio(0);
}
void Hollwo_Pelw();
signed main(){
#ifdef hollwo_pelw_local
FAST_IO("input.inp", "output.out", "error.err");
auto start = chrono::steady_clock::now();
#else
FAST_IO("hollwo_pelw.inp", "hollwo_pelw.out");
#endif
int testcases = 1;
// cin >> testcases;
for (int test = 1; test <= testcases; test++){
// cout << "Case #" << test << ": ";
Hollwo_Pelw();
}
#ifdef hollwo_pelw_local
auto end = chrono::steady_clock::now();
cout << "\nExcution time : " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl;
#endif
return 0;
}
const int N = 1e5 + 5, K = 202;
int n, k, pre[N], tr[K][N];
int64_t dp[2][N];
inline bool chkmax(int64_t &a, int64_t b) {
return a < b ? a = b, 1 : 0;
}
int64_t calc(int l, int r) {
return 1ll * (pre[r] - pre[l]) * (pre[n] - pre[r]);
}
void solve(int lvl, int l, int r, int opl, int opr) {
if (l > r) return ;
int m = l + r >> 1, opm = 0;
dp[lvl & 1][m] = -1e18;
for (int i = opl; i <= min(m - 1, opr); i++)
if (chkmax(dp[lvl & 1][m], dp[lvl & 1 ^ 1][i] + calc(i, m))) opm = i;
tr[lvl][m] = opm;
assert(opm > 0);
solve(lvl, l, m - 1, opl, opm);
solve(lvl, m + 1, r, opm, opr);
}
void Hollwo_Pelw() {
cin >> n >> k;
for (int i = 1; i <= n; i++)
cin >> pre[i], pre[i] += pre[i - 1];
for (int i = 1; i <= k; i++)
solve(i, i, n - 1, 0, n - 1);
// for (int i = 1; i < n; i++)
// cout << dp[k & 1][i] << " ";
int64_t res = -1e18, cur = 0;
for (int i = k; i < n; i++)
if (chkmax(res, dp[k & 1][i])) cur = i;
assert(cur != 0);
cout << res << '\n';
for (; k; cur = tr[k --][cur])
cout << cur << ' ';
}
Compilation message (stderr)
sequence.cpp: In function 'void solve(int, int, int, int, int)':
sequence.cpp:65:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
65 | int m = l + r >> 1, opm = 0;
| ~~^~~
sequence.cpp:68:37: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
68 | if (chkmax(dp[lvl & 1][m], dp[lvl & 1 ^ 1][i] + calc(i, m))) opm = i;
| ~~~~^~~
sequence.cpp: In function 'void FAST_IO(std::string, std::string, std::string)':
sequence.cpp:18:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
18 | freopen(filein.c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:19:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
19 | freopen(fileout.c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |