結果

問題 No.1334 Multiply or Add
ユーザー leaf_1415leaf_1415
提出日時 2021-01-08 23:03:08
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,962 bytes
コンパイル時間 1,251 ms
コンパイル使用メモリ 88,328 KB
実行使用メモリ 6,656 KB
最終ジャッジ日時 2024-04-28 04:49:19
合計ジャッジ時間 5,490 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 36 ms
6,400 KB
testcase_04 AC 36 ms
6,272 KB
testcase_05 AC 35 ms
6,400 KB
testcase_06 AC 34 ms
6,400 KB
testcase_07 AC 36 ms
6,400 KB
testcase_08 AC 35 ms
6,272 KB
testcase_09 AC 35 ms
6,272 KB
testcase_10 AC 35 ms
6,272 KB
testcase_11 AC 35 ms
6,400 KB
testcase_12 AC 35 ms
6,400 KB
testcase_13 AC 36 ms
6,656 KB
testcase_14 AC 36 ms
6,656 KB
testcase_15 AC 36 ms
6,528 KB
testcase_16 AC 35 ms
6,272 KB
testcase_17 AC 36 ms
6,272 KB
testcase_18 AC 35 ms
6,272 KB
testcase_19 AC 36 ms
6,400 KB
testcase_20 AC 36 ms
6,528 KB
testcase_21 AC 36 ms
6,272 KB
testcase_22 WA -
testcase_23 AC 36 ms
6,272 KB
testcase_24 AC 35 ms
6,400 KB
testcase_25 AC 36 ms
6,400 KB
testcase_26 AC 35 ms
6,400 KB
testcase_27 AC 35 ms
6,272 KB
testcase_28 AC 35 ms
6,400 KB
testcase_29 AC 35 ms
6,400 KB
testcase_30 AC 35 ms
6,400 KB
testcase_31 AC 36 ms
6,400 KB
testcase_32 AC 35 ms
6,272 KB
testcase_33 AC 36 ms
6,528 KB
testcase_34 AC 36 ms
6,528 KB
testcase_35 AC 36 ms
6,528 KB
testcase_36 AC 36 ms
6,528 KB
testcase_37 AC 36 ms
6,528 KB
testcase_38 AC 36 ms
6,528 KB
testcase_39 WA -
testcase_40 AC 17 ms
5,376 KB
testcase_41 AC 7 ms
5,376 KB
testcase_42 AC 8 ms
5,376 KB
testcase_43 AC 27 ms
5,760 KB
testcase_44 AC 16 ms
5,376 KB
testcase_45 AC 7 ms
5,376 KB
testcase_46 AC 17 ms
5,376 KB
testcase_47 AC 24 ms
5,632 KB
testcase_48 AC 17 ms
5,376 KB
testcase_49 AC 33 ms
6,272 KB
testcase_50 AC 85 ms
6,528 KB
testcase_51 AC 87 ms
6,528 KB
testcase_52 AC 85 ms
6,528 KB
testcase_53 AC 85 ms
6,656 KB
testcase_54 AC 86 ms
6,656 KB
testcase_55 AC 92 ms
6,656 KB
testcase_56 AC 2 ms
5,376 KB
testcase_57 AC 1 ms
5,376 KB
testcase_58 AC 1 ms
5,376 KB
testcase_59 AC 2 ms
5,376 KB
testcase_60 AC 2 ms
5,376 KB
testcase_61 AC 2 ms
5,376 KB
testcase_62 AC 2 ms
5,376 KB
testcase_63 AC 1 ms
5,376 KB
testcase_64 AC 2 ms
5,376 KB
testcase_65 AC 1 ms
5,376 KB
testcase_66 AC 70 ms
6,016 KB
testcase_67 AC 47 ms
5,376 KB
testcase_68 AC 4 ms
5,376 KB
testcase_69 AC 78 ms
6,400 KB
testcase_70 AC 75 ms
6,144 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;
	}
	
	//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);
	
	//for(auto x : vec) cout<< x << " "; cout << endl;
	//for(auto x : vec2) cout<< x << " "; cout << endl;
	
	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