結果

問題 No.31 悪のミックスジュース
ユーザー koba-e964koba-e964
提出日時 2021-10-09 02:36:25
言語 Rust
(1.77.0)
結果
WA  
実行時間 -
コード長 1,932 bytes
コンパイル時間 4,019 ms
コンパイル使用メモリ 145,304 KB
実行使用メモリ 5,864 KB
最終ジャッジ日時 2023-09-30 18:28:22
合計ジャッジ時間 8,612 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
5,860 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 4 ms
5,796 KB
testcase_07 AC 311 ms
5,864 KB
testcase_08 AC 310 ms
5,844 KB
testcase_09 WA -
testcase_10 AC 1 ms
4,380 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(unused_imports)]
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"));
}

fn main() {
    input! {
        n: usize, v: i64,
        c: [i64; n],
    }
    let mut d = vec![0; n];
    d[0] = c[0];
    for i in 1..n {
        d[i] = d[i - 1] + c[i];
    }
    if v < n as i64 {
        println!("{}", d[n - 1]);
        return;
    }
    let v = v - n as i64;
    // O(n^3), therefore the overall complexity is O(n^4)
    const W: usize = 510_000;
    const INF: i64 = 1 << 50;
    let mut dp = vec![INF; W];
    dp[0] = 0;
    for i in 0..n {
        let a = i + 1;
        for j in 0..W - a {
            dp[j + a] = min(dp[j + a], dp[j] + d[i]);
        }
    }
    let mut mi = INF;
    for i in 0..W {
        if i as i64 > v {
            continue;
        }
        let rest = v - i as i64;
        for j in 0..n {
            if rest % (j + 1) as i64 != 0 { continue; }
            let tmp = dp[i] + (rest / (j + 1) as i64) * d[j];
            mi = min(mi, tmp);
        }
    }
    println!("{}", mi + d[n - 1]);
}
0