結果

問題 No.783 門松計画
ユーザー ngtkanangtkana
提出日時 2023-04-30 18:49:28
言語 Rust
(1.77.0)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,849 bytes
コンパイル時間 1,230 ms
コンパイル使用メモリ 166,528 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-27 00:30:57
合計ジャッジ時間 2,311 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

use cmpmore::CmpMore;
use std::{convert::TryFrom, io::stdin, isize::MIN};

fn main() {
    let mut stdin = stdin().lines().map(Result::unwrap);
    let [_n, wmax] = <[_; 2]>::try_from(
        stdin
            .next()
            .unwrap()
            .split_whitespace()
            .map(|x| x.parse::<usize>().unwrap())
            .collect::<Vec<_>>()
            .as_slice(),
    )
    .unwrap();
    let ls = stdin
        .next()
        .unwrap()
        .split_whitespace()
        .map(|x| x.parse::<usize>().unwrap())
        .collect::<Vec<_>>();
    let lmax = ls.iter().max().unwrap() + 1;
    let ws = stdin
        .next()
        .unwrap()
        .split_whitespace()
        .map(|x| x.parse::<usize>().unwrap())
        .collect::<Vec<_>>();
    let mut dp = vec![vec![vec![MIN; lmax + 1]; lmax + 1]; wmax + 1];
    for (&l1, &dw1) in ls.iter().zip(&ws) {
        for (&l2, &dw2) in ls.iter().zip(&ws) {
            for (&l3, &dw3) in ls.iter().zip(&ws) {
                for w in dw1 + dw2 + dw3..=wmax {
                    if (l1 > l2 && l2 < l3 && l1 != l3) || (l1 < l2 && l2 > l3 && l1 != l3) {
                        dp[w - dw1 - dw2 - dw3][l1][l2].change_max((l1 + l2 + l3) as isize);
                    }
                }
            }
        }
    }
    for w in (0..=wmax).rev() {
        for (&l, &dw) in ls.iter().zip(&ws) {
            if dw <= w {
                for l1 in 0..=lmax {
                    for l2 in 0..=lmax {
                        if (l2 > l1 && l1 < l && l2 != l) || (l2 < l1 && l1 > l && l2 != l) {
                            change_max!(&mut dp[w - dw][l][l1], dp[w][l1][l2] + l as isize);
                        }
                    }
                }
            }
        }
    }
    let ans = (*dp.iter().flatten().flatten().max().unwrap()).max(0);
    println!("{}", ans);
}

// cmpmore {{{
#[allow(dead_code)]
mod cmpmore {
    pub fn change_min<T: PartialOrd>(lhs: &mut T, rhs: T) {
        if *lhs > rhs {
            *lhs = rhs;
        }
    }
    pub fn change_max<T: PartialOrd>(lhs: &mut T, rhs: T) {
        if *lhs < rhs {
            *lhs = rhs;
        }
    }
    #[macro_export]
    macro_rules! change_min {
        ($lhs:expr, $rhs:expr) => {
            let rhs = $rhs;
            let lhs = $lhs;
            $crate::cmpmore::change_min(lhs, rhs);
        };
    }
    #[macro_export]
    macro_rules! change_max {
        ($lhs:expr, $rhs:expr) => {
            let rhs = $rhs;
            let lhs = $lhs;
            $crate::cmpmore::change_max(lhs, rhs);
        };
    }
    pub trait CmpMore: PartialOrd + Sized {
        fn change_min(&mut self, rhs: Self) {
            change_min(self, rhs)
        }
        fn change_max(&mut self, rhs: Self) {
            change_max(self, rhs)
        }
    }
    impl<T: PartialOrd + Sized> CmpMore for T {}
}
// }}}
0