結果

問題 No.1334 Multiply or Add
ユーザー penguinmanpenguinman
提出日時 2020-12-31 03:40:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 105 ms / 2,000 ms
コード長 1,785 bytes
コンパイル時間 2,226 ms
コンパイル使用メモリ 202,840 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-17 05:26:40
合計ジャッジ時間 6,710 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 36 ms
5,376 KB
testcase_04 AC 36 ms
5,376 KB
testcase_05 AC 36 ms
5,376 KB
testcase_06 AC 36 ms
5,376 KB
testcase_07 AC 36 ms
5,376 KB
testcase_08 AC 36 ms
5,376 KB
testcase_09 AC 36 ms
5,376 KB
testcase_10 AC 36 ms
5,376 KB
testcase_11 AC 36 ms
5,376 KB
testcase_12 AC 36 ms
5,376 KB
testcase_13 AC 37 ms
5,376 KB
testcase_14 AC 36 ms
5,376 KB
testcase_15 AC 36 ms
5,376 KB
testcase_16 AC 36 ms
5,376 KB
testcase_17 AC 37 ms
5,376 KB
testcase_18 AC 37 ms
5,376 KB
testcase_19 AC 45 ms
5,376 KB
testcase_20 AC 46 ms
5,376 KB
testcase_21 AC 46 ms
5,376 KB
testcase_22 AC 35 ms
5,376 KB
testcase_23 AC 36 ms
5,376 KB
testcase_24 AC 36 ms
5,376 KB
testcase_25 AC 36 ms
5,376 KB
testcase_26 AC 36 ms
5,376 KB
testcase_27 AC 36 ms
5,376 KB
testcase_28 AC 36 ms
5,376 KB
testcase_29 AC 35 ms
5,376 KB
testcase_30 AC 36 ms
5,376 KB
testcase_31 AC 35 ms
5,376 KB
testcase_32 AC 36 ms
5,376 KB
testcase_33 AC 36 ms
5,376 KB
testcase_34 AC 36 ms
5,376 KB
testcase_35 AC 36 ms
5,376 KB
testcase_36 AC 36 ms
5,376 KB
testcase_37 AC 36 ms
5,376 KB
testcase_38 AC 36 ms
5,376 KB
testcase_39 AC 2 ms
5,376 KB
testcase_40 AC 15 ms
5,376 KB
testcase_41 AC 7 ms
5,376 KB
testcase_42 AC 7 ms
5,376 KB
testcase_43 AC 27 ms
5,376 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,376 KB
testcase_48 AC 17 ms
5,376 KB
testcase_49 AC 33 ms
5,376 KB
testcase_50 AC 105 ms
5,376 KB
testcase_51 AC 103 ms
5,376 KB
testcase_52 AC 103 ms
5,376 KB
testcase_53 AC 103 ms
5,376 KB
testcase_54 AC 103 ms
5,376 KB
testcase_55 AC 97 ms
5,376 KB
testcase_56 AC 2 ms
5,376 KB
testcase_57 AC 2 ms
5,376 KB
testcase_58 AC 2 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 2 ms
5,376 KB
testcase_64 AC 2 ms
5,376 KB
testcase_65 AC 2 ms
5,376 KB
testcase_66 AC 85 ms
5,376 KB
testcase_67 AC 56 ms
5,376 KB
testcase_68 AC 4 ms
5,376 KB
testcase_69 AC 94 ms
5,376 KB
testcase_70 AC 89 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
using ll=long long;
const int mod=1e9+7;
int main(){
    int N; cin>>N;
    vector<int> A(N);
    for(int i=0;i<N;i++) cin>>A[i];
    {
        vector<int> B=A;
        sort(B.begin(),B.end());
        if(B[N-1]==1){
            cout<<N<<endl;
            return 0;
        }
    }
    ll ans=1;
    for(int i=0;i<N;i++){
        ans*=A[i];
        if(ans>=2*N) break;
    }
    if(ans>=2*N){
        ans=1;
        for(int i=0;i<N;i++){
            ans*=A[i];
            ans%=mod;
        }
        for(int i=0;i<N;i++){
            if(A[i]>1) break;
            ans++;
        }
        for(int i=N-1;i>=0;i--){
            if(A[i]>1) break;
            ans++;
        }
        ans%=mod;
        cout<<ans<<endl;
        return 0;
    }
    ans=0;
    for(int i=N-1;i>=0;i--){
        if(A[i]>1) break;
        ans++;
        A.pop_back();
    }
    reverse(A.begin(),A.end());
    for(int i=A.size()-1;i>=0;i--){
        if(A[i]>1) break;
        ans++;
        A.pop_back();
    }
    vector<int> comp(1,A[0]);
    for(int i=1;i<A.size();i++){
        if(A[i]>1){
            if(A[i-1]>1) comp[comp.size()-1]*=A[i];
            else comp.push_back(A[i]);
        }
        else{
            if(A[i-1]>1) comp.push_back(1);
            else comp[comp.size()-1]++;
        }
    }
    int M=comp.size();
    int max=0;
    for(int i=0;i<(1<<M/2);i++){
        int sum=0,now=comp[0];
        for(int j=1;j<M;j+=2){
            if(i&(1<<(j/2))){
                now*=comp[j+1];
            }
            else{
                sum+=now+comp[j];
                now=comp[j+1];
            }
        }
        sum+=now;
        max=std::max(max,sum);
    }
    ans+=max;
    cout<<ans<<endl;
}
0