Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read exactly $$$m$$$ books before all entertainments. Alice and Bob will read each book together to end this exercise faster.There are $$$n$$$ books in the family library. The $$$i$$$-th book is described by three integers: $$$t_i$$$ — the amount of time Alice and Bob need to spend to read it, $$$a_i$$$ (equals $$$1$$$ if Alice likes the $$$i$$$-th book and $$$0$$$ if not), and $$$b_i$$$ (equals $$$1$$$ if Bob likes the $$$i$$$-th book and $$$0$$$ if not).So they need to choose exactly $$$m$$$ books from the given $$$n$$$ books in such a way that:  Alice likes at least $$$k$$$ books from the chosen set and Bob likes at least $$$k$$$ books from the chosen set;  the total reading time of these $$$m$$$ books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $$$t_i$$$ over all books that are in the chosen set.Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.

The first line of the input contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$1 \le k \le m \le n \le 2 \cdot 10^5$$$). The next $$$n$$$ lines contain descriptions of books, one description per line: the $$$i$$$-th line contains three integers $$$t_i$$$, $$$a_i$$$ and $$$b_i$$$ ($$$1 \le t_i \le 10^4$$$, $$$0 \le a_i, b_i \le 1$$$), where:   $$$t_i$$$ — the amount of time required for reading the $$$i$$$-th book;  $$$a_i$$$ equals $$$1$$$ if Alice likes the $$$i$$$-th book and $$$0$$$ otherwise;  $$$b_i$$$ equals $$$1$$$ if Bob likes the $$$i$$$-th book and $$$0$$$ otherwise.

If there is no solution, print only one integer -1. If the solution exists, print $$$T$$$ in the first line — the minimum total reading time of the suitable set of books. In the second line print $$$m$$$ distinct integers from $$$1$$$ to $$$n$$$ in any order — indices of books which are in the set you found. If there are several answers, print any of them.

package main

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

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

	tc := 1

	for tc > 0 {
		tc--
		n, m, k := readThreeNums(reader)
		books := make([][]int, n)
		for i := 0; i < n; i++ {
			books[i] = readNNums(reader, 3)
		}

		tot, ans := solve(n, m, k, books)
		fmt.Println(tot)
		if tot > 0 {
			var buf bytes.Buffer
			for i := 0; i < m; i++ {
				buf.WriteString(fmt.Sprintf("%d ", ans[i]))
			}
			fmt.Println(buf.String())
		}
	}
}

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func solve(n, m, k int, arr [][]int) (int64, []int) {
	times := make([][]Pair, 4)

	for i := 0; i < 4; i++ {
		times[i] = make([]Pair, 0, n)
	}

	for i, book := range arr {
		t, a, b := book[0], book[1], book[2]
		times[a*2+b] = append(times[a*2+b], Pair{t, i})
	}

	sums := make([][]int64, 4)

	for i := 0; i < 4; i++ {
		sort.Sort(Pairs(times[i]))
		sums[i] = make([]int64, 0, len(times[i])+1)
		var cur int64
		sums[i] = append(sums[i], cur)
		for j := 0; j < len(times[i]); j++ {
			cur += int64(times[i][j].first)
			sums[i] = append(sums[i], cur)
		}
	}

	var ans int64 = math.MaxInt64
	var pos = math.MaxInt32

	var st *Node
	for iter := 0; iter < 2; iter++ {
		//first iter find the best, second find the result corresponding to best
		st = nil
		var fr *Node
		var sum int64 = 0
		var start int

		for k-start >= len(sums[1]) || k-start >= len(sums[2]) || m-start-(k-start)*2 < 0 {
			start++
		}
		if start >= len(sums[3]) {
			return -1, nil
		}

		need := m - start - (k-start)*2

		for i := 0; i < 3; i++ {
			var x int
			if i != 0 {
				x = k - start
			}
			for p := len(times[i]) - 1; p >= x; p-- {
				fr = Insert(fr, times[i][p])
			}
		}
		st, fr = update(st, fr, &sum, need)
		var x = len(sums[3])
		if iter == 1 {
			x = pos
		}
		for cnt := start; cnt < x; cnt++ {
			if k-cnt >= 0 {
				if cnt+(k-cnt)*2+st.Size() == m {
					tmp := sums[3][cnt] + sums[1][k-cnt] + sums[2][k-cnt] + sum
					if tmp < ans {
						ans = tmp
						pos = cnt + 1
					}
				}
			} else {
				if cnt+st.Size() == m {
					tmp := sums[3][cnt] + sum
					if tmp < ans {
						ans = tmp
						pos = cnt + 1
					}
				}
			}
			if iter == 1 && cnt+1 == pos {
				break
			}
			need--
			if k-cnt > 0 {
				need += 2
				fr = Insert(fr, times[1][k-cnt-1])
				fr = Insert(fr, times[2][k-cnt-1])
			}
			st, fr = update(st, fr, &sum, need)
		}
	}

	res := make([]int, 0, m)

	for i := 0; i+1 < pos; i++ {
		res = append(res, times[3][i].second+1)
	}

	for i := 0; i <= k-pos; i++ {
		res = append(res, times[1][i].second+1)
		res = append(res, times[2][i].second+1)
	}

	Visit(st, func(item Pair) {
		res = append(res, item.second+1)
	})

	return ans, res
}

func update(st *Node, fr *Node, sum *int64, need int) (*Node, *Node) {
	need = max(0, need)
	ok := true
	for ok {
		ok = false
		for st.Size() > need {
			x := MaxValueNode(st)
			*sum -= int64(x.item.first)
			st = Delete(st, x.item)
			fr = Insert(fr, x.item)
			ok = true
		}

		for st.Size() < need && fr.Size() > 0 {
			x := MinValueNode(fr)
			*sum += int64(x.item.first)
			fr = Delete(fr, x.item)
			st = Insert(st, x.item)
			ok = true
		}

		for st.Size() > 0 && fr.Size() > 0 {
			x := MinValueNode(fr)
			y := MaxValueNode(st)
			if !x.item.Less(y.item) {
				break
			}
			*sum += int64(x.item.first)
			*sum -= int64(y.item.first)
			fr = Delete(fr, x.item)
			fr = Insert(fr, y.item)
			st = Delete(st, y.item)
			st = Insert(st, x.item)
			ok = true
		}
	}

	return st, fr
}

type Pair struct {
	first, second int
}

func (this Pair) Less(that Pair) bool {
	if this.first < that.first {
		return true
	}
	if this.first == that.first && (this.second < that.second) {
		return true
	}
	return false
}

type Pairs []Pair

func (ps Pairs) Len() int {
	return len(ps)
}

func (ps Pairs) Less(i, j int) bool {
	return ps[i].Less(ps[j])
}

func (ps Pairs) Swap(i, j int) {
	ps[i], ps[j] = ps[j], ps[i]
}

/**
* this is a AVL tree
 */
type Node struct {
	item        Pair
	height      int
	size        int
	left, right *Node
}

func (node *Node) Height() int {
	if node == nil {
		return 0
	}
	return node.height
}

func (node *Node) Size() int {
	if node == nil {
		return 0
	}
	return node.size
}

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

func NewNode(item Pair) *Node {
	node := new(Node)
	node.item = item
	node.height = 1
	node.size = 1
	return node
}

func rightRotate(y *Node) *Node {
	x := y.left

	t2 := x.right

	x.right = y
	y.left = t2
	y.height = max(y.left.Height(), y.right.Height()) + 1
	y.size = y.left.Size() + y.right.Size() + 1
	x.height = max(x.left.Height(), x.right.Height()) + 1
	x.size = x.left.Size() + x.right.Size() + 1

	return x
}

func leftRotate(x *Node) *Node {
	y := x.right
	t2 := y.left

	y.left = x
	x.right = t2

	x.height = max(x.left.Height(), x.right.Height()) + 1
	x.size = x.left.Size() + x.right.Size() + 1
	y.height = max(y.left.Height(), y.right.Height()) + 1
	y.size = y.left.Size() + y.right.Size() + 1

	return y
}

func (node *Node) GetBalance() int {
	if node == nil {
		return 0
	}
	return node.left.Height() - node.right.Height()
}

func FindEqualOrGreater(node *Node, item Pair) *Node {
	if node == nil {
		return nil
	}
	if node.item.Less(item) {
		return FindEqualOrGreater(node.right, item)
	}

	res := FindEqualOrGreater(node.left, item)
	if res == nil {
		return node
	}
	return res
}

func FindEqualOrLess(node *Node, item Pair) *Node {
	if node == nil {
		return nil
	}
	if item.Less(node.item) {
		return FindEqualOrLess(node.left, item)
	}

	res := FindEqualOrLess(node.right, item)
	if res == nil {
		return node
	}
	return res
}

func Insert(node *Node, item Pair) *Node {
	if node == nil {
		return NewNode(item)
	}

	if item.Less(node.item) {
		node.left = Insert(node.left, item)
	} else {
		node.right = Insert(node.right, item)
	}

	node.height = max(node.left.Height(), node.right.Height()) + 1
	node.size = node.left.Size() + node.right.Size() + 1
	balance := node.GetBalance()

	if balance > 1 && item.Less(node.left.item) {
		return rightRotate(node)
	}

	if balance < -1 && node.right.item.Less(item) {
		return leftRotate(node)
	}

	if balance > 1 && node.left.item.Less(item) {
		node.left = leftRotate(node.left)
		return rightRotate(node)
	}

	if balance < -1 && item.Less(node.right.item) {
		node.right = rightRotate(node.right)
		return leftRotate(node)
	}

	return node
}

func MinValueNode(root *Node) *Node {
	cur := root

	for cur.left != nil {
		cur = cur.left
	}

	return cur
}

func MaxValueNode(root *Node) *Node {
	cur := root

	for cur.right != nil {
		cur = cur.right
	}
	return cur
}

func Visit(node *Node, fn func(item Pair)) {
	if node == nil {
		return
	}
	fn(node.item)
	Visit(node.left, fn)
	Visit(node.right, fn)
}

func Delete(root *Node, item Pair) *Node {
	if root == nil {
		return nil
	}

	if item.Less(root.item) {
		root.left = Delete(root.left, item)
	} else if root.item.Less(item) {
		root.right = Delete(root.right, item)
	} else {
		if root.left == nil || root.right == nil {
			tmp := root.left
			if root.left == nil {
				tmp = root.right
			}
			root = tmp
		} else {
			tmp := MinValueNode(root.right)

			root.item = tmp.item
			// make sure tmp node deleted after call delete on root.right
			root.right = Delete(root.right, tmp.item)
		}
	}

	if root == nil {
		return root
	}

	root.height = max(root.left.Height(), root.right.Height()) + 1
	root.size = root.left.Size() + root.right.Size() + 1

	balance := root.GetBalance()

	if balance > 1 && root.left.GetBalance() >= 0 {
		return rightRotate(root)
	}

	if balance > 1 && root.left.GetBalance() < 0 {
		root.left = leftRotate(root.left)
		return rightRotate(root)
	}

	if balance < -1 && root.right.GetBalance() <= 0 {
		return leftRotate(root)
	}

	if balance < -1 && root.right.GetBalance() > 0 {
		root.right = rightRotate(root.right)
		return leftRotate(root)
	}

	return root
}

package main

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

const MAX int64 = int64(1 << 60)

type data struct {
	t, i int
	s    int64
}

type Data []data

func (p Data) Len() int           { return len(p) }
func (p Data) Less(i, j int) bool { return p[i].t < p[j].t }
func (p Data) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }
func (p Data) IndexValueS(i int) int64 {
	if i < 0 || i >= len(p) {
		return 0
	}

	return p[i].s
}

func (p Data) IndexValueT(i int) int {
	if i < 0 || i >= len(p) {
		return 1 << 30
	}

	return p[i].t
}

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

	var n, m, k int
	datas := make([]Data, 4)

	fmt.Fscanf(reader, "%d %d %d\n", &n, &m, &k)

	for i := 0; i < 4; i++ {
		datas[i] = make(Data, 0, n)
	}

	var t, a, b int
	for i := 1; i <= n; i++ {
		fmt.Fscanf(reader, "%d %d %d\n", &t, &a, &b)

		datas[b<<1+a] = append(datas[b<<1+a], data{t: t, i: i, s: int64(t)})
	}

	// if len(aa)+len(cc) < k || len(bb)+len(cc) < k ||
	// 	2*k-len(cc) > m {
	// 	fmt.Println(-1)
	// 	return
	// }

	sort.Sort(datas[0])
	sort.Sort(datas[1])
	sort.Sort(datas[2])
	sort.Sort(datas[3])

	for _, d := range datas {
		for j := 1; j < len(d); j++ {
			d[j].s += d[j-1].s
		}
	}

	sum := MAX
	indexs := make([]int, 4)

	f := func(cci int) int64 {
		returnSum := MAX

		nl := max(k-cci, 0)
		if nl > len(datas[1]) || nl > len(datas[2]) || cci+nl+nl > m {
			return returnSum
		}

		tempSum := datas[3].IndexValueS(cci-1) + datas[1].IndexValueS(nl-1) + datas[2].IndexValueS(nl-1)

		aai, bbi, ddi := nl, nl, 0

		for mm := m - (cci + nl + nl); mm > 0; mm-- {
			value, index := minIndex(datas[0].IndexValueT(ddi), datas[1].IndexValueT(aai), datas[2].IndexValueT(bbi))
			switch index {
			case 0:
				tempSum += value
				ddi++
			case 1:
				tempSum += value
				aai++
			case 2:
				tempSum += value
				bbi++
			}
		}

		if tempSum < sum {
			sum = tempSum
			indexs[0] = ddi
			indexs[1] = aai
			indexs[2] = bbi
			indexs[3] = cci
		}

		if tempSum < returnSum {
			returnSum = tempSum
		}

		return returnSum
	}

	left, right := 0, min(len(datas[3]), m)

	for left+3 <= right {
		lmid := left + (right-left)/3
		rmid := right - (right-left)/3

		if f(lmid) >= f(rmid) {
			left = lmid
		} else {
			right = rmid
		}
	}

	for ; left <= right; left++ {
		f(left)
	}

	if sum == MAX {
		fmt.Println(-1)
		return
	}

	fmt.Println(sum)

	for i, d := range datas {
		maxIndex := indexs[i]
		for j := 0; j < maxIndex; j++ {
			fmt.Printf("%d ", d[j].i)
		}
	}
}

