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;
#define int long long
const int N = 1e5;
int n, K, prefix[N + 5];
set<tuple<int, int, int, int>, greater<tuple<int, int, int, int>>> segment;
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> K;
for (int i = 1;i <= n; i++) {
int x;
cin >> x;
prefix[i] = prefix[i - 1] + x;
}
int index = 1, cur = 0;
for (int i = 1; i < n; i++) index = cur < prefix[i]*(prefix[n] - prefix[i]) ? i : index, cur = max(cur, prefix[i]*(prefix[n] - prefix[i]));
segment.insert({cur, 1, n, index});
int ans = 0;
while (K) {
K--;
auto x = *segment.begin();
segment.erase(segment.begin());
int v = get<0>(x), a = get<1>(x), b = get<2>(x), m = get<3>(x);
ans += v;
if (a == b) continue;
int index = a, cur = 0;
for (int i = a; i <= m; i++) {
int res = (prefix[i] - prefix[a - 1])*(prefix[m] - prefix[i]);
if (cur < res) cur = res, index = i;
}
segment.insert({cur, a, m, index});
index = m + 1, cur = 0;
for (int i = m + 1; i <= b; i++) {
int res = (prefix[i] - prefix[m])*(prefix[b] - prefix[i]);
if (cur < res) cur = res, index = i;
}
segment.insert({cur, m + 1, b, index});
}
cout << ans << '\n';
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... |