In the country there are n cities and m bidirectional roads between them. Each city has an army. Army of the i-th city consists of ai soldiers. Now soldiers roam. After roaming each soldier has to either stay in his city or to go to the one of neighboring cities by at moving along at most one road.Check if is it possible that after roaming there will be exactly bi soldiers in the i-th city.

First line of input consists of two integers n and m (1 ≤ n ≤ 100, 0 ≤ m ≤ 200). Next line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 100). Next line contains n integers b1, b2, ..., bn (0 ≤ bi ≤ 100). Then m lines follow, each of them consists of two integers p and q (1 ≤ p, q ≤ n, p ≠ q) denoting that there is an undirected road between cities p and q.  It is guaranteed that there is at most one road between each pair of cities.

If the conditions can not be met output single word "NO". Otherwise output word "YES" and then n lines, each of them consisting of n integers. Number in the i-th line in the j-th column should denote how many soldiers should road from city i to city j (if i ≠ j) or how many soldiers should stay in city i (if i = j). If there are several possible answers you may output any of them.

package main

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

var in = bufio.NewScanner(os.Stdin)

func readInt() int {
	if !in.Scan() {
		panic(in.Err())
	}
	res := int(0)
	for _, b := range in.Bytes() {
		res = res*10 + int(b-'0')
	}
	return res
}

func main() {
	in.Split(bufio.ScanWords)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	n, m := readInt(), readInt()
	a := make([]int, n)
	for i := range a {
		a[i] = readInt()
	}
	b := make([]int, n)
	for i := range b {
		b[i] = readInt()
	}
	edges := make([][]int, n*2+2)
	addEdge := func(i, j int) {
		edges[i] = append(edges[i], j)
		edges[j] = append(edges[j], i)
	}
	for i := 0; i < m; i++ {
		u, v := readInt()-1, readInt()-1
		addEdge(u, n+v)
		addEdge(v, n+u)
	}
	for i := 0; i < n; i++ {
		addEdge(i, n+i)
		addEdge(i, 2*n)
		addEdge(n+i, 2*n+1)
	}
	capacity := func(v, u int) int {
		if v < 2*n && u < 2*n {
			if v < u {
				return 0x7fffffff
			}
			return 0
		}
		if v < u {
			v, u = u, v
		}
		if v == 2*n {
			return a[u]
		}
		return b[u-n]
	}
	fl := make(map[[2]int]int)
	residual := func(v, u int) int {
		return capacity(v, u) - fl[[2]int{v, u}]
	}

	bfs := func() (path []int) {
		parent := make([]int, 2*n+2)
		for i := range parent {
			parent[i] = -1
		}
		parent[2*n] = 2 * n
		queue := []int{2 * n}
		for len(queue) > 0 {
			v := queue[0]
			queue = queue[1:]
			for _, u := range edges[v] {
				if residual(v, u) <= 0 {
					continue
				}
				if u == 2*n+1 {
					var reversed []int
					for w := v; w != 2*n; w = parent[w] {
						reversed = append(reversed, w)
					}
					rl := len(reversed)
					path := make([]int, rl+2)
					path[0] = 2 * n
					path[rl+1] = 2*n + 1
					for i, r := range reversed {
						path[rl-i] = r
					}
					return path
				}
				if parent[u] != -1 {
					continue
				}
				parent[u] = v
				queue = append(queue, u)
			}
		}
		return nil
	}
	flow := 0
	for {
		path := bfs()
		if len(path) == 0 {
			break
		}
		f := residual(path[0], path[1])
		for i := range path[2:] {
			if r := residual(path[i+1], path[i+2]); r < f {
				f = r
			}
		}
		for i := range path[1:] {
			fl[[2]int{path[i], path[i+1]}] += f
			fl[[2]int{path[i+1], path[i]}] -= f
		}
		flow += f
	}

	army := 0
	for _, c := range a {
		army += c
	}
	armyB := 0
	for _, c := range b {
		armyB += c
	}
	if army != armyB {
		fmt.Fprintln(out, "NO")
		return
	}
	if flow != army {
		if flow > army {
			panic("whoa")
		}
		fmt.Fprintln(out, "NO")
		return
	}
	fmt.Fprintln(out, "YES")
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			fmt.Fprint(out, fl[[2]int{i, n + j}])
			if j+1 == n {
				fmt.Fprintln(out)
			} else {
				fmt.Fprint(out, " ")
			}
		}
	}
}

package main

import (
	"fmt"
	"math"
)

func main() {
	var n, m int
	fmt.Scanf("%d %d\n", &n, &m)
	a := make([]int, n)
	b := make([]int, n)
	sa := 0
	sb := 0

	f := NewFlow(2*n + 2)
	ns := 0
	ne := 2*n + 1

	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &a[i])
		sa += a[i]
		f.addEdge(ns, i+1, a[i])
		f.addEdge(i+1, n+i+1, a[i])
	}
	fmt.Scanf("\n")
	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &b[i])
		sb += b[i]
		f.addEdge(i+n+1, ne, b[i])
	}
	fmt.Scanf("\n")
	for m > 0 {
		m--
		var u, v int
		fmt.Scanf("%d %d\n", &u, &v)
		f.addEdge(u, v+n, a[u-1])
		f.addEdge(v, u+n, a[v-1])
	}
	if sa != sb {
		fmt.Println("NO")
		return
	}
	maxflow := f.dinic(ns, ne)
	// maxflow := f.EK(ns, ne)
	if maxflow == sa {
		fmt.Println("YES")
		for i := 1; i <= n; i++ {
			for j := 1; j <= n; j++ {
				fmt.Print(f.flow[i][j+n])
				if j == n {
					fmt.Println("")
				} else {
					fmt.Print(" ")
				}
			}
		}
	} else {
		fmt.Println("NO")
	}
}

type flow struct {
	volume []map[int]int
	flow   []map[int]int
}

func NewFlow(n int) *flow {
	f := new(flow)
	f.volume = make([]map[int]int, n)
	for i := range f.volume {
		f.volume[i] = make(map[int]int)
	}
	f.flow = make([]map[int]int, n)
	for i := range f.flow {
		f.flow[i] = make(map[int]int)
	}
	return f
}

func (f *flow) addEdge(u, v, c int) {
	// fmt.Println("add edge", u, v, c)
	f.volume[u][v] = c
	f.flow[u][v] = 0
}

func (f *flow) EK(start, end int) int {
	maxflow := 0

	for {
		nodeFlow := make([]int, len(f.volume))
		nodeFlow[start] = math.MaxInt32

		pre := make([]int, len(f.volume))

		queue := make([]int, 0)
		queue = append(queue, start)
		for len(queue) > 0 {
			u := queue[0]
			queue = queue[1:]
			for v, ff := range f.flow[u] {
				c := f.volume[u][v]
				// fmt.Println("u:", u, nodeFlow[u], "v:", v, "flow[u][v]:", f.flow[u][v], "vol[u][v]:", c)
				if nodeFlow[v] != 0 || c <= ff {
					// 节点被标记 或 边流量用尽，跳过
					continue
				}

				nodeFlow[v] = c - ff
				if nodeFlow[u] < nodeFlow[v] {
					nodeFlow[v] = nodeFlow[u]
				}
				// fmt.Println("u:", u, nodeFlow[u], "v:", v, nodeFlow[v])

				pre[v] = u
				queue = append(queue, v)
			}
		}
		// fmt.Println("nodeFlow[end]:", nodeFlow[end])
		if nodeFlow[end] == 0 {
			return maxflow
		}
		// fmt.Println("pre:", pre)
		for u := end; u != start; u = pre[u] {
			f.flow[pre[u]][u] += nodeFlow[end]
			f.flow[u][pre[u]] -= nodeFlow[end]
		}
		// fmt.Println("flow:")
		// for u, m := range f.flow {
		// 	for v, ff := range m {
		// 		fmt.Println(u, v, ff)
		// 	}
		// }
		// fmt.Println("")
		maxflow += nodeFlow[end]
	}
}

func (f *flow) dinic(start, end int) int {
	maxflow := 0

	for {
		dis := make([]int, len(f.volume))
		dis[start] = 1
		queue := make([]int, 0)
		queue = append(queue, start)
		for len(queue) > 0 {
			u := queue[0]
			queue = queue[1:]
			for v, ff := range f.flow[u] {
				c := f.volume[u][v]
				if dis[v] != 0 || ff >= c {
					continue
				}
				dis[v] = dis[u] + 1
				queue = append(queue, v)
			}
		}
		if dis[end] == 0 {
			return maxflow
		}

		for {
			nodeFlow := make([]int, len(f.volume))
			nodeFlow[start] = math.MaxInt32
			pre := make([]int, len(f.volume))
			queue = make([]int, 0)
			queue = append(queue, start)
			for len(queue) > 0 {
				u := queue[0]
				queue = queue[1:]
				for v, ff := range f.flow[u] {
					c := f.volume[u][v]
					if nodeFlow[v] != 0 || ff >= c || dis[v] != dis[u]+1 {
						continue
					}
					pre[v] = u
					nodeFlow[v] = c - ff
					if nodeFlow[u] < nodeFlow[v] {
						nodeFlow[v] = nodeFlow[u]
					}
					queue = append(queue, v)
				}
			}
			if nodeFlow[end] == 0 {
				break
			}
			for u := end; u != start; u = pre[u] {
				f.flow[pre[u]][u] += nodeFlow[end]
				f.flow[u][pre[u]] -= nodeFlow[end]
			}
			maxflow += nodeFlow[end]
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strings"
)

func ReadLine() (string, error) {
	line, err := bufio.NewReader(os.Stdin).ReadString('\n')
	if err != nil {
		// fmt.Println("readline err:", line, err)
		return line, err
	}
	return strings.TrimSpace(string(line)), nil
}

func main() {
	var n, m int
	fmt.Scanf("%d %d\n", &n, &m)
	// line, _ := ReadLine()
	// parts := strings.Split(line, " ")
	// n, _ := strconv.Atoi(parts[0])
	// m, _ := strconv.Atoi(parts[1])
	a := make([]int, n)
	b := make([]int, n)
	sa := 0
	sb := 0
	// fmt.Println("input n m", n, m)
	// fmt.Println("parts n m", parts)

	f := NewFlow(2*n + 2)
	// fmt.Println("new flow")
	ns := 0
	ne := 2*n + 1

	// format := ""
	// params := make([]interface{}, n)
	// for i := 0; i < n; i++ {
	// 	format += "%d"
	// 	if i != n-1 {
	// 		format += " "
	// 	} else {
	// 		format += "\n"
	// 	}
	// 	params = append(params, &a[i])
	// }
	// fmt.Scanf(format, params...)
	// line, _ = ReadLine()
	// parts = strings.Split(line, " ")
	// fmt.Println("parts a", parts)
	// for i := 0; i < n; i++ {
	// 	a[i], _ = strconv.Atoi(parts[i])
	// 	fmt.Println("a[i]:", i, a[i])
	// }
	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &a[i])
		sa += a[i]
		f.addEdge(ns, i+1, a[i])
		f.addEdge(i+1, n+i+1, a[i])
	}
	fmt.Scanf("\n")
	// fmt.Println("input a", a)
	// fmt.Println("add edges a")

	// format = ""
	// params = make([]interface{}, n)
	// for i := 0; i < n; i++ {
	// 	format += "%d"
	// 	if i != n-1 {
	// 		format += " "
	// 	} else {
	// 		format += "\n"
	// 	}
	// 	params = append(params, &b[i])
	// }
	// fmt.Scanf(format, params...)
	// line, _ = ReadLine()
	// parts = strings.Split(line, " ")
	// for i := 0; i < n; i++ {
	// b[i], _ = strconv.Atoi(parts[i])
	// }
	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &b[i])
		sb += b[i]
		f.addEdge(i+n+1, ne, b[i])
	}
	fmt.Scanf("\n")
	// fmt.Println("input b", b)
	for m > 0 {
		m--
		var u, v int
		fmt.Scanf("%d %d\n", &u, &v)
		// line, _ = ReadLine()
		// parts = strings.Split(line, " ")
		// u, _ := strconv.Atoi(parts[0])
		// v, _ := strconv.Atoi(parts[1])
		f.addEdge(u, v+n, a[u-1])
		f.addEdge(v, u+n, a[v-1])
	}
	// fmt.Println("input edge")
	if sa != sb {
		fmt.Println("NO")
		return
	}
	maxflow := f.EK(ns, ne)
	// fmt.Println("EK")
	// fmt.Println("maxflow:", maxflow)
	if maxflow == sa {
		fmt.Println("YES")
		for i := 1; i <= n; i++ {
			for j := 1; j <= n; j++ {
				fmt.Print(f.flow[i][j+n])
				if j == n {
					fmt.Println("")
				} else {
					fmt.Print(" ")
				}
			}
		}
	} else {
		fmt.Println("NO")
	}
}

