Language: C++

#include<algorithm>
#include<cmath>
#include<cstdio>
#include<functional>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<string>
#include<utility>
#include<vector>
using Int = long long;
constexpr Int inf = 1e18;
int main(){
    int N,M; std::cin >> N >> M;
    std::string s; std::cin >> s;
    std::vector<std::vector<int>>edge(N + 1);
    for(int i = 0; i + 1 < edge.size(); ++i)
        edge[i + 1].emplace_back(i);
    for(int i = 0; i + 1 < edge.size(); ++i){
        int nxt = std::min(N,i + M);
        edge[i].emplace_back(nxt);
    }
    std::vector<Int>d(N + 1,inf);
    std::vector<Int>memo(N + 1,inf);
    std::priority_queue<std::tuple<Int, Int,Int>,std::vector<std::tuple<Int,Int,Int>>,std::greater<>>que;
    que.emplace(0,0);
    d[0] = 0;
    memo[0] = 0;
    while(not que.empty()){
        Int k,cv,org;
        std::tie(k,cv,org) = que.top(); que.pop();
        if(d[cv] < k ) continue;
        for(auto nxt : edge[cv]){
            bool is_virtual = nxt < cv;
            if(is_virtual){
                if(d[nxt] > d[cv] + 1){
                    d[nxt] = d[cv];
                    memo[nxt] =  org;
                    que.emplace(d[nxt],nxt,org);
                } else if(d[nxt] == d[cv] && memo[nxt] > org)
                    memo[nxt] = org;
            } else if(s[cv] != '1') {
                if(d[nxt] > d[cv] + 1){
                    d[nxt] = d[cv] + 1;
                    memo[nxt] = cv;
                    assert(nxt > cv);
                    que.emplace(d[nxt],nxt,cv);
                } else if(d[nxt] == d[cv] && memo[nxt] > cv){
                    memo[nxt] =  cv;
                }
            }
        }
    }
    std::vector<std::string> ans;
    int cpos = N;
    while(cpos != 0){
        ans.push_back(std::to_string(cpos - memo[cpos]));
        cpos = memo[cpos];
    }
    //cout << (d[N] == inf ? -1 : d[N]) << endl;
    if(d[N] != inf){
        std::reverse(ans.begin(),ans.end());
        for(auto s : ans)
            std::cout << s << std::endl;
    } else {
        puts("-1");
    }
}



Compiler output:
a.cc: In function 'int main()':
a.cc:48:21: error: 'assert' was not declared in this scope
   48 |                     assert(nxt > cv);
      |                     ^~~~~~
a.cc:12:1: note: 'assert' is defined in header '<cassert>'; this is probably fixable by adding '#include <cassert>'
   11 | #include<utility>
  +++ |+#include <cassert>
   12 | #include<vector>
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/c++allocator.h:33,
                 from /usr/include/c++/14/bits/allocator.h:46,
                 from /usr/include/c++/14/bits/alloc_traits.h:39,
                 from /usr/include/c++/14/ext/alloc_traits.h:34,
                 from /usr/include/c++/14/bits/hashtable_policy.h:39,
                 from /usr/include/c++/14/bits/hashtable.h:35,
                 from /usr/include/c++/14/bits/unordered_map.h:33,
                 from /usr/include/c++/14/unordered_map:41,
                 from /usr/include/c++/14/functional:63,
                 from a.cc:4:
