package main

import (
	"bufio"
	. "fmt"
	"io"
	"io/ioutil"
	"os"
	"strings"
)

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

	solve := func(Case int) {
		var s []byte
		Fscan(in, &s)
		ans := int64(0)
		dp := [2]int64{}
		for i, b := range s {
			if i == 0 {
				if b == '0' {
					dp[0] = 1
					dp[1] = 0
				} else if b == '1' {
					dp[1] = 1
					dp[0] = 0
				} else {
					dp[0] = 1
					dp[1] = 1
				}
			} else {
				if b == '0' {
					dp[0] = 1 + dp[1]
					dp[1] = 0
				} else if b == '1' {
					dp[1] = 1 + dp[0]
					dp[0] = 0
				} else {
					dp[0], dp[1] = 1+dp[1], 1+dp[0]
				}
			}
			ans += max(dp[0], dp[1])
		}
		Fprintln(out, ans)
	}

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

	_leftData, _ := ioutil.ReadAll(in)
	if _s := strings.TrimSpace(string(_leftData)); _s != "" {
		panic("有未读入的数据：\n" + _s)
	}
}

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

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

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

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{}) { fmt.Fscanf(reader, f, a...) }
func readline() (string, error) { return reader.ReadString('\n') }

type S struct {
	z, o, b int;
}
func solve() {
	var s string
	scanf("%s\n", &s)
	N := len(s)
	dd := make([]S, N+1)
	dd[0] = S{0, 0, 0}
	for i:=1; i<=N; i++ {
		switch s[i-1] {
		case '0':
			dd[i].z = dd[i-1].o + 1
			dd[i].o = 0
			dd[i].b = 0
		case '1':
			dd[i].z = 0
			dd[i].o = dd[i-1].z + 1
			dd[i].b = 0
		default:
			dd[i].z = dd[i-1].o + 1
			dd[i].o = dd[i-1].z + 1
			dd[i].b = dd[i-1].b + 1
		}
	}
	var ans int64 = 0
	for i:=1; i<=N; i++ {
		ans += int64(dd[i].z + dd[i].o - dd[i].b)
	}
	printf("%d\n", ans)
}

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

package main

import (
	"bufio"
	. "fmt"
	"io"
	"io/ioutil"
	"os"
	"strings"
)

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

	solve := func(Case int) {
		//if Case == 2 {
		//	print()
		//}
		var s []byte
		Fscan(in, &s)
		ans := int64(0)
		dp := [2]int64{}
		for i, b := range s {
			if i == 0 {
				if b == '0' {
					dp[0] = 1
					dp[1] = 0
				} else if b == '1' {
					dp[1] = 1
					dp[0] = 0
				} else {
					dp[0] = 1
					dp[1] = 1
				}
			} else {
				if b == '0' {
					dp[0] = 1 + dp[1]
					dp[1] = 0
				} else if b == '1' {
					dp[1] = 1 + dp[0]
					dp[0] = 0
				} else {
					dp[0], dp[1] = 1+dp[1], 1+dp[0]
				}
			}
			//println(dp[0], dp[1])
			ans += max(dp[0], dp[1])
		}
		//Println()
		Fprintln(out, ans)
	}

	T := 1
	Fscan(in, &T) //
	for Case := 1; Case <= T; Case++ {
		solve(Case)
	}

	_leftData, _ := ioutil.ReadAll(in)
	if _s := strings.TrimSpace(string(_leftData)); _s != "" {
		panic("有未读入的数据：\n" + _s)
	}
}

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

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

package main

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

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

	var t int
	fmt.Fscan(in, &t)
	for ; t > 0; t-- {
		var s string
		fmt.Fscan(in, &s)
		dp := [2]int64{}
		ans := int64(0)
		for _, c := range s {
			switch string(c) {
			case "0":
				dp[0] = dp[1] + 1
				dp[1] = 0
			case "1":
				dp[1] = dp[0] + 1
				dp[0] = 0
			default:
				dp[0], dp[1] = 1+dp[1], 1+dp[0]
			}
			ans += max(dp[0], dp[1])
		}
		fmt.Fprintln(out, ans)
	}
}