type flow struct {
	volume []map[int]int
	flow   []map[int]int
}

func NewFlow(n int) *flow {
	f := new(flow)
	f.volume = make([]map[int]int, n)
	for i := range f.volume {
		f.volume[i] = make(map[int]int)
	}
	f.flow = make([]map[int]int, n)
	for i := range f.flow {
		f.flow[i] = make(map[int]int)
	}
	return f
}

func (f *flow) addEdge(u, v, c int) {
	// fmt.Println("add edge", u, v, c)
	f.volume[u][v] = c
	f.flow[u][v] = 0
}

func (f *flow) EK(start, end int) int {
	maxflow := 0

	for {
		nodeFlow := make([]int, len(f.volume))
		nodeFlow[start] = math.MaxInt32

		pre := make([]int, len(f.volume))

		queue := make([]int, 0)
		queue = append(queue, start)
		for len(queue) > 0 {
			u := queue[0]
			queue = queue[1:]
			for v, ff := range f.flow[u] {
				c := f.volume[u][v]
				// fmt.Println("u:", u, nodeFlow[u], "v:", v, "flow[u][v]:", f.flow[u][v], "vol[u][v]:", c)
				if nodeFlow[v] != 0 || c <= ff {
					// 节点被标记 或 边流量用尽，跳过
					continue
				}

				nodeFlow[v] = c - ff
				if nodeFlow[u] < nodeFlow[v] {
					nodeFlow[v] = nodeFlow[u]
				}
				// fmt.Println("u:", u, nodeFlow[u], "v:", v, nodeFlow[v])

				pre[v] = u
				queue = append(queue, v)
			}
		}
		// fmt.Println("nodeFlow[end]:", nodeFlow[end])
		if nodeFlow[end] == 0 {
			return maxflow
		}
		// fmt.Println("pre:", pre)
		for u := end; u != start; u = pre[u] {
			f.flow[pre[u]][u] += nodeFlow[end]
			f.flow[u][pre[u]] -= nodeFlow[end]
		}
		// fmt.Println("flow:")
		// for u, m := range f.flow {
		// 	for v, ff := range m {
		// 		fmt.Println(u, v, ff)
		// 	}
		// }
		// fmt.Println("")
		maxflow += nodeFlow[end]
	}
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF546E(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	const inf int = 1e9
	min := func(a, b int) int {
		if a < b {
			return a
		}
		return b
	}

	var N, m, v, w, s, maxFlow int
	Fscan(in, &N, &m)
	st, end, n := 2*N, 2*N+1, 2*N+2
	type nb struct{ to, rid, cap int }
	g := make([][]nb, n)
	addEdge := func(from, to, cap int) {
		g[from] = append(g[from], nb{to, len(g[to]), cap})
		g[to] = append(g[to], nb{from, len(g[from]) - 1, 0})
	}
	for i := 0; i < N; i++ {
		Fscan(in, &v)
		addEdge(st, i, v)
		s += v
	}
	sum := s
	for i := N; i < 2*N; i++ {
		Fscan(in, &v)
		addEdge(i, end, v)
		s -= v
	}
	if s != 0 {
		Fprint(out, "NO")
		return
	}
	for i := 0; i < N; i++ {
		addEdge(i, i+N, inf)
	}
	for ; m > 0; m-- {
		Fscan(in, &v, &w)
		v--
		w--
		addEdge(v, w+N, inf)
		addEdge(w, v+N, inf)
	}

	dep := make([]int, n)
	bfs := func() bool {
		for i := range dep {
			dep[i] = -1
		}
		dep[st] = 0
		q := []int{st}
		for len(q) > 0 {
			v := q[0]
			q = q[1:]
			for _, e := range g[v] {
				if w := e.to; e.cap > 0 && dep[w] < 0 {
					dep[w] = dep[v] + 1
					q = append(q, w)
				}
			}
		}
		return dep[end] >= 0
	}
	var it []int
	var dfs func(int, int) int
	dfs = func(v, minF int) int {
		if v == end {
			return minF
		}
		for ; it[v] < len(g[v]); it[v]++ {
			e := &g[v][it[v]]
			if w := e.to; e.cap > 0 && dep[w] > dep[v] {
				if f := dfs(w, min(minF, e.cap)); f > 0 {
					e.cap -= f
					g[w][e.rid].cap += f
					return f
				}
			}
		}
		return 0
	}
	for bfs() {
		it = make([]int, n)
		for {
			if f := dfs(st, inf); f > 0 {
				maxFlow += f
			} else {
				break
			}
		}
	}
	if maxFlow < sum {
		Fprint(out, "NO")
		return
	}
	Fprintln(out, "YES")
	for _, es := range g[:N] {
		ans := make([]int, N)
		for _, e := range es {
			if e.to >= N && e.to < 2*N {
				ans[e.to-N] = g[e.to][e.rid].cap
			}
		}
		for _, v := range ans {
			Fprint(out, v, " ")
		}
		Fprintln(out)
	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	const inf int = 1e9
	min := func(a, b int) int {
		if a < b {
			return a
		}
		return b
	}

	var N, m, v, w, s, maxFlow int
	Fscan(in, &N, &m)
	st, end, n := 2*N, 2*N+1, 2*N+2
	type nb struct{ to, rid, cap int }
	g := make([][]nb, n)
	addEdge := func(from, to, cap int) {
		g[from] = append(g[from], nb{to, len(g[to]), cap})
		g[to] = append(g[to], nb{from, len(g[from]) - 1, 0})
	}
	for i := 0; i < N; i++ {
		Fscan(in, &v)
		addEdge(st, i, v)
		s += v
	}
	sum := s
	for i := N; i < 2*N; i++ {
		Fscan(in, &v)
		addEdge(i, end, v)
		s -= v
	}
	if s != 0 {
		Fprint(out, "NO")
		return
	}
	for i := 0; i < N; i++ {
		addEdge(i, i+N, inf)
	}
	for ; m > 0; m-- {
		Fscan(in, &v, &w)
		v--
		w--
		addEdge(v, w+N, inf)
		addEdge(w, v+N, inf)
	}

	dep := make([]int, n)
	bfs := func() bool {
		for i := range dep {
			dep[i] = -1
		}
		dep[st] = 0
		q := []int{st}
		for len(q) > 0 {
			v := q[0]
			q = q[1:]
			for _, e := range g[v] {
				if w := e.to; e.cap > 0 && dep[w] < 0 {
					dep[w] = dep[v] + 1
					q = append(q, w)
				}
			}
		}
		return dep[end] >= 0
	}
	var it []int
	var dfs func(int, int) int
	dfs = func(v, minF int) int {
		if v == end {
			return minF
		}
		for ; it[v] < len(g[v]); it[v]++ {
			e := &g[v][it[v]]
			if w := e.to; e.cap > 0 && dep[w] > dep[v] {
				if f := dfs(w, min(minF, e.cap)); f > 0 {
					e.cap -= f
					g[w][e.rid].cap += f
					return f
				}
			}
		}
		return 0
	}
	for bfs() {
		it = make([]int, n)
		for {
			if f := dfs(st, inf); f > 0 {
				maxFlow += f
			} else {
				break
			}
		}
	}
	if maxFlow < sum {
		Fprint(out, "NO")
		return
	}
	Fprintln(out, "YES")
	for _, es := range g[:N] {
		ans := make([]int, N)
		for _, e := range es {
			if w := e.to; N <= w && w < 2*N {
				ans[w-N] = g[w][e.rid].cap
			}
		}
		for _, v := range ans {
			Fprint(out, v, " ")
		}
		Fprintln(out)
	}
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF546E(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	const inf int = 1e9

	var N, m, v, w, s, maxFlow int
	Fscan(in, &N, &m)
	st, end, n := 2*N, 2*N+1, 2*N+2
	type nb struct{ to, rid, cap int }
	g := make([][]nb, n)
	addEdge := func(from, to, cap int) {
		g[from] = append(g[from], nb{to, len(g[to]), cap})
		g[to] = append(g[to], nb{from, len(g[from]) - 1, 0})
	}
	for i := 0; i < N; i++ {
		Fscan(in, &v)
		addEdge(st, i, v)
		s += v
	}
	sum := s
	for i := N; i < 2*N; i++ {
		Fscan(in, &v)
		addEdge(i, end, v)
		s -= v
	}
	if s != 0 {
		Fprint(out, "NO")
		return
	}
	for i := 0; i < N; i++ {
		addEdge(i, i+N, inf)
	}
	for ; m > 0; m-- {
		Fscan(in, &v, &w)
		v--
		w--
		addEdge(v, w+N, inf)
		addEdge(w, v+N, inf)
	}

	d := make([]int, n)
	for i := range d {
		d[i] = -1
	}
	d[end] = 0
	cd := make([]int, n+1)
	q := []int{end}
	for len(q) > 0 {
		v := q[0]
		q = q[1:]
		cd[d[v]]++
		for _, e := range g[v] {
			if w := e.to; d[w] < 0 {
				d[w] = d[v] + 1
				q = append(q, w)
			}
		}
	}

	iter := make([]int, n)
	type pair struct{ v, i int }
	fa := make([]pair, n)
o:
	for v := st; d[st] < n; {
		if v == end {
			minF := inf
			for v := end; v != st; {
				p := fa[v]
				if c := g[p.v][p.i].cap; c < minF {
					minF = c
				}
				v = p.v
			}
			for v := end; v != st; {
				p := fa[v]
				e := &g[p.v][p.i]
				e.cap -= minF
				g[v][e.rid].cap += minF
				v = p.v
			}
			maxFlow += minF
			v = st
		}
		for i := iter[v]; i < len(g[v]); i++ {
			e := g[v][i]
			if w := e.to; e.cap > 0 && d[w] < d[v] {
				fa[w] = pair{v, i}
				iter[v] = i
				v = w
				continue o
			}
		}
		if cd[d[v]] == 1 {
			break
		}
		cd[d[v]]--
		minD := n - 1
		for _, e := range g[v] {
			if e.cap > 0 && d[e.to] < minD {
				minD = d[e.to]
			}
		}
		d[v] = minD + 1
		cd[d[v]]++
		iter[v] = 0
		if v != st {
			v = fa[v].v
		}
	}
	if maxFlow < sum {
		Fprint(out, "NO")
		return
	}
	Fprintln(out, "YES")
	for _, es := range g[:N] {
		ans := make([]int, N)
		for _, e := range es {
			if w := e.to; N <= w && w < 2*N {
				ans[w-N] = g[w][e.rid].cap
			}
		}
		for _, v := range ans {
			Fprint(out, v, " ")
		}
		Fprintln(out)
	}
}

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

package main

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

const infinity = 1000000000

func slice2D(n int) [][]int {
	res := make([][]int, n)
	for i := range res {
		res[i] = make([]int, n)
	}
	return res
}

func min(a, b int) int {
	if a < b {
		return a

	}
	return b
}

func dfs(v, t, w int, cap, flow [][]int, was []bool) int {
	if v == t {
		return w
	}
	if was[v] {
		return 0
	}
	was[v] = true
	for u := range was {
		if flow[v][u] >= cap[v][u] {
			continue
		}
		nw := dfs(u, t, min(w, cap[v][u]-flow[v][u]), cap, flow, was)
		if nw > 0 {
			flow[v][u] += nw
			flow[u][v] -= nw
			return nw
		}
	}
	return 0
}

func main() {
	n := nextInt()
	m := nextInt()
	a := make([]int, n)
	for i := range a {
		a[i] = nextInt()
	}
	b := make([]int, n)
	for i := range b {
		b[i] = nextInt()
	}
	cap := slice2D(2*n + 2)
	flow := slice2D(2*n + 2)
	s := 2 * n
	t := 2*n + 1
	for i := 0; i < m; i++ {
		u := nextInt() - 1
		v := nextInt() - 1
		cap[u][v+n] = infinity
		cap[v][u+n] = infinity
	}
	var sumA, sumB int
	for i := 0; i < n; i++ {
		cap[s][i] = a[i]
		cap[i+n][t] = b[i]
		cap[i][i+n] = infinity
		sumA += a[i]
		sumB += b[i]
	}
	if sumA != sumB {
		fmt.Println("NO")
		return
	}
	flowSize := 0
	was := make([]bool, 2*n+2)
	for {
		for i := range was {
			was[i] = false
		}
		add := dfs(s, t, infinity, cap, flow, was)
		if add == 0 {
			break
		}
		flowSize += add
	}
	if flowSize != sumA {
		fmt.Println("NO")
		return
	}
	fmt.Println("YES")
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			if j > 0 {
				fmt.Print(" ")
			}
			fmt.Print(flow[i][j+n])
		}
		fmt.Println()
	}
}

func init() {
	// maybe defer writer.Flush() in main

	lineScanner = bufio.NewScanner(os.Stdin)
	writer = bufio.NewWriter(os.Stdout)

	/*
		inputFile, err := os.Open("a.in")
		if err != nil {
			panic(err)
		}
		outputFile, err := os.Create("a.out")
		if err != nil {
			panic(err)
		}
		lineScanner = bufio.NewScanner(inputFile)
		writer = bufio.NewWriter(outputFile)
	*/

	wordScanner = bufio.NewScanner(strings.NewReader(""))
	wordScanner.Split(bufio.ScanWords)
}

var lineScanner *bufio.Scanner
var wordScanner *bufio.Scanner

var writer *bufio.Writer

func nextLine() (string, error) {
	if !lineScanner.Scan() {
		if err := lineScanner.Err(); err != nil {
			panic(err)
		}
		return "", errors.New("nextLine: EOF reached")
	}
	return lineScanner.Text(), nil
}

func nextWord() string {
	for !wordScanner.Scan() {
		if err := wordScanner.Err(); err != nil {
			panic(err)
		}
		line, err := nextLine()
		if err != nil {
			return ""
		}
		wordScanner = bufio.NewScanner(strings.NewReader(line))
		wordScanner.Split(bufio.ScanWords)
	}
	return wordScanner.Text()
}

func nextInt() int {
	res, err := strconv.Atoi(nextWord())
	if err != nil {
		panic(err)
	}
	return res
}

func nextInt64() int64 {
	res, err := strconv.ParseInt(nextWord(), 10, 64)
	if err != nil {
		panic(err)
	}
	return res
}

func nextFloat64() float64 {
	res, err := strconv.ParseFloat(nextWord(), 64)
	if err != nil {
		panic(err)
	}
	return res
}

package main

import (
	"fmt"
	"math"
)

type (
	graph struct {
		source int
		sink   int
		edges  map[int]map[int]int
		caps   map[int]map[int]int
	}
)

func initGraph(n int) *graph {
	g := &graph{source: 0, sink: 2*n + 1}
	g.edges = map[int]map[int]int{}
	g.caps = map[int]map[int]int{}
	// g.edges = make([]map[int]int, 2*n+2)
	// g.caps = make([]map[int]int, 2*n+2)
	// for i := 0; i < 2*n+2; i++ {
	// 	g.edges[i] = map[int]int{}
	// 	g.caps[i] = map[int]int{}
	// }
	return g
}

func (g *graph) addEdge(src, dst, value int) {

	if _, exist := g.edges[src]; !exist {
		g.edges[src] = map[int]int{}
	}

	if _, exist := g.edges[dst]; !exist {
		g.edges[dst] = map[int]int{}
	}

	if _, exist := g.caps[src]; !exist {
		g.caps[src] = map[int]int{}
	}

	g.edges[src][dst] = value
	g.edges[dst][src] = 0
	g.caps[src][dst] = value
}

func main() {
	var n, m int
	fmt.Scanf("%d %d\n", &n, &m)

	g := initGraph(n)
	a := make([]int, n)
	b := make([]int, n)
	sa := 0
	sb := 0

	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &a[i])
		sa += a[i]
		g.addEdge(g.source, i+1, a[i])
		g.addEdge(i+1, n+i+1, a[i])
	}
	fmt.Scanf("\n")
	// fmt.Println("input a", a)

	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &b[i])
		sb += b[i]
		g.addEdge(i+n+1, g.sink, b[i])
	}
	fmt.Scanf("\n")
	// fmt.Println("input b", b)

	for m > 0 {
		m--
		var u, v int
		fmt.Scanf("%d %d\n", &u, &v)
		// fmt.Println(u, v)
		g.addEdge(u, v+n, a[u-1])
		g.addEdge(v, u+n, a[v-1])
	}
	// fmt.Println("input edge", g.edges)
	if sa != sb {
		fmt.Println("NO")
		return
	}

	maxflow := 0
	for {
		dist, hasRoad := bfs(g)
		if !hasRoad {
			break
		}

		// fmt.Println(dist)
		for {
			value := dfs(g, dist, g.source, math.MaxInt32)
			// fmt.Println(value)
			if value == 0 {
				break
			}
			maxflow += value
		}
	}
	// fmt.Println("EK")
	// fmt.Println("maxflow:", maxflow)
	if maxflow == sa {
		fmt.Println("YES")
		for i := 1; i <= n; i++ {
			for j := 1; j <= n; j++ {
				fmt.Print(g.caps[i][j+n] - g.edges[i][j+n])
				if j == n {
					fmt.Println("")
				} else {
					fmt.Print(" ")
				}
			}
		}
	} else {
		fmt.Println("NO")
	}
}

