QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#285649#7944. Max Minus Minucup-team296#AC ✓83ms12092kbRust37.0kb2023-12-16 21:09:132023-12-16 21:09:13

Judging History

你现在查看的是最新测评结果

  • [2023-12-16 21:09:13]
  • 评测
  • 测评结果:AC
  • 用时:83ms
  • 内存:12092kb
  • [2023-12-16 21:09:13]
  • 提交

answer

// 
pub mod solution {

use crate::collections::iter_ext::collect::IterCollect;
use crate::collections::iter_ext::iters::Iters;
use crate::collections::min_max::MinimMaxim;
use crate::collections::segment_tree::{SegmentTree, SegmentTreeNode};
use crate::io::input::Input;
use crate::io::output::Output;

type PreCalc = ();

fn solve(input: &mut Input, out: &mut Output, _test_case: usize, _data: &PreCalc) {
    let n = input.read_size();
    let a = input.read_long_vec(n);

    #[derive(Copy, Clone, Default)]
    struct Node {
        max: i64,
        min: i64,
    }

    impl SegmentTreeNode for Node {
        fn new(_left: usize, _right: usize) -> Self {
            Self {
                min: i64::MAX / 2,
                max: 0,
            }
        }

        fn join(
            &mut self,
            left_val: &Self,
            right_val: &Self,
            _left: usize,
            _mid: usize,
            _right: usize,
        ) {
            self.max = left_val.max.max(right_val.max);
            self.min = left_val.min.min(right_val.min);
        }

        fn accumulate(&mut self, _value: &Self, _left: usize, _right: usize) {}

        fn reset_delta(&mut self, _left: usize, _right: usize) {}
    }

    let mut st = SegmentTree::from_generator(n, |i| Node {
        max: a[i],
        min: a[i],
    });
    let mut order = (0..n).collect_vec();
    order.sort_by_key(|&i| a[i]);
    let mut left = n;
    let mut right = 0;
    let mut ans = None;
    for &i in &order {
        left.minim(i);
        right.maxim(i);
        let Node {
            min: in_min,
            max: in_max,
        } = st.query(left..=right);
        let Node {
            min: left_min,
            max: left_max,
        } = st.query(..left);
        let Node {
            min: right_min,
            max: right_max,
        } = st.query(right + 1..);
        let out_min = left_min.min(right_min);
        let out_max = left_max.max(right_max);
        let cur = (in_max - in_min).max(out_max - out_min);
        ans.minim(cur);
    }
    left = n;
    right = 0;
    for &i in order.rev() {
        left.minim(i);
        right.maxim(i);
        let Node {
            min: in_min,
            max: in_max,
        } = st.query(left..=right);
        let Node {
            min: left_min,
            max: left_max,
        } = st.query(..left);
        let Node {
            min: right_min,
            max: right_max,
        } = st.query(right + 1..);
        let out_min = left_min.min(right_min);
        let out_max = left_max.max(right_max);
        let cur = (in_max - in_min).max(out_max - out_min);
        ans.minim(cur);
    }
    out.print_line(ans);
}

pub(crate) fn run(mut input: Input, mut output: Output) -> bool {
    let pre_calc = ();

    #[allow(dead_code)]
    enum TestType {
        Single,
        MultiNumber,
        MultiEof,
    }
    let test_type = TestType::MultiNumber;
    match test_type {
        TestType::Single => solve(&mut input, &mut output, 1, &pre_calc),
        TestType::MultiNumber => {
            let t = input.read();
            for i in 1..=t {
                solve(&mut input, &mut output, i, &pre_calc);
            }
        }
        TestType::MultiEof => {
            let mut i = 1;
            while input.peek().is_some() {
                solve(&mut input, &mut output, i, &pre_calc);
                i += 1;
            }
        }
    }
    output.flush();
    input.skip_whitespace();
    input.peek().is_none()
}

}
pub mod collections {
pub mod bounds {
use std::ops::RangeBounds;

pub fn clamp(range: impl RangeBounds<usize>, n: usize) -> (usize, usize) {
    let start = match range.start_bound() {
        std::ops::Bound::Included(&x) => x,
        std::ops::Bound::Excluded(&x) => x + 1,
        std::ops::Bound::Unbounded => 0,
    };
    let end = match range.end_bound() {
        std::ops::Bound::Included(&x) => x + 1,
        std::ops::Bound::Excluded(&x) => x,
        std::ops::Bound::Unbounded => n,
    };
    (start, end.min(n))
}
}
pub mod iter_ext {
pub mod collect {
pub trait IterCollect<T>: Iterator<Item = T> + Sized {
    fn collect_vec(self) -> Vec<T> {
        self.collect()
    }
}

impl<T, I: Iterator<Item = T> + Sized> IterCollect<T> for I {}
}
pub mod iters {
use std::iter::{Chain, Enumerate, Rev, Skip, Zip};

pub trait Iters<'a>: 'a
where
    &'a Self: IntoIterator,
{
    fn enumerate(&'a self) -> Enumerate<<&'a Self as IntoIterator>::IntoIter>;
    fn rev(&'a self) -> Rev<<&'a Self as IntoIterator>::IntoIter>
    where
        <&'a Self as IntoIterator>::IntoIter: DoubleEndedIterator;
    fn skip(&'a self, n: usize) -> Skip<<&'a Self as IntoIterator>::IntoIter>;
    fn chain<V: 'a>(
        &'a self,
        chained: &'a V,
    ) -> Chain<<&'a Self as IntoIterator>::IntoIter, <&'a V as IntoIterator>::IntoIter>
    where
        &'a V: IntoIterator<Item = <&'a Self as IntoIterator>::Item>;
    fn zip<V: 'a>(
        &'a self,
        other: &'a V,
    ) -> Zip<<&'a Self as IntoIterator>::IntoIter, <&'a V as IntoIterator>::IntoIter>
    where
        &'a V: IntoIterator<Item = <&'a Self as IntoIterator>::Item>;
}

impl<'a, U: 'a> Iters<'a> for U
where
    &'a U: IntoIterator,
{
    fn enumerate(&'a self) -> Enumerate<<&'a Self as IntoIterator>::IntoIter> {
        self.into_iter().enumerate()
    }

    fn rev(&'a self) -> Rev<<&'a Self as IntoIterator>::IntoIter>
    where
        <&'a Self as IntoIterator>::IntoIter: DoubleEndedIterator,
    {
        self.into_iter().rev()
    }

    fn skip(&'a self, n: usize) -> Skip<<&'a Self as IntoIterator>::IntoIter> {
        self.into_iter().skip(n)
    }

    fn chain<V: 'a>(
        &'a self,
        chained: &'a V,
    ) -> Chain<<&'a Self as IntoIterator>::IntoIter, <&'a V as IntoIterator>::IntoIter>
    where
        &'a V: IntoIterator<Item = <&'a Self as IntoIterator>::Item>,
    {
        self.into_iter().chain(chained)
    }

    fn zip<V: 'a>(
        &'a self,
        other: &'a V,
    ) -> Zip<<&'a Self as IntoIterator>::IntoIter, <&'a V as IntoIterator>::IntoIter>
    where
        &'a V: IntoIterator<Item = <&'a Self as IntoIterator>::Item>,
    {
        self.into_iter().zip(other)
    }
}
}
}
pub mod min_max {
pub trait MinimMaxim<Rhs = Self>: PartialOrd + Sized {
    fn minim(&mut self, other: Rhs) -> bool;

    fn maxim(&mut self, other: Rhs) -> bool;
}

impl<T: PartialOrd> MinimMaxim for T {
    fn minim(&mut self, other: Self) -> bool {
        if other < *self {
            *self = other;
            true
        } else {
            false
        }
    }