func max(x, y int64) int64 {
	if x < y {
		return y
	}
	return x
}

package main

import "fmt"

func main() {
	var t int
	fmt.Scanln(&t)
	for ; t > 0; t-- {
		var s string
		fmt.Scanln(&s)
		dp := [2]int64{}
		ans := int64(0)
		for _, c := range s {
			switch string(c) {
			case "0":
				dp[0] = dp[1] + 1
				dp[1] = 0
			case "1":
				dp[1] = dp[0] + 1
				dp[0] = 0
			default:
				dp[0], dp[1] = 1+dp[1], 1+dp[0]
			}
			ans += max(dp[0], dp[1])
		}
		fmt.Println(ans)
	}
}

func max(x, y int64) int64 {
	if x < y {
		return y
	}
	return x
}

package main

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

func main() {
	io := NewFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	n:= io.NextInt()
	for i := 0; i < n; i++ {
		s := io.Next()
		var ans,k int64
		d:=make([]int64,2)
		for i:=range d{
		    d[i]=-1
		}
		k=0
		for i:=range s{
		    if(s[i]=='0'){
		        d[(i%2)]=k
		    }else if(s[i]=='1'){
		        d[(i%2)^1]=k
		    }
		    ans+=k-min(d[0],d[1])
		    k+=1
		}
		io.Println(ans)
	}
}
func min(i,j int64)int64{
    if(i>j){
        return j
    }
    return i
}

type FastIO struct {
	*bufio.Reader
	*bufio.Writer
	c [1]byte
}

func NewFastIO(r io.Reader, w io.Writer) *FastIO {
	return &FastIO{Reader: bufio.NewReader(r), Writer: bufio.NewWriter(w)}
}

func (f *FastIO) readC() byte {
	f.Read(f.c[:])
	return f.c[0]
}

func (f *FastIO) NextInt() int {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := 0
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}

func (f *FastIO) Next() string {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	var s []byte
	for ; c > ' '; c = f.readC() {
		s = append(s, f.c[0])
	}
	return string(s)
}

func (f *FastIO) Println(a ...interface{}) {
	fmt.Fprintln(f.Writer, a...)
}

package main

import "fmt"

func main() {
	var t int
	fmt.Scanf("%d\n",&t)
	for i := 0; i < t; i++ {
		var s string
		result := int64(0)
		tail := int64(0)
		q := -1
		qcount := 0
		fmt.Scanf("%s\n",&s)
		for j := 0; j < len(s); j++ {
			if s[j] == '?' {
				if q < 0 {
					q = j
					qcount++
					tail = tail + 1
				} else {
					qcount++
					tail = tail + 1
				}
			} else {
				if q < 0 {
					if j == 0 {
						tail = 1
					} else if s[j] == s[j-1] {
						tail = 1
					} else {
						tail = tail + 1
					}
				} else {
					if q == 0 {
						tail = tail + 1
					} else if s[q-1] == s[j] && qcount % 2 == 1 {
						tail = tail + 1
					} else if s[q-1] != s[j] && qcount % 2 == 0 {
						tail = tail + 1
					} else {
						tail = int64(qcount) + 1
					}
					q = -1
					qcount = 0
				}
			}
			result += tail
		}
		fmt.Printf("%d\n",result)
	}
}

package main

import(
	"fmt"
	"bufio"
	"io"
	"os"
	"sort"

)

