Submission #265467

#TimeUsernameProblemLanguageResultExecution timeMemory
265467square1001Split the sequence (APIO14_sequence)C++14
50 / 100
2067 ms24192 KiB
// APIO 2014 Problem 2 - Split the Sequence

#include <vector>
#include <iostream>
#include <algorithm>
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
const long long inf = 1LL << 60;
int main() {
	// step #1. read input
	cin.tie(0);
	ios_base::sync_with_stdio(false);
	int N, K;
	cin >> N >> K; ++K;
	vector<int> A(N);
	for(int i = 0; i < N; ++i) {
		cin >> A[i];
	}
	// step #2. preparation for calculating the answer
	vector<int> S(N + 1);
	for(int i = 0; i < N; ++i) {
		S[i + 1] = S[i] + A[i];
	}
	// step #3. calculate the answer with dynamic programming
	vector<vector<long long> > dp(K + 1, vector<long long>(N + 1, inf));
	vector<vector<int> > pre(K + 1, vector<int>(N + 1, -1));
	dp[0][0] = 0;
	for(int i = 1; i <= K; ++i) {
		for(int j = 1; j <= N; ++j) {
			for(int k = 0; k < j; ++k) {
				if(dp[i][j] > dp[i - 1][k] - 1LL * S[j] * S[k]) {
					dp[i][j] = dp[i - 1][k] - 1LL * S[j] * S[k];
					pre[i][j] = k;
				}
			}
			dp[i][j] += 1LL * S[j] * S[j];
		}
	}
	// step #4. print the answer
	cout << 1LL * S[N] * S[N] - dp[K][N] << endl;
	int pos = N;
	for(int i = K; i >= 2; --i) {
		cout << pre[i][pos] << (i != 2 ? ' ' : '\n');
		pos = pre[i][pos];
	}
	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...