func minIndex(n0, n1, n2 int) (int64, int) {
	if n0 <= n1 && n0 <= n2 {
		return int64(n0), 0
	}

	if n1 <= n0 && n1 <= n2 {
		return int64(n1), 1
	}

	if n2 <= n0 && n2 <= n1 {
		return int64(n2), 2
	}

	return 0, -1
}

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
}

package main

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

const MAX int64 = int64(1 << 60)

type data struct {
	t, i int
	s    int64
}

type Datas []data

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

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

	var n, m, k int

	fmt.Fscanf(reader, "%d %d %d\n", &n, &m, &k)

	aa := make(Datas, 0, n)
	bb := make(Datas, 0, n)
	cc := make(Datas, 0, n)
	dd := make(Datas, 0, n)

	var t, a, b int
	for i := 1; i <= n; i++ {
		fmt.Fscanf(reader, "%d %d %d\n", &t, &a, &b)

		if a == 1 && b == 1 {
			cc = append(cc, data{t: t, i: i, s: int64(t)})
			continue
		}

		if a == 1 {
			aa = append(aa, data{t: t, i: i, s: int64(t)})
			continue
		}

		if b == 1 {
			bb = append(bb, data{t: t, i: i, s: int64(t)})
			continue
		}

		if a == 0 && b == 0 {
			dd = append(dd, data{t: t, i: i, s: int64(t)})
		}
	}

	if len(aa)+len(cc) < k || len(bb)+len(cc) < k ||
		2*k-len(cc) > m {
		fmt.Println(-1)
		return
	}

	sort.Sort(aa)
	sort.Sort(bb)
	sort.Sort(cc)
	sort.Sort(dd)

	for i := 1; i < len(aa); i++ {
		aa[i].s += aa[i-1].s
	}

	for i := 1; i < len(bb); i++ {
		bb[i].s += bb[i-1].s
	}

	for i := 1; i < len(cc); i++ {
		cc[i].s += cc[i-1].s
	}

	for i := 1; i < len(dd); i++ {
		dd[i].s += dd[i-1].s
	}

	sum := MAX
	ai := 0
	bi := 0
	ci := 0
	di := 0

	// for cci := max(0, m-len(aa)-len(bb)-len(dd)); cci <= len(cc) && cci <= m; cci++ {
	// 	sumCc := int64(0)
	// 	if cci > 0 {
	// 		sumCc = cc[cci-1].s
	// 	}
	// 	if sumCc >= sum {
	// 		break
	// 	}
	// 	for aai := max(max(k-cci, 0), m-cci-len(bb)-len(dd)); aai <= len(aa) && cci+aai <= m; aai++ {
	// 		sumAa := sumCc
	// 		if aai > 0 {
	// 			sumAa += aa[aai-1].s
	// 		}
	// 		if sumAa >= sum {
	// 			break
	// 		}
	// 		for bbi := max(max(k-cci, 0), m-cci-aai-len(dd)); bbi <= len(bb) && cci+aai+bbi <= m; bbi++ {
	// 			tempSum := sumAa
	// 			if bbi > 0 {
	// 				tempSum += bb[bbi-1].s
	// 			}

	// 			if tempSum >= sum {
	// 				break
	// 			}

	// 			ddi := m - (cci + aai + bbi)

	// 			if ddi > 0 {
	// 				tempSum += dd[ddi-1].s
	// 			}

	// 			if tempSum < sum {
	// 				sum = tempSum
	// 				ai = aai
	// 				bi = bbi
	// 				ci = cci
	// 				di = ddi
	// 			}
	// 		}
	// 	}
	// }

	f := func(cci int) int64 {
		returnSum := MAX

		nl := max(k-cci, 0)
		if nl > len(aa) || nl > len(bb) || cci+nl+nl > m {
			return returnSum
		}

		sumCc := int64(0)
		if cci > 0 {
			sumCc = cc[cci-1].s
		}
		if nl > 0 {
			sumCc += (aa[nl-1].s + bb[nl-1].s)
		}
		aai, bbi, ddi := nl, nl, 0

		for mm := m - (cci + nl + nl); mm > 0; mm-- {
			tempAa, tempBb, tempDd := 1<<30, 1<<30, 1<<30
			if aai < len(aa) {
				tempAa = aa[aai].t
			}
			if bbi < len(bb) {
				tempBb = bb[bbi].t
			}

			if ddi < len(dd) {
				tempDd = dd[ddi].t
			}

			switch minIndex(tempAa, tempBb, tempDd) {
			case 1:
				sumCc += int64(tempAa)
				aai++
			case 2:
				sumCc += int64(tempBb)
				bbi++
			case 3:
				sumCc += int64(tempDd)
				ddi++
			}
		}

		if sumCc < sum {
			sum = sumCc
			ai = aai
			bi = bbi
			ci = cci
			di = ddi
		}

		if sumCc < returnSum {
			returnSum = sumCc
		}

		return returnSum
	}

	left, right := 0, len(cc)
	if right > m {
		right = m
	}

	for left+3 <= right {
		lmid := left + (right-left)/3
		rmid := right - (right-left)/3

		if f(lmid) >= f(rmid) {
			left = lmid
		} else {
			right = rmid
		}
	}

	for ; left <= right; left++ {
		f(left)
	}

	if ai+bi+ci+di == 0 {
		fmt.Println(-1)
		return
	}

	fmt.Println(sum)
	for i := 0; i < ai; i++ {
		fmt.Printf("%d ", aa[i].i)
	}

	for i := 0; i < bi; i++ {
		fmt.Printf("%d ", bb[i].i)
	}

	for i := 0; i < ci; i++ {
		fmt.Printf("%d ", cc[i].i)
	}

	for i := 0; i < di; i++ {
		fmt.Printf("%d ", dd[i].i)
	}
}

func minT(index int, d *Datas) int {
	if index < len(*d) {
		return (*d)[index].t
	}

	return 1000000
}

func minIndex(n1, n2, n3 int) int {
	if n1 <= n2 && n1 <= n3 {
		return 1
	}

	if n2 <= n1 && n2 <= n3 {
		return 2
	}

	if n3 <= n1 && n3 <= n2 {
		return 3
	}

	return 0
}

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

	return y
}

package main

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

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

	tc := 1

	for tc > 0 {
		tc--
		n, m, k := readThreeNums(reader)
		books := make([][]int, n)
		for i := 0; i < n; i++ {
			books[i] = readNNums(reader, 3)
		}

		tot, ans := solve(n, m, k, books)
		fmt.Println(tot)
		if tot > 0 {
			var buf bytes.Buffer
			for i := 0; i < m; i++ {
				buf.WriteString(fmt.Sprintf("%d ", ans[i]))
			}
			fmt.Println(buf.String())
		}
	}
}

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func solve(n, m, k int, arr [][]int) (int64, []int) {

	sets := make([][]Book, 4)

	for i := 0; i < 4; i++ {
		sets[i] = make([]Book, 0, n)
	}

	for i := 0; i < n; i++ {
		book := arr[i]
		t, a, b := book[0], book[1], book[2]
		sets[a*2+b] = append(sets[a*2+b], Book{i, t, a, b})
	}

	for i := 0; i < 4; i++ {
		sort.Sort(Books(sets[i]))
	}

	sums := make([][]int64, 4)
	for i := 0; i < 4; i++ {
		sums[i] = make([]int64, 0, len(sets[i])+1)

		var cur int64

		sums[i] = append(sums[i], cur)

		for j := 0; j < len(sets[i]); j++ {
			cur += int64(sets[i][j].time)
			sums[i] = append(sums[i], cur)
		}
	}

	pp := make([]int, 4)
	var best int64 = math.MaxInt64

	for cur := 0; cur < min(k+1, len(sums[3])); cur++ {
		// already choose cur
		i := min(max(0, k-cur), len(sets[1]))
		j := min(max(0, k-cur), len(sets[2]))

		if cur+i < k || cur+j < k {
			continue
		}
		// cur + i >= k && cur + j >= k
		x := m - cur - i - j

		if x < 0 || x > len(sets[0])+len(sets[1])-i+len(sets[2])-j {
			continue
		}
		if x == 0 {
			tmp := sums[3][cur] + sums[1][i] + sums[2][j]
			if tmp < best {
				best = tmp
				pp[0] = 0
				pp[1] = i
				pp[2] = j
				pp[3] = cur
			}
			continue
		}

		a, b, c := find3(sets[0], sets[1][i:], sets[2][j:], x)
		b += i
		c += j

		tmp := sums[0][a] + sums[1][b] + sums[2][c] + sums[3][cur]

		if tmp < best {
			best = tmp
			pp[0] = a
			pp[1] = b
			pp[2] = c
			pp[3] = cur
		}
	}

	if best == math.MaxInt64 {
		return -1, nil
	}

	res := make([]int, 0, m)
	var tot int64
	for i := 0; i < 4; i++ {
		for j := 0; j < pp[i]; j++ {
			res = append(res, sets[i][j].pos+1)
			tot += int64(sets[i][j].time)
		}
	}

	return tot, res
}

func find3(a, b, c []Book, k int) (x int, y int, z int) {
	if len(a) == 0 {
		y, z = find2(b, c, k)
		return
	}
	if len(b) == 0 {
		x, z = find2(a, c, k)
		return
	}
	if len(c) == 0 {
		x, y = find2(a, b, k)
		return
	}

	i, j, p := len(a)/2, len(b)/2, len(c)/2
	h := i + j + p + 3

	if a[i].time <= b[j].time && a[i].time <= c[p].time {
		if h > k {
			if b[j].time <= c[p].time {
				return find3(a, b[:j+1], c[:p], k)
			}
			return find3(a, b[:j], c[:p+1], k)
		}
		x, y, z = find3(a[i+1:], b, c, k-i-1)
		x += i + 1
		return
	}

	if b[j].time <= a[i].time && b[j].time <= c[p].time {
		if h > k {
			if a[i].time <= c[p].time {
				return find3(a[:i+1], b, c[:p], k)
			}
			return find3(a[:i], b, c[:p+1], k)
		}
		x, y, z = find3(a, b[j+1:], c, k-j-1)
		y += j + 1
		return
	}
	if h >= k {
		if a[i].time <= b[j].time {
			return find3(a[:i+1], b[:j], c, k)
		}
		return find3(a[:i], b[:j+1], c, k)
	}
	x, y, z = find3(a, b, c[p+1:], k-p-1)
	z += p + 1
	return
}

func find2(a, b []Book, k int) (int, int) {
	if len(a) == 0 {
		return 0, k
	}
	if len(b) == 0 {
		return k, 0
	}
	i := len(a) / 2
	j := len(b) / 2
	h := i + j + 2
	if a[i].time <= b[j].time {
		if h > k {
			return find2(a, b[:j], k)
		}
		x, y := find2(a[i+1:], b, k-i-1)
		return x + i + 1, y
	}
	if h > k {
		return find2(a[:i], b, k)
	}
	x, y := find2(a, b[j+1:], k-j-1)
	return x, y + j + 1
}

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
}

type Book struct {
	pos   int
	time  int
	alice int
	bob   int
}

type Books []Book

func (books Books) Len() int {
	return len(books)
}

func (books Books) Less(i, j int) bool {
	return books[i].time < books[j].time
}

func (books Books) Swap(i, j int) {
	books[i], books[j] = books[j], books[i]
}

package main

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

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

	tc := 1

	for tc > 0 {
		tc--
		n, m, k := readThreeNums(reader)
		books := make([][]int, n)
		for i := 0; i < n; i++ {
			books[i] = readNNums(reader, 3)
		}

		tot, ans := solve(n, m, k, books)
		fmt.Println(tot)
		if tot > 0 {
			var buf bytes.Buffer
			for i := 0; i < m; i++ {
				buf.WriteString(fmt.Sprintf("%d ", ans[i]))
			}
			fmt.Println(buf.String())
		}
	}
}

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func solve(n, m, k int, arr [][]int) (int64, []int) {

	sets := make([][]Book, 4)

	for i := 0; i < 4; i++ {
		sets[i] = make([]Book, 0, n)
	}

	for i := 0; i < n; i++ {
		book := arr[i]
		t, a, b := book[0], book[1], book[2]
		sets[a*2+b] = append(sets[a*2+b], Book{i, t, a, b})
	}

	for i := 0; i < 4; i++ {
		sort.Sort(Books(sets[i]))
	}

	sums := make([][]int64, 4)
	for i := 0; i < 4; i++ {
		sums[i] = make([]int64, 0, len(sets[i])+1)

		var cur int64

		sums[i] = append(sums[i], cur)

		for j := 0; j < len(sets[i]); j++ {
			cur += int64(sets[i][j].time)
			sums[i] = append(sums[i], cur)
		}
	}

	search := func(arr []Book, val int) int {
		return sort.Search(len(arr), func(i int) bool {
			return arr[i].time >= val
		})
	}

	find := func(a, b, c []Book, x int) int {
		n := len(a) + len(b) + len(c)
		if x > n {
			return -1
		}

		var left, right = 0, math.MaxInt32 - 1

		for left < right {
			mid := (left + right) / 2

			i := search(a, mid)

			j := search(b, mid)

			u := search(c, mid)

			if i+j+u >= x {
				right = mid
			} else {
				left = mid + 1
			}
		}
		return right
	}
	pp := make([]int, 4)
	var best int64 = math.MaxInt64

	for cur := 0; cur < min(m+1, len(sums[3])); cur++ {
		// already choose cur
		i := min(max(0, k-cur), len(sets[1]))
		j := min(max(0, k-cur), len(sets[2]))

		if cur+i < k || cur+j < k {
			continue
		}
		// cur + i >= k && cur + j >= k
		x := m - cur - i - j

		if x < 0 {
			continue
		}
		// need to find cheapest book in sets[1][i:], sets[2][j:], sets[0][0]
		y := find(sets[0], sets[1][i:], sets[2][j:], x)

		if y < 0 {
			continue
		}
		a := search(sets[0], y)
		b := search(sets[1][i:], y) + i
		c := search(sets[2][i:], y) + j

		if cur+a+b+c != m {
			continue
		}

		tmp := sums[3][cur] + sums[0][a] + sums[1][b] + sums[2][c]
		if tmp < best {
			best = tmp
			pp[0] = a
			pp[1] = b
			pp[2] = c
			pp[3] = cur
		}
	}

	if best == math.MaxInt64 {
		return -1, nil
	}

	res := make([]int, 0, m)
	var tot int64
	for i := 0; i < 4; i++ {
		for j := 0; j < pp[i]; j++ {
			res = append(res, sets[i][j].pos+1)
			tot += int64(sets[i][j].time)
		}
	}

	return tot, res
}

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
}