func run(r io.Reader ,w io.Writer){

	in := bufio.NewReader(r)
	out := bufio.NewWriter(w)

	defer out.Flush()

	solve := func(Case int){

	var str []byte
	fmt.Fscan(in,&str)
	ans := int64(0)
	dp := make([]int64,2)

	for i , v := range str{

		if i == 0{
			if v == '0'{
			dp[0] = 1
			dp[1] = 0
			} else if v == '1'{
				dp[0] = 0
				dp[1] = 1
			}else{
				dp[0] = 1
				dp[1] = 1
			}
		}else {
			if v == '0'{
			dp[0] = 1 + dp[1]
			dp[1] = 0
			}else if v == '1'{
				dp[1] = 1 + dp[0]
				dp[0] = 0

			}else{
				dp[0] , dp[1] = 1+dp[1] , 1+dp[0]

			}

	}
		ans += max(dp[0],dp[1])
	}
	fmt.Fprintln(out , ans)
}

	T := 1

	fmt.Fscan(in ,&T)

	for Case := 1; Case <= T; Case++ {
		solve(Case)
	}

	}

func ArrayReorder(arr []int)[]int{

 sort.Slice(arr , func(a , b int) bool{

 	return arr[a] & 1 == 0   // even and odd sort
 })
 	return arr
}

func GCD(a , b int)int{

	for a != 0{
		a , b = b % a , a
	}

	return b
}

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

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

func main(){

	run(os.Stdin , os.Stdout)
}

package main

import "fmt"

func main() {
	var t int
	fmt.Scanf("%d",&t)
	for i := 0; i < t; i++ {
		var s string
		result := int64(0)
		fmt.Scanf("%s",&s)
		for j := 0; j < len(s); j++ {
			inc := int64(0)
			q := -1
			qcount := 0
			for h := j; h >= 0; h-- {
				if h == j {
					if s[h] == '?' {
						inc++
						q = h
						qcount++
					} else {
						inc++
					}
					continue
				}

				if q >= 0 {
					if s[h] == '?' {
						qcount++
						inc++
					} else {
						if q == j {
							qcount++
							inc++
						} else {
							if s[q+1] == s[h] && qcount % 2 == 1 {
								inc++
							} else if s[q+1] != s[h] && qcount % 2 ==0 {
								inc++
							} else {
								break
							}
						}
						qcount = 0
						q = -1
					}
				} else {
					if s[h] == '?' {
						inc++
						q = h
						qcount++
					} else if s[h] != s[h+1] {
						inc++
					} else {
						break
					}
				}
			}
			result += inc
		}
		fmt.Printf("%d\n",result)
	}
}

package main

import(
	"fmt"
	"bufio"
	"io"
	"os"
	"sort"

)

func run(r io.Reader ,w io.Writer){

	in := bufio.NewReader(r)
	out := bufio.NewWriter(w)

	defer out.Flush()

	solve := func(Case int){

	var str []byte
	fmt.Fscan(in,&str)
	ans := int64(0)
	dp := make([]int64,2)

	for i , v := range str{

		if i == 0{
			if v == '0'{
			dp[0] = 1
			dp[1] = 0
			} else if v == '1'{
				dp[0] = 0
				dp[1] = 1
			}else{
				dp[0] = 1
				dp[1] = 1
			}
		}else {
			if v == '0'{
			dp[0] = 1 + dp[1]
			dp[1] = 0
			}else if v == '1'{
				dp[1] = 1 + dp[0]
				dp[0] = 0

			}else{
				dp[0] , dp[1] = 1+dp[1] , 1+dp[0]

			}
			ans += max(dp[0],dp[1])

	}

	}
	fmt.Fprintln(out , ans)
}

	T := 1

	fmt.Fscan(in ,&T)

	for Case := 1; Case <= T; Case++ {
		solve(Case)
	}

	}

func ArrayReorder(arr []int)[]int{

 sort.Slice(arr , func(a , b int) bool{

 	return arr[a] & 1 == 0   // even and odd sort
 })
 	return arr
}

func GCD(a , b int)int{

	for a != 0{
		a , b = b % a , a
	}

	return b
}

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

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