func bfs(g *graph) (map[int]int, bool) {

	if g == nil {
		return nil, false
	}

	// visited := map[int]bool{}
	// dist := make([]int, len(g.edges))
	dist := map[int]int{}

	queue := []int{}
	// visited[g.source] = true
	dist[g.source] = 1
	queue = append(queue, g.source)

	for len(queue) > 0 {
		tmp := queue[0]
		queue = queue[1:]

		if len(g.edges[tmp]) == 0 {
			continue
		}

		for enode, flow := range g.edges[tmp] {
			if enode == g.sink && flow > 0 {
				dist[enode] = dist[tmp] + 1
				// fmt.Println("dist:", dist)
				return dist, true
			}

			if dist[enode] != 0 || flow <= 0 {
				continue
			}

			if dist[enode] == 0 && flow > 0 {
				// visited[enode] = true
				dist[enode] = dist[tmp] + 1
				queue = append(queue, enode)
			}
		}
	}

	return nil, false
}

func dfs(g *graph, dist map[int]int, node, max int) int {

	if g == nil {
		return 0
	}

	if node == g.sink {
		return max
	}

	if len(g.edges[node]) == 0 {
		return 0
	}

	for enode, flow := range g.edges[node] {
		if dist[enode] == dist[node]+1 && flow > 0 {
			value := dfs(g, dist, enode, min(flow, max))
			// fmt.Println(value)
			if value == 0 {
				continue
			}

			if value > 0 {
				g.edges[node][enode] -= value
				g.edges[enode][node] += value
			}
			return value
		}
	}

	return 0
}

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

package main

import (
	"fmt"
	"math"
)

type (
	graph struct {
		source int
		sink   int
		edges  []map[int]int
		caps   []map[int]int
	}
)

func initGraph(n int) *graph {
	g := &graph{source: 0, sink: 2*n + 1}
	g.edges = make([]map[int]int, 2*n+2)
	g.caps = make([]map[int]int, 2*n+2)
	for i := 0; i < 2*n+2; i++ {
		g.edges[i] = map[int]int{}
		g.caps[i] = map[int]int{}
	}
	return g
}

func (g *graph) addEdge(src, dst, value int) {
	g.edges[src][dst] = value
	g.edges[dst][src] = 0
	g.caps[src][dst] = value
}

func main() {
	var n, m int
	fmt.Scanf("%d %d\n", &n, &m)

	g := initGraph(n)
	a := make([]int, n)
	b := make([]int, n)
	sa := 0
	sb := 0

	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &a[i])
		sa += a[i]
		g.addEdge(g.source, i+1, a[i])
		g.addEdge(i+1, n+i+1, a[i])
	}
	fmt.Scanf("\n")
	// fmt.Println("input a", a)

	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &b[i])
		sb += b[i]
		g.addEdge(i+n+1, g.sink, b[i])
	}
	fmt.Scanf("\n")
	// fmt.Println("input b", b)

	for m > 0 {
		m--
		var u, v int
		fmt.Scanf("%d %d\n", &u, &v)
		// fmt.Println(u, v)
		g.addEdge(u, v+n, a[u-1])
		g.addEdge(v, u+n, a[v-1])
	}
	// fmt.Println("input edge", g.edges)
	if sa != sb {
		fmt.Println("NO")
		return
	}

	maxflow := 0
	for {
		dist, hasRoad := bfs(g)
		if !hasRoad {
			break
		}

		// fmt.Println(dist)
		for {
			value := dfs(g, dist, g.source, math.MaxInt32)
			// fmt.Println(value)
			if value == 0 {
				break
			}
			maxflow += value
		}
	}
	// fmt.Println("EK")
	// fmt.Println("maxflow:", maxflow)
	if maxflow == sa {
		fmt.Println("YES")
		for i := 1; i <= n; i++ {
			for j := 1; j <= n; j++ {
				fmt.Print(g.caps[i][j+n] - g.edges[i][j+n])
				if j == n {
					fmt.Println("")
				} else {
					fmt.Print(" ")
				}
			}
		}
	} else {
		fmt.Println("NO")
	}
}

func bfs(g *graph) ([]int, bool) {

	// if g == nil {
	// 	return nil, false
	// }

	// visited := map[int]bool{}
	dist := make([]int, len(g.edges))

	queue := []int{}
	// visited[g.source] = true
	dist[g.source] = 1
	queue = append(queue, g.source)

	for len(queue) > 0 {
		tmp := queue[0]
		queue = queue[1:]

		if len(g.edges[tmp]) == 0 {
			continue
		}

		for enode, flow := range g.edges[tmp] {
			if enode == g.sink && flow > 0 {
				dist[enode] = dist[tmp] + 1
				// fmt.Println("dist:", dist)
				return dist, true
			}

			if dist[enode] != 0 || flow <= 0 {
				continue
			}

			if dist[enode] == 0 && flow > 0 {
				// visited[enode] = true
				dist[enode] = dist[tmp] + 1
				queue = append(queue, enode)
			}
		}
	}

	return nil, false
}

func dfs(g *graph, dist []int, node, max int) int {

	// if g == nil {
	// 	return 0
	// }

	if node == g.sink {
		return max
	}

	if len(g.edges[node]) == 0 {
		return 0
	}

	for enode, flow := range g.edges[node] {
		if dist[enode] == dist[node]+1 && flow > 0 {
			value := dfs(g, dist, enode, min(flow, max))
			// fmt.Println(value)
			if value == 0 {
				continue
			}

			if value > 0 {
				g.edges[node][enode] -= value
				g.edges[enode][node] += value
			}
			return value
		}
	}

	return 0
}

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

package main

import (
	"fmt"
	"math"
	"strconv"
)

type (
	graph struct {
		source string
		sink   string
		edges  map[string]map[string]int
		// caps   map[int]map[int]int
	}
)

const (
	source = "source"
	sink   = "sink"
)

