Submission #413951

#TimeUsernameProblemLanguageResultExecution timeMemory
413951LastRoninSplit the sequence (APIO14_sequence)C++14
0 / 100
20 ms31848 KiB
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define speed ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define ll long long
#define pb push_back
#define mp make_pair
#define f first
#define s second
using namespace std;

const ll N = 1e4 + 10;

ll n, k;
ll a[N];
ll dp[N][201], pr[N][201];
ll j = 1;

void dac(ll l, ll r, ll L, ll R) {
	if(l > r)return;
	ll m = (l + r) >> 1ll;
	for(int i = L; i <= min(R, m - 1); i++)
		if(dp[m][j] < dp[i - 1][j] + (a[m] - a[i - 1]) * a[i - 1])
			dp[m][j] = dp[i - 1][j] + (a[m] - a[i - 1]) * a[i - 1],
			pr[m][j] = i;
	dac(l, m - 1, L, pr[m][j]);
	dac(m + 1, r, pr[m][j], R);
}

int main() {
	speed;
	cin >> n >> k;
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
		a[i] += a[i - 1];
	}         
	dp[0][0] = 0;
	for(int p = 1; p <= k; p++) {
		j = p;
		dac(1, n, 1, n);
	}
	/*for(int j = 1; j <= n; j++) {
	    for(int p = k; p >= 1; p--) {
			for(int i = j; i > p; i--) {
				if(dp[j][p] < dp[i - 1][p - 1] + (a[j] - a[i - 1]) * a[i - 1])
					dp[j][p] = max(dp[j][p], dp[i - 1][p - 1] + (a[j] - a[i - 1]) * a[i - 1]),
					pr[j][p] = i;
			}
		}
 	}*/
 	cout << dp[n][k] << '\n';
 	vector<ll> vost;
	ll x = n, y = k;
	while(y) {
		x = pr[x][y];
		y--;
		vost.pb(x);
		x--;
 	}
 	reverse(vost.begin(), vost.end());
 	for(auto u : vost)
 		cout << u - 1 << " ";
}
/*
7 3
4 1 3 4 0 2 3
*/
#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...