type Book struct {
	pos   int
	time  int
	alice int
	bob   int
}

type Books []Book

func (books Books) Len() int {
	return len(books)
}

func (books Books) Less(i, j int) bool {
	return books[i].time < books[j].time
}

func (books Books) Swap(i, j int) {
	books[i], books[j] = books[j], books[i]
}

package main

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

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

	tc := 1

	for tc > 0 {
		tc--
		n, m, k := readThreeNums(reader)
		books := make([][]int, n)
		for i := 0; i < n; i++ {
			books[i] = readNNums(reader, 3)
		}

		tot, ans := solve(n, m, k, books)
		fmt.Println(tot)
		if tot > 0 {
			var buf bytes.Buffer
			for i := 0; i < m; i++ {
				buf.WriteString(fmt.Sprintf("%d ", ans[i]))
			}
			fmt.Println(buf.String())
		}
	}
}

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func solve(n, m, k int, arr [][]int) (int64, []int) {

	sets := make([][]Book, 4)

	for i := 0; i < 4; i++ {
		sets[i] = make([]Book, 0, n)
	}

	for i := 0; i < n; i++ {
		book := arr[i]
		t, a, b := book[0], book[1], book[2]
		sets[a*2+b] = append(sets[a*2+b], Book{i, t, a, b})
	}

	for i := 0; i < 4; i++ {
		sort.Sort(Books(sets[i]))
	}

	sums := make([][]int64, 4)
	for i := 0; i < 4; i++ {
		sums[i] = make([]int64, 0, len(sets[i])+1)

		var cur int64

		sums[i] = append(sums[i], cur)

		for j := 0; j < len(sets[i]); j++ {
			cur += int64(sets[i][j].time)
			sums[i] = append(sums[i], cur)
		}
	}

	pp := make([]int, 4)
	var best int64 = math.MaxInt64

	for cur := 0; cur < min(m+1, len(sums[3])); cur++ {
		// already choose cur
		i := min(max(0, k-cur), len(sets[1]))
		j := min(max(0, k-cur), len(sets[2]))

		if cur+i < k || cur+j < k {
			continue
		}
		// cur + i >= k && cur + j >= k
		x := m - cur - i - j

		if x < 0 || x > len(sets[0])+len(sets[1])-i+len(sets[2])-j {
			continue
		}
		if x == 0 {
			tmp := sums[3][cur] + sums[1][i] + sums[2][j]
			if tmp < best {
				best = tmp
				pp[0] = 0
				pp[1] = i
				pp[2] = j
				pp[3] = cur
			}
			continue
		}

		a, b, c := find3(sets[0], sets[1][i:], sets[2][j:], x)
		b += i
		c += j

		tmp := sums[0][a] + sums[1][b] + sums[2][c] + sums[3][cur]

		if tmp < best {
			best = tmp
			pp[0] = a
			pp[1] = b
			pp[2] = c
			pp[3] = cur
		}
	}

	if best == math.MaxInt64 {
		return -1, nil
	}

	res := make([]int, 0, m)
	var tot int64
	for i := 0; i < 4; i++ {
		for j := 0; j < pp[i]; j++ {
			res = append(res, sets[i][j].pos+1)
			tot += int64(sets[i][j].time)
		}
	}

	return tot, res
}

func find3(a, b, c []Book, k int) (x int, y int, z int) {
	if len(a) == 0 {
		y, z = find2(b, c, k)
		return
	}
	if len(b) == 0 {
		x, z = find2(a, c, k)
		return
	}
	if len(c) == 0 {
		x, y = find2(a, b, k)
		return
	}

	i, j, p := len(a)/2, len(b)/2, len(c)/2
	h := i + j + p + 3

	if a[i].time <= b[j].time && a[i].time <= c[p].time {
		if h > k {
			if b[j].time <= c[p].time {
				return find3(a, b[:j+1], c[:p], k)
			}
			return find3(a, b[:j], c[:p+1], k)
		}
		x, y, z = find3(a[i+1:], b, c, k-i-1)
		x += i + 1
		return
	}

	if b[j].time <= a[i].time && b[j].time <= c[p].time {
		if h > k {
			if a[i].time <= c[p].time {
				return find3(a[:i+1], b, c[:p], k)
			}
			return find3(a[:i], b, c[:p+1], k)
		}
		x, y, z = find3(a, b[j+1:], c, k-j-1)
		y += j + 1
		return
	}
	if h >= k {
		if a[i].time <= b[j].time {
			return find3(a[:i+1], b[:j], c, k)
		}
		return find3(a[:i], b[:j+1], c, k)
	}
	x, y, z = find3(a, b, c[p+1:], k-p-1)
	z += p + 1
	return
}

func find2(a, b []Book, k int) (int, int) {
	if len(a) == 0 {
		return 0, k
	}
	if len(b) == 0 {
		return k, 0
	}
	i := len(a) / 2
	j := len(b) / 2
	h := i + j + 2
	if a[i].time <= b[j].time {
		if h > k {
			return find2(a, b[:j], k)
		}
		x, y := find2(a[i+1:], b, k-i-1)
		return x + i + 1, y
	}
	if h > k {
		return find2(a[:i], b, k)
	}
	x, y := find2(a, b[j+1:], k-j-1)
	return x, y + j + 1
}

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
}

type Book struct {
	pos   int
	time  int
	alice int
	bob   int
}

type Books []Book

func (books Books) Len() int {
	return len(books)
}

func (books Books) Less(i, j int) bool {
	return books[i].time < books[j].time
}

func (books Books) Swap(i, j int) {
	books[i], books[j] = books[j], books[i]
}

package main

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

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

	tc := 1

	for tc > 0 {
		tc--
		n, m, k := readThreeNums(reader)
		books := make([][]int, n)
		for i := 0; i < n; i++ {
			books[i] = readNNums(reader, 3)
		}

		tot, ans := solve(n, m, k, books)
		fmt.Println(tot)
		if tot > 0 {
			var buf bytes.Buffer
			for i := 0; i < m; i++ {
				buf.WriteString(fmt.Sprintf("%d ", ans[i]))
			}
			fmt.Println(buf.String())
		}
	}
}

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func solve(n, m, k int, arr [][]int) (int64, []int) {
	times := make([][]Pair, 4)

	for i := 0; i < 4; i++ {
		times[i] = make([]Pair, 0, n)
	}

	for i, book := range arr {
		t, a, b := book[0], book[1], book[2]
		times[a*2+b] = append(times[a*2+b], Pair{t, i})
	}

	sums := make([][]int64, 4)

	for i := 0; i < 4; i++ {
		sort.Sort(Pairs(times[i]))
		sums[i] = make([]int64, 0, len(times[i])+1)
		var cur int64
		sums[i] = append(sums[i], cur)
		for j := 0; j < len(times[i]); j++ {
			cur += int64(times[i][j].first)
			sums[i] = append(sums[i], cur)
		}
	}

	var ans int64 = math.MaxInt64
	var pos = math.MaxInt32

	var sum int64
	var st *Node
	for iter := 0; iter < 2; iter++ {
		//first iter find the best, second find the result corresponding to best
		st = nil
		var fr *Node
		sum = 0
		var start int

		for k-start >= len(sums[1]) || k-start >= len(sums[2]) || m-start-(k-start)*2 < 0 {
			start++
		}
		if start >= len(sums[3]) {
			return -1, nil
		}

		need := m - start - (k-start)*2

		for i := 0; i < 3; i++ {
			var x int
			if i != 0 {
				x = k - start
			}
			for p := len(times[i]) - 1; p >= x; p-- {
				fr = Insert(fr, times[i][p])
			}
		}
		st, fr = update(st, fr, &sum, need)
		var x = len(sums[3])
		if iter == 1 {
			x = pos
		}
		for cnt := start; cnt < x; cnt++ {
			if k-cnt >= 0 {
				if cnt+(k-cnt)*2+st.Size() == m {
					tmp := sums[3][cnt] + sums[1][k-cnt] + sums[2][k-cnt] + sum
					if tmp < ans {
						ans = tmp
						pos = cnt + 1
					}
				}
			} else {
				if cnt+st.Size() == m {
					tmp := sums[3][cnt] + sum
					if tmp < ans {
						ans = tmp
						pos = cnt + 1
					}
				}
			}
			if iter == 1 && cnt+1 == pos {
				break
			}
			need--
			if k-cnt > 0 {
				need += 2
				fr = Insert(fr, times[1][k-cnt-1])
				fr = Insert(fr, times[2][k-cnt-1])
			}
			st, fr = update(st, fr, &sum, need)
		}
	}

	res := make([]int, 0, m)

	for i := 0; i+1 < pos; i++ {
		res = append(res, times[3][i].second+1)
	}

	for i := 0; i <= k-pos; i++ {
		res = append(res, times[1][i].second+1)
		res = append(res, times[2][i].second+1)
	}

	Visit(st, func(item Pair) {
		res = append(res, item.second+1)
	})

	return ans, res
}

func update(st *Node, fr *Node, sum *int64, need int) (*Node, *Node) {
	need = max(0, need)
	ok := true
	for ok {
		ok = false
		for st.Size() > need {
			x := MaxValueNode(st)
			*sum -= int64(x.item.first)
			st = Delete(st, x.item)
			fr = Insert(fr, x.item)
			ok = true
		}

		for st.Size() < need && fr.Size() > 0 {
			x := MinValueNode(fr)
			*sum += int64(x.item.first)
			fr = Delete(fr, x.item)
			st = Insert(st, x.item)
			ok = true
		}

		for st.Size() > 0 && fr.Size() > 0 {
			x := MinValueNode(fr)
			y := MaxValueNode(st)
			if !x.item.Less(y.item) {
				break
			}
			fr = Delete(fr, x.item)
			fr = Insert(fr, y.item)
			st = Delete(st, y.item)
			st = Insert(st, x.item)
			ok = true
		}
	}

	return st, fr
}

type Pair struct {
	first, second int
}

func (this Pair) Less(that Pair) bool {
	if this.first < that.first {
		return true
	}
	if this.first == that.first && (this.second < that.second) {
		return true
	}
	return false
}

type Pairs []Pair

func (ps Pairs) Len() int {
	return len(ps)
}

func (ps Pairs) Less(i, j int) bool {
	return ps[i].Less(ps[j])
}

func (ps Pairs) Swap(i, j int) {
	ps[i], ps[j] = ps[j], ps[i]
}

/**
* this is a AVL tree
 */
type Node struct {
	item        Pair
	height      int
	size        int
	left, right *Node
}

func (node *Node) Height() int {
	if node == nil {
		return 0
	}
	return node.height
}

func (node *Node) Size() int {
	if node == nil {
		return 0
	}
	return node.size
}

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

func NewNode(item Pair) *Node {
	node := new(Node)
	node.item = item
	node.height = 1
	node.size = 1
	return node
}

func rightRotate(y *Node) *Node {
	x := y.left

	t2 := x.right

	x.right = y
	y.left = t2
	y.height = max(y.left.Height(), y.right.Height()) + 1
	y.size = y.left.Size() + y.right.Size() + 1
	x.height = max(x.left.Height(), x.right.Height()) + 1
	x.size = x.left.Size() + x.right.Size() + 1

	return x
}

func leftRotate(x *Node) *Node {
	y := x.right
	t2 := y.left

	y.left = x
	x.right = t2

	x.height = max(x.left.Height(), x.right.Height()) + 1
	x.size = x.left.Size() + x.right.Size() + 1
	y.height = max(y.left.Height(), y.right.Height()) + 1
	y.size = y.left.Size() + y.right.Size() + 1

	return y
}

func (node *Node) GetBalance() int {
	if node == nil {
		return 0
	}
	return node.left.Height() - node.right.Height()
}

func FindEqualOrGreater(node *Node, item Pair) *Node {
	if node == nil {
		return nil
	}
	if node.item.Less(item) {
		return FindEqualOrGreater(node.right, item)
	}

	res := FindEqualOrGreater(node.left, item)
	if res == nil {
		return node
	}
	return res
}

func FindEqualOrLess(node *Node, item Pair) *Node {
	if node == nil {
		return nil
	}
	if item.Less(node.item) {
		return FindEqualOrLess(node.left, item)
	}

	res := FindEqualOrLess(node.right, item)
	if res == nil {
		return node
	}
	return res
}