    fn maxim(&mut self, other: Self) -> bool {
        if other > *self {
            *self = other;
            true
        } else {
            false
        }
    }
}

impl<T: PartialOrd> MinimMaxim<T> for Option<T> {
    fn minim(&mut self, other: T) -> bool {
        match self {
            None => {
                *self = Some(other);
                true
            }
            Some(v) => v.minim(other),
        }
    }

    fn maxim(&mut self, other: T) -> bool {
        match self {
            None => {
                *self = Some(other);
                true
            }
            Some(v) => v.maxim(other),
        }
    }
}
}
pub mod segment_tree {
use crate::collections::bounds::clamp;
use crate::misc::direction::Direction;
use crate::when;
use std::marker::PhantomData;
use std::ops::RangeBounds;

pub trait SegmentTreeNode {
    fn new(left: usize, right: usize) -> Self;
    fn join(&mut self, left_val: &Self, right_val: &Self, left: usize, mid: usize, right: usize);
    fn accumulate(&mut self, value: &Self, left: usize, right: usize);
    fn reset_delta(&mut self, left: usize, right: usize);
}

pub trait Pushable<T>: SegmentTreeNode {
    fn push(&mut self, delta: T, left: usize, right: usize);
}

impl<T: SegmentTreeNode> Pushable<&T> for T {
    fn push(&mut self, delta: &T, left: usize, right: usize) {
        self.accumulate(delta, left, right);
    }
}

pub trait QueryResult<Result>: SegmentTreeNode {
    fn empty_result() -> Result;
    fn result(&self) -> Result;
    fn join_results(
        left_res: Result,
        right_res: Result,
        left: usize,
        mid: usize,
        right: usize,
    ) -> Result;
}

impl<T: SegmentTreeNode + Clone> QueryResult<T> for T {
    fn empty_result() -> T {
        Self::new(0, 0)
    }

    fn result(&self) -> T {
        self.clone()
    }

    fn join_results(left_res: T, right_res: T, left: usize, mid: usize, right: usize) -> T {
        when! {
            left == mid => right_res,
            right == mid => left_res,
            else => {
                let mut res = Self::new(left, right);
                res.join(&left_res, &right_res, left, mid, right);
                res
            },
        }
    }
}

#[derive(Clone)]
pub struct SegmentTree<Node> {
    n: usize,
    nodes: Vec<Node>,
}

impl<Node: SegmentTreeNode> SegmentTree<Node> {
    pub fn new(n: usize) -> Self {
        Self::from_generator(n, |left| Node::new(left, left + 1))
    }

    pub fn from_array(arr: Vec<Node>) -> Self {
        let n = arr.len();
        let mut iter = arr.into_iter();
        Self::from_generator(n, |_| iter.next().unwrap())
    }

    pub fn from_generator<F>(n: usize, gen: F) -> Self
    where
        F: FnMut(usize) -> Node,
    {
        if n == 0 {
            panic!("Empty seg trees not allowed");
        }
        let mut res = Self {
            n,
            nodes: Vec::with_capacity(2 * n - 1),
        };
        res.init(gen);
        res
    }

    fn init<F>(&mut self, mut f: F)
    where
        F: FnMut(usize) -> Node,
    {
        self.init_impl(2 * self.n - 2, 0, self.n, &mut f);
    }

    fn init_impl<F>(&mut self, root: usize, left: usize, right: usize, f: &mut F)
    where
        F: FnMut(usize) -> Node,
    {
        if left + 1 == right {
            self.nodes.push(f(left));
        } else {
            let mid = left + ((right - left) >> 1);
            let left_child = root - 2 * (right - mid);
            let right_child = root - 1;
            self.init_impl(left_child, left, mid, f);
            self.init_impl(right_child, mid, right, f);
            let mut node = Node::new(left, right);
            node.join(
                &self.nodes[left_child],
                &self.nodes[right_child],
                left,
                mid,
                right,
            );
            self.nodes.push(node);
        }
    }

    pub fn point_query(&mut self, at: usize) -> &Node {
        assert!(at < self.n);
        self.do_point_query(2 * self.n - 2, 0, self.n, at)
    }

    fn do_point_query(&mut self, root: usize, left: usize, right: usize, at: usize) -> &Node {
        if left + 1 == right {
            &self.nodes[root]
        } else {
            let mid = (left + right) >> 1;
            self.push_down(root, left, mid, right);
            let left_child = root - 2 * (right - mid);
            let right_child = root - 1;
            if at < mid {
                self.do_point_query(left_child, left, mid, at)
            } else {
                self.do_point_query(right_child, mid, right, at)
            }
        }
    }

    pub fn point_update<T>(&mut self, at: usize, val: T)
    where
        Node: Pushable<T>,
    {
        assert!(at < self.n);
        self.do_point_update(2 * self.n - 2, 0, self.n, at, val);
    }

    fn do_point_update<T>(&mut self, root: usize, left: usize, right: usize, at: usize, val: T)
    where
        Node: Pushable<T>,
    {
        if left + 1 == right {
            self.nodes[root].push(val, left, right);
        } else {
            let mid = (left + right) >> 1;
            self.push_down(root, left, mid, right);
            let left_child = root - 2 * (right - mid);
            let right_child = root - 1;
            if at < mid {
                self.do_point_update(left_child, left, mid, at, val);
            } else {
                self.do_point_update(right_child, mid, right, at, val);
            }
            self.join(root, left, mid, right);
        }
    }

    pub fn point_through_update<'a, T>(&mut self, at: usize, val: &'a T)
    where
        Node: Pushable<&'a T>,
    {
        assert!(at < self.n);
        self.do_point_through_update(2 * self.n - 2, 0, self.n, at, val);
    }

    fn do_point_through_update<'a, T>(
        &mut self,
        root: usize,
        left: usize,
        right: usize,
        at: usize,
        val: &'a T,
    ) where
        Node: Pushable<&'a T>,
    {
        self.nodes[root].push(val, left, right);
        if left + 1 != right {
            let mid = (left + right) >> 1;
            self.push_down(root, left, mid, right);
            let left_child = root - 2 * (right - mid);
            let right_child = root - 1;
            if at < mid {
                self.do_point_update(left_child, left, mid, at, val);
            } else {
                self.do_point_update(right_child, mid, right, at, val);
            }
        }
    }

    pub fn point_operation<Args, Res>(
        &mut self,
        op: &mut dyn PointOperation<Node, Args, Res>,
        args: Args,
    ) -> Res {
        self.do_point_operation(op, 2 * self.n - 2, 0, self.n, args)
    }

    fn do_point_operation<Args, Res>(
        &mut self,
        op: &mut dyn PointOperation<Node, Args, Res>,
        root: usize,
        left: usize,
        right: usize,
        args: Args,
    ) -> Res {
        if left + 1 == right {
            op.adjust_leaf(&mut self.nodes[root], left, args)
        } else {
            let mid = (left + right) >> 1;
            self.push_down(root, left, mid, right);
            let left_child = root - 2 * (right - mid);
            let right_child = root - 1;
            let (l, r) = self.nodes.split_at_mut(root);
            let (l, m) = l.split_at_mut(right_child);
            let direction = op.select_branch(
                &mut r[0],
                &mut l[left_child],
                &mut m[0],
                &args,
                left,
                mid,
                right,
            );
            let res = match direction {
                Direction::Left => self.do_point_operation(op, left_child, left, mid, args),
                Direction::Right => self.do_point_operation(op, right_child, mid, right, args),
            };
            self.join(root, left, mid, right);
            res
        }
    }

    pub fn update<'a, T>(&mut self, range: impl RangeBounds<usize>, val: &'a T)
    where
        Node: Pushable<&'a T>,
    {
        let (from, to) = clamp(range, self.n);
        self.do_update(2 * self.n - 2, 0, self.n, from, to, val)
    }

