#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
/**zagaro & lauren <3**/
#define mod 1000000007 //1e9 + 7
#define pi acos(-1)
#define wl while
#define str string
#define ENDL "\n"
#define sal ' '
#define tp_set ll
#define prc(n) cout.precision(n);cout<<fixed;
#define ord_set tree<tp_set, null_type, less<tp_set>, rb_tree_tag, tree_order_statistics_node_update>
typedef long long ll;
typedef bool bl;
typedef char car;
using namespace std;
using namespace __gnu_pbds;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
ll n, k, a, b;
bl B;
cin>>n>>k;
k++;
vector<ll> vec(n+1);
vector<ll> prf(n+1);
vector<vector<ll> > dpi(n+1, vector<ll> (k+1, LONG_LONG_MIN));
vector<vector<ll> > dpm(n+1, vector<ll> (k+1, LONG_LONG_MIN));
vector<vector<ll> > vic(n+1, vector<ll> (k+1));
vector<vector<bl> > posi(n+1, vector<bl> (k+1, false));
vector<vector<bl> > posm(n+1, vector<bl> (k+1, false));
for(int i=1;i<=n;i++){
cin>>vec[i];
prf[i] = prf[i-1] + vec[i];
dpi[i][0] = 0;
dpm[i][0] = 0;
}
dpi[0][0] = 0;
dpm[0][0] = 0;
for(int i=1;i<=n;i++){
dpi[i][1] = 0;
dpm[i][1] = 0;
for(int j=2;j<=k;j++){
a = dpi[i-1][j-1];b = dpm[i-1][j-1];
dpi[i][j] = max(dpi[i-1][j-1], dpm[i-1][j-1])+vec[i]*prf[i-1];
if(a > b)posi[i][j] = false;
else posi[i][j] = true;
a = dpi[i-1][j]+vec[i]*prf[max(i-2, 0)];
if(dpm[i-1][j] == LONG_LONG_MIN)b=LONG_LONG_MIN;
else b = dpm[i-1][j]+vec[i]*prf[vic[i-1][j]];
if(a >= b){
dpm[i][j]=a;
vic[i][j] = i-2;
posm[i][j]=false;
}
else {
dpm[i][j]=b;
vic[i][j] = vic[i-1][j];
posm[i][j]=true;
}
}
}
cout<<max(dpm[n][k], dpi[n][k])<<ENDL;
vector<bl> vis(k+1, false);
vis[k]=vis[0]=true;
if(dpm[n][k] > dpi[n][k])B=true;
else B=false;
a=n;b=k;
wl(b >= 1){
if(!vis[b]){
cout<<a<<sal;
vis[b] = true;
}
if(B){
if(!posm[a][b])B=false;
a--;
}
else {
if(posi[a][b])B=true;
a--;b--;
}
}
cout<<ENDL;
return 0;
}
# | 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... |