# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1115579 | hainam2k9 | Split the sequence (APIO14_sequence) | C++17 | 18 ms | 8272 KiB |
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[205][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[id-1][i-1]+(suf[i]-suf[mid+1])*suf[mid+1],i});
dp[id][mid]=best.fi, trace[id][mid]=best.se;
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,i,n);
// 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[k][i]) rs.fi=dp[k][i], rs.se=trace[k][i-1];
for(int i = k; i>0; --i)
v.pb(rs.se), rs.se=trace[i-1][rs.se-1];
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)
# | 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... |