Submission #1157462

#TimeUsernameProblemLanguageResultExecution timeMemory
1157462AmirMakaMSplit the sequence (APIO14_sequence)C++20
50 / 100
2097 ms31816 KiB
#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define ll long long
#define ld long double
#define pb push_back
#define f first
#define s second 
#define sz(x) (int)x.size()
#define all(x) x.begin(),x.end()
#define pii pair<int,int> 
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define pdd pair<double,double>
#define mp make_pair   
#define AmirMakaM ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
//#define int long long
// solve it
const ull SEED = chrono::steady_clock::now().time_since_epoch().count();
mt19937_64 mrand(SEED);
ull rnd(ull x = ~(0ull)) {return mrand() % x;} 
const ll MOD = 998244353;
const ll INF = 1e16+20;
const int inf = 1e9 + 7;
const ll N = 1e5+1;
const ll M = 2e3+1;
const double pi = 2*acos(0.0);
const int dx[] = {1,-1,0,0}, dy[] = {0,0,1,-1};

void solve() {
    int n, k;
    cin >> n >> k;
    ll a[n+1], s[n+2] = {};
    for(int i=1; i<=n; i++) cin >> a[i];
    for(int i=n; i>=0; i--) s[i] = s[i+1] + a[i];

    ll dp[n+1][k+1] = {}, p[n+1][k+1] = {};
    for(int i=1; i<=n; i++) {
        for(int j=0; j<=k; j++) {
            dp[i][j] = -inf;
        }
    }
    for(int i=1; i<n; i++) {
        for(int j=0; j<i; j++) {
            for(int t=1; t<=min(i,k); t++) {
                if(dp[j][t-1] != -inf && dp[i][t] <= dp[j][t-1]+(s[j+1]-s[i+1])*(s[i+1])) {
                    dp[i][t] = dp[j][t-1]+(s[j+1]-s[i+1])*(s[i+1]);
                    p[i][t] = j;
                }
            }
        }
    }

    ll ans = -inf, id = 0;
    for(int i=1; i<=n; i++) {
        if(ans <= dp[i][k]) {
            ans = dp[i][k], id = i;
        }
    }

    vector<int> res;
    for(int i=id, cnt = k; i>0; i = p[i][cnt], cnt--) {
        res.pb(i);
    }
    reverse(all(res));

    cout << ans << '\n';
    for(int x:res) cout << x << " ";
}

int main() {
    AmirMakaM;
    srand(SEED);
    //freopen("bulk.in", "r", stdin);
    //freopen("bulk.out", "w", stdout);
    int ttt = 1;
    //cin >> ttt;
    while(ttt--) {
        solve();
    }
}
#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...