結果

問題 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
コンパイル時間 2,534 ms
コンパイル使用メモリ 88,564 KB
実行使用メモリ 6,672 KB
最終ジャッジ日時 2023-08-10 11:32:47
合計ジャッジ時間 8,155 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 34 ms
6,436 KB
testcase_04 AC 34 ms
6,200 KB
testcase_05 AC 34 ms
6,200 KB
testcase_06 AC 32 ms
6,324 KB
testcase_07 AC 34 ms
6,264 KB
testcase_08 AC 33 ms
6,116 KB
testcase_09 AC 33 ms
6,384 KB
testcase_10 AC 33 ms
6,352 KB
testcase_11 AC 33 ms
6,196 KB
testcase_12 AC 33 ms
6,172 KB
testcase_13 AC 34 ms
6,580 KB
testcase_14 AC 34 ms
6,516 KB
testcase_15 AC 34 ms
6,520 KB
testcase_16 AC 34 ms
6,484 KB
testcase_17 AC 34 ms
6,204 KB
testcase_18 AC 34 ms
6,196 KB
testcase_19 AC 35 ms
6,144 KB
testcase_20 AC 34 ms
6,540 KB
testcase_21 AC 34 ms
6,448 KB
testcase_22 AC 32 ms
6,172 KB
testcase_23 AC 33 ms
6,264 KB
testcase_24 AC 32 ms
6,192 KB
testcase_25 AC 33 ms
6,648 KB
testcase_26 AC 33 ms
6,364 KB
testcase_27 AC 32 ms
6,356 KB
testcase_28 AC 33 ms
6,356 KB
testcase_29 AC 34 ms
6,496 KB
testcase_30 AC 33 ms
6,356 KB
testcase_31 AC 34 ms
6,272 KB
testcase_32 AC 33 ms
6,376 KB
testcase_33 AC 34 ms
6,568 KB
testcase_34 AC 34 ms
6,568 KB
testcase_35 AC 33 ms
6,552 KB
testcase_36 AC 34 ms
6,552 KB
testcase_37 AC 34 ms
6,520 KB
testcase_38 AC 34 ms
6,612 KB
testcase_39 AC 1 ms
4,384 KB
testcase_40 AC 15 ms
4,968 KB
testcase_41 AC 6 ms
4,380 KB
testcase_42 AC 7 ms
4,384 KB
testcase_43 AC 25 ms
5,688 KB
testcase_44 AC 15 ms
4,704 KB
testcase_45 AC 6 ms
4,380 KB
testcase_46 AC 16 ms
4,732 KB
testcase_47 AC 22 ms
5,516 KB
testcase_48 AC 16 ms
5,080 KB
testcase_49 AC 31 ms
6,248 KB
testcase_50 AC 79 ms
6,672 KB
testcase_51 AC 79 ms
6,552 KB
testcase_52 AC 79 ms
6,556 KB
testcase_53 AC 80 ms
6,556 KB
testcase_54 AC 79 ms
6,532 KB
testcase_55 AC 85 ms
6,572 KB
testcase_56 AC 2 ms
4,380 KB
testcase_57 AC 2 ms
4,384 KB
testcase_58 AC 2 ms
4,380 KB
testcase_59 AC 1 ms
4,384 KB
testcase_60 AC 2 ms
4,384 KB
testcase_61 AC 2 ms
4,380 KB
testcase_62 AC 1 ms
4,384 KB
testcase_63 AC 1 ms
4,384 KB
testcase_64 AC 2 ms
4,384 KB
testcase_65 AC 1 ms
4,380 KB
testcase_66 AC 65 ms
5,940 KB
testcase_67 AC 42 ms
5,040 KB
testcase_68 AC 4 ms
4,380 KB
testcase_69 AC 72 ms
6,304 KB
testcase_70 AC 69 ms
6,148 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