This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//çalışan kazanır//
#include "bits/stdc++.h"
using namespace std;
#define int long long
#define pb push_back
#define S second
#define F first
#define all(x) x.begin(),x.end()
#define YOSIK() ios_base::sync_with_stdio(0),cin.tie(0)
int gcd (int a, int b) {if (b == 0){return a;}else {return gcd (b, a % b);}}
const int N = 2e5 + 1;
const int INF = 2e18 + 1;
const int MOD = 998244353ll;
const int MODD = 1070234587ll;
const double eps = 0.0001;
//const int p = 87;
int n, dp[100001][201], a[N], pref[N], suf[N];
int k;
void solution () {
cin >> n >> k;
for (int i = 1; i <= n; ++ i) cin >> a[i];
for (int i = 1; i <= n; ++ i) pref[i] = a[i] + pref[i - 1];
for (int i = n; i >= 1; -- i) suf[i] = suf[i + 1] + a[i];
for (int t = 1; t <= k; ++ t) {
for (int i = 1; i <= n; ++ i) dp[i][t] = -INF;
}
for (int i = 1; i < n; ++ i) dp[i][1] = pref[i] * suf[i + 1];
vector <pair <int, int>> ord;
for (int t = 2; t <= k; ++ t) {
ord.clear();
ord.pb ({INF, 0});
for (int i = 1; i < n; ++ i) {
if (i + 1 < k) {
dp[i][k] = -INF;
} else {
int l = 0, r = ord.size() - 1, rs = 0;
while (l <= r) {
int mid = (l + r) >> 1;
if (ord[mid].F >= suf[i + 1]) l = mid + 1, rs = mid;
else r = mid - 1;
}
rs = ord[rs].S;
dp[i][t] = dp[rs][t - 1] + (pref[i] - pref[rs]) * suf[i + 1];
}
if (dp[i][t - 1] == -INF) continue;
int per = INF;
while (ord.size()) {
if (pref[ord.back().S] == pref[i]) {;
if (dp[i][t - 1] >= dp[ord.back().S][t - 1]) ord.pop_back();
else break;
continue;
}
per = (dp[i][t - 1] - dp[ord.back().S][t - 1]) / (-pref[ord.back().S] + pref[i]);
if (per > ord.back().F) ord.pop_back();
else break;
}
ord.pb ({per, i});
}
}
int res = 0, bg = 0;
for (int i = 1; i <= n; ++ i) {
res = max (res, dp[i][k]);
if (dp[i][k] == res) bg = i;
}
cout << res << '\n';
vector <int> ans;
ans.pb (bg);
for (int t = k - 1; t >= 1; -- t) {
for (int i = bg - 1; i >= 1; -- i) {
if (dp[i][t] + (pref[bg] - pref[i]) * suf[bg + 1] == res) {
bg = i;
ans.pb (i);
res = dp[i][t];
break;
}
}
}
reverse (all (ans));
for (auto i: ans) cout << i << ' ';
cout << '\n';
return;
}
signed main() {
YOSIK();
// srand (time(0));
// freopen ("E.in", "r", stdin);
// freopen ("E.out", "w", stdout);
int TT = 1;// cin >> TT;
for (int i = 1; i <= TT; ++ i) {
// cout << "Case " << i << ": ";
solution ();
}
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... |