Problem:
w
を正整数，
p
を長さ2
2
w
+1
の文字列とする．
(w, p)
- セルオートマトンとは次のようなものである．
0 または 1 の状態をとることのできるセルが無限個一次元に並んでいる．
時刻 0 に，すぬけ君は好きな有限個のマスを選んで状態を 1 にすることができる．残りのセルは 0 である．
時刻
t (> 0)
のセル
x
の状態
f(t, x)
は，
f(t − 1, x − w), . . . , f(t − 1, x + w)
によって以下のように定まる:
\begin{eqnarray}
f(t, x) = p[ \sum^w_{i=-w}2^{w+i} f(t − 1, x + i)] \;\;\;\;\;\;\;\;\;\;(1)
\end{eqnarray}
すぬけ君は，時刻 0 にどのように初期状態を選んでも 1 の個数が初期状態から変わらないようなセルオートマトンが好きである．整数
w
と文字列
s
が与えられたとき，辞書順で
s
以上の文字列
p
であって
(w, p)
− セルオートマトンがすぬけ君に好かれるような最小の
p
を求めよ．
Constraints
1 ≤
w
≤ 3
|s|
= 2
2
w
+1
s
に現れる文字は '0', '1' のみである
Input
w
s
Output
条件を満たす最小の
p
を出力せよ．そのようなものが存在しないときは "
no
" と出力せよ．
Sample Input 1
1
00011000
Sample Output 1
00011101
Sample Input 2
1
11111111
Sample Output 2
no

Submitted solutions:

code_snippet: #ifdef NACHIA
#define _GLIBCXX_DEBUG
#else
#define NDEBUG
#endif
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long;
const ll INF = 1ll << 60;
#define REP(i,n) for(ll i=0; i<ll(n); i++)
template <class T> using V = vector<T>;
template <class A, class B> void chmax(A& l, const B& r){ if(l < r) l = r; }
template <class A, class B> void chmin(A& l, const B& r){ if(r < l) l = r; }

#include <utility>
#include <cassert>

namespace nachia{

template<class Elem>
class CsrArray{
public:
    struct ListRange{
        using iterator = typename std::vector<Elem>::iterator;
        iterator begi, endi;
        iterator begin() const { return begi; }
        iterator end() const { return endi; }
        int size() const { return (int)std::distance(begi, endi); }
        Elem& operator[](int i) const { return begi[i]; }
    };
    struct ConstListRange{
        using iterator = typename std::vector<Elem>::const_iterator;
        iterator begi, endi;
        iterator begin() const { return begi; }
        iterator end() const { return endi; }
        int size() const { return (int)std::distance(begi, endi); }
        const Elem& operator[](int i) const { return begi[i]; }
    };
private:
    int m_n;
    std::vector<Elem> m_list;
    std::vector<int> m_pos;
public:
    CsrArray() : m_n(0), m_list(), m_pos() {}
    static CsrArray Construct(int n, std::vector<std::pair<int, Elem>> items){
        CsrArray res;
        res.m_n = n;
        std::vector<int> buf(n+1, 0);
        for(auto& [u,v] : items){ ++buf[u]; }
        for(int i=1; i<=n; i++) buf[i] += buf[i-1];
        res.m_list.resize(buf[n]);
        for(int i=(int)items.size()-1; i>=0; i--){
            res.m_list[--buf[items[i].first]] = std::move(items[i].second);
        }
        res.m_pos = std::move(buf);
        return res;
    }
    static CsrArray FromRaw(std::vector<Elem> list, std::vector<int> pos){
        CsrArray res;
        res.m_n = pos.size() - 1;
        res.m_list = std::move(list);
        res.m_pos = std::move(pos);
        return res;
    }
    ListRange operator[](int u) { return ListRange{ m_list.begin() + m_pos[u], m_list.begin() + m_pos[u+1] }; }
    ConstListRange operator[](int u) const { return ConstListRange{ m_list.begin() + m_pos[u], m_list.begin() + m_pos[u+1] }; }
    int size() const { return m_n; }
    int fullSize() const { return (int)m_list.size(); }
};

} // namespace nachia

namespace nachia{


struct Graph {
public:
    struct Edge{
        int from, to;
        void reverse(){ std::swap(from, to); }
        int xorval() const { return from ^ to; }
    };
    Graph() : m_n(0), m_e(0), m_isUndir(false) {}
    explicit Graph(int n, bool undirected = false, int m = 0) : m_n(n), m_e(m), m_isUndir(undirected) {}
    explicit Graph(int n, const std::vector<std::pair<int, int>>& edges, int undirected = false) : m_n(n), m_isUndir(undirected){
        m_e.resize(edges.size());
        for(std::size_t i=0; i<edges.size(); i++) m_e[i] = { edges[i].first, edges[i].second };
    }
    template<class Cin>
    static Graph Input(Cin& cin, int n, bool undirected, int m, int offset = 0){
        Graph res(n, undirected, m);
        for(int i=0; i<m; i++){
            int u, v; cin >> u >> v;
            res[i].from = u - offset;
            res[i].to = v - offset;
        }
        return res;
    }
    int numVertices() const noexcept { return m_n; }
    int numEdges() const noexcept { return int(m_e.size()); }
    int addNode() noexcept { return m_n++; }
    int addEdge(int from, int to){ m_e.push_back({ from, to }); return numEdges() - 1; }
    Edge& operator[](int ei) noexcept { return m_e[ei]; }
    const Edge& operator[](int ei) const noexcept { return m_e[ei]; }
    Edge& at(int ei) { return m_e.at(ei); }
    const Edge& at(int ei) const { return m_e.at(ei); }
    auto begin(){ return m_e.begin(); }
    auto end(){ return m_e.end(); }
    auto begin() const { return m_e.begin(); }
    auto end() const { return m_e.end(); }
    bool isUndirected() const noexcept { return m_isUndir; }
    void reverseEdges() noexcept { for(auto& e : m_e) e.reverse(); }
    void contract(int newV, const std::vector<int>& mapping){
        assert(numVertices() == int(mapping.size()));
        for(int i=0; i<numVertices(); i++) assert(0 <= mapping[i] && mapping[i] < newV);
        for(auto& e : m_e){ e.from = mapping[e.from]; e.to = mapping[e.to]; }
        m_n = newV;
    }
    std::vector<Graph> induce(int num, const std::vector<int>& mapping) const {
        int n = numVertices();
        assert(n == int(mapping.size()));
        for(int i=0; i<n; i++) assert(-1 <= mapping[i] && mapping[i] < num);
        std::vector<int> indexV(n), newV(num);
        for(int i=0; i<n; i++) if(mapping[i] >= 0) indexV[i] = newV[mapping[i]]++;
        std::vector<Graph> res; res.reserve(num);
        for(int i=0; i<num; i++) res.emplace_back(newV[i], isUndirected());
        for(auto e : m_e) if(mapping[e.from] == mapping[e.to] && mapping[e.to] >= 0) res[mapping[e.to]].addEdge(indexV[e.from], indexV[e.to]);
        return res;
    }
    CsrArray<int> getEdgeIndexArray(bool undirected) const {
        std::vector<std::pair<int, int>> src;
        src.reserve(numEdges() * (undirected ? 2 : 1));
        for(int i=0; i<numEdges(); i++){
            auto e = operator[](i);
            src.emplace_back(e.from, i);
            if(undirected) src.emplace_back(e.to, i);
        }
        return CsrArray<int>::Construct(numVertices(), src);
    }
    CsrArray<int> getEdgeIndexArray() const { return getEdgeIndexArray(isUndirected()); }
    CsrArray<int> getAdjacencyArray(bool undirected) const {
        std::vector<std::pair<int, int>> src;
        src.reserve(numEdges() * (undirected ? 2 : 1));
        for(auto e : m_e){
            src.emplace_back(e.from, e.to);
            if(undirected) src.emplace_back(e.to, e.from);
        }
        return CsrArray<int>::Construct(numVertices(), src);
    }
    CsrArray<int> getAdjacencyArray() const { return getAdjacencyArray(isUndirected()); }
private:
    int m_n;
    std::vector<Edge> m_e;
    bool m_isUndir;
};

} // namespace nachia

