You are given a weighted tree consisting of $$$n$$$ vertices. Recall that a tree is a connected graph without cycles. Vertices $$$u_i$$$ and $$$v_i$$$ are connected by an edge with weight $$$w_i$$$.You are given $$$m$$$ queries. The $$$i$$$-th query is given as an integer $$$q_i$$$. In this query you need to calculate the number of pairs of vertices $$$(u, v)$$$ ($$$u < v$$$) such that the maximum weight of an edge on a simple path between $$$u$$$ and $$$v$$$ doesn't exceed $$$q_i$$$.

The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 2 \cdot 10^5$$$) — the number of vertices in the tree and the number of queries. Each of the next $$$n - 1$$$ lines describes an edge of the tree. Edge $$$i$$$ is denoted by three integers $$$u_i$$$, $$$v_i$$$ and $$$w_i$$$ — the labels of vertices it connects ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$) and the weight of the edge ($$$1 \le w_i \le 2 \cdot 10^5$$$). It is guaranteed that the given edges form a tree. The last line of the input contains $$$m$$$ integers $$$q_1, q_2, \dots, q_m$$$ ($$$1 \le q_i \le 2 \cdot 10^5$$$), where $$$q_i$$$ is the maximum weight of an edge in the $$$i$$$-th query.

Print $$$m$$$ integers — the answers to the queries. The $$$i$$$-th value should be equal to the number of pairs of vertices $$$(u, v)$$$ ($$$u < v$$$) such that the maximum weight of an edge on a simple path between $$$u$$$ and $$$v$$$ doesn't exceed $$$q_i$$$. Queries are numbered from $$$1$$$ to $$$m$$$ in the order of the input.

NoteThe picture shows the tree from the first example:

package main

import (
    "bufio"
    "fmt"
    "os"
    "sort"
)

type t1 []int
type t2 []int

var tot int64
var u, v, w, q []int
var par, sz, ans []int64
var p1 t1
var p2 t2

func (p t1) Len() int           { return len(p) }
func (p t1) Less(i, j int) bool { return w[p[i]] < w[p[j]] }
func (p t1) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }

func (p t2) Len() int           { return len(p) }
func (p t2) Less(i, j int) bool { return q[p[i]] < q[p[j]] }
func (p t2) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }

func find(idx int) int {
    for int(par[idx]) != idx {
        idx = int(par[idx])
    }
    return idx
}

func join(ru, rv int) {
    if ru == rv {
        return
    }
    if sz[ru] < sz[rv] {
        ru, rv = rv, ru
    }
    tot -= sz[ru] * (sz[ru] - 1) / 2
    tot -= sz[rv] * (sz[rv] - 1) / 2
    sz[ru] += sz[rv]
    tot += sz[ru] * (sz[ru] - 1) / 2
    sz[rv] = 0
    par[rv] = int64(ru)
}

func main() {
    in := bufio.NewReader(os.Stdin)
    var n, m int
    fmt.Fscan(in, &n, &m)
    u = make([]int, n)
    v = make([]int, n)
    w = make([]int, n)
    ans = make([]int64, m + 1)
    q = make([]int, m + 1)
    par = make([]int64, n + 1)
    sz = make([]int64, n + 1)
    p1 = make(t1, n)
    p2 = make(t2, m + 1)
    for i := 1; i <= n; i++ {
        sz[i] = 1
        par[i] = int64(i)
    }
    for i := 1; i < n; i++ {
        fmt.Fscan(in, &u[i], &v[i], &w[i])
        p1[i] = i
    }
    sort.Sort(t1(p1[1:]))
    for i := 1; i <= m; i++ {
        fmt.Fscan(in, &q[i])
        p2[i] = i
    }
    sort.Sort(t2(p2[1:]))
    cnt := 1
    for i := 1; i <= m; i++ {
        for cnt < n && w[p1[cnt]] <= q[p2[i]] {
            join(find(u[p1[cnt]]), find(v[p1[cnt]]))
            cnt++
        }
        ans[p2[i]] = tot
    }
    for i := 1; i <= m; i++ {
        fmt.Printf("%v ", ans[i])
    }
}

package main
import "fmt"
import "bufio"
import "os"
import "sort"

var input *bufio.Reader

const N = 200005

type edge struct {
	u,v,w int
}

type arrs []edge

func (arr arrs) Len() int{
	return len(arr)
}

func (arr arrs) Less(i, j int) bool {
	return arr[i].w < arr[j].w
}

func (arr arrs) Swap(i, j int) {
	arr[i], arr[j] = arr[j], arr[i]
}

var sz []int
var fa []int

func find_fa(u int) int {
	if fa[u]==u {
		return u
	}
	fa[u] = find_fa(fa[u])
	return fa[u]
}

func union(u, v int) {
	u = find_fa(u)
	v = find_fa(v)
	fa[u] = v
	sz[v] += sz[u]
}

