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 Rust (1.42.0) (judge verdict: Accepted)

use std::io::stdin;
use std::str::FromStr;

fn main(){
    let (n, m): (usize, usize) = get_pair();
    let s = get_chars();
    let ans = solve(n, m, &s);
    if let Some(v) = ans {
        print!("{}", v[0]);
        for i in 1..v.len() {
            print!(" {}", v[i]);
        }
        println!("");
    } else {
        println!("-1");
    }
}

fn solve(n: usize, m: usize, s: &Vec<char>) -> Option<Vec<usize>> {
    let mut v = vec![Z(None); n];
    v.push(Z(Some(Elem{
        cost: 0,
        current: n,
        after: 0,
    })));
    let mut st = SegmentTree::new(&mut v);
    for i in (0..n).rev() {
        if s[i] == '1'{ continue; }

        if let Z(Some(elem)) = st.mconcat(i+1, i+m+1).clone() {
            st.update(i, Z(Some(Elem{
                cost: elem.cost + 1,
                current: i,
                after: elem.current,
            })));
        }
    }
    if let Z(Some(elem)) = st.mconcat(0, 1).clone() {
        let mut ans = Vec::new();
        let mut c = elem;
        while c.current < n {
            ans.push(c.after - c.current);
            c = st.mconcat(c.after, c.after+1).clone().0.unwrap();
        }
        Some(ans)
    } else {
        None
    }
}


trait Monoid {
    fn mappend(&self, b: &Self) -> Self;
    fn mempty() -> Self;
}

struct Elem {
    cost: usize,
    current: usize,
    after: usize,
}

struct Z(Option<Elem>);

impl Monoid for Z {
    fn mappend(&self, b: &Self) -> Self {
        match &self.0 {
            None => b.clone(),
            Some(elem_a) => match &b.0 {
                None => self.clone(),
                Some(elem_b) => {
                    if elem_a.cost < elem_b.cost {
                        self.clone()
                    } else if elem_a.cost > elem_b.cost {
                        b.clone()
                    } else {
                        if elem_a.current < elem_b.current {
                            self.clone()
                        } else {
                            b.clone()
                        }
                    }
                },
            }
        }
    }
    fn mempty() -> Self {
        Z(None)
    }
}

impl Clone for Z {
    fn clone(&self) -> Self {
        match self.0 {
            None => Z(None),
            Some(Elem{cost, current, after}) => Z(Some(Elem{cost, current, after})),
        }
    }
}

struct SegmentTree<M: Monoid + Clone> {
    value: M,
    start: usize,
    end: usize,
    children: Option<Box<SegmentTreeChildren<M>>>
}

struct SegmentTreeChildren<M: Monoid + Clone> {
    head: SegmentTree<M>,
    tail: SegmentTree<M>
}

impl<M> SegmentTree<M> where M: Monoid + Clone {
    fn new(vec: &Vec<M>) -> SegmentTree<M> {
        SegmentTree::_build(vec, 0, vec.len())
    }

    fn mconcat(&self, i: usize, j: usize) -> M {
        if j <= self.start || self.end <=i {
            M::mempty()
        } else if i <= self.start && self.end <= j {
            self.value.clone()
        } else {
            match &self.children {
                &Some(ref children) => children.head.mconcat(i, j).mappend(&children.tail.mconcat(i, j)),
                &None => M::mempty()
            }
        }
    }

    fn update(&mut self, i: usize, m: M) {
        if let &mut Some(ref mut children) = &mut self.children {
            if i < children.head.end {
                children.head.update(i, m);
            } else {
                children.tail.update(i, m);
            }
            self.value = children.head.value.mappend(&children.tail.value);
        } else {
            self.value = m;
        }
    }

    fn _build(vec: &Vec<M>, start: usize, end: usize) -> SegmentTree<M> {
        if end <= start {
            SegmentTree{
                value: M::mempty(),
                start: start,
                end: end,
                children: None
            }
        } else if start+1 == end {
            SegmentTree {
                value: vec[start].clone(),
                start: start,
                end: end,
                children: None
            }
        } else {
            let mid = (start + end) / 2;
            let head = SegmentTree::_build(vec, start, mid);
            let tail = SegmentTree::_build(vec, mid, end);
            SegmentTree {
                value: head.value.mappend(&tail.value),
                start: start,
                end: end,
                children: Some(Box::new(
                    SegmentTreeChildren{
                        head: head,
                        tail: tail
                    }
                ))
            }
        }
    }
}

#[allow(dead_code)]
fn get_line() -> String {
    let mut s = String::new();
    match stdin().read_line(&mut s){
        Ok(_) => {s.trim().to_string()}
        Err(_) => String::new()
    }
}

#[allow(dead_code)]
fn get_vec<T: std::str::FromStr>() -> Vec<T> {
    let line = get_line();
    line.split_whitespace().filter_map(|x| x.parse().ok()).collect()
}

#[allow(dead_code)]
fn get_one<T: FromStr + Copy>() -> T {
    let v = get_vec();
    v[0]
}

#[allow(dead_code)]
fn get_pair<T: FromStr + Copy>() -> (T, T) {
    let v = get_vec();
    (v[0], v[1])
}

#[allow(dead_code)]
fn get_triple<T: FromStr + Copy>() -> (T, T, T) {
    let v = get_vec();
    (v[0], v[1], v[2])
}

#[allow(dead_code)]
fn get_chars() -> Vec<char> {
    get_line().chars().collect()
}

#[allow(dead_code)]
fn vec_min(xs: &Vec<i64>) -> i64 {
    xs.iter().map(|&x|x).fold(std::i64::MAX, std::cmp::min)
}

#[allow(dead_code)]
fn vec_max(xs: &Vec<i64>) -> i64 {
    xs.iter().map(|&x|x).fold(std::i64::MIN, std::cmp::max)
}

#[allow(dead_code)]
fn vec_sum(xs: &Vec<i64>) -> i64 {
    xs.iter().fold(0, |acc, &x| acc+x)
}