Submission #900020

#TimeUsernameProblemLanguageResultExecution timeMemory
900020ByeWorld수열 (APIO14_sequence)C++14
71 / 100
2053 ms83292 KiB
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops")
#define bupol __builtin_popcount
//#define int long long 
#define ll long long
#define ld long double
#define fi first
#define se second
#define pb push_back
#define lf (id<<1)
#define rg ((id<<1)|1)
#define md ((l+r)>>1)
using namespace std;
const int MAXN = 1e5+5;
const int MAXK = 205;
const int LOG = 20;
const int MOD = 1e9+7;
const int SQRT = 520;
const ll INF = 2e18;
typedef pair<int,int> pii;
typedef pair<int,pii> ipii;
 
int n, k;
int a[MAXN], pr[MAXN];
ll dp[MAXN][3]; int ba[MAXN][MAXK];
 
signed main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin >> n >> k;
    for(int i=1; i<=n; i++){
        cin >> a[i]; pr[i] = pr[i-1]+a[i];
    }
            
    for(int m=1; m<=k; m++){
        for(int i=1; i<=n; i++) ba[i][m] = -1;
        int las = 1;
        for(int i=2; i<=n; i++){
            dp[i][m%2] = -INF; 
            for(int j=las; j<=i-1; j++){
                if(ba[j][(m-1)] == -1) continue; // gk ada ke belakangnya
                if(dp[j][(m-1)%2] + 1ll * pr[j] * (pr[i]-pr[j]) > dp[i][m%2]){ // cek yg optimal potong dmn
                    dp[i][m%2] = dp[j][(m-1)%2] + 1ll * pr[j] * (pr[i]-pr[j]); // cmn ngebuild sampe dp[i][i-1]
                    ba[i][m] = j;
                }
            }
            las = ba[i][m];
            //cout << dp[i][m] <<' ' << i << ' ' << m << '\n';
        }
    }
    cout << dp[n][k%2] << '\n';

    int nw = n;
    for(int m=k; m>=1; m--){
        nw = ba[nw][m];
        //cout << dp[i][m] <<' ' << i << ' ' << m << '\n';
        cout << nw << ' ';
    } 
    cout << '\n';
}
#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...