Submission #420500

#TimeUsernameProblemLanguageResultExecution timeMemory
420500BertedCigle (COI21_cigle)C++14
100 / 100
490 ms165256 KiB
#include <iostream>

using namespace std;

int N, A[5001];
int cst[5001][5001], DP[5001][5001], pref[5001], suf[5001];

int main()
{
	cin >> N;
	for (int i = 0; i < N; i++) cin >> A[i];
	for (int i = 0; i < N; i++)
	{
		int sumL = 0, sumR = 0, l = i - 1, r = i;
		while (l >= 0 && r < N)
		{
			if (sumL + A[l] < sumR + A[r]) {sumL += A[l--];}
			else {sumR += A[r++];}

			if (l >= 0 && r < N && sumL == sumR) {cst[i][l] = 1; cst[i][r] = 1;}
		}
		for (int j = i + 1; j < N; j++) cst[i][j] = cst[i][j - 1] + cst[i][j];
		for (int j = i - 2; j >= 0; j--) cst[i][j] = cst[i][j + 1] + cst[i][j];
	}

	for (int i = 0; i < N; i++)
	{
		if (i)
		{
			for (int j = 0; j < i; j++) 
			{
				pref[j] = DP[j][i - 1];
				if (j) pref[j] = max(pref[j - 1], pref[j]);
			}
			for (int j = i - 1; j >= 0; j--)
			{
				suf[j] = DP[j][i - 1] + cst[i][j];
				if (j + 1 < i - 1) suf[j] = max(suf[j + 1], suf[j]);
			}
		}
		
		int k = i - 1;
		for (int j = i; j < N; j++) 
		{
			while (k >= 0 && cst[i][k] <= cst[i][j]) {k--;}
			if (k + 1 < i) DP[i][j] = suf[k + 1];
			if (k >= 0) DP[i][j] = max(DP[i][j], pref[k] + cst[i][j]);
		}
	}

	/*for (int i = 0; i < N; i++)
	{
		for (int j = i; j < N; j++)
		{
			cerr << DP[i][j] << " ";
		}
		cerr << "\n";
	}*/

	int res = 0;
	for (int i = 0; i < N; i++) {res = max(res, DP[i][N - 1]);}

	cout << res << "\n";
	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...