    pub fn do_update<'a, T>(
        &mut self,
        root: usize,
        left: usize,
        right: usize,
        from: usize,
        to: usize,
        val: &'a T,
    ) where
        Node: Pushable<&'a T>,
    {
        when! {
            left >= to || right <= from => {},
            left >= from && right <= to => self.nodes[root].push(val, left, right),
            else => {
                let mid = (left + right) >> 1;
                self.push_down(root, left, mid, right);
                let left_child = root - 2 * (right - mid);
                let right_child = root - 1;
                self.do_update(left_child, left, mid, from, to, val);
                self.do_update(right_child, mid, right, from, to, val);
                self.join(root, left, mid, right);
            },
        }
    }

    pub fn operation<Args, Res>(
        &mut self,
        range: impl RangeBounds<usize>,
        op: &mut dyn Operation<Node, Args, Res>,
        args: &Args,
    ) -> Res {
        let (from, to) = clamp(range, self.n);
        self.do_operation(2 * self.n - 2, 0, self.n, from, to, op, args)
    }

    pub fn do_operation<Args, Res>(
        &mut self,
        root: usize,
        left: usize,
        right: usize,
        from: usize,
        to: usize,
        op: &mut dyn Operation<Node, Args, Res>,
        args: &Args,
    ) -> Res {
        when! {
            left >= to || right <= from => op.empty_result(left, right, args),
            left >= from && right <= to => op.process_result(&mut self.nodes[root], args),
            else => {
                let mid = (left + right) >> 1;
                self.push_down(root, left, mid, right);
                let left_child = root - 2 * (right - mid);
                let right_child = root - 1;
                let left_result = self.do_operation(left_child, left, mid, from, to, op, args);
                let right_result = self.do_operation(right_child, mid, right, from, to, op, args);
                self.join(root, left, mid, right);
                op.join_results(left_result, right_result, args)
            },
        }
    }

    pub fn binary_search<Res>(
        &mut self,
        wh: impl FnMut(&Node, &Node) -> Direction,
        calc: impl FnMut(&Node, usize) -> Res,
    ) -> Res {
        self.do_binary_search(2 * self.n - 2, 0, self.n, wh, calc)
    }

    fn do_binary_search<Res>(
        &mut self,
        root: usize,
        left: usize,
        right: usize,
        mut wh: impl FnMut(&Node, &Node) -> Direction,
        mut calc: impl FnMut(&Node, usize) -> Res,
    ) -> Res {
        if left + 1 == right {
            calc(&self.nodes[root], left)
        } else {
            let mid = (left + right) >> 1;
            self.push_down(root, left, mid, right);
            let left_child = root - 2 * (right - mid);
            let right_child = root - 1;
            let direction = wh(&self.nodes[left_child], &self.nodes[right_child]);
            match direction {
                Direction::Left => self.do_binary_search(left_child, left, mid, wh, calc),
                Direction::Right => self.do_binary_search(right_child, mid, right, wh, calc),
            }
        }
    }

    fn join(&mut self, root: usize, left: usize, mid: usize, right: usize) {
        let left_child = root - 2 * (right - mid);
        let right_child = root - 1;
        let (left_node, right_node) = self.nodes.split_at_mut(root);
        right_node[0].join(
            &left_node[left_child],
            &left_node[right_child],
            left,
            mid,
            right,
        );
    }

    fn do_push_down(&mut self, parent: usize, to: usize, left: usize, right: usize) {
        let (left_nodes, right_nodes) = self.nodes.split_at_mut(parent);
        left_nodes[to].accumulate(&right_nodes[0], left, right);
    }

    fn push_down(&mut self, root: usize, left: usize, mid: usize, right: usize) {
        self.do_push_down(root, root - 2 * (right - mid), left, mid);
        self.do_push_down(root, root - 1, mid, right);
        self.nodes[root].reset_delta(left, right);
    }
}

impl<Node: SegmentTreeNode + Clone> SegmentTree<Node> {
    pub fn query<T>(&mut self, range: impl RangeBounds<usize>) -> T
    where
        Node: QueryResult<T>,
    {
        let (from, to) = clamp(range, self.n);
        if from >= to {
            Node::empty_result()
        } else {
            self.do_query(2 * self.n - 2, 0, self.n, from, to)
        }
    }

    fn do_query<T>(&mut self, root: usize, left: usize, right: usize, from: usize, to: usize) -> T
    where
        Node: QueryResult<T>,
    {
        if left >= from && right <= to {
            self.nodes[root].result()
        } else {
            let mid = (left + right) >> 1;
            self.push_down(root, left, mid, right);
            let left_child = root - 2 * (right - mid);
            let right_child = root - 1;
            when! {
                to <= mid => self.do_query(left_child, left, mid, from, to),
                from >= mid => self.do_query(right_child, mid, right, from, to),
                else => {
                    let left_result = self.do_query(left_child, left, mid, from, to);
                    let right_result = self.do_query(right_child, mid, right, from, to);
                    Node::join_results(left_result, right_result, left, mid, right)
                },
            }
        }
    }
}

pub trait PointOperation<Node: SegmentTreeNode, Args, Res = Node> {
    fn adjust_leaf(&mut self, leaf: &mut Node, at: usize, args: Args) -> Res;
    fn select_branch(
        &mut self,
        root: &mut Node,
        left_child: &mut Node,
        right_child: &mut Node,
        args: &Args,
        left: usize,
        mid: usize,
        right: usize,
    ) -> Direction;
}

pub struct PointOperationClosure<'s, Node: SegmentTreeNode, Args, Res = Node> {
    adjust_leaf: Box<dyn FnMut(&mut Node, usize, Args) -> Res + 's>,
    select_branch: Box<
        dyn FnMut(&mut Node, &mut Node, &mut Node, &Args, usize, usize, usize) -> Direction + 's,
    >,
    phantom_node: PhantomData<Node>,
    phantom_args: PhantomData<Args>,
    phantom_res: PhantomData<Res>,
}

impl<'s, Node: SegmentTreeNode, Args, Res> PointOperationClosure<'s, Node, Args, Res> {
    pub fn new<F1, F2>(adjust_leaf: F1, select_branch: F2) -> Self
    where
        F1: FnMut(&mut Node, usize, Args) -> Res + 's,
        F2: FnMut(&mut Node, &mut Node, &mut Node, &Args, usize, usize, usize) -> Direction + 's,
    {
        Self {
            adjust_leaf: Box::new(adjust_leaf),
            select_branch: Box::new(select_branch),
            phantom_node: Default::default(),
            phantom_args: Default::default(),
            phantom_res: Default::default(),
        }
    }
}

impl<'s, Node: SegmentTreeNode, Args, Res> PointOperation<Node, Args, Res>
    for PointOperationClosure<'s, Node, Args, Res>
{
    fn adjust_leaf(&mut self, leaf: &mut Node, at: usize, args: Args) -> Res {
        (self.adjust_leaf)(leaf, at, args)
    }

    fn select_branch(
        &mut self,
        root: &mut Node,
        left_child: &mut Node,
        right_child: &mut Node,
        args: &Args,
        left: usize,
        mid: usize,
        right: usize,
    ) -> Direction {
        (self.select_branch)(root, left_child, right_child, args, left, mid, right)
    }
}

pub trait Operation<Node: SegmentTreeNode, Args, Res = Node> {
    fn process_result(&mut self, node: &mut Node, args: &Args) -> Res;
    fn join_results(&mut self, left_res: Res, right_res: Res, args: &Args) -> Res;
    fn empty_result(&mut self, left: usize, right: usize, args: &Args) -> Res;
}

pub struct OperationClosure<'s, Node: SegmentTreeNode, Args, Res = Node> {
    process_result: Box<dyn FnMut(&mut Node, &Args) -> Res + 's>,
    join_results: Box<dyn FnMut(Res, Res, &Args) -> Res + 's>,
    empty_result: Box<dyn FnMut(usize, usize, &Args) -> Res + 's>,
    phantom_node: PhantomData<Node>,
    phantom_args: PhantomData<Args>,
    phantom_res: PhantomData<Res>,
}