func main(){
	input = bufio.NewReader(os.Stdin)
	//output := bufio.NewWriter(os.Stdout)
	var n,m int
	fmt.Fscanf(input, "%d%d\n", &n, &m)
	e := make(arrs, n-1)
	sz = make([]int, 200005)
	fa = make([]int, 200005)
	for i:=1; i<=n; i++ {
		fa[i] = i
		sz[i] = 1
	}
	for i:=0; i<n-1; i++ {
		fmt.Fscanf(input, "%d%d%d\n", &e[i].u, &e[i].v, &e[i].w)
	}
	sort.Sort(e)
	/*
	for i:=0; i<n-1; i++ {
		fmt.Printf("%d %d %d\n", e[i].u, e[i].v, e[i].w)
	}
	*/
	cnt := make([]int64, 200005)
	for i:=0; i<n-1; i++ {
		u := find_fa(e[i].u)
		v := find_fa(e[i].v)
		cnt[e[i].w] += int64(sz[u])*int64(sz[v])
		union(u, v)
	}

	for i:=2; i<=200000; i++ {
		cnt[i] += cnt[i-1]
	}

	for i:=1; i<=m; i++ {
		var x int
		fmt.Fscanf(input, "%d", &x)
		fmt.Printf("%d ", cnt[x])
		//fmt.Fprintf(output, "%d ", cnt[x])
	}

}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

type edge struct {
	x int
	y int
	c int
}

type pair struct {
	idx int
	val int
}

var dad [200100]int
var sz [200100]int64
var rs [200100]int64
var ans int64

func find(x int) int {
	if x == dad[x] {
		return x
	}
	y := find(dad[x])
	dad[x] = y
	return y
}

func join(x, y int) {
	x = find(x)
	y = find(y)

	ans -= int64(sz[x] * (sz[x] - 1) / 2)
	ans -= int64(sz[y] * (sz[y] - 1) / 2)
	sz[x] += sz[y]
	sz[y] = 0
	dad[y] = x
	ans += int64(sz[x] * (sz[x] - 1) / 2)
}

func main() {
	in := bufio.NewReader(os.Stdin)

	var n, q int
	fmt.Fscan(in, &n, &q)
	E := make([]edge, n - 1)
	a := make([]pair, q)

	for i := 0; i < n - 1; i++ {
		fmt.Fscan(in, &E[i].x, &E[i].y, &E[i].c)
	}

	for i := 0; i < q; i++ {
		fmt.Fscan(in, &a[i].val)
		a[i].idx = i
	}

	sort.Slice(a, func(i, j int) bool {
		return a[i].val < a[j].val
	})

	sort.Slice(E, func(i, j int) bool {
		return E[i].c < E[j].c
	})

	for i := 1; i <= n; i++ {
		dad[i] = i
		sz[i] = 1
	}

	p := 0
	for i := 0; i < q; i++ {
		for p < n - 1 {
			if E[p].c <= a[i].val {
				join(E[p].x, E[p].y)
				p++
			} else {
				break
			}
		}
		rs[a[i].idx] = ans
	}

	for i := 0; i < q; i++ {
		fmt.Print(rs[i], " ")
	}
}

package main

import (
    "bufio"
    "fmt"
    "os"
    "sort"
)

type t1 []int
type t2 []int

var tot int
var u, v, w, q, ans, par, sz []int
var p1 t1
var p2 t2

func (p t1) Len() int           { return len(p) }
func (p t1) Less(i, j int) bool { return w[p[i]] < w[p[j]] }
func (p t1) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }

func (p t2) Len() int           { return len(p) }
func (p t2) Less(i, j int) bool { return q[p[i]] < q[p[j]] }
func (p t2) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }

func find(idx int) int {
    for par[idx] != idx {
        idx = par[idx]
    }
    return idx
}

func join(ru, rv int) {
    if ru == rv {
        return
    }
    if sz[ru] < sz[rv] {
        ru, rv = rv, ru
    }
    tot -= sz[ru] * (sz[ru] - 1) / 2
    tot -= sz[rv] * (sz[rv] - 1) / 2
    sz[ru] += sz[rv]
    tot += sz[ru] * (sz[ru] - 1) / 2
    sz[rv] = 0
    par[rv] = ru
}

func main() {
    in := bufio.NewReader(os.Stdin)
    var n, m int
    fmt.Fscan(in, &n, &m)
    u = make([]int, n)
    v = make([]int, n)
    w = make([]int, n)
    ans = make([]int, m+1)
    q = make([]int, m+1)
    par = make([]int, n+1)
    sz = make([]int, n+1)
    p1 = make(t1, n)
    p2 = make(t2, m+1)
    for i := 1; i <= n; i++ {
        sz[i] = 1
        par[i] = i
    }
    for i := 1; i < n; i++ {
        fmt.Fscan(in, &u[i], &v[i], &w[i])
        p1[i] = i
    }
    sort.Sort(t1(p1[1:]))
    for i := 1; i <= m; i++ {
        fmt.Fscan(in, &q[i])
        p2[i] = i
    }
    sort.Sort(t2(p2[1:]))
    cnt := 1
    for i := 1; i <= m; i++ {
        for cnt < n && w[p1[cnt]] <= q[p2[i]] {
            join(find(u[p1[cnt]]), find(v[p1[cnt]]))
            cnt++
        }
        ans[p2[i]] = tot
    }
    for i := 1; i <= m; i++ {
        fmt.Printf("%v ", ans[i])
    }
}

