Submission #989658

#TimeUsernameProblemLanguageResultExecution timeMemory
989658anHiepSplit the sequence (APIO14_sequence)C++17
100 / 100
568 ms81868 KiB
//     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     //
//         Nhan qua khong no chung ta thu gi          //
//                 Vay nen dung oan han               //
//     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     //
//                                                    //    
//                       _oo0oo_                      //
//                      o8888888o                     //
//                      88" . "88                     //					  
//                      (| -_- |)                     //
//                      0\  =  /0                     //
//                    ___/`---'\___                   //
//                  .' \\|     |// '.                 //
//                 / \\|||  :  |||// \                //  			  
//                / _||||| -:- |||||- \               // 			  
//               |   | \\\  -  /// |   |              //			  
//               | \_|  ''\---/''  |_/ |              //			  
//               \  .-\__  '-'  ___/-. /              //			  
//             ___'. .'  /--.--\  `. .'___            //			  
//          ."" '<  `.___\_<|>_/___.' >' "".          //  		  
//         | | :  `- \`.;`\ _ /`;.`/ - ` : | |        //		  
//         \  \ `_.   \_ __\ /__ _/   .-` /  /        //    	  
//     =====`-.____`.___ \_____/___.-`___.-'=====     //     
//                       `=---='                      //
//                                                    //
//     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     //
//   Duc Phat noi day phu ho Code con chay khong Bug  //
//                 Nam mo a di da phat                //
//     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     //

#include<bits/stdc++.h>
#ifdef LOCAL
#include "D:\debug.h"
#else
#define cebug(...) "Orz_chUoNgnn"
#endif

using namespace std;

#define fi first 
#define se second
#define pb push_back
#define ll long long
#define ii pair<int, int>
#define vi vector<int> 
#define vll vector<ll>
#define vii vector<ii>
#define cd complex<double>
const ll mod = 1e9 + 7;
const ll INF = 1e18L + 5;
const double PI = acos(-1);
const int block = 320;
const int N = 1e5;
int tc, tt = 1;

int n, k;
ll a[N + 1], ps[N + 1];
ll dp[2][N + 1];
int trace[203][N + 1];
void dnc(int lvl, int l, int r, int optl, int optr) {
	if(l > r) return;
	pair<ll, int> best = {-INF, 0};
	int mid = (l + r)/2;
	for(int i=min(optr, mid); i>=max(2, optl); i--)
		if(best.fi <= dp[0][i - 1]  + (ps[mid] - ps[i - 1]) * (ps[n] - ps[mid])) {
			best.fi = dp[0][i - 1] + (ps[mid] - ps[i - 1]) * (ps[n] - ps[mid]);
			best.se = i;
		}
	trace[lvl][mid] = best.se;
	dp[1][mid] = best.fi;
	dnc(lvl, l, mid - 1, optl, best.se);
	dnc(lvl, mid + 1, r, best.se, optr);
}
void solve() {
	cin>>n>>k;
	for(int i=1; i<=n; i++) cin>>a[i];
	for(int i=1; i<=n; i++) ps[i] = ps[i - 1] + a[i];
	for(int i=1; i<=n; i++)
		dp[0][i] = ps[i] * (ps[n] - ps[i]);
	for(int i=2; i<=k+1; i++) {
		dnc(i, 1, n, 1, n);
		for(int j=0; j<=n; j++) {
			dp[0][j] = dp[1][j];
			dp[1][j] = 0;
		}
	}
	cout<<dp[0][n]<<'\n';
	int cur = n;
	vi f;
	for(int i=k; i>=1; i--) {
		cur = trace[i + 1][cur] - 1;
		f.pb(cur);
	}
	reverse(f.begin(), f.end());
	for(auto x: f)
		cout<<x<<' ';
}
signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    // freopen(".INP", "r", stdin);
    // freopen(".OUT", "w", stdout);
    for(int tc=1; tc<=tt; tc++) solve();
    cerr<<"\nTime elapsed: "<<1000.0*clock()/CLOCKS_PER_SEC<<" ms.\n";
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...