You have unweighted tree of $$$n$$$ vertices. You have to assign a positive weight to each edge so that the following condition would hold:  For every two different leaves $$$v_{1}$$$ and $$$v_{2}$$$ of this tree, bitwise XOR of weights of all edges on the simple path between $$$v_{1}$$$ and $$$v_{2}$$$ has to be equal to $$$0$$$. Note that you can put very large positive integers (like $$$10^{(10^{10})}$$$).It's guaranteed that such assignment always exists under given constraints. Now let's define $$$f$$$ as the number of distinct weights in assignment.  In this example, assignment is valid, because bitwise XOR of all edge weights between every pair of leaves is $$$0$$$. $$$f$$$ value is $$$2$$$ here, because there are $$$2$$$ distinct edge weights($$$4$$$ and $$$5$$$). In this example, assignment is invalid, because bitwise XOR of all edge weights between vertex $$$1$$$ and vertex $$$6$$$ ($$$3, 4, 5, 4$$$) is not $$$0$$$. What are the minimum and the maximum possible values of $$$f$$$ for the given tree? Find and print both.

The first line contains integer $$$n$$$ ($$$3 \le n \le 10^{5}$$$) — the number of vertices in given tree. The $$$i$$$-th of the next $$$n-1$$$ lines contains two integers $$$a_{i}$$$ and $$$b_{i}$$$ ($$$1 \le a_{i} \lt b_{i} \le n$$$) — it means there is an edge between $$$a_{i}$$$ and $$$b_{i}$$$. It is guaranteed that given graph forms tree of $$$n$$$ vertices.

Print two integers — the minimum and maximum possible value of $$$f$$$ can be made from valid assignment of given tree. Note that it's always possible to make an assignment under given constraints.

NoteIn the first example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum.   In the second example, possible assignments for each minimum and maximum are described in picture below. The $$$f$$$ value of valid assignment of this tree is always $$$3$$$.   In the third example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum.

package main

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

type BufferedWriter interface {
	Printf(format string, a ...interface{})
	Flush()
}

type writerImpl struct {
	*bufio.Writer
}

func NewBufferedWriter(writer io.Writer) BufferedWriter {
	return &writerImpl{Writer: bufio.NewWriter(writer)}
}

func (impl *writerImpl) Printf(f string, a ...interface{}) {
	fmt.Fprintf(impl.Writer, f, a...)
}

func (impl *writerImpl) Flush() {
	impl.Writer.Flush()
}

type WordScanner interface {
	NextInt() int
	NextInt64() int64
	NextString() string
}

type wordScannerImpl struct {
	*bufio.Scanner
}

func NewWordScanner(reader io.Reader) WordScanner {
	s := bufio.NewScanner(reader)
	s.Split(bufio.ScanWords)
	// adjust the following size as needed
	// 2 << 17 is good enough to scan strings of len 2*10^5
	// it could go as high as 2 << 26 before cf judge breaks
	size := 2 << 20
	buf := make([]byte, size)
	s.Buffer(buf, size)
	return &wordScannerImpl{Scanner: s}
}

func (impl *wordScannerImpl) NextInt() int {
	impl.Scan()
	bb := impl.Bytes()
	i := 0
	if bb[0] == '-' {
		for _, b := range bb[1:] {
			i *= 10
			i -= int(b - '0')
		}
		return i
	}
	for _, b := range bb {
		i *= 10
		i += int(b - '0')
	}
	return i
}

func (impl *wordScannerImpl) NextInt64() int64 {
	impl.Scan()
	bb := impl.Bytes()
	i := int64(0)
	if bb[0] == '-' {
		for _, b := range bb[1:] {
			i *= 10
			i -= int64(b - '0')
		}
		return i
	}
	for _, b := range bb {
		i *= 10
		i += int64(b - '0')
	}
	return i
}

func (impl *wordScannerImpl) NextString() string {
	impl.Scan()
	return impl.Text()
}

var reader = NewWordScanner(os.Stdin)
var writer = NewBufferedWriter(os.Stdout)

func main() {
	defer writer.Flush()
	n := reader.NextInt64()
	mat := map[int64]map[int64]bool{}
	for i := int64(0); i < n-1; i++ {
		a, b := reader.NextInt64(), reader.NextInt64()
		if mat[a] == nil {
			mat[a] = map[int64]bool{}
		}
		if mat[b] == nil {
			mat[b] = map[int64]bool{}
		}
		mat[a][b] = true
		mat[b][a] = true
	}
	//writer.Printf("%v\n", mat)
	writer.Printf("%d %d\n", solveMin(mat), solveMax(mat))
}

func solveMin(mat map[int64]map[int64]bool) int64 {
	set := map[int64]bool{}
	dfs(mat, set, 1, 0, 0)
	if len(set) == 1 {
		return 1
	} else {
		return 3
	}
}

func dfs(mat map[int64]map[int64]bool, set map[int64]bool, to int64, from int64, curr int64) {
	if len(mat[to]) == 1 {
		set[curr] = true
	}
	curr ^= 1
	for child := range mat[to] {
		if child != from {
			dfs(mat, set, child, to, curr)
		}
	}
}

func solveMax(mat map[int64]map[int64]bool) int64 {
	ret := int64(len(mat)) - 1
	for i := range mat {
		cnt := int64(0)
		for nxt := range mat[i] {
			if len(mat[nxt]) == 1 {
				cnt++
			}
		}
		if cnt != 0 {
			ret -= cnt - 1
		}
	}
	return ret
}

package main

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

type BufferedWriter interface {
	Printf(format string, a ...interface{})
	Flush()
}

type writerImpl struct {
	*bufio.Writer
}

func NewBufferedWriter(writer io.Writer) BufferedWriter {
	return &writerImpl{Writer: bufio.NewWriter(writer)}
}

func (impl *writerImpl) Printf(f string, a ...interface{}) {
	fmt.Fprintf(impl.Writer, f, a...)
}

func (impl *writerImpl) Flush() {
	impl.Writer.Flush()
}

type WordScanner interface {
	NextInt() int
	NextInt64() int64
	NextString() string
}

type wordScannerImpl struct {
	*bufio.Scanner
}

func NewWordScanner(reader io.Reader) WordScanner {
	s := bufio.NewScanner(reader)
	s.Split(bufio.ScanWords)
	// adjust the following size as needed
	// 2 << 17 is good enough to scan strings of len 2*10^5
	// it could go as high as 2 << 26 before cf judge breaks
	size := 2 << 20
	buf := make([]byte, size)
	s.Buffer(buf, size)
	return &wordScannerImpl{Scanner: s}
}

func (impl *wordScannerImpl) NextInt() int {
	impl.Scan()
	bb := impl.Bytes()
	i := 0
	if bb[0] == '-' {
		for _, b := range bb[1:] {
			i *= 10
			i -= int(b - '0')
		}
		return i
	}
	for _, b := range bb {
		i *= 10
		i += int(b - '0')
	}
	return i
}

func (impl *wordScannerImpl) NextInt64() int64 {
	impl.Scan()
	bb := impl.Bytes()
	i := int64(0)
	if bb[0] == '-' {
		for _, b := range bb[1:] {
			i *= 10
			i -= int64(b - '0')
		}
		return i
	}
	for _, b := range bb {
		i *= 10
		i += int64(b - '0')
	}
	return i
}

func (impl *wordScannerImpl) NextString() string {
	impl.Scan()
	return impl.Text()
}

var reader = NewWordScanner(os.Stdin)
var writer = NewBufferedWriter(os.Stdout)

func main() {
	defer writer.Flush()
	n := reader.NextInt64()
	mat := map[int64]map[int64]bool{}
	for i := int64(0); i < n-1; i++ {
		a, b := reader.NextInt64(), reader.NextInt64()
		if mat[a] == nil {
			mat[a] = map[int64]bool{}
		}
		if mat[b] == nil {
			mat[b] = map[int64]bool{}
		}
		mat[a][b] = true
		mat[b][a] = true
	}
	writer.Printf("%d %d\n", solveMin(mat), solveMax(mat))
}

func solveMin(mat map[int64]map[int64]bool) int64 {
	set := map[bool]struct{}{}

	var dfs func(int64, int64, bool)
	dfs = func(from, to int64, curr bool) {
		if len(mat[to]) == 1 {
			set[curr] = struct{}{}
		}
		for child := range mat[to] {
			if child != from {
				dfs(to, child, !curr)
			}
		}
	}
	dfs(0, 1, true)

	if len(set) == 1 {
		return 1
	} else {
		return 3
	}
}

func solveMax(mat map[int64]map[int64]bool) int64 {
	ret := int64(len(mat)) - 1
	for i := range mat {
		cnt := int64(0)
		for nxt := range mat[i] {
			if len(mat[nxt]) == 1 {
				cnt++
			}
		}
		if cnt != 0 {
			ret -= cnt - 1
		}
	}
	return ret
}

package main

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

type BufferedWriter interface {
	Printf(format string, a ...interface{})
	Flush()
}

type writerImpl struct {
	*bufio.Writer
}

func NewBufferedWriter(writer io.Writer) BufferedWriter {
	return &writerImpl{Writer: bufio.NewWriter(writer)}
}

func (impl *writerImpl) Printf(f string, a ...interface{}) {
	fmt.Fprintf(impl.Writer, f, a...)
}

func (impl *writerImpl) Flush() {
	impl.Writer.Flush()
}

type WordScanner interface {
	NextInt() int
	NextInt64() int64
	NextString() string
}

type wordScannerImpl struct {
	*bufio.Scanner
}

func NewWordScanner(reader io.Reader) WordScanner {
	s := bufio.NewScanner(reader)
	s.Split(bufio.ScanWords)
	// adjust the following size as needed
	// 2 << 17 is good enough to scan strings of len 2*10^5
	// it could go as high as 2 << 26 before cf judge breaks
	size := 2 << 20
	buf := make([]byte, size)
	s.Buffer(buf, size)
	return &wordScannerImpl{Scanner: s}
}

func (impl *wordScannerImpl) NextInt() int {
	impl.Scan()
	bb := impl.Bytes()
	i := 0
	if bb[0] == '-' {
		for _, b := range bb[1:] {
			i *= 10
			i -= int(b - '0')
		}
		return i
	}
	for _, b := range bb {
		i *= 10
		i += int(b - '0')
	}
	return i
}

func (impl *wordScannerImpl) NextInt64() int64 {
	impl.Scan()
	bb := impl.Bytes()
	i := int64(0)
	if bb[0] == '-' {
		for _, b := range bb[1:] {
			i *= 10
			i -= int64(b - '0')
		}
		return i
	}
	for _, b := range bb {
		i *= 10
		i += int64(b - '0')
	}
	return i
}

func (impl *wordScannerImpl) NextString() string {
	impl.Scan()
	return impl.Text()
}

var reader = NewWordScanner(os.Stdin)
var writer = NewBufferedWriter(os.Stdout)

func main() {
	defer writer.Flush()
	n := reader.NextInt64()
	mat := map[int64]map[int64]bool{}
	for i := int64(0); i < n-1; i++ {
		a, b := reader.NextInt64(), reader.NextInt64()
		if mat[a] == nil {
			mat[a] = map[int64]bool{}
		}
		if mat[b] == nil {
			mat[b] = map[int64]bool{}
		}
		mat[a][b] = true
		mat[b][a] = true
	}
	//writer.Printf("%v\n", mat)
	writer.Printf("%d %d\n", solveMin(mat), solveMax(mat))
}

func solveMin(mat map[int64]map[int64]bool) int64 {
	set := map[int64]bool{}
	dfs(mat, set, 1, 0, 0)
	if len(set) == 1 {
		return 1
	} else {
		return 3
	}
}

func dfs(mat map[int64]map[int64]bool, set map[int64]bool, to int64, from int64, curr int64) {
	if len(mat[to]) == 1 {
		set[curr] = true
	}
	curr ^= 1
	for child := range mat[to] {
		if child != from {
			dfs(mat, set, child, to, curr)
		}
	}
}

func solveMax(mat map[int64]map[int64]bool) int64 {
	ret := int64(len(mat)) - 1
	for i := range mat {
		cnt := int64(0)
		for nxt := range mat[i+1] {
			if len(mat[nxt]) == 1 {
				cnt++
			}
		}
		ret -= max(0, cnt-1)
	}
	return ret
}

func max(a int64, rest ...int64) int64 {
	for _, r := range rest {
		if r > a {
			a = r
		}
	}
	return a
}

package main

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

type BufferedWriter interface {
	Printf(format string, a ...interface{})
	Flush()
}

type writerImpl struct {
	*bufio.Writer
}

func NewBufferedWriter(writer io.Writer) BufferedWriter {
	return &writerImpl{Writer: bufio.NewWriter(writer)}
}

func (impl *writerImpl) Printf(f string, a ...interface{}) {
	fmt.Fprintf(impl.Writer, f, a...)
}

func (impl *writerImpl) Flush() {
	impl.Writer.Flush()
}

type WordScanner interface {
	NextInt() int
	NextInt64() int64
	NextString() string
}

type wordScannerImpl struct {
	*bufio.Scanner
}

func NewWordScanner(reader io.Reader) WordScanner {
	s := bufio.NewScanner(reader)
	s.Split(bufio.ScanWords)
	// adjust the following size as needed
	// 2 << 17 is good enough to scan strings of len 2*10^5
	// it could go as high as 2 << 26 before cf judge breaks
	size := 2 << 20
	buf := make([]byte, size)
	s.Buffer(buf, size)
	return &wordScannerImpl{Scanner: s}
}

func (impl *wordScannerImpl) NextInt() int {
	impl.Scan()
	bb := impl.Bytes()
	i := 0
	if bb[0] == '-' {
		for _, b := range bb[1:] {
			i *= 10
			i -= int(b - '0')
		}
		return i
	}
	for _, b := range bb {
		i *= 10
		i += int(b - '0')
	}
	return i
}

func (impl *wordScannerImpl) NextInt64() int64 {
	impl.Scan()
	bb := impl.Bytes()
	i := int64(0)
	if bb[0] == '-' {
		for _, b := range bb[1:] {
			i *= 10
			i -= int64(b - '0')
		}
		return i
	}
	for _, b := range bb {
		i *= 10
		i += int64(b - '0')
	}
	return i
}

