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>
#define x first
#define y second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MXN = 100001, MXK = 202;
int N, K, s[2], e[2], stk[MXN][2], back[MXN][MXK];
ll sum[MXN], dp[MXN][2];
double cross(int x, int y, int z) {
x = stk[x][z % 2], y = stk[y][z % 2];
if (sum[x] == sum[y]) return -1e18;
ll vx = -sum[N] * sum[x] + dp[x][z % 2], vy = -sum[N] * sum[y] + dp[y][z % 2];
return 1.0 * (vy - vx) / (sum[x] - sum[y]);
}
void insert(int x, int z) {
int zz = z % 2;
stk[e[zz]][zz] = x;
while (e[zz] > 1 && cross(e[zz] - 2, e[zz] - 1, z) > cross(e[zz] - 1, e[zz], z))
stk[e[zz] - 1][zz] = stk[e[zz]][zz], e[zz]--;
e[zz]++;
}
void query(int x, int z) {
z--;
int zz = z % 2;
while (s[zz] + 1 < e[zz] && stk[s[zz]][zz] < x - 1 && cross(s[zz], s[zz] + 1, z) <= sum[x])
s[zz]++;
int y = stk[s[zz]][zz];
if (x <= y) { dp[x][(z + 1) % 2] = -1e18; return; }
dp[x][(z + 1) % 2] = (sum[N] - sum[x]) * (sum[x] - sum[y]) + dp[y][z % 2];
back[x][z + 1] = y;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N >> K; K++;
for (int i = 1; i <= N; ++i) {
ll n; cin >> n;
sum[i] = sum[i - 1] + n;
}
insert(0, 0);
for (int k = 1; k <= K; ++k) {
s[(k - 1) % 2] = e[k % 2] = 0;
for (int i = k; i <= N; ++i) {
//dp[i][k] = max(dp[i][k], dp[j][k - 1] + (sum[N] - sum[i]) * (sum[i] - sum[j]));
query(i, k);
insert(i, k);
}
}
cout << dp[N][K % 2] << '\n';
int cur = back[N][K], cnt = K - 1;
stack<int> st;
while (cnt) {
st.push(cur);
cur = back[cur][cnt];
cnt--;
}
while (!st.empty()) {
cout << st.top() << ' ';
st.pop();
}
cout << '\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... |