제출 #591246

#제출 시각아이디문제언어결과실행 시간메모리
591246dryeab수열 (APIO14_sequence)C++17
0 / 100
9 ms1612 KiB
#include <bits/stdc++.h>

using ll = long long;

using namespace std;

int main() {

    ios::sync_with_stdio(false);
    cin.tie(0);

    int n, k;
    cin >> n >> k;

    vector<ll> a(n + 1);
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
        a[i] += a[i - 1];
    }

    int m, l, r;
    ll M = 0;

    for (int i = 1; i <= n; ++i) {
        if ((a[n] - a[i]) * a[i] > M) {
            m = i;
            M = (a[n] - a[i]) * a[i];
        }
    }

    priority_queue<pair<pair<ll, int>, pair<int, int>>> pq;
    pq.push({{M, m}, {1, n}});

    ll res = 0;
    vector<int> ord;

    while (k) {

        auto top = pq.top();
        pq.pop();

        M = top.first.first, m = top.first.second;
        l = top.second.first, r = top.second.second;

        ord.push_back(m); // add the divided point

        res += M;

        if (l != r) {
            M = 0;
            int m2;
            for (int i = l; i <= m; ++i) {
                if ((a[m] - a[i]) * (a[i] - a[l - 1]) > M) {
                    m2 = i;
                    M = (a[m] - a[i]) * (a[i] - a[l - 1]);
                }
            }

            pq.push({{M, m2}, {l, m}});

            M = 0;
            for (int i = m + 1; i <= r; ++i) {
                if (((a[r] - a[i]) * (a[i] - a[m])) > M) {
                    m2 = i;
                    M = (a[r] - a[i]) * (a[i] - a[m]);
                }
            }

            pq.push({{M, m2}, {m + 1, r}});
        }

        k--;
    }

    cout << res << '\n';

    for (auto x : ord)
        cout << x << " ";
}
#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...