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>
using namespace std;
#define all(x) x.begin(), x.end()
#define pb push_back
#define ln '\n'
#define int long long
bool chmin(int &x, int y){
bool flag = false;
if ( x > y ){
x = y; flag |= true;
}
return flag;
}
bool chmax(int &x, int y){
bool flag = false;
if ( x < y ){
x = y; flag |= true;
}
return flag;
}
const int N = 1e5 + 1, inf = 1e15 + 1;
int dp[N][202], par[N][202];
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, k; cin >> n >> k;
vector <int> p(n), pref(n + 1);
for ( auto &i: p ) cin >> i;
for ( int i = 0; i < n; i++ ){
pref[i + 1] = pref[i] + p[i];
}
for ( int i = 0; i < N; i++ ){
for ( int j = 0; j <= 200; j++ ){
dp[i][j] = -inf;
}
}
dp[0][0] = 0; ++k;
for ( int i = 1; i <= n; i++ ){
for ( int j = 1; j <= k; j++ ){
for ( int l = 0; l < i; l++ ){
int v = (pref[i] - pref[l]) * pref[l];
if ( chmax(dp[i][j], dp[l][j - 1] + v) ){
par[i][j] = l;
}
}
}
}
cout << dp[n][k] << ln;
vector <int> res;
int it = n;
while ( k > 0 ){
it = par[it][k--];
if ( it ) res.pb(it);
}
reverse(all(res));
for ( auto i: res ){
cout << i << ' ';
}
cout << '\n';
}
/*
7 3
4 1 3 4 0 2 3
*/
# | 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... |