Problem:
Score : 100 points

Problem Statement

You are given a grid of N rows and M columns. The square at the i-th row and j-th column will be denoted as (i,j).
A nonnegative integer A_{i,j} is written for each square (i,j).

You choose some of the squares so that each row and column contains at most K chosen squares.
Under this constraint, calculate the maximum value of the sum of the integers written on the chosen squares.
Additionally, calculate a way to choose squares that acheives the maximum.

Constraints

1 \leq N \leq 50

1 \leq K \leq N

0 \leq A_{i,j} \leq 10^9

All values in Input are integer.

Input

Input is given from Standard Input in the following format:

N K
A_{1,1} A_{1,2} \cdots A_{1,N}
A_{2,1} A_{2,2} \cdots A_{2,N}
\vdots
A_{N,1} A_{N,2} \cdots A_{N,N}

Output

On the first line, print the maximum value of the sum of the integers written on the chosen squares.

On the next N lines, print a way that achieves the maximum.

Precisely, output the strings t_1,t_2,\cdots,t_N, that satisfies t_{i,j}=X if you choose (i,j) and t_{i,j}=. otherwise.

You may print any way to choose squares that maximizes the sum.

Sample Input 1

3 1
5 3 2
1 4 8
7 6 9

Sample Output 1

19
X..
..X
.X.

Sample Input 2

3 2
10 10 1
10 10 1
1 1 10

Sample Output 2

50
XX.
XX.
..X

Solution in Rust (1.42.0) (judge verdict: Accepted)

#![allow(unused_imports)]
#![allow(dead_code)]
#![allow(unused_mut)]
// #![allow(non_camel_case_types)]
use crate::rust::libs::util::{io::*,util::*,lower_bound::*,memoize::*};
use crate::rust::libs::math::{mod_int::*,prime_factor::*,floor_sum::*};
use crate::rust::libs::data_structure::{union_find::UF,segment_tree::*};
use crate::rust::libs::{algebraic_structure::monoid::{plus::*,max::*,min::*}};
use crate::rust::libs::graph::flow::min_cost_flow::MinCostFlow;
use std::ops::Range;
use  std::collections::*;
use std::cmp::*;
use std::process::*;
use bitset_fixed::BitSet;
const MOD: u32 = 1_000_000_007;
const INF:usize=1usize<<60;
const_mod!(P,MOD);

fn main(){
    input!{
        n:usize,k:usize,
        v:[[i64;n];n]
    }
    let mut mcf=MinCostFlow::new(n*n+2*n+2);
    for i in 0..n{
        mcf.add_edge(n*n+2*n, n*n+i, k as i64, 0);
        mcf.add_edge(n*n+n+i, n*n+2*n+1, k as i64, 0);
        for j in 0..n{
            mcf.add_edge(n*n+i,n*i+j, 1, -v[i][j]);
            mcf.add_edge(n*i+j,n*n+n+j, 1, 0);
        }
    }
    mcf.add_edge(n*n+2*n,n*n+2*n+1,INF as i64, 0);
    println!("{}",-mcf.run(n*n+2*n,n*n+2*n+1,(k*n) as i64));
    let mut ans=vec![vec!['.';n];n];
    for i in 0..n{
        for j in 0..mcf.e[n*n+i].len(){
            let ed=mcf.e[n*n+i][j];
            if ed.to-n*i<n&&ed.is_rev{
                if ed.cap==0{
                    ans[i][ed.to-n*i]='X';
                }
            }
        }
    }
    for e in ans{
        println!("{}",e.iter().collect::<String>());
    }
}




