#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, R=0, a, b, d, x, y, z;
cin>>n>>k;
vector<ll> prf(n+1, 0);
vector<ll> vec(n+1, 0);
vector<vector<pair<ll,ll> > > dp(n+1, vector<pair<ll,ll> > (k+1, {LONG_LONG_MIN, LONG_LONG_MIN}));
vector<vector<pair<tuple<ll,ll,ll>, tuple<ll,ll,ll> > > > pos(n+1, vector<pair<tuple<ll,ll,ll>, tuple<ll,ll,ll>> > (k+1));
vector<vector<ll> > pd(n+1, vector<ll> (k+1, 0));
for(int i=0;i<=n;i++)dp[i][0] = {0, 0};
for(int i=1;i<=n;i++){
cin>>vec[i];
prf[i] = prf[i-1]+vec[i];
}
for(int i=1;i<=n;i++){
pd[i][0] = pd[i-1][0] + vec[i];
pd[i][0] = pd[i-1][0] + vec[i];
dp[i][0] = {0, 0};
for(int j=1;j<=k;j++){
if(dp[i-1][j].first >= dp[i-1][j].second){
dp[i][j].first = dp[i-1][j].first;
pd[i][j] = pd[i-1][j] + vec[i];
pos[i][j].first = {i-1, j, 0};
}
else {
dp[i][j].first = dp[i-1][j].second;
pd[i][j] = vec[i];
pos[i][j].first = {i-1, j, 1};
}
dp[i][j].second = max(dp[i-1][j-1].first+((vec[i]+pd[i-1][j-1])*(prf[n]-prf[i])), dp[i-1][j-1].second+vec[i]*(prf[n]-prf[i]));
if(dp[i-1][j-1].first+((vec[i]+pd[i-1][j-1])*(prf[n]-prf[i])) > dp[i-1][j-1].second+vec[i]*(prf[n]-prf[i]))pos[i][j].second = {i-1, j-1, 0};
else pos[i][j].second = {i-1, j-1, 1};
}
}
cout<<dp[n][k].first<<ENDL;
a=n;b=k;d=0;
wl(b != 0){
if(d == 0)tie(x, y, z) = pos[a][b].first;
else tie(x, y, z) = pos[a][b].second;
a=x;b=y;d=z;
if(d == 1 && a != 0)cout<<a<<sal;
}
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... |