You found a painting on a canvas of size $$$n \times m$$$. The canvas can be represented as a grid with $$$n$$$ rows and $$$m$$$ columns. Each cell has some color. Cell $$$(i, j)$$$ has color $$$c_{i,j}$$$.Near the painting you also found a brush in the shape of a $$$2 \times 2$$$ square, so the canvas was surely painted in the following way: initially, no cell was painted. Then, the following painting operation has been performed some number of times:  Choose two integers $$$i$$$ and $$$j$$$ ($$$1 \le i < n$$$, $$$1 \le j < m$$$) and some color $$$k$$$ ($$$1 \le k \le nm$$$).  Paint cells $$$(i, j)$$$, $$$(i + 1, j)$$$, $$$(i, j + 1)$$$, $$$(i + 1, j + 1)$$$ in color $$$k$$$. All cells must be painted at least once. A cell can be painted multiple times. In this case, its final color will be the last one.Find any sequence of at most $$$nm$$$ operations that could have led to the painting you found or state that it's impossible.

The first line of input contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n, m \le 1000$$$) — the dimensions of the canvas. On the $$$i$$$-th of the next $$$n$$$ lines of input, there will be $$$m$$$ integers. The $$$j$$$-th of them is $$$a_{i,j}$$$ ($$$1 \le a_{i,j} \le nm$$$) — the color of cell $$$(i, j)$$$.

If there is no solution, print a single integer $$$-1$$$. Otherwise, on the first line, print one integer $$$q$$$ ($$$1 \le q \le nm$$$) — the number of operations. Next, print the operations in order. On the $$$k$$$-th of the next $$$q$$$ lines, print three integers $$$i$$$, $$$j$$$, $$$c$$$ ($$$1 \le i < n$$$, $$$1 \le j < m$$$, $$$1 \le c \le nm$$$) — the description of the $$$k$$$-th operation. If there are multiple solutions, print any.

NoteIn the first test case, the solution is not unique. Here's one of them:  In the second test case, there is no way one could obtain the given painting, thus the answer is $$$-1$$$.

/*
https://codeforces.com/contest/1638/problem/D
*/

package main

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

type st struct {
	I int
	J int
	C int
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	line1, _ := reader.ReadString('\n')
	line1_arr := strings.Fields(line1)
	//fmt.Println(line1_arr)
	n, _ := strconv.Atoi(line1_arr[0])
	m, _ := strconv.Atoi(line1_arr[1])
	// fmt.Println("T:", T)
	arr := make([][]int, n)
	//used := make([][]int, n)
	for i := 0; i < n; i++ {
		arr[i] = make([]int, m)
		//used[i] = make([]int, m)
		line2, _ := reader.ReadString('\n')
		line2_arr := strings.Fields(line2)
		for j := 0; j < m; j++ {
			arr[i][j], _ = strconv.Atoi(line2_arr[j])
		}
	}
	//fmt.Println("arr:", arr)

	var q []st
	mp := make(map[st]bool)
	//painted := make(map[st]bool)
	//q = append(q, st{1,2,3})
	// find first level 'all 4' of same color and add to queue
	for i := 0; i < n-1; i++ {
		for j := 0; j < m-1; j++ {
			c := all_four_same(st{i, j, 0}, arr)
			if c > 0 {
				q = append(q, st{i, j, c})
				key := st{i, j, -1}
				mp[key] = true
				add_cell_to_used_arr(arr, i, j)
			}
		}
	}

	//fmt.Println("q:", q)

	qlen := len(q)
	for qind := 0; qind < qlen; qind++ {
		// find max of 8 adjacent matches which have remaining cells of same color
		cell := q[qind]
		//fmt.Println("ranging for cell:", cell)
		i := cell.I - 1
		if i < 0 {
			i = 0
		}
		for ; i < n-1 && i <= cell.I+1; i++ {
			j := cell.J - 1
			if j < 0 {
				j = 0
			}
			for ; j < m-1 && j <= cell.J+1; j++ {
				if i == cell.I && j == cell.J {
					continue
				}

				if mp[st{i, j, arr[i][j]}] {
					continue
				}

				c := all_four_same_or_special(i, j, arr)
				if c > 0 {
					q = append(q, st{i, j, c})
					key := st{i, j, -1}
					mp[key] = true
					qlen = len(q)
					add_cell_to_used_arr(arr, i, j)
				}
			}
		}
	}

	total_painted := 0
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if arr[i][j] == -1 {
				total_painted++
			}
		}
	}
	// fmt.Println("q:", q)
	// fmt.Println("used:", used)
	// fmt.Println("total_painted:", total_painted)

	if total_painted < n*m {
		fmt.Println(-1)
	} else {
		fmt.Println(qlen)
		var buffer bytes.Buffer
		for r := qlen - 1; r >= 0; r-- {
			s := q[r]
			//fmt.Println(s.I+1, s.J+1, s.C)
			buffer.WriteString(strconv.Itoa(s.I+1) + " " + strconv.Itoa(s.J+1) + " " + strconv.Itoa(s.C) + "\n")
		}
		fmt.Println(buffer.String())
	}
}

func all_four_same(pos st, arr [][]int) int {
	i := pos.I
	j := pos.J
	x := arr[i][j]
	if x == arr[i+1][j] && x == arr[i][j+1] && x == arr[i+1][j+1] {
		return x
	} else {
		return -1
	}
}

// special cell will have -1
func all_four_same_or_special(i, j int, arr [][]int) int {
	colors := make(map[int]bool)
	c := -1
	for x := i; x <= i+1; x++ {
		for y := j; y <= j+1; y++ {
			if arr[x][y] != -1 {
				c = arr[x][y]
				colors[c] = true
			}
		}
	}

	if len(colors) == 1 {
		return c
	} else {
		return -1
	}
}

func add_cell_to_used_arr(used [][]int, i, j int) {
	used[i][j] = -1
	used[i][j+1] = -1
	used[i+1][j] = -1
	used[i+1][j+1] = -1
}

/*
func add_cell_to_painted_map(painted map[st]bool, s st) {
	painted[s] = true
	painted[st{s.I, s.J + 1, 0}] = true
	painted[st{s.I + 1, s.J, 0}] = true
	painted[st{s.I + 1, s.J + 1, 0}] = true
}
*/

/*
https://codeforces.com/contest/1638/problem/D
*/

package main

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

type st struct {
	I int
	J int
	C int
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	line1, _ := reader.ReadString('\n')
	line1_arr := strings.Fields(line1)
	//fmt.Println(line1_arr)
	n, _ := strconv.Atoi(line1_arr[0])
	m, _ := strconv.Atoi(line1_arr[1])
	// fmt.Println("T:", T)
	arr := make([][]int, n)
	//used := make([][]int, n)
	for i := 0; i < n; i++ {
		arr[i] = make([]int, m)
		//used[i] = make([]int, m)
		line2, _ := reader.ReadString('\n')
		line2_arr := strings.Fields(line2)
		for j := 0; j < m; j++ {
			arr[i][j], _ = strconv.Atoi(line2_arr[j])
		}
	}
	//fmt.Println("arr:", arr)

	var q []st
	mp := make(map[st]bool)
	//painted := make(map[st]bool)
	//q = append(q, st{1,2,3})
	// find first level 'all 4' of same color and add to queue
	for i := 0; i < n-1; i++ {
		for j := 0; j < m-1; j++ {
			c := all_four_same(st{i, j, 0}, arr)
			if c > 0 {
				q = append(q, st{i, j, -1})
				key := st{i, j, -1}
				mp[key] = true
				add_cell_to_used_arr(arr, i, j)
			}
		}
	}

	//fmt.Println("q:", q)

	qlen := len(q)
	for qind := 0; qind < qlen; qind++ {
		// find max of 8 adjacent matches which have remaining cells of same color
		cell := q[qind]
		//fmt.Println("ranging for cell:", cell)
		i := cell.I - 1
		if i < 0 {
			i = 0
		}
		for ; i < n-1 && i <= cell.I+1; i++ {
			j := cell.J - 1
			if j < 0 {
				j = 0
			}
			for ; j < m-1 && j <= cell.J+1; j++ {
				if i == cell.I && j == cell.J {
					continue
				}

				if mp[st{i, j, arr[i][j]}] {
					continue
				}

				if all_four_same_or_special(i, j, arr) {
					q = append(q, st{i, j, -1})
					key := st{i, j, -1}
					mp[key] = true
					qlen = len(q)
					add_cell_to_used_arr(arr, i, j)
				}
			}
		}
	}

	total_painted := 0
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if arr[i][j] == -1 {
				total_painted++
			}
		}
	}
	// fmt.Println("q:", q)
	// fmt.Println("used:", used)
	// fmt.Println("total_painted:", total_painted)

	if total_painted < n*m {
		fmt.Println(-1)
	} else {
		fmt.Println(qlen)
		for r := qlen - 1; r >= 0; r-- {
			s := q[r]
			fmt.Println(s.I+1, s.J+1)
		}
	}
}