package main
import "fmt"
import "bufio"
import "os"
import "sort"

var input *bufio.Reader

const N = 200005

type edge struct {
	u,v,w int
}

type arrs []edge

func (arr arrs) Len() int{
	return len(arr)
}

func (arr arrs) Less(i, j int) bool {
	return arr[i].w < arr[j].w
}

func (arr arrs) Swap(i, j int) {
	arr[i], arr[j] = arr[j], arr[i]
}

var sz []int
var fa []int

func find_fa(u int) int {
	if fa[u]==u {
		return u
	}
	fa[u] = find_fa(fa[u])
	return fa[u]
}

func union(u, v int) {
	u = find_fa(u)
	v = find_fa(v)
	fa[u] = v
	sz[v] += sz[u]
}

func main(){
	input = bufio.NewReader(os.Stdin)
	output := bufio.NewWriter(os.Stdout)
	var n,m int
	fmt.Fscanf(input, "%d%d\n", &n, &m)
	e := make(arrs, n-1)
	sz = make([]int, 200005)
	fa = make([]int, 200005)
	for i:=1; i<=n; i++ {
		fa[i] = i
		sz[i] = 1
	}
	for i:=0; i<n-1; i++ {
		fmt.Fscanf(input, "%d%d%d\n", &e[i].u, &e[i].v, &e[i].w)
	}
	sort.Sort(e)
	/*
	for i:=0; i<n-1; i++ {
		fmt.Printf("%d %d %d\n", e[i].u, e[i].v, e[i].w)
	}
	*/
	cnt := make([]int64, 200005)
	for i:=0; i<n-1; i++ {
		u := find_fa(e[i].u)
		v := find_fa(e[i].v)
		cnt[e[i].w] += int64(sz[u])*int64(sz[v])
		union(u, v)
	}

	for i:=2; i<=200000; i++ {
		cnt[i] += cnt[i-1]
	}

	for i:=1; i<=m; i++ {
		var x int
		fmt.Fscanf(input, "%d", &x)
		//fmt.Printf("%d ", cnt[x])
		fmt.Fprintf(output, "%d ", cnt[x])
	}

}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

type edge struct {
	x int
	y int
	c int
}

type pair struct {
	idx int
	val int
}

var dad [200100]int
var sz [200100]int
var rs [200100]int64
var ans int64

func find(x int) int {
	if x == dad[x] {
		return x
	}
	y := find(dad[x])
	dad[x] = y
	return y
}

func join(x, y int) {
	x = find(x)
	y = find(y)

	ans -= int64(sz[x] * (sz[x] - 1) / 2)
	ans -= int64(sz[y] * (sz[y] - 1) / 2)
	sz[x] += sz[y]
	sz[y] = 0
	dad[y] = x
	ans += int64(sz[x] * (sz[x] - 1) / 2)
}

func main() {
	in := bufio.NewReader(os.Stdin)

	var n, q int
	fmt.Fscan(in, &n, &q)
	E := make([]edge, n - 1)
	a := make([]pair, q)

	for i := 0; i < n - 1; i++ {
		fmt.Fscan(in, &E[i].x, &E[i].y, &E[i].c)
	}

	for i := 0; i < q; i++ {
		fmt.Fscan(in, &a[i].val)
		a[i].idx = i
	}

	sort.Slice(a, func(i, j int) bool {
		return a[i].val < a[j].val
	})

	sort.Slice(E, func(i, j int) bool {
		return E[i].c < E[j].c
	})

	for i := 1; i <= n; i++ {
		dad[i] = i
		sz[i] = 1
	}

	p := 0
	for i := 0; i < q; i++ {
		for p < n - 1 {
			if E[p].c <= a[i].val {
				join(E[p].x, E[p].y)
				p++
			} else {
				break
			}
		}
		rs[a[i].idx] = ans
	}

	for i := 0; i < q; i++ {
		fmt.Print(rs[i], " ")
	}
}


Palo Alto is an unusual city because it is an endless coordinate line. It is also known for the office of Lyft Level 5.Lyft has become so popular so that it is now used by all $$$m$$$ taxi drivers in the city, who every day transport the rest of the city residents — $$$n$$$ riders.Each resident (including taxi drivers) of Palo-Alto lives in its unique location (there is no such pair of residents that their coordinates are the same).The Lyft system is very clever: when a rider calls a taxi, his call does not go to all taxi drivers, but only to the one that is the closest to that person. If there are multiple ones with the same distance, then to taxi driver with a smaller coordinate is selected.But one morning the taxi drivers wondered: how many riders are there that would call the given taxi driver if they were the first to order a taxi on that day? In other words, you need to find for each taxi driver $$$i$$$ the number $$$a_{i}$$$ — the number of riders that would call the $$$i$$$-th taxi driver when all drivers and riders are at their home?The taxi driver can neither transport himself nor other taxi drivers.

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n,m \le 10^5$$$) — number of riders and taxi drivers. The second line contains $$$n + m$$$ integers $$$x_1, x_2, \ldots, x_{n+m}$$$ ($$$1 \le x_1 < x_2 < \ldots < x_{n+m} \le 10^9$$$), where $$$x_i$$$ is the coordinate where the $$$i$$$-th resident lives.  The third line contains $$$n + m$$$ integers $$$t_1, t_2, \ldots, t_{n+m}$$$ ($$$0 \le t_i \le 1$$$). If $$$t_i = 1$$$, then the $$$i$$$-th resident is a taxi driver, otherwise $$$t_i = 0$$$. It is guaranteed that the number of $$$i$$$ such that $$$t_i = 1$$$ is equal to $$$m$$$.