func Insert(node *Node, item Pair) *Node {
	if node == nil {
		return NewNode(item)
	}

	if item.Less(node.item) {
		node.left = Insert(node.left, item)
	} else {
		node.right = Insert(node.right, item)
	}

	node.height = max(node.left.Height(), node.right.Height()) + 1
	node.size = node.left.Size() + node.right.Size() + 1
	balance := node.GetBalance()

	if balance > 1 && item.Less(node.left.item) {
		return rightRotate(node)
	}

	if balance < -1 && node.right.item.Less(item) {
		return leftRotate(node)
	}

	if balance > 1 && node.left.item.Less(item) {
		node.left = leftRotate(node.left)
		return rightRotate(node)
	}

	if balance < -1 && item.Less(node.right.item) {
		node.right = rightRotate(node.right)
		return leftRotate(node)
	}

	return node
}

func MinValueNode(root *Node) *Node {
	cur := root

	for cur.left != nil {
		cur = cur.left
	}

	return cur
}

func MaxValueNode(root *Node) *Node {
	cur := root

	for cur.right != nil {
		cur = cur.right
	}
	return cur
}

func Visit(node *Node, fn func(item Pair)) {
	if node == nil {
		return
	}
	fn(node.item)
	Visit(node.left, fn)
	Visit(node.right, fn)
}

func Delete(root *Node, item Pair) *Node {
	if root == nil {
		return nil
	}

	if item.Less(root.item) {
		root.left = Delete(root.left, item)
	} else if root.item.Less(item) {
		root.right = Delete(root.right, item)
	} else {
		if root.left == nil || root.right == nil {
			tmp := root.left
			if root.left == nil {
				tmp = root.right
			}
			root = tmp
		} else {
			tmp := MinValueNode(root.right)

			root.item = tmp.item
			// make sure tmp node deleted after call delete on root.right
			root.right = Delete(root.right, tmp.item)
		}
	}

	if root == nil {
		return root
	}

	root.height = max(root.left.Height(), root.right.Height()) + 1
	root.size = root.left.Size() + root.right.Size() + 1

	balance := root.GetBalance()

	if balance > 1 && root.left.GetBalance() >= 0 {
		return rightRotate(root)
	}

	if balance > 1 && root.left.GetBalance() < 0 {
		root.left = leftRotate(root.left)
		return rightRotate(root)
	}

	if balance < -1 && root.right.GetBalance() <= 0 {
		return leftRotate(root)
	}

	if balance < -1 && root.right.GetBalance() > 0 {
		root.right = rightRotate(root.right)
		return leftRotate(root)
	}

	return root
}

package main

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

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

	tc := 1

	for tc > 0 {
		tc--
		n, m, k := readThreeNums(reader)
		books := make([][]int, n)
		for i := 0; i < n; i++ {
			books[i] = readNNums(reader, 3)
		}

		tot, ans := solve(n, m, k, books)
		fmt.Println(tot)
		if tot > 0 {
			var buf bytes.Buffer
			for i := 0; i < m; i++ {
				buf.WriteString(fmt.Sprintf("%d ", ans[i]))
			}
			fmt.Println(buf.String())
		}
	}
}

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func solve(n, m, k int, arr [][]int) (int64, []int) {
	times := make([][]Pair, 4)

	for i := 0; i < 4; i++ {
		times[i] = make([]Pair, 0, n)
	}

	for i, book := range arr {
		t, a, b := book[0], book[1], book[2]
		times[a*2+b] = append(times[a*2+b], Pair{t, i})
	}

	sums := make([][]int64, 4)

	for i := 0; i < 4; i++ {
		sort.Sort(Pairs(times[i]))
		sums[i] = make([]int64, 0, len(times[i])+1)
		var cur int64
		sums[i] = append(sums[i], cur)
		for j := 0; j < len(times[i]); j++ {
			cur += int64(times[i][j].first)
			sums[i] = append(sums[i], cur)
		}
	}

	var ans int64 = math.MaxInt64
	var pos = math.MaxInt32

	var sum int64
	var st *Node
	for iter := 0; iter < 2; iter++ {
		//first iter find the best, second find the result corresponding to best
		st = nil
		var fr *Node
		sum = 0
		var start int

		for k-start >= len(sums[1]) || k-start >= len(sums[2]) || m-start-(k-start)*2 < 0 {
			start++
		}
		if start >= len(sums[3]) {
			return -1, nil
		}

		need := m - start - (k-start)*2

		for i := 0; i < 3; i++ {
			var x int
			if i != 0 {
				x = k - start
			}
			for p := len(times[i]) - 1; p >= x; p-- {
				fr = Insert(fr, times[i][p])
			}
		}
		st, fr = update(st, fr, &sum, need)
		var x = len(sums[3])
		if iter == 1 {
			x = pos
		}
		for cnt := start; cnt < x; cnt++ {
			if k-cnt >= 0 {
				if cnt+(k-cnt)*2+st.Size() == m {
					tmp := sums[3][cnt] + sums[1][k-cnt] + sums[2][k-cnt] + sum
					if tmp < ans {
						ans = tmp
						pos = cnt + 1
					}
				}
			} else {
				if cnt+st.Size() == m {
					tmp := sums[3][cnt] + sum
					if tmp < ans {
						ans = tmp
						pos = cnt + 1
					}
				}
			}
			if iter == 1 && cnt+1 == pos {
				break
			}
			need--
			if k-cnt > 0 {
				need += 2
				fr = Insert(fr, times[1][k-cnt-1])
				fr = Insert(fr, times[2][k-cnt-1])
			}
			st, fr = update(st, fr, &sum, need)
		}
	}

	res := make([]int, 0, m)

	for i := 0; i+1 < pos; i++ {
		res = append(res, times[3][i].second+1)
	}

	for i := 0; i <= k-pos; i++ {
		res = append(res, times[1][i].second+1)
		res = append(res, times[2][i].second+1)
	}

	Visit(st, func(item Pair) {
		res = append(res, item.second+1)
	})

	return ans, res
}

func update(st *Node, fr *Node, sum *int64, need int) (*Node, *Node) {
	need = max(0, need)
	ok := true
	for ok {
		ok = false
		for st.Size() > need {
			x := MaxValueNode(st)
			*sum -= int64(x.item.first)
			st = Delete(st, x.item)
			fr = Insert(fr, x.item)
			ok = true
		}

		for st.Size() < need && fr.Size() > 0 {
			x := MinValueNode(fr)
			*sum += int64(x.item.first)
			fr = Delete(fr, x.item)
			st = Insert(st, x.item)
			ok = true
		}

		for st.Size() > 0 && fr.Size() > 0 {
			x := MinValueNode(fr)
			y := MaxValueNode(st)
			if !x.item.Less(y.item) {
				break
			}
			fr = Delete(fr, x.item)
			fr = Insert(fr, y.item)
			st = Delete(st, y.item)
			st = Insert(st, x.item)
			ok = true
		}
	}

	return st, fr
}

type Pair struct {
	first, second int
}

func (this Pair) Less(that Pair) bool {
	return this.first < that.first
}

type Pairs []Pair

func (ps Pairs) Len() int {
	return len(ps)
}

func (ps Pairs) Less(i, j int) bool {
	return ps[i].Less(ps[j])
}

func (ps Pairs) Swap(i, j int) {
	ps[i], ps[j] = ps[j], ps[i]
}

/**
* this is a AVL tree
 */
type Node struct {
	item        Pair
	height      int
	size        int
	left, right *Node
}

func (node *Node) Height() int {
	if node == nil {
		return 0
	}
	return node.height
}

func (node *Node) Size() int {
	if node == nil {
		return 0
	}
	return node.size
}

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

func NewNode(item Pair) *Node {
	node := new(Node)
	node.item = item
	node.height = 1
	node.size = 1
	return node
}

func rightRotate(y *Node) *Node {
	x := y.left

	t2 := x.right

	x.right = y
	y.left = t2
	y.height = max(y.left.Height(), y.right.Height()) + 1
	y.size = y.left.Size() + y.right.Size() + 1
	x.height = max(x.left.Height(), x.right.Height()) + 1
	x.size = x.left.Size() + x.right.Size() + 1

	return x
}

func leftRotate(x *Node) *Node {
	y := x.right
	t2 := y.left

	y.left = x
	x.right = t2

	x.height = max(x.left.Height(), x.right.Height()) + 1
	x.size = x.left.Size() + x.right.Size() + 1
	y.height = max(y.left.Height(), y.right.Height()) + 1
	y.size = y.left.Size() + y.right.Size() + 1

	return y
}

func (node *Node) GetBalance() int {
	if node == nil {
		return 0
	}
	return node.left.Height() - node.right.Height()
}

func FindEqualOrGreater(node *Node, item Pair) *Node {
	if node == nil {
		return nil
	}
	if node.item.Less(item) {
		return FindEqualOrGreater(node.right, item)
	}

	res := FindEqualOrGreater(node.left, item)
	if res == nil {
		return node
	}
	return res
}

func FindEqualOrLess(node *Node, item Pair) *Node {
	if node == nil {
		return nil
	}
	if item.Less(node.item) {
		return FindEqualOrLess(node.left, item)
	}

	res := FindEqualOrLess(node.right, item)
	if res == nil {
		return node
	}
	return res
}

func Insert(node *Node, item Pair) *Node {
	if node == nil {
		return NewNode(item)
	}

	if item.Less(node.item) {
		node.left = Insert(node.left, item)
	} else {
		node.right = Insert(node.right, item)
	}

	node.height = max(node.left.Height(), node.right.Height()) + 1
	node.size = node.left.Size() + node.right.Size() + 1
	balance := node.GetBalance()

	if balance > 1 && item.Less(node.left.item) {
		return rightRotate(node)
	}

	if balance < -1 && node.right.item.Less(item) {
		return leftRotate(node)
	}

	if balance > 1 && node.left.item.Less(item) {
		node.left = leftRotate(node.left)
		return rightRotate(node)
	}

	if balance < -1 && item.Less(node.right.item) {
		node.right = rightRotate(node.right)
		return leftRotate(node)
	}

	return node
}

func MinValueNode(root *Node) *Node {
	cur := root

	for cur.left != nil {
		cur = cur.left
	}

	return cur
}

func MaxValueNode(root *Node) *Node {
	cur := root

	for cur.right != nil {
		cur = cur.right
	}
	return cur
}

func Visit(node *Node, fn func(item Pair)) {
	if node == nil {
		return
	}
	fn(node.item)
	Visit(node.left, fn)
	Visit(node.right, fn)
}

func Delete(root *Node, item Pair) *Node {
	if root == nil {
		return nil
	}

	if item.Less(root.item) {
		root.left = Delete(root.left, item)
	} else if root.item.Less(item) {
		root.right = Delete(root.right, item)
	} else {
		if root.left == nil || root.right == nil {
			tmp := root.left
			if root.left == nil {
				tmp = root.right
			}
			root = tmp
		} else {
			tmp := MinValueNode(root.right)

			root.item = tmp.item
			// make sure tmp node deleted after call delete on root.right
			root.right = Delete(root.right, tmp.item)
		}
	}

	if root == nil {
		return root
	}

	root.height = max(root.left.Height(), root.right.Height()) + 1
	root.size = root.left.Size() + root.right.Size() + 1

	balance := root.GetBalance()

	if balance > 1 && root.left.GetBalance() >= 0 {
		return rightRotate(root)
	}

	if balance > 1 && root.left.GetBalance() < 0 {
		root.left = leftRotate(root.left)
		return rightRotate(root)
	}

	if balance < -1 && root.right.GetBalance() <= 0 {
		return leftRotate(root)
	}

	if balance < -1 && root.right.GetBalance() > 0 {
		root.right = rightRotate(root.right)
		return leftRotate(root)
	}

	return root
}

package main

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

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

	tc := 1

	for tc > 0 {
		tc--
		n, m, k := readThreeNums(reader)
		books := make([][]int, n)
		for i := 0; i < n; i++ {
			books[i] = readNNums(reader, 3)
		}

		fmt.Println(solve(n, m, k, books))
	}
}

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func solve(n, m, k int, arr [][]int) (int64, []int) {

	sets := make([][]Book, 4)

	for i := 0; i < 4; i++ {
		sets[i] = make([]Book, 0, n)
	}

	for i := 0; i < n; i++ {
		book := arr[i]
		t, a, b := book[0], book[1], book[2]
		sets[a*2+b] = append(sets[a*2+b], Book{i, t, a, b})
	}

	for i := 0; i < 4; i++ {
		sort.Sort(Books(sets[i]))
	}

	sums := make([][]int64, 4)
	for i := 0; i < 4; i++ {
		sums[i] = make([]int64, 0, len(sets[i])+1)

		var cur int64

		sums[i] = append(sums[i], cur)

		for j := 0; j < len(sets[i]); j++ {
			cur += int64(sets[i][j].time)
			sums[i] = append(sums[i], cur)
		}
	}

	search := func(arr []Book, val int) int {
		return sort.Search(len(arr), func(i int) bool {
			return arr[i].time >= val
		})
	}

	find := func(a, b, c []Book, x int) int {
		n := len(a) + len(b) + len(c)
		if x >= n {
			return -1
		}

		var left, right = 0, math.MaxInt32 - 1

		for left < right {
			mid := (left + right) / 2

			i := search(a, mid)

			j := search(b, mid)

			u := search(c, mid)

			if i+j+u >= x {
				right = mid
			} else {
				left = mid + 1
			}
		}
		return right
	}
	pp := make([]int, 4)
	var best int64 = math.MaxInt64

	for cur := 0; cur < min(m+1, len(sums[3])); cur++ {
		// already choose cur
		i := min(max(0, k-cur), len(sets[1]))
		j := min(max(0, k-cur), len(sets[2]))

		if cur+i < k || cur+j < k {
			continue
		}
		// cur + i >= k && cur + j >= k
		x := m - cur - i - j

		if x < 0 {
			continue
		}
		// need to find cheapest book in sets[1][i:], sets[2][j:], sets[0][0]
		y := find(sets[0], sets[1][i:], sets[2][j:], x)

		a := search(sets[0], y)
		b := search(sets[1][i:], y) + i
		c := search(sets[2][i:], y) + j

		tmp := sums[3][cur] + sums[0][a] + sums[1][b] + sums[2][c]
		if tmp < best {
			best = tmp
			pp[0] = a
			pp[1] = b
			pp[2] = c
			pp[3] = cur
		}
	}

	if best == math.MaxInt64 {
		return -1, nil
	}

	res := make([]int, 0, m)
	var tot int64
	for i := 0; i < 4; i++ {
		for j := 0; j < pp[i]; j++ {
			res = append(res, sets[i][j].pos+1)
			tot += int64(sets[i][j].time)
		}
	}

	return tot, res
}

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
}

