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>
#define all(x) (x).begin() , (x).end()
#define pll pair<long long, long long>
#define pii pair<int , int>
#define fi first
#define se second
#define bit(i,j) ((j >> i) & 1)
using namespace std;
#define int long long
const long long inf = 1e18+1;
const int mod = 998244353;
const int MAXN = 100000;
int x[MAXN+10] , pre[MAXN + 10];
int dp[MAXN+10] , lst[MAXN+ 10] ;int32_t trace[MAXN+10][210];
void compute(int l , int r , int optl , int optr , int k){
if(l > r) return;
int m = l + r >> 1;
pll res = {-1e18 , -1};
for(int i = optl ; i <= min(m , optr) ; i++){
int t = lst[i-1] + ((pre[m] - pre[i-1]) * pre[i-1]);
res= max(res , {t , i});
}
trace[m][k] = res.se-1;
dp[m] = res.fi;
int opt = res.se;
compute(l , m-1 , optl , opt , k);
compute(m + 1 , r , opt, optr , k);
}
void solve(){
int n , k; cin >> n >> k;
for(int i = 1 ; i <= n ; i++){
cin >> x[i];
pre[i] = pre[i-1] + x[i];
}
for(int i = 1 ; i <= n ; i++) lst[i] = -inf;
lst[0] = 0;
for(int i = 1 ; i <= k+1; i++){
compute(1 , n , 1 , n , i);
for(int i = 0 ; i <= n ; i ++) lst[i] = dp[i];
}
cout << lst[n] << "\n";
int t = n , h = k + 1;
while(h>0){
if(trace[t][h] != 0) cout << trace[t][h] << " ";
t = trace[t][h];
h--;
}
cout << "\n";
}
int32_t main(){
//freopen("PAT.INP", "r", stdin);
//freopen("PAT.OUT", "w", stdout);
ios_base::sync_with_stdio(0); cin.tie(0);
solve();
return 0;
}
Compilation message (stderr)
sequence.cpp: In function 'void compute(long long int, long long int, long long int, long long int, long long int)':
sequence.cpp:21:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
21 | int m = l + r >> 1;
| ~~^~~
# | 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... |