163492025-04-28 18:16:51GervidJancsi és Juliska kitalálós játékacpp17Accepted 100/100903ms1268 KiB
#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/0902ms1268 KiB
3Accepted4/41ms500 KiB
4Accepted5/51ms316 KiB
5Accepted5/51ms316 KiB
6Accepted5/51ms316 KiB
7Accepted5/513ms484 KiB
8Accepted5/513ms492 KiB
9Accepted5/534ms540 KiB
10Accepted5/545ms560 KiB
11Accepted5/557ms564 KiB
12Accepted5/5119ms756 KiB
13Accepted5/5160ms692 KiB
14Accepted5/5178ms732 KiB
15Accepted5/5425ms820 KiB
16Accepted5/5500ms948 KiB
17Accepted5/5588ms988 KiB
18Accepted5/5671ms824 KiB
19Accepted5/5771ms1268 KiB
20Accepted5/5902ms1260 KiB
21Accepted5/5902ms1076 KiB
22Accepted6/6903ms1076 KiB