func initGraph(n int) *graph {
	g := &graph{source: source, sink: sink}
	g.edges = map[string]map[string]int{}
	return g
}

func getVnode(idx int) string {
	return "v_" + strconv.Itoa(idx)
}

func (g *graph) addEdge(src, dst string, value int) {

	if _, exist := g.edges[src]; !exist {
		g.edges[src] = map[string]int{}
	}

	if _, exist := g.edges[dst]; !exist {
		g.edges[dst] = map[string]int{}
	}

	g.edges[src][dst] = value
	g.edges[dst][src] = 0
}

func main() {
	var n, m int
	fmt.Scanf("%d %d\n", &n, &m)

	g := initGraph(n)
	a := make([]int, n)
	b := make([]int, n)
	sa := 0
	sb := 0

	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &a[i])
		sa += a[i]
		g.addEdge(g.source, strconv.Itoa(i+1), a[i])
		g.addEdge(strconv.Itoa(i+1), getVnode(i+1), a[i])
	}
	fmt.Scanf("\n")
	// fmt.Println("input a", a)

	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &b[i])
		sb += b[i]
		g.addEdge(getVnode(i+1), g.sink, b[i])
	}
	fmt.Scanf("\n")
	// fmt.Println("input b", b)

	for m > 0 {
		m--
		var u, v int
		fmt.Scanf("%d %d\n", &u, &v)
		// fmt.Println(u, v)
		g.addEdge(strconv.Itoa(u), getVnode(v), a[u-1])
		g.addEdge(strconv.Itoa(v), getVnode(u), a[v-1])
	}
	// fmt.Println("input edge", g.edges)
	if sa != sb {
		fmt.Println("NO")
		return
	}

	maxflow := 0
	for {
		dist, hasRoad := bfs(g)
		if !hasRoad {
			break
		}

		// fmt.Println(dist)
		for {
			value := dfs(g, dist, g.source, math.MaxInt32)
			// fmt.Println(value)
			if value == 0 {
				break
			}
			maxflow += value
		}
	}
	// fmt.Println("EK")
	// fmt.Println("maxflow:", maxflow)
	if maxflow == sa {
		fmt.Println("YES")
		for i := 1; i <= n; i++ {
			for j := 1; j <= n; j++ {
				// fmt.Print(g.caps[i][j+n] - g.edges[i][j+n])
				// fmt.Print(g.edges[j+n][i])
				fmt.Print(g.edges[getVnode(j)][strconv.Itoa(i)])
				if j == n {
					fmt.Println("")
				} else {
					fmt.Print(" ")
				}
			}
		}
	} else {
		fmt.Println("NO")
	}
}

func bfs(g *graph) (map[string]int, bool) {

	if g == nil {
		return nil, false
	}

	// visited := map[int]bool{}
	// dist := make([]int, len(g.edges))
	dist := map[string]int{}

	queue := []string{}
	// visited[g.source] = true
	dist[g.source] = 1
	queue = append(queue, g.source)

	for len(queue) > 0 {
		tmp := queue[0]
		queue = queue[1:]

		if len(g.edges[tmp]) == 0 {
			continue
		}

		for enode, flow := range g.edges[tmp] {
			if enode == g.sink && flow > 0 {
				dist[enode] = dist[tmp] + 1
				// fmt.Println("dist:", dist)
				return dist, true
			}

			if dist[enode] != 0 || flow <= 0 {
				continue
			}

			if dist[enode] == 0 && flow > 0 {
				// visited[enode] = true
				dist[enode] = dist[tmp] + 1
				queue = append(queue, enode)
			}
		}
	}

	return nil, false
}

func dfs(g *graph, dist map[string]int, node string, max int) int {

	if g == nil {
		return 0
	}

	if node == g.sink {
		return max
	}

	if len(g.edges[node]) == 0 {
		return 0
	}

	for enode, flow := range g.edges[node] {
		if dist[enode] == dist[node]+1 && flow > 0 {
			value := dfs(g, dist, enode, min(flow, max))
			// fmt.Println(value)
			if value == 0 {
				continue
			}

			if value > 0 {
				g.edges[node][enode] -= value
				g.edges[enode][node] += value
			}
			return value
		}
	}

	return 0
}

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

package main

import (
	"fmt"
	"math"
)

type (
	graph struct {
		source int
		sink   int
		edges  map[int]map[int]int
		// caps   map[int]map[int]int
	}
)

func initGraph(n int) *graph {
	g := &graph{source: 0, sink: 2*n + 1}
	g.edges = map[int]map[int]int{}
	// g.caps = map[int]map[int]int{}
	// g.edges = make([]map[int]int, 2*n+2)
	// g.caps = make([]map[int]int, 2*n+2)
	// for i := 0; i < 2*n+2; i++ {
	// 	g.edges[i] = map[int]int{}
	// 	g.caps[i] = map[int]int{}
	// }
	return g
}

func (g *graph) addEdge(src, dst, value int) {

	if _, exist := g.edges[src]; !exist {
		g.edges[src] = map[int]int{}
	}

	if _, exist := g.edges[dst]; !exist {
		g.edges[dst] = map[int]int{}
	}

	// if _, exist := g.caps[src]; !exist {
	// 	g.caps[src] = map[int]int{}
	// }

	g.edges[src][dst] = value
	g.edges[dst][src] = 0
	// g.caps[src][dst] = value
}

func main() {
	var n, m int
	fmt.Scanf("%d %d\n", &n, &m)

	g := initGraph(n)
	a := make([]int, n)
	b := make([]int, n)
	sa := 0
	sb := 0

	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &a[i])
		sa += a[i]
		g.addEdge(g.source, i+1, a[i])
		g.addEdge(i+1, n+i+1, a[i])
	}
	fmt.Scanf("\n")
	// fmt.Println("input a", a)

	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &b[i])
		sb += b[i]
		g.addEdge(i+n+1, g.sink, b[i])
	}
	fmt.Scanf("\n")
	// fmt.Println("input b", b)

	for m > 0 {
		m--
		var u, v int
		fmt.Scanf("%d %d\n", &u, &v)
		// fmt.Println(u, v)
		g.addEdge(u, v+n, a[u-1])
		g.addEdge(v, u+n, a[v-1])
	}
	// fmt.Println("input edge", g.edges)
	if sa != sb {
		fmt.Println("NO")
		return
	}

	maxflow := 0
	for {
		dist, hasRoad := bfs(g)
		if !hasRoad {
			break
		}

		// fmt.Println(dist)
		for {
			value := dfs(g, dist, g.source, math.MaxInt32)
			// fmt.Println(value)
			if value == 0 {
				break
			}
			maxflow += value
		}
	}
	// fmt.Println("EK")
	// fmt.Println("maxflow:", maxflow)
	if maxflow == sa {
		fmt.Println("YES")
		for i := 1; i <= n; i++ {
			for j := 1; j <= n; j++ {
				// fmt.Print(g.caps[i][j+n] - g.edges[i][j+n])
				fmt.Print(g.edges[j+n][i])
				if j == n {
					fmt.Println("")
				} else {
					fmt.Print(" ")
				}
			}
		}
	} else {
		fmt.Println("NO")
	}
}

func bfs(g *graph) (map[int]int, bool) {

	if g == nil {
		return nil, false
	}

	// visited := map[int]bool{}
	// dist := make([]int, len(g.edges))
	dist := map[int]int{}

	queue := []int{}
	// visited[g.source] = true
	dist[g.source] = 1
	queue = append(queue, g.source)

	for len(queue) > 0 {
		tmp := queue[0]
		queue = queue[1:]

		if len(g.edges[tmp]) == 0 {
			continue
		}

		for enode, flow := range g.edges[tmp] {
			if enode == g.sink && flow > 0 {
				dist[enode] = dist[tmp] + 1
				// fmt.Println("dist:", dist)
				return dist, true
			}

			if dist[enode] != 0 || flow <= 0 {
				continue
			}

			if dist[enode] == 0 && flow > 0 {
				// visited[enode] = true
				dist[enode] = dist[tmp] + 1
				queue = append(queue, enode)
			}
		}
	}

	return nil, false
}

func dfs(g *graph, dist map[int]int, node, max int) int {

	if g == nil {
		return 0
	}

	if node == g.sink {
		return max
	}

	if len(g.edges[node]) == 0 {
		return 0
	}

	for enode, flow := range g.edges[node] {
		if dist[enode] == dist[node]+1 && flow > 0 {
			value := dfs(g, dist, enode, min(flow, max))
			// fmt.Println(value)
			if value == 0 {
				continue
			}

			if value > 0 {
				g.edges[node][enode] -= value
				g.edges[enode][node] += value
			}
			return value
		}
	}

	return 0
}

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

package main

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

var in = bufio.NewScanner(os.Stdin)

func readInt() int {
	if !in.Scan() {
		panic(in.Err())
	}
	res := int(0)
	for _, b := range in.Bytes() {
		res = res*10 + int(b-'0')
	}
	return res
}

func main() {
	in.Split(bufio.ScanWords)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	n, m := readInt(), readInt()
	a := make([]int, n)
	for i := range a {
		a[i] = readInt()
	}
	b := make([]int, n)
	for i := range b {
		b[i] = readInt()
	}
	edges := make([][]int, n*2+2)
	addEdge := func(i, j int) {
		edges[i] = append(edges[i], j)
		edges[j] = append(edges[j], i)
	}
	for i := 0; i < m; i++ {
		u, v := readInt()-1, readInt()-1
		addEdge(u, n+v)
		addEdge(v, n+u)
	}
	for i := 0; i < n; i++ {
		addEdge(i, n+i)
		addEdge(i, 2*n)
		addEdge(n+i, 2*n+1)
	}
	capacity := func(v, u int) int {
		if v < 2*n && u < 2*n {
			if v < u {
				return 0x7fffffff
			}
			return 0
		}
		if v < u {
			v, u = u, v
		}
		if v == 2*n {
			return a[u]
		}
		return b[u-n]
	}
	fl := make(map[[2]int]int)
	residual := func(v, u int) int {
		return capacity(v, u) - fl[[2]int{v, u}]
	}

	bfs := func() (path []int) {
		parent := make([]int, 2*n+2)
		for i := range parent {
			parent[i] = -1
		}
		parent[2*n] = 2 * n
		queue := []int{2 * n}
		for len(queue) > 0 {
			v := queue[0]
			queue = queue[1:]
			for _, u := range edges[v] {
				if residual(v, u) <= 0 {
					continue
				}
				if u == 2*n+1 {
					var reversed []int
					for w := v; w != 2*n; w = parent[w] {
						reversed = append(reversed, w)
					}
					rl := len(reversed)
					path := make([]int, rl+2)
					path[0] = 2 * n
					path[rl+1] = 2*n + 1
					for i, r := range reversed {
						path[rl-i] = r
					}
					return path
				}
				if parent[u] != -1 {
					continue
				}
				parent[u] = v
				queue = append(queue, u)
			}
		}
		return nil
	}
	flow := 0
	for {
		path := bfs()
		if len(path) == 0 {
			break
		}
		f := residual(path[0], path[1])
		for i := range path[2:] {
			if r := residual(path[i+1], path[i+2]); r < f {
				f = r
			}
		}
		for i := range path[1:] {
			fl[[2]int{path[i], path[i+1]}] += f
			fl[[2]int{path[i+1], path[i]}] -= f
		}
		flow += f
	}

	army := 0
	for _, c := range a {
		army += c
	}
	if flow != army {
		if flow > army {
			panic("whoa")
		}
		fmt.Fprintln(out, "NO")
		return
	}
	fmt.Fprintln(out, "YES")
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			fmt.Fprint(out, fl[[2]int{i, n + j}])
			if j+1 == n {
				fmt.Fprintln(out)
			} else {
				fmt.Fprint(out, " ")
			}
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strings"
)

func ReadLine() (string, error) {
	line, err := bufio.NewReader(os.Stdin).ReadString('\n')
	if err != nil {
		fmt.Println("readline err:", line, err)
		return line, err
	}
	return strings.TrimSpace(string(line)), nil
}