impl<'s, Node: SegmentTreeNode, Args, Res> OperationClosure<'s, Node, Args, Res> {
    pub fn new<F1, F2, F3>(process_result: F1, join_results: F2, empty_result: F3) -> Self
    where
        F1: FnMut(&mut Node, &Args) -> Res + 's,
        F2: FnMut(Res, Res, &Args) -> Res + 's,
        F3: FnMut(usize, usize, &Args) -> Res + 's,
    {
        Self {
            process_result: Box::new(process_result),
            join_results: Box::new(join_results),
            empty_result: Box::new(empty_result),
            phantom_node: Default::default(),
            phantom_args: Default::default(),
            phantom_res: Default::default(),
        }
    }
}

impl<'s, Node: SegmentTreeNode, Args, Res> Operation<Node, Args, Res>
    for OperationClosure<'s, Node, Args, Res>
{
    fn process_result(&mut self, node: &mut Node, args: &Args) -> Res {
        (self.process_result)(node, args)
    }

    fn join_results(&mut self, left_res: Res, right_res: Res, args: &Args) -> Res {
        (self.join_results)(left_res, right_res, args)
    }

    fn empty_result(&mut self, left: usize, right: usize, args: &Args) -> Res {
        (self.empty_result)(left, right, args)
    }
}
}
pub mod vec_ext {
pub mod default {
pub fn default_vec<T: Default>(len: usize) -> Vec<T> {
    let mut v = Vec::with_capacity(len);
    for _ in 0..len {
        v.push(T::default());
    }
    v
}
}
}
}
pub mod io {
pub mod input {
use crate::collections::vec_ext::default::default_vec;
use std::io::Read;

pub struct Input<'s> {
    input: &'s mut dyn Read,
    buf: Vec<u8>,
    at: usize,
    buf_read: usize,
}

macro_rules! read_impl {
    ($t: ty, $read_name: ident, $read_vec_name: ident) => {
        pub fn $read_name(&mut self) -> $t {
            self.read()
        }

        pub fn $read_vec_name(&mut self, len: usize) -> Vec<$t> {
            self.read_vec(len)
        }
    };

    ($t: ty, $read_name: ident, $read_vec_name: ident, $read_pair_vec_name: ident) => {
        read_impl!($t, $read_name, $read_vec_name);

        pub fn $read_pair_vec_name(&mut self, len: usize) -> Vec<($t, $t)> {
            self.read_vec(len)
        }
    };
}

impl<'s> Input<'s> {
    const DEFAULT_BUF_SIZE: usize = 4096;

    pub fn new(input: &'s mut dyn Read) -> Self {
        Self {
            input,
            buf: default_vec(Self::DEFAULT_BUF_SIZE),
            at: 0,
            buf_read: 0,
        }
    }

    pub fn new_with_size(input: &'s mut dyn Read, buf_size: usize) -> Self {
        Self {
            input,
            buf: default_vec(buf_size),
            at: 0,
            buf_read: 0,
        }
    }

    pub fn get(&mut self) -> Option<u8> {
        if self.refill_buffer() {
            let res = self.buf[self.at];
            self.at += 1;
            if res == b'\r' {
                if self.refill_buffer() && self.buf[self.at] == b'\n' {
                    self.at += 1;
                }
                return Some(b'\n');
            }
            Some(res)
        } else {
            None
        }
    }

    pub fn peek(&mut self) -> Option<u8> {
        if self.refill_buffer() {
            let res = self.buf[self.at];
            Some(if res == b'\r' { b'\n' } else { res })
        } else {
            None
        }
    }

    pub fn skip_whitespace(&mut self) {
        while let Some(b) = self.peek() {
            if !char::from(b).is_whitespace() {
                return;
            }
            self.get();
        }
    }

    pub fn next_token(&mut self) -> Option<Vec<u8>> {
        self.skip_whitespace();
        let mut res = Vec::new();
        while let Some(c) = self.get() {
            if char::from(c).is_whitespace() {
                break;
            }
            res.push(c);
        }
        if res.is_empty() {
            None
        } else {
            Some(res)
        }
    }

    //noinspection RsSelfConvention
    pub fn is_exhausted(&mut self) -> bool {
        self.peek().is_none()
    }

    pub fn read<T: Readable>(&mut self) -> T {
        T::read(self)
    }

    pub fn read_vec<T: Readable>(&mut self, size: usize) -> Vec<T> {
        let mut res = Vec::with_capacity(size);
        for _ in 0..size {
            res.push(self.read());
        }
        res
    }

    pub fn read_char(&mut self) -> char {
        self.skip_whitespace();
        self.get().unwrap().into()
    }

    read_impl!(u32, read_unsigned, read_unsigned_vec);
    read_impl!(u64, read_u64, read_u64_vec);
    read_impl!(usize, read_size, read_size_vec, read_size_pair_vec);
    read_impl!(i32, read_int, read_int_vec, read_int_pair_vec);
    read_impl!(i64, read_long, read_long_vec, read_long_pair_vec);
    read_impl!(i128, read_i128, read_i128_vec);

    fn refill_buffer(&mut self) -> bool {
        if self.at == self.buf_read {
            self.at = 0;
            self.buf_read = self.input.read(&mut self.buf).unwrap();
            self.buf_read != 0
        } else {
            true
        }
    }
}

pub trait Readable {
    fn read(input: &mut Input) -> Self;
}

impl Readable for char {
    fn read(input: &mut Input) -> Self {
        input.read_char()
    }
}

impl<T: Readable> Readable for Vec<T> {
    fn read(input: &mut Input) -> Self {
        let size = input.read();
        input.read_vec(size)
    }
}

macro_rules! read_integer {
    ($($t:ident)+) => {$(
        impl Readable for $t {
            fn read(input: &mut Input) -> Self {
                input.skip_whitespace();
                let mut c = input.get().unwrap();
                let sgn = match c {
                    b'-' => {
                        c = input.get().unwrap();
                        true
                    }
                    b'+' => {
                        c = input.get().unwrap();
                        false
                    }
                    _ => false,
                };
                let mut res = 0;
                loop {
                    assert!(c.is_ascii_digit());
                    res *= 10;
                    let d = (c - b'0') as $t;
                    if sgn {
                        res -= d;
                    } else {
                        res += d;
                    }
                    match input.get() {
                        None => break,
                        Some(ch) => {
                            if ch.is_ascii_whitespace() {
                                break;
                            } else {
                                c = ch;
                            }
                        }
                    }
                }
                res
            }
        }
    )+};
}

read_integer!(i8 i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usize);

macro_rules! tuple_readable {
    ($($name:ident)+) => {
        impl<$($name: Readable), +> Readable for ($($name,)+) {
            fn read(input: &mut Input) -> Self {
                ($($name::read(input),)+)
            }
        }
    }
}

tuple_readable! {T}
tuple_readable! {T U}
tuple_readable! {T U V}
tuple_readable! {T U V X}
tuple_readable! {T U V X Y}
tuple_readable! {T U V X Y Z}
tuple_readable! {T U V X Y Z A}
tuple_readable! {T U V X Y Z A B}
tuple_readable! {T U V X Y Z A B C}
tuple_readable! {T U V X Y Z A B C D}
tuple_readable! {T U V X Y Z A B C D E}
tuple_readable! {T U V X Y Z A B C D E F}

impl Read for Input<'_> {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        if self.at == self.buf_read {
            self.input.read(buf)
        } else {
            let mut i = 0;
            while i < buf.len() && self.at < self.buf_read {
                buf[i] = self.buf[self.at];
                i += 1;
                self.at += 1;
            }
            Ok(i)
        }
    }
}
}
pub mod output {
use crate::collections::vec_ext::default::default_vec;
use std::io::{stderr, Stderr, Write};

#[derive(Copy, Clone)]
pub enum BoolOutput {
    YesNo,
    YesNoCaps,
    PossibleImpossible,
    Custom(&'static str, &'static str),
}

impl BoolOutput {
    pub fn output(&self, output: &mut Output, val: bool) {
        (if val { self.yes() } else { self.no() }).write(output);
    }

