結果

問題 No.1334 Multiply or Add
ユーザー leaf_1415leaf_1415
提出日時 2021-01-08 23:04:47
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 85 ms / 2,000 ms
コード長 1,917 bytes
コンパイル時間 904 ms
コンパイル使用メモリ 90,476 KB
実行使用メモリ 6,656 KB
最終ジャッジ日時 2024-04-28 04:51:39
合計ジャッジ時間 4,910 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 32 ms
6,400 KB
testcase_04 AC 32 ms
6,528 KB
testcase_05 AC 31 ms
6,400 KB
testcase_06 AC 30 ms
6,272 KB
testcase_07 AC 32 ms
6,400 KB
testcase_08 AC 33 ms
6,528 KB
testcase_09 AC 32 ms
6,400 KB
testcase_10 AC 32 ms
6,400 KB
testcase_11 AC 33 ms
6,272 KB
testcase_12 AC 32 ms
6,272 KB
testcase_13 AC 31 ms
6,528 KB
testcase_14 AC 31 ms
6,656 KB
testcase_15 AC 32 ms
6,528 KB
testcase_16 AC 31 ms
6,272 KB
testcase_17 AC 31 ms
6,272 KB
testcase_18 AC 32 ms
6,528 KB
testcase_19 AC 33 ms
6,272 KB
testcase_20 AC 32 ms
6,656 KB
testcase_21 AC 32 ms
6,272 KB
testcase_22 AC 33 ms
6,400 KB
testcase_23 AC 35 ms
6,400 KB
testcase_24 AC 31 ms
6,400 KB
testcase_25 AC 33 ms
6,528 KB
testcase_26 AC 32 ms
6,400 KB
testcase_27 AC 30 ms
6,400 KB
testcase_28 AC 32 ms
6,272 KB
testcase_29 AC 37 ms
6,400 KB
testcase_30 AC 30 ms
6,400 KB
testcase_31 AC 31 ms
6,400 KB
testcase_32 AC 33 ms
6,272 KB
testcase_33 AC 34 ms
6,656 KB
testcase_34 AC 32 ms
6,528 KB
testcase_35 AC 32 ms
6,528 KB
testcase_36 AC 31 ms
6,656 KB
testcase_37 AC 31 ms
6,656 KB
testcase_38 AC 31 ms
6,656 KB
testcase_39 AC 1 ms
5,376 KB
testcase_40 AC 16 ms
5,376 KB
testcase_41 AC 6 ms
5,376 KB
testcase_42 AC 6 ms
5,376 KB
testcase_43 AC 24 ms
5,888 KB
testcase_44 AC 14 ms
5,376 KB
testcase_45 AC 6 ms
5,376 KB
testcase_46 AC 15 ms
5,376 KB
testcase_47 AC 22 ms
5,632 KB
testcase_48 AC 16 ms
5,376 KB
testcase_49 AC 30 ms
6,400 KB
testcase_50 AC 76 ms
6,656 KB
testcase_51 AC 78 ms
6,656 KB
testcase_52 AC 79 ms
6,656 KB
testcase_53 AC 76 ms
6,528 KB
testcase_54 AC 78 ms
6,656 KB
testcase_55 AC 85 ms
6,528 KB
testcase_56 AC 1 ms
5,376 KB
testcase_57 AC 2 ms
5,376 KB
testcase_58 AC 1 ms
5,376 KB
testcase_59 AC 1 ms
5,376 KB
testcase_60 AC 2 ms
5,376 KB
testcase_61 AC 1 ms
5,376 KB
testcase_62 AC 1 ms
5,376 KB
testcase_63 AC 1 ms
5,376 KB
testcase_64 AC 1 ms
5,376 KB
testcase_65 AC 2 ms
5,376 KB
testcase_66 AC 68 ms
6,016 KB
testcase_67 AC 45 ms
5,376 KB
testcase_68 AC 3 ms
5,376 KB
testcase_69 AC 72 ms
6,272 KB
testcase_70 AC 69 ms
6,272 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cassert>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <string>
#include <algorithm>
#include <utility>
#include <complex>
#define rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define all(x) (x).begin(),(x).end()
#define inf 1e18
#define mod 1000000007

using namespace std;

typedef long long llint;
typedef long long ll;
typedef pair<llint, llint> P;

ll n;
ll a[200005];
ll dp[105];
deque<ll> deq;
ll M = 1e18;

int main(void)
{
	cin >> n;
	rep(i, 1, n){
		cin >> a[i];
		deq.push_back(a[i]);
	}
	
	ll add = 0;
	while(deq.size() && deq.front() == 1) deq.pop_front(), add++;
	while(deq.size() && deq.back() == 1) deq.pop_back(), add++;
	
	ll mul = 1; bool flag = false;
	for(auto x : deq){
		if(x >= M / mul){
			flag = true;
			break;
		}
		mul *= x;
	}
	if(flag){
		ll ans = 1;
		for(auto x : deq) ans *= x, ans %= mod;
		ans += add, ans %= mod;
		cout << ans << endl;
		return 0;
	}
	
	if(deq.size() == 0){
		cout << add << endl;
		return 0;
	}
	
	//for(auto x : deq) cout<< x << " "; cout << endl;
	
	vector<ll> vec, vec2;
	mul = 1; ll cnt = 0;
	for(auto x : deq){
		if(x == 1){
			if(cnt == 0){
				vec.push_back(mul);
				cnt = 1;
			}
			else cnt++;
		}
		else{
			if(cnt > 0){
				vec2.push_back(cnt);
				mul = x;
				cnt = 0;
			}
			else mul *= x;
		}
	}
	vec.push_back(mul);
	vec2.push_back(cnt);
	
	ll m = vec.size();
	
	rep(i, 0, m) dp[i] = -inf;
	dp[0] = 0;
	
	rep(i, 0, m-1){
		rep(j, i+1, m){
			ll mul = 1;
			rep(k, i+1, j) mul *= vec[k-1];
			chmax(dp[j], dp[i] + mul + vec2[j-1]);
		}
	}
	//rep(i, 0, m) cout << dp[i] << " "; cout<< endl;
	
	ll ans = (dp[m] + add) % mod;
	cout << ans << endl;
	
	return 0;
}
0