100062024-03-23 23:15:30111Jancsi és Juliska kitalálós játékacpp17Time limit exceeded 100/1001.001s6696 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 xx=max({solve(l,i-1),solve(i,j),solve(j+1,r)});
			int x=C[i][j]+xx;
			if(x<a){
				a=x;
			}
			if(solve(j+1,r)<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/03ms1924 KiB
2Time limit exceeded0/01.001s5064 KiB
3Accepted4/43ms2580 KiB
4Accepted5/53ms2608 KiB
5Accepted5/53ms3016 KiB
6Accepted5/53ms3140 KiB
7Accepted5/512ms3900 KiB
8Accepted5/510ms4032 KiB
9Accepted5/534ms4536 KiB
10Accepted5/539ms4560 KiB
11Accepted5/532ms4548 KiB
12Accepted5/571ms4956 KiB
13Accepted5/5125ms5008 KiB
14Accepted5/5157ms5340 KiB
15Accepted5/5342ms5796 KiB
16Accepted5/5563ms5996 KiB
17Accepted5/5259ms6380 KiB
18Accepted5/5287ms6388 KiB
19Accepted5/5772ms6572 KiB
20Accepted5/5354ms6696 KiB
21Accepted5/5882ms6604 KiB
22Accepted6/6689ms6608 KiB