int id[1 << 18]; voidbfs(){ queue<int> Q; fill(id, id + (1 << 18), 0); for (int i = 1; i <= n; ++i) id[a[i]] = i, Q.push(a[i]); while (!Q.empty()) { int u = Q.front(); Q.pop(); for (int o = 0; o < 18; ++o) { int v = u ^ 1 << o; if (!id[v]) id[v] = id[u], Q.push(v); elseif (id[u] != id[v]) E.push_back({ id[u], id[v], __builtin_popcount(a[id[u]] ^ a[id[v]]) }); } } }
int fa[maxn]; voidinit_fa(int n){ for (int i = 1; i <= n; ++i) fa[i] = i; }
intfind(int x){ return fa[x] == x ? x : fa[x] = find(fa[x]); }
inlineboolmerge(int x, int y){ int fx = find(x), fy = find(y); if (fx == fy) return0; fa[fx] = fy; return1; }
vector<int> G[maxn]; vector<pair<int, int>> ans; voiddfs(int u, int fa){ ans.emplace_back(u, fa ? fa : n); for (auto v : G[u]) { if (v == fa) continue; dfs(v, u); } }
voidwork(){ cin >> n; E.clear(); ans.clear(); for (int i = 1; i <= n; ++i) G[i].clear(); for (int i = 1; i <= n; ++i) cin >> a[i]; bfs(); sort(E.begin(), E.end()); init_fa(n); int mst = 0; for (auto [u, v, w] : E) if (merge(u, v)) { mst += w; G[u].push_back(v); G[v].push_back(u); } dfs(1, 0); cout << mst << "\n"; for (auto [fi, se] : ans) cout << fi << " "; cout << "\n"; for (auto [fi, se] : ans) cout << se << " "; cout << "\n"; }