Problem p03531 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using ll = long long;
using ld = long double;
using namespace std;

const int N = 18;
const int MN = (1 << N);

vector<int> e[MN];
vector<int> reve[MN];

int c[MN];

int req[MN];
int a[MN];

int cur;

set<int> avail;

void no() {
  cout << "NO\\n";
  exit(0);
}

int dfs(int x) {
  if (a[x] != -1) {
    return a[x];
  }

  int vl = MN;

  for (int y : e[x]) {
    vl = min(vl, dfs(y));
  }

  auto it = avail.lower_bound(vl);

  if (it == avail.begin()) {
    no();
  }

  --it;
  vl = a[x] = *it;
  avail.erase(it);

  return vl;
}

int buf[MN];
int b[MN];
int inita[MN];

void dfsgo(int v, int val) {
  for (int u : e[v]) {
    if (dfs(u) <= val) {
      no();
    }
  }

  a[v] = val;
}

int n, k;

void cool() {
  for (int i = k; i > 0; --i) {
    copy(a, a + n, buf);
    int c = 1 << i;
    for (int l = 0; l < n; l += c) {
      int r = l + c;
      int md = (l + r) / 2;
      for (int cc = 0; cc < (md - l); ++cc) {
        a[l + cc] = buf[l + cc * 2];
        a[md + cc] = buf[l + cc * 2 + 1];
      }
    }
  }

  for (int i = 0; i < n; ++i) {
    assert(a[i] == inita[i]);
  }
}

int main() {
#ifdef PAUNSVOKNO
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  cin >> k;
  n = (1 << k);
  cur = n - 1;
  fill(req, req + n, -1);

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < k; ++j) {
      if ((i & (1 << j)) == 0) {
        e[i].push_back(i + (1 << j));
      }
    }
  }

  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    --a[i];
    if (a[i] >= 0) {
      req[a[i]] = i;
    }
  }

  for (int i = 0; i < n; ++i) {
    if (req[i] == -1) {
      avail.insert(i);
    }
  }

  for (int i = n - 1; i >= 0; --i) {
    if (req[i] != -1) {
      dfsgo(req[i], i);
    }
  }

  if (a[0] > 0) {
    no();
  }

  dfsgo(0, 0);
  copy(a, a + n, inita);

  for (int i = 2; i <= k; ++i) {
    copy(a, a + n, buf);
    for (int j = 0; j < (1 << (k - i)); ++j) {
      int l = (j << i);
      int r = ((j + 1) << i);
      int m = (l + r) / 2;

      for (int i2 = 0; i2 < (1 << (i - 1)); ++i2) {
        a[l + i2 * 2] = buf[l + i2];
        a[l + i2 * 2 + 1] = buf[m + i2];
      }
    }
  }

  cout << "YES\\n";
  for (int i = 0; i < n; ++i) {
    cout << a[i] + 1 << " ";
  }

  cool();
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using ll = long long;
using ld = long double;
using namespace std;

const int N = 18;
const int MN = (1 << N);

vector<int> e[MN];
vector<int> reve[MN];

int c[MN];

int req[MN];
int a[MN];

int cur;

set<int> avail;

void no() {
  cout << "NO\\n";
  exit(0);
}

int dfs(int x) {
  if (a[x] != -1) {
    return a[x];
  }

  int vl = MN;

  for (int y : e[x]) {
    vl = min(vl, dfs(y));
  }

  auto it = avail.lower_bound(vl);

  if (it == avail.begin()) {
    no();
  }

  --it;
  vl = a[x] = *it;
  avail.erase(it);

  return vl;
}

int buf[N];
int b[N];
int inita[N];

void dfsgo(int v, int val) {
  for (int u : e[v]) {
    if (dfs(u) <= val) {
      no();
    }
  }

  a[v] = val;
}

int n, k;

void cool() {
  for (int i = k; i > 0; --i) {
    copy(a, a + n, buf);
    int c = 1 << i;
    for (int l = 0; l < n; l += c) {
      int r = l + c;
      int md = (l + r) / 2;
      for (int cc = 0; cc < (md - l); ++cc) {
        a[l + cc] = buf[l + cc * 2];
        a[md + cc] = buf[l + cc * 2 + 1];
      }
    }
  }

  for (int i = 0; i < n; ++i) {
    // assert(a[i] == inita[i]);
  }
}

int main() {
#ifdef PAUNSVOKNO
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  cin >> k;
  n = (1 << k);
  cur = n - 1;
  fill(req, req + n, -1);

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < k; ++j) {
      if ((i & (1 << j)) == 0) {
        e[i].push_back(i + (1 << j));
      }
    }
  }

  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    --a[i];
    if (a[i] >= 0) {
      req[a[i]] = i;
    }
  }

  for (int i = 0; i < n; ++i) {
    if (req[i] == -1) {
      avail.insert(i);
    }
  }

  for (int i = n - 1; i >= 0; --i) {
    if (req[i] != -1) {
      dfsgo(req[i], i);
    }
  }

  if (a[0] > 0) {
    no();
  }

  dfsgo(0, 0);
  copy(a, a + n, inita);

  for (int i = 2; i <= k; ++i) {
    copy(a, a + n, buf);
    for (int j = 0; j < (1 << (k - i)); ++j) {
      int l = (j << i);
      int r = ((j + 1) << i);
      int m = (l + r) / 2;

      for (int i2 = 0; i2 < (1 << (i - 1)); ++i2) {
        a[l + i2 * 2] = buf[l + i2];
        a[l + i2 * 2 + 1] = buf[m + i2];
      }
    }
  }

  cout << "YES\\n";
  for (int i = 0; i < n; ++i) {
    cout << a[i] + 1 << " ";
  }

  cool();
}