結果
問題 |
No.2028 Even Choice
|
ユーザー |
|
提出日時 | 2022-09-01 01:17:07 |
言語 | Rust (1.83.0 + proconio) |
結果 |
AC
|
実行時間 | 26 ms / 2,000 ms |
コード長 | 1,122 bytes |
コンパイル時間 | 26,842 ms |
コンパイル使用メモリ | 377,240 KB |
実行使用メモリ | 7,296 KB |
最終ジャッジ日時 | 2024-11-08 19:41:09 |
合計ジャッジ時間 | 16,951 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 28 |
コンパイルメッセージ
warning: unused variable: `n` --> src/main.rs:7:9 | 7 | let n = nk[0]; | ^ help: if this is intentional, prefix it with an underscore: `_n` | = note: `#[warn(unused_variables)]` on by default
ソースコード
use std::{cmp::Reverse, collections::BinaryHeap}; fn main() { let mut nk = String::new(); std::io::stdin().read_line(&mut nk).ok(); let nk: Vec<usize> = nk.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let n = nk[0]; let k = nk[1]; let mut a = String::new(); std::io::stdin().read_line(&mut a).ok(); let a: Vec<usize> = a.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let mut result = 0usize; let mut summary = 0usize; let a = a.into_iter().skip(1).collect::<Vec<usize>>(); let mut pque = BinaryHeap::new(); for i in (0..a.len()).rev().take(k) { summary += a[i]; pque.push(Reverse(a[i])); } if (a.len()-k) % 2 == 0 { result = summary; } for i in (0..a.len()).rev().skip(k) { let val = (*pque.peek().unwrap()).0; if i % 2 == 0 { result = result.max(summary + a[i] - val); } pque.push(Reverse(a[i])); summary += a[i]; while pque.len() > k { summary -= pque.pop().unwrap().0; } } println!("{}", result); }