結果

問題 No.1077 Noelちゃんと星々4
ユーザー akakimidori
提出日時 2020-06-12 21:37:53
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 29 ms / 2,000 ms
コード長 790 bytes
コンパイル時間 24,814 ms
コンパイル使用メモリ 401,300 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-24 04:36:45
合計ジャッジ時間 16,402 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::Read;

fn run() {
    let mut s = String::new();
    std::io::stdin().read_to_string(&mut s).unwrap();
    let mut it = s.trim().split_whitespace();
    let _n: usize = it.next().unwrap().parse().unwrap();
    let a: Vec<i32> = it.map(|s| s.parse().unwrap()).collect();
    let w = 10000;
    let mut dp = vec![0i32; w + 1];
    for i in 0..=w {
        dp[i] = (i as i32 - a[0]).abs();
    }
    for &a in a.iter().skip(1) {
        let mut next = vec![std::i32::MAX / 10; w + 1];
        let mut val = std::i32::MAX / 10;
        for i in 0..=w {
            val = std::cmp::min(val, dp[i]);
            next[i] = val + (a - i as i32).abs();
        }
        dp = next;
    }
    let ans = dp.into_iter().min().unwrap();
    println!("{}", ans);
}

fn main() {
    run();
}
0