結果

問題 No.505 カードの数式2
ユーザー srup٩(๑`н´๑)۶srup٩(๑`н´๑)۶
提出日時 2017-04-21 23:11:38
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,192 bytes
コンパイル時間 1,043 ms
コンパイル使用メモリ 144,176 KB
実行使用メモリ 4,500 KB
最終ジャッジ日時 2023-09-27 20:42:48
合計ジャッジ時間 2,177 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 1 ms
4,380 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 2 ms
4,380 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 8 ms
4,380 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vint;
typedef pair<int,int> pint;
typedef vector<pint> vpint;
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,n) for(int i=n-1;i>=(0);i--)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#define all(v) (v).begin(),(v).end()
#define eall(v) unique(all(v), v.end())
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define chmax(a, b) a = (((a)<(b)) ? (b) : (a))
#define chmin(a, b) a = (((a)>(b)) ? (b) : (a))
const int MOD = 1e9 + 7;
const int INF = 1e9;
const ll INFF = 1e18;

int N;
int a[20];

ll dfs(int k, ll now){
	if(k == N) return now;
	/*
	if(now == 0){
		if(a[i] >= 0) now += a[i];
		else now -= a[i];
	}else if(now > a[i]){

	}
	if(a[k] < 0){

	}else if(a[k] == 0){
		return dfs(k + 1, now);
	}else if(a[k] == 1){

	}else if(a[k]){

	}
	*/
	ll ret = -INFF;
	chmax(ret, dfs(k + 1, now + a[k]));
	chmax(ret, dfs(k + 1, now * a[k]));
	chmax(ret, dfs(k + 1, now - a[k]));
	if(a[k] != 0) dfs(k + 1, now / a[k]);
	return ret;
}

ll dp[20][2];

int main(void){
	cin >> N;
	rep(i, N) cin >> a[i];
	
	if(N <= 10){
		ll ret = dfs(0, 0);
		printf("%lld\n", ret);
		return 0;
	}

	rep(i, 20) dp[i][0] = -INFF;
	rep(i, 20) dp[i][1] = INFF;
	if(a[0] >= 0) dp[1][0] = a[0];
	else dp[1][1] = a[0];
	reps(i, 1, N){
		rep(j, 2){
			if(j == 0 && dp[i][j] == -INFF) continue;
			else if(j == 1 && dp[i][j] == INFF) continue;

			if(dp[i][j] * a[i] >= 0) chmax(dp[i + 1][0], dp[i][j] * a[i]);
			else chmin(dp[i + 1][1], dp[i][j] * a[i]);
			if(dp[i][j] + a[i] >= 0) chmax(dp[i + 1][0], dp[i][j] + a[i]);
			else chmin(dp[i + 1][1], dp[i][j] + a[i]);
			if(dp[i][j] - a[i] >= 0) chmax(dp[i + 1][0], dp[i][j] - a[i]);
			else chmin(dp[i + 1][1], dp[i][j] - a[i]);
			if(a[i] != 0){
			if(dp[i][j] / a[i] >= 0) chmax(dp[i + 1][0], dp[i][j] / a[i]);
			else chmin(dp[i + 1][1], dp[i][j] / a[i]);
			}
		}
		// printf("%lld %lld\n", dp[i + 1][0], dp[i + 1][1]);
	}
	ll ans = -INFF;
	if(dp[N][0] != -INFF) chmax(ans, dp[N][0]);
	if(dp[N][1] != INFF) chmax(ans, dp[N][0]);
	printf("%lld\n", ans);
	return 0;
}
0