제출 #973371

#제출 시각아이디문제언어결과실행 시간메모리
973371avighnaSplit the sequence (APIO14_sequence)C++17
50 / 100
2054 ms95692 KiB
#include <bits/stdc++.h>

typedef long long ll;

template <typename T> class Vector : public std::vector<T> {
public:
    using std::vector<T>::vector;
    std::vector<ll> psum;

    void calculate_prefix_sum() {
        psum.resize(this->size());
        std::partial_sum(this->begin(), this->end(), psum.begin());
    }

    T sum(ll a, ll b) {
        if (a > b) {
            return 0;
        }
        T ans = psum[b];
        if (a - 1 >= 0) {
            ans -= psum[a - 1];
        }
        return ans;
    }
};

const ll N = 1e5 + 1, K = 201;
const ll inf = 1e15;

ll dp[K][N];

int main() {
    ll n, k;
    std::cin >> n >> k;
    Vector<ll> a(n);
    for (auto &i : a) {
        std::cin >> i;
    }
    a.calculate_prefix_sum();

    if (n == 2) {
        std::cout << a[0] * a[1] << "\n1\n";
        return 0;
    }

    for (ll i = 0; i <= k; ++ i) {
        for (ll j = n - 1; j >= 0; -- j) {
            if (j == n - 1) {
                if (i != 0) {
                    dp[i][j] = -inf;
                } else {
                    dp[i][j] = 0;
                }
                continue;
            }
            if (i == 0) {
                dp[i][j] = 0;
                continue;
            }

            dp[i][j] = 0;
            for (ll c = j; c < n - 1; c++) {
                dp[i][j] = std::max(dp[i][j], a.sum(c + 1, n - 1) * a.sum(j, c) + dp[i - 1][c + 1]);
            }
        }
    }

    std::cout << dp[k][0] << "\n";
    ll i = 0;
    ll ops = k;
    for (ll c = i; c < n - 1 && ops > 0; c++) {
        if (dp[ops][i] == a.sum(i, c) * a.sum(c + 1, n - 1) + dp[ops - 1][c + 1]) {
            std::cout << c + 1 << " ";
            ops--;
            i = c + 1;
        }
    }
}
#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...