func main() {
	var n, m int
	fmt.Scanf("%d %d\n", &n, &m)
	// line, _ := ReadLine()
	// parts := strings.Split(line, " ")
	// n, _ := strconv.Atoi(parts[0])
	// m, _ := strconv.Atoi(parts[1])
	a := make([]int, n)
	b := make([]int, n)
	sa := 0
	sb := 0
	fmt.Println("input n m", n, m)
	// fmt.Println("parts n m", parts)

	f := NewFlow(2*n + 2)
	fmt.Println("new flow")
	ns := 0
	ne := 2*n + 1

	// format := ""
	// params := make([]interface{}, n)
	// for i := 0; i < n; i++ {
	// 	format += "%d"
	// 	if i != n-1 {
	// 		format += " "
	// 	} else {
	// 		format += "\n"
	// 	}
	// 	params = append(params, &a[i])
	// }
	// fmt.Scanf(format, params...)
	// line, _ = ReadLine()
	// parts = strings.Split(line, " ")
	// fmt.Println("parts a", parts)
	// for i := 0; i < n; i++ {
	// 	a[i], _ = strconv.Atoi(parts[i])
	// 	fmt.Println("a[i]:", i, a[i])
	// }
	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &a[i])
		sa += a[i]
		f.addEdge(ns, i+1, a[i])
		f.addEdge(i+1, n+i+1, a[i])
	}
	fmt.Scanf("\n")
	fmt.Println("input a", a)
	fmt.Println("add edges a")

	// format = ""
	// params = make([]interface{}, n)
	// for i := 0; i < n; i++ {
	// 	format += "%d"
	// 	if i != n-1 {
	// 		format += " "
	// 	} else {
	// 		format += "\n"
	// 	}
	// 	params = append(params, &b[i])
	// }
	// fmt.Scanf(format, params...)
	// line, _ = ReadLine()
	// parts = strings.Split(line, " ")
	// for i := 0; i < n; i++ {
	// b[i], _ = strconv.Atoi(parts[i])
	// }
	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &b[i])
		sb += b[i]
		f.addEdge(i+n+1, ne, b[i])
	}
	fmt.Scanf("\n")
	fmt.Println("input b", b)
	for m > 0 {
		m--
		var u, v int
		fmt.Scanf("%d %d\n", &u, &v)
		// line, _ = ReadLine()
		// parts = strings.Split(line, " ")
		// u, _ := strconv.Atoi(parts[0])
		// v, _ := strconv.Atoi(parts[1])
		f.addEdge(u, v+n, a[u-1])
		f.addEdge(v, u+n, a[v-1])
	}
	fmt.Println("input edge")
	if sa != sb {
		fmt.Println("NO")
		return
	}
	maxflow := f.EK(ns, ne)
	fmt.Println("EK")
	// fmt.Println("maxflow:", maxflow)
	if maxflow == sa {
		fmt.Println("YES")
		for i := 1; i <= n; i++ {
			for j := 1; j <= n; j++ {
				fmt.Print(f.flow[i][j+n])
				if j == n {
					fmt.Println("")
				} else {
					fmt.Print(" ")
				}
			}
		}
	} else {
		fmt.Println("NO")
	}
}

type flow struct {
	volume []map[int]int
	flow   []map[int]int
}

func NewFlow(n int) *flow {
	f := new(flow)
	f.volume = make([]map[int]int, n)
	for i := range f.volume {
		f.volume[i] = make(map[int]int)
	}
	f.flow = make([]map[int]int, n)
	for i := range f.flow {
		f.flow[i] = make(map[int]int)
	}
	return f
}

func (f *flow) addEdge(u, v, c int) {
	fmt.Println("add edge", u, v, c)
	f.volume[u][v] = c
	f.flow[u][v] = 0
}

func (f *flow) EK(start, end int) int {
	maxflow := 0

	for {
		nodeFlow := make([]int, len(f.volume))
		nodeFlow[start] = math.MaxInt32

		pre := make([]int, len(f.volume))

		queue := make([]int, 0)
		queue = append(queue, start)
		for len(queue) > 0 {
			u := queue[0]
			queue = queue[1:]
			for v, ff := range f.flow[u] {
				c := f.volume[u][v]
				// fmt.Println("u:", u, nodeFlow[u], "v:", v, "flow[u][v]:", f.flow[u][v], "vol[u][v]:", c)
				if nodeFlow[v] != 0 || c <= ff {
					// 节点被标记 或 边流量用尽，跳过
					continue
				}

				nodeFlow[v] = c - ff
				if nodeFlow[u] < nodeFlow[v] {
					nodeFlow[v] = nodeFlow[u]
				}
				// fmt.Println("u:", u, nodeFlow[u], "v:", v, nodeFlow[v])

				pre[v] = u
				queue = append(queue, v)
			}
		}
		// fmt.Println("nodeFlow[end]:", nodeFlow[end])
		if nodeFlow[end] == 0 {
			return maxflow
		}
		// fmt.Println("pre:", pre)
		for u := end; u != start; u = pre[u] {
			f.flow[pre[u]][u] += nodeFlow[end]
			f.flow[u][pre[u]] -= nodeFlow[end]
		}
		// fmt.Println("flow:")
		// for u, m := range f.flow {
		// 	for v, ff := range m {
		// 		fmt.Println(u, v, ff)
		// 	}
		// }
		// fmt.Println("")
		maxflow += nodeFlow[end]
	}
}

package main

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

const infinity = 1000000000

func slice2D(n int) [][]int {
	res := make([][]int, n)
	for i := range res {
		res[i] = make([]int, n)
	}
	return res
}

func min(a, b int) int {
	if a < b {
		return a

	}
	return b
}

func dfs(v, t, w int, cap, flow [][]int, was []bool) int {
	if v == t {
		return w
	}
	if was[v] {
		return 0
	}
	was[v] = true
	for u := range was {
		if flow[v][u] >= cap[v][u] {
			continue
		}
		nw := dfs(u, t, min(w, cap[v][u]-flow[v][u]), cap, flow, was)
		if nw > 0 {
			flow[v][u] += nw
			flow[u][v] -= nw
			return nw
		}
	}
	return 0
}

func main() {
	n := nextInt()
	m := nextInt()
	a := make([]int, n)
	for i := range a {
		a[i] = nextInt()
	}
	b := make([]int, n)
	for i := range b {
		b[i] = nextInt()
	}
	cap := slice2D(2*n + 2)
	flow := slice2D(2*n + 2)
	s := 2 * n
	t := 2*n + 1
	for i := 0; i < m; i++ {
		u := nextInt() - 1
		v := nextInt() - 1
		cap[u][v+n] = infinity
		cap[v][u+n] = infinity
	}
	var sumA, sumB int
	for i := 0; i < n; i++ {
		cap[s][i] = a[i]
		cap[i+n][t] = b[i]
		cap[i][i+n] = infinity
		sumA += a[i]
		sumB += b[i]
	}
	if sumA != sumB {
		fmt.Println("NO")
		return
	}
	flowSize := 0
	was := make([]bool, 2*n+2)
	for {
		for i := range was {
			was[i] = false
		}
		add := dfs(s, t, infinity, cap, flow, was)
		if add == 0 {
			break
		}
		flowSize += add
	}
	fmt.Println(flowSize, sumA)
	if flowSize != sumA {
		fmt.Println("NO")
		return
	}
	fmt.Println("YES")
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			if j > 0 {
				fmt.Print(" ")
			}
			fmt.Print(flow[i][j+n])
		}
		fmt.Println()
	}
}

func init() {
	// maybe defer writer.Flush() in main

	lineScanner = bufio.NewScanner(os.Stdin)
	writer = bufio.NewWriter(os.Stdout)

	/*
		inputFile, err := os.Open("a.in")
		if err != nil {
			panic(err)
		}
		outputFile, err := os.Create("a.out")
		if err != nil {
			panic(err)
		}
		lineScanner = bufio.NewScanner(inputFile)
		writer = bufio.NewWriter(outputFile)
	*/

	wordScanner = bufio.NewScanner(strings.NewReader(""))
	wordScanner.Split(bufio.ScanWords)
}

var lineScanner *bufio.Scanner
var wordScanner *bufio.Scanner

var writer *bufio.Writer

func nextLine() (string, error) {
	if !lineScanner.Scan() {
		if err := lineScanner.Err(); err != nil {
			panic(err)
		}
		return "", errors.New("nextLine: EOF reached")
	}
	return lineScanner.Text(), nil
}

func nextWord() string {
	for !wordScanner.Scan() {
		if err := wordScanner.Err(); err != nil {
			panic(err)
		}
		line, err := nextLine()
		if err != nil {
			return ""
		}
		wordScanner = bufio.NewScanner(strings.NewReader(line))
		wordScanner.Split(bufio.ScanWords)
	}
	return wordScanner.Text()
}

func nextInt() int {
	res, err := strconv.Atoi(nextWord())
	if err != nil {
		panic(err)
	}
	return res
}

func nextInt64() int64 {
	res, err := strconv.ParseInt(nextWord(), 10, 64)
	if err != nil {
		panic(err)
	}
	return res
}

func nextFloat64() float64 {
	res, err := strconv.ParseFloat(nextWord(), 64)
	if err != nil {
		panic(err)
	}
	return res
}

package main

import (
	"fmt"
	"math"
)

type (
	graph struct {
		source int
		sink   int
		edges  map[int]map[int]int
		caps   map[int]map[int]int
	}
)

func initGraph(n int) *graph {
	g := &graph{source: 0, sink: 2*n + 1}
	g.edges = map[int]map[int]int{}
	g.caps = map[int]map[int]int{}
	return g
}

func (g *graph) addEdge(src, dst, value int) {
	_, exist := g.edges[src]
	if !exist {
		g.edges[src] = map[int]int{}
	}

	_, exist = g.edges[dst]
	if !exist {
		g.edges[dst] = map[int]int{}
	}

	_, exist = g.caps[src]
	if !exist {
		g.caps[src] = map[int]int{}
	}

	g.edges[src][dst] = value
	g.edges[dst][src] = 0
	g.caps[src][dst] = value
}

func main() {
	var n, m int
	fmt.Scanf("%d %d\n", &n, &m)
	// line, _ := ReadLine()
	// parts := strings.Split(line, " ")
	// n, _ := strconv.Atoi(parts[0])
	// m, _ := strconv.Atoi(parts[1])
	g := initGraph(n)
	a := make([]int, n)
	b := make([]int, n)
	sa := 0
	sb := 0

	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &a[i])
		sa += a[i]
		// f.addEdge(ns, i+1, a[i])
		g.addEdge(g.source, i+1, a[i])
		// f.addEdge(i+1, n+i+1, a[i])
		g.addEdge(i+1, n+i+1, a[i])
	}
	fmt.Scanf("\n")
	fmt.Println("input a", a)
	// fmt.Println("add edges a")

	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &b[i])
		sb += b[i]
		// f.addEdge(i+n+1, ne, b[i])
		g.addEdge(i+n+1, g.sink, b[i])
	}
	fmt.Scanf("\n")
	fmt.Println("input b", b)

	for m > 0 {
		m--
		var u, v int
		fmt.Scanf("%d %d\n", &u, &v)
		// f.addEdge(u, v+n, a[u-1])
		fmt.Println(u, v)
		g.addEdge(u, v+n, a[u-1])
		// f.addEdge(v, u+n, a[v-1])
		g.addEdge(v, u+n, a[v-1])
	}
	fmt.Println("input edge", g.edges)
	if sa != sb {
		fmt.Println("NO", sa, sb)
		return
	}

	maxflow := 0

	for {
		dist, hasRoad := bfs(g)
		if !hasRoad {
			break
		}

		fmt.Println(dist)
		for {
			value := dfs(g, dist, g.source, math.MaxInt32)
			fmt.Println(value)
			if value == 0 {
				break
			}
			maxflow += value
		}
	}
	// fmt.Println("EK")
	// fmt.Println("maxflow:", maxflow)
	if maxflow == sa {
		fmt.Println("YES")
		for i := 1; i <= n; i++ {
			for j := 1; j <= n; j++ {
				fmt.Print(g.caps[i][j+n] - g.edges[i][j+n])
				if j == n {
					fmt.Println("")
				} else {
					fmt.Print(" ")
				}
			}
		}
	} else {
		fmt.Println("NO", maxflow)
	}
}

