Submission #1202528

#TimeUsernameProblemLanguageResultExecution timeMemory
1202528adiyer수열 (APIO14_sequence)C++20
71 / 100
75 ms165960 KiB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 1e5 + 2;
const ll is_query = -(1ll << 62);

ll n, k;
ll a[N], p[N], s[N], sz[201];

pair < ll, ll > ans, pro;

ll dp[N][201];


struct convex_hull{

	ll l = 1, r;

	struct line {
	
		ll m, b, i;

		ll get(ll x){
			return m * x + b;
		}

	};

	vector < line > c;

	bool bad(line nw){
		line x = c[c.size() - 2], y = c[c.size() - 1];
		if((x.b - nw.b) * (y.m - x.m) == (x.b - y.b) * (nw.m - x.m)) return nw.b > y.b;
		return (x.b - nw.b) * (y.m - x.m) < (x.b - y.b) * (nw.m - x.m); 
	}

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

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

} hull[201];


void solve(){
	cin >> n >> k;
	for(ll i = 0; i <= 200; i++) sz[i] = 1;
	for(ll i = 1; i <= n; i++) cin >> a[i];
	for(ll i = 1; i <= n; i++) p[i] = p[i - 1] + a[i];
	for(ll i = n; i >= 1; i--) s[i] = s[i + 1] + a[i];
	hull[0].insert_line({0, 0, 0});
	for(ll i = 1; i < n; i++){
		for(ll 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});
		}
	}
	ll 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...