100092024-03-23 23:21:02111Jancsi és Juliska kitalálós játékacpp17Accepted 100/100994ms6764 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long

int N,F[300],C[300][300],A[300][300];

int solve(int l,int r){
	if(l>=r){
		return 0;
	}
	if(A[l][r]!=-1){
		return A[l][r];
	}
	int a=INT_MAX;
	for(int i=l;i<=r;i++){
		for(int j=i;j<=r;j++){
			if(i==l&&j==r){
				continue;
			}
			int xxx=solve(j+1,r);
			int xx=max({solve(l,i-1),solve(i,j)});
			int x=C[i][j]+max({xx,xxx});
			if(x<a){
				a=x;
			}
			if(xxx<=xx)break;
		}
	}
	A[l][r]=a;
	return a;
}

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cin>>N;
	for(int i=0;i<N;i++){
		cin>>F[i];
	}
	for(int i=0;i<N;i++){
		C[i][i]=F[i];
		for(int j=i+1;j<N;j++){
			C[i][j]=max(C[i][j-1],F[j]);
		}
	}
	for(int i=0;i<N;i++){
		for(int j=i;j<N;j++){
			A[i][j]=-1;
		}
	}
	cout<<solve(0,N-1)<<'\n';
	return 0;
}
SubtaskSumTestVerdictTimeMemory
base100/100
1Accepted0/03ms1936 KiB
2Accepted0/0994ms5044 KiB
3Accepted4/43ms2552 KiB
4Accepted5/53ms2772 KiB
5Accepted5/53ms2912 KiB
6Accepted5/53ms2956 KiB
7Accepted5/510ms3756 KiB
8Accepted5/59ms3944 KiB
9Accepted5/532ms4216 KiB
10Accepted5/537ms4268 KiB
11Accepted5/530ms4648 KiB
12Accepted5/567ms5016 KiB
13Accepted5/5119ms5368 KiB
14Accepted5/5150ms5652 KiB
15Accepted5/5330ms5872 KiB
16Accepted5/5547ms6132 KiB
17Accepted5/5252ms6228 KiB
18Accepted5/5275ms6280 KiB
19Accepted5/5754ms6456 KiB
20Accepted5/5330ms6752 KiB
21Accepted5/5839ms6704 KiB
22Accepted6/6660ms6764 KiB