func bfs(g *graph) (map[int]int, bool) {

	if g == nil {
		return nil, false
	}

	visited := map[int]bool{}
	dist := map[int]int{}

	queue := []int{}
	visited[g.source] = true
	dist[g.source] = 0
	queue = append(queue, g.source)

	for len(queue) > 0 {
		tmp := queue[0]
		queue = queue[1:]
		edgeFlows, exist := g.edges[tmp]

		if !exist {
			continue
		}

		for enode, flow := range edgeFlows {
			if enode == g.sink && flow > 0 {
				dist[enode] = dist[tmp] + 1
				fmt.Println("dist:", dist)
				return dist, true
			}

			if !visited[enode] && flow > 0 {
				visited[enode] = true
				dist[enode] = dist[tmp] + 1
				queue = append(queue, enode)
			}
		}
	}

	return nil, false
}

func dfs(g *graph, dist map[int]int, node, max int) int {

	if g == nil {
		return 0
	}

	if node == g.sink {
		return max
	}

	edgeFlows, exist := g.edges[node]
	if exist {
		for enode, flow := range edgeFlows {
			if dist[enode] == dist[node]+1 && flow > 0 {
				value := dfs(g, dist, enode, min(flow, max))
				fmt.Println(value)
				if value > 0 {
					g.edges[node][enode] -= value
					g.edges[enode][node] += value
				}
				return value
			}
		}
	}

	return 0
}

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

package main

import (
	"fmt"
	"math"
)

type (
	graph struct {
		source int
		sink   int
		edges  map[int]map[int]int
		caps   map[int]map[int]int
	}
)

func initGraph(n int) *graph {
	g := &graph{source: 0, sink: 2*n + 1}
	g.edges = map[int]map[int]int{}
	g.caps = map[int]map[int]int{}
	return g
}

func (g *graph) addEdge(src, dst, value int) {
	_, exist := g.edges[src]
	if !exist {
		g.edges[src] = map[int]int{}
	}

	_, exist = g.edges[dst]
	if !exist {
		g.edges[dst] = map[int]int{}
	}

	_, exist = g.caps[src]
	if !exist {
		g.caps[src] = map[int]int{}
	}

	g.edges[src][dst] = value
	g.edges[dst][src] = 0
	g.caps[src][dst] = value
}

func main() {
	var n, m int
	fmt.Scanf("%d %d\n", &n, &m)

	g := initGraph(n)
	a := make([]int, n)
	b := make([]int, n)
	sa := 0
	sb := 0

	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &a[i])
		sa += a[i]
		g.addEdge(g.source, i+1, a[i])
		g.addEdge(i+1, n+i+1, a[i])
	}
	fmt.Scanf("\n")
	// fmt.Println("input a", a)

	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &b[i])
		sb += b[i]
		g.addEdge(i+n+1, g.sink, b[i])
	}
	fmt.Scanf("\n")
	// fmt.Println("input b", b)

	for m > 0 {
		m--
		var u, v int
		fmt.Scanf("%d %d\n", &u, &v)
		fmt.Println(u, v)
		g.addEdge(u, v+n, a[u-1])
		g.addEdge(v, u+n, a[v-1])
	}
	// fmt.Println("input edge", g.edges)
	if sa != sb {
		fmt.Println("NO")
		return
	}

	maxflow := 0

	for {
		dist, hasRoad := bfs(g)
		if !hasRoad {
			break
		}

		// fmt.Println(dist)
		for {
			value := dfs(g, dist, g.source, math.MaxInt32)
			// fmt.Println(value)
			if value == 0 {
				break
			}
			maxflow += value
		}
	}
	// fmt.Println("EK")
	// fmt.Println("maxflow:", maxflow)
	if maxflow == sa {
		fmt.Println("YES")
		for i := 1; i <= n; i++ {
			for j := 1; j <= n; j++ {
				fmt.Print(g.caps[i][j+n] - g.edges[i][j+n])
				if j == n {
					fmt.Println("")
				} else {
					fmt.Print(" ")
				}
			}
		}
	} else {
		fmt.Println("NO")
	}
}

func bfs(g *graph) (map[int]int, bool) {

	if g == nil {
		return nil, false
	}

	visited := map[int]bool{}
	dist := map[int]int{}

	queue := []int{}
	visited[g.source] = true
	dist[g.source] = 0
	queue = append(queue, g.source)

	for len(queue) > 0 {
		tmp := queue[0]
		queue = queue[1:]
		edgeFlows, exist := g.edges[tmp]

		if !exist {
			continue
		}

		for enode, flow := range edgeFlows {
			if enode == g.sink && flow > 0 {
				dist[enode] = dist[tmp] + 1
				// fmt.Println("dist:", dist)
				return dist, true
			}

			if !visited[enode] && flow > 0 {
				visited[enode] = true
				dist[enode] = dist[tmp] + 1
				queue = append(queue, enode)
			}
		}
	}

	return nil, false
}

func dfs(g *graph, dist map[int]int, node, max int) int {

	if g == nil {
		return 0
	}

	if node == g.sink {
		return max
	}

	edgeFlows, exist := g.edges[node]
	if exist {
		for enode, flow := range edgeFlows {
			if dist[enode] == dist[node]+1 && flow > 0 {
				value := dfs(g, dist, enode, min(flow, max))
				fmt.Println(value)
				if value > 0 {
					g.edges[node][enode] -= value
					g.edges[enode][node] += value
				}
				return value
			}
		}
	}

	return 0
}

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


In this problem, a $$$n \times m$$$ rectangular matrix $$$a$$$ is called increasing if, for each row of $$$i$$$, when go from left to right, the values strictly increase (that is, $$$a_{i,1}<a_{i,2}<\dots<a_{i,m}$$$) and for each column $$$j$$$, when go from top to bottom, the values strictly increase (that is, $$$a_{1,j}<a_{2,j}<\dots<a_{n,j}$$$).In a given matrix of non-negative integers, it is necessary to replace each value of $$$0$$$ with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.It is guaranteed that in a given value matrix all values of $$$0$$$ are contained only in internal cells (that is, not in the first or last row and not in the first or last column).

The first line contains integers $$$n$$$ and $$$m$$$ ($$$3 \le n, m \le 500$$$) — the number of rows and columns in the given matrix $$$a$$$. The following lines contain $$$m$$$ each of non-negative integers — the values in the corresponding row of the given matrix: $$$a_{i,1}, a_{i,2}, \dots, a_{i,m}$$$ ($$$0 \le a_{i,j} \le 8000$$$). It is guaranteed that for all $$$a_{i,j}=0$$$, $$$1 < i < n$$$ and $$$1 < j < m$$$ are true.

If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.

NoteIn the first example, the resulting matrix is as follows: 1 3 5 6 73 6 7 8 95 7 8 9 108 9 10 11 12In the second example, the value $$$3$$$ must be put in the middle cell.In the third example, the desired resultant matrix does not exist.

//HOWDY! IT'S ME! A HARD BLACKISH DICK MOVE!
package main

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

/********** I/O usage **********/

//str := ReadString()
//i := ReadInt()
//X := ReadIntSlice(n)
//S := ReadRuneSlice()
//a := ReadFloat64()
//A := ReadFloat64Slice(n)

//str := PrintlnInts(X...)

/*******************************************************************/
func increasing(mat [][]int) bool {
	n := len(mat)
	m := len(mat[0])
	for i := 0; i < n-1; i++ {
		for j := 0; j < m; j++ {
			if mat[i][j] >= mat[i+1][j] {
				return false
			}
		}
	}
	for i := 0; i < n; i++ {
		for j := 0; j < m-1; j++ {
			if mat[i][j] >= mat[i][j+1] {
				return false
			}
		}
	}
	return true
}

func sum(mat [][]int) int {
	ret := 0
	for _, row := range mat {
		for _, el := range row {
			ret += el
		}
	}
	return ret
}

func main() {
	n, m := ReadInt2()
	mat := make([][]int, n)
	for i := 0; i < n; i++ {
		mat[i] = ReadIntSlice(m)
	}
	for i := n - 2; i >= 1; i-- {
		for j := m - 2; j >= 1; j-- {
			if mat[i][j] == 0 {
				mat[i][j] = min(mat[i][j+1], mat[i+1][j]) - 1
			}
		}
	}
	if increasing(mat) {
		fmt.Println(sum(mat))
	} else {
		fmt.Println(-1)
	}
}

/*********** Math ***********/
func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func min(args ...int) int {
	if len(args) == 0 {
		panic("empty!")
	}
	min := args[0]
	for i := 1; i < len(args); i++ {
		if args[i] < min {
			min = args[i]
		}
	}
	return min
}

func max(args ...int) int {
	if len(args) == 0 {
		panic("empty!")
	}
	max := args[0]
	for i := 1; i < len(args); i++ {
		if args[i] > max {
			max = args[i]
		}
	}
	return max
}

func _gcd(a, b int) int {
	if b == 0 {
		return a
	}
	return _gcd(b, a%b)
}

func gcd(args ...int) int {
	if len(args) == 0 {
		panic("empty!")
	}
	gcd := args[0]
	for i := 1; i < len(args); i++ {
		gcd = _gcd(gcd, args[i])
	}
	return gcd
}

/*********** I/O ***********/

var (
	// ReadString returns a WORD string.
	ReadString func() string
	stdout     *bufio.Writer
)

func init() {
	ReadString = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) func() string {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), int(1e+9))
	// Split sets the split function for the Scanner. The default split function is ScanLines.
	// Split panics if it is called after scanning has started.
	r.Split(bufio.ScanWords)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// ReadInt returns an integer.
func ReadInt() int {
	return readInt()
}
func ReadInt2() (int, int) {
	return readInt(), readInt()
}
func ReadInt3() (int, int, int) {
	return readInt(), readInt(), readInt()
}
func ReadInt4() (int, int, int, int) {
	return readInt(), readInt(), readInt(), readInt()
}

func readInt64() int64 {
	return readIntNbits(64)
}

func readInt() int {
	return int(readIntNbits(32))
}

func readIntNbits(n int) int64 {
	i, err := strconv.ParseInt(ReadString(), 0, n)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// ReadIntSlice returns an integer slice that has n integers.
func ReadIntSlice(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt()
	}
	return b
}

// ReadFloat64 returns an float64.
func ReadFloat64() float64 {
	return float64(readFloat64())
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(ReadString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func ReadFloat64Slice(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadFloat64()
	}
	return b
}

// ReadRuneSlice returns a rune slice.
func ReadRuneSlice() []rune {
	return []rune(ReadString())
}

// PrintIntsLine returns integers string delimited by a space.
func PrintlnInts(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}


Vasya claims that he had a paper square. He cut it into two rectangular parts using one vertical or horizontal cut. Then Vasya informed you the dimensions of these two rectangular parts. You need to check whether Vasya originally had a square. In other words, check if it is possible to make a square using two given rectangles.

The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case is given in two lines. The first line contains two integers $$$a_1$$$ and $$$b_1$$$ ($$$1 \le a_1, b_1 \le 100$$$) — the dimensions of the first one obtained after cutting rectangle. The sizes are given in random order (that is, it is not known which of the numbers is the width, and which of the numbers is the length). The second line contains two integers $$$a_2$$$ and $$$b_2$$$ ($$$1 \le a_2, b_2 \le 100$$$) — the dimensions of the second obtained after cutting rectangle. The sizes are given in random order (that is, it is not known which of the numbers is the width, and which of the numbers is the length).

Print $$$t$$$ answers, each of which is a string "YES" (in the case of a positive answer) or "NO" (in the case of a negative answer). The letters in words can be printed in any case (upper or lower).

// Problem statement can be found in
// https://codeforces.com/problemset/problem/1351/B

package main

import (
	"fmt"
	"math"
)

func main(){
	var t int
	fmt.Scan(&t)

	for i:=0; i < t ; i++ {
		var a1, a2, b1, b2 float64
		fmt.Scan(&a1, &b1, &a2, &b2)

		if math.Max(a1, b1)==math.Max(a2, b2) && (math.Min(a1, b1)+math.Min(a2, b2) == math.Max(a1, b1)) {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

// https://codeforces.com/contest/1348/problem/A
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
var input io.Reader

func main() {
	var (
		T int
	)

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input = bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &T)
	for i := 0; i < T; i++ {
		solver()
	}
}

func solver() {
	var a, b, c,d int
	fmt.Fscan(input, &a, &b, &c, &d)
	if a > b {
		a, b = b, a
	}
	if c > d {
		c, d = d, c
	}

	if d == b && a + c == d {
		fmt.Println("Yes")
	} else {
		fmt.Println("No")
	}

}

package main

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

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

	var t, a, b, c, d int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &a, &b, &c, &d)
		if a > b {
			a, b = b, a
		}
		if c > d {
			c, d = d, c
		}
		if b == d && b == a+c {
			Fprintln(out, "Yes")
		} else {
			Fprintln(out, "No")
		}
	}
}

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

