Submission #856718

#TimeUsernameProblemLanguageResultExecution timeMemory
856718a_l_i_r_e_z_aSplit the sequence (APIO14_sequence)C++14
71 / 100
1477 ms131072 KiB
// Be name Khoda
// Khodaya khodet komak kon
// Hope is last to die

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;

#define pb push_back
#define S second
#define F first
#define mp make_pair
#define smax(x, y) (x) = max((x), (y))
#define smin(x, y) (x) = min((x), (y))
#define all(x) (x).begin(), (x).end()
#define set_dec(x) cout << fixed << setprecision(x);
#define kill(x) cout << x << endl, exit(0)
#define sz(x) ((int)(x).size())
#define file_io freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);

const ll maxN = 100000 + 10, maxK = 210, inf = 1e18 + 7;
ll sm[maxN], dp[2][maxN];
vector<int> O[2][maxN];

void divide(ll k, ll l, ll r, ll optl, ll optr){
    if(r - l < 0) return;
    ll mid = (l + r) / 2, op = 0;
    dp[k][mid] = -inf;
    for(int i = optl; i <= min(optr, mid - 1); i++){
        ll res = dp[1 - k][i] + sm[i] * (sm[mid] - sm[i]);
        if(res >= dp[k][mid]) dp[k][mid] = res, op = i;
    }
    O[k][mid] = O[1 - k][op];
    O[k][mid].pb(op);
    divide(k, l, mid - 1, optl, op);
    divide(k, mid + 1, r, op, optr);
}

int32_t main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    ll n, k; cin >> n >> k;
    for(int i = 0; i < n; i++){
        ll x; cin >> x;
        sm[i + 1] = sm[i] + x;
    }
    for(int i = 1; i <= k; i++) divide(i % 2, 1, n, 0, n);
    cout << dp[k % 2][n] << '\n';

    for(auto u: O[k % 2][n]) cout << u << " ";

    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...