#include <bits/stdc++.h>
// #include <ext/rope>
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
// using namespace __gnu_cxx;
using namespace std;
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
// #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define bit(mask, i) ((mask >> i) & 1)
#define el '\n'
#define F first
#define S second
template <class X, class Y> bool maximize(X &x, const Y &y) { return (x < y ? x = y, 1 : 0); }
template <class X, class Y> bool minimize(X &x, const Y &y) { return (x > y ? x = y, 1 : 0); }
const int INF = 1e9;
const ll LINF = 1e18;
const int MOD = 1e9 + 7;
const int MULTI = 0;
const ld eps = 1e-9;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; // R D L U
const int ddx[4] = {-1, 1, 1, -1}, ddy[4] = {1, 1, -1, -1}; // UR DR DL UL
const char cx[4] = {'R', 'D', 'L', 'U'};
const ll base = 31;
const int nMOD = 2;
const ll mods[] = {(ll)1e9 + 10777, (ll)1e9 + 19777, (ll)1e9 + 3, (ll)1e9 + 3777};
const int N = 1e5 + 5;
const int K = 2e2 + 5;
int n, k, a[N], ptr, tr[N][K];
ll dp[N][2], suf[N];
vector<ll> A, B;
vector<int> id;
#define dp(i, j) dp[i][j & 1]
bool bad(int l1, int l2, int l3) {
return (B[l3] - B[l1]) * (A[l1] - A[l2]) < (B[l2] - B[l1]) * (A[l1] - A[l3]);
}
void add(ll a, ll b, int i) {
A.push_back(a);
B.push_back(b);
id.push_back(i);
if ((int) A.size() == 1) return;
if (A[(int) A.size() - 2] == a) {
if (a <= A[(int) A.size() - 2]) {
A.pop_back();
B.pop_back();
id.pop_back();
return;
}
A.erase(A.end() - 2);
B.erase(B.end() - 2);
id.erase(id.end() - 2);
}
while ((int) A.size() >= 3 && bad((int) A.size() - 3, (int) A.size() - 2, (int) A.size() - 1)) {
A.erase(A.end() - 2);
B.erase(B.end() - 2);
id.erase(id.end() - 2);
}
}
pair<ll, int> query(ll x) {
if (ptr >= (int) A.size())
ptr = (int) A.size() - 1;
while (ptr < (int) A.size() - 1 && A[ptr + 1] * x + B[ptr + 1] > A[ptr] * x + B[ptr])
++ptr;
return make_pair(A[ptr] * x + B[ptr], id[ptr]);
}
void solve() {
cin >> n >> k;
for (int i = 1; i <= n; ++i)
cin >> a[i];
for (int i = n; i >= 1; --i)
suf[i] = suf[i + 1] + a[i];
for (int i = 1; i <= n; ++i)
dp(i, 0) = 0;
for (int j = 1; j <= k; ++j) {
A.clear(); B.clear(); id.clear();
ptr = 0;
for (int i = 1; i <= n; ++i)
dp(i, j) = -1;
for (int i = n; i > n - j; --i)
if (dp(i, j - 1) != -1)
add(suf[i], dp(i, j - 1) - suf[i] * suf[i], i);
for (int i = n - j; i >= 1; --i) {
pair<ll, int> get = query(suf[i]);
dp(i, j) = get.F; tr[i][j] = get.S;
if (dp(i, j - 1) != -1)
add(suf[i], dp(i, j - 1) - suf[i] * suf[i], i);
}
}
vector<int> trace; int u = 1, v = k;
while (v) trace.push_back(tr[u][v] - 1), u = tr[u][v], v--;
cout << dp(1, k) << el;
for (int x: trace) cout << x << ' ';
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (!MULTI) solve();
else {
int test; cin >> test;
while (test--) solve();
}
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... |