Print $$$m$$$ integers $$$a_1, a_2, \ldots, a_{m}$$$, where $$$a_i$$$ is the answer for the $$$i$$$-th taxi driver. The taxi driver has the number $$$i$$$ if among all the taxi drivers he lives in the $$$i$$$-th smallest coordinate (see examples for better understanding).

NoteIn the first example, we have only one taxi driver, which means an order from any of $$$n$$$ riders will go to him.In the second example, the first taxi driver lives at the point with the coordinate $$$2$$$, and the second one lives at the point with the coordinate $$$6$$$. Obviously, the nearest taxi driver to the rider who lives on the $$$3$$$ coordinate is the first one, and to the rider who lives on the coordinate $$$5$$$ is the second one. The rider who lives on the $$$4$$$ coordinate has the same distance to the first and the second taxi drivers, but since the first taxi driver has a smaller coordinate, the call from this rider will go to the first taxi driver.In the third example, we have one rider and the taxi driver nearest to him is the fourth one.

package main

import (
	"os"
	"io/ioutil"
	"bytes"
	"fmt"
)

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)

	var n, m int
	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	cord := make([]int, n + m)
	rider := make([]int, 0, n)
	taxi := make([]int, 0, m)
	ans := make([]int, m)
	for i := 0;i < n + m;i ++ {
		if i == n + m - 1 {
			fmt.Fscanf(reader, "%d\n", &cord[i])
		} else {
			fmt.Fscanf(reader, "%d ", &cord[i])
		}
	}
	for i := 0;i < n + m;i ++ {
		var b int
		if i == n + m - 1 {
			fmt.Fscanf(reader, "%d\n", &b)
		} else {
			fmt.Fscanf(reader, "%d ", &b)
		}
		if b == 1 {
			taxi = append(taxi, cord[i])
		} else {
			rider = append(rider, cord[i])
		}
	}

	for i, j := 0, 0; i < n; {
		if rider[i] < taxi[0] {
			ans[0] ++
			i ++
		} else if rider[i] > taxi[m - 1] {
			ans[m - 1] ++
			i ++
		} else if rider[i] < taxi[j] {
			i ++
		} else if rider[i] > taxi[j + 1] {
			j ++
		} else {
			if rider[i] - taxi[j] <= taxi[j + 1] - rider[i] {
				ans[j] ++
			} else {
				ans[j + 1] ++
			}
			i ++
		}
	}
	for i := 0;i < m;i ++ {
		fmt.Printf("%d ", ans[i])
	}
}

package main

import (
	"fmt"
	"bufio"
	"os"
)

type FlagStatus struct {
	flag int
	left int
	right int
}
func Slove(left int ,right int ,k int,pos []int)int{
	if left==-1{
		return right
	}
	if right==-1{
		return left
	}
	if (pos[right]-pos[k]<pos[k]-pos[left]){
		return  right
	}else{
		return left
	}
}
var in = bufio.NewReaderSize(os.Stdin, 2<<16)
func main(){
	var x,y int
	var t int
	fmt.Fscan(in, &x,&y)
	pos:=[]int{}
	ans:=[]int{}
    for index:=0;index<x+y;index++{
    	fmt.Fscan(in,&t)
    	pos=append(pos,t)
    	ans=append(ans,0)
	}
	flag:=[]FlagStatus{}
	for index:=0;index<x+y;index++{
		fmt.Fscan(in,&t)
		flag=append(flag,FlagStatus{flag:t})
	}

	left_init:=-1
	for index:=0;index<x+y;index++{
		if flag[index].flag==1{
			left_init=index
			continue
		}
		flag[index].left=left_init
	}
	right_init:=-1
	for index:=x+y-1;index>=0;index--{
		if flag[index].flag==1{
			right_init=index
			continue
		}
		flag[index].right=right_init
	}

	for k,v:=range flag{
		//fmt.Println("flag",v.flag,"left",v.left,"right",v.right)
		if v.flag==1{
			continue
		}
		t:=Slove(v.left,v.right,k,pos)
		ans[t]++
	}
	kong:=0
	for k,v:=range ans{
		if flag[k].flag==1{
			if kong==0{
				fmt.Print(v)
				kong=1
			}else{
				fmt.Print(" ",v)
			}
		}
	}
	fmt.Print("\n")

}