type Book struct {
	pos   int
	time  int
	alice int
	bob   int
}

type Books []Book

func (books Books) Len() int {
	return len(books)
}

func (books Books) Less(i, j int) bool {
	return books[i].time < books[j].time
}

func (books Books) Swap(i, j int) {
	books[i], books[j] = books[j], books[i]
}

package main

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

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

	tc := 1

	for tc > 0 {
		tc--
		n, m, k := readThreeNums(reader)
		books := make([][]int, n)
		for i := 0; i < n; i++ {
			books[i] = readNNums(reader, 3)
		}

		tot, ans := solve(n, m, k, books)
		fmt.Println(tot)
		if tot > 0 {
			var buf bytes.Buffer
			for i := 0; i < m; i++ {
				buf.WriteString(fmt.Sprintf("%d ", ans[i]))
			}
			fmt.Println(buf.String())
		}
	}
}

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func solve(n, m, k int, arr [][]int) (int64, []int) {

	sets := make([][]Book, 4)

	for i := 0; i < 4; i++ {
		sets[i] = make([]Book, 0, n)
	}

	for i := 0; i < n; i++ {
		book := arr[i]
		t, a, b := book[0], book[1], book[2]
		sets[a*2+b] = append(sets[a*2+b], Book{i, t, a, b})
	}

	for i := 0; i < 4; i++ {
		sort.Sort(Books(sets[i]))
	}

	sums := make([][]int64, 4)
	for i := 0; i < 4; i++ {
		sums[i] = make([]int64, 0, len(sets[i])+1)

		var cur int64

		sums[i] = append(sums[i], cur)

		for j := 0; j < len(sets[i]); j++ {
			cur += int64(sets[i][j].time)
			sums[i] = append(sums[i], cur)
		}
	}

	search := func(arr []Book, val int) int {
		return sort.Search(len(arr), func(i int) bool {
			return arr[i].time >= val
		})
	}

	find := func(a, b, c []Book, x int) int {
		n := len(a) + len(b) + len(c)
		if x > n {
			return -1
		}

		var left, right = 0, math.MaxInt32 - 1

		for left < right {
			mid := (left + right) / 2

			i := search(a, mid)

			j := search(b, mid)

			u := search(c, mid)

			if i+j+u >= x {
				right = mid
			} else {
				left = mid + 1
			}
		}
		return right
	}
	pp := make([]int, 4)
	var best int64 = math.MaxInt64

	for cur := 0; cur < min(m+1, len(sums[3])); cur++ {
		// already choose cur
		i := min(max(0, k-cur), len(sets[1]))
		j := min(max(0, k-cur), len(sets[2]))

		if cur+i < k || cur+j < k {
			continue
		}
		// cur + i >= k && cur + j >= k
		x := m - cur - i - j

		if x < 0 {
			continue
		}
		// need to find cheapest book in sets[1][i:], sets[2][j:], sets[0][0]
		y := find(sets[0], sets[1][i:], sets[2][j:], x)

		a := search(sets[0], y)
		b := search(sets[1][i:], y) + i
		c := search(sets[2][i:], y) + j

		if cur+a+b+c != m {
			continue
		}

		tmp := sums[3][cur] + sums[0][a] + sums[1][b] + sums[2][c]
		if tmp < best {
			best = tmp
			pp[0] = a
			pp[1] = b
			pp[2] = c
			pp[3] = cur
		}
	}

	if best == math.MaxInt64 {
		return -1, nil
	}

	res := make([]int, 0, m)
	var tot int64
	for i := 0; i < 4; i++ {
		for j := 0; j < pp[i]; j++ {
			res = append(res, sets[i][j].pos+1)
			tot += int64(sets[i][j].time)
		}
	}

	return tot, res
}

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
}

type Book struct {
	pos   int
	time  int
	alice int
	bob   int
}

type Books []Book

func (books Books) Len() int {
	return len(books)
}

func (books Books) Less(i, j int) bool {
	return books[i].time < books[j].time
}

func (books Books) Swap(i, j int) {
	books[i], books[j] = books[j], books[i]
}

package main

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

type data struct{ t, i int }

type Datas []data

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

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

	var n, m, k int

	fmt.Fscanf(reader, "%d %d %d\n", &n, &m, &k)

	aa := make(Datas, 0, n)
	bb := make(Datas, 0, n)
	cc := make(Datas, 0, n)
	dd := make(Datas, 0, n)

	var t, a, b int
	for i := 1; i <= n; i++ {
		fmt.Fscanf(reader, "%d %d %d\n", &t, &a, &b)

		if a == 1 && b == 1 {
			cc = append(cc, data{t: t, i: i})
			continue
		}

		if a == 1 {
			aa = append(aa, data{t: t, i: i})
			continue
		}

		if b == 1 {
			bb = append(bb, data{t: t, i: i})
			continue
		}

		if a == 0 && b == 0 {
			dd = append(dd, data{t: t, i: i})
		}
	}

	if len(aa)+len(cc) < k || len(bb)+len(cc) < k ||
		2*k-len(cc) > m {
		fmt.Println(-1)
		return
	}

	sort.Sort(aa)
	sort.Sort(bb)
	sort.Sort(cc)
	sort.Sort(dd)

	selectIndex := make([]int, 0, m)

	sum := int64(0)
	abi := 0
	ci := 0
	mm := 0
	for i := 0; i < k; i++ {
		if abi >= len(aa) || abi >= len(bb) ||
			(ci < len(cc) && aa[abi].t+bb[abi].t > cc[ci].t) ||
			mm+1 == m {
			sum += int64(cc[ci].t)
			selectIndex = append(selectIndex, cc[ci].i)
			ci++
			mm++
		} else {
			sum += int64(aa[abi].t + bb[abi].t)
			selectIndex = append(selectIndex, aa[abi].i, bb[abi].i)
			abi++
			mm += 2
		}
	}

	ai := abi
	bi := abi
	di := 0

	for mm < m {
		// fmt.Println(ai, aa)
		// fmt.Println(bi, bb)
		// fmt.Println(ci, cc)
		// fmt.Println(di, dd, "\n")
		var index int
		switch minIndex(minT(ai, &aa), minT(bi, &bb), minT(ci, &cc), minT(di, &dd)) {
		case 1:
			sum += int64(aa[ai].t)
			index = aa[ai].i
			ai++
		case 2:
			sum += int64(bb[bi].t)
			index = bb[bi].i
			bi++
		case 3:
			sum += int64(cc[ci].t)
			index = cc[ci].i
			ci++
		case 4:
			sum += int64(dd[di].t)
			index = dd[di].i
			di++
		}
		selectIndex = append(selectIndex, index)
		mm++
	}

	fmt.Println(sum)
	for i := 0; i < m; i++ {
		if i+1 != m {
			fmt.Printf("%d ", selectIndex[i])
		} else {
			fmt.Printf("%d\n", selectIndex[i])
		}
	}
}

func minT(index int, d *Datas) int {
	if index < len(*d) {
		return (*d)[index].t
	}

	return 1000000
}

func minIndex(n1, n2, n3, n4 int) int {
	if n1 <= n2 && n1 <= n3 && n1 <= n4 {
		return 1
	}

	if n2 <= n1 && n2 <= n3 && n2 <= n4 {
		return 2
	}

	if n3 <= n1 && n3 <= n2 && n3 <= n4 {
		return 3
	}

	if n4 <= n1 && n4 <= n2 && n4 <= n3 {
		return 4
	}
	return 0
}

package main

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

type data struct {
	t, i int
	s    int64
}

type Datas []data

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

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

	var n, m, k int

	fmt.Fscanf(reader, "%d %d %d\n", &n, &m, &k)

	aa := make(Datas, 0, n)
	bb := make(Datas, 0, n)
	cc := make(Datas, 0, n)
	dd := make(Datas, 0, n)

	var t, a, b int
	for i := 1; i <= n; i++ {
		fmt.Fscanf(reader, "%d %d %d\n", &t, &a, &b)

		if a == 1 && b == 1 {
			cc = append(cc, data{t: t, i: i, s: int64(t)})
			continue
		}

		if a == 1 {
			aa = append(aa, data{t: t, i: i, s: int64(t)})
			continue
		}

		if b == 1 {
			bb = append(bb, data{t: t, i: i, s: int64(t)})
			continue
		}

		if a == 0 && b == 0 {
			dd = append(dd, data{t: t, i: i, s: int64(t)})
		}
	}

	if len(aa)+len(cc) < k || len(bb)+len(cc) < k ||
		2*k-len(cc) > m {
		fmt.Println(-1)
		return
	}

	sort.Sort(aa)
	sort.Sort(bb)
	sort.Sort(cc)
	sort.Sort(dd)

	for i := 1; i < len(aa); i++ {
		aa[i].s += aa[i-1].s
	}

	for i := 1; i < len(bb); i++ {
		bb[i].s += bb[i-1].s
	}

	for i := 1; i < len(cc); i++ {
		cc[i].s += cc[i-1].s
	}

	for i := 1; i < len(dd); i++ {
		dd[i].s += dd[i-1].s
	}

	sum := int64(0)
	abi := 0
	ci := 0
	mm := 0
	for i := 0; i < k; i++ {
		if abi >= len(aa) || abi >= len(bb) ||
			(ci < len(cc) && aa[abi].t+bb[abi].t > cc[ci].t) ||
			mm+1 == m {
			sum += int64(cc[ci].t)
			ci++
			mm++
		} else {
			sum += int64(aa[abi].t + bb[abi].t)
			abi++
			mm += 2
		}
	}

	ai := abi
	bi := abi
	di := 0

	for mm < m {
		// fmt.Println(ai, aa)
		// fmt.Println(bi, bb)
		// fmt.Println(ci, cc)
		// fmt.Println(di, dd, "\n")

		switch minIndex(minT(ai, &aa), minT(bi, &bb), minT(ci, &cc), minT(di, &dd)) {
		case 1:
			sum += int64(aa[ai].t)
			ai++
		case 2:
			sum += int64(bb[bi].t)
			bi++
		case 3:
			sum += int64(cc[ci].t)
			ci++
		case 4:
			sum += int64(dd[di].t)
			di++
		}
		mm++

		if ai > abi && bi > abi && ci > 0 && di < len(dd) && cc[ci-1].s > dd[di].s {
			sum -= int64(cc[ci-1].t - dd[di].t)
			ci--
			di++
			abi++
		}
	}

	fmt.Println(sum)
	for i := 0; i < ai; i++ {
		fmt.Printf("%d ", aa[i].i)
	}

	for i := 0; i < bi; i++ {
		fmt.Printf("%d ", bb[i].i)
	}

	for i := 0; i < ci; i++ {
		fmt.Printf("%d ", cc[i].i)
	}

	for i := 0; i < di; i++ {
		fmt.Printf("%d ", dd[i].i)
	}
}

func minT(index int, d *Datas) int {
	if index < len(*d) {
		return (*d)[index].t
	}

	return 1000000
}

func minIndex(n1, n2, n3, n4 int) int {
	if n1 <= n2 && n1 <= n3 && n1 <= n4 {
		return 1
	}

	if n2 <= n1 && n2 <= n3 && n2 <= n4 {
		return 2
	}

	if n3 <= n1 && n3 <= n2 && n3 <= n4 {
		return 3
	}

	if n4 <= n1 && n4 <= n2 && n4 <= n3 {
		return 4
	}
	return 0
}

package main

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

type data struct{ t, i int }

type Datas []data

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

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

	var n, m, k int

	fmt.Fscanf(reader, "%d %d %d\n", &n, &m, &k)

	aa := make(Datas, 0, n)
	bb := make(Datas, 0, n)
	cc := make(Datas, 0, n)
	dd := make(Datas, 0, n)

	var t, a, b int
	for i := 1; i <= n; i++ {
		fmt.Fscanf(reader, "%d %d %d\n", &t, &a, &b)

		if a == 1 && b == 1 {
			cc = append(cc, data{t: t, i: i})
			continue
		}

		if a == 1 {
			aa = append(aa, data{t: t, i: i})
			continue
		}

		if b == 1 {
			bb = append(bb, data{t: t, i: i})
			continue
		}

		if a == 0 && b == 0 {
			dd = append(dd, data{t: t, i: i})
		}
	}

	if len(aa)+len(cc) < k || len(bb)+len(cc) < k ||
		2*k-len(cc) > m {
		fmt.Println(-1)
		return
	}

	sort.Sort(aa)
	sort.Sort(bb)
	sort.Sort(cc)
	sort.Sort(dd)

	sum := int64(0)
	abi := 0
	ci := 0
	mm := 0
	for i := 0; i < k; i++ {
		if abi >= len(aa) || abi >= len(bb) ||
			(ci < len(cc) && aa[abi].t+bb[abi].t > cc[ci].t) ||
			mm+1 == m {
			sum += int64(cc[ci].t)
			ci++
			mm++
		} else {
			sum += int64(aa[abi].t + bb[abi].t)
			abi++
			mm += 2
		}
	}

	ai := abi
	bi := abi
	di := 0

	for mm < m {
		// fmt.Println(ai, aa)
		// fmt.Println(bi, bb)
		// fmt.Println(ci, cc)
		// fmt.Println(di, dd, "\n")

		switch minIndex(minT(ai, &aa), minT(bi, &bb), minT(ci, &cc), minT(di, &dd)) {
		case 1:
			sum += int64(aa[ai].t)
			ai++
		case 2:
			sum += int64(bb[bi].t)
			bi++
		case 3:
			sum += int64(cc[ci].t)
			ci++
		case 4:
			sum += int64(dd[di].t)
			di++
		}
		mm++

		if ai > abi && bi > abi && ci > 0 && di < len(dd) && cc[ci-1].t > dd[di].t {
			sum -= int64(cc[ci-1].t - dd[di].t)
			ci--
			di++
			abi++
		}
	}

	fmt.Println(sum)
	for i := 0; i < ai; i++ {
		fmt.Printf("%d ", aa[i].i)
	}

	for i := 0; i < bi; i++ {
		fmt.Printf("%d ", bb[i].i)
	}

	for i := 0; i < ci; i++ {
		fmt.Printf("%d ", cc[i].i)
	}

	for i := 0; i < di; i++ {
		fmt.Printf("%d ", dd[i].i)
	}
}