func (impl *wordScannerImpl) NextString() string {
	impl.Scan()
	return impl.Text()
}

var reader = NewWordScanner(os.Stdin)
var writer = NewBufferedWriter(os.Stdout)

func main() {
	defer writer.Flush()
	n := reader.NextInt64()
	mat := map[int64]map[int64]bool{}
	for i := int64(0); i < n-1; i++ {
		a, b := reader.NextInt64(), reader.NextInt64()
		if mat[a] == nil {
			mat[a] = map[int64]bool{}
		}
		if mat[b] == nil {
			mat[b] = map[int64]bool{}
		}
		mat[a][b] = true
		mat[b][a] = true
	}
	//writer.Printf("%v\n", mat)
	writer.Printf("%d %d\n", solveMin(mat), solveMax(mat))
}

func solveMin(mat map[int64]map[int64]bool) int64 {
	set := map[int64]bool{}
	dfs(mat, set,  1, 0, 0)
	if len(set) == 1 {
		return 1
	} else {
		return 3
	}
}

func dfs(mat map[int64]map[int64]bool, set map[int64]bool, to int64, from int64, curr int64) {
	if len(mat[to]) == 1 {
		set[curr] = true
	}
	curr ^= 1
	for child := range mat[to] {
		if child != from {
			dfs(mat, set, child, to, curr)
		}
	}
}

func solveMax(mat map[int64]map[int64]bool) int64 {
	ret := int64(len(mat)) - 1
	for i := range mat {
		cnt := int64(0)
		for nxt := range mat[i+1] {
			if len(mat[nxt]) == 1 {
				cnt++
			}
		}
		if cnt != 0 {
			ret -= cnt-1
		}
	}
	return ret
}


William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times:   Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$  $$$a_i = \frac{a_i}{2}$$$  $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i < 16)$$$, the contents of William's array.

For each test case output the maximal sum of array elements after performing an optimal sequence of operations.

NoteIn the first example test case the optimal sequence would be:   Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2].  Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2].  Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.

package main

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

func main(){
	var N_INPS int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	fmt.Sscanf(scanner.Text(), "%d", &N_INPS)
	var inps [][]int64
	var a int64
	for i:=0; i < N_INPS; i++ {
		scanner.Scan()
		scanner.Scan()
		var inp []int64
		for _, part := range strings.Split(scanner.Text(), " "){
			a, _ = strconv.ParseInt(part, 10, 64)
			inp = append(inp, a)
		}
		inps = append(inps, inp)
	}
	for _, inp := range inps {
		fmt.Println(compute_max(inp))
	}
}

func compute_max(arr []int64) int64{
	count := 0
	for i, _ := range arr {
		for arr[i]%2 == 0 {
			arr[i] /= 2
			count ++
		}
	}
	//sort.Slice(arr, func (x, y int) bool {return arr[x] < arr[y]})
	//arr[len(arr)-1] *= int64(math.Pow(2, float64(count)))
	max_el := 0
	for i, _ := range arr {
		if arr[i] > arr[max_el]{
			max_el = i
		}
	}
	arr[max_el] *= int64(math.Pow(2, float64(count)))
	return sum_arr(arr)
}

func sum_arr(arr []int64) int64{
	sum := int64(0)
	for _, num := range arr {
		sum += num
	}
	return sum
}

package main

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

func main(){
	var N_INPS int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	fmt.Sscanf(scanner.Text(), "%d", &N_INPS)
	var inps [][]int64
	var a int64
	for i:=0; i < N_INPS; i++ {
		scanner.Scan()
		scanner.Scan()
		var inp []int64
		for _, part := range strings.Split(scanner.Text(), " "){
			a, _ = strconv.ParseInt(part, 10, 64)
			inp = append(inp, a)
		}
		inps = append(inps, inp)
	}
	for _, inp := range inps {
		fmt.Println(compute_max(inp))
	}
}

func compute_max(arr []int64) int64{
	count := 0
	for i, _ := range arr {
		for arr[i]%2 == 0 {
			arr[i] /= 2
			count ++
		}
	}
	sort.Slice(arr, func (x, y int) bool {return arr[x] < arr[y]})
	arr[len(arr)-1] *= int64(math.Pow(2, float64(count)))
	return sum_arr(arr)
}

func sum_arr(arr []int64) int64{
	sum := int64(0)
	for _, num := range arr {
		sum += num
	}
	return sum
}

package main

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

func solve() {
	f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	N, n := 0, 0
	fmt.Fscan(f, &N)
	for i := 0; i < N; i++ {
		fmt.Fscan(f, &n)
		o, tot := make([]int64, n), 0
		for j := 0; j < n; j++ {
			fmt.Fscan(f, &o[j])
		}
		for j := 0; j < n; j++ {
			for o[j]%2 == 0 {
				o[j] = o[j] >> 1
				tot++
			}
		}
		sort.Slice(o, func(i, j int) bool { return o[i] < o[j] })
		for j := 0; j < tot; j++ {
			o[n-1] = o[n-1] << 1
		}
		ans := int64(0)
		for j := 0; j < n; j++ {
			ans += o[j]
		}
		fmt.Fprintln(w, ans)
	}
	w.Flush()
}

func main() {
	solve()
}

package main

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

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

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n int
		fmt.Fscan(in, &n)

		arr := make([]int, n)
		t := int64(1)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])

			for arr[i]%2 == 0 {
				arr[i] /= 2
				t *= 2
			}
		}

		sort.Ints(arr)
		t *= int64(arr[n-1])
		for i := 0; i < n-1; i++ {
			t += int64(arr[i])
		}
		fmt.Fprintln(out, t)
	}
}

package main

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

var (
	input = bufio.NewReader(os.Stdin)
	output = bufio.NewWriter(os.Stdout)
)

func assert(test bool) {
    if !test {panic("assert!")}
}

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
// TODO***
// - read the statement carefully
// - get solution before coding
// - check the boundaries (int/int64)

func main() {
    defer output.Flush()
    //solve()
    //return
	var t int64
	fmt.Fscan(input, &t)
    for t > 0 {
        t--
        solve()
    }
}

func solve() {
    var n int
    fmt.Fscan(input, &n)
    a := make([]int64, n)
    ans := int64(0)
    for i := 0; i < n; i ++ {
        fmt.Fscan(input, &a[i])
    }
    shift := 0
    for i := 0; i < n; i++ {
        if a[i] & 1 == 1 { continue }
        for a[i] & 1 == 0 {
            shift++
            a[i] /= 2
        }
    }
    sort.Slice(a, func(i, j int) bool { return a[i] < a[j] })
    a[n - 1] = a[n - 1] << shift
    for i := 0; i < n; i++ {
        ans += a[i]
    }
    fmt.Fprintln(output, ans)
}

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

package main

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

/*
3
1
1
1
3
2
8 8
---
1
3
64
======
1
3
2 3 4
---
26
======
1
15
8 8 8 8 8 8 8 8 8 8 8 8 8 8 15
---
*/

//goland:noinspection GoUnhandledErrorResult
func main() {
	defer writer.Flush()

	bld := strings.Builder{}

	var t = 1
	scanf("%d\n", &t)

	for ; t > 0; t-- {
		var n int
		scanf("%d\n", &n)

		a := make([]int64, n)

		for i := 0; i < n; i++ {
			scanf("%d", &a[i])
		}
		scanf("\n")

		k := int64(1)

		for i := 0; i < n; i++ {
			for a[i]%2 == 0 {
				a[i] /= 2
				k *= 2
			}
		}

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

		a[n-1] *= k

		sum := int64(0)
		for _, num := range a {
			sum += num
		}

		fmt.Fprintln(&bld, sum)
	}

	printf("%s", bld.String())
}

const maxUint = ^uint(0)

//goland:noinspection GoUnusedConst
const minUint = 0
const maxInt = int(maxUint >> 1)

//goland:noinspection GoUnusedConst
const minInt = -maxInt - 1

//goland:noinspection GoUnusedType
type pair struct {
	fst int
	snd int
}

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

//goland:noinspection GoUnusedFunction
func printf(f string, a ...interface{}) {
	if _, err := fmt.Fprintf(writer, f, a...); err != nil {
		panic(err)
	}
}

func scanf(f string, a ...interface{}) {
	if _, err := fmt.Fscanf(reader, f, a...); err != nil {
		panic(err)
	}
}

//goland:noinspection GoUnusedFunction
func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

//goland:noinspection GoUnusedFunction
func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

//goland:noinspection GoUnusedFunction
func abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

//goland:noinspection GoUnusedFunction
func isPrime(n int) bool {
	if n == 2 || n == 3 {
		return true
	}

	if n <= 1 || n%2 == 0 || n%3 == 0 {
		return false
	}

	for i := 5; i*i <= n; i += 6 {
		if n%i == 0 || n%(i+2) == 0 {
			return false
		}
	}

	return true
}

//goland:noinspection GoUnusedFunction
func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

//goland:noinspection GoUnusedFunction
func lcm(a, b int) int {
	return abs(a) / gcd(a, b) * abs(b)
}

// Examples:
//	digitAt(1234, 0) -> 4
//	digitAt(1234, 1) -> 3
//	digitAt(1234, 2) -> 2
//	digitAt(1234, 3) -> 2
//	digitAt(1234, 4) -> panic!
//goland:noinspection GoUnusedFunction
func digitAt(num, pos int) int {
	lenNum := int(math.Log10(float64(num))) + 1
	if pos >= lenNum {
		err := fmt.Sprintf("Out of bound access for digit. Position passed %d, number %d", pos, num)
		panic(err)
	}

	rem := num % int(math.Pow10(pos+1))
	return rem / int(math.Pow10(pos))
}

package main

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

func solve() {
	f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	N, n := 0, 0
	fmt.Fscan(f, &N)
	for i := 0; i < N; i++ {
		fmt.Fscan(f, &n)
		o, tot := make([]int, n), 0
		for j := 0; j < n; j++ {
			fmt.Fscan(f, &o[j])
		}
		for j := 0; j < n; j++ {
			for o[j]%2 == 0 {
				o[j] = o[j] >> 1
				tot++
			}
		}
		sort.Slice(o, func(i, j int) bool { return o[i] < o[j] })
		for j := 0; j < tot; j++ {
			o[n-1] = o[n-1] << 1
		}
		ans := int64(0)
		for j := 0; j < n; j++ {
			ans += int64(o[j])
		}
		fmt.Fprintln(w, ans)
	}
	w.Flush()
}

func main() {
	solve()
}

package main

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

func solve() {
	f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	N, n := 0, 0
	fmt.Fscan(f, &N)
	for i := 0; i < N; i++ {
		fmt.Fscan(f, &n)
		o, tot, mx, k := make([]int64, n), 0, int64(-1), 0
		for j := 0; j < n; j++ {
			fmt.Fscan(f, &o[j])
		}
		sort.Slice(o, func(i, j int) bool { return o[i] < o[j] })
		for j := 0; j < n; j++ {
			if o[j]%2 != 0 && o[j] > mx {
				mx, k = o[j], j
			}
		}
		if mx != -1 {
			o[k], o[n-1] = o[n-1], o[k]
		}
		for j := 0; j < n; j++ {
			for o[j]%2 == 0 {
				if j == n-1 && mx == -1 {
					break
				}
				o[j] = o[j] >> 1
				tot++
			}
		}
		for j := 0; j < tot; j++ {
			o[n-1] = o[n-1] << 1
		}
		ans := int64(0)
		for j := 0; j < n; j++ {
			ans += o[j]
		}
		fmt.Fprintln(w, ans)
	}
	w.Flush()
}

func main() {
	solve()
}

package main

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

func solve() {
	f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	N, n := 0, 0
	fmt.Fscan(f, &N)
	for i := 0; i < N; i++ {
		fmt.Fscan(f, &n)
		o, tot, mx, k := make([]int64, n), 0, int64(-1), 0
		for j := 0; j < n; j++ {
			fmt.Fscan(f, &o[j])
			if o[j]%2 != 0 && o[j] > mx {
				mx, k = o[j], j
			}
		}
		sort.Slice(o, func(i, j int) bool { return o[i] < o[j] })
		if mx != -1 {
			o[k], o[n-1] = o[n-1], o[k]
		}
		for j := 0; j < n; j++ {
			for o[j]%2 == 0 {
				if j == n-1 && mx == -1 {
					break
				}
				o[j] = o[j] >> 1
				tot++
			}
		}
		for j := 0; j < tot; j++ {
			o[n-1] = o[n-1] << 1
		}
		ans := int64(0)
		for j := 0; j < n; j++ {
			ans += o[j]
		}
		fmt.Fprintln(w, ans)
	}
	w.Flush()
}

func main() {
	solve()
}

package main

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

func Sum(o []int) int {
	sum := 0
	for i := 0; i < len(o); i++ {
		sum += o[i]
	}
	return sum
}

func solve() {
	f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	N, n := 0, 0
	fmt.Fscan(f, &N)
	for i := 0; i < N; i++ {
		fmt.Fscan(f, &n)
		o, tot, mx, k := make([]int, n), 0, -1, 0
		for j := 0; j < n; j++ {
			fmt.Fscan(f, &o[j])
			if o[j]%2 != 0 && o[j] > mx {
				mx, k = o[j], j
			}
		}
		sort.Ints(o)
		if mx != -1 {
			o[k], o[n-1] = o[n-1], o[k]
		}
		for j := 0; j < n; j++ {
			for o[j]%2 == 0 {
				if j == n-1 && mx == -1 {
					break
				}
				o[j] = o[j] >> 1
				tot++
			}
		}
		for j := 0; j < tot; j++ {
			o[n-1] = o[n-1] << 1
		}
		fmt.Fprintln(w, Sum(o))
	}
	w.Flush()
}

func main() {
	solve()
}

