use std::io::{self, Read}; #[derive(Debug)] struct Input { ws: Vec, } fn next_token(cin_lock: &mut io::StdinLock) -> String { cin_lock .by_ref() .bytes() .map(|c| c.unwrap() as char) .skip_while(|c| c.is_whitespace()) .take_while(|c| !c.is_whitespace()) .collect::() } fn read_input(cin_lock: &mut io::StdinLock) -> Input { let n = next_token(cin_lock).parse().unwrap(); let ws = (0..n) .map(|_| next_token(cin_lock).parse().unwrap()) .collect(); Input { ws } } #[derive(Debug, Eq, PartialEq)] struct Solution { possible: bool, } impl Solution { fn new(possible: bool) -> Solution { Solution { possible } } } fn simulate(input: Input) -> Solution { let sum = input.ws.iter().sum::(); if sum % 2 != 0 { return Solution::new(false); } let half = sum / 2; let mut ws = input.ws; ws.sort(); let mut dp = vec![vec![false; half + 1]; ws.len() + 1]; dp[0][0] = true; for item_index in 0..ws.len() { if dp[item_index][half] { return Solution { possible: true }; } for sum in 0..=half { dp[item_index + 1][sum] = dp[item_index][sum]; if sum >= ws[item_index] { dp[item_index + 1][sum] |= dp[item_index][sum - ws[item_index]]; } } } let possible = dp[ws.len()][half]; Solution { possible } } fn solve(input: Input, _cin_lock: &mut io::StdinLock) { let answer = simulate(input); println!( "{}", if answer.possible { "possible" } else { "impossible" } ); } fn main() { let cin = io::stdin(); let mut cin_lock = cin.lock(); let input = read_input(&mut cin_lock); solve(input, &mut cin_lock); } #[cfg(test)] mod tests { use super::*; #[test] fn sample_verify() { assert_eq!( Solution::new(true), simulate(Input { ws: vec![62, 1, 90, 3, 24] }) ); } #[test] fn sample1() { assert_eq!(Solution::new(true), simulate(Input { ws: vec![1, 2, 3] })); } #[test] fn sample2() { assert_eq!( Solution::new(false), simulate(Input { ws: vec![1, 2, 3, 4, 5] }) ); } #[test] fn sample3() { assert_eq!( Solution::new(false), simulate(Input { ws: vec![62, 8, 90, 2, 24, 62, 38, 64, 76, 60, 30, 76, 80, 74, 72] }) ); } }