func all_four_same(pos st, arr [][]int) int {
	i := pos.I
	j := pos.J
	x := arr[i][j]
	if x == arr[i+1][j] && x == arr[i][j+1] && x == arr[i+1][j+1] {
		return x
	} else {
		return -1
	}
}

// special cell will have -1
func all_four_same_or_special(i, j int, arr [][]int) bool {
	colors := make(map[int]bool)
	for x := i; x <= i+1; x++ {
		for y := j; y <= j+1; y++ {
			if arr[x][y] != -1 {
				colors[arr[x][y]] = true
			}
		}
	}

	if len(colors) > 1 {
		return false
	} else {
		return true
	}
}

func add_cell_to_used_arr(used [][]int, i, j int) {
	used[i][j] = -1
	used[i][j+1] = -1
	used[i+1][j] = -1
	used[i+1][j+1] = -1
}

/*
func add_cell_to_painted_map(painted map[st]bool, s st) {
	painted[s] = true
	painted[st{s.I, s.J + 1, 0}] = true
	painted[st{s.I + 1, s.J, 0}] = true
	painted[st{s.I + 1, s.J + 1, 0}] = true
}
*/

/*
https://codeforces.com/contest/1638/problem/D
*/

package main

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

type st struct {
	I int
	J int
	C int
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	line1, _ := reader.ReadString('\n')
	line1_arr := strings.Fields(line1)
	//fmt.Println(line1_arr)
	n, _ := strconv.Atoi(line1_arr[0])
	m, _ := strconv.Atoi(line1_arr[1])
	// fmt.Println("T:", T)
	arr := make([][]int, n)
	used := make([][]int, n)
	for i := 0; i < n; i++ {
		arr[i] = make([]int, m)
		used[i] = make([]int, m)
		line2, _ := reader.ReadString('\n')
		line2_arr := strings.Fields(line2)
		for j := 0; j < m; j++ {
			arr[i][j], _ = strconv.Atoi(line2_arr[j])
		}
	}
	//fmt.Println("arr:", arr)

	var q []st
	mp := make(map[st]bool)
	//painted := make(map[st]bool)
	//q = append(q, st{1,2,3})
	// find first level 'all 4' of same color and add to queue
	for i := 0; i < n-1; i++ {
		for j := 0; j < m-1; j++ {
			c := all_four_same(st{i, j, 0}, arr)
			if c > 0 {
				q = append(q, st{i, j, c})
				key := st{i, j, c}
				mp[key] = true
				add_cell_to_used_arr(used, i, j)
			}
		}
	}

	//fmt.Println("q:", q)

	qlen := len(q)
	for qind := 0; qind < qlen; qind++ {
		// find max of 8 adjacent matches which have remaining cells of same color
		cell := q[qind]
		//fmt.Println("ranging for cell:", cell)
		i := cell.I - 1
		if i < 0 {
			i = 0
		}
		for ; i < n-1 && i <= cell.I+1; i++ {
			j := cell.J - 1
			if j < 0 {
				j = 0
			}
			for ; j < m-1 && j <= cell.J+1; j++ {
				if i == cell.I && j == cell.J {
					continue
				}

				if mp[st{i,j, arr[i][j]}] {
					continue
				}

				v := -1
				all_same := true
				non_sp := 0
				for x := i; x <= i+1; x++ {
					for y := j; y <= j+1; y++ {
						if (x == cell.I || x == cell.I+1) && (y == cell.J || y == cell.J+1) {
							continue
						}
						non_sp++
						if v == -1 {
							v = arr[x][y]
							continue
						}

						if v != arr[x][y] {
							all_same = false
						}
					}
				}

				if all_same {
					q = append(q, st{i, j, v})
					key := st{i, j, v}
					mp[key] = true
					qlen = len(q)
					add_cell_to_used_arr(used, i, j)
				}
			}
		}
	}

	total_painted := 0
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if used[i][j] == -1 {
				total_painted++
			}
		}
	}
	// fmt.Println("q:", q)
	// fmt.Println("used:", used)
	// fmt.Println("total_painted:", total_painted)

	if total_painted < n*m {
		fmt.Println(-1)
	} else {
		fmt.Println(qlen)
		for r := qlen - 1; r >= 0; r-- {
			s := q[r]
			fmt.Println(s.I+1, s.J+1, s.C)
		}
	}
}

func all_four_same(pos st, arr [][]int) int {
	i := pos.I
	j := pos.J
	x := arr[i][j]
	if x == arr[i+1][j] && x == arr[i][j+1] && x == arr[i+1][j+1] {
		return x
	} else {
		return -1
	}
}

func add_cell_to_used_arr(used [][]int, i, j int) {
	used[i][j] = -1
	used[i][j+1] = -1
	used[i+1][j] = -1
	used[i+1][j+1] = -1
}

/*
func add_cell_to_painted_map(painted map[st]bool, s st) {
	painted[s] = true
	painted[st{s.I, s.J + 1, 0}] = true
	painted[st{s.I + 1, s.J, 0}] = true
	painted[st{s.I + 1, s.J + 1, 0}] = true
}
*/

/*
https://codeforces.com/contest/1638/problem/D
*/

package main

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

type st struct {
	I int
	J int
	C int
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	line1, _ := reader.ReadString('\n')
	line1_arr := strings.Fields(line1)
	//fmt.Println(line1_arr)
	n, _ := strconv.Atoi(line1_arr[0])
	m, _ := strconv.Atoi(line1_arr[1])
	// fmt.Println("T:", T)
	arr := make([][]int, n)
	used := make([][]int, n)
	for i := 0; i < n; i++ {
		arr[i] = make([]int, m)
		used[i] = make([]int, m)
		line2, _ := reader.ReadString('\n')
		line2_arr := strings.Fields(line2)
		for j := 0; j < m; j++ {
			arr[i][j], _ = strconv.Atoi(line2_arr[j])
		}
	}
	//fmt.Println("arr:", arr)

	var q []st
	mp := make(map[st]bool)
	//painted := make(map[st]bool)
	//q = append(q, st{1,2,3})
	// find first level 'all 4' of same color and add to queue
	for i := 0; i < n-1; i++ {
		for j := 0; j < m-1; j++ {
			c := all_four_same(st{i, j, 0}, arr)
			if c > 0 {
				q = append(q, st{i, j, c})
				key := st{i, j, 0}
				mp[key] = true
				add_cell_to_used_arr(used, i, j)
			}
		}
	}

	//fmt.Println("q:", q)

	qlen := len(q)
	for qind := 0; qind < qlen; qind++ {
		// find max of 8 adjacent matches which have remaining cells of same color
		cell := q[qind]
		//fmt.Println("ranging for cell:", cell)
		i := cell.I - 1
		if i < 0 {
			i = 0
		}
		for ; i < n-1 && i <= cell.I+1; i++ {
			j := cell.J - 1
			if j < 0 {
				j = 0
			}
			for ; j < m-1 && j <= cell.J+1; j++ {
				if i == cell.I && j == cell.J {
					continue
				}

				if mp[st{i,j, 0}] {
					continue
				}

				v := -1
				all_same := true
				non_sp := 0
				for x := i; x <= i+1; x++ {
					for y := j; y <= j+1; y++ {
						if (x == cell.I || x == cell.I+1) && (y == cell.J || y == cell.J+1) {
							continue
						}
						non_sp++
						if v == -1 {
							v = arr[x][y]
							continue
						}

						if v != arr[x][y] {
							all_same = false
						}
					}
				}

				if all_same {
					q = append(q, st{i, j, v})
					key := st{i, j, 0}
					mp[key] = true
					qlen = len(q)
					add_cell_to_used_arr(used, i, j)
				}
			}
		}
	}

	total_painted := 0
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if used[i][j] == -1 {
				total_painted++
			}
		}
	}
	// fmt.Println("q:", q)
	// fmt.Println("used:", used)
	// fmt.Println("total_painted:", total_painted)

	if total_painted < n*m {
		fmt.Println(-1)
	} else {
		fmt.Println(qlen)
		for r := qlen - 1; r >= 0; r-- {
			s := q[r]
			fmt.Println(s.I+1, s.J+1, s.C)
		}
	}
}