namespace nachia{

struct StronglyConnectedComponents{
private:
    int m_n;
    CsrArray<int> induce;
    int componentNum;
public:

    StronglyConnectedComponents() : m_n(0), induce(), componentNum(0) {}
    StronglyConnectedComponents(Graph E) {
        int n = E.numVertices();
        m_n = n;
        std::vector<int> O(n);
        {
            auto adj = E.getAdjacencyArray();
            int Oi = n;
            std::vector<int> P(n, -1), EI(n, 0);
            for(int s=0; s<n; s++) if(P[s] == -1){
                P[s] = -2;
                int p = s;
                while(p >= 0){
                    if(EI[p] == adj[p].size()){ O[--Oi] = p; p = P[p]; continue; }
                    int q = adj[p][EI[p]++];
                    if(P[q] == -1){ P[q] = p; p = q; }
                }
            }
        }
        E.reverseEdges();
        auto adj = E.getAdjacencyArray();
        std::vector<int> sep = {0}, csr(n), vis(n,0);
        int p1 = 0, p2 = 0;
        for(int s : O) if(!vis[s]){
            csr[p2++] = s; vis[s] = 1;
            for(; p1<p2; p1++){
                int v = csr[p1];
                for(auto e : adj[v]) if(!vis[e]){ vis[e] = 1; csr[p2++] = e; }
            }
            sep.push_back(p2);
        }
        induce = CsrArray<int>::FromRaw(std::move(csr), std::move(sep));
        componentNum = induce.size();
    }

    int numComponents() const noexcept { return componentNum; }
    const CsrArray<int>& getCsr() const noexcept { return induce; }
    std::vector<int> getMapping() const {
        std::vector<int> res(m_n);
        for(int i=0; i<numComponents(); i++) for(int v : induce[i]) res[v] = i;
        return res;
    }
};

} // namespace nachia

bool detect(ll W, string S){
  nachia::Graph digraph((W+3)<<W, 0);
  auto idx = [&](ll a, ll wa, ll da) -> ll {
    return ((a * (W + 3)) + wa) * 2 + da;
  };
  auto e = [&](ll a, ll wa, ll da, ll b, ll wb, ll db) -> void {
    digraph.addEdge(idx(a, wa, 1-da), idx(b, wb, db));
    digraph.addEdge(idx(b, wb, 1-db), idx(a, wa, da));
  };
  REP(i,1<<(W-1)) REP(w,W+2) e(i,w,1, i,w+1,0);
  REP(i,1<<(W-1)) e(i,W+2,0,i,W+2,0);
  REP(i,1<<(W-1)) e(i,0,1,i,0,1);
  REP(i,1<<W){
    ll from = i / 2, to = i - (i & (1 << (W-1)));
    ll diff_min = -1, diff_max = 0;
    if(S[i] == '0') diff_min++;
    if(S[i] == '1') diff_max--;
    if(i % 2){ diff_min++; diff_max++; }
    for(ll j=0; j<=W+2; j++) e(from,j,0, to,min(max(j+diff_min,0ll),W+2),1);
    for(ll j=0; j<=W+2; j++) e(from,j,1, to,min(max(j+diff_max,0ll),W+2),0);
  }
  auto scc = nachia::StronglyConnectedComponents(digraph).getMapping();
  REP(i,digraph.numVertices()) if(scc[i] == scc[i^1]) return false;
  return true;
}

void testcase(){
  ll W; cin >> W; W = W * 2 + 1;
  string S; cin >> S;
  if(detect(W, S)){ cout << S << "\n"; return; }
  ll f = S.size();
  while(0 < f){
    if(S[f-1] == '1'){ S[--f] = '?'; continue; }
    S[f-1] = '1';
    if(detect(W, S)) break;
    S[--f] = '?';
  }
  if(f == 0){ cout << "no\n"; return; }
  while(f < S.size()){
    S[f] = '0';
    if(detect(W, S)){ f++; continue; }
    S[f++] = '1';
  }
  cout << S << "\n";
}

