제출 #874980

#제출 시각아이디문제언어결과실행 시간메모리
874980NintsiChkhaidzeSplit the sequence (APIO14_sequence)C++17
50 / 100
2094 ms131072 KiB
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define s second
#define f first
#define int ll
using namespace std;
 
const int N = 1e5 + 3;
 
int a[N],p[N],dp[N][203],lst[N][203];

signed main() {
    ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
 
	int n,k;
	cin>>n>>k;
	
	for (int i = 1; i <= n; i++){
		cin >> a[i];
		p[i] = p[i - 1] + a[i];
	}
	
	for (int r = 0; r <= n + 1; r++)
		for (int kk = 1; kk <= k; kk++)
			dp[r][kk] = -1;
	
	for (int i = 2; i <= n; i++){
		for (int j = 1; j <= min(k,i - 1); j++){
			if (j == 1){
				int s1 = p[n] - p[i - 1],s2 = p[i - 1];
				int cur = s1*s2;

				if (cur > dp[i][j]){
					dp[i][j] = cur;
					lst[i][j] = i;
				}

				continue;
			}

			for (int l = 2; l < i; l++){
				if (dp[l][j - 1] == -1) continue;

				int s1 = p[n] - p[i - 1],s2 = p[i - 1] - p[l - 1];
				int reward = s1*s2;
				int cur = dp[l][j - 1] + reward;
		
				if (cur > dp[i][j]){
					dp[i][j] = cur;
					lst[i][j] = l;
				}
			}
		}
	}

	int id=n;
	for (int i = 1; i <= n; i++){
		if (dp[i][k] > dp[id][k]) id = i;
	}
	cout<<dp[id][k]<<endl;
	
	vector <int> ans;
	for (int i = k; i >= 1; i--){
		ans.pb(id);
		id = lst[id][i];
	}
	
	for (int i = k - 1; i >= 0; i--)
		cout<<ans[i] - 1<<" ";
} 
#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...