/usr/include/c++/14/bits/new_allocator.h: In instantiation of 'void std::__new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = std::tuple<long long int, long long int, long long int>; _Args = {int, int}; _Tp = std::tuple<long long int, long long int, long long int>]':
/usr/include/c++/14/bits/alloc_traits.h:575:17:   required from 'static void std::allocator_traits<std::allocator<_Tp1> >::construct(allocator_type&, _Up*, _Args&& ...) [with _Up = std::tuple<long long int, long long int, long long int>; _Args = {int, int}; _Tp = std::tuple<long long int, long long int, long long int>; allocator_type = std::allocator<std::tuple<long long int, long long int, long long int> >]'
  575 |           __a.construct(__p, std::forward<_Args>(__args)...);
      |           ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/vector.tcc:117:30:   required from 'std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {int, int}; _Tp = std::tuple<long long int, long long int, long long int>; _Alloc = std::allocator<std::tuple<long long int, long long int, long long int> >; reference = std::tuple<long long int, long long int, long long int>&]'
  117 |             _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
      |             ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  118 |                                      std::forward<_Args>(__args)...);
      |                                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_queue.h:754:18:   required from 'void std::priority_queue<_Tp, _Sequence, _Compare>::emplace(_Args&& ...) [with _Args = {int, int}; _Tp = std::tuple<long long int, long long int, long long int>; _Sequence = std::vector<std::tuple<long long int, long long int, long long int> >; _Compare = std::greater<void>]'
  754 |           c.emplace_back(std::forward<_Args>(__args)...);
      |           ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:28:16:   required from here
   28 |     que.emplace(0,0);
      |     ~~~~~~~~~~~^~~~~
