結果

問題 No.1334 Multiply or Add
ユーザー 👑 NachiaNachia
提出日時 2021-01-16 12:57:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,033 bytes
コンパイル時間 3,437 ms
コンパイル使用メモリ 202,644 KB
実行使用メモリ 4,584 KB
最終ジャッジ日時 2023-08-18 08:56:10
合計ジャッジ時間 9,447 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 17 ms
4,376 KB
testcase_04 AC 17 ms
4,376 KB
testcase_05 AC 17 ms
4,380 KB
testcase_06 AC 17 ms
4,380 KB
testcase_07 AC 17 ms
4,376 KB
testcase_08 AC 18 ms
4,380 KB
testcase_09 AC 18 ms
4,376 KB
testcase_10 AC 17 ms
4,376 KB
testcase_11 AC 18 ms
4,380 KB
testcase_12 AC 17 ms
4,380 KB
testcase_13 AC 17 ms
4,380 KB
testcase_14 AC 17 ms
4,376 KB
testcase_15 AC 17 ms
4,376 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 19 ms
4,456 KB
testcase_20 AC 19 ms
4,376 KB
testcase_21 AC 18 ms
4,376 KB
testcase_22 AC 17 ms
4,384 KB
testcase_23 AC 17 ms
4,504 KB
testcase_24 AC 17 ms
4,380 KB
testcase_25 AC 17 ms
4,380 KB
testcase_26 AC 17 ms
4,376 KB
testcase_27 AC 17 ms
4,380 KB
testcase_28 AC 18 ms
4,376 KB
testcase_29 AC 17 ms
4,376 KB
testcase_30 AC 17 ms
4,376 KB
testcase_31 AC 17 ms
4,484 KB
testcase_32 AC 18 ms
4,380 KB
testcase_33 AC 18 ms
4,456 KB
testcase_34 AC 17 ms
4,376 KB
testcase_35 AC 17 ms
4,384 KB
testcase_36 AC 18 ms
4,380 KB
testcase_37 AC 17 ms
4,376 KB
testcase_38 AC 17 ms
4,384 KB
testcase_39 AC 1 ms
4,376 KB
testcase_40 AC 8 ms
4,380 KB
testcase_41 AC 3 ms
4,376 KB
testcase_42 AC 4 ms
4,380 KB
testcase_43 AC 13 ms
4,376 KB
testcase_44 AC 8 ms
4,380 KB
testcase_45 AC 4 ms
4,376 KB
testcase_46 AC 9 ms
4,376 KB
testcase_47 AC 12 ms
4,376 KB
testcase_48 AC 9 ms
4,380 KB
testcase_49 AC 15 ms
4,376 KB
testcase_50 AC 28 ms
4,380 KB
testcase_51 AC 28 ms
4,376 KB
testcase_52 AC 28 ms
4,376 KB
testcase_53 AC 28 ms
4,380 KB
testcase_54 AC 28 ms
4,376 KB
testcase_55 AC 29 ms
4,380 KB
testcase_56 AC 2 ms
4,380 KB
testcase_57 AC 1 ms
4,380 KB
testcase_58 AC 2 ms
4,376 KB
testcase_59 WA -
testcase_60 AC 2 ms
4,376 KB
testcase_61 AC 2 ms
4,380 KB
testcase_62 AC 1 ms
4,380 KB
testcase_63 WA -
testcase_64 AC 1 ms
4,376 KB
testcase_65 AC 2 ms
4,376 KB
testcase_66 AC 23 ms
4,380 KB
testcase_67 AC 16 ms
4,376 KB
testcase_68 AC 2 ms
4,376 KB
testcase_69 AC 26 ms
4,376 KB
testcase_70 AC 24 ms
4,380 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; }
  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);
  }
  else{
    printf("%llu\n",m);
  }
  return 0;
}
0