    fn yes(&self) -> &str {
        match self {
            BoolOutput::YesNo => "Yes",
            BoolOutput::YesNoCaps => "YES",
            BoolOutput::PossibleImpossible => "Possible",
            BoolOutput::Custom(yes, _) => yes,
        }
    }

    fn no(&self) -> &str {
        match self {
            BoolOutput::YesNo => "No",
            BoolOutput::YesNoCaps => "NO",
            BoolOutput::PossibleImpossible => "Impossible",
            BoolOutput::Custom(_, no) => no,
        }
    }
}

pub struct Output<'s> {
    output: &'s mut dyn Write,
    buf: Vec<u8>,
    at: usize,
    auto_flush: bool,
    bool_output: BoolOutput,
}

impl<'s> Output<'s> {
    const DEFAULT_BUF_SIZE: usize = 4096;

    pub fn new(output: &'s mut dyn Write) -> Self {
        Self {
            output,
            buf: default_vec(Self::DEFAULT_BUF_SIZE),
            at: 0,
            auto_flush: false,
            bool_output: BoolOutput::YesNoCaps,
        }
    }

    pub fn new_with_auto_flush(output: &'s mut dyn Write) -> Self {
        Self {
            output,
            buf: default_vec(Self::DEFAULT_BUF_SIZE),
            at: 0,
            auto_flush: true,
            bool_output: BoolOutput::YesNoCaps,
        }
    }

    pub fn flush(&mut self) {
        if self.at != 0 {
            self.output.write_all(&self.buf[..self.at]).unwrap();
            self.output.flush().unwrap();
            self.at = 0;
        }
    }

    pub fn print<T: Writable>(&mut self, s: T) {
        s.write(self);
        self.maybe_flush();
    }

    pub fn print_line<T: Writable>(&mut self, s: T) {
        self.print(s);
        self.put(b'\n');
        self.maybe_flush();
    }

    pub fn put(&mut self, b: u8) {
        self.buf[self.at] = b;
        self.at += 1;
        if self.at == self.buf.len() {
            self.flush();
        }
    }

    pub fn maybe_flush(&mut self) {
        if self.auto_flush {
            self.flush();
        }
    }

    pub fn print_per_line<T: Writable>(&mut self, arg: &[T]) {
        for i in arg {
            i.write(self);
            self.put(b'\n');
        }
    }

    pub fn print_iter<T: Writable, I: Iterator<Item = T>>(&mut self, iter: I) {
        let mut first = true;
        for e in iter {
            if first {
                first = false;
            } else {
                self.put(b' ');
            }
            e.write(self);
        }
    }

    pub fn print_iter_ref<'a, T: 'a + Writable, I: Iterator<Item = &'a T>>(&mut self, iter: I) {
        let mut first = true;
        for e in iter {
            if first {
                first = false;
            } else {
                self.put(b' ');
            }
            e.write(self);
        }
    }

    pub fn set_bool_output(&mut self, bool_output: BoolOutput) {
        self.bool_output = bool_output;
    }
}

impl Write for Output<'_> {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        let mut start = 0usize;
        let mut rem = buf.len();
        while rem > 0 {
            let len = (self.buf.len() - self.at).min(rem);
            self.buf[self.at..self.at + len].copy_from_slice(&buf[start..start + len]);
            self.at += len;
            if self.at == self.buf.len() {
                self.flush();
            }
            start += len;
            rem -= len;
        }
        self.maybe_flush();
        Ok(buf.len())
    }

    fn flush(&mut self) -> std::io::Result<()> {
        self.flush();
        Ok(())
    }
}

pub trait Writable {
    fn write(&self, output: &mut Output);
}

impl Writable for &str {
    fn write(&self, output: &mut Output) {
        output.write_all(self.as_bytes()).unwrap();
    }
}

impl Writable for String {
    fn write(&self, output: &mut Output) {
        output.write_all(self.as_bytes()).unwrap();
    }
}

impl Writable for char {
    fn write(&self, output: &mut Output) {
        output.put(*self as u8);
    }
}

impl<T: Writable> Writable for [T] {
    fn write(&self, output: &mut Output) {
        output.print_iter_ref(self.iter());
    }
}

impl<T: Writable, const N: usize> Writable for [T; N] {
    fn write(&self, output: &mut Output) {
        output.print_iter_ref(self.iter());
    }
}

impl<T: Writable> Writable for &T {
    fn write(&self, output: &mut Output) {
        T::write(self, output)
    }
}

impl<T: Writable> Writable for Vec<T> {
    fn write(&self, output: &mut Output) {
        self.as_slice().write(output);
    }
}

impl Writable for () {
    fn write(&self, _output: &mut Output) {}
}

macro_rules! write_to_string {
    ($($t:ident)+) => {$(
        impl Writable for $t {
            fn write(&self, output: &mut Output) {
                self.to_string().write(output);
            }
        }
    )+};
}

write_to_string!(u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize);

macro_rules! tuple_writable {
    ($name0:ident $($name:ident: $id:tt )*) => {
        impl<$name0: Writable, $($name: Writable,)*> Writable for ($name0, $($name,)*) {
            fn write(&self, out: &mut Output) {
                self.0.write(out);
                $(
                out.put(b' ');
                self.$id.write(out);
                )*
            }
        }
    }
}

tuple_writable! {T}
tuple_writable! {T U:1}
tuple_writable! {T U:1 V:2}
tuple_writable! {T U:1 V:2 X:3}
tuple_writable! {T U:1 V:2 X:3 Y:4}
tuple_writable! {T U:1 V:2 X:3 Y:4 Z:5}
tuple_writable! {T U:1 V:2 X:3 Y:4 Z:5 A:6}
tuple_writable! {T U:1 V:2 X:3 Y:4 Z:5 A:6 B:7}

impl<T: Writable> Writable for Option<T> {
    fn write(&self, output: &mut Output) {
        match self {
            None => (-1).write(output),
            Some(t) => t.write(output),
        }
    }
}

impl Writable for bool {
    fn write(&self, output: &mut Output) {
        let bool_output = output.bool_output;
        bool_output.output(output, *self)
    }
}

static mut ERR: Option<Stderr> = None;
pub fn err() -> Output<'static> {
    unsafe {
        if ERR.is_none() {
            ERR = Some(stderr());
        }
        Output::new_with_auto_flush(ERR.as_mut().unwrap())
    }
}
}
}
pub mod misc {
pub mod direction {
#[derive(Copy, Clone)]
pub enum Direction {
    Left,
    Right,
}
}
pub mod when {
#[macro_export]
macro_rules! when {
    {$($cond: expr => $then: expr,)*} => {
        match () {
            $(_ if $cond => $then,)*
            _ => unreachable!(),
        }
    };
    {$($cond: expr => $then: expr,)* else $(=>)? $else: expr,} => {
        match () {
            $(_ if $cond => $then,)*
            _ => $else,
        }
    };
}
}
}
fn main() {
    let mut sin = std::io::stdin();
    let input = if false {
        io::input::Input::new_with_size(&mut sin, 1)
    } else {
        io::input::Input::new(&mut sin)
    };

    let mut stdout = std::io::stdout();
    let output = if false {
        io::output::Output::new_with_auto_flush(&mut stdout)
    } else {
        io::output::Output::new(&mut stdout)
    };

    solution::run(input, output);
}


这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 2108kb

input:

4
3
42 42 42
4
1 2 2 1
5
1 100 1 100 1
6
1 2 3 4 5 6

output:

0
0
99
2

result:

ok 4 number(s): "0 0 99 2"

Test #2:

score: 0
Accepted
time: 9ms
memory: 2092kb

input:

19530
6
2 3 3 3 4 3
6
2 4 4 2 5 5
6
2 5 2 5 1 3
6
4 4 1 2 4 1
5
5 4 5 3 3
5
2 2 1 5 1
6
3 1 2 4 2 3
5
5 5 4 4 5
6
5 3 3 1 4 2
6
3 3 2 4 2 4
6
1 2 4 5 2 5
5
3 4 5 5 3
6
4 3 3 3 4 3
6
1 5 1 2 3 1
5
5 5 2 1 4
6
1 2 5 3 5 2
6
4 5 2 4 5 2
5
2 4 2 4 1
4
2 3 3 3
6
1 2 2 1 4 5
6
3 2 1 3 3 2
6
2 1 1 2 5 3
6
...

output:

1
2
3
3
1
1
2
0
3
2
3
1
1
2
1
2
3
2
0
1
1
2
0
3
2
2
3
2
2
2
3
3
2
2
1
2
2
2
2
2
2
1
2
1
2
1
2
2
2
1
1
2
2
1
2
2
1
1
1
2
1
2
2
1
2
2
3
2
2
1
1
2
1
2
3
2
0
2
1
1
2
1
1
2
2
2
1
3
2
1
2
3
2
1
1
2
2
3
1
1
1
2
2
1
1
1
1
2
2
2
2
2
3
2
1
2
0
1
1
1
1
1
1
2
2
2
2
2
2
1
2
1
2
4
1
1
1
3
2
2
2
1
2
1
2
1
2
2
1
3
...

result:

ok 19530 numbers

Test #3:

score: 0
Accepted
time: 81ms
memory: 12056kb

input:

1
199996
95228303 201285494 63848235 748936712 940169142 379639162 189291770 224201078 335564466 792345215 948056869 35198826 312793561 194588099 297198853 665606109 163797196 584404459 996890415 458867609 331820116 713293915 858136035 520976262 519894660 918315819 660535535 639896052 141007070 1151...

output:

999986012

result:

ok 1 number(s): "999986012"

Test #4:

score: 0
Accepted
time: 81ms
memory: 12092kb

input:

1
199998
527515482 831759304 786398217 142876272 541300660 926936654 117644051 72438862 579434512 298922247 726038416 333034763 721144032 811978092 585764800 216757507 702854739 157207076 124829370 951519274 884953479 246664247 437913730 867100723 186107370 511591980 673723365 46011051 452152759 270...

output:

999982414

result:

ok 1 number(s): "999982414"

Test #5:

score: 0
Accepted
time: 83ms
memory: 12044kb

input:

1
199996
664835369 535862041 172511710 536815834 363770542 769201437 45996332 625709355 118271849 100466570 798987255 630870699 908156139 60771864 242926966 399312687 905475794 730009693 916331839 370542011 733054133 853663508 722724134 623290601 852320082 809900849 350474706 862191469 468331156 761...

output:

999985829

result:

ok 1 number(s): "999985829"

Test #6:

score: 0
Accepted
time: 82ms
memory: 12064kb

input:

1
199998
728526329 166335849 895061692 562159176 891273133 980062441 974348614 473947140 67174604 607043603 282001509 560110415 316506610 973129149 531492913 655496794 444533336 597779601 44270795 789564748 917591276 755630061 302501830 969415063 854969281 108209717 953597119 604742957 484509554 621...

output:

999969572

result:

ok 1 number(s): "999969572"

Test #7:

score: 0
Accepted
time: 82ms
memory: 12036kb

input:

1
199998
865846216 165405878 912578966 251066028 787371942 822327224 271297114 322184924 311044650 408587926 986354129 857946351 798486009 926955630 525091568 543084682 647154390 170582217 835773264 577183705 470724637 952563905 660941162 315539524 521181991 406518586 335381168 10857956 500687951 11...

output:

999964695

result:

ok 1 number(s): "999964695"

Test #8:

score: 0
Accepted
time: 75ms
memory: 9340kb

input:

2
53064
626000898 221967145 345169505 82068734 702234813 12584781 696818177 437027007 675473634 401025770 489012528 838016710 229255283 146265714 58851900 50317524 118508121 443178663 688346675 26420200 243286360 900568139 507939681 714342908 406102977 701868009 826713254 705530551 980176160 4536422...

output:

999949128
999983244

result:

ok 2 number(s): "999949128 999983244"

Test #9:

score: 0
Accepted
time: 78ms
memory: 7636kb

input:

2
86135
961507416 239484419 739109067 683200252 618128524 645969771 881492450 680897053 477017958 105378389 491881172 246367180 773016348 434831661 610003299 884342359 986278030 866084911 812402121 210957343 850285621 553974762 854064143 85588326 704411846 378619350 232828254 353112728 840052815 818...

output:

999989368
999979159

result:

ok 2 number(s): "999989368 999979159"

Test #10:

score: 0
Accepted
time: 70ms
memory: 7600kb

input:

2
114819
665610152 257001693 764452408 210702842 755360600 574322052 434762943 924767099 278562281 588392644 789717109 728346580 95439048 723397608 792558479 791996121 854047938 362620087 600021077 764090705 383655953 133752457 200188604 383204817 666284226 686774472 975379743 369291125 331333250 84...

output:

999957148
999925013

result:

ok 2 number(s): "999957148 999925013"

Test #11:

score: 0
Accepted
time: 79ms
memory: 9332kb

input:

2
51745
296083961 979551675 158391969 33172724 671254311 502674333 283000727 168637145 80106605 661341483 718956825 505293270 639200113 11963554 48742585 920988247 426850554 154122555 19043814 22256775 580589798 418562861 914909286 754450236 964593095 994929594 86527451 680436815 822613686 213225598...

output:

999935991
999977026

result:

ok 2 number(s): "999935991 999977026"

Test #12:

score: 0
Accepted
time: 83ms
memory: 11500kb

input:

2
190655
295153990 365665168 478702602 929271535 513519094 431026614 131238512 412507191 881650930 439323030 721825469 618676449 961622815 5562209 231297765 828642010 294620462 282061512 438066552 206793918 482556350 998340557 966066456 420662947 557869256 671680935 829078940 696615212 682490341 652...

output:

999986925
999410177

result:

ok 2 number(s): "999986925 999410177"

Test #13:

score: 0
Accepted
time: 74ms
memory: 10376kb

input:

3
509
945103490 925258646 551206125 526700079 667773410 95427191 362812642 886786434 870232904 495179381 81906730 281227871 253895133 37646925 42217132 359205845 471862629 935227244 26261416 979759614 224254713 556220075 695518823 269277086 725271797 910519987 326988819 137416508 605439760 724706756...

output:

992734363
999989683
999826782

result:

ok 3 number(s): "992734363 999989683 999826782"

Test #14:

score: 0
Accepted
time: 71ms
memory: 7316kb

input:

4
25729
148578218 399443910 770570125 469317733 578441446 955615870 169612 119026676 783745328 338090837 820285414 826697750 460553174 534868797 912339208 373829181 515004940 372385878 645972324 522563583 864375197 101633822 580422775 216552232 275859942 529623463 963747478 462104612 760212230 83453...

output:

999888931
999891034
999960265
999881981

result:

ok 4 number(s): "999888931 999891034 999960265 999881981"

Test #15:

score: 0
Accepted
time: 75ms
memory: 6156kb

input:

5
7824
952714403 309407462 270862057 651390285 253451805 777116303 367820449 72311274 520646017 22906468 694467658 957088351 658924243 170505277 275795733 168411563 718510340 648621523 115839743 836093830 844185312 596601172 707832668 9796117 805887036 495111155 245140536 427121484 204604275 6740390...

output:

998412528
999965938
999453971
999964102
999954744

result:

ok 5 number(s): "998412528 999965938 999453971 999964102 999954744"

Test #16:

score: 0
Accepted
time: 62ms
memory: 5708kb

input:

6
7149
258310216 72406381 429371832 256320449 890499482 985210442 729473441 776830124 561964011 562237566 748590818 77946979 650009712 177762285 453221967 769667510 314834233 119181320 849281660 955333020 612779569 567709323 670103366 82150607 321442125 733209168 462626958 869644823 848320244 706702...

output:

999480642
999875981
999949063
997982749
999914475
999892960

result:

ok 6 numbers

Test #17:

score: 0
Accepted
time: 72ms
memory: 7248kb

input:

7
16819
578983413 502320670 554156386 667446173 234004214 18039387 959385303 395988845 135040182 876529775 496969717 203143074 6099910 32999662 115791971 981046945 417490189 157436781 66480727 628957967 353957051 109072249 284785251 852805803 884841312 424503504 534685369 727634172 946504457 2615944...

output:

999790819
999967769
999353168
999916205
999029434
999468671
999517643

result:

ok 7 numbers

Test #18:

score: 0
Accepted
time: 68ms
memory: 6332kb

input:

8
29021
616738706 483396102 75796643 146361498 306605334 510536702 598609899 707842799 668032243 989621382 756276437 908066463 317810067 461916433 278663435 10766349 834188123 103999508 645136364 845237487 843008426 561048823 974104064 667877236 796445467 904758625 196882683 186306199 953584113 4257...

output:

999918412
999570747
999973927
999050677
999539352
999916183
999887707
998670357

result:

ok 8 numbers

Test #19:

score: 0
Accepted
time: 64ms
memory: 3812kb

input:

9
37136
486264746 557776042 100187980 300203989 398124589 432634734 575612707 164567419 408644119 645846287 810033015 897587763 218106311 649908854 309075218 142343244 920179925 956282053 410146850 503315673 132279686 505467742 155945868 126918233 864766464 371163903 131140649 350606443 429219755 22...

output:

999799709
999929058
999948989
999811617
999786771
999526295
999805610
997448953
999646655

result:

ok 9 numbers

Test #20:

score: 0
Accepted
time: 67ms
memory: 5220kb

input:

10
5543
167330214 188915787 22596977 283315922 625320639 228617645 282545327 230150386 552702835 716228254 474861297 817414746 774990558 855963275 14435565 876904270 168862381 704047836 629575366 451396215 56903186 809266075 332272060 232038554 741712502 479043833 601664005 176505178 453922701 29541...

output:

998232492
999430106
999686964
998491202
999850116
999350387
999769603
999914606
999874299
999833527

result:

ok 10 numbers

Test #21:

score: 0
Accepted
time: 54ms
memory: 2644kb

input:

100
2336
727549620 741730767 408911556 945446245 572038064 685609097 128205004 829936008 281081951 754715688 816339011 33520063 908787051 515313288 551649559 542879422 387539530 197674304 87720945 641686985 185882310 676226017 871051739 957002232 2277122 142180596 415788089 732362363 798930745 37640...

output:

998110791
979981535
975760894
998275283
998571629
999239016
998749377
997462569
866294046
999621358
987378829
997755336
995794597
999517959
997306324
997251487
969392768
979661575
998803233
971497444
998386112
898283347
994848399
997540982
987569738
999424136
997032282
833052200
994422667
994107886
...

result:

ok 100 numbers

Test #22:

score: 0
Accepted
time: 53ms
memory: 2396kb

input:

101
92
748815764 52836497 239211746 926328503 457005514 362549910 847956435 311908451 715487690 113473456 856164917 682059029 279376747 56020931 292111798 527630987 803997910 307425826 9505886 383939448 168148606 58064748 589134789 12889055 540816503 367442046 936570185 456635272 900824079 584704020...

output:

916822617
999587143
986653688
997295870
998982413
998596567
999282421
987902946
975064392
998868906
998723758
978684900
998987354
999269581
998302796
998104715
989826461
347664379
988875623
967250809
997267187
998546035
997483539
998336100
996311521
905418064
995887723
998942854
998597226
998726772
...

result:

ok 101 numbers

Test #23:

score: 0
Accepted
time: 54ms
memory: 2764kb

input:

102
8381
106826855 901573467 690684358 859805711 301927525 939605790 47767658 339823203 42011683 637340573 45265590 633374791 150326888 967715247 331285955 410321516 232163414 672292080 876963879 70136611 466416122 221267346 97129916 865823481 950499785 435745298 114339799 56643928 806249445 8722450...

output:

999750079
995835241
998581282
996346209
828301082
994637497
994472892
992393622
996899447
998993629
998837122
997431542
998665181
984069187
997809826
991765301
996506551
992462207
990881658
995946839
985781242
996598496
999152100
996999899
958901029
998651232
995931369
994743672
999028005
999440843
...

result:

ok 102 numbers

Test #24:

score: 0
Accepted
time: 49ms
memory: 2600kb

input:

103
1976
195338968 750007505 631202128 536272431 326222435 488659575 300595205 749211547 418516229 449941349 618776615 581069333 348351402 766344703 385241341 788304783 40110980 75021017 267091908 243363715 189836392 886403485 895863167 238590230 303516631 772044326 286092705 659198650 897547665 424...

output:

998742295
983171062
984523490
997738224
999060348
998048981
979831642
998244494
933788691
997958580
999184410
992659923
995134165
837920171
940135087
999240010
997046964
993366641
998924388
996196978
999228781
998710632
997024736
996106983
996837300
999193037
986012096
996680890
999375996
998051166
...

result:

ok 103 numbers

Test #25:

score: 0
Accepted
time: 54ms
memory: 2440kb

input:

104
3135
440734432 34002325 770617337 49275570 224518783 556334499 677749773 609757302 518180618 677807366 85440706 392551070 275032379 286532239 713042372 407929882 568045448 464047205 430376724 116936241 265611637 294499073 190244187 802691745 724716145 441912554 880744075 996479249 668290318 3661...

output:

996890258
999371416
987976840
996728872
995039393
988180910
996234785
948511896
989142827
989700696
994935642
997577394
999059116
996183678
996840127
995582627
997613196
980478893
978103305
997422333
997632259
998422012
999190659
999145093
996617630
995379574
993813734
999188765
999249743
998563026
...

result:

ok 104 numbers

Test #26:

score: 0
Accepted
time: 53ms
memory: 2480kb

input:

105
1344
341398731 140924924 255345283 517106502 606288000 390932958 291452596 663209190 179746663 773187226 78687347 892855845 637779960 407152564 766102586 661002502 838728098 454101507 981256278 324538759 773301926 301866859 382420672 966328624 807322208 726814614 322337818 435608448 512775190 61...

output:

998414440
992390370
968852988
997071091
998769069
947642796
999027454
999049587
996068191
996267770
995843938
999391321
997806025
995822076
998635946
863254155
997351774
999438663
998380515
997114291
995617488
995356674
995161399
999147222
997699362
924654080
997453774
996913555
994518257
994080113
...

result:

ok 105 numbers

Test #27:

score: 0
Accepted
time: 41ms
memory: 2256kb

input:

1000
1284
127329611 310153641 979675449 435569180 557896712 997251070 331469719 301259897 408998506 447544853 650045138 608994345 756769582 552399397 283995477 539927507 794577363 940833596 91286896 16474340 219502426 373372083 723985582 187881335 197540637 431880054 956881874 727205707 744966467 15...

output:

999367150
989592242
965936164
942730995
975911121
946633006
991729127
986285164
897958800
992144440
939791323
977542667
961014527
578783878
980981967
995960018
810250901
963305290
806037833
979749804
985231975
878772895
996042738
978110539
913232853
938048293
990760104
992595620
979300937
461231909
...

result:

ok 1000 numbers

Test #28:

score: 0
Accepted
time: 41ms
memory: 2380kb

input:

1001
151
419018897 327336549 728566446 963016996 623198669 814459281 68781665 620841283 415770459 386223899 656683897 250478036 463184092 486605719 860293795 467727438 296335144 865617383 924364943 639625856 849418565 405542994 515306818 421335958 555223271 165494879 115926459 74143264 240482760 718...