func all_four_same(pos st, arr [][]int) int {
	i := pos.I
	j := pos.J
	x := arr[i][j]
	if x == arr[i+1][j] && x == arr[i][j+1] && x == arr[i+1][j+1] {
		return x
	} else {
		return -1
	}
}

func add_cell_to_used_arr(used [][]int, i, j int) {
	used[i][j] = -1
	used[i][j+1] = -1
	used[i+1][j] = -1
	used[i+1][j+1] = -1
}

/*
func add_cell_to_painted_map(painted map[st]bool, s st) {
	painted[s] = true
	painted[st{s.I, s.J + 1, 0}] = true
	painted[st{s.I + 1, s.J, 0}] = true
	painted[st{s.I + 1, s.J + 1, 0}] = true
}
*/

/*
https://codeforces.com/contest/1638/problem/D
*/

package main

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

type st struct {
	I int
	J int
	C int
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	line1, _ := reader.ReadString('\n')
	line1_arr := strings.Fields(line1)
	//fmt.Println(line1_arr)
	n, _ := strconv.Atoi(line1_arr[0])
	m, _ := strconv.Atoi(line1_arr[1])
	// fmt.Println("T:", T)
	arr := make([][]int, n)
	for i := 0; i < n; i++ {
		arr[i] = make([]int, m)
		line2, _ := reader.ReadString('\n')
		line2_arr := strings.Fields(line2)
		for j := 0; j < m; j++ {
			arr[i][j], _ = strconv.Atoi(line2_arr[j])
		}
	}
	//fmt.Println("arr:", arr)

	var q []st
	mp := make(map[st]bool)
	painted := make(map[st]bool)
	//q = append(q, st{1,2,3})
	// find first level 'all 4' of same color and add to queue
	for i := 0; i < n-1; i++ {
		for j := 0; j < m-1; j++ {
			c := all_four_same(st{i, j, 0}, arr)
			if c > 0 {
				q = append(q, st{i, j, c})
				key := st{i, j, 0}
				mp[key] = true
				add_cell_to_painted_map(painted, key)
			}
		}
	}

	//fmt.Println("q:", q)

	qlen := len(q)
	for qind := 0; qind < qlen; qind++ {
		// find max of 8 adjacent matches which have remaining cells of same color
		cell := q[qind]
		//fmt.Println("ranging for cell:", cell)
		i := cell.I - 1
		if i < 0 {
			i = 0
		}
		for ; i < n-1 && i <= cell.I+1; i++ {
			j := cell.J - 1
			if j < 0 {
				j = 0
			}
			for ; j < m-1 && j <= cell.J+1; j++ {
				if i == cell.I && j == cell.J {
					continue
				}

				if mp[st{i, j, 0}] {
					continue
				}
				v := -1
				all_same := true
				non_sp := 0
				for x := i; x <= i+1; x++ {
					for y := j; y <= j+1; y++ {
						if (x == cell.I || x == cell.I+1) && (y == cell.J || y == cell.J+1) {
							continue
						}
						non_sp++
						if v == -1 {
							v = arr[x][y]
							continue
						}

						if v != arr[x][y] {
							all_same = false
						}
					}
				}

				if all_same {
					q = append(q, st{i, j, v})
					key := st{i, j, 0}
					mp[key] = true
					qlen = len(q)
					add_cell_to_painted_map(painted, key)
				}
			}
		}
	}
	//fmt.Println("q:", q)
	//fmt.Println("total_painted:", total_painted)

	if len(painted) < n*m {
		fmt.Println(-1)
	} else {
		fmt.Println(qlen)
		for r := qlen - 1; r >= 0; r-- {
			s := q[r]
			fmt.Println(s.I+1, s.J+1, s.C)
		}
	}
}

func all_four_same(pos st, arr [][]int) int {
	i := pos.I
	j := pos.J
	x := arr[i][j]
	if x == arr[i+1][j] && x == arr[i][j+1] && x == arr[i+1][j+1] {
		return x
	} else {
		return -1
	}
}

func add_cell_to_painted_map(painted map[st]bool, s st) {
	painted[s] = true
	painted[st{s.I, s.J + 1, 0}] = true
	painted[st{s.I + 1, s.J, 0}] = true
	painted[st{s.I + 1, s.J + 1, 0}] = true
}

/*
https://codeforces.com/contest/1638/problem/D
*/

package main

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

type st struct {
	I int
	J int
	C int
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	line1, _ := reader.ReadString('\n')
	line1_arr := strings.Fields(line1)
	//fmt.Println(line1_arr)
	n, _ := strconv.Atoi(line1_arr[0])
	m, _ := strconv.Atoi(line1_arr[1])
	// fmt.Println("T:", T)
	arr := make([][]int, n)
	for i := 0; i < n; i++ {
		arr[i] = make([]int, m)
		line2, _ := reader.ReadString('\n')
		line2_arr := strings.Fields(line2)
		for j := 0; j < m; j++ {
			arr[i][j], _ = strconv.Atoi(line2_arr[j])
		}
	}
	//fmt.Println("arr:", arr)

	total_painted := 0
	var q []st
	mp := make(map[st]bool)
	//q = append(q, st{1,2,3})
	// find first level 'all 4' of same color and add to queue
	for i := 0; i < n-1; i++ {
		for j := 0; j < m-1; j++ {
			c := all_four_same(st{i, j, 0}, arr)
			if c > 0 {
				q = append(q, st{i, j, c})
				mp[st{i, j, 0}] = true
				total_painted += 4
			}
		}
	}

	//fmt.Println("q:", q)

	qlen := len(q)
	for qind := 0; qind < qlen; qind++ {
		// find max of 8 adjacent matches which have remaining cells of same color
		cell := q[qind]
		//fmt.Println("ranging for cell:", cell)
		i := cell.I - 1
		if i < 0 {
			i = 0
		}
		for ; i < n-1 && i <= cell.I+1; i++ {
			j := cell.J - 1
			if j < 0 {
				j = 0
			}
			for ; j < m-1 && j <= cell.J+1; j++ {
				if i == cell.I && j == cell.J {
					continue
				}

				if mp[st{i, j, 0}] {
					continue
				}
				v := -1
				all_same := true
				non_sp := 0
				for x := i; x <= i+1; x++ {
					for y := j; y <= j+1; y++ {
						if (x == cell.I || x == cell.I+1) && (y == cell.J || y == cell.J+1) {
							continue
						}
						non_sp++
						if v == -1 {
							v = arr[x][y]
							continue
						}

						if v != arr[x][y] {
							all_same = false
						}
					}
				}

				if all_same {
					q = append(q, st{i, j, v})
					mp[st{i, j, 0}] = true
					qlen = len(q)
					total_painted += non_sp
				}
				if total_painted >= n*m {
					break
				}
			}
			if total_painted >= n*m {
				break
			}
		}
		if total_painted >= n*m {
			break
		}
	}
	//fmt.Println("q:", q)
	//fmt.Println("total_painted:", total_painted)

	if total_painted < n*m {
		fmt.Println(-1)
	} else {
		fmt.Println(qlen)
		for r := qlen - 1; r >= 0; r-- {
			s := q[r]
			fmt.Println(s.I+1, s.J+1, s.C)
		}
	}
}