func main(){

	run(os.Stdin , os.Stdout)
}

package main

import(
	"fmt"
	"bufio"
	"io"
	"os"
	"sort"

)

func run(r io.Reader ,w io.Writer){

	in := bufio.NewReader(r)
	out := bufio.NewWriter(w)

	defer out.Flush()

	solve := func(Case int){

	var str []byte
	fmt.Fscan(in,&str)
	ans := int64(0)
	dp := make([]int64,2)

	for i , v := range str{

		if i == '0'{
			if v == 0{
			dp[0] = 1
			dp[1] = 0
			} else if v == '1'{
				dp[0] = 0
				dp[1] = 1
			}else{
				dp[0] = 1
				dp[1] = 1
			}
		}else {
			if v == '0'{
			dp[0] = 1 + dp[1]
			dp[1] = 0
			}else if v == '1'{
				dp[1] = 1 + dp[0]
				dp[0] = 0

			}else{
				dp[0] , dp[1] = 1+dp[1] , 1+dp[0]

			}
			ans += max(dp[0],dp[1])

	}

	}
	fmt.Fprintln(out , ans)
}

	T := 1

	fmt.Fscan(in ,&T)

	for Case := 1; Case <= T; Case++ {
		solve(Case)
	}

	}

func ArrayReorder(arr []int)[]int{

 sort.Slice(arr , func(a , b int) bool{

 	return arr[a] & 1 == 0   // even and odd sort
 })
 	return arr
}

func GCD(a , b int)int{

	for a != 0{
		a , b = b % a , a
	}

	return b
}

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

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

func main(){

	run(os.Stdin , os.Stdout)
}

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

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{}) { fmt.Fscanf(reader, f, a...) }
func readline() (string, error) { return reader.ReadString('\n') }

type S struct {
	z, o, b int;
}
func solve() {
	s, _ := readline()
	N := len(s)-1
	dd := make([]S, N+1)
	dd[0] = S{0, 0, 0}
	for i:=1; i<=N; i++ {
		switch s[i-1] {
		case '0':
			dd[i].z = dd[i-1].o + 1
			dd[i].o = 0
			dd[i].b = 0
		case '1':
			dd[i].z = 0
			dd[i].o = dd[i-1].z + 1
			dd[i].b = 0
		default:
			dd[i].z = dd[i-1].o + 1
			dd[i].o = dd[i-1].z + 1
			dd[i].b = dd[i-1].b + 1
		}
	}
	var ans int64 = 0
	for i:=1; i<=N; i++ {
		ans += int64(dd[i].z + dd[i].o - dd[i].b)
	}
	printf("%d\n", ans)
}

func main() {
	// defer writer.Flush()
	var tc int
	scanf("%d ", &tc)
	// tc = 1
	for ; tc>0; tc-- {
		solve()
		writer.Flush()
	}
}

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

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{}) { fmt.Fscanf(reader, f, a...) }
func readline() (string, error) { return reader.ReadString('\n') }

type S struct {
	z, o, b int;
}
func solve() {
	s, _ := readline()
	N := len(s)-1
	dd := make([]S, N+1)
	dd[0] = S{0, 0, 0}
	for i:=1; i<=N; i++ {
		switch s[i-1] {
		case '0':
			dd[i].z = dd[i-1].o + 1
			dd[i].o = 0
			dd[i].b = 0
		case '1':
			dd[i].z = 0
			dd[i].o = dd[i-1].z + 1
			dd[i].b = 0
		default:
			dd[i].z = dd[i-1].o + 1
			dd[i].o = dd[i-1].z + 1
			dd[i].b = dd[i-1].b + 1
		}
	}
	ans := 0
	for i:=1; i<=N; i++ {
		ans += dd[i].z + dd[i].o - dd[i].b
	}
	printf("%d\n", ans)
}