pub mod rust {

pub mod libs {

pub mod algebraic_structure {

pub mod monoid {

pub mod max {

use crate::rust::libs::algebraic_structure::monoid::monoid::Monoid;
use std::option::Option;
#[derive(Debug,Copy,Clone)]
pub struct Max<T>{
    val:Option<T>,
}
impl<T:Ord+Copy> Monoid for Max<T>{
    type MonoidType=T;
    fn new()->Self{
        Self{
            val:None,
        }
    }
    fn make(t:T)->Self{
        Self{
            val:Some(t),
        }
    }
    fn f(&self,t:&Self)->Self{
        if self.val.is_none() {*t}
        else if t.val.is_none() {*self}
        else{
            Self{
                val:Some(T::max(self.val.unwrap(),t.val.unwrap())),
            }
        }
    }
    fn get(self)->Option<T>{
        self.val
    }
}

}  // mod max

pub mod min {

use crate::rust::libs::algebraic_structure::monoid::monoid::Monoid;
use std::option::Option;
#[derive(Debug,Copy,Clone)]
pub struct Min<T>{
    val:Option<T>,
}
impl<T:Ord+Copy> Monoid for Min<T>{
    type MonoidType=T;
    fn new()->Self{
        Self{
            val:None,
        }
    }
    fn make(t:T)->Self{
        Self{
            val:Some(t),
        }
    }
    fn f(&self,t:&Self)->Self{
        if self.val.is_none() {*t}
        else if t.val.is_none() {*self}
        else{
            Self{
                val:Some(T::min(self.val.unwrap(),t.val.unwrap())),
            }
        }
    }
    fn get(self)->Option<T>{
        self.val
    }
}

}  // mod min

pub mod monoid {

pub trait Monoid{
    type MonoidType;
    fn new()->Self;
    fn make(t:Self::MonoidType)->Self;
    fn get(self)->Option<Self::MonoidType>;
    fn f(&self,t:&Self)->Self;
}

}  // mod monoid

pub mod plus {

use crate::rust::libs::algebraic_structure::monoid::monoid::Monoid;
use std::option::Option;
use std::ops::Add;
#[derive(Debug,Copy,Clone)]
pub struct Plus<T>{
    val:Option<T>,
}
impl<T:Add<T,Output=T>+Copy> Monoid for Plus<T>{
    type MonoidType=T;
    fn new()->Self{
        Self{
            val:None,
        }
    }
    fn make(t:T)->Self{
        Self{
            val:Some(t),
        }
    }
    fn f(&self,t:&Self)->Self{
        if self.val.is_none() {*t}
        else if t.val.is_none() {*self}
        else{
            Self{
                val:Some(self.val.unwrap()+t.val.unwrap()),
            }
        }
    }
    fn get(self)->Option<T>{
        self.val
    }
}

}  // mod plus

}  // mod monoid

}  // mod algebraic_structure

pub mod data_structure {

pub mod segment_tree {

use crate::rust::libs::algebraic_structure::monoid::monoid::Monoid;
use std::ops::Range;
pub struct SegmentTree<T>{
    n:usize,
    v:Vec<T>,
}

impl<T:Monoid+Copy+std::fmt::Debug> SegmentTree<T>where T::MonoidType:Copy{
    pub fn new(n:usize)->Self{
        Self{
            n:n,
            v:vec![T::new();n*2],
        }
    }
    pub fn make(v:&[T::MonoidType])->Self{
        let n=v.len();
        let mut tmp=vec![T::new();n*2];
        for i in 0..n{
            tmp[v.len()+i]=T::make(v[i]);
        }
        for i in (1..n).rev(){
            tmp[i]=tmp[i*2].f(&tmp[i*2+1]);
        }
        Self{
            n:n,
            v:tmp,
        }
    }
    pub fn get(&self,ran:Range<usize>)->Option<T::MonoidType>{
        let mut l=ran.start+self.n;
        let mut r=ran.end+self.n;
        let mut s=T::new();
        let mut t=T::new();
        while l<r{
            if (l&1)==1{s=s.f(&self.v[l]);l+=1;}
            if (r&1)==1{r-=1;t=self.v[r].f(&t);}
            l>>=1;r>>=1;
        }
        s.f(&t).get()
    }
    pub fn set(&mut self,t:usize,val:T::MonoidType){
        let mut tp=t+self.n;
        self.v[tp]=self.v[tp].f(&T::make(val));
        while tp>1{
            tp=tp>>1;
            self.v[tp]=self.v[tp*2].f(&self.v[tp*2+1]);
        }
    }
    pub fn out(self){
        println!("{:?}",self.v);
    }
}

}  // mod segment_tree

pub mod union_find {


pub struct UF{
    par:Vec<usize>,
    rank:Vec<usize>,
}
impl UF{
    pub fn new(n:usize)->UF{
        let mut v=vec![0;n];
        for i in 0..n{
            v[i]=i;
        }
        UF{
            par:v,
            rank:vec![1;n],
        }
    }
    pub fn root(&mut self,x:usize)->usize{
        if x==self.par[x]{
            x
        }else{
            let par=self.par[x];
            let res=self.root(par);
            self.par[x]=res;
            res
        }
    }
    pub fn same(&mut self,a:usize,b:usize)->bool{
        self.root(a)==self.root(b)
    }
    pub fn unite(&mut self,a:usize,b:usize)->bool{
        let ap=self.root(a);
        let bp=self.root(b);
        if ap==bp{
            return false;
        }
        if self.rank[ap]<self.rank[bp]{
            self.par[bp]=ap;
            self.rank[ap]+=self.rank[bp];
        }else{
            self.par[ap]=bp;
            self.rank[bp]+=self.rank[ap];
        }
        return true;
    }
    pub fn size(&mut self,a:usize)->usize{
        let ap=self.root(a);
        self.rank[ap]
    }
}

}  // mod union_find

}  // mod data_structure

pub mod graph {

pub mod flow {

pub mod min_cost_flow {


use std::collections::VecDeque;
use std::cmp::min;
#[derive(Copy, Clone)]
pub struct Edge{
    pub to:usize,
    pub cap:i64,
    pub cost:i64,
    pub rev:usize,
    pub is_rev:bool,
}
#[derive(Clone)]
pub struct MinCostFlow{
    v:usize,
    ans:i64,
    pub e:Vec<Vec<Edge>>,
    d:Vec<i64>,
    active:VecDeque<usize>,
    eps:i64,
}

impl MinCostFlow{
    pub fn new(v:usize)->Self{
        Self{
            v:v,
            ans:0,
            e:vec![Vec::<Edge>::new();v],
            d:vec![0;v],
            active:VecDeque::<usize>::new(),
            eps:1i64<<60,
        }
    }
    pub fn add_edge(&mut self,from:usize,to:usize,cap:i64,cost:i64){
        if cost>=0{
            {
                let tmp=Edge{
                    to:to,
                    cap:cap,
                    cost:cost*(self.v as i64),
                    rev:self.e[to].len(),
                    is_rev:false,
                };
                self.e[from].push(tmp);
            }
            {
                let tmp=Edge{
                    to:from,
                    cap:0,
                    cost:-cost*(self.v as i64),
                    rev:self.e[from].len()-1,
                    is_rev:true,
                };
                self.e[to].push(tmp);
            }
        }else{
            {
                let tmp=Edge{
                    to:to,
                    cap:0,
                    cost:cost*(self.v as i64),
                    rev:self.e[to].len(),
                    is_rev:true,
                };
                self.e[from].push(tmp);
            }
            {
                let tmp=Edge{
                    to:from,
                    cap:cap,
                    cost:-cost*(self.v as i64),
                    rev:self.e[from].len()-1,
                    is_rev:false,
                };
                self.e[to].push(tmp);
            }
            self.ans+=cap*cost;
            self.d[from]-=cap;
            self.d[to]+=cap;
        }
    }
    pub fn flow(&mut self)->i64{
        let mut p=vec![0i64;self.v];
        while self.eps!=1{
            self.eps>>=1;
            for i in 0..self.v{
                for j in 0..self.e[i].len(){
                    let ed=self.e[i][j];
                    if ed.is_rev{
                        continue;
                    }
                    if ed.cost-p[i]+p[ed.to]<0{
                        let f=ed.cap;
                        self.e[i][j].cap-=f;
                        self.d[i]-=f;
                        self.d[ed.to]+=f;
                        self.e[ed.to][ed.rev].cap+=f;
                    }
                    if ed.cost-p[i]+p[ed.to]>0{
                        let f=-self.e[ed.to][ed.rev].cap;
                        self.e[i][j].cap-=f;
                        self.d[i]-=f;
                        self.d[ed.to]+=f;
                        self.e[ed.to][ed.rev].cap+=f;
                    }
                }
            }
            for i in 0..self.v{
                if self.d[i]>0{
                    self.active.push_back(i);
                }
            }
            while !self.active.is_empty(){
                let node=self.active.pop_front().unwrap();
                if self.d[node]<=0{continue;}
                let mut b=false;
                for j in 0..self.e[node].len(){
                    let ed=self.e[node][j];
                    if self.d[node]==0{break;}
                    if -self.eps<=ed.cost-p[node]+p[ed.to]&&ed.cost-p[node]+p[ed.to]<0{
                        let f=min(self.d[node],ed.cap);
                        if f==0{continue;}
                        self.e[node][j].cap-=f;
                        self.d[node]-=f;
                        self.d[ed.to]+=f;
                        self.e[ed.to][ed.rev].cap+=f;
                        if self.d[ed.to]>0{
                            self.active.push_back(ed.to);
                        }
                        b=true;
                    }
                }
                if self.d[node]>0{self.active.push_back(node);}
                if !b{p[node]+=self.eps;}
            }
        }
        for i in 0..self.v{
            for j in 0..self.e[i].len(){
                let ed=self.e[i][j];
                if ed.is_rev{continue;}
                self.ans+=self.e[ed.to][ed.rev].cap*ed.cost/(self.v as i64);
            }
        }
        self.ans
    }
    pub fn run(&mut self,s:usize,t:usize,f:i64)->i64{
        self.d[s]+=f;
        self.d[t]-=f;
        self.flow()
    }

}

}  // mod min_cost_flow

}  // mod flow

}  // mod graph

pub mod math {

pub mod floor_sum {


pub fn floor_sum(n_:usize,m_:usize,a_:usize,b_:usize)->usize{
    let (n,m,mut a,mut b)=(n_ as i64,m_ as i64,a_ as i64,b_ as i64);
    let mut ans:usize=0;
    if a>=m{
        ans+=((n-1)*n*(a/m)/2) as usize;
        a%=m;
    }
    if b>=m{
        ans+=(n*(b/m)) as usize;
        b%=m;
    }
    let y_max:i64=(a*n+b)/m;
    let x_max:i64=y_max*m-b;
    if y_max==0{
        return ans;
    }
    ans+=((n-(x_max+a-1)/a)*y_max) as usize;
    ans+=floor_sum(y_max as usize,a as usize,m as usize,((a-x_max%a)%a) as usize);
    return ans;
}

}  // mod floor_sum

pub mod mod_int {

use std::ops::{Add,AddAssign,Sub,SubAssign,Mul,MulAssign,Div,DivAssign,Neg};
use std::str::FromStr;
use std::num::ParseIntError;
use std::iter::{Sum,Product};

pub fn inv_mod(a:u64,m:u64)->u64{
    let m=m as i64;
    let mut a=a as i64;
    let mut b=m as i64;
    let mut u=1i64;
    let mut v=0i64;
    while b>0 {
        let t=a/b;
        a-=t*b;
        u-=t*v;
        std::mem::swap(&mut a,&mut b);
        std::mem::swap(&mut u,&mut v);
    }
    let ans =(if u>=0{u%m}else{m+(u%m)%m})as u64;
    ans
}

pub trait Mod:Sized{
    fn m()->u32;
    fn m64()->u64;
    fn mi64()->i64;
}
#[macro_export]
macro_rules! const_mod{
    ($st:ident,$m:expr)=>{
        struct $st{}
        impl Mod for $st {
            fn m()->u32{$m}
            fn m64()->u64{$m as u64}
            fn mi64()->i64{$m as i64}
        }
        //const MAX=10000000;
        type Fp=ModInt<P>;
        //const fact_table:[Fp;MAX]=(0..MAX)
    }
}
pub struct ModInt<M:Mod>{a:u32,_p:std::marker::PhantomData<M>}

impl<M: Mod> ModInt<M>{
    pub fn new(a:u32)->Self{
        ModInt{a,_p:std::marker::PhantomData}
    }
    pub fn newu64(a:u64)->Self{
        ModInt{a:(a%M::m64())as u32,_p:std::marker::PhantomData}
    }
    pub fn newi64(a:i64)->Self{
        ModInt{a:((a%M::mi64()+M::mi64())%M::mi64()) as u32,_p:std::marker::PhantomData}
    }
    pub fn value(&self)->u32{self.a}
    pub fn pow(&self,p:u64)->Self{
        let mut exp=p;
        let mut now=*self;
        let mut ans=ModInt::new(1);
        while exp>0{
            if (exp&1)==1{ans*=now;}
            now*=now;
            exp>>=1;
        }
        ans
    }
    // pub fn comb(k:i32)->Self{
    //     Self::new(k)
    // }
    pub fn inv(&self)->Self{Self::new(inv_mod(self.a as u64,M::m64())as u32)}
}

impl<M:Mod>Clone for ModInt<M>{
    fn clone(&self)->Self{ModInt::new(self.a)}
}
impl<M:Mod>Copy for ModInt<M>{}
impl<M:Mod>From<i64>for ModInt<M>{
    fn from(i:i64)->Self{Self::newi64(i)}
}
impl<M:Mod>From<i32>for ModInt<M>{
    fn from(i:i32)->Self{Self::newi64(i as i64)}
}

impl<M:Mod>FromStr for ModInt<M>{
    type Err = ParseIntError;
    fn from_str(i:&str)->Result<Self, Self::Err>{
        let res=i.parse::<i64>()?;
        Ok(Self::newi64(res))
    }
}
impl<M:Mod>Sum for ModInt<M>{
    fn sum<I>(iter: I) -> Self where I: Iterator<Item = Self>,{
        iter.fold(Self::new(0), |b, i| b + i)
    }
}

impl<M:Mod>Product for ModInt<M>{
    fn product<I>(iter: I) -> Self where I: Iterator<Item = Self>,{
        iter.fold(Self::new(0), |b, i| b * i)
    }
}
impl<M:Mod>Add for ModInt<M>{
    type Output=Self;
    fn add(self,rhs:Self)->Self{
        let a=self.a+rhs.a;
        ModInt::new(if a>=M::m(){a-M::m()}else{a})
    }
}
impl<M:Mod>Sub for ModInt<M>{
    type Output=Self;
    fn sub(self,rhs:Self)->Self{
        ModInt::new(if self.a<rhs.a{M::m()+self.a-rhs.a}else{self.a-rhs.a})
    }
}
impl<M:Mod>Mul for ModInt<M>{
    type Output=Self;
    fn mul(self,rhs:Self)->Self{
        ModInt::newu64(self.a as u64*rhs.a as u64)
    }
}
impl<M:Mod>Div for ModInt<M>{
    type Output=Self;
    fn div(self,rhs:Self)->Self{
        self*rhs.inv()
    }
}
impl<M:Mod>Neg for ModInt<M> {
    type Output = Self;
    fn neg(self) -> Self {
        ModInt::new(M::m()-self.value())
    }
}
impl<M:Mod>AddAssign for ModInt<M>{fn add_assign(&mut self,rhs:Self){*self=*self+rhs;}}
impl<M:Mod>SubAssign for ModInt<M>{fn sub_assign(&mut self,rhs:Self){*self=*self-rhs;}}
impl<M:Mod>MulAssign for ModInt<M>{fn mul_assign(&mut self,rhs:Self){*self=*self*rhs;}}
impl<M:Mod>DivAssign for ModInt<M>{fn div_assign(&mut self,rhs:Self){*self=*self/rhs;}}

impl<M:Mod>std::fmt::Debug for ModInt<M>{
    fn fmt(&self,f:&mut std::fmt::Formatter)->Result<(),std::fmt::Error>{
        write!(f,"M{}",self.a)
    }
}
impl<M:Mod> std::fmt::Display for ModInt<M> {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
        self.value().fmt(f)
    }
}

// impl<M:Mod> Sum for ModInt<M>{
//     fn sum<I>(iter: I) -> ModInt<M>
//     where
//         I: Iterator<Item = ModInt<M>>,
//     {
//         iter.fold(Self::new(0), |b, i| b + i)
//     }
// }

}  // mod mod_int

pub mod prime_factor {

use std::collections::BTreeMap;
pub fn prime_factor(n:usize)->BTreeMap<usize,usize>{
    let mut ret:BTreeMap<usize,usize>=BTreeMap::<usize,usize>::new();
    let mut m=n;
    let mut i=2;
    while i*i<=m {
        while m%i==0{
            let tmp=ret.entry(i).or_insert(0);
            *tmp+=1;
            m/=i;
        }
        i+=1;
    }
    ret
}

}  // mod prime_factor

}  // mod math

pub mod util {

pub mod io {

#[macro_export]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let mut s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

#[macro_export]
macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
    ($iter:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

#[macro_export] 
macro_rules! read_value {
    ($iter:expr, [tree;$len:expr]) => {
        {
            let mut g:Vec<Vec<usize>>=vec![Vec::new();$len];
            for (s,t) in read_value!($iter,[(usize1,usize1);$len-1]){
                g[s].push(t);
                g[t].push(s);
            }
            g
        }
    };
    ($iter:expr, [graph;$vertex:expr,$edge:expr]) => {
        {
            let mut g:Vec<Vec<usize>>=vec![Vec::new();$vertex];
            for (s,t) in read_value!($iter,[(usize1,usize1);$edge]){
                g[s].push(t);
                g[t].push(s);
            }
            g
        }
    };
    ($iter:expr, [directed;$vertex:expr,$edge:expr]) => {
        {
            let mut g:Vec<Vec<usize>>=vec![Vec::new();$vertex];
            for (s,t) in read_value!($iter,[(usize1,usize1);$edge]){
                g[s].push(t);
            }
            g
        }
    };
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, bytes) => {
        read_value!($iter, String).into_bytes()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };
    ($iter:expr, Fp) => {
        Fp::newi64(read_value!($iter,i64))
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}


}  // mod io

pub mod lower_bound {

pub trait LowerBound<T> {
    fn lower_bound(&self,v:T)->usize;
}

impl<T:Ord> LowerBound<T> for Vec<T>{
    fn lower_bound(&self,x:T)->usize{
        let mut l:usize=0;
        let mut r:usize=self.len()+1;
        while r-l>1{
            let m=(l+r)/2;
            if self[m-1]<x {l=m;}
            else {r=m;}
        }
        r-1
    }
}
pub trait UpperBound<T> {
    fn upper_bound(&self,v:T)->usize;
}

impl<T:Ord> UpperBound<T> for Vec<T>{
    fn upper_bound(&self,x:T)->usize{
        let mut l:usize=0;
        let mut r:usize=self.len()+1;
        while r-l>1{
            let m=(l+r)/2;
            if self[m-1]<=x {l=m;}
            else {r=m;}
        }
        r-1
    }
}

}  // mod lower_bound

pub mod memoize {

#[macro_export]
macro_rules! make_vec{
    ( $val:expr , $head:expr)=>{
        vec![$val;$head]
    };
    ( $val:expr , $head:expr , $($tail:expr),+ )=>{
        vec![make_vec!($val,$($tail),+);$head]
    };
}

#[macro_export]
macro_rules! access{
    ($v:expr,$tail:expr)=>{
        $v[$tail]
    };
    ($v:expr,$tail:expr,)=>{
        $v[$tail]
    };
    ($v:expr,$($r:expr),+,$tail:expr)=>{
        access!($v,$($r),+)[$tail]
    };
}
#[macro_export]
macro_rules! make_type_vec{
    ($v:ty,$tail:expr)=>{
        Vec<$v>
    };
    ($v:ty,$tail:expr,)=>{
        Vec<$v>
    };
    ($v:ty,$($r:expr),+,$tail:expr)=>{
        Vec<make_type_vec!($v,$($r),+)>
    };
}
#[macro_export]
macro_rules! access_local{
    ($v:expr,$($r:expr),+)=>{
        v.with()
    };
}
#[macro_export]
macro_rules! memoized{
    (fn $func:tt ($($val:tt [$r:expr]:$typ:ty),*)->$ret:ty{$($t:tt)*} ( $($st:expr),* );)=>{
        {
            use std::cell::RefCell;
            use std::borrow::BorrowMut;
            fn $func($($val : $typ),*)->$ret{
                thread_local! {
                    pub static MEMO :RefCell<make_type_vec!(Option<$ret>,$($r),*)>= RefCell::new(make_vec!(None,$($r),*));
                }                
                let __function__=|$($val : $typ),*|->$ret {$($t)*};
                
                if MEMO.with(|v|access!(v.borrow(),$($val),*).is_none()){
                    MEMO.with(|v|access!(v.borrow_mut(),$($val),*)=Some(__function__($($val),*)));
                }
                MEMO.with(|v|access!(v.borrow(),$($val),*).clone()).unwrap()
            }
            $func($($st),*)
        }
    };
    (fn $func:tt ($($val:tt [$r:expr]:$typ:ty),*)[$($cap_val:tt : $cap_typ:ty),*]->$ret:ty{$($t:tt)*} $func2:tt( $($st:expr),* );)=>{
        {
            use std::cell::RefCell;
            assert!(stringify!($func)==stringify!($func2));
            fn __function2__($($val : $typ),*,$($cap_val:$cap_typ),*)->$ret{
                thread_local! {
                    pub static MEMO :RefCell<make_type_vec!(Option<$ret>,$($r),*)>= RefCell::new(make_vec!(None,$($r),*));
                }
                let $func=|$($val : $typ),*|->$ret {__function2__($($val),*,$($cap_val),*)};
                let __function__=|$($val : $typ),*,$($cap_val:$cap_typ),*|->$ret {
                    $($t)*
                };
                if MEMO.with(|v|access!(v.borrow(),$($val),*).is_none()){
                    MEMO.with(|v|access!(v.borrow_mut(),$($val),*)=Some(__function__($($val),*,$($cap_val),*)));
                }
                MEMO.with(|v|access!(v.borrow(),$($val),*).clone()).unwrap()
            }
            __function2__($($st),*)
        }
    };
}

}  // mod memoize

pub mod util {

use std::ops::Add;
pub trait CumSum<T> {
    fn cumsum(&self)->Vec<T>;
}

impl<T:Add<Output=T>+Default+Copy> CumSum<T> for Vec<T>{
    fn cumsum(&self)->Vec<T>{
        let mut ret=Vec::new();
        ret.push(T::default());
        for i in 0..self.len(){
            ret.push(ret[i]+self[i]);
        }
        ret
    }
}


pub fn type_of<T>(_: T) -> String{
    let a = std::any::type_name::<T>();
    return a.to_string();
}

#[macro_export]
macro_rules! btoi{
    ( $val:expr)=>{
        if $val{1}else{0}
    };
}

pub trait Isomorphism<T> {
    fn isomorphism(&self)->Vec<T>;
}

use std::ops::{Rem,Div,Mul};
use std::default::Default;
pub fn gcd<T:Rem<Output=T>+Eq+Default+Copy>(a:T,b:T)->T{
    if b==T::default(){a}else{gcd(b, a % b)}
}
pub fn lcm<T:Rem<Output=T>+Eq+Default+Copy+Div<Output=T>+Mul<Output=T>>(a:T,b:T)->T{
    a/gcd(a,b)*b
}

use crate::rust::libs::util::lower_bound::*;
pub trait Lis<T> {
    fn lis(&self)->Vec<T>;
}

impl<T:Ord+Copy> Lis<T> for Vec<T>{
    fn lis(&self)->Vec<T>{
        let mut ret=Vec::<T>::new();
        for i in 0..self.len(){
            let t=ret.lower_bound(self[i]);
            if t==ret.len(){ret.push(self[i]);}
            else {ret[t]=self[i];}
        }
        ret
    }
}

use std::convert::TryInto;
pub trait Ac<T>{
    fn ac(&self,idx:T)->T;
}
impl<T> Ac<T> for Vec<T>where T:TryInto<usize>+Copy{
    fn ac(&self,idx:T)->T{
        match idx.try_into(){
            Ok(i)=>self[i],
            Err(_e)=>{
                panic!("cannot convert to usize");
            }
        }
    }
}

pub fn d(s:usize,t:usize)->usize{
    ((s as i64)-(t as i64)).abs()as usize
}

}  // mod util

}  // mod util

}  // mod libs

}  // mod rust