163512025-04-28 18:17:10algoproJancsi és Juliska kitalálós játékacpp17Accepted 100/100902ms1120 KiB
// UUID: 7ab6df8b-4955-4969-bf0d-a59c80859422
#include <bits/stdc++.h>
using namespace std;

int main() {
    int n;
	cin >> n;
	vector<vector<int>> dp(n, vector<int>(n, INT_MAX / 2));
	vector<vector<int>> cost(n, vector<int>(n));
	for (int i = 0; i < n; i++)
	{
		cin >> cost[i][i];
	}
	for	(int i = 0; i < n; i++)
	{
		for (int j = i+1; j < n; j++)
		{
			cost[i][j] = max(cost[i][j-1], cost[j][j]);
		}
	}

	for(int h = 0; h < n; h++){
		for(int i = 0; i + h < n; i++){
			int j = i + h;
			if(h == 0){
				dp[i][i] = 0;
				continue;
			}
			if(h == 1){
				dp[i][i + 1] = min(cost[i][i], cost[i + 1][i + 1]);
				continue;
			}
			for(int l = i; l <= j; l++){
				for(int r = l; r <= j; r++){
					dp[i][j] = min(dp[i][j], max(dp[i][max(l - 1, i)], max(dp[l][r], dp[min(r + 1, j)][j])) + cost[l][r]);
				}
			}
		}
	}

	for	(int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			//cout << dp[i][j] << " ";
		}
		//cout << "\n";
	}

	cout << dp[0][n - 1];
}
SubtaskSumTestVerdictTimeMemory
base100/100
1Accepted0/01ms316 KiB
2Accepted0/0902ms1120 KiB
3Accepted4/41ms316 KiB
4Accepted5/51ms316 KiB
5Accepted5/51ms316 KiB
6Accepted5/51ms508 KiB
7Accepted5/513ms480 KiB
8Accepted5/513ms484 KiB
9Accepted5/534ms540 KiB
10Accepted5/546ms564 KiB
11Accepted5/559ms584 KiB
12Accepted5/5119ms564 KiB
13Accepted5/5160ms564 KiB
14Accepted5/5178ms724 KiB
15Accepted5/5428ms908 KiB
16Accepted5/5500ms944 KiB
17Accepted5/5591ms820 KiB
18Accepted5/5671ms820 KiB
19Accepted5/5771ms1076 KiB
20Accepted5/5902ms1076 KiB
21Accepted5/5902ms1076 KiB
22Accepted6/6902ms1076 KiB