int main(){
  cin.tie(0)->sync_with_stdio(0);
  testcase();
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3712
final_rank: 10

code_snippet: #ifdef NACHIA
#define _GLIBCXX_DEBUG
#else
#define NDEBUG
#endif
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long;
const ll INF = 1ll << 60;
#define REP(i,n) for(ll i=0; i<ll(n); i++)
template <class T> using V = vector<T>;
template <class A, class B> void chmax(A& l, const B& r){ if(l < r) l = r; }
template <class A, class B> void chmin(A& l, const B& r){ if(r < l) l = r; }

#include <utility>
#include <cassert>

namespace nachia{

template<class Elem>
class CsrArray{
public:
    struct ListRange{
        using iterator = typename std::vector<Elem>::iterator;
        iterator begi, endi;
        iterator begin() const { return begi; }
        iterator end() const { return endi; }
        int size() const { return (int)std::distance(begi, endi); }
        Elem& operator[](int i) const { return begi[i]; }
    };
    struct ConstListRange{
        using iterator = typename std::vector<Elem>::const_iterator;
        iterator begi, endi;
        iterator begin() const { return begi; }
        iterator end() const { return endi; }
        int size() const { return (int)std::distance(begi, endi); }
        const Elem& operator[](int i) const { return begi[i]; }
    };
private:
    int m_n;
    std::vector<Elem> m_list;
    std::vector<int> m_pos;
public:
    CsrArray() : m_n(0), m_list(), m_pos() {}
    static CsrArray Construct(int n, std::vector<std::pair<int, Elem>> items){
        CsrArray res;
        res.m_n = n;
        std::vector<int> buf(n+1, 0);
        for(auto& [u,v] : items){ ++buf[u]; }
        for(int i=1; i<=n; i++) buf[i] += buf[i-1];
        res.m_list.resize(buf[n]);
        for(int i=(int)items.size()-1; i>=0; i--){
            res.m_list[--buf[items[i].first]] = std::move(items[i].second);
        }
        res.m_pos = std::move(buf);
        return res;
    }
    static CsrArray FromRaw(std::vector<Elem> list, std::vector<int> pos){
        CsrArray res;
        res.m_n = pos.size() - 1;
        res.m_list = std::move(list);
        res.m_pos = std::move(pos);
        return res;
    }
    ListRange operator[](int u) { return ListRange{ m_list.begin() + m_pos[u], m_list.begin() + m_pos[u+1] }; }
    ConstListRange operator[](int u) const { return ConstListRange{ m_list.begin() + m_pos[u], m_list.begin() + m_pos[u+1] }; }
    int size() const { return m_n; }
    int fullSize() const { return (int)m_list.size(); }
};

} // namespace nachia

namespace nachia{


struct Graph {
public:
    struct Edge{
        int from, to;
        void reverse(){ std::swap(from, to); }
        int xorval() const { return from ^ to; }
    };
    Graph() : m_n(0), m_e(0), m_isUndir(false) {}
    explicit Graph(int n, bool undirected = false, int m = 0) : m_n(n), m_e(m), m_isUndir(undirected) {}
    explicit Graph(int n, const std::vector<std::pair<int, int>>& edges, int undirected = false) : m_n(n), m_isUndir(undirected){
        m_e.resize(edges.size());
        for(std::size_t i=0; i<edges.size(); i++) m_e[i] = { edges[i].first, edges[i].second };
    }
    template<class Cin>
    static Graph Input(Cin& cin, int n, bool undirected, int m, int offset = 0){
        Graph res(n, undirected, m);
        for(int i=0; i<m; i++){
            int u, v; cin >> u >> v;
            res[i].from = u - offset;
            res[i].to = v - offset;
        }
        return res;
    }
    int numVertices() const noexcept { return m_n; }
    int numEdges() const noexcept { return int(m_e.size()); }
    int addNode() noexcept { return m_n++; }
    int addEdge(int from, int to){ m_e.push_back({ from, to }); return numEdges() - 1; }
    Edge& operator[](int ei) noexcept { return m_e[ei]; }
    const Edge& operator[](int ei) const noexcept { return m_e[ei]; }
    Edge& at(int ei) { return m_e.at(ei); }
    const Edge& at(int ei) const { return m_e.at(ei); }
    auto begin(){ return m_e.begin(); }
    auto end(){ return m_e.end(); }
    auto begin() const { return m_e.begin(); }
    auto end() const { return m_e.end(); }
    bool isUndirected() const noexcept { return m_isUndir; }
    void reverseEdges() noexcept { for(auto& e : m_e) e.reverse(); }
    void contract(int newV, const std::vector<int>& mapping){
        assert(numVertices() == int(mapping.size()));
        for(int i=0; i<numVertices(); i++) assert(0 <= mapping[i] && mapping[i] < newV);
        for(auto& e : m_e){ e.from = mapping[e.from]; e.to = mapping[e.to]; }
        m_n = newV;
    }
    std::vector<Graph> induce(int num, const std::vector<int>& mapping) const {
        int n = numVertices();
        assert(n == int(mapping.size()));
        for(int i=0; i<n; i++) assert(-1 <= mapping[i] && mapping[i] < num);
        std::vector<int> indexV(n), newV(num);
        for(int i=0; i<n; i++) if(mapping[i] >= 0) indexV[i] = newV[mapping[i]]++;
        std::vector<Graph> res; res.reserve(num);
        for(int i=0; i<num; i++) res.emplace_back(newV[i], isUndirected());
        for(auto e : m_e) if(mapping[e.from] == mapping[e.to] && mapping[e.to] >= 0) res[mapping[e.to]].addEdge(indexV[e.from], indexV[e.to]);
        return res;
    }
    CsrArray<int> getEdgeIndexArray(bool undirected) const {
        std::vector<std::pair<int, int>> src;
        src.reserve(numEdges() * (undirected ? 2 : 1));
        for(int i=0; i<numEdges(); i++){
            auto e = operator[](i);
            src.emplace_back(e.from, i);
            if(undirected) src.emplace_back(e.to, i);
        }
        return CsrArray<int>::Construct(numVertices(), src);
    }
    CsrArray<int> getEdgeIndexArray() const { return getEdgeIndexArray(isUndirected()); }
    CsrArray<int> getAdjacencyArray(bool undirected) const {
        std::vector<std::pair<int, int>> src;
        src.reserve(numEdges() * (undirected ? 2 : 1));
        for(auto e : m_e){
            src.emplace_back(e.from, e.to);
            if(undirected) src.emplace_back(e.to, e.from);
        }
        return CsrArray<int>::Construct(numVertices(), src);
    }
    CsrArray<int> getAdjacencyArray() const { return getAdjacencyArray(isUndirected()); }
private:
    int m_n;
    std::vector<Edge> m_e;
    bool m_isUndir;
};

} // namespace nachia

namespace nachia{

struct StronglyConnectedComponents{
private:
    int m_n;
    CsrArray<int> induce;
    int componentNum;
public:

    StronglyConnectedComponents() : m_n(0), induce(), componentNum(0) {}
    StronglyConnectedComponents(Graph E) {
        int n = E.numVertices();
        m_n = n;
        std::vector<int> O(n);
        {
            auto adj = E.getAdjacencyArray();
            int Oi = n;
            std::vector<int> P(n, -1), EI(n, 0);
            for(int s=0; s<n; s++) if(P[s] == -1){
                P[s] = -2;
                int p = s;
                while(p >= 0){
                    if(EI[p] == adj[p].size()){ O[--Oi] = p; p = P[p]; continue; }
                    int q = adj[p][EI[p]++];
                    if(P[q] == -1){ P[q] = p; p = q; }
                }
            }
        }
        E.reverseEdges();
        auto adj = E.getAdjacencyArray();
        std::vector<int> sep = {0}, csr(n), vis(n,0);
        int p1 = 0, p2 = 0;
        for(int s : O) if(!vis[s]){
            csr[p2++] = s; vis[s] = 1;
            for(; p1<p2; p1++){
                int v = csr[p1];
                for(auto e : adj[v]) if(!vis[e]){ vis[e] = 1; csr[p2++] = e; }
            }
            sep.push_back(p2);
        }
        induce = CsrArray<int>::FromRaw(std::move(csr), std::move(sep));
        componentNum = induce.size();
    }

    int numComponents() const noexcept { return componentNum; }
    const CsrArray<int>& getCsr() const noexcept { return induce; }
    std::vector<int> getMapping() const {
        std::vector<int> res(m_n);
        for(int i=0; i<numComponents(); i++) for(int v : induce[i]) res[v] = i;
        return res;
    }
};

} // namespace nachia

bool detect(ll W, string S){
  nachia::Graph digraph((W+3)<<W, 0);
  auto idx = [&](ll a, ll wa, ll da) -> ll {
    return ((a * (W + 3)) + wa) * 2 + da;
  };
  auto e = [&](ll a, ll wa, ll da, ll b, ll wb, ll db) -> void {
    digraph.addEdge(idx(a, wa, 1-da), idx(b, wb, db));
    digraph.addEdge(idx(b, wb, 1-db), idx(a, wa, da));
  };
  REP(i,1<<(W-1)) REP(w,W+2) e(i,w,1, i,w+1,0);
  REP(i,1<<(W-1)) e(i,W+2,0,i,W+2,0);
  REP(i,1<<(W-1)) e(i,0,1,i,0,1);
  REP(i,1<<W){
    ll from = i / 2, to = i - (i & (1 << (W-1)));
    ll diff_min = -1, diff_max = 0;
    if(S[i] == '0') diff_min++;
    if(S[i] == '1') diff_max--;
    if(i % 2){ diff_min++; diff_max++; }
    for(ll j=0; j<=W+2; j++) e(from,j,0, to,min(max(j+diff_min,0ll),W+2),1);
    for(ll j=0; j<=W+2; j++) e(from,j,1, to,min(max(j+diff_max,0ll),W+2),0);
  }
  auto scc = nachia::StronglyConnectedComponents(digraph).getMapping();
  REP(i,digraph.numVertices()) if(scc[i] == scc[i^1]) return false;
  return true;
}

void testcase(){
  ll W; cin >> W; W = W * 2 + 1;
  string S; cin >> S;
  ll f = S.size();
  while(0 < f){
    if(S[f-1] == '1'){ S[--f] = '?'; continue; }
    S[f-1] = '1';
    if(detect(W, S)) break;
    S[--f] = '?';
  }
  if(f == 0){ cout << "no\n"; return; }
  while(f < S.size()){
    S[f] = '0';
    if(detect(W, S)){ f++; continue; }
    S[f++] = '1';
  }
  cout << S << "\n";
}

int main(){
  cin.tie(0)->sync_with_stdio(0);
  testcase();
  return 0;
}
accuracy: 0.9166666666666666
time_ms: 20
memory_kb: 3840
final_rank: 13

code_snippet: #include <bits/stdc++.h>
#define INF 1000000007
using namespace std;

int w,l[128],r[128],dist[128],vis[128],cnt[128];
string s;
vector<pair<int,int> > v[128];

inline bool check(string &str)
{
	for(int i=0;i<(1<<(2*w+1));i++){
		if(str[i]=='0') l[i]=0,r[i]=0;
		else if(str[i]=='1') l[i]=1,r[i]=1;
		else l[i]=0,r[i]=1;
	}
	for(int i=0;i<(1<<(2*w+1));i++) v[i].clear();
	for(int i=0;i<(1<<(2*w+1));i++){
		int pre=((i&((1<<(2*w))-1))<<1);
		int v0=pre,v1=(pre^1);
		v[v0].push_back(make_pair(i,-(0-r[i])));
		v[i].push_back(make_pair(v0,0-l[i]));
		v[v1].push_back(make_pair(i,-(1-r[i])));
		v[i].push_back(make_pair(v1,1-l[i]));
	}
	for(int i=0;i<(1<<(2*w+1));i++) dist[i]=INF,vis[i]=0,cnt[i]=0;
	queue<int> q;q.push(0);dist[0]=0;vis[0]=1;cnt[0]=1;
	bool flag=true;
	while(!q.empty()){
		int t=q.front();q.pop();vis[t]=0;
		for(int i=0;i<(int)v[t].size();i++){
			int u=v[t][i].first,d=v[t][i].second;
			if(dist[u]>dist[t]+d){
				dist[u]=dist[t]+d;
				if(!vis[u]){
					vis[u]=1;cnt[u]++;
					if(cnt[u]>(1<<(2*w+1))){
						flag=false;break;
					}
					q.push(u);
				}
			}
		}
		if(!flag) break;
	}
	if(!flag) return false;
	return true;
}

signed main()
{
	ios::sync_with_stdio(false);cin.tie(0);
	cin>>w>>s;
	if(check(s)){
		cout<<s<<'\n';
		return 0;
	}
	for(int i=(1<<(2*w+1))-1;i>=0;i--){
		if(s[i]=='1') s[i]='?';
		else{
			s[i]='1';
			if(check(s)){
				for(int j=i+1;j<(1<<(2*w+1));j++){
					s[j]='0';
					if(!check(s)) s[j]='1';
				}
				cout<<s<<'\n';
				return 0;
			}
			s[i]='?';
		}
	}
	cout<<"no"<<'\n';
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3476
final_rank: 7

code_snippet: #include <bits/stdc++.h>
#define INF 1000000007
using namespace std;

int w,l[128],r[128],dist[128],vis[128],cnt[128];
string s;
vector<pair<int,int> > v[128];

inline bool check(string &str)
{
	for(int i=0;i<(1<<(2*w+1));i++){
		if(str[i]=='0') l[i]=0,r[i]=0;
		else if(str[i]=='1') l[i]=1,r[i]=1;
		else l[i]=0,r[i]=1;
	}
	for(int i=0;i<(1<<(2*w+1));i++) v[i].clear();
	for(int i=0;i<(1<<(2*w+1));i++){
		int pre=((i&((1<<(2*w))-1))<<1);
		int v0=pre,v1=(pre^1);
		v[v0].push_back(make_pair(i,-(0-r[i])));
		v[i].push_back(make_pair(v0,0-l[i]));
		v[v1].push_back(make_pair(i,-(1-r[i])));
		v[i].push_back(make_pair(v1,1-l[i]));
	}
	for(int i=0;i<(1<<(2*w+1));i++) dist[i]=INF,vis[i]=0,cnt[i]=0;
	queue<int> q;q.push(0);dist[0]=0;vis[0]=1;cnt[0]=1;
	bool flag=true;
	while(!q.empty()){
		int t=q.front();q.pop();vis[t]=0;
		for(int i=0;i<(int)v[t].size();i++){
			int u=v[t][i].first,d=v[t][i].second;
			if(dist[u]>dist[t]+d){
				dist[u]=dist[t]+d;
				if(!vis[u]){
					vis[u]=1;cnt[u]++;
					if(cnt[u]>(1<<(2*w+1))){
						flag=false;break;
					}
					q.push(u);
				}
			}
		}
		if(!flag) break;
	}
	if(!flag) return false;
	return true;
}

signed main()
{
	ios::sync_with_stdio(false);cin.tie(0);
	cin>>w>>s;
	if(check(s)){
		cout<<s<<'\n';
		return 0;
	}
	for(int i=(1<<(2*w+1))-1;i>=0;i--){
		if(s[i]=='1') s[i]='?';
		else{
			s[i]='1';
			if(check(s)){
				for(int j=i+1;j<(1<<(2*w+1));j++){
					s[j]='0';
					if(!check(s)) s[j]='1';
				}
				cout<<s<<'\n';
				return 0;
			}
			s[i]='?';
		}
	}
	cout<<"no"<<'\n';
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3472
final_rank: 6

code_snippet: #include <bits/stdc++.h>
#define INF 1000000000
#define LINF 1000000000000000000
#define MOD 1000000007
#define mod 998244353
#define F first
#define S second
#define ll long long
#define N 300
using namespace std;
ll n,a[N],dist[N],tms[N];
bool inq[N];
string s;
vector<pair<ll,ll> > vt[N];
bool spfa()
{
	ll i;
	queue<ll> q;
	for(i=0;i<n*2;i++)
	{
		tms[i]=inq[i]=0;
		dist[i]=LINF;
	}
	dist[0]=0;
	q.push(0);
	inq[0]=true;
	while(!q.empty())
	{
		ll x=q.front();
		q.pop();
		if(tms[x]>n*2+5)
		{
			return false;
		}
		inq[x]=false;
		tms[x]++;
		for(i=0;i<vt[x].size();i++)
		{
			if(dist[vt[x][i].F]>dist[x]+vt[x][i].S)
			{
				dist[vt[x][i].F]=dist[x]+vt[x][i].S;
				if(!inq[vt[x][i].F])
				{
					inq[vt[x][i].F]=true;
					q.push(vt[x][i].F);
				}
			}
		}
	}
	return true;
}
bool check()
{
	ll i;
	for(i=0;i<n*2;i++)
	{
		vt[i].clear();
	}
	for(i=0;i<n;i++)
	{
		if(a[i]==-1)
		{
//			cout<<i<<" -> "<<i+n<<": "<<1<<endl;
			vt[i].push_back(make_pair(i+n,1));
//			cout<<i+n<<" -> "<<i<<": "<<0<<endl;
			vt[i+n].push_back(make_pair(i,0));
		}
		else
		{
//			cout<<i<<" -> "<<i+n<<": "<<a[i]<<endl;
			vt[i].push_back(make_pair(i+n,a[i]));
//			cout<<i+n<<" -> "<<i<<": "<<-a[i]<<endl;
			vt[i+n].push_back(make_pair(i,-a[i]));
		}
//		cout<<i+n<<" -> "<<(i>>1)<<": "<<0<<endl;
		vt[i+n].push_back(make_pair(i>>1,0));
//		cout<<(i>>1)<<" -> "<<i+n<<": "<<0<<endl;
		vt[i>>1].push_back(make_pair(i+n,0));
//		cout<<i+n<<" -> "<<((i|n)>>1)<<": "<<-1<<endl;
		vt[i+n].push_back(make_pair((i|n)>>1,-1));
//		cout<<((i|n)>>1)<<" -> "<<i+n<<": "<<1<<endl;
		vt[(i|n)>>1].push_back(make_pair(i+n,1));
	}
	return spfa();
}
int main(){
	ll i,j;
	cin>>n>>s;
	n=1<<(2*n+1);
	for(i=0;i<n;i++)
	{
		a[i]=s[i]-'0';
	}
	if(check())
	{
		cout<<s<<endl;
		return 0;
	}
	for(i=n-1;i>=0;i--)
	{
		if(s[i]=='0')
		{
			memset(a,-1,sizeof(a));
			for(j=0;j<i;j++)
			{
				a[j]=s[j]-'0';
			}
			a[i]=1;
			if(check())
			{
				for(j=i+1;j<n;j++)
				{
					a[j]=0;
					if(!check())
					{
						a[j]=1;
					}
				}
				for(j=0;j<n;j++)
				{
					cout<<a[j];
				}
				puts("");
				return 0;
			}
		}
	}
	puts("no");
	return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 3472
final_rank: 11

code_snippet: #include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define op(x) ((x&1)?x+1:x-1)
#define odd(x) (x&1)
#define even(x) (!odd(x))
#define lc(x) (x<<1)
#define rc(x) (lc(x)|1)
#define lowbit(x) (x&-x)
#define mp(x,y) make_pair(x,y)
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
using namespace std;
const int MAXN=130,INF=1e9;
int n,w,a[MAXN],b[MAXN];
vector<array<int,2> >e[MAXN];
//
void LT(int x,int y,int d){ //dis[x]-dis[y]<=d
    e[y].push_back({x,d});
}
void GT(int x,int y,int d){ //dis[x]-dis[y]>=d
    e[x].push_back({y,-d});
}
void EQ(int x,int y,int d){ //dis[x]-dis[y]=d
    LT(x,y,d);
    GT(x,y,d);
}
void EQ(int x,int d){ //dis[x]=d
    EQ(x,n,d);
}
//
int dis[MAXN];
int bellman_ford(){
    rep(i,0,n)dis[i]=INF;
    dis[0]=0;
    rep(i,0,n+5){
        rep(u,0,n)for(auto p:e[u]){
            int v=p[0],w=p[1];
            if(dis[v]>dis[u]+w){
                dis[v]=dis[u]+w;
            }
        }
    }
    rep(u,0,n)for(auto p:e[u]){
        int v=p[0],w=p[1];
        if(dis[v]>dis[u]+w)return 0;
    }
    return 1;
}
int check(){
    rep(i,0,n)e[i].clear();
    EQ(0,0);
    rep(u,0,n-1){
        int v0=(u>>1),v1=(u>>1)|(1<<(2*w));
        if(b[u]!=-1){
            EQ(v0,u,b[u]);
            EQ(v1,u,b[u]-1);
        }else{
            EQ(v0,v1,1);
            
            LT(v0,u,1);GT(v0,u,0);
            LT(v1,u,0);GT(v1,u,-1);
        }
    }
    return bellman_ford();
}
int main(){
    cin>>w;n=1<<(2*w+1);

    string tmp;cin>>tmp;
    rep(i,0,n-1)a[i]=tmp[i]-'0';
    per(i,n,0)if(a[i]==0){
        rep(j,0,i-1)b[j]=a[j];
        b[i]=1;
        rep(j,i+1,n-1)b[j]=-1;
        if(check()){
            rep(j,i+1,n-1){
                b[j]=0;
                if(!check())b[j]=1;
            }
            rep(j,0,n-1)cout<<b[j];cout<<"\n";
            exit(0);    
        }
    }       
    cout<<"no\n";

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3444
final_rank: 4

code_snippet: #include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define op(x) ((x&1)?x+1:x-1)
#define odd(x) (x&1)
#define even(x) (!odd(x))
#define lc(x) (x<<1)
#define rc(x) (lc(x)|1)
#define lowbit(x) (x&-x)
#define mp(x,y) make_pair(x,y)
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
using namespace std;
const int MAXN=130,INF=1e9;
int n,w,a[MAXN],b[MAXN];
vector<array<int,2> >e[MAXN];
//
void LT(int x,int y,int d){ //dis[x]-dis[y]<=d
    e[y].push_back({x,d});
}
void GT(int x,int y,int d){ //dis[x]-dis[y]>=d
    e[x].push_back({y,-d});
}
void EQ(int x,int y,int d){ //dis[x]-dis[y]=d
    LT(x,y,d);
    GT(x,y,d);
}
void EQ(int x,int d){ //dis[x]=d
    EQ(x,n,d);
}
//
int dis[MAXN];
int bellman_ford(){
    rep(i,0,n)dis[i]=INF;
    dis[0]=0;
    rep(i,0,n+5){
        rep(u,0,n)for(auto p:e[u]){
            int v=p[0],w=p[1];
            if(dis[v]>dis[u]+w){
                dis[v]=dis[u]+w;
            }
        }
    }
    rep(u,0,n)for(auto p:e[u]){
        int v=p[0],w=p[1];
        if(dis[v]>dis[u]+w)return 0;
    }
    return 1;
}
int check(){
    rep(i,0,n)e[i].clear();
    EQ(0,0);
    rep(u,0,n-1){
        int v0=(u>>1),v1=(u>>1)|(1<<(2*w));
        if(b[u]!=-1){
            EQ(v0,u,b[u]);
            EQ(v1,u,b[u]-1);
        }else{
            EQ(v0,v1,1);
            
            LT(v0,u,1);GT(v0,u,0);
            LT(v1,u,0);GT(v1,u,-1);
        }
    }
    return bellman_ford();
}
int main(){
    cin>>w;n=1<<(2*w+1);

    string tmp;cin>>tmp;
    rep(i,0,n-1)a[i]=tmp[i]-'0';
    per(i,n-1,0)if(a[i]==0){
        rep(j,0,i-1)b[j]=a[j];
        b[i]=1;
        rep(j,i+1,n-1)b[j]=-1;
        if(check()){
            rep(j,i+1,n-1){
                b[j]=0;
                if(!check())b[j]=1;
            }
            rep(j,0,n-1)cout<<b[j];cout<<"\n";
            exit(0);    
        }
    }       
    cout<<"no\n";

    return 0;
}
accuracy: 0.9166666666666666
time_ms: 10
memory_kb: 3436
final_rank: 12

code_snippet: #include<bits/stdc++.h>
#define ll long long
#define gmax(x,y) x=max(x,y)
#define gmin(x,y) x=min(x,y)
#define F first
#define S second
#define P pair
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define rep(i,a,b) for(int i=a;i<b;i++)
#define V vector
#define RE return
#define ALL(a) a.begin(),a.end()
#define MP make_pair
#define PB emplace_back
#define PF push_front
#define FILL(a,b) memset(a,b,sizeof(a))
#define lwb lower_bound
#define upb upper_bound
#define lc (x<<1)
#define rc ((x<<1)|1)
#define sz(x) ((int)x.size())
using namespace std;
const int inf=1e9;
int val[1<<7],w,hav[1<<7];
int dis[1<<7],cnt[1<<7],in[1<<7];
V<P<int,int> > v[1<<7];
bool check(){
	rep(i,0,1<<w)v[i].clear(),dis[i]=inf,cnt[i]=0;
	dis[0]=0;
	int tot=(1<<w)-1;
	rep(i,0,1<<w){
		int t=(i&(1<<(w-1)))>0;t=-t;
		int to1=i*2,to2=i*2+1;
		if(hav[to1]==0){
			v[i].PB(MP(to1&tot,t));
			v[to1&tot].PB(MP(i,-t));
		}else if(hav[to1]==1){
			t++;
			v[i].PB(MP(to1&tot,t));
			v[to1&tot].PB(MP(i,-t));
		}else{
			v[to1&tot].PB(MP(i,-t));
			t++;
			v[i].PB(MP(to1&tot,t));
		}
		t=(i&(1<<(w-1)))>0;t=-t;
		to1=to2;
		if(hav[to1]==0){
			v[i].PB(MP(to1&tot,t));
			v[to1&tot].PB(MP(i,-t));
		}else if(hav[to1]==1){
			t++;
			v[i].PB(MP(to1&tot,t));
			v[to1&tot].PB(MP(i,-t));
		}else{
			v[to1&tot].PB(MP(i,-t));
			t++;
			v[i].PB(MP(to1&tot,t));
		}
	}
	FILL(in,0);
	queue<int> q;
	q.emplace(0);in[0]=1;
	while(!q.empty()){
		int x=q.front();q.pop();in[x]=0;
		cnt[x]++;
		if(cnt[x]>200)RE 0;
		for(auto u:v[x]){
			if(dis[u.F]>dis[x]+u.S){
				dis[u.F]=dis[x]+u.S;
				if(!in[u.F]){
					in[u.F]=1;q.emplace(u.F);
				}
			}
		}
	}
	RE 1;
}
signed main(){
//	freopen("cell.in","r",stdin);
//	freopen("cell.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin>>w;w*=2;
	rep(i,0,1<<(w+1)){
		char c;
		cin>>c;
		val[i]=c-'0';hav[i]=val[i];
	}
	if(check()){
		rep(i,0,1<<(w+1))cout<<hav[i];
		cout<<'\n';
		RE 0;
	}
	for(int i=(1<<(w+1))-1;i>=0;i--){
		if(val[i]==1){
			hav[i]=1;continue;
		}
		hav[i]=1;
		rep(j,i+1,(1<<(w+1)))hav[j]=2;
		if(check()){
			rep(j,i+1,(1<<(w+1))){
				hav[j]=0;
				if(!check())hav[j]=1;
			}
			rep(j,0,1<<(w+1))cout<<hav[j];
			cout<<'\n';
			RE 0;
		}
	}
	cout<<"no\n";
	RE 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3448
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define mod 1000000007
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
int w;
bool ok(string s){
    int dist[128]={};
    int num = (1<<w)*(2<<w);
    for(int i=0;i<num;i++) dist[i] = INF;
    dist[0] = 0;
    for(int r=0;r<=num+5;r++){
        bool upd = false;
        for(int i=0;i<num/2;i++){
            int mn = min(dist[2*i],dist[2*i+1]);
            if(dist[2*i] != dist[2*i+1]) upd = 1;
            dist[2*i] = mn; dist[2*i+1] = mn;
            int go = 0; if(s[i] == '1') go = -1;
            int from = 1; if(s[i] == '0') from = 0;
            if(dist[i]+go < dist[2*i]) dist[2*i]=dist[i]+go,upd=1;
            if(dist[i]+go < dist[2*i+1]) dist[2*i+1]=dist[i]+go,upd=1;
            if(dist[i] > dist[2*i]+from) dist[i]=dist[2*i]+from,upd=1;
            if(dist[i] > dist[2*i+1]+from) dist[i]=dist[2*i+1]+from,upd=1;
            go = 1; if(s[i+num/2] == '1') go = 0;
            from = 0; if(s[i+num/2] == '0') from = -1;
            if(dist[i+num/2]+go < dist[2*i]) dist[2*i]=dist[i+num/2]+go,upd=1;
            if(dist[i+num/2]+go < dist[2*i+1]) dist[2*i+1]=dist[i+num/2]+go,upd=1;
            if(dist[i+num/2] > dist[2*i]+from) dist[i+num/2]=dist[2*i]+from,upd=1;
            if(dist[i+num/2] > dist[2*i+1]+from) dist[i+num/2]=dist[2*i+1]+from,upd=1;
        }
        if(r == num+5){
            return !upd;
        }
    }
}
string s;
int main(){
    cin>>w>>s;
    if(ok(s)){
        cout << s << endl;
        return 0;
    }
    for(int i=s.size()-1;i>=0;i--){
        if(s[i] == '1'){
            s[i] = '?'; continue;
        }
        else{
            s[i] = '1';
            if(ok(s)){
                for(int j=i+1;j<s.size();j++){
                    s[j] = '0';
                    if(ok(s)) continue;
                    else s[j] = '1';
                }
                assert(ok(s));
                cout << s << endl;
                return 0;
            }
            else{
                s[i] = '?';
            }
        }
    }
    cout << "no" << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3240
final_rank: 2

code_snippet: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
using namespace std;
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){return o<<"("<<p.fs<<","<<p.sc<<")";}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){o<<"sz = "<<vc.size()<<endl<<"[";for(const T& v:vc) o<<v<<",";o<<"]";return o;}

struct Cow{
	using D = int;
	const D inf = 1e9;
	struct Edge{
		int to;
		D cost;
		Edge(int to,D cost):to(to),cost(cost){}
	};
	int N,M;
	vector<vector<Edge>> G;
	vector<D> d;
	Cow(int N):N(N){
		G.assign(N,vector<Edge>());
		d.assign(N,inf);
	}
	void add_edge(int t,int s,D c){	// correspond to t-s <= c
//		printf("%d->%d   :%d\n",t,s,c);
		G[s].pb(Edge(t,c));
	}
	void add_edge(int t,int s,D clow,D chigh){	// clow <= t-s <= chigh
		add_edge(t,s,chigh);
		add_edge(s,t,-clow);
	}
	/*
		return the inequalities have a valid assignment
		if satisfiable, minimize d[T]-d[S]

		negative cycle    <=> unsatisfiable
		d[T] - d[S] = inf <=> feasible unboundedly

		d[S]=0

		T??????????????????????(?°???\???)
	*/
	bool IsValidAssign(int S = 0){
		vector<int> prev(N);
		d[S] = 0;
		rep(ph,N){
			bool update = 0;
			int v = -1;
			rep(s,N) for(auto e:G[s]){
				int t = e.to;
				D cost = e.cost;
				if(d[s]+cost < d[t]){
					update=1;
					v = t;
					d[t] = d[s]+cost;
					prev[t] = s;
				}
			}
			if(ph == N-1 && update){
				// vector<int> path;
				// path.pb(v);
				// int u = v;
				// do{
				// 	show(u);
				// 	u = prev[u];
				// 	path.pb(u);
				// }while(u!=v);
				// reverse(all(path));
				// show(path);
				// show(d[7]);
				// show(d[15]);
				return 0;
			}
			if(!update) break;
		}
		return 1;
	}
};

int W;
bool can(string s){
	int N = s.size();
	int V = N*2;
	Cow cow(V);
	rep(i,N){
		int clow = 0, chigh = 1;
		if(s[i]=='0') chigh = 0;
		if(s[i]=='1') clow = 1;
		cow.add_edge(i+N,i,clow,chigh);
		rep(t,2){
			int ni = (i*2+t)%N;
			cow.add_edge(ni,i+N,-t,-t);
		}
	}
	return cow.IsValidAssign();
}

string solve(){
	string s;
	cin>>W>>s;
	W = W*2+1;
	int N = s.size();
	if(can(s)) return s;
	int I = -1;
	for(int i=N-1;i>=0;i--){
		if(s[i]=='0'){
			s[i]='1';
			for(int j=i+1;j<N;j++) s[j]='*';
			if(can(s)){
				I = i;
				break;
			}
		}
	}
	if(I==-1) return "no";
	for(int i=I+1;i<N;i++){
		s[i]='0';
		if(can(s)) continue;
		else s[i]='1';
	}
	return s;
}
int main(){
	cout<<solve()<<endl;
}
accuracy: 1
time_ms: 40
memory_kb: 3196
final_rank: 8

code_snippet: #define _USE_MATH_DEFINES
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <cfloat>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge { int u, v; ll w; };

ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

ll d[256];

bool bellman_ford(int n, vector<edge>& E, int s, ll d[]) {
	fill(d, d + n, LLONG_MAX); d[s] = 0;
	for (int i = 0; i < n; i++) {
		bool updated = false;
		for (int j = 0; j < E.size(); j++) {
			edge e = E[j];
			if (d[e.u] < LLONG_MAX && d[e.v] > d[e.u] + e.w) {
				d[e.v] = d[e.u] + e.w;
				updated = true;
			}
		}
		if (!updated) return true;
	}
	return false;
}

string check(int N, string s) {
	vector<edge> E;
	for (int u = 0; u < N; u++) {
		E.push_back(edge{u, N + u, 0});
		E.push_back(edge{N + u, u, 1});
		int v;
		v = u * 2 % N;
		E.push_back(edge{N + u, v, 0});
		E.push_back(edge{v, N + u, 0});
		v = (u * 2 + 1) % N;
		E.push_back(edge{N + u, v, 1});
		E.push_back(edge{v, N + u, -1});
	}
	for (int u = 0; u < N; u++) {
		if (s[u] == '0')
			E.push_back(edge{N + u, u, 0});
		if (s[u] == '1')
			E.push_back(edge{u, N + u, -1});
	}
	if (!bellman_ford(N * 2, E, 0, d)) return "";
	string t(N, ' ');
	for (int u = 0; u < N; u++)
		if (d[u] == d[N + u]) t[u] = '0';
		else t[u] = '1';
	return t;
}

int main() {
	int w; cin >> w;
	string s; cin >> s;
	int N = s.length();
	string t = check(N, s);
	if (t != "") {
		cout << t << endl;
		return 0;
	}
	for (int i = N - 1; i >= 0; i--) {
		if (s[i] == '0') {
			s[i] = '1';
			string t = check(N, s);
			if (t != "") {
				cout << t << endl;
				return 0;
			}
		}
		s[i] = '?';
	}
	cout << "no" << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 3204
final_rank: 3

code_snippet: #include<stdio.h>
#include<algorithm>
#include<vector>
using namespace std;
int n;
char in[1000];
char ret[1000];
int dist[1000];
bool check(){
	vector<pair<pair<int,int>,int> >edge;
	for(int i=0;i<(1<<(2*n+1));i++){
		int L,R,to;
		L=0;R=0;to=i/2;
		if(ret[i]=='?'){
			L--;
		}else if(ret[i]=='1'){
			L--;R--;
		}
		edge.push_back(make_pair(make_pair(i*2+1,i*2),R));
		edge.push_back(make_pair(make_pair(i*2,i*2+1),-L));
		
		edge.push_back(make_pair(make_pair(i*2,to*2+1),0));
		edge.push_back(make_pair(make_pair(to*2+1,i*2),0));
		L=0;R=0;to=i/2+(1<<(2*n));
		if(ret[i]=='?'){
			L--;
		}else if(ret[i]=='1'){
			L--;R--;
		}
		edge.push_back(make_pair(make_pair(i*2,to*2+1),1));
		edge.push_back(make_pair(make_pair(to*2+1,i*2),-1));
	}
	for(int i=0;i<(1<<(2*n+2));i++)dist[i]=999999999;
	dist[0]=0;
	for(int i=0;i<=(1<<(2*n+2));i++){
		for(int j=0;j<edge.size();j++){
			if(dist[edge[j].first.first]+edge[j].second<dist[edge[j].first.second]){
				dist[edge[j].first.second]=dist[edge[j].first.first]+edge[j].second;
				if(i==(1<<(n*2+2)))return false;
			}
		}
	}
	return true;
}
int main(){
	int a;scanf("%d",&a);
	n=a;
	scanf("%s",in);
	for(int i=0;i<(1<<(2*a+1));i++)ret[i]=in[i];
	if(check()){
		printf("%s\n",ret);return 0;
	}
	for(int i=(1<<(2*a+1))-1;i>=0;i--){
		ret[i]='?';
		if(in[i]=='0'){
			ret[i]='1';
			if(check()){
				for(int j=i+1;j<(1<<(2*a+1));j++){
					ret[j]='0';
		//			printf("%s\n",ret);
					if(!check())ret[j]='1';
				}
				printf("%s\n",ret);return 0;
			}
			ret[i]='?';
		}
	}
	printf("no\n");
}
accuracy: 1
time_ms: 50
memory_kb: 1044
final_rank: 1

code_snippet: #include<cstdio>
#include<string>
#include<algorithm>

using namespace std;

const int MAX_V = 256, MAX_E = 768 + 1;

int head[MAX_V],nxt[MAX_E],to[MAX_E],cost[MAX_E];
int m;

void init_edge(){
	m=0;
	for(int i=0;i<MAX_V;i++) head[i]=-1;
}

void add_edge(int u,int v,int c){
	nxt[m]=head[u];to[m]=v;cost[m]=c;head[u]=m;m++;
}

int mask;

int V,E;
int N;

void add_edge2(int u,int v,int lo,int hi){
	add_edge(v,u,-lo);
	add_edge(u,v,hi);
}

void getGraph(string str){
	init_edge();
	for(int i=0;i<N;i++){
		int lo,hi;
		if(i<str.size()){
			lo=str[i]-'0';
			hi=lo;
		}else{
			lo=0;
			hi=1;
		}
		add_edge2(i*2,i*2+1,lo,hi);
	}
	for(int v=0;v<N;v++){
		for(int bit=0;bit<=1;bit++){
			int u=((v<<1)&mask)|bit;
			int cost=-bit;
			add_edge2(v*2+1,u*2,cost,cost);
		}
	}
	add_edge2(1,0,0,0);
}

int dis[MAX_V];
bool negCycle(){
	bool flg=false;
	for(int v=0;v<V;v++){
		dis[v]=0;
	}
	for(int stage=0;stage<=E+1;stage++){
		bool update=false;
		for(int v=0;v<V;v++){
			for(int e=head[v];e!=-1;e=nxt[e]){
				int u=to[e];
				int c=cost[e];
				int ndis=dis[v]+c;
				if(dis[u]>ndis){
					update=true;
					dis[u]=ndis;
				}
			}
		}
		if(stage==E+1) flg=update;
		if(update==false) break;
	}
	return flg;
}

bool check(string str){
	getGraph(str);
	bool flg=negCycle();
	return !flg;
}

string solve(string lo_str){
	if(lo_str[0]=='1') return "no";
	if(check(lo_str)) return lo_str;
	int match=lo_str.size()-1;
	string str=lo_str;
	string ans="";
	for(;match>=1;match--){
		if(lo_str[match]=='1') continue;
		string cur=lo_str.substr(0,match);
		cur+='1';
		bool flg=check(cur);
		if(flg){
			ans=cur;
			break;
		}
	}
	if(ans=="") return "no";
//	printf("%s\n",ans.c_str());
//	ans+='1';
	for(int i=match+1;i<lo_str.size();i++){
		string cur=ans+'0';
		if(check(cur)) ans=cur;
		else ans+='1';
	}
	return ans;
}

char ch[130];

int main(){
	int w;
	scanf("%d%s",&w,ch);
	string lo_str=ch;
	N=lo_str.size();
	V=N*2;
	E=N*6+1;
	mask=N-1;
	string ans=solve(lo_str);
	printf("%s\n",ans.c_str());
	return 0;
}
accuracy: 1
time_ms: 150
memory_kb: 1072
final_rank: 9