func minT(index int, d *Datas) int {
	if index < len(*d) {
		return (*d)[index].t
	}

	return 1000000
}

func minIndex(n1, n2, n3, n4 int) int {
	if n1 <= n2 && n1 <= n3 && n1 <= n4 {
		return 1
	}

	if n2 <= n1 && n2 <= n3 && n2 <= n4 {
		return 2
	}

	if n3 <= n1 && n3 <= n2 && n3 <= n4 {
		return 3
	}

	if n4 <= n1 && n4 <= n2 && n4 <= n3 {
		return 4
	}
	return 0
}

package main

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

type data struct {
	t, i int
	s    int64
}

type Datas []data

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

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

	var n, m, k int

	fmt.Fscanf(reader, "%d %d %d\n", &n, &m, &k)

	aa := make(Datas, 0, n)
	bb := make(Datas, 0, n)
	cc := make(Datas, 0, n)
	dd := make(Datas, 0, n)

	var t, a, b int
	for i := 1; i <= n; i++ {
		fmt.Fscanf(reader, "%d %d %d\n", &t, &a, &b)

		if a == 1 && b == 1 {
			cc = append(cc, data{t: t, i: i, s: int64(t)})
			continue
		}

		if a == 1 {
			aa = append(aa, data{t: t, i: i, s: int64(t)})
			continue
		}

		if b == 1 {
			bb = append(bb, data{t: t, i: i, s: int64(t)})
			continue
		}

		if a == 0 && b == 0 {
			dd = append(dd, data{t: t, i: i, s: int64(t)})
		}
	}

	if len(aa)+len(cc) < k || len(bb)+len(cc) < k ||
		2*k-len(cc) > m {
		fmt.Println(-1)
		return
	}

	sort.Sort(aa)
	sort.Sort(bb)
	sort.Sort(cc)
	sort.Sort(dd)

	for i := 1; i < len(aa); i++ {
		aa[i].s += aa[i-1].s
	}

	for i := 1; i < len(bb); i++ {
		bb[i].s += bb[i-1].s
	}

	for i := 1; i < len(cc); i++ {
		cc[i].s += cc[i-1].s
	}

	for i := 1; i < len(dd); i++ {
		dd[i].s += dd[i-1].s
	}

	sum := int64(1 << 60)
	ai := 0
	bi := 0
	ci := 0
	di := 0

	for cci := 0; cci <= len(cc); cci++ {
		tempSum := int64(0)
		if cci > 0 {
			tempSum = cc[cci-1].s
		}

		aai := max(k-cci, 0)
		bbi := aai

		for ; aai <= len(aa) && cci+aai <= m; aai++ {
			if aai > 0 {
				tempSum += aa[aai-1].s
			}
			for ; bbi <= len(bb) && cci+aai+bbi <= m; bbi++ {
				if bbi > 0 {
					tempSum += bb[bbi-1].s
				}

				ddi := m - (cci + aai + bbi)
				if ddi < 0 || ddi > len(dd) {
					break
				}

				if ddi > 0 {
					tempSum += dd[ddi-1].s
				}

				if tempSum < sum {
					sum = tempSum
					ai = aai
					bi = bbi
					ci = cci
					di = ddi
				}
			}
		}
	}

	if ai+bi+ci+di == 0 {
		fmt.Println(-1)
		return
	}

	fmt.Println(sum)
	for i := 0; i < ai; i++ {
		fmt.Printf("%d ", aa[i].i)
	}

	for i := 0; i < bi; i++ {
		fmt.Printf("%d ", bb[i].i)
	}

	for i := 0; i < ci; i++ {
		fmt.Printf("%d ", cc[i].i)
	}

	for i := 0; i < di; i++ {
		fmt.Printf("%d ", dd[i].i)
	}
}

func minT(index int, d *Datas) int {
	if index < len(*d) {
		return (*d)[index].t
	}

	return 1000000
}

func minIndex(n1, n2, n3, n4 int) int {
	if n1 <= n2 && n1 <= n3 && n1 <= n4 {
		return 1
	}

	if n2 <= n1 && n2 <= n3 && n2 <= n4 {
		return 2
	}

	if n3 <= n1 && n3 <= n2 && n3 <= n4 {
		return 3
	}

	if n4 <= n1 && n4 <= n2 && n4 <= n3 {
		return 4
	}
	return 0
}

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

	return y
}

package main

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

type data struct{ t, i int }

type Datas []data

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

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

	var n, m, k int

	fmt.Fscanf(reader, "%d %d %d\n", &n, &m, &k)

	aa := make(Datas, 0, n)
	bb := make(Datas, 0, n)
	cc := make(Datas, 0, n)
	dd := make(Datas, 0, n)

	var t, a, b int
	for i := 1; i <= n; i++ {
		fmt.Fscanf(reader, "%d %d %d\n", &t, &a, &b)

		if a == 1 && b == 1 {
			cc = append(cc, data{t: t, i: i})
			continue
		}

		if a == 1 {
			aa = append(aa, data{t: t, i: i})
			continue
		}

		if b == 1 {
			bb = append(bb, data{t: t, i: i})
			continue
		}

		if a == 0 && b == 0 {
			dd = append(dd, data{t: t, i: i})
		}
	}

	if len(aa)+len(cc) < k || len(bb)+len(cc) < k ||
		2*k-len(cc) > m {
		fmt.Println(-1)
		return
	}

	sort.Sort(aa)
	sort.Sort(bb)
	sort.Sort(cc)
	sort.Sort(dd)

	sum := int64(0)
	abi := 0
	ci := 0
	mm := 0
	for i := 0; i < k; i++ {
		if abi >= len(aa) || abi >= len(bb) ||
			(ci < len(cc) && aa[abi].t+bb[abi].t > cc[ci].t) ||
			mm+1 == m {
			sum += int64(cc[ci].t)
			ci++
			mm++
		} else {
			sum += int64(aa[abi].t + bb[abi].t)
			abi++
			mm += 2
		}
	}

	ai := abi
	bi := abi
	di := 0

	for mm < m {
		// fmt.Println(ai, aa)
		// fmt.Println(bi, bb)
		// fmt.Println(ci, cc)
		// fmt.Println(di, dd, "\n")

		switch minIndex(minT(ai, &aa), minT(bi, &bb), minT(ci, &cc), minT(di, &dd)) {
		case 1:
			sum += int64(aa[ai].t)
			ai++
		case 2:
			sum += int64(bb[bi].t)
			bi++
		case 3:
			sum += int64(cc[ci].t)
			ci++
		case 4:
			sum += int64(dd[di].t)
			di++
		}
		mm++

		if ai > abi && ai > abi && ci > 0 && di < len(dd) && cc[ci-1].t > dd[di].t {
			sum -= int64(cc[ci-1].t - dd[di].t)
			ci--
			di++
			abi++
		}
	}

	fmt.Println(sum)
	for i := 0; i < ai; i++ {
		fmt.Printf("%d ", aa[i].i)
	}

	for i := 0; i < bi; i++ {
		fmt.Printf("%d ", bb[i].i)
	}

	for i := 0; i < ci; i++ {
		fmt.Printf("%d ", cc[i].i)
	}

	for i := 0; i < di; i++ {
		fmt.Printf("%d ", dd[i].i)
	}
}

func minT(index int, d *Datas) int {
	if index < len(*d) {
		return (*d)[index].t
	}

	return 1000000
}

func minIndex(n1, n2, n3, n4 int) int {
	if n1 <= n2 && n1 <= n3 && n1 <= n4 {
		return 1
	}

	if n2 <= n1 && n2 <= n3 && n2 <= n4 {
		return 2
	}

	if n3 <= n1 && n3 <= n2 && n3 <= n4 {
		return 3
	}

	if n4 <= n1 && n4 <= n2 && n4 <= n3 {
		return 4
	}
	return 0
}

package main

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

type data struct {
	t, i int
	s    int64
}

type Datas []data

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

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

	var n, m, k int

	fmt.Fscanf(reader, "%d %d %d\n", &n, &m, &k)

	aa := make(Datas, 0, n)
	bb := make(Datas, 0, n)
	cc := make(Datas, 0, n)
	dd := make(Datas, 0, n)

	var t, a, b int
	for i := 1; i <= n; i++ {
		fmt.Fscanf(reader, "%d %d %d\n", &t, &a, &b)

		if a == 1 && b == 1 {
			cc = append(cc, data{t: t, i: i, s: int64(t)})
			continue
		}

		if a == 1 {
			aa = append(aa, data{t: t, i: i, s: int64(t)})
			continue
		}

		if b == 1 {
			bb = append(bb, data{t: t, i: i, s: int64(t)})
			continue
		}

		if a == 0 && b == 0 {
			dd = append(dd, data{t: t, i: i, s: int64(t)})
		}
	}

	if len(aa)+len(cc) < k || len(bb)+len(cc) < k ||
		2*k-len(cc) > m {
		fmt.Println(-1)
		return
	}

	sort.Sort(aa)
	sort.Sort(bb)
	sort.Sort(cc)
	sort.Sort(dd)

	for i := 1; i < len(aa); i++ {
		aa[i].s += aa[i-1].s
	}

	for i := 1; i < len(bb); i++ {
		bb[i].s += bb[i-1].s
	}

	for i := 1; i < len(cc); i++ {
		cc[i].s += cc[i-1].s
	}

	for i := 1; i < len(dd); i++ {
		dd[i].s += dd[i-1].s
	}

	sum := int64(1 << 60)
	ai := 0
	bi := 0
	ci := 0
	di := 0

	for cci := 0; cci <= len(cc) && cci <= m; cci++ {
		aai := max(k-cci, 0)
		bbi := aai

		for ; aai <= len(aa) && cci+aai <= m; aai++ {
			for ; bbi <= len(bb) && cci+aai+bbi <= m; bbi++ {
				tempSum := int64(0)
				if cci > 0 {
					tempSum += cc[cci-1].s
				}
				if aai > 0 {
					tempSum += aa[aai-1].s
				}
				if bbi > 0 {
					tempSum += bb[bbi-1].s
				}

				ddi := m - (cci + aai + bbi)

				if ddi > len(dd) {
					continue
				}

				if ddi > 0 {
					tempSum += dd[ddi-1].s
				}

				if tempSum < sum {
					sum = tempSum
					ai = aai
					bi = bbi
					ci = cci
					di = ddi
				}
			}
		}
	}

	if ai+bi+ci+di == 0 {
		fmt.Println(-1)
		return
	}

	fmt.Println(sum)
	for i := 0; i < ai; i++ {
		fmt.Printf("%d ", aa[i].i)
	}

	for i := 0; i < bi; i++ {
		fmt.Printf("%d ", bb[i].i)
	}

	for i := 0; i < ci; i++ {
		fmt.Printf("%d ", cc[i].i)
	}

	for i := 0; i < di; i++ {
		fmt.Printf("%d ", dd[i].i)
	}
}

func minT(index int, d *Datas) int {
	if index < len(*d) {
		return (*d)[index].t
	}

	return 1000000
}

func minIndex(n1, n2, n3, n4 int) int {
	if n1 <= n2 && n1 <= n3 && n1 <= n4 {
		return 1
	}

	if n2 <= n1 && n2 <= n3 && n2 <= n4 {
		return 2
	}

	if n3 <= n1 && n3 <= n2 && n3 <= n4 {
		return 3
	}

	if n4 <= n1 && n4 <= n2 && n4 <= n3 {
		return 4
	}
	return 0
}

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

	return y
}

package main

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

const MAX int64 = int64(1 << 60)

type data struct {
	t, i int
	s    int64
}

