This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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][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 i=0; i<=n+1; i++)
for(int j=1; j<=k+1; j++) dp[i][j] = -1;
for(int m=1; m<=k; m++){
int las = 1;
for(int i=2; i<=n; i++){
dp[i][m] = -INF;
int opt = -1;
for(int j=las; j<=i-1; j++){
if(dp[j][m-1] == -1) continue; // gk ada ke belakangnya
if(dp[j][m-1] + 1ll * pr[j] * (pr[i]-pr[j]) > dp[i][m]){ // cek yg optimal potong dmn
dp[i][m] = dp[j][m-1] + 1ll * pr[j] * (pr[i]-pr[j]); // cmn ngebuild sampe dp[i][i-1]
opt = j;
}
}
las = opt;
//cout << dp[i][m] <<' ' << i << ' ' << m << '\n';
}
}
cout << dp[n][k] << '\n';
int nw = n;
for(int m=k; m>=1; m--){
int i = nw;
dp[i][m] = -INF;
int opt = -1;
for(int j=1; j<=i-1; j++){
if(dp[j][m-1] == -1) continue; // gk ada ke belakangnya
if(dp[j][m-1] + 1ll * pr[j] * (pr[i]-pr[j]) > dp[i][m]){ // cek yg optimal potong dmn
dp[i][m] = dp[j][m-1] + 1ll * pr[j] * (pr[i]-pr[j]); // cmn ngebuild sampe dp[i][i-1]
opt = j;
}
}
nw = opt;
//cout << dp[i][m] <<' ' << i << ' ' << m << '\n';
cout << nw << ' ';
}
cout << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |