QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#289682#7866. Teleportationucup-team296#AC ✓36ms16308kbRust36.9kb2023-12-23 21:06:242023-12-23 21:06:24

Judging History

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

  • [2023-12-23 21:06:24]
  • 评测
  • 测评结果:AC
  • 用时:36ms
  • 内存:16308kb
  • [2023-12-23 21:06:24]
  • 提交

answer

// 
pub mod solution {

use crate::graph::distances::Distances;
use crate::graph::edges::weighted_edge::WeightedEdge;
use crate::graph::graph::Graph;
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 target = input.read_size();
    let a = input.read_size_vec(n);

    let mut graph = Graph::new(n);
    for i in 0..n {
        graph.add_edge(WeightedEdge::new(i, (i + 1) % n, 1));
        graph.add_edge(WeightedEdge::new(i, (i + a[i]) % n, 1));
    }
    let dist = graph.distance(a[0], target).unwrap().0 + 1;
    out.print_line(dist);
}

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::Single;
    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 dsu {
use crate::collections::iter_ext::collect::IterCollect;
use crate::collections::slice_ext::bounds::Bounds;
use crate::collections::slice_ext::legacy_fill::LegacyFill;
use std::cell::Cell;

#[derive(Clone)]
pub struct DSU {
    id: Vec<Cell<u32>>,
    size: Vec<u32>,
    count: usize,
}

impl DSU {
    pub fn new(n: usize) -> Self {
        Self {
            id: (0..n).map(|i| Cell::new(i as u32)).collect_vec(),
            size: vec![1; n],
            count: n,
        }
    }

    pub fn size(&self, i: usize) -> usize {
        self.size[self.get(i)] as usize
    }

    #[allow(clippy::len_without_is_empty)]
    pub fn len(&self) -> usize {
        self.id.len()
    }

    pub fn iter(&self) -> impl Iterator<Item = usize> + '_ {
        self.id.iter().enumerate().filter_map(|(i, id)| {
            if (i as u32) == id.get() {
                Some(i)
            } else {
                None
            }
        })
    }

    pub fn set_count(&self) -> usize {
        self.count
    }

    pub fn join(&mut self, mut a: usize, mut b: usize) -> bool {
        a = self.get(a);
        b = self.get(b);
        if a == b {
            false
        } else {
            self.size[a] += self.size[b];
            self.id[b].replace(a as u32);
            self.count -= 1;
            true
        }
    }

    pub fn get(&self, i: usize) -> usize {
        if self.id[i].get() != i as u32 {
            let res = self.get(self.id[i].get() as usize);
            self.id[i].replace(res as u32);
        }
        self.id[i].get() as usize
    }

    pub fn clear(&mut self) {
        self.count = self.id.len();
        self.size.legacy_fill(1);
        self.id.iter().enumerate().for_each(|(i, id)| {
            id.replace(i as u32);
        });
    }

    pub fn parts(&self) -> Vec<Vec<usize>> {
        let roots = self.iter().collect_vec();
        let mut res = vec![Vec::new(); roots.len()];
        for i in 0..self.id.len() {
            res[roots.as_slice().bin_search(&self.get(i)).unwrap()].push(i);
        }
        res
    }
}
}
pub mod indexed_heap {
use crate::collections::slice_ext::legacy_fill::LegacyFill;
use std::mem;

#[derive(Default)]
enum Opt<T> {
    #[default]
    None,
    Some(u32, T),
}

impl<T> Opt<T> {
    fn index(&self) -> usize {
        match self {
            Opt::None => panic!("unreachable"),
            Opt::Some(index, _) => *index as usize,
        }
    }

    fn val(&self) -> &T {
        match self {
            Opt::None => panic!("unreachable"),
            Opt::Some(_, val) => val,
        }
    }

    fn set_index(&mut self, index: usize) {
        match self {
            Opt::None => panic!("unreachable"),
            Opt::Some(ind, _) => {
                *ind = index as u32;
            }
        }
    }

    fn set_val(&mut self, t: T) {
        match self {
            Opt::None => panic!("unreachable"),
            Opt::Some(_, val) => {
                *val = t;
            }
        }
    }

    fn take(&mut self) -> (usize, T) {
        let val = mem::take(self);
        match val {
            Opt::None => panic!("unreachable"),
            Opt::Some(ind, val) => (ind as usize, val),
        }
    }
}

impl<T> Clone for Opt<T> {
    fn clone(&self) -> Self {
        match self {
            Opt::None => Opt::None,
            Opt::Some(..) => panic!("unreachable"),
        }
    }
}

pub struct IndexedHeap<T> {
    heap: Vec<u32>,
    pos: Vec<Opt<T>>,
}

impl<T: PartialOrd> IndexedHeap<T> {
    pub fn new(capacity: usize) -> Self {
        Self {
            heap: Vec::with_capacity(capacity),
            pos: vec![Opt::None; capacity],
        }
    }

    pub fn len(&self) -> usize {
        self.heap.len()
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    pub fn iter(&self) -> impl Iterator<Item = usize> + '_ {
        self.heap.iter().map(|x| *x as usize)
    }

    pub fn add_or_adjust(&mut self, el: usize, val: T) {
        match self.pos[el] {
            Opt::None => {
                self.pos[el] = Opt::Some(self.heap.len() as u32, val);
                self.heap.push(el as u32);
            }
            Opt::Some(..) => {
                self.pos[el].set_val(val);
            }
        }
        self.sift_up(self.pos[el].index());
        self.sift_down(self.pos[el].index());
    }

    pub fn add_or_relax(&mut self, el: usize, val: T) {
        match &self.pos[el] {
            Opt::None => {
                self.add_or_adjust(el, val);
            }
            Opt::Some(_, value) => {
                if &val < value {
                    self.add_or_adjust(el, val);
                }
            }
        }
    }

    pub fn peek(&self) -> Option<(usize, &T)> {
        if self.is_empty() {
            None
        } else {
            let at = self.heap[0] as usize;
            Some((self.pos[at].index(), self.pos[at].val()))
        }
    }

    pub fn pop(&mut self) -> Option<(usize, T)> {
        if self.is_empty() {
            None
        } else {
            let el = self.heap[0] as usize;
            Some((el, self.remove(el).unwrap()))
        }
    }

    pub fn clear(&mut self) {
        self.heap.clear();
        self.pos.legacy_fill(Opt::None);
    }

    pub fn remove(&mut self, el: usize) -> Option<T> {
        match self.pos[el] {
            Opt::None => None,
            Opt::Some(pos, _) => {
                let last = self.heap.pop().unwrap();
                let val = self.pos[last as usize].take().1;
                if self.is_empty() {
                    Some(val)
                } else {
                    let top_val = self.pos[el].take().1;
                    self.pos[last as usize] = Opt::Some(pos, val);
                    self.heap[pos as usize] = last;
                    self.sift_down(pos as usize);
                    self.sift_up(pos as usize);
                    Some(top_val)
                }
            }
        }
    }

    pub fn value(&self, el: usize) -> Option<&T> {
        match &self.pos[el] {
            Opt::None => None,
            Opt::Some(_, val) => Some(val),
        }
    }

    fn sift_up(&mut self, mut index: usize) {
        let v = self.heap[index] as usize;
        while index > 0 {
            let parent = (index - 1) >> 1;
            let par_val = self.heap[parent] as usize;
            if self.pos[par_val].val() <= self.pos[v].val() {
                self.heap[index] = v as u32;
                self.pos[v].set_index(index);
                return;
            }
            self.heap[index] = par_val as u32;
            self.pos[par_val].set_index(index);
            index = parent;
        }
        self.heap[0] = v as u32;
        self.pos[v].set_index(0);
    }

    fn sift_down(&mut self, mut index: usize) {
        let v = self.heap[index] as usize;
        loop {
            let mut child = (index << 1) + 1;
            if child >= self.len() {
                break;
            }
            if child + 1 < self.len()
                && self.pos[self.heap[child] as usize].val()
                    > self.pos[self.heap[child + 1] as usize].val()
            {
                child += 1;
            }
            if self.pos[self.heap[child] as usize].val() >= self.pos[v].val() {
                break;
            }
            self.heap[index] = self.heap[child];
            self.pos[self.heap[index] as usize].set_index(index);
            index = child;
        }
        self.heap[index] = v as u32;
        self.pos[v].set_index(index);
    }
}
}
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 slice_ext {
pub mod bounds {
pub trait Bounds<T: PartialOrd> {
    fn lower_bound(&self, el: &T) -> usize;
    fn upper_bound(&self, el: &T) -> usize;
    fn bin_search(&self, el: &T) -> Option<usize>;
    fn more(&self, el: &T) -> usize;
    fn more_or_eq(&self, el: &T) -> usize;
    fn less(&self, el: &T) -> usize;
    fn less_or_eq(&self, el: &T) -> usize;
}

impl<T: PartialOrd> Bounds<T> for [T] {
    fn lower_bound(&self, el: &T) -> usize {
        let mut left = 0;
        let mut right = self.len();
        while left < right {
            let mid = left + ((right - left) >> 1);
            if &self[mid] < el {
                left = mid + 1;
            } else {
                right = mid;
            }
        }
        left
    }

    fn upper_bound(&self, el: &T) -> usize {
        let mut left = 0;
        let mut right = self.len();
        while left < right {
            let mid = left + ((right - left) >> 1);
            if &self[mid] <= el {
                left = mid + 1;
            } else {
                right = mid;
            }
        }
        left
    }

    fn bin_search(&self, el: &T) -> Option<usize> {
        let at = self.lower_bound(el);
        if at == self.len() || &self[at] != el {
            None
        } else {
            Some(at)
        }
    }

    fn more(&self, el: &T) -> usize {
        self.len() - self.upper_bound(el)
    }

    fn more_or_eq(&self, el: &T) -> usize {
        self.len() - self.lower_bound(el)
    }

    fn less(&self, el: &T) -> usize {
        self.lower_bound(el)
    }

    fn less_or_eq(&self, el: &T) -> usize {
        self.upper_bound(el)
    }
}
}
pub mod legacy_fill {
// 1.50
pub trait LegacyFill<T> {
    fn legacy_fill(&mut self, val: T);
}

impl<T: Clone> LegacyFill<T> for [T] {
    fn legacy_fill(&mut self, val: T) {
        for el in self.iter_mut() {
            *el = val.clone();
        }
    }
}
}
}
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 graph {
pub mod distances {
use crate::collections::indexed_heap::IndexedHeap;
use crate::graph::edges::weighted_edge_trait::WeightedEdgeTrait;
use crate::graph::graph::Graph;
use crate::numbers::num_traits::add_sub::Addable;
use crate::numbers::num_traits::zero_one::ZeroOne;

pub trait Distances<W: Addable + PartialOrd + Copy + ZeroOne> {
    fn distances_from(&self, source: usize) -> Vec<Option<(W, usize, usize)>>;

    fn distance(&self, source: usize, mut destination: usize) -> Option<(W, Vec<(usize, usize)>)> {
        let dist = self.distances_from(source);
        dist[destination].map(|(w, ..)| {
            let mut path = Vec::new();
            while destination != source {
                let (_, from, edge) = dist[destination].unwrap();
                path.push((from, edge));
                destination = from;
            }
            path.reverse();
            (w, path)
        })
    }
}

impl<W: Addable + PartialOrd + Copy + ZeroOne, E: WeightedEdgeTrait<W>> Distances<W> for Graph<E> {
    fn distances_from(&self, source: usize) -> Vec<Option<(W, usize, usize)>> {
        let n = self.vertex_count();
        let mut res = vec![None; n];
        let mut heap = IndexedHeap::new(n);
        heap.add_or_adjust(source, (W::zero(), source, self[source].len()));
        while let Some((cur, dist)) = heap.pop() {
            res[cur] = Some(dist);
            let dist = dist.0;
            for (i, e) in self[cur].iter().enumerate() {
                let next = e.to();
                if res[next].is_some() {
                    continue;
                }
                let total = dist + e.weight();
                let next_dist = (total, cur, i);
                heap.add_or_relax(next, next_dist);
            }
        }
        res
    }
}
}
pub mod edges {
pub mod bi_edge {
use crate::graph::edges::bi_edge_trait::BiEdgeTrait;
use crate::graph::edges::edge_id::{EdgeId, NoId, WithId};
use crate::graph::edges::edge_trait::{BidirectionalEdgeTrait, EdgeTrait};

#[derive(Clone)]
pub struct BiEdgeRaw<Id: EdgeId, P> {
    to: u32,
    id: Id,
    payload: P,
}

impl<Id: EdgeId> BiEdgeRaw<Id, ()> {
    pub fn new(from: usize, to: usize) -> (usize, Self) {
        (
            from,
            Self {
                to: to as u32,
                id: Id::new(),
                payload: (),
            },
        )
    }
}

impl<Id: EdgeId, P> BiEdgeRaw<Id, P> {
    pub fn with_payload(from: usize, to: usize, payload: P) -> (usize, Self) {
        (from, Self::with_payload_impl(to, payload))
    }

    fn with_payload_impl(to: usize, payload: P) -> BiEdgeRaw<Id, P> {
        Self {
            to: to as u32,
            id: Id::new(),
            payload,
        }
    }
}

impl<Id: EdgeId, P: Clone> BidirectionalEdgeTrait for BiEdgeRaw<Id, P> {}

impl<Id: EdgeId, P: Clone> EdgeTrait for BiEdgeRaw<Id, P> {
    type Payload = P;

    const REVERSABLE: bool = true;

    fn to(&self) -> usize {
        self.to as usize
    }

    fn id(&self) -> usize {
        self.id.id()
    }

    fn set_id(&mut self, id: usize) {
        self.id.set_id(id);
    }

    fn reverse_id(&self) -> usize {
        panic!("no reverse id")
    }

    fn set_reverse_id(&mut self, _: usize) {}

    fn reverse_edge(&self, from: usize) -> Self {
        Self::with_payload_impl(from, self.payload.clone())
    }

    fn payload(&self) -> &P {
        &self.payload
    }
}

impl<Id: EdgeId, P: Clone> BiEdgeTrait for BiEdgeRaw<Id, P> {}

pub type BiEdge<P> = BiEdgeRaw<NoId, P>;
pub type BiEdgeWithId<P> = BiEdgeRaw<WithId, P>;
}
pub mod bi_edge_trait {
use crate::graph::edges::edge_trait::EdgeTrait;

pub trait BiEdgeTrait: EdgeTrait {}
}
pub mod edge {
use crate::graph::edges::edge_id::{EdgeId, NoId, WithId};
use crate::graph::edges::edge_trait::EdgeTrait;

#[derive(Clone)]
pub struct EdgeRaw<Id: EdgeId, P> {
    to: u32,
    id: Id,
    payload: P,
}

impl<Id: EdgeId> EdgeRaw<Id, ()> {
    pub fn new(from: usize, to: usize) -> (usize, Self) {
        (
            from,
            Self {
                to: to as u32,
                id: Id::new(),
                payload: (),
            },
        )
    }
}

impl<Id: EdgeId, P> EdgeRaw<Id, P> {
    pub fn with_payload(from: usize, to: usize, payload: P) -> (usize, Self) {
        (from, Self::with_payload_impl(to, payload))
    }

    fn with_payload_impl(to: usize, payload: P) -> Self {
        Self {
            to: to as u32,
            id: Id::new(),
            payload,
        }
    }
}

impl<Id: EdgeId, P: Clone> EdgeTrait for EdgeRaw<Id, P> {
    type Payload = P;

    const REVERSABLE: bool = false;

    fn to(&self) -> usize {
        self.to as usize
    }

    fn id(&self) -> usize {
        self.id.id()
    }

    fn set_id(&mut self, id: usize) {
        self.id.set_id(id);
    }

    fn reverse_id(&self) -> usize {
        panic!("no reverse")
    }

    fn set_reverse_id(&mut self, _: usize) {
        panic!("no reverse")
    }

    fn reverse_edge(&self, _: usize) -> Self {
        panic!("no reverse")
    }

    fn payload(&self) -> &P {
        &self.payload
    }
}

pub type Edge<P> = EdgeRaw<NoId, P>;
pub type EdgeWithId<P> = EdgeRaw<WithId, P>;
}
pub mod edge_id {
pub trait EdgeId: Clone {
    fn new() -> Self;
    fn id(&self) -> usize;
    fn set_id(&mut self, id: usize);
}

#[derive(Clone)]
pub struct WithId {
    id: u32,
}

impl EdgeId for WithId {
    fn new() -> Self {
        Self { id: 0 }
    }

    fn id(&self) -> usize {
        self.id as usize
    }

    fn set_id(&mut self, id: usize) {
        self.id = id as u32;
    }
}

#[derive(Clone)]
pub struct NoId {}

impl EdgeId for NoId {
    fn new() -> Self {
        Self {}
    }

    fn id(&self) -> usize {
        panic!("Id called on no id")
    }

    fn set_id(&mut self, _: usize) {}
}
}
pub mod edge_trait {
pub trait EdgeTrait: Clone {
    type Payload;
    
    const REVERSABLE: bool;

    fn to(&self) -> usize;
    fn id(&self) -> usize;
    fn set_id(&mut self, id: usize);
    fn reverse_id(&self) -> usize;
    fn set_reverse_id(&mut self, reverse_id: usize);
    #[must_use]
    fn reverse_edge(&self, from: usize) -> Self;
    fn payload(&self) -> &Self::Payload;
}

pub trait BidirectionalEdgeTrait: EdgeTrait {}
}
pub mod weighted_edge {
use crate::graph::edges::edge_id::{EdgeId, NoId, WithId};
use crate::graph::edges::edge_trait::EdgeTrait;
use crate::graph::edges::weighted_edge_trait::WeightedEdgeTrait;
use crate::numbers::num_traits::add_sub::Addable;
use crate::numbers::num_traits::zero_one::ZeroOne;

#[derive(Clone)]
pub struct WeightedEdgeRaw<W: Addable + PartialOrd + Copy + ZeroOne, Id: EdgeId, P> {
    to: u32,
    weight: W,
    id: Id,
    payload: P,
}

impl<W: Addable + PartialOrd + Copy + ZeroOne, Id: EdgeId> WeightedEdgeRaw<W, Id, ()> {
    pub fn new(from: usize, to: usize, w: W) -> (usize, Self) {
        (
            from,
            Self {
                to: to as u32,
                weight: w,
                id: Id::new(),
                payload: (),
            },
        )
    }
}

impl<W: Addable + PartialOrd + Copy + ZeroOne, Id: EdgeId, P> WeightedEdgeRaw<W, Id, P> {
    pub fn with_payload(from: usize, to: usize, w: W, payload: P) -> (usize, Self) {
        (from, Self::with_payload_impl(to, w, payload))
    }

    fn with_payload_impl(to: usize, w: W, payload: P) -> Self {
        Self {
            to: to as u32,
            weight: w,
            id: Id::new(),
            payload,
        }
    }
}

impl<W: Addable + PartialOrd + Copy + ZeroOne, Id: EdgeId, P: Clone> EdgeTrait
    for WeightedEdgeRaw<W, Id, P>
{
    type Payload = P;
    const REVERSABLE: bool = false;

    fn to(&self) -> usize {
        self.to as usize
    }

    fn id(&self) -> usize {
        self.id.id()
    }

    fn set_id(&mut self, id: usize) {
        self.id.set_id(id);
    }

    fn reverse_id(&self) -> usize {
        panic!("no reverse")
    }

    fn set_reverse_id(&mut self, _: usize) {
        panic!("no reverse")
    }

    fn reverse_edge(&self, _: usize) -> Self {
        panic!("no reverse")
    }

    fn payload(&self) -> &P {
        &self.payload
    }
}

impl<W: Addable + PartialOrd + Copy + ZeroOne, Id: EdgeId, P: Clone> WeightedEdgeTrait<W>
    for WeightedEdgeRaw<W, Id, P>
{
    fn weight(&self) -> W {
        self.weight
    }

    fn weight_mut(&mut self) -> &mut W {
        &mut self.weight
    }
}

pub type WeightedEdge<W, P> = WeightedEdgeRaw<W, NoId, P>;
pub type WeightedEdgeWithId<W, P> = WeightedEdgeRaw<W, WithId, P>;
}
pub mod weighted_edge_trait {
use crate::graph::edges::edge_trait::EdgeTrait;

pub trait WeightedEdgeTrait<W: Copy>: EdgeTrait {
    fn weight(&self) -> W;
    fn weight_mut(&mut self) -> &mut W;
}
}
}
pub mod graph {
use crate::collections::dsu::DSU;
use crate::graph::edges::bi_edge::BiEdge;
use crate::graph::edges::edge::Edge;
use crate::graph::edges::edge_trait::{BidirectionalEdgeTrait, EdgeTrait};
use std::ops::{Index, IndexMut};

pub struct Graph<E: EdgeTrait> {
    pub(super) edges: Vec<Vec<E>>,
    edge_count: usize,
}

impl<E: EdgeTrait> Graph<E> {
    pub fn new(vertex_count: usize) -> Self {
        Self {
            edges: vec![Vec::new(); vertex_count],
            edge_count: 0,
        }
    }

    pub fn add_edge(&mut self, (from, mut edge): (usize, E)) -> usize {
        let to = edge.to();
        assert!(to < self.edges.len());
        let direct_id = self.edges[from].len();
        edge.set_id(self.edge_count);
        self.edges[from].push(edge);
        if E::REVERSABLE {
            let rev_id = self.edges[to].len();
            self.edges[from][direct_id].set_reverse_id(rev_id);
            let mut rev_edge = self.edges[from][direct_id].reverse_edge(from);
            rev_edge.set_id(self.edge_count);
            rev_edge.set_reverse_id(direct_id);
            self.edges[to].push(rev_edge);
        }
        self.edge_count += 1;
        direct_id
    }

    pub fn add_vertices(&mut self, cnt: usize) {
        self.edges.resize(self.edges.len() + cnt, Vec::new());
    }

    pub fn clear(&mut self) {
        self.edge_count = 0;
        for ve in self.edges.iter_mut() {
            ve.clear();
        }
    }

    pub fn vertex_count(&self) -> usize {
        self.edges.len()
    }

    pub fn edge_count(&self) -> usize {
        self.edge_count
    }

    pub fn degrees(&self) -> Vec<usize> {
        self.edges.iter().map(|v| v.len()).collect()
    }
}

impl<E: BidirectionalEdgeTrait> Graph<E> {
    pub fn is_tree(&self) -> bool {
        if self.edge_count + 1 != self.vertex_count() {
            false
        } else {
            self.is_connected()
        }
    }

    pub fn is_forest(&self) -> bool {
        let mut dsu = DSU::new(self.vertex_count());
        for i in 0..self.vertex_count() {
            for e in self[i].iter() {
                if i <= e.to() && !dsu.join(i, e.to()) {
                    return false;
                }
            }
        }
        true
    }

    pub fn is_connected(&self) -> bool {
        let mut dsu = DSU::new(self.vertex_count());
        for i in 0..self.vertex_count() {
            for e in self[i].iter() {
                dsu.join(i, e.to());
            }
        }
        dsu.set_count() == 1
    }
}

impl<E: EdgeTrait> Index<usize> for Graph<E> {
    type Output = [E];

    fn index(&self, index: usize) -> &Self::Output {
        &self.edges[index]
    }
}

impl<E: EdgeTrait> IndexMut<usize> for Graph<E> {
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
        &mut self.edges[index]
    }
}

impl Graph<Edge<()>> {
    pub fn from_edges(n: usize, edges: &[(usize, usize)]) -> Self {
        let mut graph = Self::new(n);
        for &(from, to) in edges {
            graph.add_edge(Edge::new(from, to));
        }
        graph
    }
}

impl Graph<BiEdge<()>> {
    pub fn from_biedges(n: usize, edges: &[(usize, usize)]) -> Self {
        let mut graph = Self::new(n);
        for &(from, to) in edges {
            graph.add_edge(BiEdge::new(from, to));
        }
        graph
    }
}
}
}
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()
    }

    //noinspection RsSelfConvention
    pub fn is_empty(&mut self) -> bool {
        self.skip_whitespace();
        self.is_exhausted()
    }

    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 numbers {
pub mod num_traits {
pub mod add_sub {
use std::ops::{Add, AddAssign, Sub, SubAssign};

pub trait Addable: Add<Output = Self> + AddAssign + Copy {}
impl<T: Add<Output = Self> + AddAssign + Copy> Addable for T {}

pub trait AddSub: Addable + Sub<Output = Self> + SubAssign {}
impl<T: Addable + Sub<Output = Self> + SubAssign> AddSub for T {}
}
pub mod zero_one {
pub trait ZeroOne {
    fn zero() -> Self;
    fn one() -> Self;
}

macro_rules! zero_one_integer_impl {
    ($($t: ident)+) => {$(
        impl ZeroOne for $t {
            fn zero() -> Self {
                0
            }

            fn one() -> Self {
                1
            }
        }
    )+};
}

zero_one_integer_impl!(i128 i64 i32 i16 i8 isize u128 u64 u32 u16 u8 usize);
}
}
}
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: 2036kb

input:

4 3
0 1 2 3

output:

4

result:

ok 1 number(s): "4"

Test #2:

score: 0
Accepted
time: 0ms
memory: 2096kb

input:

4 3
0 0 0 0

output:

4

result:

ok 1 number(s): "4"

Test #3:

score: 0
Accepted
time: 0ms
memory: 2156kb

input:

4 3
2 2 2 2

output:

2

result:

ok 1 number(s): "2"

Test #4:

score: 0
Accepted
time: 0ms
memory: 2072kb

input:

2 1
0 0

output:

2

result:

ok 1 number(s): "2"

Test #5:

score: 0
Accepted
time: 0ms
memory: 2076kb

input:

2 1
1 1

output:

1

result:

ok 1 number(s): "1"

Test #6:

score: 0
Accepted
time: 0ms
memory: 2264kb

input:

300 25
182 9 13 211 258 132 27 42 218 200 271 258 164 121 8 84 29 195 141 290 110 0 272 93 142 134 140 32 232 99 162 199 297 287 212 29 182 53 61 98 116 282 75 245 230 165 22 4 179 89 274 231 46 299 248 208 200 285 221 50 221 199 294 241 195 138 22 204 113 100 132 276 158 146 238 178 100 94 131 157 ...

output:

10

result:

ok 1 number(s): "10"

Test #7:

score: 0
Accepted
time: 0ms
memory: 2264kb

input:

300 49
5 0 5 6 8 1 2 6 8 8 0 7 0 0 2 0 7 3 6 0 7 2 6 4 3 9 9 6 5 0 9 1 4 1 5 2 5 5 2 5 5 5 9 9 2 7 0 0 6 6 9 7 10 3 5 2 6 3 0 8 6 4 4 9 7 4 8 0 2 4 5 0 6 5 7 0 9 5 1 3 9 2 3 5 8 2 0 1 0 8 2 4 5 1 10 8 8 8 5 3 1 7 6 8 10 1 6 5 8 2 1 1 10 1 5 1 6 1 7 5 3 3 6 8 8 6 2 0 4 9 7 1 8 5 9 5 0 3 1 8 0 0 2 8 0...

output:

14

result:

ok 1 number(s): "14"

Test #8:

score: 0
Accepted
time: 1ms
memory: 2328kb

input:

2000 1535
297 1103 1985 224 645 892 493 1146 258 729 686 230 1509 1197 1970 577 1462 672 1133 737 226 1248 1236 492 473 495 1395 942 641 1010 39 759 625 1684 725 235 327 1217 321 512 675 331 1864 915 1386 713 1559 1475 643 1347 1697 101 1533 1511 1596 1486 776 439 148 460 919 1529 1463 1070 1940 597...

output:

12

result:

ok 1 number(s): "12"

Test #9:

score: 0
Accepted
time: 0ms
memory: 3208kb

input:

10000 8363
435 437 137 171 494 479 76 224 219 348 459 215 351 368 101 323 208 271 149 40 290 281 491 234 1 304 405 106 26 112 89 471 232 237 445 482 215 17 357 26 33 3 185 45 234 426 61 60 5 244 32 88 93 128 19 381 222 187 356 177 259 116 60 268 157 50 293 152 380 38 296 466 425 460 281 168 408 138 ...

output:

30

result:

ok 1 number(s): "30"

Test #10:

score: 0
Accepted
time: 8ms
memory: 6320kb

input:

30000 9089
1388 1944 499 465 901 1627 647 1311 1135 1649 263 898 852 1600 940 33 164 18 110 916 1705 1975 966 1493 336 457 695 1893 1575 2000 1146 1268 1633 1047 1106 1541 681 807 236 596 471 374 954 851 1283 392 1042 624 1244 1593 160 1976 1856 115 1100 743 1874 1580 1775 440 1470 1150 287 1526 109...

output:

17

result:

ok 1 number(s): "17"

Test #11:

score: 0
Accepted
time: 3ms
memory: 9112kb

input:

50000 10744
8 6 9 7 10 8 7 9 7 8 4 6 9 5 4 8 9 3 0 3 6 0 6 0 6 2 1 3 7 10 4 8 7 8 2 7 8 3 0 9 2 10 10 3 0 5 8 3 1 6 3 0 3 8 6 7 8 0 5 3 6 0 4 2 6 3 8 3 0 1 9 0 6 1 5 5 8 3 5 2 7 0 9 0 7 2 5 8 4 9 0 8 2 8 6 0 4 2 6 8 8 8 4 6 9 1 6 6 8 9 8 6 4 9 4 5 2 6 5 5 1 10 1 8 9 5 2 0 2 4 8 5 6 9 10 0 7 8 7 5 9 ...

output:

1845

result:

ok 1 number(s): "1845"

Test #12:

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

input:

100000 80007
23901 71465 9481 98727 78470 22914 2469 5998 53210 25993 3995 11348 30510 56447 17276 78307 18315 42068 38635 63126 26255 63984 57248 58304 64365 17838 28517 18979 95944 36315 6075 69529 96508 6939 6038 56047 41846 82117 41053 49669 95895 45890 74635 90735 75412 27250 87729 68343 66201 ...

output:

16

result:

ok 1 number(s): "16"

Test #13:

score: 0
Accepted
time: 10ms
memory: 16224kb

input:

100000 94250
1 8 8 6 6 0 8 3 0 7 1 4 1 9 9 3 5 5 1 8 5 9 0 1 4 5 8 6 5 6 8 6 7 5 4 5 7 8 5 5 1 5 5 4 6 5 5 7 6 3 3 10 10 2 6 5 6 8 3 4 7 3 2 2 6 10 9 7 3 7 6 7 2 5 8 9 4 5 2 7 1 1 7 9 10 9 3 7 0 3 5 5 7 5 5 0 10 6 1 8 7 2 9 4 1 1 7 1 6 5 3 6 7 5 9 8 6 3 6 0 8 2 0 10 2 0 2 3 0 7 10 3 3 5 6 10 4 10 2 ...

output:

16242

result:

ok 1 number(s): "16242"

Test #14:

score: 0
Accepted
time: 13ms
memory: 16080kb

input:

100000 20417
4 20 16 4 17 18 11 5 7 12 9 6 14 9 9 5 12 2 1 5 3 4 18 14 20 16 12 3 19 10 5 4 3 10 17 9 6 15 2 0 6 16 14 15 10 20 3 2 11 11 19 15 1 20 5 0 1 1 9 2 20 10 0 6 6 15 16 11 13 7 6 16 9 8 1 2 8 7 8 14 10 8 14 15 2 13 18 5 12 4 9 4 20 16 11 17 0 19 5 14 2 10 7 5 8 4 19 2 20 16 10 19 10 18 18 ...

output:

1707

result:

ok 1 number(s): "1707"

Test #15:

score: 0
Accepted
time: 19ms
memory: 16108kb

input:

100000 77449
183 51 136 72 1 114 108 185 35 169 104 25 21 96 181 124 8 92 26 20 94 79 46 99 111 128 110 178 19 146 97 56 67 50 4 186 79 71 150 8 133 170 196 60 75 143 101 188 197 120 138 5 129 85 56 17 127 94 69 141 78 72 21 133 39 47 170 191 83 181 79 9 74 78 65 126 193 167 165 153 128 19 173 183 4...

output:

622

result:

ok 1 number(s): "622"

Test #16:

score: 0
Accepted
time: 28ms
memory: 16108kb

input:

100000 75846
1031 1136 1083 43 1149 1431 1975 1513 1240 350 425 558 1179 32 2237 1575 1501 383 615 700 1437 208 96 1622 1161 2372 1486 2251 2267 1134 1123 2776 2105 2337 1130 882 412 2548 1199 2340 2216 2603 2338 2316 2675 2542 1924 2846 1340 1367 581 2560 1149 2377 1742 1955 2119 1102 588 1416 724 ...

output:

46

result:

ok 1 number(s): "46"

Test #17:

score: 0
Accepted
time: 29ms
memory: 16108kb

input:

100000 29144
1180 13 991 2756 4797 4801 2930 2650 1472 2188 314 1724 200 1556 314 4580 2808 4528 820 1878 1768 917 384 4498 2358 3523 4161 2135 4804 3450 1838 3821 964 2476 1913 1514 805 1416 3330 458 2433 1547 3858 1381 103 704 3996 721 3988 392 1871 654 4829 2670 4480 3400 4588 1433 3763 4892 1868...

output:

18

result:

ok 1 number(s): "18"

Test #18:

score: 0
Accepted
time: 26ms
memory: 16308kb

input:

100000 62039
8812 8350 4373 2152 6155 6457 4157 170 2598 7398 638 8675 3641 2693 8757 334 2846 148 5707 107 6572 7238 2792 2904 2605 202 2496 9806 8620 1704 272 6287 1935 5733 6380 4852 5374 4133 4648 4674 7842 2907 6474 8853 3878 9137 5848 5721 2274 6585 2287 1480 7059 9873 5551 6701 8177 999 842 4...

output:

15

result:

ok 1 number(s): "15"

Test #19:

score: 0
Accepted
time: 11ms
memory: 16096kb

input:

100000 94687
0 0 0 1 0 1 0 1 1 1 0 0 1 1 1 1 0 0 1 1 0 1 1 0 1 0 1 1 1 1 1 0 0 0 0 0 1 0 0 1 1 1 1 1 1 0 0 0 1 0 0 1 0 0 0 1 0 1 0 1 1 0 1 1 0 1 1 0 0 1 0 1 1 0 0 1 0 1 1 1 0 1 0 0 0 0 0 1 1 1 0 1 1 0 1 0 0 0 0 1 0 0 1 1 1 1 0 1 0 1 0 0 0 0 0 1 1 1 1 1 0 1 1 1 0 1 0 1 1 1 1 1 0 0 0 1 1 0 0 0 1 0 0 1...

output:

94688

result:

ok 1 number(s): "94688"

Test #20:

score: 0
Accepted
time: 3ms
memory: 16208kb

input:

100000 92517
0 1 0 0 0 1 1 1 2 0 2 0 1 2 2 0 0 1 0 1 1 2 2 1 1 0 1 0 1 2 0 0 2 1 2 1 2 1 1 1 2 2 2 1 0 2 1 2 1 2 1 0 1 0 2 1 2 0 1 2 1 0 2 2 1 1 0 1 0 0 0 2 0 1 2 2 2 1 2 0 1 2 1 1 1 2 1 1 0 1 0 0 0 0 0 0 1 1 1 0 1 1 1 2 2 2 0 0 0 1 0 1 0 0 2 1 0 2 0 1 2 2 2 2 1 0 0 2 1 1 1 2 2 1 2 1 0 1 1 1 0 0 2 2...

output:

69476

result:

ok 1 number(s): "69476"

Test #21:

score: 0
Accepted
time: 11ms
memory: 16228kb

input:

100000 80101
3 3 1 0 1 3 2 0 0 0 2 2 1 1 1 0 1 2 2 0 1 0 1 0 2 2 3 2 2 2 2 0 1 2 2 1 1 3 1 2 2 1 0 3 3 1 0 0 3 2 2 0 1 0 3 1 0 1 0 1 3 3 3 3 3 3 0 0 0 1 1 3 0 1 3 1 0 3 3 2 1 0 2 2 0 3 1 1 2 1 1 3 1 2 0 0 1 2 2 2 0 1 1 2 2 2 2 2 2 0 2 2 3 0 2 3 0 0 3 2 1 0 2 3 3 0 1 0 0 3 0 1 0 3 1 3 1 2 1 2 1 2 1 1...

output:

44759

result:

ok 1 number(s): "44759"

Test #22:

score: 0
Accepted
time: 10ms
memory: 16220kb

input:

100000 96857
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...

output:

96858

result:

ok 1 number(s): "96858"

Test #23:

score: 0
Accepted
time: 3ms
memory: 16144kb

input:

100000 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...

output:

2

result:

ok 1 number(s): "2"

Test #24:

score: 0
Accepted
time: 10ms
memory: 16284kb

input:

100000 1
1 0 0 0 1 0 1 0 1 1 1 0 0 1 1 1 1 0 0 1 1 0 1 1 0 1 0 1 1 1 1 1 0 0 0 0 0 1 0 0 1 1 1 1 1 1 0 0 0 1 0 0 1 0 0 0 1 0 1 0 1 1 0 1 1 0 1 1 0 0 1 0 1 1 0 0 1 0 1 1 1 0 1 0 0 0 0 0 1 1 1 0 1 1 0 1 0 0 0 0 1 0 0 1 1 1 1 0 1 0 1 0 0 0 0 0 1 1 1 1 1 0 1 1 1 0 1 0 1 1 1 1 1 0 0 0 1 1 0 0 0 1 0 0 1 0...

output:

1

result:

ok 1 number(s): "1"

Test #25:

score: 0
Accepted
time: 7ms
memory: 16108kb

input:

100000 1
2 0 1 0 0 0 1 1 1 2 0 2 0 1 2 2 0 0 1 0 1 1 2 2 1 1 0 1 0 1 2 0 0 2 1 2 1 2 1 1 1 2 2 2 1 0 2 1 2 1 2 1 0 1 0 2 1 2 0 1 2 1 0 2 2 1 1 0 1 0 0 0 2 0 1 2 2 2 1 2 0 1 2 1 1 1 2 1 1 0 1 0 0 0 0 0 0 1 1 1 0 1 1 1 2 2 2 0 0 0 1 0 1 0 0 2 1 0 2 0 1 2 2 2 2 1 0 0 2 1 1 1 2 2 1 2 1 0 1 1 1 0 0 2 2 0...

output:

75114

result:

ok 1 number(s): "75114"

Test #26:

score: 0
Accepted
time: 11ms
memory: 16104kb

input:

100000 1
3 3 3 1 0 1 3 2 0 0 0 2 2 1 1 1 0 1 2 2 0 1 0 1 0 2 2 3 2 2 2 2 0 1 2 2 1 1 3 1 2 2 1 0 3 3 1 0 0 3 2 2 0 1 0 3 1 0 1 0 1 3 3 3 3 3 3 0 0 0 1 1 3 0 1 3 1 0 3 3 2 1 0 2 2 0 3 1 1 2 1 1 3 1 2 0 0 1 2 2 2 0 1 1 2 2 2 2 2 2 0 2 2 3 0 2 3 0 0 3 2 1 0 2 3 3 0 1 0 0 3 0 1 0 3 1 3 1 2 1 2 1 2 1 1 2...

output:

55920

result:

ok 1 number(s): "55920"

Test #27:

score: 0
Accepted
time: 7ms
memory: 16100kb

input:

100000 1
10 1 8 8 6 6 0 8 3 0 7 1 4 1 9 9 3 5 5 1 8 5 9 0 1 4 5 8 6 5 6 8 6 7 5 4 5 7 8 5 5 1 5 5 4 6 5 5 7 6 3 3 10 10 2 6 5 6 8 3 4 7 3 2 2 6 10 9 7 3 7 6 7 2 5 8 9 4 5 2 7 1 1 7 9 10 9 3 7 0 3 5 5 7 5 5 0 10 6 1 8 7 2 9 4 1 1 7 1 6 5 3 6 7 5 9 8 6 3 6 0 8 2 0 10 2 0 2 3 0 7 10 3 3 5 6 10 4 10 2 2...

output:

17206

result:

ok 1 number(s): "17206"

Test #28:

score: 0
Accepted
time: 8ms
memory: 16208kb

input:

100000 1
7 4 20 16 4 17 18 11 5 7 12 9 6 14 9 9 5 12 2 1 5 3 4 18 14 20 16 12 3 19 10 5 4 3 10 17 9 6 15 2 0 6 16 14 15 10 20 3 2 11 11 19 15 1 20 5 0 1 1 9 2 20 10 0 6 6 15 16 11 13 7 6 16 9 8 1 2 8 7 8 14 10 8 14 15 2 13 18 5 12 4 9 4 20 16 11 17 0 19 5 14 2 10 7 5 8 4 19 2 20 16 10 19 10 18 18 18...

output:

8417

result:

ok 1 number(s): "8417"

Test #29:

score: 0
Accepted
time: 12ms
memory: 16208kb

input:

100000 1
168 183 51 136 72 1 114 108 185 35 169 104 25 21 96 181 124 8 92 26 20 94 79 46 99 111 128 110 178 19 146 97 56 67 50 4 186 79 71 150 8 133 170 196 60 75 143 101 188 197 120 138 5 129 85 56 17 127 94 69 141 78 72 21 133 39 47 170 191 83 181 79 9 74 78 65 126 193 167 165 153 128 19 173 183 4...

output:

802

result:

ok 1 number(s): "802"

Test #30:

score: 0
Accepted
time: 16ms
memory: 16272kb

input:

100000 1
1126 1744 1123 676 1947 1771 1087 297 1869 1579 611 1284 1996 196 1508 61 1454 984 1135 1091 554 267 139 705 1730 577 1794 406 520 1592 1972 1999 1262 1036 147 1851 700 1769 772 91 1708 1576 1673 1996 903 1697 1542 1934 707 406 1506 429 62 127 256 252 1345 35 1812 1308 960 905 189 1094 1739...

output:

83

result:

ok 1 number(s): "83"

Test #31:

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

input:

100000 1
4720 19200 10083 15551 13583 5448 2076 14999 23516 26633 8237 3943 20711 23104 25054 29027 18249 14921 22216 16015 18077 20462 3890 17619 1530 9641 21464 25301 14217 29872 24288 25652 3614 11756 16852 3058 4986 22079 14250 18815 27526 17657 24930 27557 17019 2337 917 7104 26322 16070 7636 9...

output:

14

result:

ok 1 number(s): "14"

Test #32:

score: 0
Accepted
time: 36ms
memory: 16124kb

input:

100000 1
13035 23901 71465 9481 98727 78470 22914 2469 5998 53210 25993 3995 11348 30510 56447 17276 78307 18315 42068 38635 63126 26255 63984 57248 58304 64365 17838 28517 18979 95944 36315 6075 69529 96508 6939 6038 56047 41846 82117 41053 49669 95895 45890 74635 90735 75412 27250 87729 68343 6620...

output:

17

result:

ok 1 number(s): "17"

Extra Test:

score: 0
Extra Test Passed