func main() {
	// defer writer.Flush()
	var tc int
	scanf("%d ", &tc)
	// tc = 1
	for ; tc>0; tc-- {
		solve()
		writer.Flush()
	}
}

package main

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

func main() {
	io := NewFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	n:= io.NextInt()
	for i := 0; i < n; i++ {
		s := io.Next()
		ans:=0
		d:=make([]int,2)
		for i:=range d{
		    d[i]=-1
		}
		for i:=range s{
		    if(s[i]=='0'){
		        d[(i%2)]=i
		    }else if(s[i]=='1'){
		        d[(i%2)^1]=i
		    }
		    ans+=i-min(d[0],d[1])
		}
		io.Println(ans)
	}
}
func min(i,j int)int{
    if(i>j){
        return j
    }
    return i
}

type FastIO struct {
	*bufio.Reader
	*bufio.Writer
	c [1]byte
}

func NewFastIO(r io.Reader, w io.Writer) *FastIO {
	return &FastIO{Reader: bufio.NewReader(r), Writer: bufio.NewWriter(w)}
}

func (f *FastIO) readC() byte {
	f.Read(f.c[:])
	return f.c[0]
}

func (f *FastIO) NextInt() int {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := 0
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}

func (f *FastIO) Next() string {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	var s []byte
	for ; c > ' '; c = f.readC() {
		s = append(s, f.c[0])
	}
	return string(s)
}

func (f *FastIO) Println(a ...interface{}) {
	fmt.Fprintln(f.Writer, a...)
}


package main

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

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

	var n, k int
	Fscan(in, &n, &k)
	sz := make([]int, n)
	for i := range sz {
		Fscan(in, &sz[i])
	}
	sort.Ints(sz)
	uppCnt := make([]int, k)
	for i := range uppCnt {
		Fscan(in, &uppCnt[i])
	}
	ans := sort.Search(n+1, func(caseNum int) bool {
		cnt := 0
		for i := n - 1; i >= 0; i -= caseNum {
			cnt++
			if cnt > uppCnt[sz[i]-1] {
				return false
			}
		}
		return true
	})
	Fprintln(out, ans)
	for st := 0; st < ans; st++ {
		Fprint(out, (n-1-st)/ans+1)
		for i := st; i < n; i += ans {
			Fprint(out, " ", sz[i])
		}
		Fprintln(out)
	}
}

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

package main

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

func main() {
    in := bufio.NewReader(os.Stdin)
    out := bufio.NewWriter(os.Stdout)
    defer out.Flush()
    var n, k int
    Fscan(in, &n, &k)
    arr := make([]int, n)
    c := make([]int, k)
    for i := 0; i < n; i++ {
        Fscan(in, &arr[i])
    }
    for i := 0; i < k; i++ {
        Fscan(in, &c[i])
    }
    counter := make([]int, k)
    suffix := make([]int, k)
    for i := 0; i < n; i++ {
        counter[arr[i] - 1]++
    }

    for i := k - 1; i >= 0; i-- {
        suffix[i] = counter[i]
        if i + 1 < k {
            suffix[i] += suffix[i + 1]
        }
    }

    answer := 0
    for i := 0; i < k; i++ {
        currentValue := (suffix[i] + c[i] - 1) / c[i]
        if currentValue > answer {
            answer = currentValue
        }
    }
    Fprintln(out, answer)
    result := make([][]int, answer)
    itr := 0
    for i := 0; i < k; i++ {
        for j := 0; j < counter[i]; j++ {
            result[itr % answer] = append(result[itr % answer], i + 1)
            itr++
        }
    }

    for i := 0; i < answer; i++ {
        Fprint(out, len(result[i]), " ");
        for j := 0; j < len(result[i]); j++ {
            Fprint(out, result[i][j], " ");
        }
        Fprint(out, "\n");
    }
}

