Submission #967627

#TimeUsernameProblemLanguageResultExecution timeMemory
967627Halym2007수열 (APIO14_sequence)C++17
50 / 100
2051 ms75208 KiB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define sz size()
const int N = 1e5 + 5;
ll a[N], dp[N][205], par[N][205], p[N];
int n, k;
 
int main () {
//	freopen ("sequence.in", "r", stdin);
//	freopen ("sequence.out", "w", stdout);
//	freopen ("input.txt", "r", stdin);
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin >> n >> k;
	for (int i = 1; i <= n; ++i) {
		cin >> a[i];
	}
//	reverse (a + 1,a + n + 1);
	for (int i = 1; i <= n; ++i) {
		p[i] = p[i - 1] + a[i];
	}
	// dp[i][j] i-cenli j grupba bolemde maximum value.
	for (int i = 2; i <= n; ++i) {
		for (int x = 2; x <= min (i, k + 1); ++x) {
			for (int j = x - 1; j < i; ++j) {
				ll kl = p[i] - p[j], lk = p[j];
				ll val = dp[j][x - 1] + kl * lk;
				if (dp[i][x] <= val) {
					par[i][x] = j;
					dp[i][x] = val;
				} 
			}
		}
	}
	vector <int> cyk;
	int y = k + 1, x = n;
	while (1) {
		x = par[x][y];
		if (!x) break;
		cyk.pb (x);
		y--;
	}
	cout << dp[n][k + 1] << "\n";
	assert ((int)cyk.sz == k);
	for (int i : cyk) {
		cout << i << " ";
//		cout << n - i << " ";
	}
	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...