結果

問題 No.1334 Multiply or Add
ユーザー 👑 NachiaNachia
提出日時 2021-01-28 17:40:21
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,169 bytes
コンパイル時間 2,109 ms
コンパイル使用メモリ 203,248 KB
実行使用メモリ 4,524 KB
最終ジャッジ日時 2023-09-08 08:53:58
合計ジャッジ時間 6,673 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 17 ms
4,376 KB
testcase_04 AC 18 ms
4,376 KB
testcase_05 AC 17 ms
4,376 KB
testcase_06 AC 17 ms
4,380 KB
testcase_07 AC 17 ms
4,380 KB
testcase_08 AC 17 ms
4,376 KB
testcase_09 AC 17 ms
4,380 KB
testcase_10 AC 18 ms
4,376 KB
testcase_11 AC 18 ms
4,380 KB
testcase_12 AC 17 ms
4,376 KB
testcase_13 AC 18 ms
4,376 KB
testcase_14 AC 18 ms
4,404 KB
testcase_15 AC 18 ms
4,380 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 19 ms
4,384 KB
testcase_20 AC 19 ms
4,380 KB
testcase_21 AC 19 ms
4,380 KB
testcase_22 AC 17 ms
4,376 KB
testcase_23 AC 17 ms
4,376 KB
testcase_24 AC 17 ms
4,376 KB
testcase_25 AC 18 ms
4,376 KB
testcase_26 AC 18 ms
4,380 KB
testcase_27 AC 17 ms
4,376 KB
testcase_28 AC 18 ms
4,524 KB
testcase_29 AC 18 ms
4,380 KB
testcase_30 AC 18 ms
4,380 KB
testcase_31 AC 18 ms
4,380 KB
testcase_32 AC 17 ms
4,376 KB
testcase_33 AC 17 ms
4,376 KB
testcase_34 AC 18 ms
4,432 KB
testcase_35 AC 17 ms
4,380 KB
testcase_36 AC 18 ms
4,380 KB
testcase_37 AC 18 ms
4,380 KB
testcase_38 AC 18 ms
4,380 KB
testcase_39 AC 2 ms
4,376 KB
testcase_40 AC 8 ms
4,380 KB
testcase_41 AC 4 ms
4,376 KB
testcase_42 AC 5 ms
4,380 KB
testcase_43 AC 13 ms
4,380 KB
testcase_44 AC 8 ms
4,380 KB
testcase_45 AC 4 ms
4,380 KB
testcase_46 AC 9 ms
4,380 KB
testcase_47 AC 12 ms
4,376 KB
testcase_48 AC 9 ms
4,380 KB
testcase_49 AC 16 ms
4,504 KB
testcase_50 AC 29 ms
4,384 KB
testcase_51 AC 29 ms
4,376 KB
testcase_52 AC 28 ms
4,380 KB
testcase_53 AC 29 ms
4,376 KB
testcase_54 AC 28 ms
4,380 KB
testcase_55 AC 29 ms
4,504 KB
testcase_56 AC 2 ms
4,380 KB
testcase_57 AC 1 ms
4,376 KB
testcase_58 AC 2 ms
4,380 KB
testcase_59 AC 1 ms
4,376 KB
testcase_60 AC 1 ms
4,376 KB
testcase_61 AC 1 ms
4,376 KB
testcase_62 AC 2 ms
4,376 KB
testcase_63 AC 1 ms
4,380 KB
testcase_64 AC 2 ms
4,380 KB
testcase_65 AC 2 ms
4,376 KB
testcase_66 AC 24 ms
4,380 KB
testcase_67 AC 16 ms
4,380 KB
testcase_68 AC 2 ms
4,376 KB
testcase_69 AC 27 ms
4,376 KB
testcase_70 AC 25 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using LL=long long;
using ULL=unsigned long long;
#define rep(i,n) for(int i=0; i<(n); i++)

const ULL M=1000000007;

int N;
int A[200000];

int main(){
  scanf("%d",&N);
  rep(i,N) scanf("%d",&A[i]);
  ULL m=1;
  bool large=false;
  rep(i,N){ m*=A[i]; if(m>N*2) large=true; m%=M; }
  int ans_offset=0;
  rep(t,2){
    while((N==0)?false:(A[N-1]==1)){ ans_offset++; N--; }
    reverse(A,A+N);
  }
  if(!large){
    vector<int> V={0};
    rep(i,N){
      if(V.size()%2==1){
        if(A[i]==1) V.back()++;
        else V.push_back(A[i]);
      }
      else{
        if(A[i]==1) V.push_back(1);
        else V.back()*=A[i];
      }
    }
    int ans=0;
    struct Node{ int factor,term; } dp[32]={};
    int n=V.size()/2; int p=n-1;
    rep(i,1<<n){
      int p=n-1; while(p>=0&&(1<<(n-1-p))&~i) p--;
      if(p!=-1) dp[p+1]={dp[p].factor*V[p*2+1],dp[p].term}; p++;
      for(; p<n; p++) dp[p+1]={V[p*2+1],dp[p].term+dp[p].factor+V[p*2]};
      ans=max(ans,dp[n].factor+dp[n].term);
    }
    if(V.size()%2==1) ans+=V.back();
    printf("%d\n",ans+ans_offset);
  }
  else{
    printf("%llu\n",m+ans_offset);
  }
  return 0;
}
0