type Datas []data

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

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

	var n, m, k int

	fmt.Fscanf(reader, "%d %d %d\n", &n, &m, &k)

	aa := make(Datas, 0, n)
	bb := make(Datas, 0, n)
	cc := make(Datas, 0, n)
	dd := make(Datas, 0, n)

	var t, a, b int
	for i := 1; i <= n; i++ {
		fmt.Fscanf(reader, "%d %d %d\n", &t, &a, &b)

		if a == 1 && b == 1 {
			cc = append(cc, data{t: t, i: i, s: int64(t)})
			continue
		}

		if a == 1 {
			aa = append(aa, data{t: t, i: i, s: int64(t)})
			continue
		}

		if b == 1 {
			bb = append(bb, data{t: t, i: i, s: int64(t)})
			continue
		}

		if a == 0 && b == 0 {
			dd = append(dd, data{t: t, i: i, s: int64(t)})
		}
	}

	if len(aa)+len(cc) < k || len(bb)+len(cc) < k ||
		2*k-len(cc) > m {
		fmt.Println(-1)
		return
	}

	sort.Sort(aa)
	sort.Sort(bb)
	sort.Sort(cc)
	sort.Sort(dd)

	for i := 1; i < len(aa); i++ {
		aa[i].s += aa[i-1].s
	}

	for i := 1; i < len(bb); i++ {
		bb[i].s += bb[i-1].s
	}

	for i := 1; i < len(cc); i++ {
		cc[i].s += cc[i-1].s
	}

	for i := 1; i < len(dd); i++ {
		dd[i].s += dd[i-1].s
	}

	sum := MAX
	ai := 0
	bi := 0
	ci := 0
	di := 0

	// for cci := max(0, m-len(aa)-len(bb)-len(dd)); cci <= len(cc) && cci <= m; cci++ {
	// 	sumCc := int64(0)
	// 	if cci > 0 {
	// 		sumCc = cc[cci-1].s
	// 	}
	// 	if sumCc >= sum {
	// 		break
	// 	}
	// 	for aai := max(max(k-cci, 0), m-cci-len(bb)-len(dd)); aai <= len(aa) && cci+aai <= m; aai++ {
	// 		sumAa := sumCc
	// 		if aai > 0 {
	// 			sumAa += aa[aai-1].s
	// 		}
	// 		if sumAa >= sum {
	// 			break
	// 		}
	// 		for bbi := max(max(k-cci, 0), m-cci-aai-len(dd)); bbi <= len(bb) && cci+aai+bbi <= m; bbi++ {
	// 			tempSum := sumAa
	// 			if bbi > 0 {
	// 				tempSum += bb[bbi-1].s
	// 			}

	// 			if tempSum >= sum {
	// 				break
	// 			}

	// 			ddi := m - (cci + aai + bbi)

	// 			if ddi > 0 {
	// 				tempSum += dd[ddi-1].s
	// 			}

	// 			if tempSum < sum {
	// 				sum = tempSum
	// 				ai = aai
	// 				bi = bbi
	// 				ci = cci
	// 				di = ddi
	// 			}
	// 		}
	// 	}
	// }

	f := func(cci int) int64 {
		returnSum := MAX

		sumCc := int64(0)
		if cci > 0 {
			sumCc = cc[cci-1].s
		}

		for aai := max(max(k-cci, 0), m-cci-len(bb)-len(dd)); aai <= len(aa) && cci+aai <= m; aai++ {
			sumAa := sumCc
			if aai > 0 {
				sumAa += aa[aai-1].s
			}

			if sumAa > returnSum {
				break
			}

			for bbi := max(max(k-cci, 0), m-cci-aai-len(dd)); bbi <= len(bb) && cci+aai+bbi <= m; bbi++ {
				tempSum := sumAa
				if bbi > 0 {
					tempSum += bb[bbi-1].s
				}

				if tempSum > returnSum {
					break
				}

				ddi := m - (cci + aai + bbi)

				if ddi > 0 {
					tempSum += dd[ddi-1].s
				}

				if tempSum < sum {
					sum = tempSum
					ai = aai
					bi = bbi
					ci = cci
					di = ddi
				}

				if tempSum < returnSum {
					returnSum = tempSum
				}

				//fmt.Println(tempSum, returnSum, sum)
			}
		}

		return returnSum
	}

	left, right := 0, len(cc)
	for left+3 <= right {
		lmid := left + (right-left)/3
		rmid := right - (right-left)/3

		if f(lmid) >= f(rmid) {
			left = lmid
		} else {
			right = rmid
		}
	}

	for ; left <= right; left++ {
		f(left)
	}

	if ai+bi+ci+di == 0 {
		fmt.Println(-1)
		return
	}

	fmt.Println(sum)
	for i := 0; i < ai; i++ {
		fmt.Printf("%d ", aa[i].i)
	}

	for i := 0; i < bi; i++ {
		fmt.Printf("%d ", bb[i].i)
	}

	for i := 0; i < ci; i++ {
		fmt.Printf("%d ", cc[i].i)
	}

	for i := 0; i < di; i++ {
		fmt.Printf("%d ", dd[i].i)
	}
}

func minT(index int, d *Datas) int {
	if index < len(*d) {
		return (*d)[index].t
	}

	return 1000000
}

func minIndex(n1, n2, n3, n4 int) int {
	if n1 <= n2 && n1 <= n3 && n1 <= n4 {
		return 1
	}

	if n2 <= n1 && n2 <= n3 && n2 <= n4 {
		return 2
	}

	if n3 <= n1 && n3 <= n2 && n3 <= n4 {
		return 3
	}

	if n4 <= n1 && n4 <= n2 && n4 <= n3 {
		return 4
	}
	return 0
}

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

	return y
}

package main

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

const MAX int64 = int64(1 << 60)

type data struct {
	t, i int
	s    int64
}

type Datas []data

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

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

	var n, m, k int

	fmt.Fscanf(reader, "%d %d %d\n", &n, &m, &k)

	aa := make(Datas, 0, n)
	bb := make(Datas, 0, n)
	cc := make(Datas, 0, n)
	dd := make(Datas, 0, n)

	var t, a, b int
	for i := 1; i <= n; i++ {
		fmt.Fscanf(reader, "%d %d %d\n", &t, &a, &b)

		if a == 1 && b == 1 {
			cc = append(cc, data{t: t, i: i, s: int64(t)})
			continue
		}

		if a == 1 {
			aa = append(aa, data{t: t, i: i, s: int64(t)})
			continue
		}

		if b == 1 {
			bb = append(bb, data{t: t, i: i, s: int64(t)})
			continue
		}

		if a == 0 && b == 0 {
			dd = append(dd, data{t: t, i: i, s: int64(t)})
		}
	}

	if len(aa)+len(cc) < k || len(bb)+len(cc) < k ||
		2*k-len(cc) > m {
		fmt.Println(-1)
		return
	}

	sort.Sort(aa)
	sort.Sort(bb)
	sort.Sort(cc)
	sort.Sort(dd)

	for i := 1; i < len(aa); i++ {
		aa[i].s += aa[i-1].s
	}

	for i := 1; i < len(bb); i++ {
		bb[i].s += bb[i-1].s
	}

	for i := 1; i < len(cc); i++ {
		cc[i].s += cc[i-1].s
	}

	for i := 1; i < len(dd); i++ {
		dd[i].s += dd[i-1].s
	}

	sum := MAX
	ai := 0
	bi := 0
	ci := 0
	di := 0

	// for cci := max(0, m-len(aa)-len(bb)-len(dd)); cci <= len(cc) && cci <= m; cci++ {
	// 	sumCc := int64(0)
	// 	if cci > 0 {
	// 		sumCc = cc[cci-1].s
	// 	}
	// 	if sumCc >= sum {
	// 		break
	// 	}
	// 	for aai := max(max(k-cci, 0), m-cci-len(bb)-len(dd)); aai <= len(aa) && cci+aai <= m; aai++ {
	// 		sumAa := sumCc
	// 		if aai > 0 {
	// 			sumAa += aa[aai-1].s
	// 		}
	// 		if sumAa >= sum {
	// 			break
	// 		}
	// 		for bbi := max(max(k-cci, 0), m-cci-aai-len(dd)); bbi <= len(bb) && cci+aai+bbi <= m; bbi++ {
	// 			tempSum := sumAa
	// 			if bbi > 0 {
	// 				tempSum += bb[bbi-1].s
	// 			}

	// 			if tempSum >= sum {
	// 				break
	// 			}

	// 			ddi := m - (cci + aai + bbi)

	// 			if ddi > 0 {
	// 				tempSum += dd[ddi-1].s
	// 			}

	// 			if tempSum < sum {
	// 				sum = tempSum
	// 				ai = aai
	// 				bi = bbi
	// 				ci = cci
	// 				di = ddi
	// 			}
	// 		}
	// 	}
	// }

	f := func(cci int) int64 {
		returnSum := MAX

		sumCc := int64(0)
		if cci > 0 {
			sumCc = cc[cci-1].s
		}

		for aai := max(max(k-cci, 0), m-cci-len(bb)-len(dd)); aai <= len(aa) && cci+aai <= m; aai++ {
			sumAa := sumCc
			if aai > 0 {
				sumAa += aa[aai-1].s
			}

			if sumAa > returnSum {
				break
			}

			for bbi := max(max(k-cci, 0), m-cci-aai-len(dd)); bbi <= len(bb) && cci+aai+bbi <= m; bbi++ {
				tempSum := sumAa
				if bbi > 0 {
					tempSum += bb[bbi-1].s
				}

				if tempSum > returnSum {
					break
				}

				ddi := m - (cci + aai + bbi)

				if ddi > 0 {
					tempSum += dd[ddi-1].s
				}

				if tempSum < sum {
					sum = tempSum
					ai = aai
					bi = bbi
					ci = cci
					di = ddi
				}

				if tempSum < returnSum {
					returnSum = tempSum
				}
			}
		}

		return returnSum
	}

	left, right := 0, len(cc)
	for left+3 <= right {
		lmid := left + (right-left)/3
		rmid := right - (right-left)/3

		if f(lmid) > f(rmid) {
			left = lmid
		} else {
			right = rmid
		}
	}

	for ; left <= right; left++ {
		f(left)
	}

	if ai+bi+ci+di == 0 {
		fmt.Println(-1)
		return
	}

	fmt.Println(sum)
	for i := 0; i < ai; i++ {
		fmt.Printf("%d ", aa[i].i)
	}

	for i := 0; i < bi; i++ {
		fmt.Printf("%d ", bb[i].i)
	}

	for i := 0; i < ci; i++ {
		fmt.Printf("%d ", cc[i].i)
	}

	for i := 0; i < di; i++ {
		fmt.Printf("%d ", dd[i].i)
	}
}

func minT(index int, d *Datas) int {
	if index < len(*d) {
		return (*d)[index].t
	}

	return 1000000
}

func minIndex(n1, n2, n3, n4 int) int {
	if n1 <= n2 && n1 <= n3 && n1 <= n4 {
		return 1
	}

	if n2 <= n1 && n2 <= n3 && n2 <= n4 {
		return 2
	}

	if n3 <= n1 && n3 <= n2 && n3 <= n4 {
		return 3
	}

	if n4 <= n1 && n4 <= n2 && n4 <= n3 {
		return 4
	}
	return 0
}

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

	return y
}


You are given a string $$$s$$$ consisting only of first $$$20$$$ lowercase Latin letters ('a', 'b', ..., 't').Recall that the substring $$$s[l; r]$$$ of the string $$$s$$$ is the string $$$s_l s_{l + 1} \dots s_r$$$. For example, the substrings of "codeforces" are "code", "force", "f", "for", but not "coder" and "top".You can perform the following operation no more than once: choose some substring $$$s[l; r]$$$ and reverse it (i.e. the string $$$s_l s_{l + 1} \dots s_r$$$ becomes $$$s_r s_{r - 1} \dots s_l$$$).Your goal is to maximize the length of the maximum substring of $$$s$$$ consisting of distinct (i.e. unique) characters.The string consists of distinct characters if no character in this string appears more than once. For example, strings "abcde", "arctg" and "minecraft" consist of distinct characters but strings "codeforces", "abacaba" do not consist of distinct characters.

The only line of the input contains one string $$$s$$$ consisting of no more than $$$10^6$$$ characters 'a', 'b', ..., 't' (first $$$20$$$ lowercase Latin letters).

Print one integer — the maximum possible length of the maximum substring of $$$s$$$ consisting of distinct characters after reversing no more than one its substring.

package main

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

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

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...)
	}
}

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

func main() {
	defer writer.Flush()
	var s string
	Scanf("%s", &s)

	maxSubmaskCnt := make([]int, 1<<20)

	// Find all substrings.
	for i := 0; i < len(s); i++ {
		var seenMask uint
		for j := i; j < len(s); j++ {
			m := uint(1) << (s[j] - 'a')
			if seenMask&m > 0 {
				break
			}
			seenMask |= m
			maxSubmaskCnt[seenMask] = bits.OnesCount(seenMask)
		}
	}

	// Run dynamic programming algoritm.
	for mask := 0; mask < 1<<20; mask++ {
		for bit := uint(0); bit < 20; bit++ {
			m := 1 << bit
			if mask&m == 0 {
				continue
			}
			maxSubmaskCnt[mask] = max(maxSubmaskCnt[mask], maxSubmaskCnt[mask&(^m)])
		}
	}

	// Finally, for each mask, find out the  max combination.
	maxComb := 0
	for mask := 0; mask < 1<<20; mask++ {
		mc := maxSubmaskCnt[mask] + maxSubmaskCnt[mask^1048575]
		if mc > maxComb {
			maxComb = mc
		}
	}
	Printf("%d", maxComb)
}

package main

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

func main() {

	reader := bufio.NewReader(os.Stdin)

	var s string
	fmt.Fscanf(reader, "%s\n", &s)
	const LEN int = 1 << 20
	a := make([]int, LEN)

	for i := 0; i < len(s); i++ {
		sum := 0
		temp := 0
		for j := i; j < len(s); j++ {
			temp = 1 << (s[j] - 'a')
			if (temp & sum) > 0 {
				break
			}
			sum += temp
			a[sum] = j - i + 1
		}
	}

	for i := 0; i < 20; i++ {
		for j := 0; j < LEN; j++ {
			k := j | (1 << uint(i))
			if a[k] < a[j] {
				a[k] = a[j]
			}
		}
	}

	ans := 0
	temp := 0
	for i := 0; i < LEN; i++ {
		temp = a[i] + a[LEN-1-i]
		if ans < temp {
			ans = temp
		}
	}

	fmt.Printf("%d\n", ans)
}


Let's denote that some array $$$b$$$ is bad if it contains a subarray $$$b_l, b_{l+1}, \dots, b_{r}$$$ of odd length more than $$$1$$$ ($$$l < r$$$ and $$$r - l + 1$$$ is odd) such that $$$\forall i \in \{0, 1, \dots, r - l\}$$$ $$$b_{l + i} = b_{r - i}$$$.If an array is not bad, it is good.Now you are given an array $$$a_1, a_2, \dots, a_n$$$. Some elements are replaced by $$$-1$$$. Calculate the number of good arrays you can obtain by replacing each $$$-1$$$ with some integer from $$$1$$$ to $$$k$$$.Since the answer can be large, print it modulo $$$998244353$$$.

