Submission #1202550

#TimeUsernameProblemLanguageResultExecution timeMemory
1202550adiyerSplit the sequence (APIO14_sequence)C++20
0 / 100
0 ms328 KiB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair < pair < int, ll >, int > line;

const int N = 1e5 + 2;

int n, k;
int a[N], sz[201];
int dp[N][201];

pair < ll, int > ans, pro;

ll p[N], s[N];

struct convex_hull{

	int l = 1, r;

	vector < line > c;

	bool bad(line nw){
		line x = c[c.size() - 2], y = c[c.size() - 1];
		return (x.first.second - nw.first.second) * 1ll * (y.first.first - x.first.first) < (x.first.second - y.first.second) * 1ll * (nw.first.first - x.first.first); 
	}

	pair < ll, int > eval(int x){
		if(l >= c.size()) return {0ll, 0};
		while(l + 1 < c.size() && c[l].first.first * 1ll * x + c[l].first.second < c[l + 1].first.first * 1ll * x + c[l + 1].first.second) l++;
		return {c[l].first.first * 1ll * x + c[l].first.second, c[l].second};
	}

	void insert_line(line nw){
		if(c.empty()) c.push_back({{0, 0ll}, 0});
		while(l + 1 < c.size() && bad(nw)) c.pop_back();
		c.push_back(nw);
	}

} hull[201];


void solve(){
	cin >> n >> k;
	for(int i = 0; i <= 200; i++) sz[i] = 1;
	for(int i = 1; i <= n; i++) cin >> a[i];
	for(int i = 1; i <= n; i++) p[i] = p[i - 1] + a[i];
	for(int i = n; i >= 1; i--) s[i] = s[i + 1] + a[i];
	hull[0].insert_line({{0, 0}, 0});
	for(int i = 1; i < n; i++){
		for(int lay = min(i, k); lay >= 1; lay--){
			pro = hull[lay - 1].eval(-(s[i + 1]));
			pro.first += p[i] * s[i + 1], dp[i][lay] = pro.second;
			if(pro.first >= ans.first) ans = {pro.first, i};
			hull[lay].insert_line({{p[i], pro.first}, i});
		}
	}
	int pos = ans.second;
	cout << ans.first << '\n';
	while(k > 0){
		cout << pos << ' ';
		pos = dp[pos][k];
		k--;
	}
}

signed main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int tt = 1;
	// cin >> tt;
	while(tt--) solve();
}
#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...