/usr/include/c++/14/bits/new_allocator.h:191:11: error: no matching function for call to 'std::tuple<long long int, long long int, long long int>::tuple(int, int)'
  191 |         { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/functional:53:
/usr/include/c++/14/tuple:1646:9: note: candidate: 'template<class _Alloc, class ... _UElements, bool _Valid, typename std::enable_if<std::tuple<long long int, long long int, long long int>::_TCC<_Valid>::__is_explicitly_constructible<_UElements ...>(), bool>::type <anonymous> > std::tuple< <template-parameter-1-1> >::tuple(std::allocator_arg_t, const _Alloc&, std::tuple<_Args2 ...>&&) [with _UElements = _Alloc; bool _Valid = {_UElements ...}; typename std::enable_if<_TCC<_Valid>::__is_explicitly_constructible<_UElements ...>(), bool>::type <anonymous> = _Valid; _Elements = {long long int, long long int, long long int}]'
 1646 |         tuple(allocator_arg_t __tag, const _Alloc& __a,
      |         ^~~~~
/usr/include/c++/14/tuple:1646:9: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/tuple:1634:9: note: candidate: 'template<class _Alloc, class ... _UElements, bool _Valid, typename std::enable_if<std::tuple<long long int, long long int, long long int>::_TCC<_Valid>::__is_implicitly_constructible<_UElements ...>(), bool>::type <anonymous> > std::tuple< <template-parameter-1-1> >::tuple(std::allocator_arg_t, const _Alloc&, std::tuple<_Args2 ...>&&) [with _UElements = _Alloc; bool _Valid = {_UElements ...}; typename std::enable_if<_TCC<_Valid>::__is_implicitly_constructible<_UElements ...>(), bool>::type <anonymous> = _Valid; _Elements = {long long int, long long int, long long int}]'
 1634 |         tuple(allocator_arg_t __tag, const _Alloc& __a,
      |         ^~~~~
/usr/include/c++/14/tuple:1634:9: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/tuple:1623:9: note: candidate: 'template<class _Alloc, class ... _UElements, bool _Valid, typename std::enable_if<std::tuple<long long int, long long int, long long int>::_TCC<_Valid>::__is_explicitly_constructible<const _UElements& ...>(), bool>::type <anonymous> > std::tuple< <template-parameter-1-1> >::tuple(std::allocator_arg_t, const _Alloc&, const std::tuple<_Args2 ...>&) [with _UElements = _Alloc; bool _Valid = {_UElements ...}; typename std::enable_if<_TCC<_Valid>::__is_explicitly_constructible<const _UElements& ...>(), bool>::type <anonymous> = _Valid; _Elements = {long long int, long long int, long long int}]'
 1623 |         tuple(allocator_arg_t __tag, const _Alloc& __a,
      |         ^~~~~
/usr/include/c++/14/tuple:1623:9: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/tuple:1611:9: note: candidate: 'template<class _Alloc, class ... _UElements, bool _Valid, typename std::enable_if<std::tuple<long long int, long long int, long long int>::_TCC<_Valid>::__is_implicitly_constructible<const _UElements& ...>(), bool>::type <anonymous> > std::tuple< <template-parameter-1-1> >::tuple(std::allocator_arg_t, const _Alloc&, const std::tuple<_Args2 ...>&) [with _UElements = _Alloc; bool _Valid = {_UElements ...}; typename std::enable_if<_TCC<_Valid>::__is_implicitly_constructible<const _UElements& ...>(), bool>::type <anonymous> = _Valid; _Elements = {long long int, long long int, long long int}]'
 1611 |         tuple(allocator_arg_t __tag, const _Alloc& __a,
      |         ^~~~~
/usr/include/c++/14/tuple:1611:9: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/tuple:1603:9: note: candidate: 'template<class _Alloc> std::tuple< <template-parameter-1-1> >::tuple(std::allocator_arg_t, const _Alloc&, std::tuple< <template-parameter-1-1> >&&) [with _Elements = {long long int, long long int, long long int}]'
 1603 |         tuple(allocator_arg_t __tag, const _Alloc& __a, tuple&& __in)
      |         ^~~~~
/usr/include/c++/14/tuple:1603:9: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/tuple:1598:9: note: candidate: 'template<class _Alloc> std::tuple< <template-parameter-1-1> >::tuple(std::allocator_arg_t, const _Alloc&, const std::tuple< <template-parameter-1-1> >&) [with _Elements = {long long int, long long int, long long int}]'
 1598 |         tuple(allocator_arg_t __tag, const _Alloc& __a, const tuple& __in)
      |         ^~~~~
/usr/include/c++/14/tuple:1598:9: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/tuple:1591:9: note: candidate: 'template<class _Alloc, class ... _UElements, bool _Valid, typename std::enable_if<std::tuple<long long int, long long int, long long int>::_TCC<_Valid>::__is_explicitly_constructible<_UElements ...>(), bool>::type <anonymous> > std::tuple< <template-parameter-1-1> >::tuple(std::allocator_arg_t, const _Alloc&, _UElements&& ...) [with _UElements = _Alloc; bool _Valid = {_UElements ...}; typename std::enable_if<_TCC<_Valid>::__is_explicitly_constructible<_UElements ...>(), bool>::type <anonymous> = _Valid; _Elements = {long long int, long long int, long long int}]'
 1591 |         tuple(allocator_arg_t __tag, const _Alloc& __a,
      |         ^~~~~
/usr/include/c++/14/tuple:1591:9: note:   template argument deduction/substitution failed:
/usr/include/c++/14/tuple:1587:59: error: no matching function for call to 'std::tuple<long long int, long long int, long long int>::__valid_args<>()'
 1587 |                  bool _Valid = __valid_args<_UElements...>(),
      |                                ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
/usr/include/c++/14/tuple:1390:31: note: candidate: 'template<class _Up> static constexpr bool std::tuple< <template-parameter-1-1> >::__valid_args() [with _Elements = {long long int, long long int, long long int}]'
 1390 |         static constexpr bool __valid_args()
      |                               ^~~~~~~~~~~~
/usr/include/c++/14/tuple:1390:31: note:   template argument deduction/substitution failed:
/usr/include/c++/14/tuple:1587:59: note:   couldn't deduce template parameter '_Up'
 1587 |                  bool _Valid = __valid_args<_UElements...>(),
      |                                ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
/usr/include/c++/14/tuple:1398:31: note: candidate: 'template<class, class, class ... _Tail> static constexpr bool std::tuple< <template-parameter-1-1> >::__valid_args() [with _Tail = <template-parameter-1-1>; _Elements = {long long int, long long int, long long int}]'
 1398 |         static constexpr bool __valid_args()
      |                               ^~~~~~~~~~~~
/usr/include/c++/14/tuple:1398:31: note:   template argument deduction/substitution failed:
/usr/include/c++/14/tuple:1587:59: note:   couldn't deduce template parameter '<template-parameter-1-1>'
 1587 |                  bool _Valid = __valid_args<_UElements...>(),
      |                                ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
/usr/include/c++/14/tuple:1581:9: note: candidate: 'template<class _Alloc, class ... _UElements, bool _Valid, typename std::enable_if<std::tuple<long long int, long long int, long long int>::_TCC