// Codeforces 1075 B Taxi drivers and Lyft train
package main

import (
	"bufio"
	"fmt"
	"strings"
	"strconv"
	"os"
	"sort"
)
var in *bufio.Reader

func readFive() (int,int,int,int,int) {
	var a [5]int
	s, err := in.ReadString('\n')
	if err != nil {
		fmt.Println("first read failure", err)
		panic(err)
	}
	ss := strings.Split(strings.Trim(s, " \n\r"), " ")
	for i:=0; i<len(ss); i++ {
		a[i], err = strconv.Atoi(ss[i])
		if err != nil {
			fmt.Println("n atoi failure", err)
			panic(err)
		}
	}
	return a[0],a[1],a[2],a[3],a[4]
}

func readLineNumbs(n int) []int {
	a := make([]int, n)
	s, err := in.ReadString('\n')
	if err != nil {
		fmt.Println("read failure", err)
		panic(err)
	}
	ss := strings.Split(strings.Trim(s, " \n\r"), " ")
	for j:=0; j<n; j++ {
		aa,err := strconv.Atoi(ss[j])
		if err != nil {
			fmt.Println("aa conv failed", err)
			panic(err)
		}
		a[j] = aa
	}
	return a
}

var n,m,nm int
var x []int
var t []int
var res []int
var cntr []int

func readData() {
	in = bufio.NewReader(os.Stdin)
	n,m,_,_,_ = readFive()
	nm = n + m
	x = readLineNumbs(nm)
	t = readLineNumbs(nm)
	res = make([]int, m)
}

// inverted x, binary search
func pos2idx(pos int) int {
	idx := sort.Search(nm, func(i int) bool { return x[i] >= pos })
	//fmt.Println("pos2idx(", pos, ") ret ", idx)
	return idx
}

// riders in interval fro i0 to i1 (inclusive)
func ridersInterval(i0 int, i1 int) int {
	nrid := cntr[i1+1] - cntr[i0]
	//fmt.Printf("ridersInterval(%d, %d) ret %d\n", i0,i1,nrid)
	return nrid
}

func solve() {
	cnt := 0
	itax := 0
	cntr = make([]int, nm+1)
	tidx := make([]int, m)
	for i:=0; i<nm; i++ {
		cntr[i] = cnt
		if t[i]==0 {
			cnt += 1
		} else {
			tidx[itax] = i
			itax++
		}
	}
	cntr[nm] = cnt
	// counting res
	for it:=0; it<m; it++ {
		if it==m-1 {
			// last interval, only add to last taxist
			res[it] += ridersInterval(tidx[it], nm-1)
		} else {
			// middle, divide interval it, it+1 and add to 2 taxists
			x0,x1 := x[tidx[it]], x[tidx[it+1]]
			midpos := (x0 + x1) / 2
			mididx := pos2idx(midpos)
			//fmt.Printf("x0,x1 = %d %d\n", x0,x1)
			midpos = x[mididx]
			//fmt.Printf("midpos,mididx = %d %d\n", midpos,mididx)
			if (midpos-x0) > (x1-midpos) {
				// added to second
				mididx--
			}
			m0 := mididx
			m1 := mididx+1
			//fmt.Printf("m0,m1 = %d %d\n", m0,m1)
			res[it] += ridersInterval(tidx[it], m0)
			res[it+1] += ridersInterval(m1, tidx[it+1])
		}
		if it==0 {
			// first interval, only add to first taxist
			res[it] += ridersInterval(0, tidx[it])
		}
	}
}

func printRes() {
	for i :=0; i<m; i++ {
		fmt.Print(res[i], " ")
	}
	fmt.Println("")
}

func main() {
	readData()
	solve()
	printRes()
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }

func main() {
	defer writer.Flush()
	var n, m int
	scanf("%d %d\n", &n, &m)
	loc := make([]int, n+m)
	ist := make([]int, n+m)
	rt := make([]int, n+m)
	ans := make([]int, n+m)

	for i := 0; i < n+m; i++ {
		scanf("%d ", &loc[i])
	}
	//fmt.Println("hic")
	scanf("\n")
	//fmt.Println("hic")
	for i := 0; i < n+m; i++ {
		scanf("%d ", &ist[i])
	}
	//fmt.Println("hic")

	lst := -1

	for i := n + m - 1; i >= 0; i-- {
		if ist[i] == 1 {
			lst = i
		} else {
			rt[i] = lst
		}
	}

	cur := -1

	for i := 0; i < n+m; i++ {
		if ist[i] == 1 {
			cur = i
		} else {
			if cur != -1 && rt[i] != -1 {
				if loc[i]-loc[cur] <= loc[rt[i]]-loc[i] {
					ans[cur]++
				} else {
					ans[rt[i]]++
				}
			} else if cur != -1 {
				ans[cur]++
			} else {
				ans[rt[i]]++
			}
		}
	}

	for i := 0; i < n+m; i++ {
		if ist[i] == 1 {
			printf("%d ", ans[i])
		}
	}

}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var scanner = bufio.NewScanner(os.Stdin)

