Submission #3415581


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 1000000000
#define LLINF 20000000000000000ll
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define all(x) (x).begin(),(x).end()
#define sq(x) ((x)*(x))
#define rep(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define repn(i,a,n) for(int (i)=(a);(i)<(int)(n);(i)++)
#define EQ(a,b) (abs((a)-(b))<eps)
string S;
int dp[55][55][55][2];
const int F = -500;
const int INVALID = -INF;
int rec(int l,int r,int k,int s){ // s: 0->max , 1->min
	if(dp[l][r][k][s]!=F)return dp[l][r][k][s];
	if((r-l+1)%2==0)return dp[l][r][k][s]=INVALID;
	if((r-l+1)==1){
		if(k>0){
			if(s==0)return dp[l][r][k][s]=9;
			else return dp[l][r][k][s]=0;
		}else{
			if(S[l]!='+'&&S[l]!='-')return dp[l][r][k][s]=(int)(S[l]-'0');
			else return dp[l][r][k][s]=INVALID;
		}
	}else{
		int res = (s==0)?-INF:INF;
		for(int i=l+1;i<r;i++){
			for(int j=0;j<k;j++){
				int a = rec(l,i-1,j,0);
				int b = rec(i,r-1,k-1-j,0);
				int c = rec(l,i-1,j,1);
				int d = rec(i,r-1,k-1-j,1);
				if(s==0){
					if(a!=INVALID&&b!=INVALID)res = max(res,a+b);
					if(a!=INVALID&&d!=INVALID)res = max(res,a-d);
				}else{
					if(c!=INVALID&&d!=INVALID)res = min(res,c+d);
					if(c!=INVALID&&b!=INVALID)res = min(res,c-b);
				}
			}
		}
		if(S[r]=='+'||S[r]=='-'){
			for(int i=l+1;i<r;i++){
				for(int j=0;j<=k;j++){
					int a = rec(l,i-1,j,0);
					int b = rec(i,r-1,k-j,0);
					int c = rec(l,i-1,j,1);
					int d = rec(i,r-1,k-j,1);
					if(s==0){
						if(S[r]=='+'){
							if(a!=INVALID&&b!=INVALID)res = max(res,a+b);
						}else{
							if(a!=INVALID&&d!=INVALID)res = max(res,a-d);
						}
					}else{
						if(S[r]=='+'){
							if(c!=INVALID&&d!=INVALID)res = min(res,c+d);
						}else{
							if(c!=INVALID&&b!=INVALID)res = min(res,c-b);
						}
					}
				}
			}
		}
		if(abs(res)==INF)return dp[l][r][k][s]=INVALID;
		else return dp[l][r][k][s]=res;
	}
}
int main(){
	int K;
	cin >> K;
	cin >> S;
	for(int i=0;i<53;i++){
		for(int j=0;j<53;j++){
			for(int k=0;k<53;k++){
				for(int l=0;l<2;l++){
					dp[i][j][k][l]=F;
				}
			}
		}
	}
	int ans = rec(0,S.size()-1,K,0);
	if(ans==INVALID)printf("NG\n");
	else{
		printf("OK\n");
		printf("%d\n",ans);
	}
	return 0;
}

Submission Info

Submission Time
Task A - 計算ドリル
User okura
Language C++14 (GCC 5.4.1)
Score 500
Code Size 2468 Byte
Status AC
Exec Time 393 ms
Memory 1536 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 5
AC × 53
Set Name Test Cases
Sample example0, example1, example2, example3, example4
All example0, example1, example2, example3, example4, handmade0, handmade1, handmade2, maxrand0, maxrand1, maxrand10, maxrand11, maxrand12, maxrand13, maxrand14, maxrand15, maxrand16, maxrand17, maxrand18, maxrand19, maxrand2, maxrand3, maxrand4, maxrand5, maxrand6, maxrand7, maxrand8, maxrand9, rand0, rand1, rand10, rand11, rand12, rand13, rand14, rand15, rand16, rand17, rand18, rand19, rand2, rand3, rand4, rand5, rand6, rand7, rand8, rand9, smallrand0, smallrand1, smallrand2, smallrand3, smallrand4
Case Name Status Exec Time Memory
example0 AC 2 ms 1536 KB
example1 AC 2 ms 1536 KB
example2 AC 2 ms 1536 KB
example3 AC 2 ms 1536 KB
example4 AC 2 ms 1536 KB
handmade0 AC 2 ms 1536 KB
handmade1 AC 2 ms 1536 KB
handmade2 AC 2 ms 1536 KB
maxrand0 AC 2 ms 1536 KB
maxrand1 AC 2 ms 1536 KB
maxrand10 AC 149 ms 1536 KB
maxrand11 AC 2 ms 1536 KB
maxrand12 AC 2 ms 1536 KB
maxrand13 AC 2 ms 1536 KB
maxrand14 AC 393 ms 1536 KB
maxrand15 AC 169 ms 1536 KB
maxrand16 AC 2 ms 1536 KB
maxrand17 AC 174 ms 1536 KB
maxrand18 AC 2 ms 1536 KB
maxrand19 AC 285 ms 1536 KB
maxrand2 AC 2 ms 1536 KB
maxrand3 AC 300 ms 1536 KB
maxrand4 AC 79 ms 1536 KB
maxrand5 AC 2 ms 1536 KB
maxrand6 AC 2 ms 1536 KB
maxrand7 AC 2 ms 1536 KB
maxrand8 AC 2 ms 1536 KB
maxrand9 AC 344 ms 1536 KB
rand0 AC 2 ms 1536 KB
rand1 AC 3 ms 1536 KB
rand10 AC 6 ms 1536 KB
rand11 AC 2 ms 1536 KB
rand12 AC 2 ms 1536 KB
rand13 AC 2 ms 1536 KB
rand14 AC 2 ms 1536 KB
rand15 AC 2 ms 1536 KB
rand16 AC 2 ms 1536 KB
rand17 AC 2 ms 1536 KB
rand18 AC 2 ms 1536 KB
rand19 AC 2 ms 1536 KB
rand2 AC 2 ms 1536 KB
rand3 AC 2 ms 1536 KB
rand4 AC 154 ms 1536 KB
rand5 AC 2 ms 1536 KB
rand6 AC 2 ms 1536 KB
rand7 AC 2 ms 1536 KB
rand8 AC 2 ms 1536 KB
rand9 AC 2 ms 1536 KB
smallrand0 AC 2 ms 1536 KB
smallrand1 AC 2 ms 1536 KB
smallrand2 AC 2 ms 1536 KB
smallrand3 AC 2 ms 1536 KB
smallrand4 AC 2 ms 1536 KB