Problem: 2061D - Kevin and Numbers
Language: Rust 2021

Previous submission (verdict: Wrong answer on test 3):
#![allow(unused)]



use std::{

    cmp::{max, min, Ordering, Reverse},

    collections::{BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque},

    io::stdin,

    mem::swap,

    ops::Index,

    str::FromStr,

    thread::current,

    usize,

};



fn take_usize() -> usize {

    let mut input = String::new();

    stdin().read_line(&mut input).unwrap();

    input.trim().parse().unwrap()

}



fn take_vector<T>() -> Vec<T>

where

    T: FromStr,

    <T as FromStr>::Err: std::fmt::Debug,

{

    let mut input = String::new();

    stdin().read_line(&mut input).unwrap();

    input

        .trim()

        .split_whitespace()

        .map(|x| T::from_str(x).unwrap())

        .collect()

}



fn take_vector_usize() -> Vec<usize> {

    take_vector()

}



fn take_vector_int() -> Vec<i64> {

    take_vector()

}



fn take_string() -> String {

    let mut input = String::new();

    stdin().read_line(&mut input).unwrap();

    input = input.trim().to_string();

    input

}



fn take_chars() -> Vec<char> {

    take_string().chars().collect()

}



fn create_vector<T>(n: usize, initial_value: T) -> Vec<T>

where

    T: Clone,

{

    (0..n).map(|_| initial_value.clone()).collect()

}



type VV<T> = Vec<Vec<T>>;



fn solve() {

    let input = take_vector_usize();

    let n = input[0];

    let m = input[1];



    let a = take_vector_usize();

    let mut b = take_vector_usize();

    b.sort();



    let mut pq = a.iter().map(|val| Reverse(*val)).collect::<BinaryHeap<_>>();

    let mut cnt: HashMap<usize, i32> = HashMap::with_capacity(n); 

    for el in a {

        cnt.insert(el, cnt.get(&el).unwrap_or(&0) + 1);

    }



    let mut can = true;



    for i in 0..m {

        loop {

            if pq.is_empty() {

                can = false;

                break;

            }



            if *cnt.get(&b[i]).unwrap_or(&0) > 0 {

                cnt.insert(b[i], cnt.get(&b[i]).unwrap() - 1);

                break;

            }



            let top = pq.pop().unwrap().0;

            if *cnt.get(&top).unwrap_or(&0) == 0 {

                continue;

            }

            

            if top > b[i] {

                can = false;

                break;

            } else if top == b[i] {

                cnt.insert(top, cnt.get(&top).unwrap() - 1);

                break;

            } else if (b[i] - top == top + 1 && *cnt.get(&(b[i] - top)).unwrap_or(&0) > 0) {

                cnt.insert(top, cnt.get(&top).unwrap() - 1);

                cnt.insert(b[i] - top, cnt.get(&(b[i] - top)).unwrap() - 1);

                break;

            } else if !pq.is_empty() {

                let next = pq.pop().unwrap().0;

                if next == top && *cnt.get(&next).unwrap() == 1 {

                    pq.push(Reverse(next));

                    continue;

                } else if *cnt.get(&next).unwrap_or(&0) == 0 {

                    pq.push(Reverse(top));

                    continue;

                }



                if next - top <= 1 {

                    let val = next + top;

                    pq.push(Reverse(val));

                    cnt.insert(val, cnt.get(&val).unwrap_or(&0) + 1);

                    cnt.insert(next, cnt.get(&next).unwrap_or(&1) - 1);

                    cnt.insert(top, cnt.get(&top).unwrap_or(&1) - 1);

                } else {

                    can = false;

                    break;

                }

            } else {

                can = false;

                break;

            }

        }



        if !can {

            break;

        }

    }



    if can && cnt.values().sum::<i32>() == 0 {

        println!("YES");

    } else {

        println!("NO");

    }

}



pub fn main() {

    let t = take_usize();



    for t in 0..t {

        solve();

    }



    // solve();

}



Submission (verdict: Wrong answer on test 3):
#![allow(unused)]



use std::{

    cmp::{max, min, Ordering, Reverse},

    collections::{BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque},

    io::stdin,

    mem::swap,

    ops::Index,

    str::FromStr,

    thread::current,

    usize,

};



fn take_usize() -> usize {

    let mut input = String::new();

    stdin().read_line(&mut input).unwrap();

    input.trim().parse().unwrap()

}



fn take_vector<T>() -> Vec<T>

where

    T: FromStr,

    <T as FromStr>::Err: std::fmt::Debug,

{

    let mut input = String::new();

    stdin().read_line(&mut input).unwrap();

    input

        .trim()

        .split_whitespace()

        .map(|x| T::from_str(x).unwrap())

        .collect()

}



fn take_vector_usize() -> Vec<usize> {

    take_vector()

}



fn take_vector_int() -> Vec<i64> {

    take_vector()

}



fn take_string() -> String {

    let mut input = String::new();

    stdin().read_line(&mut input).unwrap();

    input = input.trim().to_string();

    input

}



fn take_chars() -> Vec<char> {

    take_string().chars().collect()

}



fn create_vector<T>(n: usize, initial_value: T) -> Vec<T>

where

    T: Clone,

{

    (0..n).map(|_| initial_value.clone()).collect()

}



type VV<T> = Vec<Vec<T>>;



fn solve() {

    let input = take_vector_usize();

    let n = input[0];

    let m = input[1];



    let a = take_vector_usize();

    let mut b = take_vector_usize();



    let mut cnt: HashMap<usize, i64> = HashMap::with_capacity(n); 

    for el in a {

        cnt.insert(el, cnt.get(&el).unwrap_or(&0) + 1);

    }

    let mut pq = b.iter().map(|val| *val).collect::<BinaryHeap<_>>();



    while !pq.is_empty() && pq.len() <= n {

        let el = pq.pop().unwrap();



        if let Some(i) = cnt.get(&el) {

            cnt.insert(el, i - 1);

        } else {

            let v1 = f64::ceil(el as f64 / 2f64) as i64;

            let v2 = f64::floor(el as f64 / 2f64) as i64;



            if v1 == 0 || v2 == 0 {

                println!("NO");

                return;

            }



            pq.push(v1 as usize);

            pq.push(v2 as usize);

        }

    }



    if cnt.values().sum::<i64>() != 0 || pq.len() > 0 {

        println!("NO");

    } else {

        println!("YES");

    }



}



pub fn main() {

    let t = take_usize();



    for t in 0..t {

        solve();

    }



    // solve();

}