func init() {
	scanner.Split(bufio.ScanWords)
}

func scanString() string {
	scanner.Scan()
	return scanner.Text()
}

func scanInt64() int64 {
	scanner.Scan()
	v, _ := strconv.ParseInt(scanner.Text(), 10, 64)
	return v
}

func scanInt() int {
	scanner.Scan()
	v, _ := strconv.Atoi(scanner.Text())
	return v
}

func scanInts(n int) []int {
	l := make([]int, n)
	for i := 0; i < n; i++ {
		scanner.Scan()
		l[i], _ = strconv.Atoi(scanner.Text())
	}

	return l
}

func scanInts64(n int64) []int64 {
	l := make([]int64, n)
	for i := int64(0); i < n; i++ {
		scanner.Scan()
		l[i], _ = strconv.ParseInt(scanner.Text(), 10, 64)
	}

	return l
}

func scanBool() bool {
	scanner.Scan()
	v, _ := strconv.ParseInt(scanner.Text(), 10, 64)
	return v == 1
}

func scanBools(n int64) []bool {
	l := make([]bool, n)
	for i := int64(0); i < n; i++ {
		scanner.Scan()
		v, _ := strconv.ParseInt(scanner.Text(), 10, 64)
		l[i] = v == 1
	}

	return l
}

func main() {
	n, m := scanInt64(), scanInt64()
	POS, TAXI := make([]int64, n+m+2), make([]bool, n+m+2)

	POS[0], POS[n+m+1] = -2*10e9, 2*10e9
	for i := int64(1); i <= n+m; i++ {
		POS[i] = scanInt64()
	}

	for i := int64(1); i <= n+m; i++ {
		TAXI[i] = scanBool()
	}

	L := make([]int64, n+m+2)
	R := make([]int64, n+m+2)

	L[0] = 0
	R[n+m+1] = n + m + 1

	for i := int64(1); i <= n+m; i++ {
		if !TAXI[i] {
			L[i] = L[i-1]
			continue
		}

		L[i] = i
	}

	for i := n + m; i > 0; i-- {
		if !TAXI[i] {
			R[i] = R[i+1]
			continue
		}

		R[i] = i
	}

	RES := make([]int64, n+m+2)
	for i := int64(1); i <= n+m; i++ {
		if TAXI[i] {
			continue
		}

		distLeft := POS[i] - POS[L[i]]
		distRight := POS[R[i]] - POS[i]

		if distRight < distLeft {
			RES[R[i]]++
		} else {
			RES[L[i]]++
		}
	}

	for i := int64(1); i <= n+m; i++ {
		if TAXI[i] {
			fmt.Println(RES[i])
		}
	}
}

func abs(a int64) int64 {
	if a >= 0 {
		return a
	}

	return -a
}

func min(a, b int64) int64 {
	if a < b {
		return a
	}

	return b
}

// Codeforces 1075 B Taxi drivers and Lyft train
package main

import (
	"bufio"
	"fmt"
	"strings"
	"strconv"
	"os"
	"sort"
)
var in *bufio.Reader

func readFive() (int,int,int,int,int) {
	var a [5]int
	s, err := in.ReadString('\n')
	if err != nil {
		fmt.Println("first read failure", err)
		panic(err)
	}
	ss := strings.Split(strings.Trim(s, " \n\r"), " ")
	for i:=0; i<len(ss); i++ {
		a[i], err = strconv.Atoi(ss[i])
		if err != nil {
			fmt.Println("n atoi failure", err)
			panic(err)
		}
	}
	return a[0],a[1],a[2],a[3],a[4]
}

func readLineNumbs(n int) []int {
	a := make([]int, n)
	s, err := in.ReadString('\n')
	if err != nil {
		fmt.Println("read failure", err)
		panic(err)
	}
	ss := strings.Split(strings.Trim(s, " \n\r"), " ")
	for j:=0; j<n; j++ {
		aa,err := strconv.Atoi(ss[j])
		if err != nil {
			fmt.Println("aa conv failed", err)
			panic(err)
		}
		a[j] = aa
	}
	return a
}

var n,m,nm int
var x []int
var t []int
var res []int
var cntr []int

func readData() {
	in = bufio.NewReader(os.Stdin)
	n,m,_,_,_ = readFive()
	nm = n + m
	x = readLineNumbs(nm)
	t = readLineNumbs(nm)
	res = make([]int, m)
}

// inverted x, binary search
func pos2idx(pos int) int {
	idx := sort.Search(nm, func(i int) bool { return x[i] >= pos })
	//fmt.Println("pos2idx(", pos, ") ret ", idx)
	return idx
}

// riders in interval fro i0 to i1 (inclusive)
func ridersInterval(i0 int, i1 int) int {
	nrid := cntr[i1+1] - cntr[i0]
	//fmt.Printf("ridersInterval(%d, %d) ret %d\n", i0,i1,nrid)
	return nrid
}