func all_four_same(pos st, arr [][]int) int {
	i := pos.I
	j := pos.J
	x := arr[i][j]
	if x == arr[i+1][j] && x == arr[i][j+1] && x == arr[i+1][j+1] {
		return x
	} else {
		return -1
	}
}


You are given a sequence of n integers a1, a2, ..., an. Determine a real number x such that the weakness of the sequence a1 - x, a2 - x, ..., an - x is as small as possible.The weakness of a sequence is defined as the maximum value of the poorness over all segments (contiguous subsequences) of a sequence.The poorness of a segment is defined as the absolute value of sum of the elements of segment.

The first line contains one integer n (1 ≤ n ≤ 200 000), the length of a sequence. The second line contains n integers a1, a2, ..., an (|ai| ≤ 10 000).

Output a real number denoting the minimum possible weakness of a1 - x, a2 - x, ..., an - x. Your answer will be considered correct if its relative or absolute error doesn't exceed 10 - 6.

NoteFor the first case, the optimal value of x is 2 so the sequence becomes  - 1, 0, 1 and the max poorness occurs at the segment "-1" or segment "1". The poorness value (answer) equals to 1 in this case. For the second sample the optimal value of x is 2.5 so the sequence becomes  - 1.5,  - 0.5, 0.5, 1.5 and the max poorness occurs on segment "-1.5 -0.5" or "0.5 1.5". The poorness value (answer) equals to 2 in this case.

package main

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