package main

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

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

	var n, k int
	Fscan(in, &n, &k)
	sz := make([]int, n)
	for i := range sz {
		Fscan(in, &sz[i])
	}
	sort.Ints(sz)
	uppCnt := make([]int, k)
	for i := range uppCnt {
		Fscan(in, &uppCnt[i])
	}
	ans := sort.Search(n+1, func(caseNum int) bool {
		cnt := 0
		for i := n - 1; i >= 0; i -= caseNum {
			cnt++
			if cnt > uppCnt[sz[i]-1] {
				return false
			}
		}
		return true
	})
	Fprintln(out, ans)
	for st := 0; st < ans; st++ {
		a := []interface{}{}
		for i := st; i < n; i += ans {
			a = append(a, sz[i])
		}
		Fprint(out, len(a), " ")
		Fprintln(out, a...)
	}
}

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

package main

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

func main() {
    in := bufio.NewReader(os.Stdin)
    out := bufio.NewWriter(os.Stdout)
    defer out.Flush()
    var n, k int
    Fscan(in, &n, &k)
    arr := make([]int, n)
    c := make([]int, k)
    for i := 0; i < n; i++ {
        Fscan(in, &arr[i])
    }
    for i := 0; i < k; i++ {
        Fscan(in, &c[i])
    }

    counter := make([]int, k)
    suffix := make([]int, k)
    for i := 0; i < n; i++ {
        counter[arr[i] - 1]++
    }

    for i := k - 1; i >= 0; i-- {
        suffix[i] = counter[i]
        if i + 1 < k {
            suffix[i] += suffix[i + 1]
        }
    }

    answer := 0
    for i := 0; i < k; i++ {
        currentValue := (suffix[i] + c[i] - 1) / c[i]
        if currentValue > answer {
            answer = currentValue
        }
    }
    Fprintln(out, answer)
    result := make([][]int, answer)
    for i := 0; i < answer; i++ {
        result[i] = make([]int, (n + answer - 1) / answer)
    }
    itr := 0
    for i := k - 1; i >= 0; i-- {
        for j := 0; j < counter[i]; j++ {
            result[itr % answer][itr / answer] = i + 1
            itr++
        }
    }

    for i := 0; i < answer; i++ {
        size := len(result[i])
        if result[i][size - 1] == 0 {
            size--
        }
        Fprint(out, size, " ")
        for j := 0; j < size; j++ {
            Fprint(out, result[i][j], " ")
        }
        Fprint(out, "\n")
    }
}

package main

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

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

	var n, k int
	Fscan(in, &n, &k)
	sz := make([]int, n+1)
	for i := 1; i <= n; i++ {
		Fscan(in, &sz[i])
	}
	sort.Ints(sz)
	uppCnt := make([]int, k+1)
	for i := 1; i <= k; i++ {
		Fscan(in, &uppCnt[i])
	}

	//min := func(a, b int) int {
	//	if a < b {
	//		return a
	//	}
	//	return b
	//}
	ans := [][]interface{}{}
	for i := 1; i <= n; {
		tmp := []interface{}{sz[i]}

		//curLen := sz[i]
		maxCnt := uppCnt[sz[i]] // uppCnt[curLen]
		r := i + maxCnt
		if r > n {
			r = n
		}
		//pre := maxCnt
		//cnt:=0
		j := i + 1
		for ; j < r; j++ {
			//cnt++
			//if maxCnt == 1 {
			//	break
			//}
			tmp = append(tmp, sz[j])

			if sz[j] > sz[j-1] {
				if s := j + uppCnt[sz[j]]; s < r {
					r = s
				}
			}

			//maxCnt = min(maxCnt, uppCnt[ sz[i]]-1)

		}
		i = j

		//if upp == 1 {
		//	tmp = append(tmp, curLen)
		//	i++
		//} else {
		//	j := i
		//	for ; j <= n; j++ {
		//		if uppCnt[sz[j] ] > sz[j] {
		//			break
		//		}
		//		tmp = append(tmp, sz[j])
		//	}
		//	i = j
		//	//downSz := sz[i+upp-1]
		//	//if downSz <= uppLen[downSz] {
		//	//	j := i
		//	//	for ; j < i+upp; j++ {
		//	//		tmp = append(tmp, sz[j])
		//	//	}
		//	//	i = j
		//	//} else {
		//	//	for curLen++; curLen <= k; curLen++ {
		//	//
		//	//	}
		//	//}
		//}

		ans = append(ans, tmp)
	}

	Fprintln(out, len(ans))
	for _, an := range ans {
		Fprint(out, len(an), " ")
		Fprintln(out, an...)
	}
}

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