output:

968571116
946856644
983397635
991610979
989799735
975331234
942813502
992714609
935316447
227097849
981897943
971344084
989692448
881256943
921884851
982875480
930947381
887615668
996011439
960599475
964588977
990690623
992084075
972528734
409251568
980462528
963099303
956886975
988821479
828103856
...

result:

ok 1001 numbers

Test #29:

score: 0
Accepted
time: 42ms
memory: 2176kb

input:

1002
182
828478042 258127784 608700952 146291128 58614696 255200938 181077376 384274435 97404885 511927887 91473385 810037856 369688577 844167979 354857584 143136120 770325093 923862351 584584722 318578059 598733581 517193823 88273542 512394363 290239166 183179631 255581133 138974101 56773857 894905...

output:

955410960
899657064
762453814
976519450
980600565
979606204
578563243
492256913
990014087
738643825
993466665
987271044
932917804
992135070
989041675
917235082
967742692
976205423
353470369
959216311
979666208
923538135
975607009
942400951
841830985
982439679
800922634
900996165
996223536
975460677
...

result:

ok 1002 numbers

Test #30:

score: 0
Accepted
time: 38ms
memory: 2204kb

input:

1003
95
866216223 223356928 594707770 930639724 888188392 974818801 815931666 356449711 13728752 906722095 109814226 14729969 428437283 584116068 557045008 634718388 494283785 321317952 208098845 951154659 231613124 387193053 276615200 916291107 932922775 685890237 660812218 934243017 559926248 2609...

output:

928309342
997702119
968764474
978369254
963112360
966037554
973449118
981154261
991795541
894149519
875520621
983766612
985827737
893897490
932300771
746695880
994381190
906674497
990602387
958502647
994505543
994969790
969909812
987468067
996866894
920005986
985294958
948893117
965193287
989600677
...

result:

ok 1003 numbers

Test #31:

score: 0
Accepted
time: 37ms
memory: 2368kb

input:

1004
322
106560487 635656060 28301232 594851267 621174715 293649166 669684541 81695101 826789337 11779289 347443083 76296784 643019263 46836711 798543560 933485549 914579192 66769814 872310812 492335340 686320816 439615481 839216036 36036576 449341379 279822259 860202943 769848584 244308680 68591110...

output:

989026529
977490494
976761945
951333634
991305970
951955792
950019264
989241981
989247884
984388295
925484122
862558986
972701618
957369105
883635158
139239102
968024371
771551580
984470899
938486360
983336110
992838143
912920278
987294592
964078830
958750522
992704552
805278643
980481199
986089918
...

result:

ok 1004 numbers

Test #32:

score: 0
Accepted
time: 41ms
memory: 2232kb

input:

1005
508
700063189 759616608 226398544 238080778 625481013 585888644 421087465 297128962 936200898 156760292 42779343 861243139 370268847 307938342 14958797 563036215 344288551 423303671 407975614 347858045 352650546 217610091 131894441 277358942 331754451 960886722 247481170 554374345 811895959 742...

output:

990300564
960477320
968470007
881223248
992772139
997830519
946229332
951318473
898328379
978645762
982439733
968552855
975840586
924738673
0
961332951
981790090
958303091
962255012
928181975
697292976
978239704
987597080
933976142
974095638
946760283
874978579
967936754
987118265
971188111
99406000...

result:

ok 1005 numbers

Test #33:

score: 0
Accepted
time: 32ms
memory: 2380kb

input:

9995
9
199441079 80272045 691546667 916708126 5397408 250925470 458726120 833755746 21035844
1
304531310
7
79585774 432303032 668846878 499995845 55538702 559182669 116490629
25
643951846 159527279 682394395 740639447 752793882 24305816 227318082 489475213 885428260 656943634 997702307 481098840 981...

output:

828358338
0
503643967
800483265
851274617
558438334
805351456
718103300
755873256
925331226
923367200
781239629
705174644
870670490
710345092
890845236
196435836
888619399
873774529
348551556
957066972
703669310
0
691068553
766880974
865175659
958383897
876086428
384400029
199288312
922062681
681167...

result:

ok 9995 numbers

Test #34:

score: 0
Accepted
time: 27ms
memory: 2196kb

input:

9996
27
382674063 789481927 740897647 976448741 628751254 35292189 592376712 992191307 342474919 492270576 326719438 725690129 655972353 563454235 885622927 869720675 970619102 874393653 99197414 419576610 422371437 693380871 716641901 351751016 525660819 813453686 587808190
7
916243971 409926439 29...

output:

892993893
408528109
823545206
865167098
284048148
858239032
748648420
798740421
594403561
773515049
493504407
962834015
720601302
494069109
0
312181319
904986863
897772543
532956871
720565878
0
958462641
943591503
7301537
230559667
182065165
889822310
876729906
433776805
913004883
640906807
21077927...

result:

ok 9996 numbers

Test #35:

score: 0
Accepted
time: 27ms
memory: 2168kb

input:

9997
15
139336026 660216122 283091274 346966886 957414578 503222526 371036622 818101610 942390109 943464476 970215911 22466688 142860081 223804982 699855597
1
579184000
37
351771936 169237333 417917143 116251393 94652286 871673550 721053278 316590943 717193687 443755805 234454373 405523174 634763079...

output:

677388909
0
844041705
921201649
965935244
289040313
0
524757462
735930374
844517572
820127418
519003478
0
301461378
937220657
161903099
467381752
229313443
445472081
482630678
865025320
868839012
682953284
951110660
884330378
891437975
751283459
767019764
0
925067743
206310695
881981845
728711180
31...

result:

ok 9997 numbers

Test #36:

score: 0
Accepted
time: 31ms
memory: 2308kb

input:

9998
28
385827709 742806027 111630838 380665999 15795737 617461263 334059980 510066333 162924458 321190190 952065146 931028614 686013207 351247365 712868590 676649170 35973174 761952426 569324454 492457717 608031473 99060271 915855984 64152105 368442556 394092827 833722115 917262921
12
782008188 122...

output:

901467184
579589801
329526421
53057244
896444299
9983132
955570616
0
692633856
792913514
591195926
666425310
0
274372389
828992612
936244997
757359995
0
654398971
611843580
974012196
703643697
623118318
462559064
816375312
932916790
718102701
697888026
181600494
0
903372368
200681832
553347069
0
0
6...

result:

ok 9998 numbers

Test #37:

score: 0
Accepted
time: 32ms
memory: 2380kb

input:

9999
65
176236592 18488953 102521767 193060592 144762162 179961909 183114152 863877794 245757032 658078387 581873400 35867308 816478213 428535638 752948645 801256686 977990157 935277202 647186136 801309279 716036352 551590663 690345622 982519893 246841490 630676504 366847619 650207322 860219946 6053...

output:

968144099
769273565
953028977
797485240
586253060
689624037
328290453
555814759
911180422
603802702
633887984
168532922
773532537
622725225
646451597
346637245
873743288
639793533
784184706
863471373
856646282
621791951
484634796
611418900
890713628
769933641
430138248
916164167
530778319
562126660
...

result:

ok 9999 numbers

Test #38:

score: 0
Accepted
time: 31ms
memory: 2124kb

input:

10000
15
497432565 588591050 243417399 755895857 293124813 878145367 991193935 660045926 258481710 241115340 492762177 179350649 503748210 126727518 284529196
3
163731802 526844405 169420311
34
598711397 52641114 208304908 910771933 745932642 251446917 136564330 678921490 396046025 472662202 8656022...

output:

629168339
5688509
893135342
167904168
834380695
703414200
804858116
323084429
92587102
85793778
810437122
917892314
838947190
802776638
903351492
142665277
881031613
852984718
431317782
542845134
740544916
844260863
0
961497508
0
509236414
741907211
633895696
200566601
809072891
245287319
384873051
...

result:

ok 10000 numbers

Extra Test:

score: 0
Extra Test Passed