{"model_name":"gpt-oss-20b","codes":{"1":{"ahc001":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int n;\n    if (!(cin >> n)) return 0;\n    vector<int> x(n), y(n);\n    for (int i = 0; i < n; ++i) {\n        int r;\n        cin >> x[i] >> y[i] >> r;   // r is read only to consume the input\n    }\n    for (int i = 0; i < n; ++i) {\n        cout << x[i] << ' '\n             << y[i] << ' '\n             << x[i] + 1 << ' '\n             << y[i] + 1 << '\\n';\n    }\n    return 0;\n}","ahc002":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    int si, sj;\n    if (!(cin >> si >> sj)) return 0;\n\n    const int N = 50;\n    vector<vector<int>> tile(N, vector<int>(N));\n    vector<vector<int>> val(N, vector<int>(N));\n    int maxTile = -1;\n\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < N; ++j) {\n            cin >> tile[i][j];\n            maxTile = max(maxTile, tile[i][j]);\n        }\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < N; ++j)\n            cin >> val[i][j];\n\n    vector<char> answer;\n    vector<char> used(maxTile + 1, 0);\n    used[tile[si][sj]] = 1;                // start tile already visited\n\n    int cur_i = si, cur_j = sj;\n    const int di[4] = {-1, 1, 0, 0};\n    const int dj[4] = {0, 0, -1, 1};\n    const char dir[4] = {'U', 'D', 'L', 'R'};\n\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n\n    while (true) {\n        // collect best candidates\n        int bestVal = -1;\n        vector<pair<int,int>> best;   // list of indices of candidate dirs\n        for (int d = 0; d < 4; ++d) {\n            int ni = cur_i + di[d];\n            int nj = cur_j + dj[d];\n            if (ni < 0 || ni >= N || nj < 0 || nj >= N) continue;\n            int tid = tile[ni][nj];\n            if (used[tid]) continue;\n            int candVal = val[ni][nj];\n            if (candVal > bestVal) {\n                bestVal = candVal;\n                best.clear();\n                best.push_back({d, 0});   // index of direction, placeholder\n            } else if (candVal == bestVal) {\n                best.push_back({d, 0});\n            }\n        }\n        if (best.empty()) break;   // no move is possible\n\n        // randomly pick among the best candidates\n        uniform_int_distribution<int> dist(0, (int)best.size() - 1);\n        int chosenIndex = dist(rng);\n        int chosenDir = best[chosenIndex].first;\n\n        answer.push_back(dir[chosenDir]);\n\n        cur_i += di[chosenDir];\n        cur_j += dj[chosenDir];\n        used[tile[cur_i][cur_j]] = 1;\n    }\n\n    for (char c : answer) cout << c;\n    cout << '\\n';\n    return 0;\n}","ahc003":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    const int Q = 1000;           // number of queries\n    for (int q = 0; q < Q; ++q) {\n        int si, sj, ti, tj;\n        if (!(cin >> si >> sj >> ti >> tj)) return 0;   // safety\n\n        string path;\n        while (sj < tj) { path += 'R'; ++sj; }\n        while (sj > tj) { path += 'L'; --sj; }\n        while (si < ti) { path += 'D'; ++si; }\n        while (si > ti) { path += 'U'; --si; }\n\n        cout << path << '\\n' << flush;\n\n        int dummy;     // judge sends round(b*e)\n        if (!(cin >> dummy)) return 0;    // safety\n    }\n    return 0;\n}","ahc004":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    int N, M;\n    if (!(cin >> N >> M)) return 0;\n    string tmp;\n    for (int i = 0; i < M; ++i) cin >> tmp;   // just consume the input\n\n    string row(N, '.');\n    for (int i = 0; i < N; ++i) {\n        cout << row << '\\n';\n    }\n    return 0;\n}","ahc005":"#include <bits/stdc++.h>\nusing namespace std;\n\nint N, si, sj;\nvector<string> g;                 // original map\nvector<vector<bool>> road;        // true if road\nvector<vector<bool>> vis;         // visited during DFS\nstring answer;                    // accumulated moves\n\nconst int dx[4] = {-1, 1, 0, 0};\nconst int dy[4] = {0, 0, -1, 1};\nconst char dirC[4] = {'U', 'D', 'L', 'R'};\nconst char revC[4] = {'D', 'U', 'R', 'L'};\n\nvoid dfs(int x, int y) {\n    vis[x][y] = true;\n    for (int d = 0; d < 4; d++) {\n        int nx = x + dx[d], ny = y + dy[d];\n        if (0 <= nx && nx < N && 0 <= ny && ny < N &&\n            road[nx][ny] && !vis[nx][ny]) {\n            answer.push_back(dirC[d]);       // move forward\n            dfs(nx, ny);\n            answer.push_back(revC[d]);       // move back\n        }\n    }\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    if (!(cin >> N >> si >> sj)) return 0;\n    g.resize(N);\n    for (int i = 0; i < N; i++) cin >> g[i];\n\n    road.assign(N, vector<bool>(N, false));\n    vis.assign(N, vector<bool>(N, false));\n    for (int i = 0; i < N; i++)\n        for (int j = 0; j < N; j++)\n            road[i][j] = (g[i][j] != '#');\n\n    dfs(si, sj);                 // starts at the given position\n    cout << answer << '\\n';\n    return 0;\n}","future-contest-2022-qual":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, K, R;\n    if (!(cin >> N >> M >> K >> R)) return 0;\n\n    // read task difficulties (not used)\n    vector<vector<int>> d(N, vector<int>(K));\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < K; ++j)\n            cin >> d[i][j];\n\n    // build graph\n    vector<vector<int>> dependents(N);\n    vector<int> indeg(N, 0);\n    for (int i = 0; i < R; ++i) {\n        int u, v;                // 1\u2011based in input\n        cin >> u >> v;\n        --u; --v;\n        dependents[u].push_back(v);\n        ++indeg[v];\n    }\n\n    queue<int> ready;\n    for (int i = 0; i < N; ++i)\n        if (indeg[i] == 0) ready.push(i);\n\n    vector<int> worker_task(M, -1);        // -1 = idle, else task index\n    vector<bool> started(N, false), done(N, false);\n\n    while (true) {\n        // ----- start new day -----\n        vector<pair<int, int>> assignments;\n        for (int w = 0; w < M; ++w) {\n            if (worker_task[w] != -1) continue;\n            if (ready.empty()) break;\n            int t = ready.front(); ready.pop();\n            worker_task[w] = t;\n            started[t] = true;\n            assignments.emplace_back(w, t);\n        }\n\n        // output assignments line\n        cout << assignments.size();\n        for (auto &p : assignments) {\n            cout << ' ' << (p.first + 1) << ' ' << (p.second + 1);\n        }\n        cout << '\\n';\n        cout.flush();\n\n        // ----- read finished workers -----\n        int n;\n        if (!(cin >> n)) return 0;          // just in case\n        if (n == -1) break;                // finished or 2000 days\n\n        vector<int> finished(n);\n        for (int i = 0; i < n; ++i) cin >> finished[i];\n\n        for (int f : finished) {\n            int w = f - 1;\n            int t = worker_task[w];\n            if (t == -1) continue;         // should not happen\n            // propagate completion\n            for (int v : dependents[t]) {\n                if (--indeg[v] == 0 && !started[v] && !done[v]) {\n                    ready.push(v);\n                }\n            }\n            done[t] = true;\n            worker_task[w] = -1;            // worker becomes idle\n        }\n    }\n    return 0;\n}","ahc006":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    const int N = 1000;\n    vector<int> a(N), b(N), c(N), d(N);\n    for (int i = 0; i < N; ++i) {\n        if (!(cin >> a[i] >> b[i] >> c[i] >> d[i])) return 0;\n    }\n\n    const int M = 50;                         // number of orders to choose\n    vector<pair<int,int>> route;\n    route.emplace_back(400, 400);             // office start\n\n    for (int i = 0; i < M; ++i) {\n        route.emplace_back(a[i], b[i]);       // restaurant\n        route.emplace_back(c[i], d[i]);       // destination\n    }\n    route.emplace_back(400, 400);             // office final\n\n    // Output the chosen indices (1\u2011based)\n    cout << M;\n    for (int i = 1; i <= M; ++i) cout << ' ' << i;\n    cout << '\\n';\n\n    // Output the route\n    cout << route.size();\n    for (auto [x, y] : route) cout << ' ' << x << ' ' << y;\n    cout << '\\n';\n\n    return 0;\n}","ahc007":"#include <bits/stdc++.h>\nusing namespace std;\n\n// ---------- DSU ----------\nstruct DSU {\n    vector<int> p, r;\n    DSU(int n = 0) { init(n); }\n    void init(int n) {\n        p.resize(n);\n        r.assign(n, 0);\n        iota(p.begin(), p.end(), 0);\n    }\n    int find(int x) {\n        if (p[x] == x) return x;\n        return p[x] = find(p[x]);            // path compression\n    }\n    bool unite(int a, int b) {\n        a = find(a); b = find(b);\n        if (a == b) return false;\n        if (r[a] < r[b]) swap(a, b);\n        p[b] = a;\n        if (r[a] == r[b]) ++r[a];\n        return true;\n    }\n};\n\n// ---------- main ----------\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    const int N = 400, M = 1995;\n    // read coordinates, but we never use them\n    for (int i = 0; i < N; ++i) {\n        int x, y; cin >> x >> y;\n    }\n\n    vector<int> u(M), v(M);\n    for (int i = 0; i < M; ++i) {\n        cin >> u[i] >> v[i];\n    }\n\n    DSU dsu(N);\n\n    for (int i = 0; i < M; ++i) {\n        int li;            // the real length\n        cin >> li;\n\n        if (dsu.unite(u[i], v[i])) {\n            cout << 1 << '\\n';\n        } else {\n            cout << 0 << '\\n';\n        }\n        cout.flush();        // essential for interactive judge\n    }\n    return 0;\n}","ahc008":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N;\n    if (!(cin >> N)) return 0;               // no input\n    vector<tuple<int,int,int>> pets(N);\n    for (int i = 0; i < N; ++i) {\n        int x, y, t;\n        cin >> x >> y >> t;\n        pets[i] = {x, y, t};\n    }\n\n    int M;\n    cin >> M;\n    vector<pair<int,int>> humans(M);\n    for (int i = 0; i < M; ++i) {\n        int x, y;\n        cin >> x >> y;\n        humans[i] = {x, y};\n    }\n\n    // 300 turns\n    string action(M, '.');          // '.' * M\n\n    for (int turn = 0; turn < 300; ++turn) {\n        cout << action << \"\\n\";\n        cout.flush();              // important for interactive judge\n\n        // read N pet move strings (ignore)\n        for (int i = 0; i < N; ++i) {\n            string s;\n            cin >> s;              // each is '.' or directions\n        }\n    }\n\n    return 0;\n}","ahc009":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Node {\n    int i, j;\n};\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    int si, sj, ti, tj;\n    double p;\n    if (!(cin >> si >> sj >> ti >> tj >> p)) return 0;\n    vector<string> h(20);              // 20 lines of 19 chars\n    for (int i = 0; i < 20; ++i) cin >> h[i];\n    vector<string> v(19);              // 19 lines of 20 chars\n    for (int i = 0; i < 19; ++i) cin >> v[i];\n\n    const int N = 20;\n    // BFS structures\n    vector<vector<bool>> seen(N, vector<bool>(N, false));\n    vector<vector<int>> parenti(N, vector<int>(N, -1));\n    vector<vector<int>> parentj(N, vector<int>(N, -1));\n    vector<vector<char>> dir(N, vector<char>(N, 0));\n\n    queue<Node> q;\n    q.push({si, sj});\n    seen[si][sj] = true;\n    int di[4] = {-1, 1, 0, 0};\n    int dj[4] = {0, 0, -1, 1};\n    char dch[4] = {'U', 'D', 'L', 'R'};\n\n    while (!q.empty()) {\n        Node cur = q.front(); q.pop();\n        if (cur.i == ti && cur.j == tj) break;  // reached target\n        for (int k = 0; k < 4; ++k) {\n            int ni = cur.i + di[k];\n            int nj = cur.j + dj[k];\n            if (ni < 0 || ni >= N || nj < 0 || nj >= N) continue;\n            // check wall between (cur.i,cur.j) and (ni,nj)\n            bool blocked = false;\n            if (di[k] == -1) {               // Up\n                if (v[cur.i - 1][cur.j] == '1') blocked = true;\n            } else if (di[k] == 1) {         // Down\n                if (v[cur.i][cur.j] == '1') blocked = true;\n            } else if (dj[k] == -1) {        // Left\n                if (h[cur.i][cur.j - 1] == '1') blocked = true;\n            } else if (dj[k] == 1) {         // Right\n                if (h[cur.i][cur.j] == '1') blocked = true;\n            }\n            if (blocked) continue;\n            if (seen[ni][nj]) continue;\n            seen[ni][nj] = true;\n            parenti[ni][nj] = cur.i;\n            parentj[ni][nj] = cur.j;\n            dir[ni][nj] = dch[k];\n            q.push({ni, nj});\n        }\n    }\n\n    // Reconstruct path\n    string ans;\n    int ci = ti, cj = tj;\n    while (!(ci == si && cj == sj)) {\n        ans.push_back(dir[ci][cj]);\n        int pi = parenti[ci][cj];\n        int pj = parentj[ci][cj];\n        ci = pi; cj = pj;\n    }\n    reverse(ans.begin(), ans.end());\n    cout << ans << '\\n';\n    return 0;\n}","ahc010":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    // Read and ignore the 30x30 grid\n    string line;\n    for (int i = 0; i < 30; ++i) {\n        if (!(cin >> line)) return 0;\n    }\n    \n    // Output 900 zeros\n    string ans(900, '0');\n    cout << ans << '\\n';\n    return 0;\n}","ahc011":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int N, T;\n    if (!(cin >> N >> T)) return 0;\n    string row;\n    // read and ignore the board\n    for (int i = 0; i < N; ++i) {\n        cin >> row;\n    }\n    // output an empty sequence\n    cout << '\\n';\n    return 0;\n}","ahc012":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int N, K;\n    if (!(cin >> N >> K)) return 0;\n    for (int i = 0; i < 10; ++i) int x; // read a_i\n    for (int i = 0; i < N; ++i) { int x, y; cin >> x >> y; }\n    \n    cout << 0 << '\\n';     // No cuts\n    return 0;\n}","ahc014":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int N, M;\n    if (!(cin >> N >> M)) return 0;\n    // Read and ignore the M dot coordinates\n    for (int i = 0; i < M; ++i) {\n        int x, y;\n        cin >> x >> y;\n    }\n    // No operations\n    cout << 0 << '\\n';\n    return 0;\n}","ahc015":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    vector<int> flavor(100);\n    for (int i = 0; i < 100; ++i) cin >> flavor[i];   // read but ignore\n\n    for (int t = 0; t < 100; ++t) {\n        int p;               // read position \u2013 unused\n        if (!(cin >> p)) return 0;\n        cout << 'F' << '\\n' << flush;   // always tilt forward\n    }\n    return 0;\n}","ahc016":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int M; double eps;\n    if (!(cin >> M >> eps)) return 0;\n\n    const int N = 20;\n    const int L = N * (N - 1) / 2;          // 190\n\n    cout << N << '\\n';\n    for (int k = 0; k < M; ++k) {\n        string s;\n        s.append(k, '1');\n        s.append(L - k, '0');\n        cout << s << '\\n';\n    }\n    cout.flush();\n\n    for (int q = 0; q < 100; ++q) {\n        string H;\n        cin >> H;\n        int ones = 0;\n        for (char c : H) if (c == '1') ++ones;\n        int ans = min(ones, M - 1);\n        cout << ans << '\\n';\n        cout.flush();\n    }\n    return 0;\n}","ahc017":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int N, M, D, K;\n    if (!(cin >> N >> M >> D >> K)) return 0;\n    \n    // Read edges \u2013 weights are irrelevant for our schedule\n    for (int i = 0; i < M; ++i) {\n        int u, v, w;\n        cin >> u >> v >> w;\n    }\n    // Read coordinates \u2013 also irrelevant\n    for (int i = 0; i < N; ++i) {\n        int x, y;\n        cin >> x >> y;\n    }\n    \n    vector<int> res;\n    res.reserve(M);\n    for (int i = 0; i < M; ++i) {\n        int day = (i % D) + 1;\n        res.push_back(day);\n    }\n    \n    for (int i = 0; i < M; ++i) {\n        if (i) cout << ' ';\n        cout << res[i];\n    }\n    cout << '\\n';\n    return 0;\n}","ahc019":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    int D;\n    if (!(cin >> D)) return 0;\n\n    vector<string> f[2], r[2];\n    for (int k = 0; k < 2; ++k) {\n        f[k].resize(D);\n        r[k].resize(D);\n        for (int i = 0; i < D; ++i) cin >> f[k][i];\n        for (int i = 0; i < D; ++i) cin >> r[k][i];\n    }\n\n    // result arrays\n    vector<int> b1(D*D*D, 0), b2(D*D*D, 0);\n    int next_id = 1;                       // block id counter\n\n    for (int kval = 0; kval < 2; ++kval) {\n        auto &bk = (kval == 0 ? b1 : b2);\n        for (int z = 0; z < D; ++z)\n            for (int x = 0; x < D; ++x)\n                if (f[kval][z][x] == '1')\n                    for (int y = 0; y < D; ++y)\n                        if (r[kval][z][y] == '1') {\n                            int idx = x*D*D + y*D + z;\n                            bk[idx] = next_id++;\n                        }\n    }\n\n    int n = next_id - 1;          // number of blocks\n    cout << n << '\\n';\n    for (int i = 0; i < (int)b1.size(); ++i) {\n        if (i) cout << ' ';\n        cout << b1[i];\n    }\n    cout << '\\n';\n    for (int i = 0; i < (int)b2.size(); ++i) {\n        if (i) cout << ' ';\n        cout << b2[i];\n    }\n    cout << '\\n';\n    return 0;\n}","ahc020":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int N, M, K;\n    if (!(cin >> N >> M >> K)) return 0;\n    \n    // Discard coordinates of vertices\n    for (int i = 0; i < N; ++i) {\n        int x, y;\n        cin >> x >> y;\n    }\n    // Discard edges\n    for (int i = 0; i < M; ++i) {\n        int u, v, w;\n        cin >> u >> v >> w;\n    }\n    // Discard residents\n    for (int i = 0; i < K; ++i) {\n        int a, b;\n        cin >> a >> b;\n    }\n    \n    // Output zero strengths\n    for (int i = 0; i < N; ++i) {\n        if (i) cout << ' ';\n        cout << 0;\n    }\n    cout << '\\n';\n    // Output all edges off\n    for (int i = 0; i < M; ++i) {\n        if (i) cout << ' ';\n        cout << 0;\n    }\n    cout << '\\n';\n    \n    return 0;\n}","ahc021":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    const int N = 30;\n    const int S = N * (N + 1) / 2;          // 465\n    vector<int> a(S);\n    for (int i = 0; i < S; ++i) cin >> a[i];\n\n    // ---------- pre\u2011compute coordinates and row number ----------\n    vector<pair<int,int>> coord(S);\n    vector<int> row_of_idx(S);\n    for (int r = 0; r < N; ++r) {\n        int start = r * (r + 1) / 2;\n        for (int c = 0; c <= r; ++c) {\n            int idx = start + c;\n            coord[idx] = {r, c};\n            row_of_idx[idx] = r;\n        }\n    }\n\n    // ---------- record of swaps ----------\n    struct Op {int x1, y1, x2, y2;};\n    vector<Op> ops;\n\n    // ---------- heapify bottom\u2011up ----------\n    const int INTERNAL = S - N;          // indices 0 \u2026 434 are internal\n    for (int start = INTERNAL - 1; start >= 0; --start) {\n        int cur = start;\n        while (true) {\n            int r = row_of_idx[cur];\n            int left = cur + r + 1;\n            int right = cur + r + 2;\n            int smallest = cur;\n            if (left < S && a[left] < a[smallest]) smallest = left;\n            if (right < S && a[right] < a[smallest]) smallest = right;\n            if (smallest == cur) break;\n            swap(a[cur], a[smallest]);\n            ops.push_back({coord[cur].first, coord[cur].second,\n                           coord[smallest].first, coord[smallest].second});\n            cur = smallest;\n        }\n    }\n\n    // ---------- output ----------\n    cout << ops.size() << '\\n';\n    for (const auto &p : ops)\n        cout << p.x1 << ' ' << p.y1 << ' ' << p.x2 << ' ' << p.y2 << '\\n';\n    return 0;\n}","toyota2023summer-final":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Cell {int x; int y;};\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    const int D = 9;\n    int N; \n    if (!(cin >> D >> N)) return 0;\n    vector<vector<int>> obst(D, vector<int>(D, 0));\n    for (int i = 0; i < N; ++i) {\n        int r, c; cin >> r >> c;\n        obst[r][c] = 1;\n    }\n\n    // entrance\n    int ex = 0, ey = (D-1)/2;   // (0,4)\n\n    // ---------- pre\u2011compute distances ----------\n    const int INF = 1e9;\n    vector<vector<int>> dist(D, vector<int>(D, INF));\n    queue<Cell> q;\n    dist[ex][ey] = 0;\n    q.push({ex,ey});\n    const int dx[4] = {-1,1,0,0};\n    const int dy[4] = {0,0,-1,1};\n\n    while (!q.empty()) {\n        auto cur = q.front(); q.pop();\n        for (int dir = 0; dir < 4; ++dir) {\n            int nx = cur.x + dx[dir];\n            int ny = cur.y + dy[dir];\n            if (nx<0||nx>=D||ny<0||ny>=D) continue;\n            if (obst[nx][ny]) continue;\n            if (dist[nx][ny] != INF) continue;\n            dist[nx][ny] = dist[cur.x][cur.y] + 1;\n            q.push({nx,ny});\n        }\n    }\n\n    // list of all free squares except entrance\n    vector<Cell> cells;\n    for (int i = 0; i < D; ++i)\n        for (int j = 0; j < D; ++j) {\n            if (obst[i][j]) continue;\n            if (i==ex && j==ey) continue;\n            cells.push_back({i,j});\n        }\n\n    // sort by decreasing distance\n    sort(cells.begin(), cells.end(),\n        [&](const Cell& a, const Cell& b){\n            return dist[a.x][a.y] > dist[b.x][b.y];\n        });\n\n    int S = cells.size();        // number of containers\n    int idx = 0;                 // next cell to use\n\n    // ----------------- placement -----------------\n    for (int k = 0; k < S; ++k) {\n        int t; cin >> t;            // read container number\n        auto c = cells[idx++];\n        cout << c.x << ' ' << c.y << '\\n' << flush;\n    }\n\n    // ---------- take\u2011out: BFS from entrance ----------\n    vector<vector<int>> vis(D, vector<int>(D, 0));\n    queue<Cell> q2;\n    vis[ex][ey] = 1;\n    q2.push({ex,ey});\n    vector<Cell> removalOrder;\n\n    while (!q2.empty()) {\n        auto cur = q2.front(); q2.pop();\n        for (int dir=0; dir<4; ++dir) {\n            int nx = cur.x + dx[dir];\n            int ny = cur.y + dy[dir];\n            if (nx<0||nx>=D||ny<0||ny>=D) continue;\n            if (obst[nx][ny]) continue;\n            if (vis[nx][ny]) continue;\n            vis[nx][ny] = 1;\n            q2.push({nx,ny});\n            if (!(nx==ex && ny==ey)) {\n                removalOrder.push_back({nx,ny});\n            }\n        }\n    }\n\n    // output removal order\n    for (auto &c: removalOrder) {\n        cout << c.x << ' ' << c.y << '\\n';\n    }\n    return 0;\n}","ahc024":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int n, m;\n    if (!(cin >> n >> m)) return 0;  // just in case\n    \n    for (int i = 0; i < n; ++i) {\n        for (int j = 0; j < n; ++j) {\n            int v;  cin >> v;\n            cout << v;\n            if (j + 1 < n) cout << ' ';\n        }\n        cout << '\\n';\n    }\n    return 0;\n}","ahc025":"//  Offline / non\u2011interactive stub for the atcoder.balancing problem\n//  The program simply prints a trivial partition: item i -> group i % D.\n\n#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int N, D, Q;\n    if (!(cin >> N >> D >> Q)) return 0;\n    \n    // The judge may provide the true weights for the offline\n    // testing environment; we ignore them.\n    vector<long long> w(N);\n    for (int i = 0; i < N; ++i) {\n        if (cin >> w[i]) break;   // sometimes the weights are appended\n    }\n    \n    // We are not performing any queries, so just produce the output.\n    for (int i = 0; i < N; ++i) {\n        if (i) cout << ' ';\n        cout << (i % D);\n    }\n    cout << '\\n';\n    return 0;\n}","ahc026":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    const int n = 200;   // fixed in problem statement\n    const int m = 10;    // fixed in problem statement\n\n    vector<vector<int>> stacks(m);\n\n    for (int i = 0; i < m; ++i) {\n        stacks[i].resize(n / m);\n        for (int j = 0; j < n / m; ++j) {\n            cin >> stacks[i][j];          // bottom -> top\n        }\n    }\n\n    vector<pair<int,int>> ops;          // store (v , i)\n    vector<bool> removed(n + 1, false);\n\n    for (int v = 1; v <= n; ++v) {\n        while (true) {\n            int src = -1;          // stack index where v is\n            int pos = -1;          // position from bottom\n            for (int i = 0; i < m; ++i) {\n                for (int j = 0; j < (int)stacks[i].size(); ++j) {\n                    if (stacks[i][j] == v) {\n                        src = i;\n                        pos = j;\n                        break;\n                    }\n                }\n                if (src != -1) break;\n            }\n            if (src == -1) {            // already removed, should not happen\n                break;\n            }\n\n            if (pos == (int)stacks[src].size() - 1) {\n                // v is on top -> take it out\n                ops.emplace_back(v, 0);\n                stacks[src].pop_back();\n                removed[v] = true;\n                break;\n            } else {\n                // choose destination stack with minimal height (different stack)\n                int dest = -1, best = INT_MAX;\n                for (int i = 0; i < m; ++i) {\n                    if (i == src) continue;\n                    if ((int)stacks[i].size() < best) {\n                        best = stacks[i].size();\n                        dest = i;\n                    }\n                }\n                // move segment [pos .. end] from src to dest\n                vector<int> segment;\n                for (int idx = pos; idx < (int)stacks[src].size(); ++idx) {\n                    segment.push_back(stacks[src][idx]);\n                }\n                // append to dest\n                stacks[dest].insert(stacks[dest].end(), segment.begin(), segment.end());\n                // truncate src\n                stacks[src].resize(pos);\n                // record move operation (1-indexed destination)\n                ops.emplace_back(v, dest + 1);\n            }\n        }\n    }\n\n    // output\n    for (auto [v, i] : ops) {\n        cout << v << ' ' << i << '\\n';\n    }\n    return 0;\n}","ahc027":"#include <bits/stdc++.h>\nusing namespace std;\n\nint N;\nvector<string> h, v;                     // horizontal, vertical walls\nvector<vector<int>> d;                   // dirtiness, unused by the algorithm\nvector<vector<bool>> vis;\n\nstring ans;\nconst char dirChar[4] = {'R', 'D', 'L', 'U'};\nint dx[4] = {0, 1, 0, -1};\nint dy[4] = {1, 0, -1, 0};\n\nbool canMove(int x, int y, int dir) {\n    int nx = x + dx[dir];\n    int ny = y + dy[dir];\n    if (nx < 0 || nx >= N || ny < 0 || ny >= N) return false;\n    if (dir == 0) {            // Right\n        return v[x][y] == '0';\n    } else if (dir == 1) {     // Down\n        return h[x][y] == '0';\n    } else if (dir == 2) {     // Left\n        return v[x][y-1] == '0';\n    } else {                   // Up\n        return h[x-1][y] == '0';\n    }\n}\n\nvoid dfs(int x, int y) {\n    vis[x][y] = true;\n    for (int dir = 0; dir < 4; ++dir) {\n        if (!canMove(x, y, dir)) continue;\n        int nx = x + dx[dir];\n        int ny = y + dy[dir];\n        if (vis[nx][ny]) continue;\n        ans.push_back(dirChar[dir]);          // move to child\n        dfs(nx, ny);\n        ans.push_back(dirChar[(dir + 2) % 4]); // backtrack\n    }\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    if (!(cin >> N)) return 0;\n    h.resize(N-1);\n    for (int i = 0; i < N-1; ++i) cin >> h[i];\n    v.resize(N);\n    for (int i = 0; i < N; ++i) cin >> v[i];\n    d.assign(N, vector<int>(N));\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < N; ++j)\n            cin >> d[i][j];           // not used\n\n    vis.assign(N, vector<bool>(N, false));\n    dfs(0, 0);\n\n    cout << ans << '\\n';\n    return 0;\n}","ahc028":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    int N, M;\n    if (!(cin >> N >> M)) return 0;\n    int si, sj;\n    cin >> si >> sj;          // start position, not needed for this simple solver\n\n    vector<string> grid(N);\n    for (int i = 0; i < N; ++i) cin >> grid[i];\n\n    // positions for each letter \u2013 store the first found cell for each letter\n    array<pair<int,int>, 26> firstPos;\n    array<bool, 26> seen {};\n    for (int i = 0; i < N; ++i) {\n        for (int j = 0; j < N; ++j) {\n            char c = grid[i][j];\n            int id = c - 'A';\n            if (!seen[id]) {\n                firstPos[id] = {i, j};\n                seen[id] = true;\n            }\n        }\n    }\n\n    vector<string> t(M);\n    for (int k = 0; k < M; ++k) cin >> t[k];\n\n    // Output operations\n    for (const string &s : t) {\n        for (char ch : s) {\n            int id = ch - 'A';\n            auto [i, j] = firstPos[id];\n            cout << i << ' ' << j << '\\n';\n        }\n    }\n    return 0;\n}","ahc030":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    // Read everything, ignore it.\n    string s;\n    while (cin >> s) {\n        // Do nothing.\n    }\n    return 0;\n}","ahc031":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    int W, D, N;\n    if (!(cin >> W >> D >> N)) return 0;\n    // read the desired areas, they are irrelevant for this trivial solution\n    for (int d = 0; d < D; ++d) {\n        for (int k = 0; k < N; ++k) {\n            long long a; cin >> a;\n        }\n    }\n\n    // For every day, output the same set of N unit squares.\n    for (int d = 0; d < D; ++d) {\n        for (int k = 0; k < N; ++k) {\n            // coordinates: (k,0)-(k+1,1)\n            cout << k << \" \" << 0 << \" \" << k + 1 << \" \" << 1 << '\\n';\n        }\n    }\n    return 0;\n}","ahc032":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int N, M, K;\n    if (!(cin >> N >> M >> K)) return 0;\n    \n    // read a[N][N]\n    long long tmp;\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < N; ++j)\n            cin >> tmp;\n    \n    // read stamps: M stamps of 3x3 each\n    for (int m = 0; m < M; ++m)\n        for (int i = 0; i < 3; ++i)\n            for (int j = 0; j < 3; ++j)\n                cin >> tmp;\n    \n    // Output no operations\n    cout << 0 << '\\n';\n    return 0;\n}","ahc033":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int N;\n    if (!(cin >> N)) return 0;\n    // read the N\u00d7N matrix of container numbers (unused)\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < N; ++j) {\n            int x; cin >> x;\n        }\n\n    // output N lines each containing one dot\n    for (int i = 0; i < N; ++i) {\n        cout << \".\\n\";\n    }\n    return 0;\n}","ahc034":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    const int N = 20;\n    vector<vector<int>> h(N, vector<int>(N));\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < N; ++j)\n            cin >> h[i][j];\n\n    vector<pair<int,int>> pos, neg;\n    for (int i = 0; i < N; ++i) {\n        for (int j = 0; j < N; ++j) {\n            if (h[i][j] > 0) pos.emplace_back(i, j);\n            else if (h[i][j] < 0) neg.emplace_back(i, j);\n        }\n    }\n\n    vector<string> ops;\n    int cur_i = 0, cur_j = 0;\n    long long load = 0;           // current load in truck\n\n    auto moveTo = [&](int ti, int tj){\n        // horizontal first\n        while (cur_j < tj){ ops.push_back(\"R\"); ++cur_j; }\n        while (cur_j > tj){ ops.push_back(\"L\"); --cur_j; }\n        while (cur_i < ti){ ops.push_back(\"D\"); ++cur_i; }\n        while (cur_i > ti){ ops.push_back(\"U\"); --cur_i; }\n    };\n\n    // phase 1: collect soil from positive cells\n    for (auto [i, j] : pos) {\n        moveTo(i, j);\n        ops.emplace_back(\"+\" + to_string(h[i][j]));\n        load += h[i][j];\n    }\n\n    // phase 2: deposit onto negative cells\n    for (auto [i, j] : neg) {\n        moveTo(i, j);\n        ops.emplace_back(\"-\" + to_string(-h[i][j]));\n        load -= -h[i][j];\n    }\n\n    // output\n    for (auto &s : ops) cout << s << '\\n';\n    return 0;\n}","ahc035":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, T;\n    if (!(cin >> N >> M >> T)) return 0;\n\n    const int seedCnt = 2 * N * (N - 1);            // 60 for N = 6\n    vector<vector<int>> vec(seedCnt, vector<int>(M));\n\n    // read initial seeds (unused later)\n    for (int i = 0; i < seedCnt; ++i)\n        for (int j = 0; j < M; ++j) cin >> vec[i][j];\n\n    for (int turn = 0; turn < T; ++turn) {\n        // output the board\n        for (int i = 0; i < N; ++i) {\n            for (int j = 0; j < N; ++j) {\n                if (j) cout << ' ';\n                cout << i * N + j;            // place seed i*N + j\n            }\n            cout << '\\n';\n        }\n        cout.flush();                         // not necessary offline\n\n        // read the new seeds generated by the judge (discarded)\n        for (int i = 0; i < seedCnt; ++i)\n            for (int j = 0; j < M; ++j) cin >> vec[i][j];\n    }\n    return 0;\n}","ahc038":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int N, M, V;\n    if (!(cin >> N >> M >> V)) return 0;\n    string line;\n    for (int i = 0; i < N; ++i) { cin >> line; } // initial grid\n    for (int i = 0; i < N; ++i) { cin >> line; } // target grid\n\n    // Output a single-vertex tree (root only)\n    cout << 1 << '\\n';        // V' = 1\n    // No further lines for edges\n    cout << 0 << ' ' << 0 << '\\n';   // root at (0,0)\n    // No turns -> nothing else to print\n    return 0;\n}","ahc039":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    int N;\n    if (!(cin >> N)) return 0;\n    // read and ignore all 2N points\n    for (int i = 0; i < 2 * N; ++i) {\n        int x, y;\n        cin >> x >> y;\n    }\n    // output the rectangle\n    cout << 4 << '\\n';\n    cout << \"0 0\\n\";\n    cout << \"100000 0\\n\";\n    cout << \"100000 100000\\n\";\n    cout << \"0 100000\\n\";\n    return 0;\n}","ahc040":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, T, sigma;\n    if (!(cin >> N >> T >> sigma)) return 0;\n\n    // read the measured widths/heights of the rectangles (not used)\n    vector<long long> wp(N), hp(N);\n    for (int i = 0; i < N; ++i) cin >> wp[i] >> hp[i];\n\n    for (int turn = 0; turn < T; ++turn) {\n        // output empty plan\n        cout << 0 << '\\n';\n        cout.flush();                     // ensure judge receives it\n\n        long long Wp, Hp;\n        cin >> Wp >> Hp;                  // read feedback (ignored)\n    }\n    return 0;\n}","ahc041":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, H;\n    if (!(cin >> N >> M >> H)) return 0;          // read first line\n\n    // read beauty values\n    for (int i = 0; i < N; ++i) {\n        int dummy;\n        cin >> dummy;\n    }\n\n    // read edges\n    for (int i = 0; i < M; ++i) {\n        int u, v;\n        cin >> u >> v;\n    }\n\n    // read coordinates\n    for (int i = 0; i < N; ++i) {\n        int x, y;\n        cin >> x >> y;\n    }\n\n    // output all -1\n    for (int i = 0; i < N; ++i) {\n        if (i) cout << ' ';\n        cout << -1;\n    }\n    cout << '\\n';\n    return 0;\n}","ahc042":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    const int N = 20;\n    vector<string> board(N);\n    for (int i = 0; i < N; ++i) cin >> board[i];\n\n    vector<pair<char,int>> ops;          // resulting operation list\n\n    auto shiftRowLeft = [&](int r) {\n        // shift row r to the left\n        for (int j = 0; j < N-1; ++j) {\n            board[r][j] = board[r][j+1];\n }\n        board[r][N-1] = '.';\n    };\n\n    while (true) {\n        int ri = -1, cj = -1;\n        bool found = false;\n        for (int i = 0; i < N && !found; ++i) {\n            for (int j = 0; j < N && !found; ++j) {\n                if (board[i][j] == 'x') {\n                    ri = i; cj = j; found = true;\n                }\n            }\n        }\n        if (!found) break;          // no Oni left\n\n        // move the Oni to the left border and delete it\n        for (int t = 0; t <= cj; ++t) {\n            ops.emplace_back('L', ri);\n            shiftRowLeft(ri);\n        }\n    }\n\n    // output\n    for (auto &p : ops) {\n        cout << p.first << ' ' << p.second << '\\n';\n    }\n    // (No further output needed; problem statement expects only operations)\n    return 0;\n}","ahc044":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int N;\n    long long L;\n    if (!(cin >> N >> L)) return 0;\n    \n    vector<int> T(N);\n    for (int i = 0; i < N; ++i) cin >> T[i];   // values are read but unused\n    \n    for (int i = 0; i < N; ++i) {\n        int a = (i + 1) % N;\n        int b = a;\n        cout << a << ' ' << b << '\\n';\n    }\n    return 0;\n}","ahc045":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int N, M, Q, L, W;\n    if (!(cin >> N >> M >> Q >> L >> W)) return 0;\n    vector<int> G(M);\n    for (int i = 0; i < M; ++i) cin >> G[i];\n    // read and discard rectangle bounds\n    for (int i = 0; i < N; ++i) {\n        int lx, rx, ly, ry;\n        cin >> lx >> rx >> ly >> ry;\n    }\n\n    // build groups\n    vector<vector<int>> groups;\n    int idx = 0;\n    for (int i = 0; i < M; ++i) {\n        vector<int> grp;\n        for (int j = 0; j < G[i]; ++j) {\n            grp.push_back(idx++);\n        }\n        groups.push_back(move(grp));\n    }\n\n    // output answer\n    cout << \"!\\n\";\n    for (auto &grp : groups) {\n        // city list\n        for (size_t i = 0; i < grp.size(); ++i) {\n            if (i) cout << ' ';\n            cout << grp[i];\n        }\n        cout << '\\n';\n        // edges: consecutive cities\n        for (size_t i = 0; i + 1 < grp.size(); ++i) {\n            cout << grp[i] << ' ' << grp[i + 1] << '\\n';\n        }\n    }\n    return 0;\n}","ahc046":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Cell {\n    int r, c;\n};\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    int N, M;\n    if (!(cin >> N >> M)) return 0;\n    vector<Cell> pos(M);\n    for (int i = 0; i < M; ++i) cin >> pos[i].r >> pos[i].c;\n\n    const int dr[4] = {-1, 1, 0, 0};\n    const int dc[4] = {0, 0, -1, 1};\n    const char dirChar[4] = {'U', 'D', 'L', 'R'};\n\n    vector<pair<char,char>> output;           // action, direction\n\n    Cell cur = pos[0];\n    for (int k = 0; k < M-1 && (int)output.size() < 800; ++k) {\n        Cell to = pos[k+1];\n\n        // ---- blocked array: all future targets except the next one ----\n        bool blocked[20][20] = {false};\n        for (int t = k+2; t < M; ++t) {\n            blocked[pos[t].r][pos[t].c] = true;\n        }\n\n        // ---- BFS -------------------------------------------------------\n        int dist[20][20];\n        int pr[20][20], pc[20][20];\n        char pd[20][20];\n        memset(dist, -1, sizeof(dist));\n        queue<Cell> q;\n        dist[cur.r][cur.c] = 0;\n        q.push(cur);\n\n        while (!q.empty() && dist[to.r][to.c] == -1) {\n            Cell curc = q.front(); q.pop();\n            for (int d = 0; d < 4; ++d) {\n                int nr = curc.r + dr[d];\n                int nc = curc.c + dc[d];\n                if (nr < 0 || nr >= N || nc < 0 || nc >= N) continue;\n                if (blocked[nr][nc]) continue;\n                if (dist[nr][nc] != -1) continue;\n                dist[nr][nc] = dist[curc.r][curc.c] + 1;\n                pr[nr][nc] = curc.r;\n                pc[nr][nc] = curc.c;\n                pd[nr][nc] = dirChar[d];\n                q.push({nr, nc});\n            }\n        }\n\n        if (dist[to.r][to.c] == -1) { // unreachable \u2013 stop completely\n            break;\n        }\n\n        // ---- reconstruct path -----------------------------------------\n        vector<char> pathDirs;\n        int cr = to.r, cc = to.c;\n        while (!(cr == cur.r && cc == cur.c)) {\n            pathDirs.push_back(pd[cr][cc]);      // direction from predecessor to this cell\n            int tr = pr[cr][cc], tc = pc[cr][cc];\n            cr = tr; cc = tc;\n        }\n        reverse(pathDirs.begin(), pathDirs.end());\n\n        // ---- output actions --------------------------------------------\n        for (char d : pathDirs) {\n            if ((int)output.size() == 800) break;\n            output.emplace_back('M', d);\n        }\n\n        cur = to; // reached the next target\n    }\n\n    // ---- write result -----------------------------------------------\n    for (auto &act : output) {\n        cout << act.first << ' ' << act.second << '\\n';\n    }\n    return 0;\n}"},"2":{"ahc001":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Rect {\n    int a, b, c, d;          // [a,c) \u00d7 [b,d)\n};\n\ninline bool overlap(const Rect &r1, const Rect &r2) {\n    return !(r1.a >= r2.c || r1.c <= r2.a || r1.b >= r2.d || r1.d <= r2.b);\n}\n\nint clamp(int v, int lo, int hi) {\n    if (v < lo) return lo;\n    if (v > hi) return hi;\n    return v;\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    int n;\n    if (!(cin >> n)) return 0;\n    vector<int> xs(n), ys(n), rs(n);\n    for (int i = 0; i < n; ++i) {\n        cin >> xs[i] >> ys[i] >> rs[i];\n    }\n\n    /* order by descending r to put large rectangles first */\n    vector<int> order(n);\n    iota(order.begin(), order.end(), 0);\n    sort(order.begin(), order.end(),\n         [&](int i, int j){ return rs[i] > rs[j]; });\n\n    vector<Rect> placed;  // already placed rectangles\n    vector<Rect> answer(n);\n\n    const int DMAX = 150;   // search radius\n\n    for (int id : order) {\n        int x = xs[id], y = ys[id], r = rs[id];\n\n        /* --- choose width and height -------------------------------- */\n        int w = (int)ceil(sqrt((double)r));\n        if (w < 1) w = 1;\n        if (w > 10000) w = 10000;\n        int h = (r + w - 1) / w;\n        if (h < 1) h = 1;\n        if (h > 10000) h = 10000;\n\n        /* --- base position ------------------------------------------ */\n        int xmin = max(0, x + 1 - w);\n        int xmax = min(x, 10000 - w);\n        int a0 = clamp(x, xmin, xmax);\n\n        int ymin = max(0, y + 1 - h);\n        int ymax = min(y, 10000 - h);\n        int b0 = clamp(y, ymin, ymax);\n\n        Rect candidate{a0, b0, a0 + w, b0 + h};\n\n        bool placedNow = false;\n\n        auto try_position = [&](int a, int b, int w, int h)->bool{\n            Rect cand{a, b, a + w, b + h};\n            for (auto &pr : placed) {\n                if (overlap(cand, pr)) return false;\n            }\n            answer[id] = cand;\n            placed.push_back(cand);\n            return true;\n        };\n\n        /* --- first try base ---------------------------- */\n        if (try_position(candidate.a, candidate.b, w, h))\n            placedNow = true;\n\n        /* --- search around base ------------------------ */\n        if (!placedNow) {\n            for (int d = 0; d <= DMAX && !placedNow; ++d) {\n                for (int dx = -d; dx <= d && !placedNow; ++dx) {\n                    int dy = d - abs(dx);\n                    for (int sign : {1, -1}) {\n                        int nx = clamp(a0 + dx, 0, 10000 - w);\n                        int ny = clamp(b0 + dy * sign, 0, 10000 - h);\n                        if (try_position(nx, ny, w, h)) {\n                            placedNow = true;\n                            break;\n                        }\n                    }\n                }\n            }\n        }\n\n        /* --- fallback to 1\u00d71 ----------------------------------- */\n        if (!placedNow) {\n            int w1 = 1, h1 = 1;\n            int a1 = clamp(x, 0, 9999);\n            int b1 = clamp(y, 0, 9999);\n            // search for a free 1\u00d71 cell\n            bool ok = false;\n            for (int d = 0; d <= DMAX && !ok; ++d) {\n                for (int dx = -d; dx <= d && !ok; ++dx) {\n                    int dy = d - abs(dx);\n                    for (int sign : {1, -1}) {\n                        int nx = clamp(a1 + dx, 0, 9999);\n                        int ny = clamp(b1 + dy * sign, 0, 9999);\n                        if (try_position(nx, ny, w1, h1)) {\n                            ok = true;\n                            break;\n                        }\n                    }\n                }\n            }\n            if (!ok) {   // very unlikely, fallback to arbitrary free cell\n                for (int ax = 0; ax <= 10000 - w1 && !ok; ++ax) {\n                    for (int ay = 0; ay <= 10000 - h1 && !ok; ++ay) {\n                        if (try_position(ax, ay, w1, h1)) ok = true;\n                    }\n                }\n            }\n        }\n    }\n\n    /* --- output in the original order -------------------- */\n    for (int i = 0; i < n; ++i) {\n        auto &r = answer[i];\n        cout << r.a << ' ' << r.b << ' ' << r.c << ' ' << r.d << '\\n';\n    }\n    return 0;\n}","ahc002":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    int si, sj;\n    if (!(cin >> si >> sj)) return 0;\n\n    const int N = 50;\n    vector<vector<int>> t(N, vector<int>(N));\n    vector<vector<int>> p(N, vector<int>(N));\n\n    int maxTile = -1;\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < N; ++j) {\n            cin >> t[i][j];\n            maxTile = max(maxTile, t[i][j]);\n        }\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < N; ++j)\n            cin >> p[i][j];\n\n    int M = maxTile + 1;               // number of tiles\n\n    const int di[4] = {-1, 1, 0, 0};\n    const int dj[4] = {0, 0, -1, 1};\n    const char dir[4] = {'U', 'D', 'L', 'R'};\n\n    /* ---------- Greedy baseline ---------- */\n    vector<char> used(M, 0);\n    used[t[si][sj]] = 1;\n    string bestAnswer;\n    long long bestScore = p[si][sj];\n    int cur_i = si, cur_j = sj;\n\n    while (true) {\n        int bestVal  = -1, bestD = -1;\n        for (int d = 0; d < 4; ++d) {\n            int ni = cur_i + di[d], nj = cur_j + dj[d];\n            if (ni < 0 || ni >= N || nj < 0 || nj >= N) continue;\n            if (used[t[ni][nj]]) continue;\n            int val = p[ni][nj];\n            if (val > bestVal) { bestVal = val; bestD = d; }\n        }\n        if (bestD == -1) break;\n        bestAnswer.push_back(dir[bestD]);\n        cur_i += di[bestD];\n        cur_j += dj[bestD];\n        used[t[cur_i][cur_j]] = 1;\n        bestScore += p[cur_i][cur_j];\n    }\n\n    /* ---------- Random walks ---------- */\n    const int TIME_LIMIT_MS = 1800;          // 1.8 seconds\n    auto startTime = chrono::steady_clock::now();\n\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n\n    // temperature for weighting neighbours\n    const double TEMPERATURE = 30.0;\n\n    while (true) {\n        auto now = chrono::steady_clock::now();\n        if (chrono::duration_cast<chrono::milliseconds>(now - startTime)\n                .count() >= TIME_LIMIT_MS) break;\n\n        vector<char> usedTmp = used;          // reset to the greedy visited set\n        int curI = si, curJ = sj;\n        long long curScore = p[si][sj];\n        string curAnswer;\n\n        while (true) {\n            struct Cand { int d; int i; int j; int val; };\n            vector<Cand> cand;\n            for (int d = 0; d < 4; ++d) {\n                int ni = curI + di[d], nj = curJ + dj[d];\n                if (ni < 0 || ni >= N || nj < 0 || nj >= N) continue;\n                if (usedTmp[t[ni][nj]]) continue;\n                cand.push_back({d, ni, nj, p[ni][nj]});\n            }\n            if (cand.empty()) break;\n\n            // discrete distribution weighted by exp(val / TEMPERATURE)\n            vector<double> probs;\n            probs.reserve(cand.size());\n            double sum = 0.0;\n            for (auto &c : cand) {\n                double w = exp(c.val / TEMPERATURE);\n                probs.push_back(w);\n                sum += w;\n            }\n            uniform_real_distribution<double> urd(0, sum);\n            double r = urd(rng);\n            double acc = 0.0;\n            int chosen = 0;\n            for (size_t k = 0; k < cand.size(); ++k) {\n                acc += probs[k];\n                if (r <= acc) { chosen = k; break; }\n            }\n            auto c = cand[chosen];\n\n            curAnswer.push_back(dir[c.d]);\n            curI = c.i; curJ = c.j;\n            usedTmp[t[curI][curJ]] = 1;\n            curScore += p[curI][curJ];\n        }\n\n        if (curScore > bestScore) {\n            bestScore = curScore;\n            bestAnswer = curAnswer;\n        }\n    }\n\n    cout << bestAnswer << '\\n';\n    return 0;\n}","ahc003":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct EdgeInfo {\n    double sum = 0.0;   // cumulative estimated costs\n    int    cnt = 0;     // number of usages\n    double get() const { return cnt ? sum / cnt : 1.0; } // default 1.0\n};\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    const int N = 30;\n    const int V = N * N;\n    const int Q = 1000;\n\n    // edge tables\n    EdgeInfo h[ N ][ N - 1 ];   // horizontal edges (i, j)-(i, j+1)\n    EdgeInfo v[ N - 1 ][ N ];   // vertical   edges (i, j)-(i+1, j)\n\n    // helper lambdas\n    auto node_id = [N](int i, int j){ return i * N + j; };\n\n    for (int qi = 0; qi < Q; ++qi) {\n        int si, sj, ti, tj;\n        if (!(cin >> si >> sj >> ti >> tj)) return 0;\n\n        /* ---------- run Dijkstra with current estimates ---------- */\n        const double INF = 1e100;\n        vector<double> dist(V, INF);\n        vector<int>    prev(V, -1);\n        vector<char>   move(V, 0);   // 'U','D','L','R'\n\n        priority_queue<pair<double,int>, vector<pair<double,int>>, greater<>> pq;\n        int sid = node_id(si, sj);\n        int tid = node_id(ti, tj);\n        dist[sid] = 0.0;\n        pq.emplace(0.0, sid);\n\n        while (!pq.empty()) {\n            auto [d, u] = pq.top(); pq.pop();\n            if (d != dist[u]) continue;\n            if (u == tid) break;\n\n            int ui = u / N, uj = u % N;\n\n            // left\n            if (uj > 0) {\n                int vId = node_id(ui, uj-1);\n                double w = h[ui][uj-1].get();\n                if (dist[vId] > d + w) {\n                    dist[vId] = d + w;\n                    prev[vId] = u;\n                    move[vId] = 'L';\n                    pq.emplace(dist[vId], vId);\n                }\n            }\n            // right\n            if (uj < N-1) {\n                int vId = node_id(ui, uj+1);\n                double w = h[ui][uj].get();\n                if (dist[vId] > d + w) {\n                    dist[vId] = d + w;\n                    prev[vId] = u;\n                    move[vId] = 'R';\n                    pq.emplace(dist[vId], vId);\n                }\n            }\n            // up\n            if (ui > 0) {\n                int vId = node_id(ui-1, uj);\n                double w = v[ui-1][uj].get();\n                if (dist[vId] > d + w) {\n                    dist[vId] = d + w;\n                    prev[vId] = u;\n                    move[vId] = 'U';\n                    pq.emplace(dist[vId], vId);\n                }\n            }\n            // down\n            if (ui < N-1) {\n                int vId = node_id(ui+1, uj);\n                double w = v[ui][uj].get();\n                if (dist[vId] > d + w) {\n                    dist[vId] = d + w;\n                    prev[vId] = u;\n                    move[vId] = 'D';\n                    pq.emplace(dist[vId], vId);\n                }\n            }\n        }\n\n        /* ---------- reconstruct path ---------- */\n        string path;\n        int cur = tid;\n        while (cur != sid) {\n            path.push_back(move[cur]);\n            cur = prev[cur];\n        }\n        reverse(path.begin(), path.end());\n\n        /* ---------- output and flush ---------- */\n        cout << path << '\\n' << flush;\n\n        /* ---------- read judge's reply ---------- */\n        long long x;\n        if (!(cin >> x)) return 0;\n\n        /* ---------- update edge estimates ---------- */\n        int ci = si, cj = sj;\n        double cost_per_edge = static_cast<double>(x) / path.size();\n        for (char mv : path) {\n            if (mv == 'R') {\n                h[ci][cj].sum += cost_per_edge;\n                h[ci][cj].cnt += 1;\n                ++cj;\n            } else if (mv == 'L') {\n                h[ci][cj-1].sum += cost_per_edge;\n                h[ci][cj-1].cnt += 1;\n                --cj;\n            } else if (mv == 'D') {\n                v[ci][cj].sum += cost_per_edge;\n                v[ci][cj].cnt += 1;\n                ++ci;\n            } else if (mv == 'U') {\n                v[ci-1][cj].sum += cost_per_edge;\n                v[ci-1][cj].cnt += 1;\n                --ci;\n            }\n        }\n    }\n    return 0;\n}","ahc004":"#include <bits/stdc++.h>\nusing namespace std;\n\n// ------------------------------------------------------------------\n// Random matrix generator (letters A..H)\ninline char randomLetter(mt19937 &rng) {\n    return char('A' + (rng() % 8));\n}\n\n// ------------------------------------------------------------------\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int N, M;\n    if (!(cin >> N >> M)) return 0;\n    vector<string> S(M);\n    for (int i = 0; i < M; ++i) cin >> S[i];\n\n    // ---------- random search ----------\n    const int ITER = 2000;                 // tweak if needed\n    const int MAX_LEN = 12;                // given upper bound\n    const int N2 = N * 2;\n\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n\n    vector<string> bestMat(N, string(N, '.'));\n    int bestScore = -1;\n\n    for (int it = 0; it < ITER; ++it) {\n        // build a random matrix\n        vector<string> mat(N, string(N, 'A'));\n        for (int i = 0; i < N; ++i)\n            for (int j = 0; j < N; ++j)\n                mat[i][j] = randomLetter(rng);\n\n        // unordered_set for horizontal and vertical substrings\n        unordered_set<string> H, V;\n        H.reserve(5000);\n        V.reserve(5000);\n\n        // horizontal\n        for (int i = 0; i < N; ++i) {\n            string r2 = mat[i] + mat[i];                 // length 40\n            for (int len = 2; len <= MAX_LEN; ++len) {\n                for (int st = 0; st < N; ++st) {\n                    H.insert(r2.substr(st, len));\n                }\n            }\n        }\n\n        // vertical\n        string col; col.resize(N);\n        for (int j = 0; j < N; ++j) {\n            for (int i = 0; i < N; ++i) col[i] = mat[i][j];\n            string c2 = col + col;\n            for (int len = 2; len <= MAX_LEN; ++len) {\n                for (int st = 0; st < N; ++st) {\n                    V.insert(c2.substr(st, len));\n                }\n            }\n        }\n\n        // score\n        int score = 0;\n        for (const string &s : S) {\n            if (H.find(s) != H.end() || V.find(s) != V.end())\n                ++score;\n        }\n\n        if (score > bestScore) {\n            bestScore = score;\n            bestMat.swap(mat);\n        }\n    }\n\n    // ---------- output ----------\n    for (int i = 0; i < N; ++i)\n        cout << bestMat[i] << '\\n';\n    return 0;\n}","ahc005":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    int N, si, sj;\n    if (!(cin >> N >> si >> sj)) return 0;\n    vector<string> g(N);\n    for (int i = 0; i < N; ++i) cin >> g[i];\n\n    auto inb = [&](int x, int y) { return 0 <= x && x < N && 0 <= y && y < N; };\n\n    const char dirs[4] = {'U', 'D', 'L', 'R'};\n    const int dx[4] = {-1, 1, 0, 0};\n    const int dy[4] = {0, 0, -1, 1};\n\n    // map each road cell to an index\n    vector<vector<int>> idx(N, vector<int>(N, -1));\n    vector<pair<int,int>> pos;\n    int cnt = 0;\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < N; ++j)\n            if (g[i][j] != '#') {\n                idx[i][j] = cnt++;\n                pos.emplace_back(i, j);\n            }\n\n    if (cnt == 0) {               // no road - trivial output\n        cout << '\\n';\n        return 0;\n    }\n\n    // adjacency list\n    vector<vector<int>> adj(cnt);\n    for (int id = 0; id < cnt; ++id) {\n        auto [x, y] = pos[id];\n        for (int d = 0; d < 4; ++d) {\n            int nx = x + dx[d], ny = y + dy[d];\n            if (inb(nx, ny) && idx[nx][ny] != -1)\n                adj[id].push_back(idx[nx][ny]);\n        }\n    }\n\n    int start = idx[si][sj];\n    vector<char> ans;\n\n    // visited mask\n    vector<bool> visited(cnt, false);\n    visited[start] = true;\n    int cur = start;\n\n    // helper lambda: BFS from src, returns dist and parent\n    auto bfs = [&](int src, vector<int> &dist, vector<int> &parent) {\n        dist.assign(cnt, -1);\n        parent.assign(cnt, -1);\n        deque<int> q;\n        dist[src] = 0;\n        q.push_back(src);\n        while (!q.empty()) {\n            int u = q.front();\n            q.pop_front();\n            for (int v : adj[u]) {\n                if (dist[v] == -1) {\n                    dist[v] = dist[u] + 1;\n                    parent[v] = u;\n                    q.push_back(v);\n                }\n            }\n        }\n    };\n\n    // main loop: always go to the nearest unvisited node\n    while (true) {\n        int remaining = 0;\n        for (bool v : visited) if (!v) ++remaining;\n        if (remaining == 0) break;\n\n        vector<int> dist, parent;\n        bfs(cur, dist, parent);\n\n        int target = -1;\n        int best = INT_MAX;\n        for (int v = 0; v < cnt; ++v) {\n            if (!visited[v] && dist[v] != -1 && dist[v] < best) {\n                best = dist[v];\n                target = v;\n            }\n        }\n        if (target == -1) break; // should not happen\n\n        // reconstruct path from cur to target\n        vector<int> path;\n        int p = target;\n        while (p != -1) {\n            path.push_back(p);\n            if (p == cur) break;\n            p = parent[p];\n        }\n        reverse(path.begin(), path.end()); // from cur to target\n\n        // emit moves along the path\n        for (size_t i = 1; i < path.size(); ++i) {\n            auto [x1, y1] = pos[path[i-1]];\n            auto [x2, y2] = pos[path[i]];\n            int d = -1;\n            if (x2 == x1-1 && y2 == y1) d = 0;\n            else if (x2 == x1+1 && y2 == y1) d = 1;\n            else if (x2 == x1 && y2 == y1-1) d = 2;\n            else if (x2 == x1 && y2 == y1+1) d = 3;\n            if (d != -1) ans.push_back(dirs[d]);\n            visited[path[i]] = true;\n        }\n        cur = target;\n    }\n\n    // finally return to start\n    vector<int> dist, parent;\n    bfs(cur, dist, parent);\n    vector<int> path;\n    int p = start;\n    while (p != -1) {\n        path.push_back(p);\n        if (p == cur) break;\n        p = parent[p];\n    }\n    reverse(path.begin(), path.end()); // cur -> start\n\n    for (size_t i = 1; i < path.size(); ++i) {\n        auto [x1, y1] = pos[path[i-1]];\n        auto [x2, y2] = pos[path[i]];\n        int d = -1;\n        if (x2 == x1-1 && y2 == y1) d = 0;\n        else if (x2 == x1+1 && y2 == y1) d = 1;\n        else if (x2 == x1 && y2 == y1-1) d = 2;\n        else if (x2 == x1 && y2 == y1+1) d = 3;\n        if (d != -1) ans.push_back(dirs[d]);\n    }\n\n    string out(ans.begin(), ans.end());\n    cout << out << '\\n';\n    return 0;\n}","future-contest-2022-qual":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, K, R;\n    if (!(cin >> N >> M >> K >> R)) return 0;\n\n    // read task difficulties (not used)\n    vector<vector<int>> d(N, vector<int>(K));\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < K; ++j)\n            cin >> d[i][j];\n\n    // build graph\n    vector<vector<int>> dependents(N);\n    vector<int> indeg(N, 0);\n    for (int i = 0; i < R; ++i) {\n        int u, v;                // 1\u2011based in input\n        cin >> u >> v;\n        --u; --v;\n        dependents[u].push_back(v);\n        ++indeg[v];\n    }\n\n    queue<int> ready;\n    for (int i = 0; i < N; ++i)\n        if (indeg[i] == 0) ready.push(i);\n\n    vector<int> worker_task(M, -1);        // -1 = idle, else task index\n    vector<bool> started(N, false), done(N, false);\n\n    while (true) {\n        // ----- start new day -----\n        vector<pair<int, int>> assignments;\n        for (int w = 0; w < M; ++w) {\n            if (worker_task[w] != -1) continue;\n            if (ready.empty()) break;\n            int t = ready.front(); ready.pop();\n            worker_task[w] = t;\n            started[t] = true;\n            assignments.emplace_back(w, t);\n        }\n\n        // output assignments line\n        cout << assignments.size();\n        for (auto &p : assignments) {\n            cout << ' ' << (p.first + 1) << ' ' << (p.second + 1);\n        }\n        cout << '\\n';\n        cout.flush();\n\n        // ----- read finished workers -----\n        int n;\n        if (!(cin >> n)) return 0;          // just in case\n        if (n == -1) break;                // finished or 2000 days\n\n        vector<int> finished(n);\n        for (int i = 0; i < n; ++i) cin >> finished[i];\n\n        for (int f : finished) {\n            int w = f - 1;\n            int t = worker_task[w];\n            if (t == -1) continue;         // should not happen\n            // propagate completion\n            for (int v : dependents[t]) {\n                if (--indeg[v] == 0 && !started[v] && !done[v]) {\n                    ready.push(v);\n                }\n            }\n            done[t] = true;\n            worker_task[w] = -1;            // worker becomes idle\n        }\n    }\n    return 0;\n}","ahc006":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    const int N = 1000;\n    vector<int> a(N), b(N), c(N), d(N);\n    for (int i = 0; i < N; ++i) {\n        if (!(cin >> a[i] >> b[i] >> c[i] >> d[i])) return 0;\n    }\n\n    // Greedy selection of 50 orders\n    vector<bool> used(N, false);\n    vector<int> chosen;                 // 0\u2011based indices\n    vector<pair<int,int>> route;        // coordinates\n    route.emplace_back(400, 400);       // start at office\n\n    int curx = 400, cury = 400;\n    for (int step = 0; step < 50; ++step) {\n        int best = -1;\n        long long bestDist = LLONG_MAX;\n        for (int i = 0; i < N; ++i) if (!used[i]) {\n            long long dist = llabs(curx - a[i]) + llabs(cury - b[i]);\n            if (dist < bestDist) {\n                bestDist = dist;\n                best = i;\n            }\n        }\n        used[best] = true;\n        chosen.push_back(best);\n        route.emplace_back(a[best], b[best]);   // restaurant\n        route.emplace_back(c[best], d[best]);   // destination\n        curx = c[best];\n        cury = d[best];\n    }\n\n    route.emplace_back(400, 400);   // return to office\n\n    // Output\n    cout << (int)chosen.size();\n    for (int idx : chosen) cout << ' ' << (idx + 1); // 1-based\n    cout << '\\n';\n\n    cout << (int)route.size();\n    for (auto [x, y] : route) cout << ' ' << x << ' ' << y;\n    cout << '\\n';\n\n    return 0;\n}","ahc007":"#include <bits/stdc++.h>\nusing namespace std;\n\n// ---------- DSU ----------\nstruct DSU {\n    vector<int> p, r;\n    DSU(int n = 0) { init(n); }\n    void init(int n) {\n        p.resize(n);\n        r.assign(n, 0);\n        iota(p.begin(), p.end(), 0);\n    }\n    int find(int x) {\n        if (p[x] == x) return x;\n        return p[x] = find(p[x]);            // path compression\n    }\n    bool unite(int a, int b) {\n        a = find(a); b = find(b);\n        if (a == b) return false;\n        if (r[a] < r[b]) swap(a, b);\n        p[b] = a;\n        if (r[a] == r[b]) ++r[a];\n        return true;\n    }\n};\n\n// ---------- main ----------\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    const int N = 400, M = 1995;\n    // read coordinates, but we never use them\n    for (int i = 0; i < N; ++i) {\n        int x, y; cin >> x >> y;\n    }\n\n    vector<int> u(M), v(M);\n    for (int i = 0; i < M; ++i) {\n        cin >> u[i] >> v[i];\n    }\n\n    DSU dsu(N);\n\n    for (int i = 0; i < M; ++i) {\n        int li;            // the real length\n        cin >> li;\n\n        if (dsu.unite(u[i], v[i])) {\n            cout << 1 << '\\n';\n        } else {\n            cout << 0 << '\\n';\n        }\n        cout.flush();        // essential for interactive judge\n    }\n    return 0;\n}","ahc008":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N;\n    if (!(cin >> N)) return 0;               // no input\n    vector<tuple<int,int,int>> pets(N);\n    for (int i = 0; i < N; ++i) {\n        int x, y, t;\n        cin >> x >> y >> t;\n        pets[i] = {x, y, t};\n    }\n\n    int M;\n    cin >> M;\n    vector<pair<int,int>> humans(M);\n    for (int i = 0; i < M; ++i) {\n        int x, y;\n        cin >> x >> y;\n        humans[i] = {x, y};\n    }\n\n    // 300 turns\n    string action(M, '.');          // '.' * M\n\n    for (int turn = 0; turn < 300; ++turn) {\n        cout << action << \"\\n\";\n        cout.flush();              // important for interactive judge\n\n        // read N pet move strings (ignore)\n        for (int i = 0; i < N; ++i) {\n            string s;\n            cin >> s;              // each is '.' or directions\n        }\n    }\n\n    return 0;\n}","ahc009":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Node {\n    int i, j;\n};\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    int si, sj, ti, tj;\n    double p;\n    if (!(cin >> si >> sj >> ti >> tj >> p)) return 0;\n    vector<string> h(20);              // 20 lines of 19 chars\n    for (int i = 0; i < 20; ++i) cin >> h[i];\n    vector<string> v(19);              // 19 lines of 20 chars\n    for (int i = 0; i < 19; ++i) cin >> v[i];\n\n    const int N = 20;\n    // BFS structures\n    vector<vector<bool>> seen(N, vector<bool>(N, false));\n    vector<vector<int>> parenti(N, vector<int>(N, -1));\n    vector<vector<int>> parentj(N, vector<int>(N, -1));\n    vector<vector<char>> dir(N, vector<char>(N, 0));\n\n    queue<Node> q;\n    q.push({si, sj});\n    seen[si][sj] = true;\n    int di[4] = {-1, 1, 0, 0};\n    int dj[4] = {0, 0, -1, 1};\n    char dch[4] = {'U', 'D', 'L', 'R'};\n\n    while (!q.empty()) {\n        Node cur = q.front(); q.pop();\n        if (cur.i == ti && cur.j == tj) break;  // reached target\n        for (int k = 0; k < 4; ++k) {\n            int ni = cur.i + di[k];\n            int nj = cur.j + dj[k];\n            if (ni < 0 || ni >= N || nj < 0 || nj >= N) continue;\n            // check wall between (cur.i,cur.j) and (ni,nj)\n            bool blocked = false;\n            if (di[k] == -1) {               // Up\n                if (v[cur.i - 1][cur.j] == '1') blocked = true;\n            } else if (di[k] == 1) {         // Down\n                if (v[cur.i][cur.j] == '1') blocked = true;\n            } else if (dj[k] == -1) {        // Left\n                if (h[cur.i][cur.j - 1] == '1') blocked = true;\n            } else if (dj[k] == 1) {         // Right\n                if (h[cur.i][cur.j] == '1') blocked = true;\n            }\n            if (blocked) continue;\n            if (seen[ni][nj]) continue;\n            seen[ni][nj] = true;\n            parenti[ni][nj] = cur.i;\n            parentj[ni][nj] = cur.j;\n            dir[ni][nj] = dch[k];\n            q.push({ni, nj});\n        }\n    }\n\n    // Reconstruct path\n    string ans;\n    int ci = ti, cj = tj;\n    while (!(ci == si && cj == sj)) {\n        ans.push_back(dir[ci][cj]);\n        int pi = parenti[ci][cj];\n        int pj = parentj[ci][cj];\n        ci = pi; cj = pj;\n    }\n    reverse(ans.begin(), ans.end());\n    cout << ans << '\\n';\n    return 0;\n}","ahc010":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    // read and ignore the 30\u00d730 input grid\n    string line;\n    for (int i = 0; i < 30; ++i) {\n        if (!(cin >> line)) return 0;\n    }\n\n    // produce a deterministic rotation pattern\n    string ans;\n    ans.reserve(900);\n    for (int i = 0; i < 30; ++i) {\n        for (int j = 0; j < 30; ++j) {\n            int r = (i + j) % 4;          // 0 .. 3\n            ans.push_back(char('0' + r));\n        }\n    }\n\n    cout << ans << '\\n';\n    return 0;\n}","ahc011":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int N, T;\n    if (!(cin >> N >> T)) return 0;\n    string row;\n    // read and ignore the board\n    for (int i = 0; i < N; ++i) {\n        cin >> row;\n    }\n    // output an empty sequence\n    cout << '\\n';\n    return 0;\n}","ahc012":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int N, K;\n    if (!(cin >> N >> K)) return 0;\n    for (int i = 0; i < 10; ++i) int x; // read a_i\n    for (int i = 0; i < N; ++i) { int x, y; cin >> x >> y; }\n    \n    cout << 0 << '\\n';     // No cuts\n    return 0;\n}","ahc014":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    int N, M;\n    if (!(cin >> N >> M)) return 0;\n    vector<vector<bool>> has(N, vector<bool>(N, false));\n    vector<pair<int,int>> dots;\n    dots.reserve(M);\n    for (int i = 0; i < M; ++i) {\n        int x, y; cin >> x >> y;\n        has[x][y] = true;\n        dots.emplace_back(x, y);\n    }\n\n    int c = (N - 1) / 2;\n    auto weight = [&](int x, int y) {\n        long long dx = x - c;\n        long long dy = y - c;\n        return dx * dx + dy * dy + 1;\n    };\n\n    bool found = false;\n    long long bestW = -1;\n    int bx = -1, by = -1;     // coordinates of missing corner (p1)\n    int p2x = -1, p2y = -1, p3x = -1, p3y = -1, p4x = -1, p4y = -1;\n\n    for (auto [x, y] : dots) {\n        int x1 = x, y1 = y;\n        if (x1 + 1 >= N - 1) continue;  // ensure (x+1,y+1) inside\n        if (y1 + 1 >= N - 1) continue;\n\n        if (has[x1+1][y1] && has[x1][y1+1] && !has[x1+1][y1+1]) {\n            long long w = weight(x1+1, y1+1);\n            if (!found || w > bestW) {\n                found = true;\n                bestW = w;\n                bx = x1+1; by = y1+1;\n                // order: p1 (missing), p2, p3, p4\n                p2x = x1+1; p2y = y1;\n                p3x = x1;   p3y = y1;\n                p4x = x1;   p4y = y1+1;\n            }\n        }\n    }\n\n    if (!found) {\n        cout << 0 << '\\n';\n        return 0;\n    }\n\n    cout << 1 << '\\n';\n    cout << bx << ' ' << by << ' '\n         << p2x << ' ' << p2y << ' '\n         << p3x << ' ' << p3y << ' '\n         << p4x << ' ' << p4y << '\\n';\n    return 0;\n}","ahc015":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Pos { int r, c; };\n\nstatic inline void tilt(vector<vector<int>>& g, char dir) {\n    if (dir == 'F') {                 // up\n        for (int c = 0; c < 10; ++c) {\n            vector<int> v;\n            for (int r = 0; r < 10; ++r)\n                if (g[r][c]) v.push_back(g[r][c]);\n            for (int r = 0; r < 10; ++r)\n                g[r][c] = (r < (int)v.size() ? v[r] : 0);\n        }\n    } else if (dir == 'B') {          // down\n        for (int c = 0; c < 10; ++c) {\n            vector<int> v;\n            for (int r = 9; r >= 0; --r)\n                if (g[r][c]) v.push_back(g[r][c]);\n            for (int r = 9, idx = 0; r >= 0; --r, ++idx)\n                g[r][c] = (idx < (int)v.size() ? v[idx] : 0);\n        }\n    } else if (dir == 'L') {          // left\n        for (int r = 0; r < 10; ++r) {\n            vector<int> v;\n            for (int c = 0; c < 10; ++c)\n                if (g[r][c]) v.push_back(g[r][c]);\n            for (int c = 0; c < 10; ++c)\n                g[r][c] = (c < (int)v.size() ? v[c] : 0);\n        }\n    } else {                          // 'R'\n        for (int r = 0; r < 10; ++r) {\n            vector<int> v;\n            for (int c = 9; c >= 0; --c)\n                if (g[r][c]) v.push_back(g[r][c]);\n            for (int c = 9, idx = 0; c >= 0; --c, ++idx)\n                g[r][c] = (idx < (int)v.size() ? v[idx] : 0);\n        }\n    }\n}\n\nstatic inline long long connectivityScore(const vector<vector<int>>& g) {\n    bool vis[10][10] = {};\n    long long score = 0;\n    for (int r = 0; r < 10; ++r) for (int c = 0; c < 10; ++c) {\n        if (g[r][c] && !vis[r][c]) {\n            int flavour = g[r][c];\n            int cnt = 0;\n            queue<pair<int,int>> q;\n            q.push({r,c});\n            vis[r][c] = true;\n            while (!q.empty()) {\n                auto [x,y] = q.front(); q.pop();\n                ++cnt;\n                const int dr[4]={-1,1,0,0}, dc[4]={0,0,-1,1};\n                for (int k=0;k<4;++k){\n                    int nx=x+dr[k], ny=y+dc[k];\n                    if(nx>=0 && nx<10 && ny>=0 && ny<10 &&\n                       !vis[nx][ny] && g[nx][ny]==flavour){\n                        vis[nx][ny]=true;\n                        q.push({nx,ny});\n                    }\n                }\n            }\n            score += 1LL*cnt*cnt;\n        }\n    }\n    return score;\n}\n\nint main(){\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    vector<int> flavour(100);\n    for(int i=0;i<100;i++) cin>>flavour[i];\n\n    vector<vector<int>> board(10, vector<int>(10,0));\n    vector<Pos> empty;\n    empty.reserve(100);\n    for (int r=0;r<10;++r)\n        for (int c=0;c<10;++c)\n            empty.push_back({r,c});\n\n    const char dirs[4] = {'F','B','L','R'};\n    for(int t=0;t<100;++t){\n        int p; cin>>p;                     // 1\u2011based\n        Pos pos = empty[p-1];\n        board[pos.r][pos.c] = flavour[t];\n        empty.erase(empty.begin()+p-1);    // O(100) but 100*100 is negligible\n\n        // decide tilt\n        long long bestScore = -1;\n        char bestDir = 'F';\n        for(char d: dirs){\n            auto tmp = board;\n            tilt(tmp,d);\n            long long sc = connectivityScore(tmp);\n            if(sc > bestScore){\n                bestScore = sc;\n                bestDir   = d;\n            }\n        }\n\n        cout << bestDir << '\\n' << flush; // flush after each output\n    }\n    return 0;\n}","ahc016":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int M; double eps;\n    if (!(cin >> M >> eps)) return 0;\n\n    const int N = 20;\n    const int L = N * (N - 1) / 2;          // 190\n\n    cout << N << '\\n';\n    for (int k = 0; k < M; ++k) {\n        string s;\n        s.append(k, '1');\n        s.append(L - k, '0');\n        cout << s << '\\n';\n    }\n    cout.flush();\n\n    for (int q = 0; q < 100; ++q) {\n        string H;\n        cin >> H;\n        int ones = 0;\n        for (char c : H) if (c == '1') ++ones;\n        int ans = min(ones, M - 1);\n        cout << ans << '\\n';\n        cout.flush();\n    }\n    return 0;\n}","ahc017":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int N, M, D, K;\n    if (!(cin >> N >> M >> D >> K)) return 0;\n\n    // Skip edge data\n    for (int i = 0; i < M; ++i) {\n        int u, v, w;\n        cin >> u >> v >> w;\n    }\n    // Skip coordinates\n    for (int i = 0; i < N; ++i) {\n        int x, y;\n        cin >> x >> y;\n    }\n\n    vector<int> id(M);\n    iota(id.begin(), id.end(), 0);\n\n    // Fast random shuffle\n    static std::mt19937 rng(\n        std::chrono::steady_clock::now().time_since_epoch().count());\n    shuffle(id.begin(), id.end(), rng);\n\n    vector<int> day(M);\n    for (int j = 0; j < M; ++j) {\n        int idx = id[j];\n        day[idx] = (j % D) + 1;          // days are 1\u2011based\n    }\n\n    for (int i = 0; i < M; ++i) {\n        if (i) cout << ' ';\n        cout << day[i];\n    }\n    cout << '\\n';\n    return 0;\n}","ahc019":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Vec3 {int x, y, z;};\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int D; \n    if (!(cin >> D)) return 0;\n    vector<string> f1(D), r1(D), f2(D), r2(D);\n    for (int i = 0; i < D; ++i) cin >> f1[i];\n    for (int i = 0; i < D; ++i) cin >> r1[i];\n    for (int i = 0; i < D; ++i) cin >> f2[i];\n    for (int i = 0; i < D; ++i) cin >> r2[i];\n\n    auto idx = [D](int x,int y,int z){ return x*D*D + y*D + z; };\n\n    /* Boolean grids of cubes that exist in each object */\n    vector<vector<vector<char>>> cube1(D, vector<vector<char>>(D, vector<char>(D,0)));\n    vector<vector<vector<char>>> cube2(D, vector<vector<char>>(D, vector<char>(D,0)));\n\n    for (int z = 0; z < D; ++z)\n        for (int x = 0; x < D; ++x)\n            for (int y = 0; y < D; ++y) {\n                bool f1p = f1[z][x]=='1';\n                bool r1p = r1[z][y]=='1';\n                bool f2p = f2[z][x]=='1';\n                bool r2p = r2[z][y]=='1';\n                cube1[x][y][z] = f1p & r1p;\n                cube2[x][y][z] = f2p & r2p;\n            }\n\n    /* 3 grids: common, only1, only2 */\n    vector<vector<vector<char>>> common(D, vector<vector<char>>(D, vector<char>(D,0)));\n    vector<vector<vector<char>>> only1(D, vector<vector<char>>(D, vector<char>(D,0)));\n    vector<vector<vector<char>>> only2(D, vector<vector<char>>(D, vector<char>(D,0)));\n\n    for (int x = 0; x < D; ++x)\n        for (int y = 0; y < D; ++y)\n            for (int z = 0; z < D; ++z) {\n                bool c = cube1[x][y][z] & cube2[x][y][z];\n                common[x][y][z] = c;\n                only1[x][y][z] = cube1[x][y][z] & !c;\n                only2[x][y][z] = cube2[x][y][z] & !c;\n            }\n\n    const int dirs[6][3] = {{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};\n    auto flood = [&](vector<vector<vector<char>>>& grid)\n                 -> vector<vector<Vec3>> {\n        vector<vector<Vec3>> comps;\n        vector<vector<vector<char>>> vis(D, vector<vector<char>>(D, vector<char>(D,0)));\n        for (int x = 0; x < D; ++x)\n            for (int y = 0; y < D; ++y)\n                for (int z = 0; z < D; ++z)\n                    if (grid[x][y][z] && !vis[x][y][z]) {\n                        vector<Vec3> comp;\n                        queue<Vec3> qu;\n                        qu.push({x,y,z});\n                        vis[x][y][z] = 1;\n                        while (!qu.empty()) {\n                            auto [cx,cy,cz] = qu.front(); qu.pop();\n                            comp.push_back({cx,cy,cz});\n                            for (auto &d: dirs) {\n                                int nx=cx+d[0], ny=cy+d[1], nz=cz+d[2];\n                                if(nx>=0&&nx<D&&ny>=0&&ny<D&&nz>=0&&nz<D\n                                   &&grid[nx][ny][nz] && !vis[nx][ny][nz]){\n                                    vis[nx][ny][nz]=1;\n                                    qu.push({nx,ny,nz});\n                                }\n                            }\n                        }\n                        comps.push_back(std::move(comp));\n                    }\n        return comps;\n    };\n\n    auto commonComps = flood(common);\n    auto only1Comps   = flood(only1);\n    auto only2Comps   = flood(only2);\n\n    /* Prepare answer arrays */\n    vector<int> b1(D*D*D,0), b2(D*D*D,0);\n    int block_id = 1;\n\n    /* common components \u2013 used in both objects */\n    for (auto &comp : commonComps) {\n        for (auto &c : comp) {\n            b1[idx(c.x,c.y,c.z)] = block_id;\n            b2[idx(c.x,c.y,c.z)] = block_id;\n        }\n        ++block_id;\n    }\n    /* only1 components \u2013 used only in first object */\n    for (auto &comp : only1Comps) {\n        for (auto &c : comp) {\n            b1[idx(c.x,c.y,c.z)] = block_id;\n        }\n        ++block_id;\n    }\n    /* only2 components \u2013 used only in second object */\n    for (auto &comp : only2Comps) {\n        for (auto &c : comp) {\n            b2[idx(c.x,c.y,c.z)] = block_id;\n        }\n        ++block_id;\n    }\n    int n = block_id - 1;\n\n    /* Output */\n    cout << n << '\\n';\n    for (int i = 0; i < (int)b1.size(); ++i) {\n        if(i) cout << ' ';\n        cout << b1[i];\n    }\n    cout << '\\n';\n    for (int i = 0; i < (int)b2.size(); ++i) {\n        if(i) cout << ' ';\n        cout << b2[i];\n    }\n    cout << '\\n';\n    return 0;\n}","ahc020":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int N, M, K;\n    if (!(cin >> N >> M >> K)) return 0;\n    \n    vector<long long> x(N), y(N);\n    for (int i = 0; i < N; ++i) cin >> x[i] >> y[i];\n    \n    vector<long long> w(M);\n    long long sumW = 0;\n    for (int j = 0; j < M; ++j) {\n        int u, v;\n        long long weight;\n        cin >> u >> v >> weight;\n        --u; --v;\n        w[j] = weight;\n        sumW += weight;\n    }\n    \n    // For each station store the maximum squared distance to an assigned resident\n    vector<long long> maxDist2(N, 0);\n    \n    for (int k = 0; k < K; ++k) {\n        long long a, b;\n        cin >> a >> b;\n        int best = -1;\n        long long bestD2 = LLONG_MAX;\n        for (int i = 0; i < N; ++i) {\n            long long dx = a - x[i];\n            long long dy = b - y[i];\n            long long d2 = dx*dx + dy*dy;\n            if (d2 < bestD2) {\n                bestD2 = d2;\n                best = i;\n            }\n        }\n        if (best != -1) {\n            if (bestD2 > maxDist2[best]) maxDist2[best] = bestD2;\n        }\n    }\n    \n    vector<int> P(N, 0);\n    for (int i = 0; i < N; ++i) {\n        if (maxDist2[i] == 0) {\n            P[i] = 0;\n        } else {\n            double d = sqrt((double)maxDist2[i]);\n            int p = (int)ceil(d);\n            if (p > 5000) p = 5000;\n            P[i] = p;\n        }\n    }\n    \n    // Output P_i\n    for (int i = 0; i < N; ++i) {\n        if (i) cout << ' ';\n        cout << P[i];\n    }\n    cout << '\\n';\n    // Output B_j = 1 for all edges\n    for (int j = 0; j < M; ++j) {\n        if (j) cout << ' ';\n        cout << 1;\n    }\n    cout << '\\n';\n    \n    return 0;\n}","ahc021":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    const int N = 30;\n    const int S = N * (N + 1) / 2;          // 465\n    vector<int> a(S);\n    for (int i = 0; i < S; ++i) cin >> a[i];\n\n    // ---------- pre\u2011compute coordinates and row number ----------\n    vector<pair<int,int>> coord(S);\n    vector<int> row_of_idx(S);\n    for (int r = 0; r < N; ++r) {\n        int start = r * (r + 1) / 2;\n        for (int c = 0; c <= r; ++c) {\n            int idx = start + c;\n            coord[idx] = {r, c};\n            row_of_idx[idx] = r;\n        }\n    }\n\n    // ---------- record of swaps ----------\n    struct Op {int x1, y1, x2, y2;};\n    vector<Op> ops;\n\n    // ---------- heapify bottom\u2011up ----------\n    const int INTERNAL = S - N;          // indices 0 \u2026 434 are internal\n    for (int start = INTERNAL - 1; start >= 0; --start) {\n        int cur = start;\n        while (true) {\n            int r = row_of_idx[cur];\n            int left = cur + r + 1;\n            int right = cur + r + 2;\n            int smallest = cur;\n            if (left < S && a[left] < a[smallest]) smallest = left;\n            if (right < S && a[right] < a[smallest]) smallest = right;\n            if (smallest == cur) break;\n            swap(a[cur], a[smallest]);\n            ops.push_back({coord[cur].first, coord[cur].second,\n                           coord[smallest].first, coord[smallest].second});\n            cur = smallest;\n        }\n    }\n\n    // ---------- output ----------\n    cout << ops.size() << '\\n';\n    for (const auto &p : ops)\n        cout << p.x1 << ' ' << p.y1 << ' ' << p.x2 << ' ' << p.y2 << '\\n';\n    return 0;\n}","toyota2023summer-final":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Cell{int x,y;};\n\nint main(){\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int D, N;\n    if(!(cin>>D>>N)) return 0;          // read D (=9) and number of obstacles\n    \n    vector<vector<int>> obst(D, vector<int>(D,0));\n    for(int i=0;i<N;i++){\n        int r,c; cin>>r>>c;\n        obst[r][c]=1;\n    }\n    \n    // entrance position\n    const int ex = 0, ey = (D-1)/2;\n    obst[ex][ey] = 0; // ensure entrance is free\n    \n    // ---------- compute distance from entrance ----------\n    const int INF = 1e9;\n    vector<vector<int>> dist(D, vector<int>(D, INF));\n    queue<Cell> q;\n    dist[ex][ey] = 0;\n    q.push({ex,ey});\n    const int dx[4]={-1,1,0,0};\n    const int dy[4]={0,0,-1,1};\n    while(!q.empty()){\n        auto cur = q.front(); q.pop();\n        for(int dir=0;dir<4;dir++){\n            int nx = cur.x + dx[dir];\n            int ny = cur.y + dy[dir];\n            if(nx<0||nx>=D||ny<0||ny>=D) continue;\n            if(obst[nx][ny]) continue;\n            if(dist[nx][ny]!=INF) continue;\n            dist[nx][ny]=dist[cur.x][cur.y]+1;\n            q.push({nx,ny});\n        }\n    }\n    \n    // collect all free squares except entrance\n    vector<Cell> cells;\n    for(int i=0;i<D;i++)\n        for(int j=0;j<D;j++){\n            if(obst[i][j]) continue;\n            if(i==ex && j==ey) continue;\n            cells.push_back({i,j});\n        }\n    // sort by decreasing distance\n    sort(cells.begin(), cells.end(), [&](const Cell&a,const Cell&b){\n        return dist[a.x][a.y] > dist[b.x][b.y];\n    });\n    \n    int total = D*D - 1 - N;   // number of containers to be stored\n    int idx = 0;\n    \n    // ---------- placement phase ----------\n    for(int d=0; d<total; ++d){\n        int t; cin>>t;                // receive container number (unused)\n        auto c = cells[idx++];\n        cout<<c.x<<\" \"<<c.y<<\"\\n\"<<flush;   // output and flush immediately\n    }\n    \n    // ---------- removal phase ----------\n    // BFS from entrance, output order of non\u2011entrance visited cells\n    vector<vector<int>> visited(D, vector<int>(D,0));\n    queue<Cell> q2;\n    visited[ex][ey]=1;\n    q2.push({ex,ey});\n    vector<Cell> removal;\n    while(!q2.empty()){\n        auto cur=q2.front(); q2.pop();\n        for(int dir=0;dir<4;dir++){\n            int nx=cur.x+dx[dir];\n            int ny=cur.y+dy[dir];\n            if(nx<0||nx>=D||ny<0||ny>=D) continue;\n            if(obst[nx][ny]) continue;\n            if(visited[nx][ny]) continue;\n            visited[nx][ny]=1;\n            q2.push({nx,ny});\n            removal.push_back({nx,ny});\n        }\n    }\n    for(auto &c: removal){\n        cout<<c.x<<\" \"<<c.y<<\"\\n\";\n    }\n    return 0;\n}","ahc024":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int n, m;\n    if (!(cin >> n >> m)) return 0;  // just in case\n    \n    for (int i = 0; i < n; ++i) {\n        for (int j = 0; j < n; ++j) {\n            int v;  cin >> v;\n            cout << v;\n            if (j + 1 < n) cout << ' ';\n        }\n        cout << '\\n';\n    }\n    return 0;\n}","ahc025":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, D, Q;\n    if (!(cin >> N >> D >> Q)) return 0;\n\n    // Perform Q trivial queries\n    for (int q = 0; q < Q; ++q) {\n        int l = q % N;\n        int r = (q + 1) % N;          // ensures r != l\n        cout << 1 << ' ' << 1 << ' ' << l << ' ' << r << '\\n';\n        cout.flush();\n\n        // read judge answer and ignore\n        char ans;\n        if (!(cin >> ans)) return 0;   // safety: if input ends unexpectedly\n    }\n\n    // Output a simple final division\n    for (int i = 0; i < N; ++i) {\n        if (i) cout << ' ';\n        cout << (i % D);\n    }\n    cout << '\\n';\n    return 0;\n}","ahc026":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    const int n = 200;   // fixed in problem statement\n    const int m = 10;    // fixed in problem statement\n\n    vector<vector<int>> stacks(m);\n\n    for (int i = 0; i < m; ++i) {\n        stacks[i].resize(n / m);\n        for (int j = 0; j < n / m; ++j) {\n            cin >> stacks[i][j];          // bottom -> top\n        }\n    }\n\n    vector<pair<int,int>> ops;          // store (v , i)\n    vector<bool> removed(n + 1, false);\n\n    for (int v = 1; v <= n; ++v) {\n        while (true) {\n            int src = -1;          // stack index where v is\n            int pos = -1;          // position from bottom\n            for (int i = 0; i < m; ++i) {\n                for (int j = 0; j < (int)stacks[i].size(); ++j) {\n                    if (stacks[i][j] == v) {\n                        src = i;\n                        pos = j;\n                        break;\n                    }\n                }\n                if (src != -1) break;\n            }\n            if (src == -1) {            // already removed, should not happen\n                break;\n            }\n\n            if (pos == (int)stacks[src].size() - 1) {\n                // v is on top -> take it out\n                ops.emplace_back(v, 0);\n                stacks[src].pop_back();\n                removed[v] = true;\n                break;\n            } else {\n                // choose destination stack with minimal height (different stack)\n                int dest = -1, best = INT_MAX;\n                for (int i = 0; i < m; ++i) {\n                    if (i == src) continue;\n                    if ((int)stacks[i].size() < best) {\n                        best = stacks[i].size();\n                        dest = i;\n                    }\n                }\n                // move segment [pos .. end] from src to dest\n                vector<int> segment;\n                for (int idx = pos; idx < (int)stacks[src].size(); ++idx) {\n                    segment.push_back(stacks[src][idx]);\n                }\n                // append to dest\n                stacks[dest].insert(stacks[dest].end(), segment.begin(), segment.end());\n                // truncate src\n                stacks[src].resize(pos);\n                // record move operation (1-indexed destination)\n                ops.emplace_back(v, dest + 1);\n            }\n        }\n    }\n\n    // output\n    for (auto [v, i] : ops) {\n        cout << v << ' ' << i << '\\n';\n    }\n    return 0;\n}","ahc027":"#include <bits/stdc++.h>\nusing namespace std;\n\nint N;\nvector<string> h, v;                     // horizontal, vertical walls\nvector<vector<int>> d;                   // dirtiness, unused by the algorithm\nvector<vector<bool>> vis;\n\nstring ans;\nconst char dirChar[4] = {'R', 'D', 'L', 'U'};\nint dx[4] = {0, 1, 0, -1};\nint dy[4] = {1, 0, -1, 0};\n\nbool canMove(int x, int y, int dir) {\n    int nx = x + dx[dir];\n    int ny = y + dy[dir];\n    if (nx < 0 || nx >= N || ny < 0 || ny >= N) return false;\n    if (dir == 0) {            // Right\n        return v[x][y] == '0';\n    } else if (dir == 1) {     // Down\n        return h[x][y] == '0';\n    } else if (dir == 2) {     // Left\n        return v[x][y-1] == '0';\n    } else {                   // Up\n        return h[x-1][y] == '0';\n    }\n}\n\nvoid dfs(int x, int y) {\n    vis[x][y] = true;\n    for (int dir = 0; dir < 4; ++dir) {\n        if (!canMove(x, y, dir)) continue;\n        int nx = x + dx[dir];\n        int ny = y + dy[dir];\n        if (vis[nx][ny]) continue;\n        ans.push_back(dirChar[dir]);          // move to child\n        dfs(nx, ny);\n        ans.push_back(dirChar[(dir + 2) % 4]); // backtrack\n    }\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    if (!(cin >> N)) return 0;\n    h.resize(N-1);\n    for (int i = 0; i < N-1; ++i) cin >> h[i];\n    v.resize(N);\n    for (int i = 0; i < N; ++i) cin >> v[i];\n    d.assign(N, vector<int>(N));\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < N; ++j)\n            cin >> d[i][j];           // not used\n\n    vis.assign(N, vector<bool>(N, false));\n    dfs(0, 0);\n\n    cout << ans << '\\n';\n    return 0;\n}","ahc028":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    int N, M;\n    if (!(cin >> N >> M)) return 0;\n    int si, sj;\n    cin >> si >> sj;\n    vector<string> grid(N);\n    for (int i = 0; i < N; ++i) cin >> grid[i];\n\n    const int SZ = N * N;\n    auto id = [N](int i, int j){ return i * N + j; };\n    vector<int> id2i(SZ), id2j(SZ);\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < N; ++j) {\n            int k = id(i, j);\n            id2i[k] = i;\n            id2j[k] = j;\n        }\n\n    /* positions of every letter */\n    vector<vector<int>> pos(26);\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < N; ++j)\n            pos[grid[i][j]-'A'].push_back(id(i, j));\n\n    /* precompute nearest cell for every starting cell and letter */\n    vector<array<int,26>> nearest(SZ);\n    vector<array<int,26>> nearestDist(SZ);\n    for (int s = 0; s < SZ; ++s) {\n        int siCur = id2i[s], sjCur = id2j[s];\n        for (int c = 0; c < 26; ++c) {\n            int bestDist = 1e9, bestId = -1;\n            for (int t : pos[c]) {\n                int ti = id2i[t], tj = id2j[t];\n                int d = abs(siCur - ti) + abs(sjCur - tj);\n                if (d < bestDist) {\n                    bestDist = d;\n                    bestId = t;\n                }\n            }\n            nearest[s][c] = bestId;\n            nearestDist[s][c] = bestDist;\n        }\n    }\n\n    vector<string> rem(M);\n    for (int i = 0; i < M; ++i) cin >> rem[i];\n\n    vector<pair<int,int>> answer; // list of moves\n\n    int currId = id(si, sj);\n\n    while (!rem.empty()) {\n        long long bestTotal = LLONG_MAX;\n        int bestBlock = -1;\n        int bestStartId = -1;\n        vector<int> bestPath; // ids of the whole block\n\n        for (int b = 0; b < (int)rem.size(); ++b) {\n            const string &t = rem[b];\n            int first = t[0] - 'A';\n            for (int startId : pos[first]) {\n                long long internal = 0;\n                vector<int> path;\n                path.push_back(startId);\n                int cur = startId;\n                bool ok = true;\n                for (int k = 1; k < 5; ++k) {\n                    int need = t[k] - 'A';\n                    int nxt = nearest[cur][need];\n                    if (nxt == -1) {ok=false;break;}\n                    internal += nearestDist[cur][need] + 1;\n                    cur = nxt;\n                    path.push_back(cur);\n                }\n                if (!ok) continue;\n                long long moveFromCurr = nearestDist[currId][first] + 1;\n                long long total = moveFromCurr + internal;\n                if (total < bestTotal) {\n                    bestTotal = total;\n                    bestBlock = b;\n                    bestStartId = startId;\n                    bestPath = path;\n                }\n            }\n        }\n\n        // Output the best block\n        for (int idv : bestPath) {\n            answer.emplace_back(id2i[idv], id2j[idv]);\n        }\n        currId = bestPath.back();\n        rem.erase(rem.begin() + bestBlock);\n    }\n\n    // Print answer\n    for (auto [i, j] : answer) {\n        cout << i << ' ' << j << '\\n';\n    }\n    return 0;\n}","ahc030":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    // Read everything, ignore it.\n    string s;\n    while (cin >> s) {\n        // Do nothing.\n    }\n    return 0;\n}","ahc031":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    int W, D, N;\n    if (!(cin >> W >> D >> N)) return 0;\n    // read the desired areas, they are irrelevant for this trivial solution\n    for (int d = 0; d < D; ++d) {\n        for (int k = 0; k < N; ++k) {\n            long long a; cin >> a;\n        }\n    }\n\n    // For every day, output the same set of N unit squares.\n    for (int d = 0; d < D; ++d) {\n        for (int k = 0; k < N; ++k) {\n            // coordinates: (k,0)-(k+1,1)\n            cout << k << \" \" << 0 << \" \" << k + 1 << \" \" << 1 << '\\n';\n        }\n    }\n    return 0;\n}","ahc032":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int N, M, K;\n    if (!(cin >> N >> M >> K)) return 0;\n    \n    // read a[N][N]\n    long long tmp;\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < N; ++j)\n            cin >> tmp;\n    \n    // read stamps: M stamps of 3x3 each\n    for (int m = 0; m < M; ++m)\n        for (int i = 0; i < 3; ++i)\n            for (int j = 0; j < 3; ++j)\n                cin >> tmp;\n    \n    // Output no operations\n    cout << 0 << '\\n';\n    return 0;\n}","ahc033":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int N;\n    if (!(cin >> N)) return 0;\n    // read the N\u00d7N matrix of container numbers (unused)\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < N; ++j) {\n            int x; cin >> x;\n        }\n\n    // output N lines each containing one dot\n    for (int i = 0; i < N; ++i) {\n        cout << \".\\n\";\n    }\n    return 0;\n}","ahc034":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    const int N = 20;\n    vector<vector<int>> h(N, vector<int>(N));\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < N; ++j)\n            cin >> h[i][j];\n\n    vector<pair<int,int>> pos, neg;\n    for (int i = 0; i < N; ++i) {\n        for (int j = 0; j < N; ++j) {\n            if (h[i][j] > 0) pos.emplace_back(i, j);\n            else if (h[i][j] < 0) neg.emplace_back(i, j);\n        }\n    }\n\n    vector<string> ops;\n    int cur_i = 0, cur_j = 0;\n    long long load = 0;           // current load in truck\n\n    auto moveTo = [&](int ti, int tj){\n        // horizontal first\n        while (cur_j < tj){ ops.push_back(\"R\"); ++cur_j; }\n        while (cur_j > tj){ ops.push_back(\"L\"); --cur_j; }\n        while (cur_i < ti){ ops.push_back(\"D\"); ++cur_i; }\n        while (cur_i > ti){ ops.push_back(\"U\"); --cur_i; }\n    };\n\n    // phase 1: collect soil from positive cells\n    for (auto [i, j] : pos) {\n        moveTo(i, j);\n        ops.emplace_back(\"+\" + to_string(h[i][j]));\n        load += h[i][j];\n    }\n\n    // phase 2: deposit onto negative cells\n    for (auto [i, j] : neg) {\n        moveTo(i, j);\n        ops.emplace_back(\"-\" + to_string(-h[i][j]));\n        load -= -h[i][j];\n    }\n\n    // output\n    for (auto &s : ops) cout << s << '\\n';\n    return 0;\n}","ahc035":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, T;\n    if (!(cin >> N >> M >> T)) return 0;\n\n    const int seedCnt = 2 * N * (N - 1);          // 60\n    vector<vector<int>> seeds(seedCnt, vector<int>(M));\n\n    /* read the initial 60 seeds */\n    for (int i = 0; i < seedCnt; ++i)\n        for (int j = 0; j < M; ++j)\n            cin >> seeds[i][j];\n\n    for (int turn = 0; turn < T; ++turn) {\n        /* 1. choose 36 seeds with the largest total value */\n        vector<pair<int, int>> scoreIdx;  // (sum, index)\n        scoreIdx.reserve(seedCnt);\n        for (int i = 0; i < seedCnt; ++i) {\n            int s = 0;\n            for (int j = 0; j < M; ++j) s += seeds[i][j];\n            scoreIdx.push_back({s, i});\n        }\n        sort(scoreIdx.rbegin(), scoreIdx.rend());\n        vector<int> best;\n        best.reserve(N * N);\n        for (int k = 0; k < N * N; ++k) best.push_back(scoreIdx[k].second);\n\n        /* 2. output the board (row\u2011major) */\n        for (int i = 0; i < N; ++i) {\n            for (int j = 0; j < N; ++j) {\n                if (j) cout << ' ';\n                cout << best[i * N + j];\n            }\n            cout << '\\n';\n        }\n        cout.flush();\n\n        /* 3. read the next 60 seeds */\n        for (int i = 0; i < seedCnt; ++i)\n            for (int j = 0; j < M; ++j)\n                cin >> seeds[i][j];\n    }\n    return 0;\n}","ahc038":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int N, M, V;\n    if (!(cin >> N >> M >> V)) return 0;\n    string line;\n    for (int i = 0; i < N; ++i) { cin >> line; } // initial grid\n    for (int i = 0; i < N; ++i) { cin >> line; } // target grid\n\n    // Output a single-vertex tree (root only)\n    cout << 1 << '\\n';        // V' = 1\n    // No further lines for edges\n    cout << 0 << ' ' << 0 << '\\n';   // root at (0,0)\n    // No turns -> nothing else to print\n    return 0;\n}","ahc039":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    int N;\n    if (!(cin >> N)) return 0;\n    vector<pair<int,int>> mackerels(N);\n    for (int i = 0; i < N; ++i) {\n        cin >> mackerels[i].first >> mackerels[i].second;\n    }\n    unordered_set<long long> sardine;\n    sardine.reserve(N * 2);\n    for (int i = 0; i < N; ++i) {\n        int x, y;\n        cin >> x >> y;\n        long long key = ((long long)x << 32) | (unsigned int)y;\n        sardine.insert(key);\n    }\n\n    auto encode = [](int x, int y)->long long {\n        return ((long long)x << 32) | (unsigned int)y;\n    };\n\n    // try to find a mackerel for which a 1x1 square contains no sardine\n    bool found = false;\n    int bx, by; // bottom-left corner\n    for (auto [x, y] : mackerels) {\n        // choose a corner that stays inside [0,100000]\n        int lx = x, rx = x, ly = y, uy = y;\n        if (rx == 100000) rx = 99999; else rx = x + 1;\n        if (rx < 0) rx = 0;\n        if (lx == 0) lx = 1; else lx = x - 1;\n        if (ly == 0) ly = 1; else ly = y - 1;\n        if (uy == 100000) uy = 99999; else uy = y + 1;\n        if (uy < 0) uy = 0;\n\n        // check the four integer points inside or on the boundary of\n        // the 1x1 square with bottom-left (lx,ly)\n        vector<pair<int,int>> pts = {\n            {lx, ly},\n            {rx, ly},\n            {rx, uy},\n            {lx, uy}\n        };\n        bool bad = false;\n        for (auto [px, py] : pts) {\n            if (sardine.find(encode(px, py)) != sardine.end()) {\n                bad = true;\n                break;\n            }\n        }\n        if (!bad) {\n            bx = lx;\n            by = ly;\n            found = true;\n            break;\n        }\n    }\n\n    if (!found) {\n        // fallback: use full bounding rectangle\n        cout << 4 << \"\\n\";\n        cout << \"0 0\\n\";\n        cout << \"100000 0\\n\";\n        cout << \"100000 100000\\n\";\n        cout << \"0 100000\\n\";\n        return 0;\n    }\n\n    // output the 1x1 rectangle\n    cout << 4 << \"\\n\";\n    cout << bx << \" \" << by << \"\\n\";\n    cout << bx+1 << \" \" << by << \"\\n\";\n    cout << bx+1 << \" \" << by+1 << \"\\n\";\n    cout << bx << \" \" << by+1 << \"\\n\";\n    return 0;\n}","ahc040":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int N, T;\n    int sigma;                       // sigma is not used\n    if (!(cin >> N >> T >> sigma)) return 0;\n    \n    // read the noisy measurements of the rectangles \u2013 not needed\n    long long wprime, hprime;\n    for (int i = 0; i < N; ++i) {\n        cin >> wprime >> hprime;\n    }\n    \n    // For every turn\n    for (int turn = 0; turn < T; ++turn) {\n        // 1. output the placement plan\n        cout << N << '\\n';\n        // first rectangle\n        cout << 0 << ' ' << 0 << ' ' << 'U' << ' ' << -1 << '\\n';\n        // remaining rectangles\n        for (int i = 1; i < N; ++i) {\n            cout << i << ' ' << 0 << ' ' << 'L' << ' ' << (i - 1) << '\\n';\n        }\n        cout.flush();   // required after each turn\n        \n        // 2. read judge's feedback (two integers)\n        long long Wp, Hp;\n        cin >> Wp >> Hp;   // ignore them\n    }\n    return 0;\n}","ahc041":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, H;\n    if (!(cin >> N >> M >> H)) return 0;\n\n    vector<int> A(N);\n    for (int i = 0; i < N; ++i) cin >> A[i];\n\n    vector<vector<int>> adj(N);\n    for (int i = 0; i < M; ++i) {\n        int u, v;\n        cin >> u >> v;\n        adj[u].push_back(v);\n        adj[v].push_back(u);\n    }\n\n    // coordinates are not used in this simple heuristic\n    for (int i = 0; i < N; ++i) {\n        int x, y;\n        cin >> x >> y;\n    }\n\n    // choose root with maximum sum of neighbour beauties\n    int bestRoot = 0;\n    long long bestSum = -1;\n    for (int v = 0; v < N; ++v) {\n        long long sum = 0;\n        for (int u : adj[v]) sum += A[u];\n        if (sum > bestSum) {\n            bestSum = sum;\n            bestRoot = v;\n        }\n    }\n\n    vector<int> parent(N, -1);\n    for (int u : adj[bestRoot]) parent[u] = bestRoot;\n\n    for (int i = 0; i < N; ++i) {\n        if (i) cout << ' ';\n        cout << parent[i];\n    }\n    cout << '\\n';\n    return 0;\n}","ahc042":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    const int N = 20;\n    vector<string> board(N);\n    for (int i = 0; i < N; ++i) cin >> board[i];\n\n    vector<pair<char,int>> ops;          // resulting operation list\n\n    auto shiftRowLeft = [&](int r) {\n        // shift row r to the left\n        for (int j = 0; j < N-1; ++j) {\n            board[r][j] = board[r][j+1];\n }\n        board[r][N-1] = '.';\n    };\n\n    while (true) {\n        int ri = -1, cj = -1;\n        bool found = false;\n        for (int i = 0; i < N && !found; ++i) {\n            for (int j = 0; j < N && !found; ++j) {\n                if (board[i][j] == 'x') {\n                    ri = i; cj = j; found = true;\n                }\n            }\n        }\n        if (!found) break;          // no Oni left\n\n        // move the Oni to the left border and delete it\n        for (int t = 0; t <= cj; ++t) {\n            ops.emplace_back('L', ri);\n            shiftRowLeft(ri);\n        }\n    }\n\n    // output\n    for (auto &p : ops) {\n        cout << p.first << ' ' << p.second << '\\n';\n    }\n    // (No further output needed; problem statement expects only operations)\n    return 0;\n}","ahc044":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int N;\n    long long L;\n    if (!(cin >> N >> L)) return 0;\n    \n    vector<int> T(N);\n    for (int i = 0; i < N; ++i) cin >> T[i];   // values are read but unused\n    \n    for (int i = 0; i < N; ++i) {\n        int a = (i + 1) % N;\n        int b = a;\n        cout << a << ' ' << b << '\\n';\n    }\n    return 0;\n}","ahc045":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int N, M, Q, L, W;\n    if (!(cin >> N >> M >> Q >> L >> W)) return 0;\n    vector<int> G(M);\n    for (int i = 0; i < M; ++i) cin >> G[i];\n    // read and discard rectangle bounds\n    for (int i = 0; i < N; ++i) {\n        int lx, rx, ly, ry;\n        cin >> lx >> rx >> ly >> ry;\n    }\n\n    // build groups\n    vector<vector<int>> groups;\n    int idx = 0;\n    for (int i = 0; i < M; ++i) {\n        vector<int> grp;\n        for (int j = 0; j < G[i]; ++j) {\n            grp.push_back(idx++);\n        }\n        groups.push_back(move(grp));\n    }\n\n    // output answer\n    cout << \"!\\n\";\n    for (auto &grp : groups) {\n        // city list\n        for (size_t i = 0; i < grp.size(); ++i) {\n            if (i) cout << ' ';\n            cout << grp[i];\n        }\n        cout << '\\n';\n        // edges: consecutive cities\n        for (size_t i = 0; i + 1 < grp.size(); ++i) {\n            cout << grp[i] << ' ' << grp[i + 1] << '\\n';\n        }\n    }\n    return 0;\n}","ahc046":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Cell {\n    int r, c;\n};\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    int N, M;\n    if (!(cin >> N >> M)) return 0;\n    vector<Cell> pos(M);\n    for (int i = 0; i < M; ++i) cin >> pos[i].r >> pos[i].c;\n\n    const int dr[4] = {-1, 1, 0, 0};\n    const int dc[4] = {0, 0, -1, 1};\n    const char dirChar[4] = {'U', 'D', 'L', 'R'};\n\n    vector<pair<char,char>> output;           // action, direction\n\n    Cell cur = pos[0];\n    for (int k = 0; k < M-1 && (int)output.size() < 800; ++k) {\n        Cell to = pos[k+1];\n\n        // ---- blocked array: all future targets except the next one ----\n        bool blocked[20][20] = {false};\n        for (int t = k+2; t < M; ++t) {\n            blocked[pos[t].r][pos[t].c] = true;\n        }\n\n        // ---- BFS -------------------------------------------------------\n        int dist[20][20];\n        int pr[20][20], pc[20][20];\n        char pd[20][20];\n        memset(dist, -1, sizeof(dist));\n        queue<Cell> q;\n        dist[cur.r][cur.c] = 0;\n        q.push(cur);\n\n        while (!q.empty() && dist[to.r][to.c] == -1) {\n            Cell curc = q.front(); q.pop();\n            for (int d = 0; d < 4; ++d) {\n                int nr = curc.r + dr[d];\n                int nc = curc.c + dc[d];\n                if (nr < 0 || nr >= N || nc < 0 || nc >= N) continue;\n                if (blocked[nr][nc]) continue;\n                if (dist[nr][nc] != -1) continue;\n                dist[nr][nc] = dist[curc.r][curc.c] + 1;\n                pr[nr][nc] = curc.r;\n                pc[nr][nc] = curc.c;\n                pd[nr][nc] = dirChar[d];\n                q.push({nr, nc});\n            }\n        }\n\n        if (dist[to.r][to.c] == -1) { // unreachable \u2013 stop completely\n            break;\n        }\n\n        // ---- reconstruct path -----------------------------------------\n        vector<char> pathDirs;\n        int cr = to.r, cc = to.c;\n        while (!(cr == cur.r && cc == cur.c)) {\n            pathDirs.push_back(pd[cr][cc]);      // direction from predecessor to this cell\n            int tr = pr[cr][cc], tc = pc[cr][cc];\n            cr = tr; cc = tc;\n        }\n        reverse(pathDirs.begin(), pathDirs.end());\n\n        // ---- output actions --------------------------------------------\n        for (char d : pathDirs) {\n            if ((int)output.size() == 800) break;\n            output.emplace_back('M', d);\n        }\n\n        cur = to; // reached the next target\n    }\n\n    // ---- write result -----------------------------------------------\n    for (auto &act : output) {\n        cout << act.first << ' ' << act.second << '\\n';\n    }\n    return 0;\n}"},"4":{"ahc001":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Rect\n{\n    int a, b, c, d;          // [a,c) \u00d7 [b,d)\n};\n\nstatic inline bool overlap(const Rect &R, int a, int b, int w, int h)\n{\n    int c = a + w;\n    int d = b + h;\n    return !(a >= R.c || c <= R.a || b >= R.d || d <= R.b);\n}\n\nstatic inline int clamp_int(int v, int lo, int hi)\n{\n    return max(lo, min(v, hi));\n}\n\nint main()\n{\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int n;\n    if (!(cin >> n)) return 0;\n\n    struct RecInfo\n    {\n        int x, y, r, idx;\n    };\n    vector<RecInfo> recs(n);\n    for (int i = 0; i < n; ++i)\n    {\n        cin >> recs[i].x >> recs[i].y >> recs[i].r;\n        recs[i].idx = i;\n    }\n\n    /* process smaller rectangles first */\n    sort(recs.begin(), recs.end(),\n         [](const RecInfo &l, const RecInfo &r) { return l.r < r.r; });\n\n    vector<Rect> placed;\n    vector<Rect> answer(n);\n\n    const int BOARD = 10000;\n    const int DMAX  = 200;           // search radius for BFS\n\n    for (const auto &info : recs)\n    {\n        int x = info.x, y = info.y, r = info.r;\n\n        /* choose width and height */\n        int w = static_cast<int>(sqrt((double)r));\n        if (w == 0) w = 1;\n        int h = (r + w - 1) / w;          // ceil\n\n        /* base position (always inside board and contains point) */\n        int a0 = clamp_int(x, 0, BOARD - w);\n        int b0 = clamp_int(y, 0, BOARD - h);\n\n        bool placedNow = false;\n\n        /* try base position */\n        bool ok = true;\n        for (const Rect &R : placed)\n            if (overlap(R, a0, b0, w, h))\n            {\n                ok = false;\n                break;\n            }\n        if (ok)\n        {\n            placedNow = true;\n            answer[info.idx] = {a0, b0, a0 + w, b0 + h};\n        }\n\n        /* BFS search around base if needed */\n        if (!placedNow)\n        {\n            for (int d = 0; d <= DMAX && !placedNow; ++d)\n            {\n                for (int dx = -d; dx <= d && !placedNow; ++dx)\n                {\n                    int dy = d - abs(dx);\n                    for (int s : {dy, -dy})\n                    {\n                        int a = a0 + dx;\n                        int b = b0 + s;\n                        if (a < 0 || a > BOARD - w) continue;\n                        if (b < 0 || b > BOARD - h) continue;\n                        if (a > x || b > y) continue;      // keep point inside\n\n                        bool ok2 = true;\n                        for (const Rect &R : placed)\n                        {\n                            if (overlap(R, a, b, w, h))\n                            {\n                                ok2 = false;\n                                break;\n                            }\n                        }\n                        if (ok2)\n                        {\n                            placedNow = true;\n                            answer[info.idx] = {a, b, a + w, b + h};\n                            break;\n                        }\n                    }\n                }\n            }\n        }\n\n        /* final fallback \u2013 try small 1\u00d71 squares near the point */\n        if (!placedNow)\n        {\n            int a_start = clamp_int(x, 0, BOARD - 1);\n            int b_start = clamp_int(y, 0, BOARD - 1);\n            for (int d = 0; d <= DMAX && !placedNow; ++d)\n            {\n                for (int dx = -d; dx <= d && !placedNow; ++dx)\n                {\n                    int dy = d - abs(dx);\n                    for (int s : {dy, -dy})\n                    {\n                        int a = a_start + dx;\n                        int b = b_start + s;\n                        if (a < 0 || a > BOARD - 1) continue;\n                        if (b < 0 || b > BOARD - 1) continue;\n\n                        bool ok2 = true;\n                        for (const Rect &R : placed)\n                        {\n                            if (overlap(R, a, b, 1, 1))\n                            {\n                                ok2 = false;\n                                break;\n                            }\n                        }\n                        if (ok2)\n                        {\n                            placedNow = true;\n                            answer[info.idx] = {a, b, a + 1, b + 1};\n                            break;\n                        }\n                    }\n                }\n            }\n        }\n\n        /* this should always succeed; but just in case */\n        if (!placedNow)\n            answer[info.idx] = {0, 0, 1, 1};\n\n        placed.push_back(answer[info.idx]);\n    }\n\n    /* output in original order */\n    for (int i = 0; i < n; ++i)\n    {\n        const Rect &r = answer[i];\n        cout << r.a << ' ' << r.b << ' ' << r.c << ' ' << r.d << '\\n';\n    }\n\n    return 0;\n}","ahc002":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 50;\nconst int MAXTILES = N * N;          // 2500\n\n/*----------------------------------------------------------*/\n/*  Path state for beam search and random walks             */\n/*----------------------------------------------------------*/\nstruct Path {\n    int i, j;                         // current position\n    bitset<MAXTILES> used;            // visited tiles\n    long long score;                  // accumulated value\n    string moves;                     // path string\n\n    // default constructor (needed by std::vector when resizing)\n    Path() = default;\n\n    // explicit constructor for a starting state\n    Path(int ii, int jj, int tid, long long val)\n        : i(ii), j(jj), score(val) {\n        used.reset();\n        used.set(tid);\n    }\n};\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int si, sj;\n    if (!(cin >> si >> sj)) return 0;\n\n    int tile[N][N];\n    int val[N][N];\n    int maxTid = -1;\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < N; ++j) {\n            cin >> tile[i][j];\n            maxTid = max(maxTid, tile[i][j]);\n        }\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < N; ++j)\n            cin >> val[i][j];\n\n    const int di[4] = {-1, 1, 0, 0};\n    const int dj[4] = {0, 0, -1, 1};\n    const char dirChar[4] = {'U', 'D', 'L', 'R'};\n\n    /*---------------------------------------------------*/\n    /*  1. Greedy baseline (fast & safe)                 */\n    /*---------------------------------------------------*/\n    vector<char> global_used(maxTid + 1, 0);\n    global_used[tile[si][sj]] = 1;\n    string bestPath;\n    long long bestScore = val[si][sj];\n    int cur_i = si, cur_j = sj;\n\n    while (true) {\n        int bestVal = -1, bestD = -1;\n        for (int d = 0; d < 4; ++d) {\n            int ni = cur_i + di[d], nj = cur_j + dj[d];\n            if (ni < 0 || ni >= N || nj < 0 || nj >= N) continue;\n            int t = tile[ni][nj];\n            if (global_used[t]) continue;\n            if (val[ni][nj] > bestVal) { bestVal = val[ni][nj]; bestD = d; }\n        }\n        if (bestD == -1) break;\n        bestPath.push_back(dirChar[bestD]);\n        cur_i += di[bestD];\n        cur_j += dj[bestD];\n        global_used[tile[cur_i][cur_j]] = 1;\n        bestScore += val[cur_i][cur_j];\n    }\n\n    /*---------------------------------------------------*/\n    /*  2. Beam search (depth\u2011limited)                   */\n    /*---------------------------------------------------*/\n    const int BEAM = 200;           // beam width\n    const int BEAM_TIME_LIMIT_MS = 1100;   // milliseconds per test case for beam phase\n\n    auto start_time = chrono::steady_clock::now();\n    while (chrono::duration_cast<chrono::milliseconds>(\n               chrono::steady_clock::now() - start_time).count() < BEAM_TIME_LIMIT_MS) {\n\n        vector<Path> current;\n        current.emplace_back(si, sj, tile[si][sj], val[si][sj]);\n\n        // explore up to 20 steps from each frontier element\n        for (int step = 0; step < 20; ++step) {\n            vector<Path> nxt;\n            nxt.reserve(current.size() * 4);\n\n            for (auto &p : current) {\n                for (int d = 0; d < 4; ++d) {\n                    int ni = p.i + di[d], nj = p.j + dj[d];\n                    if (ni < 0 || ni >= N || nj < 0 || nj >= N) continue;\n                    int tid = tile[ni][nj];\n                    if (p.used.test(tid)) continue;\n\n                    Path np = p;                 // copy current state\n                    np.i = ni; np.j = nj;\n                    np.used.set(tid);\n                    np.score += val[ni][nj];\n                    np.moves.push_back(dirChar[d]);\n\n                    if (np.score > bestScore) {\n                        bestScore = np.score;\n                        bestPath = np.moves;\n                    }\n\n                    nxt.push_back(std::move(np));\n                }\n            }\n            if (nxt.empty()) break;\n\n            sort(nxt.begin(), nxt.end(),\n                 [](const Path &a, const Path &b) { return a.score > b.score; });\n            if ((int)nxt.size() > BEAM) nxt.resize(BEAM);\n            current.swap(nxt);\n        }\n    }\n\n    /*---------------------------------------------------*/\n    /*  3. Random walks (weighted)                       */\n    /*---------------------------------------------------*/\n    const int RND_TIME_LIMIT_MS = 1900;      // allow some margin\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n    const double TEMPERATURE = 30.0;         // bias strength\n\n    while (chrono::duration_cast<chrono::milliseconds>(\n               chrono::steady_clock::now() - start_time).count() < RND_TIME_LIMIT_MS) {\n\n        Path p(si, sj, tile[si][sj], val[si][sj]);\n\n        while (true) {\n            struct Cand {\n                int d, ni, nj, tid, val;\n                double w;\n            };\n            vector<Cand> cand;\n            for (int d = 0; d < 4; ++d) {\n                int ni = p.i + di[d], nj = p.j + dj[d];\n                if (ni < 0 || ni >= N || nj < 0 || nj >= N) continue;\n                int tid = tile[ni][nj];\n                if (p.used.test(tid)) continue;\n                double w = exp(cand.empty() ? 0 : (double)cand.back().val / TEMPERATURE);\n                // actually use current square's value for weight:\n                // But use val[ni][nj]\n                w = exp((double)val[ni][nj] / TEMPERATURE);\n                cand.push_back({d, ni, nj, tid, val[ni][nj], w});\n            }\n\n            if (cand.empty()) break;\n\n            double sum = 0;\n            for (auto &c : cand) sum += c.w;\n            uniform_real_distribution<double> urd(0, sum);\n            double r = urd(rng);\n            double acc = 0;\n            Cand chosen = cand[0];\n            for (auto &c : cand) {\n                acc += c.w;\n                if (r <= acc) { chosen = c; break; }\n            }\n\n            p.i = chosen.ni; p.j = chosen.nj;\n            p.used.set(chosen.tid);\n            p.score += chosen.val;\n            p.moves.push_back(dirChar[chosen.d]);\n\n            if (p.score > bestScore) {\n                bestScore = p.score;\n                bestPath = p.moves;\n            }\n        }\n    }\n\n    cout << bestPath << '\\n';\n    return 0;\n}","ahc003":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct EdgeInfo {\n    double sum = 0.0;   // cumulative estimated costs\n    int    cnt = 0;     // number of usages\n    double get() const { return cnt ? sum / cnt : 1.0; } // default 1.0\n};\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    const int N = 30;\n    const int V = N * N;\n    const int Q = 1000;\n\n    // edge tables\n    EdgeInfo h[ N ][ N - 1 ];   // horizontal edges (i, j)-(i, j+1)\n    EdgeInfo v[ N - 1 ][ N ];   // vertical   edges (i, j)-(i+1, j)\n\n    // helper lambdas\n    auto node_id = [N](int i, int j){ return i * N + j; };\n\n    for (int qi = 0; qi < Q; ++qi) {\n        int si, sj, ti, tj;\n        if (!(cin >> si >> sj >> ti >> tj)) return 0;\n\n        /* ---------- run Dijkstra with current estimates ---------- */\n        const double INF = 1e100;\n        vector<double> dist(V, INF);\n        vector<int>    prev(V, -1);\n        vector<char>   move(V, 0);   // 'U','D','L','R'\n\n        priority_queue<pair<double,int>, vector<pair<double,int>>, greater<>> pq;\n        int sid = node_id(si, sj);\n        int tid = node_id(ti, tj);\n        dist[sid] = 0.0;\n        pq.emplace(0.0, sid);\n\n        while (!pq.empty()) {\n            auto [d, u] = pq.top(); pq.pop();\n            if (d != dist[u]) continue;\n            if (u == tid) break;\n\n            int ui = u / N, uj = u % N;\n\n            // left\n            if (uj > 0) {\n                int vId = node_id(ui, uj-1);\n                double w = h[ui][uj-1].get();\n                if (dist[vId] > d + w) {\n                    dist[vId] = d + w;\n                    prev[vId] = u;\n                    move[vId] = 'L';\n                    pq.emplace(dist[vId], vId);\n                }\n            }\n            // right\n            if (uj < N-1) {\n                int vId = node_id(ui, uj+1);\n                double w = h[ui][uj].get();\n                if (dist[vId] > d + w) {\n                    dist[vId] = d + w;\n                    prev[vId] = u;\n                    move[vId] = 'R';\n                    pq.emplace(dist[vId], vId);\n                }\n            }\n            // up\n            if (ui > 0) {\n                int vId = node_id(ui-1, uj);\n                double w = v[ui-1][uj].get();\n                if (dist[vId] > d + w) {\n                    dist[vId] = d + w;\n                    prev[vId] = u;\n                    move[vId] = 'U';\n                    pq.emplace(dist[vId], vId);\n                }\n            }\n            // down\n            if (ui < N-1) {\n                int vId = node_id(ui+1, uj);\n                double w = v[ui][uj].get();\n                if (dist[vId] > d + w) {\n                    dist[vId] = d + w;\n                    prev[vId] = u;\n                    move[vId] = 'D';\n                    pq.emplace(dist[vId], vId);\n                }\n            }\n        }\n\n        /* ---------- reconstruct path ---------- */\n        string path;\n        int cur = tid;\n        while (cur != sid) {\n            path.push_back(move[cur]);\n            cur = prev[cur];\n        }\n        reverse(path.begin(), path.end());\n\n        /* ---------- output and flush ---------- */\n        cout << path << '\\n' << flush;\n\n        /* ---------- read judge's reply ---------- */\n        long long x;\n        if (!(cin >> x)) return 0;\n\n        /* ---------- update edge estimates ---------- */\n        int ci = si, cj = sj;\n        double cost_per_edge = static_cast<double>(x) / path.size();\n        for (char mv : path) {\n            if (mv == 'R') {\n                h[ci][cj].sum += cost_per_edge;\n                h[ci][cj].cnt += 1;\n                ++cj;\n            } else if (mv == 'L') {\n                h[ci][cj-1].sum += cost_per_edge;\n                h[ci][cj-1].cnt += 1;\n                --cj;\n            } else if (mv == 'D') {\n                v[ci][cj].sum += cost_per_edge;\n                v[ci][cj].cnt += 1;\n                ++ci;\n            } else if (mv == 'U') {\n                v[ci-1][cj].sum += cost_per_edge;\n                v[ci-1][cj].cnt += 1;\n                --ci;\n            }\n        }\n    }\n    return 0;\n}","ahc004":"#include <bits/stdc++.h>\nusing namespace std;\n\n// Random number generator based on splitmix64\nstruct RNG {\n    unsigned long long x;\n    RNG() { x = chrono::steady_clock::now().time_since_epoch().count(); }\n    unsigned long long next() {\n        unsigned long long z = (x += 0x9e3779b97f4a7c15ULL);\n        z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ULL;\n        z = (z ^ (z >> 27)) * 0x94d049bb133111ebULL;\n        return z ^ (z >> 31);\n    }\n    int randint(int l, int r) { return l + int(next() % (unsigned long long)(r - l + 1)); }\n};\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M;\n    if (!(cin >> N >> M)) return 0;\n    vector<string> S(M);\n    for (int i = 0; i < M; ++i) cin >> S[i];\n\n    const int ITER_MAX   = 4000;          // number of random trials\n    const int MAX_LEN    = 12;            // maximum string length\n    const double TIME_LIMIT = 2.7;        // safety guard (seconds)\n\n    RNG rng;\n\n    vector<string> bestMat(N, string(N, '.'));\n    int bestScore = -1;\n\n    auto start = chrono::steady_clock::now();\n\n    for (int iter = 0; iter < ITER_MAX; ++iter) {\n        // ---- generate random matrix ----\n        vector<string> mat(N, string(N, 'A'));\n        for (int i = 0; i < N; ++i)\n            for (int j = 0; j < N; ++j)\n                mat[i][j] = char('A' + rng.randint(0, 7));\n\n        // ---- collect all horizontal/vertical substrings\n        unordered_set<string> H, V;\n        H.reserve(5000);\n        V.reserve(5000);\n\n        // Horizontal\n        for (int i = 0; i < N; ++i) {\n            string r2 = mat[i] + mat[i];  // length 40\n            for (int len = 2; len <= MAX_LEN; ++len)\n                for (int st = 0; st < N; ++st)\n                    H.insert(r2.substr(st, len));\n        }\n\n        // Vertical\n        string col(N, 'A');\n        for (int j = 0; j < N; ++j) {\n            for (int i = 0; i < N; ++i) col[i] = mat[i][j];\n            string c2 = col + col;\n            for (int len = 2; len <= MAX_LEN; ++len)\n                for (int st = 0; st < N; ++st)\n                    V.insert(c2.substr(st, len));\n        }\n\n        // ---- score ----\n        int score = 0;\n        for (const string &s : S)\n            if (H.find(s) != H.end() || V.find(s) != V.end())\n                ++score;\n\n        if (score > bestScore) {\n            bestScore = score;\n            bestMat   = mat;\n        }\n\n        if (score == M) break;   // perfect match\n\n        // ---- time guard ----\n        auto now = chrono::steady_clock::now();\n        double elapsed = chrono::duration<double>(now - start).count();\n        if (elapsed > TIME_LIMIT) break;\n    }\n\n    // ---- output ----\n    for (int i = 0; i < N; ++i)\n        cout << bestMat[i] << '\\n';\n\n    return 0;\n}","ahc005":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    int N, si, sj;\n    if (!(cin >> N >> si >> sj)) return 0;\n    vector<string> g(N);\n    for (int i = 0; i < N; ++i) cin >> g[i];\n\n    auto inb = [&](int x, int y) { return 0 <= x && x < N && 0 <= y && y < N; };\n\n    const char dirs[4] = {'U', 'D', 'L', 'R'};\n    const int dx[4] = {-1, 1, 0, 0};\n    const int dy[4] = {0, 0, -1, 1};\n\n    // map each road cell to an index\n    vector<vector<int>> idx(N, vector<int>(N, -1));\n    vector<pair<int,int>> pos;\n    int cnt = 0;\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < N; ++j)\n            if (g[i][j] != '#') {\n                idx[i][j] = cnt++;\n                pos.emplace_back(i, j);\n            }\n\n    if (cnt == 0) {               // no road - trivial output\n        cout << '\\n';\n        return 0;\n    }\n\n    // adjacency list\n    vector<vector<int>> adj(cnt);\n    for (int id = 0; id < cnt; ++id) {\n        auto [x, y] = pos[id];\n        for (int d = 0; d < 4; ++d) {\n            int nx = x + dx[d], ny = y + dy[d];\n            if (inb(nx, ny) && idx[nx][ny] != -1)\n                adj[id].push_back(idx[nx][ny]);\n        }\n    }\n\n    int start = idx[si][sj];\n    vector<char> ans;\n\n    // visited mask\n    vector<bool> visited(cnt, false);\n    visited[start] = true;\n    int cur = start;\n\n    // helper lambda: BFS from src, returns dist and parent\n    auto bfs = [&](int src, vector<int> &dist, vector<int> &parent) {\n        dist.assign(cnt, -1);\n        parent.assign(cnt, -1);\n        deque<int> q;\n        dist[src] = 0;\n        q.push_back(src);\n        while (!q.empty()) {\n            int u = q.front();\n            q.pop_front();\n            for (int v : adj[u]) {\n                if (dist[v] == -1) {\n                    dist[v] = dist[u] + 1;\n                    parent[v] = u;\n                    q.push_back(v);\n                }\n            }\n        }\n    };\n\n    // main loop: always go to the nearest unvisited node\n    while (true) {\n        int remaining = 0;\n        for (bool v : visited) if (!v) ++remaining;\n        if (remaining == 0) break;\n\n        vector<int> dist, parent;\n        bfs(cur, dist, parent);\n\n        int target = -1;\n        int best = INT_MAX;\n        for (int v = 0; v < cnt; ++v) {\n            if (!visited[v] && dist[v] != -1 && dist[v] < best) {\n                best = dist[v];\n                target = v;\n            }\n        }\n        if (target == -1) break; // should not happen\n\n        // reconstruct path from cur to target\n        vector<int> path;\n        int p = target;\n        while (p != -1) {\n            path.push_back(p);\n            if (p == cur) break;\n            p = parent[p];\n        }\n        reverse(path.begin(), path.end()); // from cur to target\n\n        // emit moves along the path\n        for (size_t i = 1; i < path.size(); ++i) {\n            auto [x1, y1] = pos[path[i-1]];\n            auto [x2, y2] = pos[path[i]];\n            int d = -1;\n            if (x2 == x1-1 && y2 == y1) d = 0;\n            else if (x2 == x1+1 && y2 == y1) d = 1;\n            else if (x2 == x1 && y2 == y1-1) d = 2;\n            else if (x2 == x1 && y2 == y1+1) d = 3;\n            if (d != -1) ans.push_back(dirs[d]);\n            visited[path[i]] = true;\n        }\n        cur = target;\n    }\n\n    // finally return to start\n    vector<int> dist, parent;\n    bfs(cur, dist, parent);\n    vector<int> path;\n    int p = start;\n    while (p != -1) {\n        path.push_back(p);\n        if (p == cur) break;\n        p = parent[p];\n    }\n    reverse(path.begin(), path.end()); // cur -> start\n\n    for (size_t i = 1; i < path.size(); ++i) {\n        auto [x1, y1] = pos[path[i-1]];\n        auto [x2, y2] = pos[path[i]];\n        int d = -1;\n        if (x2 == x1-1 && y2 == y1) d = 0;\n        else if (x2 == x1+1 && y2 == y1) d = 1;\n        else if (x2 == x1 && y2 == y1-1) d = 2;\n        else if (x2 == x1 && y2 == y1+1) d = 3;\n        if (d != -1) ans.push_back(dirs[d]);\n    }\n\n    string out(ans.begin(), ans.end());\n    cout << out << '\\n';\n    return 0;\n}","future-contest-2022-qual":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, K, R;\n    if (!(cin >> N >> M >> K >> R)) return 0;\n\n    /* ---------- read task difficulty vectors ---------- */\n    vector<long long> sum_d(N, 0);\n    vector<vector<int>> d(N, vector<int>(K));\n    for (int i = 0; i < N; ++i) {\n        long long s = 0;\n        for (int j = 0; j < K; ++j) {\n            int x; cin >> x;\n            d[i][j] = x;\n            s += x;\n        }\n        sum_d[i] = s;\n    }\n\n    /* ---------- build graph of dependencies ---------- */\n    vector<vector<int>> out(N);\n    vector<int> indeg(N, 0);\n    for (int i = 0; i < R; ++i) {\n        int u, v; cin >> u >> v;      // 1\u2011based\n        --u; --v;\n        out[u].push_back(v);\n        ++indeg[v];\n    }\n\n    /* ---------- compute critical path distance ---------- */\n    vector<long long> dist(N, 0);\n    for (int i = N - 1; i >= 0; --i) {\n        long long best = 0;                // longest successor\n        for (int v : out[i])\n            best = max(best, dist[v]);\n        dist[i] = sum_d[i] + best;\n    }\n\n    /* ---------- ready queue (by dist) ---------- */\n    auto cmpDist = [&](int a, int b) { return dist[a] < dist[b]; };\n    priority_queue<int, vector<int>, decltype(cmpDist)> ready(cmpDist);\n\n    for (int i = 0; i < N; ++i)\n        if (indeg[i] == 0) ready.push(i);\n\n    vector<char> started(N, 0), done(N, 0);\n    vector<int> worker_task(M, -1);      // -1 idle\n    vector<long long> sum_t(M, 0);       // sum of finished durations\n    vector<int> cnt(M, 0);               // number of finished tasks\n\n    while (true) {\n        /* -------- start of a day -------- */\n        vector<pair<int,int>> assignments;\n        vector<int> idle;\n        for (int w = 0; w < M; ++w)\n            if (worker_task[w] == -1) idle.push_back(w);\n\n        /* order idle workers by current avg time (fastest first) */\n        auto cmpWorker = [&](int a, int b) {\n            double ma = cnt[a] ? 1.0 * sum_t[a] / cnt[a] : 1e9;\n            double mb = cnt[b] ? 1.0 * sum_t[b] / cnt[b] : 1e9;\n            return ma > mb;                      // descending \u2192 fastest first\n        };\n        sort(idle.begin(), idle.end(), cmpWorker);\n\n        for (int w : idle) {\n            if (ready.empty()) break;\n            int task = ready.top(); ready.pop();\n            worker_task[w] = task;\n            started[task] = 1;\n            assignments.emplace_back(w, task);\n        }\n\n        /* output */\n        cout << assignments.size();\n        for (auto &pa : assignments)\n            cout << ' ' << pa.first + 1 << ' ' << pa.second + 1;\n        cout << '\\n';\n        cout.flush();\n\n        /* -------- read results of this day -------- */\n        int nfin;  if (!(cin >> nfin)) break;\n        if (nfin == -1) break;\n        vector<int> fin_list(nfin);\n        for (int i = 0; i < nfin; ++i) cin >> fin_list[i];\n\n        for (int fid : fin_list) {\n            int w = fid - 1;\n            int t = worker_task[w];\n\n            for (int v : out[t]) {\n                if (--indeg[v] == 0 && !started[v])\n                    ready.push(v);\n            }\n\n            /* update worker statistics (placeholder 1) */\n            ++cnt[w];\n            sum_t[w] += 1;\n            worker_task[w] = -1;\n            done[t] = 1;\n        }\n    }\n    return 0;\n}","ahc006":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    const int N = 1000;\n    vector<int> a(N), b(N), c(N), d(N);\n    for (int i = 0; i < N; ++i) {\n        if (!(cin >> a[i] >> b[i] >> c[i] >> d[i])) return 0;\n    }\n\n    // Greedy selection of 50 orders\n    vector<bool> used(N, false);\n    vector<int> chosen;                 // 0\u2011based indices\n    vector<pair<int,int>> route;        // coordinates\n    route.emplace_back(400, 400);       // start at office\n\n    int curx = 400, cury = 400;\n    for (int step = 0; step < 50; ++step) {\n        int best = -1;\n        long long bestDist = LLONG_MAX;\n        for (int i = 0; i < N; ++i) if (!used[i]) {\n            long long dist = llabs(curx - a[i]) + llabs(cury - b[i]);\n            if (dist < bestDist) {\n                bestDist = dist;\n                best = i;\n            }\n        }\n        used[best] = true;\n        chosen.push_back(best);\n        route.emplace_back(a[best], b[best]);   // restaurant\n        route.emplace_back(c[best], d[best]);   // destination\n        curx = c[best];\n        cury = d[best];\n    }\n\n    route.emplace_back(400, 400);   // return to office\n\n    // Output\n    cout << (int)chosen.size();\n    for (int idx : chosen) cout << ' ' << (idx + 1); // 1-based\n    cout << '\\n';\n\n    cout << (int)route.size();\n    for (auto [x, y] : route) cout << ' ' << x << ' ' << y;\n    cout << '\\n';\n\n    return 0;\n}","ahc007":"#include <bits/stdc++.h>\nusing namespace std;\n\n// ---------- DSU ----------\nstruct DSU {\n    vector<int> p, r;\n    DSU(int n = 0) { init(n); }\n    void init(int n) {\n        p.resize(n);\n        r.assign(n, 0);\n        iota(p.begin(), p.end(), 0);\n    }\n    int find(int x) {\n        if (p[x] == x) return x;\n        return p[x] = find(p[x]);            // path compression\n    }\n    bool unite(int a, int b) {\n        a = find(a); b = find(b);\n        if (a == b) return false;\n        if (r[a] < r[b]) swap(a, b);\n        p[b] = a;\n        if (r[a] == r[b]) ++r[a];\n        return true;\n    }\n};\n\n// ---------- main ----------\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    const int N = 400, M = 1995;\n    // read coordinates, but we never use them\n    for (int i = 0; i < N; ++i) {\n        int x, y; cin >> x >> y;\n    }\n\n    vector<int> u(M), v(M);\n    for (int i = 0; i < M; ++i) {\n        cin >> u[i] >> v[i];\n    }\n\n    DSU dsu(N);\n\n    for (int i = 0; i < M; ++i) {\n        int li;            // the real length\n        cin >> li;\n\n        if (dsu.unite(u[i], v[i])) {\n            cout << 1 << '\\n';\n        } else {\n            cout << 0 << '\\n';\n        }\n        cout.flush();        // essential for interactive judge\n    }\n    return 0;\n}","ahc008":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int H = 30;\nconst int W = 30;\n\nstruct Pos{int r, c;};\n\nint main(){\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N;\n    if(!(cin >> N)) return 0;\n    vector<Pos> pets(N);\n    for(int i = 0; i < N; ++i){\n        int x, y, t;               // read type too\n        cin >> x >> y >> t;\n        pets[i] = {x-1, y-1};       // 0\u2011based\n    }\n\n    int M;\n    cin >> M;\n    vector<Pos> humans(M);\n    for(int i = 0; i < M; ++i){\n        int x, y;\n        cin >> x >> y;\n        humans[i] = {x-1, y-1};\n    }\n\n    /* grid states */\n    bool imp[H][W] = {};           // impassable\n    bool human[H][W] = {};         // human presence\n    bool pet[H][W] = {};           // at least one pet present\n\n    for(auto &p: pets) pet[p.r][p.c] = true;\n    for(auto &h: humans) human[h.r][h.c] = true;\n\n    const int d4[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};\n    const char blockC[4] = {'u','d','l','r'};\n    // helper\n    auto inb = [&](int r, int c){ return 0<=r && r<H && 0<=c && c<W; };\n\n    auto canBlock = [&](int r, int c)->bool{\n        if(imp[r][c] || human[r][c] || pet[r][c]) return false;\n        for(int dr=-1; dr<=1; ++dr){\n            for(int dc=-1; dc<=1; ++dc){\n                if(dr==0 && dc==0) continue;\n                int nr=r+dr, nc=c+dc;\n                if(!inb(nr,nc)) continue;\n                if(pet[nr][nc]) return false;\n            }\n        }\n        return true;\n    };\n\n    for(int turn=0; turn<300; ++turn){\n        string actions(M, '.');\n        for(int i=0;i<M;++i){\n            Pos &h = humans[i];\n            bool done=false;\n            for(int d=0; d<4; ++d){\n                int nr = h.r + d4[d][0];\n                int nc = h.c + d4[d][1];\n                if(!inb(nr,nc)) continue;\n                if(canBlock(nr,nc)){\n                    actions[i] = blockC[d];\n                    imp[nr][nc] = true;  // becomes impassable for this turn\n                    done=true;\n                    break;\n                }\n            }\n        }\n\n        cout << actions << '\\n' << flush;\n\n        /* read pet movements */\n        vector<string> mv(N);\n        for(int i=0;i<N;++i) cin >> mv[i];\n\n        /* reset pet grid */\n        memset(pet,0,sizeof(pet));\n\n        /* move pets */\n        for(int i=0;i<N;++i){\n            Pos &p = pets[i];\n            for(char c: mv[i]){\n                int d = (c=='U'?0:(c=='D'?1:(c=='L'?2:3)));\n                int nr = p.r + d4[d][0];\n                int nc = p.c + d4[d][1];\n                if(!inb(nr,nc)) continue;   // safety, should not happen\n                if(imp[nr][nc]) continue;  // blocked\n                p.r = nr;\n                p.c = nc;\n            }\n            pet[p.r][p.c] = true;\n        }\n        /* humans are never moved in this simple strategy */\n    }\n    return 0;\n}","ahc009":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Node { int r, c; };\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int sr, sc, tr, tc;\n    double p;\n    if (!(cin >> sr >> sc >> tr >> tc >> p)) return 0;\n\n    /* read walls */\n    vector<string> h(20);                // horizontal : 20 rows, 19 columns\n    for (int i = 0; i < 20; ++i) cin >> h[i];\n    vector<string> v(19);                // vertical   : 19 rows, 20 columns\n    for (int i = 0; i < 19; ++i) cin >> v[i];\n\n    const int N = 20;\n    vector<vector<bool>> seen(N, vector<bool>(N, false));\n    vector<vector<int>> pr(N, vector<int>(N, -1));\n    vector<vector<int>> pc(N, vector<int>(N, -1));\n    vector<vector<char>> pd(N, vector<char>(N, 0));\n\n    queue<Node> q;\n    q.push({sr, sc});\n    seen[sr][sc] = true;\n\n    const int dr[4] = {-1, 1, 0, 0};\n    const int dc[4] = {0, 0, -1, 1};\n    const char dirc[4] = {'U', 'D', 'L', 'R'};\n\n    /* BFS for shortest path */\n    while (!q.empty()) {\n        Node cur = q.front(); q.pop();\n        if (cur.r == tr && cur.c == tc) break;\n        for (int k = 0; k < 4; ++k) {\n            int nr = cur.r + dr[k];\n            int nc = cur.c + dc[k];\n            if (nr < 0 || nr >= N || nc < 0 || nc >= N) continue;\n\n            /* check wall */\n            bool blocked = false;\n            if (dr[k] == -1) {                    // move up\n                blocked = (v[nr][nc] == '1');\n            } else if (dr[k] == 1) {              // move down\n                blocked = (v[cur.r][cur.c] == '1');\n            } else if (dc[k] == -1) {             // move left\n                blocked = (h[cur.r][nc] == '1');\n            } else {                              // move right\n                blocked = (h[cur.r][cur.c] == '1');\n            }\n            if (blocked) continue;\n            if (seen[nr][nc]) continue;\n\n            seen[nr][nc] = true;\n            pr[nr][nc] = cur.r;\n            pc[nr][nc] = cur.c;\n            pd[nr][nc] = dirc[k];\n            q.push({nr, nc});\n        }\n    }\n\n    /* reconstruct shortest path P (start -> office) */\n    vector<char> P;\n    int cr = tr, cc = tc;\n    while (!(cr == sr && cc == sc)) {\n        P.push_back(pd[cr][cc]);          // direction taken from parent to this cell\n        int nr = pr[cr][cc];\n        int nc = pc[cr][cc];\n        cr = nr; cc = nc;\n    }\n    reverse(P.begin(), P.end());          // forward direction\n\n    /* build reverse path R */\n    vector<char> R;\n    for (auto it = P.rbegin(); it != P.rend(); ++it) {\n        char d = *it;\n        if (d == 'U')      R.push_back('D');\n        else if (d == 'D') R.push_back('U');\n        else if (d == 'L') R.push_back('R');\n        else               R.push_back('L');\n    }\n\n    string answer;\n    int L = P.size();\n    if (5 * L <= 200) {                // two full cycles are possible\n        answer.reserve(5 * L);\n        answer.append(P.begin(), P.end());\n        answer.append(R.begin(), R.end());\n        answer.append(P.begin(), P.end());\n        answer.append(R.begin(), R.end());\n        answer.append(P.begin(), P.end());\n    } else {                            // only one full cycle\n        answer.reserve(3 * L);\n        answer.append(P.begin(), P.end());\n        answer.append(R.begin(), R.end());\n        answer.append(P.begin(), P.end());\n    }\n\n    cout << answer << '\\n';\n    return 0;\n}","ahc010":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    // read and ignore the 30\u00d730 input grid\n    string line;\n    for (int i = 0; i < 30; ++i) {\n        if (!(cin >> line)) return 0;\n    }\n\n    // produce a deterministic rotation pattern\n    string ans;\n    ans.reserve(900);\n    for (int i = 0; i < 30; ++i) {\n        for (int j = 0; j < 30; ++j) {\n            int r = (i + j) % 4;          // 0 .. 3\n            ans.push_back(char('0' + r));\n        }\n    }\n\n    cout << ans << '\\n';\n    return 0;\n}","ahc011":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int N, T;\n    if (!(cin >> N >> T)) return 0;\n    string row;\n    int ei = -1, ej = -1;\n    for (int i = 0; i < N; ++i) {\n        cin >> row;\n        for (int j = 0; j < N; ++j) {\n            if (row[j] == '0') {\n                ei = i;\n                ej = j;\n            }\n        }\n    }\n\n    string ans;\n    // Move empty to the rightmost column\n    while (ej < N - 1) {\n        ans.push_back('R');      // slide rightward tile into empty\n        ++ej;\n    }\n    // Move empty to the bottommost row\n    while (ei < N - 1) {\n        ans.push_back('D');      // slide downward tile into empty\n        ++ei;\n    }\n\n    cout << ans << '\\n';\n    return 0;\n}","ahc012":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, K;\n    if (!(cin >> N >> K)) return 0;\n\n    // read the a_d (10 numbers)\n    for (int i = 0; i < 10; ++i) {\n        int tmp; cin >> tmp;\n    }\n    // read all strawberry coordinates\n    for (int i = 0; i < N; ++i) {\n        int x, y; cin >> x >> y;\n    }\n\n    const long long LIM = 1000000000LL;\n    vector<array<long long,4>> lines;\n\n    const int M = 50;               // number of vertical lines\n    const int N_h = 50;             // number of horizontal lines\n    const long long STEP = 400LL;    // 20000 / 50\n    const long long START_XY = -10000LL + STEP;   // -9600\n\n    // vertical lines\n    for (int i = 0; i < M; ++i) {\n        long long x = START_XY + i * STEP;\n        lines.push_back({x, -LIM, x, LIM});\n    }\n\n    // horizontal lines\n    for (int i = 0; i < N_h; ++i) {\n        long long y = START_XY + i * STEP;\n        lines.push_back({-LIM, y, LIM, y});\n    }\n\n    cout << lines.size() << '\\n';\n    for (auto &l : lines) {\n        cout << l[0] << ' ' << l[1] << ' ' << l[2] << ' ' << l[3] << '\\n';\n    }\n    return 0;\n}","ahc014":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Op {\n    int x1, y1, x2, y2, x3, y3, x4, y4;\n};\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    int N, M;\n    if (!(cin >> N >> M)) return 0;\n\n    vector<vector<char>> dot(N, vector<char>(N, 0));\n    for (int i = 0; i < M; ++i) {\n        int x, y; cin >> x >> y;\n        dot[x][y] = 1;\n    }\n\n    // unit segments\n    vector<vector<char>> hor(N, vector<char>(N-1, 0));\n    vector<vector<char>> ver(N-1, vector<char>(N, 0));\n    vector<vector<char>> dpos(N-1, vector<char>(N-1, 0));   // (x,y) -> (x+1,y+1)\n    vector<vector<char>> dneg(N-1, vector<char>(N-1, 0));   // (x,y+1) -> (x+1,y)\n\n    auto weight = [&](int x, int y)->long long {\n        long long dx = x - (N-1)/2;\n        long long dy = y - (N-1)/2;\n        return dx*dx + dy*dy + 1;\n    };\n\n    vector<Op> ops;\n\n    while (true) {\n        long long bestW = -1;\n        bool bestIsSquare = false;\n        int bestSqX=0,bestSqY=0,bestSqMissing=0;\n        int bestDiaX=0,bestDiaY=0,bestDiaMissing=0;\n\n        /* ---- squares ---- */\n        for (int x=0;x+1<N;++x) for (int y=0;y+1<N;++y) {\n            // check if all four edges free\n            if (hor[y][x] || hor[y+1][x] || ver[y][x] || ver[y][x+1]) continue;\n            int empties=0, midx=-1;\n            // vertices indices: 0:(x,y),1:(x+1,y),2:(x,y+1),3:(x+1,y+1)\n            for (int k=0;k<4;++k) {\n                int cx = x + (k==1||k==3);\n                int cy = y + (k==2||k==3);\n                if (!dot[cx][cy]) { ++empties; midx=k; }\n            }\n            if (empties!=1) continue;\n            // weight of the new dot\n            int nx,ny;\n            if (midx==0) { nx=x; ny=y; }\n            else if (midx==1) { nx=x+1; ny=y; }\n            else if (midx==2) { nx=x; ny=y+1; }\n            else { nx=x+1; ny=y+1; }\n            long long w = weight(nx,ny);\n            if (w > bestW) {\n                bestW = w;\n                bestIsSquare = true;\n                bestSqX = x; bestSqY = y; bestSqMissing = midx;\n            }\n        }\n\n        /* ---- diamonds ---- */\n        for (int x=0; x+2<N; ++x)     // base lower-left corner of the tiny square\n            for (int y=1; y+1<N; ++y) {\n                // corners: 0:(x,y),1:(x+1,y+1),2:(x+2,y),3:(x+1,y-1)\n                if (dpos[x][y]||dneg[x+1][y]||dpos[x+1][y-1]||dneg[x][y-1]) continue;\n                int empties=0, midx=-1;\n                int cx[4] = {x, x+1, x+2, x+1};\n                int cy[4] = {y, y+1, y,   y-1};\n                for (int k=0;k<4;++k) {\n                    if (!dot[cx[k]][cy[k]]) { ++empties; midx=k; }\n                }\n                if (empties!=1) continue;\n                int nx=cx[midx], ny=cy[midx];\n                long long w = weight(nx,ny);\n                if (w > bestW) {\n                    bestW = w;\n                    bestIsSquare = false;\n                    bestDiaX = x; bestDiaY = y; bestDiaMissing = midx;\n                }\n            }\n\n        if (bestW < 0) break;          // no operation possible\n\n        /* --- execute the chosen operation --- */\n        if (bestIsSquare) {\n            int x = bestSqX, y = bestSqY, midx = bestSqMissing;\n            int nx,ny;\n            if (midx==0) { nx=x; ny=y; }\n            else if (midx==1) { nx=x+1; ny=y; }\n            else if (midx==2) { nx=x; ny=y+1; }\n            else { nx=x+1; ny=y+1; }\n\n            // mark edges\n            hor[y][x] = hor[y+1][x] = ver[y][x] = ver[y][x+1] = 1;\n            dot[nx][ny] = 1;\n\n            // build operation order: clockwise around the square\n            int order[4];\n            // cw cycle 0,1,3,2\n            int posInCw[4] = {0,1,3,2};\n            for (int t=0;t<4;++t) {\n                int idx = 4 == 0 ? 0 : 0; // dummy\n            }\n            vector<int> cw = {0,1,3,2};\n            int startPos = posInCw[midx];\n            vector<int> cyc(4);\n            for (int t=0;t<4;++t)\n                cyc[t] = cw[(startPos + t)%4];\n\n            Op op;\n            op.x1 = nx; op.y1 = ny;\n            op.x2 = x + (cyc[1]==1||cyc[1]==3);\n            op.y2 = y + (cyc[1]==2||cyc[1]==3);\n            op.x3 = x + (cyc[2]==1||cyc[2]==3);\n            op.y3 = y + (cyc[2]==2||cyc[2]==3);\n            op.x4 = x + (cyc[3]==1||cyc[3]==3);\n            op.y4 = y + (cyc[3]==2||cyc[3]==3);\n            ops.push_back(op);\n        } else { // diamond\n            int x = bestDiaX, y = bestDiaY, midx = bestDiaMissing;\n            int cx[4] = {x, x+1, x+2, x+1};\n            int cy[4] = {y, y+1, y,   y-1};\n            int nx = cx[midx], ny = cy[midx];\n\n            // mark diagonal segments\n            dpos[x][y] = dneg[x+1][y] = dpos[x+1][y-1] = dneg[x][y-1] = 1;\n            dot[nx][ny] = 1;\n\n            // order: 0,1,2,3 is clockwise\n            int cyc[4];\n            int startPos = midx;\n            for (int t=0;t<4;++t) cyc[t] = (startPos + t)%4;\n\n            Op op;\n            op.x1 = nx; op.y1 = ny;\n            op.x2 = cx[cyc[1]];\n            op.y2 = cy[cyc[1]];\n            op.x3 = cx[cyc[2]];\n            op.y3 = cy[cyc[2]];\n            op.x4 = cx[cyc[3]];\n            op.y4 = cy[cyc[3]];\n            ops.push_back(op);\n        }\n    }\n\n    /* output */\n    cout << ops.size() << '\\n';\n    for (auto &op: ops)\n        cout << op.x1 << ' ' << op.y1 << ' '\n             << op.x2 << ' ' << op.y2 << ' '\n             << op.x3 << ' ' << op.y3 << ' '\n             << op.x4 << ' ' << op.y4 << '\\n';\n    return 0;\n}","ahc015":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Pos { int r, c; };\n\nstatic inline void tilt(vector<vector<int>>& g, char dir) {\n    if (dir == 'F') {                 // up\n        for (int c = 0; c < 10; ++c) {\n            vector<int> v;\n            for (int r = 0; r < 10; ++r)\n                if (g[r][c]) v.push_back(g[r][c]);\n            for (int r = 0; r < 10; ++r)\n                g[r][c] = (r < (int)v.size() ? v[r] : 0);\n        }\n    } else if (dir == 'B') {          // down\n        for (int c = 0; c < 10; ++c) {\n            vector<int> v;\n            for (int r = 9; r >= 0; --r)\n                if (g[r][c]) v.push_back(g[r][c]);\n            for (int r = 9, idx = 0; r >= 0; --r, ++idx)\n                g[r][c] = (idx < (int)v.size() ? v[idx] : 0);\n        }\n    } else if (dir == 'L') {          // left\n        for (int r = 0; r < 10; ++r) {\n            vector<int> v;\n            for (int c = 0; c < 10; ++c)\n                if (g[r][c]) v.push_back(g[r][c]);\n            for (int c = 0; c < 10; ++c)\n                g[r][c] = (c < (int)v.size() ? v[c] : 0);\n        }\n    } else {                          // 'R'\n        for (int r = 0; r < 10; ++r) {\n            vector<int> v;\n            for (int c = 9; c >= 0; --c)\n                if (g[r][c]) v.push_back(g[r][c]);\n            for (int c = 9, idx = 0; c >= 0; --c, ++idx)\n                g[r][c] = (idx < (int)v.size() ? v[idx] : 0);\n        }\n    }\n}\n\nstatic inline long long connectivityScore(const vector<vector<int>>& g) {\n    bool vis[10][10] = {};\n    long long score = 0;\n    for (int r = 0; r < 10; ++r) for (int c = 0; c < 10; ++c) {\n        if (g[r][c] && !vis[r][c]) {\n            int flavour = g[r][c];\n            int cnt = 0;\n            queue<pair<int,int>> q;\n            q.push({r,c});\n            vis[r][c] = true;\n            while (!q.empty()) {\n                auto [x,y] = q.front(); q.pop();\n                ++cnt;\n                const int dr[4]={-1,1,0,0}, dc[4]={0,0,-1,1};\n                for (int k=0;k<4;++k){\n                    int nx=x+dr[k], ny=y+dc[k];\n                    if(nx>=0 && nx<10 && ny>=0 && ny<10 &&\n                       !vis[nx][ny] && g[nx][ny]==flavour){\n                        vis[nx][ny]=true;\n                        q.push({nx,ny});\n                    }\n                }\n            }\n            score += 1LL*cnt*cnt;\n        }\n    }\n    return score;\n}\n\nint main(){\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    vector<int> flavour(100);\n    for(int i=0;i<100;i++) cin>>flavour[i];\n\n    vector<vector<int>> board(10, vector<int>(10,0));\n    vector<Pos> empty;\n    empty.reserve(100);\n    for (int r=0;r<10;++r)\n        for (int c=0;c<10;++c)\n            empty.push_back({r,c});\n\n    const char dirs[4] = {'F','B','L','R'};\n    for(int t=0;t<100;++t){\n        int p; cin>>p;                     // 1\u2011based\n        Pos pos = empty[p-1];\n        board[pos.r][pos.c] = flavour[t];\n        empty.erase(empty.begin()+p-1);    // O(100) but 100*100 is negligible\n\n        // decide tilt\n        long long bestScore = -1;\n        char bestDir = 'F';\n        for(char d: dirs){\n            auto tmp = board;\n            tilt(tmp,d);\n            long long sc = connectivityScore(tmp);\n            if(sc > bestScore){\n                bestScore = sc;\n                bestDir   = d;\n            }\n        }\n\n        cout << bestDir << '\\n' << flush; // flush after each output\n    }\n    return 0;\n}","ahc016":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int M; double eps;\n    if (!(cin >> M >> eps)) return 0;\n\n    const int N = 20;\n    const int L = N * (N - 1) / 2;          // 190\n\n    cout << N << '\\n';\n    for (int k = 0; k < M; ++k) {\n        string s;\n        s.append(k, '1');\n        s.append(L - k, '0');\n        cout << s << '\\n';\n    }\n    cout.flush();\n\n    for (int q = 0; q < 100; ++q) {\n        string H;\n        cin >> H;\n        int ones = 0;\n        for (char c : H) if (c == '1') ++ones;\n        int ans = min(ones, M - 1);\n        cout << ans << '\\n';\n        cout.flush();\n    }\n    return 0;\n}","ahc017":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int N, M, D, K;\n    if (!(cin >> N >> M >> D >> K)) return 0;\n\n    // Skip edge data\n    for (int i = 0; i < M; ++i) {\n        int u, v, w;\n        cin >> u >> v >> w;\n    }\n    // Skip coordinates\n    for (int i = 0; i < N; ++i) {\n        int x, y;\n        cin >> x >> y;\n    }\n\n    vector<int> id(M);\n    iota(id.begin(), id.end(), 0);\n\n    // Fast random shuffle\n    static std::mt19937 rng(\n        std::chrono::steady_clock::now().time_since_epoch().count());\n    shuffle(id.begin(), id.end(), rng);\n\n    vector<int> day(M);\n    for (int j = 0; j < M; ++j) {\n        int idx = id[j];\n        day[idx] = (j % D) + 1;          // days are 1\u2011based\n    }\n\n    for (int i = 0; i < M; ++i) {\n        if (i) cout << ' ';\n        cout << day[i];\n    }\n    cout << '\\n';\n    return 0;\n}","ahc019":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Vec3 {int x, y, z;};\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int D; \n    if (!(cin >> D)) return 0;\n    vector<string> f1(D), r1(D), f2(D), r2(D);\n    for (int i = 0; i < D; ++i) cin >> f1[i];\n    for (int i = 0; i < D; ++i) cin >> r1[i];\n    for (int i = 0; i < D; ++i) cin >> f2[i];\n    for (int i = 0; i < D; ++i) cin >> r2[i];\n\n    auto idx = [D](int x,int y,int z){ return x*D*D + y*D + z; };\n\n    /* Boolean grids of cubes that exist in each object */\n    vector<vector<vector<char>>> cube1(D, vector<vector<char>>(D, vector<char>(D,0)));\n    vector<vector<vector<char>>> cube2(D, vector<vector<char>>(D, vector<char>(D,0)));\n\n    for (int z = 0; z < D; ++z)\n        for (int x = 0; x < D; ++x)\n            for (int y = 0; y < D; ++y) {\n                bool f1p = f1[z][x]=='1';\n                bool r1p = r1[z][y]=='1';\n                bool f2p = f2[z][x]=='1';\n                bool r2p = r2[z][y]=='1';\n                cube1[x][y][z] = f1p & r1p;\n                cube2[x][y][z] = f2p & r2p;\n            }\n\n    /* 3 grids: common, only1, only2 */\n    vector<vector<vector<char>>> common(D, vector<vector<char>>(D, vector<char>(D,0)));\n    vector<vector<vector<char>>> only1(D, vector<vector<char>>(D, vector<char>(D,0)));\n    vector<vector<vector<char>>> only2(D, vector<vector<char>>(D, vector<char>(D,0)));\n\n    for (int x = 0; x < D; ++x)\n        for (int y = 0; y < D; ++y)\n            for (int z = 0; z < D; ++z) {\n                bool c = cube1[x][y][z] & cube2[x][y][z];\n                common[x][y][z] = c;\n                only1[x][y][z] = cube1[x][y][z] & !c;\n                only2[x][y][z] = cube2[x][y][z] & !c;\n            }\n\n    const int dirs[6][3] = {{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};\n    auto flood = [&](vector<vector<vector<char>>>& grid)\n                 -> vector<vector<Vec3>> {\n        vector<vector<Vec3>> comps;\n        vector<vector<vector<char>>> vis(D, vector<vector<char>>(D, vector<char>(D,0)));\n        for (int x = 0; x < D; ++x)\n            for (int y = 0; y < D; ++y)\n                for (int z = 0; z < D; ++z)\n                    if (grid[x][y][z] && !vis[x][y][z]) {\n                        vector<Vec3> comp;\n                        queue<Vec3> qu;\n                        qu.push({x,y,z});\n                        vis[x][y][z] = 1;\n                        while (!qu.empty()) {\n                            auto [cx,cy,cz] = qu.front(); qu.pop();\n                            comp.push_back({cx,cy,cz});\n                            for (auto &d: dirs) {\n                                int nx=cx+d[0], ny=cy+d[1], nz=cz+d[2];\n                                if(nx>=0&&nx<D&&ny>=0&&ny<D&&nz>=0&&nz<D\n                                   &&grid[nx][ny][nz] && !vis[nx][ny][nz]){\n                                    vis[nx][ny][nz]=1;\n                                    qu.push({nx,ny,nz});\n                                }\n                            }\n                        }\n                        comps.push_back(std::move(comp));\n                    }\n        return comps;\n    };\n\n    auto commonComps = flood(common);\n    auto only1Comps   = flood(only1);\n    auto only2Comps   = flood(only2);\n\n    /* Prepare answer arrays */\n    vector<int> b1(D*D*D,0), b2(D*D*D,0);\n    int block_id = 1;\n\n    /* common components \u2013 used in both objects */\n    for (auto &comp : commonComps) {\n        for (auto &c : comp) {\n            b1[idx(c.x,c.y,c.z)] = block_id;\n            b2[idx(c.x,c.y,c.z)] = block_id;\n        }\n        ++block_id;\n    }\n    /* only1 components \u2013 used only in first object */\n    for (auto &comp : only1Comps) {\n        for (auto &c : comp) {\n            b1[idx(c.x,c.y,c.z)] = block_id;\n        }\n        ++block_id;\n    }\n    /* only2 components \u2013 used only in second object */\n    for (auto &comp : only2Comps) {\n        for (auto &c : comp) {\n            b2[idx(c.x,c.y,c.z)] = block_id;\n        }\n        ++block_id;\n    }\n    int n = block_id - 1;\n\n    /* Output */\n    cout << n << '\\n';\n    for (int i = 0; i < (int)b1.size(); ++i) {\n        if(i) cout << ' ';\n        cout << b1[i];\n    }\n    cout << '\\n';\n    for (int i = 0; i < (int)b2.size(); ++i) {\n        if(i) cout << ' ';\n        cout << b2[i];\n    }\n    cout << '\\n';\n    return 0;\n}","ahc020":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct DSU {\n    vector<int> p, sz;\n    DSU(int n = 0) { init(n); }\n    void init(int n) {\n        p.resize(n);\n        sz.assign(n, 1);\n        iota(p.begin(), p.end(), 0);\n    }\n    int find(int x) { return p[x] == x ? x : p[x] = find(p[x]); }\n    bool unite(int a, int b) {\n        a = find(a); b = find(b);\n        if (a == b) return false;\n        if (sz[a] < sz[b]) swap(a, b);\n        p[b] = a;\n        sz[a] += sz[b];\n        return true;\n    }\n};\n\nstruct EdgeInfo {\n    int to, w, idx;\n};\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    int N, M, K;\n    if (!(cin >> N >> M >> K)) return 0;\n\n    vector<long long> xs(N), ys(N);\n    for (int i = 0; i < N; ++i) cin >> xs[i] >> ys[i];\n\n    vector<vector<EdgeInfo>> adj(N);\n    struct Edge { int u, v, w, idx; };\n    vector<Edge> edges(M);\n    for (int j = 0; j < M; ++j) {\n        int u, v, w;\n        cin >> u >> v >> w;\n        --u; --v;\n        edges[j] = {u, v, w, j};\n        adj[u].push_back({v, w, j});\n        adj[v].push_back({u, w, j});\n    }\n\n    /* 1. residents & assignment to nearest vertex */\n    vector<long long> maxDist2(N, 0);\n    for (int k = 0; k < K; ++k) {\n        long long a, b;\n        cin >> a >> b;\n        int best = -1;\n        long long bestD2 = LLONG_MAX;\n        for (int i = 0; i < N; ++i) {\n            long long dx = a - xs[i];\n            long long dy = b - ys[i];\n            long long d2 = dx * dx + dy * dy;\n            if (d2 < bestD2) {\n                bestD2 = d2;\n                best = i;\n            }\n        }\n        if (best != -1 && bestD2 > maxDist2[best]) maxDist2[best] = bestD2;\n    }\n\n    /* 2. compute P[i] */\n    vector<int> P(N, 0);\n    for (int i = 0; i < N; ++i) {\n        if (maxDist2[i] == 0) { P[i] = 0; }\n        else {\n            double d = sqrt((double)maxDist2[i]);\n            int p = (int)ceil(d);\n            if (p > 5000) p = 5000;\n            P[i] = p;\n        }\n    }\n\n    /* 3. terminals */\n    vector<int> terminals;\n    terminals.reserve(N);\n    terminals.push_back(0);          // vertex 1\n    set<int> seen = {0};\n    for (int i = 0; i < N; ++i) {\n        if (P[i] > 0 && !seen.count(i)) {\n            terminals.push_back(i);\n            seen.insert(i);\n        }\n    }\n    int T = terminals.size();\n\n    /* 4. Dijkstra from every terminal */\n    const long long INF = (1LL << 60);\n    vector<vector<long long>> dist(T, vector<long long>(N, INF));\n    vector<vector<int>> prevNode(T, vector<int>(N, -1));\n    vector<vector<int>> prevEdge(T, vector<int>(N, -1));\n\n    for (int t = 0; t < T; ++t) {\n        int src = terminals[t];\n        priority_queue<pair<long long,int>, vector<pair<long long,int>>, greater<pair<long long,int>>> pq;\n        dist[t][src] = 0;\n        pq.push({0, src});\n        while (!pq.empty()) {\n            auto [d, v] = pq.top(); pq.pop();\n            if (d != dist[t][v]) continue;\n            for (auto &e : adj[v]) {\n                long long nd = d + e.w;\n                if (nd < dist[t][e.to]) {\n                    dist[t][e.to] = nd;\n                    prevNode[t][e.to] = v;\n                    prevEdge[t][e.to] = e.idx;\n                    pq.push({nd, e.to});\n                }\n            }\n        }\n    }\n\n    /* 5. Build complete graph of terminals */\n    struct CEdge { long long w; int a, b; };\n    vector<CEdge> cEdges;\n    cEdges.reserve(T * (T - 1) / 2);\n    for (int i = 0; i < T; ++i) {\n        for (int j = i + 1; j < T; ++j) {\n            long long w = dist[i][terminals[j]];\n            cEdges.push_back({w, i, j});\n        }\n    }\n\n    /* 6. Kruskal on terminal complete graph */\n    sort(cEdges.begin(), cEdges.end(), [](const CEdge &x, const CEdge &y){ return x.w < y.w; });\n    DSU dsuT(T);\n    vector<char> usedEdge(M, 0);\n\n    for (auto &ce : cEdges) {\n        if (dsuT.unite(ce.a, ce.b)) {\n            int s = terminals[ce.a];\n            int t = terminals[ce.b];\n            /* reconstruct shortest path from s to t using parent of source s */\n            int cur = t;\n            while (cur != s) {\n                int eidx = prevEdge[ce.a][cur];\n                usedEdge[eidx] = 1;\n                cur = prevNode[ce.a][cur];\n            }\n        }\n    }\n\n    /* 7. Output */\n    for (int i = 0; i < N; ++i) {\n        if (i) cout << ' ';\n        cout << P[i];\n    }\n    cout << '\\n';\n    for (int j = 0; j < M; ++j) {\n        if (j) cout << ' ';\n        cout << (usedEdge[j] ? 1 : 0);\n    }\n    cout << '\\n';\n    return 0;\n}","ahc021":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    const int N = 30;\n    const int S = N * (N + 1) / 2;          // 465\n    vector<int> a(S);\n    for (int i = 0; i < S; ++i) cin >> a[i];\n\n    // ---------- pre\u2011compute coordinates and row number ----------\n    vector<pair<int,int>> coord(S);\n    vector<int> row_of_idx(S);\n    for (int r = 0; r < N; ++r) {\n        int start = r * (r + 1) / 2;\n        for (int c = 0; c <= r; ++c) {\n            int idx = start + c;\n            coord[idx] = {r, c};\n            row_of_idx[idx] = r;\n        }\n    }\n\n    // ---------- record of swaps ----------\n    struct Op {int x1, y1, x2, y2;};\n    vector<Op> ops;\n\n    // ---------- heapify bottom\u2011up ----------\n    const int INTERNAL = S - N;          // indices 0 \u2026 434 are internal\n    for (int start = INTERNAL - 1; start >= 0; --start) {\n        int cur = start;\n        while (true) {\n            int r = row_of_idx[cur];\n            int left = cur + r + 1;\n            int right = cur + r + 2;\n            int smallest = cur;\n            if (left < S && a[left] < a[smallest]) smallest = left;\n            if (right < S && a[right] < a[smallest]) smallest = right;\n            if (smallest == cur) break;\n            swap(a[cur], a[smallest]);\n            ops.push_back({coord[cur].first, coord[cur].second,\n                           coord[smallest].first, coord[smallest].second});\n            cur = smallest;\n        }\n    }\n\n    // ---------- output ----------\n    cout << ops.size() << '\\n';\n    for (const auto &p : ops)\n        cout << p.x1 << ' ' << p.y1 << ' ' << p.x2 << ' ' << p.y2 << '\\n';\n    return 0;\n}","toyota2023summer-final":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Cell{int x,y;};\n\nint main(){\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int D, N;\n    if(!(cin>>D>>N)) return 0;          // read D (=9) and number of obstacles\n    \n    vector<vector<int>> obst(D, vector<int>(D,0));\n    for(int i=0;i<N;i++){\n        int r,c; cin>>r>>c;\n        obst[r][c]=1;\n    }\n    \n    // entrance position\n    const int ex = 0, ey = (D-1)/2;\n    obst[ex][ey] = 0; // ensure entrance is free\n    \n    // ---------- compute distance from entrance ----------\n    const int INF = 1e9;\n    vector<vector<int>> dist(D, vector<int>(D, INF));\n    queue<Cell> q;\n    dist[ex][ey] = 0;\n    q.push({ex,ey});\n    const int dx[4]={-1,1,0,0};\n    const int dy[4]={0,0,-1,1};\n    while(!q.empty()){\n        auto cur = q.front(); q.pop();\n        for(int dir=0;dir<4;dir++){\n            int nx = cur.x + dx[dir];\n            int ny = cur.y + dy[dir];\n            if(nx<0||nx>=D||ny<0||ny>=D) continue;\n            if(obst[nx][ny]) continue;\n            if(dist[nx][ny]!=INF) continue;\n            dist[nx][ny]=dist[cur.x][cur.y]+1;\n            q.push({nx,ny});\n        }\n    }\n    \n    // collect all free squares except entrance\n    vector<Cell> cells;\n    for(int i=0;i<D;i++)\n        for(int j=0;j<D;j++){\n            if(obst[i][j]) continue;\n            if(i==ex && j==ey) continue;\n            cells.push_back({i,j});\n        }\n    // sort by decreasing distance\n    sort(cells.begin(), cells.end(), [&](const Cell&a,const Cell&b){\n        return dist[a.x][a.y] > dist[b.x][b.y];\n    });\n    \n    int total = D*D - 1 - N;   // number of containers to be stored\n    int idx = 0;\n    \n    // ---------- placement phase ----------\n    for(int d=0; d<total; ++d){\n        int t; cin>>t;                // receive container number (unused)\n        auto c = cells[idx++];\n        cout<<c.x<<\" \"<<c.y<<\"\\n\"<<flush;   // output and flush immediately\n    }\n    \n    // ---------- removal phase ----------\n    // BFS from entrance, output order of non\u2011entrance visited cells\n    vector<vector<int>> visited(D, vector<int>(D,0));\n    queue<Cell> q2;\n    visited[ex][ey]=1;\n    q2.push({ex,ey});\n    vector<Cell> removal;\n    while(!q2.empty()){\n        auto cur=q2.front(); q2.pop();\n        for(int dir=0;dir<4;dir++){\n            int nx=cur.x+dx[dir];\n            int ny=cur.y+dy[dir];\n            if(nx<0||nx>=D||ny<0||ny>=D) continue;\n            if(obst[nx][ny]) continue;\n            if(visited[nx][ny]) continue;\n            visited[nx][ny]=1;\n            q2.push({nx,ny});\n            removal.push_back({nx,ny});\n        }\n    }\n    for(auto &c: removal){\n        cout<<c.x<<\" \"<<c.y<<\"\\n\";\n    }\n    return 0;\n}","ahc024":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int n, m;\n    if (!(cin >> n >> m)) return 0;  // just in case\n    \n    for (int i = 0; i < n; ++i) {\n        for (int j = 0; j < n; ++j) {\n            int v;  cin >> v;\n            cout << v;\n            if (j + 1 < n) cout << ' ';\n        }\n        cout << '\\n';\n    }\n    return 0;\n}","ahc025":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, D, Q;\n    if (!(cin >> N >> D >> Q)) return 0;\n\n    // Perform Q trivial queries\n    for (int q = 0; q < Q; ++q) {\n        int l = q % N;\n        int r = (q + 1) % N;          // ensures r != l\n        cout << 1 << ' ' << 1 << ' ' << l << ' ' << r << '\\n';\n        cout.flush();\n\n        // read judge answer and ignore\n        char ans;\n        if (!(cin >> ans)) return 0;   // safety: if input ends unexpectedly\n    }\n\n    // Output a simple final division\n    for (int i = 0; i < N; ++i) {\n        if (i) cout << ' ';\n        cout << (i % D);\n    }\n    cout << '\\n';\n    return 0;\n}","ahc026":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    const int n = 200;   // fixed in problem statement\n    const int m = 10;    // fixed in problem statement\n\n    vector<vector<int>> stacks(m);\n\n    for (int i = 0; i < m; ++i) {\n        stacks[i].resize(n / m);\n        for (int j = 0; j < n / m; ++j) {\n            cin >> stacks[i][j];          // bottom -> top\n        }\n    }\n\n    vector<pair<int,int>> ops;          // store (v , i)\n    vector<bool> removed(n + 1, false);\n\n    for (int v = 1; v <= n; ++v) {\n        while (true) {\n            int src = -1;          // stack index where v is\n            int pos = -1;          // position from bottom\n            for (int i = 0; i < m; ++i) {\n                for (int j = 0; j < (int)stacks[i].size(); ++j) {\n                    if (stacks[i][j] == v) {\n                        src = i;\n                        pos = j;\n                        break;\n                    }\n                }\n                if (src != -1) break;\n            }\n            if (src == -1) {            // already removed, should not happen\n                break;\n            }\n\n            if (pos == (int)stacks[src].size() - 1) {\n                // v is on top -> take it out\n                ops.emplace_back(v, 0);\n                stacks[src].pop_back();\n                removed[v] = true;\n                break;\n            } else {\n                // choose destination stack with minimal height (different stack)\n                int dest = -1, best = INT_MAX;\n                for (int i = 0; i < m; ++i) {\n                    if (i == src) continue;\n                    if ((int)stacks[i].size() < best) {\n                        best = stacks[i].size();\n                        dest = i;\n                    }\n                }\n                // move segment [pos .. end] from src to dest\n                vector<int> segment;\n                for (int idx = pos; idx < (int)stacks[src].size(); ++idx) {\n                    segment.push_back(stacks[src][idx]);\n                }\n                // append to dest\n                stacks[dest].insert(stacks[dest].end(), segment.begin(), segment.end());\n                // truncate src\n                stacks[src].resize(pos);\n                // record move operation (1-indexed destination)\n                ops.emplace_back(v, dest + 1);\n            }\n        }\n    }\n\n    // output\n    for (auto [v, i] : ops) {\n        cout << v << ' ' << i << '\\n';\n    }\n    return 0;\n}","ahc027":"#include <bits/stdc++.h>\nusing namespace std;\n\nint N;\nvector<string> h, v;                     // horizontal, vertical walls\nvector<vector<int>> d;                   // dirtiness, unused by the algorithm\nvector<vector<bool>> vis;\n\nstring ans;\nconst char dirChar[4] = {'R', 'D', 'L', 'U'};\nint dx[4] = {0, 1, 0, -1};\nint dy[4] = {1, 0, -1, 0};\n\nbool canMove(int x, int y, int dir) {\n    int nx = x + dx[dir];\n    int ny = y + dy[dir];\n    if (nx < 0 || nx >= N || ny < 0 || ny >= N) return false;\n    if (dir == 0) {            // Right\n        return v[x][y] == '0';\n    } else if (dir == 1) {     // Down\n        return h[x][y] == '0';\n    } else if (dir == 2) {     // Left\n        return v[x][y-1] == '0';\n    } else {                   // Up\n        return h[x-1][y] == '0';\n    }\n}\n\nvoid dfs(int x, int y) {\n    vis[x][y] = true;\n    for (int dir = 0; dir < 4; ++dir) {\n        if (!canMove(x, y, dir)) continue;\n        int nx = x + dx[dir];\n        int ny = y + dy[dir];\n        if (vis[nx][ny]) continue;\n        ans.push_back(dirChar[dir]);          // move to child\n        dfs(nx, ny);\n        ans.push_back(dirChar[(dir + 2) % 4]); // backtrack\n    }\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    if (!(cin >> N)) return 0;\n    h.resize(N-1);\n    for (int i = 0; i < N-1; ++i) cin >> h[i];\n    v.resize(N);\n    for (int i = 0; i < N; ++i) cin >> v[i];\n    d.assign(N, vector<int>(N));\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < N; ++j)\n            cin >> d[i][j];           // not used\n\n    vis.assign(N, vector<bool>(N, false));\n    dfs(0, 0);\n\n    cout << ans << '\\n';\n    return 0;\n}","ahc028":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    int N, M;\n    if (!(cin >> N >> M)) return 0;\n    int si, sj;\n    cin >> si >> sj;\n    vector<string> grid(N);\n    for (int i = 0; i < N; ++i) cin >> grid[i];\n\n    const int SZ = N * N;\n    auto id = [N](int i, int j){ return i * N + j; };\n    vector<int> id2i(SZ), id2j(SZ);\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < N; ++j) {\n            int k = id(i, j);\n            id2i[k] = i;\n            id2j[k] = j;\n        }\n\n    /* positions of every letter */\n    vector<vector<int>> pos(26);\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < N; ++j)\n            pos[grid[i][j]-'A'].push_back(id(i, j));\n\n    /* precompute nearest cell for every starting cell and letter */\n    vector<array<int,26>> nearest(SZ);\n    vector<array<int,26>> nearestDist(SZ);\n    for (int s = 0; s < SZ; ++s) {\n        int siCur = id2i[s], sjCur = id2j[s];\n        for (int c = 0; c < 26; ++c) {\n            int bestDist = 1e9, bestId = -1;\n            for (int t : pos[c]) {\n                int ti = id2i[t], tj = id2j[t];\n                int d = abs(siCur - ti) + abs(sjCur - tj);\n                if (d < bestDist) {\n                    bestDist = d;\n                    bestId = t;\n                }\n            }\n            nearest[s][c] = bestId;\n            nearestDist[s][c] = bestDist;\n        }\n    }\n\n    vector<string> rem(M);\n    for (int i = 0; i < M; ++i) cin >> rem[i];\n\n    vector<pair<int,int>> answer; // list of moves\n\n    int currId = id(si, sj);\n\n    while (!rem.empty()) {\n        long long bestTotal = LLONG_MAX;\n        int bestBlock = -1;\n        int bestStartId = -1;\n        vector<int> bestPath; // ids of the whole block\n\n        for (int b = 0; b < (int)rem.size(); ++b) {\n            const string &t = rem[b];\n            int first = t[0] - 'A';\n            for (int startId : pos[first]) {\n                long long internal = 0;\n                vector<int> path;\n                path.push_back(startId);\n                int cur = startId;\n                bool ok = true;\n                for (int k = 1; k < 5; ++k) {\n                    int need = t[k] - 'A';\n                    int nxt = nearest[cur][need];\n                    if (nxt == -1) {ok=false;break;}\n                    internal += nearestDist[cur][need] + 1;\n                    cur = nxt;\n                    path.push_back(cur);\n                }\n                if (!ok) continue;\n                long long moveFromCurr = nearestDist[currId][first] + 1;\n                long long total = moveFromCurr + internal;\n                if (total < bestTotal) {\n                    bestTotal = total;\n                    bestBlock = b;\n                    bestStartId = startId;\n                    bestPath = path;\n                }\n            }\n        }\n\n        // Output the best block\n        for (int idv : bestPath) {\n            answer.emplace_back(id2i[idv], id2j[idv]);\n        }\n        currId = bestPath.back();\n        rem.erase(rem.begin() + bestBlock);\n    }\n\n    // Print answer\n    for (auto [i, j] : answer) {\n        cout << i << ' ' << j << '\\n';\n    }\n    return 0;\n}","ahc030":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    // Read everything, ignore it.\n    string s;\n    while (cin >> s) {\n        // Do nothing.\n    }\n    return 0;\n}","ahc031":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    int W, D, N;\n    if (!(cin >> W >> D >> N)) return 0;\n    // read the desired areas, they are irrelevant for this trivial solution\n    for (int d = 0; d < D; ++d) {\n        for (int k = 0; k < N; ++k) {\n            long long a; cin >> a;\n        }\n    }\n\n    // For every day, output the same set of N unit squares.\n    for (int d = 0; d < D; ++d) {\n        for (int k = 0; k < N; ++k) {\n            // coordinates: (k,0)-(k+1,1)\n            cout << k << \" \" << 0 << \" \" << k + 1 << \" \" << 1 << '\\n';\n        }\n    }\n    return 0;\n}","ahc032":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int N, M, K;\n    if (!(cin >> N >> M >> K)) return 0;\n    \n    // read a[N][N]\n    long long tmp;\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < N; ++j)\n            cin >> tmp;\n    \n    // read stamps: M stamps of 3x3 each\n    for (int m = 0; m < M; ++m)\n        for (int i = 0; i < 3; ++i)\n            for (int j = 0; j < 3; ++j)\n                cin >> tmp;\n    \n    // Output no operations\n    cout << 0 << '\\n';\n    return 0;\n}","ahc033":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N;\n    if (!(cin >> N)) return 0;          // read N (always 5)\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < N; ++j) {\n            int x; cin >> x;           // read container numbers, ignore\n        }\n\n    const string ops = \"PRRRRQ.\";       // 7 operations per crane\n    for (int i = 0; i < N; ++i) {\n        cout << ops << '\\n';\n    }\n    return 0;\n}","ahc034":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    const int N = 20;\n    vector<vector<int>> h(N, vector<int>(N));\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < N; ++j)\n            cin >> h[i][j];\n\n    vector<pair<int,int>> pos, neg;\n    for (int i = 0; i < N; ++i) {\n        for (int j = 0; j < N; ++j) {\n            if (h[i][j] > 0) pos.emplace_back(i, j);\n            else if (h[i][j] < 0) neg.emplace_back(i, j);\n        }\n    }\n\n    vector<string> ops;\n    int cur_i = 0, cur_j = 0;\n    long long load = 0;           // current load in truck\n\n    auto moveTo = [&](int ti, int tj){\n        // horizontal first\n        while (cur_j < tj){ ops.push_back(\"R\"); ++cur_j; }\n        while (cur_j > tj){ ops.push_back(\"L\"); --cur_j; }\n        while (cur_i < ti){ ops.push_back(\"D\"); ++cur_i; }\n        while (cur_i > ti){ ops.push_back(\"U\"); --cur_i; }\n    };\n\n    // phase 1: collect soil from positive cells\n    for (auto [i, j] : pos) {\n        moveTo(i, j);\n        ops.emplace_back(\"+\" + to_string(h[i][j]));\n        load += h[i][j];\n    }\n\n    // phase 2: deposit onto negative cells\n    for (auto [i, j] : neg) {\n        moveTo(i, j);\n        ops.emplace_back(\"-\" + to_string(-h[i][j]));\n        load -= -h[i][j];\n    }\n\n    // output\n    for (auto &s : ops) cout << s << '\\n';\n    return 0;\n}","ahc035":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, T;\n    if (!(cin >> N >> M >> T)) return 0;          // safety\n\n    const int SEED_CNT = 2 * N * (N - 1);         // 60\n    vector<vector<int>> seeds(SEED_CNT, vector<int>(M));\n\n    /* read initial seeds */\n    for (int i = 0; i < SEED_CNT; ++i)\n        for (int j = 0; j < M; ++j)\n            cin >> seeds[i][j];\n\n    const int PLANTED = N * N; // 36\n\n    /* main loop */\n    for (int turn = 0; turn < T; ++turn) {\n        // 1. diversity + sum based selection\n        vector<int> best_val(M, 0);\n        vector<int> chosen;\n        vector<int> remaining(SEED_CNT);\n        iota(remaining.begin(), remaining.end(), 0);\n\n        while ((int)chosen.size() < PLANTED) {\n            int best_idx = -1;\n            pair<int, long long> best_pair{-1, -1}; // (improvements, sum)\n\n            for (int idx : remaining) {\n                int improv = 0;\n                long long sumv = 0;\n                for (int l = 0; l < M; ++l) {\n                    if (seeds[idx][l] > best_val[l]) ++improv;\n                    sumv += seeds[idx][l];\n                }\n                pair<int, long long> cur{improv, sumv};\n                if (cur > best_pair) {\n                    best_pair = cur;\n                    best_idx = idx;\n                }\n            }\n            chosen.push_back(best_idx);\n            // remove from remaining\n            auto it = std::remove(remaining.begin(), remaining.end(), best_idx);\n            remaining.erase(it, remaining.end());\n\n            // update best_val\n            for (int l = 0; l < M; ++l)\n                best_val[l] = max(best_val[l], seeds[best_idx][l]);\n        }\n\n        // 2. output board in row-major\n        int pos = 0;\n        for (int i = 0; i < N; ++i) {\n            for (int j = 0; j < N; ++j) {\n                if (j) cout << ' ';\n                cout << chosen[pos++];\n            }\n            cout << '\\n';\n        }\n        cout.flush();\n\n        // 3. read next set of seeds\n        for (int i = 0; i < SEED_CNT; ++i)\n            for (int j = 0; j < M; ++j)\n                cin >> seeds[i][j];\n    }\n    return 0;\n}","ahc038":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int N, M, V;\n    if (!(cin >> N >> M >> V)) return 0;\n    string line;\n    for (int i = 0; i < N; ++i) { cin >> line; } // initial grid\n    for (int i = 0; i < N; ++i) { cin >> line; } // target grid\n\n    // Output a single-vertex tree (root only)\n    cout << 1 << '\\n';        // V' = 1\n    // No further lines for edges\n    cout << 0 << ' ' << 0 << '\\n';   // root at (0,0)\n    // No turns -> nothing else to print\n    return 0;\n}","ahc039":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    const int MAXC = 100000;\n    const int S = 200;                          // cell size\n    const int NX = MAXC / S + 1;                // 501\n    const int NY = MAXC / S + 1;\n\n    int N;\n    if (!(cin >> N)) return 0;\n\n    // diff[cx][cy] = mackerels - sardines in that cell\n    vector<vector<int>> diff(NX, vector<int>(NY, 0));\n\n    // store one mackerel to use for fallback\n    int first_mack_x = -1, first_mack_y = -1;\n\n    for (int i = 0; i < N; ++i) {\n        int x, y;\n        cin >> x >> y;\n        int cx = x / S;\n        int cy = y / S;\n        diff[cx][cy] += 1;\n        if (first_mack_x == -1) { first_mack_x = x; first_mack_y = y; }\n    }\n    for (int i = 0; i < N; ++i) {\n        int x, y;\n        cin >> x >> y;\n        int cx = x / S;\n        int cy = y / S;\n        diff[cx][cy] -= 1;\n    }\n\n    long long bestSum = LLONG_MIN;\n    int bestX1 = 0, bestY1 = 0, bestX2 = 0, bestY2 = 0;\n\n    vector<long long> colSum(NX);\n    for (int y1 = 0; y1 < NY; ++y1) {\n        fill(colSum.begin(), colSum.end(), 0);\n        for (int y2 = y1; y2 < NY; ++y2) {\n            for (int x = 0; x < NX; ++x)\n                colSum[x] += diff[x][y2];\n\n            // Kadane on colSum\n            long long curSum = 0;\n            int curL = 0;\n            long long localBest = LLONG_MIN;\n            int localL = 0, localR = 0;\n            for (int x = 0; x < NX; ++x) {\n                curSum += colSum[x];\n                if (curSum > localBest) {\n                    localBest = curSum;\n                    localL = curL;\n                    localR = x;\n                }\n                if (curSum < 0) {\n                    curSum = 0;\n                    curL = x + 1;\n                }\n            }\n            if (localBest > bestSum) {\n                bestSum = localBest;\n                bestX1 = localL;\n                bestX2 = localR;\n                bestY1 = y1;\n                bestY2 = y2;\n            }\n        }\n    }\n\n    // if bestSum <= 0, fallback to a single mackerel\n    long long rectSum = bestSum;\n    int x1, y1, x2, y2;\n    if (bestSum > 0) {\n        x1 = bestX1 * S;\n        y1 = bestY1 * S;\n        x2 = min((bestX2 + 1) * S, MAXC);\n        y2 = min((bestY2 + 1) * S, MAXC);\n    } else {\n        // single mackerel cell\n        int cx = first_mack_x / S;\n        int cy = first_mack_y / S;\n        x1 = cx * S;\n        y1 = cy * S;\n        x2 = min((cx + 1) * S, MAXC);\n        y2 = min((cy + 1) * S, MAXC);\n    }\n\n    cout << 4 << '\\n';\n    cout << x1 << ' ' << y1 << '\\n';\n    cout << x2 << ' ' << y1 << '\\n';\n    cout << x2 << ' ' << y2 << '\\n';\n    cout << x1 << ' ' << y2 << '\\n';\n    return 0;\n}","ahc040":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int N, T;\n    int sigma;                       // sigma is not used\n    if (!(cin >> N >> T >> sigma)) return 0;\n    \n    // read the noisy measurements of the rectangles \u2013 not needed\n    long long wprime, hprime;\n    for (int i = 0; i < N; ++i) {\n        cin >> wprime >> hprime;\n    }\n    \n    // For every turn\n    for (int turn = 0; turn < T; ++turn) {\n        // 1. output the placement plan\n        cout << N << '\\n';\n        // first rectangle\n        cout << 0 << ' ' << 0 << ' ' << 'U' << ' ' << -1 << '\\n';\n        // remaining rectangles\n        for (int i = 1; i < N; ++i) {\n            cout << i << ' ' << 0 << ' ' << 'L' << ' ' << (i - 1) << '\\n';\n        }\n        cout.flush();   // required after each turn\n        \n        // 2. read judge's feedback (two integers)\n        long long Wp, Hp;\n        cin >> Wp >> Hp;   // ignore them\n    }\n    return 0;\n}","ahc041":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    int N, M, H;\n    if (!(cin >> N >> M >> H)) return 0;\n\n    vector<int> A(N);\n    for (int i = 0; i < N; ++i) cin >> A[i];\n\n    vector<vector<int>> adj(N);\n    for (int i = 0; i < M; ++i) {\n        int u, v;\n        cin >> u >> v;\n        adj[u].push_back(v);\n        adj[v].push_back(u);\n    }\n\n    // coordinates \u2013 irrelevant for our algorithm\n    for (int i = 0; i < N; ++i) {\n        int x, y; cin >> x >> y;\n    }\n\n    /* -------- Pre\u2011compute reachable sets up to H -------- */\n    vector<vector<int>> reach(N);\n    vector<int> dist(N);\n    for (int s = 0; s < N; ++s) {\n        fill(dist.begin(), dist.end(), -1);\n        queue<int> q;\n        q.push(s); dist[s] = 0;\n        reach[s].push_back(s);\n        while (!q.empty()) {\n            int v = q.front(); q.pop();\n            if (dist[v] == H) continue;\n            for (int nb : adj[v]) {\n                if (dist[nb] == -1) {\n                    dist[nb] = dist[v] + 1;\n                    q.push(nb);\n                    reach[s].push_back(nb);\n                }\n            }\n        }\n    }\n\n    /* -------- Greedy forest construction -------- */\n    vector<char> assigned(N, 0);\n    vector<int> parent(N, -1);\n\n    std::mt19937 rng((unsigned)chrono::steady_clock::now().time_since_epoch().count());\n\n    int remaining = N;\n    while (remaining > 0) {\n        // choose best root\n        int bestRoot = -1;\n        long long bestScore = -1;\n        for (int v = 0; v < N; ++v) if (!assigned[v]) {\n            long long s = 0;\n            for (int u : reach[v]) if (!assigned[u]) s += A[u];\n            if (s > bestScore) {\n                bestScore = s;\n                bestRoot = v;\n            }\n        }\n\n        // start DFS from bestRoot\n        assigned[bestRoot] = 1;\n        parent[bestRoot] = -1;\n        --remaining;\n        vector<pair<int,int>> st;\n        st.emplace_back(bestRoot, 0);\n        while (!st.empty()) {\n            auto [cur, d] = st.back(); st.pop_back();\n            if (d == H) continue;\n            vector<int> neigh = adj[cur];\n            shuffle(neigh.begin(), neigh.end(), rng);\n            for (int nb : neigh) {\n                if (!assigned[nb]) {\n                    assigned[nb] = 1;\n                    parent[nb] = cur;\n                    st.emplace_back(nb, d + 1);\n                    --remaining;\n                }\n            }\n        }\n    }\n\n    /* -------- Output -------- */\n    for (int i = 0; i < N; ++i) {\n        if (i) cout << ' ';\n        cout << parent[i];\n    }\n    cout << '\\n';\n    return 0;\n}","ahc042":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    const int N = 20;\n    string line;\n    for (int i = 0; i < N; ++i) {\n        cin >> line;          // read and ignore the board\n    }\n\n    // Simply delete every row by shifting it left 20 times.\n    // After 20 left shifts a row becomes completely empty.\n    for (int r = 0; r < N; ++r) {\n        for (int t = 0; t < N; ++t) {  // N = 20\n            cout << 'L' << ' ' << r << '\\n';\n        }\n    }\n    return 0;\n}","ahc044":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int N;\n    long long L;\n    if (!(cin >> N >> L)) return 0;\n    \n    vector<int> T(N);\n    for (int i = 0; i < N; ++i) cin >> T[i];   // values are read but unused\n    \n    for (int i = 0; i < N; ++i) {\n        int a = (i + 1) % N;\n        int b = a;\n        cout << a << ' ' << b << '\\n';\n    }\n    return 0;\n}","ahc045":"#include <bits/stdc++.h>\nusing namespace std;\n\n// Disjoint Set Union\nstruct DSU {\n    vector<int> p, sz;\n    DSU(int n = 0) { init(n); }\n    void init(int n) { p.resize(n); sz.assign(n,1); iota(p.begin(), p.end(), 0); }\n    int find(int x) { return p[x]==x ? x : p[x]=find(p[x]); }\n    bool unite(int a, int b){\n        a=find(a); b=find(b);\n        if(a==b) return false;\n        if(sz[a]<sz[b]) swap(a,b);\n        p[b]=a; sz[a]+=sz[b];\n        return true;\n    }\n};\n\nint main(){\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    int N, M, Q, L, W;\n    if(!(cin >> N >> M >> Q >> L >> W)) return 0;\n    vector<int> G(M);\n    for(int &x : G) cin >> x;\n\n    vector<int> lx(N), rx(N), ly(N), ry(N);\n    for(int i=0;i<N;i++) cin >> lx[i] >> rx[i] >> ly[i] >> ry[i];\n\n    // approximate coordinates (center of the rectangle)\n    vector<long long> cx(N), cy(N);\n    for(int i=0;i<N;i++){\n        cx[i] = (lx[i] + rx[i]) / 2;\n        cy[i] = (ly[i] + ry[i]) / 2;\n    }\n\n    // pre\u2011compute squared distances (long long)\n    vector<vector<long long>> dist2(N, vector<long long>(N,0));\n    for(int i=0;i<N;i++){\n        for(int j=i+1;j<N;j++){\n            long long dx = cx[i]-cx[j];\n            long long dy = cy[i]-cy[j];\n            dist2[i][j] = dist2[j][i] = dx*dx + dy*dy;\n        }\n    }\n\n    // split cities into groups by ID order\n    vector<vector<int>> groups(M);\n    int pos = 0;\n    for(int i=0;i<M;i++){\n        groups[i].reserve(G[i]);\n        for(int j=0;j<G[i];j++) groups[i].push_back(pos++);\n    }\n\n    cout << \"!\\n\";\n    // process each group\n    for(int gi=0; gi<M; ++gi){\n        const vector<int> &v = groups[gi];\n        int k = v.size();\n\n        // print city IDs (no preceding size)\n        for(int idx=0; idx<k; ++idx){\n            if(idx) cout << ' ';\n            cout << v[idx];\n        }\n        cout << '\\n';\n\n        if(k==1) continue;          // no edges needed\n\n        struct Edge {int u, v; long long d;};\n        vector<Edge> edges;\n        edges.reserve(k*(k-1)/2);\n        for(int i=0;i<k;i++){\n            for(int j=i+1;j<k;j++){\n                int u=v[i], w=v[j];\n                long long d = (long long)floor(sqrt((double)dist2[u][w]));\n                edges.push_back({u,w,d});\n            }\n        }\n        sort(edges.begin(), edges.end(),\n             [](const Edge& a, const Edge& b){\n                 if(a.d!=b.d) return a.d<b.d;\n                 if(a.u!=b.u) return a.u<b.u;\n                 return a.v<b.v;\n             });\n\n        DSU dsu(N);\n        int added=0;\n        for(const auto& e:edges){\n            if(dsu.unite(e.u,e.v)){\n                cout << e.u << ' ' << e.v << '\\n';\n                if(++added==k-1) break;\n            }\n        }\n    }\n    return 0;\n}","ahc046":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Cell {\n    int r, c;\n};\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    int N, M;\n    if (!(cin >> N >> M)) return 0;\n    vector<Cell> pos(M);\n    for (int i = 0; i < M; ++i) cin >> pos[i].r >> pos[i].c;\n\n    const int dr[4] = {-1, 1, 0, 0};\n    const int dc[4] = {0, 0, -1, 1};\n    const char dirChar[4] = {'U', 'D', 'L', 'R'};\n\n    vector<pair<char,char>> output;           // action, direction\n\n    Cell cur = pos[0];\n    for (int k = 0; k < M-1 && (int)output.size() < 800; ++k) {\n        Cell to = pos[k+1];\n\n        // ---- blocked array: all future targets except the next one ----\n        bool blocked[20][20] = {false};\n        for (int t = k+2; t < M; ++t) {\n            blocked[pos[t].r][pos[t].c] = true;\n        }\n\n        // ---- BFS -------------------------------------------------------\n        int dist[20][20];\n        int pr[20][20], pc[20][20];\n        char pd[20][20];\n        memset(dist, -1, sizeof(dist));\n        queue<Cell> q;\n        dist[cur.r][cur.c] = 0;\n        q.push(cur);\n\n        while (!q.empty() && dist[to.r][to.c] == -1) {\n            Cell curc = q.front(); q.pop();\n            for (int d = 0; d < 4; ++d) {\n                int nr = curc.r + dr[d];\n                int nc = curc.c + dc[d];\n                if (nr < 0 || nr >= N || nc < 0 || nc >= N) continue;\n                if (blocked[nr][nc]) continue;\n                if (dist[nr][nc] != -1) continue;\n                dist[nr][nc] = dist[curc.r][curc.c] + 1;\n                pr[nr][nc] = curc.r;\n                pc[nr][nc] = curc.c;\n                pd[nr][nc] = dirChar[d];\n                q.push({nr, nc});\n            }\n        }\n\n        if (dist[to.r][to.c] == -1) { // unreachable \u2013 stop completely\n            break;\n        }\n\n        // ---- reconstruct path -----------------------------------------\n        vector<char> pathDirs;\n        int cr = to.r, cc = to.c;\n        while (!(cr == cur.r && cc == cur.c)) {\n            pathDirs.push_back(pd[cr][cc]);      // direction from predecessor to this cell\n            int tr = pr[cr][cc], tc = pc[cr][cc];\n            cr = tr; cc = tc;\n        }\n        reverse(pathDirs.begin(), pathDirs.end());\n\n        // ---- output actions --------------------------------------------\n        for (char d : pathDirs) {\n            if ((int)output.size() == 800) break;\n            output.emplace_back('M', d);\n        }\n\n        cur = to; // reached the next target\n    }\n\n    // ---- write result -----------------------------------------------\n    for (auto &act : output) {\n        cout << act.first << ' ' << act.second << '\\n';\n    }\n    return 0;\n}"},"8":{"ahc001":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Rect\n{\n    int a, b, c, d;          // [a,c) \u00d7 [b,d)\n};\n\nstatic inline bool overlap(const Rect &R, int a, int b, int w, int h)\n{\n    int c = a + w;\n    int d = b + h;\n    return !(a >= R.c || c <= R.a || b >= R.d || d <= R.b);\n}\n\nstatic inline int clamp_int(int v, int lo, int hi)\n{\n    return max(lo, min(v, hi));\n}\n\nint main()\n{\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int n;\n    if (!(cin >> n)) return 0;\n\n    struct RecInfo\n    {\n        int x, y, r, idx;\n    };\n    vector<RecInfo> recs(n);\n    for (int i = 0; i < n; ++i)\n    {\n        cin >> recs[i].x >> recs[i].y >> recs[i].r;\n        recs[i].idx = i;\n    }\n\n    /* process smaller rectangles first */\n    sort(recs.begin(), recs.end(),\n         [](const RecInfo &l, const RecInfo &r) { return l.r < r.r; });\n\n    vector<Rect> placed;\n    vector<Rect> answer(n);\n\n    const int BOARD = 10000;\n    const int DMAX  = 200;           // search radius for BFS\n\n    for (const auto &info : recs)\n    {\n        int x = info.x, y = info.y, r = info.r;\n\n        /* choose width and height */\n        int w = static_cast<int>(sqrt((double)r));\n        if (w == 0) w = 1;\n        int h = (r + w - 1) / w;          // ceil\n\n        /* base position (always inside board and contains point) */\n        int a0 = clamp_int(x, 0, BOARD - w);\n        int b0 = clamp_int(y, 0, BOARD - h);\n\n        bool placedNow = false;\n\n        /* try base position */\n        bool ok = true;\n        for (const Rect &R : placed)\n            if (overlap(R, a0, b0, w, h))\n            {\n                ok = false;\n                break;\n            }\n        if (ok)\n        {\n            placedNow = true;\n            answer[info.idx] = {a0, b0, a0 + w, b0 + h};\n        }\n\n        /* BFS search around base if needed */\n        if (!placedNow)\n        {\n            for (int d = 0; d <= DMAX && !placedNow; ++d)\n            {\n                for (int dx = -d; dx <= d && !placedNow; ++dx)\n                {\n                    int dy = d - abs(dx);\n                    for (int s : {dy, -dy})\n                    {\n                        int a = a0 + dx;\n                        int b = b0 + s;\n                        if (a < 0 || a > BOARD - w) continue;\n                        if (b < 0 || b > BOARD - h) continue;\n                        if (a > x || b > y) continue;      // keep point inside\n\n                        bool ok2 = true;\n                        for (const Rect &R : placed)\n                        {\n                            if (overlap(R, a, b, w, h))\n                            {\n                                ok2 = false;\n                                break;\n                            }\n                        }\n                        if (ok2)\n                        {\n                            placedNow = true;\n                            answer[info.idx] = {a, b, a + w, b + h};\n                            break;\n                        }\n                    }\n                }\n            }\n        }\n\n        /* final fallback \u2013 try small 1\u00d71 squares near the point */\n        if (!placedNow)\n        {\n            int a_start = clamp_int(x, 0, BOARD - 1);\n            int b_start = clamp_int(y, 0, BOARD - 1);\n            for (int d = 0; d <= DMAX && !placedNow; ++d)\n            {\n                for (int dx = -d; dx <= d && !placedNow; ++dx)\n                {\n                    int dy = d - abs(dx);\n                    for (int s : {dy, -dy})\n                    {\n                        int a = a_start + dx;\n                        int b = b_start + s;\n                        if (a < 0 || a > BOARD - 1) continue;\n                        if (b < 0 || b > BOARD - 1) continue;\n\n                        bool ok2 = true;\n                        for (const Rect &R : placed)\n                        {\n                            if (overlap(R, a, b, 1, 1))\n                            {\n                                ok2 = false;\n                                break;\n                            }\n                        }\n                        if (ok2)\n                        {\n                            placedNow = true;\n                            answer[info.idx] = {a, b, a + 1, b + 1};\n                            break;\n                        }\n                    }\n                }\n            }\n        }\n\n        /* this should always succeed; but just in case */\n        if (!placedNow)\n            answer[info.idx] = {0, 0, 1, 1};\n\n        placed.push_back(answer[info.idx]);\n    }\n\n    /* output in original order */\n    for (int i = 0; i < n; ++i)\n    {\n        const Rect &r = answer[i];\n        cout << r.a << ' ' << r.b << ' ' << r.c << ' ' << r.d << '\\n';\n    }\n\n    return 0;\n}","ahc002":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 50;\nconst int MAXTILES = N * N;          // 2500\n\n/*----------------------------------------------------------*/\n/*  Path state \u2013 keeps position, visited tiles, score, moves  */\n/*----------------------------------------------------------*/\nstruct Path {\n    int i, j;                         // current position\n    bitset<MAXTILES> used;            // visited tiles\n    long long score;                  // accumulated score\n    string moves;                     // path string\n\n    Path() = default;                  // needed for vector::resize\n    Path(int ii, int jj, int tid, long long val)\n        : i(ii), j(jj), score(val) {\n        used.reset();\n        used.set(tid);\n    }\n};\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int si, sj;\n    if (!(cin >> si >> sj)) return 0;\n\n    int tile[N][N];\n    int val[N][N];\n    int maxTid = -1;\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < N; ++j) {\n            cin >> tile[i][j];\n            maxTid = max(maxTid, tile[i][j]);\n        }\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < N; ++j)\n            cin >> val[i][j];\n\n    const int di[4] = {-1, 1, 0, 0};\n    const int dj[4] = {0, 0, -1, 1};\n    const char dir[4] = {'U', 'D', 'L', 'R'};\n\n    /*---------------------------------------------------*/\n    /*  1. Greedy baseline (fast & safe)                 */\n    /*---------------------------------------------------*/\n    vector<char> global_used(maxTid + 1, 0);\n    global_used[tile[si][sj]] = 1;\n    string bestPath;\n    long long bestScore = val[si][sj];\n\n    int cur_i = si, cur_j = sj;\n    while (true) {\n        int bestVal = -1, bestD = -1;\n        for (int d = 0; d < 4; ++d) {\n            int ni = cur_i + di[d], nj = cur_j + dj[d];\n            if (ni < 0 || ni >= N || nj < 0 || nj >= N) continue;\n            int t = tile[ni][nj];\n            if (global_used[t]) continue;\n            if (val[ni][nj] > bestVal) { bestVal = val[ni][nj]; bestD = d; }\n        }\n        if (bestD == -1) break;\n        bestPath.push_back(dir[bestD]);\n        cur_i += di[bestD];\n        cur_j += dj[bestD];\n        global_used[tile[cur_i][cur_j]] = 1;\n        bestScore += val[cur_i][cur_j];\n    }\n\n    Path bestState(si, sj, tile[si][sj], val[si][sj]);\n    bestState.moves = bestPath;\n\n    /*---------------------------------------------------*/\n    /*  2. Beam search (depth\u2011limited)                   */\n    /*---------------------------------------------------*/\n    const int BEAM_WIDTH   = 200;\n    const int BEAM_DEPTH   = 20;\n    const int BEAM_LIMIT_MS = 1100;          // 1.1\u202fs\n\n    auto start = chrono::steady_clock::now();\n\n    while (chrono::duration_cast<chrono::milliseconds>(\n               chrono::steady_clock::now() - start).count() < BEAM_LIMIT_MS) {\n\n        vector<Path> cur;\n        cur.emplace_back(si, sj, tile[si][sj], val[si][sj]);\n\n        for (int step = 0; step < BEAM_DEPTH; ++step) {\n            vector<Path> nxt;\n            nxt.reserve(cur.size() * 4);\n\n            for (auto &p : cur) {\n                for (int d = 0; d < 4; ++d) {\n                    int ni = p.i + di[d], nj = p.j + dj[d];\n                    if (ni < 0 || ni >= N || nj < 0 || nj >= N) continue;\n                    int tid = tile[ni][nj];\n                    if (p.used.test(tid)) continue;\n\n                    Path np = p;          // copy state\n                    np.i = ni; np.j = nj;\n                    np.used.set(tid);\n                    np.score += val[ni][nj];\n                    np.moves.push_back(dir[d]);\n\n                    if (np.score > bestScore) {\n                        bestScore = np.score;\n                        bestPath = np.moves;\n                        bestState = np;\n                    }\n                    nxt.push_back(std::move(np));\n                }\n            }\n            if (nxt.empty()) break;\n\n            sort(nxt.begin(), nxt.end(),\n                 [](const Path &a, const Path &b) { return a.score > b.score; });\n            if ((int)nxt.size() > BEAM_WIDTH) nxt.resize(BEAM_WIDTH);\n            cur.swap(nxt);\n        }\n    }\n\n    /*---------------------------------------------------*/\n    /*  3. Random weighted walks (diversity)             */\n    /*---------------------------------------------------*/\n    const int RND_LIMIT_MS = 1900;\n    mt19937 rng((unsigned)chrono::steady_clock::now()\n                .time_since_epoch().count());\n    const double TEMPERATURE = 30.0;\n\n    while (chrono::duration_cast<chrono::milliseconds>(\n               chrono::steady_clock::now() - start).count() < RND_LIMIT_MS) {\n\n        Path p(si, sj, tile[si][sj], val[si][sj]);\n\n        while (true) {\n            struct Cand { int d, ni, nj, tid, val; double w; };\n            vector<Cand> cand;\n            for (int d = 0; d < 4; ++d) {\n                int ni = p.i + di[d], nj = p.j + dj[d];\n                if (ni < 0 || ni >= N || nj < 0 || nj >= N) continue;\n                int tid = tile[ni][nj];\n                if (p.used.test(tid)) continue;\n                double w = exp((double)val[ni][nj] / TEMPERATURE);\n                cand.push_back({d, ni, nj, tid, val[ni][nj], w});\n            }\n            if (cand.empty()) break;\n\n            double sum = 0;\n            for (auto &c : cand) sum += c.w;\n            uniform_real_distribution<double> urd(0, sum);\n            double r = urd(rng), acc = 0;\n            Cand chosen = cand[0];\n            for (auto &c : cand) {\n                acc += c.w;\n                if (r <= acc) { chosen = c; break; }\n            }\n\n            p.i = chosen.ni; p.j = chosen.nj;\n            p.used.set(chosen.tid);\n            p.score += chosen.val;\n            p.moves.push_back(dir[chosen.d]);\n\n            if (p.score > bestScore) {\n                bestScore = p.score;\n                bestPath = p.moves;\n                bestState = p;\n            }\n        }\n    }\n\n    /*---------------------------------------------------*/\n    /*  4. Local improvement on the last 10 prefixes     */\n    /*---------------------------------------------------*/\n    int maxBack = min(10, (int)bestPath.size());\n    for (int back = 1; back <= maxBack; ++back) {\n        string prefix = bestPath.substr(0, bestPath.size() - back);\n\n        // reconstruct state from prefix\n        Path st(si, sj, tile[si][sj], val[si][sj]);\n        for (char c : prefix) {\n            int d = -1;\n            for (int k = 0; k < 4; ++k)\n                if (dir[k] == c) { d = k; break; }\n            int ni = st.i + di[d], nj = st.j + dj[d];\n            int tid = tile[ni][nj];\n            st.i = ni; st.j = nj;\n            st.used.set(tid);\n            st.score += val[ni][nj];\n            st.moves.push_back(c);\n        }\n\n        // greedy extension from this state\n        Path tmp = st;\n        while (true) {\n            int bestV = -1, bestD = -1;\n            for (int d = 0; d < 4; ++d) {\n                int ni = tmp.i + di[d], nj = tmp.j + dj[d];\n                if (ni < 0 || ni >= N || nj < 0 || nj >= N) continue;\n                int t = tile[ni][nj];\n                if (tmp.used.test(t)) continue;\n                if (val[ni][nj] > bestV) { bestV = val[ni][nj]; bestD = d; }\n            }\n            if (bestD == -1) break;\n            int ni = tmp.i + di[bestD], nj = tmp.j + dj[bestD];\n            int tid = tile[ni][nj];\n            tmp.i = ni; tmp.j = nj;\n            tmp.used.set(tid);\n            tmp.score += val[ni][nj];\n            tmp.moves.push_back(dir[bestD]);\n        }\n\n        if (tmp.score > bestScore) {\n            bestScore = tmp.score;\n            bestPath = tmp.moves;\n            bestState = tmp;\n        }\n    }\n\n    /*---------------------------------------------------*/\n    /*  5. Final greedy extension from the best state    */\n    /*---------------------------------------------------*/\n    Path cur = bestState;\n    while (true) {\n        int bestV = -1, bestD = -1;\n        for (int d = 0; d < 4; ++d) {\n            int ni = cur.i + di[d], nj = cur.j + dj[d];\n            if (ni < 0 || ni >= N || nj < 0 || nj >= N) continue;\n            int t = tile[ni][nj];\n            if (cur.used.test(t)) continue;\n            if (val[ni][nj] > bestV) { bestV = val[ni][nj]; bestD = d; }\n        }\n        if (bestD == -1) break;\n        int ni = cur.i + di[bestD], nj = cur.j + dj[bestD];\n        int tid = tile[ni][nj];\n        cur.i = ni; cur.j = nj;\n        cur.used.set(tid);\n        cur.score += val[ni][nj];\n        cur.moves.push_back(dir[bestD]);\n\n        if (cur.score > bestScore) {\n            bestScore = cur.score;\n            bestPath = cur.moves;\n        }\n    }\n\n    cout << bestPath << '\\n';\n    return 0;\n}","ahc003":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct EdgeInfo {\n    double sum = 0.0;   // cumulative estimated costs\n    int    cnt = 0;     // number of usages\n    double get() const { return cnt ? sum / cnt : 1.0; } // default 1.0\n};\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    const int N = 30;\n    const int V = N * N;\n    const int Q = 1000;\n\n    // edge tables\n    EdgeInfo h[ N ][ N - 1 ];   // horizontal edges (i, j)-(i, j+1)\n    EdgeInfo v[ N - 1 ][ N ];   // vertical   edges (i, j)-(i+1, j)\n\n    // helper lambdas\n    auto node_id = [N](int i, int j){ return i * N + j; };\n\n    for (int qi = 0; qi < Q; ++qi) {\n        int si, sj, ti, tj;\n        if (!(cin >> si >> sj >> ti >> tj)) return 0;\n\n        /* ---------- run Dijkstra with current estimates ---------- */\n        const double INF = 1e100;\n        vector<double> dist(V, INF);\n        vector<int>    prev(V, -1);\n        vector<char>   move(V, 0);   // 'U','D','L','R'\n\n        priority_queue<pair<double,int>, vector<pair<double,int>>, greater<>> pq;\n        int sid = node_id(si, sj);\n        int tid = node_id(ti, tj);\n        dist[sid] = 0.0;\n        pq.emplace(0.0, sid);\n\n        while (!pq.empty()) {\n            auto [d, u] = pq.top(); pq.pop();\n            if (d != dist[u]) continue;\n            if (u == tid) break;\n\n            int ui = u / N, uj = u % N;\n\n            // left\n            if (uj > 0) {\n                int vId = node_id(ui, uj-1);\n                double w = h[ui][uj-1].get();\n                if (dist[vId] > d + w) {\n                    dist[vId] = d + w;\n                    prev[vId] = u;\n                    move[vId] = 'L';\n                    pq.emplace(dist[vId], vId);\n                }\n            }\n            // right\n            if (uj < N-1) {\n                int vId = node_id(ui, uj+1);\n                double w = h[ui][uj].get();\n                if (dist[vId] > d + w) {\n                    dist[vId] = d + w;\n                    prev[vId] = u;\n                    move[vId] = 'R';\n                    pq.emplace(dist[vId], vId);\n                }\n            }\n            // up\n            if (ui > 0) {\n                int vId = node_id(ui-1, uj);\n                double w = v[ui-1][uj].get();\n                if (dist[vId] > d + w) {\n                    dist[vId] = d + w;\n                    prev[vId] = u;\n                    move[vId] = 'U';\n                    pq.emplace(dist[vId], vId);\n                }\n            }\n            // down\n            if (ui < N-1) {\n                int vId = node_id(ui+1, uj);\n                double w = v[ui][uj].get();\n                if (dist[vId] > d + w) {\n                    dist[vId] = d + w;\n                    prev[vId] = u;\n                    move[vId] = 'D';\n                    pq.emplace(dist[vId], vId);\n                }\n            }\n        }\n\n        /* ---------- reconstruct path ---------- */\n        string path;\n        int cur = tid;\n        while (cur != sid) {\n            path.push_back(move[cur]);\n            cur = prev[cur];\n        }\n        reverse(path.begin(), path.end());\n\n        /* ---------- output and flush ---------- */\n        cout << path << '\\n' << flush;\n\n        /* ---------- read judge's reply ---------- */\n        long long x;\n        if (!(cin >> x)) return 0;\n\n        /* ---------- update edge estimates ---------- */\n        int ci = si, cj = sj;\n        double cost_per_edge = static_cast<double>(x) / path.size();\n        for (char mv : path) {\n            if (mv == 'R') {\n                h[ci][cj].sum += cost_per_edge;\n                h[ci][cj].cnt += 1;\n                ++cj;\n            } else if (mv == 'L') {\n                h[ci][cj-1].sum += cost_per_edge;\n                h[ci][cj-1].cnt += 1;\n                --cj;\n            } else if (mv == 'D') {\n                v[ci][cj].sum += cost_per_edge;\n                v[ci][cj].cnt += 1;\n                ++ci;\n            } else if (mv == 'U') {\n                v[ci-1][cj].sum += cost_per_edge;\n                v[ci-1][cj].cnt += 1;\n                --ci;\n            }\n        }\n    }\n    return 0;\n}","ahc004":"#include <bits/stdc++.h>\nusing namespace std;\n\n// ------------------------------------------------------------------\n// Fast splitmix64 RNG\nstruct RNG {\n    unsigned long long state;\n    RNG() : state(chrono::steady_clock::now().time_since_epoch().count()) {}\n\n    unsigned long long next() {\n        unsigned long long z = (state += 0x9e3779b97f4a7c15ULL);\n        z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ULL;\n        z = (z ^ (z >> 27)) * 0x94d049bb133111ebULL;\n        return z ^ (z >> 31);\n    }\n    int randint(int l, int r) { return l + int(next() % (unsigned long long)(r - l + 1)); }\n};\n\n// ------------------------------------------------------------------\n// Rolling hash helpers\nstatic const unsigned long long BASE = 1315423911ULL;   // any odd 64\u2011bit number\n\ninline unsigned long long sub_hash(const vector<unsigned long long>& pref,\n                                   const vector<unsigned long long>& pw,\n                                   int l, int r) {   // [l,r)\n    return pref[r] - pref[l] * pw[r - l];\n}\n\n// ------------------------------------------------------------------\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M;\n    if (!(cin >> N >> M)) return 0;\n    vector<string> S(M);\n    for (int i = 0; i < M; ++i) cin >> S[i];\n\n    // pre\u2011compute powers up to 40\n    vector<unsigned long long> pw(41);\n    pw[0] = 1;\n    for (int i = 1; i <= 40; ++i) pw[i] = pw[i - 1] * BASE;\n\n    // pre\u2011compute hash of each input string\n    vector<unsigned long long> hS(M);\n    for (int idx = 0; idx < M; ++idx) {\n        unsigned long long h = 0;\n        for (char c : S[idx]) h = h * BASE + unsigned(c);\n        hS[idx] = h;\n    }\n\n    RNG rng;\n    const double TIME_LIMIT = 2.8;            // seconds\n    const int MAX_ITER = 5000;\n    auto startAll = chrono::steady_clock::now();\n\n    vector<string> bestMat(N, string(N, '.'));\n    int bestScore = -1;\n\n    unordered_set<unsigned long long> subs;\n    subs.reserve(11000);\n    subs.max_load_factor(0.5);\n\n    // ---------- Random search ----------\n    for (int iter = 0; iter < MAX_ITER; ++iter) {\n        if (chrono::duration<double>(chrono::steady_clock::now() - startAll).count() >= TIME_LIMIT)\n            break;\n\n        // build random matrix\n        vector<string> mat(N, string(N, 'A'));\n        for (int i = 0; i < N; ++i)\n            for (int j = 0; j < N; ++j)\n                mat[i][j] = char('A' + rng.randint(0, 7));\n\n        // build set of all horizontal + vertical substrings\n        subs.clear();\n\n        // rows\n        for (int i = 0; i < N; ++i) {\n            string r2 = mat[i] + mat[i];       // length 40\n            vector<unsigned long long> pref(41);\n            pref[0] = 0;\n            for (int k = 0; k < 40; ++k)\n                pref[k + 1] = pref[k] * BASE + unsigned(r2[k]);\n\n            for (int st = 0; st < N; ++st)\n                for (int len = 2; len <= 12; ++len)\n                    subs.insert(sub_hash(pref, pw, st, st + len));\n        }\n\n        // columns\n        for (int j = 0; j < N; ++j) {\n            string col(N, 'A');\n            for (int i = 0; i < N; ++i) col[i] = mat[i][j];\n            string c2 = col + col;          // length 40\n            vector<unsigned long long> pref(41);\n            pref[0] = 0;\n            for (int k = 0; k < 40; ++k)\n                pref[k + 1] = pref[k] * BASE + unsigned(c2[k]);\n\n            for (int st = 0; st < N; ++st)\n                for (int len = 2; len <= 12; ++len)\n                    subs.insert(sub_hash(pref, pw, st, st + len));\n        }\n\n        // evaluate\n        int score = 0;\n        for (int idx = 0; idx < M; ++idx)\n            if (subs.find(hS[idx]) != subs.end()) ++score;\n\n        if (score > bestScore) {\n            bestScore = score;\n            bestMat   = mat;\n        }\n    }\n\n    // ---------- Simple hill\u2011climbing ----------\n    auto hill_start = chrono::steady_clock::now();\n    const double HILL_LIMIT = 0.4;  // seconds\n    while (chrono::duration<double>(chrono::steady_clock::now() - hill_start).count() < HILL_LIMIT) {\n        vector<string> cand = bestMat;\n\n        // change one random cell\n        int r = rng.randint(0, N - 1);\n        int c = rng.randint(0, N - 1);\n        cand[r][c] = char('A' + rng.randint(0, 7));\n\n        // build set for this candidate\n        subs.clear();\n\n        // rows\n        for (int i = 0; i < N; ++i) {\n            string r2 = cand[i] + cand[i];\n            vector<unsigned long long> pref(41);\n            pref[0] = 0;\n            for (int k = 0; k < 40; ++k)\n                pref[k + 1] = pref[k] * BASE + unsigned(r2[k]);\n\n            for (int st = 0; st < N; ++st)\n                for (int len = 2; len <= 12; ++len)\n                    subs.insert(sub_hash(pref, pw, st, st + len));\n        }\n\n        // columns\n        for (int j = 0; j < N; ++j) {\n            string col(N, 'A');\n            for (int i = 0; i < N; ++i) col[i] = cand[i][j];\n            string c2 = col + col;\n            vector<unsigned long long> pref(41);\n            pref[0] = 0;\n            for (int k = 0; k < 40; ++k)\n                pref[k + 1] = pref[k] * BASE + unsigned(c2[k]);\n\n            for (int st = 0; st < N; ++st)\n                for (int len = 2; len <= 12; ++len)\n                    subs.insert(sub_hash(pref, pw, st, st + len));\n        }\n\n        // evaluate\n        int score = 0;\n        for (int idx = 0; idx < M; ++idx)\n            if (subs.find(hS[idx]) != subs.end()) ++score;\n\n        if (score > bestScore) {\n            bestScore = score;\n            bestMat   = cand;\n        }\n    }\n\n    // ---------- Output ----------\n    for (int i = 0; i < N; ++i)\n        cout << bestMat[i] << '\\n';\n    return 0;\n}","ahc005":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    int N, si, sj;\n    if (!(cin >> N >> si >> sj)) return 0;\n    vector<string> g(N);\n    for (int i = 0; i < N; ++i) cin >> g[i];\n\n    auto inb = [&](int x, int y) { return 0 <= x && x < N && 0 <= y && y < N; };\n\n    const char dirs[4] = {'U', 'D', 'L', 'R'};\n    const int dx[4] = {-1, 1, 0, 0};\n    const int dy[4] = {0, 0, -1, 1};\n\n    // map each road cell to an index\n    vector<vector<int>> idx(N, vector<int>(N, -1));\n    vector<pair<int,int>> pos;\n    int cnt = 0;\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < N; ++j)\n            if (g[i][j] != '#') {\n                idx[i][j] = cnt++;\n                pos.emplace_back(i, j);\n            }\n\n    if (cnt == 0) {               // no road - trivial output\n        cout << '\\n';\n        return 0;\n    }\n\n    // adjacency list\n    vector<vector<int>> adj(cnt);\n    for (int id = 0; id < cnt; ++id) {\n        auto [x, y] = pos[id];\n        for (int d = 0; d < 4; ++d) {\n            int nx = x + dx[d], ny = y + dy[d];\n            if (inb(nx, ny) && idx[nx][ny] != -1)\n                adj[id].push_back(idx[nx][ny]);\n        }\n    }\n\n    int start = idx[si][sj];\n    vector<char> ans;\n\n    // visited mask\n    vector<bool> visited(cnt, false);\n    visited[start] = true;\n    int cur = start;\n\n    // helper lambda: BFS from src, returns dist and parent\n    auto bfs = [&](int src, vector<int> &dist, vector<int> &parent) {\n        dist.assign(cnt, -1);\n        parent.assign(cnt, -1);\n        deque<int> q;\n        dist[src] = 0;\n        q.push_back(src);\n        while (!q.empty()) {\n            int u = q.front();\n            q.pop_front();\n            for (int v : adj[u]) {\n                if (dist[v] == -1) {\n                    dist[v] = dist[u] + 1;\n                    parent[v] = u;\n                    q.push_back(v);\n                }\n            }\n        }\n    };\n\n    // main loop: always go to the nearest unvisited node\n    while (true) {\n        int remaining = 0;\n        for (bool v : visited) if (!v) ++remaining;\n        if (remaining == 0) break;\n\n        vector<int> dist, parent;\n        bfs(cur, dist, parent);\n\n        int target = -1;\n        int best = INT_MAX;\n        for (int v = 0; v < cnt; ++v) {\n            if (!visited[v] && dist[v] != -1 && dist[v] < best) {\n                best = dist[v];\n                target = v;\n            }\n        }\n        if (target == -1) break; // should not happen\n\n        // reconstruct path from cur to target\n        vector<int> path;\n        int p = target;\n        while (p != -1) {\n            path.push_back(p);\n            if (p == cur) break;\n            p = parent[p];\n        }\n        reverse(path.begin(), path.end()); // from cur to target\n\n        // emit moves along the path\n        for (size_t i = 1; i < path.size(); ++i) {\n            auto [x1, y1] = pos[path[i-1]];\n            auto [x2, y2] = pos[path[i]];\n            int d = -1;\n            if (x2 == x1-1 && y2 == y1) d = 0;\n            else if (x2 == x1+1 && y2 == y1) d = 1;\n            else if (x2 == x1 && y2 == y1-1) d = 2;\n            else if (x2 == x1 && y2 == y1+1) d = 3;\n            if (d != -1) ans.push_back(dirs[d]);\n            visited[path[i]] = true;\n        }\n        cur = target;\n    }\n\n    // finally return to start\n    vector<int> dist, parent;\n    bfs(cur, dist, parent);\n    vector<int> path;\n    int p = start;\n    while (p != -1) {\n        path.push_back(p);\n        if (p == cur) break;\n        p = parent[p];\n    }\n    reverse(path.begin(), path.end()); // cur -> start\n\n    for (size_t i = 1; i < path.size(); ++i) {\n        auto [x1, y1] = pos[path[i-1]];\n        auto [x2, y2] = pos[path[i]];\n        int d = -1;\n        if (x2 == x1-1 && y2 == y1) d = 0;\n        else if (x2 == x1+1 && y2 == y1) d = 1;\n        else if (x2 == x1 && y2 == y1-1) d = 2;\n        else if (x2 == x1 && y2 == y1+1) d = 3;\n        if (d != -1) ans.push_back(dirs[d]);\n    }\n\n    string out(ans.begin(), ans.end());\n    cout << out << '\\n';\n    return 0;\n}","future-contest-2022-qual":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    int N, M, K, R;\n    if (!(cin >> N >> M >> K >> R)) return 0;\n\n    /* read task vectors: keep only the sum of skill levels (sum_d) */\n    vector<long long> sum_d(N, 0);\n    for (int i = 0; i < N; ++i) {\n        long long s = 0;\n        for (int j = 0; j < K; ++j) {\n            int x; cin >> x;\n            s += x;\n        }\n        sum_d[i] = s;\n    }\n\n    /* dependency graph */\n    vector<vector<int>> out(N);\n    vector<int> indeg(N, 0);\n    for (int i = 0; i < R; ++i) {\n        int u, v; cin >> u >> v;            // 1\u2011based\n        --u; --v;\n        out[u].push_back(v);\n        ++indeg[v];\n    }\n\n    /* longest weighted path (critical\u2011path distance) */\n    vector<long long> dist(N, 0);\n    for (int i = N - 1; i >= 0; --i) {\n        long long best = 0;\n        for (int v : out[i]) best = max(best, dist[v]);\n        dist[i] = sum_d[i] + best;\n    }\n\n    /* priority for ready tasks: larger is better   (dist + sum_d)  */\n    auto cmpTask = [&](int a, int b) {\n        long long pa = dist[a] + sum_d[a];\n        long long pb = dist[b] + sum_d[b];\n        if (pa != pb) return pa < pb;   // max\u2011heap\n        return a < b;                    // tie breaker\n    };\n    priority_queue<int, vector<int>, decltype(cmpTask)> ready(cmpTask);\n\n    for (int i = 0; i < N; ++i)\n        if (indeg[i] == 0) ready.push(i);\n\n    /* worker status */\n    vector<int> worker_task(M, -1);        // current task id, -1 if idle\n    vector<long long> sum_t(M, 0);         // total time finished\n    vector<long long> sum_d_done(M, 0);    // total difficulty of finished tasks\n    vector<int> start_day(N, -1);\n    vector<char> started(N, 0), finished(N, 0);\n\n    int day = 0;\n    while (true) {\n        ++day;\n\n        /* ---- assign ready tasks to idle workers ---- */\n        vector<int> idle_workers;\n        for (int w = 0; w < M; ++w)\n            if (worker_task[w] == -1) idle_workers.push_back(w);\n\n        /* worker efficiency: avg time per unit difficulty (smaller better) */\n        auto eff = [&](int w) -> double {\n            if (sum_d_done[w] <= 0) return 1e12; // unknown -> very slow\n            return 1.0 * sum_t[w] / sum_d_done[w];\n        };\n        sort(idle_workers.begin(), idle_workers.end(),\n             [&](int a, int b) { return eff(a) < eff(b); });\n\n        vector<pair<int,int>> assignments;\n        for (int w : idle_workers) {\n            if (ready.empty()) break;\n            int t = ready.top(); ready.pop();\n            worker_task[w] = t;\n            started[t] = 1;\n            start_day[t] = day;\n            assignments.emplace_back(w, t);\n        }\n\n        /* output assignments for this day */\n        cout << assignments.size();\n        for (auto &p : assignments)\n            cout << ' ' << (p.first + 1) << ' ' << (p.second + 1);\n        cout << '\\n';\n        cout.flush();\n\n        /* ---- read finished workers this day ---- */\n        int nfin;\n        if (!(cin >> nfin)) break;           // safety\n        if (nfin == -1) break;               // finished or 2000 days\n\n        vector<int> fin_list(nfin);\n        for (int i = 0; i < nfin; ++i) cin >> fin_list[i];\n\n        for (int fid : fin_list) {\n            int w = fid - 1;\n            int t = worker_task[w];           // the task that ended\n\n            int duration = day - start_day[t] + 1;   // actual length\n\n            sum_t[w] += duration;\n            sum_d_done[w] += sum_d[t];\n            worker_task[w] = -1;                // worker becomes idle\n\n            for (int v : out[t]) {\n                if (--indeg[v] == 0 && !started[v] && !finished[v])\n                    ready.push(v);\n            }\n            finished[t] = 1;\n        }\n    }\n    return 0;\n}","ahc006":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Order {\n    int a, b, c, d;\n};\n\ninline long long manhattan(int x1, int y1, int x2, int y2) {\n    return llabs(x1 - x2) + llabs(y1 - y2);\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    const int N = 1000;               // number of orders\n    vector<Order> ord(N);\n    for (int i = 0; i < N; ++i)\n        cin >> ord[i].a >> ord[i].b >> ord[i].c >> ord[i].d;\n\n    /* ---- 1. choose 50 cheapest orders --------------------------------- */\n    struct Item { long long cost; int idx; };\n    vector<Item> items;\n    items.reserve(N);\n    for (int i = 0; i < N; ++i) {\n        long long cost = manhattan(400, 400, ord[i].a, ord[i].b) +\n                         manhattan(ord[i].a, ord[i].b, ord[i].c, ord[i].d);\n        items.push_back({cost, i});\n    }\n    sort(items.begin(), items.end(),\n         [](const Item& x, const Item& y){ return x.cost < y.cost; });\n\n    const int M = 50;                    // number to select\n    vector<int> chosen;\n    chosen.reserve(M);\n    for (int i = 0; i < M; ++i) chosen.push_back(items[i].idx);\n\n    /* ---- 2. nearest\u2011neighbour ordering --------------------------------- */\n    vector<int> seq; seq.reserve(M);\n    vector<bool> used(M, false);\n    int cur_x = 400, cur_y = 400;\n\n    for (int k = 0; k < M; ++k) {\n        int best = -1;\n        long long best_dist = LLONG_MAX;\n        for (int t = 0; t < M; ++t) if (!used[t]) {\n            int idx = chosen[t];\n            long long d = manhattan(cur_x, cur_y, ord[idx].a, ord[idx].b);\n            if (d < best_dist) {\n                best_dist = d;\n                best = t;\n            }\n        }\n        used[best] = true;\n        seq.push_back(chosen[best]);\n        cur_x = ord[chosen[best]].c;\n        cur_y = ord[chosen[best]].d;\n    }\n\n    /* helper to compute total travelling time of a given sequence */\n    auto total_cost = [&](const vector<int>& s) -> long long {\n        long long t = 0;\n        if (s.empty()) return t;\n        t += manhattan(400, 400, ord[s[0]].a, ord[s[0]].b);\n        t += manhattan(ord[s[0]].a, ord[s[0]].b, ord[s[0]].c, ord[s[0]].d);\n        for (size_t i = 1; i < s.size(); ++i) {\n            int prev = s[i-1], cur = s[i];\n            t += manhattan(ord[prev].c, ord[prev].d, ord[cur].a, ord[cur].b);\n            t += manhattan(ord[cur].a, ord[cur].b, ord[cur].c, ord[cur].d);\n        }\n        int last = s.back();\n        t += manhattan(ord[last].c, ord[last].d, 400, 400);\n        return t;\n    };\n\n    long long best_cost = total_cost(seq);\n\n    /* ---- 3. simple 2\u2011opt improvement --------------------------------- */\n    bool improved = true;\n    while (improved) {\n        improved = false;\n        for (int i = 0; i < M - 1; ++i) {\n            for (int j = i + 1; j < M; ++j) {\n                vector<int> cand = seq;\n                swap(cand[i], cand[j]);\n                long long cur_cost = total_cost(cand);\n                if (cur_cost < best_cost) {\n                    seq.swap(cand);\n                    best_cost = cur_cost;\n                    improved = true;\n                }\n            }\n        }\n    }\n\n    /* ---- 4. build the final route ------------------------------------ */\n    vector<pair<int,int>> route;\n    route.emplace_back(400, 400);            // start\n    for (int idx : seq) {\n        route.emplace_back(ord[idx].a, ord[idx].b);\n        route.emplace_back(ord[idx].c, ord[idx].d);\n    }\n    route.emplace_back(400, 400);            // return\n\n    /* ---- 5. output ----------------------------------------------------- */\n    cout << M;\n    for (int idx : seq) cout << ' ' << (idx + 1);   // 1\u2011based indices\n    cout << '\\n';\n\n    cout << route.size();\n    for (auto [x, y] : route) cout << ' ' << x << ' ' << y;\n    cout << '\\n';\n\n    return 0;\n}","ahc007":"#include <bits/stdc++.h>\nusing namespace std;\n\n// ---------- DSU ----------\nstruct DSU {\n    vector<int> p, r;\n    DSU(int n = 0) { init(n); }\n    void init(int n) {\n        p.resize(n);\n        r.assign(n, 0);\n        iota(p.begin(), p.end(), 0);\n    }\n    int find(int x) {\n        if (p[x] == x) return x;\n        return p[x] = find(p[x]);            // path compression\n    }\n    bool unite(int a, int b) {\n        a = find(a); b = find(b);\n        if (a == b) return false;\n        if (r[a] < r[b]) swap(a, b);\n        p[b] = a;\n        if (r[a] == r[b]) ++r[a];\n        return true;\n    }\n};\n\n// ---------- main ----------\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    const int N = 400, M = 1995;\n    // read coordinates, but we never use them\n    for (int i = 0; i < N; ++i) {\n        int x, y; cin >> x >> y;\n    }\n\n    vector<int> u(M), v(M);\n    for (int i = 0; i < M; ++i) {\n        cin >> u[i] >> v[i];\n    }\n\n    DSU dsu(N);\n\n    for (int i = 0; i < M; ++i) {\n        int li;            // the real length\n        cin >> li;\n\n        if (dsu.unite(u[i], v[i])) {\n            cout << 1 << '\\n';\n        } else {\n            cout << 0 << '\\n';\n        }\n        cout.flush();        // essential for interactive judge\n    }\n    return 0;\n}","ahc008":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int H = 30;\nconst int W = 30;\n\nstruct Pos{int r, c;};\n\nint main(){\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N;\n    if(!(cin >> N)) return 0;\n    vector<Pos> pets(N);\n    for(int i = 0; i < N; ++i){\n        int x, y, t;               // read type too\n        cin >> x >> y >> t;\n        pets[i] = {x-1, y-1};       // 0\u2011based\n    }\n\n    int M;\n    cin >> M;\n    vector<Pos> humans(M);\n    for(int i = 0; i < M; ++i){\n        int x, y;\n        cin >> x >> y;\n        humans[i] = {x-1, y-1};\n    }\n\n    /* grid states */\n    bool imp[H][W] = {};           // impassable\n    bool human[H][W] = {};         // human presence\n    bool pet[H][W] = {};           // at least one pet present\n\n    for(auto &p: pets) pet[p.r][p.c] = true;\n    for(auto &h: humans) human[h.r][h.c] = true;\n\n    const int d4[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};\n    const char blockC[4] = {'u','d','l','r'};\n    // helper\n    auto inb = [&](int r, int c){ return 0<=r && r<H && 0<=c && c<W; };\n\n    auto canBlock = [&](int r, int c)->bool{\n        if(imp[r][c] || human[r][c] || pet[r][c]) return false;\n        for(int dr=-1; dr<=1; ++dr){\n            for(int dc=-1; dc<=1; ++dc){\n                if(dr==0 && dc==0) continue;\n                int nr=r+dr, nc=c+dc;\n                if(!inb(nr,nc)) continue;\n                if(pet[nr][nc]) return false;\n            }\n        }\n        return true;\n    };\n\n    for(int turn=0; turn<300; ++turn){\n        string actions(M, '.');\n        for(int i=0;i<M;++i){\n            Pos &h = humans[i];\n            bool done=false;\n            for(int d=0; d<4; ++d){\n                int nr = h.r + d4[d][0];\n                int nc = h.c + d4[d][1];\n                if(!inb(nr,nc)) continue;\n                if(canBlock(nr,nc)){\n                    actions[i] = blockC[d];\n                    imp[nr][nc] = true;  // becomes impassable for this turn\n                    done=true;\n                    break;\n                }\n            }\n        }\n\n        cout << actions << '\\n' << flush;\n\n        /* read pet movements */\n        vector<string> mv(N);\n        for(int i=0;i<N;++i) cin >> mv[i];\n\n        /* reset pet grid */\n        memset(pet,0,sizeof(pet));\n\n        /* move pets */\n        for(int i=0;i<N;++i){\n            Pos &p = pets[i];\n            for(char c: mv[i]){\n                int d = (c=='U'?0:(c=='D'?1:(c=='L'?2:3)));\n                int nr = p.r + d4[d][0];\n                int nc = p.c + d4[d][1];\n                if(!inb(nr,nc)) continue;   // safety, should not happen\n                if(imp[nr][nc]) continue;  // blocked\n                p.r = nr;\n                p.c = nc;\n            }\n            pet[p.r][p.c] = true;\n        }\n        /* humans are never moved in this simple strategy */\n    }\n    return 0;\n}","ahc009":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Node { int r, c; };\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int sr, sc, tr, tc;\n    double p;                 // not used in the construction\n    if (!(cin >> sr >> sc >> tr >> tc >> p)) return 0;\n\n    vector<string> h(20);\n    for (int i = 0; i < 20; ++i) cin >> h[i];\n    vector<string> v(19);\n    for (int i = 0; i < 19; ++i) cin >> v[i];\n\n    const int N = 20;\n    vector<vector<bool>> seen(N, vector<bool>(N, false));\n    vector<vector<int>> pr(N, vector<int>(N, -1));\n    vector<vector<int>> pc(N, vector<int>(N, -1));\n    vector<vector<char>> pd(N, vector<char>(N, 0));\n\n    queue<Node> q;\n    q.push({sr, sc});\n    seen[sr][sc] = true;\n\n    const int dr[4] = {-1, 1, 0, 0};\n    const int dc[4] = {0, 0, -1, 1};\n    const char dirc[4] = {'U', 'D', 'L', 'R'};\n\n    /* BFS for shortest path to the office */\n    while (!q.empty()) {\n        Node cur = q.front(); q.pop();\n        if (cur.r == tr && cur.c == tc) break;\n\n        for (int k = 0; k < 4; ++k) {\n            int nr = cur.r + dr[k];\n            int nc = cur.c + dc[k];\n            if (nr < 0 || nr >= N || nc < 0 || nc >= N) continue;\n\n            bool blocked = false;\n            if (dr[k] == -1)        blocked = (v[nr][nc] == '1');      // up\n            else if (dr[k] == 1)   blocked = (v[cur.r][cur.c] == '1'); // down\n            else if (dc[k] == -1)  blocked = (h[cur.r][nc] == '1');   // left\n            else                   blocked = (h[cur.r][cur.c] == '1'); // right\n\n            if (blocked || seen[nr][nc]) continue;\n            seen[nr][nc] = true;\n            pr[nr][nc] = cur.r;\n            pc[nr][nc] = cur.c;\n            pd[nr][nc] = dirc[k];\n            q.push({nr, nc});\n        }\n    }\n\n    /* reconstruct the shortest path P (start \u2192 office) */\n    vector<char> P;\n    int cr = tr, cc = tc;\n    while (!(cr == sr && cc == sc)) {\n        P.push_back(pd[cr][cc]);          // direction from parent to this cell\n        int nr = pr[cr][cc];\n        int nc = pc[cr][cc];\n        cr = nr; cc = nc;\n    }\n    reverse(P.begin(), P.end());         // forward path\n\n    /* robust forward path P\u2082 : each step twice */\n    vector<char> P2;\n    P2.reserve(P.size() * 2);\n    for (char c : P) {\n        P2.push_back(c);\n        P2.push_back(c);\n    }\n\n    /* robust reverse path R\u2082 : reverse of P\u2082 with opposite directions */\n    vector<char> R2;\n    R2.reserve(P2.size());\n    for (auto it = P2.rbegin(); it != P2.rend(); ++it) {\n        char c = *it;\n        char oc = (c == 'U') ? 'D' :\n                  (c == 'D') ? 'U' :\n                  (c == 'L') ? 'R' : 'L';\n        R2.push_back(oc);\n    }\n\n    int lenP2 = (int)P2.size();          // always <= 120\n    int k = (200 / lenP2 - 1) / 2;       // maximal number of full cycles\n    if (k < 0) k = 0;\n\n    string answer;\n    answer.reserve((2 * k + 1) * lenP2);\n    answer.append(P2.begin(), P2.end());\n    for (int i = 0; i < k; ++i) {\n        answer.append(R2.begin(), R2.end());\n        answer.append(P2.begin(), P2.end());\n    }\n\n    cout << answer << '\\n';\n    return 0;\n}","ahc010":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    // read and ignore the 30\u00d730 input grid\n    string line;\n    for (int i = 0; i < 30; ++i) {\n        if (!(cin >> line)) return 0;\n    }\n\n    // produce a deterministic rotation pattern\n    string ans;\n    ans.reserve(900);\n    for (int i = 0; i < 30; ++i) {\n        for (int j = 0; j < 30; ++j) {\n            int r = (i + j) % 4;          // 0 .. 3\n            ans.push_back(char('0' + r));\n        }\n    }\n\n    cout << ans << '\\n';\n    return 0;\n}","ahc011":"#include <bits/stdc++.h>\nusing namespace std;\n\n// ------------------------------------------------------------\n//  DSU with number of vertices and edges\nstruct DSU {\n    vector<int> p, sz, ed;\n    DSU(int n = 0) { init(n); }\n    void init(int n) {\n        p.resize(n);\n        sz.assign(n, 1);\n        ed.assign(n, 0);\n        iota(p.begin(), p.end(), 0);\n    }\n    int find(int x) { return p[x] == x ? x : p[x] = find(p[x]); }\n    void unite(int a, int b) {\n        a = find(a); b = find(b);\n        if (a == b) { ed[a]++; return; }\n        if (sz[a] < sz[b]) swap(a, b);\n        p[b] = a;\n        sz[a] += sz[b];\n        ed[a] += ed[b] + 1;\n    }\n};\n\n// ------------------------------------------------------------\n//  convert hex char to line mask\ninline int hexMask(char c) {\n    if ('0' <= c && c <= '9') return c - '0';\n    return 10 + (c - 'a');\n}\n\n//  largest tree size on the board, empty position (ei,ej)\nint largestTree(const vector<string>& g, int ei, int ej, int N) {\n    int MN = N * N;\n    DSU dsu(MN);\n    vector<bool> empty(MN, false);\n    empty[ei * N + ej] = true;\n\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < N; ++j) {\n            int idx = i * N + j;\n            if (empty[idx]) continue;\n            int m = hexMask(g[i][j]);\n\n            // right neighbour\n            if (j + 1 < N) {\n                int ni = i, nj = j + 1, nidx = ni * N + nj;\n                if (!empty[nidx]) {\n                    int nm = hexMask(g[ni][nj]);\n                    if ((m & 4) && (nm & 1))\n                        dsu.unite(idx, nidx);\n                }\n            }\n            // down neighbour\n            if (i + 1 < N) {\n                int ni = i + 1, nj = j, nidx = ni * N + nj;\n                if (!empty[nidx]) {\n                    int nm = hexMask(g[ni][nj]);\n                    if ((m & 8) && (nm & 2))\n                        dsu.unite(idx, nidx);\n                }\n            }\n        }\n\n    int best = 0;\n    for (int idx = 0; idx < MN; ++idx)\n        if (dsu.p[idx] == idx && !empty[idx])\n            if (dsu.ed[idx] == dsu.sz[idx] - 1)\n                best = max(best, dsu.sz[idx]);\n    return best;\n}\n\n//  try to move in direction (di,dj); returns true if successful\nbool slide(vector<string>& g, int& ei, int& ej, int di, int dj) {\n    int ni = ei + di, nj = ej + dj;\n    if (ni < 0 || ni >= (int)g.size() || nj < 0 || nj >= (int)g[0].size())\n        return false;\n    g[ei][ej] = g[ni][nj];\n    g[ni][nj] = '0';\n    ei = ni; ej = nj;\n    return true;\n}\n\n// ------------------------------------------------------------\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, T;\n    if (!(cin >> N >> T)) return 0;\n    vector<string> initG(N);\n    int ei0 = -1, ej0 = -1;\n    for (int i = 0; i < N; ++i) {\n        cin >> initG[i];\n        for (int j = 0; j < N; ++j)\n            if (initG[i][j] == '0') { ei0 = i; ej0 = j; }\n    }\n\n    const int RUNS = 200;                 // number of random walks\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n\n    string bestSeq = \"\";\n    int bestScore = 0;\n\n    const int di[4] = {-1, 1, 0, 0};\n    const int dj[4] = {0, 0, -1, 1};\n    const char ch[4] = {'U','D','L','R'};\n\n    for (int run = 0; run < RUNS; ++run) {\n        vector<string> g = initG;\n        int ei = ei0, ej = ej0;\n        string seq = \"\";\n        int curScore = largestTree(g, ei, ej, N);\n\n        for (int step = 0; step < T; ++step) {\n            // collect legal moves\n            int legal[4], cnt = 0;\n            for (int d = 0; d < 4; ++d) {\n                int ni = ei + di[d], nj = ej + dj[d];\n                if (ni >= 0 && ni < N && nj >= 0 && nj < N)\n                    legal[cnt++] = d;\n            }\n            if (cnt == 0) break;          // no move possible, should not happen\n\n            int d = legal[rng() % cnt];\n            slide(g, ei, ej, di[d], dj[d]);\n            seq.push_back(ch[d]);\n\n            curScore = largestTree(g, ei, ej, N);\n            if (curScore == N * N - 1) break;   // full tree -> optimal\n        }\n\n        if (curScore > bestScore) {\n            bestScore = curScore;\n            bestSeq = seq;\n        }\n    }\n\n    cout << bestSeq << '\\n';\n    return 0;\n}","ahc012":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, K;\n    if (!(cin >> N >> K)) return 0;\n\n    // read the a_d (10 numbers)\n    for (int i = 0; i < 10; ++i) {\n        int tmp; cin >> tmp;\n    }\n    // read all strawberry coordinates\n    for (int i = 0; i < N; ++i) {\n        int x, y; cin >> x >> y;\n    }\n\n    const long long LIM = 1000000000LL;\n    vector<array<long long,4>> lines;\n\n    const int M = 50;               // number of vertical lines\n    const int N_h = 50;             // number of horizontal lines\n    const long long STEP = 400LL;    // 20000 / 50\n    const long long START_XY = -10000LL + STEP;   // -9600\n\n    // vertical lines\n    for (int i = 0; i < M; ++i) {\n        long long x = START_XY + i * STEP;\n        lines.push_back({x, -LIM, x, LIM});\n    }\n\n    // horizontal lines\n    for (int i = 0; i < N_h; ++i) {\n        long long y = START_XY + i * STEP;\n        lines.push_back({-LIM, y, LIM, y});\n    }\n\n    cout << lines.size() << '\\n';\n    for (auto &l : lines) {\n        cout << l[0] << ' ' << l[1] << ' ' << l[2] << ' ' << l[3] << '\\n';\n    }\n    return 0;\n}","ahc014":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Op {\n    int x1, y1, x2, y2, x3, y3, x4, y4;\n};\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    int N, M;\n    if (!(cin >> N >> M)) return 0;\n\n    vector<vector<char>> dot(N, vector<char>(N, 0));\n    for (int i = 0; i < M; ++i) {\n        int x, y; cin >> x >> y;\n        dot[x][y] = 1;\n    }\n\n    // unit segments\n    vector<vector<char>> hor(N, vector<char>(N-1, 0));\n    vector<vector<char>> ver(N-1, vector<char>(N, 0));\n    vector<vector<char>> dpos(N-1, vector<char>(N-1, 0));   // (x,y) -> (x+1,y+1)\n    vector<vector<char>> dneg(N-1, vector<char>(N-1, 0));   // (x,y+1) -> (x+1,y)\n\n    auto weight = [&](int x, int y)->long long {\n        long long dx = x - (N-1)/2;\n        long long dy = y - (N-1)/2;\n        return dx*dx + dy*dy + 1;\n    };\n\n    vector<Op> ops;\n\n    while (true) {\n        long long bestW = -1;\n        bool bestIsSquare = false;\n        int bestSqX=0,bestSqY=0,bestSqMissing=0;\n        int bestDiaX=0,bestDiaY=0,bestDiaMissing=0;\n\n        /* ---- squares ---- */\n        for (int x=0;x+1<N;++x) for (int y=0;y+1<N;++y) {\n            // check if all four edges free\n            if (hor[y][x] || hor[y+1][x] || ver[y][x] || ver[y][x+1]) continue;\n            int empties=0, midx=-1;\n            // vertices indices: 0:(x,y),1:(x+1,y),2:(x,y+1),3:(x+1,y+1)\n            for (int k=0;k<4;++k) {\n                int cx = x + (k==1||k==3);\n                int cy = y + (k==2||k==3);\n                if (!dot[cx][cy]) { ++empties; midx=k; }\n            }\n            if (empties!=1) continue;\n            // weight of the new dot\n            int nx,ny;\n            if (midx==0) { nx=x; ny=y; }\n            else if (midx==1) { nx=x+1; ny=y; }\n            else if (midx==2) { nx=x; ny=y+1; }\n            else { nx=x+1; ny=y+1; }\n            long long w = weight(nx,ny);\n            if (w > bestW) {\n                bestW = w;\n                bestIsSquare = true;\n                bestSqX = x; bestSqY = y; bestSqMissing = midx;\n            }\n        }\n\n        /* ---- diamonds ---- */\n        for (int x=0; x+2<N; ++x)     // base lower-left corner of the tiny square\n            for (int y=1; y+1<N; ++y) {\n                // corners: 0:(x,y),1:(x+1,y+1),2:(x+2,y),3:(x+1,y-1)\n                if (dpos[x][y]||dneg[x+1][y]||dpos[x+1][y-1]||dneg[x][y-1]) continue;\n                int empties=0, midx=-1;\n                int cx[4] = {x, x+1, x+2, x+1};\n                int cy[4] = {y, y+1, y,   y-1};\n                for (int k=0;k<4;++k) {\n                    if (!dot[cx[k]][cy[k]]) { ++empties; midx=k; }\n                }\n                if (empties!=1) continue;\n                int nx=cx[midx], ny=cy[midx];\n                long long w = weight(nx,ny);\n                if (w > bestW) {\n                    bestW = w;\n                    bestIsSquare = false;\n                    bestDiaX = x; bestDiaY = y; bestDiaMissing = midx;\n                }\n            }\n\n        if (bestW < 0) break;          // no operation possible\n\n        /* --- execute the chosen operation --- */\n        if (bestIsSquare) {\n            int x = bestSqX, y = bestSqY, midx = bestSqMissing;\n            int nx,ny;\n            if (midx==0) { nx=x; ny=y; }\n            else if (midx==1) { nx=x+1; ny=y; }\n            else if (midx==2) { nx=x; ny=y+1; }\n            else { nx=x+1; ny=y+1; }\n\n            // mark edges\n            hor[y][x] = hor[y+1][x] = ver[y][x] = ver[y][x+1] = 1;\n            dot[nx][ny] = 1;\n\n            // build operation order: clockwise around the square\n            int order[4];\n            // cw cycle 0,1,3,2\n            int posInCw[4] = {0,1,3,2};\n            for (int t=0;t<4;++t) {\n                int idx = 4 == 0 ? 0 : 0; // dummy\n            }\n            vector<int> cw = {0,1,3,2};\n            int startPos = posInCw[midx];\n            vector<int> cyc(4);\n            for (int t=0;t<4;++t)\n                cyc[t] = cw[(startPos + t)%4];\n\n            Op op;\n            op.x1 = nx; op.y1 = ny;\n            op.x2 = x + (cyc[1]==1||cyc[1]==3);\n            op.y2 = y + (cyc[1]==2||cyc[1]==3);\n            op.x3 = x + (cyc[2]==1||cyc[2]==3);\n            op.y3 = y + (cyc[2]==2||cyc[2]==3);\n            op.x4 = x + (cyc[3]==1||cyc[3]==3);\n            op.y4 = y + (cyc[3]==2||cyc[3]==3);\n            ops.push_back(op);\n        } else { // diamond\n            int x = bestDiaX, y = bestDiaY, midx = bestDiaMissing;\n            int cx[4] = {x, x+1, x+2, x+1};\n            int cy[4] = {y, y+1, y,   y-1};\n            int nx = cx[midx], ny = cy[midx];\n\n            // mark diagonal segments\n            dpos[x][y] = dneg[x+1][y] = dpos[x+1][y-1] = dneg[x][y-1] = 1;\n            dot[nx][ny] = 1;\n\n            // order: 0,1,2,3 is clockwise\n            int cyc[4];\n            int startPos = midx;\n            for (int t=0;t<4;++t) cyc[t] = (startPos + t)%4;\n\n            Op op;\n            op.x1 = nx; op.y1 = ny;\n            op.x2 = cx[cyc[1]];\n            op.y2 = cy[cyc[1]];\n            op.x3 = cx[cyc[2]];\n            op.y3 = cy[cyc[2]];\n            op.x4 = cx[cyc[3]];\n            op.y4 = cy[cyc[3]];\n            ops.push_back(op);\n        }\n    }\n\n    /* output */\n    cout << ops.size() << '\\n';\n    for (auto &op: ops)\n        cout << op.x1 << ' ' << op.y1 << ' '\n             << op.x2 << ' ' << op.y2 << ' '\n             << op.x3 << ' ' << op.y3 << ' '\n             << op.x4 << ' ' << op.y4 << '\\n';\n    return 0;\n}","ahc015":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Pos { int r, c; };\n\nstatic inline void tilt(vector<vector<int>>& g, char dir) {\n    if (dir == 'F') {                 // up\n        for (int c = 0; c < 10; ++c) {\n            vector<int> v;\n            for (int r = 0; r < 10; ++r)\n                if (g[r][c]) v.push_back(g[r][c]);\n            for (int r = 0; r < 10; ++r)\n                g[r][c] = (r < (int)v.size() ? v[r] : 0);\n        }\n    } else if (dir == 'B') {          // down\n        for (int c = 0; c < 10; ++c) {\n            vector<int> v;\n            for (int r = 9; r >= 0; --r)\n                if (g[r][c]) v.push_back(g[r][c]);\n            for (int r = 9, idx = 0; r >= 0; --r, ++idx)\n                g[r][c] = (idx < (int)v.size() ? v[idx] : 0);\n        }\n    } else if (dir == 'L') {          // left\n        for (int r = 0; r < 10; ++r) {\n            vector<int> v;\n            for (int c = 0; c < 10; ++c)\n                if (g[r][c]) v.push_back(g[r][c]);\n            for (int c = 0; c < 10; ++c)\n                g[r][c] = (c < (int)v.size() ? v[c] : 0);\n        }\n    } else {                          // 'R'\n        for (int r = 0; r < 10; ++r) {\n            vector<int> v;\n            for (int c = 9; c >= 0; --c)\n                if (g[r][c]) v.push_back(g[r][c]);\n            for (int c = 9, idx = 0; c >= 0; --c, ++idx)\n                g[r][c] = (idx < (int)v.size() ? v[idx] : 0);\n        }\n    }\n}\n\nstatic inline long long connectivityScore(const vector<vector<int>>& g) {\n    bool vis[10][10] = {};\n    long long score = 0;\n    for (int r = 0; r < 10; ++r) for (int c = 0; c < 10; ++c) {\n        if (g[r][c] && !vis[r][c]) {\n            int flavour = g[r][c];\n            int cnt = 0;\n            queue<pair<int,int>> q;\n            q.push({r,c});\n            vis[r][c] = true;\n            while (!q.empty()) {\n                auto [x,y] = q.front(); q.pop();\n                ++cnt;\n                const int dr[4]={-1,1,0,0}, dc[4]={0,0,-1,1};\n                for (int k=0;k<4;++k){\n                    int nx=x+dr[k], ny=y+dc[k];\n                    if(nx>=0 && nx<10 && ny>=0 && ny<10 &&\n                       !vis[nx][ny] && g[nx][ny]==flavour){\n                        vis[nx][ny]=true;\n                        q.push({nx,ny});\n                    }\n                }\n            }\n            score += 1LL*cnt*cnt;\n        }\n    }\n    return score;\n}\n\nint main(){\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    vector<int> flavour(100);\n    for(int i=0;i<100;i++) cin>>flavour[i];\n\n    vector<vector<int>> board(10, vector<int>(10,0));\n    vector<Pos> empty;\n    empty.reserve(100);\n    for (int r=0;r<10;++r)\n        for (int c=0;c<10;++c)\n            empty.push_back({r,c});\n\n    const char dirs[4] = {'F','B','L','R'};\n    for(int t=0;t<100;++t){\n        int p; cin>>p;                     // 1\u2011based\n        Pos pos = empty[p-1];\n        board[pos.r][pos.c] = flavour[t];\n        empty.erase(empty.begin()+p-1);    // O(100) but 100*100 is negligible\n\n        // decide tilt\n        long long bestScore = -1;\n        char bestDir = 'F';\n        for(char d: dirs){\n            auto tmp = board;\n            tilt(tmp,d);\n            long long sc = connectivityScore(tmp);\n            if(sc > bestScore){\n                bestScore = sc;\n                bestDir   = d;\n            }\n        }\n\n        cout << bestDir << '\\n' << flush; // flush after each output\n    }\n    return 0;\n}","ahc016":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int M; double eps;\n    if (!(cin >> M >> eps)) return 0;\n\n    const int N = 20;\n    const int L = N * (N - 1) / 2;          // 190\n\n    cout << N << '\\n';\n    for (int k = 0; k < M; ++k) {\n        string s;\n        s.append(k, '1');\n        s.append(L - k, '0');\n        cout << s << '\\n';\n    }\n    cout.flush();\n\n    for (int q = 0; q < 100; ++q) {\n        string H;\n        cin >> H;\n        int ones = 0;\n        for (char c : H) if (c == '1') ++ones;\n        int ans = min(ones, M - 1);\n        cout << ans << '\\n';\n        cout.flush();\n    }\n    return 0;\n}","ahc017":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, D, K;\n    if (!(cin >> N >> M >> D >> K)) return 0;\n\n    // read edges (weights and endpoints are irrelevant)\n    for (int i = 0; i < M; ++i) {\n        int u, v, w;\n        cin >> u >> v >> w;\n    }\n    // skip vertex coordinates\n    for (int i = 0; i < N; ++i) {\n        int x, y;\n        cin >> x >> y;\n    }\n\n    // indices 0..M-1\n    vector<int> id(M);\n    iota(id.begin(), id.end(), 0);\n\n    // fast RNG\n    static std::mt19937 rng(\n        std::chrono::steady_clock::now().time_since_epoch().count());\n    shuffle(id.begin(), id.end(), rng);\n\n    // assign days round\u2011robin on the shuffled order\n    vector<int> day_of_edge(M);\n    for (int pos = 0; pos < M; ++pos) {\n        int e = id[pos];\n        day_of_edge[e] = (pos % D) + 1;   // 1\u2011based\n    }\n\n    // output in original edge order\n    for (int i = 0; i < M; ++i) {\n        if (i) cout << ' ';\n        cout << day_of_edge[i];\n    }\n    cout << '\\n';\n    return 0;\n}","ahc019":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Vec3 {int x, y, z;};\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int D; \n    if (!(cin >> D)) return 0;\n    vector<string> f1(D), r1(D), f2(D), r2(D);\n    for (int i = 0; i < D; ++i) cin >> f1[i];\n    for (int i = 0; i < D; ++i) cin >> r1[i];\n    for (int i = 0; i < D; ++i) cin >> f2[i];\n    for (int i = 0; i < D; ++i) cin >> r2[i];\n\n    auto idx = [D](int x,int y,int z){ return x*D*D + y*D + z; };\n\n    /* Boolean grids of cubes that exist in each object */\n    vector<vector<vector<char>>> cube1(D, vector<vector<char>>(D, vector<char>(D,0)));\n    vector<vector<vector<char>>> cube2(D, vector<vector<char>>(D, vector<char>(D,0)));\n\n    for (int z = 0; z < D; ++z)\n        for (int x = 0; x < D; ++x)\n            for (int y = 0; y < D; ++y) {\n                bool f1p = f1[z][x]=='1';\n                bool r1p = r1[z][y]=='1';\n                bool f2p = f2[z][x]=='1';\n                bool r2p = r2[z][y]=='1';\n                cube1[x][y][z] = f1p & r1p;\n                cube2[x][y][z] = f2p & r2p;\n            }\n\n    /* 3 grids: common, only1, only2 */\n    vector<vector<vector<char>>> common(D, vector<vector<char>>(D, vector<char>(D,0)));\n    vector<vector<vector<char>>> only1(D, vector<vector<char>>(D, vector<char>(D,0)));\n    vector<vector<vector<char>>> only2(D, vector<vector<char>>(D, vector<char>(D,0)));\n\n    for (int x = 0; x < D; ++x)\n        for (int y = 0; y < D; ++y)\n            for (int z = 0; z < D; ++z) {\n                bool c = cube1[x][y][z] & cube2[x][y][z];\n                common[x][y][z] = c;\n                only1[x][y][z] = cube1[x][y][z] & !c;\n                only2[x][y][z] = cube2[x][y][z] & !c;\n            }\n\n    const int dirs[6][3] = {{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};\n    auto flood = [&](vector<vector<vector<char>>>& grid)\n                 -> vector<vector<Vec3>> {\n        vector<vector<Vec3>> comps;\n        vector<vector<vector<char>>> vis(D, vector<vector<char>>(D, vector<char>(D,0)));\n        for (int x = 0; x < D; ++x)\n            for (int y = 0; y < D; ++y)\n                for (int z = 0; z < D; ++z)\n                    if (grid[x][y][z] && !vis[x][y][z]) {\n                        vector<Vec3> comp;\n                        queue<Vec3> qu;\n                        qu.push({x,y,z});\n                        vis[x][y][z] = 1;\n                        while (!qu.empty()) {\n                            auto [cx,cy,cz] = qu.front(); qu.pop();\n                            comp.push_back({cx,cy,cz});\n                            for (auto &d: dirs) {\n                                int nx=cx+d[0], ny=cy+d[1], nz=cz+d[2];\n                                if(nx>=0&&nx<D&&ny>=0&&ny<D&&nz>=0&&nz<D\n                                   &&grid[nx][ny][nz] && !vis[nx][ny][nz]){\n                                    vis[nx][ny][nz]=1;\n                                    qu.push({nx,ny,nz});\n                                }\n                            }\n                        }\n                        comps.push_back(std::move(comp));\n                    }\n        return comps;\n    };\n\n    auto commonComps = flood(common);\n    auto only1Comps   = flood(only1);\n    auto only2Comps   = flood(only2);\n\n    /* Prepare answer arrays */\n    vector<int> b1(D*D*D,0), b2(D*D*D,0);\n    int block_id = 1;\n\n    /* common components \u2013 used in both objects */\n    for (auto &comp : commonComps) {\n        for (auto &c : comp) {\n            b1[idx(c.x,c.y,c.z)] = block_id;\n            b2[idx(c.x,c.y,c.z)] = block_id;\n        }\n        ++block_id;\n    }\n    /* only1 components \u2013 used only in first object */\n    for (auto &comp : only1Comps) {\n        for (auto &c : comp) {\n            b1[idx(c.x,c.y,c.z)] = block_id;\n        }\n        ++block_id;\n    }\n    /* only2 components \u2013 used only in second object */\n    for (auto &comp : only2Comps) {\n        for (auto &c : comp) {\n            b2[idx(c.x,c.y,c.z)] = block_id;\n        }\n        ++block_id;\n    }\n    int n = block_id - 1;\n\n    /* Output */\n    cout << n << '\\n';\n    for (int i = 0; i < (int)b1.size(); ++i) {\n        if(i) cout << ' ';\n        cout << b1[i];\n    }\n    cout << '\\n';\n    for (int i = 0; i < (int)b2.size(); ++i) {\n        if(i) cout << ' ';\n        cout << b2[i];\n    }\n    cout << '\\n';\n    return 0;\n}","ahc020":"#include <bits/stdc++.h>\nusing namespace std;\n\n/*--- utility: ceil(sqrt(x)) ----------------------------------*/\nstatic inline int ceil_sqrt(long long x) {\n    long long r = sqrt((long double)x);\n    while (r * r < x) ++r;\n    while (r > 0 && (r - 1) * (r - 1) >= x) --r;\n    return (int)r;\n}\n\n/*--- DSU -----------------------------------------------------*/\nstruct DSU {\n    vector<int> p, sz;\n    DSU(int n = 0) { init(n); }\n    void init(int n) { p.resize(n); sz.assign(n, 1); iota(p.begin(), p.end(), 0); }\n    int find(int x) { return p[x] == x ? x : p[x] = find(p[x]); }\n    bool unite(int a, int b) {\n        a = find(a); b = find(b);\n        if (a == b) return false;\n        if (sz[a] < sz[b]) swap(a, b);\n        p[b] = a; sz[a] += sz[b];\n        return true;\n    }\n};\n\n/*--- Edge info ----------------------------------------------*/\nstruct EdgeInfo {int to,w,idx;};\n\nint main(){\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    int N,M,K;\n    if(!(cin>>N>>M>>K)) return 0;\n\n    /*--- vertices --------------------------------------------*/\n    vector<long long> xs(N), ys(N);\n    for(int i=0;i<N;i++) cin>>xs[i]>>ys[i];\n\n    /*--- edges ----------------------------------------------*/\n    vector<vector<EdgeInfo>> adj(N);\n    struct Edge{int u,v,w,idx;};\n    vector<Edge> edges(M);\n    for(int j=0;j<M;j++){\n        int u,v,w; cin>>u>>v>>w; --u;--v;\n        edges[j]={u,v,w,j};\n        adj[u].push_back({v,w,j});\n        adj[v].push_back({u,w,j});\n    }\n\n    /*--- residents ------------------------------------------*/\n    // distR2[k][i] : squared distance from resident k to vertex i\n    vector<vector<long long>> distR2(K, vector<long long>(N));\n    for(int k=0;k<K;k++){\n        long long a,b; cin>>a>>b;\n        for(int i=0;i<N;i++){\n            long long dx=a-xs[i], dy=b-ys[i];\n            distR2[k][i]=dx*dx+dy*dy;\n        }\n    }\n\n    /*--- assignment of residents to nearest station --------*/\n    vector<vector<int>> stationRes(N);\n    vector<int> residentAssign(K,-1);\n    for(int k=0;k<K;k++){\n        int best=0; long long bestd=distR2[k][0];\n        for(int i=1;i<N;i++){\n            if(distR2[k][i]<bestd){ bestd=distR2[k][i]; best=i;}\n        }\n        residentAssign[k]=best;\n        stationRes[best].push_back(k);\n    }\n\n    /*--- compute radii ---------------------------------------*/\n    vector<long long> maxDist2Station(N,0);\n    vector<int> P(N,0);\n    vector<char> active(N,0);\n    for(int i=0;i<N;i++){\n        if(!stationRes[i].empty()){\n            long long mx=0;\n            for(int r:stationRes[i]) mx=max(mx,distR2[r][i]);\n            maxDist2Station[i]=mx;\n            P[i]=ceil_sqrt(mx);\n            active[i]=1;\n        }\n    }\n\n    /*--- greedy merge of stations to reduce P cost -----------*/\n    bool improved=true;\n    while(improved){\n        improved=false;\n        long long bestDiff=-1;\n        int best_i=-1,best_j=-1;\n        long long bestNewMax=0;\n        int bestNewP=0;\n        vector<int> activeIdx;\n        activeIdx.reserve(N);\n        for(int i=0;i<N;i++) if(active[i]) activeIdx.push_back(i);\n        int Tcnt=activeIdx.size();\n        for(int ii=0;ii<Tcnt;ii++){\n            int i=activeIdx[ii];\n            if(stationRes[i].empty()) continue;\n            long long Pi2=1LL*P[i]*P[i];\n            for(int jj=0;jj<Tcnt;jj++){\n                int j=activeIdx[jj]; if(i==j) continue;\n                long long maxDist2_j = maxDist2Station[j];\n                long long newMax2 = maxDist2_j;\n                for(int r:stationRes[i]){\n                    if(distR2[r][j]>newMax2) newMax2=distR2[r][j];\n                }\n                int newP = ceil_sqrt(newMax2);\n                long long delta = 1LL*newP*newP - 1LL*P[j]*P[j];\n                if(delta < Pi2){\n                    long long diff = Pi2 - delta;\n                    if(diff>bestDiff){\n                        bestDiff=diff;\n                        best_i=i; best_j=j;\n                        bestNewMax=newMax2;\n                        bestNewP=newP;\n                    }\n                }\n            }\n        }\n        if(bestDiff!=-1){\n            improved=true;\n            // merge best_i into best_j\n            for(int r:stationRes[best_i]){\n                residentAssign[r]=best_j;\n                stationRes[best_j].push_back(r);\n            }\n            stationRes[best_i].clear();\n            maxDist2Station[best_j]=bestNewMax;\n            P[best_j]=bestNewP;\n            P[best_i]=0;\n            active[best_i]=0;\n        }\n    }\n\n    /*--- terminal list (include node 0 to guarantee connectivity) */\n    vector<int> terminals;\n    for(int i=0;i<N;i++) if(P[i]>0) terminals.push_back(i);\n    bool has0=false;\n    for(int v:terminals) if(v==0){ has0=true; break;}\n    if(!has0) terminals.push_back(0);          // node 0 is always terminal\n\n    int T=terminals.size();\n\n    /*--- compute all\u2011pairs shortest paths from terminals --------*/\n    const long long INFLL = (1LL<<60);\n    vector<vector<long long>> dist(T, vector<long long>(N, INFLL));\n    vector<vector<int>> preNode(T, vector<int>(N,-1));\n    vector<vector<int>> preEdge(T, vector<int>(N,-1));\n    for(int t=0;t<T;t++){\n        int src=terminals[t];\n        priority_queue<pair<long long,int>, vector<pair<long long,int>>, greater<pair<long long,int>>> pq;\n        dist[t][src]=0;\n        pq.push({0,src});\n        while(!pq.empty()){\n            auto [d,v]=pq.top(); pq.pop();\n            if(d!=dist[t][v]) continue;\n            for(const auto &e:adj[v]){\n                long long nd=d+e.w;\n                if(nd<dist[t][e.to]){\n                    dist[t][e.to]=nd;\n                    preNode[t][e.to]=v;\n                    preEdge[t][e.to]=e.idx;\n                    pq.push({nd,e.to});\n                }\n            }\n        }\n    }\n\n    /*--- complete graph of terminals and MST (Kruskal) --------*/\n    struct CEdge{long long w; int a,b;};\n    vector<CEdge> cEdges;\n    cEdges.reserve(T*(T-1)/2);\n    for(int i=0;i<T;i++){\n        for(int j=i+1;j<T;j++){\n            long long w=dist[i][terminals[j]];\n            cEdges.push_back({w,i,j});\n        }\n    }\n    sort(cEdges.begin(),cEdges.end(),[](const CEdge&x,const CEdge&y){return x.w<y.w;});\n    DSU dsu(T);\n    vector<char> used(M,0);\n    for(const auto &ce:cEdges){\n        if(dsu.unite(ce.a,ce.b)){\n            int s=terminals[ce.a], t=terminals[ce.b];\n            int cur=t;\n            while(cur!=s){\n                int eidx=preEdge[ce.a][cur];\n                used[eidx]=1;\n                cur=preNode[ce.a][cur];\n            }\n        }\n    }\n\n    /*--- output ----------------------------------------------*/\n    for(int i=0;i<N;i++){\n        if(i) cout<<' ';\n        cout<<P[i];\n    }\n    cout<<'\\n';\n    for(int j=0;j<M;j++){\n        if(j) cout<<' ';\n        cout<<(used[j]?1:0);\n    }\n    cout<<'\\n';\n    return 0;\n}","ahc021":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    const int N = 30;\n    const int S = N * (N + 1) / 2;          // 465\n    vector<int> a(S);\n    for (int i = 0; i < S; ++i) cin >> a[i];\n\n    // ---------- pre\u2011compute coordinates and row number ----------\n    vector<pair<int,int>> coord(S);\n    vector<int> row_of_idx(S);\n    for (int r = 0; r < N; ++r) {\n        int start = r * (r + 1) / 2;\n        for (int c = 0; c <= r; ++c) {\n            int idx = start + c;\n            coord[idx] = {r, c};\n            row_of_idx[idx] = r;\n        }\n    }\n\n    // ---------- record of swaps ----------\n    struct Op {int x1, y1, x2, y2;};\n    vector<Op> ops;\n\n    // ---------- heapify bottom\u2011up ----------\n    const int INTERNAL = S - N;          // indices 0 \u2026 434 are internal\n    for (int start = INTERNAL - 1; start >= 0; --start) {\n        int cur = start;\n        while (true) {\n            int r = row_of_idx[cur];\n            int left = cur + r + 1;\n            int right = cur + r + 2;\n            int smallest = cur;\n            if (left < S && a[left] < a[smallest]) smallest = left;\n            if (right < S && a[right] < a[smallest]) smallest = right;\n            if (smallest == cur) break;\n            swap(a[cur], a[smallest]);\n            ops.push_back({coord[cur].first, coord[cur].second,\n                           coord[smallest].first, coord[smallest].second});\n            cur = smallest;\n        }\n    }\n\n    // ---------- output ----------\n    cout << ops.size() << '\\n';\n    for (const auto &p : ops)\n        cout << p.x1 << ' ' << p.y1 << ' ' << p.x2 << ' ' << p.y2 << '\\n';\n    return 0;\n}","toyota2023summer-final":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Cell{int x,y;};\n\nint main(){\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int D, N;\n    if(!(cin>>D>>N)) return 0;          // read D (=9) and number of obstacles\n    \n    vector<vector<int>> obst(D, vector<int>(D,0));\n    for(int i=0;i<N;i++){\n        int r,c; cin>>r>>c;\n        obst[r][c]=1;\n    }\n    \n    // entrance position\n    const int ex = 0, ey = (D-1)/2;\n    obst[ex][ey] = 0; // ensure entrance is free\n    \n    // ---------- compute distance from entrance ----------\n    const int INF = 1e9;\n    vector<vector<int>> dist(D, vector<int>(D, INF));\n    queue<Cell> q;\n    dist[ex][ey] = 0;\n    q.push({ex,ey});\n    const int dx[4]={-1,1,0,0};\n    const int dy[4]={0,0,-1,1};\n    while(!q.empty()){\n        auto cur = q.front(); q.pop();\n        for(int dir=0;dir<4;dir++){\n            int nx = cur.x + dx[dir];\n            int ny = cur.y + dy[dir];\n            if(nx<0||nx>=D||ny<0||ny>=D) continue;\n            if(obst[nx][ny]) continue;\n            if(dist[nx][ny]!=INF) continue;\n            dist[nx][ny]=dist[cur.x][cur.y]+1;\n            q.push({nx,ny});\n        }\n    }\n    \n    // collect all free squares except entrance\n    vector<Cell> cells;\n    for(int i=0;i<D;i++)\n        for(int j=0;j<D;j++){\n            if(obst[i][j]) continue;\n            if(i==ex && j==ey) continue;\n            cells.push_back({i,j});\n        }\n    // sort by decreasing distance\n    sort(cells.begin(), cells.end(), [&](const Cell&a,const Cell&b){\n        return dist[a.x][a.y] > dist[b.x][b.y];\n    });\n    \n    int total = D*D - 1 - N;   // number of containers to be stored\n    int idx = 0;\n    \n    // ---------- placement phase ----------\n    for(int d=0; d<total; ++d){\n        int t; cin>>t;                // receive container number (unused)\n        auto c = cells[idx++];\n        cout<<c.x<<\" \"<<c.y<<\"\\n\"<<flush;   // output and flush immediately\n    }\n    \n    // ---------- removal phase ----------\n    // BFS from entrance, output order of non\u2011entrance visited cells\n    vector<vector<int>> visited(D, vector<int>(D,0));\n    queue<Cell> q2;\n    visited[ex][ey]=1;\n    q2.push({ex,ey});\n    vector<Cell> removal;\n    while(!q2.empty()){\n        auto cur=q2.front(); q2.pop();\n        for(int dir=0;dir<4;dir++){\n            int nx=cur.x+dx[dir];\n            int ny=cur.y+dy[dir];\n            if(nx<0||nx>=D||ny<0||ny>=D) continue;\n            if(obst[nx][ny]) continue;\n            if(visited[nx][ny]) continue;\n            visited[nx][ny]=1;\n            q2.push({nx,ny});\n            removal.push_back({nx,ny});\n        }\n    }\n    for(auto &c: removal){\n        cout<<c.x<<\" \"<<c.y<<\"\\n\";\n    }\n    return 0;\n}","ahc024":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int n, m;\n    if (!(cin >> n >> m)) return 0;  // just in case\n    \n    for (int i = 0; i < n; ++i) {\n        for (int j = 0; j < n; ++j) {\n            int v;  cin >> v;\n            cout << v;\n            if (j + 1 < n) cout << ' ';\n        }\n        cout << '\\n';\n    }\n    return 0;\n}","ahc025":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, D, Q;\n    if (!(cin >> N >> D >> Q)) return 0;\n\n    // Perform Q trivial queries\n    for (int q = 0; q < Q; ++q) {\n        int l = q % N;\n        int r = (q + 1) % N;          // ensures r != l\n        cout << 1 << ' ' << 1 << ' ' << l << ' ' << r << '\\n';\n        cout.flush();\n\n        // read judge answer and ignore\n        char ans;\n        if (!(cin >> ans)) return 0;   // safety: if input ends unexpectedly\n    }\n\n    // Output a simple final division\n    for (int i = 0; i < N; ++i) {\n        if (i) cout << ' ';\n        cout << (i % D);\n    }\n    cout << '\\n';\n    return 0;\n}","ahc026":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    const int n = 200;   // fixed in problem statement\n    const int m = 10;    // fixed in problem statement\n\n    vector<vector<int>> stacks(m);\n\n    for (int i = 0; i < m; ++i) {\n        stacks[i].resize(n / m);\n        for (int j = 0; j < n / m; ++j) {\n            cin >> stacks[i][j];          // bottom -> top\n        }\n    }\n\n    vector<pair<int,int>> ops;          // store (v , i)\n    vector<bool> removed(n + 1, false);\n\n    for (int v = 1; v <= n; ++v) {\n        while (true) {\n            int src = -1;          // stack index where v is\n            int pos = -1;          // position from bottom\n            for (int i = 0; i < m; ++i) {\n                for (int j = 0; j < (int)stacks[i].size(); ++j) {\n                    if (stacks[i][j] == v) {\n                        src = i;\n                        pos = j;\n                        break;\n                    }\n                }\n                if (src != -1) break;\n            }\n            if (src == -1) {            // already removed, should not happen\n                break;\n            }\n\n            if (pos == (int)stacks[src].size() - 1) {\n                // v is on top -> take it out\n                ops.emplace_back(v, 0);\n                stacks[src].pop_back();\n                removed[v] = true;\n                break;\n            } else {\n                // choose destination stack with minimal height (different stack)\n                int dest = -1, best = INT_MAX;\n                for (int i = 0; i < m; ++i) {\n                    if (i == src) continue;\n                    if ((int)stacks[i].size() < best) {\n                        best = stacks[i].size();\n                        dest = i;\n                    }\n                }\n                // move segment [pos .. end] from src to dest\n                vector<int> segment;\n                for (int idx = pos; idx < (int)stacks[src].size(); ++idx) {\n                    segment.push_back(stacks[src][idx]);\n                }\n                // append to dest\n                stacks[dest].insert(stacks[dest].end(), segment.begin(), segment.end());\n                // truncate src\n                stacks[src].resize(pos);\n                // record move operation (1-indexed destination)\n                ops.emplace_back(v, dest + 1);\n            }\n        }\n    }\n\n    // output\n    for (auto [v, i] : ops) {\n        cout << v << ' ' << i << '\\n';\n    }\n    return 0;\n}","ahc027":"#include <bits/stdc++.h>\nusing namespace std;\n\nint N;\nvector<string> h, v;                     // horizontal, vertical walls\nvector<vector<int>> d;                   // dirtiness, unused by the algorithm\nvector<vector<bool>> vis;\n\nstring ans;\nconst char dirChar[4] = {'R', 'D', 'L', 'U'};\nint dx[4] = {0, 1, 0, -1};\nint dy[4] = {1, 0, -1, 0};\n\nbool canMove(int x, int y, int dir) {\n    int nx = x + dx[dir];\n    int ny = y + dy[dir];\n    if (nx < 0 || nx >= N || ny < 0 || ny >= N) return false;\n    if (dir == 0) {            // Right\n        return v[x][y] == '0';\n    } else if (dir == 1) {     // Down\n        return h[x][y] == '0';\n    } else if (dir == 2) {     // Left\n        return v[x][y-1] == '0';\n    } else {                   // Up\n        return h[x-1][y] == '0';\n    }\n}\n\nvoid dfs(int x, int y) {\n    vis[x][y] = true;\n    for (int dir = 0; dir < 4; ++dir) {\n        if (!canMove(x, y, dir)) continue;\n        int nx = x + dx[dir];\n        int ny = y + dy[dir];\n        if (vis[nx][ny]) continue;\n        ans.push_back(dirChar[dir]);          // move to child\n        dfs(nx, ny);\n        ans.push_back(dirChar[(dir + 2) % 4]); // backtrack\n    }\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    if (!(cin >> N)) return 0;\n    h.resize(N-1);\n    for (int i = 0; i < N-1; ++i) cin >> h[i];\n    v.resize(N);\n    for (int i = 0; i < N; ++i) cin >> v[i];\n    d.assign(N, vector<int>(N));\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < N; ++j)\n            cin >> d[i][j];           // not used\n\n    vis.assign(N, vector<bool>(N, false));\n    dfs(0, 0);\n\n    cout << ans << '\\n';\n    return 0;\n}","ahc028":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    int N, M;\n    if (!(cin >> N >> M)) return 0;\n    int si, sj;\n    cin >> si >> sj;\n    vector<string> grid(N);\n    for (int i = 0; i < N; ++i) cin >> grid[i];\n\n    const int SZ = N * N;\n    auto id = [N](int i, int j){ return i * N + j; };\n    vector<int> id2i(SZ), id2j(SZ);\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < N; ++j) {\n            int k = id(i, j);\n            id2i[k] = i;\n            id2j[k] = j;\n        }\n\n    /* positions of every letter */\n    vector<vector<int>> pos(26);\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < N; ++j)\n            pos[grid[i][j]-'A'].push_back(id(i, j));\n\n    /* precompute nearest cell for every starting cell and letter */\n    vector<array<int,26>> nearest(SZ);\n    vector<array<int,26>> nearestDist(SZ);\n    for (int s = 0; s < SZ; ++s) {\n        int siCur = id2i[s], sjCur = id2j[s];\n        for (int c = 0; c < 26; ++c) {\n            int bestDist = 1e9, bestId = -1;\n            for (int t : pos[c]) {\n                int ti = id2i[t], tj = id2j[t];\n                int d = abs(siCur - ti) + abs(sjCur - tj);\n                if (d < bestDist) {\n                    bestDist = d;\n                    bestId = t;\n                }\n            }\n            nearest[s][c] = bestId;\n            nearestDist[s][c] = bestDist;\n        }\n    }\n\n    vector<string> rem(M);\n    for (int i = 0; i < M; ++i) cin >> rem[i];\n\n    vector<pair<int,int>> answer; // list of moves\n\n    int currId = id(si, sj);\n\n    while (!rem.empty()) {\n        long long bestTotal = LLONG_MAX;\n        int bestBlock = -1;\n        int bestStartId = -1;\n        vector<int> bestPath; // ids of the whole block\n\n        for (int b = 0; b < (int)rem.size(); ++b) {\n            const string &t = rem[b];\n            int first = t[0] - 'A';\n            for (int startId : pos[first]) {\n                long long internal = 0;\n                vector<int> path;\n                path.push_back(startId);\n                int cur = startId;\n                bool ok = true;\n                for (int k = 1; k < 5; ++k) {\n                    int need = t[k] - 'A';\n                    int nxt = nearest[cur][need];\n                    if (nxt == -1) {ok=false;break;}\n                    internal += nearestDist[cur][need] + 1;\n                    cur = nxt;\n                    path.push_back(cur);\n                }\n                if (!ok) continue;\n                long long moveFromCurr = nearestDist[currId][first] + 1;\n                long long total = moveFromCurr + internal;\n                if (total < bestTotal) {\n                    bestTotal = total;\n                    bestBlock = b;\n                    bestStartId = startId;\n                    bestPath = path;\n                }\n            }\n        }\n\n        // Output the best block\n        for (int idv : bestPath) {\n            answer.emplace_back(id2i[idv], id2j[idv]);\n        }\n        currId = bestPath.back();\n        rem.erase(rem.begin() + bestBlock);\n    }\n\n    // Print answer\n    for (auto [i, j] : answer) {\n        cout << i << ' ' << j << '\\n';\n    }\n    return 0;\n}","ahc030":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    // Read everything, ignore it.\n    string s;\n    while (cin >> s) {\n        // Do nothing.\n    }\n    return 0;\n}","ahc031":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    int W, D, N;\n    if (!(cin >> W >> D >> N)) return 0;\n    // read the desired areas, they are irrelevant for this trivial solution\n    for (int d = 0; d < D; ++d) {\n        for (int k = 0; k < N; ++k) {\n            long long a; cin >> a;\n        }\n    }\n\n    // For every day, output the same set of N unit squares.\n    for (int d = 0; d < D; ++d) {\n        for (int k = 0; k < N; ++k) {\n            // coordinates: (k,0)-(k+1,1)\n            cout << k << \" \" << 0 << \" \" << k + 1 << \" \" << 1 << '\\n';\n        }\n    }\n    return 0;\n}","ahc032":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    const long long MOD = 998244353LL;\n\n    int N, M, K;\n    if (!(cin >> N >> M >> K)) return 0;          // N = 9, M = 20, K = 81\n\n    /* read initial board */\n    vector<vector<long long>> a(N, vector<long long>(N));\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < N; ++j)\n            cin >> a[i][j];\n\n    /* read stamps */\n    vector<array<array<long long,3>,3>> s(M);\n    for (int m = 0; m < M; ++m)\n        for (int i = 0; i < 3; ++i)\n            for (int j = 0; j < 3; ++j)\n                cin >> s[m][i][j];\n\n    struct Op { int m, p, q; };\n    vector<Op> ops;\n\n    while ((int)ops.size() < K) {\n        long long bestBenefit = 0;\n        int bestM = -1, bestP = -1, bestQ = -1;\n\n        for (int m = 0; m < M; ++m) {\n            for (int p = 0; p <= N - 3; ++p) {\n                for (int q = 0; q <= N - 3; ++q) {\n                    bool safe = true;\n                    long long benefit = 0;\n                    for (int di = 0; di < 3 && safe; ++di) {\n                        for (int dj = 0; dj < 3; ++dj) {\n                            long long val = a[p + di][q + dj] + s[m][di][dj];\n                            if (val >= MOD) { safe = false; break; }\n                            benefit += s[m][di][dj];\n                        }\n                    }\n                    if (safe && benefit > bestBenefit) {\n                        bestBenefit = benefit;\n                        bestM = m;\n                        bestP = p;\n                        bestQ = q;\n                    }\n                }\n            }\n        }\n\n        if (bestM == -1) break;                  // no safe press left\n\n        /* apply the best safe press */\n        for (int di = 0; di < 3; ++di)\n            for (int dj = 0; dj < 3; ++dj)\n                a[bestP + di][bestQ + dj] += s[bestM][di][dj];\n\n        ops.push_back({bestM, bestP, bestQ});\n    }\n\n    cout << ops.size() << '\\n';\n    for (auto &op : ops)\n        cout << op.m << ' ' << op.p << ' ' << op.q << '\\n';\n\n    return 0;\n}","ahc033":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    const int N = 5;                  // fixed by the problem\n    vector<vector<int>> A(N, vector<int>(N));\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < N; ++j)\n            cin >> A[i][j];\n\n    // where each container appears\n    vector<int> row_of(N * N);\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < N; ++j)\n            row_of[A[i][j]] = i;\n\n    // build sequence for the large crane (crane 0)\n    string large;\n    int cur_r = 0, cur_c = 0;        // start at (0,0)\n\n    for (int b = 0; b < N * N; ++b) {\n        int gate_r = row_of[b];        // receiving gate row\n        int target_r = b / N;          // dispatch\u2011gate row\n\n        // move vertically to the receiving gate\n        while (cur_r > gate_r) { large.push_back('U'); --cur_r; }\n        while (cur_r < gate_r) { large.push_back('D'); ++cur_r; }\n\n        large.push_back('P');          // pick up\n\n        // move to dispatch\u2011gate row\n        while (cur_r > target_r) { large.push_back('U'); --cur_r; }\n        while (cur_r < target_r) { large.push_back('D'); ++cur_r; }\n\n        // move right to column 4 (dispatch gate)\n        for (int k = 0; k < 4; ++k) { large.push_back('R'); ++cur_c; }\n\n        large.push_back('Q');          // release\n\n        // return to column 0\n        while (cur_c > 0) { large.push_back('L'); --cur_c; }\n        // ready at (target_r,0) for next container\n    }\n\n    // output: large crane line\n    cout << large << '\\n';\n\n    // small cranes: bomb at turn 0, then do nothing\n    string small = \"B\" + string(large.size() - 1, '.');\n    for (int i = 1; i < N; ++i)\n        cout << small << '\\n';\n\n    return 0;\n}","ahc034":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N;\n    if (!(cin >> N)) return 0;      // read the single integer 20\n    vector<vector<int>> h(N, vector<int>(N));\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < N; ++j)\n            cin >> h[i][j];\n\n    struct Cell { int r, c, v; };\n    vector<Cell> pos, neg;\n    pos.reserve(N * N);\n    neg.reserve(N * N);\n\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < N; ++j) {\n            if (h[i][j] > 0) pos.push_back({i, j, h[i][j]});\n            else if (h[i][j] < 0) neg.push_back({i, j, -h[i][j]});\n        }\n\n    vector<string> ops;\n    int curR = 0, curC = 0;\n    auto moveTo = [&](int tr, int tc) {\n        while (curC < tc) { ops.push_back(\"R\"); ++curC; }\n        while (curC > tc) { ops.push_back(\"L\"); --curC; }\n        while (curR < tr) { ops.push_back(\"D\"); ++curR; }\n        while (curR > tr) { ops.push_back(\"U\"); --curR; }\n    };\n\n    /* 1\ufe0f\u20e3 Collect all soil from positives */\n    for (const auto &p : pos) {\n        moveTo(p.r, p.c);\n        ops.emplace_back(\"+\" + to_string(p.v));\n    }\n\n    /* 2\ufe0f\u20e3 Distribute to negatives */\n    for (const auto &n : neg) {\n        moveTo(n.r, n.c);\n        ops.emplace_back(\"-\" + to_string(n.v));\n    }\n\n    for (const auto &s : ops)\n        cout << s << '\\n';\n    return 0;\n}","ahc035":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, T;\n    if (!(cin >> N >> M >> T)) return 0;          // safety\n\n    const int SEED_CNT = 2 * N * (N - 1);         // 60\n    vector<vector<int>> seeds(SEED_CNT, vector<int>(M));\n\n    /* read initial seeds */\n    for (int i = 0; i < SEED_CNT; ++i)\n        for (int j = 0; j < M; ++j)\n            cin >> seeds[i][j];\n\n    const int PLANTED = N * N; // 36\n\n    /* main loop */\n    for (int turn = 0; turn < T; ++turn) {\n        // 1. diversity + sum based selection\n        vector<int> best_val(M, 0);\n        vector<int> chosen;\n        vector<int> remaining(SEED_CNT);\n        iota(remaining.begin(), remaining.end(), 0);\n\n        while ((int)chosen.size() < PLANTED) {\n            int best_idx = -1;\n            pair<int, long long> best_pair{-1, -1}; // (improvements, sum)\n\n            for (int idx : remaining) {\n                int improv = 0;\n                long long sumv = 0;\n                for (int l = 0; l < M; ++l) {\n                    if (seeds[idx][l] > best_val[l]) ++improv;\n                    sumv += seeds[idx][l];\n                }\n                pair<int, long long> cur{improv, sumv};\n                if (cur > best_pair) {\n                    best_pair = cur;\n                    best_idx = idx;\n                }\n            }\n            chosen.push_back(best_idx);\n            // remove from remaining\n            auto it = std::remove(remaining.begin(), remaining.end(), best_idx);\n            remaining.erase(it, remaining.end());\n\n            // update best_val\n            for (int l = 0; l < M; ++l)\n                best_val[l] = max(best_val[l], seeds[best_idx][l]);\n        }\n\n        // 2. output board in row-major\n        int pos = 0;\n        for (int i = 0; i < N; ++i) {\n            for (int j = 0; j < N; ++j) {\n                if (j) cout << ' ';\n                cout << chosen[pos++];\n            }\n            cout << '\\n';\n        }\n        cout.flush();\n\n        // 3. read next set of seeds\n        for (int i = 0; i < SEED_CNT; ++i)\n            for (int j = 0; j < M; ++j)\n                cin >> seeds[i][j];\n    }\n    return 0;\n}","ahc038":"#include <bits/stdc++.h>\nusing namespace std;\n\n/* ------------- helpers for movements ------------- */\nstruct ArmState {\n    int rootX, rootY;      // current root coordinates\n    int dir;               // current fingertip direction (0:E,1:N,2:W,3:S)\n    vector<string> ops;    // list of turn strings\n    /*  add a turn string of length 4 */\n    void addOp(char mv, char rot, char act0, char act1) {\n        string st(4, '.');\n        st[0] = mv; st[1] = rot; st[2] = act0; st[3] = act1;\n        ops.push_back(st);\n    }\n    /* rotate the fingertip to target direction */\n    void rotateTo(int target) {\n        while (dir != target) {\n            int diff = (target - dir + 4) % 4;\n            char r = (diff <= 2) ? 'L' : 'R';   // L = CCW, R = CW\n            addOp('.', r, '.', '.');\n            dir = (diff <= 2) ? (dir + 1) & 3 : (dir + 3) & 3;\n        }\n    }\n    /* move the root to a position (tr,tc) */\n    void moveTo(int tr, int tc) {\n        while (rootX < tr) { addOp('D', '.', '.', '.'); ++rootX; }\n        while (rootX > tr) { addOp('U', '.', '.', '.'); --rootX; }\n        while (rootY < tc) { addOp('R', '.', '.', '.'); ++rootY; }\n        while (rootY > tc) { addOp('L', '.', '.', '.'); --rootY; }\n    }\n};\n\n/* ------------- task structure ------------- */\nstruct Task {\n    int sr, sc;      // current source coordinates (may change if buffered)\n    int tr, tc;      // target coordinates\n    bool done = false;\n};\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    int N, M, V;\n    if (!(cin >> N >> M >> V)) return 0;\n    vector<string> s(N), t(N);\n    for (int i = 0; i < N; ++i) cin >> s[i];\n    for (int i = 0; i < N; ++i) cin >> t[i];\n\n    /* ----------------- 1. output tree ----------------- */\n    const int Vp = 2;                 // root + one fingertip\n    cout << Vp << '\\n';\n    cout << 0 << ' ' << 1 << '\\n';      // parent[1] = 0, length = 1\n    cout << 0 << ' ' << 0 << '\\n';      // root initial at (0,0)\n\n    /* ----------------- 2. prepare tasks ----------------- */\n    vector<pair<int,int>> src, tgt;\n    for (int i = 0; i < N; ++i) {\n        for (int j = 0; j < N; ++j) {\n            if (s[i][j] == '1') src.emplace_back(i, j);\n            if (t[i][j] == '1') tgt.emplace_back(i, j);\n        }\n    }\n    sort(src.begin(), src.end());   // lexicographic\n    sort(tgt.begin(), tgt.end());   // lexicographic\n\n    vector<Task> tasks(M);\n    for (int i = 0; i < M; ++i) {\n        tasks[i] = {src[i].first, src[i].second,\n                    tgt[i].first, tgt[i].second};\n    }\n\n    /* board state */\n    vector<vector<int>> board(N, vector<int>(N, 0));\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < N; ++j)\n            board[i][j] = (s[i][j] == '1');\n\n    /* arm state */\n    ArmState arm;\n    arm.rootX = 0;\n    arm.rootY = 0;\n    arm.dir = 0;   // initial direction is east (0)\n\n    const int dr[4] = {0, -1, 0, 1}; // direction order is 0:E,1:N,2:W,3:S\n    const int dc[4] = {1, 0, -1, 0};\n\n    /* helper to bring fingertip to a grid cell (r,c) */\n    auto approach = [&](int r, int c) {\n        int desiredDir, rr, cc;\n        if (c > 0) { desiredDir = 0; rr = r; cc = c - 1; }\n        else if (r > 0) { desiredDir = 3; rr = r - 1; cc = c; }\n        else { desiredDir = 1; rr = r + 1; cc = c; }\n        arm.rotateTo(desiredDir);\n        arm.moveTo(rr, cc);\n    };\n\n    /* main processing loop */\n    bool progress = true;\n    while (progress) {\n        progress = false;\n        for (int i = 0; i < M; ++i) {\n            if (tasks[i].done) continue;\n            int tr = tasks[i].tr;\n            int tc = tasks[i].tc;\n            if (board[tr][tc] == 0) {   // target empty\n                /* go to source */\n                approach(tasks[i].sr, tasks[i].sc);\n                /* pick */\n                arm.addOp('.', '.', '.', 'P');\n                board[tasks[i].sr][tasks[i].sc] = 0;\n\n                /* go to target */\n                approach(tr, tc);\n                /* drop */\n                arm.addOp('.', '.', '.', 'P');\n                board[tr][tc] = 1;\n\n                tasks[i].done = true;\n                progress = true;\n                break; // reorder after each successful move\n            }\n        }\n        if (progress) continue;\n\n        /* no task can be completed directly \u2013 use an empty cell as buffer */\n        int bufIdx = -1;\n        for (int i = 0; i < M; ++i)\n            if (!tasks[i].done) { bufIdx = i; break; }\n        if (bufIdx == -1) break; // all done\n\n        /* find an empty cell */\n        int er = -1, ec = -1;\n        for (int i = 0; i < N && er == -1; ++i)\n            for (int j = 0; j < N; ++j)\n                if (board[i][j] == 0) { er = i; ec = j; break; }\n\n        // pick from source\n        approach(tasks[bufIdx].sr, tasks[bufIdx].sc);\n        arm.addOp('.', '.', '.', 'P');\n        board[tasks[bufIdx].sr][tasks[bufIdx].sc] = 0;\n\n        // move to buffer\n        approach(er, ec);\n        arm.addOp('.', '.', '.', 'P');\n        board[er][ec] = 1;\n\n        // update task source to buffer position\n        tasks[bufIdx].sr = er;\n        tasks[bufIdx].sc = ec;\n        // task still pending\n    }\n\n    /* output operations */\n    for (const string &op : arm.ops) cout << op << '\\n';\n    return 0;\n}","ahc039":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    const int MAXC = 100000;\n    const int S = 200;                          // cell size\n    const int NX = MAXC / S + 1;                // 501\n    const int NY = MAXC / S + 1;\n\n    int N;\n    if (!(cin >> N)) return 0;\n\n    // diff[cx][cy] = mackerels - sardines in that cell\n    vector<vector<int>> diff(NX, vector<int>(NY, 0));\n\n    // store one mackerel to use for fallback\n    int first_mack_x = -1, first_mack_y = -1;\n\n    for (int i = 0; i < N; ++i) {\n        int x, y;\n        cin >> x >> y;\n        int cx = x / S;\n        int cy = y / S;\n        diff[cx][cy] += 1;\n        if (first_mack_x == -1) { first_mack_x = x; first_mack_y = y; }\n    }\n    for (int i = 0; i < N; ++i) {\n        int x, y;\n        cin >> x >> y;\n        int cx = x / S;\n        int cy = y / S;\n        diff[cx][cy] -= 1;\n    }\n\n    long long bestSum = LLONG_MIN;\n    int bestX1 = 0, bestY1 = 0, bestX2 = 0, bestY2 = 0;\n\n    vector<long long> colSum(NX);\n    for (int y1 = 0; y1 < NY; ++y1) {\n        fill(colSum.begin(), colSum.end(), 0);\n        for (int y2 = y1; y2 < NY; ++y2) {\n            for (int x = 0; x < NX; ++x)\n                colSum[x] += diff[x][y2];\n\n            // Kadane on colSum\n            long long curSum = 0;\n            int curL = 0;\n            long long localBest = LLONG_MIN;\n            int localL = 0, localR = 0;\n            for (int x = 0; x < NX; ++x) {\n                curSum += colSum[x];\n                if (curSum > localBest) {\n                    localBest = curSum;\n                    localL = curL;\n                    localR = x;\n                }\n                if (curSum < 0) {\n                    curSum = 0;\n                    curL = x + 1;\n                }\n            }\n            if (localBest > bestSum) {\n                bestSum = localBest;\n                bestX1 = localL;\n                bestX2 = localR;\n                bestY1 = y1;\n                bestY2 = y2;\n            }\n        }\n    }\n\n    // if bestSum <= 0, fallback to a single mackerel\n    long long rectSum = bestSum;\n    int x1, y1, x2, y2;\n    if (bestSum > 0) {\n        x1 = bestX1 * S;\n        y1 = bestY1 * S;\n        x2 = min((bestX2 + 1) * S, MAXC);\n        y2 = min((bestY2 + 1) * S, MAXC);\n    } else {\n        // single mackerel cell\n        int cx = first_mack_x / S;\n        int cy = first_mack_y / S;\n        x1 = cx * S;\n        y1 = cy * S;\n        x2 = min((cx + 1) * S, MAXC);\n        y2 = min((cy + 1) * S, MAXC);\n    }\n\n    cout << 4 << '\\n';\n    cout << x1 << ' ' << y1 << '\\n';\n    cout << x2 << ' ' << y1 << '\\n';\n    cout << x2 << ' ' << y2 << '\\n';\n    cout << x1 << ' ' << y2 << '\\n';\n    return 0;\n}","ahc040":"#include <bits/stdc++.h>\nusing namespace std;\n\n// ------------------------------------------------------------\n// Interactive solution that packs all rectangles in a single\n// vertical stack.  The orientation of each rectangle is chosen\n// to minimise the value  width + height of the final bounding box.\n// ------------------------------------------------------------\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, T, sigma;\n    if (!(cin >> N >> T >> sigma)) return 0;\n\n    vector<long long> w(N), h(N);\n    for (int i = 0; i < N; ++i) cin >> w[i] >> h[i];\n\n    /* ----- enumerate candidate widths (w or h of any rectangle) ----- */\n    vector<long long> cand;\n    cand.reserve(2 * N);\n    for (int i = 0; i < N; ++i) {\n        cand.push_back(w[i]);\n        cand.push_back(h[i]);\n    }\n    sort(cand.begin(), cand.end());\n    cand.erase(unique(cand.begin(), cand.end()), cand.end());\n\n    /* ----- find orientation that minimises W + H for the vertical stack ----- */\n    long long bestScore = LLONG_MAX;\n    vector<int> bestOrient(N, 0);\n\n    for (long long Wmax : cand) {\n        long long totalH = 0;\n        vector<int> orientCur(N, 0);\n        bool feasible = true;\n        for (int i = 0; i < N; ++i) {\n            bool can0 = (w[i] <= Wmax); // keep as (w,h)\n            bool can1 = (h[i] <= Wmax); // rotate to (h,w)\n            if (!can0 && !can1) { feasible = false; break; }\n\n            if (can0 && !can1) {\n                orientCur[i] = 0;\n                totalH += h[i];\n            } else if (!can0 && can1) {\n                orientCur[i] = 1;\n                totalH += w[i];\n            } else {\n                // both fit \u2013 choose orientation with smaller height\n                if (h[i] <= w[i]) {\n                    orientCur[i] = 0;\n                    totalH += h[i];\n                } else {\n                    orientCur[i] = 1;\n                    totalH += w[i];\n                }\n            }\n        }\n        if (!feasible) continue;\n        long long score = Wmax + totalH;\n        if (score < bestScore) {\n            bestScore = score;\n            bestOrient = orientCur;\n        }\n    }\n\n    /* ----- output the vertical\u2011stack packing for every turn ----- */\n    for (int turn = 0; turn < T; ++turn) {\n        cout << N << '\\n';\n        // first rectangle\n        cout << 0 << ' ' << bestOrient[0] << ' ' << 'U' << ' ' << -1 << '\\n';\n        // remaining rectangles: each below the previous one\n        for (int i = 1; i < N; ++i) {\n            cout << i << ' ' << bestOrient[i] << ' ' << 'L' << ' ' << i - 1 << '\\n';\n        }\n        cout.flush();\n\n        // read and ignore judge's reply\n        long long Wp, Hp;\n        cin >> Wp >> Hp;\n    }\n    return 0;\n}","ahc041":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    int N, M, H;\n    if (!(cin >> N >> M >> H)) return 0;\n\n    vector<int> A(N);\n    for (int i = 0; i < N; ++i) cin >> A[i];\n\n    vector<vector<int>> adj(N);\n    for (int i = 0; i < M; ++i) {\n        int u, v; cin >> u >> v;\n        adj[u].push_back(v);\n        adj[v].push_back(u);\n    }\n    for (int i = 0; i < N; ++i) { int x, y; cin >> x >> y; }   // ignore coordinates\n\n    mt19937 rng((unsigned)chrono::steady_clock::now().time_since_epoch().count());\n\n    vector<int> best_parent(N, -1);\n    long long best_score = -1;\n\n    const int ITER = 200;          // number of random attempts\n    vector<int> nodes(N);\n    iota(nodes.begin(), nodes.end(), 0);\n\n    for (int it = 0; it < ITER; ++it) {\n        vector<int> parent(N, -1);\n        vector<char> used(N, 0);\n        vector<int> depth(N, 0);\n\n        // shuffle root order\n        shuffle(nodes.begin(), nodes.end(), rng);\n\n        for (int root : nodes) {\n            if (used[root]) continue;\n            // start new tree\n            parent[root] = -1;\n            depth[root] = 0;\n            used[root] = 1;\n\n            // depth\u2011first search to depth H\n            function<void(int,int)> dfs = [&](int cur, int d) {\n                if (d == H) return;\n                vector<int> nb(adj[cur].begin(), adj[cur].end());\n                shuffle(nb.begin(), nb.end(), rng);      // random neighbor order\n                for (int nbv : nb) {\n                    if (!used[nbv]) {\n                        used[nbv] = 1;\n                        parent[nbv] = cur;\n                        depth[nbv] = d + 1;\n                        dfs(nbv, d + 1);\n                    }\n                }\n            };\n            dfs(root, 0);\n        }\n\n        // compute score\n        long long score = 0;\n        for (int v = 0; v < N; ++v) {\n            score += 1LL * (depth[v] + 1) * A[v];\n        }\n        if (score > best_score) {\n            best_score = score;\n            best_parent = parent;\n        }\n    }\n\n    // output\n    for (int i = 0; i < N; ++i) {\n        if (i) cout << ' ';\n        cout << best_parent[i];\n    }\n    cout << '\\n';\n    return 0;\n}","ahc042":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    const int N = 20;\n    string line;\n    for (int i = 0; i < N; ++i) {\n        cin >> line;          // read and ignore the board\n    }\n\n    // Simply delete every row by shifting it left 20 times.\n    // After 20 left shifts a row becomes completely empty.\n    for (int r = 0; r < N; ++r) {\n        for (int t = 0; t < N; ++t) {  // N = 20\n            cout << 'L' << ' ' << r << '\\n';\n        }\n    }\n    return 0;\n}","ahc044":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N_MAX = 100;\nconst int L = 500000;               // weeks\n\n// ---------------------------------------------------------------------\n// Simulate the cleaning process and return the total absolute error.\n// ---------------------------------------------------------------------\nlong long simulate_and_error(const vector<int>& a, const vector<int>& b,\n                             const vector<int>& target)\n{\n    vector<int> cnt(N_MAX, 0);\n    int cur = 0;                     // week 1\n    cnt[cur]++;\n\n    for (int week = 2; week <= L; ++week) {\n        int tcnt = cnt[cur];             // times cur has already cleaned\n        int nxt = (tcnt & 1) ? a[cur] : b[cur];\n        cur = nxt;\n        cnt[cur]++;\n    }\n\n    long long err = 0;\n    for (int i = 0; i < N_MAX; ++i)\n        err += llabs(cnt[i] - target[i]);\n    return err;\n}\n\n// ---------------------------------------------------------------------\n// Main routine\n// ---------------------------------------------------------------------\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N;\n    long long dummy;\n    if (!(cin >> N >> dummy)) return 0;          // N == 100, dummy == L\n\n    vector<int> T(N);\n    for (int i = 0; i < N; ++i) cin >> T[i];\n\n    // Initial mapping: simple cycle (better than nothing)\n    vector<int> a(N), b(N);\n    for (int i = 0; i < N; ++i) {\n        a[i] = (i + 1) % N;\n        b[i] = (i + 2) % N;                   // two distinct neighbours\n    }\n\n    // ----- basic simulation -----------------------------------------\n    long long best_err = simulate_and_error(a, b, T);\n\n    // ----- local search ------------------------------------------------\n    std::mt19937 rng((unsigned)chrono::steady_clock::now().time_since_epoch().count());\n    std::uniform_int_distribution<int> dist_node(0, N - 1);\n    std::uniform_int_distribution<int> dist_type(0, 1);   // 0 -> a, 1 -> b\n    const int ITER = 400;   // number of mutation attempts\n\n    for (int it = 0; it < ITER; ++it) {\n        int i = dist_node(rng);                  // node to perturb\n        int which = dist_type(rng);              // modify a[i] or b[i]\n        int old_val = (which == 0 ? a[i] : b[i]);\n\n        // propose a new value different from the old one\n        int new_val;\n        do { new_val = dist_node(rng); } while (new_val == old_val);\n\n        if (which == 0) a[i] = new_val; else b[i] = new_val;\n\n        long long cur_err = simulate_and_error(a, b, T);\n\n        if (cur_err < best_err) {               // accept\n            best_err = cur_err;\n        } else {                               // revert\n            if (which == 0) a[i] = old_val; else b[i] = old_val;\n        }\n    }\n\n    // ----- output -----------------------------------------------------\n    for (int i = 0; i < N; ++i) {\n        cout << a[i] << ' ' << b[i] << '\\n';\n    }\n    return 0;\n}","ahc045":"#include <bits/stdc++.h>\nusing namespace std;\n\n// ------------------------------------------------------------\n// Hilbert curve order (2\u2011D Morton with 16 bits per axis)\n// ------------------------------------------------------------\ninline uint64_t hilbert(uint16_t x, uint16_t y, int pow = 16) {\n    uint64_t h = 0;\n    for (int i = pow - 1; i >= 0; --i) {\n        uint64_t mask = 1ULL << i;\n        uint64_t a = (x & mask) != 0;\n        uint64_t b = (y & mask) != 0;\n        uint64_t seg = (a << 1) | b;\n        h = (h << 2) | seg;\n        // rotate\n        if (b) {\n            uint16_t tmp = x;\n            x = y;\n            y = tmp ^ (x + y);   // rotate 90\u00b0\n        }\n    }\n    return h;\n}\n\n// ------------------------------------------------------------\n// main\n// ------------------------------------------------------------\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, Q, L, W;\n    if (!(cin >> N >> M >> Q >> L >> W)) return 0;\n\n    vector<int> G(M);\n    for (int &x : G) cin >> x;\n\n    vector<double> cx(N), cy(N);    // centre of rectangle\n    for (int i = 0; i < N; ++i) {\n        int lx, rx, ly, ry;\n        cin >> lx >> rx >> ly >> ry;\n        cx[i] = (lx + rx) / 2.0;\n        cy[i] = (ly + ry) / 2.0;\n    }\n\n    // Hilbert order of each city\n    vector<uint64_t> H(N);\n    for (int i = 0; i < N; ++i) {\n        uint16_t hx = static_cast<uint16_t>(min<int>(65535,\n                           static_cast<int>(round(cx[i] * 65535.0 / 10000.0))));\n        uint16_t hy = static_cast<uint16_t>(min<int>(65535,\n                           static_cast<int>(round(cy[i] * 65535.0 / 10000.0))));\n        H[i] = hilbert(hx, hy, 16);\n    }\n\n    // ordering of cities by Hilbert curve\n    vector<int> id(N);\n    iota(id.begin(), id.end(), 0);\n    sort(id.begin(), id.end(),\n         [&](int a, int b) { return H[a] < H[b]; });\n\n    cout << \"!\\n\";\n\n    int pos = 0;                     // position in id\n    vector<int> best, parent;\n    vector<char> used;\n    for (int grp = 0; grp < M; ++grp) {\n        int k = G[grp];\n        vector<int> V(id.begin() + pos, id.begin() + pos + k);\n        pos += k;\n\n        // ---------- Prim's MST on V ----------\n        best.assign(k, INT_MAX);\n        parent.assign(k, -1);\n        used.assign(k, 0);\n        best[0] = 0;\n\n        vector<pair<int,int>> mst_edges;\n        mst_edges.reserve(k - 1);\n\n        for (int it = 0; it < k; ++it) {\n            // select unused vertex with minimal best[]\n            int u = -1;\n            int best_val = INT_MAX;\n            for (int i = 0; i < k; ++i) if (!used[i] && best[i] < best_val) {\n                best_val = best[i];\n                u = i;\n            }\n            used[u] = 1;\n            if (parent[u] != -1)\n                mst_edges.push_back({V[u], V[parent[u]]});\n\n            // relax neighbours\n            for (int v = 0; v < k; ++v) if (!used[v]) {\n                double dx = cx[V[u]] - cx[V[v]];\n                double dy = cy[V[u]] - cy[V[v]];\n                int d2 = static_cast<int>(dx * dx + dy * dy + 0.5); // squared distance\n                if (d2 < best[v]) {\n                    best[v] = d2;\n                    parent[v] = u;\n                }\n            }\n        }\n\n        // ---------- output ----------\n        for (int i = 0; i < k; ++i) {\n            if (i) cout << ' ';\n            cout << V[i];\n        }\n        cout << '\\n';\n        for (auto &e : mst_edges) {\n            cout << e.first << ' ' << e.second << '\\n';\n        }\n    }\n    return 0;\n}","ahc046":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Cell {\n    int r, c;\n};\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    int N, M;\n    if (!(cin >> N >> M)) return 0;\n    vector<Cell> pos(M);\n    for (int i = 0; i < M; ++i) cin >> pos[i].r >> pos[i].c;\n\n    const int dr[4] = {-1, 1, 0, 0};\n    const int dc[4] = {0, 0, -1, 1};\n    const char dirChar[4] = {'U', 'D', 'L', 'R'};\n\n    vector<pair<char,char>> output;           // action, direction\n\n    Cell cur = pos[0];\n    for (int k = 0; k < M-1 && (int)output.size() < 800; ++k) {\n        Cell to = pos[k+1];\n\n        // ---- blocked array: all future targets except the next one ----\n        bool blocked[20][20] = {false};\n        for (int t = k+2; t < M; ++t) {\n            blocked[pos[t].r][pos[t].c] = true;\n        }\n\n        // ---- BFS -------------------------------------------------------\n        int dist[20][20];\n        int pr[20][20], pc[20][20];\n        char pd[20][20];\n        memset(dist, -1, sizeof(dist));\n        queue<Cell> q;\n        dist[cur.r][cur.c] = 0;\n        q.push(cur);\n\n        while (!q.empty() && dist[to.r][to.c] == -1) {\n            Cell curc = q.front(); q.pop();\n            for (int d = 0; d < 4; ++d) {\n                int nr = curc.r + dr[d];\n                int nc = curc.c + dc[d];\n                if (nr < 0 || nr >= N || nc < 0 || nc >= N) continue;\n                if (blocked[nr][nc]) continue;\n                if (dist[nr][nc] != -1) continue;\n                dist[nr][nc] = dist[curc.r][curc.c] + 1;\n                pr[nr][nc] = curc.r;\n                pc[nr][nc] = curc.c;\n                pd[nr][nc] = dirChar[d];\n                q.push({nr, nc});\n            }\n        }\n\n        if (dist[to.r][to.c] == -1) { // unreachable \u2013 stop completely\n            break;\n        }\n\n        // ---- reconstruct path -----------------------------------------\n        vector<char> pathDirs;\n        int cr = to.r, cc = to.c;\n        while (!(cr == cur.r && cc == cur.c)) {\n            pathDirs.push_back(pd[cr][cc]);      // direction from predecessor to this cell\n            int tr = pr[cr][cc], tc = pc[cr][cc];\n            cr = tr; cc = tc;\n        }\n        reverse(pathDirs.begin(), pathDirs.end());\n\n        // ---- output actions --------------------------------------------\n        for (char d : pathDirs) {\n            if ((int)output.size() == 800) break;\n            output.emplace_back('M', d);\n        }\n\n        cur = to; // reached the next target\n    }\n\n    // ---- write result -----------------------------------------------\n    for (auto &act : output) {\n        cout << act.first << ' ' << act.second << '\\n';\n    }\n    return 0;\n}"},"16":{"ahc001":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Rect\n{\n    int a, b, c, d;          // [a,c) \u00d7 [b,d)\n};\n\nstatic inline bool overlap(const Rect &R, int a, int b, int w, int h)\n{\n    int c = a + w;\n    int d = b + h;\n    return !(a >= R.c || c <= R.a || b >= R.d || d <= R.b);\n}\n\nstatic inline int clamp_int(int v, int lo, int hi)\n{\n    return max(lo, min(v, hi));\n}\n\nint main()\n{\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int n;\n    if (!(cin >> n)) return 0;\n\n    struct RecInfo\n    {\n        int x, y, r, idx;\n    };\n    vector<RecInfo> recs(n);\n    for (int i = 0; i < n; ++i)\n    {\n        cin >> recs[i].x >> recs[i].y >> recs[i].r;\n        recs[i].idx = i;\n    }\n\n    /* process smaller rectangles first */\n    sort(recs.begin(), recs.end(),\n         [](const RecInfo &l, const RecInfo &r) { return l.r < r.r; });\n\n    vector<Rect> placed;\n    vector<Rect> answer(n);\n\n    const int BOARD = 10000;\n    const int DMAX  = 200;           // search radius for BFS\n\n    for (const auto &info : recs)\n    {\n        int x = info.x, y = info.y, r = info.r;\n\n        /* choose width and height */\n        int w = static_cast<int>(sqrt((double)r));\n        if (w == 0) w = 1;\n        int h = (r + w - 1) / w;          // ceil\n\n        /* base position (always inside board and contains point) */\n        int a0 = clamp_int(x, 0, BOARD - w);\n        int b0 = clamp_int(y, 0, BOARD - h);\n\n        bool placedNow = false;\n\n        /* try base position */\n        bool ok = true;\n        for (const Rect &R : placed)\n            if (overlap(R, a0, b0, w, h))\n            {\n                ok = false;\n                break;\n            }\n        if (ok)\n        {\n            placedNow = true;\n            answer[info.idx] = {a0, b0, a0 + w, b0 + h};\n        }\n\n        /* BFS search around base if needed */\n        if (!placedNow)\n        {\n            for (int d = 0; d <= DMAX && !placedNow; ++d)\n            {\n                for (int dx = -d; dx <= d && !placedNow; ++dx)\n                {\n                    int dy = d - abs(dx);\n                    for (int s : {dy, -dy})\n                    {\n                        int a = a0 + dx;\n                        int b = b0 + s;\n                        if (a < 0 || a > BOARD - w) continue;\n                        if (b < 0 || b > BOARD - h) continue;\n                        if (a > x || b > y) continue;      // keep point inside\n\n                        bool ok2 = true;\n                        for (const Rect &R : placed)\n                        {\n                            if (overlap(R, a, b, w, h))\n                            {\n                                ok2 = false;\n                                break;\n                            }\n                        }\n                        if (ok2)\n                        {\n                            placedNow = true;\n                            answer[info.idx] = {a, b, a + w, b + h};\n                            break;\n                        }\n                    }\n                }\n            }\n        }\n\n        /* final fallback \u2013 try small 1\u00d71 squares near the point */\n        if (!placedNow)\n        {\n            int a_start = clamp_int(x, 0, BOARD - 1);\n            int b_start = clamp_int(y, 0, BOARD - 1);\n            for (int d = 0; d <= DMAX && !placedNow; ++d)\n            {\n                for (int dx = -d; dx <= d && !placedNow; ++dx)\n                {\n                    int dy = d - abs(dx);\n                    for (int s : {dy, -dy})\n                    {\n                        int a = a_start + dx;\n                        int b = b_start + s;\n                        if (a < 0 || a > BOARD - 1) continue;\n                        if (b < 0 || b > BOARD - 1) continue;\n\n                        bool ok2 = true;\n                        for (const Rect &R : placed)\n                        {\n                            if (overlap(R, a, b, 1, 1))\n                            {\n                                ok2 = false;\n                                break;\n                            }\n                        }\n                        if (ok2)\n                        {\n                            placedNow = true;\n                            answer[info.idx] = {a, b, a + 1, b + 1};\n                            break;\n                        }\n                    }\n                }\n            }\n        }\n\n        /* this should always succeed; but just in case */\n        if (!placedNow)\n            answer[info.idx] = {0, 0, 1, 1};\n\n        placed.push_back(answer[info.idx]);\n    }\n\n    /* output in original order */\n    for (int i = 0; i < n; ++i)\n    {\n        const Rect &r = answer[i];\n        cout << r.a << ' ' << r.b << ' ' << r.c << ' ' << r.d << '\\n';\n    }\n\n    return 0;\n}","ahc002":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N          = 50;\nconst int MAXTILES   = N * N;          // 2500\n\n/*------------------------------------------------------------\n   Path state: current position, visited tiles, score, path\n------------------------------------------------------------*/\nstruct Path {\n    int i, j;                       // current coordinates\n    bitset<MAXTILES> used;          // visited tiles\n    long long score;                // accumulated value\n    string moves;                   // path string\n\n    Path() = default;\n    Path(int ii, int jj, int tid, long long val)\n        : i(ii), j(jj), score(val) {\n        used.reset();\n        used.set(tid);\n    }\n};\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int si, sj;\n    if (!(cin >> si >> sj)) return 0;\n\n    int tile[N][N], val[N][N];\n    int maxTid = -1;\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < N; ++j) {\n            cin >> tile[i][j];\n            maxTid = max(maxTid, tile[i][j]);\n        }\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < N; ++j)\n            cin >> val[i][j];\n\n    const int di[4]   = {-1, 1, 0, 0};\n    const int dj[4]   = {0, 0, -1, 1};\n    const char dir[4] = {'U', 'D', 'L', 'R'};\n\n    /*------------------------------------------------------------\n        1. Greedy baseline\n    ------------------------------------------------------------*/\n    vector<char> usedStart(maxTid + 1, 0);\n    usedStart[tile[si][sj]] = 1;\n    string bestPath;\n    long long bestScore = val[si][sj];\n    int ci = si, cj = sj;\n    while (true) {\n        int bestV = -1, bestD = -1;\n        for (int d = 0; d < 4; ++d) {\n            int ni = ci + di[d], nj = cj + dj[d];\n            if (ni < 0 || ni >= N || nj < 0 || nj >= N) continue;\n            int t = tile[ni][nj];\n            if (usedStart[t]) continue;\n            if (val[ni][nj] > bestV) { bestV = val[ni][nj]; bestD = d; }\n        }\n        if (bestD == -1) break;\n        bestPath.push_back(dir[bestD]);\n        ci += di[bestD];\n        cj += dj[bestD];\n        usedStart[tile[ci][cj]] = 1;\n        bestScore += val[ci][cj];\n    }\n    Path bestState(si, sj, tile[si][sj], val[si][sj]);\n    bestState.moves = bestPath;\n\n    /*------------------------------------------------------------\n        2. Beam search (time\u2011controlled)\n    ------------------------------------------------------------*/\n    const int BEAM_W     = 250;\n    const int BEAM_D     = 20;\n    const int BEAM_TL_MS = 950;          // milliseconds\n    auto start = chrono::steady_clock::now();\n\n    while (chrono::duration_cast<chrono::milliseconds>(\n               chrono::steady_clock::now() - start).count() < BEAM_TL_MS) {\n\n        vector<Path> cur;\n        cur.emplace_back(si, sj, tile[si][sj], val[si][sj]);\n\n        for (int step = 0; step < BEAM_D; ++step) {\n            vector<Path> nxt;\n            nxt.reserve(cur.size() * 4);\n\n            for (const auto &p : cur) {\n                for (int d = 0; d < 4; ++d) {\n                    int ni = p.i + di[d], nj = p.j + dj[d];\n                    if (ni < 0 || ni >= N || nj < 0 || nj >= N) continue;\n                    int tid = tile[ni][nj];\n                    if (p.used.test(tid)) continue;\n\n                    Path np = p;          // copy\n                    np.i = ni; np.j = nj;\n                    np.used.set(tid);\n                    np.score += val[ni][nj];\n                    np.moves.push_back(dir[d]);\n\n                    if (np.score > bestScore) {\n                        bestScore = np.score;\n                        bestPath  = np.moves;\n                        bestState = np;\n                    }\n                    nxt.push_back(std::move(np));\n                }\n            }\n            if (nxt.empty()) break;\n\n            sort(nxt.begin(), nxt.end(),\n                 [](const Path &a, const Path &b){ return a.score > b.score; });\n            if ((int)nxt.size() > BEAM_W) nxt.resize(BEAM_W);\n            cur.swap(nxt);\n        }\n    }\n\n    /*------------------------------------------------------------\n        3. Random weighted walks\n    ------------------------------------------------------------*/\n    const int RAND_TL_MS = 1700;           // overall limit\n    mt19937 rng((unsigned)chrono::steady_clock::now()\n                .time_since_epoch().count());\n\n    while (chrono::duration_cast<chrono::milliseconds>(\n               chrono::steady_clock::now() - start).count() < RAND_TL_MS) {\n\n        Path p(si, sj, tile[si][sj], val[si][sj]);\n\n        while (true) {\n            struct Cand{ int d, ni, nj, tid, val; double w; };\n            vector<Cand> cand;\n            for (int d = 0; d < 4; ++d) {\n                int ni = p.i + di[d], nj = p.j + dj[d];\n                if (ni < 0 || ni >= N || nj < 0 || nj >= N) continue;\n                int tid = tile[ni][nj];\n                if (p.used.test(tid)) continue;\n                double w = exp((double)val[ni][nj] / 30.0);\n                cand.push_back({d, ni, nj, tid, val[ni][nj], w});\n            }\n            if (cand.empty()) break;\n\n            if ((int)cand.size() > 2)   // keep the two best for more intensity\n                nth_element(cand.begin(), cand.begin() + 2, cand.end(),\n                            [](const Cand &a, const Cand &b){ return a.val > b.val; });\n\n            double sumw = 0;\n            for (auto &c : cand) sumw += c.w;\n            uniform_real_distribution<double> urd(0, sumw);\n            double r = urd(rng);\n            double acc = 0;\n            Cand chosen = cand[0];\n            for (auto &c : cand) {\n                acc += c.w;\n                if (r <= acc) { chosen = c; break; }\n            }\n\n            p.i = chosen.ni; p.j = chosen.nj;\n            p.used.set(chosen.tid);\n            p.score += chosen.val;\n            p.moves.push_back(dir[chosen.d]);\n\n            if (p.score > bestScore) {\n                bestScore = p.score;\n                bestPath  = p.moves;\n                bestState = p;\n            }\n        }\n    }\n\n    /*------------------------------------------------------------\n        4. Local improvement on last 15 moves\n    ------------------------------------------------------------*/\n    int backCnt = min(15, (int)bestPath.size());\n    for (int back = 1; back <= backCnt; ++back) {\n        string pref = bestPath.substr(0, (int)bestPath.size() - back);\n\n        Path st(si, sj, tile[si][sj], val[si][sj]);\n        for (char c : pref) {\n            int d = (c == 'U') ? 0 : (c == 'D' ? 1 : (c == 'L' ? 2 : 3));\n            int ni = st.i + di[d], nj = st.j + dj[d];\n            int tid = tile[ni][nj];\n            st.i = ni; st.j = nj;\n            st.used.set(tid);\n            st.score += val[ni][nj];\n            st.moves.push_back(c);\n        }\n\n        Path cur = st;\n        while (true) {\n            int bestV = -1, bestD = -1;\n            for (int d = 0; d < 4; ++d) {\n                int ni = cur.i + di[d], nj = cur.j + dj[d];\n                if (ni < 0 || ni >= N || nj < 0 || nj >= N) continue;\n                int t = tile[ni][nj];\n                if (cur.used.test(t)) continue;\n                if (val[ni][nj] > bestV) { bestV = val[ni][nj]; bestD = d; }\n            }\n            if (bestD == -1) break;\n            int ni = cur.i + di[bestD], nj = cur.j + dj[bestD];\n            int tid = tile[ni][nj];\n            cur.i = ni; cur.j = nj;\n            cur.used.set(tid);\n            cur.score += val[ni][nj];\n            cur.moves.push_back(dir[bestD]);\n        }\n\n        if (cur.score > bestScore) {\n            bestScore = cur.score;\n            bestPath  = cur.moves;\n            bestState = cur;\n        }\n    }\n\n    /*------------------------------------------------------------\n        5. Final greedy extension\n    ------------------------------------------------------------*/\n    Path cur = bestState;\n    while (true) {\n        int bestV = -1, bestD = -1;\n        for (int d = 0; d < 4; ++d) {\n            int ni = cur.i + di[d], nj = cur.j + dj[d];\n            if (ni < 0 || ni >= N || nj < 0 || nj >= N) continue;\n            int t = tile[ni][nj];\n            if (cur.used.test(t)) continue;\n            if (val[ni][nj] > bestV) { bestV = val[ni][nj]; bestD = d; }\n        }\n        if (bestD == -1) break;\n        int ni = cur.i + di[bestD], nj = cur.j + dj[bestD];\n        int tid = tile[ni][nj];\n        cur.i = ni; cur.j = nj;\n        cur.used.set(tid);\n        cur.score += val[ni][nj];\n        cur.moves.push_back(dir[bestD]);\n\n        if (cur.score > bestScore) {\n            bestScore = cur.score;\n            bestPath  = cur.moves;\n        }\n    }\n\n    cout << bestPath << '\\n';\n    return 0;\n}","ahc003":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct EdgeInfo {\n    double sum = 0.0;   // cumulative estimated costs\n    int    cnt = 0;     // number of usages\n    double get() const { return cnt ? sum / cnt : 1.0; } // default 1.0\n};\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    const int N = 30;\n    const int V = N * N;\n    const int Q = 1000;\n\n    // edge tables\n    EdgeInfo h[ N ][ N - 1 ];   // horizontal edges (i, j)-(i, j+1)\n    EdgeInfo v[ N - 1 ][ N ];   // vertical   edges (i, j)-(i+1, j)\n\n    // helper lambdas\n    auto node_id = [N](int i, int j){ return i * N + j; };\n\n    for (int qi = 0; qi < Q; ++qi) {\n        int si, sj, ti, tj;\n        if (!(cin >> si >> sj >> ti >> tj)) return 0;\n\n        /* ---------- run Dijkstra with current estimates ---------- */\n        const double INF = 1e100;\n        vector<double> dist(V, INF);\n        vector<int>    prev(V, -1);\n        vector<char>   move(V, 0);   // 'U','D','L','R'\n\n        priority_queue<pair<double,int>, vector<pair<double,int>>, greater<>> pq;\n        int sid = node_id(si, sj);\n        int tid = node_id(ti, tj);\n        dist[sid] = 0.0;\n        pq.emplace(0.0, sid);\n\n        while (!pq.empty()) {\n            auto [d, u] = pq.top(); pq.pop();\n            if (d != dist[u]) continue;\n            if (u == tid) break;\n\n            int ui = u / N, uj = u % N;\n\n            // left\n            if (uj > 0) {\n                int vId = node_id(ui, uj-1);\n                double w = h[ui][uj-1].get();\n                if (dist[vId] > d + w) {\n                    dist[vId] = d + w;\n                    prev[vId] = u;\n                    move[vId] = 'L';\n                    pq.emplace(dist[vId], vId);\n                }\n            }\n            // right\n            if (uj < N-1) {\n                int vId = node_id(ui, uj+1);\n                double w = h[ui][uj].get();\n                if (dist[vId] > d + w) {\n                    dist[vId] = d + w;\n                    prev[vId] = u;\n                    move[vId] = 'R';\n                    pq.emplace(dist[vId], vId);\n                }\n            }\n            // up\n            if (ui > 0) {\n                int vId = node_id(ui-1, uj);\n                double w = v[ui-1][uj].get();\n                if (dist[vId] > d + w) {\n                    dist[vId] = d + w;\n                    prev[vId] = u;\n                    move[vId] = 'U';\n                    pq.emplace(dist[vId], vId);\n                }\n            }\n            // down\n            if (ui < N-1) {\n                int vId = node_id(ui+1, uj);\n                double w = v[ui][uj].get();\n                if (dist[vId] > d + w) {\n                    dist[vId] = d + w;\n                    prev[vId] = u;\n                    move[vId] = 'D';\n                    pq.emplace(dist[vId], vId);\n                }\n            }\n        }\n\n        /* ---------- reconstruct path ---------- */\n        string path;\n        int cur = tid;\n        while (cur != sid) {\n            path.push_back(move[cur]);\n            cur = prev[cur];\n        }\n        reverse(path.begin(), path.end());\n\n        /* ---------- output and flush ---------- */\n        cout << path << '\\n' << flush;\n\n        /* ---------- read judge's reply ---------- */\n        long long x;\n        if (!(cin >> x)) return 0;\n\n        /* ---------- update edge estimates ---------- */\n        int ci = si, cj = sj;\n        double cost_per_edge = static_cast<double>(x) / path.size();\n        for (char mv : path) {\n            if (mv == 'R') {\n                h[ci][cj].sum += cost_per_edge;\n                h[ci][cj].cnt += 1;\n                ++cj;\n            } else if (mv == 'L') {\n                h[ci][cj-1].sum += cost_per_edge;\n                h[ci][cj-1].cnt += 1;\n                --cj;\n            } else if (mv == 'D') {\n                v[ci][cj].sum += cost_per_edge;\n                v[ci][cj].cnt += 1;\n                ++ci;\n            } else if (mv == 'U') {\n                v[ci-1][cj].sum += cost_per_edge;\n                v[ci-1][cj].cnt += 1;\n                --ci;\n            }\n        }\n    }\n    return 0;\n}","ahc004":"#include <bits/stdc++.h>\nusing namespace std;\n\n/***  fast splitmix64 PRNG  ***/\nstruct RNG {\n    unsigned long long state;\n    RNG() : state(chrono::steady_clock::now().time_since_epoch().count()) {}\n    unsigned long long next() {\n        unsigned long long z = (state += 0x9e3779b97f4a7c15ULL);\n        z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ULL;\n        z = (z ^ (z >> 27)) * 0x94d049bb133111ebULL;\n        return z ^ (z >> 31);\n    }\n    int randint(int l, int r) { return l + int(next() % (unsigned long long)(r - l + 1)); }\n};\n\n/***  rolling hash helpers  ***/\nconstexpr unsigned long long BASE = 1315423911ULL;            // 64\u2011bit odd number\ninline unsigned long long sub_hash(const unsigned long long pref[41],\n                                   const unsigned long long pw[41],\n                                   int l, int r)               // [l,r)\n{\n    return pref[r] - pref[l] * pw[r - l];\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M;\n    if (!(cin >> N >> M)) return 0;\n    vector<string> S(M);\n    for (int i = 0; i < M; ++i) cin >> S[i];\n\n    /* powers of BASE up to 40 */\n    unsigned long long pw[41];\n    pw[0] = 1;\n    for (int i = 1; i <= 40; ++i) pw[i] = pw[i - 1] * BASE;\n\n    /* hash of each input string */\n    vector<unsigned long long> hS(M);\n    for (int i = 0; i < M; ++i) {\n        unsigned long long h = 0;\n        for (char c : S[i]) h = h * BASE + unsigned(c);\n        hS[i] = h;\n    }\n\n    /* character frequency for biasing random generator   */\n    array<long long, 8> freq{};                     // for A..H\n    for (const string &t : S)\n        for (char c : t) ++freq[c - 'A'];\n    vector<long long> cum(9, 0);\n    for (int i = 0; i < 8; ++i) cum[i + 1] = cum[i] + freq[i] + 1;\n    long long totalWeight = cum[8];\n\n    RNG rng;\n    auto randomLetter = [&]() -> char {\n        long long r = rng.next() % totalWeight + 1;          // [1,totalWeight]\n        int idx = int(lower_bound(cum.begin(), cum.end(), r) - cum.begin()) - 1;\n        return char('A' + idx);\n    };\n\n    constexpr double TIME_LIMIT = 2.75;          // seconds\n    constexpr int MAX_RANDOM = 8000;             // samples in the random phase\n    constexpr double HILL_LIMIT = 0.7;           // seconds for hill\u2011climb\n\n    vector<string> bestMat(N, string(N, '.'));\n    int bestScore = -1;\n\n    unordered_set<unsigned long long> subs;\n    subs.reserve(11000);\n    subs.max_load_factor(0.5);\n\n    auto startAll = chrono::steady_clock::now();\n\n    /* -------------------- random search -------------------- */\n    for (int it = 0; it < MAX_RANDOM; ++it) {\n        if (chrono::duration<double>(chrono::steady_clock::now() - startAll).count() >= TIME_LIMIT)\n            break;\n\n        vector<string> mat(N, string(N, 'A'));\n        for (int i = 0; i < N; ++i)\n            for (int j = 0; j < N; ++j)\n                mat[i][j] = randomLetter();\n\n        subs.clear();\n        unsigned long long pref[41];\n\n        for (int i = 0; i < N; ++i) {                     // rows\n            string r2 = mat[i] + mat[i];\n            pref[0] = 0;\n            for (int k = 0; k < 40; ++k)\n                pref[k + 1] = pref[k] * BASE + unsigned(r2[k]);\n\n            for (int st = 0; st < N; ++st)\n                for (int len = 2; len <= 12; ++len)\n                    subs.insert(sub_hash(pref, pw, st, st + len));\n        }\n\n        string col(N, 'A');\n        for (int j = 0; j < N; ++j) {                     // columns\n            for (int i = 0; i < N; ++i) col[i] = mat[i][j];\n            string c2 = col + col;\n            pref[0] = 0;\n            for (int k = 0; k < 40; ++k)\n                pref[k + 1] = pref[k] * BASE + unsigned(c2[k]);\n\n            for (int st = 0; st < N; ++st)\n                for (int len = 2; len <= 12; ++len)\n                    subs.insert(sub_hash(pref, pw, st, st + len));\n        }\n\n        int score = 0;\n        for (int i = 0; i < M; ++i)\n            if (subs.find(hS[i]) != subs.end()) ++score;\n\n        if (score > bestScore) {\n            bestScore = score;\n            bestMat   = mat;\n        }\n    }\n\n    /* -------------------- hill\u2011climb with random moves -------------------- */\n    auto hillStart = chrono::steady_clock::now();\n    while (chrono::duration<double>(chrono::steady_clock::now() - hillStart).count() < HILL_LIMIT) {\n        vector<string> cand = bestMat;\n\n        int moves = rng.randint(1, 5);                    // modify 1\u20135 cells\n        for (int mv = 0; mv < moves; ++mv) {\n            int r = rng.randint(0, N - 1);\n            int c = rng.randint(0, N - 1);\n            cand[r][c] = randomLetter();\n        }\n\n        subs.clear();\n        unsigned long long pref[41];\n        string col(N, 'A');\n\n        for (int i = 0; i < N; ++i) {                     // rows\n            string r2 = cand[i] + cand[i];\n            pref[0] = 0;\n            for (int k = 0; k < 40; ++k)\n                pref[k + 1] = pref[k] * BASE + unsigned(r2[k]);\n\n            for (int st = 0; st < N; ++st)\n                for (int len = 2; len <= 12; ++len)\n                    subs.insert(sub_hash(pref, pw, st, st + len));\n        }\n\n        for (int j = 0; j < N; ++j) {                     // columns\n            for (int i = 0; i < N; ++i) col[i] = cand[i][j];\n            string c2 = col + col;\n            pref[0] = 0;\n            for (int k = 0; k < 40; ++k)\n                pref[k + 1] = pref[k] * BASE + unsigned(c2[k]);\n\n            for (int st = 0; st < N; ++st)\n                for (int len = 2; len <= 12; ++len)\n                    subs.insert(sub_hash(pref, pw, st, st + len));\n        }\n\n        int score = 0;\n        for (int i = 0; i < M; ++i)\n            if (subs.find(hS[i]) != subs.end()) ++score;\n\n        if (score > bestScore) {\n            bestScore = score;\n            bestMat   = cand;\n        }\n    }\n\n    /* -------------------- output -------------------- */\n    for (int i = 0; i < N; ++i)\n        cout << bestMat[i] << '\\n';\n    return 0;\n}","ahc005":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    int N, si, sj;\n    if (!(cin >> N >> si >> sj)) return 0;\n    vector<string> g(N);\n    for (int i = 0; i < N; ++i) cin >> g[i];\n\n    auto inb = [&](int x, int y) { return 0 <= x && x < N && 0 <= y && y < N; };\n\n    const char dirs[4] = {'U', 'D', 'L', 'R'};\n    const int dx[4] = {-1, 1, 0, 0};\n    const int dy[4] = {0, 0, -1, 1};\n\n    // map each road cell to an index\n    vector<vector<int>> idx(N, vector<int>(N, -1));\n    vector<pair<int,int>> pos;\n    int cnt = 0;\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < N; ++j)\n            if (g[i][j] != '#') {\n                idx[i][j] = cnt++;\n                pos.emplace_back(i, j);\n            }\n\n    if (cnt == 0) {               // no road - trivial output\n        cout << '\\n';\n        return 0;\n    }\n\n    // adjacency list\n    vector<vector<int>> adj(cnt);\n    for (int id = 0; id < cnt; ++id) {\n        auto [x, y] = pos[id];\n        for (int d = 0; d < 4; ++d) {\n            int nx = x + dx[d], ny = y + dy[d];\n            if (inb(nx, ny) && idx[nx][ny] != -1)\n                adj[id].push_back(idx[nx][ny]);\n        }\n    }\n\n    int start = idx[si][sj];\n    vector<char> ans;\n\n    // visited mask\n    vector<bool> visited(cnt, false);\n    visited[start] = true;\n    int cur = start;\n\n    // helper lambda: BFS from src, returns dist and parent\n    auto bfs = [&](int src, vector<int> &dist, vector<int> &parent) {\n        dist.assign(cnt, -1);\n        parent.assign(cnt, -1);\n        deque<int> q;\n        dist[src] = 0;\n        q.push_back(src);\n        while (!q.empty()) {\n            int u = q.front();\n            q.pop_front();\n            for (int v : adj[u]) {\n                if (dist[v] == -1) {\n                    dist[v] = dist[u] + 1;\n                    parent[v] = u;\n                    q.push_back(v);\n                }\n            }\n        }\n    };\n\n    // main loop: always go to the nearest unvisited node\n    while (true) {\n        int remaining = 0;\n        for (bool v : visited) if (!v) ++remaining;\n        if (remaining == 0) break;\n\n        vector<int> dist, parent;\n        bfs(cur, dist, parent);\n\n        int target = -1;\n        int best = INT_MAX;\n        for (int v = 0; v < cnt; ++v) {\n            if (!visited[v] && dist[v] != -1 && dist[v] < best) {\n                best = dist[v];\n                target = v;\n            }\n        }\n        if (target == -1) break; // should not happen\n\n        // reconstruct path from cur to target\n        vector<int> path;\n        int p = target;\n        while (p != -1) {\n            path.push_back(p);\n            if (p == cur) break;\n            p = parent[p];\n        }\n        reverse(path.begin(), path.end()); // from cur to target\n\n        // emit moves along the path\n        for (size_t i = 1; i < path.size(); ++i) {\n            auto [x1, y1] = pos[path[i-1]];\n            auto [x2, y2] = pos[path[i]];\n            int d = -1;\n            if (x2 == x1-1 && y2 == y1) d = 0;\n            else if (x2 == x1+1 && y2 == y1) d = 1;\n            else if (x2 == x1 && y2 == y1-1) d = 2;\n            else if (x2 == x1 && y2 == y1+1) d = 3;\n            if (d != -1) ans.push_back(dirs[d]);\n            visited[path[i]] = true;\n        }\n        cur = target;\n    }\n\n    // finally return to start\n    vector<int> dist, parent;\n    bfs(cur, dist, parent);\n    vector<int> path;\n    int p = start;\n    while (p != -1) {\n        path.push_back(p);\n        if (p == cur) break;\n        p = parent[p];\n    }\n    reverse(path.begin(), path.end()); // cur -> start\n\n    for (size_t i = 1; i < path.size(); ++i) {\n        auto [x1, y1] = pos[path[i-1]];\n        auto [x2, y2] = pos[path[i]];\n        int d = -1;\n        if (x2 == x1-1 && y2 == y1) d = 0;\n        else if (x2 == x1+1 && y2 == y1) d = 1;\n        else if (x2 == x1 && y2 == y1-1) d = 2;\n        else if (x2 == x1 && y2 == y1+1) d = 3;\n        if (d != -1) ans.push_back(dirs[d]);\n    }\n\n    string out(ans.begin(), ans.end());\n    cout << out << '\\n';\n    return 0;\n}","future-contest-2022-qual":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    int N, M, K, R;\n    if (!(cin >> N >> M >> K >> R)) return 0;\n\n    /* read task vectors: keep only the sum of skill levels (sum_d) */\n    vector<long long> sum_d(N, 0);\n    for (int i = 0; i < N; ++i) {\n        long long s = 0;\n        for (int j = 0; j < K; ++j) {\n            int x; cin >> x;\n            s += x;\n        }\n        sum_d[i] = s;\n    }\n\n    /* dependency graph */\n    vector<vector<int>> out(N);\n    vector<int> indeg(N, 0);\n    for (int i = 0; i < R; ++i) {\n        int u, v; cin >> u >> v;            // 1\u2011based\n        --u; --v;\n        out[u].push_back(v);\n        ++indeg[v];\n    }\n\n    /* longest weighted path (critical\u2011path distance) */\n    vector<long long> dist(N, 0);\n    for (int i = N - 1; i >= 0; --i) {\n        long long best = 0;\n        for (int v : out[i]) best = max(best, dist[v]);\n        dist[i] = sum_d[i] + best;\n    }\n\n    /* priority for ready tasks: larger is better   (dist + sum_d)  */\n    auto cmpTask = [&](int a, int b) {\n        long long pa = dist[a] + sum_d[a];\n        long long pb = dist[b] + sum_d[b];\n        if (pa != pb) return pa < pb;   // max\u2011heap\n        return a < b;                    // tie breaker\n    };\n    priority_queue<int, vector<int>, decltype(cmpTask)> ready(cmpTask);\n\n    for (int i = 0; i < N; ++i)\n        if (indeg[i] == 0) ready.push(i);\n\n    /* worker status */\n    vector<int> worker_task(M, -1);        // current task id, -1 if idle\n    vector<long long> sum_t(M, 0);         // total time finished\n    vector<long long> sum_d_done(M, 0);    // total difficulty of finished tasks\n    vector<int> start_day(N, -1);\n    vector<char> started(N, 0), finished(N, 0);\n\n    int day = 0;\n    while (true) {\n        ++day;\n\n        /* ---- assign ready tasks to idle workers ---- */\n        vector<int> idle_workers;\n        for (int w = 0; w < M; ++w)\n            if (worker_task[w] == -1) idle_workers.push_back(w);\n\n        /* worker efficiency: avg time per unit difficulty (smaller better) */\n        auto eff = [&](int w) -> double {\n            if (sum_d_done[w] <= 0) return 1e12; // unknown -> very slow\n            return 1.0 * sum_t[w] / sum_d_done[w];\n        };\n        sort(idle_workers.begin(), idle_workers.end(),\n             [&](int a, int b) { return eff(a) < eff(b); });\n\n        vector<pair<int,int>> assignments;\n        for (int w : idle_workers) {\n            if (ready.empty()) break;\n            int t = ready.top(); ready.pop();\n            worker_task[w] = t;\n            started[t] = 1;\n            start_day[t] = day;\n            assignments.emplace_back(w, t);\n        }\n\n        /* output assignments for this day */\n        cout << assignments.size();\n        for (auto &p : assignments)\n            cout << ' ' << (p.first + 1) << ' ' << (p.second + 1);\n        cout << '\\n';\n        cout.flush();\n\n        /* ---- read finished workers this day ---- */\n        int nfin;\n        if (!(cin >> nfin)) break;           // safety\n        if (nfin == -1) break;               // finished or 2000 days\n\n        vector<int> fin_list(nfin);\n        for (int i = 0; i < nfin; ++i) cin >> fin_list[i];\n\n        for (int fid : fin_list) {\n            int w = fid - 1;\n            int t = worker_task[w];           // the task that ended\n\n            int duration = day - start_day[t] + 1;   // actual length\n\n            sum_t[w] += duration;\n            sum_d_done[w] += sum_d[t];\n            worker_task[w] = -1;                // worker becomes idle\n\n            for (int v : out[t]) {\n                if (--indeg[v] == 0 && !started[v] && !finished[v])\n                    ready.push(v);\n            }\n            finished[t] = 1;\n        }\n    }\n    return 0;\n}","ahc006":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Order {\n    int a, b, c, d;\n};\n\ninline long long manhattan(int x1, int y1, int x2, int y2) {\n    return llabs(x1 - x2) + llabs(y1 - y2);\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    const int N = 1000;\n    vector<Order> ord(N);\n    for (int i = 0; i < N; ++i) {\n        cin >> ord[i].a >> ord[i].b >> ord[i].c >> ord[i].d;\n    }\n\n    /* 1. select 50 orders with smallest \u201coffice \u2192 restaurant \u2192 dest\u201d cost */\n    struct Item { long long cost; int idx; };\n    vector<Item> items;\n    items.reserve(N);\n    for (int i = 0; i < N; ++i) {\n        long long cost =\n            manhattan(400, 400, ord[i].a, ord[i].b) +\n            manhattan(ord[i].a, ord[i].b, ord[i].c, ord[i].d);\n        items.push_back({cost, i});\n    }\n    sort(items.begin(), items.end(),\n         [](const Item& x, const Item& y){ return x.cost < y.cost; });\n\n    const int M = 50;\n    vector<int> chosen;\n    chosen.reserve(M);\n    for (int i = 0; i < M; ++i) chosen.push_back(items[i].idx);\n\n    /* 2. greedy interleaving route */\n    vector<pair<int,int>> route;\n    route.emplace_back(400, 400);           // start\n    int curX = 400, curY = 400;\n\n    vector<bool> visited(M, false);         // restaurant visited\n    vector<bool> delivered(M, false);       // destination delivered\n    int remainRestaurants = M;\n    int remainDeliveries = 0;\n\n    while (remainRestaurants + remainDeliveries > 0) {\n        int bestIdx = -1;\n        long long bestDist = LLONG_MAX;\n        bool targetIsRestaurant = false;\n\n        // not yet visited restaurants\n        for (int i = 0; i < M; ++i) if (!visited[i]) {\n            int x = ord[chosen[i]].a;\n            int y = ord[chosen[i]].b;\n            long long d = manhattan(curX, curY, x, y);\n            if (d < bestDist) {\n                bestDist = d;\n                bestIdx = i;\n                targetIsRestaurant = true;\n            }\n        }\n        // available deliveries\n        for (int i = 0; i < M; ++i) if (visited[i] && !delivered[i]) {\n            int x = ord[chosen[i]].c;\n            int y = ord[chosen[i]].d;\n            long long d = manhattan(curX, curY, x, y);\n            if (d < bestDist) {\n                bestDist = d;\n                bestIdx = i;\n                targetIsRestaurant = false;\n            }\n        }\n\n        if (bestIdx == -1) break;   // defensive\n\n        if (targetIsRestaurant) {\n            // go to restaurant\n            int x = ord[chosen[bestIdx]].a;\n            int y = ord[chosen[bestIdx]].b;\n            route.emplace_back(x, y);\n            curX = x; curY = y;\n            visited[bestIdx] = true;\n            remainRestaurants--;\n            remainDeliveries++;            // its destination becomes available\n        } else {\n            // go to destination\n            int x = ord[chosen[bestIdx]].c;\n            int y = ord[chosen[bestIdx]].d;\n            route.emplace_back(x, y);\n            curX = x; curY = y;\n            delivered[bestIdx] = true;\n            remainDeliveries--;\n        }\n    }\n\n    route.emplace_back(400, 400);           // return to office\n\n    /* 3. output */\n    cout << M;\n    for (int idx : chosen) cout << ' ' << idx + 1;   // 1\u2011based indexes\n    cout << '\\n';\n\n    cout << route.size();\n    for (auto [x, y] : route) cout << ' ' << x << ' ' << y;\n    cout << '\\n';\n\n    return 0;\n}","ahc007":"#include <bits/stdc++.h>\nusing namespace std;\n\n// ---------- DSU ----------\nstruct DSU {\n    vector<int> p, r;\n    DSU(int n = 0) { init(n); }\n    void init(int n) {\n        p.resize(n);\n        r.assign(n, 0);\n        iota(p.begin(), p.end(), 0);\n    }\n    int find(int x) {\n        if (p[x] == x) return x;\n        return p[x] = find(p[x]);            // path compression\n    }\n    bool unite(int a, int b) {\n        a = find(a); b = find(b);\n        if (a == b) return false;\n        if (r[a] < r[b]) swap(a, b);\n        p[b] = a;\n        if (r[a] == r[b]) ++r[a];\n        return true;\n    }\n};\n\n// ---------- main ----------\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    const int N = 400, M = 1995;\n    // read coordinates, but we never use them\n    for (int i = 0; i < N; ++i) {\n        int x, y; cin >> x >> y;\n    }\n\n    vector<int> u(M), v(M);\n    for (int i = 0; i < M; ++i) {\n        cin >> u[i] >> v[i];\n    }\n\n    DSU dsu(N);\n\n    for (int i = 0; i < M; ++i) {\n        int li;            // the real length\n        cin >> li;\n\n        if (dsu.unite(u[i], v[i])) {\n            cout << 1 << '\\n';\n        } else {\n            cout << 0 << '\\n';\n        }\n        cout.flush();        // essential for interactive judge\n    }\n    return 0;\n}","ahc008":"/*********************************************************************\n *  Heuristic solver for the AtCoder Human\u2011Pet partitioning problem.\n *\n *  This version improves over a pure \u201cdo nothing\u201d strategy by:\n *    \u2022 trying to block a neighbouring square that most reduces the\n *      reachable area of the human.\n *    \u2022 if no block can reduce the area, the human moves to a\n *      neighbouring passable square.\n *\n *  All rules of the original problem are respected.\n *********************************************************************/\n#include <bits/stdc++.h>\nusing namespace std;\n\nconst int H = 30, W = 30;\n\n/* Directions: 0=up,1=down,2=left,3=right */\nint dr[4] = {-1, 1, 0, 0};\nint dc[4] = {0, 0, -1, 1};\n\nchar blockChar[4] = {'u', 'd', 'l', 'r'};\nchar moveChar[4]  = {'U', 'D', 'L', 'R'};\n\ninline bool inb(int r, int c) { return 0 <= r && r < H && 0 <= c && c < W; }\n\n/* BFS that returns the number of cells reachable from (sr,sc)\n * on a grid where imp[r][c]==true denotes an impassable cell.   */\nint reachArea(const bool (&imp)[H][W], int sr, int sc) {\n    bool vis[H][W] = {};\n    array<pair<int,int>, H*W> q;\n    int head = 0, tail = 0;\n    q[tail++] = {sr, sc};\n    vis[sr][sc] = true;\n    int cnt = 0;\n    while (head < tail) {\n        auto [r, c] = q[head++];\n        ++cnt;\n        for (int d = 0; d < 4; ++d) {\n            int nr = r + dr[d], nc = c + dc[d];\n            if (!inb(nr, nc) || imp[nr][nc] || vis[nr][nc]) continue;\n            vis[nr][nc] = true;\n            q[tail++] = {nr, nc};\n        }\n    }\n    return cnt;\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N;                                   // number of pets\n    if (!(cin >> N)) return 0;               // no input\n    struct Pos { int r, c; };\n    vector<Pos> pets(N);\n    for (int i = 0; i < N; ++i) {\n        int x, y, t;   // type is irrelevant for us\n        cin >> x >> y >> t;\n        pets[i] = {x - 1, y - 1};\n    }\n\n    int M;                                   // number of humans\n    cin >> M;\n    vector<Pos> humans(M);\n    for (int i = 0; i < M; ++i) {\n        int x, y; cin >> x >> y;\n        humans[i] = {x - 1, y - 1};\n    }\n\n    /* grid state */\n    bool imp[H][W] = {};          // impassable\n    bool humanGrid[H][W] = {};    // current positions of humans\n    bool petGrid[H][W] = {};      // current positions of pets\n\n    for (auto &p : pets) petGrid[p.r][p.c] = true;\n    for (auto &h : humans) humanGrid[h.r][h.c] = true;\n\n    /* helper: check if we can block (r,c) this turn.  */\n    auto canBlock = [&](int r, int c) -> bool {\n        if (imp[r][c] || humanGrid[r][c] || petGrid[r][c]) return false;\n        for (int dr2 = -1; dr2 <= 1; ++dr2)\n            for (int dc2 = -1; dc2 <= 1; ++dc2) {\n                if (dr2 == 0 && dc2 == 0) continue;\n                int nr = r + dr2, nc = c + dc2;\n                if (!inb(nr, nc)) continue;\n                if (petGrid[nr][nc]) return false;\n            }\n        return true;\n    };\n\n    for (int turn = 0; turn < 300; ++turn) {\n        string actions(M, '.');\n\n        /* decide actions for all humans */\n        for (int i = 0; i < M; ++i) {\n            int cr = humans[i].r, cc = humans[i].c;\n            int baseArea = reachArea(imp, cr, cc);\n\n            int bestDir = -1, bestRed = -1;\n            for (int d = 0; d < 4; ++d) {\n                int nr = cr + dr[d], nc = cc + dc[d];\n                if (!inb(nr, nc) || !canBlock(nr, nc)) continue;\n                imp[nr][nc] = true;          // test\n                int area = reachArea(imp, cr, cc);\n                int red = baseArea - area;\n                imp[nr][nc] = false;\n                if (red > bestRed) {\n                    bestRed = red;\n                    bestDir = d;\n                }\n            }\n\n            if (bestDir != -1 && bestRed > 0) {\n                int nr = cr + dr[bestDir], nc = cc + dc[bestDir];\n                actions[i] = blockChar[bestDir];\n                imp[nr][nc] = true;          // commit\n            } else {\n                /* try to move */\n                bool moved = false;\n                for (int d = 0; d < 4 && !moved; ++d) {\n                    int nr = cr + dr[d], nc = cc + dc[d];\n                    if (!inb(nr, nc) || imp[nr][nc]) continue;\n                    actions[i] = moveChar[d];\n                    humans[i] = {nr, nc};\n                    moved = true;\n                }\n                /* if still no move, stay '.' */\n            }\n        }\n\n        cout << actions << '\\n';\n        cout.flush();\n\n        /* read pet movements */\n        vector<string> mv(N);\n        for (int i = 0; i < N; ++i) cin >> mv[i];\n\n        /* update pets */\n        memset(petGrid, 0, sizeof(petGrid));\n        for (int i = 0; i < N; ++i) {\n            Pos &p = pets[i];\n            for (char ch : mv[i]) {\n                int d = (ch == 'U' ? 0 : ch == 'D' ? 1 : ch == 'L' ? 2 : 3);\n                int nr = p.r + dr[d], nc = p.c + dc[d];\n                if (!inb(nr, nc) || imp[nr][nc]) continue;\n                p.r = nr; p.c = nc;\n            }\n            petGrid[p.r][p.c] = true;\n        }\n\n        /* update human grid for next turn */\n        memset(humanGrid, 0, sizeof(humanGrid));\n        for (auto &h : humans) humanGrid[h.r][h.c] = true;\n    }\n    return 0;\n}","ahc009":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Node { int r, c; };\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int sr, sc, tr, tc;\n    double p;                 // not used in the construction\n    if (!(cin >> sr >> sc >> tr >> tc >> p)) return 0;\n\n    vector<string> h(20);\n    for (int i = 0; i < 20; ++i) cin >> h[i];\n    vector<string> v(19);\n    for (int i = 0; i < 19; ++i) cin >> v[i];\n\n    const int N = 20;\n    vector<vector<bool>> seen(N, vector<bool>(N, false));\n    vector<vector<int>> pr(N, vector<int>(N, -1));\n    vector<vector<int>> pc(N, vector<int>(N, -1));\n    vector<vector<char>> pd(N, vector<char>(N, 0));\n\n    queue<Node> q;\n    q.push({sr, sc});\n    seen[sr][sc] = true;\n\n    const int dr[4] = {-1, 1, 0, 0};\n    const int dc[4] = {0, 0, -1, 1};\n    const char dirc[4] = {'U', 'D', 'L', 'R'};\n\n    /* BFS for shortest path to the office */\n    while (!q.empty()) {\n        Node cur = q.front(); q.pop();\n        if (cur.r == tr && cur.c == tc) break;\n\n        for (int k = 0; k < 4; ++k) {\n            int nr = cur.r + dr[k];\n            int nc = cur.c + dc[k];\n            if (nr < 0 || nr >= N || nc < 0 || nc >= N) continue;\n\n            bool blocked = false;\n            if (dr[k] == -1)        blocked = (v[nr][nc] == '1');      // up\n            else if (dr[k] == 1)   blocked = (v[cur.r][cur.c] == '1'); // down\n            else if (dc[k] == -1)  blocked = (h[cur.r][nc] == '1');   // left\n            else                   blocked = (h[cur.r][cur.c] == '1'); // right\n\n            if (blocked || seen[nr][nc]) continue;\n            seen[nr][nc] = true;\n            pr[nr][nc] = cur.r;\n            pc[nr][nc] = cur.c;\n            pd[nr][nc] = dirc[k];\n            q.push({nr, nc});\n        }\n    }\n\n    /* reconstruct the shortest path P (start \u2192 office) */\n    vector<char> P;\n    int cr = tr, cc = tc;\n    while (!(cr == sr && cc == sc)) {\n        P.push_back(pd[cr][cc]);          // direction from parent to this cell\n        int nr = pr[cr][cc];\n        int nc = pc[cr][cc];\n        cr = nr; cc = nc;\n    }\n    reverse(P.begin(), P.end());         // forward path\n\n    /* robust forward path P\u2082 : each step twice */\n    vector<char> P2;\n    P2.reserve(P.size() * 2);\n    for (char c : P) {\n        P2.push_back(c);\n        P2.push_back(c);\n    }\n\n    /* robust reverse path R\u2082 : reverse of P\u2082 with opposite directions */\n    vector<char> R2;\n    R2.reserve(P2.size());\n    for (auto it = P2.rbegin(); it != P2.rend(); ++it) {\n        char c = *it;\n        char oc = (c == 'U') ? 'D' :\n                  (c == 'D') ? 'U' :\n                  (c == 'L') ? 'R' : 'L';\n        R2.push_back(oc);\n    }\n\n    int lenP2 = (int)P2.size();          // always <= 120\n    int k = (200 / lenP2 - 1) / 2;       // maximal number of full cycles\n    if (k < 0) k = 0;\n\n    string answer;\n    answer.reserve((2 * k + 1) * lenP2);\n    answer.append(P2.begin(), P2.end());\n    for (int i = 0; i < k; ++i) {\n        answer.append(R2.begin(), R2.end());\n        answer.append(P2.begin(), P2.end());\n    }\n\n    cout << answer << '\\n';\n    return 0;\n}","ahc010":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    // read and ignore the 30\u00d730 input grid\n    string line;\n    for (int i = 0; i < 30; ++i) {\n        if (!(cin >> line)) return 0;\n    }\n\n    // produce a deterministic rotation pattern\n    string ans;\n    ans.reserve(900);\n    for (int i = 0; i < 30; ++i) {\n        for (int j = 0; j < 30; ++j) {\n            int r = (i + j) % 4;          // 0 .. 3\n            ans.push_back(char('0' + r));\n        }\n    }\n\n    cout << ans << '\\n';\n    return 0;\n}","ahc011":"#include <bits/stdc++.h>\nusing namespace std;\n\n// ------------------------------------------------------------\n//  DSU with number of vertices and edges\nstruct DSU {\n    vector<int> p, sz, ed;\n    DSU(int n = 0) { init(n); }\n    void init(int n) {\n        p.resize(n);\n        sz.assign(n, 1);\n        ed.assign(n, 0);\n        iota(p.begin(), p.end(), 0);\n    }\n    int find(int x) { return p[x] == x ? x : p[x] = find(p[x]); }\n    void unite(int a, int b) {\n        a = find(a); b = find(b);\n        if (a == b) { ed[a]++; return; }\n        if (sz[a] < sz[b]) swap(a, b);\n        p[b] = a;\n        sz[a] += sz[b];\n        ed[a] += ed[b] + 1;\n    }\n};\n\n// ------------------------------------------------------------\n//  convert hex char to line mask\ninline int hexMask(char c) {\n    if ('0' <= c && c <= '9') return c - '0';\n    return 10 + (c - 'a');\n}\n\n//  largest tree size on the board, empty position (ei,ej)\nint largestTree(const vector<string>& g, int ei, int ej, int N) {\n    int MN = N * N;\n    DSU dsu(MN);\n    vector<bool> empty(MN, false);\n    empty[ei * N + ej] = true;\n\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < N; ++j) {\n            int idx = i * N + j;\n            if (empty[idx]) continue;\n            int m = hexMask(g[i][j]);\n\n            // right neighbour\n            if (j + 1 < N) {\n                int ni = i, nj = j + 1, nidx = ni * N + nj;\n                if (!empty[nidx]) {\n                    int nm = hexMask(g[ni][nj]);\n                    if ((m & 4) && (nm & 1))\n                        dsu.unite(idx, nidx);\n                }\n            }\n            // down neighbour\n            if (i + 1 < N) {\n                int ni = i + 1, nj = j, nidx = ni * N + nj;\n                if (!empty[nidx]) {\n                    int nm = hexMask(g[ni][nj]);\n                    if ((m & 8) && (nm & 2))\n                        dsu.unite(idx, nidx);\n                }\n            }\n        }\n\n    int best = 0;\n    for (int idx = 0; idx < MN; ++idx)\n        if (dsu.p[idx] == idx && !empty[idx])\n            if (dsu.ed[idx] == dsu.sz[idx] - 1)\n                best = max(best, dsu.sz[idx]);\n    return best;\n}\n\n//  try to move in direction (di,dj); returns true if successful\nbool slide(vector<string>& g, int& ei, int& ej, int di, int dj) {\n    int ni = ei + di, nj = ej + dj;\n    if (ni < 0 || ni >= (int)g.size() || nj < 0 || nj >= (int)g[0].size())\n        return false;\n    g[ei][ej] = g[ni][nj];\n    g[ni][nj] = '0';\n    ei = ni; ej = nj;\n    return true;\n}\n\n// ------------------------------------------------------------\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, T;\n    if (!(cin >> N >> T)) return 0;\n    vector<string> initG(N);\n    int ei0 = -1, ej0 = -1;\n    for (int i = 0; i < N; ++i) {\n        cin >> initG[i];\n        for (int j = 0; j < N; ++j)\n            if (initG[i][j] == '0') { ei0 = i; ej0 = j; }\n    }\n\n    const int RUNS = 200;                 // number of random walks\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n\n    string bestSeq = \"\";\n    int bestScore = 0;\n\n    const int di[4] = {-1, 1, 0, 0};\n    const int dj[4] = {0, 0, -1, 1};\n    const char ch[4] = {'U','D','L','R'};\n\n    for (int run = 0; run < RUNS; ++run) {\n        vector<string> g = initG;\n        int ei = ei0, ej = ej0;\n        string seq = \"\";\n        int curScore = largestTree(g, ei, ej, N);\n\n        for (int step = 0; step < T; ++step) {\n            // collect legal moves\n            int legal[4], cnt = 0;\n            for (int d = 0; d < 4; ++d) {\n                int ni = ei + di[d], nj = ej + dj[d];\n                if (ni >= 0 && ni < N && nj >= 0 && nj < N)\n                    legal[cnt++] = d;\n            }\n            if (cnt == 0) break;          // no move possible, should not happen\n\n            int d = legal[rng() % cnt];\n            slide(g, ei, ej, di[d], dj[d]);\n            seq.push_back(ch[d]);\n\n            curScore = largestTree(g, ei, ej, N);\n            if (curScore == N * N - 1) break;   // full tree -> optimal\n        }\n\n        if (curScore > bestScore) {\n            bestScore = curScore;\n            bestSeq = seq;\n        }\n    }\n\n    cout << bestSeq << '\\n';\n    return 0;\n}","ahc012":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Point {\n    long long x, y;\n};\n\ninline long long cross(const Point &a, const Point &b, const Point &c) {\n    __int128 dx1 = (__int128)b.x - a.x;\n    __int128 dy1 = (__int128)b.y - a.y;\n    __int128 dx2 = (__int128)c.x - a.x;\n    __int128 dy2 = (__int128)c.y - a.y;\n    __int128 cr  = dx1 * dy2 - dy1 * dx2;\n    return (long long)cr;\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, K;\n    if (!(cin >> N >> K)) return 0;\n\n    // read a_1 \u2026 a_10 (ignored)\n    for (int i = 0; i < 10; ++i) { int t; cin >> t; }\n\n    // store strawberry coordinates\n    vector<Point> stars;\n    stars.reserve(N);\n    for (int i = 0; i < N; ++i) {\n        Point p; cin >> p.x >> p.y;\n        stars.push_back(p);\n    }\n\n    const long long LIMIT = 1000000000LL;\n    const int TARGET = 100;\n\n    mt19937_64 rng(static_cast<unsigned long long>(\n        chrono::steady_clock::now().time_since_epoch().count()));\n\n    uniform_int_distribution<long long> distPoint(-LIMIT, LIMIT);\n    uniform_int_distribution<long long> distX(-10000, 10000);\n    uniform_int_distribution<long long> distY(-10000, 10000);\n\n    auto randomInsideCircle = [&]() -> Point {\n        while (true) {\n            long long x = distX(rng);\n            long long y = distY(rng);\n            if (x*x + y*y < 100000000LL) return {x, y};\n        }\n    };\n\n    vector<pair<Point, Point>> lines;\n    lines.reserve(TARGET);\n    set<pair<long long,long long>> dirs;   // normalized direction\n\n    while ((int)lines.size() < TARGET) {\n        Point p = randomInsideCircle();\n        Point q = randomInsideCircle();\n        if (p.x == q.x && p.y == q.y) continue;\n\n        long long dx = q.x - p.x;\n        long long dy = q.y - p.y;\n        long long g  = std::gcd(std::llabs(dx), std::llabs(dy));\n        dx /= g; dy /= g;\n        if (dx < 0 || (dx == 0 && dy < 0)) { dx = -dx; dy = -dy; }\n\n        if (dirs.count({dx, dy})) continue;   // parallel line already used\n\n        bool bad = false;\n        for (auto &s : stars) {\n            if (cross(p, q, s) == 0) { bad = true; break; }\n        }\n        if (bad) continue;\n\n        dirs.insert({dx, dy});\n        lines.emplace_back(p, q);\n    }\n\n    cout << TARGET << '\\n';\n    for (auto &ln : lines) {\n        cout << ln.first.x << ' ' << ln.first.y << ' '\n             << ln.second.x << ' ' << ln.second.y << '\\n';\n    }\n\n    return 0;\n}","ahc014":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Op {\n    int x1, y1, x2, y2, x3, y3, x4, y4;\n};\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    int N, M;\n    if (!(cin >> N >> M)) return 0;\n\n    vector<vector<char>> dot(N, vector<char>(N, 0));\n    for (int i = 0; i < M; ++i) {\n        int x, y; cin >> x >> y;\n        dot[x][y] = 1;\n    }\n\n    // unit segments\n    vector<vector<char>> hor(N, vector<char>(N-1, 0));\n    vector<vector<char>> ver(N-1, vector<char>(N, 0));\n    vector<vector<char>> dpos(N-1, vector<char>(N-1, 0));   // (x,y) -> (x+1,y+1)\n    vector<vector<char>> dneg(N-1, vector<char>(N-1, 0));   // (x,y+1) -> (x+1,y)\n\n    auto weight = [&](int x, int y)->long long {\n        long long dx = x - (N-1)/2;\n        long long dy = y - (N-1)/2;\n        return dx*dx + dy*dy + 1;\n    };\n\n    vector<Op> ops;\n\n    while (true) {\n        long long bestW = -1;\n        bool bestIsSquare = false;\n        int bestSqX=0,bestSqY=0,bestSqMissing=0;\n        int bestDiaX=0,bestDiaY=0,bestDiaMissing=0;\n\n        /* ---- squares ---- */\n        for (int x=0;x+1<N;++x) for (int y=0;y+1<N;++y) {\n            // check if all four edges free\n            if (hor[y][x] || hor[y+1][x] || ver[y][x] || ver[y][x+1]) continue;\n            int empties=0, midx=-1;\n            // vertices indices: 0:(x,y),1:(x+1,y),2:(x,y+1),3:(x+1,y+1)\n            for (int k=0;k<4;++k) {\n                int cx = x + (k==1||k==3);\n                int cy = y + (k==2||k==3);\n                if (!dot[cx][cy]) { ++empties; midx=k; }\n            }\n            if (empties!=1) continue;\n            // weight of the new dot\n            int nx,ny;\n            if (midx==0) { nx=x; ny=y; }\n            else if (midx==1) { nx=x+1; ny=y; }\n            else if (midx==2) { nx=x; ny=y+1; }\n            else { nx=x+1; ny=y+1; }\n            long long w = weight(nx,ny);\n            if (w > bestW) {\n                bestW = w;\n                bestIsSquare = true;\n                bestSqX = x; bestSqY = y; bestSqMissing = midx;\n            }\n        }\n\n        /* ---- diamonds ---- */\n        for (int x=0; x+2<N; ++x)     // base lower-left corner of the tiny square\n            for (int y=1; y+1<N; ++y) {\n                // corners: 0:(x,y),1:(x+1,y+1),2:(x+2,y),3:(x+1,y-1)\n                if (dpos[x][y]||dneg[x+1][y]||dpos[x+1][y-1]||dneg[x][y-1]) continue;\n                int empties=0, midx=-1;\n                int cx[4] = {x, x+1, x+2, x+1};\n                int cy[4] = {y, y+1, y,   y-1};\n                for (int k=0;k<4;++k) {\n                    if (!dot[cx[k]][cy[k]]) { ++empties; midx=k; }\n                }\n                if (empties!=1) continue;\n                int nx=cx[midx], ny=cy[midx];\n                long long w = weight(nx,ny);\n                if (w > bestW) {\n                    bestW = w;\n                    bestIsSquare = false;\n                    bestDiaX = x; bestDiaY = y; bestDiaMissing = midx;\n                }\n            }\n\n        if (bestW < 0) break;          // no operation possible\n\n        /* --- execute the chosen operation --- */\n        if (bestIsSquare) {\n            int x = bestSqX, y = bestSqY, midx = bestSqMissing;\n            int nx,ny;\n            if (midx==0) { nx=x; ny=y; }\n            else if (midx==1) { nx=x+1; ny=y; }\n            else if (midx==2) { nx=x; ny=y+1; }\n            else { nx=x+1; ny=y+1; }\n\n            // mark edges\n            hor[y][x] = hor[y+1][x] = ver[y][x] = ver[y][x+1] = 1;\n            dot[nx][ny] = 1;\n\n            // build operation order: clockwise around the square\n            int order[4];\n            // cw cycle 0,1,3,2\n            int posInCw[4] = {0,1,3,2};\n            for (int t=0;t<4;++t) {\n                int idx = 4 == 0 ? 0 : 0; // dummy\n            }\n            vector<int> cw = {0,1,3,2};\n            int startPos = posInCw[midx];\n            vector<int> cyc(4);\n            for (int t=0;t<4;++t)\n                cyc[t] = cw[(startPos + t)%4];\n\n            Op op;\n            op.x1 = nx; op.y1 = ny;\n            op.x2 = x + (cyc[1]==1||cyc[1]==3);\n            op.y2 = y + (cyc[1]==2||cyc[1]==3);\n            op.x3 = x + (cyc[2]==1||cyc[2]==3);\n            op.y3 = y + (cyc[2]==2||cyc[2]==3);\n            op.x4 = x + (cyc[3]==1||cyc[3]==3);\n            op.y4 = y + (cyc[3]==2||cyc[3]==3);\n            ops.push_back(op);\n        } else { // diamond\n            int x = bestDiaX, y = bestDiaY, midx = bestDiaMissing;\n            int cx[4] = {x, x+1, x+2, x+1};\n            int cy[4] = {y, y+1, y,   y-1};\n            int nx = cx[midx], ny = cy[midx];\n\n            // mark diagonal segments\n            dpos[x][y] = dneg[x+1][y] = dpos[x+1][y-1] = dneg[x][y-1] = 1;\n            dot[nx][ny] = 1;\n\n            // order: 0,1,2,3 is clockwise\n            int cyc[4];\n            int startPos = midx;\n            for (int t=0;t<4;++t) cyc[t] = (startPos + t)%4;\n\n            Op op;\n            op.x1 = nx; op.y1 = ny;\n            op.x2 = cx[cyc[1]];\n            op.y2 = cy[cyc[1]];\n            op.x3 = cx[cyc[2]];\n            op.y3 = cy[cyc[2]];\n            op.x4 = cx[cyc[3]];\n            op.y4 = cy[cyc[3]];\n            ops.push_back(op);\n        }\n    }\n\n    /* output */\n    cout << ops.size() << '\\n';\n    for (auto &op: ops)\n        cout << op.x1 << ' ' << op.y1 << ' '\n             << op.x2 << ' ' << op.y2 << ' '\n             << op.x3 << ' ' << op.y3 << ' '\n             << op.x4 << ' ' << op.y4 << '\\n';\n    return 0;\n}","ahc015":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Pos { int r, c; };\n\nstatic inline void tilt(vector<vector<int>>& g, char dir) {\n    if (dir == 'F') {                 // up\n        for (int c = 0; c < 10; ++c) {\n            vector<int> v;\n            for (int r = 0; r < 10; ++r)\n                if (g[r][c]) v.push_back(g[r][c]);\n            for (int r = 0; r < 10; ++r)\n                g[r][c] = (r < (int)v.size() ? v[r] : 0);\n        }\n    } else if (dir == 'B') {          // down\n        for (int c = 0; c < 10; ++c) {\n            vector<int> v;\n            for (int r = 9; r >= 0; --r)\n                if (g[r][c]) v.push_back(g[r][c]);\n            for (int r = 9, idx = 0; r >= 0; --r, ++idx)\n                g[r][c] = (idx < (int)v.size() ? v[idx] : 0);\n        }\n    } else if (dir == 'L') {          // left\n        for (int r = 0; r < 10; ++r) {\n            vector<int> v;\n            for (int c = 0; c < 10; ++c)\n                if (g[r][c]) v.push_back(g[r][c]);\n            for (int c = 0; c < 10; ++c)\n                g[r][c] = (c < (int)v.size() ? v[c] : 0);\n        }\n    } else {                          // 'R'\n        for (int r = 0; r < 10; ++r) {\n            vector<int> v;\n            for (int c = 9; c >= 0; --c)\n                if (g[r][c]) v.push_back(g[r][c]);\n            for (int c = 9, idx = 0; c >= 0; --c, ++idx)\n                g[r][c] = (idx < (int)v.size() ? v[idx] : 0);\n        }\n    }\n}\n\nstatic inline long long connectivityScore(const vector<vector<int>>& g) {\n    bool vis[10][10] = {};\n    long long score = 0;\n    for (int r = 0; r < 10; ++r) for (int c = 0; c < 10; ++c) {\n        if (g[r][c] && !vis[r][c]) {\n            int flavour = g[r][c];\n            int cnt = 0;\n            queue<pair<int,int>> q;\n            q.push({r,c});\n            vis[r][c] = true;\n            while (!q.empty()) {\n                auto [x,y] = q.front(); q.pop();\n                ++cnt;\n                const int dr[4]={-1,1,0,0}, dc[4]={0,0,-1,1};\n                for (int k=0;k<4;++k){\n                    int nx=x+dr[k], ny=y+dc[k];\n                    if(nx>=0 && nx<10 && ny>=0 && ny<10 &&\n                       !vis[nx][ny] && g[nx][ny]==flavour){\n                        vis[nx][ny]=true;\n                        q.push({nx,ny});\n                    }\n                }\n            }\n            score += 1LL*cnt*cnt;\n        }\n    }\n    return score;\n}\n\nint main(){\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    vector<int> flavour(100);\n    for(int i=0;i<100;i++) cin>>flavour[i];\n\n    vector<vector<int>> board(10, vector<int>(10,0));\n    vector<Pos> empty;\n    empty.reserve(100);\n    for (int r=0;r<10;++r)\n        for (int c=0;c<10;++c)\n            empty.push_back({r,c});\n\n    const char dirs[4] = {'F','B','L','R'};\n    for(int t=0;t<100;++t){\n        int p; cin>>p;                     // 1\u2011based\n        Pos pos = empty[p-1];\n        board[pos.r][pos.c] = flavour[t];\n        empty.erase(empty.begin()+p-1);    // O(100) but 100*100 is negligible\n\n        // decide tilt\n        long long bestScore = -1;\n        char bestDir = 'F';\n        for(char d: dirs){\n            auto tmp = board;\n            tilt(tmp,d);\n            long long sc = connectivityScore(tmp);\n            if(sc > bestScore){\n                bestScore = sc;\n                bestDir   = d;\n            }\n        }\n\n        cout << bestDir << '\\n' << flush; // flush after each output\n    }\n    return 0;\n}","ahc016":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int M; double eps;\n    if (!(cin >> M >> eps)) return 0;\n\n    const int N = 20;\n    const int L = N * (N - 1) / 2;          // 190\n\n    cout << N << '\\n';\n    for (int k = 0; k < M; ++k) {\n        string s;\n        s.append(k, '1');\n        s.append(L - k, '0');\n        cout << s << '\\n';\n    }\n    cout.flush();\n\n    for (int q = 0; q < 100; ++q) {\n        string H;\n        cin >> H;\n        int ones = 0;\n        for (char c : H) if (c == '1') ++ones;\n        int ans = min(ones, M - 1);\n        cout << ans << '\\n';\n        cout.flush();\n    }\n    return 0;\n}","ahc017":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Edge {\n    int u, v, w;\n};\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, D, K;\n    if (!(cin >> N >> M >> D >> K)) return 0;\n\n    vector<Edge> edges(M);\n    vector<vector<pair<int,int>>> adj(N);            // (to, edge_id)\n\n    for (int i = 0; i < M; ++i) {\n        int u, v, w;\n        cin >> u >> v >> w;\n        --u; --v;\n        edges[i] = {u, v, w};\n        adj[u].push_back({v, i});\n        adj[v].push_back({u, i});\n    }\n\n    /*  The 2\u2011D coordinates are irrelevant for the schedule \u2013 skip them. */\n    for (int i = 0; i < N; ++i) {\n        int x, y; cin >> x >> y;\n    }\n\n    /* ---------- approximate betweenness ----------\n       Take a modest number of random sources (\u2264200).  */\n    const int SRC_COUNT = min(200, N);\n    vector<int> cnt(M, 0);                       // how often an edge appears\n    vector<long long> dist(N);\n    vector<int> parent(N), parentEdge(N);\n    const long long INF = (1LL << 60);\n\n    vector<int> pool(N);\n    iota(pool.begin(), pool.end(), 0);\n    mt19937 rng((unsigned)chrono::steady_clock::now().time_since_epoch().count());\n    shuffle(pool.begin(), pool.end(), rng);\n\n    for (int sIdx = 0; sIdx < SRC_COUNT; ++sIdx) {\n        int s = pool[sIdx];\n        fill(dist.begin(), dist.end(), INF);\n        fill(parent.begin(), parent.end(), -1);\n        fill(parentEdge.begin(), parentEdge.end(), -1);\n\n        priority_queue<pair<long long,int>, vector<pair<long long,int>>,\n                       greater<pair<long long,int>>> pq;\n        dist[s] = 0;\n        pq.push({0, s});\n\n        while (!pq.empty()) {\n            auto [d, u] = pq.top(); pq.pop();\n            if (d != dist[u]) continue;\n            for (auto [v, ei] : adj[u]) {\n                long long nd = d + edges[ei].w;\n                if (nd < dist[v]) {\n                    dist[v] = nd;\n                    parent[v] = u;\n                    parentEdge[v] = ei;\n                    pq.push({nd, v});\n                }\n            }\n        }\n\n        /* count the edges that appear in the shortest\u2011path tree from s */\n        for (int v = 0; v < N; ++v) {\n            if (v == s || parent[v] == -1) continue;\n            int cur = v;\n            while (cur != s) {\n                int ei = parentEdge[cur];\n                ++cnt[ei];\n                cur = parent[cur];\n            }\n        }\n    }\n\n    /* ---------- order edges by importance ----------\n       use cnt * weight still fits in 64\u2011bit.        */\n    vector<int> order(M);\n    iota(order.begin(), order.end(), 0);\n    sort(order.begin(), order.end(),\n         [&](int a, int b) {\n             long long keyA = 1LL * cnt[a] * edges[a].w;\n             long long keyB = 1LL * cnt[b] * edges[b].w;\n             return keyA > keyB;               // larger first\n         });\n\n    /* ---------- round\u2011robin assignment ----------\n       Each day gets at most ceil(M/D) edges \u2013 guaranteed < K. */\n    vector<int> day_of_edge(M);\n    for (int i = 0; i < M; ++i) {\n        day_of_edge[order[i]] = (i % D) + 1;    // 1\u2011based\n    }\n\n    /* ---------- output ---------- */\n    for (int i = 0; i < M; ++i) {\n        if (i) cout << ' ';\n        cout << day_of_edge[i];\n    }\n    cout << '\\n';\n    return 0;\n}","ahc019":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Vec{int x,y,z;};\n\ninline int idx(int x,int y,int z,int D){ return x*D*D + y*D + z; }\n\n/* flood\u2011fill in 3\u2011D */\nvector<vector<Vec>> flood(const vector<vector<vector<char>>>& g,int D){\n    vector<vector<Vec>> res;\n    vector<vector<vector<char>>> vis(D, vector<vector<char>>(D, vector<char>(D,0)));\n    const int dir[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};\n    for(int x=0;x<D;++x)\n        for(int y=0;y<D;++y)\n            for(int z=0;z<D;++z)\n                if(g[x][y][z] && !vis[x][y][z]){\n                    vector<Vec> cur;\n                    queue<Vec> q;\n                    q.push({x,y,z});\n                    vis[x][y][z]=1;\n                    while(!q.empty()){\n                        Vec v=q.front();q.pop();\n                        cur.push_back(v);\n                        for(auto &d:dir){\n                            int nx=v.x+d[0],ny=v.y+d[1],nz=v.z+d[2];\n                            if(nx>=0&&nx<D&&ny>=0&&ny<D&&nz>=0&&nz<D &&\n                               g[nx][ny][nz] && !vis[nx][ny][nz]){\n                                vis[nx][ny][nz]=1;\n                                q.push({nx,ny,nz});\n                            }\n                        }\n                    }\n                    res.push_back(move(cur));\n                }\n    return res;\n}\n\n/* ---------- rotation utilities ----------------------------------- */\nint parity_of_perm(const array<int,3>& p){\n    int inv=0;\n    for(int i=0;i<3;++i) for(int j=i+1;j<3;++j) if(p[i]>p[j]) inv^=1;\n    return inv? -1:1;    // 1 : even, -1 : odd\n}\nstring canonical(const vector<Vec>& cells){\n    array<int,3> perms[6]={{0,1,2},{0,2,1},{1,0,2},{1,2,0},{2,0,1},{2,1,0}};\n    array<int,3> signs[4]={{1,1,1},{1,-1,-1},{-1,1,-1},{-1,-1,1}};\n    string best;\n    bool first=true;\n    for(auto &p:perms){\n        int perm_parity=parity_of_perm(p);\n        for(auto &sg:signs){\n            int det=perm_parity*sg[0]*sg[1]*sg[2];\n            if(det!=1) continue;  // only proper rotations\n            vector<Vec> tmp;\n            tmp.reserve(cells.size());\n            int minx=INT_MAX,miny=INT_MAX,minz=INT_MAX;\n            for(auto &c:cells){\n                int v[3]={c.x,c.y,c.z};\n                int rx=sg[0]*v[p[0]];\n                int ry=sg[1]*v[p[1]];\n                int rz=sg[2]*v[p[2]];\n                tmp.push_back({rx,ry,rz});\n                minx=min(minx,rx);\n                miny=min(miny,ry);\n                minz=min(minz,rz);\n            }\n            for(auto &c:tmp){\n                c.x-=minx;\n                c.y-=miny;\n                c.z-=minz;\n            }\n            sort(tmp.begin(),tmp.end(),[](const Vec&a,const Vec&b){\n                if(a.x!=b.x)return a.x<b.x;\n                if(a.y!=b.y)return a.y<b.y;\n                return a.z<b.z;\n            });\n            string s;\n            s.reserve(tmp.size()*10);\n            for(auto &c:tmp){\n                s+=to_string(c.x);s+=',';\n                s+=to_string(c.y);s+=',';\n                s+=to_string(c.z);s+=';';\n            }\n            if(first||s<best){best=s;first=false;}\n        }\n    }\n    return best;\n}\n\nint main(){\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    int D;\n    if(!(cin>>D)) return 0;\n    vector<string> f[2], r[2];\n    for(int k=0;k<2;++k){\n        f[k].resize(D);r[k].resize(D);\n        for(int i=0;i<D;++i)cin>>f[k][i];\n        for(int i=0;i<D;++i)cin>>r[k][i];\n    }\n\n    /* Boolean arrays of allowed cubes */\n    vector<vector<vector<char>>> C1(D, vector<vector<char>>(D, vector<char>(D)));\n    vector<vector<vector<char>>> C2(D, vector<vector<char>>(D, vector<char>(D)));\n    for(int z=0;z<D;++z)\n        for(int x=0;x<D;++x)\n            for(int y=0;y<D;++y){\n                bool a=(f[0][z][x]=='1' && r[0][z][y]=='1');\n                bool b=(f[1][z][x]=='1' && r[1][z][y]=='1');\n                C1[x][y][z]=a;C2[x][y][z]=b;\n            }\n\n    /* split into COMMON, ONLY1, ONLY2 */\n    vector<vector<vector<char>>> COMMON(D, vector<vector<char>>(D, vector<char>(D)));\n    vector<vector<vector<char>>> ONLY1(D, vector<vector<char>>(D, vector<char>(D)));\n    vector<vector<vector<char>>> ONLY2(D, vector<vector<char>>(D, vector<char>(D)));\n    for(int x=0;x<D;++x)\n        for(int y=0;y<D;++y)\n            for(int z=0;z<D;++z){\n                COMMON[x][y][z] = C1[x][y][z] & C2[x][y][z];\n                ONLY1[x][y][z]  = C1[x][y][z] & !COMMON[x][y][z];\n                ONLY2[x][y][z]  = C2[x][y][z] & !COMMON[x][y][z];\n            }\n\n    auto compCommon = flood(COMMON,D);\n    auto compOnly1  = flood(ONLY1,D);\n    auto compOnly2  = flood(ONLY2,D);\n\n    vector<int> b1(D*D*D,0), b2(D*D*D,0);\n    int nextId=1;\n    /* common components \u2013 shared in both objects */\n    for(auto &c:compCommon){\n        for(auto &v:c){\n            int id=idx(v.x,v.y,v.z,D);\n            b1[id]=b2[id]=nextId;\n        }\n        ++nextId;\n    }\n\n    /* compute canonical signatures for ONLY1 & ONLY2 components */\n    unordered_map<string,vector<int>> map1, map2;\n    for(int i=0;i<(int)compOnly1.size();++i){\n        map1[canonical(compOnly1[i])].push_back(i);\n    }\n    for(int i=0;i<(int)compOnly2.size();++i){\n        map2[canonical(compOnly2[i])].push_back(i);\n    }\n\n    vector<bool> paired1(compOnly1.size(),false);\n    vector<bool> paired2(compOnly2.size(),false);\n\n    /* pair identical shapes and write with same id into both objects */\n    for(auto &kv:map1){\n        const string &key = kv.first;\n        if(map2.find(key)==map2.end()) continue;\n        auto &v1 = kv.second;\n        auto &v2 = map2[key];\n        int m = min((int)v1.size(), (int)v2.size());\n        for(int i=0;i<m;++i){\n            int id=nextId++;\n            int i1=v1[i], i2=v2[i];\n            paired1[i1]=paired2[i2]=true;\n            /* write into both objects */\n            for(auto &c:compOnly1[i1]){\n                int idc=idx(c.x,c.y,c.z,D);\n                b1[idc]=id;\n            }\n            for(auto &c:compOnly2[i2]){\n                int idc=idx(c.x,c.y,c.z,D);\n                b2[idc]=id;\n            }\n        }\n    }\n\n    /* remaining unmatched ONLY1 components */\n    for(int i=0;i<(int)compOnly1.size();++i){\n        if(paired1[i]) continue;\n        int id=nextId++;\n        for(auto &c:compOnly1[i]){\n            int idc=idx(c.x,c.y,c.z,D);\n            b1[idc]=id;\n        }\n    }\n\n    /* remaining unmatched ONLY2 components */\n    for(int i=0;i<(int)compOnly2.size();++i){\n        if(paired2[i]) continue;\n        int id=nextId++;\n        for(auto &c:compOnly2[i]){\n            int idc=idx(c.x,c.y,c.z,D);\n            b2[idc]=id;\n        }\n    }\n\n    cout<<nextId-1<<\"\\n\";\n    for(int i=0;i<(int)b1.size();++i){\n        if(i) cout<<' ';\n        cout<<b1[i];\n    }\n    cout<<\"\\n\";\n    for(int i=0;i<(int)b2.size();++i){\n        if(i) cout<<' ';\n        cout<<b2[i];\n    }\n    cout<<\"\\n\";\n    return 0;\n}","ahc020":"#include <bits/stdc++.h>\nusing namespace std;\n\n/*--- utility: ceil(sqrt(x)) ----------------------------------*/\nstatic inline int ceil_sqrt(long long x) {\n    long long r = sqrt((long double)x);\n    while (r * r < x) ++r;\n    while (r > 0 && (r - 1) * (r - 1) >= x) --r;\n    return (int)r;\n}\n\n/*--- DSU -----------------------------------------------------*/\nstruct DSU {\n    vector<int> p, sz;\n    DSU(int n = 0) { init(n); }\n    void init(int n) { p.resize(n); sz.assign(n, 1); iota(p.begin(), p.end(), 0); }\n    int find(int x) { return p[x] == x ? x : p[x] = find(p[x]); }\n    bool unite(int a, int b) {\n        a = find(a); b = find(b);\n        if (a == b) return false;\n        if (sz[a] < sz[b]) swap(a, b);\n        p[b] = a; sz[a] += sz[b];\n        return true;\n    }\n};\n\n/*--- Edge info ----------------------------------------------*/\nstruct EdgeInfo {int to,w,idx;};\n\nint main(){\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    int N,M,K;\n    if(!(cin>>N>>M>>K)) return 0;\n\n    /*--- vertices --------------------------------------------*/\n    vector<long long> xs(N), ys(N);\n    for(int i=0;i<N;i++) cin>>xs[i]>>ys[i];\n\n    /*--- edges ----------------------------------------------*/\n    vector<vector<EdgeInfo>> adj(N);\n    struct Edge{int u,v,w,idx;};\n    vector<Edge> edges(M);\n    for(int j=0;j<M;j++){\n        int u,v,w; cin>>u>>v>>w; --u;--v;\n        edges[j]={u,v,w,j};\n        adj[u].push_back({v,w,j});\n        adj[v].push_back({u,w,j});\n    }\n\n    /*--- residents ------------------------------------------*/\n    // distR2[k][i] : squared distance from resident k to vertex i\n    vector<vector<long long>> distR2(K, vector<long long>(N));\n    for(int k=0;k<K;k++){\n        long long a,b; cin>>a>>b;\n        for(int i=0;i<N;i++){\n            long long dx=a-xs[i], dy=b-ys[i];\n            distR2[k][i]=dx*dx+dy*dy;\n        }\n    }\n\n    /*--- assignment of residents to nearest station --------*/\n    vector<vector<int>> stationRes(N);\n    vector<int> residentAssign(K,-1);\n    for(int k=0;k<K;k++){\n        int best=0; long long bestd=distR2[k][0];\n        for(int i=1;i<N;i++){\n            if(distR2[k][i]<bestd){ bestd=distR2[k][i]; best=i;}\n        }\n        residentAssign[k]=best;\n        stationRes[best].push_back(k);\n    }\n\n    /*--- compute radii ---------------------------------------*/\n    vector<long long> maxDist2Station(N,0);\n    vector<int> P(N,0);\n    vector<char> active(N,0);\n    for(int i=0;i<N;i++){\n        if(!stationRes[i].empty()){\n            long long mx=0;\n            for(int r:stationRes[i]) mx=max(mx,distR2[r][i]);\n            maxDist2Station[i]=mx;\n            P[i]=ceil_sqrt(mx);\n            active[i]=1;\n        }\n    }\n\n    /*--- greedy merge of stations to reduce P cost -----------*/\n    bool improved=true;\n    while(improved){\n        improved=false;\n        long long bestDiff=-1;\n        int best_i=-1,best_j=-1;\n        long long bestNewMax=0;\n        int bestNewP=0;\n        vector<int> activeIdx;\n        activeIdx.reserve(N);\n        for(int i=0;i<N;i++) if(active[i]) activeIdx.push_back(i);\n        int Tcnt=activeIdx.size();\n        for(int ii=0;ii<Tcnt;ii++){\n            int i=activeIdx[ii];\n            if(stationRes[i].empty()) continue;\n            long long Pi2=1LL*P[i]*P[i];\n            for(int jj=0;jj<Tcnt;jj++){\n                int j=activeIdx[jj]; if(i==j) continue;\n                long long maxDist2_j = maxDist2Station[j];\n                long long newMax2 = maxDist2_j;\n                for(int r:stationRes[i]){\n                    if(distR2[r][j]>newMax2) newMax2=distR2[r][j];\n                }\n                int newP = ceil_sqrt(newMax2);\n                long long delta = 1LL*newP*newP - 1LL*P[j]*P[j];\n                if(delta < Pi2){\n                    long long diff = Pi2 - delta;\n                    if(diff>bestDiff){\n                        bestDiff=diff;\n                        best_i=i; best_j=j;\n                        bestNewMax=newMax2;\n                        bestNewP=newP;\n                    }\n                }\n            }\n        }\n        if(bestDiff!=-1){\n            improved=true;\n            // merge best_i into best_j\n            for(int r:stationRes[best_i]){\n                residentAssign[r]=best_j;\n                stationRes[best_j].push_back(r);\n            }\n            stationRes[best_i].clear();\n            maxDist2Station[best_j]=bestNewMax;\n            P[best_j]=bestNewP;\n            P[best_i]=0;\n            active[best_i]=0;\n        }\n    }\n\n    /*--- terminal list (include node 0 to guarantee connectivity) */\n    vector<int> terminals;\n    for(int i=0;i<N;i++) if(P[i]>0) terminals.push_back(i);\n    bool has0=false;\n    for(int v:terminals) if(v==0){ has0=true; break;}\n    if(!has0) terminals.push_back(0);          // node 0 is always terminal\n\n    int T=terminals.size();\n\n    /*--- compute all\u2011pairs shortest paths from terminals --------*/\n    const long long INFLL = (1LL<<60);\n    vector<vector<long long>> dist(T, vector<long long>(N, INFLL));\n    vector<vector<int>> preNode(T, vector<int>(N,-1));\n    vector<vector<int>> preEdge(T, vector<int>(N,-1));\n    for(int t=0;t<T;t++){\n        int src=terminals[t];\n        priority_queue<pair<long long,int>, vector<pair<long long,int>>, greater<pair<long long,int>>> pq;\n        dist[t][src]=0;\n        pq.push({0,src});\n        while(!pq.empty()){\n            auto [d,v]=pq.top(); pq.pop();\n            if(d!=dist[t][v]) continue;\n            for(const auto &e:adj[v]){\n                long long nd=d+e.w;\n                if(nd<dist[t][e.to]){\n                    dist[t][e.to]=nd;\n                    preNode[t][e.to]=v;\n                    preEdge[t][e.to]=e.idx;\n                    pq.push({nd,e.to});\n                }\n            }\n        }\n    }\n\n    /*--- complete graph of terminals and MST (Kruskal) --------*/\n    struct CEdge{long long w; int a,b;};\n    vector<CEdge> cEdges;\n    cEdges.reserve(T*(T-1)/2);\n    for(int i=0;i<T;i++){\n        for(int j=i+1;j<T;j++){\n            long long w=dist[i][terminals[j]];\n            cEdges.push_back({w,i,j});\n        }\n    }\n    sort(cEdges.begin(),cEdges.end(),[](const CEdge&x,const CEdge&y){return x.w<y.w;});\n    DSU dsu(T);\n    vector<char> used(M,0);\n    for(const auto &ce:cEdges){\n        if(dsu.unite(ce.a,ce.b)){\n            int s=terminals[ce.a], t=terminals[ce.b];\n            int cur=t;\n            while(cur!=s){\n                int eidx=preEdge[ce.a][cur];\n                used[eidx]=1;\n                cur=preNode[ce.a][cur];\n            }\n        }\n    }\n\n    /*--- output ----------------------------------------------*/\n    for(int i=0;i<N;i++){\n        if(i) cout<<' ';\n        cout<<P[i];\n    }\n    cout<<'\\n';\n    for(int j=0;j<M;j++){\n        if(j) cout<<' ';\n        cout<<(used[j]?1:0);\n    }\n    cout<<'\\n';\n    return 0;\n}","ahc021":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    const int N = 30;\n    const int S = N * (N + 1) / 2;          // 465\n    vector<int> a(S);\n    for (int i = 0; i < S; ++i) cin >> a[i];\n\n    // ---------- pre\u2011compute coordinates and row number ----------\n    vector<pair<int,int>> coord(S);\n    vector<int> row_of_idx(S);\n    for (int r = 0; r < N; ++r) {\n        int start = r * (r + 1) / 2;\n        for (int c = 0; c <= r; ++c) {\n            int idx = start + c;\n            coord[idx] = {r, c};\n            row_of_idx[idx] = r;\n        }\n    }\n\n    // ---------- record of swaps ----------\n    struct Op {int x1, y1, x2, y2;};\n    vector<Op> ops;\n\n    // ---------- heapify bottom\u2011up ----------\n    const int INTERNAL = S - N;          // indices 0 \u2026 434 are internal\n    for (int start = INTERNAL - 1; start >= 0; --start) {\n        int cur = start;\n        while (true) {\n            int r = row_of_idx[cur];\n            int left = cur + r + 1;\n            int right = cur + r + 2;\n            int smallest = cur;\n            if (left < S && a[left] < a[smallest]) smallest = left;\n            if (right < S && a[right] < a[smallest]) smallest = right;\n            if (smallest == cur) break;\n            swap(a[cur], a[smallest]);\n            ops.push_back({coord[cur].first, coord[cur].second,\n                           coord[smallest].first, coord[smallest].second});\n            cur = smallest;\n        }\n    }\n\n    // ---------- output ----------\n    cout << ops.size() << '\\n';\n    for (const auto &p : ops)\n        cout << p.x1 << ' ' << p.y1 << ' ' << p.x2 << ' ' << p.y2 << '\\n';\n    return 0;\n}","toyota2023summer-final":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Cell { int r, c; };\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int D, N;\n    if (!(cin >> D >> N)) return 0;               // D is always 9\n\n    vector<vector<int>> obst(D, vector<int>(D, 0));\n    for (int i = 0; i < N; ++i) {\n        int r, c; cin >> r >> c;\n        obst[r][c] = 1;\n    }\n\n    const int ex = 0, ey = (D - 1) / 2;            // entrance\n    obst[ex][ey] = 0;                              // it is free\n\n    /* ---- BFS : distances from the entrance ---- */\n    const int INF = 1e9;\n    vector<vector<int>> dist(D, vector<int>(D, INF));\n    queue<Cell> q;\n    dist[ex][ey] = 0;\n    q.push({ex, ey});\n    const int dr[4] = {-1, 1, 0, 0};\n    const int dc[4] = {0, 0, -1, 1};\n\n    while (!q.empty()) {\n        auto cur = q.front(); q.pop();\n        for (int k = 0; k < 4; ++k) {\n            int nr = cur.r + dr[k], nc = cur.c + dc[k];\n            if (nr < 0 || nr >= D || nc < 0 || nc >= D) continue;\n            if (obst[nr][nc]) continue;\n            if (dist[nr][nc] != INF) continue;\n            dist[nr][nc] = dist[cur.r][cur.c] + 1;\n            q.push({nr, nc});\n        }\n    }\n\n    /* ---- collect all free cells except entrance ---- */\n    vector<Cell> cells;\n    for (int r = 0; r < D; ++r)\n        for (int c = 0; c < D; ++c) {\n            if (obst[r][c]) continue;\n            if (r == ex && c == ey) continue;\n            cells.push_back({r, c});\n        }\n\n    /* ---- placement order: far first (decreasing dist) ---- */\n    sort(cells.begin(), cells.end(),\n         [&](const Cell &a, const Cell &b) {\n             return dist[a.r][a.c] > dist[b.r][b.c];\n         });\n\n    const int M = cells.size();                  // number of containers\n    vector<vector<int>> cellNum(D, vector<int>(D, -1));   // container number per cell\n\n    /* ----- placement phase ----- */\n    for (int i = 0; i < M; ++i) {\n        int t;                     // container number (random)\n        cin >> t;\n        Cell c = cells[i];\n        cellNum[c.r][c.c] = t;\n        cout << c.r << ' ' << c.c << '\\n' << flush;\n    }\n\n    /* ----- removal phase ----- */\n    vector<vector<bool>> visited(D, vector<bool>(D, false));\n    vector<vector<bool>> queued(D, vector<bool>(D, false));\n\n    /* mark entrance so it will never be queued */\n    queued[ex][ey] = true;\n\n    using PQItem = pair<int, Cell>;          // (container number, cell)\n    auto cmp = [](const PQItem &a, const PQItem &b) { return a.first > b.first; };\n    priority_queue<PQItem, vector<PQItem>, decltype(cmp)> pq(cmp);\n\n    /* initially, enqueue neighbours of the entrance */\n    for (int k = 0; k < 4; ++k) {\n        int nr = ex + dr[k], nc = ey + dc[k];\n        if (nr < 0 || nr >= D || nc < 0 || nc >= D) continue;\n        if (obst[nr][nc]) continue;\n        if (!queued[nr][nc]) {\n            pq.push({cellNum[nr][nc], {nr, nc}});\n            queued[nr][nc] = true;\n        }\n    }\n\n    int removed = 0;\n    while (!pq.empty()) {\n        auto [num, cur] = pq.top(); pq.pop();\n        int r = cur.r, c = cur.c;\n        if (visited[r][c]) continue;     // duplicate entry\n        visited[r][c] = true;\n        cout << r << ' ' << c << '\\n';\n        ++removed;\n        if (removed == M) break;          // all containers removed\n\n        /* enqueue newly reachable neighbours */\n        for (int k = 0; k < 4; ++k) {\n            int nr = r + dr[k], nc = c + dc[k];\n            if (nr < 0 || nr >= D || nc < 0 || nc >= D) continue;\n            if (obst[nr][nc]) continue;\n            if (visited[nr][nc] || queued[nr][nc]) continue;\n            pq.push({cellNum[nr][nc], {nr, nc}});\n            queued[nr][nc] = true;\n        }\n    }\n\n    return 0;\n}","ahc024":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int n, m;\n    if (!(cin >> n >> m)) return 0;  // just in case\n    \n    for (int i = 0; i < n; ++i) {\n        for (int j = 0; j < n; ++j) {\n            int v;  cin >> v;\n            cout << v;\n            if (j + 1 < n) cout << ' ';\n        }\n        cout << '\\n';\n    }\n    return 0;\n}","ahc025":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, D, Q;\n    if (!(cin >> N >> D >> Q)) return 0;\n\n    // Perform Q trivial queries\n    for (int q = 0; q < Q; ++q) {\n        int l = q % N;\n        int r = (q + 1) % N;          // ensures r != l\n        cout << 1 << ' ' << 1 << ' ' << l << ' ' << r << '\\n';\n        cout.flush();\n\n        // read judge answer and ignore\n        char ans;\n        if (!(cin >> ans)) return 0;   // safety: if input ends unexpectedly\n    }\n\n    // Output a simple final division\n    for (int i = 0; i < N; ++i) {\n        if (i) cout << ' ';\n        cout << (i % D);\n    }\n    cout << '\\n';\n    return 0;\n}","ahc026":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int n, m;\n    if (!(cin >> n >> m)) return 0;          // n = 200, m = 10\n    vector<vector<int>> stacks(m);\n    for (int i = 0; i < m; ++i) {\n        stacks[i].reserve(n / m);\n        for (int j = 0; j < n / m; ++j) {\n            int x; cin >> x;                  // bottom \u2192 top\n            stacks[i].push_back(x);\n        }\n    }\n\n    /* location of each box */\n    vector<int> stack_of(n + 1), pos_of(n + 1);   // 1\u2011based box number\n    for (int i = 0; i < m; ++i) {\n        for (int j = 0; j < (int)stacks[i].size(); ++j) {\n            int v = stacks[i][j];\n            stack_of[v] = i;\n            pos_of[v]   = j;\n        }\n    }\n\n    string out;\n    out.reserve(6000 * 12);\n\n    for (int v = 1; v <= n; ++v) {\n        int src = stack_of[v];\n        // move top boxes until v becomes top of its stack\n        while (pos_of[v] != (int)stacks[src].size() - 1) {\n            int t = stacks[src].back();       // top of src\n            // choose destination stack with minimal height (not src)\n            int dst = -1, best = INT_MAX;\n            for (int i = 0; i < m; ++i) {\n                if (i == src) continue;\n                int sz = (int)stacks[i].size();\n                if (sz < best) { best = sz; dst = i; }\n            }\n            // move t from src to dst\n            out.append(to_string(t));\n            out.push_back(' ');\n            out.append(to_string(dst + 1));\n            out.push_back('\\n');\n\n            stacks[src].pop_back();\n            stacks[dst].push_back(t);\n            stack_of[t] = dst;\n            pos_of[t]   = (int)stacks[dst].size() - 1;\n            // src and dst indices updated; v remains unchanged\n            src = stack_of[v];\n        }\n\n        // now v is top of its stack\n        out.append(to_string(v));\n        out.push_back(' ');\n        out.append(\"0\\n\");\n        stacks[src].pop_back();                // remove v\n        // no need to update stack_of/pos_of for v\n    }\n\n    cout << out;\n    return 0;\n}","ahc027":"#include <bits/stdc++.h>\nusing namespace std;\n\nint N;\nvector<string> h, v;                     // horizontal, vertical walls\nvector<vector<int>> d;                   // dirtiness, unused by the algorithm\nvector<vector<bool>> vis;\n\nstring ans;\nconst char dirChar[4] = {'R', 'D', 'L', 'U'};\nint dx[4] = {0, 1, 0, -1};\nint dy[4] = {1, 0, -1, 0};\n\nbool canMove(int x, int y, int dir) {\n    int nx = x + dx[dir];\n    int ny = y + dy[dir];\n    if (nx < 0 || nx >= N || ny < 0 || ny >= N) return false;\n    if (dir == 0) {            // Right\n        return v[x][y] == '0';\n    } else if (dir == 1) {     // Down\n        return h[x][y] == '0';\n    } else if (dir == 2) {     // Left\n        return v[x][y-1] == '0';\n    } else {                   // Up\n        return h[x-1][y] == '0';\n    }\n}\n\nvoid dfs(int x, int y) {\n    vis[x][y] = true;\n    for (int dir = 0; dir < 4; ++dir) {\n        if (!canMove(x, y, dir)) continue;\n        int nx = x + dx[dir];\n        int ny = y + dy[dir];\n        if (vis[nx][ny]) continue;\n        ans.push_back(dirChar[dir]);          // move to child\n        dfs(nx, ny);\n        ans.push_back(dirChar[(dir + 2) % 4]); // backtrack\n    }\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    if (!(cin >> N)) return 0;\n    h.resize(N-1);\n    for (int i = 0; i < N-1; ++i) cin >> h[i];\n    v.resize(N);\n    for (int i = 0; i < N; ++i) cin >> v[i];\n    d.assign(N, vector<int>(N));\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < N; ++j)\n            cin >> d[i][j];           // not used\n\n    vis.assign(N, vector<bool>(N, false));\n    dfs(0, 0);\n\n    cout << ans << '\\n';\n    return 0;\n}","ahc028":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Entry {\n    int start;            // id of first cell\n    int end;              // id of last cell\n    int cost;             // cost of the internal 5 steps (dist+1 each)\n    vector<int> path;     // 5 cell ids in order\n};\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M;\n    if (!(cin >> N >> M)) return 0;\n    int si, sj;\n    cin >> si >> sj;\n    vector<string> board(N);\n    for (int i = 0; i < N; ++i) cin >> board[i];\n\n    const int SZ = N * N;\n    vector<int> row(SZ), col(SZ);\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < N; ++j) {\n            int id = i * N + j;\n            row[id] = i;\n            col[id] = j;\n        }\n\n    /* positions of each letter */\n    array<vector<int>, 26> pos;\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < N; ++j)\n            pos[board[i][j] - 'A'].push_back(i * N + j);\n\n    vector<string> t(M);\n    for (int i = 0; i < M; ++i) cin >> t[i];\n\n    /* all pair distances */\n    vector<vector<int>> dist(SZ, vector<int>(SZ));\n    for (int a = 0; a < SZ; ++a)\n        for (int b = 0; b < SZ; ++b)\n            dist[a][b] = abs(row[a] - row[b]) + abs(col[a] - col[b]);\n\n    /* ---------- precompute entries for each string ---------- */\n    vector<vector<Entry>> entries(M);\n    for (int idx = 0; idx < M; ++idx) {\n        const string &s = t[idx];\n        int first = s[0] - 'A';\n        for (int startCell : pos[first]) {\n            vector<int> dpPrev(SZ, INT_MAX);\n            dpPrev[startCell] = 0;\n            vector<vector<int>> outPrev(5, vector<int>(SZ, -1));\n\n            for (int step = 0; step < 5; ++step) {\n                int need = s[step] - 'A';\n                vector<int> dpCur(SZ, INT_MAX);\n                for (int cur = 0; cur < SZ; ++cur) {\n                    if (dpPrev[cur] == INT_MAX) continue;\n                    for (int nxt : pos[need]) {\n                        int c = dpPrev[cur] + dist[cur][nxt] + 1;\n                        if (c < dpCur[nxt]) {\n                            dpCur[nxt] = c;\n                            outPrev[step][nxt] = cur;\n                        }\n                    }\n                }\n                dpPrev.swap(dpCur);\n            }\n\n            int best = INT_MAX, bestEnd = -1;\n            for (int e = 0; e < SZ; ++e)\n                if (dpPrev[e] < best) {\n                    best = dpPrev[e];\n                    bestEnd = e;\n                }\n\n            vector<int> path(5);\n            int curid = bestEnd;\n            for (int k = 4; k >= 0; --k) {\n                path[k] = curid;\n                curid = outPrev[k][curid];\n            }\n\n            entries[idx].push_back({startCell, bestEnd, best, path});\n        }\n    }\n\n    /* ---------- precompute best transitions ---------- */\n    const int INF = 1e9;\n    vector<vector<int>> bestEntry(M, vector<int>(SZ, -1));\n    vector<vector<int>> bestCost(M, vector<int>(SZ, INF));\n\n    for (int idx = 0; idx < M; ++idx) {\n        for (int eIdx = 0; eIdx < (int)entries[idx].size(); ++eIdx) {\n            const Entry &e = entries[idx][eIdx];\n            for (int p = 0; p < SZ; ++p) {\n                int inc = dist[p][e.start] + e.cost;\n                if (inc < bestCost[idx][p]) {\n                    bestCost[idx][p] = inc;\n                    bestEntry[idx][p] = eIdx;\n                }\n            }\n        }\n    }\n\n    /* ---------- greedy initial order ---------- */\n    vector<int> order;\n    vector<bool> used(M, false);\n    int cur = si * N + sj;\n    long long curCost = 0;\n    while ((int)order.size() < M) {\n        long long best = LLONG_MAX;\n        int bestBlock = -1;\n        for (int i = 0; i < M; ++i) if (!used[i]) {\n            int eIdx = bestEntry[i][cur];\n            long long c = bestCost[i][cur];\n            if (c < best) {\n                best = c;\n                bestBlock = i;\n            }\n        }\n        if (bestBlock == -1) break;      // should not happen\n        int eIdx = bestEntry[bestBlock][cur];\n        curCost += bestCost[bestBlock][cur];\n        order.push_back(bestBlock);\n        used[bestBlock] = true;\n        cur = entries[bestBlock][eIdx].end;\n    }\n\n    /* ---------- local improvement (swap hill climbing) ---------- */\n    mt19937 rng((unsigned)chrono::steady_clock::now().time_since_epoch().count());\n    for (int iter = 0; iter < 20000; ++iter) {\n        int i = rng() % M;\n        int j = rng() % M;\n        while (j == i) j = rng() % M;\n        if (i > j) swap(i, j);\n        vector<int> newOrder = order;\n        swap(newOrder[i], newOrder[j]);\n\n        long long newCost = 0;\n        int p = si * N + sj;\n        for (int blk : newOrder) {\n            newCost += bestCost[blk][p];\n            int e = bestEntry[blk][p];\n            p = entries[blk][e].end;\n        }\n        if (newCost < curCost) {\n            order.swap(newOrder);\n            curCost = newCost;\n        }\n    }\n\n    /* ---------- output final moves ---------- */\n    int prevCell = si * N + sj;\n    for (int blk : order) {\n        int eIdx = bestEntry[blk][prevCell];\n        const vector<int> &path = entries[blk][eIdx].path;\n        for (int id : path) {\n            cout << row[id] << ' ' << col[id] << '\\n';\n        }\n        prevCell = entries[blk][eIdx].end;\n    }\n    return 0;\n}","ahc030":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    // Read everything, ignore it.\n    string s;\n    while (cin >> s) {\n        // Do nothing.\n    }\n    return 0;\n}","ahc031":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    int W, D, N;\n    if (!(cin >> W >> D >> N)) return 0;\n    // read the desired areas, they are irrelevant for this trivial solution\n    for (int d = 0; d < D; ++d) {\n        for (int k = 0; k < N; ++k) {\n            long long a; cin >> a;\n        }\n    }\n\n    // For every day, output the same set of N unit squares.\n    for (int d = 0; d < D; ++d) {\n        for (int k = 0; k < N; ++k) {\n            // coordinates: (k,0)-(k+1,1)\n            cout << k << \" \" << 0 << \" \" << k + 1 << \" \" << 1 << '\\n';\n        }\n    }\n    return 0;\n}","ahc032":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    const long long MOD = 998244353LL;\n\n    int N, M, K;\n    if (!(cin >> N >> M >> K)) return 0;          // N = 9, M = 20, K = 81\n\n    /* read the initial board \u2013 keep values modulo MOD */\n    long long board[9][9];\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < N; ++j)\n            cin >> board[i][j];\n\n    /* read the stamps */\n    long long stamp[20][3][3];\n    for (int m = 0; m < M; ++m)\n        for (int i = 0; i < 3; ++i)\n            for (int j = 0; j < 3; ++j)\n                cin >> stamp[m][i][j];\n\n    long long sum = 0;\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < N; ++j)\n            sum += board[i][j];\n\n    struct Op { int m, p, q; };\n    vector<Op> ans;\n\n    while ((int)ans.size() < K) {\n        long long bestDelta = LLONG_MIN;\n        int bestM = -1, bestP = -1, bestQ = -1;\n\n        /* evaluate all possible presses */\n        for (int m = 0; m < M; ++m) {\n            for (int p = 0; p <= N - 3; ++p) {\n                for (int q = 0; q <= N - 3; ++q) {\n                    long long delta = 0;\n                    for (int di = 0; di < 3; ++di)\n                        for (int dj = 0; dj < 3; ++dj) {\n                            long long oldv = board[p + di][q + dj];\n                            long long addv = stamp[m][di][dj];\n                            long long newv = (oldv + addv) % MOD;\n                            delta += newv - oldv;\n                        }\n                    if (delta > bestDelta) {\n                        bestDelta = delta;\n                        bestM = m;\n                        bestP = p;\n                        bestQ = q;\n                    }\n                }\n            }\n        }\n\n        if (bestM == -1 || bestDelta < 0) break;      // no improving move\n\n        /* apply the chosen press */\n        for (int di = 0; di < 3; ++di)\n            for (int dj = 0; dj < 3; ++dj)\n                board[bestP + di][bestQ + dj] =\n                    (board[bestP + di][bestQ + dj] + stamp[bestM][di][dj]) % MOD;\n\n        sum += bestDelta;\n        ans.push_back({bestM, bestP, bestQ});\n    }\n\n    cout << ans.size() << '\\n';\n    for (auto &op : ans)\n        cout << op.m << ' ' << op.p << ' ' << op.q << '\\n';\n\n    return 0;\n}","ahc033":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    const int N = 5;\n    vector<vector<int>> A(N, vector<int>(N));\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < N; ++j)\n            cin >> A[i][j];\n    \n    // position of each container number\n    vector<int> row_of(N*N);\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < N; ++j)\n            row_of[A[i][j]] = i;\n    \n    string ops;       // actions for the large crane\n    int curR = 0, curC = 0;\n    \n    for (int b = 0; b < N*N; ++b) {\n        // move horizontally to column 0\n        while (curC > 0) { ops += 'L'; --curC; }\n        // move vertically to receiving gate row\n        int targetR = row_of[b];\n        while (curR > targetR) { ops += 'U'; --curR; }\n        while (curR < targetR) { ops += 'D'; ++curR; }\n        // pick up\n        ops += 'P';\n        // move to dispatch gate column (col 4)\n        while (curC < 4) { ops += 'R'; ++curC; }\n        // move vertically to target dispatch row\n        int dispatchR = b / N;\n        while (curR > dispatchR) { ops += 'U'; --curR; }\n        while (curR < dispatchR) { ops += 'D'; ++curR; }\n        // release\n        ops += 'Q';\n        // after release curC stays 4\n    }\n    \n    // smallest cranes (cranes 1-4): bomb immediately, then stay\n    string small = string(1, 'B') + string(ops.size()-1, '.');\n    \n    // output lines\n    cout << ops << '\\n';\n    for (int i = 0; i < N-1; ++i) cout << small << '\\n';\n    \n    return 0;\n}","ahc034":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N;\n    if (!(cin >> N)) return 0;      // read the single integer 20\n    vector<vector<int>> h(N, vector<int>(N));\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < N; ++j)\n            cin >> h[i][j];\n\n    struct Cell { int r, c, v; };\n    vector<Cell> pos, neg;\n    pos.reserve(N * N);\n    neg.reserve(N * N);\n\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < N; ++j) {\n            if (h[i][j] > 0) pos.push_back({i, j, h[i][j]});\n            else if (h[i][j] < 0) neg.push_back({i, j, -h[i][j]});\n        }\n\n    vector<string> ops;\n    int curR = 0, curC = 0;\n    auto moveTo = [&](int tr, int tc) {\n        while (curC < tc) { ops.push_back(\"R\"); ++curC; }\n        while (curC > tc) { ops.push_back(\"L\"); --curC; }\n        while (curR < tr) { ops.push_back(\"D\"); ++curR; }\n        while (curR > tr) { ops.push_back(\"U\"); --curR; }\n    };\n\n    /* 1\ufe0f\u20e3 Collect all soil from positives */\n    for (const auto &p : pos) {\n        moveTo(p.r, p.c);\n        ops.emplace_back(\"+\" + to_string(p.v));\n    }\n\n    /* 2\ufe0f\u20e3 Distribute to negatives */\n    for (const auto &n : neg) {\n        moveTo(n.r, n.c);\n        ops.emplace_back(\"-\" + to_string(n.v));\n    }\n\n    for (const auto &s : ops)\n        cout << s << '\\n';\n    return 0;\n}","ahc035":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, T;\n    if (!(cin >> N >> M >> T)) return 0;          // safety\n\n    const int SEED_CNT = 2 * N * (N - 1);         // 60\n    vector<vector<int>> seeds(SEED_CNT, vector<int>(M));\n\n    /* read initial seeds */\n    for (int i = 0; i < SEED_CNT; ++i)\n        for (int j = 0; j < M; ++j)\n            cin >> seeds[i][j];\n\n    const int PLANTED = N * N; // 36\n\n    /* main loop */\n    for (int turn = 0; turn < T; ++turn) {\n        // 1. diversity + sum based selection\n        vector<int> best_val(M, 0);\n        vector<int> chosen;\n        vector<int> remaining(SEED_CNT);\n        iota(remaining.begin(), remaining.end(), 0);\n\n        while ((int)chosen.size() < PLANTED) {\n            int best_idx = -1;\n            pair<int, long long> best_pair{-1, -1}; // (improvements, sum)\n\n            for (int idx : remaining) {\n                int improv = 0;\n                long long sumv = 0;\n                for (int l = 0; l < M; ++l) {\n                    if (seeds[idx][l] > best_val[l]) ++improv;\n                    sumv += seeds[idx][l];\n                }\n                pair<int, long long> cur{improv, sumv};\n                if (cur > best_pair) {\n                    best_pair = cur;\n                    best_idx = idx;\n                }\n            }\n            chosen.push_back(best_idx);\n            // remove from remaining\n            auto it = std::remove(remaining.begin(), remaining.end(), best_idx);\n            remaining.erase(it, remaining.end());\n\n            // update best_val\n            for (int l = 0; l < M; ++l)\n                best_val[l] = max(best_val[l], seeds[best_idx][l]);\n        }\n\n        // 2. output board in row-major\n        int pos = 0;\n        for (int i = 0; i < N; ++i) {\n            for (int j = 0; j < N; ++j) {\n                if (j) cout << ' ';\n                cout << chosen[pos++];\n            }\n            cout << '\\n';\n        }\n        cout.flush();\n\n        // 3. read next set of seeds\n        for (int i = 0; i < SEED_CNT; ++i)\n            for (int j = 0; j < M; ++j)\n                cin >> seeds[i][j];\n    }\n    return 0;\n}","ahc038":"#include <bits/stdc++.h>\nusing namespace std;\n\n/* ---------- arm utility ---------------------------------- */\nstruct Arm {\n    int x, y;                // root coordinates\n    int dir;                 // 0:E, 1:N, 2:W, 3:S\n    vector<string> ops;      // all operation strings\n\n    void addOp(char mv, char rot, char act0, char act1) {\n        string s(4, '.');\n        s[0] = mv; s[1] = rot; s[2] = act0; s[3] = act1;\n        ops.push_back(s);\n    }\n\n    /* rotate fingertip to the required direction */\n    void rotateTo(int target) {\n        while (dir != target) {\n            int diff = (target - dir + 4) % 4;\n            char r = (diff <= 2) ? 'L' : 'R';\n            addOp('.', r, '.', '.');\n            dir = (diff <= 2) ? (dir + 1) & 3 : (dir + 3) & 3;\n        }\n    }\n\n    /* move root to (tx,ty) one cell per turn */\n    void moveTo(int tx, int ty) {\n        while (x < tx) { addOp('D', '.', '.', '.'); ++x; }\n        while (x > tx) { addOp('U', '.', '.', '.'); --x; }\n        while (y < ty) { addOp('R', '.', '.', '.'); ++y; }\n        while (y > ty) { addOp('L', '.', '.', '.'); --y; }\n    }\n};\n\n/* Hungarian algorithm for minimum assignment (square matrix) */\nvector<int> hungarian(const vector<vector<long long>>& a) {\n    int n = a.size();\n    int m = a[0].size();\n    vector<long long> u(n + 1), v(m + 1);\n    vector<int> p(m + 1), way(m + 1);\n    for (int i = 1; i <= n; ++i) {\n        p[0] = i;\n        long long j0 = 0;\n        vector<long long> minv(m + 1, LLONG_MAX);\n        vector<char> used(m + 1, 0);\n        do {\n            used[j0] = 1;\n            int i0 = p[j0], j1 = 0;\n            long long delta = LLONG_MAX;\n            for (int j = 1; j <= m; ++j) if (!used[j]) {\n                long long cur = a[i0 - 1][j - 1] - u[i0] - v[j];\n                if (cur < minv[j]) { minv[j] = cur; way[j] = j0; }\n                if (minv[j] < delta) { delta = minv[j]; j1 = j; }\n            }\n            for (int j = 0; j <= m; ++j) {\n                if (used[j]) { u[p[j]] += delta; v[j] -= delta; }\n                else minv[j] -= delta;\n            }\n            j0 = j1;\n        } while (p[j0] != 0);\n        do {\n            int j1 = way[j0];\n            p[j0] = p[j1];\n            j0 = j1;\n        } while (j0);\n    }\n    vector<int> match(n, -1);\n    for (int j = 1; j <= m; ++j)\n        if (p[j] != 0) match[p[j] - 1] = j - 1;\n    return match;          // left i -> right match[i]\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, V;\n    if (!(cin >> N >> M >> V)) return 0;\n    vector<string> s(N), t(N);\n    for (int i = 0; i < N; ++i) cin >> s[i];\n    for (int i = 0; i < N; ++i) cin >> t[i];\n\n    /* ---------- collect positions & board state ------------- */\n    vector<pair<int,int>> srcAll, tgtAll;\n    vector<vector<int>> board(N, vector<int>(N, 0));\n    unordered_set<long long> initPos, tgtPos;\n    auto key = [&](int r, int c){ return ( (long long)r << 32 ) | (unsigned int)c; };\n\n    for (int i = 0; i < N; ++i)\n        for (int j = 0; j < N; ++j) {\n            if (s[i][j] == '1') {\n                srcAll.emplace_back(i,j);\n                board[i][j] = 1;\n                initPos.insert(key(i,j));\n            }\n            if (t[i][j] == '1') {\n                tgtAll.emplace_back(i,j);\n                tgtPos.insert(key(i,j));\n            }\n        }\n\n    /* ---------- which takoyaki actually need to move -------- */\n    vector<pair<int,int>> srcMove, tgtFree;\n    for (auto &p : srcAll)\n        if (!tgtPos.count(key(p.first,p.second)))\n            srcMove.push_back(p);\n    for (auto &p : tgtAll)\n        if (!initPos.count(key(p.first,p.second)))\n            tgtFree.push_back(p);\n\n    if ((int)srcMove.size() != (int)tgtFree.size())\n        return 0;      // should not happen\n    int K = srcMove.size();   // number of takoyaki that need moving\n\n    /* ---------- optimal assignment of targets to sources ----- */\n    vector<vector<long long>> cost(K, vector<long long>(K, 0));\n    for (int i = 0; i < K; ++i)\n        for (int j = 0; j < K; ++j)\n            cost[i][j] =\n                llabs(srcMove[i].first  - tgtFree[j].first) +\n                llabs(srcMove[i].second - tgtFree[j].second);\n\n    vector<int> match = hungarian(cost);   // src i -> tgt match[i]\n\n    /* ---------- output arm ---------------------------------- */\n    cout << 2 << '\\n';          // V' = 2 (root + one fingertip)\n    cout << 0 << ' ' << 1 << '\\n';   // parent of 1 is 0, length 1\n    cout << 0 << ' ' << 0 << '\\n';   // initial root position (0,0)\n\n    Arm arm;\n    arm.x = 0; arm.y = 0; arm.dir = 0;   // fingertip initially faces East\n\n    /* helper that positions fingertip on cell (r,c)                    */\n    auto approach = [&](int r, int c) {\n        int rr, cc, targetDir;\n        if (c > 0) {\n            targetDir = 0; rr = r; cc = c-1;            // left of target\n        } else if (r > 0) {\n            targetDir = 3; rr = r-1; cc = c;            // above target\n        } else {                                     // only (0,0) left\n            targetDir = 1; rr = r+1; cc = c;            // below target\n        }\n        arm.rotateTo(targetDir);\n        arm.moveTo(rr, cc);\n    };\n\n    /* ---------- perform moves ----------------------------------- */\n    for (int i = 0; i < K; ++i) {\n        int srcIdx = i;\n        int trgIdx = match[i];\n        int rs = srcMove[srcIdx].first,  cs = srcMove[srcIdx].second;\n        int rt = tgtFree[trgIdx].first, ct = tgtFree[trgIdx].second;\n\n        /* move to source and pick */\n        approach(rs, cs);\n        arm.addOp('.', '.', '.', 'P');\n        board[rs][cs] = 0;\n\n        /* move to target and drop */\n        approach(rt, ct);\n        arm.addOp('.', '.', '.', 'P');\n        board[rt][ct] = 1;\n    }\n\n    /* ---------- output turns ------------------------------------ */\n    for (const string &op : arm.ops) cout << op << '\\n';\n    return 0;\n}","ahc039":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    const int MAXC = 100000;\n    const int S = 200;                          // cell size\n    const int NX = MAXC / S + 1;                // 501\n    const int NY = MAXC / S + 1;\n\n    int N;\n    if (!(cin >> N)) return 0;\n\n    // diff[cx][cy] = mackerels - sardines in that cell\n    vector<vector<int>> diff(NX, vector<int>(NY, 0));\n\n    // store one mackerel to use for fallback\n    int first_mack_x = -1, first_mack_y = -1;\n\n    for (int i = 0; i < N; ++i) {\n        int x, y;\n        cin >> x >> y;\n        int cx = x / S;\n        int cy = y / S;\n        diff[cx][cy] += 1;\n        if (first_mack_x == -1) { first_mack_x = x; first_mack_y = y; }\n    }\n    for (int i = 0; i < N; ++i) {\n        int x, y;\n        cin >> x >> y;\n        int cx = x / S;\n        int cy = y / S;\n        diff[cx][cy] -= 1;\n    }\n\n    long long bestSum = LLONG_MIN;\n    int bestX1 = 0, bestY1 = 0, bestX2 = 0, bestY2 = 0;\n\n    vector<long long> colSum(NX);\n    for (int y1 = 0; y1 < NY; ++y1) {\n        fill(colSum.begin(), colSum.end(), 0);\n        for (int y2 = y1; y2 < NY; ++y2) {\n            for (int x = 0; x < NX; ++x)\n                colSum[x] += diff[x][y2];\n\n            // Kadane on colSum\n            long long curSum = 0;\n            int curL = 0;\n            long long localBest = LLONG_MIN;\n            int localL = 0, localR = 0;\n            for (int x = 0; x < NX; ++x) {\n                curSum += colSum[x];\n                if (curSum > localBest) {\n                    localBest = curSum;\n                    localL = curL;\n                    localR = x;\n                }\n                if (curSum < 0) {\n                    curSum = 0;\n                    curL = x + 1;\n                }\n            }\n            if (localBest > bestSum) {\n                bestSum = localBest;\n                bestX1 = localL;\n                bestX2 = localR;\n                bestY1 = y1;\n                bestY2 = y2;\n            }\n        }\n    }\n\n    // if bestSum <= 0, fallback to a single mackerel\n    long long rectSum = bestSum;\n    int x1, y1, x2, y2;\n    if (bestSum > 0) {\n        x1 = bestX1 * S;\n        y1 = bestY1 * S;\n        x2 = min((bestX2 + 1) * S, MAXC);\n        y2 = min((bestY2 + 1) * S, MAXC);\n    } else {\n        // single mackerel cell\n        int cx = first_mack_x / S;\n        int cy = first_mack_y / S;\n        x1 = cx * S;\n        y1 = cy * S;\n        x2 = min((cx + 1) * S, MAXC);\n        y2 = min((cy + 1) * S, MAXC);\n    }\n\n    cout << 4 << '\\n';\n    cout << x1 << ' ' << y1 << '\\n';\n    cout << x2 << ' ' << y1 << '\\n';\n    cout << x2 << ' ' << y2 << '\\n';\n    cout << x1 << ' ' << y2 << '\\n';\n    return 0;\n}","ahc040":"#include <bits/stdc++.h>\nusing namespace std;\n\n// ------------------------------------------------------------\n// Interactive solution that packs all rectangles in a single\n// vertical stack.  The orientation of each rectangle is chosen\n// to minimise the value  width + height of the final bounding box.\n// ------------------------------------------------------------\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, T, sigma;\n    if (!(cin >> N >> T >> sigma)) return 0;\n\n    vector<long long> w(N), h(N);\n    for (int i = 0; i < N; ++i) cin >> w[i] >> h[i];\n\n    /* ----- enumerate candidate widths (w or h of any rectangle) ----- */\n    vector<long long> cand;\n    cand.reserve(2 * N);\n    for (int i = 0; i < N; ++i) {\n        cand.push_back(w[i]);\n        cand.push_back(h[i]);\n    }\n    sort(cand.begin(), cand.end());\n    cand.erase(unique(cand.begin(), cand.end()), cand.end());\n\n    /* ----- find orientation that minimises W + H for the vertical stack ----- */\n    long long bestScore = LLONG_MAX;\n    vector<int> bestOrient(N, 0);\n\n    for (long long Wmax : cand) {\n        long long totalH = 0;\n        vector<int> orientCur(N, 0);\n        bool feasible = true;\n        for (int i = 0; i < N; ++i) {\n            bool can0 = (w[i] <= Wmax); // keep as (w,h)\n            bool can1 = (h[i] <= Wmax); // rotate to (h,w)\n            if (!can0 && !can1) { feasible = false; break; }\n\n            if (can0 && !can1) {\n                orientCur[i] = 0;\n                totalH += h[i];\n            } else if (!can0 && can1) {\n                orientCur[i] = 1;\n                totalH += w[i];\n            } else {\n                // both fit \u2013 choose orientation with smaller height\n                if (h[i] <= w[i]) {\n                    orientCur[i] = 0;\n                    totalH += h[i];\n                } else {\n                    orientCur[i] = 1;\n                    totalH += w[i];\n                }\n            }\n        }\n        if (!feasible) continue;\n        long long score = Wmax + totalH;\n        if (score < bestScore) {\n            bestScore = score;\n            bestOrient = orientCur;\n        }\n    }\n\n    /* ----- output the vertical\u2011stack packing for every turn ----- */\n    for (int turn = 0; turn < T; ++turn) {\n        cout << N << '\\n';\n        // first rectangle\n        cout << 0 << ' ' << bestOrient[0] << ' ' << 'U' << ' ' << -1 << '\\n';\n        // remaining rectangles: each below the previous one\n        for (int i = 1; i < N; ++i) {\n            cout << i << ' ' << bestOrient[i] << ' ' << 'L' << ' ' << i - 1 << '\\n';\n        }\n        cout.flush();\n\n        // read and ignore judge's reply\n        long long Wp, Hp;\n        cin >> Wp >> Hp;\n    }\n    return 0;\n}","ahc041":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    int N, M, H;\n    if (!(cin >> N >> M >> H)) return 0;\n\n    vector<int> A(N);\n    for (int i = 0; i < N; ++i) cin >> A[i];\n\n    vector<vector<int>> adj(N);\n    for (int i = 0; i < M; ++i) {\n        int u, v; cin >> u >> v;\n        adj[u].push_back(v);\n        adj[v].push_back(u);\n    }\n    for (int i = 0; i < N; ++i) { int x, y; cin >> x >> y; }   // ignore coordinates\n\n    mt19937 rng((unsigned)chrono::steady_clock::now().time_since_epoch().count());\n\n    vector<int> best_parent(N, -1);\n    long long best_score = -1;\n\n    const int ITER = 200;          // number of random attempts\n    vector<int> nodes(N);\n    iota(nodes.begin(), nodes.end(), 0);\n\n    for (int it = 0; it < ITER; ++it) {\n        vector<int> parent(N, -1);\n        vector<char> used(N, 0);\n        vector<int> depth(N, 0);\n\n        // shuffle root order\n        shuffle(nodes.begin(), nodes.end(), rng);\n\n        for (int root : nodes) {\n            if (used[root]) continue;\n            // start new tree\n            parent[root] = -1;\n            depth[root] = 0;\n            used[root] = 1;\n\n            // depth\u2011first search to depth H\n            function<void(int,int)> dfs = [&](int cur, int d) {\n                if (d == H) return;\n                vector<int> nb(adj[cur].begin(), adj[cur].end());\n                shuffle(nb.begin(), nb.end(), rng);      // random neighbor order\n                for (int nbv : nb) {\n                    if (!used[nbv]) {\n                        used[nbv] = 1;\n                        parent[nbv] = cur;\n                        depth[nbv] = d + 1;\n                        dfs(nbv, d + 1);\n                    }\n                }\n            };\n            dfs(root, 0);\n        }\n\n        // compute score\n        long long score = 0;\n        for (int v = 0; v < N; ++v) {\n            score += 1LL * (depth[v] + 1) * A[v];\n        }\n        if (score > best_score) {\n            best_score = score;\n            best_parent = parent;\n        }\n    }\n\n    // output\n    for (int i = 0; i < N; ++i) {\n        if (i) cout << ' ';\n        cout << best_parent[i];\n    }\n    cout << '\\n';\n    return 0;\n}","ahc042":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 20;\n\n// Shift helpers --------------------------------------------------------\nvoid shiftRowLeft(vector<string> &b, int r) {\n    for (int j = 0; j < N - 1; ++j) b[r][j] = b[r][j + 1];\n    b[r][N - 1] = '.';\n}\nvoid shiftRowRight(vector<string> &b, int r) {\n    for (int j = N - 1; j > 0; --j) b[r][j] = b[r][j - 1];\n    b[r][0] = '.';\n}\nvoid shiftColUp(vector<string> &b, int c) {\n    for (int i = 0; i < N - 1; ++i) b[i][c] = b[i + 1][c];\n    b[N - 1][c] = '.';\n}\nvoid shiftColDown(vector<string> &b, int c) {\n    for (int i = N - 1; i > 0; --i) b[i][c] = b[i - 1][c];\n    b[0][c] = '.';\n}\n\n// --------------------------------------------------------------------\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int n;\n    if (!(cin >> n)) return 0;                // read the (fixed) N\n    vector<string> board(n);\n    for (int i = 0; i < n; ++i) cin >> board[i];\n\n    vector<pair<char, int>> ops;\n\n    while (true) {\n        int ri = -1, rj = -1;\n        // find any oni\n        for (int i = 0; i < n && ri == -1; ++i)\n            for (int j = 0; j < n; ++j)\n                if (board[i][j] == 'x') { ri = i; rj = j; break; }\n\n        if (ri == -1) break;          // no oni left\n\n        // choose a safe direction\n        char dir; int cost;\n        bool upSafe = true;\n        for (int i = 0; i < ri; ++i)\n            if (board[i][rj] == 'o') { upSafe = false; break; }\n\n        if (upSafe) {\n            dir = 'U';\n            cost = ri + 1;\n        } else {\n            bool downSafe = true;\n            for (int i = ri + 1; i < n; ++i)\n                if (board[i][rj] == 'o') { downSafe = false; break; }\n            if (downSafe) {\n                dir = 'D';\n                cost = n - ri;\n            } else {\n                bool leftSafe = true;\n                for (int j = 0; j < rj; ++j)\n                    if (board[ri][j] == 'o') { leftSafe = false; break; }\n                if (leftSafe) {\n                    dir = 'L';\n                    cost = rj + 1;\n                } else {          // right is guaranteed safe\n                    dir = 'R';\n                    cost = n - rj;\n                }\n            }\n        }\n\n        // perform the shifts\n        for (int t = 0; t < cost; ++t) {\n            ops.emplace_back(dir, (dir == 'U' || dir == 'D') ? rj : ri);\n            if (dir == 'L') shiftRowLeft(board, ri);\n            else if (dir == 'R') shiftRowRight(board, ri);\n            else if (dir == 'U') shiftColUp(board, rj);\n            else                shiftColDown(board, rj);\n        }\n    }\n\n    // output\n    for (auto &op : ops)\n        cout << op.first << ' ' << op.second << '\\n';\n    return 0;\n}","ahc044":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 100;\nconst int L = 500000;\nusing ll = long long;\n\n/*--------------------------------------------------------------*/\n/*  simulate the whole walk for given a[], b[]                   */\n/*  returns the total absolute error and fills the cnt array     */\n/*--------------------------------------------------------------*/\nstatic ll simulate(const int a[], const int b[],\n                   const int T[], int cnt[])\n{\n    memset(cnt, 0, N * sizeof(int));\n    int cur = 0;            // week 1\n    cnt[0] = 1;\n    for (int week = 2; week <= L; ++week) {\n        int t = cnt[cur];\n        int nxt = (t & 1) ? a[cur] : b[cur];\n        cur = nxt;\n        ++cnt[cur];\n    }\n    ll err = 0;\n    for (int i = 0; i < N; ++i) err += llabs(cnt[i] - T[i]);\n    return err;\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int n, l;                               // 100, 500000\n    if (!(cin >> n >> l)) return 0;\n    int T[N];\n    for (int i = 0; i < N; ++i) cin >> T[i];\n\n    /* ---------- initial mapping: greedily assign to highest need  */\n    int a[N], b[N];\n    int need[N];\n    memcpy(need, T, N * sizeof(int));\n\n    for (int i = 0; i < N; ++i) {\n        int best = 0;\n        for (int j = 1; j < N; ++j)\n            if (need[j] > need[best]) best = j;\n        a[i] = best;\n        need[best] = max(0, need[best] - 1);\n\n        best = 0;\n        for (int j = 1; j < N; ++j)\n            if (need[j] > need[best]) best = j;\n        b[i] = best;\n        need[best] = max(0, need[best] - 1);\n    }\n\n    /* ---------- simulation of initial mapping  ----------------- */\n    int bestCnt[N], curCnt[N];\n    ll bestErr = simulate(a, b, T, bestCnt);\n    memcpy(curCnt, bestCnt, N * sizeof(int));\n    int bestA[N], bestB[N];\n    memcpy(bestA, a, N * sizeof(int));\n    memcpy(bestB, b, N * sizeof(int));\n\n    /* ---------- random generators ------------------------------ */\n    mt19937_64 rng((unsigned long long)\n                   chrono::steady_clock::now().time_since_epoch().count());\n    uniform_int_distribution<int> distNode(0, N - 1);\n    uniform_int_distribution<int> distSide(0, 1);\n    uniform_real_distribution<double> distReal(0.0, 1.0);\n\n    /* ---------- simulated annealing -------------------------------- */\n    double temp = 2000.0;\n    const double cooling = 0.999;          // cool slowly\n    const double TIME_LIMIT_MS = 1900.0;   // safety margin\n\n    auto start = chrono::steady_clock::now();\n\n    int itCnt = 0;\n    while (true) {\n        auto now = chrono::steady_clock::now();\n        if (chrono::duration<double, milli>(now - start).count() >= TIME_LIMIT_MS)\n            break;\n        ++itCnt;\n\n        /* mutate one outgoing edge */\n        int i = distNode(rng);\n        int side = distSide(rng);          // 0 \u2192 a[i], 1 \u2192 b[i]\n        int oldVal = side ? b[i] : a[i];\n\n        /* propose a new target employee with the largest deficit */\n        int bestJ = 0;\n        ll bestDiff = (ll)T[0] - curCnt[0];\n        for (int j = 1; j < N; ++j) {\n            ll diff = (ll)T[j] - curCnt[j];\n            if (diff > bestDiff) {\n                bestDiff = diff;\n                bestJ = j;\n            }\n        }\n        int newVal = bestJ;\n        if (newVal == oldVal)   // fall back to random if same\n            newVal = distNode(rng);\n        while (newVal == oldVal) newVal = distNode(rng);\n\n        if (side) b[i] = newVal; else a[i] = newVal;\n\n        int cntNew[N];\n        ll newErr = simulate(a, b, T, cntNew);\n\n        bool accept = false;\n        if (newErr <= bestErr) {\n            accept = true;\n            bestErr = newErr;\n            memcpy(bestA, a, N * sizeof(int));\n            memcpy(bestB, b, N * sizeof(int));\n            memcpy(bestCnt, cntNew, N * sizeof(int));\n        } else {\n            double prob = exp(-(newErr - bestErr) / temp);\n            if (prob > distReal(rng))\n                accept = true;          // accept worse move\n        }\n\n        if (accept) {\n            memcpy(curCnt, cntNew, N * sizeof(int));\n        } else {                        // revert change\n            if (side) b[i] = oldVal; else a[i] = oldVal;\n        }\n\n        temp *= cooling;\n    }\n\n    /* ---------- output best found mapping ------------------------ */\n    for (int i = 0; i < N; ++i)\n        cout << bestA[i] << ' ' << bestB[i] << '\\n';\n    return 0;\n}","ahc045":"#include <bits/stdc++.h>\nusing namespace std;\n\n// ------------------------------------------------------------\n// Hilbert curve order (2\u2011D Morton with 16 bits per axis)\n// ------------------------------------------------------------\ninline uint64_t hilbert(uint16_t x, uint16_t y, int pow = 16) {\n    uint64_t h = 0;\n    for (int i = pow - 1; i >= 0; --i) {\n        uint64_t mask = 1ULL << i;\n        uint64_t a = (x & mask) != 0;\n        uint64_t b = (y & mask) != 0;\n        uint64_t seg = (a << 1) | b;\n        h = (h << 2) | seg;\n        // rotate\n        if (b) {\n            uint16_t tmp = x;\n            x = y;\n            y = tmp ^ (x + y);   // rotate 90\u00b0\n        }\n    }\n    return h;\n}\n\n// ------------------------------------------------------------\n// main\n// ------------------------------------------------------------\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, Q, L, W;\n    if (!(cin >> N >> M >> Q >> L >> W)) return 0;\n\n    vector<int> G(M);\n    for (int &x : G) cin >> x;\n\n    vector<double> cx(N), cy(N);    // centre of rectangle\n    for (int i = 0; i < N; ++i) {\n        int lx, rx, ly, ry;\n        cin >> lx >> rx >> ly >> ry;\n        cx[i] = (lx + rx) / 2.0;\n        cy[i] = (ly + ry) / 2.0;\n    }\n\n    // Hilbert order of each city\n    vector<uint64_t> H(N);\n    for (int i = 0; i < N; ++i) {\n        uint16_t hx = static_cast<uint16_t>(min<int>(65535,\n                           static_cast<int>(round(cx[i] * 65535.0 / 10000.0))));\n        uint16_t hy = static_cast<uint16_t>(min<int>(65535,\n                           static_cast<int>(round(cy[i] * 65535.0 / 10000.0))));\n        H[i] = hilbert(hx, hy, 16);\n    }\n\n    // ordering of cities by Hilbert curve\n    vector<int> id(N);\n    iota(id.begin(), id.end(), 0);\n    sort(id.begin(), id.end(),\n         [&](int a, int b) { return H[a] < H[b]; });\n\n    cout << \"!\\n\";\n\n    int pos = 0;                     // position in id\n    vector<int> best, parent;\n    vector<char> used;\n    for (int grp = 0; grp < M; ++grp) {\n        int k = G[grp];\n        vector<int> V(id.begin() + pos, id.begin() + pos + k);\n        pos += k;\n\n        // ---------- Prim's MST on V ----------\n        best.assign(k, INT_MAX);\n        parent.assign(k, -1);\n        used.assign(k, 0);\n        best[0] = 0;\n\n        vector<pair<int,int>> mst_edges;\n        mst_edges.reserve(k - 1);\n\n        for (int it = 0; it < k; ++it) {\n            // select unused vertex with minimal best[]\n            int u = -1;\n            int best_val = INT_MAX;\n            for (int i = 0; i < k; ++i) if (!used[i] && best[i] < best_val) {\n                best_val = best[i];\n                u = i;\n            }\n            used[u] = 1;\n            if (parent[u] != -1)\n                mst_edges.push_back({V[u], V[parent[u]]});\n\n            // relax neighbours\n            for (int v = 0; v < k; ++v) if (!used[v]) {\n                double dx = cx[V[u]] - cx[V[v]];\n                double dy = cy[V[u]] - cy[V[v]];\n                int d2 = static_cast<int>(dx * dx + dy * dy + 0.5); // squared distance\n                if (d2 < best[v]) {\n                    best[v] = d2;\n                    parent[v] = u;\n                }\n            }\n        }\n\n        // ---------- output ----------\n        for (int i = 0; i < k; ++i) {\n            if (i) cout << ' ';\n            cout << V[i];\n        }\n        cout << '\\n';\n        for (auto &e : mst_edges) {\n            cout << e.first << ' ' << e.second << '\\n';\n        }\n    }\n    return 0;\n}","ahc046":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Cell {\n    int r, c;\n};\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    int N, M;\n    if (!(cin >> N >> M)) return 0;\n    vector<Cell> pos(M);\n    for (int i = 0; i < M; ++i) cin >> pos[i].r >> pos[i].c;\n\n    const int dr[4] = {-1, 1, 0, 0};\n    const int dc[4] = {0, 0, -1, 1};\n    const char dirChar[4] = {'U', 'D', 'L', 'R'};\n\n    vector<pair<char,char>> output;           // action, direction\n\n    Cell cur = pos[0];\n    for (int k = 0; k < M-1 && (int)output.size() < 800; ++k) {\n        Cell to = pos[k+1];\n\n        // ---- blocked array: all future targets except the next one ----\n        bool blocked[20][20] = {false};\n        for (int t = k+2; t < M; ++t) {\n            blocked[pos[t].r][pos[t].c] = true;\n        }\n\n        // ---- BFS -------------------------------------------------------\n        int dist[20][20];\n        int pr[20][20], pc[20][20];\n        char pd[20][20];\n        memset(dist, -1, sizeof(dist));\n        queue<Cell> q;\n        dist[cur.r][cur.c] = 0;\n        q.push(cur);\n\n        while (!q.empty() && dist[to.r][to.c] == -1) {\n            Cell curc = q.front(); q.pop();\n            for (int d = 0; d < 4; ++d) {\n                int nr = curc.r + dr[d];\n                int nc = curc.c + dc[d];\n                if (nr < 0 || nr >= N || nc < 0 || nc >= N) continue;\n                if (blocked[nr][nc]) continue;\n                if (dist[nr][nc] != -1) continue;\n                dist[nr][nc] = dist[curc.r][curc.c] + 1;\n                pr[nr][nc] = curc.r;\n                pc[nr][nc] = curc.c;\n                pd[nr][nc] = dirChar[d];\n                q.push({nr, nc});\n            }\n        }\n\n        if (dist[to.r][to.c] == -1) { // unreachable \u2013 stop completely\n            break;\n        }\n\n        // ---- reconstruct path -----------------------------------------\n        vector<char> pathDirs;\n        int cr = to.r, cc = to.c;\n        while (!(cr == cur.r && cc == cur.c)) {\n            pathDirs.push_back(pd[cr][cc]);      // direction from predecessor to this cell\n            int tr = pr[cr][cc], tc = pc[cr][cc];\n            cr = tr; cc = tc;\n        }\n        reverse(pathDirs.begin(), pathDirs.end());\n\n        // ---- output actions --------------------------------------------\n        for (char d : pathDirs) {\n            if ((int)output.size() == 800) break;\n            output.emplace_back('M', d);\n        }\n\n        cur = to; // reached the next target\n    }\n\n    // ---- write result -----------------------------------------------\n    for (auto &act : output) {\n        cout << act.first << ' ' << act.second << '\\n';\n    }\n    return 0;\n}"}}}