The first line contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n, k \le 2 \cdot 10^5$$$) — the length of array $$$a$$$ and the size of "alphabet", i. e., the upper bound on the numbers you may use to replace $$$-1$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$a_i = -1$$$ or $$$1 \le a_i \le k$$$) — the array $$$a$$$.

Print one integer — the number of good arrays you can get, modulo $$$998244353$$$.

package main

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

var p int64 = 998244353
var k int64

func countE(l, r, n int, mat []int) int64 {
	//fmt.Printf("l=%d r=%d\n",l,r)
	var ret int64 = 1

	if l < 0 || r >= n {
		if l < 0 && r >= n {
			ret = k
		} else {
			ret = k - 1
		}
		for i := l + 4; i < r; i+=2 {
			ret = (ret * (k - 1)) % p
		}
//		fmt.Printf("ret=%d\n",ret)
		return ret
	}
	if l+4 == r {
		if mat[l] == mat[r] {
			return k - 1
		} else {
			return k - 2
		}
	}
	var noEnd, end int64
	if mat[l] == mat[r] {
		noEnd = k - 1
		end = 0
	} else {
		noEnd = k - 2
		end = 1
	}
	for i := l + 4; i < r; i+=2 {
		tmp := noEnd
		noEnd=((end*(k-1))%p+(noEnd*(k-2))%p)%p
		end = tmp
	}
	ret = noEnd%p
	return ret
}
func Solve(st, n int, mat []int) int64 {

	var ret int64 = 1
	var pre int = st - 2
	for i := st; i <= n+1; i += 2 {
		if i >= n || mat[i] != -1 {
			if i < n && i-2 >= 0 && mat[i-2] != -1 && mat[i] == mat[i-2] {
			//	fmt.Printf("0\n")
				return 0
			}
			if pre == i-2 {
				pre = i
				continue
			}
			ret = (ret * countE(pre, i, n, mat)) % p
			pre = i
		}
	}
	return ret
}
func main() {
//	fmt.Printf("%d\n",(((199999*(200000))%p)*199999)%p)
	buf := bufio.NewReader(os.Stdin)
	var n int

	fmt.Fscanf(buf, "%d %d\n", &n, &k)
	mat := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(buf, "%d", &mat[i])
	}
	fmt.Fscanf(buf, "\n")
	ans := Solve(0, n, mat)
//	fmt.Printf("ans1=%d\n",ans)
	if ans == 0 {
		fmt.Printf("0\n")
		return
	} else {
		fmt.Printf("%d\n", (ans*Solve(1, n, mat))%p)
	}

}

//In the Name of God
//Ya Ali

package main

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

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

func print(a ...interface{}) { fmt.Fprint(writer, a...) }
func scan(a ...interface{})  { fmt.Fscan(reader, a...) }

const (
	MAXN int = 200200
	M    int = 998244353
)

var (
	n, k int
	dp   [MAXN][2]int
	kp   [MAXN]int
)

func mul(x, y int) int {
	return int(int64(x) * int64(y) % int64(M))
}

func power(x, y int) int {
	ret := 1
	for i := 0; i < y; i ++ {
		ret = mul(ret, x)
	}
	return ret
}

func solve(a []int) (res int) {
	var b []int
	for i, x := range a {
		if x != -1 {
			b = append(b, i)
		}
	}
	if len(b) == 0 {
		return mul(power(k-1, len(a)-1), k)
	}
	res = power(k-1, b[0]+len(a)-b[len(b)-1]-1)
	for i := 1; i < len(b); i ++ {
		same := 0
		if a[b[i]] == a[b[i-1]] {
			same = 1
		}
		res = mul(res, dp[b[i]-b[i-1]-1][same])
	}
	return
}

func main() {
	scan(&n, &k)

	var a [2][]int

	for i := 0; i < n; i ++ {
		var x int
		scan(&x)
		a[i%2] = append(a[i%2], x)
	}

	dp[0][0] = 1
	kp[0] = 1
	for i := 1; i < MAXN; i ++ {
		dp[i][0] = (dp[i-1][1] + mul(dp[i-1][0], k-2)) % M
		dp[i][1] = mul(dp[i-1][0], k-1)
		kp[i] = mul(kp[i-1], k)
	}

	print(mul(solve(a[0]), solve(a[1])))

	writer.Flush()
	return
}

//In the Name of God
//Ya Ali

package main

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

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

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

const (
	MAXN int = 200200
	M    int = 998244353
)

var (
	n, k int
	dp   [MAXN][2]int
	kp   [MAXN]int
)

func mul(x, y int) int {
	return int(int64(x) * int64(y) % int64(M))
}

func power(x, y int) int {
	ret := 1
	for i := 0; i < y; i ++ {
		ret = mul(ret, x)
	}
	return ret
}

func solve(a []int) (res int) {
	var b []int
	for i, x := range a {
		if x != -1 {
			b = append(b, i)
		}
	}
	if len(b) == 0 {
		return mul(power(k-1, len(a)-1), k)
	}
	res = power(k-1, b[0]+len(a)-b[len(b)-1]-1)
	for i := 1; i < len(b); i ++ {
		same := 0
		if a[b[i]] == a[b[i-1]] {
			same = 1
		}
		res = mul(res, dp[b[i]-b[i-1]-1][same])
	}
	return
}

func main() {
	scanf("%d%d\n", &n, &k)

	var a [2][]int

	for i := 0; i < n; i ++ {
		var x int
		scanf("%d", &x)
		a[i%2] = append(a[i%2], x)
	}

	dp[0][0] = 1
	kp[0] = 1
	for i := 1; i < MAXN; i ++ {
		dp[i][0] = (dp[i-1][1] + mul(dp[i-1][0], k-2)) % M
		dp[i][1] = mul(dp[i-1][0], k-1)
		kp[i] = mul(kp[i-1], k)
	}

	printf("%d\n", mul(solve(a[0]), solve(a[1])))

	writer.Flush()
	return
}

package main

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

var p int64 = 998244353
var k int64

func countE(l, r, n int, mat []int) int64 {
	//fmt.Printf("l=%d r=%d\n",l,r)
	var ret int64 = 1

	if l < 0 || r >= n {
		if l < 0 && r >= n {
			ret = k
		} else {
			ret = k - 1
		}
		for i := l + 4; i < r; i+=2 {
			ret = (ret * (k - 1)) % p
		}
//		fmt.Printf("ret=%d\n",ret)
		return ret
	}
	if l+4 == r {
		if mat[l] == mat[r] {
			return k - 1
		} else {
			return k - 2
		}
	}
	var noEnd, end int64
	if mat[l] == mat[r] {
		noEnd = k - 1
		end = 0
	} else {
		noEnd = k - 2
		end = 1
	}
	for i := l + 4; i < r; i+=2 {
		tmp := noEnd
		noEnd=((end*(k-1))%p+(noEnd*(k-2))%p)%p
		end = tmp
	}
	ret = noEnd%p
	return ret
}
func Solve(st, n int, mat []int) int64 {

	var ret int64 = 1
	var pre int = st - 2
	for i := st; i <= n+1; i += 2 {
		if i >= n || mat[i] != -1 {
			if i < n && i-2 >= 0 && mat[i-2] != -1 && mat[i] == mat[i-2] {
				fmt.Printf("0\n")
				return 0
			}
			if pre == i-2 {
				pre = i
				continue
			}
			ret = (ret * countE(pre, i, n, mat)) % p
			pre = i
		}
	}
	return ret
}
func main() {
//	fmt.Printf("%d\n",(((199999*(200000))%p)*199999)%p)
	buf := bufio.NewReader(os.Stdin)
	var n int

	fmt.Fscanf(buf, "%d %d\n", &n, &k)
	mat := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(buf, "%d", &mat[i])
	}
	fmt.Fscanf(buf, "\n")
	ans := Solve(0, n, mat)
//	fmt.Printf("ans1=%d\n",ans)
	if ans == 0 {
		fmt.Printf("0\n")
		return
	} else {
		fmt.Printf("%d\n", (ans*Solve(1, n, mat))%p)
	}

}

package main

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

var p int64 = 998244353
var k int64

func countE(l, r, n int, mat []int) int64 {
	//fmt.Printf("l=%d r=%d\n",l,r)
	var ret int64 = 1

	if l < 0 || r >= n {
		if l < 0 && r >= n {
			ret = k
		} else {
			ret = k - 1
		}
		for i := l + 4; i < r; i+=2 {
			ret = (ret * (k - 1)) % p
		}
//		fmt.Printf("ret=%d\n",ret)
		return ret
	}
	if l+4 == r {
		if mat[l] == mat[r] {
			return k - 1
		} else {
			return k - 2
		}
	}
	var noEnd, end int64
	if mat[l] == mat[r] {
		noEnd = k - 1
		end = 0
	} else {
		noEnd = k - 2
		end = 1
	}
	for i := l + 4; i < r; i+=2 {
		tmp := noEnd
		noEnd=((end*(k-1))%p+(noEnd*(k-2))%p)%p
		end = tmp
	}
	ret = noEnd%p
	return ret
}
func Solve(st, n int, mat []int) int64 {

	var ret int64 = 1
	var pre int = st - 2
	for i := st; i <= n+1; i += 2 {
		if i >= n || mat[i] != -1 {
			if i < n && i-2 >= 0 && mat[i-2] != -1 && mat[i] == mat[i-2] {
				fmt.Printf("0\n")
				return 0
			}
			if pre == i-2 {
				pre = i
				continue
			}
			ret = (ret * countE(pre, i, n, mat)) % p
			pre = i
		}
	}
	return ret
}
func main() {
//	fmt.Printf("%d\n",(((199999*(200000))%p)*199999)%p)
	buf := bufio.NewReader(os.Stdin)
	var n int

	fmt.Fscanf(buf, "%d %d\n", &n, &k)
	mat := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(buf, "%d", &mat[i])
	}
	fmt.Fscanf(buf, "\n")
	ans := Solve(0, n, mat)
//	fmt.Printf("ans1=%d\n",ans)
	if ans == 0 {
		fmt.Printf("0\n")
	} else {
		fmt.Printf("%d\n", (ans*Solve(1, n, mat))%p)
	}

}

package main

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

func Solve(st, loop ,p int64)int64  {
	if loop > st{
		return 0
	}
	var ret int64 = 1
	for i:=int64(0);i < loop;i++{
		ret = (ret*st)%p
		st--
	}
	return ret
}
func main() {
	buf := bufio.NewReader(os.Stdin)
	var n ,k int
	var f bool
	var p int64 = 998244353
	fmt.Fscanf(buf, "%d %d\n", &n, &k)
	mat := make([]int64, n)
	flag1 := make([]bool,k+1)
	flag2 := make([]bool,k+1)
	if k < (n+1)/2{
		f = true
	}
	var c1,c2 int64
	for i := 0; i < n; i++ {
		fmt.Fscanf(buf, "%d", &mat[i])
		if f == true||mat[i] == -1{
			if i%2==0{
				c1++
			}else{
				c2++
			}
			continue
		}
		if (i%2)==0{
			if flag1[mat[i]]{
				f=true
			//	fmt.Printf("000 i=%d %d\n",i,mat[i])
			}
			flag1[mat[i]] = true
		}else{
			if flag2[mat[i]]{
				f=true
				//fmt.Printf("111\n")
			}
			flag2[mat[i]] = true
		}
	}
	fmt.Fscanf(buf, "\n")
	if f == true{
		fmt.Printf("0\n")
		return
	}
	var cnt1,cnt2 int64
	for i:= 1;i <= k;i++{
		if flag1[i]==false{
			cnt1++
		}
		if flag2[i] == false{
			cnt2++
		}
	}
	//fmt.Printf("%d %d\n",cnt1,cnt2)
	var ans int64 = 0
	ans = Solve(cnt1,c1,p)
	ans = (ans*Solve(cnt2,c2,p))%p
	fmt.Printf("%d\n", ans)

}

/*
1 10
10
1 1 2 1 1 1 2 1 1 1
1 1 1 1 1 1 1 1 1 1
1
1 1
*/

//In the Name of God
//Ya Ali

package main

import (
	"fmt"
)

const (
	MAXN int = 200200
	M int = 998244353
)

var (
	n, k int
	dp [MAXN][2]int
	kp [MAXN]int
)

func mul(x, y int) int {
	return int(int64(x) * int64(y) % int64(M))
}

func power(x, y int) int {
	ret := 1
	for i := 0; i < y; i ++ {
		ret = mul(ret, x)
	}
	return ret
}

func solve(a []int) (res int) {
	var b []int
	for i, x := range a {
		if x != -1 {
			b = append(b, i)
		}
	}
	if len(b) == 0 {
		return mul(power(k - 1, len(a) - 1), k)
	}
	res = power(k - 1, b[0] + len(a) - b[len(b) - 1] - 1)
	for i := 1; i < len(b); i ++ {
		same := 0
		if a[b[i]] == a[b[i - 1]] {
			same = 1
		}
		res = mul(res, dp[b[i] - b[i - 1] - 1][same])
	}
	return
}

func main() {
	fmt.Scanf("%d%d", &n, &k)

	var a [2][]int

	for i := 0; i < n; i ++ {
		var x int
		fmt.Scanf("%d", &x)
		a[i%2] = append(a[i%2], x)
	}

	dp[0][0] = 1
	kp[0] = 1
	for i := 1; i < MAXN; i ++ {
		dp[i][0] = (dp[i - 1][1] + mul(dp[i - 1][0], k - 2)) % M
		dp[i][1] = mul(dp[i - 1][0], k - 1)
		kp[i] = mul(kp[i - 1], k)
	}

	fmt.Println(mul(solve(a[0]), solve(a[1])))

	return
}