package main

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

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

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n int
		fmt.Fscan(in, &n)

		var x int
		var m1, m2 int
		var t int
		for i := 0; i < n; i++ {
			var v int
			fmt.Fscan(in, &v)

			if v == 2 || v == 4 || v == 8 || v == 16 {
				x += bits.Len(uint(v)) - 1
				t++
			} else if v%2 == 0 {
				x++
				t += v / 2
			} else {
				t += v
			}

			if v > m1 {
				m1, m2 = v, m1
			} else if v > m2 {
				m2 = v
			}

		}

		total := int64(m1)
		if m1 == 16 || m1 == 8 || m1 == 4 || m1 == 2 && m2 != 0 {
			total = int64(m2)
		}

		if total%2 == 0 {
			if bits.OnesCount(uint(total)) == 1 {
				x -= bits.Len(uint(total)) - 1
				t--
			} else {
				t -= int(total / 2)
				x--
			}
		} else {
			t -= int(total)
		}

		for i := 0; i < x; i++ {
			total *= 2
		}

		if n == 1 {
			fmt.Fprintln(out, m1)
		} else {
			fmt.Fprintln(out, total+int64(t))

		}
	}
}

package main

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

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

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n int
		fmt.Fscan(in, &n)

		var x int
		var m1, m2 int
		var t int
		for i := 0; i < n; i++ {
			var v int
			fmt.Fscan(in, &v)

			if v == 2 || v == 4 || v == 8 || v == 16 {
				x += bits.Len(uint(v)) - 1
				t++
			} else if v%2 == 0 {
				x++
				t += v / 2
			} else {
				t += v
			}

			if v > m1 {
				m1, m2 = v, m1
			} else if v > m2 {
				m2 = v
			}

		}

		total := int64(m1)
		if m1 == 16 || m1 == 8 || m1 == 4 || m1 == 2 && m2 != 0 {
			total = int64(m2)
		}

		if total%2 == 0 {
			if bits.OnesCount(uint(total)) == 1 {
				x -= bits.Len(uint(total)) - 1
				t--
			} else {
				t -= int(total / 2)
				x--
			}
		} else {
			t -= int(total)
		}

		for i := 0; i < x; i++ {
			total *= 2
		}

		fmt.Fprintln(out, total+int64(t))
	}
}

package main

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

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

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n int
		fmt.Fscan(in, &n)

		var x int
		var m1, m2 int
		var t int
		for i := 0; i < n; i++ {
			var v int
			fmt.Fscan(in, &v)

			if v == 2 || v == 4 || v == 8 || v == 16 {
				x += bits.Len(uint(v)) - 1
				t++
			} else if v%2 == 0 {
				x++
				t += v / 2
			} else {
				t += v
			}

			if v > m1 {
				m1, m2 = v, m1
			} else if v > m2 {
				m2 = v
			}

		}

		total := int64(m1)
		if m1 == 16 || m1 == 8 || m1 == 4 || m1 == 2 {
			total = int64(m2)
		}

		if total%2 == 0 {
			if bits.OnesCount(uint(total)) == 1 {
				x -= bits.Len(uint(total)) - 1
				t--
			} else {
				t -= int(total / 2)
				x--
			}
		} else {
			t -= int(total)
		}

		for i := 0; i < x; i++ {
			total *= 2
		}

		fmt.Fprintln(out, total+int64(t))
	}
}


You are given a grid with $$$n$$$ rows and $$$m$$$ columns. We denote the square on the $$$i$$$-th ($$$1\le i\le n$$$) row and $$$j$$$-th ($$$1\le j\le m$$$) column by $$$(i, j)$$$ and the number there by $$$a_{ij}$$$. All numbers are equal to $$$1$$$ or to $$$-1$$$. You start from the square $$$(1, 1)$$$ and can move one square down or one square to the right at a time. In the end, you want to end up at the square $$$(n, m)$$$.Is it possible to move in such a way so that the sum of the values written in all the visited cells (including $$$a_{11}$$$ and $$$a_{nm}$$$) is $$$0$$$?

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 1000$$$)  — the size of the grid. Each of the following $$$n$$$ lines contains $$$m$$$ integers. The $$$j$$$-th integer on the $$$i$$$-th line is $$$a_{ij}$$$ ($$$a_{ij} = 1$$$ or $$$-1$$$)  — the element in the cell $$$(i, j)$$$. It is guaranteed that the sum of $$$n\cdot m$$$ over all test cases does not exceed $$$10^6$$$.

For each test case, print "YES" if there exists a path from the top left to the bottom right that adds up to $$$0$$$, and "NO" otherwise. You can output each letter in any case.

NoteOne possible path for the fourth test case is given in the picture in the statement.

package main

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

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

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

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

	var T int
	fmt.Fscan(in, &T)
	const N = 1010
	var grid [N][N]int
	var mx [N][N]int
	var mn [N][N]int
	for t := 0; t < T; t++ {
		var n, m int
		fmt.Fscan(in, &n, &m)

		for i := 0; i < n; i++ {
			for j := 0; j < m; j++ {
				fmt.Fscan(in, &grid[i][j])
			}
		}

		mx[0][0], mn[0][0] = grid[0][0], grid[0][0]
		for i := 1; i < n; i++ {
			mx[i][0] = mx[i-1][0] + grid[i][0]
			mn[i][0] = mx[i][0]
		}

		for j := 1; j < m; j++ {
			mx[0][j] = mx[0][j-1] + grid[0][j]
			mn[0][j] = mx[0][j]
		}

		for i := 1; i < n; i++ {
			for j := 1; j < m; j++ {
				mx[i][j] = max(mx[i-1][j], mx[i][j-1]) + grid[i][j]
				mn[i][j] = min(mn[i-1][j], mn[i][j-1]) + grid[i][j]
			}
		}

		if mx[n-1][m-1]%2 != 0 || mn[n-1][m-1] > 0 || mx[n-1][m-1] < 0 {
			fmt.Fprintln(out, "NO")
		} else {
			fmt.Fprintln(out, "YES")
		}
	}
}

package main

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

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

func CF1695C(in io.Reader, out io.Writer) {
	r := bufio.NewReader(in)
	w := bufio.NewWriter(out)
	defer w.Flush()

	var T int64
	fmt.Fscan(r, &T)
	for ; T > 0; T-- {
		var n, m int64
		fmt.Fscan(r, &n, &m)
		arr := make([][]int64, n)
		mn, mx := make([][]int64, n), make([][]int64, n)
		for i := int64(0); i < n; i++ {
			arr[i] = make([]int64, m)
			mn[i], mx[i] = make([]int64, m), make([]int64, m)
			for j := int64(0); j < m; j++ {
				fmt.Fscan(r, &arr[i][j])
			}
		}
		mn[0][0], mx[0][0] = arr[0][0], arr[0][0]
		for i := int64(1); i < n; i++ {
			mx[i][0], mn[i][0] = mx[i-1][0]+arr[i][0], mx[i-1][0]+arr[i][0]
		}
		for  j := int64(1); j < m; j++ {
			mx[0][j], mn[0][j] = mx[0][j - 1] + arr[0][j], mx[0][j - 1] + arr[0][j]
		}

		for i := int64(1); i < n; i++ {
			for j := int64(1); j< m; j++ {
				mx[i][j] = max(mx[i - 1][j], mx[i][j - 1]) + arr[i][j];
				mn[i][j] = min(mn[i - 1][j], mn[i][j - 1]) + arr[i][j];
			}
		}

		if mx[n - 1][m - 1] % 2 == 1|| mn[n - 1][m - 1] > 0 || mx[n - 1][m - 1] < 0 {
			fmt.Fprintln(w, "NO")
		} else {
			fmt.Fprintln(w, "YES")
		}

	}

}

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

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

package main

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

/*
1
4 5
1 -1  1  1  1
1 -1  1 -1 -1
1  1 -1  1  1
1  1 -1  1 -1
---
YES

(1)   (0)     (1)       (2)         (3)
(2)  (-1,1)  (0,2)     (-1,1)     (-2,0,2)
(3) (0,2,4) (-1,1,3)   (0,2,4)    (-1,1,3,5)
(4) (1,3,5) (-2,0,2,4) (-1,1,3,5) (-2,0,2,4)
*/

//goland:noinspection GoUnhandledErrorResult
func solve() {
	var n, m int
	scanf("%d %d\n", &n, &m)

	a := make([][]int, n)

	for r := range a {
		a[r] = make([]int, m)

		for c := 0; c < m; c++ {
			scanf("%d", &a[r][c])
		}
		scanf("\n")
	}

	if (n+m-1)%2 != 0 {
		fmt.Fprintln(writer, "NO")
		return
	}

	mn := make([][]int, n)
	mx := make([][]int, n)

	for r := range a {
		mx[r] = make([]int, m)
		mn[r] = make([]int, m)
	}

	mx[0][0] = a[0][0]

	for c, num := range a[0] {
		mx[0][c] = num
		mn[0][c] = num

		if c != 0 {
			mx[0][c] += mx[0][c-1]
			mn[0][c] += mn[0][c-1]
		}
	}

	for r := 0; r < n; r++ {
		num := a[r][0]

		mx[r][0] = num
		mn[r][0] = num

		if r != 0 {
			mx[r][0] += mx[r-1][0]
			mn[r][0] += mn[r-1][0]
		}
	}

	for r := 1; r < n; r++ {
		for c := 1; c < m; c++ {
			num := a[r][c]

			mx[r][c] = max(mx[r-1][c], mx[r][c-1]) + num
			mn[r][c] = min(mn[r-1][c], mn[r][c-1]) + num
		}
	}

	if 0 >= mn[n-1][m-1] && 0 <= mx[n-1][m-1] {
		fmt.Fprintln(writer, "YES")
	} else {
		fmt.Fprintln(writer, "NO")
	}
}

//goland:noinspection GoUnhandledErrorResult
func main() {
	defer writer.Flush()

	t := 1
	scanf("%d\n", &t)

	for ; t > 0; t-- {
		solve()
	}
}

const maxUint = ^uint(0)

//goland:noinspection GoUnusedConst
const minUint = 0
const maxInt = int(maxUint >> 1)

//goland:noinspection GoUnusedConst
const minInt = -maxInt - 1

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

func scanf(f string, a ...interface{}) {
	if _, err := fmt.Fscanf(reader, f, a...); err != nil {
		panic(err)
	}
}

//goland:noinspection GoUnusedFunction
func reversed(s string) string {
	input := []rune(s)
	li := len(input)
	ret := make([]rune, li)
	for i, r := range input {
		ret[li-i-1] = r
	}
	return string(ret)
}

