Submission #282640

#TimeUsernameProblemLanguageResultExecution timeMemory
282640jainbot27Split the sequence (APIO14_sequence)C++17
50 / 100
2074 ms32512 KiB
#include <bits/stdc++.h>
using namespace std;
//#define int int64_t
signed main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    int n, k;
    cin >> n >> k;
    vector<int> a(n+1), p(n+1);
    for(int i=0; i < n; i++){
        cin >> a[i+1];
        p[i+1] = p[i] + a[i+1];
    }
    vector<vector<int64_t>> dp(n+1, vector<int64_t>(k+1, 0));
    vector<vector<int64_t>> par(n + 1, vector<int64_t>(k + 1, 0));
    int besti, bestj;
    int64_t ans = -1;
    for(int i=1; i <= n; i++){
        for(int j=1; j <= k; j++){
            for(int l = 0; l < i; l++){
                if(dp[i][j] < dp[l][j-1] + (int64_t)(p[i]-p[l]) * (p[n]-p[i]) && !(j>1&&l==0)){
                    dp[i][j] = dp[l][j-1] + (int64_t)(p[i] - p[l]) * (p[n] - p[i]);
                    par[i][j] = l;
                }
                if(j==k){
                    if(ans < dp[i][j]){
                        ans = dp[i][j];
                        besti = i; bestj = j;
                    }
                }
                //cerr << "I: " << i << " J: " << j << " K: " << k << " P[i]: " << p[i] << " P[l-1] " << p[l-1] << "\n";
            }
            //cerr << "(" << i << "," << j << ") " << dp[i][j] << "\n";
        }
    }
    cout << ans << "\n";
    assert(bestj == k);
    vector<int> res;
    for(int i=0; i < k; i++){
        res.push_back(besti);
        besti = par[besti][bestj];
        bestj--;
    }
    reverse(res.begin(), res.end());
    for(int i : res){
        cout << i << " ";
    }
    cout << "\n";
}

Compilation message (stderr)

sequence.cpp: In function 'int main()':
sequence.cpp:15:16: warning: 'bestj' may be used uninitialized in this function [-Wmaybe-uninitialized]
   15 |     int besti, bestj;
      |                ^~~~~
#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...