package main

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

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

	var n, k int
	Fscan(in, &n, &k)
	sz := make([]int, n+1)
	for i := 1; i <= n; i++ {
		Fscan(in, &sz[i])
	}
	sort.Ints(sz)
	uppCnt := make([]int, k+1)
	for i := 1; i <= k; i++ {
		Fscan(in, &uppCnt[i])
	}

	//min := func(a, b int) int {
	//	if a < b {
	//		return a
	//	}
	//	return b
	//}
	ans := [][]interface{}{}
	for i := 1; i <= n; {
		tmp := []interface{}{sz[i]}

		//curLen := sz[i]
		maxCnt := uppCnt[sz[i]] // uppCnt[curLen]
		r := i + maxCnt
		//if r > n {
		//	r = n
		//}
		//pre := maxCnt
		//cnt:=0
		j := i + 1
		for ; j < r; j++ {
			if j>n {
				break
			}
			//cnt++
			//if maxCnt == 1 {
			//	break
			//}
			tmp = append(tmp, sz[j])

			if sz[j] > sz[j-1] {
				if s := j + uppCnt[sz[j]]; s < r {
					r = s
				}
			}

			//maxCnt = min(maxCnt, uppCnt[ sz[i]]-1)

		}
		i = j

		//if upp == 1 {
		//	tmp = append(tmp, curLen)
		//	i++
		//} else {
		//	j := i
		//	for ; j <= n; j++ {
		//		if uppCnt[sz[j] ] > sz[j] {
		//			break
		//		}
		//		tmp = append(tmp, sz[j])
		//	}
		//	i = j
		//	//downSz := sz[i+upp-1]
		//	//if downSz <= uppLen[downSz] {
		//	//	j := i
		//	//	for ; j < i+upp; j++ {
		//	//		tmp = append(tmp, sz[j])
		//	//	}
		//	//	i = j
		//	//} else {
		//	//	for curLen++; curLen <= k; curLen++ {
		//	//
		//	//	}
		//	//}
		//}

		ans = append(ans, tmp)
	}

	Fprintln(out, len(ans))
	for _, an := range ans {
		Fprint(out, len(an), " ")
		Fprintln(out, an...)
	}
}

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

package main

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

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

	var n, k int
	Fscan(in, &n, &k)
	sz := make([]int, n+1)
	for i := 1; i <= n; i++ {
		Fscan(in, &sz[i])
	}
	sort.Ints(sz)
	uppCnt := make([]int, k+1)
	for i := 1; i <= k; i++ {
		Fscan(in, &uppCnt[i])
	}

	ans := [][]interface{}{}
	for i := 1; i <= n; {
		tmp := []interface{}{sz[i]}

		r := i + uppCnt[sz[i]]
		j := i + 1
		for ; j < r; j++ {
			if j > n {
				break
			}
			tmp = append(tmp, sz[j])
			if sz[j] > sz[j-1] {
				if s := j + uppCnt[sz[j]]; s < r {
					r = s
				}
			}
		}
		i = j

		ans = append(ans, tmp)
	}

	Fprintln(out, len(ans))
	for _, an := range ans {
		Fprint(out, len(an), " ")
		Fprintln(out, an...)
	}
}

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