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 tt cin.tie(0), cout.tie(0), ios_base::sync_with_stdio(0)
#define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
#define ll long long
#define ull unsigned long long
#define i128 __int128
#define db long double
#define sz(a) ((int)(a).size())
#define pb emplace_back
#define pf emplace_front
#define pob pop_back
#define pof pop_front
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define ins emplace
using namespace std;
const int MOD = 1e9+7, MAXN = 1e5+5;
const string NAME = "";
int n,k,a[MAXN],trace[205][MAXN];
ll dp_before[MAXN],dp_cur[MAXN],suf[MAXN];
void dnc(int id, int l, int r, int optl, int optr){
if(l>r) return;
int mid=(l+r)>>1;
pair<ll,int> best={-1e18,-1};
for(int i = optl; i<=min(mid,optr); ++i)
best=max(best,{dp_before[i-1]+(suf[i]-suf[mid+1])*suf[mid+1],i});
dp_cur[mid]=best.fi, trace[id][mid]=best.se-1;
dnc(id,l,mid-1,optl,best.se);
dnc(id,mid+1,r,best.se,optr);
}
int main()
{
tt;
if(fopen((NAME + ".INP").c_str(), "r")) fo;
cin >> n >> k;
for(int i = 1; i<=n; ++i)
cin >> a[i];
for(int i = n; i>0; --i)
suf[i]=suf[i+1]+a[i];
for(int i = 1; i<=k; ++i){
dnc(i,i,n-1,i,n-1);
for(int j = i; j<=n; ++j)
dp_before[j]=dp_cur[j];
// for(int j = i; j<=n; ++j){
// for(int x = j; x>=i; --x)
// dp[i][j]=max(dp[i][j],dp[i-1][x-1]+(suf[x]-suf[j+1])*suf[j+1]);
// }
}
pair<ll,int> rs={0,0};
vector<int> v;
for(int i = k; i<n; ++i)
if(rs.fi<=dp_cur[i]) rs.fi=dp_cur[i], rs.se=i;
for(int i = k; i>0; --i)
v.pb(rs.se), rs.se=trace[i][rs.se];
reverse(v.begin(),v.end());
cout << rs.fi << "\n";
for(int i : v) cout << i << " ";
}
//7 3
//4 1 3 4 0 2 3
Compilation message (stderr)
sequence.cpp: In function 'int main()':
sequence.cpp:3:19: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
3 | #define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:36:45: note: in expansion of macro 'fo'
36 | if(fopen((NAME + ".INP").c_str(), "r")) fo;
| ^~
sequence.cpp:3:63: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
3 | #define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:36:45: note: in expansion of macro 'fo'
36 | if(fopen((NAME + ".INP").c_str(), "r")) fo;
| ^~
# | 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... |