func solve() {
	cnt := 0
	itax := 0
	cntr = make([]int, nm+1)
	tidx := make([]int, m)
	for i:=0; i<nm; i++ {
		cntr[i] = cnt
		if t[i]==0 {
			cnt += 1
		} else {
			tidx[itax] = i
			itax++
		}
	}
	cntr[nm] = cnt
	// counting res
	for it:=0; it<m; it++ {
		if it==m-1 {
			// last interval, only add to last taxist
			res[it] += ridersInterval(tidx[it], nm-1)
		} else {
			// middle, divide interval it, it+1 and add to 2 taxists
			midpos := (x[tidx[it]] + x[tidx[it+1]]) / 2
			mididx := pos2idx(midpos)
			res[it] += ridersInterval(tidx[it], mididx)
			res[it+1] += ridersInterval(mididx+1, tidx[it+1])
		}
		if it==0 {
			// first interval, only add to first taxist
			res[it] += ridersInterval(0, tidx[it])
		}
	}
}

func printRes() {
	for i :=0; i<m; i++ {
		fmt.Print(res[i], " ")
	}
	fmt.Println("")
}

func main() {
	readData()
	solve()
	printRes()
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }

func main() {
	defer writer.Flush()
	var n, m int
	scanf("%d %d\n", &n, &m)
	loc := make([]int, n+m)
	ist := make([]int, n+m)
	rt := make([]int, n+m)
	ans := make([]int, n+m)

	for i := 0; i < n+m; i++ {
		scanf("%d ", &loc[i])
	}
	//fmt.Println("hic")
	scanf("\n")
	//fmt.Println("hic")
	for i := 0; i < n+m; i++ {
		scanf("%d ", &ist[i])
	}
	//fmt.Println("hic")

	lst := -1

	for i := n + m - 1; i >= 0; i-- {
		if ist[i] == 1 {
			lst = i
		} else {
			rt[i] = lst
		}
	}

	cur := -1

	for i := 0; i < n+m; i++ {
		if ist[i] == 1 {
			cur = i
		} else {
			if cur != -1 && rt[i] != -1 {
				if i-cur >= rt[i]-i {
					ans[cur]++
				} else {
					ans[rt[i]]++
				}
			} else if cur != -1 {
				ans[cur]++
			} else {
				ans[rt[i]]++
			}
		}
	}

	for i := 0; i < n+m; i++ {
		if ist[i] == 1 {
			printf("%d ", ans[i])
		}
	}

}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }

func main() {
	defer writer.Flush()
	var n, m int
	scanf("%d %d\n", &n, &m)
	loc := make([]int, n+m)
	ist := make([]int, n+m)
	rt := make([]int, n+m)
	ans := make([]int, n+m)

	for i := 0; i < n+m; i++ {
		scanf("%d ", &loc[i])
	}
	//fmt.Println("hic")
	//scanf("\n")
	//fmt.Println("hic")
	for i := 0; i < n+m; i++ {
		scanf("%d ", &ist[i])
	}
	//fmt.Println("hic")

	lst := -1

	for i := n + m - 1; i >= 0; i-- {
		if ist[i] == 1 {
			lst = i
		} else {
			rt[i] = lst
		}
	}

	cur := -1

	for i := 0; i < n+m; i++ {
		if ist[i] == 1 {
			cur = i
		} else {
			if cur != -1 && rt[i] != -1 {
				if i-cur >= rt[i]-i {
					ans[cur]++
				} else {
					ans[rt[i]]++
				}
			} else if cur != -1 {
				ans[cur]++
			} else {
				ans[rt[i]]++
			}
		}
	}

	for i := 0; i < n+m; i++ {
		if ist[i] == 1 {
			printf("%d ", ans[i])
		}
	}

}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
)

type citizen struct {
	pos  int64
	taxi bool
}

func main() {
	var (
		n, m int64
	)

	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)

	scanner.Scan()
	n, _ = strconv.ParseInt(scanner.Text(), 10, 64)

	scanner.Scan()
	m, _ = strconv.ParseInt(scanner.Text(), 10, 64)

	l := make([]citizen, n+m)

	for i := 0; i < len(l); i++ {
		scanner.Scan()
		l[i].pos, _ = strconv.ParseInt(scanner.Text(), 10, 64)
	}

	for i := 0; i < len(l); i++ {
		scanner.Scan()
		st, _ := strconv.ParseInt(scanner.Text(), 10, 64)
		l[i].taxi = st == 1
	}

	ll := make([]citizen, n+m)
	copy(ll, l)
	sort.Slice(ll, func(i, j int) bool {
		return ll[i].pos < ll[j].pos
	})

	// for i := 0; i < len(l); i++ {
	// 	s := "walker"
	// 	if l[i].taxi {
	// 		s = "TAXI"
	// 	}

	// 	fmt.Println(l[i].pos, " is ", s)
	// }

	a := make(map[int64]int64)
	var cnt int64 = 0
	var prev int64 = -1
	for i := int64(0); i < n+m; i++ {
		pos := ll[i].pos
		taxi := ll[i].taxi

		if !taxi {
			cnt++
			continue
		}

		if prev == -1 {
			a[pos] = cnt
			cnt = 0
			prev = pos

			continue
		}

		right := int64(cnt / 2)
		left := cnt - right

		a[prev] += left
		a[pos] = right

		prev = pos

		cnt = 0
	}

	a[prev] += cnt

	for i := 0; i < len(l); i++ {
		if !l[i].taxi {
			continue
		}

		fmt.Println(a[l[i].pos])
	}
}

