Submission #400602

#TimeUsernameProblemLanguageResultExecution timeMemory
400602AzimjonSplit the sequence (APIO14_sequence)C++17
50 / 100
2084 ms5724 KiB
// Muallif: Azimjon Mehmonali o'g'li

#include <bits/stdc++.h>

using namespace std;

#define int long long

const long double PI = 3.1415926535897;
const int mod = 1000000007LL;
const int INF = 1e18;
const int N = 111111;
const int K = 211;

int n, k;
int a[N], p[N], dp[K][N], pa[K][N];

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	cin >> n >> k;

	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		p[i] = p[i - 1] + a[i];
	}

	for (int i = 1; i <= k; i++) {
		for (int j = 1; j <= n; j++) {
			for (int l = 1; l < j; l++) {
				if (dp[i][j] <= dp[i - 1][l] + (p[j] - p[l]) * p[l]) {
					dp[i][j] = dp[i - 1][l] + (p[j] - p[l]) * p[l];
					pa[i][j] = l;
				}
			}
		}
	}

	cout << dp[k][n] << endl;
	int x = pa[k][n];
	while (k--) {
		cout << x << " ";
		x = pa[k][x];
	}

	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...