Submission #137268

#TimeUsernameProblemLanguageResultExecution timeMemory
137268egorlifarSplit the sequence (APIO14_sequence)C++17
100 / 100
1255 ms87712 KiB
/*
ЗАПУСКАЕМ
░ГУСЯ░▄▀▀▀▄░РАБОТЯГУ░░
▄███▀░◐░░░▌░░░░░░░
░░░░▌░░░░░▐░░░░░░░
░░░░▐░░░░░▐░░░░░░░
░░░░▌░░░░░▐▄▄░░░░░
░░░░▌░░░░▄▀▒▒▀▀▀▀▄
░░░▐░░░░▐▒▒▒▒▒▒▒▒▀▀▄
░░░▐░░░░▐▄▒▒▒▒▒▒▒▒▒▒▀▄
░░░░▀▄░░░░▀▄▒▒▒▒▒▒▒▒▒▒▀▄
░░░░░░▀▄▄▄▄▄█▄▄▄▄▄▄▄▄▄▄▄▀▄
░░░░░░░░░░░▌▌░▌▌░░░░░
░░░░░░░░░░░▌▌░▌▌░░░░░
░░░░░░░░░▄▄▌▌▄▌▌░░░░░
 */
#include <iostream>
#include <complex>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <numeric>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <cmath>
#include <bitset>
#include <cassert>
#include <queue>
#include <stack>
#include <deque>
#include <random>
 
using namespace std;
template<typename T1, typename T2>inline void chkmin(T1 &x, T2 y) { if (x > y) x = y; }
template<typename T1, typename T2>inline void chkmax(T1 &x, T2 y) { if (x < y) x = y; }
#define sz(c) (int)(c).size()
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define left left224
#define right right224
#define next next224
#define rank rank224
#define prev prev224
#define y1 y1224
#define read(FILENAME) freopen((FILENAME + ".in").c_str(), "r", stdin)
#define write(FILENAME) freopen((FILENAME + ".out").c_str(), "w", stdout)
#define files(FILENAME) read(FILENAME), write(FILENAME)
#define pb push_back
#define mp make_pair
const string FILENAME = "input";
const int MAXN = 100228;
using ll = long long;

struct Line
{
    ll k, b;
    int id;
    Line(){}
    Line(ll _k, ll _b, int _id): k(_k), b(_b), id(_id){}
    ll value(ll x) {
        return k * x + b;
    }
};


bool better(Line a, Line b, Line c) {
    return (__int128)(c.b - a.b) * (a.k - b.k) <= (__int128)(b.b - a.b) * (a.k - c.k);
}

int n, k;
int a[MAXN];
ll pref[MAXN];
ll dp[2][MAXN];
int pr[205][MAXN];


int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    //read(FILENAME); 
    cin >> n >> k;
    long long sum = 0;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        sum += a[i];
    }
    sum *= sum;
    for (int i = 0; i < n; i++) {
        pref[i] = a[i];
        if (i) {
            pref[i] += pref[i - 1]; 
        }
    }
    int g = 0;
    for (int i = 1; i <= k + 1; i++) {
        vector<Line> st;
        int uk = 0;
        for (int j = 0; j < n; j++) {
            if (i == 1) {
                dp[g][j] = pref[j] * pref[j];
            } else {
                if (st.empty()) {
                    dp[g][j] = 3e18;
                } else {
                    while (uk + 1 < sz(st) && st[uk + 1].value(pref[j]) >= st[uk].value(pref[j])) {
                        uk++;
                    }
                    dp[g][j] = -st[uk].value(pref[j]) + pref[j] * pref[j];
                    pr[i][j] = st[uk].id;
                }
                Line k(2 * pref[j], -(dp[g ^ 1][j] + pref[j] * pref[j]), j);
                while (sz(st) >= 2 && better(st[sz(st) - 2], st.back(), k)) {
                    st.pop_back();
                }
                chkmin(uk, sz(st) - 1);
                st.pb(k);
                chkmax(uk, 0);
            }
        }
        g ^= 1;
    }
    cout << (sum - dp[g ^ 1][n - 1]) / 2 << '\n';
    int cur = n - 1;
    vector<int> st;
    for (int j = k + 1; j > 1; j--) {
        cur = pr[j][cur];
        st.pb(cur + 1);
    }
    reverse(all(st));
    for (auto x: st) {
        cout << x << ' ';
    }
    cout << endl;
    return 0; 
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...