package main

import (
    "fmt"
)

func main() {
    var t int
    fmt.Scanf("%d\n", &t)
    for i := 0; i < t; i++ {
        var a1, b1, a2, b2 int
        fmt.Scanf("%d %d\n%d %d\n", &a1, &b1, &a2, &b2)
        if (a1 == a2 && a1 == b1 + b2) || (b1 == b2 && a1 + a2 == b1) {
            fmt.Println("Yes")
            continue
        }
        a2, b2 = b2, a2
        if (a1 == a2 && a1 == b1 + b2) || (b1 == b2 && a1 + a2 == b1) {
            fmt.Println("Yes")
            continue
        }
        fmt.Println("No")
    }
}

package main

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

func main() {
	r := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()

	var t, max1, min1, max2, min2 int

	fmt.Fscan(r, &t)

	for i := 0; i < t; i++ {
		fmt.Fscan(r, &max1, &min1)
		fmt.Fscan(r, &max2, &min2)

		if max1 < min1 {
			max1, min1 = min1, max1
		}

		if max2 < min2 {
			max2, min2 = min2, max2
		}

		if (max1 == max2) && (min2+min1 == max1) {
			fmt.Fprintln(w, "YES")
		} else {
			fmt.Fprintln(w, "NO")
		}

	}
}

package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0

	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 0
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	solve(scanner, writer)
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}

	writer.Flush()
}

func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	t := getNextInt(scanner)
	for i := 0; i < t; i++ {
		aa := make([]int, 2)
		bb := make([]int, 2)
		for j := 0; j < 2; j++ {
			aa[j] = getNextInt(scanner)
		}
		for j := 0; j < 2; j++ {
			bb[j] = getNextInt(scanner)
		}

		if isYes(aa, bb) {
			fmt.Fprintln(writer, "Yes")
			continue
		}
		fmt.Fprintln(writer, "No")
	}
}

func isYes(aa, bb []int) bool {
	for j := 0; j < 2; j++ {
		for k := 0; k < 2; k++ {
			if aa[j] != bb[k] {
				continue
			}
			if aa[j] == aa[1-j]+bb[1-k] {
				return true
			}
		}
	}
	return false
}

// Problem statement can be found in
// https://codeforces.com/problemset/problem/1351/B

package main

import (
	"fmt"
	"math"
)

