結果

問題 No.1334 Multiply or Add
ユーザー koba-e964koba-e964
提出日時 2021-11-04 14:00:51
言語 Rust
(1.77.0)
結果
AC  
実行時間 23 ms / 2,000 ms
コード長 2,254 bytes
コンパイル時間 940 ms
コンパイル使用メモリ 163,584 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-22 12:40:53
合計ジャッジ時間 3,731 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

use std::cmp::*;
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes.by_ref().map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr,) => {};
    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
    ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

const MOD: i64 = 1_000_000_007;

fn main() {
    input! {
        n: usize,
        a: [i64; n],
    }
    let inf = 400_001;
    let mut one = true;
    let mut l = 0;
    let mut v = vec![];
    let mut c = 1;
    let mut d = 1;
    for i in 0..n {
        if one != (a[i] == 1) {
            v.push((l, c, d));
            one = a[i] == 1;
            l = 1;
            c = a[i];
            d = a[i];
        } else {
            c = min(inf, c * a[i]);
            d = d * a[i] % MOD;
            l += 1;
        }
    }
    v.push((l, c, d));
    if v.len() % 2 == 0 {
        v.push((0, 1, 1));
    }
    let m = v.len() / 2;
    if m == 0 {
        println!("{}", v[0].0);
        return;
    }
    c = 1;
    d = 1;
    for i in 0..m {
        let (_, cc, dd) = v[2 * i + 1];
        c = min(inf, c * cc);
        d = d * dd % MOD;
    }
    if c >= inf {
        println!("{}", (d + v[0].0 + v[2 * m].0) % MOD);
        return;
    }
    let mut dp = vec![0; m + 1];
    for i in 0..m {
        let mut ans = 0;
        for j in 0..i + 1 {
            let mut p = 1;
            for k in j..i + 1 {
                p *= v[2 * k + 1].1;
            }
            ans = max(ans, v[2 * j].0 + p + dp[j]);
        }
        dp[i + 1] = ans;
    }
    println!("{}", dp[m] + v[2 * m].0);
}
0