This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O0")
#pragma GCC optimize("O1")
#pragma GCC optimize("O2")
#pragma GCC optimize("O3")
#pragma GCC optimize("inline")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define int long long
using db = long double;
const int maxn = 1E5+5;
int n, K, l = 1, r = 1, a[maxn], p[maxn], Q[maxn];
vector<int> dp, new_dp;
inline int read() {
int x=0;char ch=getchar_unlocked();
while (!(ch >= '0' && ch <= '9')) ch=getchar_unlocked();
while (ch >= '0' && ch <= '9'){x=x*10+ch-48;ch=getchar_unlocked();}
return x;
}
bool m(int j, int k, int i) {
return ((dp[k] - dp[j]) * (p[i] - p[k])) <= ((dp[i] - dp[k]) * (p[k] - p[j]));
}
bool idk(int j, int k, int i) {
return (p[n] - p[i]) * (p[k] - p[j]) <= (dp[k] - dp[j]);
}
int32_t main() {
ios::sync_with_stdio(0); cin.tie(0);
n = read(); K = read();
new_dp.assign(n+1, 0);
dp.assign(n+1, 0);
for (int i = 1; i <= n; i++) {
a[i] = read();
p[i] = p[i-1] + a[i];
}
for (int i = 1; i <= n; i++) {
dp[i] = p[i] * (p[n] - p[i]);
}
for (int kk = 2; kk <= K; kk++) {
for (int i = 1; i <= n; i++) {
Q[i] = 0;
new_dp[i] = dp[i];
}
l = r = 1;
for (int i = 1; i < kk; i++) Q[r++] = i;
--r;
for (int i = kk; i <= n; i++) {
while (r - l >= 1 && idk(Q[l], Q[l+1], i)) l++;
if (l <= r) new_dp[i] = dp[Q[l]] + (p[i] - p[Q[l]]) * (p[n] - p[i]);
while (r - l >= 1 && m(Q[r-1], Q[r], i)) r--;
r++; Q[r] = i;
}
swap(new_dp, dp);
}
printf("%lld\n", *max_element(dp.begin(), dp.end()));
}
# | 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... |