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 can = true;



    for i in 0..m {

        if pq.is_empty() {

            can = false;

            break;

        }



        loop {

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

            if top.0 > b[i] {

                can = false;

                break;

            } else if top.0 == b[i] {

                break;

            } else {

                if let Some(next) = pq.pop() {

                    if next.0 - top.0 <= 1 {

                        let val = next.0 + top.0;

                        pq.push(Reverse(val));

                    } else {

                        can = false;

                        break;

                    }

                } else {

                    can = false;

                    break;

                }

            }

        }



        if !can {

            break;

        }

    }



    if can && pq.is_empty() {

        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();

    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();

}