func abs(a int64) int64 {
	if a >= 0 {
		return a
	}

	return -a
}

func min(a, b int64) int64 {
	if a > b {
		return a
	}

	return b
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
)

type citizen struct {
	pos  int64
	taxi bool
}

func main() {
	var (
		n, m int64
	)

	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)

	scanner.Scan()
	n, _ = strconv.ParseInt(scanner.Text(), 10, 64)

	scanner.Scan()
	m, _ = strconv.ParseInt(scanner.Text(), 10, 64)

	l := make([]citizen, n+m)

	for i := 0; i < len(l); i++ {
		scanner.Scan()
		l[i].pos, _ = strconv.ParseInt(scanner.Text(), 10, 64)
	}

	for i := 0; i < len(l); i++ {
		scanner.Scan()
		st, _ := strconv.ParseInt(scanner.Text(), 10, 64)
		l[i].taxi = st == 1
	}

	sort.Slice(l, func(i, j int) bool {
		return l[i].pos < l[j].pos
	})

	// for i := 0; i < len(l); i++ {
	// 	s := "walker"
	// 	if l[i].taxi {
	// 		s = "TAXI"
	// 	}

	// 	fmt.Println(l[i].pos, " is ", s)
	// }

	var a []int64
	var cnt int64 = 0
	for i := 0; i < len(l); i++ {
		if !l[i].taxi {
			cnt++
			continue
		}

		if len(a) == 0 {
			a = append(a, cnt)
			cnt = 0

			continue
		}

		right := int64(cnt / 2)
		left := cnt - right

		a[len(a)-1] += left
		a = append(a, right)

		cnt = 0
	}

	a[len(a)-1] += cnt

	for i := 0; i < len(a); i++ {
		fmt.Println(a[i])
	}
}

func abs(a int64) int64 {
	if a >= 0 {
		return a
	}

	return -a
}

func min(a, b int64) int64 {
	if a > b {
		return a
	}

	return b
}


You are given a matrix of size $$$n \times n$$$ filled with lowercase English letters. You can change no more than $$$k$$$ letters in this matrix.Consider all paths from the upper left corner to the lower right corner that move from a cell to its neighboring cell to the right or down. Each path is associated with the string that is formed by all the letters in the cells the path visits. Thus, the length of each string is $$$2n - 1$$$.Find the lexicographically smallest string that can be associated with a path after changing letters in at most $$$k$$$ cells of the matrix.A string $$$a$$$ is lexicographically smaller than a string $$$b$$$, if the first different letter in $$$a$$$ and $$$b$$$ is smaller in $$$a$$$.

The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2000$$$, $$$0 \le k \le n^2$$$) — the size of the matrix and the number of letters you can change. Each of the next $$$n$$$ lines contains a string of $$$n$$$ lowercase English letters denoting one row of the matrix.

Output the lexicographically smallest string that can be associated with some valid path after changing no more than $$$k$$$ letters in the matrix.

7 6
ypnxnnp
pnxonpm
nxanpou
xnnpmud
nhtdudu
npmuduh
pmutsnz

NoteIn the first sample test case it is possible to change letters 'b' in cells $$$(2, 1)$$$ and $$$(3, 1)$$$ to 'a', then the minimum path contains cells $$$(1, 1), (2, 1), (3, 1), (4, 1), (4, 2), (4, 3), (4, 4)$$$. The first coordinate corresponds to the row and the second coordinate corresponds to the column.

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF1031D(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var n, k int
	Fscan(in, &n, &k)
	mat := make([]string, n)
	for i := range mat {
		Fscan(in, &mat[i])
	}

	ans := make([]byte, 2*n-1)
	checks := map[[2]int]int{{0, 0}: k}
	for i := range ans {
		minC := byte('z')
		for p, k := range checks {
			if k > 0 {
				minC = 'a'
				break
			}
			if c := mat[p[0]][p[1]]; c < minC {
				minC = c
			}
		}
		newChecks := map[[2]int]int{}
		for p, k := range checks {
			x, y := p[0], p[1]
			c := mat[x][y]
			if k <= 0 && c > minC {
				continue
			}
			if c > minC {
				k--
			}
			if x+1 < n {
				if kk, ok := newChecks[[2]int{x + 1, y}]; !ok || k > kk {
					newChecks[[2]int{x + 1, y}] = k
				}
			}
			if y+1 < n {
				if kk, ok := newChecks[[2]int{x, y + 1}]; !ok || k > kk {
					newChecks[[2]int{x, y + 1}] = k
				}
			}
		}
		checks = newChecks
		ans[i] = minC
	}
	Fprint(out, string(ans))
}

func main() {
	CF1031D(os.Stdin, os.Stdout)
}