func main(){
	var t int
	fmt.Scan(&t)

	for i:=0; i < t ; i++ {
		var a1, a2, b1, b2 int
		fmt.Scan(&a1, &b1, &a2, &b2)

		v := math.Sqrt(float64(a1+a2+b1+b2))
		if v - math.Floor(v) == 0 {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

// Problem statement can be found in
// https://codeforces.com/problemset/problem/1351/B

package main

import (
	"fmt"
	"math"
)

func main(){
	var t int
	fmt.Scan(&t)

	for i:=0; i < t ; i++ {
		var a1, a2, b1, b2 int
		fmt.Scan(&a1, &b1, &a2, &b2)

		v := math.Sqrt(float64((a1*b1)+(a2*b2)))
		if v - math.Floor(v) == 0 {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}


You may have already known that a standard ICPC team consists of exactly three members. The perfect team however has more restrictions. A student can have some specialization: coder or mathematician. She/he can have no specialization, but can't have both at the same time.So the team is considered perfect if it includes at least one coder, at least one mathematician and it consists of exactly three members.You are a coach at a very large university and you know that $$$c$$$ of your students are coders, $$$m$$$ are mathematicians and $$$x$$$ have no specialization.What is the maximum number of full perfect teams you can distribute them into? Note that some students can be left without a team and each student can be a part of no more than one team.You are also asked to answer $$$q$$$ independent queries.

The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of queries.  Each of the next $$$q$$$ lines contains three integers $$$c$$$, $$$m$$$ and $$$x$$$ ($$$0 \le c, m, x \le 10^8$$$) — the number of coders, mathematicians and students without any specialization in the university, respectively. Note that the no student is both coder and mathematician at the same time.

Print $$$q$$$ integers — the $$$i$$$-th of them should be the answer to the $$$i$$$ query in the order they are given in the input. The answer is the maximum number of full perfect teams you can distribute your students into.

NoteIn the first example here are how teams are formed:  the only team of 1 coder, 1 mathematician and 1 without specialization;  all three teams consist of 1 coder and 2 mathematicians;  no teams can be formed;  no teams can be formed;  one team consists of 1 coder, 1 mathematician and 1 without specialization, the rest aren't able to form any team;  one team consists of 1 coder, 1 mathematician and 1 without specialization, one consists of 2 coders and 1 mathematician and one consists of 1 coder and 2 mathematicians.

package main

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

func clear(arr []byte) []byte {
	if string(arr[len(arr)-2:len(arr)]) == "\r\n" {
		return arr[:len(arr)-2]
	} else if string(arr[len(arr)-1]) == "\n" {
		return arr[:len(arr)-1]
	}
	return arr
}

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

func isWin(b []byte) int {
	values := strings.Split(string(clear(b)), " ")
	c, _ := strconv.Atoi(values[0])
	m, _ := strconv.Atoi(values[1])
	x, _ := strconv.Atoi(values[2])

	var team int

	min := int(math.Abs(float64(c - m)))
	progers := c + m - min
	x += min

	if x <= progers/2 {
		team += x
		progers -= x * 2
		x = 0
	} else {
		team += progers / 2
		progers = 0
	}

	if progers > 2 {
		team += progers / 3
	}

	return team
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	line, _ := reader.ReadBytes('\n')
	q, _ := strconv.Atoi(string(clear(line)))

	for i := 0; i < q; i++ {
		array, _ := reader.ReadBytes('\n')
		fmt.Printf("%v\n", isWin(array))
	}
}

package main

import (
	"bufio"
	"os"
	"runtime"
	"strconv"
)

/* solution */

func solve() {
	q := readInt()
	for i := 0; i < q; i++ {
		printInt(solveOne())
	}
}

func solveOne() int {
	c, m, x := readInt(), readInt(), readInt()
	min := c
	if m < min {
		min = m
	}
	r := c + m + x - min*2
	if min <= r {
		return min
	}
	return (c + m + x) / 3
}

/* stubs */

func main() {

	var f *os.File
	if runtime.GOROOT() == "C:\\Soft\\Go" {
		f, _ = os.Open("input.txt")
		defer f.Close()
	} else {
		f = os.Stdin
	}

	writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	reader = bufio.NewScanner(f)

	const maxCapacity = 1024 * 1024
	buf := make([]byte, maxCapacity)
	reader.Buffer(buf, maxCapacity)

	reader.Split(bufio.ScanWords)

	solve()

}

var reader *bufio.Scanner
var writer *bufio.Writer

func read() string {
	reader.Scan()
	return reader.Text()
}

func readInt() int {
	reader.Scan()
	ret, _ := strconv.Atoi(reader.Text())
	return ret
}

func print(s string) {
	writer.WriteString(s)
	writer.WriteString("\n")
}

func printInt(i int) {
	print(strconv.Itoa(i))
}

func printInt64(i int64) {
	print(strconv.FormatInt(i, 10))
}

func readInt64() int64 {
	reader.Scan()
	ret, _ := strconv.ParseInt(reader.Text(), 10, 64)
	return ret
}

package main

import (
    "fmt"
    "bufio"
    "os"
)
var input *bufio.Reader

func min(x, y, z int64) int64 {
    var res int64
    res = x
    if y < res {
        res = y
    }
    if z < res {
        res = z
    }
    return res
}

func solve() {
    var (
        C, M, X int64
        max_teams int64
    )
    fmt.Fscanf(input, "%d %d %d\n", &C, &M, &X)
    max_teams = (C+M+X) / 3
    fmt.Println(min(max_teams,C,M))
}

func main () {
    input = bufio.NewReader(os.Stdin)
    var T int
    fmt.Fscanf(input, "%d\n", &T)
    for i:=0 ; i<T ; i++ {
        solve()
    }
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"math"
	"math/big"
	"os"
	"strconv"
)

var scan *bufio.Scanner
var reader io.Reader
var writer io.Writer

func init() {
	scan = bufio.NewScanner(os.Stdin)
	reader = os.Stdin
	writer = os.Stdout
	scan.Split(bufio.ScanWords)
}

func main() {
	solve()
}

func solve() {
	n := nextInt()
	var c, m, x int
	for i := 0; i < n; i++ {
		c = nextInt()
		m = nextInt()
		x = nextInt()
		fmt.Fprintln(writer, min(c, m, (c+m+x)/3))
	}
}

func nextWord() string {
	var s string
	fmt.Fscan(reader, &s)
	return s
}

func nextInt() int {
	i, e := strconv.Atoi(nextWord())
	if e != nil {
		panic(e)
	}
	return i
}

func max(x int, y ...int) int {
	res := float64(x)
	for _, z := range y {
		res = math.Max(res, float64(z))
	}
	return int(res)
}

func min(x int, y ...int) int {
	res := float64(x)
	for _, z := range y {
		res = math.Min(res, float64(z))
	}
	return int(res)
}

func abs(x int) int {
	return int(math.Abs(float64(x)))
}

func sqrt(x int) int {
	return int(math.Sqrt(float64(x)))
}

func int2Int(x int) big.Int {
	var res big.Int
	res.SetInt64(int64(x))
	return res
}

package main

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

func main() {
	input := bufio.NewReader(os.Stdin)
	var (
		count int
	)
	_, _ = fmt.Fscan(input, &count)

	for count > 0 {
		count--
		var (
			coder   int
			mather  int
			useless int
		)
		_, _ = fmt.Fscan(input, &coder, &mather, &useless)

		maxTeam := coder
		if maxTeam > mather {
			maxTeam = mather
		}

		rMaxTeam := (coder + mather + useless) / 3
		if maxTeam > rMaxTeam {
			maxTeam = rMaxTeam
		}
		fmt.Println(maxTeam)
	}

}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	q := 0
	fmt.Fscanf(in, "%d\n", &q)
	for ii := 0; ii < q; ii++ {
		c, m, x := 0, 0, 0
		fmt.Fscanf(in, "%d %d %d\n", &c, &m, &x)
		l := 0
		r := (c + m + x) / 3
		for l < r {
			mid := (l + r + 1) / 2
			if !can(c,m,x,mid) {
				r = mid  - 1
			} else {
				l = mid
			}
		}
		fmt.Fprintf(out,"%d\n", l)
	}
	return
}

func can(a, b, c, aim int) bool {
	if a < aim {
		return false
	}
	if b < aim {
		return false
	}
	if (a - aim) + b - aim + c < aim {
		return false
	}
	return true
}

func minstep(a, b byte) int {
	return min(int(a-b+26)%26, int(b-a+26)%26)
}

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

package main

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

func toInt(buf []byte) uint32 {
	n := uint32(0)
	for _, v := range buf {
		n = n*10 + uint32(v-'0')
	}

	return n
}

func solve(c, m, o uint32) uint32 {
	if c <= m && c <= o {
		return c
	}

	if m <= c && m <= o {
		return m
	}

	r := (m + c + o) / 3
	if c < r {
		r = c
	}
	if m < r {
		r = m
	}

	return r
}

func main() {
	var q uint32

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

	scanner.Scan()
	q = toInt(scanner.Bytes())

	for i := uint32(0); i < q; i++ {
		a := make([]uint32, 3)
		for j := range a {
			scanner.Scan()
			a[j] = toInt(scanner.Bytes())
		}

		res := solve(a[0], a[1], a[2])

		fmt.Println(res)
	}
}

package main

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

func main() {
	input := bufio.NewReader(os.Stdin)
	var testCase int

	_, err := fmt.Fscan(input, &testCase)
	if err != nil {
	}

	for testCase > 0 {
		testCase--

		var c, m, x int
		_, err := fmt.Fscan(input, &c, &m, &x)
		if err != nil {
		}

		teamNum := (c + m + x) / 3
		if c < m {
			if c < teamNum {
				fmt.Println(c)
			} else {
				fmt.Println(teamNum)
			}
		} else {
			if m < teamNum {
				fmt.Println(m)
			} else {
				fmt.Println(teamNum)
			}
		}

	}

}

package main

import (
	"bufio"
	"fmt"
	"io"
	"math/rand"
	"os"
)

const INF  = 1000000001

type Pair struct {
	first, second int
}

func sort(arr []int) []int {
	if len(arr) < 2 {
		return arr
	}
	left := 0
	right := len(arr) - 1
	pivot := rand.Int() % len(arr)

	arr[pivot], arr[right] = arr[right], arr[pivot]

	for i := 0; i < len(arr); i++ {
		if arr[i] < arr[right] {
			arr[i], arr[left] = arr[left], arr[i]
			left++
		}
	}

	arr[left], arr[right] = arr[right], arr[left]

	sort(arr[:left])
	sort(arr[left+1:])
	return arr
}

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

func solution(reader io.Reader, writer io.Writer) {
	var q int
	fmt.Fscanf(reader, "%d\n", &q)
	for i := 0; i < q; i++ {
		var c, m, x int
		fmt.Fscanf(reader, "%d %d %d\n", &c, &m, &x)

		mm := min(min(c, m), x)
		c -= mm
		m -= mm
		x -= mm

		if c == 0 || m == 0 {
		} else {
			if c == m {
				mm += (c + m) / 3
			} else {
				if c > m {
					c, m = m, c
				}
				mm += min(c, (c + m) / 3)
			}
		}
		fmt.Println(mm)
	}
}

func main() {

	//stdin, err := os.Open("file.in")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdin.Close()
	stdin := os.Stdin
	reader := bufio.NewReaderSize(stdin, 1024*1024)

	//stdout, err := os.Create("file.out")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdout.Close()
	stdout := os.Stdout
	writer := bufio.NewWriterSize(stdout, 1024*1024)
	defer writer.Flush()

	solution(reader, writer)
}

// Author: sighduck
// URL: https://codeforces.com/contest/1221/problem/C

package main

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

func min(x int, y int) int {
	if x < y {
		return x
	}
	return y
}

func Solve(c int, m int, x int) int {
	p := min(c, m)
	c -= p
	m -= p
	f := c + m + x
	if p < f {
		return p
	}
	return f + (p-f)*2/3
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var q int
	fmt.Fscanf(reader, "%d\n", &q)
	for i := 0; i < q; i++ {
		var c, m, x int
		fmt.Fscanf(reader, "%d %d %d\n", &c, &m, &x)
		fmt.Fprintf(writer, "%d\n", Solve(c, m, x))
	}
}

package main

import (
	"bufio"
	"os"
	"runtime"
	"strconv"
)

/* solution */

func solve() {
	q := readInt()
	for i := 0; i < q; i++ {
		printInt(solveOne())
	}
}

func solveOne() int {
	c, m, x := readInt(), readInt(), readInt()
	min := c
	if m < min {
		min = m
	}
	x += c + m - min - min
	if min <= x {
		return min
	}
	return (c + m + x) / 3
}

/* stubs */

func main() {

	var f *os.File
	if runtime.GOROOT() == "C:\\Soft\\Go" {
		f, _ = os.Open("input.txt")
		defer f.Close()
	} else {
		f = os.Stdin
	}

	writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	reader = bufio.NewScanner(f)

	const maxCapacity = 1024 * 1024
	buf := make([]byte, maxCapacity)
	reader.Buffer(buf, maxCapacity)

	reader.Split(bufio.ScanWords)

	solve()

}

var reader *bufio.Scanner
var writer *bufio.Writer

func read() string {
	reader.Scan()
	return reader.Text()
}

func readInt() int {
	reader.Scan()
	ret, _ := strconv.Atoi(reader.Text())
	return ret
}

func print(s string) {
	writer.WriteString(s)
	writer.WriteString("\n")
}

func printInt(i int) {
	print(strconv.Itoa(i))
}

func printInt64(i int64) {
	print(strconv.FormatInt(i, 10))
}

func readInt64() int64 {
	reader.Scan()
	ret, _ := strconv.ParseInt(reader.Text(), 10, 64)
	return ret
}

package main

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

func toInt(buf []byte) uint32 {
	n := uint32(0)
	for _, v := range buf {
		n = n*10 + uint32(v-'0')
	}

	return n
}

func solve(c, m, o uint32) uint32 {
	//var r uint32

	if c <= m && c <= o {
		return c
	}

	if m <= c && m <= o {
		return m
	}

	return (m + c + o) / 3
}

func main() {
	var q uint32

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

	scanner.Scan()
	q = toInt(scanner.Bytes())

	for i := uint32(0); i < q; i++ {
		a := make([]uint32, 3)
		for j := range a {
			scanner.Scan()
			a[j] = toInt(scanner.Bytes())
		}

		res := solve(a[0], a[1], a[2])

		fmt.Println(res)
	}
}

package main

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

func toInt(buf []byte) uint32 {
	n := uint32(0)
	for _, v := range buf {
		n = n*10 + uint32(v-'0')
	}

	return n
}

func solve(c, m, o uint32) uint32 {
	if c <= m && c <= o {
		return c
	}

	if m <= c && m <= o {
		return m
	}

	r := (m + c + o) / 3
	if m < c {
		r = c
	}
	if m < r {
		r = m
	}

	return r
}

func main() {
	var q uint32

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

	scanner.Scan()
	q = toInt(scanner.Bytes())

	for i := uint32(0); i < q; i++ {
		a := make([]uint32, 3)
		for j := range a {
			scanner.Scan()
			a[j] = toInt(scanner.Bytes())
		}

		res := solve(a[0], a[1], a[2])

		fmt.Println(res)
	}
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"math/rand"
	"os"
)

const INF  = 1000000001

type Pair struct {
	first, second int
}

func sort(arr []int) []int {
	if len(arr) < 2 {
		return arr
	}
	left := 0
	right := len(arr) - 1
	pivot := rand.Int() % len(arr)

	arr[pivot], arr[right] = arr[right], arr[pivot]

	for i := 0; i < len(arr); i++ {
		if arr[i] < arr[right] {
			arr[i], arr[left] = arr[left], arr[i]
			left++
		}
	}

	arr[left], arr[right] = arr[right], arr[left]

	sort(arr[:left])
	sort(arr[left+1:])
	return arr
}

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

func solution(reader io.Reader, writer io.Writer) {
	var q int
	fmt.Fscanf(reader, "%d\n", &q)
	for i := 0; i < q; i++ {
		var c, m, x int
		fmt.Fscanf(reader, "%d %d %d\n", &c, &m, &x)

		mm := min(min(c, m), x)
		c -= mm
		m -= mm
		x -= mm

		if c == 0 || m == 0 {
			fmt.Println(mm)
		} else {
			fmt.Println((c + m) / 3 + mm)
		}

	}
}

func main() {

	//stdin, err := os.Open("file.in")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdin.Close()
	stdin := os.Stdin
	reader := bufio.NewReaderSize(stdin, 1024*1024)

	//stdout, err := os.Create("file.out")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdout.Close()
	stdout := os.Stdout
	writer := bufio.NewWriterSize(stdout, 1024*1024)
	defer writer.Flush()

	solution(reader, writer)
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"math/rand"
	"os"
)

const INF  = 1000000001

type Pair struct {
	first, second int
}

func sort(arr []int) []int {
	if len(arr) < 2 {
		return arr
	}
	left := 0
	right := len(arr) - 1
	pivot := rand.Int() % len(arr)

	arr[pivot], arr[right] = arr[right], arr[pivot]

	for i := 0; i < len(arr); i++ {
		if arr[i] < arr[right] {
			arr[i], arr[left] = arr[left], arr[i]
			left++
		}
	}

	arr[left], arr[right] = arr[right], arr[left]

	sort(arr[:left])
	sort(arr[left+1:])
	return arr
}

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

func solution(reader io.Reader, writer io.Writer) {
	var q int
	fmt.Fscanf(reader, "%d\n", &q)
	for i := 0; i < q; i++ {
		var c, m, x int
		fmt.Fscanf(reader, "%d %d %d\n", &c, &m, &x)

		mm := min(min(c, m), x)
		c -= mm
		m -= mm
		x -= mm

		if c == 0 || m == 0 {
			fmt.Println(mm)
		} else {
			if c == m {
				fmt.Println((c + m) / 3 + mm)
			} else {
				if c > m {
					fmt.Println(mm + min(m, c - m))
				} else {
					fmt.Println(mm + min(c, m - c))
				}
			}
		}

	}
}

func main() {

	//stdin, err := os.Open("file.in")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdin.Close()
	stdin := os.Stdin
	reader := bufio.NewReaderSize(stdin, 1024*1024)

	//stdout, err := os.Create("file.out")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdout.Close()
	stdout := os.Stdout
	writer := bufio.NewWriterSize(stdout, 1024*1024)
	defer writer.Flush()

	solution(reader, writer)
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"math/rand"
	"os"
)

const INF  = 1000000001

type Pair struct {
	first, second int
}

func sort(arr []int) []int {
	if len(arr) < 2 {
		return arr
	}
	left := 0
	right := len(arr) - 1
	pivot := rand.Int() % len(arr)

	arr[pivot], arr[right] = arr[right], arr[pivot]

	for i := 0; i < len(arr); i++ {
		if arr[i] < arr[right] {
			arr[i], arr[left] = arr[left], arr[i]
			left++
		}
	}

	arr[left], arr[right] = arr[right], arr[left]

	sort(arr[:left])
	sort(arr[left+1:])
	return arr
}

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

func solution(reader io.Reader, writer io.Writer) {
	var q int
	fmt.Fscanf(reader, "%d\n", &q)
	for i := 0; i < q; i++ {
		var c, m, x int
		fmt.Fscanf(reader, "%d %d %d\n", &c, &m, &x)

		mm := min(min(c, m), x)
		c -= mm
		m -= mm
		x -= mm

		if c == 0 || m == 0 {
			fmt.Println(mm)
		} else {
			if c == m {
				fmt.Println((c + m) / 3 + mm)
			} else {
				if c > m {
					fmt.Println(mm + min(m, (c - m) / 2))
				} else {
					fmt.Println(mm + min(c, (m - c) / 2))
				}
			}
		}

	}
}

func main() {

	//stdin, err := os.Open("file.in")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdin.Close()
	stdin := os.Stdin
	reader := bufio.NewReaderSize(stdin, 1024*1024)

	//stdout, err := os.Create("file.out")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdout.Close()
	stdout := os.Stdout
	writer := bufio.NewWriterSize(stdout, 1024*1024)
	defer writer.Flush()

	solution(reader, writer)
}