Problem:
Score : 600 points

Problem Statement

Takahashi is playing a board game called Sugoroku.

On the board, there are N + 1 squares numbered 0 to N. Takahashi starts at Square 0, and he has to stop exactly at Square N to win the game.

The game uses a roulette with the M numbers from 1 to M. In each turn, Takahashi spins the roulette. If the number x comes up when he is at Square s, he moves to Square s+x. If this makes him go beyond Square N, he loses the game.

Additionally, some of the squares are Game Over Squares. He also loses the game if he stops at one of those squares. You are given a string S of length N + 1, representing which squares are Game Over Squares. For each i (0 \leq i \leq N), Square i is a Game Over Square if S[i] = 1 and not if S[i] = 0.

Find the sequence of numbers coming up in the roulette in which Takahashi can win the game in the fewest number of turns possible. If there are multiple such sequences, find the lexicographically smallest such sequence. If Takahashi cannot win the game, print -1.

Constraints

1 \leq N \leq 10^5

1 \leq M \leq 10^5

|S| = N + 1

S consists of 0 and 1.

S[0] = 0

S[N] = 0

Input

Input is given from Standard Input in the following format:

N M
S

Output

If Takahashi can win the game, print the lexicographically smallest sequence among the shortest sequences of numbers coming up in the roulette in which Takahashi can win the game, with spaces in between.

If Takahashi cannot win the game, print -1.

Sample Input 1

9 3
0001000100

Sample Output 1

1 3 2 3

If the numbers 1, 3, 2, 3 come up in this order, Takahashi can reach Square 9 via Square 1, 4, and 6. He cannot reach Square 9 in three or fewer turns, and this is the lexicographically smallest sequence in which he reaches Square 9 in four turns.

Sample Input 2

5 4
011110

Sample Output 2

-1

Takahashi cannot reach Square 5.

Sample Input 3

6 6
0101010

Sample Output 3

6

Solution in Kotlin (1.0.0) (judge verdict: Accepted)

import java.io.*
import java.util.*

fun solve(N: Int, M: Int, S: String) {
    // startからゴールまで辿り着く最短ステップ数
    // dp[i] = min(dp[i+1..i+M]) + 1
    val sqrt = Math.sqrt(N.toDouble()).toInt() + 1
    val dp = Array(sqrt * sqrt) { MinMonoid.empty() }
    dp[N] = 0
    val minQuery = SqrtDecomposition(dp, MinMonoid)
    for (i in N - 1 downTo 0) {
        if (S[i] == '1') continue
        val mstep = minQuery.query(i + 1, Math.min(i + M + 1, N + 1))
        if (mstep == MinMonoid.empty()) { println(-1); return }
        minQuery.update(i, mstep + 1)
    }

    val ans = mutableListOf<Int>()
    var lastIndex = 0
    var step = minQuery.get(0)
    for (i in 1..N) {
        if (minQuery.get(i) == step - 1) {
            ans.add(i - lastIndex)
            lastIndex = i
            step--
        }
    }
    println(ans.joinToString(separator = " "))
    // println(minQuery.array.toList())
    return
}

fun main(args: Array<String>) {
    fun StringArray(size: Int, init: (Int) -> String = { "\u0000" }): Array<String> {
        return Array<String>(size, init)
    }
    class Scanner(stream: InputStream) {
        private val reader = BufferedInputStream(stream).bufferedReader()
        private var st: StringTokenizer? = null
        fun next(): String {
            while (!(st?.hasMoreTokens() ?: false)) st = StringTokenizer(reader.readLine())
            return st!!.nextToken()
        }
    }
    val sc = Scanner(System.`in`)
    val N = sc.next().toInt()
    val M = sc.next().toInt()
    val S = sc.next()
    solve(N, M, S)
}

interface Monoid<T> {
    fun combine(a: T, b: T): T
    fun empty(): T
}

object MinMonoid : Monoid<Int> {
    override fun combine(a: Int, b: Int): Int = Math.min(a, b)
    override fun empty(): Int = Int.MAX_VALUE
}

class SqrtDecomposition<T>(private val array: Array<T>, private val monoid: Monoid<T>) {
    private val divSize = Math.sqrt(array.size.toDouble()).toInt()
    private val bucket = (0 until divSize)
            .map {
                array.slice(it * divSize until (it + 1) * divSize)
                        .reduce { a, b -> monoid.combine(a, b) }
            }
            .toMutableList()

    init {
        require(divSize * divSize == array.size) {
            "array size must be square number. size: ${array.size}"
        }
    }

    fun get(index: Int) = array[index]
    fun query(left: Int, right: Int): T {
        // [left, right)
        require(left in (0 until right) && right <= array.size) {
            "Invalid Range [$left, $right)"
        }
        val bl = left / divSize
        val br = right / divSize
        var res = monoid.empty()
        if (bl == br) {
            (left until right).forEach { res = monoid.combine(res, array[it]) }
        } else {
            (left until (bl + 1) * divSize).forEach { res = monoid.combine(res, array[it]) }
            (bl + 1 until br).forEach { res = monoid.combine(res, bucket[it]) }
            (br * divSize until right).forEach { res = monoid.combine(res, array[it]) }
        }
        return res
    }

    fun update(index: Int, value: T) {
        array[index] = value
        val bi = index / divSize
        bucket[bi] = array.slice(bi * divSize until (bi + 1) * divSize)
                .reduce { a, b -> monoid.combine(a, b) }
    }
}