// github.com/EndlessCheng/codeforces-go
func Sol578C(in io.Reader, out io.Writer) {
	maxSubArrayAbsSum := func(a []float64) float64 {
		min, max, abs := math.Min, math.Max, math.Abs
		curMaxSum, maxSum := a[0], a[0]
		curMinSum, minSum := a[0], a[0]
		for _, v := range a[1:] {
			curMaxSum = max(curMaxSum+v, v)
			maxSum = max(maxSum, curMaxSum)
			curMinSum = min(curMinSum+v, v)
			minSum = min(minSum, curMinSum)
		}
		return max(abs(maxSum), abs(minSum))
	}
	ternarySearch := func(l, r float64, f func(x float64) float64) float64 {
		step := int(math.Log((r-l)/1e-12) / math.Log(1.5))
		for i := 0; i < step; i++ {
			m1 := l + (r-l)/3
			m2 := r - (r-l)/3
			v1, v2 := f(m1), f(m2)
			if v1 < v2 {
				r = m2
			} else {
				l = m1
			}
		}
		return (l + r) / 2
	}

	in = bufio.NewReader(in)
	var n int
	Fscan(in, &n)
	a := make([]float64, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	f := func(x float64) float64 {
		b := make([]float64, n)
		for i, v := range a {
			b[i] = v - x
		}
		return maxSubArrayAbsSum(b)
	}
	x := ternarySearch(-1e4, 1e4, f)
	Fprintf(out, "%.15f", f(x))
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func Sol578C(in io.Reader, out io.Writer) {
	maxSubArrayAbsSum := func(a []float64) float64 {
		min, max, abs := math.Min, math.Max, math.Abs
		curMaxSum, maxSum := a[0], a[0]
		curMinSum, minSum := a[0], a[0]
		for _, v := range a[1:] {
			curMaxSum = max(curMaxSum+v, v)
			maxSum = max(maxSum, curMaxSum)
			curMinSum = min(curMinSum+v, v)
			minSum = min(minSum, curMinSum)
		}
		return max(abs(maxSum), abs(minSum))
	}
	ternarySearch := func(l, r float64, f func(x float64) float64) float64 {
		step := int(math.Log((r-l)/1e-7) / math.Log(1.5))
		for i := 0; i < step; i++ {
			m1 := l + (r-l)/3
			m2 := r - (r-l)/3
			v1, v2 := f(m1), f(m2)
			if v1 == v2 {
				break
			}
			if v1 < v2 {
				r = m2
			} else {
				l = m1
			}
		}
		return (l + r) / 2
	}

	in = bufio.NewReader(in)
	var n int
	Fscan(in, &n)
	a := make([]float64, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	f := func(x float64) float64 {
		b := make([]float64, n)
		for i, v := range a {
			b[i] = v - x
		}
		return maxSubArrayAbsSum(b)
	}
	x := ternarySearch(-1e4, 1e4, f)
	Fprintf(out, "%.15f", f(x))
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func Sol578C(in io.Reader, out io.Writer) {
	maxSubArrayAbsSum := func(a []float64) float64 {
		min, max, abs := math.Min, math.Max, math.Abs
		curMaxSum, maxSum := a[0], a[0]
		curMinSum, minSum := a[0], a[0]
		for _, v := range a[1:] {
			curMaxSum = max(curMaxSum+v, v)
			maxSum = max(maxSum, curMaxSum)
			curMinSum = min(curMinSum+v, v)
			minSum = min(minSum, curMinSum)
		}
		return max(abs(maxSum), abs(minSum))
	}
	ternarySearch := func(l, r float64, f func(x float64) float64) float64 {
		step := int(math.Log((r-l)/1e-9) / math.Log(1.5))
		for i := 0; i < step; i++ {
			m1 := l + (r-l)/3
			m2 := r - (r-l)/3
			v1, v2 := f(m1), f(m2)
			if v1 < v2 {
				r = m2
			} else {
				l = m1
			}
		}
		return (l + r) / 2
	}

	in = bufio.NewReader(in)
	var n int
	Fscan(in, &n)
	a := make([]float64, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	f := func(x float64) float64 {
		b := make([]float64, n)
		for i, v := range a {
			b[i] = v - x
		}
		return maxSubArrayAbsSum(b)
	}
	x := ternarySearch(-1e4, 1e4, f)
	Fprintf(out, "%.15f", f(x))
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func Sol578C(in io.Reader, out io.Writer) {
	maxSubArrayAbsSum := func(a []float64) float64 {
		min, max, abs := math.Min, math.Max, math.Abs
		curMaxSum, maxSum := a[0], a[0]
		curMinSum, minSum := a[0], a[0]
		for _, v := range a[1:] {
			curMaxSum = max(curMaxSum+v, v)
			maxSum = max(maxSum, curMaxSum)
			curMinSum = min(curMinSum+v, v)
			minSum = min(minSum, curMinSum)
		}
		return max(abs(maxSum), abs(minSum))
	}
	ternarySearch := func(l, r float64, f func(x float64) float64) float64 {
		step := int(math.Log((r-l)/1e-7) / math.Log(1.5))
		for i := 0; i < step; i++ {
			m1 := l + (r-l)/3
			m2 := r - (r-l)/3
			v1, v2 := f(m1), f(m2)
			if v1 < v2 {
				r = m2
			} else {
				l = m1
			}
		}
		return (l + r) / 2
	}

	in = bufio.NewReader(in)
	var n int
	Fscan(in, &n)
	a := make([]float64, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	f := func(x float64) float64 {
		b := make([]float64, n)
		for i, v := range a {
			b[i] = v - x
		}
		return maxSubArrayAbsSum(b)
	}
	x := ternarySearch(-1e4, 1e4, f)
	Fprintf(out, "%.15f", f(x))
}

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


After returned from forest, Alyona started reading a book. She noticed strings s and t, lengths of which are n and m respectively. As usual, reading bored Alyona and she decided to pay her attention to strings s and t, which she considered very similar.Alyona has her favourite positive integer k and because she is too small, k does not exceed 10. The girl wants now to choose k disjoint non-empty substrings of string s such that these strings appear as disjoint substrings of string t and in the same order as they do in string s. She is also interested in that their length is maximum possible among all variants.Formally, Alyona wants to find a sequence of k non-empty strings p1, p2, p3, ..., pk satisfying following conditions:  s can be represented as concatenation a1p1a2p2... akpkak + 1, where a1, a2, ..., ak + 1 is a sequence of arbitrary strings (some of them may be possibly empty);  t can be represented as concatenation b1p1b2p2... bkpkbk + 1, where b1, b2, ..., bk + 1 is a sequence of arbitrary strings (some of them may be possibly empty);  sum of the lengths of strings in sequence is maximum possible. Please help Alyona solve this complicated problem and find at least the sum of the lengths of the strings in a desired sequence.A substring of a string is a subsequence of consecutive characters of the string.

In the first line of the input three integers n, m, k (1 ≤ n, m ≤ 1000, 1 ≤ k ≤ 10) are given — the length of the string s, the length of the string t and Alyona's favourite number respectively. The second line of the input contains string s, consisting of lowercase English letters. The third line of the input contains string t, consisting of lowercase English letters.

In the only line print the only non-negative integer — the sum of the lengths of the strings in a desired sequence. It is guaranteed, that at least one desired sequence exists.

NoteThe following image describes the answer for the second sample case:

package main

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

func max(a, b int) int {
	if a > b{
		return a
	}
	return b
}

var dp [1001][1001][11][2] int
func main() {
	reader := bufio.NewReaderSize(os.Stdin, 1005)
	bytes, _, _ := reader.ReadLine()
	strs := strings.Split(string(bytes), " ")
	n, _ := strconv.Atoi(strs[0])
	m, _ := strconv.Atoi(strs[1])
	k, _ := strconv.Atoi(strs[2])
	bytes, _, _ = reader.ReadLine()
	str1 := string(bytes)
	bytes, _, _ = reader.ReadLine()
	str2 := string(bytes)
	for i:=1; i<= n; i++ {
		for j:=1; j<= m; j++ {
			for p := 1; p <= k; p++ {
				dp[i][j][p][0] = max(max(dp[i-1][j][p][0], dp[i-1][j][p][1]), max(dp[i][j-1][p][0], dp[i][j-1][p][1]))
				if str1[i-1] != str2[j-1] {
					dp[i][j][p][1] = -1
				} else {
					dp[i][j][p][1] = max(dp[i][j][p][1], max(dp[i-1][j-1][p-1][0], dp[i-1][j-1][p-1][1]) + 1)
					dp[i][j][p][1] = max(dp[i][j][p][1], dp[i-1][j-1][p][1] + 1)
				}
			}
		}
	}
	fmt.Println(max(dp[n][m][k][0], dp[n][m][k][1]))
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF682D(in io.Reader, out io.Writer) {
	max := func(a, b int) int {
		if a > b {
			return a
		}
		return b
	}
	var n, m, K int
	var s, t []byte
	Fscan(bufio.NewReader(in), &n, &m, &K, &s, &t)
	dp := make([][][][2]int, n)
	for i := range dp {
		dp[i] = make([][][2]int, m)
		for j := range dp[i] {
			dp[i][j] = make([][2]int, K+1)
			for k := range dp[i][j] {
				dp[i][j][k] = [2]int{-1, -1}
			}
		}
	}
	var f func(i, j, k, prevSame int) int
	f = func(i, j, k, prevSame int) (res int) {
		if k > K {
			return -1e9
		}
		if i == n || j == m {
			return
		}
		dv := &dp[i][j][k][prevSame]
		if *dv != -1 {
			return *dv
		}
		defer func() { *dv = res }()
		res = max(f(i+1, j, k, 0), f(i, j+1, k, 0))
		if s[i] == t[j] {
			res = max(res, 1+f(i+1, j+1, k+1-prevSame, 1))
		}
		return
	}
	Fprint(out, f(0, 0, 0, 0))
}

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

package main

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

const N = 1002

var T [10][N][N]int16
var LM [N][N]int16
var s, t string
var n, m int16

// from i && j, k strings left
func f(i, j, k int16) int16 {
	if k == 0 || i == n || j == m {
		return 0
	}

	x := T[k-1][i][j]
	if x != 0 {
		return max16(0, x)
	}

	lmij := lm(i, j)
	x = max16(
		max16(
			f(i+1, j+1, k),
			lmij+f(i+lmij, j+lmij, k-1),
		),
		max16(
			f(i, j+1, k),
			f(i+1, j, k),
		))

	T[k-1][i][j] = x
	if x == 0 {
		T[k-1][i][j] = -1
	}
	return x
}

func max16(a int16, b int16) int16 {
	if b > a {
		a = b
	}
	return a
}

func lm(i, j int16) int16 {
	if i >= n || j >= m {
		return 0
	}
	x := LM[i][j]
	if x != 0 {
		return max16(0, x)
	}
	if s[i] == t[j] {
		x = 1 + lm(i+1, j+1)
		LM[i][j] = x
	} else {
		LM[i][j] = -1
		x = 0
	}
	return x
}

func main() {
	n = int16(getInt())
	m = int16(getInt())
	k := int16(getInt())
	s = getWord()
	t = getWord()
	if n > m {
		n, m = m, n
		s, t = t, s
	}
	// s is shorter

	fmt.Println(f(0, 0, k))
}

type Input struct {
	*bufio.Scanner
}

var globalInput = wrapStdin()

func wrapStdin() Input {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	return Input{Scanner: sc}
}
func getInt() int {
	globalInput.Scan()
	x, _ := strconv.Atoi(globalInput.Text())
	return x
}
func getIntOk() (int, bool) {
	ok := globalInput.Scan()
	x, _ := strconv.Atoi(globalInput.Text())
	return x, ok
}
func getNint(n int) []int {
	v := make([]int, n)
	for i := 0; i < n; i++ {
		v[i] = getInt()
	}
	return v
}
func getVint(v []int) bool {
	var ok bool
	for i := 0; i < len(v); i++ {
		v[i], ok = getIntOk()
	}
	return ok
}
func getWord() string {
	globalInput.Scan()
	return globalInput.Text()
}
func getWordOk() (string, bool) {
	ok := globalInput.Scan()
	return globalInput.Text(), ok
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}
func min64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}
func max64(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}
func minStr(a, b string) string {
	if a < b {
		return a
	}
	return b
}
func maxStr(a, b string) string {
	if a < b {
		return b
	}
	return a
}
func remin(a *int, b int) bool {
	if b < *a {
		*a = b
		return true
	}
	return false
}
func remax(a *int, b int) bool {
	if b > *a {
		*a = b
		return true
	}
	return false
}
func remin64(a *int64, b int64) bool {
	if b < *a {
		*a = b
		return true
	}
	return false
}
func remax64(a *int64, b int64) bool {
	if b > *a {
		*a = b
		return true
	}
	return false
}
func reminStr(a *string, b string) bool {
	if b < *a {
		*a = b
		return true
	}
	return false
}
func remaxStr(a *string, b string) bool {
	if b > *a {
		*a = b
		return true
	}
	return false
}

func abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}
func abs64(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}

type (
	FLess    func(i, j int) bool
	FSwap    func(i, j int)
	wrapSort struct {
		Length int
		FLess
		FSwap
	}
)

func (w *wrapSort) Len() int           { return w.Length }
func (w *wrapSort) Less(i, j int) bool { return w.FLess(i, j) }
func (w *wrapSort) Swap(i, j int)      { w.FSwap(i, j) }
func wsort(length int, fless func(i, j int) bool, fswap func(i, j int)) {
	sort.Sort(&wrapSort{length, fless, fswap})
}

package main

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

const N = 1002

var T [10][N][N]int16
var LM [N][N]int16
var s, t string
var n, m int16

// from i && j, k strings left
func f(i, j, k int16) int16 {
	if k == 0 || i == n || j == m {
		return 0
	}

	x := T[k-1][i][j] - 1
	if x >= 0 {
		return x
	}

	lmij := lm(i, j)
	x = max16(
		max16(
			f(i+1, j+1, k),
			lmij+f(i+lmij, j+lmij, k-1),
		),
		max16(
			f(i, j+1, k),
			f(i+1, j, k),
		))

	T[k-1][i][j] = x + 1
	return x
}

func max16(a int16, b int16) int16 {
	if b > a {
		a = b
	}
	return a
}

func lm(i, j int16) int16 {
	if i >= n || j >= m {
		return 0
	}
	x := LM[i][j] - 1
	if x >= 0 {
		return x
	}
	if s[i] == t[j] {
		x = 1 + lm(i+1, j+1)
	} else {
		x = 0
	}
	LM[i][j] = x + 1
	return x
}

func main() {
	n = int16(getInt())
	m = int16(getInt())
	k := int16(getInt())
	s = getWord()
	t = getWord()
	if n > m {
		n, m = m, n
		s, t = t, s
	}
	// s is shorter

	fmt.Println(f(0, 0, k))
}

type Input struct {
	*bufio.Scanner
}

var globalInput = wrapStdin()

func wrapStdin() Input {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	return Input{Scanner: sc}
}
func getInt() int {
	globalInput.Scan()
	x, _ := strconv.Atoi(globalInput.Text())
	return x
}
func getIntOk() (int, bool) {
	ok := globalInput.Scan()
	x, _ := strconv.Atoi(globalInput.Text())
	return x, ok
}
func getNint(n int) []int {
	v := make([]int, n)
	for i := 0; i < n; i++ {
		v[i] = getInt()
	}
	return v
}
func getVint(v []int) bool {
	var ok bool
	for i := 0; i < len(v); i++ {
		v[i], ok = getIntOk()
	}
	return ok
}
func getWord() string {
	globalInput.Scan()
	return globalInput.Text()
}
func getWordOk() (string, bool) {
	ok := globalInput.Scan()
	return globalInput.Text(), ok
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}
func min64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}
func max64(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}
func minStr(a, b string) string {
	if a < b {
		return a
	}
	return b
}
func maxStr(a, b string) string {
	if a < b {
		return b
	}
	return a
}
func remin(a *int, b int) bool {
	if b < *a {
		*a = b
		return true
	}
	return false
}
func remax(a *int, b int) bool {
	if b > *a {
		*a = b
		return true
	}
	return false
}
func remin64(a *int64, b int64) bool {
	if b < *a {
		*a = b
		return true
	}
	return false
}
func remax64(a *int64, b int64) bool {
	if b > *a {
		*a = b
		return true
	}
	return false
}
func reminStr(a *string, b string) bool {
	if b < *a {
		*a = b
		return true
	}
	return false
}
func remaxStr(a *string, b string) bool {
	if b > *a {
		*a = b
		return true
	}
	return false
}

func abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}
func abs64(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}

type (
	FLess    func(i, j int) bool
	FSwap    func(i, j int)
	wrapSort struct {
		Length int
		FLess
		FSwap
	}
)

func (w *wrapSort) Len() int           { return w.Length }
func (w *wrapSort) Less(i, j int) bool { return w.FLess(i, j) }
func (w *wrapSort) Swap(i, j int)      { w.FSwap(i, j) }
func wsort(length int, fless func(i, j int) bool, fswap func(i, j int)) {
	sort.Sort(&wrapSort{length, fless, fswap})
}

package main

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

func max(a, b int) int {
	if a > b{
		return a
	}
	return b
}

var dp [1001][1001][11][2] int
func main() {
	reader := bufio.NewReaderSize(os.Stdin, 1005)
	bytes, _, _ := reader.ReadLine()
	strs := strings.Split(string(bytes), " ")
	n, _ := strconv.Atoi(strs[0])
	m, _ := strconv.Atoi(strs[1])
	k, _ := strconv.Atoi(strs[2])
	str1, _, _ := reader.ReadLine()
	str2, _, _ := reader.ReadLine()
	for i:=1; i<= n; i++ {
		for j:=1; j<= m; j++ {
			for p := 1; p <= k; p++ {
				dp[i][j][p][0] = max(max(dp[i-1][j][p][0], dp[i-1][j][p][1]), max(dp[i][j-1][p][0], dp[i][j-1][p][1]))
				if str1[i-1] != str2[j-1] {
					dp[i][j][p][1] = -1
				} else {
					dp[i][j][p][1] = max(dp[i][j][p][1], max(dp[i-1][j-1][p-1][0], dp[i-1][j-1][p-1][1]) + 1)
					dp[i][j][p][1] = max(dp[i][j][p][1], dp[i-1][j-1][p][1] + 1)
				}
			}
		}
	}
	fmt.Println(max(dp[n][m][k][0], dp[n][m][k][1]))
}

package main

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

func max(a, b int) int {
	if a > b{
		return a
	}
	return b
}

var dp [1001][1001][11][2] int
func main() {
	reader := bufio.NewReaderSize(os.Stdin, 1005)
	bytes, _, _ := reader.ReadLine()
	strs := strings.Split(string(bytes), " ")
	n, _ := strconv.Atoi(strs[0])
	m, _ := strconv.Atoi(strs[1])
	k, _ := strconv.Atoi(strs[2])
	bytes, _, _ = reader.ReadLine()
	str1 := string(bytes)
	bytes, _, _ = reader.ReadLine()
	str2 := string(bytes)
	for i:=1; i<= n; i++ {
		for j:=1; j<= m; j++ {
			for p := 1; p <= k; p++ {
				if str1[i-1] != str2[j-1] {
					dp[i][j][p][0] = max(max(dp[i-1][j][p][0], dp[i-1][j][p][1]), max(dp[i][j-1][p][0], dp[i][j-1][p][1]))
					dp[i][j][p][1] = -1
				} else {
					dp[i][j][p][0] = -1
					dp[i][j][p][1] = max(max(dp[i-1][j][p][0], dp[i-1][j][p][1]), max(dp[i][j-1][p][0], dp[i][j-1][p][1]))
					dp[i][j][p][1] = max(dp[i][j][p][1], max(dp[i-1][j-1][p-1][0], dp[i-1][j-1][p-1][1]) + 1)
					dp[i][j][p][1] = max(dp[i][j][p][1], dp[i-1][j-1][p][1] + 1)
				}
				//fmt.Printf("show %d %d %d\n", i, j, p)
				//fmt.Printf("show %d %d %d\n", i, j, p)
			}
		}
	}
	fmt.Println(max(dp[n][m][k][0], dp[n][m][k][1]))
}

package main

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

func max(a, b int) int {
	if a > b{
		return a
	}
	return b
}

var dp [1001][1001][11][2] int
func main() {
	reader := bufio.NewReaderSize(os.Stdin, 1005)
	bytes, _, _ := reader.ReadLine()
	strs := strings.Split(string(bytes), " ")
	n, _ := strconv.Atoi(strs[0])
	m, _ := strconv.Atoi(strs[1])
	k, _ := strconv.Atoi(strs[2])
	str1, _, _ := reader.ReadLine()
	str2, _, _ := reader.ReadLine()
	for i:=1; i<= n; i++ {
		for j:=1; j<= m; j++ {
			for p := 1; p <= k; p++ {
				dp[i][j][p][0] = max(max(dp[i-1][j][p][0], dp[i-1][j][p][1]), max(dp[i][j-1][p][0], dp[i][j-1][p][1]))
				if str1[i-1] == str2[j-1] {
					dp[i][j][p][1] = max(dp[i][j][p][1], max(dp[i-1][j-1][p-1][0], dp[i-1][j-1][p-1][1]) + 1)
					dp[i][j][p][1] = max(dp[i][j][p][1], dp[i-1][j-1][p][1] + 1)
				}
			}
		}
	}
	fmt.Println(max(dp[n][m][k][0], dp[n][m][k][1]))
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF682D(in io.Reader, out io.Writer) {
	max := func(a, b int) int {
		if a > b {
			return a
		}
		return b
	}
	var n, m, K int
	var s, t []byte
	Fscan(bufio.NewReader(in), &n, &m, &K, &s, &t)
	dp := make([][][]int, n)
	for i := range dp {
		dp[i] = make([][]int, m)
		for j := range dp[i] {
			dp[i][j] = make([]int, K+1)
			for k := range dp[i][j] {
				dp[i][j][k] = -1
			}
		}
	}
	var f func(i, j, k int) int
	f = func(i, j, k int) (res int) {
		if k > K {
			return -1e9
		}
		if i == n || j == m {
			return
		}
		dv := &dp[i][j][k]
		if *dv != -1 {
			return *dv
		}
		defer func() { *dv = res }()
		res = max(f(i+1, j, k), f(i, j+1, k))
		if s[i] == t[j] {
			if i > 0 && j > 0 && s[i-1] != t[j-1] {
				k++
			}
			res = max(res, 1+f(i+1, j+1, k))
		}
		return
	}
	Fprint(out, f(0, 0, 0))
}

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


One day n friends gathered together to play "Mafia". During each round of the game some player must be the supervisor and other n - 1 people take part in the game. For each person we know in how many rounds he wants to be a player, not the supervisor: the i-th person wants to play ai rounds. What is the minimum number of rounds of the "Mafia" game they need to play to let each person play at least as many rounds as they want?

The first line contains integer n (3 ≤ n ≤ 105). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the i-th number in the list is the number of rounds the i-th person wants to play.

In a single line print a single integer — the minimum number of game rounds the friends need to let the i-th person play at least ai rounds. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

NoteYou don't need to know the rules of "Mafia" to solve this problem. If you're curious, it's a game Russia got from the Soviet times: http://en.wikipedia.org/wiki/Mafia_(party_game).

// 348A-mic
package main

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

func main() {
    var n, i, max, sum, x, y float64
    z := bufio.NewReader(os.Stdin)
    fmt.Scan(&n)
    for i = 0; i < n; i++ {
        fmt.Fscan(z, &x)
        if x > max {
            max = x
        }
        sum += x
    }
    y = math.Ceil(sum / (n - 1))
    if y < max {
        fmt.Println(int64(max))
    } else {
        fmt.Println(int64(y))
    }
}

package main

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

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

func main() {
    var n,i,x,m,s int64
    s=0
    fmt.Scan(&n)
    input := bufio.NewScanner(os.Stdin)
    input.Split(bufio.ScanWords)
    for i=1;i<=n;i++ {
    	input.Scan()
    	aa,_:=strconv.ParseInt(input.Text(),0,64)
    	x=aa
    	m=max(m,x)
    	s+=x
    	//fmt.Scan(&arr[i])
    }
    fmt.Println(max(m,(s-1)/(n-1)+1))
}

//348A
package main
import (
	"os"
	"bufio"
	"strconv"
)

func main() {
	var i int
	var x, min int64
	var a[1000000]int64
	r:=bufio.NewScanner(os.Stdin)
	r.Split(bufio.ScanWords)
	w := bufio.NewWriter(os.Stdout)
	r.Scan()
	i, _ = strconv.Atoi(r.Text())
	for j:=0; j<i; j++ {
		r.Scan()
		a[j], _ = strconv.ParseInt(r.Text(), 0, 64)
		if a[j]>min {
			min=a[j]
		}
	}
	for j:=0; j<i; j++ {
		x=x+(min-a[j])
	}

	if x>min {
		w.WriteString(strconv.FormatInt(min, 10)+"\n")
	} else {
		for x=x; x<min; x+=int64(i) {
			min++
		}
		w.WriteString(strconv.FormatInt(min, 10)+"\n")
	}
	w.Flush()
}

package main

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

func main() {
    scanner := bufio.NewScanner(os.Stdin)
    scanner.Split(bufio.ScanWords)
    var n int
    fmt.Scan(&n)
    total, max := int64(0), int64(0)
    for i := 0; i < n; i++ {
        scanner.Scan()
        a, _ := strconv.Atoi(scanner.Text())
        total += int64(a)
        if max < int64(a) { max = int64(a) }
    }
    ans := int64(math.Ceil(float64(total)/float64(n-1)))
    if ans < max { ans = max }
    fmt.Println(ans)
}

// 348A
package main

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

func main() {
	var n, x, c, rem, max, ans int64
	in := bufio.NewReader(os.Stdin)
	fmt.Scan(&n)
	max, rem = 0, 0
	for i := int64(0); i < n; i++ {
		fmt.Fscan(in, &x)
		rem = rem + x
		if max < x {
			max = x
		}
	}
	if rem%(n-1) == 0 {
		c = 0
	} else {
		c = 1
	}
	ans = rem/(n-1) + c
	if max < ans {
		fmt.Println(ans)
	} else {
		fmt.Println(max)
	}
}

package main
import (
	"os"
	"bufio"
	"strconv"
)

func main() {
	var i, x, min int
	var a[100000]int
	r:=bufio.NewScanner(os.Stdin)
	r.Split(bufio.ScanWords)
	w := bufio.NewWriter(os.Stdout)
	r.Scan()
	i, _ = strconv.Atoi(r.Text())
	for j:=0; j<i; j++ {
		r.Scan()
		a[j], _ = strconv.Atoi(r.Text())
		if a[j]>min {
			min=a[j]
		}
	}
	for j:=0; j<i; j++ {
		x=x+(min-a[j])
	}

	if x>min {
		w.WriteString(strconv.Itoa(min)+"\n")
	} else {
		for x=x; x<min; x+=i {
			min++
		}
		w.WriteString(strconv.Itoa(min)+"\n")
	}
	w.Flush()
}

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

func main() {
	var i, x, min int
	var a[100000]int
	r:=bufio.NewScanner(os.Stdin)
	r.Split(bufio.ScanWords)
	r.Scan()
	i, _ = strconv.Atoi(r.Text())
	for j:=0; j<i; j++ {
		r.Scan()
		a[j], _ = strconv.Atoi(r.Text())
		if a[j]>min {
			min=a[j]
		}
	}
	for j:=0; j<i; j++ {
		x=x+(min-a[j])
	}

	if x>min {
		fmt.Println(min)
	} else {
		for x=x; x<min; x+=i {
			min++
		}
		fmt.Println(min)
	}
}

package main

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

func main() {
    scanner := bufio.NewScanner(os.Stdin)
    scanner.Split(bufio.ScanWords)
    var n int
    fmt.Scan(&n)
    total := int64(0)
    for i := 0; i < n; i++ {
        scanner.Scan()
        a, _ := strconv.Atoi(scanner.Text())
        total += int64(a)
    }
    fmt.Println(int64(math.Ceil(float64(total)/float64(n-1))))
}

// 348A-mic_rizal
package main

import (
    "fmt"
    "math"
)

func main() {
    var n, i, max, sum, x, y float64
    fmt.Scan(&n)
    for i = 0; i < n; i++ {
        fmt.Scan(&x)
        if x > max {
            max = x
        }
        sum += x
    }
    y = math.Ceil(sum / (n - 1))
    if y < max {
        fmt.Println(max)
    } else {
        fmt.Println(y)
    }
}


You are given an integer $$$n$$$ ($$$n > 1$$$).Your task is to find a sequence of integers $$$a_1, a_2, \ldots, a_k$$$ such that:  each $$$a_i$$$ is strictly greater than $$$1$$$;  $$$a_1 \cdot a_2 \cdot \ldots \cdot a_k = n$$$ (i. e. the product of this sequence is $$$n$$$);  $$$a_{i + 1}$$$ is divisible by $$$a_i$$$ for each $$$i$$$ from $$$1$$$ to $$$k-1$$$;  $$$k$$$ is the maximum possible (i. e. the length of this sequence is the maximum possible). If there are several such sequences, any of them is acceptable. It can be proven that at least one valid sequence always exists for any integer $$$n > 1$$$.You have to answer $$$t$$$ independent test cases.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$2 \le n \le 10^{10}$$$). It is guaranteed that the sum of $$$n$$$ does not exceed $$$10^{10}$$$ ($$$\sum n \le 10^{10}$$$).

For each test case, print the answer: in the first line, print one positive integer $$$k$$$ — the maximum possible length of $$$a$$$. In the second line, print $$$k$$$ integers $$$a_1, a_2, \ldots, a_k$$$ — the sequence of length $$$k$$$ satisfying the conditions from the problem statement. If there are several answers, you can print any. It can be proven that at least one valid sequence always exists for any integer $$$n > 1$$$.

package main

import (
	"fmt"
	"bufio"
	"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 solve() {
	var n int64
	scanf("%d\n", &n)
	mxdeg := 1
	val := n
	m := n
	for i := int64(2); i * i <= m && n > 1; i++ {
		if n % i == 0 {
			curr := 0
			for ; n % i == 0; curr++ {
				n /= i
			}
			if curr > mxdeg {
				mxdeg = curr
				val = i
			}
		}
	}
	printf("%d\n", mxdeg)
	rest := m
	for i := 0; i < mxdeg - 1; i++ {
		printf("%d ", val)
		rest /= val
	}
	printf("%d\n", rest)
}

func main() {
	defer writer.Flush()
	var t int
	scanf("%d\n", &t)
	for ; t > 0; t-- {
		solve()
	}
}

package main

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

// github.com/EndlessCheng/codeforces-go
func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	type pair struct {
		v int64
		e int
	}

	primeFactorization := func(x int64) (factors []pair) {
		for i := int64(2); i*i <= x; i++ {
			e := 0
			for ; x%i == 0; x /= i {
				e++
			}
			if e > 0 {
				factors = append(factors, pair{i, e})
			}
		}
		if x > 1 {
			factors = append(factors, pair{x, 1})
		}
		return
	}
	solve := func(Case int) {
		var n int64
		Fscan(in, &n)
		fs := primeFactorization(n)
		mxI := 0
		for i, f := range fs {
			if f.e > fs[mxI].e {
				mxI = i
			}
		}
		p := fs[mxI]
		Fprintln(out, p.e)
		for i := 0; i < p.e-1; i++ {
			Fprint(out,p.v," " )
			n/=p.v
		}
		Fprintln(out, n)
	}

	var t int
	Fscan(in, &t)
	for Case := 1; Case <= t; Case++ {
		solve(Case)
	}
}

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

package main

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

func factorize(n int64) (primeFactors map[int64]int64, maxPower int64, remainder int64) {
	primeFactors = make(map[int64]int64)
	maxPower = 1
	remainder = n

	// calculate prime factors
	for i := int64(2); i*i <= n; i++ {
		if i%2 == 0 && i > 2 {
			continue
		}
		for remainder%i == 0 {
			primeFactors[i]++
			remainder = remainder / i
		}
		if v, ok := primeFactors[i]; ok && (v > maxPower) {
			maxPower = v
		}
	}

	// check if n is prime
	if len(primeFactors) == 0 {
		primeFactors[n]++
		remainder = 1
	}

	return primeFactors, maxPower, remainder
}

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

	var t, n int64
	fmt.Fscan(in, &t)

	for ; t > 0; t-- {
		fmt.Fscan(in, &n)

		primeFactors, maxPower, remainder := factorize(n)

		result := make([]int64, maxPower)
		for i := range result {
			result[i] = 1
		}
		result[maxPower-1] *= remainder

		for k, v := range primeFactors {
			baseMultiplier := int64(1)
			for i := int64(0); i < v/maxPower; i++ {
				baseMultiplier *= k
			}
			for i := int64(0); i < maxPower; i++ {
				result[i] *= baseMultiplier
				if i > maxPower-1-v%maxPower {
					result[i] *= k
				}
			}
		}

		fmt.Println(maxPower)
		for i := range result {
			fmt.Printf("%d ", result[i])
		}
		fmt.Println()
	}
}

package main

import(

	"fmt"
	."fmt"
	"bufio"
	"os"
)

const hell int = 1000000007
const N int = 1000005

var sp [N]int

func seive(){
	for i := 2;i<N;i++{
		for j := i;j<N;j+=i{
			if(sp[j]==0){
				sp[j]=i
			}
		}
	}
}

func max(x,y int)int{
	if x>y{
		x,y = y,x
	}
	return y
}

func solve(n int64){

	ans := 1
	val := n

	for i := int64(1);i*i<=n;i++{
		if n%i==0{
			f1,f2 := i,n/i
			if ans < count_freq(n,f1){
				ans = count_freq(n,f1)
				val = f1
			}
			if ans < count_freq(n,f2){
				ans = count_freq(n,f2)
				val = f2
			}
		}
	}

	v := []int64{}
	pdt := int64(1)

	for i:=0;i<ans;i++{
		v = append(v,val)
		pdt *= val
	}

	v[len(v)-1] *= (n/pdt)

	fmt.Println(ans)
	for _,val:= range v{
		fmt.Printf("%v ",val)
	}
	fmt.Println()
}

func count_freq(n,factor int64)int{
	ct := 0
	if factor>=1000005 || sp[int(factor)]!=int(factor){
		return ct
	}

	for ;n%factor==0;{
		ct+=1
		n/=factor
	}
	return ct
}

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

	Fscan(in,&TESTS)

	seive()

	for ;TESTS>0;TESTS--{
		var n int64
		Fscan(in,&n)
		solve(n)
	}
}

package main

import (
	"fmt"
	"bufio"
	"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 solve() {
	var n int64
	scanf("%d\n", &n)
	mxdeg := 1
	val := n
	m := n
	for i := int64(2); i * i < m && n > 1; i++ {
		if n % i == 0 {
			curr := 0
			for ; n % i == 0; curr++ {
				n /= i
			}
			if curr > mxdeg {
				mxdeg = curr
				val = i
			}
		}
	}
	printf("%d\n", mxdeg)
	rest := m
	for i := 0; i < mxdeg - 1; i++ {
		printf("%d ", val)
		rest /= val
	}
	printf("%d\n", rest)
}

func main() {
	defer writer.Flush()
	var t int
	scanf("%d\n", &t)
	for ; t > 0; t-- {
		solve()
	}
}

package main

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

func factorize(n int64) (primeFactors map[int64]int64, maxPower int64) {
	primeFactors = make(map[int64]int64)
	maxPower = 1

	// calculate prime factors
	remainder := n
	for i := int64(2); i*i <= n; i++ {
		if i%2 == 0 && i > 2 {
			continue
		}
		for remainder%i == 0 {
			primeFactors[i]++
			remainder = remainder / i
		}
		if v, ok := primeFactors[i]; ok && (v > maxPower) {
			maxPower = v
		}
	}

	// check if n is prime
	if len(primeFactors) == 0 {
		primeFactors[n]++
	}

	return primeFactors, maxPower
}

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

	var t, n int64
	fmt.Fscan(in, &t)

	for ; t > 0; t-- {
		fmt.Fscan(in, &n)

		primeFactors, maxPower := factorize(n)

		result := make([]int64, maxPower)
		for i := range result {
			result[i] = 1
		}

		for k, v := range primeFactors {
			baseMultiplier := int64(1)
			for i := int64(0); i < v/maxPower; i++ {
				baseMultiplier *= k
			}
			for i := int64(0); i < maxPower; i++ {
				result[i] *= baseMultiplier
				if i > maxPower-1-v%maxPower {
					result[i] *= k
				}
			}
		}

		fmt.Println(maxPower)
		for i := range result {
			fmt.Printf("%d ", result[i])
		}
		fmt.Println()
	}
}