//goland:noinspection GoUnusedFunction
func divCeil(num, div int64) int64 {
	quot := num / div
	if num%div > 0 {
		quot++
	} else if num%div < 0 {
		quot--
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func min(nums ...int) int {
	return nums[minIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func minIdx(nums ...int) int {
	minNumIdx := 0
	for i, num := range nums {
		if num < nums[minNumIdx] {
			minNumIdx = i
		}
	}

	return minNumIdx
}

//goland:noinspection GoUnusedFunction
func max(nums ...int) int {
	return nums[maxIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func maxIdx(nums ...int) int {
	maxNumIdx := 0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = i
		}
	}

	return maxNumIdx
}

//goland:noinspection GoUnusedFunction
func sum(nums ...int) (sum int) {
	for _, num := range nums {
		sum += num
	}
	return
}

//goland:noinspection GoUnusedFunction
func abs(num int) int {
	if num < 0 {
		return -num
	}
	return num
}

//goland:noinspection GoUnusedFunction
func isPrime(num int64) bool {
	if num == 2 || num == 3 {
		return true
	}

	if num <= 1 || num%2 == 0 || num%3 == 0 {
		return false
	}

	for i := int64(5); i*i <= num; i += 6 {
		if num%i == 0 || num%(i+2) == 0 {
			return false
		}
	}

	return true
}

//goland:noinspection GoUnusedFunction
func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

//goland:noinspection GoUnusedFunction
func lcm(a, b int) int {
	return abs(a) / gcd(a, b) * abs(b)
}

// Examples:
//	digitAt(1234, -1) -> panic!
//	digitAt(1234, 0) -> 4
//	digitAt(1234, 1) -> 3
//	digitAt(1234, 2) -> 2
//	digitAt(1234, 3) -> 1
//	digitAt(1234, 4) -> panic!
//goland:noinspection GoUnusedFunction
func digitAt(num, pos int) int {
	if num == 0 {
		return 0
	}

	num = abs(num)

	lenNum := int(math.Log10(float64(num))) + 1
	if pos < 0 || pos >= lenNum {
		err := fmt.Sprintf("Out of bound access for digit. Position passed %d, number %d", pos, num)
		panic(err)
	}

	rem := num % int(math.Pow10(pos+1))
	return rem / int(math.Pow10(pos))
}

//goland:noinspection GoUnusedFunction
func factorize(num int64) []int64 {
	factors := make([]int64, 0)

	for ftr := int64(2); ftr*ftr <= num; ftr++ {
		for num%ftr == 0 {
			num /= ftr
			factors = append(factors, ftr)
		}
	}

	if num > 1 {
		factors = append(factors, num)
	}

	return factors
}

package main

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

/*
1
4 5
1 -1  1  1  1
1 -1  1 -1 -1
1  1 -1  1  1
1  1 -1  1 -1
---
YES

(1)   (0)     (1)       (2)         (3)
(2)  (-1,1)  (0,2)     (-1,1)     (-2,0,2)
(3) (0,2,4) (-1,1,3)   (0,2,4)    (-1,1,3,5)
(4) (1,3,5) (-2,0,2,4) (-1,1,3,5) (-2,0,2,4)
*/

//goland:noinspection GoUnhandledErrorResult
func solve() {
	var n, m int
	scanf("%d %d\n", &n, &m)

	a := make([][]int, n)

	for r := range a {
		a[r] = make([]int, m)

		for c := 0; c < m; c++ {
			scanf("%d", &a[r][c])
		}
		scanf("\n")
	}

	if (n+m-1)%2 != 0 {
		fmt.Fprintln(writer, "NO")
		return
	}

	mn := make([][]int, n)
	mx := make([][]int, n)

	for r := range a {
		mx[r] = make([]int, m)
		mn[r] = make([]int, m)
	}

	mx[0][0] = a[0][0]

	for c, num := range a[0] {
		mx[0][c] = num
		mn[0][c] = num

		if c != 0 {
			mx[0][c] += mx[0][c-1]
			mn[0][c] += mn[0][c-1]
		}
	}

	for r := 0; r < n; r++ {
		num := a[r][0]

		mx[r][0] = num
		mn[r][0] = num

		if r != 0 {
			mx[r][0] += mx[r-1][0]
			mn[r][0] += mn[r-1][0]
		}
	}

	for r := 1; r < n; r++ {
		for c := 1; c < m; c++ {
			num := a[r][c]

			mx[r][c] = max(mx[r-1][c], mx[r][c-1]) + num
			mn[r][c] = min(mn[r-1][c], mn[r][c-1]) + num
		}
	}

	if 0 >= mn[n-1][m-1] && 0 <= mx[n-1][m-1] && mx[n-1][m-1]%2 == 0 {
		fmt.Fprintln(writer, "YES")
	} else {
		fmt.Fprintln(writer, "NO")
	}
}

//goland:noinspection GoUnhandledErrorResult
func main() {
	defer writer.Flush()

	t := 1
	scanf("%d\n", &t)

	for ; t > 0; t-- {
		solve()
	}
}

const maxUint = ^uint(0)

//goland:noinspection GoUnusedConst
const minUint = 0
const maxInt = int(maxUint >> 1)

//goland:noinspection GoUnusedConst
const minInt = -maxInt - 1

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

func scanf(f string, a ...interface{}) {
	if _, err := fmt.Fscanf(reader, f, a...); err != nil {
		panic(err)
	}
}

//goland:noinspection GoUnusedFunction
func reversed(s string) string {
	input := []rune(s)
	li := len(input)
	ret := make([]rune, li)
	for i, r := range input {
		ret[li-i-1] = r
	}
	return string(ret)
}

//goland:noinspection GoUnusedFunction
func divCeil(num, div int64) int64 {
	quot := num / div
	if num%div > 0 {
		quot++
	} else if num%div < 0 {
		quot--
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func min(nums ...int) int {
	return nums[minIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func minIdx(nums ...int) int {
	minNumIdx := 0
	for i, num := range nums {
		if num < nums[minNumIdx] {
			minNumIdx = i
		}
	}

	return minNumIdx
}

//goland:noinspection GoUnusedFunction
func max(nums ...int) int {
	return nums[maxIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func maxIdx(nums ...int) int {
	maxNumIdx := 0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = i
		}
	}

	return maxNumIdx
}

//goland:noinspection GoUnusedFunction
func sum(nums ...int) (sum int) {
	for _, num := range nums {
		sum += num
	}
	return
}

//goland:noinspection GoUnusedFunction
func abs(num int) int {
	if num < 0 {
		return -num
	}
	return num
}

//goland:noinspection GoUnusedFunction
func isPrime(num int64) bool {
	if num == 2 || num == 3 {
		return true
	}

	if num <= 1 || num%2 == 0 || num%3 == 0 {
		return false
	}

	for i := int64(5); i*i <= num; i += 6 {
		if num%i == 0 || num%(i+2) == 0 {
			return false
		}
	}

	return true
}

//goland:noinspection GoUnusedFunction
func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

//goland:noinspection GoUnusedFunction
func lcm(a, b int) int {
	return abs(a) / gcd(a, b) * abs(b)
}

// Examples:
//	digitAt(1234, -1) -> panic!
//	digitAt(1234, 0) -> 4
//	digitAt(1234, 1) -> 3
//	digitAt(1234, 2) -> 2
//	digitAt(1234, 3) -> 1
//	digitAt(1234, 4) -> panic!
//goland:noinspection GoUnusedFunction
func digitAt(num, pos int) int {
	if num == 0 {
		return 0
	}

	num = abs(num)

	lenNum := int(math.Log10(float64(num))) + 1
	if pos < 0 || pos >= lenNum {
		err := fmt.Sprintf("Out of bound access for digit. Position passed %d, number %d", pos, num)
		panic(err)
	}

	rem := num % int(math.Pow10(pos+1))
	return rem / int(math.Pow10(pos))
}

//goland:noinspection GoUnusedFunction
func factorize(num int64) []int64 {
	factors := make([]int64, 0)

	for ftr := int64(2); ftr*ftr <= num; ftr++ {
		for num%ftr == 0 {
			num /= ftr
			factors = append(factors, ftr)
		}
	}

	if num > 1 {
		factors = append(factors, num)
	}

	return factors
}

package main

import (
	"bufio"
	"fmt"
	"os"
)
var a [200005]int
type Node struct{
	v int
	l int
	r int
	left *Node
	right *Node
}
func max(x,y int)int{
	if x>y{
		return x
	}
	return y
}
func min(x,y int)int{
	if x<y{
		return x
	}
	return y
}
func abs(x int)int{
	if x<0{
		return -x
	}
	return x
}
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for t>0{
		t--
		var n, m int
		fmt.Fscan(in,&n,&m)
		a := make([][]int,n+5)
		for i:=0;i<len(a);i++{
			a[i] = make([]int,m+5)
		}
		for i:=1;i<=n;i++{
			for j:=1;j<=m;j++{
				fmt.Fscan(in,&a[i][j])
			}
		}
		fmin := make([][]int,n+5)
		for i:=0;i<len(fmin);i++{
			fmin[i] = make([]int, m+5)
		}
		fmax := make([][]int,n+5)
		for i:=0;i<len(fmax);i++{
			fmax[i] = make([]int,m+5)
		}
		fmax[1][1] = a[1][1]
		fmin[1][1] = a[1][1]
		for i:=1;i<=n;i++{
			for j:=1;j<=m;j++{
				if i==1 && j==1{
					continue
				}
				if i-1>=1 && j-1 >=1{
					fmax[i][j] = a[i][j] + max(fmax[i-1][j],fmax[i][j-1])
					fmin[i][j] = a[i][j] + min(fmin[i-1][j],fmin[i][j-1])
					continue
				}
				if i-1>=1{
					fmax[i][j]=fmax[i-1][j] + a[i][j]
					fmin[i][j]=fmin[i-1][j] + a[i][j]
				}
				if j-1>=1{
					fmax[i][j]=fmax[i][j-1] + a[i][j]
					fmin[i][j]=fmin[i][j-1] + a[i][j]
				}
			}
		}
		if fmax[n][m]<0 || fmin[n][m] > 0 || (n+m)%2==0{
			fmt.Fprintln(out,"NO")
		}else{
			fmt.Fprintln(out,"YES")
		}
	}
}

package main

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

var _, _, _ = main, rw, do

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

func rw(r io.Reader, w io.Writer) {
	var t int
	fmt.Fscan(r, &t)
	for ti := 0; ti < t; ti++ {
		var n, m int
		fmt.Fscan(r, &n, &m)
		a := make([][]int, n)
		for i := 0; i < n; i++ {
			a[i] = make([]int, m)
			for j := 0; j < m; j++ {
				fmt.Fscan(r, &a[i][j])
			}
		}
		w1 := do(n, m, a)
		fmt.Fprintln(w, w1)
	}
}

func do(n, m int, a [][]int) string {
	if (n+m-1)%2 == 1 {
		return "NO"
	}
	b := make([]int, m)
	c := make([]int, m)
	b[0] = a[0][0]
	c[0] = a[0][0]
	min := func(a, b int) int {
		if a < b {
			return a
		} else {
			return b
		}
	}
	max := func(a, b int) int {
		if a > b {
			return a
		} else {
			return b
		}
	}
	for i := range a {
		for j := range a[i] {
			if i > 0 && j > 0 {
				b[j] = min(b[j], b[j-1]) + a[i][j]
				c[j] = max(c[j], c[j-1]) + a[i][j]
			} else if i > 0 {
				b[j] += a[i][j]
				c[j] += a[i][j]
			} else if j > 0 {
				b[j] = b[j-1] + a[i][j]
				c[j] = c[j-1] + a[i][j]
			}
		}
	}
	if b[m-1] > 0 || c[m-1] < 0 {
		return "NO"
	}
	return "YES"
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)
var wrtr = bufio.NewWriterSize(os.Stdout, 10_000_000)
var rdr = bufio.NewScanner(os.Stdin)
func gs() string  { rdr.Scan(); return rdr.Text() }
func gi() int     { i,e := strconv.Atoi(gs()); if e != nil {panic(e)}; return i }
func gis(n int) []int  { res := make([]int,n); for i:=0;i<n;i++ { res[i] = gi() }; return res }
func gf() float64 { f,e := strconv.ParseFloat(gs(),64); if e != nil {panic(e)}; return f }
func gbs() []byte { return []byte(gs()) }
func gfs(n int) []float64  { res := make([]float64,n); for i:=0;i<n;i++ { res[i] = gf() }; return res }
func gss(n int) []string  { res := make([]string,n); for i:=0;i<n;i++ { res[i] = gs() }; return res }
func gi64() int64     { i,e := strconv.ParseInt(gs(),10,64); if e != nil {panic(e)}; return i }
func gis64(n int) []int64  { res := make([]int64,n); for i:=0;i<n;i++ { res[i] = gi64() }; return res }

func ia(m int) []int { return make([]int,m) }
func iai(m int,v int) []int { a := make([]int,m); for i:=0;i<m;i++ { a[i] = v }; return a }
func twodi(n int,m int,v int) [][]int {
	r := make([][]int,n); for i:=0;i<n;i++ { x := make([]int,m); for j:=0;j<m;j++ { x[j] = v }; r[i] = x }; return r
}
func fill2(m int) ([]int,[]int) { a,b := ia(m),ia(m); for i:=0;i<m;i++ {a[i],b[i] = gi(),gi()}; return a,b }
func fill3(m int) ([]int,[]int,[]int) { a,b,c := ia(m),ia(m),ia(m); for i:=0;i<m;i++ {a[i],b[i],c[i] = gi(),gi(),gi()}; return a,b,c }
func fill4(m int) ([]int,[]int,[]int,[]int) { a,b,c,d := ia(m),ia(m),ia(m),ia(m); for i:=0;i<m;i++ {a[i],b[i],c[i],d[i] = gi(),gi(),gi(),gi()}; return a,b,c,d }
func abs(a int) int { if a < 0 { return -a }; return a }
func rev(a []int) { i,j := 0,len(a)-1; for i < j { a[i],a[j] = a[j],a[i]; i++; j-- } }
func max(a,b int) int { if a > b { return a }; return b }
func min(a,b int) int { if a > b { return b }; return a }
func maxarr(a []int) int { ans := a[0]; for _,aa := range(a) { if aa > ans { ans = aa } }; return ans }
func minarr(a []int) int { ans := a[0]; for _,aa := range(a) { if aa < ans { ans = aa } }; return ans }
func sumarr(a []int) int { ans := 0; for _,aa := range(a) { ans += aa }; return ans }
func zeroarr(a []int) { for i:=0; i<len(a); i++ { a[i] = 0 } }
func powmod(a,e,mod int) int { res, m := 1, a; for e > 0 { if e&1 != 0 { res = res * m % mod }; m = m * m % mod; e >>= 1 }; return res }
func powint(a,e int) int { res, m := 1, a; for e > 0 { if e&1 != 0 { res = res * m }; m = m * m; e >>= 1 }; return res }
func gcd(a,b int) int { for b != 0 { t:=b; b=a%b; a=t }; return a }
func gcdExtended(a,b int) (int,int,int) { if a == 0 { return b,0,1 }; gcd,x1,y1 := gcdExtended(b%a,a); return gcd, y1-(b/a)*x1,x1 }
func modinv(a,m int) (int,bool) { g,x,_ := gcdExtended(a,m); if g != 1 { return 0,false }; return (x % m + m) % m,true  }
func vecintstring(a []int) string { astr := make([]string,len(a)); for i,a := range a { astr[i] = strconv.Itoa(a) }; return strings.Join(astr," ") }
func makefact(n int,mod int) ([]int,[]int) {
	fact,factinv := make([]int,n+1),make([]int,n+1)
	fact[0] = 1; for i:=1;i<=n;i++ { fact[i] = fact[i-1] * i % mod }
	factinv[n] = powmod(fact[n],mod-2,mod); for i:=n-1;i>=0;i-- { factinv[i] = factinv[i+1] * (i+1) % mod }
	return fact,factinv
}

func ia64(m int) []int64 { return make([]int64,m) }
func iai64(m int,v int64) []int64 { a := make([]int64,m); for i:=0;i<m;i++ { a[i] = v }; return a }
func twodi64(n int,m int,v int64) [][]int64 {
	r := make([][]int64,n); for i:=0;i<n;i++ { x := make([]int64,m); for j:=0;j<m;j++ { x[j] = v }; r[i] = x }; return r
}
func fill264(m int) ([]int64,[]int64) { a,b := ia64(m),ia64(m); for i:=0;i<m;i++ {a[i],b[i] = gi64(),gi64()}; return a,b }
func fill364(m int) ([]int64,[]int64,[]int64) { a,b,c := ia64(m),ia64(m),ia64(m); for i:=0;i<m;i++ {a[i],b[i],c[i] = gi64(),gi64(),gi64()}; return a,b,c }
func fill464(m int) ([]int64,[]int64,[]int64,[]int64) { a,b,c,d := ia64(m),ia64(m),ia64(m),ia64(m); for i:=0;i<m;i++ {a[i],b[i],c[i],d[i] = gi64(),gi64(),gi64(),gi64()}; return a,b,c,d }
func abs64(a int64) int64 { if a < 0 { return -a }; return a }
func rev64(a []int64) { i,j := 0,len(a)-1; for i < j { a[i],a[j] = a[j],a[i]; i++; j-- } }
func max64(a,b int64) int64 { if a > b { return a }; return b }
func min64(a,b int64) int64 { if a > b { return b }; return a }
func maxarr64(a []int64) int64 { ans := a[0]; for _,aa := range(a) { if aa > ans { ans = aa } }; return ans }
func minarr64(a []int64) int64 { ans := a[0]; for _,aa := range(a) { if aa < ans { ans = aa } }; return ans }
func sumarr64(a []int64) int64 { ans := int64(0); for _,aa := range(a) { ans += aa }; return ans }
func zeroarr64(a []int64) { for i:=0; i<len(a); i++ { a[i] = 0 } }
func powmod64(a,e,mod int64) int64 { res, m := int64(1), a; for e > 0 { if e&1 != 0 { res = res * m % mod }; m = m * m % mod; e >>= 1 }; return res }
func powint64(a,e int64) int64 { res, m := int64(1), a; for e > 0 { if e&1 != 0 { res = res * m }; m = m * m; e >>= 1 }; return res }
func gcd64(a,b int64) int64 { for b != 0 { t:=b; b=a%b; a=t }; return a }
func gcdExtended64(a,b int64) (int64,int64,int64) { if a == 0 { return b,0,1 }; gcd,x1,y1 := gcdExtended64(b%a,a); return gcd, y1-(b/a)*x1,x1 }
func modinv64(a,m int64) (int64,bool) { g,x,_ := gcdExtended64(a,m); if g != 1 { return 0,false }; return (x % m + m) % m,true  }
func vecint64string(a []int64) string { astr := make([]string,len(a)); for i,a := range a { astr[i] = strconv.FormatInt(a,10) }; return strings.Join(astr," ") }
func makefact64(n int,mod int64) ([]int64,[]int64) {
	fact,factinv := make([]int64,n+1),make([]int64,n+1)
	fact[0] = 1; for i:=1;i<=n;i++ { fact[i] = fact[i-1] * int64(i) % mod }
	factinv[n] = powmod64(fact[n],mod-2,mod); for i:=n-1;i>=0;i-- { factinv[i] = factinv[i+1] * int64(i+1) % mod }
	return fact,factinv
}
func main() {
	//f1, _ := os.Create("cpu.prof"); pprof.StartCPUProfile(f1); defer pprof.StopCPUProfile()
	defer wrtr.Flush()
	infn := ""; if infn == "" && len(os.Args) > 1 {	infn = os.Args[1] }
	if infn != "" {	f, e := os.Open(infn); if e != nil { panic(e) }; rdr = bufio.NewScanner(f) }
	rdr.Split(bufio.ScanWords); rdr.Buffer(make([]byte,1024),1_000_000_000)
	// PROGRAM STARTS HERE
	T := gi();
	for tt:=1;tt<=T;tt++ {
		N,M := gi(),gi(); gr := make([][]int,N); for i:=0;i<N;i++ { gr[i] = gis(M); }
		// Just need to check if zero is in between the max and min and has the right parity
		dpmax := twodi(N,M,-999999999)
		dpmin := twodi(N,M,999999999)
		dpmax[0][0] = gr[0][0]
		dpmin[0][0] = gr[0][0]
		for i:=0;i<N;i++ {
			for j:=0;j<M;j++ {
				if i > 0 { dpmax[i][j] = max(dpmax[i][j],dpmax[i-1][j]+gr[i][j]); dpmin[i][j] = min(dpmin[i][j],dpmin[i-1][j]+gr[i][j]) }
				if j > 0 { dpmax[i][j] = max(dpmax[i][j],dpmax[i][j-1]+gr[i][j]); dpmin[i][j] = min(dpmin[i][j],dpmin[i][j-1]+gr[i][j]) }
			}
		}
		ans := "NO"
		if (N+M) % 2 == 1 && dpmax[N-1][M-1] >= 0 && dpmin[N-1][M-1] <= 0 { ans = "YES" }
		fmt.Fprintln(wrtr,ans)
	}
}

package main

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

func abs(n int) int {
	if n < 0 {
		return n * -1
	}
	return n
}

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

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n, m int
		fmt.Fscan(in, &n, &m)

		aa := make([][]int, n)
		for i := 0; i < n; i++ {
			aa[i] = make([]int, m)
			for j := 0; j < m; j++ {
				fmt.Fscan(in, &aa[i][j])
			}
		}

		vis := make([][]bool, n)
		for i := 0; i < n; i++ {
			vis[i] = make([]bool, m)
		}
		var f bool
		var walk func(i, j, tot int)
		walk = func(i, j, tot int) {
			t2 := tot + aa[i][j]
			if f || abs(t2) > (n-i-1)+(m-j-1) {
				return
			}
			vis[i][j] = true
			if i < n-1 && !vis[i+1][j] {
				walk(i+1, j, t2)
			}

			if j < m-1 && !vis[i][j+1] {
				walk(i, j+1, t2)
			}

			if i == n-1 && j == m-1 && t2 == 0 {
				f = true
			}
		}
		walk(0, 0, 0)

		if f {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}
}

package main

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

func abs(n int) int {
	if n < 0 {
		return n * -1
	}
	return n
}

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

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n, m int
		fmt.Fscan(in, &n, &m)

		aa := make([][]int, n)
		for i := 0; i < n; i++ {
			aa[i] = make([]int, m)
			for j := 0; j < m; j++ {
				fmt.Fscan(in, &aa[i][j])
			}
		}

		var f bool
		var walk func(i, j, tot int)
		vis := make([]bool, n*m+1)
		walk = func(i, j, tot int) {
			t2 := tot + aa[i][j]
			if f || abs(t2) > (n-i-1)+(m-j-1) || vis[(i*m)+j] {
				return
			}
			vis[(i*m)+j] = true
			if i < n-1 {
				walk(i+1, j, t2)
			}

			if j < m-1 {
				walk(i, j+1, t2)
			}

			if i == n-1 && j == m-1 && t2 == 0 {
				f = true
			}
		}
		walk(0, 0, 0)

		if f {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)
var a [200005]int
type Node struct{
	v int
	l int
	r int
	left *Node
	right *Node
}
func max(x,y int)int{
	if x>y{
		return x
	}
	return y
}
func min(x,y int)int{
	if x<y{
		return x
	}
	return y
}
func abs(x int)int{
	if x<0{
		return -x
	}
	return x
}
func build(x *Node, l, r int){
	x.l = l
	x.r = r
	mid := (l+r)>>1
	if l == r{
		x.v=a[mid]
		return
	}
	x.left = &Node{}
	x.right = &Node{}
	build(x.left,l,mid)
	build(x.right,mid+1,r)
	x.v = max(x.left.v,x.right.v)
}
func getMax(x *Node, l, r int) int{
	if(l<=x.l&&r>=x.r){
		return x.v
	}
	if r<x.l||l>x.r{
		return -1
	}
	lv := getMax(x.left,l,r)
	rv := getMax(x.right,l,r)
	return max(lv,rv)
}
var n, m int64
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for t>0{
		t--
		var n, m int
		fmt.Fscan(in,&n,&m)
		a := make([][]int,n+5)
		for i:=0;i<len(a);i++{
			a[i] = make([]int,m+5)
		}
		for i:=1;i<=n;i++{
			for j:=1;j<=m;j++{
				fmt.Fscan(in,&a[i][j])
			}
		}
		fmin := make([][]int,n+5)
		for i:=0;i<len(fmin);i++{
			fmin[i] = make([]int, m+5)
		}
		fmax := make([][]int,n+5)
		for i:=0;i<len(fmax);i++{
			fmax[i] = make([]int,m+5)
		}
		fmax[1][1] = a[1][1]
		fmin[1][1] = a[1][1]
		for i:=1;i<=n;i++{
			for j:=1;j<=m;j++{
				if i==1 && j==1{
					continue
				}
				if i-1>=1 && j-1 >=1{
					fmax[i][j] = a[i][j] + max(fmax[i-1][j],fmax[i][j-1])
					fmin[i][j] = a[i][j] + min(fmin[i-1][j],fmin[i][j-1])
					continue
				}
				if i-1>=1{
					fmax[i][j]=fmax[i-1][j] + a[i][j]
					fmin[i][j]=fmin[i-1][j] + a[i][j]
				}
				if j-1>=1{
					fmax[i][j]=fmax[i][j-1] + a[i][j]
					fmin[i][j]=fmin[i][j-1] + a[i][j]
				}
			}
		}
		if fmax[n][m]<0 || fmin[n][m] > 0{
			fmt.Fprintln(out,"NO")
		}else{
			fmt.Fprintln(out,"YES")
		}
	}
}


Again, there are hard times in Berland! Many towns have such tensions that even civil war is possible. There are n towns in Reberland, some pairs of which connected by two-way roads. It is not guaranteed that it is possible to reach one town from any other town using these roads. Towns s and t announce the final break of any relationship and intend to rule out the possibility of moving between them by the roads. Now possibly it is needed to close several roads so that moving from s to t using roads becomes impossible. Each town agrees to spend money on closing no more than one road, therefore, the total number of closed roads will be no more than two.Help them find set of no more than two roads such that there will be no way between s and t after closing these roads. For each road the budget required for its closure was estimated. Among all sets find such that the total budget for the closure of a set of roads is minimum.

The first line of the input contains two integers n and m (2 ≤ n ≤ 1000, 0 ≤ m ≤ 30 000) — the number of towns in Berland and the number of roads. The second line contains integers s and t (1 ≤ s, t ≤ n, s ≠ t) — indices of towns which break up the relationships. Then follow m lines, each of them contains three integers xi, yi and wi (1 ≤ xi, yi ≤ n, 1 ≤ wi ≤ 109) — indices of towns connected by the i-th road, and the budget on its closure. All roads are bidirectional. It is allowed that the pair of towns is connected by more than one road. Roads that connect the city to itself are allowed.

In the first line print the minimum budget required to break up the relations between s and t, if it is allowed to close no more than two roads. In the second line print the value c (0 ≤ c ≤ 2) — the number of roads to be closed in the found solution. In the third line print in any order c diverse integers from 1 to m — indices of closed roads. Consider that the roads are numbered from 1 to m in the order they appear in the input.  If it is impossible to make towns s and t disconnected by removing no more than 2 roads, the output should contain a single line -1.  If there are several possible answers, you may print any of them.

package main

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

var bufin *bufio.Reader
var bufout *bufio.Writer

const maxn = 1000
const maxm = 30000

var n, m, s, t, e, cnt, closed, p, q, budget, e1, e2 int
var adj, idx, next, weight [maxm*2 + 1]int
var last, trace, num, low, path [maxn + 1]int

func add(i int, x int, y int, w int) {
	idx[e] = i
	adj[e] = y
	weight[e] = w
	next[e] = last[x]
	last[x] = e
	e++
}

func dfs(x int) {
	cnt++
	num[x] = cnt
	low[x] = cnt

	for i := last[x]; i != -1; i = next[i] {
		if (i != closed) && ((i ^ 1) != closed) {
			y := adj[i]
			if trace[y] == -1 {
				trace[y] = i
				dfs(y)
				if low[x] > low[y] {
					low[x] = low[y]
				}
			} else if trace[x] != (i ^ 1) {
				if low[x] > num[y] {
					low[x] = num[y]
				}
			}
		}
	}
}

func find(track bool) bool {
	for i := 1; i <= n; i++ {
		trace[i] = -1
	}

	trace[s] = 0
	cnt = 0
	dfs(s)

	if trace[t] == -1 {
		return false
	}

	if track {
		p = 0
	}
	y := t

	for y != s {
		i := trace[y]

		if track {
			path[p] = i
			p++
		}

		if low[y] >= num[y] {
			cost := weight[i]
			if closed != -1 {
				cost += weight[closed]
			}
			if (budget == -1) || (cost < budget) {
				budget = cost
				e1 = idx[i]
				if closed != -1 {
					e2 = idx[closed]
				}
			}
		}

		y = adj[i^1]
	}

	return true
}

func main() {
	bufin = bufio.NewReader(os.Stdin)
	bufout = bufio.NewWriter(os.Stdout)
	defer bufout.Flush()

	fmt.Fscan(bufin, &n, &m, &s, &t)

	e = 0
	for i := 1; i <= n; i++ {
		last[i] = -1
	}

	var x, y, w int
	for i := 1; i <= m; i++ {
		fmt.Fscan(bufin, &x, &y, &w)
		add(i, x, y, w)
		add(i, y, x, w)
	}

	closed = -1
	budget = -1
	e1 = -1
	e2 = -1

	if !find(true) {
		fmt.Fprintln(bufout, 0)
		fmt.Fprintln(bufout, 0)
	} else {
		for i := 0; i < p; i++ {
			closed = path[i]
			find(false)
		}

		fmt.Fprintln(bufout, budget)
		if budget != -1 {
			if e2 == -1 {
				fmt.Fprintln(bufout, 1)
				fmt.Fprintln(bufout, e1)
			} else {
				fmt.Fprintln(bufout, 2)
				fmt.Fprintln(bufout, e1, e2)
			}
		}
	}
}

package main

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

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

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

func Dprint(format string, a ...interface{}) {
	if Debug {
		fmt.Fprintf(os.Stderr, format+"\n", a...)
	}
}

type road struct {
	w   int // weight
	idx int // index of the road (starting from 0)
}

var disRoad *road

func (r *road) String() string {
	return fmt.Sprintf("<%d:%d>", r.idx+1, r.w)
}

type roadPtr struct {
	v int
	r *road
}

var (
	n, m    int
	s, t    int
	adj     [][]roadPtr
	visited []bool
)

func main() {
	defer writer.Flush()

	// Read inpput.
	Scanf("%d %d\n", &n, &m)
	Scanf("%d %d\n", &s, &t)
	s--
	t--

	adj = make([][]roadPtr, n)
	for i := 0; i < m; i++ {
		var xi, yi, wi int
		Scanf("%d %d %d\n", &xi, &yi, &wi)
		xi--
		yi--
		// TODO(stefan): idx: i+1?
		r := road{w: wi, idx: i}
		adj[xi] = append(adj[xi], roadPtr{yi, &r})
		adj[yi] = append(adj[yi], roadPtr{xi, &r})
	}

	// Run DFS(s, t) and try to remove each of the roads. Then run bridge-finding algo.
	visited = make([]bool, n)
	roads := dfs(s, t)

	if len(roads) == 0 {
		// They are already disconnected.
		Printf("0\n0\n")
		return
	}

	Dprint("%v", roads)
	runBridges(roads)

	if len(minRoads) == 0 {
		// Impossible to disconnect.
		Printf("-1")
		return
	}

	Printf("%d\n%d\n", minCost, len(minRoads))
	for _, r := range minRoads {
		Printf("%d ", r.idx+1)
	}
}

// run dfs from start, where previous node is prev and terminating at end.
// It will return list of roads from start to end including both.
// If we can't get from start to end, it will return empty list.
func dfs(start int, end int) []*road {
	visited[start] = true

	for _, nex := range adj[start] {
		if visited[nex.v] {
			// We are only interested in unvisited nodes.
			continue
		}

		if nex.v == end {
			visited[nex.v] = true
			return []*road{nex.r}
		}

		roads := dfs(nex.v, end)
		if len(roads) > 0 {
			return append([]*road{nex.r}, roads...)
		}
	}

	return nil
}

var (
	time    int
	times   []int
	low     []int
	bridges []*road

	minCost  = math.MaxInt32
	minRoads []*road
)

func runBridges(roads []*road) {
	// For every path in paths, try to disable this path and run dfs2 that finds bridges.
	for _, r := range roads {
		// Initialize dfs stuff.
		time = 1
		times = make([]int, n)
		low = make([]int, n)
		for i := 0; i < n; i++ {
			low[i] = math.MaxInt32
		}
		bridges = nil

		disRoad = r
		connected := findBridges(s, nil, t)
		Dprint("road=%v, bridges=%v, low=%v", r, bridges, low)

		if connected {
			// s and t are still connected after removing road.
			for _, br := range bridges {
				cost := disRoad.w + br.w
				if cost < minCost {
					minCost = cost
					minRoads = []*road{r, br}
				}
			}
		} else {
			// s and t are disconnected.
			cost := disRoad.w
			if cost < minCost {
				minCost = cost
				minRoads = []*road{r}
			}
		}
	}
}

// findBridges returns list of bridges, that is edges which removal would cause s and t to disconnect.
// It returns true if end is reachable from start. There should be only one way that contains all the bridges.
func findBridges(start int, prev *road, end int) bool {
	times[start] = time
	low[start] = time
	time++
	foundEnd := false
	for _, nex := range adj[start] {
		// Dprint(" >> dfs(%d) analyze %v", start+1, nex.r)
		if nex.r == disRoad {
			// This road was explicitly disabled in runBridges.
			continue
		}
		if nex.r == prev {
			// Do not look at parent road in DFS tree.
			continue
		}
		nexFoundEnd := false
		// Dprint("   >> not disabled and not prev")
		if times[nex.v] == 0 {
			// Dprint("     >> running DFS")
			// We haven't visited nex.v, so visit it.
			nexFoundEnd = findBridges(nex.v, nex.r, end)
			foundEnd = foundEnd || nexFoundEnd
		}
		if low[nex.v] < low[start] {
			// Update low either from child or back edge.
			low[start] = low[nex.v]
		}
		if nexFoundEnd && low[nex.v] > times[start] && foundEnd {
			// this is a bridge from s to t.
			bridges = append(bridges, nex.r)
		}
	}

	if start == end {
		return true
	}
	return foundEnd
}

package main

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

var bufin *bufio.Reader
var bufout *bufio.Writer

var n, m, s, t, e, x, y, w, cnt, budget, c, e1, e2, removed, np, ns int
var adj, next, last, idx, weight, trace, num, low, path, save []int
var visited, bridge []bool

func add(x int, y int, i int, w int) {
	adj[e] = y
	idx[e] = i
	weight[e] = w
	next[e] = last[x]
	last[x] = e
	e++
}

func dfs(u int) {
	cnt++
	num[u] = cnt
	low[u] = num[u]

	for i := last[u]; i != -1; i = next[i] {
		if (i != removed) && ((i ^ 1) != removed) {
			v := adj[i]
			if trace[v] == -1 {
				visited[i] = true
				visited[i^1] = true
				trace[v] = i
				dfs(v)
				if low[u] > low[v] {
					low[u] = low[v]
				}
				if low[v] >= num[v] {
					bridge[i] = true
					bridge[i^1] = true
				}
			} else if !visited[i] {
				if low[u] > num[v] {
					low[u] = num[v]
				}
			}
		}
	}
}

func find(ce int, bg int) bool {
	for i := range visited {
		visited[i] = false
		bridge[i] = false
	}

	for i := range trace {
		trace[i] = -1
	}

	trace[s] = s
	cnt = 0

	dfs(s)
	if trace[t] == -1 {
		return false
	}

	np = 0
	v := t

	for v != s {
		i := trace[v]
		path[np] = i
		np++
		v = adj[i^1]
		if bridge[i] && ((budget == -1) || (bg+weight[i] < budget)) {
			budget = bg + weight[i]
			c = ce
			e1 = idx[i]
			if removed >= 0 {
				e2 = idx[removed]
			}
		}
	}

	return true
}

func solution() {
	trace = make([]int, n)
	num = make([]int, n)
	low = make([]int, n)
	path = make([]int, n)
	save = make([]int, n)

	visited = make([]bool, e)
	bridge = make([]bool, e)

	removed = -1
	budget = -1
	c = -1
	e1 = -1
	e2 = -1

	if !find(1, 0) {
		fmt.Fprintln(bufout, 0)
		fmt.Fprintln(bufout, 0)
		return
	}

	ns = np
	for i, val := range path {
		save[i] = val
	}

	for j := 0; j < ns; j++ {
		removed = save[j]
		find(2, weight[removed])
	}

	fmt.Fprintln(bufout, budget)
	if budget != -1 {
		fmt.Fprintln(bufout, c)
		fmt.Fprint(bufout, e1)
		if c == 2 {
			fmt.Fprint(bufout, " ", e2)
		}
	}
}

func main() {
	bufin = bufio.NewReader(os.Stdin)
	bufout = bufio.NewWriter(os.Stdout)
	defer bufout.Flush()

	fmt.Fscan(bufin, &n, &m, &s, &t)
	s--
	t--

	adj = make([]int, m*2)
	idx = make([]int, m*2)
	weight = make([]int, m*2)
	next = make([]int, m*2)
	last = make([]int, n)

	for i := range last {
		last[i] = -1
	}
	e = 0

	for i := 1; i <= m; i++ {
		fmt.Fscan(bufin, &x, &y, &w)
		x--
		y--
		add(x, y, i, w)
		add(y, x, i, w)
	}

	solution()

}

package main

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

var bufin *bufio.Reader
var bufout *bufio.Writer

var n, m, s, t, e, x, y, w, cnt, budget, c, e1, e2 int
var adj, next, last, idx, weight, trace, num, low, save []int
var visited, bridge, path, removed []bool

func add(x int, y int, i int, w int) {
	adj[e] = y
	idx[e] = i
	weight[e] = w
	next[e] = last[x]
	last[x] = e
	e++
}

func dfs(u int) {
	cnt++
	num[u] = cnt
	low[u] = num[u]

	for i := last[u]; i != -1; i = next[i] {
		if !removed[i] {
			v := adj[i]
			if trace[v] == -1 {
				visited[i] = true
				visited[i^1] = true
				trace[v] = i
				dfs(v)
				if low[u] > low[v] {
					low[u] = low[v]
				}
				if low[v] >= num[v] {
					bridge[i] = true
					bridge[i^1] = true
				}
			} else if !visited[i] {
				if low[u] > num[v] {
					low[u] = num[v]
				}
			}
		}
	}
}

func find(ce int, bg int, oe int) bool {
	trace = make([]int, n)
	num = make([]int, n)
	low = make([]int, n)

	visited = make([]bool, e)
	bridge = make([]bool, e)

	for i := range visited {
		visited[i] = false
		bridge[i] = false
	}

	for i := range trace {
		trace[i] = -1
	}

	trace[s] = s
	cnt = 0

	dfs(s)
	if trace[t] == -1 {
		return false
	}

	path = make([]bool, e)
	for i := range path {
		path[i] = false
	}

	v := t
	for v != s {
		i := trace[v]
		path[i] = true
		path[i^1] = true
		v = adj[i^1]
		if bridge[i] && ((budget == -1) || (bg+weight[i] < budget)) {
			budget = bg + weight[i]
			c = ce
			e1 = idx[i]
			e2 = oe
		}
	}

	return true
}

func solution() {
	removed = make([]bool, e)
	for i := range removed {
		removed[i] = false
	}

	budget = -1
	c = -1
	e1 = -1
	e2 = -1

	if !find(1, 0, -1) {
		fmt.Fprintln(bufout, 0)
		fmt.Fprintln(bufout, 0)
		return
	}

	save = make([]int, e)
	ns := 0
	for i := 0; i < e; i += 2 {
		if path[i] {
			save[ns] = i
			ns++
		}
	}

	for j := 0; j < ns; j++ {
		i := save[j]
		removed[i] = true
		removed[i^1] = true
		find(2, weight[i], idx[i])
		removed[i] = false
		removed[i^1] = false
	}

	fmt.Fprintln(bufout, budget)
	if budget != -1 {
		fmt.Fprintln(bufout, c)
		fmt.Fprint(bufout, e1)
		if c == 2 {
			fmt.Fprint(bufout, " ", e2)
		}
	}
}

func main() {
	bufin = bufio.NewReader(os.Stdin)
	bufout = bufio.NewWriter(os.Stdout)
	defer bufout.Flush()

	fmt.Fscan(bufin, &n, &m, &s, &t)
	s--
	t--

	adj = make([]int, m*2)
	idx = make([]int, m*2)
	weight = make([]int, m*2)
	next = make([]int, m*2)
	last = make([]int, n)

	for i := range last {
		last[i] = -1
	}
	e = 0

	for i := 1; i <= m; i++ {
		fmt.Fscan(bufin, &x, &y, &w)
		x--
		y--
		add(x, y, i, w)
		add(y, x, i, w)
	}

	solution()

}

package main

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

type edge struct {
	to, cost, id int
}

type graph [][]edge

var path []int

func findPath(g graph, cur, target int, visited []bool) bool {
	if cur == target {
		return true
	}
	if visited[cur] {
		return false
	}
	visited[cur] = true
	for _, e := range g[cur] {
		if findPath(g, e.to, target, visited) {
			path = append(path, e.id)
			return true
		}
	}
	return false
}

var curTime = 0

func findCheapestBridge(g graph, cur, pi, target, forb int, tin, fup, cost []int) (bool, int) {
	tin[cur] = curTime
	fup[cur] = curTime
	curTime++

	can := cur == target
	idBest := -1
	for _, e := range g[cur] {
		if e.id == forb || e.id == pi {
			continue
		}
		if tin[e.to] != 0 {
			fup[cur] = min(fup[cur], tin[e.to])
		} else {
			c, id := findCheapestBridge(g, e.to, e.id, target, forb, tin, fup, cost)
			if id != -1 {
				if idBest == -1 || cost[id] < cost[idBest] {
					idBest = id
				}
			}
			if fup[e.to] == tin[e.to] && c {
				if idBest == -1 || e.cost < cost[idBest] {
					idBest = e.id
				}
			}
			can = can || c

			fup[cur] = min(fup[cur], fup[e.to])
		}
	}

	return can, idBest
}

func main() {
	cin := NewReader()

	n := cin.NextInt()
	m := cin.NextInt()
	s := cin.NextInt() - 1
	t := cin.NextInt() - 1

	g := make(graph, n)
	cost := make([]int, m)
	for i := 0; i < m; i++ {
		u := cin.NextInt() - 1
		v := cin.NextInt() - 1
		c := cin.NextInt()
		g[u] = append(g[u], edge{v, c, i})
		g[v] = append(g[v], edge{u, c, i})
		cost[i] = c
	}

	visited := make([]bool, n)
	path = []int{}

	if !findPath(g, s, t, visited) {
		fmt.Printf("0\n0\n")
		return
	}

	best := -1
	ans := []int{}

	tin := make([]int, n)
	fup := make([]int, n)

	for _, id := range path {
		for i := 0; i < n; i++ {
			tin[i] = 0
			fup[i] = 0
		}

		curTime = 1
		found, bridgeID := findCheapestBridge(g, s, -1, t, id, tin, fup, cost)

		if bridgeID == -1 {
			if !found && (best == -1 || cost[id] < best) {
				best = cost[id]
				ans = []int{id}
			}
		} else {
			if best == -1 || (cost[id]+cost[bridgeID] < best) {
				best = cost[id] + cost[bridgeID]
				ans = []int{id, bridgeID}
			}
		}
	}

	if len(ans) > 0 {
		fmt.Printf("%d\n%d\n", best, len(ans))
		for _, it := range ans {
			fmt.Printf("%d ", it+1)
		}
		fmt.Println()
	} else {
		fmt.Printf("-1\n")
	}
}

// Reader is a fast input for programming competitions
type Reader struct {
	scanner *bufio.Scanner
}

// NewReader ...
func NewReader() *Reader {
	var reader Reader
	reader.scanner = bufio.NewScanner(os.Stdin)
	reader.scanner.Split(bufio.ScanWords)
	return &reader
}

// NextInt ...
func (r *Reader) NextInt() int {
	r.scanner.Scan()
	res, _ := strconv.Atoi(r.scanner.Text())
	return res
}

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

package main

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

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

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

func Dprint(format string, a ...interface{}) {
	if Debug {
		fmt.Fprintf(os.Stderr, format+"\n", a...)
	}
}

type road struct {
	w   int // weight
	idx int // index of the road (starting from 0)
}

var disRoad *road

func (r *road) String() string {
	return fmt.Sprintf("<%d:%d>", r.idx+1, r.w)
}

type roadPtr struct {
	v int
	r *road
}

var (
	n, m    int
	s, t    int
	adj     [][]roadPtr
	visited []bool
)

func main() {
	defer writer.Flush()

	// Read inpput.
	Scanf("%d %d\n", &n, &m)
	Scanf("%d %d\n", &s, &t)
	s--
	t--

	adj = make([][]roadPtr, n)
	for i := 0; i < m; i++ {
		var xi, yi, wi int
		Scanf("%d %d %d\n", &xi, &yi, &wi)
		xi--
		yi--
		// TODO(stefan): idx: i+1?
		r := road{w: wi, idx: i}
		adj[xi] = append(adj[xi], roadPtr{yi, &r})
		adj[yi] = append(adj[yi], roadPtr{xi, &r})
	}

	// Run DFS(s, t) and try to remove each of the roads. Then run bridge-finding algo.
	visited = make([]bool, n)
	roads := dfs(s, t)

	if len(roads) == 0 {
		// They are already disconnected.
		Printf("0\n0\n")
		return
	}

	Dprint("%v", roads)
	runBridges(roads)

	if len(minRoads) == 0 {
		// Impossible to disconnect.
		Printf("-1")
		return
	}

	Printf("%d\n%d\n", minCost, len(minRoads))
	for _, r := range minRoads {
		Printf("%d ", r.idx+1)
	}
}

// run dfs from start, where previous node is prev and terminating at end.
// It will return list of roads from start to end including both.
// If we can't get from start to end, it will return empty list.
func dfs(start int, end int) []*road {
	visited[start] = true

	for _, nex := range adj[start] {
		if visited[nex.v] {
			// We are only interested in unvisited nodes.
			continue
		}

		if nex.v == end {
			visited[nex.v] = true
			return []*road{nex.r}
		}

		roads := dfs(nex.v, end)
		if len(roads) > 0 {
			return append([]*road{nex.r}, roads...)
		}
	}

	return nil
}

var (
	time    int
	times   []int
	low     []int
	bridges []*road

	minCost  = math.MaxInt32
	minRoads []*road
)

func runBridges(roads []*road) {
	// For every path in paths, try to disable this path and run dfs2 that finds bridges.
	for _, r := range roads {
		// Initialize dfs stuff.
		time = 1
		times = make([]int, n)
		low = make([]int, n)
		for i := 0; i < n; i++ {
			low[i] = math.MaxInt32
		}
		bridges = nil

		disRoad = r
		connected := findBridges(s, nil, t)
		Dprint("road=%v, bridges=%v, low=%v", r, bridges, low)

		if connected {
			// s and t are still connected after removing road.
			for _, br := range bridges {
				cost := disRoad.w + br.w
				if cost < minCost {
					minCost = cost
					minRoads = []*road{r, br}
				}
			}
		} else {
			// s and t are disconnected.
			cost := disRoad.w
			if cost < minCost {
				minCost = cost
				minRoads = []*road{r}
			}
		}
	}
}

// findBridges returns list of bridges, that is edges which removal would cause s and t to disconnect.
// It returns true if end is reachable from start. There should be only one way that contains all the bridges.
func findBridges(start int, prev *road, end int) bool {
	times[start] = time
	low[start] = time
	time++
	foundEnd := false
	for _, nex := range adj[start] {
		// Dprint(" >> dfs(%d) analyze %v", start+1, nex.r)
		if nex.r == disRoad {
			// This road was explicitly disabled in runBridges.
			continue
		}
		if nex.r == prev {
			// Do not look at parent road in DFS tree.
			continue
		}
		nexFoundEnd := false
		// Dprint("   >> not disabled and not prev")
		if times[nex.v] == 0 {
			// Dprint("     >> running DFS")
			// We haven't visited nex.v, so visit it.
			nexFoundEnd = findBridges(nex.v, nex.r, end)
			foundEnd = foundEnd || nexFoundEnd
		}
		if low[nex.v] < low[start] {
			// Update low either from child or back edge.
			low[start] = low[nex.v]
		}
		if nexFoundEnd && low[nex.v] > low[start] && foundEnd {
			// this is a bridge from s to t.
			bridges = append(bridges, nex.r)
		}
	}

	if start == end {
		return true
	}
	return foundEnd
}

package main

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

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

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

func Dprint(format string, a ...interface{}) {
	if Debug {
		fmt.Fprintf(os.Stderr, format+"\n", a...)
	}
}

type road struct {
	w   int // weight
	idx int // index of the road (starting from 0)
}

var disRoad *road

func (r *road) String() string {
	return fmt.Sprintf("<%d:%d>", r.idx+1, r.w)
}

type roadPtr struct {
	v int
	r *road
}

var (
	n, m    int
	s, t    int
	adj     [][]roadPtr
	visited []bool
)

func main() {
	defer writer.Flush()

	// Read inpput.
	Scanf("%d %d\n", &n, &m)
	Scanf("%d %d\n", &s, &t)
	s--
	t--

	adj = make([][]roadPtr, n)
	for i := 0; i < m; i++ {
		var xi, yi, wi int
		Scanf("%d %d %d\n", &xi, &yi, &wi)
		xi--
		yi--
		// TODO(stefan): idx: i+1?
		r := road{w: wi, idx: i}
		adj[xi] = append(adj[xi], roadPtr{yi, &r})
		adj[yi] = append(adj[yi], roadPtr{xi, &r})
	}

	// Run DFS(s, t) and try to remove each of the roads. Then run bridge-finding algo.
	visited = make([]bool, n)
	roads := dfs(s, t)

	if len(roads) == 0 {
		// They are already disconnected.
		Printf("0\n\n")
		return
	}

	Dprint("%v", roads)
	runBridges(roads)

	if len(minRoads) == 0 {
		// Impossible to disconnect.
		Printf("-1")
		return
	}

	Printf("%d\n%d\n", minCost, len(minRoads))
	for _, r := range minRoads {
		Printf("%d ", r.idx+1)
	}
}

// run dfs from start, where previous node is prev and terminating at end.
// It will return list of roads from start to end including both.
// If we can't get from start to end, it will return empty list.
func dfs(start int, end int) []*road {
	visited[start] = true

	for _, nex := range adj[start] {
		if visited[nex.v] {
			// We are only interested in unvisited nodes.
			continue
		}

		if nex.v == end {
			visited[nex.v] = true
			return []*road{nex.r}
		}

		roads := dfs(nex.v, end)
		if len(roads) > 0 {
			return append([]*road{nex.r}, roads...)
		}
	}

	return nil
}

var (
	time    int
	times   []int
	low     []int
	bridges []*road

	minCost  = math.MaxInt32
	minRoads []*road
)

func runBridges(roads []*road) {
	// For every path in paths, try to disable this path and run dfs2 that finds bridges.
	for _, r := range roads {
		// Initialize dfs stuff.
		time = 1
		times = make([]int, n)
		low = make([]int, n)
		for i := 0; i < n; i++ {
			low[i] = math.MaxInt32
		}
		bridges = nil

		disRoad = r
		connected := findBridges(s, nil, t)
		Dprint("road=%v, bridges=%v, low=%v", r, bridges, low)

		if connected {
			// s and t are still connected after removing road.
			for _, br := range bridges {
				cost := disRoad.w + br.w
				if cost < minCost {
					minCost = cost
					minRoads = []*road{r, br}
				}
			}
		} else {
			// s and t are disconnected.
			cost := disRoad.w
			if cost < minCost {
				minCost = cost
				minRoads = []*road{r}
			}
		}
	}
}

// findBridges returns list of bridges, that is edges which removal would cause s and t to disconnect.
// It returns true if end is reachable from start. There should be only one way that contains all the bridges.
func findBridges(start int, prev *road, end int) bool {
	times[start] = time
	low[start] = time
	time++
	foundEnd := false
	for _, nex := range adj[start] {
		// Dprint(" >> dfs(%d) analyze %v", start+1, nex.r)
		if nex.r == disRoad {
			// This road was explicitly disabled in runBridges.
			continue
		}
		if nex.r == prev {
			// Do not look at parent road in DFS tree.
			continue
		}
		nexFoundEnd := false
		// Dprint("   >> not disabled and not prev")
		if times[nex.v] == 0 {
			// Dprint("     >> running DFS")
			// We haven't visited nex.v, so visit it.
			nexFoundEnd = findBridges(nex.v, nex.r, end)
			foundEnd = foundEnd || nexFoundEnd
		}
		if low[nex.v] < low[start] {
			// Update low either from child or back edge.
			low[start] = low[nex.v]
		}
		if nexFoundEnd && low[nex.v] > low[start] && foundEnd {
			// this is a bridge from s to t.
			bridges = append(bridges, nex.r)
		}
	}

	if start == end {
		return true
	}
	return foundEnd
}

package main

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

type edge struct {
	to, cost, id int
}

type graph [][]edge

const inf = 1 << 30

var path []int

func findPath(g graph, cur, t int, visited []bool) bool {
	if cur == t {
		return true
	}
	if visited[cur] {
		return false
	}
	visited[cur] = true
	for _, e := range g[cur] {
		if findPath(g, e.to, t, visited) {
			path = append(path, e.id)
			return true
		}
	}
	return false
}

var curTime = 0

func findCheapestBridge(g graph, cur, pi, target, forb int, tin, fup, cost []int) (bool, int) {
	tin[cur] = curTime
	fup[cur] = curTime
	curTime++

	can := cur == target
	idBest := -1
	for _, e := range g[cur] {
		if e.id == forb || e.id == pi {
			continue
		}
		if tin[e.to] != 0 {
			fup[cur] = min(fup[cur], tin[e.to])
		} else {
			c, id := findCheapestBridge(g, e.to, e.id, target, forb, tin, fup, cost)
			if id != -1 {
				if idBest == -1 || cost[id] < cost[idBest] {
					idBest = id
				}
			}
			if fup[e.to] > tin[cur] && c {
				if idBest == -1 || e.cost < cost[idBest] {
					idBest = e.id
				}
			}
			can = can || c

			fup[cur] = min(fup[cur], fup[e.to])
		}
	}

	return can, idBest
}

func main() {
	cin := NewReader()

	n := cin.NextInt()
	m := cin.NextInt()
	s := cin.NextInt() - 1
	t := cin.NextInt() - 1

	g := make(graph, n)
	cost := make([]int, m)
	for i := 0; i < m; i++ {
		u := cin.NextInt() - 1
		v := cin.NextInt() - 1
		c := cin.NextInt()
		g[u] = append(g[u], edge{v, c, i})
		g[v] = append(g[v], edge{u, c, i})
		cost[i] = c
	}

	visited := make([]bool, n)
	path = []int{}

	if !findPath(g, s, t, visited) {
		fmt.Printf("0\n0\n")
		return
	}

	best := inf
	ans := []int{}

	tin := make([]int, n)
	fup := make([]int, n)

	for _, id := range path {
		for i := 0; i < n; i++ {
			tin[i] = 0
			fup[i] = 0
		}

		curTime = 1
		_, c := findCheapestBridge(g, s, -1, t, id, tin, fup, cost)

		if c == -1 {
			if tin[t] == 0 && cost[id] < best {
				best = cost[id]
				ans = []int{id}
			}
		} else {
			if cost[id]+cost[c] < best {
				best = cost[id] + cost[c]
				ans = []int{id, c}
			}
		}
	}

	if len(ans) > 0 {
		fmt.Printf("%d\n%d\n", best, len(ans))
		for _, it := range ans {
			fmt.Printf("%d ", it+1)
		}
		fmt.Println()
	} else {
		fmt.Printf("-1\n")
	}
}

// Reader is a fast input for programming competitions
type Reader struct {
	scanner *bufio.Scanner
}

// NewReader ...
func NewReader() *Reader {
	var reader Reader
	reader.scanner = bufio.NewScanner(os.Stdin)
	reader.scanner.Split(bufio.ScanWords)
	return &reader
}

// NextInt ...
func (r *Reader) NextInt() int {
	r.scanner.Scan()
	res, _ := strconv.Atoi(r.scanner.Text())
	return res
}

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

package main

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

type edge struct {
	to, cost, id int
}

type graph [][]edge

const inf = 1 << 30

var path []int

func findPath(g graph, cur, t int, visited []bool) bool {
	if cur == t {
		return true
	}
	if visited[cur] {
		return false
	}
	visited[cur] = true
	for _, e := range g[cur] {
		if findPath(g, e.to, t, visited) {
			path = append(path, e.id)
			return true
		}
	}
	return false
}

var curTime = 0

func findCheapestBridge(g graph, cur, pi, target, forb int, tin, fup []int, cost []int) (bool, int) {
	tin[cur] = curTime
	fup[cur] = curTime
	curTime++

	can := cur == target
	idBest := -1
	for _, e := range g[cur] {
		if e.id == forb || e.id == pi {
			continue
		}
		if tin[e.to] != 0 {
			fup[cur] = min(fup[cur], tin[e.to])
		} else {
			c, id := findCheapestBridge(g, e.to, e.id, target, forb, tin, fup, cost)
			if id != -1 {
				if idBest == -1 || cost[id] < cost[idBest] {
					idBest = id
				}
			}
			if fup[e.to] > tin[cur] && c {
				if idBest == -1 || e.cost < cost[idBest] {
					idBest = e.id
				}
			}
			can = can || c

			fup[cur] = min(fup[cur], fup[e.to])
		}
	}

	return can, idBest
}

func main() {
	cin := NewReader()

	n := cin.NextInt()
	m := cin.NextInt()
	s := cin.NextInt() - 1
	t := cin.NextInt() - 1

	g := make(graph, n)
	cost := make([]int, m)
	for i := 0; i < m; i++ {
		u := cin.NextInt() - 1
		v := cin.NextInt() - 1
		c := cin.NextInt()
		g[u] = append(g[u], edge{v, c, i})
		g[v] = append(g[v], edge{u, c, i})
		cost[i] = c
	}

	visited := make([]bool, n)
	path = []int{}

	if !findPath(g, s, t, visited) {
		fmt.Printf("0\n0\n")
		return
	}

	best := inf
	ans := []int{}

	tin := make([]int, n)
	fup := make([]int, n)

	for _, id := range path {
		for i := 0; i < n; i++ {
			tin[i] = 0
			fup[i] = 0
		}

		curTime = 1
		found, c := findCheapestBridge(g, s, -1, t, id, tin, fup, cost)
		if found && c != -1 {
			if cost[id]+cost[c] < best {
				best = cost[id] + cost[c]
				ans = []int{id, c}
			}
		} else {
			if !found && cost[id] < best {
				best = cost[id]
				ans = []int{id}
			}
		}
	}

	if len(ans) > 0 {
		fmt.Printf("%d\n%d\n", best, len(ans))
		for _, it := range ans {
			fmt.Printf("%d ", it+1)
		}
		fmt.Println()
	} else {
		fmt.Printf("-1\n")
	}
}

// Reader is a fast input for programming competitions
type Reader struct {
	scanner *bufio.Scanner
}

// NewReader ...
func NewReader() *Reader {
	var reader Reader
	reader.scanner = bufio.NewScanner(os.Stdin)
	reader.scanner.Split(bufio.ScanWords)
	return &reader
}

// NextInt ...
func (r *Reader) NextInt() int {
	r.scanner.Scan()
	res, _ := strconv.Atoi(r.scanner.Text())
	return res
}

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

package main

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

type edge struct {
	to, cost, id int
}

type graph [][]edge

const inf = 1 << 30

var path []int

func findPath(g graph, cur, t int, visited []bool) bool {
	if cur == t {
		return true
	}
	if visited[cur] {
		return false
	}
	visited[cur] = true
	for _, e := range g[cur] {
		if !visited[e.to] && findPath(g, e.to, t, visited) {
			path = append(path, e.id)
			return true
		}
	}
	return false
}

var curTime = 0

func findCheapestBridge(g graph, cur, pi, target, forb int, tin, fup []int, cost []int) (bool, int) {
	tin[cur] = curTime
	fup[cur] = curTime
	curTime++

	can := cur == target
	idBest := -1
	for _, e := range g[cur] {
		if e.id == forb || e.id == pi {
			continue
		}
		if tin[e.to] != 0 {
			fup[cur] = min(fup[cur], tin[e.to])
		} else {
			c, id := findCheapestBridge(g, e.to, e.id, target, forb, tin, fup, cost)
			if id != -1 {
				if idBest == -1 || cost[id] < cost[idBest] {
					idBest = id
				}
			}
			if fup[e.to] > tin[cur] && c {
				if idBest == -1 || e.cost < cost[idBest] {
					idBest = e.id
				}
			}
			can = can || c

			fup[cur] = min(fup[cur], fup[e.to])
		}
	}

	return can, idBest
}

func main() {
	cin := NewReader()

	n := cin.NextInt()
	m := cin.NextInt()
	s := cin.NextInt() - 1
	t := cin.NextInt() - 1

	g := make(graph, n)
	cost := make([]int, m)
	for i := 0; i < m; i++ {
		u := cin.NextInt() - 1
		v := cin.NextInt() - 1
		c := cin.NextInt()
		g[u] = append(g[u], edge{v, c, i})
		g[v] = append(g[v], edge{u, c, i})
		cost[i] = c
	}

	visited := make([]bool, n)
	path = []int{}

	if !findPath(g, s, t, visited) {
		fmt.Printf("0\n0\n")
		return
	}

	best := inf
	ans := []int{}

	tin := make([]int, n)
	fup := make([]int, n)

	for _, id := range path {
		for i := 0; i < n; i++ {
			tin[i] = 0
			fup[i] = 0
		}

		curTime = 1
		found, c := findCheapestBridge(g, s, -1, t, id, tin, fup, cost)
		if found && c != -1 {
			if cost[id]+cost[c] < best {
				best = cost[id] + cost[c]
				ans = []int{id, c}
			}
		} else {
			if !found && cost[id] < best {
				best = cost[id]
				ans = []int{id}
			}
		}
	}

	if len(ans) > 0 {
		fmt.Printf("%d\n%d\n", best, len(ans))
		for _, it := range ans {
			fmt.Printf("%d ", it+1)
		}
		fmt.Println()
	} else {
		fmt.Printf("-1\n")
	}
}

// Reader is a fast input for programming competitions
type Reader struct {
	scanner *bufio.Scanner
}

// NewReader ...
func NewReader() *Reader {
	var reader Reader
	reader.scanner = bufio.NewScanner(os.Stdin)
	reader.scanner.Split(bufio.ScanWords)
	return &reader
}

// NextInt ...
func (r *Reader) NextInt() int {
	r.scanner.Scan()
	res, _ := strconv.Atoi(r.scanner.Text())
	return res
}

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

package main

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

type edge struct {
	to, cost, id int
}

type graph [][]edge

const inf = 1 << 30

var path []int

func findPath(g graph, cur, t int, visited []bool) bool {
	if cur == t {
		return true
	}
	if visited[cur] {
		return false
	}
	visited[cur] = true
	for _, e := range g[cur] {
		if findPath(g, e.to, t, visited) {
			path = append(path, e.id)
			return true
		}
	}
	return false
}

var curTime = 0

func findCheapestBridge(g graph, cur, pi, target, forb int, tin, fup, cost []int) (bool, int) {
	tin[cur] = curTime
	fup[cur] = curTime
	curTime++

	can := cur == target
	idBest := -1
	for _, e := range g[cur] {
		if e.id == forb || e.id == pi {
			continue
		}
		if tin[e.to] != 0 {
			fup[cur] = min(fup[cur], tin[e.to])
		} else {
			c, id := findCheapestBridge(g, e.to, e.id, target, forb, tin, fup, cost)
			if id != -1 {
				if idBest == -1 || cost[id] < cost[idBest] {
					idBest = id
				}
			}
			if fup[e.to] > tin[cur] && c {
				if idBest == -1 || e.cost < cost[idBest] {
					idBest = e.id
				}
			}
			can = can || c

			fup[cur] = min(fup[cur], fup[e.to])
		}
	}

	return can, idBest
}

func main() {
	cin := NewReader()

	n := cin.NextInt()
	m := cin.NextInt()
	s := cin.NextInt() - 1
	t := cin.NextInt() - 1

	g := make(graph, n)
	cost := make([]int, m)
	for i := 0; i < m; i++ {
		u := cin.NextInt() - 1
		v := cin.NextInt() - 1
		c := cin.NextInt()
		g[u] = append(g[u], edge{v, c, i})
		g[v] = append(g[v], edge{u, c, i})
		cost[i] = c
	}

	visited := make([]bool, n)
	path = []int{}

	if !findPath(g, s, t, visited) {
		fmt.Printf("0\n0\n")
		return
	}

	best := inf
	ans := []int{}

	tin := make([]int, n)
	fup := make([]int, n)

	for _, id := range path {
		for i := 0; i < n; i++ {
			tin[i] = 0
			fup[i] = 0
		}

		curTime = 1
		found, c := findCheapestBridge(g, s, -1, t, id, tin, fup, cost)

		if c == -1 {
			if !found && cost[id] < best {
				best = cost[id]
				ans = []int{id}
			}
		} else {
			if cost[id]+cost[c] < best {
				best = cost[id] + cost[c]
				ans = []int{id, c}
			}
		}
	}

	if len(ans) > 0 {
		fmt.Printf("%d\n%d\n", best, len(ans))
		for _, it := range ans {
			fmt.Printf("%d ", it+1)
		}
		fmt.Println()
	} else {
		fmt.Printf("-1\n")
	}
}

// Reader is a fast input for programming competitions
type Reader struct {
	scanner *bufio.Scanner
}

// NewReader ...
func NewReader() *Reader {
	var reader Reader
	reader.scanner = bufio.NewScanner(os.Stdin)
	reader.scanner.Split(bufio.ScanWords)
	return &reader
}

// NextInt ...
func (r *Reader) NextInt() int {
	r.scanner.Scan()
	res, _ := strconv.Atoi(r.scanner.Text())
	return res
}

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

package main

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

type edge struct {
	to, cost, id int
}

type graph [][]edge

const inf = 1 << 30

func findPath(g graph, cur, t int, visited []bool) bool {
	if cur == t {
		return true
	}
	if visited[cur] {
		return false
	}
	visited[cur] = true
	for _, e := range g[cur] {
		if !visited[e.to] && findPath(g, e.to, t, visited) {
			return true
		}
	}
	return false
}

var curTime = 0

func findCheapestBridge(g graph, cur, pi, target, forb int, tin, fup, visited, cost []int) (bool, int) {
	visited[cur] = 1
	tin[cur] = curTime
	fup[cur] = curTime
	curTime++

	can := cur == target
	idBest := -1
	for _, e := range g[cur] {
		if e.id == forb || e.id == pi {
			continue
		}
		if tin[e.to] != 0 {
			fup[cur] = min(fup[cur], tin[e.to])
		} else {
			c, id := findCheapestBridge(g, e.to, e.id, target, forb, tin, fup, visited, cost)
			if id != -1 {
				if idBest == -1 || cost[id] < cost[idBest] {
					idBest = id
				}
			}
			if fup[e.to] > tin[cur] && c {
				if idBest == -1 || e.cost < cost[idBest] {
					idBest = e.id
				}
			}
			can = can || c

			fup[cur] = min(fup[cur], fup[e.to])
		}
	}

	return can, idBest
}

func main() {
	cin := NewReader()

	n := cin.NextInt()
	m := cin.NextInt()
	s := cin.NextInt() - 1
	t := cin.NextInt() - 1

	g := make(graph, n)
	cost := make([]int, m)
	for i := 0; i < m; i++ {
		u := cin.NextInt() - 1
		v := cin.NextInt() - 1
		c := cin.NextInt()
		g[u] = append(g[u], edge{v, c, i})
		g[v] = append(g[v], edge{u, c, i})
		cost[i] = c
	}

	visited := make([]bool, n)

	if !findPath(g, s, t, visited) {
		fmt.Printf("0\n0\n")
		return
	}

	best := inf
	ans := []int{}

	if m < 3000 {
		for i := 0; i < m; i++ {
			tin := make([]int, n)
			fup := make([]int, n)
			visited := make([]int, n)
			curTime = 1
			found, c := findCheapestBridge(g, s, -1, t, i, tin, fup, visited, cost)
			if found && c != -1 {
				if cost[i]+cost[c] < best {
					best = cost[i] + cost[c]
					ans = []int{i, c}
				}
			} else {
				if !found && cost[i] < best {
					best = cost[i]
					ans = []int{i}
				}
			}
		}
	}

	if len(ans) > 0 {
		fmt.Printf("%d\n%d\n", best, len(ans))
		for _, it := range ans {
			fmt.Printf("%d ", it+1)
		}
		fmt.Println()
	} else {
		fmt.Printf("-1\n")
	}
}

// Reader is a fast input for programming competitions
type Reader struct {
	scanner *bufio.Scanner
}

// NewReader ...
func NewReader() *Reader {
	var reader Reader
	reader.scanner = bufio.NewScanner(os.Stdin)
	reader.scanner.Split(bufio.ScanWords)
	return &reader
}

// NextInt ...
func (r *Reader) NextInt() int {
	r.scanner.Scan()
	res, _ := strconv.Atoi(r.scanner.Text())
	return res
}

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

package main

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

type edge struct {
	to, cost, id int
}

type graph [][]edge

const inf = 1 << 30

var path []int

func findPath(g graph, cur, target int, visited []bool) bool {
	if cur == target {
		return true
	}
	if visited[cur] {
		return false
	}
	visited[cur] = true
	for _, e := range g[cur] {
		if findPath(g, e.to, target, visited) {
			path = append(path, e.id)
			return true
		}
	}
	return false
}

var curTime = 0

func findCheapestBridge(g graph, cur, pi, target, forb int, tin, fup, cost []int) (bool, int) {
	tin[cur] = curTime
	fup[cur] = curTime
	curTime++

	can := cur == target
	idBest := -1
	for _, e := range g[cur] {
		if e.id == forb || e.id == pi {
			continue
		}
		if tin[e.to] != 0 {
			fup[cur] = min(fup[cur], tin[e.to])
		} else {
			c, id := findCheapestBridge(g, e.to, e.id, target, forb, tin, fup, cost)
			if id != -1 {
				if idBest == -1 || cost[id] < cost[idBest] {
					idBest = id
				}
			}
			if fup[e.to] == tin[e.to] && c {
				if idBest == -1 || e.cost < cost[idBest] {
					idBest = e.id
				}
			}
			can = can || c

			fup[cur] = min(fup[cur], fup[e.to])
		}
	}

	return can, idBest
}

func main() {
	cin := NewReader()

	n := cin.NextInt()
	m := cin.NextInt()
	s := cin.NextInt() - 1
	t := cin.NextInt() - 1

	g := make(graph, n)
	cost := make([]int, m)
	for i := 0; i < m; i++ {
		u := cin.NextInt() - 1
		v := cin.NextInt() - 1
		c := cin.NextInt()
		g[u] = append(g[u], edge{v, c, i})
		g[v] = append(g[v], edge{u, c, i})
		cost[i] = c
	}

	visited := make([]bool, n)
	path = []int{}

	if !findPath(g, s, t, visited) {
		fmt.Printf("0\n0\n")
		return
	}

	best := inf
	ans := []int{}

	tin := make([]int, n)
	fup := make([]int, n)

	for _, id := range path {
		for i := 0; i < n; i++ {
			tin[i] = 0
			fup[i] = 0
		}

		curTime = 1
		found, c := findCheapestBridge(g, s, -1, t, id, tin, fup, cost)

		if c == -1 {
			if !found && cost[id] < best {
				best = cost[id]
				ans = []int{id}
			}
		} else {
			if cost[id]+cost[c] < best {
				best = cost[id] + cost[c]
				ans = []int{id, c}
			}
		}
	}

	if len(ans) > 0 {
		fmt.Printf("%d\n%d\n", best, len(ans))
		for _, it := range ans {
			fmt.Printf("%d ", it+1)
		}
		fmt.Println()
	} else {
		fmt.Printf("-1\n")
	}
}

// Reader is a fast input for programming competitions
type Reader struct {
	scanner *bufio.Scanner
}

// NewReader ...
func NewReader() *Reader {
	var reader Reader
	reader.scanner = bufio.NewScanner(os.Stdin)
	reader.scanner.Split(bufio.ScanWords)
	return &reader
}

// NextInt ...
func (r *Reader) NextInt() int {
	r.scanner.Scan()
	res, _ := strconv.Atoi(r.scanner.Text())
	return res
}

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