From f173859de5d3b7e72a0535adb347e5c3d59f0ee7 Mon Sep 17 00:00:00 2001 From: Muhammad Nauman Raza Date: Wed, 22 Jan 2025 21:58:24 +0000 Subject: [PATCH] solutions: 34B --- solutions/rust/src/bin/34B.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 solutions/rust/src/bin/34B.rs diff --git a/solutions/rust/src/bin/34B.rs b/solutions/rust/src/bin/34B.rs new file mode 100644 index 0000000..4f501ba --- /dev/null +++ b/solutions/rust/src/bin/34B.rs @@ -0,0 +1,33 @@ +use std::io; + +fn main() { + let mut s = String::new(); + io::stdin().read_line(&mut s).unwrap(); + + let a1 = s.trim().split_whitespace().map(|i| i.parse::().unwrap()).collect::>(); + let max = a1[1]; + + let mut s = String::new(); + io::stdin().read_line(&mut s).unwrap(); + + let mut a2 = s.trim().split_whitespace().map(|i| i.parse::().unwrap()).collect::>(); + + let mut cost = 0; + let mut count = 0; + a2.sort(); + + for i in a2 { + if count == max { + println!("{}", -1*cost); + std::process::exit(0); + } else if cost+i > cost { + println!("{}", -1*cost); + std::process::exit(0); + } else { + count += 1; + cost += i; + } + } + + println!("{}", -1*cost); +}