{"model_name":"deepseek-r1-0528","codes":{"1":{"ahc001":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <cmath>\n#include <cstdlib>\n#include <iomanip>\nusing namespace std;\n\nstruct Rect {\n    int a, b, c, d;\n    int area;\n    int r_val;\n};\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int n;\n    cin >> n;\n    vector<Rect> rects;\n    for (int i = 0; i < n; i++) {\n        int x, y, r;\n        cin >> x >> y >> r;\n        rects.push_back({x, y, x+1, y+1, 1, r});\n    }\n\n    bool changed = true;\n    int pass_count = 0;\n    const int MAX_PASS = 1000;\n\n    while (changed && pass_count < MAX_PASS) {\n        pass_count++;\n        changed = false;\n        vector<int> indices(n);\n        for (int i = 0; i < n; i++) {\n            indices[i] = i;\n        }\n\n        sort(indices.begin(), indices.end(), [&](int i, int j) {\n            double sat_i = (double)rects[i].area / rects[i].r_val;\n            double sat_j = (double)rects[j].area / rects[j].r_val;\n            return sat_i < sat_j;\n        });\n\n        for (int idx = 0; idx < n; idx++) {\n            int i = indices[idx];\n            if (rects[i].area >= rects[i].r_val) \n                continue;\n\n            double best_improvement = 0.0;\n            int best_dir = -1;\n            int best_d_use = 0;\n\n            for (int dir = 0; dir < 4; dir++) {\n                int d_max;\n                if (dir == 0) {\n                    d_max = rects[i].a;\n                } else if (dir == 1) {\n                    d_max = 10000 - rects[i].c;\n                } else if (dir == 2) {\n                    d_max = rects[i].b;\n                } else {\n                    d_max = 10000 - rects[i].d;\n                }\n\n                if (d_max <= 0) continue;\n\n                int side_length;\n                if (dir < 2) {\n                    side_length = rects[i].d - rects[i].b;\n                } else {\n                    side_length = rects[i].c - rects[i].a;\n                }\n                if (side_length <= 0) continue;\n\n                for (int j = 0; j < n; j++) {\n                    if (i == j) continue;\n                    if (dir == 0) {\n                        if (rects[i].b < rects[j].d && rects[j].b < rects[i].d) {\n                            if (rects[j].c <= rects[i].a) {\n                                d_max = min(d_max, rects[i].a - rects[j].c);\n                            }\n                        }\n                    } else if (dir == 1) {\n                        if (rects[i].b < rects[j].d && rects[j].b < rects[i].d) {\n                            if (rects[j].a >= rects[i].c) {\n                                d_max = min(d_max, rects[j].a - rects[i].c);\n                            }\n                        }\n                    } else if (dir == 2) {\n                        if (rects[i].a < rects[j].c && rects[j].a < rects[i].c) {\n                            if (rects[j].d <= rects[i].b) {\n                                d_max = min(d_max, rects[i].b - rects[j].d);\n                            }\n                        }\n                    } else if (dir == 3) {\n                        if (rects[i].a < rects[j].c && rects[j].a < rects[i].c) {\n                            if (rects[j].b >= rects[i].d) {\n                                d_max = min(d_max, rects[j].b - rects[i].d);\n                            }\n                        }\n                    }\n                }\n\n                if (d_max <= 0) continue;\n\n                int d_desired = (rects[i].r_val - rects[i].area) / side_length;\n                if (d_desired <= 0) continue;\n\n                int d_use = min(d_max, d_desired);\n                if (d_use <= 0) continue;\n\n                long long dA = (long long)d_use * side_length;\n                double s_i_val = rects[i].area;\n                double r_i_val = rects[i].r_val;\n                double old_p = 1.0 - pow(1.0 - min(s_i_val, r_i_val) / max(s_i_val, r_i_val), 2);\n                double new_s = s_i_val + dA;\n                double new_p = 1.0 - pow(1.0 - min(new_s, r_i_val) / max(new_s, r_i_val), 2);\n                double improvement = new_p - old_p;\n\n                if (improvement > best_improvement) {\n                    best_improvement = improvement;\n                    best_dir = dir;\n                    best_d_use = d_use;\n                }\n            }\n\n            if (best_dir != -1) {\n                if (best_dir == 0) {\n                    rects[i].a -= best_d_use;\n                    rects[i].area += best_d_use * (rects[i].d - rects[i].b);\n                } else if (best_dir == 1) {\n                    rects[i].c += best_d_use;\n                    rects[i].area += best_d_use * (rects[i].d - rects[i].b);\n                } else if (best_dir == 2) {\n                    rects[i].b -= best_d_use;\n                    rects[i].area += best_d_use * (rects[i].c - rects[i].a);\n                } else if (best_dir == 3) {\n                    rects[i].d += best_d_use;\n                    rects[i].area += best_d_use * (rects[i].c - rects[i].a);\n                }\n                changed = true;\n            }\n        }\n    }\n\n    for (int i = 0; i < n; i++) {\n        cout << rects[i].a << \" \" << rects[i].b << \" \" << rects[i].c << \" \" << rects[i].d << \"\\n\";\n    }\n\n    return 0;\n}","ahc002":"#include <iostream>\n#include <vector>\n#include <set>\n#include <algorithm>\n#include <tuple>\nusing namespace std;\n\nint main() {\n    int si, sj;\n    cin >> si >> sj;\n    \n    vector<vector<int>> t(50, vector<int>(50));\n    vector<vector<int>> p(50, vector<int>(50));\n    \n    for (int i = 0; i < 50; i++) {\n        for (int j = 0; j < 50; j++) {\n            cin >> t[i][j];\n        }\n    }\n    for (int i = 0; i < 50; i++) {\n        for (int j = 0; j < 50; j++) {\n            cin >> p[i][j];\n        }\n    }\n    \n    int M = 0;\n    for (int i = 0; i < 50; i++) {\n        for (int j = 0; j < 50; j++) {\n            if (t[i][j] + 1 > M) {\n                M = t[i][j] + 1;\n            }\n        }\n    }\n    \n    vector<set<int>> adj_tiles(M);\n    const int dx[4] = {1, -1, 0, 0};\n    const int dy[4] = {0, 0, 1, -1};\n    \n    for (int i = 0; i < 50; i++) {\n        for (int j = 0; j < 50; j++) {\n            for (int d = 0; d < 4; d++) {\n                int ni = i + dx[d];\n                int nj = j + dy[d];\n                if (ni >= 0 && ni < 50 && nj >= 0 && nj < 50) {\n                    if (t[i][j] != t[ni][nj]) {\n                        adj_tiles[t[i][j]].insert(t[ni][nj]);\n                    }\n                }\n            }\n        }\n    }\n    \n    vector<int> current_degree(M);\n    for (int i = 0; i < M; i++) {\n        current_degree[i] = adj_tiles[i].size();\n    }\n    \n    vector<bool> visited_tile(M, false);\n    int cx = si, cy = sj;\n    int start_tile = t[si][sj];\n    visited_tile[start_tile] = true;\n    \n    for (int adj : adj_tiles[start_tile]) {\n        if (!visited_tile[adj]) {\n            current_degree[adj]--;\n        }\n    }\n    \n    string path = \"\";\n    const char dir_char[4] = {'D', 'U', 'R', 'L'};\n    \n    while (true) {\n        vector<tuple<int, char, int, int>> candidates;\n        for (int d = 0; d < 4; d++) {\n            int nx = cx + dx[d];\n            int ny = cy + dy[d];\n            if (nx < 0 || nx >= 50 || ny < 0 || ny >= 50) continue;\n            int tile_next = t[nx][ny];\n            if (visited_tile[tile_next]) continue;\n            \n            int score = p[nx][ny] + 100 * current_degree[tile_next];\n            candidates.push_back(make_tuple(score, dir_char[d], nx, ny));\n        }\n        \n        if (candidates.empty()) break;\n        \n        auto best = *max_element(candidates.begin(), candidates.end(),\n            [](const tuple<int, char, int, int>& a, const tuple<int, char, int, int>& b) {\n                return get<0>(a) < get<0>(b);\n            });\n        \n        char dir = get<1>(best);\n        int nx = get<2>(best);\n        int ny = get<3>(best);\n        int tile_next = t[nx][ny];\n        \n        path += dir;\n        cx = nx;\n        cy = ny;\n        visited_tile[tile_next] = true;\n        \n        for (int adj : adj_tiles[tile_next]) {\n            if (!visited_tile[adj]) {\n                current_degree[adj]--;\n            }\n        }\n    }\n    \n    cout << path << endl;\n    \n    return 0;\n}","ahc003":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\n#include <tuple>\n#include <cmath>\n#include <iomanip>\n\nusing namespace std;\n\nconst int N = 30;\nconst double INF = 1e18;\n\nvector<vector<double>> h_est(N, vector<double>(N-1, 5000.0));\nvector<vector<double>> v_est(N-1, vector<double>(N, 5000.0));\nvector<vector<int>> count_h(N, vector<int>(N-1, 0));\nvector<vector<int>> count_v(N-1, vector<int>(N, 0));\n\nconst int dx[4] = {0, 1, 0, -1};\nconst int dy[4] = {1, 0, -1, 0};\nconst char dir_char[4] = {'R', 'D', 'L', 'U'};\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    for (int k = 0; k < 1000; k++) {\n        int si, sj, ti, tj;\n        cin >> si >> sj >> ti >> tj;\n\n        vector<vector<double>> dist(N, vector<double>(N, INF));\n        vector<vector<pair<int, int>>> prev(N, vector<pair<int, int>>(N, {-1, -1}));\n        priority_queue<tuple<double, int, int>, vector<tuple<double, int, int>>, greater<tuple<double, int, int>>> pq;\n\n        dist[si][sj] = 0;\n        pq.push({0, si, sj});\n\n        while (!pq.empty()) {\n            auto [d, i, j] = pq.top();\n            pq.pop();\n            if (d > dist[i][j]) continue;\n            if (i == ti && j == tj) break;\n\n            for (int dir = 0; dir < 4; dir++) {\n                int ni = i + dx[dir];\n                int nj = j + dy[dir];\n                if (ni < 0 || ni >= N || nj < 0 || nj >= N) continue;\n\n                double weight;\n                if (dir == 0) {\n                    weight = h_est[i][j];\n                } else if (dir == 1) {\n                    weight = v_est[i][j];\n                } else if (dir == 2) {\n                    weight = h_est[i][j-1];\n                } else {\n                    weight = v_est[i-1][j];\n                }\n\n                double new_dist = dist[i][j] + weight;\n                if (new_dist < dist[ni][nj]) {\n                    dist[ni][nj] = new_dist;\n                    prev[ni][nj] = {i, j};\n                    pq.push({new_dist, ni, nj});\n                }\n            }\n        }\n\n        vector<char> moves;\n        int ci = ti, cj = tj;\n        while (ci != si || cj != sj) {\n            int pi = prev[ci][cj].first;\n            int pj = prev[ci][cj].second;\n            if (pi == ci) {\n                if (pj < cj) {\n                    moves.push_back('R');\n                } else {\n                    moves.push_back('L');\n                }\n            } else {\n                if (pi < ci) {\n                    moves.push_back('D');\n                } else {\n                    moves.push_back('U');\n                }\n            }\n            ci = pi;\n            cj = pj;\n        }\n        reverse(moves.begin(), moves.end());\n        string path_str(moves.begin(), moves.end());\n        cout << path_str << endl;\n        cout.flush();\n\n        long long z_k;\n        cin >> z_k;\n\n        double estimated_sum = dist[ti][tj];\n        int L = moves.size();\n\n        ci = si;\n        cj = sj;\n        for (char move : moves) {\n            if (move == 'R') {\n                double w_est = h_est[ci][cj];\n                double error = (z_k - estimated_sum) / L;\n                double alpha = 1.0 / (1 + count_h[ci][cj]);\n                double new_est = w_est + alpha * error;\n                new_est = max(1000.0, min(9000.0, new_est));\n                h_est[ci][cj] = new_est;\n                count_h[ci][cj]++;\n                cj++;\n            } else if (move == 'D') {\n                double w_est = v_est[ci][cj];\n                double error = (z_k - estimated_sum) / L;\n                double alpha = 1.0 / (1 + count_v[ci][cj]);\n                double new_est = w_est + alpha * error;\n                new_est = max(1000.0, min(9000.0, new_est));\n                v_est[ci][cj] = new_est;\n                count_v[ci][cj]++;\n                ci++;\n            } else if (move == 'L') {\n                double w_est = h_est[ci][cj-1];\n                double error = (z_k - estimated_sum) / L;\n                double alpha = 1.0 / (1 + count_h[ci][cj-1]);\n                double new_est = w_est + alpha * error;\n                new_est = max(1000.0, min(9000.0, new_est));\n                h_est[ci][cj-1] = new_est;\n                count_h[ci][cj-1]++;\n                cj--;\n            } else if (move == 'U') {\n                double w_est = v_est[ci-1][cj];\n                double error = (z_k - estimated_sum) / L;\n                double alpha = 1.0 / (1 + count_v[ci-1][cj]);\n                double new_est = w_est + alpha * error;\n                new_est = max(1000.0, min(9000.0, new_est));\n                v_est[ci-1][cj] = new_est;\n                count_v[ci-1][cj]++;\n                ci--;\n            }\n        }\n    }\n\n    return 0;\n}","ahc004":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <string>\nusing namespace std;\n\nbool is_subsequence(vector<string> &mat, string s) {\n    int len = s.length();\n    int N = mat.size();\n    for (int dir = 0; dir < 2; dir++) {\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                bool match = true;\n                for (int p = 0; p < len; p++) {\n                    int ni = i, nj = j;\n                    if (dir == 0) {\n                        nj = (j + p) % N;\n                        if (nj < 0) nj += N;\n                    } else {\n                        ni = (i + p) % N;\n                        if (ni < 0) ni += N;\n                    }\n                    if (mat[ni][nj] != s[p]) {\n                        match = false;\n                        break;\n                    }\n                }\n                if (match) {\n                    return true;\n                }\n            }\n        }\n    }\n    return false;\n}\n\nint compute_coverage(vector<string> &mat, vector<string> &strings) {\n    int covered = 0;\n    for (const string& s : strings) {\n        if (is_subsequence(mat, s)) {\n            covered++;\n        }\n    }\n    return covered;\n}\n\nint main() {\n    int N, M;\n    cin >> N >> M;\n    vector<string> strings(M);\n    for (int i = 0; i < M; i++) {\n        cin >> strings[i];\n    }\n\n    vector<string> mat1(N, string(N, '.'));\n    vector<string> sorted1 = strings;\n    sort(sorted1.begin(), sorted1.end(), [](const string& a, const string& b) {\n        return a.size() > b.size();\n    });\n    int count1 = 0;\n    for (const string& s : sorted1) {\n        bool found = false;\n        int len = s.size();\n        for (int dir = 0; dir < 2; dir++) {\n            for (int i = 0; i < N; i++) {\n                for (int j = 0; j < N; j++) {\n                    bool ok = true;\n                    for (int p = 0; p < len; p++) {\n                        int ni = i, nj = j;\n                        if (dir == 0) {\n                            nj = (j + p) % N;\n                            if (nj < 0) nj += N;\n                        } else {\n                            ni = (i + p) % N;\n                            if (ni < 0) ni += N;\n                        }\n                        if (mat1[ni][nj] != '.' && mat1[ni][nj] != s[p]) {\n                            ok = false;\n                            break;\n                        }\n                    }\n                    if (ok) {\n                        for (int p = 0; p < len; p++) {\n                            int ni = i, nj = j;\n                            if (dir == 0) {\n                                nj = (j + p) % N;\n                                if (nj < 0) nj += N;\n                            } else {\n                                ni = (i + p) % N;\n                                if (ni < 0) ni += N;\n                            }\n                            if (mat1[ni][nj] == '.') {\n                                mat1[ni][nj] = s[p];\n                            }\n                        }\n                        found = true;\n                        count1++;\n                        goto next_string1;\n                    }\n                }\n            }\n        }\n        next_string1: ;\n    }\n\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (mat1[i][j] == '.') {\n                mat1[i][j] = 'A';\n            }\n        }\n    }\n    int c1 = compute_coverage(mat1, strings);\n\n    if (c1 == M) {\n        for (int i = 0; i < N; i++) {\n            cout << mat1[i] << endl;\n        }\n        return 0;\n    }\n\n    vector<string> mat2(N, string(N, '.'));\n    vector<string> sorted2 = strings;\n    sort(sorted2.begin(), sorted2.end(), [](const string& a, const string& b) {\n        return a.size() < b.size();\n    });\n    int count2 = 0;\n    for (const string& s : sorted2) {\n        bool found = false;\n        int len = s.size();\n        for (int dir = 0; dir < 2; dir++) {\n            for (int i = 0; i < N; i++) {\n                for (int j = 0; j < N; j++) {\n                    bool ok = true;\n                    for (int p = 0; p < len; p++) {\n                        int ni = i, nj = j;\n                        if (dir == 0) {\n                            nj = (j + p) % N;\n                            if (nj < 0) nj += N;\n                        } else {\n                            ni = (i + p) % N;\n                            if (ni < 0) ni += N;\n                        }\n                        if (mat2[ni][nj] != '.' && mat2[ni][nj] != s[p]) {\n                            ok = false;\n                            break;\n                        }\n                    }\n                    if (ok) {\n                        for (int p = 0; p < len; p++) {\n                            int ni = i, nj = j;\n                            if (dir == 0) {\n                                nj = (j + p) % N;\n                                if (nj < 0) nj += N;\n                            } else {\n                                ni = (i + p) % N;\n                                if (ni < 0) ni += N;\n                            }\n                            if (mat2[ni][nj] == '.') {\n                                mat2[ni][nj] = s[p];\n                            }\n                        }\n                        found = true;\n                        count2++;\n                        goto next_string2;\n                    }\n                }\n            }\n        }\n        next_string2: ;\n    }\n\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (mat2[i][j] == '.') {\n                mat2[i][j] = 'A';\n            }\n        }\n    }\n    int c2 = compute_coverage(mat2, strings);\n\n    if (c1 >= c2) {\n        for (int i = 0; i < N; i++) {\n            cout << mat1[i] << endl;\n        }\n    } else {\n        for (int i = 0; i < N; i++) {\n            cout << mat2[i] << endl;\n        }\n    }\n\n    return 0;\n}","ahc005":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int di[] = {-1, 1, 0, 0};\nconst int dj[] = {0, 0, -1, 1};\nconst char dir_char[] = {'U', 'D', 'L', 'R'};\nconst int INF = 1e9;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, si, sj;\n    cin >> N >> si >> sj;\n    vector<string> grid;\n    for (int i = 0; i < N; i++) {\n        string s;\n        cin >> s;\n        grid.push_back(s);\n    }\n\n    vector<vector<int>> h_segment_id(N, vector<int>(N, -1));\n    vector<vector<int>> v_segment_id(N, vector<int>(N, -1));\n    vector<vector<pair<int, int>>> horizontal_segments;\n    vector<vector<pair<int, int>>> vertical_segments;\n\n    for (int i = 0; i < N; i++) {\n        int j = 0;\n        while (j < N) {\n            if (grid[i][j] == '#') {\n                j++;\n                continue;\n            }\n            int j0 = j;\n            vector<pair<int, int>> seg;\n            while (j < N && grid[i][j] != '#') {\n                seg.push_back({i, j});\n                j++;\n            }\n            int seg_id = horizontal_segments.size();\n            horizontal_segments.push_back(seg);\n            for (const auto& p : seg) {\n                h_segment_id[p.first][p.second] = seg_id;\n            }\n        }\n    }\n\n    for (int j = 0; j < N; j++) {\n        int i = 0;\n        while (i < N) {\n            if (grid[i][j] == '#') {\n                i++;\n                continue;\n            }\n            int i0 = i;\n            vector<pair<int, int>> seg;\n            while (i < N && grid[i][j] != '#') {\n                seg.push_back({i, j});\n                i++;\n            }\n            int seg_id = vertical_segments.size();\n            vertical_segments.push_back(seg);\n            for (const auto& p : seg) {\n                v_segment_id[p.first][p.second] = seg_id;\n            }\n        }\n    }\n\n    int totalH = horizontal_segments.size();\n    int totalV = vertical_segments.size();\n    vector<bool> coveredH(totalH, false);\n    vector<bool> coveredV(totalV, false);\n    set<pair<int, int>> uncoveredCells;\n\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (grid[i][j] == '#') continue;\n            if (!coveredH[h_segment_id[i][j]] || !coveredV[v_segment_id[i][j]]) {\n                uncoveredCells.insert({i, j});\n            }\n        }\n    }\n\n    auto coverCell = [&](int i, int j) {\n        bool updated = false;\n        int h_id = h_segment_id[i][j];\n        int v_id = v_segment_id[i][j];\n        if (h_id != -1 && !coveredH[h_id]) {\n            coveredH[h_id] = true;\n            for (const auto& cell : horizontal_segments[h_id]) {\n                int x = cell.first, y = cell.second;\n                if (v_segment_id[x][y] != -1 && coveredV[v_segment_id[x][y]]) {\n                    if (uncoveredCells.find({x, y}) != uncoveredCells.end()) {\n                        uncoveredCells.erase({x, y});\n                    }\n                }\n            }\n            updated = true;\n        }\n        if (v_id != -1 && !coveredV[v_id]) {\n            coveredV[v_id] = true;\n            for (const auto& cell : vertical_segments[v_id]) {\n                int x = cell.first, y = cell.second;\n                if (h_segment_id[x][y] != -1 && coveredH[h_segment_id[x][y]]) {\n                    if (uncoveredCells.find({x, y}) != uncoveredCells.end()) {\n                        uncoveredCells.erase({x, y});\n                    }\n                }\n            }\n            updated = true;\n        }\n        return updated;\n    };\n\n    coverCell(si, sj);\n    int cur_i = si, cur_j = sj;\n    string route = \"\";\n\n    while (!uncoveredCells.empty()) {\n        vector<vector<int>> dist(N, vector<int>(N, INF));\n        vector<vector<pair<int, int>>> prev(N, vector<pair<int, int>>(N, {-1, -1}));\n        priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<tuple<int, int, int>>> pq;\n        dist[cur_i][cur_j] = 0;\n        pq.push({0, cur_i, cur_j});\n        pair<int, int> found = {-1, -1};\n\n        while (!pq.empty()) {\n            auto [cost, i, j] = pq.top();\n            pq.pop();\n            if (cost != dist[i][j]) continue;\n            if (uncoveredCells.find({i, j}) != uncoveredCells.end()) {\n                found = {i, j};\n                break;\n            }\n            for (int d = 0; d < 4; d++) {\n                int ni = i + di[d];\n                int nj = j + dj[d];\n                if (ni < 0 || ni >= N || nj < 0 || nj >= N || grid[ni][nj] == '#') continue;\n                int weight = grid[ni][nj] - '0';\n                int new_cost = cost + weight;\n                if (new_cost < dist[ni][nj]) {\n                    dist[ni][nj] = new_cost;\n                    prev[ni][nj] = {i, j};\n                    pq.push({new_cost, ni, nj});\n                }\n            }\n        }\n\n        if (found.first == -1) break;\n\n        vector<pair<int, int>> path_cells;\n        int ci = found.first, cj = found.second;\n        while (ci != cur_i || cj != cur_j) {\n            path_cells.push_back({ci, cj});\n            auto p = prev[ci][cj];\n            ci = p.first;\n            cj = p.second;\n        }\n        reverse(path_cells.begin(), path_cells.end());\n        int now_i = cur_i, now_j = cur_j;\n        string path_str = \"\";\n        for (const auto& cell : path_cells) {\n            int ni = cell.first, nj = cell.second;\n            if (ni == now_i + 1) path_str += 'D';\n            else if (ni == now_i - 1) path_str += 'U';\n            else if (nj == now_j + 1) path_str += 'R';\n            else if (nj == now_j - 1) path_str += 'L';\n            now_i = ni;\n            now_j = nj;\n            coverCell(ni, nj);\n        }\n        route += path_str;\n        cur_i = found.first;\n        cur_j = found.second;\n    }\n\n    vector<vector<int>> dist_return(N, vector<int>(N, INF));\n    vector<vector<pair<int, int>>> prev_return(N, vector<pair<int, int>>(N, {-1, -1}));\n    priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<tuple<int, int, int>>> pq_return;\n    dist_return[cur_i][cur_j] = 0;\n    pq_return.push({0, cur_i, cur_j});\n    while (!pq_return.empty()) {\n        auto [cost, i, j] = pq_return.top();\n        pq_return.pop();\n        if (cost != dist_return[i][j]) continue;\n        if (i == si && j == sj) break;\n        for (int d = 0; d < 4; d++) {\n            int ni = i + di[d];\n            int nj = j + dj[d];\n            if (ni < 0 || ni >= N || nj < 0 || nj >= N || grid[ni][nj] == '#') continue;\n            int weight = grid[ni][nj] - '0';\n            int new_cost = cost + weight;\n            if (new_cost < dist_return[ni][nj]) {\n                dist_return[ni][nj] = new_cost;\n                prev_return[ni][nj] = {i, j};\n                pq_return.push({new_cost, ni, nj});\n            }\n        }\n    }\n\n    vector<pair<int, int>> return_path;\n    int ci = si, cj = sj;\n    while (ci != cur_i || cj != cur_j) {\n        return_path.push_back({ci, cj});\n        auto p = prev_return[ci][cj];\n        ci = p.first;\n        cj = p.second;\n    }\n    reverse(return_path.begin(), return_path.end());\n    int now_i = cur_i, now_j = cur_j;\n    string return_str = \"\";\n    for (const auto& cell : return_path) {\n        int ni = cell.first, nj = cell.second;\n        if (ni == now_i + 1) return_str += 'D';\n        else if (ni == now_i - 1) return_str += 'U';\n        else if (nj == now_j + 1) return_str += 'R';\n        else if (nj == now_j - 1) return_str += 'L';\n        now_i = ni;\n        now_j = nj;\n    }\n    route += return_str;\n\n    cout << route << endl;\n\n    return 0;\n}","future-contest-2022-qual":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <set>\n#include <algorithm>\n#include <cmath>\n#include <iomanip>\n#include <cctype>\n#include <cstdio>\n#include <cstring>\n#include <map>\n#include <unordered_map>\n#include <unordered_set>\n#include <stack>\n#include <deque>\n#include <string>\n#include <climits>\n#include <cfloat>\n#include <bitset>\nusing namespace std;\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, K, R;\n    cin >> N >> M >> K >> R;\n\n    vector<vector<int>> task_diff(N, vector<int>(K));\n    for (int i = 0; i < N; i++) {\n        for (int k = 0; k < K; k++) {\n            cin >> task_diff[i][k];\n        }\n    }\n\n    vector<vector<int>> children(N+1);\n    vector<vector<int>> parents(N+1);\n    vector<int> in_degree(N+1, 0);\n    for (int i = 0; i < R; i++) {\n        int u, v;\n        cin >> u >> v;\n        children[u].push_back(v);\n        parents[v].push_back(u);\n        in_degree[v]++;\n    }\n\n    vector<int> num_desc(N+1, 0);\n    for (int i = 1; i <= N; i++) {\n        vector<bool> visited(N+1, false);\n        queue<int> q;\n        q.push(i);\n        visited[i] = true;\n        int count = 0;\n        while (!q.empty()) {\n            int u = q.front(); q.pop();\n            count++;\n            for (int v : children[u]) {\n                if (!visited[v]) {\n                    visited[v] = true;\n                    q.push(v);\n                }\n            }\n        }\n        num_desc[i] = count;\n    }\n\n    vector<bool> completed(N+1, false);\n    vector<bool> assigned(N+1, false);\n    vector<int> task_start_day(N+1, -1);\n    vector<int> member_status(M+1, -1);\n    vector<vector<double>> s_hat(M+1, vector<double>(K, 30.0));\n    vector<vector<int>> min_skill_lower_bound(M+1, vector<int>(K, 0));\n    set<int> available_set;\n    for (int i = 1; i <= N; i++) {\n        if (in_degree[i] == 0) {\n            available_set.insert(i);\n        }\n    }\n\n    int day = 0;\n    while (true) {\n        day++;\n        if (day > 1) {\n            int n_finished;\n            cin >> n_finished;\n            if (n_finished == -1) {\n                break;\n            }\n            vector<int> finished_members(n_finished);\n            for (int i = 0; i < n_finished; i++) {\n                cin >> finished_members[i];\n            }\n            for (int j : finished_members) {\n                int task_i = member_status[j];\n                completed[task_i] = true;\n                assigned[task_i] = false;\n                member_status[j] = -1;\n                for (int child : children[task_i]) {\n                    in_degree[child]--;\n                    if (in_degree[child] == 0 && !completed[child] && !assigned[child]) {\n                        available_set.insert(child);\n                    }\n                }\n                int start_day = task_start_day[task_i];\n                int t_days = day - start_day;\n                vector<int>& d_vec = task_diff[task_i-1];\n                if (t_days == 1) {\n                    for (int k = 0; k < K; k++) {\n                        if (d_vec[k] > min_skill_lower_bound[j][k]) {\n                            min_skill_lower_bound[j][k] = d_vec[k];\n                        }\n                        if (s_hat[j][k] < min_skill_lower_bound[j][k]) {\n                            s_hat[j][k] = min_skill_lower_bound[j][k];\n                        }\n                    }\n                } else {\n                    double w_hat = 0.0;\n                    for (int k = 0; k < K; k++) {\n                        if (d_vec[k] > s_hat[j][k]) {\n                            w_hat += (d_vec[k] - s_hat[j][k]);\n                        }\n                    }\n                    double w_obs = t_days;\n                    double e = w_obs - w_hat;\n                    double learning_rate = 0.1;\n                    for (int k = 0; k < K; k++) {\n                        if (d_vec[k] > s_hat[j][k]) {\n                            s_hat[j][k] += learning_rate * e;\n                            if (s_hat[j][k] < min_skill_lower_bound[j][k]) {\n                                s_hat[j][k] = min_skill_lower_bound[j][k];\n                            }\n                        } else {\n                            if (s_hat[j][k] < min_skill_lower_bound[j][k]) {\n                                s_hat[j][k] = min_skill_lower_bound[j][k];\n                            }\n                        }\n                    }\n                }\n            }\n        }\n\n        vector<int> free_members;\n        for (int j = 1; j <= M; j++) {\n            if (member_status[j] == -1) {\n                free_members.push_back(j);\n            }\n        }\n\n        if (free_members.empty() || available_set.empty()) {\n            cout << 0 << endl;\n            for (int j = 1; j <= M; j++) {\n                cout << \"#s \" << j;\n                for (int k = 0; k < K; k++) {\n                    cout << \" \" << (int)round(s_hat[j][k]);\n                }\n                cout << endl;\n            }\n            cout.flush();\n            continue;\n        }\n\n        vector<int> available_tasks_list(available_set.begin(), available_set.end());\n        sort(available_tasks_list.begin(), available_tasks_list.end(), [&](int a, int b) {\n            return num_desc[a] > num_desc[b];\n        });\n        if (available_tasks_list.size() > 100) {\n            available_tasks_list.resize(100);\n        }\n\n        int n_tasks = available_tasks_list.size();\n        int n_free = free_members.size();\n        vector<vector<double>> time_est(n_tasks, vector<double>(n_free, 0.0));\n        for (int i = 0; i < n_tasks; i++) {\n            int task_id = available_tasks_list[i];\n            vector<int>& d_vec = task_diff[task_id-1];\n            for (int j = 0; j < n_free; j++) {\n                int member_id = free_members[j];\n                double w = 0.0;\n                for (int k = 0; k < K; k++) {\n                    if (d_vec[k] > s_hat[member_id][k]) {\n                        w += d_vec[k] - s_hat[member_id][k];\n                    }\n                }\n                if (w < 1e-5) {\n                    w = 1.0;\n                }\n                time_est[i][j] = w;\n            }\n        }\n\n        vector<pair<double, int>> task_priority;\n        for (int i = 0; i < n_tasks; i++) {\n            double min_time = 1e18;\n            for (int j = 0; j < n_free; j++) {\n                if (time_est[i][j] < min_time) {\n                    min_time = time_est[i][j];\n                }\n            }\n            int task_id = available_tasks_list[i];\n            double priority_val = (double)num_desc[task_id] / (min_time + 1.0);\n            task_priority.push_back({priority_val, i});\n        }\n        sort(task_priority.rbegin(), task_priority.rend());\n\n        vector<bool> free_flag(n_free, true);\n        vector<pair<int, int>> assignments;\n        vector<bool> task_assigned_flag(n_tasks, false);\n        for (auto& p : task_priority) {\n            int idx_task = p.second;\n            int task_id = available_tasks_list[idx_task];\n            double best_time = 1e18;\n            int best_j_index = -1;\n            for (int j = 0; j < n_free; j++) {\n                if (free_flag[j] && time_est[idx_task][j] < best_time) {\n                    best_time = time_est[idx_task][j];\n                    best_j_index = j;\n                }\n            }\n            if (best_j_index != -1) {\n                int member_id = free_members[best_j_index];\n                assignments.push_back({member_id, task_id});\n                free_flag[best_j_index] = false;\n                task_assigned_flag[idx_task] = true;\n                assigned[task_id] = true;\n                available_set.erase(task_id);\n                member_status[member_id] = task_id;\n                task_start_day[task_id] = day;\n            }\n        }\n\n        cout << assignments.size();\n        for (auto& a : assignments) {\n            cout << \" \" << a.first << \" \" << a.second;\n        }\n        cout << endl;\n        for (int j = 1; j <= M; j++) {\n            cout << \"#s \" << j;\n            for (int k = 0; k < K; k++) {\n                cout << \" \" << (int)round(s_hat[j][k]);\n            }\n            cout << endl;\n        }\n        cout.flush();\n    }\n\n    return 0;\n}","ahc006":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <cmath>\n#include <cstdlib>\nusing namespace std;\n\nstruct Point {\n    int x, y;\n    Point(int x=0, int y=0) : x(x), y(y) {}\n};\n\nint manhattan(const Point& a, const Point& b) {\n    return abs(a.x - b.x) + abs(a.y - b.y);\n}\n\nstruct Order {\n    int a, b, c, d;\n};\n\nint main() {\n    vector<Order> orders(1000);\n    for (int i = 0; i < 1000; i++) {\n        cin >> orders[i].a >> orders[i].b >> orders[i].c >> orders[i].d;\n    }\n\n    vector<Point> tour;\n    tour.push_back(Point(400, 400));\n    tour.push_back(Point(400, 400));\n\n    vector<int> point_to_order;\n    vector<int> point_type;\n    point_to_order.push_back(-1);\n    point_to_order.push_back(-1);\n    point_type.push_back(0);\n    point_type.push_back(0);\n\n    vector<int> rest_index(1000, -1);\n    vector<int> dest_index(1000, -1);\n    vector<bool> used(1000, false);\n    vector<int> chosen_ids;\n\n    int n = 2;\n    for (int step = 0; step < 50; step++) {\n        long long best_cost = (long long)1e18;\n        int best_order = -1;\n        int best_edge = -1;\n\n        for (int idx = 0; idx < 1000; idx++) {\n            if (used[idx]) continue;\n            Order& o = orders[idx];\n            Point R = {o.a, o.b};\n            Point D = {o.c, o.d};\n            for (int edge = 0; edge < n-1; edge++) {\n                Point A = tour[edge];\n                Point B = tour[edge+1];\n                long long cost_insert = (long long)manhattan(A, R) + manhattan(R, D) + manhattan(D, B) - manhattan(A, B);\n                if (cost_insert < best_cost) {\n                    best_cost = cost_insert;\n                    best_order = idx;\n                    best_edge = edge;\n                }\n            }\n        }\n\n        if (best_order == -1) {\n            break;\n        }\n\n        Order& o = orders[best_order];\n        Point R = {o.a, o.b};\n        Point D = {o.c, o.d};\n        tour.insert(tour.begin() + best_edge + 1, R);\n        tour.insert(tour.begin() + best_edge + 2, D);\n\n        point_to_order.insert(point_to_order.begin() + best_edge + 1, best_order);\n        point_to_order.insert(point_to_order.begin() + best_edge + 2, best_order);\n        point_type.insert(point_type.begin() + best_edge + 1, 1);\n        point_type.insert(point_type.begin() + best_edge + 2, 2);\n\n        rest_index[best_order] = best_edge + 1;\n        dest_index[best_order] = best_edge + 2;\n        used[best_order] = true;\n        chosen_ids.push_back(best_order + 1);\n\n        n += 2;\n    }\n\n    n = tour.size();\n    bool improved = true;\n    while (improved) {\n        improved = false;\n        for (int i = 0; i <= n - 4; i++) {\n            int j = i + 1;\n            int k = i + 2;\n            int id1 = point_to_order[j];\n            int id2 = point_to_order[k];\n            if (id1 == id2 && id1 != -1) {\n                continue;\n            }\n\n            Point A = tour[i];\n            Point B = tour[j];\n            Point C = tour[k];\n            Point D = tour[i + 3];\n            long long original_cost = (long long)manhattan(A, B) + manhattan(B, C) + manhattan(C, D);\n            long long new_cost = (long long)manhattan(A, C) + manhattan(C, B) + manhattan(B, D);\n            long long delta = new_cost - original_cost;\n            if (delta >= 0) {\n                continue;\n            }\n\n            bool valid = true;\n            if (id1 != -1) {\n                int r1 = rest_index[id1];\n                int d1 = dest_index[id1];\n                if (j == r1) {\n                    r1 = k;\n                } else if (j == d1) {\n                    d1 = k;\n                }\n                if (r1 >= d1) {\n                    valid = false;\n                }\n            }\n            if (valid && id2 != -1) {\n                int r2 = rest_index[id2];\n                int d2 = dest_index[id2];\n                if (k == r2) {\n                    r2 = j;\n                } else if (k == d2) {\n                    d2 = j;\n                }\n                if (r2 >= d2) {\n                    valid = false;\n                }\n            }\n\n            if (valid) {\n                swap(tour[j], tour[k]);\n                swap(point_to_order[j], point_to_order[k]);\n                swap(point_type[j], point_type[k]);\n\n                if (id1 != -1) {\n                    if (rest_index[id1] == j) {\n                        rest_index[id1] = k;\n                    } else if (dest_index[id1] == j) {\n                        dest_index[id1] = k;\n                    }\n                }\n                if (id2 != -1) {\n                    if (rest_index[id2] == k) {\n                        rest_index[id2] = j;\n                    } else if (dest_index[id2] == k) {\n                        dest_index[id2] = j;\n                    }\n                }\n                improved = true;\n            }\n        }\n    }\n\n    cout << \"50\";\n    for (int i = 0; i < 50; i++) {\n        cout << \" \" << chosen_ids[i];\n    }\n    cout << endl;\n    cout << tour.size();\n    for (int i = 0; i < tour.size(); i++) {\n        cout << \" \" << tour[i].x << \" \" << tour[i].y;\n    }\n    cout << endl;\n\n    return 0;\n}","ahc007":"#include <bits/stdc++.h>\n#include <cmath>\n#include <vector>\n#include <tuple>\n#include <algorithm>\n#include <atcoder/dsu>\n\nusing namespace std;\nusing namespace atcoder;\n\nint main() {\n    const int N = 400;\n    const int M = 1995;\n    vector<double> x(N), y(N);\n    for (int i = 0; i < N; i++) {\n        cin >> x[i] >> y[i];\n    }\n\n    vector<pair<int, int>> edges_list(M);\n    for (int i = 0; i < M; i++) {\n        cin >> edges_list[i].first >> edges_list[i].second;\n    }\n\n    vector<double> d_list(M);\n    for (int i = 0; i < M; i++) {\n        int u = edges_list[i].first;\n        int v = edges_list[i].second;\n        double dx = x[u] - x[v];\n        double dy = y[u] - y[v];\n        d_list[i] = round(sqrt(dx*dx + dy*dy));\n    }\n\n    vector<tuple<double, int, int, int>> edges_for_mst;\n    for (int i = 0; i < M; i++) {\n        edges_for_mst.push_back(make_tuple(d_list[i], edges_list[i].first, edges_list[i].second, i));\n    }\n    sort(edges_for_mst.begin(), edges_for_mst.end());\n\n    dsu dsu_mst(N);\n    vector<bool> in_mst_low(M, false);\n    for (auto &e : edges_for_mst) {\n        double cost = get<0>(e);\n        int u = get<1>(e);\n        int v = get<2>(e);\n        int idx = get<3>(e);\n        if (dsu_mst.same(u, v)) continue;\n        dsu_mst.merge(u, v);\n        in_mst_low[idx] = true;\n    }\n\n    dsu dsu_online(N);\n    int comp_count = N;\n\n    for (int i = 0; i < M; i++) {\n        int l_i;\n        cin >> l_i;\n\n        int u = edges_list[i].first;\n        int v = edges_list[i].second;\n\n        if (dsu_online.same(u, v)) {\n            cout << 0 << endl;\n            cout.flush();\n        } else {\n            int remaining_edges_after_skip = M - i - 1;\n            int needed = comp_count - 1;\n\n            if (needed > remaining_edges_after_skip) {\n                cout << 1 << endl;\n                cout.flush();\n                dsu_online.merge(u, v);\n                comp_count--;\n            } else {\n                if (in_mst_low[i]) {\n                    if (l_i <= 2.5 * d_list[i]) {\n                        cout << 1 << endl;\n                        cout.flush();\n                        dsu_online.merge(u, v);\n                        comp_count--;\n                    } else {\n                        cout << 0 << endl;\n                        cout.flush();\n                    }\n                } else {\n                    if (l_i <= 2 * d_list[i]) {\n                        cout << 1 << endl;\n                        cout.flush();\n                        dsu_online.merge(u, v);\n                        comp_count--;\n                    } else {\n                        cout << 0 << endl;\n                        cout.flush();\n                    }\n                }\n            }\n        }\n    }\n\n    return 0;\n}","ahc008":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\n#include <set>\n#include <tuple>\n#include <climits>\nusing namespace std;\n\nconst int GRID_SIZE = 32;\n\nstruct Pet {\n    int x, y, type;\n};\n\nstruct Human {\n    int x, y;\n};\n\nstruct BarrierTask {\n    int wall_x, wall_y;\n    int hx, hy;\n    char action;\n};\n\nint manhattan(int x1, int y1, int x2, int y2) {\n    return abs(x1 - x2) + abs(y1 - y2);\n}\n\nint main() {\n    int N;\n    cin >> N;\n    vector<Pet> pets(N);\n    for (int i = 0; i < N; i++) {\n        cin >> pets[i].x >> pets[i].y >> pets[i].type;\n    }\n    int M;\n    cin >> M;\n    vector<Human> humans(M);\n    for (int i = 0; i < M; i++) {\n        cin >> humans[i].x >> humans[i].y;\n    }\n\n    bool wall[GRID_SIZE][GRID_SIZE] = {false};\n    for (int i = 0; i < GRID_SIZE; i++) {\n        for (int j = 0; j < GRID_SIZE; j++) {\n            if (i == 0 || i == GRID_SIZE-1 || j == 0 || j == GRID_SIZE-1) {\n                wall[i][j] = true;\n            }\n        }\n    }\n\n    vector<BarrierTask> all_tasks;\n    for (int y = 2; y <= 29; y++) {\n        BarrierTask task;\n        task.wall_x = 2; task.wall_y = y;\n        task.hx = 3; task.hy = y;\n        task.action = 'u';\n        all_tasks.push_back(task);\n    }\n    for (int y = 2; y <= 29; y++) {\n        BarrierTask task;\n        task.wall_x = 29; task.wall_y = y;\n        task.hx = 28; task.hy = y;\n        task.action = 'd';\n        all_tasks.push_back(task);\n    }\n    for (int x = 3; x <= 28; x++) {\n        BarrierTask task;\n        task.wall_x = x; task.wall_y = 2;\n        task.hx = x; task.hy = 3;\n        task.action = 'l';\n        all_tasks.push_back(task);\n    }\n    for (int x = 3; x <= 28; x++) {\n        BarrierTask task;\n        task.wall_x = x; task.wall_y = 29;\n        task.hx = x; task.hy = 28;\n        task.action = 'r';\n        all_tasks.push_back(task);\n    }\n\n    BarrierTask gap_task = {2, 15, 3, 15, 'u'};\n    for (auto it = all_tasks.begin(); it != all_tasks.end(); ) {\n        if (it->wall_x == gap_task.wall_x && it->wall_y == gap_task.wall_y) {\n            it = all_tasks.erase(it);\n            break;\n        } else {\n            it++;\n        }\n    }\n\n    vector<queue<BarrierTask>> tasks(M);\n    for (int i = 0; i < M; i++) {\n        vector<BarrierTask> tasks_for_i = all_tasks;\n        sort(tasks_for_i.begin(), tasks_for_i.end(), [&](const BarrierTask& a, const BarrierTask& b) {\n            int d1 = manhattan(a.hx, a.hy, humans[i].x, humans[i].y);\n            int d2 = manhattan(b.hx, b.hy, humans[i].x, humans[i].y);\n            return d1 < d2;\n        });\n        for (auto& task : tasks_for_i) {\n            tasks[i].push(task);\n        }\n    }\n\n    bool gap_assigned = false;\n\n    for (int turn = 0; turn < 300; turn++) {\n        set<pair<int, int>> planned_walls;\n        vector<string> actions(M, \".\");\n\n        for (int i = 0; i < M; i++) {\n            if (tasks[i].empty()) {\n                continue;\n            }\n            BarrierTask current_task = tasks[i].front();\n            if (wall[current_task.wall_x][current_task.wall_y]) {\n                tasks[i].pop();\n                continue;\n            }\n            if (humans[i].x == current_task.hx && humans[i].y == current_task.hy) {\n                bool can_build = true;\n                int dx[4] = {0,0,1,-1};\n                int dy[4] = {1,-1,0,0};\n                for (int d=0; d<4; d++) {\n                    int nx = current_task.wall_x + dx[d];\n                    int ny = current_task.wall_y + dy[d];\n                    if (nx < 1 || nx > 30 || ny < 1 || ny > 30) continue;\n                    for (const auto& pet : pets) {\n                        if (pet.x == nx && pet.y == ny) {\n                            can_build = false;\n                            break;\n                        }\n                    }\n                    if (!can_build) break;\n                }\n                if (current_task.wall_x == gap_task.wall_x && current_task.wall_y == gap_task.wall_y) {\n                    if (turn < 290) {\n                        can_build = false;\n                    }\n                }\n                if (can_build) {\n                    actions[i] = string(1, current_task.action);\n                    planned_walls.insert({current_task.wall_x, current_task.wall_y});\n                }\n            } else {\n                actions[i] = \"?\";\n            }\n        }\n\n        if (turn >= 290 && !gap_assigned && !wall[gap_task.wall_x][gap_task.wall_y]) {\n            int best_i = -1;\n            int best_dist = INT_MAX;\n            for (int i=0; i<M; i++) {\n                int d = manhattan(humans[i].x, humans[i].y, gap_task.hx, gap_task.hy);\n                if (d < best_dist) {\n                    best_dist = d;\n                    best_i = i;\n                }\n            }\n            if (best_i != -1) {\n                queue<BarrierTask> empty;\n                swap(tasks[best_i], empty);\n                tasks[best_i].push(gap_task);\n                gap_assigned = true;\n            }\n        }\n\n        for (int i = 0; i < M; i++) {\n            if (actions[i] == \"?\") {\n                if (tasks[i].empty()) {\n                    actions[i] = '.';\n                    continue;\n                }\n                BarrierTask current_task = tasks[i].front();\n                if (wall[current_task.wall_x][current_task.wall_y]) {\n                    tasks[i].pop();\n                    actions[i] = '.';\n                    continue;\n                }\n                int sx = humans[i].x;\n                int sy = humans[i].y;\n                int tx = current_task.hx;\n                int ty = current_task.hy;\n\n                vector<vector<bool>> visited(GRID_SIZE, vector<bool>(GRID_SIZE, false));\n                vector<vector<char>> first_step_dir(GRID_SIZE, vector<char>(GRID_SIZE, '.'));\n                queue<pair<int, int>> q;\n                q.push({sx, sy});\n                visited[sx][sy] = true;\n                bool found = false;\n\n                while (!q.empty()) {\n                    auto [x, y] = q.front(); q.pop();\n                    if (x == tx && y == ty) {\n                        found = true;\n                        break;\n                    }\n                    vector<tuple<int, int, char>> dirs = {{-1,0,'U'}, {1,0,'D'}, {0,-1,'L'}, {0,1,'R'}};\n                    for (auto [dx, dy, dir] : dirs) {\n                        int nx = x + dx;\n                        int ny = y + dy;\n                        if (nx < 1 || nx > 30 || ny < 1 || ny > 30) continue;\n                        if (visited[nx][ny]) continue;\n                        if (wall[nx][ny] || planned_walls.count({nx,ny})) {\n                            continue;\n                        }\n                        visited[nx][ny] = true;\n                        if (x == sx && y == sy) {\n                            first_step_dir[nx][ny] = dir;\n                        } else {\n                            first_step_dir[nx][ny] = first_step_dir[x][y];\n                        }\n                        q.push({nx, ny});\n                    }\n                }\n\n                if (found) {\n                    actions[i] = string(1, first_step_dir[tx][ty]);\n                } else {\n                    actions[i] = '.';\n                }\n            }\n        }\n\n        string output_line;\n        for (int i=0; i<M; i++) {\n            output_line += actions[i];\n        }\n        cout << output_line << endl;\n        cout.flush();\n\n        for (int i=0; i<M; i++) {\n            if (actions[i] == \"u\" || actions[i] == \"d\" || actions[i] == \"l\" || actions[i] == \"r\") {\n                BarrierTask current_task = tasks[i].front();\n                wall[current_task.wall_x][current_task.wall_y] = true;\n                tasks[i].pop();\n            } else if (actions[i] == \"U\") {\n                humans[i].x--;\n            } else if (actions[i] == \"D\") {\n                humans[i].x++;\n            } else if (actions[i] == \"L\") {\n                humans[i].y--;\n            } else if (actions[i] == \"R\") {\n                humans[i].y++;\n            }\n        }\n\n        for (int i=0; i<N; i++) {\n            string move;\n            cin >> move;\n            for (char c : move) {\n                if (c == 'U') pets[i].x--;\n                else if (c == 'D') pets[i].x++;\n                else if (c == 'L') pets[i].y--;\n                else if (c == 'R') pets[i].y++;\n            }\n        }\n    }\n\n    return 0;\n}","ahc009":"#include <iostream>\n#include <vector>\n#include <cctype>\n#include <algorithm>\n#include <iomanip>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int s_i, s_j, t_i, t_j;\n    double p;\n    cin >> s_i >> s_j >> t_i >> t_j >> p;\n\n    vector<string> h(20);\n    for (int i = 0; i < 20; i++) {\n        cin >> h[i];\n    }\n\n    vector<string> v(19);\n    for (int i = 0; i < 19; i++) {\n        cin >> v[i];\n    }\n\n    vector<vector<vector<char>>> best_move(200, vector<vector<char>>(20, vector<char>(20, 'U')));\n    vector<vector<double>> dp_next(20, vector<double>(20, 0.0));\n\n    for (int i = 0; i < 20; i++) {\n        for (int j = 0; j < 20; j++) {\n            if (i == t_i && j == t_j) {\n                dp_next[i][j] = 201.0;\n            } else {\n                dp_next[i][j] = 0.0;\n            }\n        }\n    }\n\n    for (int tt = 199; tt >= 0; tt--) {\n        vector<vector<double>> dp_curr(20, vector<double>(20, 0.0));\n        for (int i = 0; i < 20; i++) {\n            for (int j = 0; j < 20; j++) {\n                if (i == t_i && j == t_j) {\n                    continue;\n                }\n                double best_val = -1e18;\n                char best_char = 'U';\n                for (char move : {'U','D','L','R'}) {\n                    int ni = i, nj = j;\n                    if (move == 'U') {\n                        if (i > 0 && v[i-1][j] == '0') {\n                            ni = i - 1;\n                        }\n                    } else if (move == 'D') {\n                        if (i < 19 && v[i][j] == '0') {\n                            ni = i + 1;\n                        }\n                    } else if (move == 'L') {\n                        if (j > 0 && h[i][j-1] == '0') {\n                            nj = j - 1;\n                        }\n                    } else if (move == 'R') {\n                        if (j < 19 && h[i][j] == '0') {\n                            nj = j + 1;\n                        }\n                    }\n\n                    double val;\n                    if (ni == t_i && nj == t_j) {\n                        val = p * dp_next[i][j] + (1-p) * (401 - (tt+1));\n                    } else {\n                        val = p * dp_next[i][j] + (1-p) * dp_next[ni][nj];\n                    }\n\n                    if (val > best_val) {\n                        best_val = val;\n                        best_char = move;\n                    }\n                }\n                dp_curr[i][j] = best_val;\n                best_move[tt][i][j] = best_char;\n            }\n        }\n        dp_next = dp_curr;\n    }\n\n    int cur_i = s_i, cur_j = s_j;\n    string route = \"\";\n    for (int t = 0; t < 200; t++) {\n        if (cur_i == t_i && cur_j == t_j) {\n            int rem = 200 - t;\n            route += string(rem, 'U');\n            break;\n        }\n        char move = best_move[t][cur_i][cur_j];\n        route += move;\n\n        if (move == 'U') {\n            if (cur_i > 0 && v[cur_i-1][cur_j] == '0') {\n                cur_i--;\n            }\n        } else if (move == 'D') {\n            if (cur_i < 19 && v[cur_i][cur_j] == '0') {\n                cur_i++;\n            }\n        } else if (move == 'L') {\n            if (cur_j > 0 && h[cur_i][cur_j-1] == '0') {\n                cur_j--;\n            }\n        } else if (move == 'R') {\n            if (cur_j < 19 && h[cur_i][cur_j] == '0') {\n                cur_j++;\n            }\n        }\n    }\n\n    cout << route << endl;\n\n    return 0;\n}","ahc010":"#include <iostream>\n#include <vector>\n#include <cstdlib>\n#include <cmath>\n#include <algorithm>\n#include <random>\n#include <chrono>\n\nusing namespace std;\n\nconst vector<int> di = {0, -1, 0, 1};\nconst vector<int> dj = {-1, 0, 1, 0};\n\nconst vector<vector<int>> to_table = {\n    {1, 0, -1, -1},\n    {3, -1, -1, 0},\n    {-1, -1, 3, 2},\n    {-1, 2, 1, -1},\n    {1, 0, 3, 2},\n    {3, 2, 1, 0},\n    {2, -1, 0, -1},\n    {-1, 3, -1, 1}\n};\n\nlong long compute_score(const vector<vector<int>>& grid, const vector<vector<int>>& rot, vector<vector<vector<bool>>>& visited) {\n    int n = 30;\n    vector<vector<int>> eff(n, vector<int>(n));\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            int t = grid[i][j];\n            int r = rot[i][j];\n            if (t < 4) {\n                eff[i][j] = (t + r) % 4;\n            } else if (t < 6) {\n                if (r % 2 == 0) {\n                    eff[i][j] = t;\n                } else {\n                    eff[i][j] = t ^ 1;\n                }\n            } else {\n                if (r % 2 == 0) {\n                    eff[i][j] = t;\n                } else {\n                    eff[i][j] = t ^ 1;\n                }\n            }\n        }\n    }\n\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            for (int d = 0; d < 4; d++) {\n                visited[i][j][d] = false;\n            }\n        }\n    }\n\n    vector<long long> loops;\n\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            for (int d = 0; d < 4; d++) {\n                if (visited[i][j][d]) continue;\n\n                int ci = i, cj = j, cd = d;\n                visited[ci][cj][cd] = true;\n                long long steps = 0;\n                bool found_loop = false;\n\n                while (true) {\n                    int eff_tile = eff[ci][cj];\n                    int d_out = to_table[eff_tile][cd];\n                    if (d_out == -1) {\n                        break;\n                    }\n                    int ni = ci + di[d_out];\n                    int nj = cj + dj[d_out];\n                    if (ni < 0 || ni >= n || nj < 0 || nj >= n) {\n                        break;\n                    }\n                    int nd = (d_out + 2) % 4;\n\n                    if (ni == i && nj == j && nd == d) {\n                        steps++;\n                        loops.push_back(steps);\n                        found_loop = true;\n                        break;\n                    }\n\n                    if (visited[ni][nj][nd]) {\n                        break;\n                    }\n\n                    visited[ni][nj][nd] = true;\n                    steps++;\n                    ci = ni;\n                    cj = nj;\n                    cd = nd;\n                }\n            }\n        }\n    }\n\n    if (loops.size() < 2) {\n        return 0;\n    }\n\n    sort(loops.rbegin(), loops.rend());\n    return (long long)loops[0] * loops[1];\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int num_test_cases = 100;\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n\n    vector<vector<vector<bool>>> visited(30, vector<vector<bool>>(30, vector<bool>(4, false)));\n\n    for (int tc = 0; tc < num_test_cases; tc++) {\n        vector<vector<int>> grid(30, vector<int>(30));\n        for (int i = 0; i < 30; i++) {\n            string s;\n            cin >> s;\n            for (int j = 0; j < 30; j++) {\n                grid[i][j] = s[j] - '0';\n            }\n        }\n\n        vector<vector<int>> rot(30, vector<int>(30));\n        for (int i = 0; i < 30; i++) {\n            for (int j = 0; j < 30; j++) {\n                rot[i][j] = rng() % 4;\n            }\n        }\n\n        long long current_score = compute_score(grid, rot, visited);\n\n        double T0 = 10000.0;\n        int max_iter = 1000;\n\n        uniform_int_distribution<int> rand_tile(0, 29);\n        uniform_int_distribution<int> rand_rot_delta(1, 3);\n        uniform_real_distribution<double> rand_double(0.0, 1.0);\n\n        for (int iter = 0; iter < max_iter; iter++) {\n            int i = rand_tile(rng);\n            int j = rand_tile(rng);\n            int old_rot_val = rot[i][j];\n            int delta = rand_rot_delta(rng);\n            int new_rot_val = (old_rot_val + delta) % 4;\n\n            rot[i][j] = new_rot_val;\n            long long new_score = compute_score(grid, rot, visited);\n\n            double T = T0 * (1.0 - (double)iter / max_iter);\n            if (T < 1e-9) T = 1e-9;\n\n            if (new_score > current_score) {\n                current_score = new_score;\n            } else {\n                double diff = new_score - current_score;\n                if (diff >= 0) {\n                    current_score = new_score;\n                } else {\n                    double prob = exp(diff / T);\n                    if (rand_double(rng) < prob) {\n                        current_score = new_score;\n                    } else {\n                        rot[i][j] = old_rot_val;\n                    }\n                }\n            }\n        }\n\n        for (int i = 0; i < 30; i++) {\n            for (int j = 0; j < 30; j++) {\n                cout << rot[i][j];\n            }\n        }\n        cout << '\\n';\n    }\n\n    return 0;\n}","ahc011":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <set>\n#include <map>\n#include <algorithm>\n#include <cctype>\nusing namespace std;\n\npair<int, int> evaluate(vector<vector<int>>& board, int N) {\n    vector<pair<pair<int,int>, pair<int,int>>> edge_list;\n    int total_edges = 0;\n\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N-1; j++) {\n            if (board[i][j] != 0 && board[i][j+1] != 0) {\n                if ((board[i][j] & 4) && (board[i][j+1] & 1)) {\n                    edge_list.push_back({{i, j}, {i, j+1}});\n                    total_edges++;\n                }\n            }\n        }\n    }\n\n    for (int i = 0; i < N-1; i++) {\n        for (int j = 0; j < N; j++) {\n            if (board[i][j] != 0 && board[i+1][j] != 0) {\n                if ((board[i][j] & 8) && (board[i+1][j] & 2)) {\n                    edge_list.push_back({{i, j}, {i+1, j}});\n                    total_edges++;\n                }\n            }\n        }\n    }\n\n    map<pair<int,int>, vector<pair<int,int>>> adj;\n    for (auto &e : edge_list) {\n        auto u = e.first;\n        auto v = e.second;\n        adj[u].push_back(v);\n        adj[v].push_back(u);\n    }\n\n    vector<vector<bool>> visited(N, vector<bool>(N, false));\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (board[i][j] == 0) {\n                visited[i][j] = true;\n            }\n        }\n    }\n\n    int max_tree_size = 0;\n\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (!visited[i][j]) {\n                vector<pair<int,int>> component;\n                queue<pair<int,int>> q;\n                q.push({i, j});\n                visited[i][j] = true;\n\n                while (!q.empty()) {\n                    auto node = q.front();\n                    q.pop();\n                    component.push_back(node);\n\n                    if (adj.find(node) != adj.end()) {\n                        for (auto &nei : adj[node]) {\n                            if (!visited[nei.first][nei.second]) {\n                                visited[nei.first][nei.second] = true;\n                                q.push(nei);\n                            }\n                        }\n                    }\n                }\n\n                set<pair<int,int>> comp_set(component.begin(), component.end());\n                int count_edges = 0;\n                for (auto &node : component) {\n                    if (adj.find(node) != adj.end()) {\n                        for (auto &nei : adj[node]) {\n                            if (comp_set.find(nei) != comp_set.end()) {\n                                count_edges++;\n                            }\n                        }\n                    }\n                }\n                int E_component = count_edges / 2;\n                int V = component.size();\n                if (E_component == V - 1) {\n                    if (V > max_tree_size) {\n                        max_tree_size = V;\n                    }\n                }\n            }\n        }\n    }\n\n    if (max_tree_size == 0) {\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                if (board[i][j] != 0) {\n                    max_tree_size = 1;\n                }\n            }\n        }\n    }\n\n    return {max_tree_size, total_edges};\n}\n\nint main() {\n    int N, T_val;\n    cin >> N >> T_val;\n    vector<string> input_grid(N);\n    for (int i = 0; i < N; i++) {\n        cin >> input_grid[i];\n    }\n\n    vector<vector<int>> board(N, vector<int>(N));\n    int empty_r = -1, empty_c = -1;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            char c = input_grid[i][j];\n            if (c >= '0' && c <= '9') {\n                board[i][j] = c - '0';\n            } else if (c >= 'a' && c <= 'f') {\n                board[i][j] = 10 + (c - 'a');\n            } else {\n                board[i][j] = 0;\n            }\n            if (board[i][j] == 0) {\n                empty_r = i;\n                empty_c = j;\n            }\n        }\n    }\n\n    string sequence = \"\";\n    char last_move = ' ';\n\n    map<char, pair<int,int>> dir;\n    dir['U'] = {-1, 0};\n    dir['D'] = {1, 0};\n    dir['L'] = {0, -1};\n    dir['R'] = {0, 1};\n\n    vector<char> moves = {'U','D','L','R'};\n    for (int step = 0; step < T_val; step++) {\n        char best_move = ' ';\n        int best_S = -1;\n        int best_C = -1;\n        bool found = false;\n\n        for (char move : moves) {\n            int dr = dir[move].first;\n            int dc = dir[move].second;\n            int new_r = empty_r + dr;\n            int new_c = empty_c + dc;\n\n            if (new_r < 0 || new_r >= N || new_c < 0 || new_c >= N) {\n                continue;\n            }\n\n            swap(board[empty_r][empty_c], board[new_r][new_c]);\n            auto [S, C] = evaluate(board, N);\n            swap(board[empty_r][empty_c], board[new_r][new_c]);\n\n            if (S > best_S || (S == best_S && C > best_C)) {\n                best_S = S;\n                best_C = C;\n                best_move = move;\n                found = true;\n            }\n        }\n\n        if (!found) {\n            break;\n        }\n\n        int dr = dir[best_move].first;\n        int dc = dir[best_move].second;\n        int new_r = empty_r + dr;\n        int new_c = empty_c + dc;\n        swap(board[empty_r][empty_c], board[new_r][new_c]);\n        empty_r = new_r;\n        empty_c = new_c;\n        sequence += best_move;\n        last_move = best_move;\n\n        if (best_S == N*N-1) {\n            break;\n        }\n    }\n\n    cout << sequence << endl;\n\n    return 0;\n}","ahc012":"#include <bits/stdc++.h>\n#include <random>\nusing namespace std;\ntypedef long long ll;\ntypedef __int128 i128;\n\nconst int B = 20000;\nconst ll R2 = 100000000;\n\nstruct Point {\n    ll x, y;\n    Point() {}\n    Point(ll x, ll y) : x(x), y(y) {}\n    bool operator==(const Point& other) const {\n        return x == other.x && y == other.y;\n    }\n};\n\nstruct Line {\n    Point p, q;\n    ll a, b, c;\n    Line(Point p1, Point p2) : p(p1), q(p2) {\n        a = p.y - q.y;\n        b = q.x - p.x;\n        c = p.x * q.y - q.x * p.y;\n    }\n};\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    random_device rd;\n    mt19937 gen(rd());\n    uniform_int_distribution<int> distrib(-B, B);\n\n    const int CANDIDATE_SIZE = 1000;\n    const int num_test_cases = 100;\n    for (int tc = 0; tc < num_test_cases; tc++) {\n        int N, K;\n        cin >> N >> K;\n        vector<int> a(11, 0);\n        for (int i = 1; i <= 10; i++) {\n            cin >> a[i];\n        }\n        vector<Point> points;\n        for (int i = 0; i < N; i++) {\n            ll x, y;\n            cin >> x >> y;\n            points.push_back(Point(x, y));\n        }\n\n        vector<Line> candidate_lines;\n        vector<bool> used;\n        for (int i = 0; i < CANDIDATE_SIZE; ) {\n            bool found = false;\n            for (int t = 0; t < 100; t++) {\n                Point p(distrib(gen), distrib(gen));\n                Point q(distrib(gen), distrib(gen));\n                if (p == q) continue;\n                Line L(p, q);\n                i128 a_val = L.a;\n                i128 b_val = L.b;\n                i128 c_val = L.c;\n                i128 lhs = c_val * c_val;\n                i128 rhs = R2 * (a_val * a_val + b_val * b_val);\n                if (lhs > rhs) continue;\n\n                bool valid = true;\n                for (const Point& s : points) {\n                    ll D = L.a * s.x + L.b * s.y + L.c;\n                    if (D == 0) {\n                        valid = false;\n                        break;\n                    }\n                }\n                if (valid) {\n                    candidate_lines.push_back(L);\n                    found = true;\n                    i++;\n                    break;\n                }\n            }\n            if (!found) {\n                break;\n            }\n        }\n        used.assign(candidate_lines.size(), false);\n\n        vector<vector<int>> current_groups;\n        if (N > 0) {\n            current_groups.push_back(vector<int>());\n            for (int i = 0; i < N; i++) {\n                current_groups[0].push_back(i);\n            }\n        }\n\n        vector<int> current_b_d(11, 0);\n        if (N >= 1 && N <= 10) {\n            current_b_d[N] = 1;\n        }\n        int current_score = 0;\n        for (int d = 1; d <= 10; d++) {\n            current_score += min(a[d], current_b_d[d]);\n        }\n\n        vector<Line> chosen_lines;\n\n        for (int step = 0; step < K; step++) {\n            int best_next_score = current_score;\n            int best_candidate = -1;\n\n            if (candidate_lines.empty()) break;\n\n            for (int idx = 0; idx < candidate_lines.size(); idx++) {\n                if (used[idx]) continue;\n                const Line& L = candidate_lines[idx];\n                vector<int> new_b_d(11, 0);\n                for (const vector<int>& group : current_groups) {\n                    int cnt0 = 0, cnt1 = 0;\n                    for (int sid : group) {\n                        const Point& s = points[sid];\n                        ll D = L.a * s.x + L.b * s.y + L.c;\n                        if (D > 0) cnt1++;\n                        else cnt0++;\n                    }\n                    if (cnt0 > 0 && cnt0 <= 10) new_b_d[cnt0]++;\n                    if (cnt1 > 0 && cnt1 <= 10) new_b_d[cnt1]++;\n                }\n                int score = 0;\n                for (int d = 1; d <= 10; d++) {\n                    score += min(a[d], new_b_d[d]);\n                }\n                if (score > best_next_score) {\n                    best_next_score = score;\n                    best_candidate = idx;\n                }\n            }\n\n            if (best_candidate == -1) break;\n\n            used[best_candidate] = true;\n            const Line& L = candidate_lines[best_candidate];\n            chosen_lines.push_back(L);\n\n            vector<vector<int>> new_groups;\n            for (const vector<int>& group : current_groups) {\n                vector<int> group0, group1;\n                for (int sid : group) {\n                    const Point& s = points[sid];\n                    ll D = L.a * s.x + L.b * s.y + L.c;\n                    if (D > 0) group1.push_back(sid);\n                    else group0.push_back(sid);\n                }\n                if (!group0.empty()) new_groups.push_back(group0);\n                if (!group1.empty()) new_groups.push_back(group1);\n            }\n            current_groups = move(new_groups);\n\n            current_b_d.assign(11, 0);\n            for (const auto& g : current_groups) {\n                int s = g.size();\n                if (s >= 1 && s <= 10) current_b_d[s]++;\n            }\n            current_score = best_next_score;\n        }\n\n        cout << chosen_lines.size() << endl;\n        for (const Line& L : chosen_lines) {\n            cout << L.p.x << \" \" << L.p.y << \" \" << L.q.x << \" \" << L.q.y << endl;\n        }\n    }\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    cin >> N >> M;\n    vector<vector<bool>> dot(N, vector<bool>(N, false));\n    vector<set<int>> rows(N);\n    vector<set<int>> cols(N);\n\n    vector<vector<int>> next_in_row(N, vector<int>(N, N));\n    vector<vector<int>> prev_in_row(N, vector<int>(N, -1));\n    vector<vector<int>> next_in_col(N, vector<int>(N, N));\n    vector<vector<int>> prev_in_col(N, vector<int>(N, -1));\n\n    vector<vector<bool>> hor(N, vector<bool>(N, false));\n    vector<vector<bool>> ver(N, vector<bool>(N, false));\n\n    for (int i = 0; i < M; i++) {\n        int x, y;\n        cin >> x >> y;\n        dot[x][y] = true;\n        rows[y].insert(x);\n        cols[x].insert(y);\n    }\n\n    for (int y = 0; y < N; y++) {\n        int last = -1;\n        for (int x = 0; x < N; x++) {\n            if (dot[x][y]) {\n                last = x;\n            }\n            prev_in_row[y][x] = last;\n        }\n        last = N;\n        for (int x = N-1; x >= 0; x--) {\n            if (dot[x][y]) {\n                last = x;\n            }\n            next_in_row[y][x] = last;\n        }\n    }\n\n    for (int x = 0; x < N; x++) {\n        int last = -1;\n        for (int y = 0; y < N; y++) {\n            if (dot[x][y]) {\n                last = y;\n            }\n            prev_in_col[x][y] = last;\n        }\n        last = N;\n        for (int y = N-1; y >= 0; y--) {\n            if (dot[x][y]) {\n                last = y;\n            }\n            next_in_col[x][y] = last;\n        }\n    }\n\n    int c = (N - 1) / 2;\n    priority_queue<tuple<int, int, int>> pq;\n    for (int x = 0; x < N; x++) {\n        for (int y = 0; y < N; y++) {\n            if (!dot[x][y]) {\n                int weight = (x - c) * (x - c) + (y - c) * (y - c) + 1;\n                pq.push(make_tuple(weight, x, y));\n            }\n        }\n    }\n\n    vector<array<int, 8>> operations;\n    auto start_time = chrono::high_resolution_clock::now();\n\n    while (!pq.empty()) {\n        auto now = chrono::high_resolution_clock::now();\n        auto duration = chrono::duration_cast<chrono::milliseconds>(now - start_time);\n        if (duration.count() > 4500) {\n            break;\n        }\n\n        auto top = pq.top();\n        int w = get<0>(top);\n        int x1 = get<1>(top);\n        int y1 = get<2>(top);\n        pq.pop();\n        if (dot[x1][y1]) {\n            continue;\n        }\n\n        bool found = false;\n        const auto& col_x1 = cols[x1];\n        for (int y2 : col_x1) {\n            if (y2 == y1) continue;\n            int y_min = min(y1, y2);\n            int y_max = max(y1, y2);\n\n            if (next_in_col[x1][y_min + 1] < y_max) {\n                continue;\n            }\n\n            set<int> common;\n            const set<int>& row_y1 = rows[y1];\n            const set<int>& row_y2 = rows[y2];\n            if (row_y1.size() < row_y2.size()) {\n                for (int x : row_y1) {\n                    if (row_y2.find(x) != row_y2.end()) {\n                        common.insert(x);\n                    }\n                }\n            } else {\n                for (int x : row_y2) {\n                    if (row_y1.find(x) != row_y1.end()) {\n                        common.insert(x);\n                    }\n                }\n            }\n\n            for (int x2 : common) {\n                if (x2 == x1) continue;\n                int x_min = min(x1, x2);\n                int x_max = max(x1, x2);\n\n                if (next_in_row[y1][x_min + 1] < x_max) {\n                    continue;\n                }\n                if (next_in_row[y2][x_min + 1] < x_max) {\n                    continue;\n                }\n                if (next_in_col[x2][y_min + 1] < y_max) {\n                    continue;\n                }\n\n                bool edgeValid = true;\n                for (int y = y_min; y < y_max; y++) {\n                    if (y < N - 1 && ver[x1][y]) {\n                        edgeValid = false;\n                        break;\n                    }\n                }\n                if (!edgeValid) continue;\n\n                for (int y = y_min; y < y_max; y++) {\n                    if (y < N - 1 && ver[x2][y]) {\n                        edgeValid = false;\n                        break;\n                    }\n                }\n                if (!edgeValid) continue;\n\n                for (int x = x_min; x < x_max; x++) {\n                    if (x < N - 1 && hor[x][y_min]) {\n                        edgeValid = false;\n                        break;\n                    }\n                }\n                if (!edgeValid) continue;\n\n                for (int x = x_min; x < x_max; x++) {\n                    if (x < N - 1 && hor[x][y_max]) {\n                        edgeValid = false;\n                        break;\n                    }\n                }\n                if (!edgeValid) continue;\n\n                operations.push_back({x1, y1, x1, y2, x2, y2, x2, y1});\n                dot[x1][y1] = true;\n                rows[y1].insert(x1);\n                cols[x1].insert(y1);\n\n                int last_val = -1;\n                for (int x = 0; x < N; x++) {\n                    if (dot[x][y1]) {\n                        last_val = x;\n                    }\n                    prev_in_row[y1][x] = last_val;\n                }\n                last_val = N;\n                for (int x = N - 1; x >= 0; x--) {\n                    if (dot[x][y1]) {\n                        last_val = x;\n                    }\n                    next_in_row[y1][x] = last_val;\n                }\n\n                last_val = -1;\n                for (int y = 0; y < N; y++) {\n                    if (dot[x1][y]) {\n                        last_val = y;\n                    }\n                    prev_in_col[x1][y] = last_val;\n                }\n                last_val = N;\n                for (int y = N - 1; y >= 0; y--) {\n                    if (dot[x1][y]) {\n                        last_val = y;\n                    }\n                    next_in_col[x1][y] = last_val;\n                }\n\n                for (int y = y_min; y < y_max; y++) {\n                    if (y < N - 1) {\n                        ver[x1][y] = true;\n                    }\n                }\n                for (int y = y_min; y < y_max; y++) {\n                    if (y < N - 1) {\n                        ver[x2][y] = true;\n                    }\n                }\n                for (int x = x_min; x < x_max; x++) {\n                    if (x < N - 1) {\n                        hor[x][y_min] = true;\n                    }\n                }\n                for (int x = x_min; x < x_max; x++) {\n                    if (x < N - 1) {\n                        hor[x][y_max] = true;\n                    }\n                }\n\n                found = true;\n                break;\n            }\n            if (found) break;\n        }\n    }\n\n    cout << operations.size() << endl;\n    for (auto& op : operations) {\n        for (int i = 0; i < 8; i++) {\n            if (i > 0) cout << \" \";\n            cout << op[i];\n        }\n        cout << endl;\n    }\n\n    return 0;\n}","ahc015":"#include <iostream>\n#include <vector>\n#include <climits>\nusing namespace std;\n\nvector<vector<int>> simulate_tilt(vector<vector<int>> grid, char dir) {\n    vector<vector<int>> new_grid(10, vector<int>(10, 0));\n    if (dir == 'L' || dir == 'R') {\n        for (int i = 0; i < 10; i++) {\n            vector<int> non_empty;\n            for (int j = 0; j < 10; j++) {\n                if (grid[i][j] != 0) {\n                    non_empty.push_back(grid[i][j]);\n                }\n            }\n            if (dir == 'L') {\n                for (int j = 0; j < non_empty.size(); j++) {\n                    new_grid[i][j] = non_empty[j];\n                }\n            } else {\n                int start = 10 - non_empty.size();\n                for (int j = 0; j < non_empty.size(); j++) {\n                    new_grid[i][start + j] = non_empty[j];\n                }\n            }\n        }\n    } else {\n        for (int j = 0; j < 10; j++) {\n            vector<int> non_empty;\n            for (int i = 0; i < 10; i++) {\n                if (grid[i][j] != 0) {\n                    non_empty.push_back(grid[i][j]);\n                }\n            }\n            if (dir == 'F') {\n                for (int i = 0; i < non_empty.size(); i++) {\n                    new_grid[i][j] = non_empty[i];\n                }\n            } else {\n                int start = 10 - non_empty.size();\n                for (int i = 0; i < non_empty.size(); i++) {\n                    new_grid[start + i][j] = non_empty[i];\n                }\n            }\n        }\n    }\n    return new_grid;\n}\n\nint compute_penalty(vector<vector<int>>& grid) {\n    int penalty = 0;\n    for (int i = 0; i < 10; i++) {\n        for (int j = 0; j < 10; j++) {\n            if (grid[i][j] == 1) {\n                penalty += i + j;\n            } else if (grid[i][j] == 2) {\n                penalty += i + (9 - j);\n            } else if (grid[i][j] == 3) {\n                penalty += (9 - i) + j;\n            }\n        }\n    }\n    return penalty;\n}\n\nint main() {\n    vector<int> flavors;\n    for (int i = 0; i < 100; i++) {\n        int f;\n        cin >> f;\n        flavors.push_back(f);\n    }\n\n    vector<vector<int>> grid(10, vector<int>(10, 0));\n\n    for (int t = 0; t < 100; t++) {\n        int p;\n        cin >> p;\n\n        vector<pair<int, int>> emptyCells;\n        for (int i = 0; i < 10; i++) {\n            for (int j = 0; j < 10; j++) {\n                if (grid[i][j] == 0) {\n                    emptyCells.push_back(make_pair(i, j));\n                }\n            }\n        }\n        int r = emptyCells[p-1].first;\n        int c = emptyCells[p-1].second;\n        grid[r][c] = flavors[t];\n\n        char best_dir = 'L';\n        int best_penalty = INT_MAX;\n\n        vector<char> directions = {'L', 'R', 'F', 'B'};\n        for (char dir : directions) {\n            vector<vector<int>> new_grid = simulate_tilt(grid, dir);\n            int penalty_val = compute_penalty(new_grid);\n            if (penalty_val < best_penalty) {\n                best_penalty = penalty_val;\n                best_dir = dir;\n            }\n        }\n\n        cout << best_dir << endl;\n        cout.flush();\n\n        grid = simulate_tilt(grid, best_dir);\n    }\n\n    return 0;\n}","ahc016":"#include <iostream>\n#include <vector>\n#include <cstdlib>\n#include <algorithm>\n#include <cmath>\nusing namespace std;\n\nint main() {\n    int M;\n    double eps;\n    cin >> M >> eps;\n\n    srand(42);\n    int n = 100;\n    int L = n * (n - 1) / 2;\n\n    cout << n << endl;\n\n    vector<vector<int>> stored_degrees;\n\n    for (int i = 0; i < M; i++) {\n        double p_i = 0.1 + 0.8 * (static_cast<double>(i) / (M - 1));\n        string s = \"\";\n        for (int j = 0; j < L; j++) {\n            double r = static_cast<double>(rand()) / RAND_MAX;\n            if (r < p_i) {\n                s += '1';\n            } else {\n                s += '0';\n            }\n        }\n        cout << s << endl;\n\n        vector<int> deg(n, 0);\n        int index = 0;\n        for (int u = 0; u < n; u++) {\n            for (int v = u + 1; v < n; v++) {\n                if (s[index] == '1') {\n                    deg[u]++;\n                    deg[v]++;\n                }\n                index++;\n            }\n        }\n        sort(deg.begin(), deg.end());\n        stored_degrees.push_back(deg);\n    }\n    cout.flush();\n\n    for (int q = 0; q < 100; q++) {\n        string H;\n        cin >> H;\n\n        vector<int> degH(n, 0);\n        int index = 0;\n        for (int u = 0; u < n; u++) {\n            for (int v = u + 1; v < n; v++) {\n                if (H[index] == '1') {\n                    degH[u]++;\n                    degH[v]++;\n                }\n                index++;\n            }\n        }\n        sort(degH.begin(), degH.end());\n\n        double minDist = 1e18;\n        int best_i = 0;\n        for (int i = 0; i < M; i++) {\n            vector<double> expDeg;\n            for (int j = 0; j < n; j++) {\n                double ed = stored_degrees[i][j] * (1 - 2 * eps) + eps * (n - 1);\n                expDeg.push_back(ed);\n            }\n            double dist = 0.0;\n            for (int j = 0; j < n; j++) {\n                double diff = expDeg[j] - degH[j];\n                dist += diff * diff;\n            }\n            if (dist < minDist) {\n                minDist = dist;\n                best_i = i;\n            }\n        }\n\n        cout << best_i << endl;\n        cout.flush();\n    }\n\n    return 0;\n}","ahc017":"#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, D, K;\n    cin >> N >> M >> D >> K;\n    vector<tuple<int, int, int>> edges;\n    vector<vector<tuple<int, int, int>>> adj(N);\n    map<pair<int, int>, int> edgeMap;\n\n    for (int i = 0; i < M; i++) {\n        int u, v, w;\n        cin >> u >> v >> w;\n        u--; v--;\n        edges.push_back({u, v, w});\n        adj[u].push_back({v, w, i});\n        adj[v].push_back({u, w, i});\n        int a = min(u, v), b = max(u, v);\n        edgeMap[{a, b}] = i;\n    }\n    for (int i = 0; i < N; i++) {\n        int x, y;\n        cin >> x >> y;\n    }\n\n    vector<double> bc(M, 0.0);\n\n    for (int s = 0; s < N; s++) {\n        vector<ll> dist(N, 1e18);\n        vector<int> numPaths(N, 0);\n        vector<vector<int>> pred(N);\n        vector<int> order;\n\n        dist[s] = 0;\n        numPaths[s] = 1;\n        priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;\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            order.push_back(u);\n            for (auto [v, w, eid] : adj[u]) {\n                ll nd = d + w;\n                if (nd < dist[v]) {\n                    dist[v] = nd;\n                    numPaths[v] = numPaths[u];\n                    pred[v] = {u};\n                    pq.push({nd, v});\n                } else if (nd == dist[v]) {\n                    numPaths[v] += numPaths[u];\n                    pred[v].push_back(u);\n                }\n            }\n        }\n\n        vector<double> dep(N, 0.0);\n        for (int i = order.size() - 1; i >= 0; i--) {\n            int w = order[i];\n            for (int u : pred[w]) {\n                if (numPaths[w] == 0) continue;\n                double frac = static_cast<double>(numPaths[u]) / numPaths[w] * (1.0 + dep[w]);\n                int a = min(u, w);\n                int b = max(u, w);\n                auto it = edgeMap.find({a, b});\n                if (it == edgeMap.end()) continue;\n                int eid = it->second;\n                bc[eid] += frac;\n                dep[u] += frac;\n            }\n        }\n    }\n\n    vector<int> assignment(M, -1);\n    vector<int> day_count(D, 0);\n    vector<double> day_sum(D, 0.0);\n\n    vector<int> idx_edges(M);\n    iota(idx_edges.begin(), idx_edges.end(), 0);\n    sort(idx_edges.begin(), idx_edges.end(), [&](int i, int j) { return bc[i] > bc[j]; });\n\n    for (int i : idx_edges) {\n        int best_d = -1;\n        double min_sum = 1e18;\n        for (int d = 0; d < D; d++) {\n            if (day_count[d] < K) {\n                if (day_sum[d] < min_sum) {\n                    min_sum = day_sum[d];\n                    best_d = d;\n                }\n            }\n        }\n        if (best_d == -1) {\n            for (int d = 0; d < D; d++) {\n                if (day_count[d] < K) {\n                    best_d = d;\n                    break;\n                }\n            }\n            if (best_d == -1) {\n                best_d = 0;\n            }\n        }\n        assignment[i] = best_d;\n        day_count[best_d]++;\n        day_sum[best_d] += bc[i];\n    }\n\n    vector<vector<int>> adj_uw(N);\n    for (int i = 0; i < M; i++) {\n        int u = get<0>(edges[i]);\n        int v = get<1>(edges[i]);\n        adj_uw[u].push_back(v);\n        adj_uw[v].push_back(u);\n    }\n\n    random_device rd;\n    mt19937 gen(rd());\n\n    for (int d = 0; d < D; d++) {\n        vector<vector<int>> adj_d(N);\n        for (int u = 0; u < N; u++) {\n            for (int v : adj_uw[u]) {\n                int a = min(u, v);\n                int b = max(u, v);\n                int eid = edgeMap[{a, b}];\n                if (assignment[eid] == d) continue;\n                adj_d[u].push_back(v);\n            }\n        }\n\n        vector<bool> vis(N, false);\n        queue<int> q;\n        q.push(0);\n        vis[0] = true;\n        int cnt = 0;\n        while (!q.empty()) {\n            int u = q.front(); q.pop();\n            cnt++;\n            for (int v : adj_d[u]) {\n                if (!vis[v]) {\n                    vis[v] = true;\n                    q.push(v);\n                }\n            }\n        }\n        if (cnt == N) continue;\n\n        vector<int> edges_d;\n        for (int i = 0; i < M; i++) {\n            if (assignment[i] == d) {\n                edges_d.push_back(i);\n            }\n        }\n\n        bool fixed = false;\n        if (edges_d.empty()) continue;\n\n        for (int attempt = 0; attempt < 100; attempt++) {\n            uniform_int_distribution<int> dist_e(0, edges_d.size()-1);\n            int idx_e = dist_e(gen);\n            int e = edges_d[idx_e];\n\n            int d2 = gen() % D;\n            if (d2 == d) continue;\n\n            vector<int> edges_d2;\n            for (int i = 0; i < M; i++) {\n                if (assignment[i] == d2) {\n                    edges_d2.push_back(i);\n                }\n            }\n            if (edges_d2.empty()) continue;\n\n            uniform_int_distribution<int> dist_f(0, edges_d2.size()-1);\n            int idx_f = dist_f(gen);\n            int f = edges_d2[idx_f];\n\n            assignment[e] = d2;\n            assignment[f] = d;\n\n            vector<vector<int>> adj_d1(N);\n            for (int u = 0; u < N; u++) {\n                for (int v : adj_uw[u]) {\n                    int a = min(u, v);\n                    int b = max(u, v);\n                    int eid_t = edgeMap[{a, b}];\n                    if (assignment[eid_t] == d) continue;\n                    adj_d1[u].push_back(v);\n                }\n            }\n            vector<bool> vis1(N, false);\n            queue<int> q1;\n            q1.push(0);\n            vis1[0] = true;\n            int cnt1 = 0;\n            while (!q1.empty()) {\n                int u = q1.front(); q1.pop();\n                cnt1++;\n                for (int v : adj_d1[u]) {\n                    if (!vis1[v]) {\n                        vis1[v] = true;\n                        q1.push(v);\n                    }\n                }\n            }\n            if (cnt1 != N) {\n                assignment[e] = d;\n                assignment[f] = d2;\n                continue;\n            }\n\n            vector<vector<int>> adj_d2(N);\n            for (int u = 0; u < N; u++) {\n                for (int v : adj_uw[u]) {\n                    int a = min(u, v);\n                    int b = max(u, v);\n                    int eid_t = edgeMap[{a, b}];\n                    if (assignment[eid_t] == d2) continue;\n                    adj_d2[u].push_back(v);\n                }\n            }\n            vector<bool> vis2(N, false);\n            queue<int> q2;\n            q2.push(0);\n            vis2[0] = true;\n            int cnt2 = 0;\n            while (!q2.empty()) {\n                int u = q2.front(); q2.pop();\n                cnt2++;\n                for (int v : adj_d2[u]) {\n                    if (!vis2[v]) {\n                        vis2[v] = true;\n                        q2.push(v);\n                    }\n                }\n            }\n            if (cnt2 != N) {\n                assignment[e] = d;\n                assignment[f] = d2;\n                continue;\n            }\n\n            fixed = true;\n            break;\n        }\n    }\n\n    for (int i = 0; i < M; i++) {\n        cout << assignment[i] + 1;\n        if (i < M-1) cout << \" \";\n    }\n    cout << endl;\n\n    return 0;\n}","ahc019":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <tuple>\n#include <algorithm>\n#include <set>\n#include <cctype>\n#include <map>\nusing namespace std;\n\nstruct Rotation {\n    int perm[3];\n    int sign[3];\n};\n\nvector<Rotation> generate_rotations() {\n    vector<int> perm = {0,1,2};\n    vector<Rotation> rots;\n    do {\n        int inv = 0;\n        for (int i=0; i<3; i++)\n            for (int j=i+1; j<3; j++)\n                if (perm[i] > perm[j]) inv++;\n        int perm_sign = (inv%2==0) ? 1 : -1;\n\n        for (int s=0; s<8; s++) {\n            int sx = (s&1) ? -1 : 1;\n            int sy = (s&2) ? -1 : 1;\n            int sz = (s&4) ? -1 : 1;\n            if (perm_sign * sx * sy * sz == 1) {\n                Rotation r;\n                r.perm[0] = perm[0]; r.sign[0] = sx;\n                r.perm[1] = perm[1]; r.sign[1] = sy;\n                r.perm[2] = perm[2]; r.sign[2] = sz;\n                rots.push_back(r);\n            }\n        }\n    } while (next_permutation(perm.begin(), perm.end()));\n    return rots;\n}\n\nint main() {\n    vector<Rotation> rots = generate_rotations();\n\n    int D;\n    cin >> D;\n    vector<vector<string>> f(2, vector<string>(D));\n    vector<vector<string>> r(2, vector<string>(D));\n    for (int i=0; i<2; i++) {\n        for (int j=0; j<D; j++) {\n            cin >> f[i][j];\n        }\n        for (int j=0; j<D; j++) {\n            cin >> r[i][j];\n        }\n    }\n\n    vector<vector<vector<vector<bool>>>> Allowed(2, vector<vector<vector<bool>>>(D, vector<vector<bool>>(D, vector<bool>(D, false))));\n    vector<vector<tuple<int,int,int>>> Base(2);\n\n    for (int obj=0; obj<2; obj++) {\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                    if (f[obj][z][x]=='1' && r[obj][z][y]=='1') {\n                        Allowed[obj][z][x][y] = true;\n                    }\n                }\n            }\n        }\n\n        for (int z=0; z<D; z++) {\n            vector<int> L, R;\n            for (int x=0; x<D; x++) {\n                if (f[obj][z][x]=='1') \n                    L.push_back(x);\n            }\n            for (int y=0; y<D; y++) {\n                if (r[obj][z][y]=='1') \n                    R.push_back(y);\n            }\n            if (L.empty() || R.empty()) \n                continue;\n            for (int x : L) {\n                Base[obj].push_back(make_tuple(x, R[0], z));\n            }\n            for (int j=1; j<R.size(); j++) {\n                Base[obj].push_back(make_tuple(L[0], R[j], z));\n            }\n        }\n    }\n\n    vector<tuple<int,int,int>> dirs = {{1,0,0}, {-1,0,0}, {0,1,0}, {0,-1,0}, {0,0,1}, {0,0,-1}};\n    vector<vector<vector<tuple<int,int,int>>>> blocks(2);\n\n    for (int obj=0; obj<2; obj++) {\n        vector<vector<vector<bool>>> visited(D, vector<vector<bool>>(D, vector<bool>(D, false)));\n        for (auto point : Base[obj]) {\n            int x = get<0>(point);\n            int y = get<1>(point);\n            int z = get<2>(point);\n            if (visited[z][x][y]) \n                continue;\n            queue<tuple<int,int,int>> q;\n            q.push(point);\n            visited[z][x][y] = true;\n            vector<tuple<int,int,int>> block;\n            while (!q.empty()) {\n                auto [cx, cy, cz] = q.front();\n                q.pop();\n                block.push_back(make_tuple(cx, cy, cz));\n                for (auto [dx,dy,dz] : dirs) {\n                    int nx = cx + dx;\n                    int ny = cy + dy;\n                    int nz = cz + dz;\n                    if (nx>=0 && nx<D && ny>=0 && ny<D && nz>=0 && nz<D) {\n                        if (!visited[nz][nx][ny] && Allowed[obj][nz][nx][ny]) {\n                            visited[nz][nx][ny] = true;\n                            q.push(make_tuple(nx, ny, nz));\n                        }\n                    }\n                }\n            }\n            blocks[obj].push_back(block);\n        }\n    }\n\n    vector<vector<vector<tuple<int,int,int>>>> canonical_blocks(2);\n    for (int obj=0; obj<2; obj++) {\n        for (auto &block : blocks[obj]) {\n            vector<vector<tuple<int,int,int>>> candidates;\n            for (auto &rot : rots) {\n                vector<tuple<int,int,int>> transformed;\n                for (auto [x,y,z] : block) {\n                    int coords[3] = {x, y, z};\n                    int nx = rot.sign[0] * coords[rot.perm[0]];\n                    int ny = rot.sign[1] * coords[rot.perm[1]];\n                    int nz = rot.sign[2] * coords[rot.perm[2]];\n                    transformed.push_back(make_tuple(nx, ny, nz));\n                }\n                int min_x = get<0>(transformed[0]), min_y = get<1>(transformed[0]), min_z = get<2>(transformed[0]);\n                for (auto &p : transformed) {\n                    min_x = min(min_x, get<0>(p));\n                    min_y = min(min_y, get<1>(p));\n                    min_z = min(min_z, get<2>(p));\n                }\n                vector<tuple<int,int,int>> shifted;\n                for (auto p : transformed) {\n                    shifted.push_back(make_tuple(get<0>(p)-min_x, get<1>(p)-min_y, get<2>(p)-min_z));\n                }\n                sort(shifted.begin(), shifted.end());\n                candidates.push_back(shifted);\n            }\n            sort(candidates.begin(), candidates.end());\n            canonical_blocks[obj].push_back(candidates[0]);\n        }\n    }\n\n    vector<tuple<int, vector<tuple<int,int,int>>, int>> list0;\n    for (int i=0; i<blocks[0].size(); i++) {\n        list0.push_back(make_tuple(blocks[0][i].size(), canonical_blocks[0][i], i));\n    }\n    vector<tuple<int, vector<tuple<int,int,int>>, int>> list1;\n    for (int i=0; i<blocks[1].size(); i++) {\n        list1.push_back(make_tuple(blocks[1][i].size(), canonical_blocks[1][i], i));\n    }\n\n    sort(list0.rbegin(), list0.rend());\n    sort(list1.rbegin(), list1.rend());\n\n    vector<int> global_id_counter = {1};\n    vector<int> mapping0(blocks[0].size(), -1);\n    vector<int> mapping1(blocks[1].size(), -1);\n\n    for (int i=0; i<list0.size(); i++) {\n        auto [vol0, canon0, idx0] = list0[i];\n        if (mapping0[idx0] != -1) continue;\n        for (int j=0; j<list1.size(); j++) {\n            auto [vol1, canon1, idx1] = list1[j];\n            if (mapping1[idx1] != -1) continue;\n            if (canon0 == canon1) {\n                mapping0[idx0] = global_id_counter[0];\n                mapping1[idx1] = global_id_counter[0];\n                global_id_counter[0]++;\n                break;\n            }\n        }\n    }\n\n    for (int i=0; i<blocks[0].size(); i++) {\n        if (mapping0[i] == -1) {\n            mapping0[i] = global_id_counter[0];\n            global_id_counter[0]++;\n        }\n    }\n    for (int i=0; i<blocks[1].size(); i++) {\n        if (mapping1[i] == -1) {\n            mapping1[i] = global_id_counter[0];\n            global_id_counter[0]++;\n        }\n    }\n\n    int n_blocks = global_id_counter[0] - 1;\n\n    vector<vector<vector<int>>> grid0(D, vector<vector<int>>(D, vector<int>(D,0)));\n    for (int idx=0; idx<blocks[0].size(); idx++) {\n        int gid = mapping0[idx];\n        for (auto [x,y,z] : blocks[0][idx]) {\n            grid0[x][y][z] = gid;\n        }\n    }\n\n    vector<vector<vector<int>>> grid1(D, vector<vector<int>>(D, vector<int>(D,0)));\n    for (int idx=0; idx<blocks[1].size(); idx++) {\n        int gid = mapping1[idx];\n        for (auto [x,y,z] : blocks[1][idx]) {\n            grid1[x][y][z] = gid;\n        }\n    }\n\n    vector<int> flat0, flat1;\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                flat0.push_back(grid0[x][y][z]);\n                flat1.push_back(grid1[x][y][z]);\n            }\n        }\n    }\n\n    cout << n_blocks << endl;\n    for (int i=0; i<flat0.size(); i++) {\n        if (i>0) cout << \" \";\n        cout << flat0[i];\n    }\n    cout << endl;\n    for (int i=0; i<flat1.size(); i++) {\n        if (i>0) cout << \" \";\n        cout << flat1[i];\n    }\n    cout << endl;\n\n    return 0;\n}","ahc020":"#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef tuple<int, ll, int> EdgeTuple;\n\nstruct Edge {\n    int u, v, w, id;\n};\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, K;\n    cin >> N >> M >> K;\n\n    vector<ll> x(N), y(N);\n    for (int i = 0; i < N; i++) {\n        cin >> x[i] >> y[i];\n    }\n\n    vector<Edge> edges;\n    for (int j = 0; j < M; j++) {\n        int u, v, w;\n        cin >> u >> v >> w;\n        u--; v--;\n        edges.push_back({u, v, w, j});\n    }\n\n    vector<ll> a(K), b(K);\n    for (int k = 0; k < K; k++) {\n        cin >> a[k] >> b[k];\n    }\n\n    vector<int> assigned_station(K, -1);\n    vector<vector<int>> cluster(N);\n\n    for (int k = 0; k < K; k++) {\n        ll min_d_sq = 1e18;\n        int best_i = -1;\n        for (int i = 0; i < N; i++) {\n            ll dx = a[k] - x[i];\n            ll dy = b[k] - y[i];\n            ll d_sq = dx*dx + dy*dy;\n            if (d_sq < min_d_sq) {\n                min_d_sq = d_sq;\n                best_i = i;\n            }\n        }\n        assigned_station[k] = best_i;\n        cluster[best_i].push_back(k);\n    }\n\n    vector<ll> P(N, 0);\n    vector<bool> in_S(N, false);\n\n    for (int i = 0; i < N; i++) {\n        if (cluster[i].empty()) {\n            P[i] = 0;\n        } else {\n            in_S[i] = true;\n            ll max_d_sq = 0;\n            for (int k : cluster[i]) {\n                ll dx = a[k] - x[i];\n                ll dy = b[k] - y[i];\n                ll d_sq = dx*dx + dy*dy;\n                if (d_sq > max_d_sq) max_d_sq = d_sq;\n            }\n            if (max_d_sq == 0) {\n                P[i] = 0;\n            } else {\n                ll candidate = sqrt(max_d_sq);\n                if (candidate * candidate < max_d_sq) {\n                    candidate++;\n                }\n                P[i] = candidate;\n                if (P[i] > 5000) {\n                    P[i] = 5000;\n                }\n            }\n        }\n    }\n\n    set<int> T_set;\n    for (int i = 0; i < N; i++) {\n        if (in_S[i]) {\n            T_set.insert(i);\n        }\n    }\n    T_set.insert(0);\n\n    vector<vector<EdgeTuple>> adj(N);\n    for (const Edge& e : edges) {\n        int u = e.u, v = e.v;\n        ll w = e.w;\n        int id = e.id;\n        adj[u].push_back(make_tuple(v, w, id));\n        adj[v].push_back(make_tuple(u, w, id));\n    }\n\n    vector<bool> in_tree(N, false);\n    vector<bool> edge_used(M, false);\n\n    in_tree[0] = true;\n    set<int> T_remaining = T_set;\n    T_remaining.erase(0);\n\n    while (!T_remaining.empty()) {\n        vector<ll> dist(N, 1e18);\n        vector<int> parent_node(N, -1);\n        vector<int> parent_edge_id(N, -1);\n        priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<>> pq;\n\n        for (int i = 0; i < N; i++) {\n            if (in_tree[i]) {\n                dist[i] = 0;\n                pq.push({0, i});\n            }\n        }\n\n        bool found = false;\n        int found_node = -1;\n\n        while (!pq.empty()) {\n            auto [d, u] = pq.top();\n            pq.pop();\n            if (d != dist[u]) continue;\n\n            if (T_remaining.find(u) != T_remaining.end()) {\n                found = true;\n                found_node = u;\n                break;\n            }\n\n            for (auto &edge : adj[u]) {\n                int v = get<0>(edge);\n                ll w = get<1>(edge);\n                int eid = get<2>(edge);\n                if (dist[v] > dist[u] + w) {\n                    dist[v] = dist[u] + w;\n                    parent_node[v] = u;\n                    parent_edge_id[v] = eid;\n                    pq.push({dist[v], v});\n                }\n            }\n        }\n\n        if (!found) {\n            break;\n        }\n\n        int cur = found_node;\n        while (!in_tree[cur]) {\n            edge_used[parent_edge_id[cur]] = true;\n            in_tree[cur] = true;\n            cur = parent_node[cur];\n        }\n\n        T_remaining.erase(found_node);\n    }\n\n    for (int i = 0; i < N; i++) {\n        cout << P[i];\n        if (i < N-1) cout << \" \";\n    }\n    cout << endl;\n\n    for (int j = 0; j < M; j++) {\n        cout << edge_used[j];\n        if (j < M-1) cout << \" \";\n    }\n    cout << endl;\n\n    return 0;\n}","ahc021":"#include <iostream>\n#include <vector>\n#include <tuple>\nusing namespace std;\n\nint main() {\n    int grid[30][30];\n\n    for (int i = 0; i < 30; i++) {\n        for (int j = 0; j <= i; j++) {\n            cin >> grid[i][j];\n        }\n    }\n\n    vector<tuple<int, int, int, int>> ops;\n\n    for (int x = 28; x >= 0; x--) {\n        for (int y = 0; y <= x; y++) {\n            int cx = x;\n            int cy = y;\n            while (true) {\n                int minChildVal = 1000000;\n                int minChildX = -1;\n                int minChildY = -1;\n\n                if (cx + 1 < 30) {\n                    if (grid[cx+1][cy] < minChildVal) {\n                        minChildVal = grid[cx+1][cy];\n                        minChildX = cx+1;\n                        minChildY = cy;\n                    }\n                    if (grid[cx+1][cy+1] < minChildVal) {\n                        minChildVal = grid[cx+1][cy+1];\n                        minChildX = cx+1;\n                        minChildY = cy+1;\n                    }\n                }\n\n                if (minChildX != -1 && minChildVal < grid[cx][cy]) {\n                    swap(grid[cx][cy], grid[minChildX][minChildY]);\n                    ops.push_back(make_tuple(cx, cy, minChildX, minChildY));\n                    cx = minChildX;\n                    cy = minChildY;\n                } else {\n                    break;\n                }\n            }\n        }\n    }\n\n    cout << ops.size() << endl;\n    for (auto op : ops) {\n        cout << get<0>(op) << \" \" << get<1>(op) << \" \" << get<2>(op) << \" \" << get<3>(op) << endl;\n    }\n\n    return 0;\n}","toyota2023summer-final":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\n#include <functional>\n#include <tuple>\n#include <climits>\n#include <cmath>\n\nusing namespace std;\n\nint main() {\n    int D = 9;\n    int N;\n    cin >> D >> N;\n\n    vector<vector<int>> grid(D, vector<int>(D, -3));\n    grid[0][(D-1)/2] = -2;\n\n    for (int i = 0; i < N; i++) {\n        int ri, rj;\n        cin >> ri >> rj;\n        grid[ri][rj] = -1;\n    }\n\n    vector<vector<int>> parent_i(D, vector<int>(D, -1));\n    vector<vector<int>> parent_j(D, vector<int>(D, -1));\n    vector<vector<vector<pair<int, int>>>> children(D, vector<vector<pair<int, int>>>(D));\n\n    vector<int> dx = {0, 1, 0, -1};\n    vector<int> dy = {1, 0, -1, 0};\n\n    queue<pair<int, int>> q_bfs_parent;\n    q_bfs_parent.push({0, (D-1)/2});\n    parent_i[0][(D-1)/2] = -1;\n    parent_j[0][(D-1)/2] = -1;\n\n    while (!q_bfs_parent.empty()) {\n        auto [i, j] = q_bfs_parent.front();\n        q_bfs_parent.pop();\n        for (int d = 0; d < 4; d++) {\n            int ni = i + dx[d];\n            int nj = j + dy[d];\n            if (ni >= 0 && ni < D && nj >= 0 && nj < D) {\n                if (parent_i[ni][nj] == -1) {\n                    if (grid[ni][nj] != -1) {\n                        parent_i[ni][nj] = i;\n                        parent_j[ni][nj] = j;\n                        q_bfs_parent.push({ni, nj});\n                    }\n                }\n            }\n        }\n    }\n\n    for (int i = 0; i < D; i++) {\n        for (int j = 0; j < D; j++) {\n            if (i == 0 && j == (D-1)/2) continue;\n            if (parent_i[i][j] != -1) {\n                int pi = parent_i[i][j];\n                int pj = parent_j[i][j];\n                children[pi][pj].push_back({i, j});\n            }\n        }\n    }\n\n    for (int i = 0; i < D; i++) {\n        for (int j = 0; j < D; j++) {\n            sort(children[i][j].begin(), children[i][j].end());\n        }\n    }\n\n    vector<vector<int>> post_index(D, vector<int>(D, -1));\n    int index_counter = 0;\n\n    function<void(int, int)> dfs = [&](int i, int j) {\n        for (auto& child : children[i][j]) {\n            dfs(child.first, child.second);\n        }\n        if (!(i == 0 && j == (D-1)/2)) {\n            post_index[i][j] = index_counter;\n            index_counter++;\n        }\n    };\n\n    for (auto& child : children[0][(D-1)/2]) {\n        dfs(child.first, child.second);\n    }\n\n    int M = D * D - 1 - N;\n\n    for (int d = 0; d < M; d++) {\n        int t_d;\n        cin >> t_d;\n\n        vector<vector<bool>> visited(D, vector<bool>(D, false));\n        vector<pair<int, int>> free_list;\n        queue<pair<int, int>> q_bfs;\n        q_bfs.push({0, (D-1)/2});\n        visited[0][(D-1)/2] = true;\n\n        while (!q_bfs.empty()) {\n            auto [i, j] = q_bfs.front();\n            q_bfs.pop();\n            if (grid[i][j] == -3) {\n                free_list.push_back({i, j});\n            }\n            for (int dir = 0; dir < 4; dir++) {\n                int ni = i + dx[dir];\n                int nj = j + dy[dir];\n                if (ni >= 0 && ni < D && nj >= 0 && nj < D && !visited[ni][nj]) {\n                    if (grid[ni][nj] == -1 || grid[ni][nj] >= 0) continue;\n                    visited[ni][nj] = true;\n                    q_bfs.push({ni, nj});\n                }\n            }\n        }\n\n        int best_i = -1, best_j = -1;\n        int best_diff = INT_MAX;\n        for (auto [i, j] : free_list) {\n            if (post_index[i][j] == -1) continue;\n            int diff = abs(post_index[i][j] - t_d);\n            if (diff < best_diff || (diff == best_diff && (i < best_i || (i == best_i && j < best_j)))) {\n                best_diff = diff;\n                best_i = i;\n                best_j = j;\n            }\n        }\n\n        if (best_i == -1) {\n            for (auto [i, j] : free_list) {\n                best_i = i;\n                best_j = j;\n                break;\n            }\n        }\n\n        cout << best_i << \" \" << best_j << endl;\n        cout.flush();\n        grid[best_i][best_j] = t_d;\n    }\n\n    vector<pair<int, int>> removal_sequence;\n\n    for (int step = 0; step < M; step++) {\n        vector<vector<bool>> visited(D, vector<bool>(D, false));\n        queue<pair<int, int>> q_bfs;\n        q_bfs.push({0, (D-1)/2});\n        visited[0][(D-1)/2] = true;\n\n        while (!q_bfs.empty()) {\n            auto [i, j] = q_bfs.front();\n            q_bfs.pop();\n            for (int dir = 0; dir < 4; dir++) {\n                int ni = i + dx[dir];\n                int nj = j + dy[dir];\n                if (ni >= 0 && ni < D && nj >= 0 && nj < D && !visited[ni][nj]) {\n                    if (grid[ni][nj] == -1) continue;\n                    if (grid[ni][nj] >= 0) continue;\n                    visited[ni][nj] = true;\n                    q_bfs.push({ni, nj});\n                }\n            }\n        }\n\n        vector<tuple<int, int, int>> removable;\n        for (int i = 0; i < D; i++) {\n            for (int j = 0; j < D; j++) {\n                if (grid[i][j] >= 0) {\n                    bool flag = false;\n                    for (int dir = 0; dir < 4; dir++) {\n                        int ni = i + dx[dir];\n                        int nj = j + dy[dir];\n                        if (ni >= 0 && ni < D && nj >= 0 && nj < D) {\n                            if (visited[ni][nj]) {\n                                flag = true;\n                                break;\n                            }\n                        }\n                    }\n                    if (flag) {\n                        removable.push_back(make_tuple(grid[i][j], i, j));\n                    }\n                }\n            }\n        }\n\n        if (removable.empty()) {\n            for (int i = 0; i < D; i++) {\n                for (int j = 0; j < D; j++) {\n                    if (grid[i][j] >= 0) {\n                        removable.push_back(make_tuple(grid[i][j], i, j));\n                    }\n                }\n            }\n            sort(removable.begin(), removable.end());\n        } else {\n            sort(removable.begin(), removable.end());\n        }\n\n        if (removable.empty()) {\n            break;\n        }\n\n        auto [num, i, j] = removable[0];\n        removal_sequence.push_back({i, j});\n        grid[i][j] = -3;\n    }\n\n    for (auto [i, j] : removal_sequence) {\n        cout << i << \" \" << j << endl;\n    }\n    cout.flush();\n\n    return 0;\n}","ahc024":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\n#include <cstring>\nusing namespace std;\n\ntypedef pair<int, int> pii;\n\nconst int dx[4] = {1, 0, -1, 0};\nconst int dy[4] = {0, 1, 0, -1};\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int n, m;\n    cin >> n >> m;\n    vector<vector<int>> grid(n, vector<int>(n));\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            cin >> grid[i][j];\n        }\n    }\n\n    vector<vector<bool>> orig_adj(101, vector<bool>(101, false));\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            if (i == 0 || i == n-1 || j == 0 || j == n-1) {\n                int c = grid[i][j];\n                if (c > 0) {\n                    orig_adj[0][c] = true;\n                }\n            }\n        }\n    }\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            if (i < n-1 && grid[i][j] != grid[i+1][j] && grid[i][j] > 0 && grid[i+1][j] > 0) {\n                int c1 = min(grid[i][j], grid[i+1][j]);\n                int c2 = max(grid[i][j], grid[i+1][j]);\n                orig_adj[c1][c2] = true;\n            }\n            if (j < n-1 && grid[i][j] != grid[i][j+1] && grid[i][j] > 0 && grid[i][j+1] > 0) {\n                int c1 = min(grid[i][j], grid[i][j+1]);\n                int c2 = max(grid[i][j], grid[i][j+1]);\n                orig_adj[c1][c2] = true;\n            }\n        }\n    }\n\n    vector<vector<int>> current_adj_count(101, vector<int>(101, 0));\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            if (i == 0 || i == n-1 || j == 0 || j == n-1) {\n                int c = grid[i][j];\n                if (c > 0) {\n                    current_adj_count[0][c]++;\n                }\n            }\n        }\n    }\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            if (i < n-1 && grid[i][j] != grid[i+1][j] && grid[i][j] > 0 && grid[i+1][j] > 0) {\n                int c1 = min(grid[i][j], grid[i+1][j]);\n                int c2 = max(grid[i][j], grid[i+1][j]);\n                current_adj_count[c1][c2]++;\n            }\n            if (j < n-1 && grid[i][j] != grid[i][j+1] && grid[i][j] > 0 && grid[i][j+1] > 0) {\n                int c1 = min(grid[i][j], grid[i][j+1]);\n                int c2 = max(grid[i][j], grid[i][j+1]);\n                current_adj_count[c1][c2]++;\n            }\n        }\n    }\n\n    vector<int> ward_size(m+1, 0);\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            int c = grid[i][j];\n            if (c > 0) {\n                ward_size[c]++;\n            }\n        }\n    }\n\n    int num_zeros = 0;\n    vector<vector<bool>> in_queue(n, vector<bool>(n, true));\n    queue<pii> candidate_queue;\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            candidate_queue.push({i, j});\n        }\n    }\n\n    while (!candidate_queue.empty()) {\n        auto [i, j] = candidate_queue.front();\n        candidate_queue.pop();\n        in_queue[i][j] = false;\n        if (grid[i][j] == 0) continue;\n\n        int c = grid[i][j];\n        if (ward_size[c] == 1) {\n            continue;\n        }\n\n        vector<pii> neighbors_in_c;\n        for (int d = 0; d < 4; d++) {\n            int ni = i + dx[d];\n            int nj = j + dy[d];\n            if (ni < 0 || ni >= n || nj < 0 || nj >= n) continue;\n            if (grid[ni][nj] == c) {\n                neighbors_in_c.push_back({ni, nj});\n            }\n        }\n        if (neighbors_in_c.empty()) {\n            continue;\n        }\n\n        int total_after = ward_size[c] - 1;\n        vector<vector<bool>> visited(n, vector<bool>(n, false));\n        queue<pii> q_bfs;\n        auto [start_i, start_j] = neighbors_in_c[0];\n        q_bfs.push({start_i, start_j});\n        visited[start_i][start_j] = true;\n        int count_visited = 1;\n\n        while (!q_bfs.empty()) {\n            auto [x, y] = q_bfs.front();\n            q_bfs.pop();\n            for (int d = 0; d < 4; d++) {\n                int nx = x + dx[d];\n                int ny = y + dy[d];\n                if (nx < 0 || nx >= n || ny < 0 || ny >= n) continue;\n                if (grid[nx][ny] != c) continue;\n                if (visited[nx][ny]) continue;\n                if (nx == i && ny == j) continue;\n                visited[nx][ny] = true;\n                count_visited++;\n                q_bfs.push({nx, ny});\n            }\n        }\n\n        if (count_visited != total_after) {\n            continue;\n        }\n\n        bool skip = false;\n        for (int d = 0; d < 4; d++) {\n            int ni = i + dx[d];\n            int nj = j + dy[d];\n            if (ni < 0 || ni >= n || nj < 0 || nj >= n) continue;\n            int d_val = grid[ni][nj];\n            if (d_val == c) continue;\n            if (d_val == 0) {\n                if (current_adj_count[0][c] == 1 && orig_adj[0][c]) {\n                    skip = true;\n                    break;\n                }\n            } else {\n                int c1 = min(c, d_val);\n                int c2 = max(c, d_val);\n                if (current_adj_count[c1][c2] == 1 && orig_adj[c1][c2]) {\n                    skip = true;\n                    break;\n                }\n            }\n        }\n        if (skip) continue;\n\n        for (int d = 0; d < 4; d++) {\n            int ni = i + dx[d];\n            int nj = j + dy[d];\n            if (ni < 0 || ni >= n || nj < 0 || nj >= n) continue;\n            int d_val = grid[ni][nj];\n            if (d_val > 0 && d_val != c) {\n                if (!orig_adj[0][d_val]) {\n                    skip = true;\n                    break;\n                }\n            }\n        }\n        if (skip) continue;\n\n        if (num_zeros > 0) {\n            bool has_zero_neighbor = false;\n            for (int d = 0; d < 4; d++) {\n                int ni = i + dx[d];\n                int nj = j + dy[d];\n                if (ni < 0 || ni >= n || nj < 0 || nj >= n) continue;\n                if (grid[ni][nj] == 0) {\n                    has_zero_neighbor = true;\n                    break;\n                }\n            }\n            if (!has_zero_neighbor) {\n                continue;\n            }\n        }\n\n        for (int d = 0; d < 4; d++) {\n            int ni = i + dx[d];\n            int nj = j + dy[d];\n            if (ni < 0 || ni >= n || nj < 0 || nj >= n) continue;\n            int d_val = grid[ni][nj];\n            if (d_val == c) continue;\n            if (d_val == 0) {\n                current_adj_count[0][c]--;\n            } else {\n                int c1 = min(c, d_val);\n                int c2 = max(c, d_val);\n                current_adj_count[c1][c2]--;\n            }\n        }\n\n        if (i == 0 || i == n-1 || j == 0 || j == n-1) {\n            current_adj_count[0][c]--;\n        }\n\n        grid[i][j] = 0;\n        num_zeros++;\n        ward_size[c]--;\n\n        for (int d = 0; d < 4; d++) {\n            int ni = i + dx[d];\n            int nj = j + dy[d];\n            if (ni < 0 || ni >= n || nj < 0 || nj >= n) continue;\n            int d_val = grid[ni][nj];\n            if (d_val > 0) {\n                current_adj_count[0][d_val]++;\n            }\n        }\n\n        for (int d = 0; d < 4; d++) {\n            int ni = i + dx[d];\n            int nj = j + dy[d];\n            if (ni < 0 || ni >= n || nj < 0 || nj >= n) continue;\n            if (grid[ni][nj] != 0 && !in_queue[ni][nj]) {\n                candidate_queue.push({ni, nj});\n                in_queue[ni][nj] = true;\n            }\n        }\n    }\n\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            if (j > 0) cout << \" \";\n            cout << grid[i][j];\n        }\n        cout << endl;\n    }\n\n    return 0;\n}","ahc025":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <random>\n#include <cstdlib>\n#include <ctime>\n\nusing namespace std;\n\nbool my_compare(int a, int b, int &query_count) {\n    cout << \"1 1 \" << a << \" \" << b << endl;\n    query_count++;\n    string res;\n    cin >> res;\n    if (res == \"<\") {\n        return true;\n    } else if (res == \">\") {\n        return false;\n    } else {\n        return a < b;\n    }\n}\n\nvector<int> merge(vector<int> left, vector<int> right, int &query_count) {\n    vector<int> res;\n    int i = 0, j = 0;\n    while (i < left.size() && j < right.size()) {\n        if (my_compare(left[i], right[j], query_count)) {\n            res.push_back(left[i]);\n            i++;\n        } else {\n            res.push_back(right[j]);\n            j++;\n        }\n    }\n    while (i < left.size()) {\n        res.push_back(left[i]);\n        i++;\n    }\n    while (j < right.size()) {\n        res.push_back(right[j]);\n        j++;\n    }\n    return res;\n}\n\nvector<int> merge_sort(vector<int> v, int &query_count) {\n    if (v.size() <= 1) {\n        return v;\n    }\n    int mid = v.size() / 2;\n    vector<int> left(v.begin(), v.begin() + mid);\n    vector<int> right(v.begin() + mid, v.end());\n    left = merge_sort(left, query_count);\n    right = merge_sort(right, query_count);\n    return merge(left, right, query_count);\n}\n\nint main() {\n    int N, D, Q;\n    cin >> N >> D >> Q;\n\n    vector<int> items;\n    for (int i = 0; i < N; i++) {\n        items.push_back(i);\n    }\n\n    int query_count = 0;\n    items = merge_sort(items, query_count);\n    reverse(items.begin(), items.end());\n\n    vector<vector<int>> groups(D);\n    for (int i = 0; i < D; i++) {\n        groups[i].push_back(items[i]);\n    }\n\n    int remaining_items = N - D;\n    if (remaining_items > 0) {\n        int remaining_queries = Q - query_count;\n        int per_item_budget = remaining_queries / remaining_items;\n        int k = min(D, per_item_budget + 1);\n\n        vector<int> all_groups(D);\n        for (int i = 0; i < D; i++) {\n            all_groups[i] = i;\n        }\n\n        if (k == 0) {\n            for (int i = D; i < N; i++) {\n                int group_index = rand() % D;\n                groups[group_index].push_back(items[i]);\n            }\n        } else if (k == 1) {\n            for (int i = D; i < N; i++) {\n                int group_index = rand() % D;\n                groups[group_index].push_back(items[i]);\n            }\n        } else {\n            random_device rd;\n            mt19937 g(rd());\n            for (int i = D; i < N; i++) {\n                shuffle(all_groups.begin(), all_groups.end(), g);\n                vector<int> candidates;\n                for (int j = 0; j < k; j++) {\n                    candidates.push_back(all_groups[j]);\n                }\n                while (candidates.size() > 1) {\n                    int idx1 = candidates[0];\n                    int idx2 = candidates[1];\n                    int n1 = groups[idx1].size();\n                    int n2 = groups[idx2].size();\n                    cout << n1 << \" \" << n2;\n                    for (int item : groups[idx1]) {\n                        cout << \" \" << item;\n                    }\n                    for (int item : groups[idx2]) {\n                        cout << \" \" << item;\n                    }\n                    cout << endl;\n                    string res;\n                    cin >> res;\n                    query_count++;\n                    if (res == \"<\") {\n                        candidates.erase(candidates.begin() + 1);\n                    } else if (res == \">\") {\n                        candidates.erase(candidates.begin());\n                    } else {\n                        candidates.erase(candidates.begin() + 1);\n                    }\n                }\n                int group_index = candidates[0];\n                groups[group_index].push_back(items[i]);\n            }\n        }\n    }\n\n    while (query_count < Q) {\n        cout << \"1 1 \" << items[0] << \" \" << items[1] << endl;\n        string res;\n        cin >> res;\n        query_count++;\n    }\n\n    vector<int> ans(N);\n    for (int g = 0; g < D; g++) {\n        for (int item : groups[g]) {\n            ans[item] = g;\n        }\n    }\n\n    for (int i = 0; i < N; i++) {\n        if (i > 0) cout << \" \";\n        cout << ans[i];\n    }\n    cout << endl;\n\n    return 0;\n}","ahc026":"#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int n, m;\n    cin >> n >> m;\n    vector<vector<int>> stacks(m);\n    for (int i = 0; i < m; i++) {\n        for (int j = 0; j < n/m; j++) {\n            int x;\n            cin >> x;\n            stacks[i].push_back(x);\n        }\n    }\n\n    vector<pair<int, int>> operations;\n\n    for (int v = 1; v <= n; v++) {\n        int s = -1, pos = -1;\n        for (int i = 0; i < m; i++) {\n            for (int j = 0; j < stacks[i].size(); j++) {\n                if (stacks[i][j] == v) {\n                    s = i;\n                    pos = j;\n                    break;\n                }\n            }\n            if (s != -1) break;\n        }\n\n        if (s == -1) {\n            continue;\n        }\n\n        if (pos == (int)stacks[s].size() - 1) {\n            operations.push_back({v, 0});\n            stacks[s].pop_back();\n        } else {\n            int w = stacks[s][pos+1];\n            int d = -1;\n            int best_top = -1;\n\n            for (int i = 0; i < m; i++) {\n                if (i == s) continue;\n                if (stacks[i].empty()) {\n                    if (1000 > best_top) {\n                        best_top = 1000;\n                        d = i;\n                    }\n                } else {\n                    int top_val = stacks[i].back();\n                    if (top_val > best_top) {\n                        best_top = top_val;\n                        d = i;\n                    }\n                }\n            }\n\n            if (d == -1) {\n                d = (s == 0) ? 1 : 0;\n            }\n\n            vector<int> seg;\n            for (int j = pos+1; j < stacks[s].size(); j++) {\n                seg.push_back(stacks[s][j]);\n            }\n            stacks[s].resize(pos+1);\n            for (int box : seg) {\n                stacks[d].push_back(box);\n            }\n\n            operations.push_back({w, d+1});\n            operations.push_back({v, 0});\n            stacks[s].pop_back();\n        }\n    }\n\n    for (auto &op : operations) {\n        cout << op.first << \" \" << op.second << '\\n';\n    }\n\n    return 0;\n}","ahc027":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\n#include <cmath>\n#include <unordered_map>\n#include <cctype>\n#include <climits>\n#include <functional>\n#include <set>\n#include <iomanip>\n\nusing namespace std;\n\nconst int dx[4] = {0, 1, 0, -1};\nconst string dir_str = \"RDLU\";\nconst int dy[4] = {1, 0, -1, 0};\n\nstruct Cell {\n    int i, j;\n    Cell(int i=0, int j=0) : i(i), j(j) {}\n    bool operator==(const Cell& other) const {\n        return i == other.i && j == other.j;\n    }\n    bool operator!=(const Cell& other) const {\n        return !(*this == other);\n    }\n};\n\nnamespace std {\n    template <>\n    struct hash<Cell> {\n        size_t operator()(const Cell& c) const {\n            return c.i * 100 + c.j;\n        }\n    };\n}\n\nint get_index(int i, int j, int N) {\n    return i * N + j;\n}\n\nCell get_cell(int idx, int N) {\n    return Cell(idx / N, idx % N);\n}\n\nchar get_move_char(Cell from, Cell to) {\n    if (to.i == from.i + 1) return 'D';\n    if (to.i == from.i - 1) return 'U';\n    if (to.j == from.j + 1) return 'R';\n    if (to.j == from.j - 1) return 'L';\n    return '?';\n}\n\nchar reverse_move(char m) {\n    if (m == 'U') return 'D';\n    if (m == 'D') return 'U';\n    if (m == 'L') return 'R';\n    if (m == 'R') return 'L';\n    return m;\n}\n\nvector<vector<int>> precompute_all_pairs_shortest_path(int N, const vector<vector<bool>>& down_wall, const vector<vector<bool>>& right_wall) {\n    int total_cells = N * N;\n    vector<vector<int>> dist(total_cells, vector<int>(total_cells, INT_MAX));\n\n    for (int idx = 0; idx < total_cells; idx++) {\n        Cell src = get_cell(idx, N);\n        vector<int>& d_vec = dist[idx];\n        d_vec[idx] = 0;\n        queue<int> q;\n        q.push(idx);\n\n        while (!q.empty()) {\n            int u_idx = q.front();\n            q.pop();\n            Cell u = get_cell(u_idx, N);\n\n            for (int d = 0; d < 4; d++) {\n                Cell v = Cell(u.i + dx[d], u.j + dy[d]);\n                if (v.i < 0 || v.i >= N || v.j < 0 || v.j >= N) continue;\n                if (d == 0) {\n                    if (right_wall[u.i][u.j]) continue;\n                } else if (d == 1) {\n                    if (down_wall[u.i][u.j]) continue;\n                } else if (d == 2) {\n                    if (v.j < u.j && (u.j-1>=0) && right_wall[u.i][u.j-1]) continue;\n                } else if (d == 3) {\n                    if (v.i < u.i && (u.i-1>=0) && down_wall[u.i-1][u.j]) continue;\n                }\n\n                int v_idx = get_index(v.i, v.j, N);\n                if (d_vec[v_idx] == INT_MAX) {\n                    d_vec[v_idx] = d_vec[u_idx] + 1;\n                    q.push(v_idx);\n                }\n            }\n        }\n    }\n\n    return dist;\n}\n\nvector<vector<int>> precompute_parents_all(int N, const vector<vector<bool>>& down_wall, const vector<vector<bool>>& right_wall) {\n    int total_cells = N * N;\n    vector<vector<int>> parent_all(total_cells, vector<int>(total_cells, -1));\n\n    for (int src_idx = 0; src_idx < total_cells; src_idx++) {\n        vector<int>& parent = parent_all[src_idx];\n        vector<int> dist(total_cells, INT_MAX);\n        dist[src_idx] = 0;\n        parent[src_idx] = src_idx;\n        queue<int> q;\n        q.push(src_idx);\n\n        while (!q.empty()) {\n            int u_idx = q.front();\n            q.pop();\n            Cell u = get_cell(u_idx, N);\n\n            for (int d = 0; d < 4; d++) {\n                Cell v = Cell(u.i + dx[d], u.j + dy[d]);\n                if (v.i < 0 || v.i >= N || v.j < 0 || v.j >= N) continue;\n                if (d == 0) {\n                    if (right_wall[u.i][u.j]) continue;\n                } else if (d == 1) {\n                    if (down_wall[u.i][u.j]) continue;\n                } else if (d == 2) {\n                    if (v.j < u.j && (u.j-1>=0) && right_wall[u.i][u.j-1]) continue;\n                } else if (d == 3) {\n                    if (v.i < u.i && (u.i-1>=0) && down_wall[u.i-1][u.j]) continue;\n                }\n\n                int v_idx = get_index(v.i, v.j, N);\n                if (dist[v_idx] == INT_MAX) {\n                    dist[v_idx] = dist[u_idx] + 1;\n                    parent[v_idx] = u_idx;\n                    q.push(v_idx);\n                }\n            }\n        }\n    }\n\n    return parent_all;\n}\n\nstring generate_base_route(int N, const vector<vector<bool>>& down_wall, const vector<vector<bool>>& right_wall, const vector<vector<int>>& d) {\n    int total_cells = N * N;\n    vector<int> parent_tree(total_cells, -1);\n    vector<int> dist(total_cells, INT_MAX);\n    vector<vector<int>> children(total_cells);\n\n    queue<int> q;\n    dist[0] = 0;\n    parent_tree[0] = 0;\n    q.push(0);\n\n    while (!q.empty()) {\n        int u_idx = q.front(); q.pop();\n        Cell u = get_cell(u_idx, N);\n        for (int d = 0; d < 4; d++) {\n            Cell v = Cell(u.i + dx[d], u.j + dy[d]);\n            if (v.i < 0 || v.i >= N || v.j < 0 || v.j >= N) continue;\n            if (d == 0) {\n                if (right_wall[u.i][u.j]) continue;\n            } else if (d == 1) {\n                if (down_wall[u.i][u.j]) continue;\n            } else if (d == 2) {\n                if (v.j < u.j && (u.j-1>=0) && right_wall[u.i][u.j-1]) continue;\n            } else if (d == 3) {\n                if (v.i < u.i && (u.i-1>=0) && down_wall[u.i-1][u.j]) continue;\n            }\n\n            int v_idx = get_index(v.i, v.j, N);\n            if (dist[v_idx] == INT_MAX) {\n                dist[v_idx] = dist[u_idx] + 1;\n                parent_tree[v_idx] = u_idx;\n                q.push(v_idx);\n                children[u_idx].push_back(v_idx);\n            }\n        }\n    }\n\n    for (int i = 0; i < total_cells; i++) {\n        if (dist[i] == INT_MAX) {\n            cerr << \"Cell not reached: \" << i/N << \", \" << i%N << endl;\n        }\n    }\n\n    function<string(int)> dfs = [&](int u_idx) {\n        string route = \"\";\n        Cell u = get_cell(u_idx, N);\n        vector<int>& child_list = children[u_idx];\n        sort(child_list.begin(), child_list.end(), [&](int a, int b) {\n            return d[get_cell(a, N).i][get_cell(a, N).j] > d[get_cell(b, N).i][get_cell(b, N).j];\n        });\n\n        for (int v_idx : child_list) {\n            Cell v = get_cell(v_idx, N);\n            char move_char = get_move_char(u, v);\n            route += move_char;\n            route += dfs(v_idx);\n            route += reverse_move(move_char);\n        }\n        return route;\n    };\n\n    string base_route = dfs(0);\n    return base_route;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N;\n    cin >> N;\n    vector<string> h_lines(N-1);\n    vector<string> v_lines(N);\n    vector<vector<int>> d(N, vector<int>(N));\n\n    for (int i = 0; i < N-1; i++) {\n        cin >> h_lines[i];\n    }\n    for (int i = 0; i < N; i++) {\n        cin >> v_lines[i];\n    }\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cin >> d[i][j];\n        }\n    }\n\n    vector<vector<bool>> down_wall(N-1, vector<bool>(N, false));\n    vector<vector<bool>> right_wall(N, vector<bool>(N-1, false));\n\n    for (int i = 0; i < N-1; i++) {\n        for (int j = 0; j < N; j++) {\n            down_wall[i][j] = (h_lines[i][j] == '1');\n        }\n    }\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N-1; j++) {\n            right_wall[i][j] = (v_lines[i][j] == '1');\n        }\n    }\n\n    vector<vector<int>> all_dist = precompute_all_pairs_shortest_path(N, down_wall, right_wall);\n    vector<vector<int>> parent_all = precompute_parents_all(N, down_wall, right_wall);\n\n    string base_route = generate_base_route(N, down_wall, right_wall, d);\n    string current_route = base_route;\n\n    int total_cells = N * N;\n    vector<vector<int>> occ(total_cells);\n    vector<Cell> positions;\n    Cell cur(0, 0);\n    positions.push_back(cur);\n    for (int i = 0; i < current_route.size(); i++) {\n        char m = current_route[i];\n        Cell next = cur;\n        if (m == 'R') next.j++;\n        else if (m == 'D') next.i++;\n        else if (m == 'L') next.j--;\n        else if (m == 'U') next.i--;\n        positions.push_back(next);\n        cur = next;\n    }\n\n    for (int idx = 0; idx < positions.size(); idx++) {\n        Cell cell = positions[idx];\n        if (idx > 0) {\n            int cell_idx = get_index(cell.i, cell.j, N);\n            occ[cell_idx].push_back(idx-1);\n        }\n    }\n\n    int L0 = current_route.size();\n    vector<int> freq(total_cells, 0);\n    for (int i = 0; i < total_cells; i++) {\n        freq[i] = occ[i].size();\n    }\n\n    vector<double> gap_est(total_cells, 0.0);\n    for (int i = 0; i < total_cells; i++) {\n        if (freq[i] > 0) {\n            gap_est[i] = L0 * 1.0 / freq[i];\n        }\n    }\n\n    using pdi = pair<double, int>;\n    priority_queue<pdi> pq;\n    for (int i = 0; i < total_cells; i++) {\n        if (freq[i] > 0) {\n            double priority_val = d[get_cell(i, N).i][get_cell(i, N).j] * gap_est[i] * gap_est[i];\n            pq.push({priority_val, i});\n        }\n    }\n\n    int insertion_count = 0;\n    const int MAX_INSERTION = 1000;\n    vector<int> dummy_occ_count(total_cells, 0);\n\n    while (insertion_count < MAX_INSERTION && !pq.empty()) {\n        double priority_val = pq.top().first;\n        int c_idx = pq.top().second;\n        pq.pop();\n        if (freq[c_idx] == 0) continue;\n\n        int best_u_idx = -1;\n        int best_dist = INT_MAX;\n        for (int u_idx = 0; u_idx < total_cells; u_idx++) {\n            if (freq[u_idx] == 0) continue;\n            if (all_dist[u_idx][c_idx] < best_dist) {\n                best_dist = all_dist[u_idx][c_idx];\n                best_u_idx = u_idx;\n            }\n        }\n\n        if (best_u_idx == -1 || best_dist == INT_MAX) {\n            continue;\n        }\n\n        int detour_length = 2 * best_dist;\n        if (L0 + detour_length > 100000) {\n            continue;\n        }\n\n        if (occ[best_u_idx].empty()) {\n            continue;\n        }\n        int insert_move_index = occ[best_u_idx][0];\n\n        Cell best_u = get_cell(best_u_idx, N);\n        Cell c = get_cell(c_idx, N);\n        string moves_to_c = \"\";\n        int current_idx = best_u_idx;\n        while (current_idx != c_idx) {\n            int next_idx = parent_all[c_idx][current_idx];\n            if (next_idx == -1) break;\n            Cell cur_cell = get_cell(current_idx, N);\n            Cell next_cell = get_cell(next_idx, N);\n            char m = get_move_char(cur_cell, next_cell);\n            moves_to_c += m;\n            current_idx = next_idx;\n        }\n\n        string moves_back = \"\";\n        for (int i = moves_to_c.size() - 1; i >= 0; i--) {\n            moves_back += reverse_move(moves_to_c[i]);\n        }\n        string detour = moves_to_c + moves_back;\n\n        string new_route = current_route.substr(0, insert_move_index+1) + detour + current_route.substr(insert_move_index+1);\n        int new_L = new_route.size();\n\n        vector<Cell> new_positions;\n        vector<vector<int>> new_occ(total_cells);\n        Cell pos(0,0);\n        new_positions.push_back(pos);\n        for (int i = 0; i < new_route.size(); i++) {\n            char m = new_route[i];\n            Cell next = pos;\n            if (m == 'R') next.j++;\n            else if (m == 'D') next.i++;\n            else if (m == 'L') next.j--;\n            else if (m == 'U') next.i--;\n            new_positions.push_back(next);\n            int cell_idx = get_index(next.i, next.j, N);\n            new_occ[cell_idx].push_back(i);\n            pos = next;\n        }\n\n        current_route = new_route;\n        L0 = new_L;\n        for (int i = 0; i < total_cells; i++) {\n            freq[i] = new_occ[i].size();\n            if (freq[i] > 0) {\n                gap_est[i] = L0 * 1.0 / freq[i];\n            }\n        }\n        occ = new_occ;\n\n        priority_queue<pdi> new_pq;\n        for (int i = 0; i < total_cells; i++) {\n            if (freq[i] > 0) {\n                double new_priority = d[get_cell(i, N).i][get_cell(i, N).j] * gap_est[i] * gap_est[i];\n                new_pq.push({new_priority, i});\n            }\n        }\n        pq = new_pq;\n\n        insertion_count++;\n    }\n\n    cout << current_route << endl;\n\n    return 0;\n}","ahc028":"#include <iostream>\n#include <vector>\n#include <deque>\n#include <algorithm>\n#include <unordered_set>\n#include <climits>\n#include <tuple>\n#include <set>\n#include <cctype>\n#include <cmath>\n\nusing namespace std;\n\nconst int INF = 1e8;\nconst int N_grid = 15;\n\nvoid manhattan_transform(vector<vector<int>>& grid) {\n    int n = grid.size();\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            if (i > 0) grid[i][j] = min(grid[i][j], grid[i-1][j] + 1);\n            if (j > 0) grid[i][j] = min(grid[i][j], grid[i][j-1] + 1);\n        }\n    }\n    for (int i = n-1; i >=0; i--) {\n        for (int j = n-1; j >=0; j--) {\n            if (i+1 < n) grid[i][j] = min(grid[i][j], grid[i+1][j] + 1);\n            if (j+1 < n) grid[i][j] = min(grid[i][j], grid[i][j+1] + 1);\n        }\n    }\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M;\n    cin >> N >> M;\n    int s_i, s_j;\n    cin >> s_i >> s_j;\n\n    vector<string> grid(N);\n    for (int i = 0; i < N; i++) {\n        cin >> grid[i];\n    }\n\n    vector<string> targets;\n    for (int i = 0; i < M; i++) {\n        string t;\n        cin >> t;\n        targets.push_back(t);\n    }\n\n    unordered_set<string> target_set(targets.begin(), targets.end());\n    vector<vector<pair<int, int>>> positions(26);\n\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            char c = grid[i][j];\n            positions[c-'A'].push_back({i, j});\n        }\n    }\n\n    vector<vector<int>> d_table(26, vector<int>(26, INF));\n\n    for (int b = 0; b < 26; b++) {\n        vector<vector<int>> dist(N, vector<int>(N, INF));\n        for (auto [x, y] : positions[b]) {\n            dist[x][y] = 0;\n        }\n        manhattan_transform(dist);\n\n        for (int a = 0; a < 26; a++) {\n            int min_val = INF;\n            for (auto [x, y] : positions[a]) {\n                if (dist[x][y] < min_val) {\n                    min_val = dist[x][y];\n                }\n            }\n            d_table[a][b] = min_val;\n        }\n    }\n\n    int cur_x = s_i, cur_y = s_j;\n    vector<pair<int, int>> operations;\n    deque<char> buffer;\n    unordered_set<string> covered;\n\n    while (covered.size() < M && operations.size() < 5000) {\n        vector<int> min_dist_letter(26, INF);\n        for (int c = 0; c < 26; c++) {\n            for (auto [x, y] : positions[c]) {\n                int d = abs(cur_x - x) + abs(cur_y - y);\n                if (d < min_dist_letter[c]) {\n                    min_dist_letter[c] = d;\n                }\n            }\n        }\n\n        vector<pair<int, string>> candidates;\n        for (string t : targets) {\n            if (covered.find(t) != covered.end()) continue;\n            int lb = min_dist_letter[t[0]-'A'] \n                     + d_table[t[0]-'A'][t[1]-'A'] \n                     + d_table[t[1]-'A'][t[2]-'A'] \n                     + d_table[t[2]-'A'][t[3]-'A'] \n                     + d_table[t[3]-'A'][t[4]-'A'] \n                     + 5;\n            candidates.push_back({lb, t});\n        }\n\n        sort(candidates.begin(), candidates.end());\n        int K_candidate = min(10, (int)candidates.size());\n        vector<string> top_candidates;\n        for (int i = 0; i < K_candidate; i++) {\n            top_candidates.push_back(candidates[i].second);\n        }\n\n        string best_string = \"\";\n        vector<tuple<int, int>> best_path;\n        int best_cost = INF;\n\n        for (string t : top_candidates) {\n            vector<vector<vector<int>>> dp(5, vector<vector<int>>(N, vector<int>(N, INF)));\n            for (int x = 0; x < N; x++) {\n                for (int y = 0; y < N; y++) {\n                    if (grid[x][y] == t[0]) {\n                        dp[0][x][y] = abs(cur_x - x) + abs(cur_y - y) + 1;\n                    }\n                }\n            }\n\n            for (int k = 1; k < 5; k++) {\n                vector<vector<int>> g = dp[k-1];\n                manhattan_transform(g);\n                for (int x = 0; x < N; x++) {\n                    for (int y = 0; y < N; y++) {\n                        if (grid[x][y] == t[k]) {\n                            dp[k][x][y] = g[x][y] + 1;\n                        }\n                    }\n                }\n            }\n\n            int min_cost = INF;\n            int best_x4 = -1, best_y4 = -1;\n            for (int x = 0; x < N; x++) {\n                for (int y = 0; y < N; y++) {\n                    if (dp[4][x][y] < min_cost) {\n                        min_cost = dp[4][x][y];\n                        best_x4 = x;\n                        best_y4 = y;\n                    }\n                }\n            }\n\n            if (min_cost < best_cost) {\n                best_cost = min_cost;\n                best_string = t;\n                vector<tuple<int, int>> path(5);\n                path[4] = {best_x4, best_y4};\n                int cx = best_x4, cy = best_y4;\n                bool valid = true;\n                for (int k = 3; k >= 0; k--) {\n                    int best_x = -1, best_y = -1;\n                    int best_val = INF;\n                    for (int x = 0; x < N; x++) {\n                        for (int y = 0; y < N; y++) {\n                            if (grid[x][y] != t[k]) continue;\n                            if (dp[k][x][y] == INF) continue;\n                            int move_cost = abs(x - cx) + abs(y - cy);\n                            if (dp[k][x][y] + move_cost == dp[k+1][cx][cy] - 1) {\n                                best_x = x;\n                                best_y = y;\n                                best_val = dp[k][x][y];\n                                break;\n                            }\n                        }\n                        if (best_val != INF) break;\n                    }\n                    if (best_val == INF) {\n                        int min_diff = INF;\n                        for (int x = 0; x < N; x++) {\n                            for (int y = 0; y < N; y++) {\n                                if (grid[x][y] != t[k]) continue;\n                                if (dp[k][x][y] == INF) continue;\n                                int move_cost = abs(x - cx) + abs(y - cy);\n                                int total = dp[k][x][y] + move_cost;\n                                if (total < min_diff) {\n                                    min_diff = total;\n                                    best_x = x;\n                                    best_y = y;\n                                }\n                            }\n                        }\n                    }\n                    path[k] = {best_x, best_y};\n                    cx = best_x;\n                    cy = best_y;\n                }\n                best_path = path;\n            }\n        }\n\n        if (best_cost == INF) {\n            break;\n        }\n\n        for (int k = 0; k < 5; k++) {\n            int x = get<0>(best_path[k]);\n            int y = get<1>(best_path[k]);\n            operations.push_back({x, y});\n            cur_x = x;\n            cur_y = y;\n            buffer.push_back(grid[x][y]);\n            if (buffer.size() > 5) {\n                buffer.pop_front();\n            }\n            if (buffer.size() == 5) {\n                string gram = string(buffer.begin(), buffer.end());\n                if (target_set.find(gram) != target_set.end() && covered.find(gram) == covered.end()) {\n                    covered.insert(gram);\n                }\n            }\n            if (operations.size() >= 5000) {\n                break;\n            }\n        }\n    }\n\n    for (auto [x, y] : operations) {\n        cout << x << \" \" << y << \"\\n\";\n    }\n\n    return 0;\n}","ahc030":"#include <iostream>\n#include <vector>\n#include <cmath>\n#include <algorithm>\nusing namespace std;\n\nint main() {\n    int N, M;\n    double eps;\n    cin >> N >> M >> eps;\n\n    vector<vector<pair<int, int>>> fields;\n    for (int k = 0; k < M; k++) {\n        int d;\n        cin >> d;\n        vector<pair<int, int>> field;\n        for (int i = 0; i < d; i++) {\n            int x, y;\n            cin >> x >> y;\n            field.push_back({x, y});\n        }\n        fields.push_back(field);\n    }\n\n    vector<vector<int>> grid(N, vector<int>(N, -1));\n    vector<pair<int, int>> has_oil;\n    const int block_size = 5;\n\n    for (int i0 = 0; i0 < N; i0 += block_size) {\n        for (int j0 = 0; j0 < N; j0 += block_size) {\n            int i1 = min(i0 + block_size, N);\n            int j1 = min(j0 + block_size, N);\n            vector<pair<int, int>> block;\n            for (int i = i0; i < i1; i++) {\n                for (int j = j0; j < j1; j++) {\n                    block.push_back({i, j});\n                }\n            }\n            int k = block.size();\n            bool nonempty = false;\n            for (int trial = 0; trial < 3; trial++) {\n                cout << \"q \" << k;\n                for (auto [i, j] : block) {\n                    cout << \" \" << i << \" \" << j;\n                }\n                cout << endl;\n                int X;\n                cin >> X;\n                if (X >= 1) {\n                    nonempty = true;\n                    break;\n                }\n            }\n            if (nonempty) {\n                for (auto [i, j] : block) {\n                    cout << \"q 1 \" << i << \" \" << j << endl;\n                    int v;\n                    cin >> v;\n                    grid[i][j] = v;\n                    if (v > 0) {\n                        has_oil.push_back({i, j});\n                    }\n                }\n            } else {\n                for (auto [i, j] : block) {\n                    grid[i][j] = 0;\n                }\n            }\n        }\n    }\n\n    cout << \"a \" << has_oil.size();\n    for (auto [i, j] : has_oil) {\n        cout << \" \" << i << \" \" << j;\n    }\n    cout << endl;\n    int resp;\n    cin >> resp;\n\n    return 0;\n}","ahc031":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <cmath>\n#include <tuple>\n#include <climits>\nusing namespace std;\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int W, D, N;\n    cin >> W >> D >> N;\n    vector<vector<int>> a(D, vector<int>(N));\n    for (int d = 0; d < D; ++d) {\n        for (int k = 0; k < N; ++k) {\n            cin >> a[d][k];\n        }\n    }\n\n    for (int d = 0; d < D; ++d) {\n        vector<int> skyline(W, 0);\n        vector<tuple<int, int, int, int>> placements(N);\n\n        vector<pair<int, int>> events;\n        for (int k = 0; k < N; ++k) {\n            events.push_back({a[d][k], k});\n        }\n        sort(events.begin(), events.end(), [](const auto &x, const auto &y) {\n            return x.first > y.first;\n        });\n\n        for (const auto& event : events) {\n            int area = event.first;\n            int idx = event.second;\n            int w = ceil(sqrt(area));\n            int h = (area + w - 1) / w;\n\n            bool placed = false;\n            for (int orientation = 0; orientation < 2; ++orientation) {\n                for (int start_col = 0; start_col <= W - w; ++start_col) {\n                    int base = 0;\n                    for (int col = start_col; col < start_col + w; ++col) {\n                        if (skyline[col] > base) {\n                            base = skyline[col];\n                        }\n                    }\n                    if (base + h <= W) {\n                        for (int col = start_col; col < start_col + w; ++col) {\n                            skyline[col] = base + h;\n                        }\n                        placements[idx] = make_tuple(base, start_col, base + h, start_col + w);\n                        placed = true;\n                        break;\n                    }\n                }\n                if (placed) break;\n                swap(w, h);\n            }\n\n            if (!placed) {\n                int h_emerg = (area + W - 1) / W;\n                int w_emerg = W;\n                int base = 0;\n                for (int col = 0; col < W; ++col) {\n                    if (skyline[col] > base) {\n                        base = skyline[col];\n                    }\n                }\n                if (base + h_emerg > W) {\n                    h_emerg = W - base;\n                }\n                for (int col = 0; col < W; ++col) {\n                    skyline[col] = base + h_emerg;\n                }\n                placements[idx] = make_tuple(base, 0, base + h_emerg, w_emerg);\n            }\n        }\n\n        for (int k = 0; k < N; ++k) {\n            auto [i0, j0, i1, j1] = placements[k];\n            cout << i0 << \" \" << j0 << \" \" << i1 << \" \" << j1 << \"\\n\";\n        }\n    }\n\n    return 0;\n}","ahc032":"#include <iostream>\n#include <vector>\n#include <tuple>\n#include <algorithm>\n#include <climits>\nusing namespace std;\n\nconst long long mod = 998244353;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, K;\n    cin >> N >> M >> K;\n\n    vector<vector<long long>> grid(N, vector<long long>(N));\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cin >> grid[i][j];\n        }\n    }\n\n    vector<vector<vector<long long>>> stamps(M, vector<vector<long long>>(3, vector<long long>(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 >> stamps[m][i][j];\n            }\n        }\n    }\n\n    vector<tuple<int, int, int>> operations;\n\n    for (int op = 0; op < K; op++) {\n        long long best_delta = LLONG_MIN;\n        int best_m = -1, best_p = -1, best_q = -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                    long long delta = 0;\n                    for (int i = 0; i < 3; i++) {\n                        for (int j = 0; j < 3; j++) {\n                            long long old_val = grid[p+i][q+j];\n                            long long add = stamps[m][i][j];\n                            long long new_val = old_val + add;\n                            if (new_val >= mod) {\n                                new_val -= mod;\n                            }\n                            delta += (new_val - old_val);\n                        }\n                    }\n                    if (delta > best_delta) {\n                        best_delta = delta;\n                        best_m = m;\n                        best_p = p;\n                        best_q = q;\n                    }\n                }\n            }\n        }\n\n        if (best_delta <= 0) {\n            break;\n        }\n\n        for (int i = 0; i < 3; i++) {\n            for (int j = 0; j < 3; j++) {\n                grid[best_p+i][best_q+j] += stamps[best_m][i][j];\n                if (grid[best_p+i][best_q+j] >= mod) {\n                    grid[best_p+i][best_q+j] -= mod;\n                }\n            }\n        }\n\n        operations.push_back(make_tuple(best_m, best_p, best_q));\n    }\n\n    cout << operations.size() << '\\n';\n    for (const auto& op : operations) {\n        cout << get<0>(op) << ' ' << get<1>(op) << ' ' << get<2>(op) << '\\n';\n    }\n\n    return 0;\n}","ahc033":"#include <iostream>\n#include <vector>\n#include <string>\n#include <algorithm>\nusing namespace std;\n\nvoid move_to(string& s, int& cr, int& cc, int tr, int tc, int& turn_count) {\n    while (cr < tr) {\n        s += 'D';\n        cr++;\n        turn_count++;\n    }\n    while (cr > tr) {\n        s += 'U';\n        cr--;\n        turn_count++;\n    }\n    while (cc < tc) {\n        s += 'R';\n        cc++;\n        turn_count++;\n    }\n    while (cc > tc) {\n        s += 'L';\n        cc--;\n        turn_count++;\n    }\n}\n\nint main() {\n    int N;\n    cin >> N;\n    vector<vector<int>> A(5, vector<int>(5));\n    for (int i = 0; i < 5; i++) {\n        for (int j = 0; j < 5; j++) {\n            cin >> A[i][j];\n        }\n    }\n\n    vector<string> crane_actions(5);\n    for (int i = 1; i < 5; i++) {\n        crane_actions[i] = \"RB\";\n    }\n\n    string large_actions = \"R\";\n    int turn_count = 1;\n    int cr = 0, cc = 1;\n\n    vector<int> next_exp = {0, 5, 10, 15, 20};\n    vector<int> count_nonnext(5, 0);\n\n    for (int i = 0; i < 5; i++) {\n        for (int j = 0; j < 5; j++) {\n            move_to(large_actions, cr, cc, i, 1, turn_count);\n            large_actions += 'L';\n            turn_count++;\n            cr = i;\n            cc = 0;\n\n            large_actions += 'P';\n            turn_count++;\n\n            int x = A[i][j];\n            int r = x / 5;\n            int target_col;\n            if (x == next_exp[r]) {\n                target_col = 4;\n                next_exp[r]++;\n            } else {\n                if (count_nonnext[r] < 2) {\n                    target_col = (count_nonnext[r] % 2 == 0) ? 2 : 3;\n                    count_nonnext[r]++;\n                } else {\n                    target_col = 4;\n                }\n            }\n\n            move_to(large_actions, cr, cc, r, target_col, turn_count);\n            cr = r;\n            cc = target_col;\n\n            large_actions += 'Q';\n            turn_count++;\n\n            move_to(large_actions, cr, cc, i, 1, turn_count);\n            cr = i;\n            cc = 1;\n        }\n    }\n\n    for (int r = 0; r < 5; r++) {\n        for (int col : {2, 3}) {\n            move_to(large_actions, cr, cc, r, col, turn_count);\n            large_actions += 'P';\n            turn_count++;\n            move_to(large_actions, cr, cc, r, 4, turn_count);\n            large_actions += 'Q';\n            turn_count++;\n        }\n    }\n\n    crane_actions[0] = large_actions;\n    size_t max_len = crane_actions[0].size();\n    for (int i = 1; i < 5; i++) {\n        if (crane_actions[i].size() < max_len) {\n            crane_actions[i] += string(max_len - crane_actions[i].size(), '.');\n        }\n    }\n\n    for (int i = 0; i < 5; i++) {\n        cout << crane_actions[i] << endl;\n    }\n\n    return 0;\n}","ahc034":"#include <iostream>\n#include <vector>\n#include <string>\nusing namespace std;\n\nint main() {\n    int N;\n    cin >> N;\n    vector<vector<int>> grid(N, vector<int>(N));\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cin >> grid[i][j];\n        }\n    }\n\n    vector<string> operations;\n    int x = 0, y = 0;\n\n    if (grid[0][0] > 0) {\n        operations.push_back(\"+\" + to_string(grid[0][0]));\n    }\n\n    for (int i = 0; i < N; i++) {\n        if (i % 2 == 0) {\n            for (int j = 0; j < N-1; j++) {\n                operations.push_back(\"R\");\n                y++;\n                if (grid[x][y] > 0) {\n                    operations.push_back(\"+\" + to_string(grid[x][y]));\n                }\n            }\n        } else {\n            for (int j = 0; j < N-1; j++) {\n                operations.push_back(\"L\");\n                y--;\n                if (grid[x][y] > 0) {\n                    operations.push_back(\"+\" + to_string(grid[x][y]));\n                }\n            }\n        }\n        if (i < N-1) {\n            operations.push_back(\"D\");\n            x++;\n            if (grid[x][y] > 0) {\n                operations.push_back(\"+\" + to_string(grid[x][y]));\n            }\n        }\n    }\n\n    for (int i = 0; i < x; i++) {\n        operations.push_back(\"U\");\n    }\n    for (int j = 0; j < y; j++) {\n        operations.push_back(\"L\");\n    }\n\n    x = 0;\n    y = 0;\n    if (grid[0][0] < 0) {\n        operations.push_back(\"-\" + to_string(-grid[0][0]));\n    }\n\n    for (int i = 0; i < N; i++) {\n        if (i % 2 == 0) {\n            for (int j = 0; j < N-1; j++) {\n                operations.push_back(\"R\");\n                y++;\n                if (grid[x][y] < 0) {\n                    operations.push_back(\"-\" + to_string(-grid[x][y]));\n                }\n            }\n        } else {\n            for (int j = 0; j < N-1; j++) {\n                operations.push_back(\"L\");\n                y--;\n                if (grid[x][y] < 0) {\n                    operations.push_back(\"-\" + to_string(-grid[x][y]));\n                }\n            }\n        }\n        if (i < N-1) {\n            operations.push_back(\"D\");\n            x++;\n            if (grid[x][y] < 0) {\n                operations.push_back(\"-\" + to_string(-grid[x][y]));\n            }\n        }\n    }\n\n    for (const string& op : operations) {\n        cout << op << endl;\n    }\n\n    return 0;\n}","ahc035":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <set>\n#include <utility>\n\nusing namespace std;\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, T;\n    cin >> N >> M >> T;\n\n    const int seed_count = 2 * N * (N - 1);\n    vector<vector<int>> seeds(seed_count, vector<int>(M, 0));\n\n    for (int i = 0; i < seed_count; i++) {\n        for (int j = 0; j < M; j++) {\n            cin >> seeds[i][j];\n        }\n    }\n\n    for (int t = 0; t < T; t++) {\n        vector<int> current_max(M, -1);\n        for (int l = 0; l < M; l++) {\n            for (int k = 0; k < seed_count; k++) {\n                if (seeds[k][l] > current_max[l]) {\n                    current_max[l] = seeds[k][l];\n                }\n            }\n        }\n\n        vector<int> V(seed_count, 0);\n        for (int k = 0; k < seed_count; k++) {\n            for (int l = 0; l < M; l++) {\n                V[k] += seeds[k][l];\n            }\n        }\n\n        vector<vector<bool>> trait(seed_count, vector<bool>(M, false));\n        for (int k = 0; k < seed_count; k++) {\n            for (int l = 0; l < M; l++) {\n                if (seeds[k][l] == current_max[l]) {\n                    trait[k][l] = true;\n                }\n            }\n        }\n\n        vector<int> coverage_count(M, 0);\n        set<int> chosen_seeds;\n\n        for (int l = 0; l < M; l++) {\n            if (coverage_count[l] > 0) continue;\n            int best_k = -1;\n            int best_V = -1;\n            for (int k = 0; k < seed_count; k++) {\n                if (chosen_seeds.find(k) != chosen_seeds.end()) continue;\n                if (trait[k][l]) {\n                    if (V[k] > best_V) {\n                        best_V = V[k];\n                        best_k = k;\n                    }\n                }\n            }\n            if (best_k != -1) {\n                chosen_seeds.insert(best_k);\n                for (int ll = 0; ll < M; ll++) {\n                    if (trait[best_k][ll]) {\n                        coverage_count[ll]++;\n                    }\n                }\n            }\n        }\n\n        for (int l = 0; l < M; l++) {\n            if (coverage_count[l] >= 2) continue;\n            int best_k = -1;\n            int best_V = -1;\n            for (int k = 0; k < seed_count; k++) {\n                if (chosen_seeds.find(k) != chosen_seeds.end()) continue;\n                if (trait[k][l]) {\n                    if (V[k] > best_V) {\n                        best_V = V[k];\n                        best_k = k;\n                    }\n                }\n            }\n            if (best_k != -1) {\n                chosen_seeds.insert(best_k);\n                for (int ll = 0; ll < M; ll++) {\n                    if (trait[best_k][ll]) {\n                        coverage_count[ll]++;\n                    }\n                }\n            }\n        }\n\n        if (chosen_seeds.size() < N * N) {\n            vector<pair<int, int>> candidates;\n            for (int k = 0; k < seed_count; k++) {\n                if (chosen_seeds.find(k) == chosen_seeds.end()) {\n                    candidates.push_back({V[k], k});\n                }\n            }\n            sort(candidates.begin(), candidates.end(), [](const pair<int, int>& a, const pair<int, int>& b) {\n                return a.first > b.first;\n            });\n            int rem = N * N - chosen_seeds.size();\n            for (int i = 0; i < rem; i++) {\n                chosen_seeds.insert(candidates[i].second);\n            }\n        }\n\n        int half1 = M / 2;\n        vector<pair<int, int>> seeds_with_signature;\n        for (int k : chosen_seeds) {\n            int score1 = 0, score2 = 0;\n            for (int l = 0; l < half1; l++) {\n                if (trait[k][l]) score1++;\n            }\n            for (int l = half1; l < M; l++) {\n                if (trait[k][l]) score2++;\n            }\n            int sig = score1 - score2;\n            seeds_with_signature.push_back({sig, k});\n        }\n\n        sort(seeds_with_signature.begin(), seeds_with_signature.end(), [](const pair<int, int>& a, const pair<int, int>& b) {\n            return a.first > b.first;\n        });\n\n        vector<int> groupA, groupB;\n        for (int i = 0; i < seeds_with_signature.size(); i++) {\n            if (i < 18) {\n                groupA.push_back(seeds_with_signature[i].second);\n            } else {\n                groupB.push_back(seeds_with_signature[i].second);\n            }\n        }\n\n        sort(groupA.begin(), groupA.end(), [&](int a, int b) { return V[a] > V[b]; });\n        sort(groupB.begin(), groupB.end(), [&](int a, int b) { return V[a] > V[b]; });\n\n        vector<vector<int>> grid(N, vector<int>(N, -1));\n        int idxA = 0, idxB = 0;\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                if ((i + j) % 2 == 0) {\n                    grid[i][j] = groupA[idxA++];\n                } else {\n                    grid[i][j] = groupB[idxB++];\n                }\n            }\n        }\n\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                cout << grid[i][j];\n                if (j < N - 1) {\n                    cout << \" \";\n                } else {\n                    cout << endl;\n                }\n            }\n        }\n        cout.flush();\n\n        for (int i = 0; i < seed_count; i++) {\n            for (int j = 0; j < M; j++) {\n                cin >> seeds[i][j];\n            }\n        }\n    }\n\n    return 0;\n}","ahc038":"#include <iostream>\n#include <vector>\n#include <cstdlib>\n#include <algorithm>\n#include <climits>\n#include <tuple>\nusing namespace std;\n\nint main() {\n    int N, M, V;\n    cin >> N >> M >> V;\n\n    vector<string> s_grid(N), t_grid(N);\n    for (int i = 0; i < N; i++) {\n        cin >> s_grid[i];\n    }\n    for (int i = 0; i < N; i++) {\n        cin >> t_grid[i];\n    }\n\n    int L0 = (N - 1) / 2;\n\n    cout << V << endl;\n    for (int i = 1; i < V; i++) {\n        cout << \"0 \" << L0 << endl;\n    }\n\n    int rx0 = N / 2;\n    int ry0 = N / 2;\n    cout << rx0 << \" \" << ry0 << endl;\n\n    int cur_x = rx0, cur_y = ry0;\n    vector<int> dirs(V, 0);\n    vector<bool> holding(V, false);\n\n    vector<pair<int, int>> initial_list;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (s_grid[i][j] == '1') {\n                initial_list.push_back({i, j});\n            }\n        }\n    }\n\n    vector<pair<int, int>> target_list;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (t_grid[i][j] == '1') {\n                target_list.push_back({i, j});\n            }\n        }\n    }\n\n    vector<string> outputs;\n\n    while (!initial_list.empty()) {\n        int F_available = V - 1;\n        int batch_size = min(F_available, (int)initial_list.size());\n        vector<tuple<int, int, int, int, int, int>> pickup_batch;\n        vector<int> available_fingertips;\n        for (int i = 1; i < V; i++) {\n            available_fingertips.push_back(i);\n        }\n\n        int current_start_x = cur_x;\n        int current_start_y = cur_y;\n\n        for (int idx = 0; idx < batch_size; idx++) {\n            int best_cost = INT_MAX;\n            int best_i = -1, best_j = -1, best_cx = -1, best_cy = -1, best_d = -1, best_f = -1;\n            for (int k = 0; k < initial_list.size(); k++) {\n                int i = initial_list[k].first;\n                int j = initial_list[k].second;\n                vector<tuple<int, int, int>> candidates;\n                if (j - L0 >= 0) {\n                    candidates.push_back(make_tuple(i, j - L0, 0));\n                }\n                if (i - L0 >= 0) {\n                    candidates.push_back(make_tuple(i - L0, j, 1));\n                }\n                if (j + L0 < N) {\n                    candidates.push_back(make_tuple(i, j + L0, 2));\n                }\n                if (i + L0 < N) {\n                    candidates.push_back(make_tuple(i + L0, j, 3));\n                }\n                for (auto &cand : candidates) {\n                    int cx = get<0>(cand);\n                    int cy = get<1>(cand);\n                    int d = get<2>(cand);\n                    for (int f_index = 0; f_index < available_fingertips.size(); f_index++) {\n                        int f = available_fingertips[f_index];\n                        int dist = abs(cx - current_start_x) + abs(cy - current_start_y);\n                        int cur_dir = dirs[f];\n                        int diff = (d - cur_dir + 4) % 4;\n                        int rot_cost = min(diff, 4 - diff);\n                        int cost = dist + rot_cost;\n                        if (cost < best_cost) {\n                            best_cost = cost;\n                            best_i = i;\n                            best_j = j;\n                            best_cx = cx;\n                            best_cy = cy;\n                            best_d = d;\n                            best_f = f;\n                        }\n                    }\n                }\n            }\n            if (best_cost == INT_MAX) {\n                batch_size = idx;\n                break;\n            }\n\n            for (auto it = initial_list.begin(); it != initial_list.end(); it++) {\n                if (it->first == best_i && it->second == best_j) {\n                    initial_list.erase(it);\n                    break;\n                }\n            }\n\n            for (auto it = available_fingertips.begin(); it != available_fingertips.end(); it++) {\n                if (*it == best_f) {\n                    available_fingertips.erase(it);\n                    break;\n                }\n            }\n\n            pickup_batch.push_back(make_tuple(best_i, best_j, best_cx, best_cy, best_d, best_f));\n            current_start_x = best_cx;\n            current_start_y = best_cy;\n        }\n\n        for (auto &item : pickup_batch) {\n            int i = get<0>(item);\n            int j = get<1>(item);\n            int cx = get<2>(item);\n            int cy = get<3>(item);\n            int d = get<4>(item);\n            int f = get<5>(item);\n\n            int dx = cx - cur_x;\n            int dy = cy - cur_y;\n\n            vector<char> moves;\n            if (dx > 0) {\n                for (int k = 0; k < dx; k++) moves.push_back('R');\n            } else if (dx < 0) {\n                for (int k = 0; k < -dx; k++) moves.push_back('L');\n            }\n            if (dy > 0) {\n                for (int k = 0; k < dy; k++) moves.push_back('D');\n            } else if (dy < 0) {\n                for (int k = 0; k < -dy; k++) moves.push_back('U');\n            }\n\n            int cur_dir = dirs[f];\n            int diff = (d - cur_dir + 4) % 4;\n            vector<char> rots;\n            if (diff == 1) {\n                rots.push_back('R');\n            } else if (diff == 2) {\n                rots.push_back('R');\n                rots.push_back('R');\n            } else if (diff == 3) {\n                rots.push_back('L');\n            }\n\n            int T = max(moves.size(), rots.size());\n            for (int t = 0; t < T; t++) {\n                string op = \"\";\n                if (t < moves.size()) {\n                    op += moves[t];\n                } else {\n                    op += '.';\n                }\n\n                for (int v = 1; v < V; v++) {\n                    if (v == f && t < rots.size()) {\n                        op += rots[t];\n                    } else {\n                        op += '.';\n                    }\n                }\n\n                for (int v = 0; v < V; v++) {\n                    if (v == f && t == T - 1) {\n                        op += 'P';\n                        holding[f] = true;\n                    } else {\n                        op += '.';\n                    }\n                }\n                outputs.push_back(op);\n            }\n\n            cur_x = cx;\n            cur_y = cy;\n            dirs[f] = d;\n        }\n\n        vector<pair<int, int>> available_targets = target_list;\n        vector<tuple<int, int, int, int, int, int>> delivery_batch;\n        current_start_x = cur_x;\n        current_start_y = cur_y;\n\n        for (int idx = 0; idx < batch_size; idx++) {\n            int best_cost = INT_MAX;\n            int best_i = -1, best_j = -1, best_cx = -1, best_cy = -1, best_d = -1, best_f = -1;\n            for (int k = 0; k < available_targets.size(); k++) {\n                int i = available_targets[k].first;\n                int j = available_targets[k].second;\n                vector<tuple<int, int, int>> candidates;\n                if (j - L0 >= 0) {\n                    candidates.push_back(make_tuple(i, j - L0, 0));\n                }\n                if (i - L0 >= 0) {\n                    candidates.push_back(make_tuple(i - L0, j, 1));\n                }\n                if (j + L0 < N) {\n                    candidates.push_back(make_tuple(i, j + L0, 2));\n                }\n                if (i + L0 < N) {\n                    candidates.push_back(make_tuple(i + L0, j, 3));\n                }\n                for (auto &cand : candidates) {\n                    int cx = get<0>(cand);\n                    int cy = get<1>(cand);\n                    int d = get<2>(cand);\n                    for (auto &p_item : pickup_batch) {\n                        int f = get<5>(p_item);\n                        if (holding[f]) {\n                            int dist = abs(cx - current_start_x) + abs(cy - current_start_y);\n                            int cur_dir = dirs[f];\n                            int diff = (d - cur_dir + 4) % 4;\n                            int rot_cost = min(diff, 4 - diff);\n                            int cost = dist + rot_cost;\n                            if (cost < best_cost) {\n                                best_cost = cost;\n                                best_i = i;\n                                best_j = j;\n                                best_cx = cx;\n                                best_cy = cy;\n                                best_d = d;\n                                best_f = f;\n                            }\n                        }\n                    }\n                }\n            }\n            if (best_cost == INT_MAX) {\n                batch_size = idx;\n                break;\n            }\n\n            for (auto it = available_targets.begin(); it != available_targets.end(); it++) {\n                if (it->first == best_i && it->second == best_j) {\n                    available_targets.erase(it);\n                    break;\n                }\n            }\n\n            delivery_batch.push_back(make_tuple(best_i, best_j, best_cx, best_cy, best_d, best_f));\n            current_start_x = best_cx;\n            current_start_y = best_cy;\n        }\n\n        for (auto &item : delivery_batch) {\n            int i = get<0>(item);\n            int j = get<1>(item);\n            int cx = get<2>(item);\n            int cy = get<3>(item);\n            int d = get<4>(item);\n            int f = get<5>(item);\n\n            int dx = cx - cur_x;\n            int dy = cy - cur_y;\n\n            vector<char> moves;\n            if (dx > 0) {\n                for (int k = 0; k < dx; k++) moves.push_back('R');\n            } else if (dx < 0) {\n                for (int k = 0; k < -dx; k++) moves.push_back('L');\n            }\n            if (dy > 0) {\n                for (int k = 0; k < dy; k++) moves.push_back('D');\n            } else if (dy < 0) {\n                for (int k = 0; k < -dy; k++) moves.push_back('U');\n            }\n\n            int cur_dir = dirs[f];\n            int diff = (d - cur_dir + 4) % 4;\n            vector<char> rots;\n            if (diff == 1) {\n                rots.push_back('R');\n            } else if (diff == 2) {\n                rots.push_back('R');\n                rots.push_back('R');\n            } else if (diff == 3) {\n                rots.push_back('L');\n            }\n\n            int T = max(moves.size(), rots.size());\n            for (int t = 0; t < T; t++) {\n                string op = \"\";\n                if (t < moves.size()) {\n                    op += moves[t];\n                } else {\n                    op += '.';\n                }\n\n                for (int v = 1; v < V; v++) {\n                    if (v == f && t < rots.size()) {\n                        op += rots[t];\n                    } else {\n                        op += '.';\n                    }\n                }\n\n                for (int v = 0; v < V; v++) {\n                    if (v == f && t == T - 1) {\n                        op += 'P';\n                        holding[f] = false;\n                    } else {\n                        op += '.';\n                    }\n                }\n                outputs.push_back(op);\n            }\n\n            cur_x = cx;\n            cur_y = cy;\n            dirs[f] = d;\n        }\n\n        for (auto &item : delivery_batch) {\n            int i = get<0>(item);\n            int j = get<1>(item);\n            for (auto it = target_list.begin(); it != target_list.end(); it++) {\n                if (it->first == i && it->second == j) {\n                    target_list.erase(it);\n                    break;\n                }\n            }\n        }\n    }\n\n    for (string &s : outputs) {\n        cout << s << endl;\n    }\n\n    return 0;\n}","ahc039":"#include <iostream>\n#include <vector>\n#include <set>\n#include <algorithm>\n#include <cctype>\n#include <cmath>\n#include <queue>\n#include <map>\n#include <unordered_set>\nusing namespace std;\n\nstruct Point {\n    int x, y;\n    Point(int x=0, int y=0) : x(x), y(y) {}\n};\n\nconst int GRID = 15;\n\nint main() {\n    int N;\n    cin >> N;\n    vector<Point> mackerels;\n    vector<Point> sardines;\n    for (int i = 0; i < 2*N; i++) {\n        int x, y;\n        cin >> x >> y;\n        if (i < N) {\n            mackerels.push_back(Point(x, y));\n        } else {\n            sardines.push_back(Point(x, y));\n        }\n    }\n\n    set<pair<int, int>> sardine_set;\n    for (auto& p : sardines) {\n        sardine_set.insert({p.x, p.y});\n    }\n\n    vector<int> x_grid(GRID+1);\n    vector<int> y_grid(GRID+1);\n    for (int i = 0; i <= GRID; i++) {\n        x_grid[i] = (long long)i * 100000 / GRID;\n        y_grid[i] = (long long)i * 100000 / GRID;\n    }\n    x_grid[GRID] = 100000;\n    y_grid[GRID] = 100000;\n\n    vector<vector<int>> grid_m(GRID, vector<int>(GRID, 0));\n    vector<vector<int>> grid_s(GRID, vector<int>(GRID, 0));\n    vector<vector<int>> grid_count(GRID, vector<int>(GRID, 0));\n\n    for (auto& p : mackerels) {\n        int i = lower_bound(x_grid.begin(), x_grid.end(), p.x + 1) - x_grid.begin() - 1;\n        int j = lower_bound(y_grid.begin(), y_grid.end(), p.y + 1) - y_grid.begin() - 1;\n        if (i < 0) i = 0;\n        if (i >= GRID) i = GRID-1;\n        if (j < 0) j = 0;\n        if (j >= GRID) j = GRID-1;\n        grid_m[i][j]++;\n    }\n\n    for (auto& p : sardines) {\n        int i = lower_bound(x_grid.begin(), x_grid.end(), p.x + 1) - x_grid.begin() - 1;\n        int j = lower_bound(y_grid.begin(), y_grid.end(), p.y + 1) - y_grid.begin() - 1;\n        if (i < 0) i = 0;\n        if (i >= GRID) i = GRID-1;\n        if (j < 0) j = 0;\n        if (j >= GRID) j = GRID-1;\n        grid_s[i][j]++;\n    }\n\n    for (int i = 0; i < GRID; i++) {\n        for (int j = 0; j < GRID; j++) {\n            grid_count[i][j] = grid_m[i][j] - grid_s[i][j];\n        }\n    }\n\n    int best_score = -1000000;\n    int best_i = -1, best_j = -1;\n    for (int i = 0; i < GRID; i++) {\n        for (int j = 0; j < GRID; j++) {\n            if (grid_count[i][j] > best_score) {\n                best_score = grid_count[i][j];\n                best_i = i;\n                best_j = j;\n            }\n        }\n    }\n\n    if (best_score <= 0) {\n        int best_cnt = 10;\n        Point best_point;\n        bool found = false;\n        for (auto& p : mackerels) {\n            int cnt = 0;\n            for (int dx = -1; dx <= 1; dx++) {\n                for (int dy = -1; dy <= 1; dy++) {\n                    int x1 = p.x + dx;\n                    int y1 = p.y + dy;\n                    if (x1 < 0 || x1 > 100000 || y1 < 0 || y1 > 100000) continue;\n                    if (sardine_set.find({x1, y1}) != sardine_set.end()) {\n                        cnt++;\n                    }\n                }\n            }\n            if (cnt < best_cnt) {\n                best_cnt = cnt;\n                best_point = p;\n                found = true;\n            }\n        }\n        if (!found) {\n            best_point = mackerels[0];\n        }\n        int x0 = best_point.x;\n        int y0 = best_point.y;\n        vector<Point> poly;\n        poly.push_back(Point(x0-1, y0-1));\n        poly.push_back(Point(x0-1, y0+1));\n        poly.push_back(Point(x0+1, y0+1));\n        poly.push_back(Point(x0+1, y0-1));\n        cout << 4 << \"\\n\";\n        for (auto& p : poly) {\n            cout << p.x << \" \" << p.y << \"\\n\";\n        }\n        return 0;\n    }\n\n    set<pair<int, int>> R;\n    set<pair<int, int>> frontier;\n    R.insert({best_i, best_j});\n    int total_perimeter = 0;\n    const int dx4[4] = {0, 1, 0, -1};\n    const int dy4[4] = {1, 0, -1, 0};\n\n    int dx_cell = x_grid[best_i+1] - x_grid[best_i];\n    int dy_cell = y_grid[best_j+1] - y_grid[best_j];\n    total_perimeter = 2 * (dx_cell + dy_cell);\n\n    for (int d = 0; d < 4; d++) {\n        int ni = best_i + dx4[d];\n        int nj = best_j + dy4[d];\n        if (ni < 0 || ni >= GRID || nj < 0 || nj >= GRID) continue;\n        if (R.find({ni, nj}) == R.end()) {\n            frontier.insert({ni, nj});\n        }\n    }\n\n    while (!frontier.empty()) {\n        int best_cell_i = -1, best_cell_j = -1;\n        int best_cell_score = -1000000;\n        int best_change_perimeter = 0;\n        vector<pair<int, int>> to_remove;\n        for (auto cell : frontier) {\n            int i = cell.first;\n            int j = cell.second;\n            if (R.find(cell) != R.end()) {\n                to_remove.push_back(cell);\n                continue;\n            }\n            int dx_local = x_grid[i+1] - x_grid[i];\n            int dy_local = y_grid[j+1] - y_grid[j];\n            int change = 0;\n            for (int d = 0; d < 4; d++) {\n                int ni = i + dx4[d];\n                int nj = j + dy4[d];\n                if (ni < 0 || ni >= GRID || nj < 0 || nj >= GRID) {\n                    if (d == 0) change += dy_local;\n                    else if (d == 1) change += dx_local;\n                    else if (d == 2) change += dy_local;\n                    else if (d == 3) change += dx_local;\n                    continue;\n                }\n                if (R.find({ni, nj}) != R.end()) {\n                    if (d == 0) change -= dy_local;\n                    else if (d == 1) change -= dx_local;\n                    else if (d == 2) change -= dy_local;\n                    else if (d == 3) change -= dx_local;\n                } else {\n                    if (d == 0) change += dy_local;\n                    else if (d == 1) change += dx_local;\n                    else if (d == 2) change += dy_local;\n                    else if (d == 3) change += dx_local;\n                }\n            }\n            int new_perimeter = total_perimeter + change;\n            if (new_perimeter > 400000) continue;\n            if (grid_count[i][j] > best_cell_score) {\n                best_cell_score = grid_count[i][j];\n                best_cell_i = i;\n                best_cell_j = j;\n                best_change_perimeter = change;\n            }\n        }\n        for (auto& cell : to_remove) {\n            frontier.erase(cell);\n        }\n        if (best_cell_score <= 0) {\n            break;\n        }\n        if (best_cell_i == -1) {\n            break;\n        }\n        R.insert({best_cell_i, best_cell_j});\n        total_perimeter += best_change_perimeter;\n        frontier.erase({best_cell_i, best_cell_j});\n        for (int d = 0; d < 4; d++) {\n            int ni = best_cell_i + dx4[d];\n            int nj = best_cell_j + dy4[d];\n            if (ni < 0 || ni >= GRID || nj < 0 || nj >= GRID) continue;\n            if (R.find({ni, nj}) == R.end()) {\n                frontier.insert({ni, nj});\n            }\n        }\n    }\n\n    int min_x = 100000, max_x = 0, min_y = 100000, max_y = 0;\n    for (auto cell : R) {\n        int i = cell.first;\n        int j = cell.second;\n        min_x = min(min_x, x_grid[i]);\n        max_x = max(max_x, x_grid[i+1]);\n        min_y = min(min_y, y_grid[j]);\n        max_y = max(max_y, y_grid[j+1]);\n    }\n    vector<Point> poly;\n    poly.push_back(Point(min_x, max_y));\n    poly.push_back(Point(max_x, max_y));\n    poly.push_back(Point(max_x, min_y));\n    poly.push_back(Point(min_x, min_y));\n    cout << 4 << \"\\n\";\n    for (auto& p : poly) {\n        cout << p.x << \" \" << p.y << \"\\n\";\n    }\n\n    return 0;\n}","ahc040":"#include <iostream>\n#include <vector>\n#include <set>\n#include <map>\n#include <algorithm>\n#include <cmath>\n#include <cctype>\n#include <tuple>\n#include <iomanip>\n\nusing namespace std;\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, T;\n    double sigma;\n    cin >> N >> T >> sigma;\n\n    vector<double> w_est(N);\n    vector<double> h_est(N);\n    vector<int> w_count(N, 0);\n    vector<int> h_count(N, 0);\n\n    for (int i = 0; i < N; i++) {\n        cin >> w_est[i] >> h_est[i];\n    }\n\n    vector<int> exploration_order(N);\n    for (int i = 0; i < N; i++) {\n        exploration_order[i] = i;\n    }\n    sort(exploration_order.begin(), exploration_order.end(), [&](int i, int j) {\n        return w_est[i] * h_est[i] > w_est[j] * h_est[j];\n    });\n\n    for (int turn = 0; turn < T; turn++) {\n        if (turn < N) {\n            int i = exploration_order[turn];\n            cout << 1 << '\\n';\n            cout << i << \" \" << 0 << \" \" << \"U\" << \" \" << -1 << '\\n';\n            cout.flush();\n\n            long long Wp, Hp;\n            cin >> Wp >> Hp;\n\n            if (w_count[i] == 0) {\n                w_est[i] = Wp;\n                h_est[i] = Hp;\n            } else {\n                w_est[i] = (w_est[i] * w_count[i] + Wp) / (w_count[i] + 1);\n                h_est[i] = (h_est[i] * h_count[i] + Hp) / (h_count[i] + 1);\n            }\n            w_count[i]++;\n            h_count[i]++;\n        } else {\n            vector<tuple<int, int, char, int>> sequence;\n            set<int> placed_set;\n            map<int, tuple<long long, long long, long long, long long>> rect_by_index;\n            long long current_max_x = 0;\n            long long current_max_y = 0;\n\n            vector<long long> w_sim(N), h_sim(N);\n            for (int i = 0; i < N; i++) {\n                w_sim[i] = static_cast<long long>(round(w_est[i]));\n                h_sim[i] = static_cast<long long>(round(h_est[i]));\n                if (w_sim[i] < 1) w_sim[i] = 1;\n                if (w_sim[i] > 1000000000) w_sim[i] = 1000000000;\n                if (h_sim[i] < 1) h_sim[i] = 1;\n                if (h_sim[i] > 1000000000) h_sim[i] = 1000000000;\n            }\n\n            int last_placed_index = -1;\n\n            for (int i = 0; i < N; i++) {\n                vector<tuple<long long, long long, int>> rotations;\n                rotations.emplace_back(w_sim[i], h_sim[i], 0);\n                rotations.emplace_back(h_sim[i], w_sim[i], 1);\n\n                long long best_bbox_sum = 1LL << 60;\n                tuple<int, char, int, long long, long long, long long, long long> best_choice;\n                bool found = false;\n\n                for (const auto& rot_data : rotations) {\n                    long long w = get<0>(rot_data);\n                    long long h = get<1>(rot_data);\n                    int rot = get<2>(rot_data);\n                    for (char d : {'U', 'L'}) {\n                        set<int> ref_candidates;\n                        ref_candidates.insert(-1);\n                        if (!placed_set.empty()) {\n                            if (last_placed_index != -1) {\n                                ref_candidates.insert(last_placed_index);\n                            }\n                            long long max_area = -1;\n                            int best_j = -1;\n                            for (int j : placed_set) {\n                                auto [x_pl, y_pl, w_pl, h_pl] = rect_by_index[j];\n                                long long area = w_pl * h_pl;\n                                if (area > max_area) {\n                                    max_area = area;\n                                    best_j = j;\n                                }\n                            }\n                            if (best_j != -1) {\n                                ref_candidates.insert(best_j);\n                            }\n                        }\n\n                        for (int b_ref : ref_candidates) {\n                            long long x0 = 0, y0 = 0;\n                            if (d == 'U') {\n                                if (b_ref == -1) {\n                                    x0 = 0;\n                                } else {\n                                    if (rect_by_index.find(b_ref) != rect_by_index.end()) {\n                                        auto [x_pl, y_pl, w_pl, h_pl] = rect_by_index[b_ref];\n                                        x0 = x_pl + w_pl;\n                                    } else {\n                                        continue;\n                                    }\n                                }\n                                y0 = 0;\n                                for (int j : placed_set) {\n                                    auto [x_pl, y_pl, w_pl, h_pl] = rect_by_index[j];\n                                    if (x0 < x_pl + w_pl && x0 + w > x_pl) {\n                                        if (y_pl + h_pl > y0) {\n                                            y0 = y_pl + h_pl;\n                                        }\n                                    }\n                                }\n                            } else {\n                                if (b_ref == -1) {\n                                    y0 = 0;\n                                } else {\n                                    if (rect_by_index.find(b_ref) != rect_by_index.end()) {\n                                        auto [x_pl, y_pl, w_pl, h_pl] = rect_by_index[b_ref];\n                                        y0 = y_pl + h_pl;\n                                    } else {\n                                        continue;\n                                    }\n                                }\n                                x0 = 0;\n                                for (int j : placed_set) {\n                                    auto [x_pl, y_pl, w_pl, h_pl] = rect_by_index[j];\n                                    if (y0 < y_pl + h_pl && y0 + h > y_pl) {\n                                        if (x_pl + w_pl > x0) {\n                                            x0 = x_pl + w_pl;\n                                        }\n                                    }\n                                }\n                            }\n\n                            long long new_max_x = max(current_max_x, x0 + w);\n                            long long new_max_y = max(current_max_y, y0 + h);\n                            long long bbox_sum = new_max_x + new_max_y;\n\n                            if (bbox_sum < best_bbox_sum) {\n                                best_bbox_sum = bbox_sum;\n                                best_choice = make_tuple(rot, d, b_ref, x0, y0, new_max_x, new_max_y);\n                                found = true;\n                            }\n                        }\n                    }\n                }\n\n                long long skip_penalty = w_sim[i] + h_sim[i];\n                if (found && (best_bbox_sum - (current_max_x + current_max_y) <= skip_penalty)) {\n                    auto [rot, d, b_ref, x0, y0, new_max_x, new_max_y] = best_choice;\n                    long long w_used, h_used;\n                    if (rot == 0) {\n                        w_used = w_sim[i];\n                        h_used = h_sim[i];\n                    } else {\n                        w_used = h_sim[i];\n                        h_used = w_sim[i];\n                    }\n                    placed_set.insert(i);\n                    rect_by_index[i] = make_tuple(x0, y0, w_used, h_used);\n                    current_max_x = new_max_x;\n                    current_max_y = new_max_y;\n                    sequence.emplace_back(i, rot, d, b_ref);\n                    last_placed_index = i;\n                }\n            }\n\n            cout << sequence.size() << '\\n';\n            for (auto [i, rot, d, b_ref] : sequence) {\n                cout << i << \" \" << rot << \" \" << d << \" \" << b_ref << '\\n';\n            }\n            cout.flush();\n\n            long long Wp, Hp;\n            cin >> Wp >> Hp;\n        }\n    }\n\n    return 0;\n}","ahc041":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, H;\n    cin >> N >> M >> H;\n\n    vector<int> A(N);\n    for (int i = 0; i < N; i++) {\n        cin >> A[i];\n    }\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    for (int i = 0; i < N; i++) {\n        int x, y;\n        cin >> x >> y;\n    }\n\n    vector<int> parent(N, -1);\n    vector<int> depth(N, -1);\n\n    vector<int> indices(N);\n    for (int i = 0; i < N; i++) {\n        indices[i] = i;\n    }\n    sort(indices.begin(), indices.end(), [&](int i, int j) {\n        return A[i] < A[j];\n    });\n\n    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;\n\n    for (int i = 0; i < N; i++) {\n        int v = indices[i];\n        if (depth[v] == -1) {\n            depth[v] = 0;\n            parent[v] = -1;\n            pq.push({A[v], v});\n        }\n    }\n\n    while (!pq.empty()) {\n        auto [beauty, u] = pq.top();\n        pq.pop();\n        if (depth[u] == H) {\n            continue;\n        }\n        for (int w : adj[u]) {\n            if (depth[w] == -1) {\n                depth[w] = depth[u] + 1;\n                parent[w] = u;\n                pq.push({A[w], w});\n            }\n        }\n    }\n\n    for (int i = 0; i < N; i++) {\n        if (i > 0) \n            cout << \" \";\n        cout << parent[i];\n    }\n    cout << endl;\n\n    return 0;\n}","ahc042":"#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\nint main() {\n    const int N = 20;\n    vector<string> board(N);\n    for (int i = 0; i < N; i++) {\n        cin >> board[i];\n    }\n\n    vector<pair<int, int>> onis;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (board[i][j] == 'x') {\n                onis.push_back({i, j});\n            }\n        }\n    }\n\n    vector<pair<char, int>> ans;\n\n    for (auto& oni : onis) {\n        int i = oni.first;\n        int j = oni.second;\n        vector<pair<int, char>> options;\n\n        bool valid = true;\n        for (int k = 0; k < i; k++) {\n            if (board[k][j] == 'o') {\n                valid = false;\n                break;\n            }\n        }\n        if (valid) {\n            options.push_back({2 * (i + 1), 'U'});\n        }\n\n        valid = true;\n        for (int k = i + 1; k < N; k++) {\n            if (board[k][j] == 'o') {\n                valid = false;\n                break;\n            }\n        }\n        if (valid) {\n            options.push_back({2 * (N - i), 'D'});\n        }\n\n        valid = true;\n        for (int k = 0; k < j; k++) {\n            if (board[i][k] == 'o') {\n                valid = false;\n                break;\n            }\n        }\n        if (valid) {\n            options.push_back({2 * (j + 1), 'L'});\n        }\n\n        valid = true;\n        for (int k = j + 1; k < N; k++) {\n            if (board[i][k] == 'o') {\n                valid = false;\n                break;\n            }\n        }\n        if (valid) {\n            options.push_back({2 * (N - j), 'R'});\n        }\n\n        if (options.empty()) {\n            vector<pair<int, char>> all_options = {\n                {2*(i+1), 'U'},\n                {2*(N-i), 'D'},\n                {2*(j+1), 'L'},\n                {2*(N-j), 'R'}\n            };\n            sort(all_options.begin(), all_options.end());\n            char chosen_dir = all_options[0].second;\n            if (chosen_dir == 'U') {\n                for (int k = 0; k <= i; k++) ans.push_back({'U', j});\n                for (int k = 0; k <= i; k++) ans.push_back({'D', j});\n            } else if (chosen_dir == 'D') {\n                for (int k = 0; k < N - i; k++) ans.push_back({'D', j});\n                for (int k = 0; k < N - i; k++) ans.push_back({'U', j});\n            } else if (chosen_dir == 'L') {\n                for (int k = 0; k <= j; k++) ans.push_back({'L', i});\n                for (int k = 0; k <= j; k++) ans.push_back({'R', i});\n            } else if (chosen_dir == 'R') {\n                for (int k = 0; k < N - j; k++) ans.push_back({'R', i});\n                for (int k = 0; k < N - j; k++) ans.push_back({'L', i});\n            }\n        } else {\n            sort(options.begin(), options.end());\n            char chosen_dir = options[0].second;\n            if (chosen_dir == 'U') {\n                for (int k = 0; k <= i; k++) ans.push_back({'U', j});\n                for (int k = 0; k <= i; k++) ans.push_back({'D', j});\n            } else if (chosen_dir == 'D') {\n                for (int k = 0; k < N - i; k++) ans.push_back({'D', j});\n                for (int k = 0; k < N - i; k++) ans.push_back({'U', j});\n            } else if (chosen_dir == 'L') {\n                for (int k = 0; k <= j; k++) ans.push_back({'L', i});\n                for (int k = 0; k <= j; k++) ans.push_back({'R', i});\n            } else if (chosen_dir == 'R') {\n                for (int k = 0; k < N - j; k++) ans.push_back({'R', i});\n                for (int k = 0; k < N - j; k++) ans.push_back({'L', i});\n            }\n        }\n    }\n\n    for (auto& move : ans) {\n        cout << move.first << \" \" << move.second << '\\n';\n    }\n\n    return 0;\n}","ahc044":"#include <iostream>\n#include <vector>\n#include <cstdlib>\n#include <algorithm>\n#include <random>\n#include <cmath>\n#include <climits>\nusing namespace std;\n\nlong long simulate(vector<int>& a, vector<int>& b, const vector<int>& T, int L) {\n    int N = a.size();\n    vector<int> total_count(N, 0);\n    vector<char> parity(N, 0);\n\n    vector<int> next_table_arr(2 * N);\n    for (int i = 0; i < N; i++) {\n        next_table_arr[i] = b[i];\n        next_table_arr[N + i] = a[i];\n    }\n\n    total_count[0] = 1;\n    parity[0] = 1;\n    int current = 0;\n\n    for (int week = 2; week <= L; week++) {\n        int p = parity[current];\n        int next_emp = next_table_arr[p * N + current];\n        total_count[next_emp]++;\n        parity[next_emp] ^= 1;\n        current = next_emp;\n    }\n\n    long long error = 0;\n    for (int i = 0; i < N; i++) {\n        error += abs(total_count[i] - T[i]);\n    }\n    return error;\n}\n\nint main() {\n    int N, L;\n    cin >> N >> L;\n    vector<int> T(N);\n    for (int i = 0; i < N; i++) {\n        cin >> T[i];\n    }\n\n    mt19937 rng(0);\n    vector<int> a(N), b(N);\n    for (int i = 0; i < N; i++) {\n        a[i] = uniform_int_distribution<int>(0, N-1)(rng);\n        b[i] = uniform_int_distribution<int>(0, N-1)(rng);\n    }\n\n    long long best_error = simulate(a, b, T, L);\n    vector<int> best_a = a;\n    vector<int> best_b = b;\n\n    int iterations = 200;\n    uniform_int_distribution<int> emp_dist(0, N-1);\n    uniform_int_distribution<int> bit_dist(0, 1);\n\n    for (int iter = 0; iter < iterations; iter++) {\n        vector<int> new_a = a;\n        vector<int> new_b = b;\n\n        int i = emp_dist(rng);\n        int which = bit_dist(rng);\n        int old_val;\n\n        if (which == 0) {\n            old_val = a[i];\n            int new_val;\n            do {\n                new_val = emp_dist(rng);\n            } while (new_val == old_val);\n            new_a[i] = new_val;\n        } else {\n            old_val = b[i];\n            int new_val;\n            do {\n                new_val = emp_dist(rng);\n            } while (new_val == old_val);\n            new_b[i] = new_val;\n        }\n\n        long long new_error = simulate(new_a, new_b, T, L);\n        if (new_error < best_error) {\n            best_error = new_error;\n            best_a = new_a;\n            best_b = new_b;\n            a = new_a;\n            b = new_b;\n        }\n    }\n\n    for (int i = 0; i < N; i++) {\n        cout << best_a[i] << \" \" << best_b[i] << endl;\n    }\n\n    return 0;\n}","ahc045":"#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\nint N, M, Q, L, W;\nvector<int> cx, cy;\n\nvector<pair<int, int>> query_cluster(vector<int> cluster) {\n    sort(cluster.begin(), cluster.end());\n    int l = cluster.size();\n    cout << \"? \" << l;\n    for (int x : cluster) {\n        cout << \" \" << x;\n    }\n    cout << endl;\n    cout.flush();\n    \n    vector<pair<int, int>> edges;\n    for (int i = 0; i < l - 1; i++) {\n        int a, b;\n        cin >> a >> b;\n        edges.push_back({a, b});\n    }\n    return edges;\n}\n\nvector<pair<int, int>> build_mst_for_group(vector<int> group) {\n    sort(group.begin(), group.end(), [&](int i, int j) {\n        if (cx[i] != cx[j]) \n            return cx[i] < cx[j];\n        return cy[i] < cy[j];\n    });\n    \n    vector<pair<int, int>> edges;\n    int i = 0;\n    int n = group.size();\n    while (i < n) {\n        int rem = n - i;\n        if (rem == 1) {\n            break;\n        }\n        int take = (rem > L) ? L : rem;\n        vector<int> cluster;\n        for (int j = 0; j < take; j++) {\n            cluster.push_back(group[i + j]);\n        }\n        vector<pair<int, int>> e = query_cluster(cluster);\n        edges.insert(edges.end(), e.begin(), e.end());\n        \n        if (rem > L) {\n            i += (L - 1);\n        } else {\n            i = n;\n        }\n    }\n    return edges;\n}\n\nint main() {\n    cin >> N >> M >> Q >> L >> W;\n    vector<int> G(M);\n    for (int i = 0; i < M; i++) {\n        cin >> G[i];\n    }\n    \n    vector<int> lx(N), rx(N), ly(N), ry(N);\n    cx.resize(N);\n    cy.resize(N);\n    for (int i = 0; i < N; i++) {\n        cin >> lx[i] >> rx[i] >> ly[i] >> ry[i];\n        cx[i] = (lx[i] + rx[i]) / 2;\n        cy[i] = (ly[i] + ry[i]) / 2;\n    }\n    \n    vector<int> all_cities(N);\n    for (int i = 0; i < N; i++) {\n        all_cities[i] = i;\n    }\n    sort(all_cities.begin(), all_cities.end(), [&](int i, int j) {\n        if (cx[i] != cx[j]) \n            return cx[i] < cx[j];\n        return cy[i] < cy[j];\n    });\n    \n    vector<vector<int>> groups(M);\n    int idx = 0;\n    for (int k = 0; k < M; k++) {\n        groups[k].resize(G[k]);\n        for (int j = 0; j < G[k]; j++) {\n            groups[k][j] = all_cities[idx++];\n        }\n    }\n    \n    vector<vector<pair<int, int>>> group_edges(M);\n    for (int k = 0; k < M; k++) {\n        group_edges[k] = build_mst_for_group(groups[k]);\n    }\n    \n    cout << \"!\" << endl;\n    for (int k = 0; k < M; k++) {\n        for (int j = 0; j < groups[k].size(); j++) {\n            if (j > 0) \n                cout << \" \";\n            cout << groups[k][j];\n        }\n        cout << endl;\n        for (auto edge : group_edges[k]) {\n            cout << edge.first << \" \" << edge.second << endl;\n        }\n    }\n    \n    return 0;\n}","ahc046":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int N, M;\n    cin >> N >> M;\n    vector<pair<int, int>> points;\n    for (int i = 0; i < M; i++) {\n        int a, b;\n        cin >> a >> b;\n        points.push_back({a, b});\n    }\n\n    vector<vector<bool>> blocks(N, vector<bool>(N, false));\n    map<char, pair<int, int>> dirs;\n    dirs['U'] = {-1, 0};\n    dirs['D'] = {1, 0};\n    dirs['L'] = {0, -1};\n    dirs['R'] = {0, 1};\n\n    auto is_blocked = [&](int i, int j) -> bool {\n        if (i < 0 || i >= N || j < 0 || j >= N)\n            return true;\n        return blocks[i][j];\n    };\n\n    vector<string> outputs;\n    int ci = points[0].first, cj = points[0].second;\n\n    for (int idx = 1; idx < M; idx++) {\n        int ti = points[idx].first, tj = points[idx].second;\n        while (ci != ti || cj != tj) {\n            bool did_slide = false;\n            for (char d : {'R','L','D','U'}) {\n                int dx = dirs[d].first, dy = dirs[d].second;\n                if (d == 'R' || d == 'L') {\n                    if (ci != ti) continue;\n                } else {\n                    if (cj != tj) continue;\n                }\n\n                if (d == 'R') {\n                    if (cj >= tj) continue;\n                } else if (d == 'L') {\n                    if (cj <= tj) continue;\n                } else if (d == 'D') {\n                    if (ci >= ti) continue;\n                } else if (d == 'U') {\n                    if (ci <= ti) continue;\n                }\n\n                int ni_after = ti + dx;\n                int nj_after = tj + dy;\n                if (!is_blocked(ni_after, nj_after)) {\n                    continue;\n                }\n\n                if (is_blocked(ti, tj)) {\n                    continue;\n                }\n\n                bool path_clear = true;\n                if (d == 'R' || d == 'L') {\n                    int step_j = (d == 'R') ? 1 : -1;\n                    int steps = abs(cj - tj);\n                    for (int step = 1; step <= steps; step++) {\n                        int j = cj + step * step_j;\n                        if (is_blocked(ci, j)) {\n                            path_clear = false;\n                            break;\n                        }\n                    }\n                } else {\n                    int step_i = (d == 'D') ? 1 : -1;\n                    int steps = abs(ci - ti);\n                    for (int step = 1; step <= steps; step++) {\n                        int i = ci + step * step_i;\n                        if (is_blocked(i, cj)) {\n                            path_clear = false;\n                            break;\n                        }\n                    }\n                }\n                if (!path_clear) continue;\n\n                outputs.push_back(string(\"S \") + d);\n                ci = ti;\n                cj = tj;\n                did_slide = true;\n                break;\n            }\n            if (did_slide) {\n                continue;\n            }\n\n            char dir;\n            if (ci != ti) {\n                if (ci < ti) dir = 'D';\n                else dir = 'U';\n            } else {\n                if (cj < tj) dir = 'R';\n                else dir = 'L';\n            }\n\n            int dx = dirs[dir].first, dy = dirs[dir].second;\n            int ni = ci + dx;\n            int nj = cj + dy;\n\n            if (ni < 0 || ni >= N || nj < 0 || nj >= N) {\n                if (ci != ti) {\n                    if (cj < tj) dir = 'R';\n                    else dir = 'L';\n                    dx = dirs[dir].first;\n                    dy = dirs[dir].second;\n                    ni = ci + dx;\n                    nj = cj + dy;\n                    if (ni < 0 || ni >= N || nj < 0 || nj >= N) {\n                        if (ci < ti) dir = 'D';\n                        else dir = 'U';\n                        dx = dirs[dir].first;\n                        dy = dirs[dir].second;\n                        ni = ci + dx;\n                        nj = cj + dy;\n                    }\n                } else {\n                    if (dir == 'R') dir = 'L';\n                    else dir = 'R';\n                    dx = dirs[dir].first;\n                    dy = dirs[dir].second;\n                    ni = ci + dx;\n                    nj = cj + dy;\n                }\n            }\n\n            if (is_blocked(ni, nj)) {\n                outputs.push_back(string(\"A \") + dir);\n                blocks[ni][nj] = !blocks[ni][nj];\n                outputs.push_back(string(\"M \") + dir);\n                ci = ni;\n                cj = nj;\n            } else {\n                outputs.push_back(string(\"M \") + dir);\n                ci = ni;\n                cj = nj;\n            }\n        }\n    }\n\n    for (string& s : outputs) {\n        cout << s << '\\n';\n    }\n\n    return 0;\n}"},"2":{"ahc001":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <cmath>\n#include <cstdlib>\n#include <iomanip>\nusing namespace std;\n\nstruct Rect {\n    int a, b, c, d;\n    long long area;\n    long long r_val;\n};\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int n;\n    cin >> n;\n    vector<Rect> rects;\n    for (int i = 0; i < n; i++) {\n        int x, y, r;\n        cin >> x >> y >> r;\n        rects.push_back({x, y, x+1, y+1, 1LL, (long long)r});\n    }\n\n    bool changed = true;\n    int pass_count = 0;\n    const int MAX_PASS = 100;\n\n    while (changed && pass_count < MAX_PASS) {\n        pass_count++;\n        changed = false;\n        vector<int> indices(n);\n        for (int i = 0; i < n; i++) {\n            indices[i] = i;\n        }\n\n        sort(indices.begin(), indices.end(), [&](int i, int j) {\n            double sat_i = (double)rects[i].area / rects[i].r_val;\n            double sat_j = (double)rects[j].area / rects[j].r_val;\n            return sat_i < sat_j;\n        });\n\n        for (int idx = 0; idx < n; idx++) {\n            int i = indices[idx];\n            if (rects[i].area >= rects[i].r_val) \n                continue;\n\n            double best_improvement = -1e9;\n            int best_dir = -1;\n            int best_d_use = 0;\n\n            for (int dir = 0; dir < 4; dir++) {\n                int d_max;\n                if (dir == 0) {\n                    d_max = rects[i].a;\n                } else if (dir == 1) {\n                    d_max = 10000 - rects[i].c;\n                } else if (dir == 2) {\n                    d_max = rects[i].b;\n                } else {\n                    d_max = 10000 - rects[i].d;\n                }\n\n                if (d_max <= 0) continue;\n\n                int side_length;\n                if (dir < 2) {\n                    side_length = rects[i].d - rects[i].b;\n                } else {\n                    side_length = rects[i].c - rects[i].a;\n                }\n                if (side_length <= 0) continue;\n\n                for (int j = 0; j < n; j++) {\n                    if (i == j) continue;\n                    if (dir == 0) {\n                        if (rects[i].b < rects[j].d && rects[j].b < rects[i].d) {\n                            if (rects[j].c <= rects[i].a) {\n                                d_max = min(d_max, rects[i].a - rects[j].c);\n                            }\n                        }\n                    } else if (dir == 1) {\n                        if (rects[i].b < rects[j].d && rects[j].b < rects[i].d) {\n                            if (rects[j].a >= rects[i].c) {\n                                d_max = min(d_max, rects[j].a - rects[i].c);\n                            }\n                        }\n                    } else if (dir == 2) {\n                        if (rects[i].a < rects[j].c && rects[j].a < rects[i].c) {\n                            if (rects[j].d <= rects[i].b) {\n                                d_max = min(d_max, rects[i].b - rects[j].d);\n                            }\n                        }\n                    } else if (dir == 3) {\n                        if (rects[i].a < rects[j].c && rects[j].a < rects[i].c) {\n                            if (rects[j].b >= rects[i].d) {\n                                d_max = min(d_max, rects[j].b - rects[i].d);\n                            }\n                        }\n                    }\n                }\n\n                if (d_max <= 0) continue;\n\n                double d0 = (double)(rects[i].r_val - rects[i].area) / side_length;\n                if (d0 < 1e-5) continue;\n\n                vector<int> cands;\n                int d1 = (int)floor(d0);\n                int d2 = (int)ceil(d0);\n                d1 = min(d_max, d1);\n                d2 = min(d_max, d2);\n                if (d1 > 0) cands.push_back(d1);\n                if (d2 > 0 && d2 != d1) cands.push_back(d2);\n\n                double best_improve_dir = -1e9;\n                int best_d_use_dir = -1;\n\n                for (int d_candidate : cands) {\n                    long long new_area = rects[i].area + (long long)d_candidate * side_length;\n                    double min_val = min(rects[i].r_val, rects[i].area);\n                    double max_val = max(rects[i].r_val, rects[i].area);\n                    double old_sat = 1.0 - pow(1.0 - min_val / max_val, 2);\n\n                    min_val = min(rects[i].r_val, new_area);\n                    max_val = max(rects[i].r_val, new_area);\n                    double new_sat = 1.0 - pow(1.0 - min_val / max_val, 2);\n\n                    double improvement = new_sat - old_sat;\n                    if (improvement > best_improve_dir) {\n                        best_improve_dir = improvement;\n                        best_d_use_dir = d_candidate;\n                    }\n                }\n\n                if (best_improve_dir > best_improvement) {\n                    best_improvement = best_improve_dir;\n                    best_dir = dir;\n                    best_d_use = best_d_use_dir;\n                }\n            }\n\n            if (best_improvement > 1e-8) {\n                if (best_dir == 0) {\n                    rects[i].a -= best_d_use;\n                    rects[i].area += (long long)best_d_use * (rects[i].d - rects[i].b);\n                } else if (best_dir == 1) {\n                    rects[i].c += best_d_use;\n                    rects[i].area += (long long)best_d_use * (rects[i].d - rects[i].b);\n                } else if (best_dir == 2) {\n                    rects[i].b -= best_d_use;\n                    rects[i].area += (long long)best_d_use * (rects[i].c - rects[i].a);\n                } else if (best_dir == 3) {\n                    rects[i].d += best_d_use;\n                    rects[i].area += (long long)best_d_use * (rects[i].c - rects[i].a);\n                }\n                changed = true;\n            }\n        }\n    }\n\n    for (int i = 0; i < n; i++) {\n        cout << rects[i].a << \" \" << rects[i].b << \" \" << rects[i].c << \" \" << rects[i].d << \"\\n\";\n    }\n\n    return 0;\n}","ahc002":"#include <iostream>\n#include <vector>\n#include <set>\n#include <algorithm>\n#include <tuple>\nusing namespace std;\n\nint main() {\n    int si, sj;\n    cin >> si >> sj;\n    \n    vector<vector<int>> t(50, vector<int>(50));\n    vector<vector<int>> p(50, vector<int>(50));\n    \n    for (int i = 0; i < 50; i++) {\n        for (int j = 0; j < 50; j++) {\n            cin >> t[i][j];\n        }\n    }\n    for (int i = 0; i < 50; i++) {\n        for (int j = 0; j < 50; j++) {\n            cin >> p[i][j];\n        }\n    }\n    \n    int M = 0;\n    for (int i = 0; i < 50; i++) {\n        for (int j = 0; j < 50; j++) {\n            if (t[i][j] + 1 > M) {\n                M = t[i][j] + 1;\n            }\n        }\n    }\n    \n    vector<set<int>> adj_tiles(M);\n    const int dx[4] = {1, -1, 0, 0};\n    const int dy[4] = {0, 0, 1, -1};\n    \n    for (int i = 0; i < 50; i++) {\n        for (int j = 0; j < 50; j++) {\n            for (int d = 0; d < 4; d++) {\n                int ni = i + dx[d];\n                int nj = j + dy[d];\n                if (ni >= 0 && ni < 50 && nj >= 0 && nj < 50) {\n                    if (t[i][j] != t[ni][nj]) {\n                        adj_tiles[t[i][j]].insert(t[ni][nj]);\n                    }\n                }\n            }\n        }\n    }\n    \n    vector<int> current_degree(M);\n    for (int i = 0; i < M; i++) {\n        current_degree[i] = adj_tiles[i].size();\n    }\n    \n    vector<bool> visited_tile(M, false);\n    int cx = si, cy = sj;\n    int start_tile = t[si][sj];\n    visited_tile[start_tile] = true;\n    \n    for (int adj : adj_tiles[start_tile]) {\n        if (!visited_tile[adj]) {\n            current_degree[adj]--;\n        }\n    }\n    \n    string path = \"\";\n    const char dir_char[4] = {'D', 'U', 'R', 'L'};\n    \n    while (true) {\n        vector<tuple<int, char, int, int>> candidates;\n        for (int d1 = 0; d1 < 4; d1++) {\n            int nx1 = cx + dx[d1];\n            int ny1 = cy + dy[d1];\n            if (nx1 < 0 || nx1 >= 50 || ny1 < 0 || ny1 >= 50) continue;\n            int tile1 = t[nx1][ny1];\n            if (visited_tile[tile1]) continue;\n            \n            int score1 = p[nx1][ny1];\n            int best_next = 0;\n            \n            for (int d2 = 0; d2 < 4; d2++) {\n                int nx2 = nx1 + dx[d2];\n                int ny2 = ny1 + dy[d2];\n                if (nx2 < 0 || nx2 >= 50 || ny2 < 0 || ny2 >= 50) continue;\n                int tile2 = t[nx2][ny2];\n                if (tile2 == tile1) continue;\n                if (visited_tile[tile2]) continue;\n                \n                bool adjacent = (adj_tiles[tile2].find(tile1) != adj_tiles[tile2].end());\n                int next_deg = current_degree[tile2] - (adjacent ? 1 : 0);\n                int candidate_score = p[nx2][ny2] + 100 * next_deg;\n                if (candidate_score > best_next) {\n                    best_next = candidate_score;\n                }\n            }\n            \n            int total_score = score1 + best_next;\n            candidates.push_back(make_tuple(total_score, dir_char[d1], nx1, ny1));\n        }\n        \n        if (candidates.empty()) break;\n        \n        auto best = *max_element(candidates.begin(), candidates.end(),\n            [](const tuple<int, char, int, int>& a, const tuple<int, char, int, int>& b) {\n                return get<0>(a) < get<0>(b);\n            });\n        \n        char dir = get<1>(best);\n        int nx1 = get<2>(best);\n        int ny1 = get<3>(best);\n        int tile1 = t[nx1][ny1];\n        \n        path += dir;\n        cx = nx1;\n        cy = ny1;\n        visited_tile[tile1] = true;\n        \n        for (int adj : adj_tiles[tile1]) {\n            if (!visited_tile[adj]) {\n                current_degree[adj]--;\n            }\n        }\n    }\n    \n    cout << path << endl;\n    \n    return 0;\n}","ahc003":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\n#include <tuple>\n#include <cmath>\n#include <iomanip>\n\nusing namespace std;\n\nconst int N = 30;\nconst double INF = 1e18;\n\nvector<vector<double>> h_est(N, vector<double>(N-1, 5000.0));\nvector<vector<double>> v_est(N-1, vector<double>(N, 5000.0));\nvector<vector<int>> count_h(N, vector<int>(N-1, 0));\nvector<vector<int>> count_v(N-1, vector<int>(N, 0));\n\nconst int dx[4] = {0, 1, 0, -1};\nconst int dy[4] = {1, 0, -1, 0};\nconst char dir_char[4] = {'R', 'D', 'L', 'U'};\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    for (int k = 0; k < 1000; k++) {\n        int si, sj, ti, tj;\n        cin >> si >> sj >> ti >> tj;\n\n        vector<vector<double>> dist(N, vector<double>(N, INF));\n        vector<vector<pair<int, int>>> prev(N, vector<pair<int, int>>(N, {-1, -1}));\n        priority_queue<tuple<double, int, int>, vector<tuple<double, int, int>>, greater<tuple<double, int, int>>> pq;\n\n        dist[si][sj] = 0;\n        pq.push({0, si, sj});\n\n        while (!pq.empty()) {\n            auto [d, i, j] = pq.top();\n            pq.pop();\n            if (d > dist[i][j]) continue;\n            if (i == ti && j == tj) break;\n\n            for (int dir = 0; dir < 4; dir++) {\n                int ni = i + dx[dir];\n                int nj = j + dy[dir];\n                if (ni < 0 || ni >= N || nj < 0 || nj >= N) continue;\n\n                double weight;\n                if (dir == 0) {\n                    weight = h_est[i][j];\n                } else if (dir == 1) {\n                    weight = v_est[i][j];\n                } else if (dir == 2) {\n                    weight = h_est[i][j-1];\n                } else {\n                    weight = v_est[i-1][j];\n                }\n\n                double new_dist = dist[i][j] + weight;\n                if (new_dist < dist[ni][nj]) {\n                    dist[ni][nj] = new_dist;\n                    prev[ni][nj] = {i, j};\n                    pq.push({new_dist, ni, nj});\n                }\n            }\n        }\n\n        vector<char> moves;\n        int ci = ti, cj = tj;\n        while (ci != si || cj != sj) {\n            int pi = prev[ci][cj].first;\n            int pj = prev[ci][cj].second;\n            if (pi == ci) {\n                if (pj < cj) {\n                    moves.push_back('R');\n                } else {\n                    moves.push_back('L');\n                }\n            } else {\n                if (pi < ci) {\n                    moves.push_back('D');\n                } else {\n                    moves.push_back('U');\n                }\n            }\n            ci = pi;\n            cj = pj;\n        }\n        reverse(moves.begin(), moves.end());\n        string path_str(moves.begin(), moves.end());\n        cout << path_str << endl;\n        cout.flush();\n\n        long long z_k;\n        cin >> z_k;\n\n        double estimated_sum = dist[ti][tj];\n        int L = moves.size();\n\n        ci = si;\n        cj = sj;\n        for (char move : moves) {\n            if (move == 'R') {\n                double w_est = h_est[ci][cj];\n                double error = (z_k - estimated_sum) / L;\n                double alpha = 1.0 / sqrt(1 + count_h[ci][cj]);\n                double new_est = w_est + alpha * error;\n                new_est = max(1000.0, min(9000.0, new_est));\n                h_est[ci][cj] = new_est;\n                count_h[ci][cj]++;\n                cj++;\n            } else if (move == 'D') {\n                double w_est = v_est[ci][cj];\n                double error = (z_k - estimated_sum) / L;\n                double alpha = 1.0 / sqrt(1 + count_v[ci][cj]);\n                double new_est = w_est + alpha * error;\n                new_est = max(1000.0, min(9000.0, new_est));\n                v_est[ci][cj] = new_est;\n                count_v[ci][cj]++;\n                ci++;\n            } else if (move == 'L') {\n                double w_est = h_est[ci][cj-1];\n                double error = (z_k - estimated_sum) / L;\n                double alpha = 1.0 / sqrt(1 + count_h[ci][cj-1]);\n                double new_est = w_est + alpha * error;\n                new_est = max(1000.0, min(9000.0, new_est));\n                h_est[ci][cj-1] = new_est;\n                count_h[ci][cj-1]++;\n                cj--;\n            } else if (move == 'U') {\n                double w_est = v_est[ci-1][cj];\n                double error = (z_k - estimated_sum) / L;\n                double alpha = 1.0 / sqrt(1 + count_v[ci-1][cj]);\n                double new_est = w_est + alpha * error;\n                new_est = max(1000.0, min(9000.0, new_est));\n                v_est[ci-1][cj] = new_est;\n                count_v[ci-1][cj]++;\n                ci--;\n            }\n        }\n    }\n\n    return 0;\n}","ahc004":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <string>\n#include <cmath>\n#include <cstdlib>\n#include <ctime>\n#include <random>\nusing namespace std;\n\nbool is_subsequence(vector<string> &mat, string s) {\n    int len = s.length();\n    int N = mat.size();\n    for (int dir = 0; dir < 2; dir++) {\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                bool match = true;\n                for (int p = 0; p < len; p++) {\n                    int ni = i, nj = j;\n                    if (dir == 0) {\n                        nj = (j + p) % N;\n                        if (nj < 0) nj += N;\n                    } else {\n                        ni = (i + p) % N;\n                        if (ni < 0) ni += N;\n                    }\n                    if (mat[ni][nj] != s[p]) {\n                        match = false;\n                        break;\n                    }\n                }\n                if (match) {\n                    return true;\n                }\n            }\n        }\n    }\n    return false;\n}\n\nint compute_coverage(vector<string> &mat, vector<string> &strings) {\n    int covered = 0;\n    for (const string& s : strings) {\n        if (is_subsequence(mat, s)) {\n            covered++;\n        }\n    }\n    return covered;\n}\n\nint main() {\n    srand(time(0));\n    int N, M;\n    cin >> N >> M;\n    vector<string> strings(M);\n    for (int i = 0; i < M; i++) {\n        cin >> strings[i];\n    }\n\n    auto build_greedy = [&](bool desc) {\n        vector<string> mat(N, string(N, '.'));\n        vector<string> sorted = strings;\n        if (desc) {\n            sort(sorted.begin(), sorted.end(), [](const string& a, const string& b) {\n                return a.size() > b.size();\n            });\n        } else {\n            sort(sorted.begin(), sorted.end(), [](const string& a, const string& b) {\n                return a.size() < b.size();\n            });\n        }\n        for (const string& s : sorted) {\n            int len = s.size();\n            for (int dir = 0; dir < 2; dir++) {\n                for (int i = 0; i < N; i++) {\n                    for (int j = 0; j < N; j++) {\n                        bool ok = true;\n                        for (int p = 0; p < len; p++) {\n                            int ni = i, nj = j;\n                            if (dir == 0) {\n                                nj = (j + p) % N;\n                                if (nj < 0) nj += N;\n                            } else {\n                                ni = (i + p) % N;\n                                if (ni < 0) ni += N;\n                            }\n                            if (mat[ni][nj] != '.' && mat[ni][nj] != s[p]) {\n                                ok = false;\n                                break;\n                            }\n                        }\n                        if (ok) {\n                            for (int p = 0; p < len; p++) {\n                                int ni = i, nj = j;\n                                if (dir == 0) {\n                                    nj = (j + p) % N;\n                                    if (nj < 0) nj += N;\n                                } else {\n                                    ni = (i + p) % N;\n                                    if (ni < 0) ni += N;\n                                }\n                                if (mat[ni][nj] == '.') {\n                                    mat[ni][nj] = s[p];\n                                }\n                            }\n                            goto next_string;\n                        }\n                    }\n                }\n            }\n            next_string: ;\n        }\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                if (mat[i][j] == '.') {\n                    mat[i][j] = 'A';\n                }\n            }\n        }\n        return mat;\n    };\n\n    vector<string> mat1 = build_greedy(true);\n    int c1 = compute_coverage(mat1, strings);\n    vector<string> mat2 = build_greedy(false);\n    int c2 = compute_coverage(mat2, strings);\n\n    vector<string> best_mat = (c1 >= c2) ? mat1 : mat2;\n    int best_cov = max(c1, c2);\n\n    if (best_cov == M) {\n        for (int i = 0; i < N; i++) {\n            cout << best_mat[i] << endl;\n        }\n        return 0;\n    }\n\n    vector<string> current = best_mat;\n    int current_cov = best_cov;\n    const int MAX_ITER = 100;\n    const double T0 = 10.0;\n\n    for (int iter = 0; iter < MAX_ITER; iter++) {\n        double T = T0 * (1.0 - static_cast<double>(iter) / MAX_ITER);\n        int i = rand() % N;\n        int j = rand() % N;\n        char old_char = current[i][j];\n        char new_char;\n        do {\n            new_char = 'A' + (rand() % 8);\n        } while (new_char == old_char);\n\n        vector<string> candidate = current;\n        candidate[i][j] = new_char;\n        int new_cov = compute_coverage(candidate, strings);\n\n        if (new_cov > best_cov) {\n            best_cov = new_cov;\n            best_mat = candidate;\n        }\n\n        if (new_cov > current_cov) {\n            current = candidate;\n            current_cov = new_cov;\n        } else {\n            double delta = new_cov - current_cov;\n            if (delta == 0) {\n                current = candidate;\n                current_cov = new_cov;\n            } else {\n                double prob = exp(delta / T);\n                double r = static_cast<double>(rand()) / RAND_MAX;\n                if (r < prob) {\n                    current = candidate;\n                    current_cov = new_cov;\n                }\n            }\n        }\n    }\n\n    for (int i = 0; i < N; i++) {\n        cout << best_mat[i] << endl;\n    }\n    return 0;\n}","ahc005":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int di[] = {-1, 1, 0, 0};\nconst int dj[] = {0, 0, -1, 1};\nconst char dir_char[] = {'U', 'D', 'L', 'R'};\nconst int INF = 1e9;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, si, sj;\n    cin >> N >> si >> sj;\n    vector<string> grid;\n    for (int i = 0; i < N; i++) {\n        string s;\n        cin >> s;\n        grid.push_back(s);\n    }\n\n    vector<vector<int>> h_segment_id(N, vector<int>(N, -1));\n    vector<vector<int>> v_segment_id(N, vector<int>(N, -1));\n    vector<vector<pair<int, int>>> horizontal_segments;\n    vector<vector<pair<int, int>>> vertical_segments;\n\n    for (int i = 0; i < N; i++) {\n        int j = 0;\n        while (j < N) {\n            if (grid[i][j] == '#') {\n                j++;\n                continue;\n            }\n            int j0 = j;\n            vector<pair<int, int>> seg;\n            while (j < N && grid[i][j] != '#') {\n                seg.push_back({i, j});\n                j++;\n            }\n            int seg_id = horizontal_segments.size();\n            horizontal_segments.push_back(seg);\n            for (const auto& p : seg) {\n                h_segment_id[p.first][p.second] = seg_id;\n            }\n        }\n    }\n\n    for (int j = 0; j < N; j++) {\n        int i = 0;\n        while (i < N) {\n            if (grid[i][j] == '#') {\n                i++;\n                continue;\n            }\n            int i0 = i;\n            vector<pair<int, int>> seg;\n            while (i < N && grid[i][j] != '#') {\n                seg.push_back({i, j});\n                i++;\n            }\n            int seg_id = vertical_segments.size();\n            vertical_segments.push_back(seg);\n            for (const auto& p : seg) {\n                v_segment_id[p.first][p.second] = seg_id;\n            }\n        }\n    }\n\n    int totalH = horizontal_segments.size();\n    int totalV = vertical_segments.size();\n    vector<bool> coveredH(totalH, false);\n    vector<bool> coveredV(totalV, false);\n    set<pair<int, int>> uncoveredCells;\n\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (grid[i][j] == '#') continue;\n            if (!coveredH[h_segment_id[i][j]] || !coveredV[v_segment_id[i][j]]) {\n                uncoveredCells.insert({i, j});\n            }\n        }\n    }\n\n    auto coverCell = [&](int i, int j) {\n        bool updated = false;\n        int h_id = h_segment_id[i][j];\n        int v_id = v_segment_id[i][j];\n        if (h_id != -1 && !coveredH[h_id]) {\n            coveredH[h_id] = true;\n            for (const auto& cell : horizontal_segments[h_id]) {\n                int x = cell.first, y = cell.second;\n                if (v_segment_id[x][y] != -1 && coveredV[v_segment_id[x][y]]) {\n                    if (uncoveredCells.find({x, y}) != uncoveredCells.end()) {\n                        uncoveredCells.erase({x, y});\n                    }\n                }\n            }\n            updated = true;\n        }\n        if (v_id != -1 && !coveredV[v_id]) {\n            coveredV[v_id] = true;\n            for (const auto& cell : vertical_segments[v_id]) {\n                int x = cell.first, y = cell.second;\n                if (h_segment_id[x][y] != -1 && coveredH[h_segment_id[x][y]]) {\n                    if (uncoveredCells.find({x, y}) != uncoveredCells.end()) {\n                        uncoveredCells.erase({x, y});\n                    }\n                }\n            }\n            updated = true;\n        }\n        return updated;\n    };\n\n    coverCell(si, sj);\n    int cur_i = si, cur_j = sj;\n    string route = \"\";\n\n    while (!uncoveredCells.empty()) {\n        vector<vector<int>> dist(N, vector<int>(N, INF));\n        vector<vector<pair<int, int>>> prev(N, vector<pair<int, int>>(N, {-1, -1}));\n        priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<tuple<int, int, int>>> pq;\n        dist[cur_i][cur_j] = 0;\n        pq.push({0, cur_i, cur_j});\n        pair<int, int> found = {-1, -1};\n\n        while (!pq.empty()) {\n            auto [cost, i, j] = pq.top();\n            pq.pop();\n            if (cost != dist[i][j]) continue;\n            if (uncoveredCells.find({i, j}) != uncoveredCells.end()) {\n                found = {i, j};\n                break;\n            }\n            for (int d = 0; d < 4; d++) {\n                int ni = i + di[d];\n                int nj = j + dj[d];\n                if (ni < 0 || ni >= N || nj < 0 || nj >= N || grid[ni][nj] == '#') continue;\n                int weight = grid[ni][nj] - '0';\n                int new_cost = cost + weight;\n                if (new_cost < dist[ni][nj]) {\n                    dist[ni][nj] = new_cost;\n                    prev[ni][nj] = {i, j};\n                    pq.push({new_cost, ni, nj});\n                }\n            }\n        }\n\n        if (found.first == -1) break;\n\n        vector<pair<int, int>> path_cells;\n        int ci = found.first, cj = found.second;\n        while (ci != cur_i || cj != cur_j) {\n            path_cells.push_back({ci, cj});\n            auto p = prev[ci][cj];\n            ci = p.first;\n            cj = p.second;\n        }\n        reverse(path_cells.begin(), path_cells.end());\n        int now_i = cur_i, now_j = cur_j;\n        string path_str = \"\";\n        for (const auto& cell : path_cells) {\n            int ni = cell.first, nj = cell.second;\n            if (ni == now_i + 1) path_str += 'D';\n            else if (ni == now_i - 1) path_str += 'U';\n            else if (nj == now_j + 1) path_str += 'R';\n            else if (nj == now_j - 1) path_str += 'L';\n            now_i = ni;\n            now_j = nj;\n            coverCell(ni, nj);\n        }\n        route += path_str;\n        cur_i = found.first;\n        cur_j = found.second;\n    }\n\n    vector<vector<int>> dist_return(N, vector<int>(N, INF));\n    vector<vector<pair<int, int>>> prev_return(N, vector<pair<int, int>>(N, {-1, -1}));\n    priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<tuple<int, int, int>>> pq_return;\n    dist_return[cur_i][cur_j] = 0;\n    pq_return.push({0, cur_i, cur_j});\n    while (!pq_return.empty()) {\n        auto [cost, i, j] = pq_return.top();\n        pq_return.pop();\n        if (cost != dist_return[i][j]) continue;\n        if (i == si && j == sj) break;\n        for (int d = 0; d < 4; d++) {\n            int ni = i + di[d];\n            int nj = j + dj[d];\n            if (ni < 0 || ni >= N || nj < 0 || nj >= N || grid[ni][nj] == '#') continue;\n            int weight = grid[ni][nj] - '0';\n            int new_cost = cost + weight;\n            if (new_cost < dist_return[ni][nj]) {\n                dist_return[ni][nj] = new_cost;\n                prev_return[ni][nj] = {i, j};\n                pq_return.push({new_cost, ni, nj});\n            }\n        }\n    }\n\n    vector<pair<int, int>> return_path;\n    int ci = si, cj = sj;\n    while (ci != cur_i || cj != cur_j) {\n        return_path.push_back({ci, cj});\n        auto p = prev_return[ci][cj];\n        ci = p.first;\n        cj = p.second;\n    }\n    reverse(return_path.begin(), return_path.end());\n    int now_i = cur_i, now_j = cur_j;\n    string return_str = \"\";\n    for (const auto& cell : return_path) {\n        int ni = cell.first, nj = cell.second;\n        if (ni == now_i + 1) return_str += 'D';\n        else if (ni == now_i - 1) return_str += 'U';\n        else if (nj == now_j + 1) return_str += 'R';\n        else if (nj == now_j - 1) return_str += 'L';\n        now_i = ni;\n        now_j = nj;\n    }\n    route += return_str;\n\n    cout << route << endl;\n\n    return 0;\n}","future-contest-2022-qual":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <set>\n#include <algorithm>\n#include <cmath>\n#include <iomanip>\n#include <cctype>\n#include <cstdio>\n#include <cstring>\n#include <map>\n#include <unordered_map>\n#include <unordered_set>\n#include <stack>\n#include <deque>\n#include <string>\n#include <climits>\n#include <cfloat>\n#include <bitset>\nusing namespace std;\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, K, R;\n    cin >> N >> M >> K >> R;\n\n    vector<vector<int>> task_diff(N, vector<int>(K));\n    for (int i = 0; i < N; i++) {\n        for (int k = 0; k < K; k++) {\n            cin >> task_diff[i][k];\n        }\n    }\n\n    vector<vector<int>> children(N+1);\n    vector<vector<int>> parents(N+1);\n    vector<int> in_degree(N+1, 0);\n    for (int i = 0; i < R; i++) {\n        int u, v;\n        cin >> u >> v;\n        children[u].push_back(v);\n        parents[v].push_back(u);\n        in_degree[v]++;\n    }\n\n    vector<int> num_desc(N+1, 0);\n    for (int i = 1; i <= N; i++) {\n        vector<bool> visited(N+1, false);\n        queue<int> q;\n        q.push(i);\n        visited[i] = true;\n        int count = 0;\n        while (!q.empty()) {\n            int u = q.front(); q.pop();\n            count++;\n            for (int v : children[u]) {\n                if (!visited[v]) {\n                    visited[v] = true;\n                    q.push(v);\n                }\n            }\n        }\n        num_desc[i] = count;\n    }\n\n    vector<bool> completed(N+1, false);\n    vector<bool> assigned(N+1, false);\n    vector<int> task_start_day(N+1, -1);\n    vector<int> member_status(M+1, -1);\n    vector<vector<double>> s_hat(M+1, vector<double>(K, 20.0));\n    vector<vector<int>> min_skill_lower_bound(M+1, vector<int>(K, 0));\n    set<int> available_set;\n    for (int i = 1; i <= N; i++) {\n        if (in_degree[i] == 0) {\n            available_set.insert(i);\n        }\n    }\n\n    int day = 0;\n    while (true) {\n        day++;\n        if (day > 1) {\n            int n_finished;\n            cin >> n_finished;\n            if (n_finished == -1) {\n                break;\n            }\n            vector<int> finished_members(n_finished);\n            for (int i = 0; i < n_finished; i++) {\n                cin >> finished_members[i];\n            }\n            for (int j : finished_members) {\n                int task_i = member_status[j];\n                completed[task_i] = true;\n                assigned[task_i] = false;\n                member_status[j] = -1;\n                for (int child : children[task_i]) {\n                    in_degree[child]--;\n                    if (in_degree[child] == 0 && !completed[child] && !assigned[child]) {\n                        available_set.insert(child);\n                    }\n                }\n                int start_day = task_start_day[task_i];\n                int t_days = day - start_day;\n                vector<int>& d_vec = task_diff[task_i-1];\n                if (t_days == 1) {\n                    for (int k = 0; k < K; k++) {\n                        if (d_vec[k] > min_skill_lower_bound[j][k]) {\n                            min_skill_lower_bound[j][k] = d_vec[k];\n                        }\n                        if (s_hat[j][k] < min_skill_lower_bound[j][k]) {\n                            s_hat[j][k] = min_skill_lower_bound[j][k];\n                        }\n                    }\n                } else {\n                    double w_hat = 0.0;\n                    for (int k = 0; k < K; k++) {\n                        if (d_vec[k] > s_hat[j][k]) {\n                            w_hat += (d_vec[k] - s_hat[j][k]);\n                        }\n                    }\n                    double w_obs = t_days;\n                    double e = w_obs - w_hat;\n                    double learning_rate = 0.1;\n                    for (int k = 0; k < K; k++) {\n                        if (d_vec[k] > s_hat[j][k]) {\n                            s_hat[j][k] -= learning_rate * e;\n                            if (s_hat[j][k] < min_skill_lower_bound[j][k]) {\n                                s_hat[j][k] = min_skill_lower_bound[j][k];\n                            }\n                        } else {\n                            if (s_hat[j][k] < min_skill_lower_bound[j][k]) {\n                                s_hat[j][k] = min_skill_lower_bound[j][k];\n                            }\n                        }\n                    }\n                }\n            }\n        }\n\n        vector<int> free_members;\n        for (int j = 1; j <= M; j++) {\n            if (member_status[j] == -1) {\n                free_members.push_back(j);\n            }\n        }\n\n        if (free_members.empty() || available_set.empty()) {\n            cout << 0 << endl;\n            for (int j = 1; j <= M; j++) {\n                cout << \"#s \" << j;\n                for (int k = 0; k < K; k++) {\n                    cout << \" \" << (int)round(s_hat[j][k]);\n                }\n                cout << endl;\n            }\n            cout.flush();\n            continue;\n        }\n\n        vector<int> available_tasks_list(available_set.begin(), available_set.end());\n        sort(available_tasks_list.begin(), available_tasks_list.end(), [&](int a, int b) {\n            return num_desc[a] > num_desc[b];\n        });\n\n        int n_tasks = available_tasks_list.size();\n        int n_free = free_members.size();\n        vector<vector<double>> time_est(n_tasks, vector<double>(n_free, 0.0));\n        for (int i = 0; i < n_tasks; i++) {\n            int task_id = available_tasks_list[i];\n            vector<int>& d_vec = task_diff[task_id-1];\n            for (int j = 0; j < n_free; j++) {\n                int member_id = free_members[j];\n                double w = 0.0;\n                for (int k = 0; k < K; k++) {\n                    if (d_vec[k] > s_hat[member_id][k]) {\n                        w += d_vec[k] - s_hat[member_id][k];\n                    }\n                }\n                if (w < 1e-5) {\n                    w = 1.0;\n                }\n                time_est[i][j] = w;\n            }\n        }\n\n        vector<pair<double, int>> task_priority;\n        for (int i = 0; i < n_tasks; i++) {\n            double min_time = 1e18;\n            for (int j = 0; j < n_free; j++) {\n                if (time_est[i][j] < min_time) {\n                    min_time = time_est[i][j];\n                }\n            }\n            int task_id = available_tasks_list[i];\n            double priority_val = (double)num_desc[task_id] / (min_time + 1.0);\n            task_priority.push_back({priority_val, i});\n        }\n        sort(task_priority.rbegin(), task_priority.rend());\n\n        vector<bool> free_flag(n_free, true);\n        vector<pair<int, int>> assignments;\n        vector<bool> task_assigned_flag(n_tasks, false);\n        for (auto& p : task_priority) {\n            int idx_task = p.second;\n            if (task_assigned_flag[idx_task]) continue;\n            int task_id = available_tasks_list[idx_task];\n            double best_time = 1e18;\n            int best_j_index = -1;\n            for (int j = 0; j < n_free; j++) {\n                if (free_flag[j] && time_est[idx_task][j] < best_time) {\n                    best_time = time_est[idx_task][j];\n                    best_j_index = j;\n                }\n            }\n            if (best_j_index != -1) {\n                int member_id = free_members[best_j_index];\n                assignments.push_back({member_id, task_id});\n                free_flag[best_j_index] = false;\n                task_assigned_flag[idx_task] = true;\n                assigned[task_id] = true;\n                available_set.erase(task_id);\n                member_status[member_id] = task_id;\n                task_start_day[task_id] = day;\n            }\n        }\n\n        cout << assignments.size();\n        for (auto& a : assignments) {\n            cout << \" \" << a.first << \" \" << a.second;\n        }\n        cout << endl;\n        for (int j = 1; j <= M; j++) {\n            cout << \"#s \" << j;\n            for (int k = 0; k < K; k++) {\n                cout << \" \" << (int)round(s_hat[j][k]);\n            }\n            cout << endl;\n        }\n        cout.flush();\n    }\n\n    return 0;\n}","ahc006":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <cmath>\n#include <cstdlib>\nusing namespace std;\n\nstruct Point {\n    int x, y;\n    Point(int x=0, int y=0) : x(x), y(y) {}\n};\n\nint manhattan(const Point& a, const Point& b) {\n    return abs(a.x - b.x) + abs(a.y - b.y);\n}\n\nstruct Order {\n    int a, b, c, d;\n};\n\nint main() {\n    vector<Order> orders(1000);\n    for (int i = 0; i < 1000; i++) {\n        cin >> orders[i].a >> orders[i].b >> orders[i].c >> orders[i].d;\n    }\n\n    vector<Point> tour;\n    tour.push_back(Point(400, 400));\n    tour.push_back(Point(400, 400));\n\n    vector<int> point_to_order;\n    vector<int> point_type;\n    point_to_order.push_back(-1);\n    point_to_order.push_back(-1);\n    point_type.push_back(0);\n    point_type.push_back(0);\n\n    vector<int> rest_index(1000, -1);\n    vector<int> dest_index(1000, -1);\n    vector<bool> used(1000, false);\n    vector<int> chosen_ids;\n\n    int n = 2;\n    for (int step = 0; step < 50; step++) {\n        long long best_cost = (long long)1e18;\n        int best_order = -1;\n        int best_edge = -1;\n\n        for (int idx = 0; idx < 1000; idx++) {\n            if (used[idx]) continue;\n            Order& o = orders[idx];\n            Point R = {o.a, o.b};\n            Point D = {o.c, o.d};\n            for (int edge = 0; edge < n-1; edge++) {\n                Point A = tour[edge];\n                Point B = tour[edge+1];\n                long long cost_insert = (long long)manhattan(A, R) + manhattan(R, D) + manhattan(D, B) - manhattan(A, B);\n                if (cost_insert < best_cost) {\n                    best_cost = cost_insert;\n                    best_order = idx;\n                    best_edge = edge;\n                }\n            }\n        }\n\n        if (best_order == -1) {\n            break;\n        }\n\n        Order& o = orders[best_order];\n        Point R = {o.a, o.b};\n        Point D = {o.c, o.d};\n        tour.insert(tour.begin() + best_edge + 1, R);\n        tour.insert(tour.begin() + best_edge + 2, D);\n\n        point_to_order.insert(point_to_order.begin() + best_edge + 1, best_order);\n        point_to_order.insert(point_to_order.begin() + best_edge + 2, best_order);\n        point_type.insert(point_type.begin() + best_edge + 1, 1);\n        point_type.insert(point_type.begin() + best_edge + 2, 2);\n\n        rest_index[best_order] = best_edge + 1;\n        dest_index[best_order] = best_edge + 2;\n        used[best_order] = true;\n        chosen_ids.push_back(best_order + 1);\n\n        n += 2;\n    }\n\n    n = tour.size();\n    bool improved = true;\n    while (improved) {\n        improved = false;\n        for (int i = 0; i <= n - 4; i++) {\n            int j = i + 1;\n            int k = i + 2;\n            int id1 = point_to_order[j];\n            int id2 = point_to_order[k];\n            if (id1 == id2 && id1 != -1) {\n                continue;\n            }\n\n            Point A = tour[i];\n            Point B = tour[j];\n            Point C = tour[k];\n            Point D = tour[i + 3];\n            long long original_cost = (long long)manhattan(A, B) + manhattan(B, C) + manhattan(C, D);\n            long long new_cost = (long long)manhattan(A, C) + manhattan(C, B) + manhattan(B, D);\n            long long delta = new_cost - original_cost;\n            if (delta >= 0) {\n                continue;\n            }\n\n            bool valid = true;\n            if (id1 != -1) {\n                int r1 = rest_index[id1];\n                int d1 = dest_index[id1];\n                if (j == r1) {\n                    r1 = k;\n                } else if (j == d1) {\n                    d1 = k;\n                }\n                if (r1 >= d1) {\n                    valid = false;\n                }\n            }\n            if (valid && id2 != -1) {\n                int r2 = rest_index[id2];\n                int d2 = dest_index[id2];\n                if (k == r2) {\n                    r2 = j;\n                } else if (k == d2) {\n                    d2 = j;\n                }\n                if (r2 >= d2) {\n                    valid = false;\n                }\n            }\n\n            if (valid) {\n                swap(tour[j], tour[k]);\n                swap(point_to_order[j], point_to_order[k]);\n                swap(point_type[j], point_type[k]);\n\n                if (id1 != -1) {\n                    if (rest_index[id1] == j) {\n                        rest_index[id1] = k;\n                    } else if (dest_index[id1] == j) {\n                        dest_index[id1] = k;\n                    }\n                }\n                if (id2 != -1) {\n                    if (rest_index[id2] == k) {\n                        rest_index[id2] = j;\n                    } else if (dest_index[id2] == k) {\n                        dest_index[id2] = j;\n                    }\n                }\n                improved = true;\n            }\n        }\n    }\n\n    cout << \"50\";\n    for (int i = 0; i < 50; i++) {\n        cout << \" \" << chosen_ids[i];\n    }\n    cout << endl;\n    cout << tour.size();\n    for (int i = 0; i < tour.size(); i++) {\n        cout << \" \" << tour[i].x << \" \" << tour[i].y;\n    }\n    cout << endl;\n\n    return 0;\n}","ahc007":"#include <bits/stdc++.h>\n#include <cmath>\n#include <vector>\n#include <tuple>\n#include <algorithm>\n#include <atcoder/dsu>\n\nusing namespace std;\nusing namespace atcoder;\n\nint main() {\n    const int N = 400;\n    const int M = 1995;\n    vector<double> x(N), y(N);\n    for (int i = 0; i < N; i++) {\n        cin >> x[i] >> y[i];\n    }\n\n    vector<pair<int, int>> edges_list(M);\n    for (int i = 0; i < M; i++) {\n        cin >> edges_list[i].first >> edges_list[i].second;\n    }\n\n    vector<double> d_list(M);\n    for (int i = 0; i < M; i++) {\n        int u = edges_list[i].first;\n        int v = edges_list[i].second;\n        double dx = x[u] - x[v];\n        double dy = y[u] - y[v];\n        d_list[i] = round(sqrt(dx*dx + dy*dy));\n    }\n\n    vector<tuple<double, int, int, int>> edges_for_mst;\n    for (int i = 0; i < M; i++) {\n        edges_for_mst.push_back(make_tuple(d_list[i], edges_list[i].first, edges_list[i].second, i));\n    }\n    sort(edges_for_mst.begin(), edges_for_mst.end());\n\n    dsu dsu_mst(N);\n    vector<bool> in_mst_low(M, false);\n    for (auto &e : edges_for_mst) {\n        double cost = get<0>(e);\n        int u = get<1>(e);\n        int v = get<2>(e);\n        int idx = get<3>(e);\n        if (dsu_mst.same(u, v)) continue;\n        dsu_mst.merge(u, v);\n        in_mst_low[idx] = true;\n    }\n\n    dsu dsu_online(N);\n    int comp_count = N;\n\n    for (int i = 0; i < M; i++) {\n        int l_i;\n        cin >> l_i;\n\n        int u = edges_list[i].first;\n        int v = edges_list[i].second;\n\n        if (dsu_online.same(u, v)) {\n            cout << 0 << endl;\n            cout.flush();\n        } else {\n            dsu temp = dsu_online;\n            int temp_comp = comp_count;\n            for (int j = i+1; j < M; j++) {\n                int uj = edges_list[j].first;\n                int vj = edges_list[j].second;\n                if (!temp.same(uj, vj)) {\n                    temp.merge(uj, vj);\n                    temp_comp--;\n                }\n            }\n            if (temp_comp != 1) {\n                cout << 1 << endl;\n                cout.flush();\n                dsu_online.merge(u, v);\n                comp_count--;\n            } else {\n                if (in_mst_low[i]) {\n                    if (l_i <= 2.5 * d_list[i]) {\n                        cout << 1 << endl;\n                        cout.flush();\n                        dsu_online.merge(u, v);\n                        comp_count--;\n                    } else {\n                        cout << 0 << endl;\n                        cout.flush();\n                    }\n                } else {\n                    if (l_i <= 2 * d_list[i]) {\n                        cout << 1 << endl;\n                        cout.flush();\n                        dsu_online.merge(u, v);\n                        comp_count--;\n                    } else {\n                        cout << 0 << endl;\n                        cout.flush();\n                    }\n                }\n            }\n        }\n    }\n\n    return 0;\n}","ahc008":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\n#include <set>\n#include <tuple>\n#include <climits>\nusing namespace std;\n\nconst int GRID_SIZE = 32;\n\nstruct Pet {\n    int x, y, type;\n};\n\nstruct Human {\n    int x, y;\n};\n\nstruct BarrierTask {\n    int wall_x, wall_y;\n    int hx, hy;\n    char action;\n};\n\nint manhattan(int x1, int y1, int x2, int y2) {\n    return abs(x1 - x2) + abs(y1 - y2);\n}\n\nint main() {\n    int N;\n    cin >> N;\n    vector<Pet> pets(N);\n    for (int i = 0; i < N; i++) {\n        cin >> pets[i].x >> pets[i].y >> pets[i].type;\n    }\n    int M;\n    cin >> M;\n    vector<Human> humans(M);\n    for (int i = 0; i < M; i++) {\n        cin >> humans[i].x >> humans[i].y;\n    }\n\n    bool wall[GRID_SIZE][GRID_SIZE] = {false};\n    for (int i = 0; i < GRID_SIZE; i++) {\n        for (int j = 0; j < GRID_SIZE; j++) {\n            if (i == 0 || i == GRID_SIZE-1 || j == 0 || j == GRID_SIZE-1) {\n                wall[i][j] = true;\n            }\n        }\n    }\n\n    vector<BarrierTask> all_tasks;\n    for (int y = 2; y <= 29; y++) {\n        BarrierTask task;\n        task.wall_x = 2; task.wall_y = y;\n        task.hx = 3; task.hy = y;\n        task.action = 'u';\n        all_tasks.push_back(task);\n    }\n    for (int y = 2; y <= 29; y++) {\n        BarrierTask task;\n        task.wall_x = 29; task.wall_y = y;\n        task.hx = 28; task.hy = y;\n        task.action = 'd';\n        all_tasks.push_back(task);\n    }\n    for (int x = 3; x <= 28; x++) {\n        BarrierTask task;\n        task.wall_x = x; task.wall_y = 2;\n        task.hx = x; task.hy = 3;\n        task.action = 'l';\n        all_tasks.push_back(task);\n    }\n    for (int x = 3; x <= 28; x++) {\n        BarrierTask task;\n        task.wall_x = x; task.wall_y = 29;\n        task.hx = x; task.hy = 28;\n        task.action = 'r';\n        all_tasks.push_back(task);\n    }\n\n    BarrierTask gap_task = {2, 15, 3, 15, 'u'};\n    vector<BarrierTask> all_tasks_without_gap;\n    for (auto& task : all_tasks) {\n        if (task.wall_x == gap_task.wall_x && task.wall_y == gap_task.wall_y) {\n            continue;\n        }\n        all_tasks_without_gap.push_back(task);\n    }\n\n    vector<queue<BarrierTask>> tasks(M);\n    vector<int> current_x(M), current_y(M);\n    for (int i=0; i<M; i++) {\n        current_x[i] = humans[i].x;\n        current_y[i] = humans[i].y;\n    }\n\n    vector<BarrierTask> unassigned = all_tasks_without_gap;\n    while (!unassigned.empty()) {\n        int best_i = -1, best_j = -1;\n        int min_dist = INT_MAX;\n        for (int i=0; i<M; i++) {\n            for (int j=0; j<unassigned.size(); j++) {\n                BarrierTask& task = unassigned[j];\n                int d = manhattan(current_x[i], current_y[i], task.hx, task.hy);\n                if (d < min_dist) {\n                    min_dist = d;\n                    best_i = i;\n                    best_j = j;\n                }\n            }\n        }\n        if (best_i == -1) break;\n        tasks[best_i].push(unassigned[best_j]);\n        current_x[best_i] = unassigned[best_j].hx;\n        current_y[best_i] = unassigned[best_j].hy;\n        unassigned.erase(unassigned.begin() + best_j);\n    }\n\n    bool gap_assigned = false;\n\n    for (int turn = 0; turn < 300; turn++) {\n        set<pair<int, int>> planned_walls;\n        vector<string> actions(M, \".\");\n\n        for (int i = 0; i < M; i++) {\n            if (tasks[i].empty()) {\n                continue;\n            }\n            BarrierTask current_task = tasks[i].front();\n            if (wall[current_task.wall_x][current_task.wall_y]) {\n                tasks[i].pop();\n                continue;\n            }\n            if (humans[i].x == current_task.hx && humans[i].y == current_task.hy) {\n                bool can_build = true;\n                for (const auto& pet : pets) {\n                    if (pet.x == current_task.wall_x && pet.y == current_task.wall_y) {\n                        can_build = false;\n                        break;\n                    }\n                }\n                if (can_build) {\n                    for (int j = 0; j < M; j++) {\n                        if (humans[j].x == current_task.wall_x && humans[j].y == current_task.wall_y) {\n                            can_build = false;\n                            break;\n                        }\n                    }\n                }\n                if (can_build) {\n                    int dx[4] = {0,0,1,-1};\n                    int dy[4] = {1,-1,0,0};\n                    for (int d=0; d<4; d++) {\n                        int nx = current_task.wall_x + dx[d];\n                        int ny = current_task.wall_y + dy[d];\n                        if (nx < 1 || nx > 30 || ny < 1 || ny > 30) continue;\n                        for (const auto& pet : pets) {\n                            if (pet.x == nx && pet.y == ny) {\n                                can_build = false;\n                                break;\n                            }\n                        }\n                        if (!can_build) break;\n                    }\n                }\n                if (current_task.wall_x == gap_task.wall_x && current_task.wall_y == gap_task.wall_y) {\n                    if (turn < 290) {\n                        can_build = false;\n                    }\n                }\n                if (can_build) {\n                    actions[i] = string(1, current_task.action);\n                    planned_walls.insert({current_task.wall_x, current_task.wall_y});\n                }\n            } else {\n                actions[i] = \"?\";\n            }\n        }\n\n        if (turn >= 290 && !gap_assigned && !wall[gap_task.wall_x][gap_task.wall_y]) {\n            int best_i = -1;\n            int best_dist = INT_MAX;\n            for (int i=0; i<M; i++) {\n                int d = manhattan(humans[i].x, humans[i].y, gap_task.hx, gap_task.hy);\n                if (d < best_dist) {\n                    best_dist = d;\n                    best_i = i;\n                }\n            }\n            if (best_i != -1) {\n                tasks[best_i].push(gap_task);\n                gap_assigned = true;\n            }\n        }\n\n        for (int i = 0; i < M; i++) {\n            if (actions[i] == \"?\") {\n                if (tasks[i].empty()) {\n                    actions[i] = '.';\n                    continue;\n                }\n                BarrierTask current_task = tasks[i].front();\n                if (wall[current_task.wall_x][current_task.wall_y]) {\n                    tasks[i].pop();\n                    actions[i] = '.';\n                    continue;\n                }\n                int sx = humans[i].x;\n                int sy = humans[i].y;\n                int tx = current_task.hx;\n                int ty = current_task.hy;\n\n                vector<vector<bool>> visited(GRID_SIZE, vector<bool>(GRID_SIZE, false));\n                vector<vector<char>> first_step_dir(GRID_SIZE, vector<char>(GRID_SIZE, '.'));\n                queue<pair<int, int>> q;\n                q.push({sx, sy});\n                visited[sx][sy] = true;\n                bool found = false;\n\n                while (!q.empty()) {\n                    auto [x, y] = q.front(); q.pop();\n                    if (x == tx && y == ty) {\n                        found = true;\n                        break;\n                    }\n                    vector<tuple<int, int, char>> dirs = {{-1,0,'U'}, {1,0,'D'}, {0,-1,'L'}, {0,1,'R'}};\n                    for (auto [dx, dy, dir] : dirs) {\n                        int nx = x + dx;\n                        int ny = y + dy;\n                        if (nx < 1 || nx > 30 || ny < 1 || ny > 30) continue;\n                        if (visited[nx][ny]) continue;\n                        if (wall[nx][ny] || planned_walls.count({nx,ny})) {\n                            continue;\n                        }\n                        visited[nx][ny] = true;\n                        if (x == sx && y == sy) {\n                            first_step_dir[nx][ny] = dir;\n                        } else {\n                            first_step_dir[nx][ny] = first_step_dir[x][y];\n                        }\n                        q.push({nx, ny});\n                    }\n                }\n\n                if (found) {\n                    actions[i] = string(1, first_step_dir[tx][ty]);\n                } else {\n                    actions[i] = '.';\n                }\n            }\n        }\n\n        string output_line;\n        for (int i=0; i<M; i++) {\n            output_line += actions[i];\n        }\n        cout << output_line << endl;\n        cout.flush();\n\n        for (int i=0; i<M; i++) {\n            if (actions[i] == \"u\" || actions[i] == \"d\" || actions[i] == \"l\" || actions[i] == \"r\") {\n                BarrierTask current_task = tasks[i].front();\n                wall[current_task.wall_x][current_task.wall_y] = true;\n                tasks[i].pop();\n            } else if (actions[i] == \"U\") {\n                humans[i].x--;\n            } else if (actions[i] == \"D\") {\n                humans[i].x++;\n            } else if (actions[i] == \"L\") {\n                humans[i].y--;\n            } else if (actions[i] == \"R\") {\n                humans[i].y++;\n            }\n        }\n\n        for (int i=0; i<N; i++) {\n            string move;\n            cin >> move;\n            for (char c : move) {\n                if (c == 'U') pets[i].x--;\n                else if (c == 'D') pets[i].x++;\n                else if (c == 'L') pets[i].y--;\n                else if (c == 'R') pets[i].y++;\n            }\n        }\n    }\n\n    return 0;\n}","ahc009":"#include <iostream>\n#include <vector>\n#include <cctype>\n#include <algorithm>\n#include <iomanip>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int s_i, s_j, t_i, t_j;\n    double p;\n    cin >> s_i >> s_j >> t_i >> t_j >> p;\n\n    vector<string> h(20);\n    for (int i = 0; i < 20; i++) {\n        cin >> h[i];\n    }\n\n    vector<string> v(19);\n    for (int i = 0; i < 19; i++) {\n        cin >> v[i];\n    }\n\n    vector<vector<vector<char>>> best_move(200, vector<vector<char>>(20, vector<char>(20, 'U')));\n    vector<vector<double>> dp_next(20, vector<double>(20, 0.0));\n\n    for (int i = 0; i < 20; i++) {\n        for (int j = 0; j < 20; j++) {\n            if (i == t_i && j == t_j) {\n                dp_next[i][j] = 201.0;\n            } else {\n                dp_next[i][j] = 0.0;\n            }\n        }\n    }\n\n    for (int tt = 199; tt >= 0; tt--) {\n        vector<vector<double>> dp_curr(20, vector<double>(20, 0.0));\n        for (int i = 0; i < 20; i++) {\n            for (int j = 0; j < 20; j++) {\n                if (i == t_i && j == t_j) {\n                    continue;\n                }\n                double best_val = -1e18;\n                char best_char = 'U';\n                for (char move : {'U','D','L','R'}) {\n                    int ni = i, nj = j;\n                    if (move == 'U') {\n                        if (i > 0 && v[i-1][j] == '0') {\n                            ni = i - 1;\n                        }\n                    } else if (move == 'D') {\n                        if (i < 19 && v[i][j] == '0') {\n                            ni = i + 1;\n                        }\n                    } else if (move == 'L') {\n                        if (j > 0 && h[i][j-1] == '0') {\n                            nj = j - 1;\n                        }\n                    } else if (move == 'R') {\n                        if (j < 19 && h[i][j] == '0') {\n                            nj = j + 1;\n                        }\n                    }\n\n                    double val;\n                    if (ni == t_i && nj == t_j) {\n                        val = p * dp_next[i][j] + (1-p) * (401 - (tt+1));\n                    } else {\n                        val = p * dp_next[i][j] + (1-p) * dp_next[ni][nj];\n                    }\n\n                    if (val > best_val) {\n                        best_val = val;\n                        best_char = move;\n                    }\n                }\n                dp_curr[i][j] = best_val;\n                best_move[tt][i][j] = best_char;\n            }\n        }\n        dp_next = dp_curr;\n    }\n\n    int cur_i = s_i, cur_j = s_j;\n    string route = \"\";\n    for (int t = 0; t < 200; t++) {\n        if (cur_i == t_i && cur_j == t_j) {\n            int rem = 200 - t;\n            route += string(rem, 'U');\n            break;\n        }\n        char move = best_move[t][cur_i][cur_j];\n        route += move;\n\n        if (move == 'U') {\n            if (cur_i > 0 && v[cur_i-1][cur_j] == '0') {\n                cur_i--;\n            }\n        } else if (move == 'D') {\n            if (cur_i < 19 && v[cur_i][cur_j] == '0') {\n                cur_i++;\n            }\n        } else if (move == 'L') {\n            if (cur_j > 0 && h[cur_i][cur_j-1] == '0') {\n                cur_j--;\n            }\n        } else if (move == 'R') {\n            if (cur_j < 19 && h[cur_i][cur_j] == '0') {\n                cur_j++;\n            }\n        }\n    }\n\n    cout << route << endl;\n\n    return 0;\n}","ahc010":"#include <iostream>\n#include <vector>\n#include <cstdlib>\n#include <cmath>\n#include <algorithm>\n#include <random>\n#include <chrono>\n\nusing namespace std;\n\nconst vector<int> di = {0, -1, 0, 1};\nconst vector<int> dj = {-1, 0, 1, 0};\n\nconst vector<vector<int>> to_table = {\n    {1, 0, -1, -1},\n    {3, -1, -1, 0},\n    {-1, -1, 3, 2},\n    {-1, 2, 1, -1},\n    {1, 0, 3, 2},\n    {3, 2, 1, 0},\n    {2, -1, 0, -1},\n    {-1, 3, -1, 1}\n};\n\nlong long compute_score(const vector<vector<int>>& grid, const vector<vector<int>>& rot, vector<vector<vector<bool>>>& visited) {\n    int n = 30;\n    vector<vector<int>> eff(n, vector<int>(n));\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            int t = grid[i][j];\n            int r = rot[i][j];\n            if (t < 4) {\n                eff[i][j] = (t + r) % 4;\n            } else if (t < 6) {\n                if (r % 2 == 0) {\n                    eff[i][j] = t;\n                } else {\n                    eff[i][j] = t ^ 1;\n                }\n            } else {\n                if (r % 2 == 0) {\n                    eff[i][j] = t;\n                } else {\n                    eff[i][j] = t ^ 1;\n                }\n            }\n        }\n    }\n\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            for (int d = 0; d < 4; d++) {\n                visited[i][j][d] = false;\n            }\n        }\n    }\n\n    vector<long long> loops;\n\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            for (int d = 0; d < 4; d++) {\n                if (visited[i][j][d]) continue;\n\n                int ci = i, cj = j, cd = d;\n                visited[ci][cj][cd] = true;\n                long long steps = 0;\n                bool found_loop = false;\n\n                while (true) {\n                    int eff_tile = eff[ci][cj];\n                    int d_out = to_table[eff_tile][cd];\n                    if (d_out == -1) {\n                        break;\n                    }\n                    int ni = ci + di[d_out];\n                    int nj = cj + dj[d_out];\n                    if (ni < 0 || ni >= n || nj < 0 || nj >= n) {\n                        break;\n                    }\n                    int nd = (d_out + 2) % 4;\n\n                    if (ni == i && nj == j && nd == d) {\n                        steps++;\n                        loops.push_back(steps);\n                        found_loop = true;\n                        break;\n                    }\n\n                    if (visited[ni][nj][nd]) {\n                        break;\n                    }\n\n                    visited[ni][nj][nd] = true;\n                    steps++;\n                    ci = ni;\n                    cj = nj;\n                    cd = nd;\n                }\n            }\n        }\n    }\n\n    if (loops.size() < 2) {\n        return 0;\n    }\n\n    sort(loops.rbegin(), loops.rend());\n    return (long long)loops[0] * loops[1];\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int num_test_cases = 100;\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n\n    vector<vector<vector<bool>>> visited(30, vector<vector<bool>>(30, vector<bool>(4, false)));\n\n    for (int tc = 0; tc < num_test_cases; tc++) {\n        vector<vector<int>> grid(30, vector<int>(30));\n        for (int i = 0; i < 30; i++) {\n            string s;\n            cin >> s;\n            for (int j = 0; j < 30; j++) {\n                grid[i][j] = s[j] - '0';\n            }\n        }\n\n        vector<vector<int>> rot(30, vector<int>(30));\n        for (int i = 0; i < 30; i++) {\n            for (int j = 0; j < 30; j++) {\n                rot[i][j] = rng() % 4;\n            }\n        }\n\n        long long current_score = compute_score(grid, rot, visited);\n\n        double T0 = 10000.0;\n        int max_iter = 1000;\n\n        uniform_int_distribution<int> rand_tile(0, 29);\n        uniform_int_distribution<int> rand_rot_delta(1, 3);\n        uniform_real_distribution<double> rand_double(0.0, 1.0);\n\n        for (int iter = 0; iter < max_iter; iter++) {\n            int i = rand_tile(rng);\n            int j = rand_tile(rng);\n            int old_rot_val = rot[i][j];\n            int delta = rand_rot_delta(rng);\n            int new_rot_val = (old_rot_val + delta) % 4;\n\n            rot[i][j] = new_rot_val;\n            long long new_score = compute_score(grid, rot, visited);\n\n            double T = T0 * (1.0 - (double)iter / max_iter);\n            if (T < 1e-9) T = 1e-9;\n\n            if (new_score > current_score) {\n                current_score = new_score;\n            } else {\n                double diff = new_score - current_score;\n                if (diff >= 0) {\n                    current_score = new_score;\n                } else {\n                    double prob = exp(diff / T);\n                    if (rand_double(rng) < prob) {\n                        current_score = new_score;\n                    } else {\n                        rot[i][j] = old_rot_val;\n                    }\n                }\n            }\n        }\n\n        for (int i = 0; i < 30; i++) {\n            for (int j = 0; j < 30; j++) {\n                cout << rot[i][j];\n            }\n        }\n        cout << '\\n';\n    }\n\n    return 0;\n}","ahc011":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\n#include <tuple>\n#include <map>\n#include <set>\n#include <climits>\nusing namespace std;\n\nconst int BEAM_WIDTH = 10;\n\nstruct State {\n    vector<vector<int>> board;\n    int empty_r, empty_c;\n    string moves;\n    char last_move;\n    int S, C, dist;\n};\n\npair<int, int> evaluate(vector<vector<int>>& board, int N) {\n    vector<pair<pair<int,int>, pair<int,int>>> edge_list;\n    int total_edges = 0;\n\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N-1; j++) {\n            if (board[i][j] != 0 && board[i][j+1] != 0) {\n                if ((board[i][j] & 4) && (board[i][j+1] & 1)) {\n                    edge_list.push_back({{i, j}, {i, j+1}});\n                    total_edges++;\n                }\n            }\n        }\n    }\n\n    for (int i = 0; i < N-1; i++) {\n        for (int j = 0; j < N; j++) {\n            if (board[i][j] != 0 && board[i+1][j] != 0) {\n                if ((board[i][j] & 8) && (board[i+1][j] & 2)) {\n                    edge_list.push_back({{i, j}, {i+1, j}});\n                    total_edges++;\n                }\n            }\n        }\n    }\n\n    map<pair<int,int>, vector<pair<int,int>>> adj;\n    for (auto &e : edge_list) {\n        auto u = e.first;\n        auto v = e.second;\n        adj[u].push_back(v);\n        adj[v].push_back(u);\n    }\n\n    vector<vector<bool>> visited(N, vector<bool>(N, false));\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (board[i][j] == 0) {\n                visited[i][j] = true;\n            }\n        }\n    }\n\n    int max_tree_size = 0;\n\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (!visited[i][j]) {\n                vector<pair<int,int>> component;\n                queue<pair<int,int>> q;\n                q.push({i, j});\n                visited[i][j] = true;\n\n                while (!q.empty()) {\n                    auto node = q.front();\n                    q.pop();\n                    component.push_back(node);\n\n                    if (adj.find(node) != adj.end()) {\n                        for (auto &nei : adj[node]) {\n                            if (!visited[nei.first][nei.second]) {\n                                visited[nei.first][nei.second] = true;\n                                q.push(nei);\n                            }\n                        }\n                    }\n                }\n\n                set<pair<int,int>> comp_set(component.begin(), component.end());\n                int count_edges = 0;\n                for (auto &node : component) {\n                    if (adj.find(node) != adj.end()) {\n                        for (auto &nei : adj[node]) {\n                            if (comp_set.find(nei) != comp_set.end()) {\n                                count_edges++;\n                            }\n                        }\n                    }\n                }\n                int E_component = count_edges / 2;\n                int V = component.size();\n                if (E_component == V - 1) {\n                    if (V > max_tree_size) {\n                        max_tree_size = V;\n                    }\n                }\n            }\n        }\n    }\n\n    if (max_tree_size == 0) {\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                if (board[i][j] != 0) {\n                    max_tree_size = 1;\n                }\n            }\n        }\n    }\n\n    return {max_tree_size, total_edges};\n}\n\nint main() {\n    int N, T_val;\n    cin >> N >> T_val;\n    vector<string> input_grid(N);\n    for (int i = 0; i < N; i++) {\n        cin >> input_grid[i];\n    }\n\n    vector<vector<int>> initial_board(N, vector<int>(N));\n    int empty_r = -1, empty_c = -1;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            char c = input_grid[i][j];\n            if (c >= '0' && c <= '9') {\n                initial_board[i][j] = c - '0';\n            } else if (c >= 'a' && c <= 'f') {\n                initial_board[i][j] = 10 + (c - 'a');\n            } else {\n                initial_board[i][j] = 0;\n            }\n            if (initial_board[i][j] == 0) {\n                empty_r = i;\n                empty_c = j;\n            }\n        }\n    }\n\n    auto [S0, C0] = evaluate(initial_board, N);\n    int dist0 = abs(empty_r - (N-1)) + abs(empty_c - (N-1));\n    State initial_state = {initial_board, empty_r, empty_c, \"\", ' ', S0, C0, dist0};\n\n    vector<State> beam;\n    beam.push_back(initial_state);\n\n    string final_sequence = \"\";\n    bool found_full_tree = false;\n\n    map<char, char> reverse_move = {{'U','D'}, {'D','U'}, {'L','R'}, {'R','L'}};\n    map<char, pair<int,int>> dir = {{'U',{-1,0}}, {'D',{1,0}}, {'L',{0,-1}}, {'R',{0,1}}};\n\n    for (int step = 0; step < T_val && !found_full_tree; step++) {\n        vector<State> candidates;\n\n        for (const State& state : beam) {\n            for (const char move : {'U','D','L','R'}) {\n                if (state.last_move != ' ' && move == reverse_move[state.last_move]) {\n                    continue;\n                }\n\n                int dr = dir[move].first;\n                int dc = dir[move].second;\n                int new_r = state.empty_r + dr;\n                int new_c = state.empty_c + dc;\n\n                if (new_r < 0 || new_r >= N || new_c < 0 || new_c >= N) {\n                    continue;\n                }\n\n                State new_state = state;\n                new_state.empty_r = new_r;\n                new_state.empty_c = new_c;\n                swap(new_state.board[state.empty_r][state.empty_c], new_state.board[new_r][new_c]);\n                new_state.moves = state.moves + move;\n                new_state.last_move = move;\n                \n                tie(new_state.S, new_state.C) = evaluate(new_state.board, N);\n                new_state.dist = abs(new_r - (N-1)) + abs(new_c - (N-1));\n                \n                candidates.push_back(new_state);\n\n                if (new_state.S == N*N-1) {\n                    final_sequence = new_state.moves;\n                    found_full_tree = true;\n                    break;\n                }\n            }\n            if (found_full_tree) break;\n        }\n        if (found_full_tree) break;\n\n        if (candidates.empty()) {\n            break;\n        }\n\n        sort(candidates.begin(), candidates.end(), [](const State& a, const State& b) {\n            if (a.S != b.S) return a.S > b.S;\n            if (a.C != b.C) return a.C > b.C;\n            return a.dist < b.dist;\n        });\n\n        beam.clear();\n        int width = min(BEAM_WIDTH, (int)candidates.size());\n        for (int i = 0; i < width; i++) {\n            beam.push_back(candidates[i]);\n        }\n    }\n\n    if (!found_full_tree && !beam.empty()) {\n        sort(beam.begin(), beam.end(), [](const State& a, const State& b) {\n            if (a.S != b.S) return a.S > b.S;\n            if (a.C != b.C) return a.C > b.C;\n            return a.dist < b.dist;\n        });\n        final_sequence = beam[0].moves;\n    }\n\n    cout << final_sequence << endl;\n\n    return 0;\n}","ahc012":"#include <bits/stdc++.h>\n#include <random>\nusing namespace std;\ntypedef long long ll;\ntypedef __int128 i128;\n\nconst int B = 20000;\nconst ll R2 = 100000000;\n\nstruct Point {\n    ll x, y;\n    Point() {}\n    Point(ll x, ll y) : x(x), y(y) {}\n    bool operator==(const Point& other) const {\n        return x == other.x && y == other.y;\n    }\n};\n\nstruct Line {\n    Point p, q;\n    ll a, b, c;\n    Line(Point p1, Point p2) : p(p1), q(p2) {\n        a = p.y - q.y;\n        b = q.x - p.x;\n        c = p.x * q.y - q.x * p.y;\n    }\n};\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    random_device rd;\n    mt19937 gen(rd());\n    uniform_int_distribution<int> distrib(-B, B);\n\n    const int CANDIDATE_SIZE = 1000;\n    const int num_test_cases = 100;\n    for (int tc = 0; tc < num_test_cases; tc++) {\n        int N, K;\n        cin >> N >> K;\n        vector<int> a(11, 0);\n        for (int i = 1; i <= 10; i++) {\n            cin >> a[i];\n        }\n        vector<Point> points;\n        for (int i = 0; i < N; i++) {\n            ll x, y;\n            cin >> x >> y;\n            points.push_back(Point(x, y));\n        }\n\n        vector<Line> candidate_lines;\n        vector<bool> used;\n        for (int i = 0; i < CANDIDATE_SIZE; ) {\n            bool found = false;\n            for (int t = 0; t < 100; t++) {\n                Point p(distrib(gen), distrib(gen));\n                Point q(distrib(gen), distrib(gen));\n                if (p == q) continue;\n                Line L(p, q);\n                i128 a_val = L.a;\n                i128 b_val = L.b;\n                i128 c_val = L.c;\n                i128 lhs = c_val * c_val;\n                i128 rhs = R2 * (a_val * a_val + b_val * b_val);\n                if (lhs > rhs) continue;\n\n                bool valid = true;\n                for (const Point& s : points) {\n                    ll D = L.a * s.x + L.b * s.y + L.c;\n                    if (D == 0) {\n                        valid = false;\n                        break;\n                    }\n                }\n                if (valid) {\n                    candidate_lines.push_back(L);\n                    found = true;\n                    i++;\n                    break;\n                }\n            }\n            if (!found) {\n                break;\n            }\n        }\n        used.assign(candidate_lines.size(), false);\n\n        vector<vector<int>> current_groups;\n        if (N > 0) {\n            current_groups.push_back(vector<int>());\n            for (int i = 0; i < N; i++) {\n                current_groups[0].push_back(i);\n            }\n        }\n\n        vector<int> current_b_d(11, 0);\n        if (N >= 1 && N <= 10) {\n            current_b_d[N] = 1;\n        }\n        int current_score = 0;\n        for (int d = 1; d <= 10; d++) {\n            current_score += min(a[d], current_b_d[d]);\n        }\n\n        vector<Line> chosen_lines;\n\n        for (int step = 0; step < K; step++) {\n            int best_next_score = current_score;\n            int best_candidate = -1;\n\n            if (candidate_lines.empty()) break;\n\n            for (int idx = 0; idx < candidate_lines.size(); idx++) {\n                if (used[idx]) continue;\n                const Line& L = candidate_lines[idx];\n                vector<int> new_b_d(11, 0);\n                for (const vector<int>& group : current_groups) {\n                    int cnt0 = 0, cnt1 = 0;\n                    for (int sid : group) {\n                        const Point& s = points[sid];\n                        ll D = L.a * s.x + L.b * s.y + L.c;\n                        if (D > 0) cnt1++;\n                        else cnt0++;\n                    }\n                    if (cnt0 > 0 && cnt0 <= 10) new_b_d[cnt0]++;\n                    if (cnt1 > 0 && cnt1 <= 10) new_b_d[cnt1]++;\n                }\n                int score = 0;\n                for (int d = 1; d <= 10; d++) {\n                    score += min(a[d], new_b_d[d]);\n                }\n                if (score > best_next_score) {\n                    best_next_score = score;\n                    best_candidate = idx;\n                }\n            }\n\n            if (best_candidate == -1) break;\n\n            used[best_candidate] = true;\n            const Line& L = candidate_lines[best_candidate];\n            chosen_lines.push_back(L);\n\n            vector<vector<int>> new_groups;\n            for (const vector<int>& group : current_groups) {\n                vector<int> group0, group1;\n                for (int sid : group) {\n                    const Point& s = points[sid];\n                    ll D = L.a * s.x + L.b * s.y + L.c;\n                    if (D > 0) group1.push_back(sid);\n                    else group0.push_back(sid);\n                }\n                if (!group0.empty()) new_groups.push_back(group0);\n                if (!group1.empty()) new_groups.push_back(group1);\n            }\n            current_groups = move(new_groups);\n\n            current_b_d.assign(11, 0);\n            for (const auto& g : current_groups) {\n                int s = g.size();\n                if (s >= 1 && s <= 10) current_b_d[s]++;\n            }\n            current_score = best_next_score;\n        }\n\n        cout << chosen_lines.size() << endl;\n        for (const Line& L : chosen_lines) {\n            cout << L.p.x << \" \" << L.p.y << \" \" << L.q.x << \" \" << L.q.y << endl;\n        }\n    }\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    cin >> N >> M;\n    vector<vector<bool>> dot(N, vector<bool>(N, false));\n    vector<set<int>> rows(N);\n    vector<set<int>> cols(N);\n\n    vector<vector<int>> next_in_row(N, vector<int>(N, N));\n    vector<vector<int>> prev_in_row(N, vector<int>(N, -1));\n    vector<vector<int>> next_in_col(N, vector<int>(N, N));\n    vector<vector<int>> prev_in_col(N, vector<int>(N, -1));\n\n    vector<vector<bool>> hor(N, vector<bool>(N, false));\n    vector<vector<bool>> ver(N, vector<bool>(N, false));\n\n    for (int i = 0; i < M; i++) {\n        int x, y;\n        cin >> x >> y;\n        dot[x][y] = true;\n        rows[y].insert(x);\n        cols[x].insert(y);\n    }\n\n    for (int y = 0; y < N; y++) {\n        int last = -1;\n        for (int x = 0; x < N; x++) {\n            if (dot[x][y]) {\n                last = x;\n            }\n            prev_in_row[y][x] = last;\n        }\n        last = N;\n        for (int x = N-1; x >= 0; x--) {\n            if (dot[x][y]) {\n                last = x;\n            }\n            next_in_row[y][x] = last;\n        }\n    }\n\n    for (int x = 0; x < N; x++) {\n        int last = -1;\n        for (int y = 0; y < N; y++) {\n            if (dot[x][y]) {\n                last = y;\n            }\n            prev_in_col[x][y] = last;\n        }\n        last = N;\n        for (int y = N-1; y >= 0; y--) {\n            if (dot[x][y]) {\n                last = y;\n            }\n            next_in_col[x][y] = last;\n        }\n    }\n\n    int c = (N - 1) / 2;\n    priority_queue<tuple<int, int, int>> pq;\n    for (int x = 0; x < N; x++) {\n        for (int y = 0; y < N; y++) {\n            if (!dot[x][y]) {\n                int weight = (x - c) * (x - c) + (y - c) * (y - c) + 1;\n                pq.push(make_tuple(weight, x, y));\n            }\n        }\n    }\n\n    vector<array<int, 8>> operations;\n    auto start_time = chrono::high_resolution_clock::now();\n\n    while (!pq.empty()) {\n        auto now = chrono::high_resolution_clock::now();\n        auto duration = chrono::duration_cast<chrono::milliseconds>(now - start_time);\n        if (duration.count() > 4500) {\n            break;\n        }\n\n        auto top = pq.top();\n        int w = get<0>(top);\n        int x1 = get<1>(top);\n        int y1 = get<2>(top);\n        pq.pop();\n        if (dot[x1][y1]) {\n            continue;\n        }\n\n        bool found = false;\n        const auto& col_x1 = cols[x1];\n        for (int y2 : col_x1) {\n            if (y2 == y1) continue;\n            int y_min = min(y1, y2);\n            int y_max = max(y1, y2);\n\n            if (next_in_col[x1][y_min + 1] < y_max) {\n                continue;\n            }\n\n            set<int> common;\n            const set<int>& row_y1 = rows[y1];\n            const set<int>& row_y2 = rows[y2];\n            if (row_y1.size() < row_y2.size()) {\n                for (int x : row_y1) {\n                    if (row_y2.find(x) != row_y2.end()) {\n                        common.insert(x);\n                    }\n                }\n            } else {\n                for (int x : row_y2) {\n                    if (row_y1.find(x) != row_y1.end()) {\n                        common.insert(x);\n                    }\n                }\n            }\n\n            for (int x2 : common) {\n                if (x2 == x1) continue;\n                int x_min = min(x1, x2);\n                int x_max = max(x1, x2);\n\n                if (next_in_row[y1][x_min + 1] < x_max) {\n                    continue;\n                }\n                if (next_in_row[y2][x_min + 1] < x_max) {\n                    continue;\n                }\n                if (next_in_col[x2][y_min + 1] < y_max) {\n                    continue;\n                }\n\n                bool edgeValid = true;\n                for (int y = y_min; y < y_max; y++) {\n                    if (y < N - 1 && ver[x1][y]) {\n                        edgeValid = false;\n                        break;\n                    }\n                }\n                if (!edgeValid) continue;\n\n                for (int y = y_min; y < y_max; y++) {\n                    if (y < N - 1 && ver[x2][y]) {\n                        edgeValid = false;\n                        break;\n                    }\n                }\n                if (!edgeValid) continue;\n\n                for (int x = x_min; x < x_max; x++) {\n                    if (x < N - 1 && hor[x][y_min]) {\n                        edgeValid = false;\n                        break;\n                    }\n                }\n                if (!edgeValid) continue;\n\n                for (int x = x_min; x < x_max; x++) {\n                    if (x < N - 1 && hor[x][y_max]) {\n                        edgeValid = false;\n                        break;\n                    }\n                }\n                if (!edgeValid) continue;\n\n                operations.push_back({x1, y1, x1, y2, x2, y2, x2, y1});\n                dot[x1][y1] = true;\n                rows[y1].insert(x1);\n                cols[x1].insert(y1);\n\n                int last_val = -1;\n                for (int x = 0; x < N; x++) {\n                    if (dot[x][y1]) {\n                        last_val = x;\n                    }\n                    prev_in_row[y1][x] = last_val;\n                }\n                last_val = N;\n                for (int x = N - 1; x >= 0; x--) {\n                    if (dot[x][y1]) {\n                        last_val = x;\n                    }\n                    next_in_row[y1][x] = last_val;\n                }\n\n                last_val = -1;\n                for (int y = 0; y < N; y++) {\n                    if (dot[x1][y]) {\n                        last_val = y;\n                    }\n                    prev_in_col[x1][y] = last_val;\n                }\n                last_val = N;\n                for (int y = N - 1; y >= 0; y--) {\n                    if (dot[x1][y]) {\n                        last_val = y;\n                    }\n                    next_in_col[x1][y] = last_val;\n                }\n\n                for (int y = y_min; y < y_max; y++) {\n                    if (y < N - 1) {\n                        ver[x1][y] = true;\n                    }\n                }\n                for (int y = y_min; y < y_max; y++) {\n                    if (y < N - 1) {\n                        ver[x2][y] = true;\n                    }\n                }\n                for (int x = x_min; x < x_max; x++) {\n                    if (x < N - 1) {\n                        hor[x][y_min] = true;\n                    }\n                }\n                for (int x = x_min; x < x_max; x++) {\n                    if (x < N - 1) {\n                        hor[x][y_max] = true;\n                    }\n                }\n\n                found = true;\n                break;\n            }\n            if (found) break;\n        }\n    }\n\n    cout << operations.size() << endl;\n    for (auto& op : operations) {\n        for (int i = 0; i < 8; i++) {\n            if (i > 0) cout << \" \";\n            cout << op[i];\n        }\n        cout << endl;\n    }\n\n    return 0;\n}","ahc015":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <climits>\n#include <algorithm>\nusing namespace std;\n\nvector<vector<int>> simulate_tilt(vector<vector<int>> grid, char dir) {\n    vector<vector<int>> new_grid(10, vector<int>(10, 0));\n    if (dir == 'L' || dir == 'R') {\n        for (int i = 0; i < 10; i++) {\n            vector<int> non_empty;\n            for (int j = 0; j < 10; j++) {\n                if (grid[i][j] != 0) {\n                    non_empty.push_back(grid[i][j]);\n                }\n            }\n            if (dir == 'L') {\n                for (int j = 0; j < non_empty.size(); j++) {\n                    new_grid[i][j] = non_empty[j];\n                }\n            } else {\n                int start = 10 - non_empty.size();\n                for (int j = 0; j < non_empty.size(); j++) {\n                    new_grid[i][start + j] = non_empty[j];\n                }\n            }\n        }\n    } else {\n        for (int j = 0; j < 10; j++) {\n            vector<int> non_empty;\n            for (int i = 0; i < 10; i++) {\n                if (grid[i][j] != 0) {\n                    non_empty.push_back(grid[i][j]);\n                }\n            }\n            if (dir == 'F') {\n                for (int i = 0; i < non_empty.size(); i++) {\n                    new_grid[i][j] = non_empty[i];\n                }\n            } else {\n                int start = 10 - non_empty.size();\n                for (int i = 0; i < non_empty.size(); i++) {\n                    new_grid[start + i][j] = non_empty[i];\n                }\n            }\n        }\n    }\n    return new_grid;\n}\n\nint compute_manhattan(vector<vector<int>>& grid) {\n    int penalty = 0;\n    for (int i = 0; i < 10; i++) {\n        for (int j = 0; j < 10; j++) {\n            if (grid[i][j] == 1) {\n                penalty += i + j;\n            } else if (grid[i][j] == 2) {\n                penalty += i + (9 - j);\n            } else if (grid[i][j] == 3) {\n                penalty += (9 - i) + j;\n            }\n        }\n    }\n    return penalty;\n}\n\nint compute_score(vector<vector<int>>& grid) {\n    int n = 10;\n    vector<vector<bool>> visited(n, vector<bool>(n, false));\n    int total_score = 0;\n    vector<int> dr = {1, -1, 0, 0};\n    vector<int> dc = {0, 0, 1, -1};\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            if (!visited[i][j] && grid[i][j] != 0) {\n                int flavor = grid[i][j];\n                int count = 0;\n                queue<pair<int, int>> q;\n                q.push({i, j});\n                visited[i][j] = true;\n                while (!q.empty()) {\n                    auto cell = q.front();\n                    q.pop();\n                    count++;\n                    int r = cell.first;\n                    int c = cell.second;\n                    for (int d = 0; d < 4; d++) {\n                        int nr = r + dr[d];\n                        int nc = c + dc[d];\n                        if (nr >= 0 && nr < n && nc >= 0 && nc < n && !visited[nr][nc] && grid[nr][nc] == flavor) {\n                            visited[nr][nc] = true;\n                            q.push({nr, nc});\n                        }\n                    }\n                }\n                total_score += count * count;\n            }\n        }\n    }\n    return total_score;\n}\n\nint main() {\n    vector<int> flavors;\n    for (int i = 0; i < 100; i++) {\n        int f;\n        cin >> f;\n        flavors.push_back(f);\n    }\n\n    vector<vector<int>> grid(10, vector<int>(10, 0));\n    vector<char> dirs = {'L', 'R', 'F', 'B'};\n\n    for (int t = 0; t < 100; t++) {\n        int p;\n        cin >> p;\n\n        vector<pair<int, int>> emptyCells;\n        for (int i = 0; i < 10; i++) {\n            for (int j = 0; j < 10; j++) {\n                if (grid[i][j] == 0) {\n                    emptyCells.push_back(make_pair(i, j));\n                }\n            }\n        }\n        sort(emptyCells.begin(), emptyCells.end(), [](const pair<int, int>& a, const pair<int, int>& b) {\n            if (a.first != b.first) return a.first < b.first;\n            return a.second < b.second;\n        });\n        int r = emptyCells[p-1].first;\n        int c = emptyCells[p-1].second;\n        grid[r][c] = flavors[t];\n\n        int best_score = -1;\n        int best_penalty = INT_MAX;\n        char best_dir = 'L';\n\n        for (char dir : dirs) {\n            vector<vector<int>> new_grid = simulate_tilt(grid, dir);\n            int score = compute_score(new_grid);\n            int penalty = compute_manhattan(new_grid);\n            if (score > best_score || (score == best_score && penalty < best_penalty)) {\n                best_score = score;\n                best_penalty = penalty;\n                best_dir = dir;\n            }\n        }\n\n        cout << best_dir << endl;\n        cout.flush();\n\n        grid = simulate_tilt(grid, best_dir);\n    }\n\n    return 0;\n}","ahc016":"#include <iostream>\n#include <vector>\n#include <cstdlib>\n#include <algorithm>\n#include <cmath>\nusing namespace std;\n\nint main() {\n    int M;\n    double eps;\n    cin >> M >> eps;\n\n    srand(42);\n    int n = 100;\n    int L = n * (n - 1) / 2;\n\n    cout << n << endl;\n\n    vector<vector<int>> stored_degrees;\n\n    for (int i = 0; i < M; i++) {\n        double p_i = 0.1 + 0.8 * (static_cast<double>(i) / (M - 1));\n        string s = \"\";\n        for (int j = 0; j < L; j++) {\n            double r = static_cast<double>(rand()) / RAND_MAX;\n            if (r < p_i) {\n                s += '1';\n            } else {\n                s += '0';\n            }\n        }\n        cout << s << endl;\n\n        vector<int> deg(n, 0);\n        int index = 0;\n        for (int u = 0; u < n; u++) {\n            for (int v = u + 1; v < n; v++) {\n                if (s[index] == '1') {\n                    deg[u]++;\n                    deg[v]++;\n                }\n                index++;\n            }\n        }\n        sort(deg.begin(), deg.end());\n        stored_degrees.push_back(deg);\n    }\n    cout.flush();\n\n    for (int q = 0; q < 100; q++) {\n        string H;\n        cin >> H;\n\n        vector<int> degH(n, 0);\n        int index = 0;\n        for (int u = 0; u < n; u++) {\n            for (int v = u + 1; v < n; v++) {\n                if (H[index] == '1') {\n                    degH[u]++;\n                    degH[v]++;\n                }\n                index++;\n            }\n        }\n        sort(degH.begin(), degH.end());\n\n        double minDist = 1e18;\n        int best_i = 0;\n        for (int i = 0; i < M; i++) {\n            vector<double> expDeg;\n            for (int j = 0; j < n; j++) {\n                double ed = stored_degrees[i][j] * (1 - 2 * eps) + eps * (n - 1);\n                expDeg.push_back(ed);\n            }\n            double dist = 0.0;\n            for (int j = 0; j < n; j++) {\n                double diff = expDeg[j] - degH[j];\n                dist += diff * diff;\n            }\n            if (dist < minDist) {\n                minDist = dist;\n                best_i = i;\n            }\n        }\n\n        cout << best_i << endl;\n        cout.flush();\n    }\n\n    return 0;\n}","ahc017":"#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, D, K;\n    cin >> N >> M >> D >> K;\n    vector<tuple<int, int, int>> edges;\n    vector<vector<tuple<int, int, int>>> adj(N);\n    map<pair<int, int>, int> edgeMap;\n\n    for (int i = 0; i < M; i++) {\n        int u, v, w;\n        cin >> u >> v >> w;\n        u--; v--;\n        edges.push_back({u, v, w});\n        adj[u].push_back({v, w, i});\n        adj[v].push_back({u, w, i});\n        int a = min(u, v), b = max(u, v);\n        edgeMap[{a, b}] = i;\n    }\n    for (int i = 0; i < N; i++) {\n        int x, y;\n        cin >> x >> y;\n    }\n\n    vector<double> bc(M, 0.0);\n\n    for (int s = 0; s < N; s++) {\n        vector<ll> dist(N, 1e18);\n        vector<int> numPaths(N, 0);\n        vector<vector<int>> pred(N);\n        vector<int> order;\n\n        dist[s] = 0;\n        numPaths[s] = 1;\n        priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;\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            order.push_back(u);\n            for (auto [v, w, eid] : adj[u]) {\n                ll nd = d + w;\n                if (nd < dist[v]) {\n                    dist[v] = nd;\n                    numPaths[v] = numPaths[u];\n                    pred[v] = {u};\n                    pq.push({nd, v});\n                } else if (nd == dist[v]) {\n                    numPaths[v] += numPaths[u];\n                    pred[v].push_back(u);\n                }\n            }\n        }\n\n        vector<double> dep(N, 0.0);\n        for (int i = order.size() - 1; i >= 0; i--) {\n            int w = order[i];\n            for (int u : pred[w]) {\n                if (numPaths[w] == 0) continue;\n                double frac = static_cast<double>(numPaths[u]) / numPaths[w] * (1.0 + dep[w]);\n                int a = min(u, w);\n                int b = max(u, w);\n                auto it = edgeMap.find({a, b});\n                if (it == edgeMap.end()) continue;\n                int eid = it->second;\n                bc[eid] += frac;\n                dep[u] += frac;\n            }\n        }\n    }\n\n    vector<int> assignment(M, -1);\n    vector<int> day_count(D, 0);\n    vector<double> day_sum(D, 0.0);\n\n    vector<int> idx_edges(M);\n    iota(idx_edges.begin(), idx_edges.end(), 0);\n    sort(idx_edges.begin(), idx_edges.end(), [&](int i, int j) { return bc[i] > bc[j]; });\n\n    for (int i : idx_edges) {\n        int best_d = -1;\n        double min_sum = 1e18;\n        for (int d = 0; d < D; d++) {\n            if (day_count[d] < K) {\n                if (day_sum[d] < min_sum) {\n                    min_sum = day_sum[d];\n                    best_d = d;\n                }\n            }\n        }\n        if (best_d == -1) {\n            for (int d = 0; d < D; d++) {\n                if (day_count[d] < K) {\n                    best_d = d;\n                    break;\n                }\n            }\n            if (best_d == -1) {\n                best_d = 0;\n            }\n        }\n        assignment[i] = best_d;\n        day_count[best_d]++;\n        day_sum[best_d] += bc[i];\n    }\n\n    vector<vector<int>> adj_uw(N);\n    for (int i = 0; i < M; i++) {\n        int u = get<0>(edges[i]);\n        int v = get<1>(edges[i]);\n        adj_uw[u].push_back(v);\n        adj_uw[v].push_back(u);\n    }\n\n    random_device rd;\n    mt19937 gen(rd());\n\n    for (int d = 0; d < D; d++) {\n        vector<vector<int>> adj_d(N);\n        for (int u = 0; u < N; u++) {\n            for (int v : adj_uw[u]) {\n                int a = min(u, v);\n                int b = max(u, v);\n                int eid = edgeMap[{a, b}];\n                if (assignment[eid] == d) continue;\n                adj_d[u].push_back(v);\n            }\n        }\n\n        vector<bool> vis(N, false);\n        queue<int> q;\n        q.push(0);\n        vis[0] = true;\n        int cnt = 0;\n        while (!q.empty()) {\n            int u = q.front(); q.pop();\n            cnt++;\n            for (int v : adj_d[u]) {\n                if (!vis[v]) {\n                    vis[v] = true;\n                    q.push(v);\n                }\n            }\n        }\n        if (cnt == N) continue;\n\n        vector<int> edges_d;\n        for (int i = 0; i < M; i++) {\n            if (assignment[i] == d) {\n                edges_d.push_back(i);\n            }\n        }\n\n        bool fixed = false;\n        if (edges_d.empty()) continue;\n\n        for (int attempt = 0; attempt < 100; attempt++) {\n            uniform_int_distribution<int> dist_e(0, edges_d.size()-1);\n            int idx_e = dist_e(gen);\n            int e = edges_d[idx_e];\n\n            int d2 = gen() % D;\n            if (d2 == d) continue;\n\n            vector<int> edges_d2;\n            for (int i = 0; i < M; i++) {\n                if (assignment[i] == d2) {\n                    edges_d2.push_back(i);\n                }\n            }\n            if (edges_d2.empty()) continue;\n\n            uniform_int_distribution<int> dist_f(0, edges_d2.size()-1);\n            int idx_f = dist_f(gen);\n            int f = edges_d2[idx_f];\n\n            assignment[e] = d2;\n            assignment[f] = d;\n\n            vector<vector<int>> adj_d1(N);\n            for (int u = 0; u < N; u++) {\n                for (int v : adj_uw[u]) {\n                    int a = min(u, v);\n                    int b = max(u, v);\n                    int eid_t = edgeMap[{a, b}];\n                    if (assignment[eid_t] == d) continue;\n                    adj_d1[u].push_back(v);\n                }\n            }\n            vector<bool> vis1(N, false);\n            queue<int> q1;\n            q1.push(0);\n            vis1[0] = true;\n            int cnt1 = 0;\n            while (!q1.empty()) {\n                int u = q1.front(); q1.pop();\n                cnt1++;\n                for (int v : adj_d1[u]) {\n                    if (!vis1[v]) {\n                        vis1[v] = true;\n                        q1.push(v);\n                    }\n                }\n            }\n            if (cnt1 != N) {\n                assignment[e] = d;\n                assignment[f] = d2;\n                continue;\n            }\n\n            vector<vector<int>> adj_d2(N);\n            for (int u = 0; u < N; u++) {\n                for (int v : adj_uw[u]) {\n                    int a = min(u, v);\n                    int b = max(u, v);\n                    int eid_t = edgeMap[{a, b}];\n                    if (assignment[eid_t] == d2) continue;\n                    adj_d2[u].push_back(v);\n                }\n            }\n            vector<bool> vis2(N, false);\n            queue<int> q2;\n            q2.push(0);\n            vis2[0] = true;\n            int cnt2 = 0;\n            while (!q2.empty()) {\n                int u = q2.front(); q2.pop();\n                cnt2++;\n                for (int v : adj_d2[u]) {\n                    if (!vis2[v]) {\n                        vis2[v] = true;\n                        q2.push(v);\n                    }\n                }\n            }\n            if (cnt2 != N) {\n                assignment[e] = d;\n                assignment[f] = d2;\n                continue;\n            }\n\n            fixed = true;\n            break;\n        }\n    }\n\n    for (int i = 0; i < M; i++) {\n        cout << assignment[i] + 1;\n        if (i < M-1) cout << \" \";\n    }\n    cout << endl;\n\n    return 0;\n}","ahc019":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <tuple>\n#include <algorithm>\n#include <set>\n#include <cctype>\n#include <map>\nusing namespace std;\n\nstruct Rotation {\n    int perm[3];\n    int sign[3];\n};\n\nvector<Rotation> generate_rotations() {\n    vector<int> perm = {0,1,2};\n    vector<Rotation> rots;\n    do {\n        int inv = 0;\n        for (int i=0; i<3; i++)\n            for (int j=i+1; j<3; j++)\n                if (perm[i] > perm[j]) inv++;\n        int perm_sign = (inv%2==0) ? 1 : -1;\n\n        for (int s=0; s<8; s++) {\n            int sx = (s&1) ? -1 : 1;\n            int sy = (s&2) ? -1 : 1;\n            int sz = (s&4) ? -1 : 1;\n            if (perm_sign * sx * sy * sz == 1) {\n                Rotation r;\n                r.perm[0] = perm[0]; r.sign[0] = sx;\n                r.perm[1] = perm[1]; r.sign[1] = sy;\n                r.perm[2] = perm[2]; r.sign[2] = sz;\n                rots.push_back(r);\n            }\n        }\n    } while (next_permutation(perm.begin(), perm.end()));\n    return rots;\n}\n\nint main() {\n    vector<Rotation> rots = generate_rotations();\n\n    int D;\n    cin >> D;\n    vector<vector<string>> f(2, vector<string>(D));\n    vector<vector<string>> r(2, vector<string>(D));\n    for (int i=0; i<2; i++) {\n        for (int j=0; j<D; j++) {\n            cin >> f[i][j];\n        }\n        for (int j=0; j<D; j++) {\n            cin >> r[i][j];\n        }\n    }\n\n    vector<vector<vector<vector<bool>>>> Allowed(2, vector<vector<vector<bool>>>(D, vector<vector<bool>>(D, vector<bool>(D, false))));\n    vector<vector<tuple<int,int,int>>> Base(2);\n\n    for (int obj=0; obj<2; obj++) {\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                    if (f[obj][z][x]=='1' && r[obj][z][y]=='1') {\n                        Allowed[obj][z][x][y] = true;\n                    }\n                }\n            }\n        }\n\n        for (int z=0; z<D; z++) {\n            vector<int> L, R;\n            for (int x=0; x<D; x++) {\n                if (f[obj][z][x]=='1') \n                    L.push_back(x);\n            }\n            for (int y=0; y<D; y++) {\n                if (r[obj][z][y]=='1') \n                    R.push_back(y);\n            }\n            if (L.empty() || R.empty()) \n                continue;\n            for (int x : L) {\n                Base[obj].push_back(make_tuple(x, R[0], z));\n            }\n            for (int j=1; j<R.size(); j++) {\n                Base[obj].push_back(make_tuple(L[0], R[j], z));\n            }\n        }\n    }\n\n    vector<tuple<int,int,int>> dirs = {{1,0,0}, {-1,0,0}, {0,1,0}, {0,-1,0}, {0,0,1}, {0,0,-1}};\n    vector<vector<vector<tuple<int,int,int>>>> blocks(2);\n\n    for (int obj=0; obj<2; obj++) {\n        vector<vector<vector<bool>>> visited(D, vector<vector<bool>>(D, vector<bool>(D, false)));\n        for (auto point : Base[obj]) {\n            int x = get<0>(point);\n            int y = get<1>(point);\n            int z = get<2>(point);\n            if (visited[z][x][y]) \n                continue;\n            queue<tuple<int,int,int>> q;\n            q.push(point);\n            visited[z][x][y] = true;\n            vector<tuple<int,int,int>> block;\n            while (!q.empty()) {\n                auto [cx, cy, cz] = q.front();\n                q.pop();\n                block.push_back(make_tuple(cx, cy, cz));\n                for (auto [dx,dy,dz] : dirs) {\n                    int nx = cx + dx;\n                    int ny = cy + dy;\n                    int nz = cz + dz;\n                    if (nx>=0 && nx<D && ny>=0 && ny<D && nz>=0 && nz<D) {\n                        if (!visited[nz][nx][ny] && Allowed[obj][nz][nx][ny]) {\n                            visited[nz][nx][ny] = true;\n                            q.push(make_tuple(nx, ny, nz));\n                        }\n                    }\n                }\n            }\n            blocks[obj].push_back(block);\n        }\n    }\n\n    vector<vector<vector<tuple<int,int,int>>>> canonical_blocks(2);\n    for (int obj=0; obj<2; obj++) {\n        for (auto &block : blocks[obj]) {\n            vector<vector<tuple<int,int,int>>> candidates;\n            for (auto &rot : rots) {\n                vector<tuple<int,int,int>> transformed;\n                for (auto [x,y,z] : block) {\n                    int coords[3] = {x, y, z};\n                    int nx = rot.sign[0] * coords[rot.perm[0]];\n                    int ny = rot.sign[1] * coords[rot.perm[1]];\n                    int nz = rot.sign[2] * coords[rot.perm[2]];\n                    transformed.push_back(make_tuple(nx, ny, nz));\n                }\n                int min_x = get<0>(transformed[0]), min_y = get<1>(transformed[0]), min_z = get<2>(transformed[0]);\n                for (auto &p : transformed) {\n                    min_x = min(min_x, get<0>(p));\n                    min_y = min(min_y, get<1>(p));\n                    min_z = min(min_z, get<2>(p));\n                }\n                vector<tuple<int,int,int>> shifted;\n                for (auto p : transformed) {\n                    shifted.push_back(make_tuple(get<0>(p)-min_x, get<1>(p)-min_y, get<2>(p)-min_z));\n                }\n                sort(shifted.begin(), shifted.end());\n                candidates.push_back(shifted);\n            }\n            sort(candidates.begin(), candidates.end());\n            canonical_blocks[obj].push_back(candidates[0]);\n        }\n    }\n\n    vector<tuple<int, vector<tuple<int,int,int>>, int>> list0;\n    for (int i=0; i<blocks[0].size(); i++) {\n        list0.push_back(make_tuple(blocks[0][i].size(), canonical_blocks[0][i], i));\n    }\n    vector<tuple<int, vector<tuple<int,int,int>>, int>> list1;\n    for (int i=0; i<blocks[1].size(); i++) {\n        list1.push_back(make_tuple(blocks[1][i].size(), canonical_blocks[1][i], i));\n    }\n\n    sort(list0.rbegin(), list0.rend());\n    sort(list1.rbegin(), list1.rend());\n\n    vector<int> global_id_counter = {1};\n    vector<int> mapping0(blocks[0].size(), -1);\n    vector<int> mapping1(blocks[1].size(), -1);\n\n    for (int i=0; i<list0.size(); i++) {\n        auto [vol0, canon0, idx0] = list0[i];\n        if (mapping0[idx0] != -1) continue;\n        for (int j=0; j<list1.size(); j++) {\n            auto [vol1, canon1, idx1] = list1[j];\n            if (mapping1[idx1] != -1) continue;\n            if (canon0 == canon1) {\n                mapping0[idx0] = global_id_counter[0];\n                mapping1[idx1] = global_id_counter[0];\n                global_id_counter[0]++;\n                break;\n            }\n        }\n    }\n\n    for (int i=0; i<blocks[0].size(); i++) {\n        if (mapping0[i] == -1) {\n            mapping0[i] = global_id_counter[0];\n            global_id_counter[0]++;\n        }\n    }\n    for (int i=0; i<blocks[1].size(); i++) {\n        if (mapping1[i] == -1) {\n            mapping1[i] = global_id_counter[0];\n            global_id_counter[0]++;\n        }\n    }\n\n    int n_blocks = global_id_counter[0] - 1;\n\n    vector<vector<vector<int>>> grid0(D, vector<vector<int>>(D, vector<int>(D,0)));\n    for (int idx=0; idx<blocks[0].size(); idx++) {\n        int gid = mapping0[idx];\n        for (auto [x,y,z] : blocks[0][idx]) {\n            grid0[x][y][z] = gid;\n        }\n    }\n\n    vector<vector<vector<int>>> grid1(D, vector<vector<int>>(D, vector<int>(D,0)));\n    for (int idx=0; idx<blocks[1].size(); idx++) {\n        int gid = mapping1[idx];\n        for (auto [x,y,z] : blocks[1][idx]) {\n            grid1[x][y][z] = gid;\n        }\n    }\n\n    vector<int> flat0, flat1;\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                flat0.push_back(grid0[x][y][z]);\n                flat1.push_back(grid1[x][y][z]);\n            }\n        }\n    }\n\n    cout << n_blocks << endl;\n    for (int i=0; i<flat0.size(); i++) {\n        if (i>0) cout << \" \";\n        cout << flat0[i];\n    }\n    cout << endl;\n    for (int i=0; i<flat1.size(); i++) {\n        if (i>0) cout << \" \";\n        cout << flat1[i];\n    }\n    cout << endl;\n\n    return 0;\n}","ahc020":"#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef tuple<int, ll, int> EdgeTuple;\n\nstruct Edge {\n    int u, v, w, id;\n};\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, K;\n    cin >> N >> M >> K;\n\n    vector<ll> x(N), y(N);\n    for (int i = 0; i < N; i++) {\n        cin >> x[i] >> y[i];\n    }\n\n    vector<Edge> edges;\n    for (int j = 0; j < M; j++) {\n        int u, v, w;\n        cin >> u >> v >> w;\n        u--; v--;\n        edges.push_back({u, v, w, j});\n    }\n\n    vector<ll> a(K), b(K);\n    for (int k = 0; k < K; k++) {\n        cin >> a[k] >> b[k];\n    }\n\n    vector<int> assigned_station(K, -1);\n    vector<vector<int>> cluster(N);\n\n    for (int k = 0; k < K; k++) {\n        ll min_d_sq = 1e18;\n        int best_i = -1;\n        for (int i = 0; i < N; i++) {\n            ll dx = a[k] - x[i];\n            ll dy = b[k] - y[i];\n            ll d_sq = dx*dx + dy*dy;\n            if (d_sq < min_d_sq) {\n                min_d_sq = d_sq;\n                best_i = i;\n            }\n        }\n        assigned_station[k] = best_i;\n        cluster[best_i].push_back(k);\n    }\n\n    vector<ll> P(N, 0);\n    vector<bool> in_S(N, false);\n\n    for (int i = 0; i < N; i++) {\n        if (cluster[i].empty()) {\n            P[i] = 0;\n        } else {\n            in_S[i] = true;\n            ll max_d_sq = 0;\n            for (int k : cluster[i]) {\n                ll dx = a[k] - x[i];\n                ll dy = b[k] - y[i];\n                ll d_sq = dx*dx + dy*dy;\n                if (d_sq > max_d_sq) max_d_sq = d_sq;\n            }\n            if (max_d_sq == 0) {\n                P[i] = 0;\n            } else {\n                ll candidate = sqrt(max_d_sq);\n                if (candidate * candidate < max_d_sq) {\n                    candidate++;\n                }\n                P[i] = candidate;\n                if (P[i] > 5000) {\n                    P[i] = 5000;\n                }\n            }\n        }\n    }\n\n    set<int> T_set;\n    for (int i = 0; i < N; i++) {\n        if (in_S[i]) {\n            T_set.insert(i);\n        }\n    }\n    T_set.insert(0);\n\n    vector<vector<EdgeTuple>> adj(N);\n    for (const Edge& e : edges) {\n        int u = e.u, v = e.v;\n        ll w = e.w;\n        int id = e.id;\n        adj[u].push_back(make_tuple(v, w, id));\n        adj[v].push_back(make_tuple(u, w, id));\n    }\n\n    vector<bool> in_tree(N, false);\n    vector<bool> edge_used(M, false);\n\n    in_tree[0] = true;\n    set<int> T_remaining = T_set;\n    T_remaining.erase(0);\n\n    while (!T_remaining.empty()) {\n        vector<ll> dist(N, 1e18);\n        vector<int> parent_node(N, -1);\n        vector<int> parent_edge_id(N, -1);\n        priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<>> pq;\n\n        for (int i = 0; i < N; i++) {\n            if (in_tree[i]) {\n                dist[i] = 0;\n                pq.push({0, i});\n            }\n        }\n\n        bool found = false;\n        int found_node = -1;\n\n        while (!pq.empty()) {\n            auto [d, u] = pq.top();\n            pq.pop();\n            if (d != dist[u]) continue;\n\n            if (T_remaining.find(u) != T_remaining.end()) {\n                found = true;\n                found_node = u;\n                break;\n            }\n\n            for (auto &edge : adj[u]) {\n                int v = get<0>(edge);\n                ll w = get<1>(edge);\n                int eid = get<2>(edge);\n                if (dist[v] > dist[u] + w) {\n                    dist[v] = dist[u] + w;\n                    parent_node[v] = u;\n                    parent_edge_id[v] = eid;\n                    pq.push({dist[v], v});\n                }\n            }\n        }\n\n        if (!found) {\n            break;\n        }\n\n        int cur = found_node;\n        while (!in_tree[cur]) {\n            edge_used[parent_edge_id[cur]] = true;\n            in_tree[cur] = true;\n            cur = parent_node[cur];\n        }\n\n        T_remaining.erase(found_node);\n    }\n\n    vector<bool> in_steiner(N, false);\n    {\n        queue<int> q;\n        q.push(0);\n        in_steiner[0] = true;\n        while (!q.empty()) {\n            int u = q.front(); q.pop();\n            for (auto &edge : adj[u]) {\n                int v = get<0>(edge);\n                int id = get<2>(edge);\n                if (edge_used[id] && !in_steiner[v]) {\n                    in_steiner[v] = true;\n                    q.push(v);\n                }\n            }\n        }\n    }\n\n    vector<ll> P_reassign(N, 0);\n    {\n        vector<pair<ll, int>> resident_min_dist;\n        for (int k = 0; k < K; k++) {\n            ll min_d_sq = 1e18;\n            for (int i = 0; i < N; i++) {\n                if (!in_steiner[i]) continue;\n                ll dx = x[i] - a[k];\n                ll dy = y[i] - b[k];\n                ll d_sq = dx*dx + dy*dy;\n                if (d_sq < min_d_sq) {\n                    min_d_sq = d_sq;\n                }\n            }\n            resident_min_dist.push_back({min_d_sq, k});\n        }\n        sort(resident_min_dist.begin(), resident_min_dist.end());\n\n        for (auto [d_sq_val, k] : resident_min_dist) {\n            ll best_increase = 1e18;\n            int best_station = -1;\n            for (int i = 0; i < N; i++) {\n                if (!in_steiner[i]) continue;\n                ll dx = x[i] - a[k];\n                ll dy = y[i] - b[k];\n                ll d_sq = dx*dx + dy*dy;\n                ll d = sqrt(d_sq);\n                if (d*d < d_sq) d++;\n\n                ll current_power = P_reassign[i];\n                if (d <= current_power) {\n                    if (0 < best_increase) {\n                        best_increase = 0;\n                        best_station = i;\n                    }\n                } else {\n                    ll increase = d*d - current_power*current_power;\n                    if (increase < best_increase) {\n                        best_increase = increase;\n                        best_station = i;\n                    }\n                }\n            }\n            if (best_station != -1) {\n                ll dx = x[best_station] - a[k];\n                ll dy = y[best_station] - b[k];\n                ll d_sq = dx*dx + dy*dy;\n                ll d = sqrt(d_sq);\n                if (d*d < d_sq) d++;\n                if (d > P_reassign[best_station]) {\n                    P_reassign[best_station] = d;\n                }\n            }\n        }\n    }\n\n    for (int i = 0; i < N; i++) {\n        if (in_steiner[i]) {\n            if (P_reassign[i] > 5000) {\n                P[i] = 5000;\n            } else {\n                P[i] = P_reassign[i];\n            }\n        } else {\n            P[i] = 0;\n        }\n    }\n\n    for (int i = 0; i < N; i++) {\n        cout << P[i];\n        if (i < N-1) cout << \" \";\n    }\n    cout << endl;\n\n    for (int j = 0; j < M; j++) {\n        cout << (edge_used[j] ? 1 : 0);\n        if (j < M-1) cout << \" \";\n    }\n    cout << endl;\n\n    return 0;\n}","ahc021":"#include <iostream>\n#include <vector>\n#include <tuple>\nusing namespace std;\n\nint main() {\n    int grid[30][30];\n\n    for (int i = 0; i < 30; i++) {\n        for (int j = 0; j <= i; j++) {\n            cin >> grid[i][j];\n        }\n    }\n\n    vector<tuple<int, int, int, int>> ops;\n\n    for (int x = 28; x >= 0; x--) {\n        for (int y = 0; y <= x; y++) {\n            int cx = x;\n            int cy = y;\n            while (true) {\n                int minChildVal = 1000000;\n                int minChildX = -1;\n                int minChildY = -1;\n\n                if (cx + 1 < 30) {\n                    if (grid[cx+1][cy] < minChildVal) {\n                        minChildVal = grid[cx+1][cy];\n                        minChildX = cx+1;\n                        minChildY = cy;\n                    }\n                    if (grid[cx+1][cy+1] < minChildVal) {\n                        minChildVal = grid[cx+1][cy+1];\n                        minChildX = cx+1;\n                        minChildY = cy+1;\n                    }\n                }\n\n                if (minChildX != -1 && minChildVal < grid[cx][cy]) {\n                    swap(grid[cx][cy], grid[minChildX][minChildY]);\n                    ops.push_back(make_tuple(cx, cy, minChildX, minChildY));\n                    cx = minChildX;\n                    cy = minChildY;\n                } else {\n                    break;\n                }\n            }\n        }\n    }\n\n    cout << ops.size() << endl;\n    for (auto op : ops) {\n        cout << get<0>(op) << \" \" << get<1>(op) << \" \" << get<2>(op) << \" \" << get<3>(op) << endl;\n    }\n\n    return 0;\n}","toyota2023summer-final":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\n#include <functional>\n#include <tuple>\n#include <climits>\n#include <cmath>\n\nusing namespace std;\n\nint main() {\n    int D = 9;\n    int N;\n    cin >> D >> N;\n\n    vector<vector<int>> grid(D, vector<int>(D, -3));\n    grid[0][(D-1)/2] = -2;\n\n    for (int i = 0; i < N; i++) {\n        int ri, rj;\n        cin >> ri >> rj;\n        grid[ri][rj] = -1;\n    }\n\n    vector<vector<int>> parent_i(D, vector<int>(D, -1));\n    vector<vector<int>> parent_j(D, vector<int>(D, -1));\n    vector<vector<vector<pair<int, int>>>> children(D, vector<vector<pair<int, int>>>(D));\n\n    vector<int> dx = {0, 1, 0, -1};\n    vector<int> dy = {1, 0, -1, 0};\n\n    queue<pair<int, int>> q_bfs_parent;\n    q_bfs_parent.push({0, (D-1)/2});\n    parent_i[0][(D-1)/2] = -1;\n    parent_j[0][(D-1)/2] = -1;\n\n    while (!q_bfs_parent.empty()) {\n        auto [i, j] = q_bfs_parent.front();\n        q_bfs_parent.pop();\n        for (int d = 0; d < 4; d++) {\n            int ni = i + dx[d];\n            int nj = j + dy[d];\n            if (ni >= 0 && ni < D && nj >= 0 && nj < D) {\n                if (parent_i[ni][nj] == -1) {\n                    if (grid[ni][nj] != -1) {\n                        parent_i[ni][nj] = i;\n                        parent_j[ni][nj] = j;\n                        q_bfs_parent.push({ni, nj});\n                    }\n                }\n            }\n        }\n    }\n\n    for (int i = 0; i < D; i++) {\n        for (int j = 0; j < D; j++) {\n            if (i == 0 && j == (D-1)/2) continue;\n            if (parent_i[i][j] != -1) {\n                int pi = parent_i[i][j];\n                int pj = parent_j[i][j];\n                children[pi][pj].push_back({i, j});\n            }\n        }\n    }\n\n    for (int i = 0; i < D; i++) {\n        for (int j = 0; j < D; j++) {\n            sort(children[i][j].begin(), children[i][j].end());\n        }\n    }\n\n    vector<vector<int>> post_index(D, vector<int>(D, -1));\n    int index_counter = 0;\n\n    function<void(int, int)> dfs = [&](int i, int j) {\n        for (auto& child : children[i][j]) {\n            dfs(child.first, child.second);\n        }\n        if (!(i == 0 && j == (D-1)/2)) {\n            post_index[i][j] = index_counter;\n            index_counter++;\n        }\n    };\n\n    for (auto& child : children[0][(D-1)/2]) {\n        dfs(child.first, child.second);\n    }\n\n    int M = D * D - 1 - N;\n\n    for (int d = 0; d < M; d++) {\n        int t_d;\n        cin >> t_d;\n\n        vector<vector<bool>> visited(D, vector<bool>(D, false));\n        vector<pair<int, int>> free_list;\n        queue<pair<int, int>> q_bfs;\n        q_bfs.push({0, (D-1)/2});\n        visited[0][(D-1)/2] = true;\n\n        while (!q_bfs.empty()) {\n            auto [i, j] = q_bfs.front();\n            q_bfs.pop();\n            if (grid[i][j] == -3) {\n                free_list.push_back({i, j});\n            }\n            for (int dir = 0; dir < 4; dir++) {\n                int ni = i + dx[dir];\n                int nj = j + dy[dir];\n                if (ni >= 0 && ni < D && nj >= 0 && nj < D && !visited[ni][nj]) {\n                    if (grid[ni][nj] == -1 || grid[ni][nj] >= 0) continue;\n                    visited[ni][nj] = true;\n                    q_bfs.push({ni, nj});\n                }\n            }\n        }\n\n        int best_i = -1, best_j = -1;\n        int best_diff = INT_MAX;\n        for (auto [i, j] : free_list) {\n            if (post_index[i][j] == -1) continue;\n            int diff = abs(post_index[i][j] - t_d);\n            if (diff < best_diff || (diff == best_diff && (i < best_i || (i == best_i && j < best_j)))) {\n                best_diff = diff;\n                best_i = i;\n                best_j = j;\n            }\n        }\n\n        if (best_i == -1) {\n            for (auto [i, j] : free_list) {\n                best_i = i;\n                best_j = j;\n                break;\n            }\n        }\n\n        cout << best_i << \" \" << best_j << endl;\n        cout.flush();\n        grid[best_i][best_j] = t_d;\n    }\n\n    vector<pair<int, int>> removal_sequence;\n\n    for (int step = 0; step < M; step++) {\n        vector<vector<bool>> visited(D, vector<bool>(D, false));\n        queue<pair<int, int>> q_bfs;\n        q_bfs.push({0, (D-1)/2});\n        visited[0][(D-1)/2] = true;\n\n        while (!q_bfs.empty()) {\n            auto [i, j] = q_bfs.front();\n            q_bfs.pop();\n            for (int dir = 0; dir < 4; dir++) {\n                int ni = i + dx[dir];\n                int nj = j + dy[dir];\n                if (ni >= 0 && ni < D && nj >= 0 && nj < D && !visited[ni][nj]) {\n                    if (grid[ni][nj] == -1) continue;\n                    if (grid[ni][nj] >= 0) continue;\n                    visited[ni][nj] = true;\n                    q_bfs.push({ni, nj});\n                }\n            }\n        }\n\n        vector<tuple<int, int, int>> removable;\n        for (int i = 0; i < D; i++) {\n            for (int j = 0; j < D; j++) {\n                if (grid[i][j] >= 0) {\n                    bool flag = false;\n                    for (int dir = 0; dir < 4; dir++) {\n                        int ni = i + dx[dir];\n                        int nj = j + dy[dir];\n                        if (ni >= 0 && ni < D && nj >= 0 && nj < D) {\n                            if (visited[ni][nj]) {\n                                flag = true;\n                                break;\n                            }\n                        }\n                    }\n                    if (flag) {\n                        removable.push_back(make_tuple(grid[i][j], i, j));\n                    }\n                }\n            }\n        }\n\n        if (removable.empty()) {\n            for (int i = 0; i < D; i++) {\n                for (int j = 0; j < D; j++) {\n                    if (grid[i][j] >= 0) {\n                        removable.push_back(make_tuple(grid[i][j], i, j));\n                    }\n                }\n            }\n            sort(removable.begin(), removable.end());\n        } else {\n            sort(removable.begin(), removable.end());\n        }\n\n        if (removable.empty()) {\n            break;\n        }\n\n        auto [num, i, j] = removable[0];\n        removal_sequence.push_back({i, j});\n        grid[i][j] = -3;\n    }\n\n    for (auto [i, j] : removal_sequence) {\n        cout << i << \" \" << j << endl;\n    }\n    cout.flush();\n\n    return 0;\n}","ahc024":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\n#include <cstring>\nusing namespace std;\n\ntypedef pair<int, int> pii;\n\nconst int dx[4] = {1, 0, -1, 0};\nconst int dy[4] = {0, 1, 0, -1};\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int n, m;\n    cin >> n >> m;\n    vector<vector<int>> grid(n, vector<int>(n));\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            cin >> grid[i][j];\n        }\n    }\n\n    vector<vector<bool>> orig_adj(101, vector<bool>(101, false));\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            if (i == 0 || i == n-1 || j == 0 || j == n-1) {\n                int c = grid[i][j];\n                if (c > 0) {\n                    orig_adj[0][c] = true;\n                }\n            }\n        }\n    }\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            if (i < n-1 && grid[i][j] != grid[i+1][j] && grid[i][j] > 0 && grid[i+1][j] > 0) {\n                int c1 = min(grid[i][j], grid[i+1][j]);\n                int c2 = max(grid[i][j], grid[i+1][j]);\n                orig_adj[c1][c2] = true;\n            }\n            if (j < n-1 && grid[i][j] != grid[i][j+1] && grid[i][j] > 0 && grid[i][j+1] > 0) {\n                int c1 = min(grid[i][j], grid[i][j+1]);\n                int c2 = max(grid[i][j], grid[i][j+1]);\n                orig_adj[c1][c2] = true;\n            }\n        }\n    }\n\n    vector<vector<int>> current_adj_count(101, vector<int>(101, 0));\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            if (i == 0 || i == n-1 || j == 0 || j == n-1) {\n                int c = grid[i][j];\n                if (c > 0) {\n                    current_adj_count[0][c]++;\n                }\n            }\n        }\n    }\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            if (i < n-1 && grid[i][j] != grid[i+1][j] && grid[i][j] > 0 && grid[i+1][j] > 0) {\n                int c1 = min(grid[i][j], grid[i+1][j]);\n                int c2 = max(grid[i][j], grid[i+1][j]);\n                current_adj_count[c1][c2]++;\n            }\n            if (j < n-1 && grid[i][j] != grid[i][j+1] && grid[i][j] > 0 && grid[i][j+1] > 0) {\n                int c1 = min(grid[i][j], grid[i][j+1]);\n                int c2 = max(grid[i][j], grid[i][j+1]);\n                current_adj_count[c1][c2]++;\n            }\n        }\n    }\n\n    vector<int> ward_size(m+1, 0);\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            int c = grid[i][j];\n            if (c > 0) {\n                ward_size[c]++;\n            }\n        }\n    }\n\n    int num_zeros = 0;\n    vector<vector<bool>> in_queue(n, vector<bool>(n, true));\n    queue<pii> candidate_queue;\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            candidate_queue.push({i, j});\n        }\n    }\n\n    while (!candidate_queue.empty()) {\n        auto [i, j] = candidate_queue.front();\n        candidate_queue.pop();\n        in_queue[i][j] = false;\n        if (grid[i][j] == 0) continue;\n\n        int c = grid[i][j];\n        if (ward_size[c] == 1) {\n            continue;\n        }\n\n        vector<pii> neighbors_in_c;\n        for (int d = 0; d < 4; d++) {\n            int ni = i + dx[d];\n            int nj = j + dy[d];\n            if (ni < 0 || ni >= n || nj < 0 || nj >= n) continue;\n            if (grid[ni][nj] == c) {\n                neighbors_in_c.push_back({ni, nj});\n            }\n        }\n        if (neighbors_in_c.empty()) {\n            continue;\n        }\n\n        int total_after = ward_size[c] - 1;\n        vector<vector<bool>> visited(n, vector<bool>(n, false));\n        queue<pii> q_bfs;\n        auto [start_i, start_j] = neighbors_in_c[0];\n        q_bfs.push({start_i, start_j});\n        visited[start_i][start_j] = true;\n        int count_visited = 1;\n\n        while (!q_bfs.empty()) {\n            auto [x, y] = q_bfs.front();\n            q_bfs.pop();\n            for (int d = 0; d < 4; d++) {\n                int nx = x + dx[d];\n                int ny = y + dy[d];\n                if (nx < 0 || nx >= n || ny < 0 || ny >= n) continue;\n                if (grid[nx][ny] != c) continue;\n                if (visited[nx][ny]) continue;\n                if (nx == i && ny == j) continue;\n                visited[nx][ny] = true;\n                count_visited++;\n                q_bfs.push({nx, ny});\n            }\n        }\n\n        if (count_visited != total_after) {\n            continue;\n        }\n\n        bool skip = false;\n        for (int d = 0; d < 4; d++) {\n            int ni = i + dx[d];\n            int nj = j + dy[d];\n            if (ni < 0 || ni >= n || nj < 0 || nj >= n) continue;\n            int d_val = grid[ni][nj];\n            if (d_val == c) continue;\n            if (d_val == 0) {\n                if (current_adj_count[0][c] <= 2 && orig_adj[0][c]) {\n                    skip = true;\n                    break;\n                }\n            } else {\n                int c1 = min(c, d_val);\n                int c2 = max(c, d_val);\n                if (current_adj_count[c1][c2] <= 2 && orig_adj[c1][c2]) {\n                    skip = true;\n                    break;\n                }\n            }\n        }\n        if (skip) continue;\n\n        for (int d = 0; d < 4; d++) {\n            int ni = i + dx[d];\n            int nj = j + dy[d];\n            if (ni < 0 || ni >= n || nj < 0 || nj >= n) continue;\n            int d_val = grid[ni][nj];\n            if (d_val > 0 && d_val != c) {\n                if (!orig_adj[0][d_val]) {\n                    skip = true;\n                    break;\n                }\n            }\n        }\n        if (skip) continue;\n\n        if (num_zeros > 0) {\n            bool has_zero_neighbor = false;\n            for (int d = 0; d < 4; d++) {\n                int ni = i + dx[d];\n                int nj = j + dy[d];\n                if (ni < 0 || ni >= n || nj < 0 || nj >= n) continue;\n                if (grid[ni][nj] == 0) {\n                    has_zero_neighbor = true;\n                    break;\n                }\n            }\n            if (!has_zero_neighbor) {\n                continue;\n            }\n        }\n\n        for (int d = 0; d < 4; d++) {\n            int ni = i + dx[d];\n            int nj = j + dy[d];\n            if (ni < 0 || ni >= n || nj < 0 || nj >= n) continue;\n            int d_val = grid[ni][nj];\n            if (d_val == c) continue;\n            if (d_val == 0) {\n                current_adj_count[0][c]--;\n            } else {\n                int c1 = min(c, d_val);\n                int c2 = max(c, d_val);\n                current_adj_count[c1][c2]--;\n            }\n        }\n\n        if (i == 0 || i == n-1 || j == 0 || j == n-1) {\n            current_adj_count[0][c]--;\n        }\n\n        grid[i][j] = 0;\n        num_zeros++;\n        ward_size[c]--;\n\n        for (int d = 0; d < 4; d++) {\n            int ni = i + dx[d];\n            int nj = j + dy[d];\n            if (ni < 0 || ni >= n || nj < 0 || nj >= n) continue;\n            int d_val = grid[ni][nj];\n            if (d_val > 0) {\n                current_adj_count[0][d_val]++;\n            }\n        }\n\n        for (int d = 0; d < 4; d++) {\n            int ni = i + dx[d];\n            int nj = j + dy[d];\n            if (ni < 0 || ni >= n || nj < 0 || nj >= n) continue;\n            if (grid[ni][nj] != 0 && !in_queue[ni][nj]) {\n                candidate_queue.push({ni, nj});\n                in_queue[ni][nj] = true;\n            }\n        }\n    }\n\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            if (j > 0) cout << \" \";\n            cout << grid[i][j];\n        }\n        cout << endl;\n    }\n\n    return 0;\n}","ahc025":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <random>\n#include <cstdlib>\n#include <ctime>\n#include <numeric>\n\nusing namespace std;\n\nint main() {\n    random_device rd;\n    mt19937 g(rd());\n\n    int N, D, Q;\n    cin >> N >> D >> Q;\n\n    vector<int> perm(N);\n    iota(perm.begin(), perm.end(), 0);\n    shuffle(perm.begin(), perm.end(), g);\n\n    vector<vector<int>> groups(D);\n    for (int i = 0; i < D; i++) {\n        groups[i].push_back(perm[i]);\n    }\n\n    int total_queries_used = 0;\n\n    for (int i = D; i < N; i++) {\n        int remaining_queries = Q - total_queries_used;\n        int remaining_items = N - i;\n\n        if (remaining_queries <= 0) {\n            uniform_int_distribution<int> dist(0, D-1);\n            int group_index = dist(g);\n            groups[group_index].push_back(perm[i]);\n        } else {\n            int k = min(D, 1 + remaining_queries / remaining_items);\n            if (k < 2) {\n                uniform_int_distribution<int> dist(0, D-1);\n                int group_index = dist(g);\n                groups[group_index].push_back(perm[i]);\n            } else {\n                vector<int> indices(D);\n                iota(indices.begin(), indices.end(), 0);\n                shuffle(indices.begin(), indices.end(), g);\n                vector<int> candidates(indices.begin(), indices.begin() + k);\n\n                while (candidates.size() > 1 && total_queries_used < Q) {\n                    int a = candidates[0];\n                    int b = candidates[1];\n                    cout << groups[a].size() << \" \" << groups[b].size();\n                    for (int item : groups[a]) {\n                        cout << \" \" << item;\n                    }\n                    for (int item : groups[b]) {\n                        cout << \" \" << item;\n                    }\n                    cout << endl;\n\n                    string res;\n                    cin >> res;\n                    total_queries_used++;\n\n                    if (res == \"<\") {\n                        candidates.erase(candidates.begin() + 1);\n                    } else if (res == \">\") {\n                        candidates.erase(candidates.begin());\n                    } else {\n                        candidates.erase(candidates.begin() + 1);\n                    }\n                }\n                int group_index = candidates[0];\n                groups[group_index].push_back(perm[i]);\n            }\n        }\n    }\n\n    while (total_queries_used < Q) {\n        cout << \"1 1 \" << perm[0] << \" \" << perm[1] << endl;\n        string res;\n        cin >> res;\n        total_queries_used++;\n    }\n\n    vector<int> ans(N);\n    for (int group_id = 0; group_id < D; group_id++) {\n        for (int item : groups[group_id]) {\n            ans[item] = group_id;\n        }\n    }\n\n    for (int i = 0; i < N; i++) {\n        if (i > 0) cout << \" \";\n        cout << ans[i];\n    }\n    cout << endl;\n\n    return 0;\n}","ahc026":"#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int n, m;\n    cin >> n >> m;\n    vector<vector<int>> stacks(m);\n    for (int i = 0; i < m; i++) {\n        for (int j = 0; j < n/m; j++) {\n            int x;\n            cin >> x;\n            stacks[i].push_back(x);\n        }\n    }\n\n    vector<pair<int, int>> operations;\n\n    for (int v = 1; v <= n; v++) {\n        int s = -1, pos = -1;\n        for (int i = 0; i < m; i++) {\n            for (int j = 0; j < stacks[i].size(); j++) {\n                if (stacks[i][j] == v) {\n                    s = i;\n                    pos = j;\n                    break;\n                }\n            }\n            if (s != -1) break;\n        }\n\n        if (s == -1) {\n            continue;\n        }\n\n        if (pos == (int)stacks[s].size() - 1) {\n            operations.push_back({v, 0});\n            stacks[s].pop_back();\n        } else {\n            int w = stacks[s][pos+1];\n            int d = -1;\n            int best_top = -1;\n\n            for (int i = 0; i < m; i++) {\n                if (i == s) continue;\n                if (stacks[i].empty()) {\n                    if (1000 > best_top) {\n                        best_top = 1000;\n                        d = i;\n                    }\n                } else {\n                    int top_val = stacks[i].back();\n                    if (top_val > best_top) {\n                        best_top = top_val;\n                        d = i;\n                    }\n                }\n            }\n\n            if (d == -1) {\n                d = (s == 0) ? 1 : 0;\n            }\n\n            vector<int> seg;\n            for (int j = pos+1; j < stacks[s].size(); j++) {\n                seg.push_back(stacks[s][j]);\n            }\n            stacks[s].resize(pos+1);\n            for (int box : seg) {\n                stacks[d].push_back(box);\n            }\n\n            operations.push_back({w, d+1});\n            operations.push_back({v, 0});\n            stacks[s].pop_back();\n        }\n    }\n\n    for (auto &op : operations) {\n        cout << op.first << \" \" << op.second << '\\n';\n    }\n\n    return 0;\n}","ahc027":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\n#include <cmath>\n#include <unordered_map>\n#include <cctype>\n#include <climits>\n#include <functional>\n#include <set>\n#include <iomanip>\n\nusing namespace std;\n\nconst int dx[4] = {0, 1, 0, -1};\nconst string dir_str = \"RDLU\";\nconst int dy[4] = {1, 0, -1, 0};\n\nstruct Cell {\n    int i, j;\n    Cell(int i=0, int j=0) : i(i), j(j) {}\n    bool operator==(const Cell& other) const {\n        return i == other.i && j == other.j;\n    }\n    bool operator!=(const Cell& other) const {\n        return !(*this == other);\n    }\n};\n\nnamespace std {\n    template <>\n    struct hash<Cell> {\n        size_t operator()(const Cell& c) const {\n            return c.i * 100 + c.j;\n        }\n    };\n}\n\nint get_index(int i, int j, int N) {\n    return i * N + j;\n}\n\nCell get_cell(int idx, int N) {\n    return Cell(idx / N, idx % N);\n}\n\nchar get_move_char(Cell from, Cell to) {\n    if (to.i == from.i + 1) return 'D';\n    if (to.i == from.i - 1) return 'U';\n    if (to.j == from.j + 1) return 'R';\n    if (to.j == from.j - 1) return 'L';\n    return '?';\n}\n\nchar reverse_move(char m) {\n    if (m == 'U') return 'D';\n    if (m == 'D') return 'U';\n    if (m == 'L') return 'R';\n    if (m == 'R') return 'L';\n    return m;\n}\n\nvector<vector<int>> precompute_all_pairs_shortest_path(int N, const vector<vector<bool>>& down_wall, const vector<vector<bool>>& right_wall) {\n    int total_cells = N * N;\n    vector<vector<int>> dist(total_cells, vector<int>(total_cells, INT_MAX));\n\n    for (int idx = 0; idx < total_cells; idx++) {\n        Cell src = get_cell(idx, N);\n        vector<int>& d_vec = dist[idx];\n        d_vec[idx] = 0;\n        queue<int> q;\n        q.push(idx);\n\n        while (!q.empty()) {\n            int u_idx = q.front();\n            q.pop();\n            Cell u = get_cell(u_idx, N);\n\n            for (int d = 0; d < 4; d++) {\n                Cell v = Cell(u.i + dx[d], u.j + dy[d]);\n                if (v.i < 0 || v.i >= N || v.j < 0 || v.j >= N) continue;\n                if (d == 0) {\n                    if (right_wall[u.i][u.j]) continue;\n                } else if (d == 1) {\n                    if (down_wall[u.i][u.j]) continue;\n                } else if (d == 2) {\n                    if (v.j < u.j && (u.j-1>=0) && right_wall[u.i][u.j-1]) continue;\n                } else if (d == 3) {\n                    if (v.i < u.i && (u.i-1>=0) && down_wall[u.i-1][u.j]) continue;\n                }\n\n                int v_idx = get_index(v.i, v.j, N);\n                if (d_vec[v_idx] == INT_MAX) {\n                    d_vec[v_idx] = d_vec[u_idx] + 1;\n                    q.push(v_idx);\n                }\n            }\n        }\n    }\n\n    return dist;\n}\n\nvector<vector<int>> precompute_parents_all(int N, const vector<vector<bool>>& down_wall, const vector<vector<bool>>& right_wall) {\n    int total_cells = N * N;\n    vector<vector<int>> parent_all(total_cells, vector<int>(total_cells, -1));\n\n    for (int src_idx = 0; src_idx < total_cells; src_idx++) {\n        vector<int>& parent = parent_all[src_idx];\n        vector<int> dist(total_cells, INT_MAX);\n        dist[src_idx] = 0;\n        parent[src_idx] = src_idx;\n        queue<int> q;\n        q.push(src_idx);\n\n        while (!q.empty()) {\n            int u_idx = q.front();\n            q.pop();\n            Cell u = get_cell(u_idx, N);\n\n            for (int d = 0; d < 4; d++) {\n                Cell v = Cell(u.i + dx[d], u.j + dy[d]);\n                if (v.i < 0 || v.i >= N || v.j < 0 || v.j >= N) continue;\n                if (d == 0) {\n                    if (right_wall[u.i][u.j]) continue;\n                } else if (d == 1) {\n                    if (down_wall[u.i][u.j]) continue;\n                } else if (d == 2) {\n                    if (v.j < u.j && (u.j-1>=0) && right_wall[u.i][u.j-1]) continue;\n                } else if (d == 3) {\n                    if (v.i < u.i && (u.i-1>=0) && down_wall[u.i-1][u.j]) continue;\n                }\n\n                int v_idx = get_index(v.i, v.j, N);\n                if (dist[v_idx] == INT_MAX) {\n                    dist[v_idx] = dist[u_idx] + 1;\n                    parent[v_idx] = u_idx;\n                    q.push(v_idx);\n                }\n            }\n        }\n    }\n\n    return parent_all;\n}\n\nstring generate_base_route(int N, const vector<vector<bool>>& down_wall, const vector<vector<bool>>& right_wall, const vector<vector<int>>& d) {\n    int total_cells = N * N;\n    vector<int> parent_tree(total_cells, -1);\n    vector<int> dist(total_cells, INT_MAX);\n    vector<vector<int>> children(total_cells);\n\n    queue<int> q;\n    dist[0] = 0;\n    parent_tree[0] = 0;\n    q.push(0);\n\n    while (!q.empty()) {\n        int u_idx = q.front(); q.pop();\n        Cell u = get_cell(u_idx, N);\n        for (int d = 0; d < 4; d++) {\n            Cell v = Cell(u.i + dx[d], u.j + dy[d]);\n            if (v.i < 0 || v.i >= N || v.j < 0 || v.j >= N) continue;\n            if (d == 0) {\n                if (right_wall[u.i][u.j]) continue;\n            } else if (d == 1) {\n                if (down_wall[u.i][u.j]) continue;\n            } else if (d == 2) {\n                if (v.j < u.j && (u.j-1>=0) && right_wall[u.i][u.j-1]) continue;\n            } else if (d == 3) {\n                if (v.i < u.i && (u.i-1>=0) && down_wall[u.i-1][u.j]) continue;\n            }\n\n            int v_idx = get_index(v.i, v.j, N);\n            if (dist[v_idx] == INT_MAX) {\n                dist[v_idx] = dist[u_idx] + 1;\n                parent_tree[v_idx] = u_idx;\n                q.push(v_idx);\n                children[u_idx].push_back(v_idx);\n            }\n        }\n    }\n\n    for (int i = 0; i < total_cells; i++) {\n        if (dist[i] == INT_MAX) {\n            cerr << \"Cell not reached: \" << i/N << \", \" << i%N << endl;\n        }\n    }\n\n    function<string(int)> dfs = [&](int u_idx) {\n        string route = \"\";\n        Cell u = get_cell(u_idx, N);\n        vector<int>& child_list = children[u_idx];\n        sort(child_list.begin(), child_list.end(), [&](int a, int b) {\n            return d[get_cell(a, N).i][get_cell(a, N).j] > d[get_cell(b, N).i][get_cell(b, N).j];\n        });\n\n        for (int v_idx : child_list) {\n            Cell v = get_cell(v_idx, N);\n            char move_char = get_move_char(u, v);\n            route += move_char;\n            route += dfs(v_idx);\n            route += reverse_move(move_char);\n        }\n        return route;\n    };\n\n    string base_route = dfs(0);\n    return base_route;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N;\n    cin >> N;\n    vector<string> h_lines(N-1);\n    vector<string> v_lines(N);\n    vector<vector<int>> d(N, vector<int>(N));\n\n    for (int i = 0; i < N-1; i++) {\n        cin >> h_lines[i];\n    }\n    for (int i = 0; i < N; i++) {\n        cin >> v_lines[i];\n    }\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cin >> d[i][j];\n        }\n    }\n\n    vector<vector<bool>> down_wall(N-1, vector<bool>(N, false));\n    vector<vector<bool>> right_wall(N, vector<bool>(N-1, false));\n\n    for (int i = 0; i < N-1; i++) {\n        for (int j = 0; j < N; j++) {\n            down_wall[i][j] = (h_lines[i][j] == '1');\n        }\n    }\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N-1; j++) {\n            right_wall[i][j] = (v_lines[i][j] == '1');\n        }\n    }\n\n    vector<vector<int>> all_dist = precompute_all_pairs_shortest_path(N, down_wall, right_wall);\n    vector<vector<int>> parent_all = precompute_parents_all(N, down_wall, right_wall);\n\n    string base_route = generate_base_route(N, down_wall, right_wall, d);\n    string current_route = base_route;\n\n    int total_cells = N * N;\n    vector<vector<int>> occ(total_cells);\n    vector<Cell> positions;\n    Cell cur(0, 0);\n    positions.push_back(cur);\n    for (int i = 0; i < current_route.size(); i++) {\n        char m = current_route[i];\n        Cell next = cur;\n        if (m == 'R') next.j++;\n        else if (m == 'D') next.i++;\n        else if (m == 'L') next.j--;\n        else if (m == 'U') next.i--;\n        positions.push_back(next);\n        cur = next;\n    }\n\n    for (int idx = 0; idx < positions.size(); idx++) {\n        Cell cell = positions[idx];\n        if (idx > 0) {\n            int cell_idx = get_index(cell.i, cell.j, N);\n            occ[cell_idx].push_back(idx-1);\n        }\n    }\n\n    int L0 = current_route.size();\n    vector<int> freq(total_cells, 0);\n    for (int i = 0; i < total_cells; i++) {\n        freq[i] = occ[i].size();\n    }\n\n    vector<double> gap_est(total_cells, 0.0);\n    for (int i = 0; i < total_cells; i++) {\n        if (freq[i] > 0) {\n            gap_est[i] = L0 * 1.0 / freq[i];\n        }\n    }\n\n    using pdi = pair<double, int>;\n    priority_queue<pdi> pq;\n    for (int i = 0; i < total_cells; i++) {\n        if (freq[i] > 0) {\n            double priority_val = d[get_cell(i, N).i][get_cell(i, N).j] * gap_est[i] * gap_est[i];\n            pq.push({priority_val, i});\n        }\n    }\n\n    int insertion_count = 0;\n    const int MAX_INSERTION = 1000;\n    vector<int> dummy_occ_count(total_cells, 0);\n\n    while (insertion_count < MAX_INSERTION && !pq.empty()) {\n        double priority_val = pq.top().first;\n        int c_idx = pq.top().second;\n        pq.pop();\n        if (freq[c_idx] == 0) continue;\n\n        int best_u_idx = -1;\n        int best_dist = INT_MAX;\n        for (int u_idx = 0; u_idx < total_cells; u_idx++) {\n            if (freq[u_idx] == 0) continue;\n            if (all_dist[u_idx][c_idx] < best_dist) {\n                best_dist = all_dist[u_idx][c_idx];\n                best_u_idx = u_idx;\n            }\n        }\n\n        if (best_u_idx == -1 || best_dist == INT_MAX) {\n            continue;\n        }\n\n        int detour_length = 2 * best_dist;\n        if (L0 + detour_length > 100000) {\n            continue;\n        }\n\n        if (occ[best_u_idx].empty()) {\n            continue;\n        }\n        int insert_move_index = occ[best_u_idx][0];\n\n        Cell best_u = get_cell(best_u_idx, N);\n        Cell c = get_cell(c_idx, N);\n        string moves_to_c = \"\";\n        int current_idx = best_u_idx;\n        while (current_idx != c_idx) {\n            int next_idx = parent_all[c_idx][current_idx];\n            if (next_idx == -1) break;\n            Cell cur_cell = get_cell(current_idx, N);\n            Cell next_cell = get_cell(next_idx, N);\n            char m = get_move_char(cur_cell, next_cell);\n            moves_to_c += m;\n            current_idx = next_idx;\n        }\n\n        string moves_back = \"\";\n        for (int i = moves_to_c.size() - 1; i >= 0; i--) {\n            moves_back += reverse_move(moves_to_c[i]);\n        }\n        string detour = moves_to_c + moves_back;\n\n        string new_route = current_route.substr(0, insert_move_index+1) + detour + current_route.substr(insert_move_index+1);\n        int new_L = new_route.size();\n\n        vector<Cell> new_positions;\n        vector<vector<int>> new_occ(total_cells);\n        Cell pos(0,0);\n        new_positions.push_back(pos);\n        for (int i = 0; i < new_route.size(); i++) {\n            char m = new_route[i];\n            Cell next = pos;\n            if (m == 'R') next.j++;\n            else if (m == 'D') next.i++;\n            else if (m == 'L') next.j--;\n            else if (m == 'U') next.i--;\n            new_positions.push_back(next);\n            int cell_idx = get_index(next.i, next.j, N);\n            new_occ[cell_idx].push_back(i);\n            pos = next;\n        }\n\n        current_route = new_route;\n        L0 = new_L;\n        for (int i = 0; i < total_cells; i++) {\n            freq[i] = new_occ[i].size();\n            if (freq[i] > 0) {\n                gap_est[i] = L0 * 1.0 / freq[i];\n            }\n        }\n        occ = new_occ;\n\n        priority_queue<pdi> new_pq;\n        for (int i = 0; i < total_cells; i++) {\n            if (freq[i] > 0) {\n                double new_priority = d[get_cell(i, N).i][get_cell(i, N).j] * gap_est[i] * gap_est[i];\n                new_pq.push({new_priority, i});\n            }\n        }\n        pq = new_pq;\n\n        insertion_count++;\n    }\n\n    cout << current_route << endl;\n\n    return 0;\n}","ahc028":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <unordered_set>\n#include <climits>\n#include <tuple>\n#include <cmath>\n\nusing namespace std;\n\nconst int INF = 1e8;\nconst int N_grid = 15;\n\nvoid manhattan_transform(vector<vector<int>>& grid) {\n    int n = grid.size();\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            if (i > 0) grid[i][j] = min(grid[i][j], grid[i-1][j] + 1);\n            if (j > 0) grid[i][j] = min(grid[i][j], grid[i][j-1] + 1);\n        }\n    }\n    for (int i = n-1; i >=0; i--) {\n        for (int j = n-1; j >=0; j--) {\n            if (i+1 < n) grid[i][j] = min(grid[i][j], grid[i+1][j] + 1);\n            if (j+1 < n) grid[i][j] = min(grid[i][j], grid[i][j+1] + 1);\n        }\n    }\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M;\n    cin >> N >> M;\n    int s_i, s_j;\n    cin >> s_i >> s_j;\n\n    vector<string> grid(N);\n    for (int i = 0; i < N; i++) {\n        cin >> grid[i];\n    }\n\n    vector<string> targets;\n    for (int i = 0; i < M; i++) {\n        string t;\n        cin >> t;\n        targets.push_back(t);\n    }\n\n    unordered_set<string> target_set(targets.begin(), targets.end());\n    vector<vector<pair<int, int>>> positions(26);\n\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            char c = grid[i][j];\n            positions[c-'A'].push_back({i, j});\n        }\n    }\n\n    vector<vector<int>> d_table(26, vector<int>(26, INF));\n\n    for (int b = 0; b < 26; b++) {\n        vector<vector<int>> dist(N, vector<int>(N, INF));\n        for (auto [x, y] : positions[b]) {\n            dist[x][y] = 0;\n        }\n        manhattan_transform(dist);\n\n        for (int a = 0; a < 26; a++) {\n            int min_val = INF;\n            for (auto [x, y] : positions[a]) {\n                if (dist[x][y] < min_val) {\n                    min_val = dist[x][y];\n                }\n            }\n            d_table[a][b] = min_val;\n        }\n    }\n\n    string S = \"\";\n    unordered_set<string> covered;\n    int cur_x = s_i, cur_y = s_j;\n    vector<pair<int, int>> operations;\n\n    while (covered.size() < M && operations.size() < 5000) {\n        vector<int> min_dist_letter(26, INF);\n        for (int c = 0; c < 26; c++) {\n            for (auto [x, y] : positions[c]) {\n                int d = abs(cur_x - x) + abs(cur_y - y);\n                if (d < min_dist_letter[c]) {\n                    min_dist_letter[c] = d;\n                }\n            }\n        }\n\n        vector<tuple<double, int, string, double>> candidate_list;\n        for (string t : targets) {\n            if (covered.find(t) != covered.end()) continue;\n            int lb = min_dist_letter[t[0]-'A'] \n                     + d_table[t[0]-'A'][t[1]-'A'] \n                     + d_table[t[1]-'A'][t[2]-'A'] \n                     + d_table[t[2]-'A'][t[3]-'A'] \n                     + d_table[t[3]-'A'][t[4]-'A'] \n                     + 5;\n            string last_part = (S.length() >= 4) ? S.substr(S.length()-4) : S;\n            string candidate_string = last_part + t;\n            unordered_set<string> new_grams;\n            for (int i = 0; i <= (int)candidate_string.length()-5; i++) {\n                string gram = candidate_string.substr(i, 5);\n                if (target_set.find(gram) != target_set.end() && covered.find(gram) == covered.end()) {\n                    new_grams.insert(gram);\n                }\n            }\n            int new_count = new_grams.size();\n            if (new_count == 0) continue;\n            double score_lb = (double)lb / new_count;\n            candidate_list.push_back({score_lb, new_count, t, (double)lb});\n        }\n\n        if (candidate_list.empty()) {\n            break;\n        }\n\n        sort(candidate_list.begin(), candidate_list.end());\n        int K_candidate = min(10, (int)candidate_list.size());\n        vector<tuple<double, int, string, double>> top_candidates(candidate_list.begin(), candidate_list.begin() + K_candidate);\n\n        string best_string = \"\";\n        vector<tuple<int, int>> best_path;\n        double best_score = 1e18;\n        int best_new_count = 0;\n\n        for (auto [score_lb, new_count, t, lb] : top_candidates) {\n            vector<vector<vector<int>>> dp(5, vector<vector<int>>(N, vector<int>(N, INF)));\n            for (int x = 0; x < N; x++) {\n                for (int y = 0; y < N; y++) {\n                    if (grid[x][y] == t[0]) {\n                        dp[0][x][y] = abs(cur_x - x) + abs(cur_y - y) + 1;\n                    }\n                }\n            }\n\n            for (int k = 1; k < 5; k++) {\n                vector<vector<int>> g = dp[k-1];\n                manhattan_transform(g);\n                for (int x = 0; x < N; x++) {\n                    for (int y = 0; y < N; y++) {\n                        if (grid[x][y] == t[k]) {\n                            dp[k][x][y] = g[x][y] + 1;\n                        }\n                    }\n                }\n            }\n\n            int min_cost = INF;\n            int best_x4 = -1, best_y4 = -1;\n            for (int x = 0; x < N; x++) {\n                for (int y = 0; y < N; y++) {\n                    if (dp[4][x][y] < min_cost) {\n                        min_cost = dp[4][x][y];\n                        best_x4 = x;\n                        best_y4 = y;\n                    }\n                }\n            }\n\n            if (min_cost == INF) continue;\n\n            double actual_score = (double)min_cost / new_count;\n            if (actual_score < best_score) {\n                best_score = actual_score;\n                best_string = t;\n                best_new_count = new_count;\n\n                int cx = best_x4, cy = best_y4;\n                vector<tuple<int, int>> path(5);\n                path[4] = {cx, cy};\n                for (int k = 3; k >= 0; k--) {\n                    int best_val = INF;\n                    int best_x = -1, best_y = -1;\n                    for (int x = 0; x < N; x++) {\n                        for (int y = 0; y < N; y++) {\n                            if (dp[k][x][y] >= INF) continue;\n                            int cost_here = dp[k][x][y] + abs(x - cx) + abs(y - cy);\n                            if (cost_here < best_val) {\n                                best_val = cost_here;\n                                best_x = x;\n                                best_y = y;\n                            }\n                        }\n                    }\n                    if (best_val == INF) {\n                        for (int x = 0; x < N; x++) {\n                            for (int y = 0; y < N; y++) {\n                                if (dp[k][x][y] < INF) {\n                                    best_x = x;\n                                    best_y = y;\n                                    best_val = dp[k][x][y] + abs(x-cx) + abs(y-cy);\n                                    break;\n                                }\n                            }\n                            if (best_x != -1) break;\n                        }\n                    }\n                    path[k] = {best_x, best_y};\n                    cx = best_x;\n                    cy = best_y;\n                }\n                best_path = path;\n            }\n        }\n\n        if (best_string.empty()) {\n            break;\n        }\n\n        for (int k = 0; k < 5; k++) {\n            int x = get<0>(best_path[k]);\n            int y = get<1>(best_path[k]);\n            operations.push_back({x, y});\n            S += grid[x][y];\n            cur_x = x;\n            cur_y = y;\n        }\n\n        int L = S.length();\n        int start_index = max(0, L - 9);\n        int end_index = L - 5;\n        for (int i = start_index; i <= end_index; i++) {\n            string gram = S.substr(i, 5);\n            if (target_set.find(gram) != target_set.end()) {\n                covered.insert(gram);\n            }\n        }\n\n        if (covered.size() >= M) break;\n        if (operations.size() >= 5000) break;\n    }\n\n    for (auto [x, y] : operations) {\n        cout << x << \" \" << y << \"\\n\";\n    }\n\n    return 0;\n}","ahc030":"#include <iostream>\n#include <vector>\n#include <cmath>\n#include <algorithm>\nusing namespace std;\n\nint main() {\n    int N, M;\n    double eps;\n    cin >> N >> M >> eps;\n\n    vector<vector<pair<int, int>>> fields;\n    for (int k = 0; k < M; k++) {\n        int d;\n        cin >> d;\n        vector<pair<int, int>> field;\n        for (int i = 0; i < d; i++) {\n            int x, y;\n            cin >> x >> y;\n            field.push_back({x, y});\n        }\n        fields.push_back(field);\n    }\n\n    vector<vector<int>> grid(N, vector<int>(N, -1));\n    vector<pair<int, int>> has_oil;\n    const int block_size = 5;\n\n    for (int i0 = 0; i0 < N; i0 += block_size) {\n        for (int j0 = 0; j0 < N; j0 += block_size) {\n            int i1 = min(i0 + block_size, N);\n            int j1 = min(j0 + block_size, N);\n            vector<pair<int, int>> block;\n            for (int i = i0; i < i1; i++) {\n                for (int j = j0; j < j1; j++) {\n                    block.push_back({i, j});\n                }\n            }\n            int k = block.size();\n            bool nonempty = false;\n            for (int trial = 0; trial < 3; trial++) {\n                cout << \"q \" << k;\n                for (auto [i, j] : block) {\n                    cout << \" \" << i << \" \" << j;\n                }\n                cout << endl;\n                int X;\n                cin >> X;\n                if (X >= 1) {\n                    nonempty = true;\n                    break;\n                }\n            }\n            if (nonempty) {\n                for (auto [i, j] : block) {\n                    cout << \"q 1 \" << i << \" \" << j << endl;\n                    int v;\n                    cin >> v;\n                    grid[i][j] = v;\n                    if (v > 0) {\n                        has_oil.push_back({i, j});\n                    }\n                }\n            } else {\n                for (auto [i, j] : block) {\n                    grid[i][j] = 0;\n                }\n            }\n        }\n    }\n\n    cout << \"a \" << has_oil.size();\n    for (auto [i, j] : has_oil) {\n        cout << \" \" << i << \" \" << j;\n    }\n    cout << endl;\n    int resp;\n    cin >> resp;\n\n    return 0;\n}","ahc031":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <cmath>\n#include <tuple>\n#include <climits>\nusing namespace std;\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int W, D, N;\n    cin >> W >> D >> N;\n    vector<vector<int>> a(D, vector<int>(N));\n    for (int d = 0; d < D; ++d) {\n        for (int k = 0; k < N; ++k) {\n            cin >> a[d][k];\n        }\n    }\n\n    for (int d = 0; d < D; ++d) {\n        vector<int> skyline(W, 0);\n        vector<tuple<int, int, int, int>> placements(N);\n\n        vector<pair<int, int>> events;\n        for (int k = 0; k < N; ++k) {\n            events.push_back({a[d][k], k});\n        }\n        sort(events.begin(), events.end(), [](const auto &x, const auto &y) {\n            return x.first > y.first;\n        });\n\n        for (const auto& event : events) {\n            int area = event.first;\n            int idx = event.second;\n            int w = ceil(sqrt(area));\n            int h = (area + w - 1) / w;\n\n            bool placed = false;\n            for (int orientation = 0; orientation < 2; ++orientation) {\n                for (int start_col = 0; start_col <= W - w; ++start_col) {\n                    int base = 0;\n                    for (int col = start_col; col < start_col + w; ++col) {\n                        if (skyline[col] > base) {\n                            base = skyline[col];\n                        }\n                    }\n                    if (base + h <= W) {\n                        for (int col = start_col; col < start_col + w; ++col) {\n                            skyline[col] = base + h;\n                        }\n                        placements[idx] = make_tuple(base, start_col, base + h, start_col + w);\n                        placed = true;\n                        break;\n                    }\n                }\n                if (placed) break;\n                swap(w, h);\n            }\n\n            if (!placed) {\n                int h_emerg = (area + W - 1) / W;\n                int w_emerg = W;\n                int base = 0;\n                for (int col = 0; col < W; ++col) {\n                    if (skyline[col] > base) {\n                        base = skyline[col];\n                    }\n                }\n                if (base + h_emerg > W) {\n                    h_emerg = W - base;\n                }\n                for (int col = 0; col < W; ++col) {\n                    skyline[col] = base + h_emerg;\n                }\n                placements[idx] = make_tuple(base, 0, base + h_emerg, w_emerg);\n            }\n        }\n\n        for (int k = 0; k < N; ++k) {\n            auto [i0, j0, i1, j1] = placements[k];\n            cout << i0 << \" \" << j0 << \" \" << i1 << \" \" << j1 << \"\\n\";\n        }\n    }\n\n    return 0;\n}","ahc032":"#include <iostream>\n#include <vector>\n#include <tuple>\n#include <algorithm>\n#include <climits>\nusing namespace std;\n\nconst long long mod = 998244353;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, K;\n    cin >> N >> M >> K;\n\n    vector<vector<long long>> grid(N, vector<long long>(N));\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cin >> grid[i][j];\n        }\n    }\n\n    vector<vector<vector<long long>>> stamps(M, vector<vector<long long>>(3, vector<long long>(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 >> stamps[m][i][j];\n            }\n        }\n    }\n\n    vector<tuple<int, int, int>> operations;\n    long long total_score = 0;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            total_score += grid[i][j];\n        }\n    }\n\n    for (int op = 0; op < K; op++) {\n        long long best_delta = LLONG_MIN;\n        int best_m = -1, best_p = -1, best_q = -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                    long long delta = 0;\n                    for (int i = 0; i < 3; i++) {\n                        for (int j = 0; j < 3; j++) {\n                            long long old_val = grid[p+i][q+j];\n                            long long add = stamps[m][i][j];\n                            long long new_val = old_val + add;\n                            if (new_val >= mod) {\n                                new_val -= mod;\n                            }\n                            delta += (new_val - old_val);\n                        }\n                    }\n                    if (delta > best_delta) {\n                        best_delta = delta;\n                        best_m = m;\n                        best_p = p;\n                        best_q = q;\n                    }\n                }\n            }\n        }\n\n        if (best_delta <= 0) {\n            break;\n        }\n\n        for (int i = 0; i < 3; i++) {\n            for (int j = 0; j < 3; j++) {\n                long long old_val = grid[best_p+i][best_q+j];\n                grid[best_p+i][best_q+j] = old_val + stamps[best_m][i][j];\n                if (grid[best_p+i][best_q+j] >= mod) {\n                    grid[best_p+i][best_q+j] -= mod;\n                }\n                total_score += (grid[best_p+i][best_q+j] - old_val);\n            }\n        }\n\n        operations.push_back(make_tuple(best_m, best_p, best_q));\n    }\n\n    bool improved = true;\n    int passes = 0;\n    while (improved && passes < 10) {\n        improved = false;\n        vector<tuple<int, int, int>> current_ops = operations;\n        for (int idx = 0; idx < current_ops.size(); idx++) {\n            vector<vector<long long>> grid_bak = grid;\n            long long total_score_bak = total_score;\n            auto [m_remove, p_remove, q_remove] = current_ops[idx];\n\n            for (int i = 0; i < 3; i++) {\n                for (int j = 0; j < 3; j++) {\n                    long long old_val = grid[p_remove+i][q_remove+j];\n                    long long new_val = (old_val - stamps[m_remove][i][j] + mod) % mod;\n                    grid[p_remove+i][q_remove+j] = new_val;\n                    total_score += (new_val - old_val);\n                }\n            }\n\n            long long best_delta1 = LLONG_MIN;\n            int best_m1 = -1, best_p1 = -1, best_q1 = -1;\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 i = 0; i < 3; i++) {\n                            for (int j = 0; j < 3; j++) {\n                                long long old_val = grid[p+i][q+j];\n                                long long add = stamps[m][i][j];\n                                long long new_val = old_val + add;\n                                if (new_val >= mod) new_val -= mod;\n                                delta += (new_val - old_val);\n                            }\n                        }\n                        if (delta > best_delta1) {\n                            best_delta1 = delta;\n                            best_m1 = m;\n                            best_p1 = p;\n                            best_q1 = q;\n                        }\n                    }\n                }\n            }\n\n            if (best_delta1 > 0 && total_score + best_delta1 > total_score_bak) {\n                for (int i = 0; i < 3; i++) {\n                    for (int j = 0; j < 3; j++) {\n                        long long old_val = grid[best_p1+i][best_q1+j];\n                        long long new_val = old_val + stamps[best_m1][i][j];\n                        if (new_val >= mod) new_val -= mod;\n                        grid[best_p1+i][best_q1+j] = new_val;\n                        total_score += (new_val - old_val);\n                    }\n                }\n                operations = current_ops;\n                operations.erase(operations.begin() + idx);\n                operations.push_back(make_tuple(best_m1, best_p1, best_q1));\n                improved = true;\n                break;\n            } else {\n                grid = grid_bak;\n                total_score = total_score_bak;\n            }\n        }\n        passes++;\n    }\n\n    cout << operations.size() << '\\n';\n    for (const auto& op : operations) {\n        cout << get<0>(op) << ' ' << get<1>(op) << ' ' << get<2>(op) << '\\n';\n    }\n\n    return 0;\n}","ahc033":"#include <iostream>\n#include <vector>\n#include <string>\n#include <algorithm>\nusing namespace std;\n\nvoid move_to(string& s, int& cr, int& cc, int tr, int tc, int& turn_count) {\n    while (cr < tr) {\n        s += 'D';\n        cr++;\n        turn_count++;\n    }\n    while (cr > tr) {\n        s += 'U';\n        cr--;\n        turn_count++;\n    }\n    while (cc < tc) {\n        s += 'R';\n        cc++;\n        turn_count++;\n    }\n    while (cc > tc) {\n        s += 'L';\n        cc--;\n        turn_count++;\n    }\n}\n\nint main() {\n    int N;\n    cin >> N;\n    vector<vector<int>> A(5, vector<int>(5));\n    for (int i = 0; i < 5; i++) {\n        for (int j = 0; j < 5; j++) {\n            cin >> A[i][j];\n        }\n    }\n\n    vector<string> crane_actions(5);\n    for (int i = 1; i < 5; i++) {\n        crane_actions[i] = \"RB\";\n    }\n\n    string large_actions = \"R\";\n    int turn_count = 1;\n    int cr = 0, cc = 1;\n\n    vector<int> next_exp = {0, 5, 10, 15, 20};\n    vector<int> count_nonnext(5, 0);\n    vector<vector<bool>> buffer_occupied(5, vector<bool>(5, false));\n\n    for (int i = 0; i < 5; i++) {\n        for (int j = 0; j < 5; j++) {\n            move_to(large_actions, cr, cc, i, 1, turn_count);\n            large_actions += 'L';\n            turn_count++;\n            cr = i;\n            cc = 0;\n\n            large_actions += 'P';\n            turn_count++;\n\n            int x = A[i][j];\n            int r = x / 5;\n            int target_col;\n            if (x == next_exp[r]) {\n                target_col = 4;\n                next_exp[r]++;\n            } else {\n                if (count_nonnext[r] < 2) {\n                    target_col = (count_nonnext[r] % 2 == 0) ? 2 : 3;\n                    buffer_occupied[r][target_col] = true;\n                    count_nonnext[r]++;\n                } else {\n                    target_col = 4;\n                }\n            }\n\n            move_to(large_actions, cr, cc, r, target_col, turn_count);\n            cr = r;\n            cc = target_col;\n\n            large_actions += 'Q';\n            turn_count++;\n\n            move_to(large_actions, cr, cc, i, 1, turn_count);\n            cr = i;\n            cc = 1;\n        }\n    }\n\n    for (int r = 0; r < 5; r++) {\n        if (buffer_occupied[r][2]) {\n            move_to(large_actions, cr, cc, r, 2, turn_count);\n            large_actions += 'P';\n            turn_count++;\n            move_to(large_actions, cr, cc, r, 4, turn_count);\n            large_actions += 'Q';\n            turn_count++;\n        }\n        if (buffer_occupied[r][3]) {\n            move_to(large_actions, cr, cc, r, 3, turn_count);\n            large_actions += 'P';\n            turn_count++;\n            move_to(large_actions, cr, cc, r, 4, turn_count);\n            large_actions += 'Q';\n            turn_count++;\n        }\n    }\n\n    crane_actions[0] = large_actions;\n    size_t max_len = crane_actions[0].size();\n    for (int i = 1; i < 5; i++) {\n        if (crane_actions[i].size() < max_len) {\n            crane_actions[i] += string(max_len - crane_actions[i].size(), '.');\n        }\n    }\n\n    for (int i = 0; i < 5; i++) {\n        cout << crane_actions[i] << endl;\n    }\n\n    return 0;\n}","ahc034":"#include <iostream>\n#include <vector>\n#include <string>\nusing namespace std;\n\nint main() {\n    int N;\n    cin >> N;\n    vector<vector<int>> grid(N, vector<int>(N));\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cin >> grid[i][j];\n        }\n    }\n\n    vector<string> operations;\n    int x = 0, y = 0;\n\n    if (grid[0][0] > 0) {\n        operations.push_back(\"+\" + to_string(grid[0][0]));\n    }\n\n    for (int i = 0; i < N; i++) {\n        if (i % 2 == 0) {\n            for (int j = 0; j < N-1; j++) {\n                operations.push_back(\"R\");\n                y++;\n                if (grid[x][y] > 0) {\n                    operations.push_back(\"+\" + to_string(grid[x][y]));\n                }\n            }\n        } else {\n            for (int j = 0; j < N-1; j++) {\n                operations.push_back(\"L\");\n                y--;\n                if (grid[x][y] > 0) {\n                    operations.push_back(\"+\" + to_string(grid[x][y]));\n                }\n            }\n        }\n        if (i < N-1) {\n            operations.push_back(\"D\");\n            x++;\n            if (grid[x][y] > 0) {\n                operations.push_back(\"+\" + to_string(grid[x][y]));\n            }\n        }\n    }\n\n    for (int i = 0; i < x; i++) {\n        operations.push_back(\"U\");\n    }\n    for (int j = 0; j < y; j++) {\n        operations.push_back(\"L\");\n    }\n\n    x = 0;\n    y = 0;\n    if (grid[0][0] < 0) {\n        operations.push_back(\"-\" + to_string(-grid[0][0]));\n    }\n\n    for (int i = 0; i < N; i++) {\n        if (i % 2 == 0) {\n            for (int j = 0; j < N-1; j++) {\n                operations.push_back(\"R\");\n                y++;\n                if (grid[x][y] < 0) {\n                    operations.push_back(\"-\" + to_string(-grid[x][y]));\n                }\n            }\n        } else {\n            for (int j = 0; j < N-1; j++) {\n                operations.push_back(\"L\");\n                y--;\n                if (grid[x][y] < 0) {\n                    operations.push_back(\"-\" + to_string(-grid[x][y]));\n                }\n            }\n        }\n        if (i < N-1) {\n            operations.push_back(\"D\");\n            x++;\n            if (grid[x][y] < 0) {\n                operations.push_back(\"-\" + to_string(-grid[x][y]));\n            }\n        }\n    }\n\n    for (const string& op : operations) {\n        cout << op << endl;\n    }\n\n    return 0;\n}","ahc035":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <set>\n#include <utility>\n\nusing namespace std;\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, T;\n    cin >> N >> M >> T;\n\n    const int seed_count = 2 * N * (N - 1);\n    vector<vector<int>> seeds(seed_count, vector<int>(M, 0));\n\n    for (int i = 0; i < seed_count; i++) {\n        for (int j = 0; j < M; j++) {\n            cin >> seeds[i][j];\n        }\n    }\n\n    for (int t = 0; t < T; t++) {\n        vector<int> current_max(M, -1);\n        for (int l = 0; l < M; l++) {\n            for (int k = 0; k < seed_count; k++) {\n                if (seeds[k][l] > current_max[l]) {\n                    current_max[l] = seeds[k][l];\n                }\n            }\n        }\n\n        vector<int> V(seed_count, 0);\n        for (int k = 0; k < seed_count; k++) {\n            for (int l = 0; l < M; l++) {\n                V[k] += seeds[k][l];\n            }\n        }\n\n        vector<vector<bool>> trait(seed_count, vector<bool>(M, false));\n        for (int k = 0; k < seed_count; k++) {\n            for (int l = 0; l < M; l++) {\n                if (seeds[k][l] == current_max[l]) {\n                    trait[k][l] = true;\n                }\n            }\n        }\n\n        vector<int> coverage_count(M, 0);\n        set<int> chosen_seeds;\n\n        while (true) {\n            bool all_covered = true;\n            for (int l = 0; l < M; l++) {\n                if (coverage_count[l] < 2) {\n                    all_covered = false;\n                    break;\n                }\n            }\n            if (all_covered) break;\n\n            int best_seed = -1;\n            int best_cover = -1;\n            int best_V = -1;\n\n            for (int k = 0; k < seed_count; k++) {\n                if (chosen_seeds.find(k) != chosen_seeds.end()) continue;\n\n                int count = 0;\n                for (int l = 0; l < M; l++) {\n                    if (trait[k][l] && coverage_count[l] < 2) {\n                        count++;\n                    }\n                }\n\n                if (count == 0) continue;\n\n                if (count > best_cover || (count == best_cover && V[k] > best_V)) {\n                    best_cover = count;\n                    best_V = V[k];\n                    best_seed = k;\n                }\n            }\n\n            if (best_seed == -1) break;\n\n            chosen_seeds.insert(best_seed);\n            for (int l = 0; l < M; l++) {\n                if (trait[best_seed][l]) {\n                    coverage_count[l]++;\n                }\n            }\n        }\n\n        if (chosen_seeds.size() < N * N) {\n            vector<pair<int, int>> candidates;\n            for (int k = 0; k < seed_count; k++) {\n                if (chosen_seeds.find(k) == chosen_seeds.end()) {\n                    candidates.push_back({V[k], k});\n                }\n            }\n            sort(candidates.begin(), candidates.end(), [](const pair<int, int>& a, const pair<int, int>& b) {\n                return a.first > b.first;\n            });\n            int rem = N * N - chosen_seeds.size();\n            for (int i = 0; i < rem; i++) {\n                chosen_seeds.insert(candidates[i].second);\n            }\n        }\n\n        int half1 = 8;\n        vector<pair<int, int>> seeds_with_signature;\n        for (int k : chosen_seeds) {\n            int score1 = 0, score2 = 0;\n            for (int l = 0; l < half1; l++) {\n                if (trait[k][l]) score1++;\n            }\n            for (int l = half1; l < M; l++) {\n                if (trait[k][l]) score2++;\n            }\n            seeds_with_signature.push_back({score1 - score2, k});\n        }\n\n        sort(seeds_with_signature.begin(), seeds_with_signature.end(), [](const pair<int, int>& a, const pair<int, int>& b) {\n            return a.first > b.first;\n        });\n\n        vector<int> groupA, groupB;\n        for (int i = 0; i < seeds_with_signature.size(); i++) {\n            if (i < 18) {\n                groupA.push_back(seeds_with_signature[i].second);\n            } else {\n                groupB.push_back(seeds_with_signature[i].second);\n            }\n        }\n\n        sort(groupA.begin(), groupA.end(), [&](int a, int b) { return V[a] > V[b]; });\n        sort(groupB.begin(), groupB.end(), [&](int a, int b) { return V[a] > V[b]; });\n\n        vector<vector<int>> grid(N, vector<int>(N, -1));\n        int idxA = 0, idxB = 0;\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                if ((i + j) % 2 == 0) {\n                    grid[i][j] = groupA[idxA++];\n                } else {\n                    grid[i][j] = groupB[idxB++];\n                }\n            }\n        }\n\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                cout << grid[i][j];\n                if (j < N - 1) {\n                    cout << \" \";\n                } else {\n                    cout << endl;\n                }\n            }\n        }\n        cout.flush();\n\n        for (int i = 0; i < seed_count; i++) {\n            for (int j = 0; j < M; j++) {\n                cin >> seeds[i][j];\n            }\n        }\n    }\n\n    return 0;\n}","ahc038":"#include <iostream>\n#include <vector>\n#include <cstdlib>\n#include <algorithm>\n#include <climits>\n#include <tuple>\nusing namespace std;\n\nint main() {\n    int N, M, V;\n    cin >> N >> M >> V;\n\n    vector<string> s_grid(N), t_grid(N);\n    for (int i = 0; i < N; i++) {\n        cin >> s_grid[i];\n    }\n    for (int i = 0; i < N; i++) {\n        cin >> t_grid[i];\n    }\n\n    int L0 = (N - 1) / 2;\n\n    cout << V << endl;\n    for (int i = 1; i < V; i++) {\n        cout << \"0 \" << L0 << endl;\n    }\n\n    int rx0 = N / 2;\n    int ry0 = N / 2;\n    cout << rx0 << \" \" << ry0 << endl;\n\n    int cur_x = rx0, cur_y = ry0;\n    vector<int> dirs(V, 0);\n    vector<bool> holding(V, false);\n\n    vector<pair<int, int>> initial_list;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (s_grid[i][j] == '1') {\n                initial_list.push_back({i, j});\n            }\n        }\n    }\n\n    vector<pair<int, int>> target_list;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (t_grid[i][j] == '1') {\n                target_list.push_back({i, j});\n            }\n        }\n    }\n\n    vector<string> outputs;\n\n    while (!initial_list.empty()) {\n        int F_available = V - 1;\n        int batch_size = min(F_available, (int)initial_list.size());\n        vector<tuple<int, int, int, int, int, int>> pickup_batch;\n        vector<int> available_fingertips;\n        for (int i = 1; i < V; i++) {\n            available_fingertips.push_back(i);\n        }\n\n        int current_start_x = cur_x;\n        int current_start_y = cur_y;\n\n        for (int idx = 0; idx < batch_size; idx++) {\n            int best_cost = INT_MAX;\n            int best_i = -1, best_j = -1, best_cx = -1, best_cy = -1, best_d = -1, best_f = -1;\n            for (int k = 0; k < initial_list.size(); k++) {\n                int i = initial_list[k].first;\n                int j = initial_list[k].second;\n                vector<tuple<int, int, int>> candidates;\n                if (j - L0 >= 0) {\n                    candidates.push_back(make_tuple(i, j - L0, 0));\n                }\n                if (i - L0 >= 0) {\n                    candidates.push_back(make_tuple(i - L0, j, 1));\n                }\n                if (j + L0 < N) {\n                    candidates.push_back(make_tuple(i, j + L0, 2));\n                }\n                if (i + L0 < N) {\n                    candidates.push_back(make_tuple(i + L0, j, 3));\n                }\n                for (auto &cand : candidates) {\n                    int cx = get<0>(cand);\n                    int cy = get<1>(cand);\n                    int d = get<2>(cand);\n                    for (int f_index = 0; f_index < available_fingertips.size(); f_index++) {\n                        int f = available_fingertips[f_index];\n                        int dist = abs(cx - current_start_x) + abs(cy - current_start_y);\n                        int cur_dir = dirs[f];\n                        int diff = (d - cur_dir + 4) % 4;\n                        int rot_cost = min(diff, 4 - diff);\n                        int cost = dist + rot_cost;\n                        if (cost < best_cost) {\n                            best_cost = cost;\n                            best_i = i;\n                            best_j = j;\n                            best_cx = cx;\n                            best_cy = cy;\n                            best_d = d;\n                            best_f = f;\n                        }\n                    }\n                }\n            }\n            if (best_cost == INT_MAX) {\n                batch_size = idx;\n                break;\n            }\n\n            for (auto it = initial_list.begin(); it != initial_list.end(); it++) {\n                if (it->first == best_i && it->second == best_j) {\n                    initial_list.erase(it);\n                    break;\n                }\n            }\n\n            for (auto it = available_fingertips.begin(); it != available_fingertips.end(); it++) {\n                if (*it == best_f) {\n                    available_fingertips.erase(it);\n                    break;\n                }\n            }\n\n            pickup_batch.push_back(make_tuple(best_i, best_j, best_cx, best_cy, best_d, best_f));\n            current_start_x = best_cx;\n            current_start_y = best_cy;\n        }\n\n        for (auto &item : pickup_batch) {\n            int i = get<0>(item);\n            int j = get<1>(item);\n            int cx = get<2>(item);\n            int cy = get<3>(item);\n            int d = get<4>(item);\n            int f = get<5>(item);\n\n            int dx = cx - cur_x;\n            int dy = cy - cur_y;\n\n            vector<char> moves;\n            if (dx > 0) {\n                for (int k = 0; k < dx; k++) moves.push_back('R');\n            } else if (dx < 0) {\n                for (int k = 0; k < -dx; k++) moves.push_back('L');\n            }\n            if (dy > 0) {\n                for (int k = 0; k < dy; k++) moves.push_back('D');\n            } else if (dy < 0) {\n                for (int k = 0; k < -dy; k++) moves.push_back('U');\n            }\n\n            int cur_dir = dirs[f];\n            int diff = (d - cur_dir + 4) % 4;\n            vector<char> rots;\n            if (diff == 1) {\n                rots.push_back('R');\n            } else if (diff == 2) {\n                rots.push_back('R');\n                rots.push_back('R');\n            } else if (diff == 3) {\n                rots.push_back('L');\n            }\n\n            int T = max(moves.size(), rots.size());\n            for (int t = 0; t < T; t++) {\n                string op = \"\";\n                if (t < moves.size()) {\n                    op += moves[t];\n                } else {\n                    op += '.';\n                }\n\n                for (int v = 1; v < V; v++) {\n                    if (v == f && t < rots.size()) {\n                        op += rots[t];\n                    } else {\n                        op += '.';\n                    }\n                }\n\n                for (int v = 0; v < V; v++) {\n                    if (v == f && t == T - 1) {\n                        op += 'P';\n                        holding[f] = true;\n                    } else {\n                        op += '.';\n                    }\n                }\n                outputs.push_back(op);\n            }\n\n            cur_x = cx;\n            cur_y = cy;\n            dirs[f] = d;\n        }\n\n        vector<pair<int, int>> available_targets = target_list;\n        vector<tuple<int, int, int, int, int, int>> delivery_batch;\n        current_start_x = cur_x;\n        current_start_y = cur_y;\n\n        for (int idx = 0; idx < batch_size; idx++) {\n            int best_cost = INT_MAX;\n            int best_i = -1, best_j = -1, best_cx = -1, best_cy = -1, best_d = -1, best_f = -1;\n            for (int k = 0; k < available_targets.size(); k++) {\n                int i = available_targets[k].first;\n                int j = available_targets[k].second;\n                vector<tuple<int, int, int>> candidates;\n                if (j - L0 >= 0) {\n                    candidates.push_back(make_tuple(i, j - L0, 0));\n                }\n                if (i - L0 >= 0) {\n                    candidates.push_back(make_tuple(i - L0, j, 1));\n                }\n                if (j + L0 < N) {\n                    candidates.push_back(make_tuple(i, j + L0, 2));\n                }\n                if (i + L0 < N) {\n                    candidates.push_back(make_tuple(i + L0, j, 3));\n                }\n                for (auto &cand : candidates) {\n                    int cx = get<0>(cand);\n                    int cy = get<1>(cand);\n                    int d = get<2>(cand);\n                    for (auto &p_item : pickup_batch) {\n                        int f = get<5>(p_item);\n                        if (holding[f]) {\n                            int dist = abs(cx - current_start_x) + abs(cy - current_start_y);\n                            int cur_dir = dirs[f];\n                            int diff = (d - cur_dir + 4) % 4;\n                            int rot_cost = min(diff, 4 - diff);\n                            int cost = dist + rot_cost;\n                            if (cost < best_cost) {\n                                best_cost = cost;\n                                best_i = i;\n                                best_j = j;\n                                best_cx = cx;\n                                best_cy = cy;\n                                best_d = d;\n                                best_f = f;\n                            }\n                        }\n                    }\n                }\n            }\n            if (best_cost == INT_MAX) {\n                batch_size = idx;\n                break;\n            }\n\n            for (auto it = available_targets.begin(); it != available_targets.end(); it++) {\n                if (it->first == best_i && it->second == best_j) {\n                    available_targets.erase(it);\n                    break;\n                }\n            }\n\n            delivery_batch.push_back(make_tuple(best_i, best_j, best_cx, best_cy, best_d, best_f));\n            current_start_x = best_cx;\n            current_start_y = best_cy;\n        }\n\n        for (auto &item : delivery_batch) {\n            int i = get<0>(item);\n            int j = get<1>(item);\n            int cx = get<2>(item);\n            int cy = get<3>(item);\n            int d = get<4>(item);\n            int f = get<5>(item);\n\n            int dx = cx - cur_x;\n            int dy = cy - cur_y;\n\n            vector<char> moves;\n            if (dx > 0) {\n                for (int k = 0; k < dx; k++) moves.push_back('R');\n            } else if (dx < 0) {\n                for (int k = 0; k < -dx; k++) moves.push_back('L');\n            }\n            if (dy > 0) {\n                for (int k = 0; k < dy; k++) moves.push_back('D');\n            } else if (dy < 0) {\n                for (int k = 0; k < -dy; k++) moves.push_back('U');\n            }\n\n            int cur_dir = dirs[f];\n            int diff = (d - cur_dir + 4) % 4;\n            vector<char> rots;\n            if (diff == 1) {\n                rots.push_back('R');\n            } else if (diff == 2) {\n                rots.push_back('R');\n                rots.push_back('R');\n            } else if (diff == 3) {\n                rots.push_back('L');\n            }\n\n            int T = max(moves.size(), rots.size());\n            for (int t = 0; t < T; t++) {\n                string op = \"\";\n                if (t < moves.size()) {\n                    op += moves[t];\n                } else {\n                    op += '.';\n                }\n\n                for (int v = 1; v < V; v++) {\n                    if (v == f && t < rots.size()) {\n                        op += rots[t];\n                    } else {\n                        op += '.';\n                    }\n                }\n\n                for (int v = 0; v < V; v++) {\n                    if (v == f && t == T - 1) {\n                        op += 'P';\n                        holding[f] = false;\n                    } else {\n                        op += '.';\n                    }\n                }\n                outputs.push_back(op);\n            }\n\n            cur_x = cx;\n            cur_y = cy;\n            dirs[f] = d;\n        }\n\n        for (auto &item : delivery_batch) {\n            int i = get<0>(item);\n            int j = get<1>(item);\n            for (auto it = target_list.begin(); it != target_list.end(); it++) {\n                if (it->first == i && it->second == j) {\n                    target_list.erase(it);\n                    break;\n                }\n            }\n        }\n    }\n\n    for (string &s : outputs) {\n        cout << s << endl;\n    }\n\n    return 0;\n}","ahc039":"#include <iostream>\n#include <vector>\n#include <set>\n#include <algorithm>\n#include <cctype>\n#include <cmath>\n#include <queue>\n#include <map>\n#include <unordered_set>\nusing namespace std;\n\nstruct Point {\n    int x, y;\n    Point(int x=0, int y=0) : x(x), y(y) {}\n};\n\nconst int GRID = 15;\n\nint main() {\n    int N;\n    cin >> N;\n    vector<Point> mackerels;\n    vector<Point> sardines;\n    for (int i = 0; i < 2*N; i++) {\n        int x, y;\n        cin >> x >> y;\n        if (i < N) {\n            mackerels.push_back(Point(x, y));\n        } else {\n            sardines.push_back(Point(x, y));\n        }\n    }\n\n    set<pair<int, int>> sardine_set;\n    for (auto& p : sardines) {\n        sardine_set.insert({p.x, p.y});\n    }\n\n    vector<int> x_grid(GRID+1);\n    vector<int> y_grid(GRID+1);\n    for (int i = 0; i <= GRID; i++) {\n        x_grid[i] = (long long)i * 100000 / GRID;\n        y_grid[i] = (long long)i * 100000 / GRID;\n    }\n    x_grid[GRID] = 100000;\n    y_grid[GRID] = 100000;\n\n    vector<vector<int>> grid_m(GRID, vector<int>(GRID, 0));\n    vector<vector<int>> grid_s(GRID, vector<int>(GRID, 0));\n    vector<vector<int>> grid_count(GRID, vector<int>(GRID, 0));\n\n    for (auto& p : mackerels) {\n        int i = lower_bound(x_grid.begin(), x_grid.end(), p.x + 1) - x_grid.begin() - 1;\n        int j = lower_bound(y_grid.begin(), y_grid.end(), p.y + 1) - y_grid.begin() - 1;\n        if (i < 0) i = 0;\n        if (i >= GRID) i = GRID-1;\n        if (j < 0) j = 0;\n        if (j >= GRID) j = GRID-1;\n        grid_m[i][j]++;\n    }\n\n    for (auto& p : sardines) {\n        int i = lower_bound(x_grid.begin(), x_grid.end(), p.x + 1) - x_grid.begin() - 1;\n        int j = lower_bound(y_grid.begin(), y_grid.end(), p.y + 1) - y_grid.begin() - 1;\n        if (i < 0) i = 0;\n        if (i >= GRID) i = GRID-1;\n        if (j < 0) j = 0;\n        if (j >= GRID) j = GRID-1;\n        grid_s[i][j]++;\n    }\n\n    for (int i = 0; i < GRID; i++) {\n        for (int j = 0; j < GRID; j++) {\n            grid_count[i][j] = grid_m[i][j] - grid_s[i][j];\n        }\n    }\n\n    int best_score = -1000000;\n    int best_i = -1, best_j = -1;\n    for (int i = 0; i < GRID; i++) {\n        for (int j = 0; j < GRID; j++) {\n            if (grid_count[i][j] > best_score) {\n                best_score = grid_count[i][j];\n                best_i = i;\n                best_j = j;\n            }\n        }\n    }\n\n    if (best_score <= 0) {\n        int best_cnt = 10;\n        Point best_point;\n        bool found = false;\n        for (auto& p : mackerels) {\n            int cnt = 0;\n            for (int dx = -1; dx <= 1; dx++) {\n                for (int dy = -1; dy <= 1; dy++) {\n                    int x1 = p.x + dx;\n                    int y1 = p.y + dy;\n                    if (x1 < 0 || x1 > 100000 || y1 < 0 || y1 > 100000) continue;\n                    if (sardine_set.find({x1, y1}) != sardine_set.end()) {\n                        cnt++;\n                    }\n                }\n            }\n            if (cnt < best_cnt) {\n                best_cnt = cnt;\n                best_point = p;\n                found = true;\n            }\n        }\n        if (!found) {\n            best_point = mackerels[0];\n        }\n        int x0 = best_point.x;\n        int y0 = best_point.y;\n        vector<Point> poly;\n        poly.push_back(Point(x0-1, y0-1));\n        poly.push_back(Point(x0-1, y0+1));\n        poly.push_back(Point(x0+1, y0+1));\n        poly.push_back(Point(x0+1, y0-1));\n        cout << 4 << \"\\n\";\n        for (auto& p : poly) {\n            cout << p.x << \" \" << p.y << \"\\n\";\n        }\n        return 0;\n    }\n\n    set<pair<int, int>> R;\n    set<pair<int, int>> frontier;\n    R.insert({best_i, best_j});\n    int total_perimeter = 0;\n    const int dx4[4] = {0, 1, 0, -1};\n    const int dy4[4] = {1, 0, -1, 0};\n\n    int dx_cell = x_grid[best_i+1] - x_grid[best_i];\n    int dy_cell = y_grid[best_j+1] - y_grid[best_j];\n    total_perimeter = 2 * (dx_cell + dy_cell);\n\n    for (int d = 0; d < 4; d++) {\n        int ni = best_i + dx4[d];\n        int nj = best_j + dy4[d];\n        if (ni < 0 || ni >= GRID || nj < 0 || nj >= GRID) continue;\n        if (R.find({ni, nj}) == R.end()) {\n            frontier.insert({ni, nj});\n        }\n    }\n\n    while (!frontier.empty()) {\n        int best_cell_i = -1, best_cell_j = -1;\n        int best_cell_score = -1000000;\n        int best_change_perimeter = 0;\n        vector<pair<int, int>> to_remove;\n        for (auto cell : frontier) {\n            int i = cell.first;\n            int j = cell.second;\n            if (R.find(cell) != R.end()) {\n                to_remove.push_back(cell);\n                continue;\n            }\n            int dx_local = x_grid[i+1] - x_grid[i];\n            int dy_local = y_grid[j+1] - y_grid[j];\n            int change = 0;\n            for (int d = 0; d < 4; d++) {\n                int ni = i + dx4[d];\n                int nj = j + dy4[d];\n                if (ni < 0 || ni >= GRID || nj < 0 || nj >= GRID) {\n                    if (d == 0) change += dy_local;\n                    else if (d == 1) change += dx_local;\n                    else if (d == 2) change += dy_local;\n                    else if (d == 3) change += dx_local;\n                    continue;\n                }\n                if (R.find({ni, nj}) != R.end()) {\n                    if (d == 0) change -= dy_local;\n                    else if (d == 1) change -= dx_local;\n                    else if (d == 2) change -= dy_local;\n                    else if (d == 3) change -= dx_local;\n                } else {\n                    if (d == 0) change += dy_local;\n                    else if (d == 1) change += dx_local;\n                    else if (d == 2) change += dy_local;\n                    else if (d == 3) change += dx_local;\n                }\n            }\n            int new_perimeter = total_perimeter + change;\n            if (new_perimeter > 400000) continue;\n            if (grid_count[i][j] > best_cell_score) {\n                best_cell_score = grid_count[i][j];\n                best_cell_i = i;\n                best_cell_j = j;\n                best_change_perimeter = change;\n            }\n        }\n        for (auto& cell : to_remove) {\n            frontier.erase(cell);\n        }\n        if (best_cell_score <= 0) {\n            break;\n        }\n        if (best_cell_i == -1) {\n            break;\n        }\n        R.insert({best_cell_i, best_cell_j});\n        total_perimeter += best_change_perimeter;\n        frontier.erase({best_cell_i, best_cell_j});\n        for (int d = 0; d < 4; d++) {\n            int ni = best_cell_i + dx4[d];\n            int nj = best_cell_j + dy4[d];\n            if (ni < 0 || ni >= GRID || nj < 0 || nj >= GRID) continue;\n            if (R.find({ni, nj}) == R.end()) {\n                frontier.insert({ni, nj});\n            }\n        }\n    }\n\n    int min_x = 100000, max_x = 0, min_y = 100000, max_y = 0;\n    for (auto cell : R) {\n        int i = cell.first;\n        int j = cell.second;\n        min_x = min(min_x, x_grid[i]);\n        max_x = max(max_x, x_grid[i+1]);\n        min_y = min(min_y, y_grid[j]);\n        max_y = max(max_y, y_grid[j+1]);\n    }\n    vector<Point> poly;\n    poly.push_back(Point(min_x, max_y));\n    poly.push_back(Point(max_x, max_y));\n    poly.push_back(Point(max_x, min_y));\n    poly.push_back(Point(min_x, min_y));\n    cout << 4 << \"\\n\";\n    for (auto& p : poly) {\n        cout << p.x << \" \" << p.y << \"\\n\";\n    }\n\n    return 0;\n}","ahc040":"#include <iostream>\n#include <vector>\n#include <set>\n#include <map>\n#include <algorithm>\n#include <cmath>\n#include <cctype>\n#include <tuple>\n#include <iomanip>\n\nusing namespace std;\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, T;\n    double sigma;\n    cin >> N >> T >> sigma;\n\n    vector<double> w_est(N);\n    vector<double> h_est(N);\n    vector<int> w_count(N, 0);\n    vector<int> h_count(N, 0);\n\n    for (int i = 0; i < N; i++) {\n        cin >> w_est[i] >> h_est[i];\n    }\n\n    vector<int> exploration_order(N);\n    for (int i = 0; i < N; i++) {\n        exploration_order[i] = i;\n    }\n    sort(exploration_order.begin(), exploration_order.end(), [&](int i, int j) {\n        return w_est[i] * h_est[i] > w_est[j] * h_est[j];\n    });\n\n    for (int turn = 0; turn < T; turn++) {\n        if (turn < N) {\n            int i = exploration_order[turn];\n            cout << 1 << '\\n';\n            cout << i << \" \" << 0 << \" \" << \"U\" << \" \" << -1 << '\\n';\n            cout.flush();\n\n            long long Wp, Hp;\n            cin >> Wp >> Hp;\n\n            if (w_count[i] == 0) {\n                w_est[i] = Wp;\n                h_est[i] = Hp;\n            } else {\n                w_est[i] = (w_est[i] * w_count[i] + Wp) / (w_count[i] + 1);\n                h_est[i] = (h_est[i] * h_count[i] + Hp) / (h_count[i] + 1);\n            }\n            w_count[i]++;\n            h_count[i]++;\n        } else {\n            vector<tuple<int, int, char, int>> sequence;\n            set<int> placed_set;\n            map<int, tuple<long long, long long, long long, long long>> rect_by_index;\n            long long current_max_x = 0;\n            long long current_max_y = 0;\n\n            vector<long long> w_sim(N), h_sim(N);\n            for (int i = 0; i < N; i++) {\n                w_sim[i] = static_cast<long long>(round(w_est[i]));\n                h_sim[i] = static_cast<long long>(round(h_est[i]));\n                if (w_sim[i] < 1) w_sim[i] = 1;\n                if (w_sim[i] > 1000000000) w_sim[i] = 1000000000;\n                if (h_sim[i] < 1) h_sim[i] = 1;\n                if (h_sim[i] > 1000000000) h_sim[i] = 1000000000;\n            }\n\n            int last_placed_index = -1;\n\n            for (int i = 0; i < N; i++) {\n                vector<tuple<long long, long long, int>> rotations;\n                rotations.emplace_back(w_sim[i], h_sim[i], 0);\n                rotations.emplace_back(h_sim[i], w_sim[i], 1);\n\n                long long best_bbox_sum = 1LL << 60;\n                tuple<int, char, int, long long, long long, long long, long long> best_choice;\n                bool found = false;\n\n                for (const auto& rot_data : rotations) {\n                    long long w = get<0>(rot_data);\n                    long long h = get<1>(rot_data);\n                    int rot = get<2>(rot_data);\n                    for (char d : {'U', 'L'}) {\n                        set<int> ref_candidates;\n                        ref_candidates.insert(-1);\n                        if (!placed_set.empty()) {\n                            if (last_placed_index != -1) {\n                                ref_candidates.insert(last_placed_index);\n                            }\n                            long long max_area = -1;\n                            int best_j = -1;\n                            for (int j : placed_set) {\n                                auto [x_pl, y_pl, w_pl, h_pl] = rect_by_index[j];\n                                long long area = w_pl * h_pl;\n                                if (area > max_area) {\n                                    max_area = area;\n                                    best_j = j;\n                                }\n                            }\n                            if (best_j != -1) {\n                                ref_candidates.insert(best_j);\n                            }\n                        }\n\n                        for (int b_ref : ref_candidates) {\n                            long long x0 = 0, y0 = 0;\n                            if (d == 'U') {\n                                if (b_ref == -1) {\n                                    x0 = 0;\n                                } else {\n                                    if (rect_by_index.find(b_ref) != rect_by_index.end()) {\n                                        auto [x_pl, y_pl, w_pl, h_pl] = rect_by_index[b_ref];\n                                        x0 = x_pl + w_pl;\n                                    } else {\n                                        continue;\n                                    }\n                                }\n                                y0 = 0;\n                                for (int j : placed_set) {\n                                    auto [x_pl, y_pl, w_pl, h_pl] = rect_by_index[j];\n                                    if (x0 < x_pl + w_pl && x0 + w > x_pl) {\n                                        if (y_pl + h_pl > y0) {\n                                            y0 = y_pl + h_pl;\n                                        }\n                                    }\n                                }\n                            } else {\n                                if (b_ref == -1) {\n                                    y0 = 0;\n                                } else {\n                                    if (rect_by_index.find(b_ref) != rect_by_index.end()) {\n                                        auto [x_pl, y_pl, w_pl, h_pl] = rect_by_index[b_ref];\n                                        y0 = y_pl + h_pl;\n                                    } else {\n                                        continue;\n                                    }\n                                }\n                                x0 = 0;\n                                for (int j : placed_set) {\n                                    auto [x_pl, y_pl, w_pl, h_pl] = rect_by_index[j];\n                                    if (y0 < y_pl + h_pl && y0 + h > y_pl) {\n                                        if (x_pl + w_pl > x0) {\n                                            x0 = x_pl + w_pl;\n                                        }\n                                    }\n                                }\n                            }\n\n                            long long new_max_x = max(current_max_x, x0 + w);\n                            long long new_max_y = max(current_max_y, y0 + h);\n                            long long bbox_sum = new_max_x + new_max_y;\n\n                            if (bbox_sum < best_bbox_sum) {\n                                best_bbox_sum = bbox_sum;\n                                best_choice = make_tuple(rot, d, b_ref, x0, y0, new_max_x, new_max_y);\n                                found = true;\n                            }\n                        }\n                    }\n                }\n\n                long long skip_penalty = w_sim[i] + h_sim[i];\n                if (found && (best_bbox_sum - (current_max_x + current_max_y) <= skip_penalty)) {\n                    auto [rot, d, b_ref, x0, y0, new_max_x, new_max_y] = best_choice;\n                    long long w_used, h_used;\n                    if (rot == 0) {\n                        w_used = w_sim[i];\n                        h_used = h_sim[i];\n                    } else {\n                        w_used = h_sim[i];\n                        h_used = w_sim[i];\n                    }\n                    placed_set.insert(i);\n                    rect_by_index[i] = make_tuple(x0, y0, w_used, h_used);\n                    current_max_x = new_max_x;\n                    current_max_y = new_max_y;\n                    sequence.emplace_back(i, rot, d, b_ref);\n                    last_placed_index = i;\n                }\n            }\n\n            cout << sequence.size() << '\\n';\n            for (auto [i, rot, d, b_ref] : sequence) {\n                cout << i << \" \" << rot << \" \" << d << \" \" << b_ref << '\\n';\n            }\n            cout.flush();\n\n            long long Wp, Hp;\n            cin >> Wp >> Hp;\n        }\n    }\n\n    return 0;\n}","ahc041":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\n#include <utility>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, H;\n    cin >> N >> M >> H;\n\n    vector<int> A(N);\n    for (int i = 0; i < N; i++) {\n        cin >> A[i];\n    }\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    vector<int> x(N), y(N);\n    for (int i = 0; i < N; i++) {\n        cin >> x[i] >> y[i];\n    }\n\n    vector<int> depth(N, -1);\n    vector<int> parent(N, -1);\n    vector<bool> finalized(N, false);\n\n    auto cmp = greater<pair<int, int>>();\n    priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(cmp)> unassigned_pq(cmp);\n    priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(cmp)> bfs_pq(cmp);\n\n    for (int i = 0; i < N; i++) {\n        unassigned_pq.push({A[i], i});\n    }\n\n    int finalized_count = 0;\n\n    while (finalized_count < N) {\n        int v = -1;\n        if (!bfs_pq.empty()) {\n            while (!bfs_pq.empty() && v == -1) {\n                auto [a_val, node] = bfs_pq.top();\n                bfs_pq.pop();\n                if (!finalized[node]) {\n                    v = node;\n                }\n            }\n        }\n        if (v == -1) {\n            while (!unassigned_pq.empty() && v == -1) {\n                auto [a_val, node] = unassigned_pq.top();\n                unassigned_pq.pop();\n                if (!finalized[node]) {\n                    v = node;\n                }\n            }\n        }\n        if (v == -1) break;\n\n        finalized[v] = true;\n        finalized_count++;\n\n        if (depth[v] == -1) {\n            depth[v] = 0;\n            parent[v] = -1;\n        }\n\n        if (depth[v] < H) {\n            for (int w : adj[v]) {\n                if (finalized[w]) continue;\n                int new_depth = depth[v] + 1;\n                if (depth[w] == -1 || new_depth > depth[w]) {\n                    depth[w] = new_depth;\n                    parent[w] = v;\n                    bfs_pq.push({A[w], w});\n                }\n            }\n        }\n    }\n\n    for (int i = 0; i < N; i++) {\n        if (i > 0) cout << \" \";\n        cout << parent[i];\n    }\n    cout << endl;\n\n    return 0;\n}","ahc042":"#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\nint main() {\n    const int N = 20;\n    vector<string> board(N);\n    for (int i = 0; i < N; i++) {\n        cin >> board[i];\n    }\n\n    vector<pair<int, int>> onis;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (board[i][j] == 'x') {\n                onis.push_back({i, j});\n            }\n        }\n    }\n\n    vector<pair<char, int>> ans;\n\n    for (auto& oni : onis) {\n        int i = oni.first;\n        int j = oni.second;\n        vector<pair<int, char>> options;\n\n        bool valid = true;\n        for (int k = 0; k < i; k++) {\n            if (board[k][j] == 'o') {\n                valid = false;\n                break;\n            }\n        }\n        if (valid) {\n            options.push_back({2 * (i + 1), 'U'});\n        }\n\n        valid = true;\n        for (int k = i + 1; k < N; k++) {\n            if (board[k][j] == 'o') {\n                valid = false;\n                break;\n            }\n        }\n        if (valid) {\n            options.push_back({2 * (N - i), 'D'});\n        }\n\n        valid = true;\n        for (int k = 0; k < j; k++) {\n            if (board[i][k] == 'o') {\n                valid = false;\n                break;\n            }\n        }\n        if (valid) {\n            options.push_back({2 * (j + 1), 'L'});\n        }\n\n        valid = true;\n        for (int k = j + 1; k < N; k++) {\n            if (board[i][k] == 'o') {\n                valid = false;\n                break;\n            }\n        }\n        if (valid) {\n            options.push_back({2 * (N - j), 'R'});\n        }\n\n        if (options.empty()) {\n            vector<pair<int, char>> all_options = {\n                {2*(i+1), 'U'},\n                {2*(N-i), 'D'},\n                {2*(j+1), 'L'},\n                {2*(N-j), 'R'}\n            };\n            sort(all_options.begin(), all_options.end());\n            char chosen_dir = all_options[0].second;\n            if (chosen_dir == 'U') {\n                for (int k = 0; k <= i; k++) ans.push_back({'U', j});\n                for (int k = 0; k <= i; k++) ans.push_back({'D', j});\n            } else if (chosen_dir == 'D') {\n                for (int k = 0; k < N - i; k++) ans.push_back({'D', j});\n                for (int k = 0; k < N - i; k++) ans.push_back({'U', j});\n            } else if (chosen_dir == 'L') {\n                for (int k = 0; k <= j; k++) ans.push_back({'L', i});\n                for (int k = 0; k <= j; k++) ans.push_back({'R', i});\n            } else if (chosen_dir == 'R') {\n                for (int k = 0; k < N - j; k++) ans.push_back({'R', i});\n                for (int k = 0; k < N - j; k++) ans.push_back({'L', i});\n            }\n        } else {\n            sort(options.begin(), options.end());\n            char chosen_dir = options[0].second;\n            if (chosen_dir == 'U') {\n                for (int k = 0; k <= i; k++) ans.push_back({'U', j});\n                for (int k = 0; k <= i; k++) ans.push_back({'D', j});\n            } else if (chosen_dir == 'D') {\n                for (int k = 0; k < N - i; k++) ans.push_back({'D', j});\n                for (int k = 0; k < N - i; k++) ans.push_back({'U', j});\n            } else if (chosen_dir == 'L') {\n                for (int k = 0; k <= j; k++) ans.push_back({'L', i});\n                for (int k = 0; k <= j; k++) ans.push_back({'R', i});\n            } else if (chosen_dir == 'R') {\n                for (int k = 0; k < N - j; k++) ans.push_back({'R', i});\n                for (int k = 0; k < N - j; k++) ans.push_back({'L', i});\n            }\n        }\n    }\n\n    for (auto& move : ans) {\n        cout << move.first << \" \" << move.second << '\\n';\n    }\n\n    return 0;\n}","ahc044":"#include <iostream>\n#include <vector>\n#include <cstdlib>\n#include <algorithm>\n#include <random>\n#include <cmath>\n#include <climits>\nusing namespace std;\n\nlong long simulate(vector<int>& a, vector<int>& b, const vector<int>& T, int L) {\n    int N = a.size();\n    vector<int> total_count(N, 0);\n    vector<char> parity(N, 0);\n\n    vector<int> next_table_arr(2 * N);\n    for (int i = 0; i < N; i++) {\n        next_table_arr[i] = b[i];\n        next_table_arr[N + i] = a[i];\n    }\n\n    total_count[0] = 1;\n    parity[0] = 1;\n    int current = 0;\n\n    for (int week = 2; week <= L; week++) {\n        int p = parity[current];\n        int next_emp = next_table_arr[p * N + current];\n        total_count[next_emp]++;\n        parity[next_emp] ^= 1;\n        current = next_emp;\n    }\n\n    long long error = 0;\n    for (int i = 0; i < N; i++) {\n        error += abs(total_count[i] - T[i]);\n    }\n    return error;\n}\n\nint main() {\n    int N, L;\n    cin >> N >> L;\n    vector<int> T(N);\n    for (int i = 0; i < N; i++) {\n        cin >> T[i];\n    }\n\n    mt19937 rng(0);\n    vector<int> a(N), b(N);\n    for (int i = 0; i < N; i++) {\n        a[i] = uniform_int_distribution<int>(0, N-1)(rng);\n        b[i] = uniform_int_distribution<int>(0, N-1)(rng);\n    }\n\n    long long best_error = simulate(a, b, T, L);\n    vector<int> best_a = a;\n    vector<int> best_b = b;\n\n    int iterations = 200;\n    uniform_int_distribution<int> emp_dist(0, N-1);\n    uniform_int_distribution<int> bit_dist(0, 1);\n\n    for (int iter = 0; iter < iterations; iter++) {\n        vector<int> new_a = a;\n        vector<int> new_b = b;\n\n        int i = emp_dist(rng);\n        int which = bit_dist(rng);\n        int old_val;\n\n        if (which == 0) {\n            old_val = a[i];\n            int new_val;\n            do {\n                new_val = emp_dist(rng);\n            } while (new_val == old_val);\n            new_a[i] = new_val;\n        } else {\n            old_val = b[i];\n            int new_val;\n            do {\n                new_val = emp_dist(rng);\n            } while (new_val == old_val);\n            new_b[i] = new_val;\n        }\n\n        long long new_error = simulate(new_a, new_b, T, L);\n        if (new_error < best_error) {\n            best_error = new_error;\n            best_a = new_a;\n            best_b = new_b;\n            a = new_a;\n            b = new_b;\n        }\n    }\n\n    for (int i = 0; i < N; i++) {\n        cout << best_a[i] << \" \" << best_b[i] << endl;\n    }\n\n    return 0;\n}","ahc045":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <cstdint>\nusing namespace std;\n\nuint32_t xy2d(uint32_t n, uint32_t x, uint32_t y) {\n    uint32_t rx, ry, s, d = 0;\n    for (s = n / 2; s > 0; s /= 2) {\n        rx = (x & s) > 0;\n        ry = (y & s) > 0;\n        d += s * s * ((3 * rx) ^ ry);\n        if (ry == 0) {\n            if (rx == 1) {\n                x = s - 1 - x;\n                y = s - 1 - y;\n            }\n            swap(x, y);\n        }\n    }\n    return d;\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, Q_max, L, W;\n    cin >> N >> M >> Q_max >> L >> W;\n    vector<int> G(M);\n    for (int i = 0; i < M; i++) {\n        cin >> G[i];\n    }\n\n    vector<int> lx(N), rx(N), ly(N), ry(N);\n    vector<int> cx(N), cy(N);\n    for (int i = 0; i < N; i++) {\n        cin >> lx[i] >> rx[i] >> ly[i] >> ry[i];\n        cx[i] = (lx[i] + rx[i]) / 2;\n        cy[i] = (ly[i] + ry[i]) / 2;\n    }\n\n    const uint32_t n_hilbert = 1 << 16;\n    const long long scale_val = (1LL << 16) - 1;\n    vector<uint32_t> hilbert_index(N);\n    for (int i = 0; i < N; i++) {\n        long long nx_val = static_cast<long long>(cx[i]) * scale_val;\n        long long ny_val = static_cast<long long>(cy[i]) * scale_val;\n        uint32_t nx = static_cast<uint32_t>(nx_val / 10000);\n        uint32_t ny = static_cast<uint32_t>(ny_val / 10000);\n        hilbert_index[i] = xy2d(n_hilbert, nx, ny);\n    }\n\n    vector<int> cities(N);\n    for (int i = 0; i < N; i++) cities[i] = i;\n    sort(cities.begin(), cities.end(), [&](int i, int j) {\n        return hilbert_index[i] < hilbert_index[j];\n    });\n\n    vector<vector<int>> groups(M);\n    int idx = 0;\n    for (int k = 0; k < M; k++) {\n        groups[k].resize(G[k]);\n        for (int j = 0; j < G[k]; j++) {\n            groups[k][j] = cities[idx++];\n        }\n    }\n\n    int query_count = 0;\n    vector<vector<pair<int, int>>> group_edges(M);\n\n    auto query_cluster = [](const vector<int>& cluster) {\n        vector<int> sorted_cluster = cluster;\n        sort(sorted_cluster.begin(), sorted_cluster.end());\n        cout << \"? \" << sorted_cluster.size();\n        for (int c : sorted_cluster) {\n            cout << \" \" << c;\n        }\n        cout << endl;\n        cout.flush();\n\n        vector<pair<int, int>> edges;\n        for (int i = 0; i < sorted_cluster.size() - 1; i++) {\n            int a, b;\n            cin >> a >> b;\n            edges.push_back({a, b});\n        }\n        return edges;\n    };\n\n    for (int k = 0; k < M; k++) {\n        vector<int>& group = groups[k];\n        int n = group.size();\n        if (n == 1) {\n            continue;\n        }\n\n        vector<pair<int, int>> edges;\n        int i = 0;\n        while (i < n && query_count < Q_max) {\n            int take = min(L, n - i);\n            if (take < 2) break;\n\n            vector<int> cluster;\n            for (int j = 0; j < take; j++) {\n                cluster.push_back(group[i + j]);\n            }\n\n            vector<pair<int, int>> e = query_cluster(cluster);\n            query_count++;\n            for (const auto& edge : e) {\n                edges.push_back(edge);\n            }\n\n            i += (take - 1);\n        }\n\n        if (i < n - 1) {\n            edges.push_back({group[i], group[i + 1]});\n            for (int j = i + 1; j < n - 1; j++) {\n                edges.push_back({group[j], group[j + 1]});\n            }\n        }\n\n        group_edges[k] = edges;\n    }\n\n    cout << \"!\" << endl;\n    for (int k = 0; k < M; k++) {\n        vector<int>& group = groups[k];\n        for (int j = 0; j < group.size(); j++) {\n            if (j > 0) cout << \" \";\n            cout << group[j];\n        }\n        cout << endl;\n\n        vector<pair<int, int>>& edges = group_edges[k];\n        for (const auto& edge : edges) {\n            cout << edge.first << \" \" << edge.second << endl;\n        }\n    }\n\n    return 0;\n}","ahc046":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int N, M;\n    cin >> N >> M;\n    vector<pair<int, int>> points;\n    for (int i = 0; i < M; i++) {\n        int a, b;\n        cin >> a >> b;\n        points.push_back({a, b});\n    }\n\n    vector<vector<bool>> blocks(N, vector<bool>(N, false));\n    map<char, pair<int, int>> dirs;\n    dirs['U'] = {-1, 0};\n    dirs['D'] = {1, 0};\n    dirs['L'] = {0, -1};\n    dirs['R'] = {0, 1};\n\n    auto is_blocked = [&](int i, int j) -> bool {\n        if (i < 0 || i >= N || j < 0 || j >= N)\n            return true;\n        return blocks[i][j];\n    };\n\n    vector<string> outputs;\n    int ci = points[0].first, cj = points[0].second;\n\n    for (int idx = 1; idx < M; idx++) {\n        int ti = points[idx].first, tj = points[idx].second;\n        while (ci != ti || cj != tj) {\n            int current_manhattan = abs(ci - ti) + abs(cj - tj);\n            double best_score = -1e9;\n            char best_act = ' ';\n            char best_dir = ' ';\n\n            for (char d : {'U', 'D', 'L', 'R'}) {\n                int dx = dirs[d].first, dy = dirs[d].second;\n                int ni = ci + dx;\n                int nj = cj + dy;\n\n                if (ni >= 0 && ni < N && nj >= 0 && nj < N) {\n                    if (!blocks[ni][nj]) {\n                        int new_manhattan = abs(ni - ti) + abs(nj - tj);\n                        int reduction = current_manhattan - new_manhattan;\n                        if (reduction > best_score) {\n                            best_score = reduction;\n                            best_act = 'M';\n                            best_dir = d;\n                        }\n\n                        int slide_steps = 1;\n                        int nni = ni, nnj = nj;\n                        while (true) {\n                            nni += dx;\n                            nnj += dy;\n                            if (nni < 0 || nni >= N || nnj < 0 || nnj >= N || blocks[nni][nnj]) {\n                                break;\n                            }\n                            slide_steps++;\n                        }\n                        int slide_i = ci + slide_steps * dx;\n                        int slide_j = cj + slide_steps * dy;\n                        int new_manhattan_slide = abs(slide_i - ti) + abs(slide_j - tj);\n                        int reduction_slide = current_manhattan - new_manhattan_slide;\n                        if (reduction_slide > best_score) {\n                            best_score = reduction_slide;\n                            best_act = 'S';\n                            best_dir = d;\n                        }\n                    } else {\n                        int new_manhattan = abs(ni - ti) + abs(nj - tj);\n                        int reduction = current_manhattan - new_manhattan;\n                        if (reduction > best_score) {\n                            best_score = reduction;\n                            best_act = 'M';\n                            best_dir = d;\n                        }\n\n                        int slide_steps = 1;\n                        int nni = ni, nnj = nj;\n                        while (true) {\n                            nni += dx;\n                            nnj += dy;\n                            if (nni < 0 || nni >= N || nnj < 0 || nnj >= N || blocks[nni][nnj]) {\n                                break;\n                            }\n                            slide_steps++;\n                        }\n                        int slide_i = ci + slide_steps * dx;\n                        int slide_j = cj + slide_steps * dy;\n                        int new_manhattan_slide = abs(slide_i - ti) + abs(slide_j - tj);\n                        int reduction_slide = current_manhattan - new_manhattan_slide;\n                        if (reduction_slide > best_score) {\n                            best_score = reduction_slide;\n                            best_act = 'S';\n                            best_dir = d;\n                        }\n                    }\n                }\n            }\n\n            if (best_score == -1e9) {\n                double best_alter_score = -1e9;\n                char best_alter_dir = 'U';\n                for (char d : {'U', 'D', 'L', 'R'}) {\n                    int dx = dirs[d].first, dy = dirs[d].second;\n                    int ni = ci + dx, nj = cj + dy;\n                    if (ni < 0 || ni >= N || nj < 0 || nj >= N) continue;\n                    int dist_block = abs(ni - ti) + abs(nj - tj);\n                    double alter_score = -dist_block;\n                    if (alter_score > best_alter_score) {\n                        best_alter_score = alter_score;\n                        best_alter_dir = d;\n                    }\n                }\n                best_act = 'A';\n                best_dir = best_alter_dir;\n            }\n\n            outputs.push_back(string(1, best_act) + \" \" + best_dir);\n            int dx = dirs[best_dir].first, dy = dirs[best_dir].second;\n            if (best_act == 'M') {\n                ci = ci + dx;\n                cj = cj + dy;\n            } else if (best_act == 'S') {\n                int steps = 0;\n                int ni = ci, nj = cj;\n                while (true) {\n                    ni += dx;\n                    nj += dy;\n                    if (ni < 0 || ni >= N || nj < 0 || nj >= N || blocks[ni][nj]) {\n                        ni -= dx;\n                        nj -= dy;\n                        break;\n                    }\n                    steps++;\n                }\n                ci = ni;\n                cj = nj;\n            } else if (best_act == 'A') {\n                int ni = ci + dx, nj = cj + dy;\n                blocks[ni][nj] = !blocks[ni][nj];\n            }\n        }\n    }\n\n    for (const string& s : outputs) {\n        cout << s << '\\n';\n    }\n\n    return 0;\n}"},"4":{"ahc001":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <cmath>\n#include <cstdlib>\n#include <iomanip>\nusing namespace std;\n\nstruct Rect {\n    int a, b, c, d;\n    long long area;\n    long long r_val;\n};\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int n;\n    cin >> n;\n    vector<Rect> rects;\n    for (int i = 0; i < n; i++) {\n        int x, y, r;\n        cin >> x >> y >> r;\n        rects.push_back({x, y, x+1, y+1, 1LL, (long long)r});\n    }\n\n    bool changed = true;\n    int pass_count = 0;\n    const int MAX_PASS = 100;\n\n    while (changed && pass_count < MAX_PASS) {\n        pass_count++;\n        changed = false;\n        vector<int> indices(n);\n        for (int i = 0; i < n; i++) {\n            indices[i] = i;\n        }\n\n        sort(indices.begin(), indices.end(), [&](int i, int j) {\n            double sat_i = (double)rects[i].area / rects[i].r_val;\n            double sat_j = (double)rects[j].area / rects[j].r_val;\n            return sat_i < sat_j;\n        });\n\n        for (int idx = 0; idx < n; idx++) {\n            int i = indices[idx];\n            if (rects[i].area >= rects[i].r_val) \n                continue;\n\n            double best_improvement = -1e9;\n            int best_dir = -1;\n            int best_d_use = 0;\n\n            for (int dir = 0; dir < 4; dir++) {\n                int d_max;\n                if (dir == 0) {\n                    d_max = rects[i].a;\n                } else if (dir == 1) {\n                    d_max = 10000 - rects[i].c;\n                } else if (dir == 2) {\n                    d_max = rects[i].b;\n                } else {\n                    d_max = 10000 - rects[i].d;\n                }\n\n                if (d_max <= 0) continue;\n\n                int side_length;\n                if (dir < 2) {\n                    side_length = rects[i].d - rects[i].b;\n                } else {\n                    side_length = rects[i].c - rects[i].a;\n                }\n                if (side_length <= 0) continue;\n\n                for (int j = 0; j < n; j++) {\n                    if (i == j) continue;\n                    if (dir == 0) {\n                        if (rects[i].b < rects[j].d && rects[j].b < rects[i].d) {\n                            if (rects[j].c <= rects[i].a) {\n                                d_max = min(d_max, rects[i].a - rects[j].c);\n                            }\n                        }\n                    } else if (dir == 1) {\n                        if (rects[i].b < rects[j].d && rects[j].b < rects[i].d) {\n                            if (rects[j].a >= rects[i].c) {\n                                d_max = min(d_max, rects[j].a - rects[i].c);\n                            }\n                        }\n                    } else if (dir == 2) {\n                        if (rects[i].a < rects[j].c && rects[j].a < rects[i].c) {\n                            if (rects[j].d <= rects[i].b) {\n                                d_max = min(d_max, rects[i].b - rects[j].d);\n                            }\n                        }\n                    } else if (dir == 3) {\n                        if (rects[i].a < rects[j].c && rects[j].a < rects[i].c) {\n                            if (rects[j].b >= rects[i].d) {\n                                d_max = min(d_max, rects[j].b - rects[i].d);\n                            }\n                        }\n                    }\n                }\n\n                if (d_max <= 0) continue;\n\n                double d0 = (double)(rects[i].r_val - rects[i].area) / side_length;\n                if (d0 < 1e-5) continue;\n\n                vector<int> cands;\n                int d1 = (int)floor(d0);\n                int d2 = (int)ceil(d0);\n                d1 = min(d_max, d1);\n                d2 = min(d_max, d2);\n                if (d1 > 0) cands.push_back(d1);\n                if (d2 > 0 && d2 != d1) cands.push_back(d2);\n\n                double best_improve_dir = -1e9;\n                int best_d_use_dir = -1;\n\n                for (int d_candidate : cands) {\n                    long long new_area = rects[i].area + (long long)d_candidate * side_length;\n                    double min_val = min(rects[i].r_val, rects[i].area);\n                    double max_val = max(rects[i].r_val, rects[i].area);\n                    double old_sat = 1.0 - pow(1.0 - min_val / max_val, 2);\n\n                    min_val = min(rects[i].r_val, new_area);\n                    max_val = max(rects[i].r_val, new_area);\n                    double new_sat = 1.0 - pow(1.0 - min_val / max_val, 2);\n\n                    double improvement = new_sat - old_sat;\n                    if (improvement > best_improve_dir) {\n                        best_improve_dir = improvement;\n                        best_d_use_dir = d_candidate;\n                    }\n                }\n\n                if (best_improve_dir > best_improvement) {\n                    best_improvement = best_improve_dir;\n                    best_dir = dir;\n                    best_d_use = best_d_use_dir;\n                }\n            }\n\n            if (best_improvement > 1e-8) {\n                if (best_dir == 0) {\n                    rects[i].a -= best_d_use;\n                    rects[i].area += (long long)best_d_use * (rects[i].d - rects[i].b);\n                } else if (best_dir == 1) {\n                    rects[i].c += best_d_use;\n                    rects[i].area += (long long)best_d_use * (rects[i].d - rects[i].b);\n                } else if (best_dir == 2) {\n                    rects[i].b -= best_d_use;\n                    rects[i].area += (long long)best_d_use * (rects[i].c - rects[i].a);\n                } else if (best_dir == 3) {\n                    rects[i].d += best_d_use;\n                    rects[i].area += (long long)best_d_use * (rects[i].c - rects[i].a);\n                }\n                changed = true;\n            }\n        }\n    }\n\n    for (int i = 0; i < n; i++) {\n        cout << rects[i].a << \" \" << rects[i].b << \" \" << rects[i].c << \" \" << rects[i].d << \"\\n\";\n    }\n\n    return 0;\n}","ahc002":"#include <iostream>\n#include <vector>\n#include <set>\n#include <algorithm>\n#include <tuple>\nusing namespace std;\n\nint main() {\n    int si, sj;\n    cin >> si >> sj;\n    \n    vector<vector<int>> t(50, vector<int>(50));\n    vector<vector<int>> p(50, vector<int>(50));\n    \n    for (int i = 0; i < 50; i++) {\n        for (int j = 0; j < 50; j++) {\n            cin >> t[i][j];\n        }\n    }\n    for (int i = 0; i < 50; i++) {\n        for (int j = 0; j < 50; j++) {\n            cin >> p[i][j];\n        }\n    }\n    \n    int M = 0;\n    for (int i = 0; i < 50; i++) {\n        for (int j = 0; j < 50; j++) {\n            if (t[i][j] + 1 > M) {\n                M = t[i][j] + 1;\n            }\n        }\n    }\n    \n    vector<set<int>> adj_tiles(M);\n    const int dx[4] = {1, -1, 0, 0};\n    const int dy[4] = {0, 0, 1, -1};\n    \n    for (int i = 0; i < 50; i++) {\n        for (int j = 0; j < 50; j++) {\n            for (int d = 0; d < 4; d++) {\n                int ni = i + dx[d];\n                int nj = j + dy[d];\n                if (ni >= 0 && ni < 50 && nj >= 0 && nj < 50) {\n                    if (t[i][j] != t[ni][nj]) {\n                        adj_tiles[t[i][j]].insert(t[ni][nj]);\n                    }\n                }\n            }\n        }\n    }\n    \n    vector<int> current_degree(M);\n    for (int i = 0; i < M; i++) {\n        current_degree[i] = adj_tiles[i].size();\n    }\n    \n    vector<bool> visited_tile(M, false);\n    int cx = si, cy = sj;\n    int start_tile = t[si][sj];\n    visited_tile[start_tile] = true;\n    \n    for (int adj : adj_tiles[start_tile]) {\n        if (!visited_tile[adj]) {\n            current_degree[adj]--;\n        }\n    }\n    \n    string path = \"\";\n    const char dir_char[4] = {'D', 'U', 'R', 'L'};\n    \n    while (true) {\n        vector<tuple<int, char, int, int>> candidates;\n        for (int d1 = 0; d1 < 4; d1++) {\n            int nx1 = cx + dx[d1];\n            int ny1 = cy + dy[d1];\n            if (nx1 < 0 || nx1 >= 50 || ny1 < 0 || ny1 >= 50) continue;\n            int tile1 = t[nx1][ny1];\n            if (visited_tile[tile1]) continue;\n            \n            int score1 = p[nx1][ny1];\n            int best_next = 0;\n            \n            for (int d2 = 0; d2 < 4; d2++) {\n                int nx2 = nx1 + dx[d2];\n                int ny2 = ny1 + dy[d2];\n                if (nx2 < 0 || nx2 >= 50 || ny2 < 0 || ny2 >= 50) continue;\n                int tile2 = t[nx2][ny2];\n                if (tile2 == tile1) continue;\n                if (visited_tile[tile2]) continue;\n                \n                bool adjacent = (adj_tiles[tile2].find(tile1) != adj_tiles[tile2].end());\n                int next_deg = current_degree[tile2] - (adjacent ? 1 : 0);\n                int candidate_score = p[nx2][ny2] + 100 * next_deg;\n                if (candidate_score > best_next) {\n                    best_next = candidate_score;\n                }\n            }\n            \n            int total_score = score1 + best_next;\n            candidates.push_back(make_tuple(total_score, dir_char[d1], nx1, ny1));\n        }\n        \n        if (candidates.empty()) break;\n        \n        auto best = *max_element(candidates.begin(), candidates.end(),\n            [](const tuple<int, char, int, int>& a, const tuple<int, char, int, int>& b) {\n                return get<0>(a) < get<0>(b);\n            });\n        \n        char dir = get<1>(best);\n        int nx1 = get<2>(best);\n        int ny1 = get<3>(best);\n        int tile1 = t[nx1][ny1];\n        \n        path += dir;\n        cx = nx1;\n        cy = ny1;\n        visited_tile[tile1] = true;\n        \n        for (int adj : adj_tiles[tile1]) {\n            if (!visited_tile[adj]) {\n                current_degree[adj]--;\n            }\n        }\n    }\n    \n    cout << path << endl;\n    \n    return 0;\n}","ahc003":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\n#include <tuple>\n#include <cmath>\n\nusing namespace std;\n\nconst int N = 30;\nconst double INF = 1e18;\n\nvector<vector<double>> h_est(N, vector<double>(N-1, 5000.0));\nvector<vector<double>> v_est(N-1, vector<double>(N, 5000.0));\nvector<vector<int>> count_h(N, vector<int>(N-1, 0));\nvector<vector<int>> count_v(N-1, vector<int>(N, 0));\n\nconst int dx[4] = {0, 1, 0, -1};\nconst int dy[4] = {1, 0, -1, 0};\nconst char dir_char[4] = {'R', 'D', 'L', 'U'};\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    for (int k = 0; k < 1000; k++) {\n        int si, sj, ti, tj;\n        cin >> si >> sj >> ti >> tj;\n\n        vector<vector<double>> dist(N, vector<double>(N, INF));\n        vector<vector<pair<int, int>>> prev(N, vector<pair<int, int>>(N, {-1, -1}));\n        priority_queue<tuple<double, int, int>, vector<tuple<double, int, int>>, greater<tuple<double, int, int>>> pq;\n\n        dist[si][sj] = 0;\n        pq.push({0, si, sj});\n\n        while (!pq.empty()) {\n            auto [d, i, j] = pq.top();\n            pq.pop();\n            if (d > dist[i][j]) continue;\n            if (i == ti && j == tj) break;\n\n            for (int dir = 0; dir < 4; dir++) {\n                int ni = i + dx[dir];\n                int nj = j + dy[dir];\n                if (ni < 0 || ni >= N || nj < 0 || nj >= N) continue;\n\n                double weight;\n                if (dir == 0) weight = h_est[i][j];\n                else if (dir == 1) weight = v_est[i][j];\n                else if (dir == 2) weight = h_est[i][j-1];\n                else weight = v_est[i-1][j];\n\n                double new_dist = dist[i][j] + weight;\n                if (new_dist < dist[ni][nj]) {\n                    dist[ni][nj] = new_dist;\n                    prev[ni][nj] = {i, j};\n                    pq.push({new_dist, ni, nj});\n                }\n            }\n        }\n\n        vector<char> moves;\n        int ci = ti, cj = tj;\n        while (ci != si || cj != sj) {\n            auto [pi, pj] = prev[ci][cj];\n            if (pi == ci) moves.push_back(pj < cj ? 'R' : 'L');\n            else moves.push_back(pi < ci ? 'D' : 'U');\n            ci = pi;\n            cj = pj;\n        }\n        reverse(moves.begin(), moves.end());\n        cout << string(moves.begin(), moves.end()) << endl;\n        cout.flush();\n\n        long long z_k;\n        cin >> z_k;\n\n        double estimated_sum = dist[ti][tj];\n        double relative_error = (z_k - estimated_sum) / estimated_sum;\n        int ci2 = si, cj2 = sj;\n        \n        for (char move : moves) {\n            double* w_est = nullptr;\n            int* cnt = nullptr;\n            if (move == 'R') {\n                w_est = &h_est[ci2][cj2];\n                cnt = &count_h[ci2][cj2];\n                cj2++;\n            } else if (move == 'D') {\n                w_est = &v_est[ci2][cj2];\n                cnt = &count_v[ci2][cj2];\n                ci2++;\n            } else if (move == 'L') {\n                w_est = &h_est[ci2][cj2-1];\n                cnt = &count_h[ci2][cj2-1];\n                cj2--;\n            } else {\n                w_est = &v_est[ci2-1][cj2];\n                cnt = &count_v[ci2-1][cj2];\n                ci2--;\n            }\n            \n            double alpha = 1.0 / sqrt(1 + *cnt);\n            double new_est = *w_est * (1.0 + alpha * relative_error);\n            *w_est = max(1000.0, min(9000.0, new_est));\n            (*cnt)++;\n        }\n    }\n\n    return 0;\n}","ahc004":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <string>\n#include <cmath>\n#include <cstdlib>\n#include <ctime>\n#include <random>\nusing namespace std;\n\nbool is_subsequence(vector<string> &mat, string s) {\n    int len = s.length();\n    int N = mat.size();\n    for (int dir = 0; dir < 2; dir++) {\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                bool match = true;\n                for (int p = 0; p < len; p++) {\n                    int ni = i, nj = j;\n                    if (dir == 0) {\n                        nj = (j + p) % N;\n                        if (nj < 0) nj += N;\n                    } else {\n                        ni = (i + p) % N;\n                        if (ni < 0) ni += N;\n                    }\n                    if (mat[ni][nj] != s[p]) {\n                        match = false;\n                        break;\n                    }\n                }\n                if (match) {\n                    return true;\n                }\n            }\n        }\n    }\n    return false;\n}\n\nint compute_coverage(vector<string> &mat, vector<string> &strings) {\n    int covered = 0;\n    for (const string& s : strings) {\n        if (is_subsequence(mat, s)) {\n            covered++;\n        }\n    }\n    return covered;\n}\n\nint main() {\n    srand(time(0));\n    int N, M;\n    cin >> N >> M;\n    vector<string> strings(M);\n    for (int i = 0; i < M; i++) {\n        cin >> strings[i];\n    }\n\n    auto build_greedy = [&](bool desc) {\n        vector<string> mat(N, string(N, '.'));\n        vector<string> sorted = strings;\n        if (desc) {\n            sort(sorted.begin(), sorted.end(), [](const string& a, const string& b) {\n                return a.size() > b.size();\n            });\n        } else {\n            sort(sorted.begin(), sorted.end(), [](const string& a, const string& b) {\n                return a.size() < b.size();\n            });\n        }\n        for (const string& s : sorted) {\n            int len = s.size();\n            for (int dir = 0; dir < 2; dir++) {\n                for (int i = 0; i < N; i++) {\n                    for (int j = 0; j < N; j++) {\n                        bool ok = true;\n                        for (int p = 0; p < len; p++) {\n                            int ni = i, nj = j;\n                            if (dir == 0) {\n                                nj = (j + p) % N;\n                                if (nj < 0) nj += N;\n                            } else {\n                                ni = (i + p) % N;\n                                if (ni < 0) ni += N;\n                            }\n                            if (mat[ni][nj] != '.' && mat[ni][nj] != s[p]) {\n                                ok = false;\n                                break;\n                            }\n                        }\n                        if (ok) {\n                            for (int p = 0; p < len; p++) {\n                                int ni = i, nj = j;\n                                if (dir == 0) {\n                                    nj = (j + p) % N;\n                                    if (nj < 0) nj += N;\n                                } else {\n                                    ni = (i + p) % N;\n                                    if (ni < 0) ni += N;\n                                }\n                                if (mat[ni][nj] == '.') {\n                                    mat[ni][nj] = s[p];\n                                }\n                            }\n                            goto next_string;\n                        }\n                    }\n                }\n            }\n            next_string: ;\n        }\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                if (mat[i][j] == '.') {\n                    mat[i][j] = 'A';\n                }\n            }\n        }\n        return mat;\n    };\n\n    vector<string> mat1 = build_greedy(true);\n    int c1 = compute_coverage(mat1, strings);\n    vector<string> mat2 = build_greedy(false);\n    int c2 = compute_coverage(mat2, strings);\n\n    vector<string> best_mat = (c1 >= c2) ? mat1 : mat2;\n    int best_cov = max(c1, c2);\n\n    if (best_cov == M) {\n        for (int i = 0; i < N; i++) {\n            cout << best_mat[i] << endl;\n        }\n        return 0;\n    }\n\n    vector<string> current = best_mat;\n    int current_cov = best_cov;\n    const int MAX_ITER = 100;\n    const double T0 = 10.0;\n\n    for (int iter = 0; iter < MAX_ITER; iter++) {\n        double T = T0 * (1.0 - static_cast<double>(iter) / MAX_ITER);\n        int i = rand() % N;\n        int j = rand() % N;\n        char old_char = current[i][j];\n        char new_char;\n        do {\n            new_char = 'A' + (rand() % 8);\n        } while (new_char == old_char);\n\n        vector<string> candidate = current;\n        candidate[i][j] = new_char;\n        int new_cov = compute_coverage(candidate, strings);\n\n        if (new_cov > best_cov) {\n            best_cov = new_cov;\n            best_mat = candidate;\n        }\n\n        if (new_cov > current_cov) {\n            current = candidate;\n            current_cov = new_cov;\n        } else {\n            double delta = new_cov - current_cov;\n            if (delta == 0) {\n                current = candidate;\n                current_cov = new_cov;\n            } else {\n                double prob = exp(delta / T);\n                double r = static_cast<double>(rand()) / RAND_MAX;\n                if (r < prob) {\n                    current = candidate;\n                    current_cov = new_cov;\n                }\n            }\n        }\n    }\n\n    for (int i = 0; i < N; i++) {\n        cout << best_mat[i] << endl;\n    }\n    return 0;\n}","ahc005":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int di[] = {-1, 1, 0, 0};\nconst int dj[] = {0, 0, -1, 1};\nconst char dir_char[] = {'U', 'D', 'L', 'R'};\nconst int INF = 1e9;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, si, sj;\n    cin >> N >> si >> sj;\n    vector<string> grid;\n    for (int i = 0; i < N; i++) {\n        string s;\n        cin >> s;\n        grid.push_back(s);\n    }\n\n    vector<vector<int>> h_segment_id(N, vector<int>(N, -1));\n    vector<vector<int>> v_segment_id(N, vector<int>(N, -1));\n    vector<vector<pair<int, int>>> horizontal_segments;\n    vector<vector<pair<int, int>>> vertical_segments;\n\n    for (int i = 0; i < N; i++) {\n        int j = 0;\n        while (j < N) {\n            if (grid[i][j] == '#') {\n                j++;\n                continue;\n            }\n            vector<pair<int, int>> seg;\n            while (j < N && grid[i][j] != '#') {\n                seg.push_back({i, j});\n                j++;\n            }\n            int seg_id = horizontal_segments.size();\n            horizontal_segments.push_back(seg);\n            for (const auto& p : seg) {\n                h_segment_id[p.first][p.second] = seg_id;\n            }\n        }\n    }\n\n    for (int j = 0; j < N; j++) {\n        int i = 0;\n        while (i < N) {\n            if (grid[i][j] == '#') {\n                i++;\n                continue;\n            }\n            vector<pair<int, int>> seg;\n            while (i < N && grid[i][j] != '#') {\n                seg.push_back({i, j});\n                i++;\n            }\n            int seg_id = vertical_segments.size();\n            vertical_segments.push_back(seg);\n            for (const auto& p : seg) {\n                v_segment_id[p.first][p.second] = seg_id;\n            }\n        }\n    }\n\n    int totalH = horizontal_segments.size();\n    int totalV = vertical_segments.size();\n    vector<bool> coveredH(totalH, false);\n    vector<bool> coveredV(totalV, false);\n    int cur_i = si, cur_j = sj;\n    string route = \"\";\n\n    auto coverCell = [&](int i, int j) {\n        int h_id = h_segment_id[i][j];\n        int v_id = v_segment_id[i][j];\n        if (h_id != -1) coveredH[h_id] = true;\n        if (v_id != -1) coveredV[v_id] = true;\n    };\n\n    coverCell(si, sj);\n\n    while (true) {\n        bool allCovered = true;\n        for (bool b : coveredH) if (!b) { allCovered = false; break; }\n        if (allCovered) {\n            for (bool b : coveredV) if (!b) { allCovered = false; break; }\n            if (allCovered) break;\n        }\n\n        vector<vector<int>> dist(N, vector<int>(N, INF));\n        vector<vector<pair<int, int>>> prev(N, vector<pair<int, int>>(N, {-1, -1}));\n        priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<tuple<int, int, int>>> pq;\n        dist[cur_i][cur_j] = 0;\n        pq.push({0, cur_i, cur_j});\n\n        while (!pq.empty()) {\n            auto [cost, i, j] = pq.top();\n            pq.pop();\n            if (cost != dist[i][j]) continue;\n            for (int d = 0; d < 4; d++) {\n                int ni = i + di[d];\n                int nj = j + dj[d];\n                if (ni < 0 || ni >= N || nj < 0 || nj >= N || grid[ni][nj] == '#') continue;\n                int weight = grid[ni][nj] - '0';\n                int new_cost = cost + weight;\n                if (new_cost < dist[ni][nj]) {\n                    dist[ni][nj] = new_cost;\n                    prev[ni][nj] = {i, j};\n                    pq.push({new_cost, ni, nj});\n                }\n            }\n        }\n\n        int min_dist = INF;\n        int target_i = -1, target_j = -1;\n        int target_seg_id = -1;\n        bool is_target_horizontal = false;\n\n        for (int idx = 0; idx < totalH; idx++) {\n            if (coveredH[idx]) continue;\n            for (const auto& cell : horizontal_segments[idx]) {\n                int i = cell.first, j = cell.second;\n                if (dist[i][j] < min_dist) {\n                    min_dist = dist[i][j];\n                    target_i = i;\n                    target_j = j;\n                    target_seg_id = idx;\n                    is_target_horizontal = true;\n                }\n            }\n        }\n\n        for (int idx = 0; idx < totalV; idx++) {\n            if (coveredV[idx]) continue;\n            for (const auto& cell : vertical_segments[idx]) {\n                int i = cell.first, j = cell.second;\n                if (dist[i][j] < min_dist) {\n                    min_dist = dist[i][j];\n                    target_i = i;\n                    target_j = j;\n                    target_seg_id = idx;\n                    is_target_horizontal = false;\n                }\n            }\n        }\n\n        if (min_dist == INF) break;\n\n        vector<pair<int, int>> path_cells;\n        int ci = target_i, cj = target_j;\n        while (ci != cur_i || cj != cur_j) {\n            path_cells.push_back({ci, cj});\n            auto p = prev[ci][cj];\n            ci = p.first;\n            cj = p.second;\n        }\n        reverse(path_cells.begin(), path_cells.end());\n\n        int now_i = cur_i, now_j = cur_j;\n        string path_str = \"\";\n        bool covered_target_segment = false;\n        for (const auto& cell : path_cells) {\n            int ni = cell.first, nj = cell.second;\n            char move;\n            if (ni == now_i + 1) move = 'D';\n            else if (ni == now_i - 1) move = 'U';\n            else if (nj == now_j + 1) move = 'R';\n            else if (nj == now_j - 1) move = 'L';\n            else continue;\n            \n            path_str += move;\n            now_i = ni;\n            now_j = nj;\n            coverCell(ni, nj);\n            \n            if (is_target_horizontal) {\n                if (h_segment_id[ni][nj] == target_seg_id) {\n                    covered_target_segment = true;\n                    break;\n                }\n            } else {\n                if (v_segment_id[ni][nj] == target_seg_id) {\n                    covered_target_segment = true;\n                    break;\n                }\n            }\n        }\n        route += path_str;\n        cur_i = now_i;\n        cur_j = now_j;\n    }\n\n    vector<vector<int>> dist_return(N, vector<int>(N, INF));\n    vector<vector<pair<int, int>>> prev_return(N, vector<pair<int, int>>(N, {-1, -1}));\n    priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<tuple<int, int, int>>> pq_return;\n    dist_return[cur_i][cur_j] = 0;\n    pq_return.push({0, cur_i, cur_j});\n    while (!pq_return.empty()) {\n        auto [cost, i, j] = pq_return.top();\n        pq_return.pop();\n        if (cost != dist_return[i][j]) continue;\n        if (i == si && j == sj) break;\n        for (int d = 0; d < 4; d++) {\n            int ni = i + di[d];\n            int nj = j + dj[d];\n            if (ni < 0 || ni >= N || nj < 0 || nj >= N || grid[ni][nj] == '#') continue;\n            int weight = grid[ni][nj] - '0';\n            int new_cost = cost + weight;\n            if (new_cost < dist_return[ni][nj]) {\n                dist_return[ni][nj] = new_cost;\n                prev_return[ni][nj] = {i, j};\n                pq_return.push({new_cost, ni, nj});\n            }\n        }\n    }\n\n    vector<pair<int, int>> return_path;\n    int ci = si, cj = sj;\n    while (ci != cur_i || cj != cur_j) {\n        return_path.push_back({ci, cj});\n        auto p = prev_return[ci][cj];\n        ci = p.first;\n        cj = p.second;\n    }\n    reverse(return_path.begin(), return_path.end());\n    int now_i = cur_i, now_j = cur_j;\n    string return_str = \"\";\n    for (const auto& cell : return_path) {\n        int ni = cell.first, nj = cell.second;\n        if (ni == now_i + 1) return_str += 'D';\n        else if (ni == now_i - 1) return_str += 'U';\n        else if (nj == now_j + 1) return_str += 'R';\n        else if (nj == now_j - 1) return_str += 'L';\n        now_i = ni;\n        now_j = nj;\n    }\n    route += return_str;\n\n    cout << route << endl;\n\n    return 0;\n}","future-contest-2022-qual":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <set>\n#include <algorithm>\n#include <cmath>\n#include <iomanip>\n#include <cctype>\n#include <cstdio>\n#include <cstring>\n#include <map>\n#include <unordered_map>\n#include <unordered_set>\n#include <stack>\n#include <deque>\n#include <string>\n#include <climits>\n#include <cfloat>\n#include <bitset>\nusing namespace std;\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, K, R;\n    cin >> N >> M >> K >> R;\n\n    vector<vector<int>> task_diff(N, vector<int>(K));\n    for (int i = 0; i < N; i++) {\n        for (int k = 0; k < K; k++) {\n            cin >> task_diff[i][k];\n        }\n    }\n\n    vector<vector<int>> children(N+1);\n    vector<vector<int>> parents(N+1);\n    vector<int> in_degree(N+1, 0);\n    for (int i = 0; i < R; i++) {\n        int u, v;\n        cin >> u >> v;\n        children[u].push_back(v);\n        parents[v].push_back(u);\n        in_degree[v]++;\n    }\n\n    vector<int> num_desc(N+1, 0);\n    for (int i = 1; i <= N; i++) {\n        vector<bool> visited(N+1, false);\n        queue<int> q;\n        q.push(i);\n        visited[i] = true;\n        int count = 0;\n        while (!q.empty()) {\n            int u = q.front(); q.pop();\n            count++;\n            for (int v : children[u]) {\n                if (!visited[v]) {\n                    visited[v] = true;\n                    q.push(v);\n                }\n            }\n        }\n        num_desc[i] = count;\n    }\n\n    vector<bool> completed(N+1, false);\n    vector<bool> assigned(N+1, false);\n    vector<int> task_start_day(N+1, -1);\n    vector<int> member_status(M+1, -1);\n    vector<vector<double>> s_hat(M+1, vector<double>(K, 10.0));\n    vector<vector<int>> min_skill_lower_bound(M+1, vector<int>(K, 0));\n    set<int> available_set;\n    for (int i = 1; i <= N; i++) {\n        if (in_degree[i] == 0) {\n            available_set.insert(i);\n        }\n    }\n\n    int day = 0;\n    while (true) {\n        day++;\n        if (day > 1) {\n            int n_finished;\n            cin >> n_finished;\n            if (n_finished == -1) {\n                break;\n            }\n            vector<int> finished_members(n_finished);\n            for (int i = 0; i < n_finished; i++) {\n                cin >> finished_members[i];\n            }\n            for (int j : finished_members) {\n                int task_i = member_status[j];\n                completed[task_i] = true;\n                assigned[task_i] = false;\n                member_status[j] = -1;\n                for (int child : children[task_i]) {\n                    in_degree[child]--;\n                    if (in_degree[child] == 0 && !completed[child] && !assigned[child]) {\n                        available_set.insert(child);\n                    }\n                }\n                int start_day = task_start_day[task_i];\n                int t_days = day - start_day;\n                vector<int>& d_vec = task_diff[task_i-1];\n                if (t_days == 1) {\n                    for (int k = 0; k < K; k++) {\n                        if (d_vec[k] > min_skill_lower_bound[j][k]) {\n                            min_skill_lower_bound[j][k] = d_vec[k];\n                        }\n                        if (s_hat[j][k] < min_skill_lower_bound[j][k]) {\n                            s_hat[j][k] = min_skill_lower_bound[j][k];\n                        }\n                    }\n                } else {\n                    double w_hat = 0.0;\n                    for (int k = 0; k < K; k++) {\n                        if (d_vec[k] > s_hat[j][k]) {\n                            w_hat += (d_vec[k] - s_hat[j][k]);\n                        }\n                    }\n                    double w_obs = t_days;\n                    double e = w_obs - w_hat;\n                    double learning_rate = 0.1;\n                    for (int k = 0; k < K; k++) {\n                        if (d_vec[k] > s_hat[j][k]) {\n                            s_hat[j][k] -= learning_rate * e;\n                            if (s_hat[j][k] < min_skill_lower_bound[j][k]) {\n                                s_hat[j][k] = min_skill_lower_bound[j][k];\n                            }\n                        } else {\n                            if (s_hat[j][k] < min_skill_lower_bound[j][k]) {\n                                s_hat[j][k] = min_skill_lower_bound[j][k];\n                            }\n                        }\n                    }\n                }\n            }\n        }\n\n        vector<int> free_members;\n        for (int j = 1; j <= M; j++) {\n            if (member_status[j] == -1) {\n                free_members.push_back(j);\n            }\n        }\n\n        if (free_members.empty() || available_set.empty()) {\n            cout << 0 << endl;\n            for (int j = 1; j <= M; j++) {\n                cout << \"#s \" << j;\n                for (int k = 0; k < K; k++) {\n                    cout << \" \" << (int)round(s_hat[j][k]);\n                }\n                cout << endl;\n            }\n            cout.flush();\n            continue;\n        }\n\n        vector<double> base_time(N+1, 0.0);\n        for (int i = 1; i <= N; i++) {\n            if (completed[i]) {\n                base_time[i] = 0.0;\n                continue;\n            }\n            double min_est = 1e18;\n            for (int j = 1; j <= M; j++) {\n                double w = 0.0;\n                for (int k = 0; k < K; k++) {\n                    if (task_diff[i-1][k] > s_hat[j][k]) {\n                        w += task_diff[i-1][k] - s_hat[j][k];\n                    }\n                }\n                if (w < 1e-5) {\n                    w = 1.0;\n                }\n                if (w < min_est) {\n                    min_est = w;\n                }\n            }\n            base_time[i] = min_est;\n        }\n\n        vector<double> earliest_start(N+1, 0.0);\n        vector<int> in_degree_temp = in_degree;\n        queue<int> q_forward;\n        for (int i = 1; i <= N; i++) {\n            if (in_degree_temp[i] == 0) {\n                q_forward.push(i);\n            }\n        }\n        while (!q_forward.empty()) {\n            int u = q_forward.front();\n            q_forward.pop();\n            for (int v : children[u]) {\n                if (earliest_start[v] < earliest_start[u] + base_time[u]) {\n                    earliest_start[v] = earliest_start[u] + base_time[u];\n                }\n                in_degree_temp[v]--;\n                if (in_degree_temp[v] == 0) {\n                    q_forward.push(v);\n                }\n            }\n        }\n\n        double total_project_time = 0.0;\n        for (int i = 1; i <= N; i++) {\n            total_project_time = max(total_project_time, earliest_start[i] + base_time[i]);\n        }\n\n        vector<double> latest_finish(N+1, total_project_time);\n        vector<double> latest_start(N+1, 0.0);\n        vector<int> out_degree(N+1, 0);\n        for (int i = 1; i <= N; i++) {\n            out_degree[i] = children[i].size();\n        }\n        vector<int> out_degree_temp = out_degree;\n        queue<int> q_backward;\n        for (int i = 1; i <= N; i++) {\n            if (out_degree_temp[i] == 0) {\n                q_backward.push(i);\n            }\n        }\n        while (!q_backward.empty()) {\n            int u = q_backward.front();\n            q_backward.pop();\n            latest_start[u] = latest_finish[u] - base_time[u];\n            for (int p : parents[u]) {\n                if (latest_finish[p] > latest_start[u]) {\n                    latest_finish[p] = latest_start[u];\n                }\n                out_degree_temp[p]--;\n                if (out_degree_temp[p] == 0) {\n                    q_backward.push(p);\n                }\n            }\n        }\n\n        vector<double> slack(N+1, 0.0);\n        for (int i = 1; i <= N; i++) {\n            slack[i] = latest_start[i] - earliest_start[i];\n            if (slack[i] < 0) slack[i] = 0;\n        }\n\n        vector<int> available_tasks_list(available_set.begin(), available_set.end());\n        int n_tasks = available_tasks_list.size();\n        int n_free = free_members.size();\n        vector<vector<double>> time_est(n_tasks, vector<double>(n_free, 0.0));\n        for (int i = 0; i < n_tasks; i++) {\n            int task_id = available_tasks_list[i];\n            vector<int>& d_vec = task_diff[task_id-1];\n            for (int j = 0; j < n_free; j++) {\n                int member_id = free_members[j];\n                double w = 0.0;\n                for (int k = 0; k < K; k++) {\n                    if (d_vec[k] > s_hat[member_id][k]) {\n                        w += d_vec[k] - s_hat[member_id][k];\n                    }\n                }\n                if (w < 1e-5) {\n                    w = 1.0;\n                }\n                time_est[i][j] = w;\n            }\n        }\n\n        vector<pair<double, int>> task_priority;\n        for (int i = 0; i < n_tasks; i++) {\n            int task_id = available_tasks_list[i];\n            task_priority.push_back({slack[task_id], i});\n        }\n        sort(task_priority.begin(), task_priority.end(), [&](const pair<double, int>& a, const pair<double, int>& b) {\n            if (a.first != b.first) {\n                return a.first < b.first;\n            }\n            int task_a = available_tasks_list[a.second];\n            int task_b = available_tasks_list[b.second];\n            return num_desc[task_a] > num_desc[task_b];\n        });\n\n        vector<bool> free_flag(n_free, true);\n        vector<pair<int, int>> assignments;\n        vector<bool> task_assigned_flag(n_tasks, false);\n        for (auto& p : task_priority) {\n            int idx_task = p.second;\n            int task_id = available_tasks_list[idx_task];\n            double best_time = 1e18;\n            int best_j_index = -1;\n            for (int j = 0; j < n_free; j++) {\n                if (free_flag[j] && time_est[idx_task][j] < best_time) {\n                    best_time = time_est[idx_task][j];\n                    best_j_index = j;\n                }\n            }\n            if (best_j_index != -1) {\n                int member_id = free_members[best_j_index];\n                assignments.push_back({member_id, task_id});\n                free_flag[best_j_index] = false;\n                task_assigned_flag[idx_task] = true;\n                assigned[task_id] = true;\n                available_set.erase(task_id);\n                member_status[member_id] = task_id;\n                task_start_day[task_id] = day;\n            }\n        }\n\n        cout << assignments.size();\n        for (auto& a : assignments) {\n            cout << \" \" << a.first << \" \" << a.second;\n        }\n        cout << endl;\n        for (int j = 1; j <= M; j++) {\n            cout << \"#s \" << j;\n            for (int k = 0; k < K; k++) {\n                cout << \" \" << (int)round(s_hat[j][k]);\n            }\n            cout << endl;\n        }\n        cout.flush();\n    }\n\n    return 0;\n}","ahc006":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <cmath>\n#include <cstdlib>\nusing namespace std;\n\nstruct Point {\n    int x, y;\n    Point(int x=0, int y=0) : x(x), y(y) {}\n};\n\nint manhattan(const Point& a, const Point& b) {\n    return abs(a.x - b.x) + abs(a.y - b.y);\n}\n\nstruct Order {\n    int a, b, c, d;\n};\n\nint main() {\n    vector<Order> orders(1000);\n    for (int i = 0; i < 1000; i++) {\n        cin >> orders[i].a >> orders[i].b >> orders[i].c >> orders[i].d;\n    }\n\n    vector<Point> tour;\n    tour.push_back(Point(400, 400));\n    tour.push_back(Point(400, 400));\n\n    vector<int> point_to_order;\n    vector<int> point_type;\n    point_to_order.push_back(-1);\n    point_to_order.push_back(-1);\n    point_type.push_back(0);\n    point_type.push_back(0);\n\n    vector<int> rest_index(1000, -1);\n    vector<int> dest_index(1000, -1);\n    vector<bool> used(1000, false);\n    vector<int> chosen_ids;\n\n    int n = 2;\n    for (int step = 0; step < 50; step++) {\n        long long best_cost = (long long)1e18;\n        int best_order = -1;\n        int best_edge = -1;\n\n        for (int idx = 0; idx < 1000; idx++) {\n            if (used[idx]) continue;\n            Order& o = orders[idx];\n            Point R = {o.a, o.b};\n            Point D = {o.c, o.d};\n            for (int edge = 0; edge < n-1; edge++) {\n                Point A = tour[edge];\n                Point B = tour[edge+1];\n                long long cost_insert = (long long)manhattan(A, R) + manhattan(R, D) + manhattan(D, B) - manhattan(A, B);\n                if (cost_insert < best_cost) {\n                    best_cost = cost_insert;\n                    best_order = idx;\n                    best_edge = edge;\n                }\n            }\n        }\n\n        if (best_order == -1) {\n            break;\n        }\n\n        Order& o = orders[best_order];\n        Point R = {o.a, o.b};\n        Point D = {o.c, o.d};\n        tour.insert(tour.begin() + best_edge + 1, R);\n        tour.insert(tour.begin() + best_edge + 2, D);\n\n        point_to_order.insert(point_to_order.begin() + best_edge + 1, best_order);\n        point_to_order.insert(point_to_order.begin() + best_edge + 2, best_order);\n        point_type.insert(point_type.begin() + best_edge + 1, 1);\n        point_type.insert(point_type.begin() + best_edge + 2, 2);\n\n        rest_index[best_order] = best_edge + 1;\n        dest_index[best_order] = best_edge + 2;\n        used[best_order] = true;\n        chosen_ids.push_back(best_order + 1);\n\n        n += 2;\n    }\n\n    n = tour.size();\n    bool improved = true;\n    while (improved) {\n        improved = false;\n        for (int i = 0; i <= n - 4; i++) {\n            int j = i + 1;\n            int k = i + 2;\n            int id1 = point_to_order[j];\n            int id2 = point_to_order[k];\n            if (id1 == id2 && id1 != -1) {\n                continue;\n            }\n\n            Point A = tour[i];\n            Point B = tour[j];\n            Point C = tour[k];\n            Point D = tour[i + 3];\n            long long original_cost = (long long)manhattan(A, B) + manhattan(B, C) + manhattan(C, D);\n            long long new_cost = (long long)manhattan(A, C) + manhattan(C, B) + manhattan(B, D);\n            long long delta = new_cost - original_cost;\n            if (delta >= 0) {\n                continue;\n            }\n\n            bool valid = true;\n            if (id1 != -1) {\n                int r1 = rest_index[id1];\n                int d1 = dest_index[id1];\n                if (j == r1) {\n                    r1 = k;\n                } else if (j == d1) {\n                    d1 = k;\n                }\n                if (r1 >= d1) {\n                    valid = false;\n                }\n            }\n            if (valid && id2 != -1) {\n                int r2 = rest_index[id2];\n                int d2 = dest_index[id2];\n                if (k == r2) {\n                    r2 = j;\n                } else if (k == d2) {\n                    d2 = j;\n                }\n                if (r2 >= d2) {\n                    valid = false;\n                }\n            }\n\n            if (valid) {\n                swap(tour[j], tour[k]);\n                swap(point_to_order[j], point_to_order[k]);\n                swap(point_type[j], point_type[k]);\n\n                if (id1 != -1) {\n                    if (rest_index[id1] == j) {\n                        rest_index[id1] = k;\n                    } else if (dest_index[id1] == j) {\n                        dest_index[id1] = k;\n                    }\n                }\n                if (id2 != -1) {\n                    if (rest_index[id2] == k) {\n                        rest_index[id2] = j;\n                    } else if (dest_index[id2] == k) {\n                        dest_index[id2] = j;\n                    }\n                }\n                improved = true;\n            }\n        }\n    }\n\n    cout << \"50\";\n    for (int i = 0; i < 50; i++) {\n        cout << \" \" << chosen_ids[i];\n    }\n    cout << endl;\n    cout << tour.size();\n    for (int i = 0; i < tour.size(); i++) {\n        cout << \" \" << tour[i].x << \" \" << tour[i].y;\n    }\n    cout << endl;\n\n    return 0;\n}","ahc007":"#include <bits/stdc++.h>\n#include <cmath>\n#include <vector>\n#include <tuple>\n#include <algorithm>\n#include <atcoder/dsu>\n\nusing namespace std;\nusing namespace atcoder;\n\nint main() {\n    const int N = 400;\n    const int M = 1995;\n    vector<double> x(N), y(N);\n    for (int i = 0; i < N; i++) {\n        cin >> x[i] >> y[i];\n    }\n\n    vector<pair<int, int>> edges_list(M);\n    for (int i = 0; i < M; i++) {\n        cin >> edges_list[i].first >> edges_list[i].second;\n    }\n\n    vector<double> d_list(M);\n    for (int i = 0; i < M; i++) {\n        int u = edges_list[i].first;\n        int v = edges_list[i].second;\n        double dx = x[u] - x[v];\n        double dy = y[u] - y[v];\n        d_list[i] = round(sqrt(dx*dx + dy*dy));\n    }\n\n    vector<tuple<double, int, int, int>> edges_for_mst;\n    for (int i = 0; i < M; i++) {\n        edges_for_mst.push_back(make_tuple(d_list[i], edges_list[i].first, edges_list[i].second, i));\n    }\n    sort(edges_for_mst.begin(), edges_for_mst.end());\n\n    dsu dsu_mst(N);\n    vector<bool> in_mst_low(M, false);\n    for (auto &e : edges_for_mst) {\n        double cost = get<0>(e);\n        int u = get<1>(e);\n        int v = get<2>(e);\n        int idx = get<3>(e);\n        if (dsu_mst.same(u, v)) continue;\n        dsu_mst.merge(u, v);\n        in_mst_low[idx] = true;\n    }\n\n    dsu dsu_online(N);\n    int comp_count = N;\n\n    for (int i = 0; i < M; i++) {\n        int l_i;\n        cin >> l_i;\n\n        int u = edges_list[i].first;\n        int v = edges_list[i].second;\n\n        if (dsu_online.same(u, v)) {\n            cout << 0 << endl;\n            cout.flush();\n        } else {\n            dsu temp = dsu_online;\n            int temp_comp = comp_count;\n            for (int j = i+1; j < M; j++) {\n                int uj = edges_list[j].first;\n                int vj = edges_list[j].second;\n                if (!temp.same(uj, vj)) {\n                    temp.merge(uj, vj);\n                    temp_comp--;\n                }\n            }\n            if (temp_comp != 1) {\n                cout << 1 << endl;\n                cout.flush();\n                dsu_online.merge(u, v);\n                comp_count--;\n            } else {\n                if (in_mst_low[i]) {\n                    if (l_i <= 2.5 * d_list[i]) {\n                        cout << 1 << endl;\n                        cout.flush();\n                        dsu_online.merge(u, v);\n                        comp_count--;\n                    } else {\n                        cout << 0 << endl;\n                        cout.flush();\n                    }\n                } else {\n                    if (l_i <= 2 * d_list[i]) {\n                        cout << 1 << endl;\n                        cout.flush();\n                        dsu_online.merge(u, v);\n                        comp_count--;\n                    } else {\n                        cout << 0 << endl;\n                        cout.flush();\n                    }\n                }\n            }\n        }\n    }\n\n    return 0;\n}","ahc008":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\n#include <set>\n#include <tuple>\n#include <climits>\nusing namespace std;\n\nconst int GRID_SIZE = 32;\n\nstruct Pet {\n    int x, y, type;\n};\n\nstruct Human {\n    int x, y;\n};\n\nstruct BarrierTask {\n    int wall_x, wall_y;\n    int hx, hy;\n    char action;\n};\n\nint manhattan(int x1, int y1, int x2, int y2) {\n    return abs(x1 - x2) + abs(y1 - y2);\n}\n\nint main() {\n    int N;\n    cin >> N;\n    vector<Pet> pets(N);\n    for (int i = 0; i < N; i++) {\n        cin >> pets[i].x >> pets[i].y >> pets[i].type;\n    }\n    int M;\n    cin >> M;\n    vector<Human> humans(M);\n    for (int i = 0; i < M; i++) {\n        cin >> humans[i].x >> humans[i].y;\n    }\n\n    bool wall[GRID_SIZE][GRID_SIZE] = {false};\n    for (int i = 0; i < GRID_SIZE; i++) {\n        for (int j = 0; j < GRID_SIZE; j++) {\n            if (i == 0 || i == GRID_SIZE-1 || j == 0 || j == GRID_SIZE-1) {\n                wall[i][j] = true;\n            }\n        }\n    }\n\n    vector<BarrierTask> all_tasks;\n    for (int y = 2; y <= 29; y++) {\n        BarrierTask task;\n        task.wall_x = 2; task.wall_y = y;\n        task.hx = 3; task.hy = y;\n        task.action = 'u';\n        all_tasks.push_back(task);\n    }\n    for (int y = 2; y <= 29; y++) {\n        BarrierTask task;\n        task.wall_x = 29; task.wall_y = y;\n        task.hx = 28; task.hy = y;\n        task.action = 'd';\n        all_tasks.push_back(task);\n    }\n    for (int x = 3; x <= 28; x++) {\n        BarrierTask task;\n        task.wall_x = x; task.wall_y = 2;\n        task.hx = x; task.hy = 3;\n        task.action = 'l';\n        all_tasks.push_back(task);\n    }\n    for (int x = 3; x <= 28; x++) {\n        BarrierTask task;\n        task.wall_x = x; task.wall_y = 29;\n        task.hx = x; task.hy = 28;\n        task.action = 'r';\n        all_tasks.push_back(task);\n    }\n\n    BarrierTask gap_task = {2, 15, 3, 15, 'u'};\n    vector<BarrierTask> all_tasks_without_gap;\n    for (auto& task : all_tasks) {\n        if (task.wall_x == gap_task.wall_x && task.wall_y == gap_task.wall_y) {\n            continue;\n        }\n        all_tasks_without_gap.push_back(task);\n    }\n\n    vector<queue<BarrierTask>> tasks(M);\n    vector<int> current_x(M), current_y(M);\n    for (int i=0; i<M; i++) {\n        current_x[i] = humans[i].x;\n        current_y[i] = humans[i].y;\n    }\n\n    vector<BarrierTask> unassigned = all_tasks_without_gap;\n    while (!unassigned.empty()) {\n        int best_i = -1, best_j = -1;\n        int min_dist = INT_MAX;\n        for (int i=0; i<M; i++) {\n            for (int j=0; j<unassigned.size(); j++) {\n                BarrierTask& task = unassigned[j];\n                int d = manhattan(current_x[i], current_y[i], task.hx, task.hy);\n                if (d < min_dist) {\n                    min_dist = d;\n                    best_i = i;\n                    best_j = j;\n                }\n            }\n        }\n        if (best_i == -1) break;\n        tasks[best_i].push(unassigned[best_j]);\n        current_x[best_i] = unassigned[best_j].hx;\n        current_y[best_i] = unassigned[best_j].hy;\n        unassigned.erase(unassigned.begin() + best_j);\n    }\n\n    bool gap_assigned = false;\n\n    for (int turn = 0; turn < 300; turn++) {\n        set<pair<int, int>> planned_walls;\n        vector<string> actions(M, \".\");\n\n        for (int i = 0; i < M; i++) {\n            if (tasks[i].empty()) {\n                continue;\n            }\n            BarrierTask current_task = tasks[i].front();\n            if (wall[current_task.wall_x][current_task.wall_y]) {\n                tasks[i].pop();\n                continue;\n            }\n            if (humans[i].x == current_task.hx && humans[i].y == current_task.hy) {\n                bool can_build = true;\n                for (const auto& pet : pets) {\n                    if (pet.x == current_task.wall_x && pet.y == current_task.wall_y) {\n                        can_build = false;\n                        break;\n                    }\n                }\n                if (can_build) {\n                    for (int j = 0; j < M; j++) {\n                        if (humans[j].x == current_task.wall_x && humans[j].y == current_task.wall_y) {\n                            can_build = false;\n                            break;\n                        }\n                    }\n                }\n                if (can_build) {\n                    int dx[4] = {0,0,1,-1};\n                    int dy[4] = {1,-1,0,0};\n                    for (int d=0; d<4; d++) {\n                        int nx = current_task.wall_x + dx[d];\n                        int ny = current_task.wall_y + dy[d];\n                        if (nx < 1 || nx > 30 || ny < 1 || ny > 30) continue;\n                        for (const auto& pet : pets) {\n                            if (pet.x == nx && pet.y == ny) {\n                                can_build = false;\n                                break;\n                            }\n                        }\n                        if (!can_build) break;\n                    }\n                }\n                if (current_task.wall_x == gap_task.wall_x && current_task.wall_y == gap_task.wall_y) {\n                    if (turn < 290) {\n                        can_build = false;\n                    }\n                }\n                if (can_build) {\n                    actions[i] = string(1, current_task.action);\n                    planned_walls.insert({current_task.wall_x, current_task.wall_y});\n                }\n            } else {\n                actions[i] = \"?\";\n            }\n        }\n\n        if (turn >= 290 && !gap_assigned && !wall[gap_task.wall_x][gap_task.wall_y]) {\n            int best_i = -1;\n            int best_dist = INT_MAX;\n            for (int i=0; i<M; i++) {\n                int d = manhattan(humans[i].x, humans[i].y, gap_task.hx, gap_task.hy);\n                if (d < best_dist) {\n                    best_dist = d;\n                    best_i = i;\n                }\n            }\n            if (best_i != -1) {\n                tasks[best_i].push(gap_task);\n                gap_assigned = true;\n            }\n        }\n\n        for (int i = 0; i < M; i++) {\n            if (actions[i] == \"?\") {\n                if (tasks[i].empty()) {\n                    actions[i] = '.';\n                    continue;\n                }\n                BarrierTask current_task = tasks[i].front();\n                if (wall[current_task.wall_x][current_task.wall_y]) {\n                    tasks[i].pop();\n                    actions[i] = '.';\n                    continue;\n                }\n                int sx = humans[i].x;\n                int sy = humans[i].y;\n                int tx = current_task.hx;\n                int ty = current_task.hy;\n\n                vector<vector<bool>> visited(GRID_SIZE, vector<bool>(GRID_SIZE, false));\n                vector<vector<char>> first_step_dir(GRID_SIZE, vector<char>(GRID_SIZE, '.'));\n                queue<pair<int, int>> q;\n                q.push({sx, sy});\n                visited[sx][sy] = true;\n                bool found = false;\n\n                while (!q.empty()) {\n                    auto [x, y] = q.front(); q.pop();\n                    if (x == tx && y == ty) {\n                        found = true;\n                        break;\n                    }\n                    vector<tuple<int, int, char>> dirs = {{-1,0,'U'}, {1,0,'D'}, {0,-1,'L'}, {0,1,'R'}};\n                    for (auto [dx, dy, dir] : dirs) {\n                        int nx = x + dx;\n                        int ny = y + dy;\n                        if (nx < 1 || nx > 30 || ny < 1 || ny > 30) continue;\n                        if (visited[nx][ny]) continue;\n                        if (wall[nx][ny] || planned_walls.count({nx,ny})) {\n                            continue;\n                        }\n                        visited[nx][ny] = true;\n                        if (x == sx && y == sy) {\n                            first_step_dir[nx][ny] = dir;\n                        } else {\n                            first_step_dir[nx][ny] = first_step_dir[x][y];\n                        }\n                        q.push({nx, ny});\n                    }\n                }\n\n                if (found) {\n                    actions[i] = string(1, first_step_dir[tx][ty]);\n                } else {\n                    actions[i] = '.';\n                }\n            }\n        }\n\n        string output_line;\n        for (int i=0; i<M; i++) {\n            output_line += actions[i];\n        }\n        cout << output_line << endl;\n        cout.flush();\n\n        for (int i=0; i<M; i++) {\n            if (actions[i] == \"u\" || actions[i] == \"d\" || actions[i] == \"l\" || actions[i] == \"r\") {\n                BarrierTask current_task = tasks[i].front();\n                wall[current_task.wall_x][current_task.wall_y] = true;\n                tasks[i].pop();\n            } else if (actions[i] == \"U\") {\n                humans[i].x--;\n            } else if (actions[i] == \"D\") {\n                humans[i].x++;\n            } else if (actions[i] == \"L\") {\n                humans[i].y--;\n            } else if (actions[i] == \"R\") {\n                humans[i].y++;\n            }\n        }\n\n        for (int i=0; i<N; i++) {\n            string move;\n            cin >> move;\n            for (char c : move) {\n                if (c == 'U') pets[i].x--;\n                else if (c == 'D') pets[i].x++;\n                else if (c == 'L') pets[i].y--;\n                else if (c == 'R') pets[i].y++;\n            }\n        }\n    }\n\n    return 0;\n}","ahc009":"#include <iostream>\n#include <vector>\n#include <cctype>\n#include <algorithm>\n#include <iomanip>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int s_i, s_j, t_i, t_j;\n    double p;\n    cin >> s_i >> s_j >> t_i >> t_j >> p;\n\n    vector<string> h(20);\n    for (int i = 0; i < 20; i++) {\n        cin >> h[i];\n    }\n\n    vector<string> v(19);\n    for (int i = 0; i < 19; i++) {\n        cin >> v[i];\n    }\n\n    vector<vector<vector<char>>> best_move(200, vector<vector<char>>(20, vector<char>(20, 'U')));\n    vector<vector<double>> dp_next(20, vector<double>(20, 0.0));\n\n    for (int i = 0; i < 20; i++) {\n        for (int j = 0; j < 20; j++) {\n            if (i == t_i && j == t_j) {\n                dp_next[i][j] = 201.0;\n            } else {\n                dp_next[i][j] = 0.0;\n            }\n        }\n    }\n\n    for (int tt = 199; tt >= 0; tt--) {\n        vector<vector<double>> dp_curr(20, vector<double>(20, 0.0));\n        for (int i = 0; i < 20; i++) {\n            for (int j = 0; j < 20; j++) {\n                if (i == t_i && j == t_j) {\n                    continue;\n                }\n                double best_val = -1e18;\n                char best_char = 'U';\n                for (char move : {'U','D','L','R'}) {\n                    int ni = i, nj = j;\n                    if (move == 'U') {\n                        if (i > 0 && v[i-1][j] == '0') {\n                            ni = i - 1;\n                        }\n                    } else if (move == 'D') {\n                        if (i < 19 && v[i][j] == '0') {\n                            ni = i + 1;\n                        }\n                    } else if (move == 'L') {\n                        if (j > 0 && h[i][j-1] == '0') {\n                            nj = j - 1;\n                        }\n                    } else if (move == 'R') {\n                        if (j < 19 && h[i][j] == '0') {\n                            nj = j + 1;\n                        }\n                    }\n\n                    double val;\n                    if (ni == t_i && nj == t_j) {\n                        val = p * dp_next[i][j] + (1-p) * (401 - (tt+1));\n                    } else {\n                        val = p * dp_next[i][j] + (1-p) * dp_next[ni][nj];\n                    }\n\n                    if (val > best_val) {\n                        best_val = val;\n                        best_char = move;\n                    }\n                }\n                dp_curr[i][j] = best_val;\n                best_move[tt][i][j] = best_char;\n            }\n        }\n        dp_next = dp_curr;\n    }\n\n    int cur_i = s_i, cur_j = s_j;\n    string route = \"\";\n    for (int t = 0; t < 200; t++) {\n        if (cur_i == t_i && cur_j == t_j) {\n            int rem = 200 - t;\n            route += string(rem, 'U');\n            break;\n        }\n        char move = best_move[t][cur_i][cur_j];\n        route += move;\n\n        if (move == 'U') {\n            if (cur_i > 0 && v[cur_i-1][cur_j] == '0') {\n                cur_i--;\n            }\n        } else if (move == 'D') {\n            if (cur_i < 19 && v[cur_i][cur_j] == '0') {\n                cur_i++;\n            }\n        } else if (move == 'L') {\n            if (cur_j > 0 && h[cur_i][cur_j-1] == '0') {\n                cur_j--;\n            }\n        } else if (move == 'R') {\n            if (cur_j < 19 && h[cur_i][cur_j] == '0') {\n                cur_j++;\n            }\n        }\n    }\n\n    cout << route << endl;\n\n    return 0;\n}","ahc010":"#include <iostream>\n#include <vector>\n#include <algorithm>\n\nusing namespace std;\n\nconst vector<int> di = {0, -1, 0, 1};\nconst vector<int> dj = {-1, 0, 1, 0};\n\nconst vector<vector<int>> to_table = {\n    {1, 0, -1, -1},\n    {3, -1, -1, 0},\n    {-1, -1, 3, 2},\n    {-1, 2, 1, -1},\n    {1, 0, 3, 2},\n    {3, 2, 1, 0},\n    {2, -1, 0, -1},\n    {-1, 3, -1, 1}\n};\n\nlong long compute_score(const vector<vector<int>>& grid, const vector<vector<int>>& rot) {\n    int n = 30;\n    vector<vector<int>> eff(n, vector<int>(n));\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            int t = grid[i][j];\n            int r = rot[i][j];\n            if (t < 4) {\n                eff[i][j] = (t + r) % 4;\n            } else if (t < 6) {\n                eff[i][j] = (r % 2 == 0) ? t : (t ^ 1);\n            } else {\n                eff[i][j] = (r % 2 == 0) ? t : (t ^ 1);\n            }\n        }\n    }\n\n    vector<vector<vector<bool>>> visited(n, vector<vector<bool>>(n, vector<bool>(4, false)));\n    vector<long long> loops;\n\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            for (int d = 0; d < 4; d++) {\n                if (visited[i][j][d]) continue;\n\n                int ci = i, cj = j, cd = d;\n                long long steps = 0;\n                bool found_loop = false;\n                vector<vector<vector<bool>>> current_visited(n, vector<vector<bool>>(n, vector<bool>(4, false)));\n\n                while (true) {\n                    if (current_visited[ci][cj][cd]) break;\n                    current_visited[ci][cj][cd] = true;\n                    visited[ci][cj][cd] = true;\n\n                    int eff_tile = eff[ci][cj];\n                    int d2 = to_table[eff_tile][cd];\n                    if (d2 == -1) break;\n\n                    int ni = ci + di[d2];\n                    int nj = cj + dj[d2];\n                    if (ni < 0 || ni >= n || nj < 0 || nj >= n) break;\n\n                    int nd = (d2 + 2) % 4;\n                    steps++;\n\n                    if (ni == i && nj == j && nd == d) {\n                        found_loop = true;\n                        break;\n                    }\n\n                    ci = ni;\n                    cj = nj;\n                    cd = nd;\n                }\n\n                if (found_loop) {\n                    loops.push_back(steps);\n                }\n            }\n        }\n    }\n\n    if (loops.size() < 2) {\n        return 0;\n    }\n\n    sort(loops.rbegin(), loops.rend());\n    return (long long)loops[0] * loops[1];\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int num_test_cases = 100;\n    for (int tc = 0; tc < num_test_cases; tc++) {\n        vector<vector<int>> grid(30, vector<int>(30));\n        for (int i = 0; i < 30; i++) {\n            string s;\n            cin >> s;\n            for (int j = 0; j < 30; j++) {\n                grid[i][j] = s[j] - '0';\n            }\n        }\n\n        vector<vector<int>> rot(30, vector<int>(30));\n        for (int i = 0; i < 30; i++) {\n            for (int j = 0; j < 30; j++) {\n                rot[i][j] = (i + j) % 4;\n            }\n        }\n\n        for (int i = 0; i < 30; i++) {\n            for (int j = 0; j < 30; j++) {\n                cout << rot[i][j];\n            }\n        }\n        cout << '\\n';\n    }\n\n    return 0;\n}","ahc011":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\n#include <tuple>\n#include <map>\n#include <climits>\nusing namespace std;\n\nconst int BEAM_WIDTH = 10;\n\nstruct State {\n    vector<vector<int>> board;\n    int empty_r, empty_c;\n    string moves;\n    char last_move;\n    int S, C, comp_count, dist;\n};\n\ntuple<int, int, int> evaluate(vector<vector<int>>& board, int N) {\n    int total_edges = 0;\n    map<pair<int, int>, vector<pair<int, int>>> adj;\n\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N - 1; j++) {\n            if (board[i][j] != 0 && board[i][j + 1] != 0) {\n                if ((board[i][j] & 4) && (board[i][j + 1] & 1)) {\n                    total_edges++;\n                    adj[{i, j}].push_back({i, j + 1});\n                    adj[{i, j + 1}].push_back({i, j});\n                }\n            }\n        }\n    }\n\n    for (int i = 0; i < N - 1; i++) {\n        for (int j = 0; j < N; j++) {\n            if (board[i][j] != 0 && board[i + 1][j] != 0) {\n                if ((board[i][j] & 8) && (board[i + 1][j] & 2)) {\n                    total_edges++;\n                    adj[{i, j}].push_back({i + 1, j});\n                    adj[{i + 1, j}].push_back({i, j});\n                }\n            }\n        }\n    }\n\n    vector<vector<bool>> visited(N, vector<bool>(N, false));\n    vector<vector<bool>> in_comp(N, vector<bool>(N, false));\n    int max_tree_size = 0;\n    int comp_count = 0;\n\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (board[i][j] == 0) {\n                visited[i][j] = true;\n            }\n        }\n    }\n\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (!visited[i][j]) {\n                comp_count++;\n                queue<pair<int, int>> q;\n                vector<pair<int, int>> component;\n                q.push({i, j});\n                visited[i][j] = true;\n                in_comp[i][j] = true;\n                component.push_back({i, j});\n\n                while (!q.empty()) {\n                    auto [r, c] = q.front();\n                    q.pop();\n                    if (adj.find({r, c}) != adj.end()) {\n                        for (auto &nei : adj[{r, c}]) {\n                            if (!visited[nei.first][nei.second]) {\n                                visited[nei.first][nei.second] = true;\n                                in_comp[nei.first][nei.second] = true;\n                                q.push(nei);\n                                component.push_back(nei);\n                            }\n                        }\n                    }\n                }\n\n                int edge_count = 0;\n                for (auto &node : component) {\n                    if (adj.find(node) != adj.end()) {\n                        for (auto &nei : adj[node]) {\n                            if (in_comp[nei.first][nei.second]) {\n                                edge_count++;\n                            }\n                        }\n                    }\n                }\n                edge_count /= 2;\n\n                int V = component.size();\n                if (edge_count == V - 1) {\n                    max_tree_size = max(max_tree_size, V);\n                }\n\n                for (auto &node : component) {\n                    in_comp[node.first][node.second] = false;\n                }\n            }\n        }\n    }\n\n    if (max_tree_size == 0) {\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                if (board[i][j] != 0) {\n                    max_tree_size = 1;\n                }\n            }\n        }\n    }\n\n    return {max_tree_size, total_edges, comp_count};\n}\n\nint main() {\n    int N, T_val;\n    cin >> N >> T_val;\n    vector<string> input_grid(N);\n    for (int i = 0; i < N; i++) {\n        cin >> input_grid[i];\n    }\n\n    vector<vector<int>> initial_board(N, vector<int>(N));\n    int empty_r = -1, empty_c = -1;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            char c = input_grid[i][j];\n            if (c >= '0' && c <= '9') {\n                initial_board[i][j] = c - '0';\n            } else if (c >= 'a' && c <= 'f') {\n                initial_board[i][j] = 10 + (c - 'a');\n            } else {\n                initial_board[i][j] = 0;\n            }\n            if (initial_board[i][j] == 0) {\n                empty_r = i;\n                empty_c = j;\n            }\n        }\n    }\n\n    auto [S0, C0, comp0] = evaluate(initial_board, N);\n    int dist0 = abs(empty_r - (N - 1)) + abs(empty_c - (N - 1));\n    State initial_state = {initial_board, empty_r, empty_c, \"\", ' ', S0, C0, comp0, dist0};\n\n    vector<State> beam;\n    beam.push_back(initial_state);\n\n    string final_sequence = \"\";\n    int min_full_moves = INT_MAX;\n    bool found_full_tree = false;\n\n    map<char, char> reverse_move = {{'U', 'D'}, {'D', 'U'}, {'L', 'R'}, {'R', 'L'}};\n    map<char, pair<int, int>> dir = {{'U', {-1, 0}}, {'D', {1, 0}}, {'L', {0, -1}}, {'R', {0, 1}}};\n\n    for (int step = 0; step < T_val; step++) {\n        vector<State> candidates;\n\n        for (const State& state : beam) {\n            for (const char move : {'U', 'D', 'L', 'R'}) {\n                if (state.last_move != ' ' && move == reverse_move[state.last_move]) {\n                    continue;\n                }\n\n                int dr = dir[move].first;\n                int dc = dir[move].second;\n                int new_r = state.empty_r + dr;\n                int new_c = state.empty_c + dc;\n\n                if (new_r < 0 || new_r >= N || new_c < 0 || new_c >= N) {\n                    continue;\n                }\n\n                State new_state = state;\n                swap(new_state.board[state.empty_r][state.empty_c], new_state.board[new_r][new_c]);\n                new_state.empty_r = new_r;\n                new_state.empty_c = new_c;\n                new_state.moves = state.moves + move;\n                new_state.last_move = move;\n\n                tie(new_state.S, new_state.C, new_state.comp_count) = evaluate(new_state.board, N);\n                new_state.dist = abs(new_r - (N - 1)) + abs(new_c - (N - 1));\n\n                candidates.push_back(new_state);\n\n                if (new_state.S == N * N - 1 && new_state.moves.length() < min_full_moves) {\n                    final_sequence = new_state.moves;\n                    found_full_tree = true;\n                    min_full_moves = final_sequence.length();\n                }\n            }\n        }\n\n        if (candidates.empty()) {\n            break;\n        }\n\n        sort(candidates.begin(), candidates.end(), [](const State& a, const State& b) {\n            if (a.S != b.S) return a.S > b.S;\n            if (a.C != b.C) return a.C > b.C;\n            if (a.comp_count != b.comp_count) return a.comp_count < b.comp_count;\n            return a.dist < b.dist;\n        });\n\n        beam.clear();\n        int width = min(BEAM_WIDTH, (int)candidates.size());\n        for (int i = 0; i < width; i++) {\n            beam.push_back(candidates[i]);\n        }\n\n        if (found_full_tree) {\n            break;\n        }\n    }\n\n    if (!found_full_tree && !beam.empty()) {\n        sort(beam.begin(), beam.end(), [](const State& a, const State& b) {\n            if (a.S != b.S) return a.S > b.S;\n            if (a.C != b.C) return a.C > b.C;\n            if (a.comp_count != b.comp_count) return a.comp_count < b.comp_count;\n            return a.dist < b.dist;\n        });\n        final_sequence = beam[0].moves;\n    }\n\n    cout << final_sequence << endl;\n\n    return 0;\n}","ahc012":"#include <bits/stdc++.h>\n#include <random>\nusing namespace std;\ntypedef long long ll;\ntypedef __int128 i128;\n\nconst int B = 20000;\nconst ll R2 = 100000000;\n\nstruct Point {\n    ll x, y;\n    Point() {}\n    Point(ll x, ll y) : x(x), y(y) {}\n    bool operator==(const Point& other) const {\n        return x == other.x && y == other.y;\n    }\n};\n\nstruct Line {\n    Point p, q;\n    ll a, b, c;\n    Line(Point p1, Point p2) : p(p1), q(p2) {\n        a = p.y - q.y;\n        b = q.x - p.x;\n        c = p.x * q.y - q.x * p.y;\n    }\n};\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    random_device rd;\n    mt19937 gen(rd());\n    uniform_int_distribution<int> distrib(-B, B);\n\n    const int CANDIDATE_SIZE = 1000;\n    const int num_test_cases = 100;\n    for (int tc = 0; tc < num_test_cases; tc++) {\n        int N, K;\n        cin >> N >> K;\n        vector<int> a(11, 0);\n        for (int i = 1; i <= 10; i++) {\n            cin >> a[i];\n        }\n        vector<Point> points;\n        for (int i = 0; i < N; i++) {\n            ll x, y;\n            cin >> x >> y;\n            points.push_back(Point(x, y));\n        }\n\n        vector<Line> candidate_lines;\n        vector<bool> used;\n        for (int i = 0; i < CANDIDATE_SIZE; ) {\n            bool found = false;\n            for (int t = 0; t < 100; t++) {\n                Point p(distrib(gen), distrib(gen));\n                Point q(distrib(gen), distrib(gen));\n                if (p == q) continue;\n                Line L(p, q);\n                i128 a_val = L.a;\n                i128 b_val = L.b;\n                i128 c_val = L.c;\n                i128 lhs = c_val * c_val;\n                i128 rhs = R2 * (a_val * a_val + b_val * b_val);\n                if (lhs > rhs) continue;\n\n                bool valid = true;\n                for (const Point& s : points) {\n                    ll D = L.a * s.x + L.b * s.y + L.c;\n                    if (D == 0) {\n                        valid = false;\n                        break;\n                    }\n                }\n                if (valid) {\n                    candidate_lines.push_back(L);\n                    found = true;\n                    i++;\n                    break;\n                }\n            }\n            if (!found) {\n                break;\n            }\n        }\n        used.assign(candidate_lines.size(), false);\n\n        vector<vector<int>> current_groups;\n        if (N > 0) {\n            current_groups.push_back(vector<int>());\n            for (int i = 0; i < N; i++) {\n                current_groups[0].push_back(i);\n            }\n        }\n\n        vector<int> current_b_d(11, 0);\n        if (N >= 1 && N <= 10) {\n            current_b_d[N] = 1;\n        }\n        int current_score = 0;\n        for (int d = 1; d <= 10; d++) {\n            current_score += min(a[d], current_b_d[d]);\n        }\n\n        vector<Line> chosen_lines;\n\n        for (int step = 0; step < K; step++) {\n            int best_next_score = current_score;\n            int best_candidate = -1;\n\n            if (candidate_lines.empty()) break;\n\n            for (int idx = 0; idx < candidate_lines.size(); idx++) {\n                if (used[idx]) continue;\n                const Line& L = candidate_lines[idx];\n                vector<int> new_b_d(11, 0);\n                for (const vector<int>& group : current_groups) {\n                    int cnt0 = 0, cnt1 = 0;\n                    for (int sid : group) {\n                        const Point& s = points[sid];\n                        ll D = L.a * s.x + L.b * s.y + L.c;\n                        if (D > 0) cnt1++;\n                        else cnt0++;\n                    }\n                    if (cnt0 > 0 && cnt0 <= 10) new_b_d[cnt0]++;\n                    if (cnt1 > 0 && cnt1 <= 10) new_b_d[cnt1]++;\n                }\n                int score = 0;\n                for (int d = 1; d <= 10; d++) {\n                    score += min(a[d], new_b_d[d]);\n                }\n                if (score > best_next_score) {\n                    best_next_score = score;\n                    best_candidate = idx;\n                }\n            }\n\n            if (best_candidate == -1) break;\n\n            used[best_candidate] = true;\n            const Line& L = candidate_lines[best_candidate];\n            chosen_lines.push_back(L);\n\n            vector<vector<int>> new_groups;\n            for (const vector<int>& group : current_groups) {\n                vector<int> group0, group1;\n                for (int sid : group) {\n                    const Point& s = points[sid];\n                    ll D = L.a * s.x + L.b * s.y + L.c;\n                    if (D > 0) group1.push_back(sid);\n                    else group0.push_back(sid);\n                }\n                if (!group0.empty()) new_groups.push_back(group0);\n                if (!group1.empty()) new_groups.push_back(group1);\n            }\n            current_groups = move(new_groups);\n\n            current_b_d.assign(11, 0);\n            for (const auto& g : current_groups) {\n                int s = g.size();\n                if (s >= 1 && s <= 10) current_b_d[s]++;\n            }\n            current_score = best_next_score;\n        }\n\n        cout << chosen_lines.size() << endl;\n        for (const Line& L : chosen_lines) {\n            cout << L.p.x << \" \" << L.p.y << \" \" << L.q.x << \" \" << L.q.y << endl;\n        }\n    }\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    cin >> N >> M;\n    vector<vector<bool>> dot(N, vector<bool>(N, false));\n    vector<set<int>> rows(N);\n    vector<set<int>> cols(N);\n\n    vector<vector<int>> next_in_row(N, vector<int>(N, N));\n    vector<vector<int>> prev_in_row(N, vector<int>(N, -1));\n    vector<vector<int>> next_in_col(N, vector<int>(N, N));\n    vector<vector<int>> prev_in_col(N, vector<int>(N, -1));\n\n    vector<vector<bool>> hor(N, vector<bool>(N, false));\n    vector<vector<bool>> ver(N, vector<bool>(N, false));\n\n    for (int i = 0; i < M; i++) {\n        int x, y;\n        cin >> x >> y;\n        dot[x][y] = true;\n        rows[y].insert(x);\n        cols[x].insert(y);\n    }\n\n    for (int y = 0; y < N; y++) {\n        int last = -1;\n        for (int x = 0; x < N; x++) {\n            if (dot[x][y]) {\n                last = x;\n            }\n            prev_in_row[y][x] = last;\n        }\n        last = N;\n        for (int x = N-1; x >= 0; x--) {\n            if (dot[x][y]) {\n                last = x;\n            }\n            next_in_row[y][x] = last;\n        }\n    }\n\n    for (int x = 0; x < N; x++) {\n        int last = -1;\n        for (int y = 0; y < N; y++) {\n            if (dot[x][y]) {\n                last = y;\n            }\n            prev_in_col[x][y] = last;\n        }\n        last = N;\n        for (int y = N-1; y >= 0; y--) {\n            if (dot[x][y]) {\n                last = y;\n            }\n            next_in_col[x][y] = last;\n        }\n    }\n\n    int c = (N - 1) / 2;\n    priority_queue<tuple<int, int, int>> pq;\n    for (int x = 0; x < N; x++) {\n        for (int y = 0; y < N; y++) {\n            if (!dot[x][y]) {\n                int weight = (x - c) * (x - c) + (y - c) * (y - c) + 1;\n                pq.push(make_tuple(weight, x, y));\n            }\n        }\n    }\n\n    vector<array<int, 8>> operations;\n    auto start_time = chrono::high_resolution_clock::now();\n\n    while (!pq.empty()) {\n        auto now = chrono::high_resolution_clock::now();\n        auto duration = chrono::duration_cast<chrono::milliseconds>(now - start_time);\n        if (duration.count() > 4500) {\n            break;\n        }\n\n        auto top = pq.top();\n        int w = get<0>(top);\n        int x1 = get<1>(top);\n        int y1 = get<2>(top);\n        pq.pop();\n        if (dot[x1][y1]) {\n            continue;\n        }\n\n        bool found = false;\n        const auto& col_x1 = cols[x1];\n        for (int y2 : col_x1) {\n            if (y2 == y1) continue;\n            int y_min = min(y1, y2);\n            int y_max = max(y1, y2);\n\n            if (next_in_col[x1][y_min + 1] < y_max) {\n                continue;\n            }\n\n            set<int> common;\n            const set<int>& row_y1 = rows[y1];\n            const set<int>& row_y2 = rows[y2];\n            if (row_y1.size() < row_y2.size()) {\n                for (int x : row_y1) {\n                    if (row_y2.find(x) != row_y2.end()) {\n                        common.insert(x);\n                    }\n                }\n            } else {\n                for (int x : row_y2) {\n                    if (row_y1.find(x) != row_y1.end()) {\n                        common.insert(x);\n                    }\n                }\n            }\n\n            for (int x2 : common) {\n                if (x2 == x1) continue;\n                int x_min = min(x1, x2);\n                int x_max = max(x1, x2);\n\n                if (next_in_row[y1][x_min + 1] < x_max) {\n                    continue;\n                }\n                if (next_in_row[y2][x_min + 1] < x_max) {\n                    continue;\n                }\n                if (next_in_col[x2][y_min + 1] < y_max) {\n                    continue;\n                }\n\n                bool edgeValid = true;\n                for (int y = y_min; y < y_max; y++) {\n                    if (y < N - 1 && ver[x1][y]) {\n                        edgeValid = false;\n                        break;\n                    }\n                }\n                if (!edgeValid) continue;\n\n                for (int y = y_min; y < y_max; y++) {\n                    if (y < N - 1 && ver[x2][y]) {\n                        edgeValid = false;\n                        break;\n                    }\n                }\n                if (!edgeValid) continue;\n\n                for (int x = x_min; x < x_max; x++) {\n                    if (x < N - 1 && hor[x][y_min]) {\n                        edgeValid = false;\n                        break;\n                    }\n                }\n                if (!edgeValid) continue;\n\n                for (int x = x_min; x < x_max; x++) {\n                    if (x < N - 1 && hor[x][y_max]) {\n                        edgeValid = false;\n                        break;\n                    }\n                }\n                if (!edgeValid) continue;\n\n                operations.push_back({x1, y1, x1, y2, x2, y2, x2, y1});\n                dot[x1][y1] = true;\n                rows[y1].insert(x1);\n                cols[x1].insert(y1);\n\n                int last_val = -1;\n                for (int x = 0; x < N; x++) {\n                    if (dot[x][y1]) {\n                        last_val = x;\n                    }\n                    prev_in_row[y1][x] = last_val;\n                }\n                last_val = N;\n                for (int x = N - 1; x >= 0; x--) {\n                    if (dot[x][y1]) {\n                        last_val = x;\n                    }\n                    next_in_row[y1][x] = last_val;\n                }\n\n                last_val = -1;\n                for (int y = 0; y < N; y++) {\n                    if (dot[x1][y]) {\n                        last_val = y;\n                    }\n                    prev_in_col[x1][y] = last_val;\n                }\n                last_val = N;\n                for (int y = N - 1; y >= 0; y--) {\n                    if (dot[x1][y]) {\n                        last_val = y;\n                    }\n                    next_in_col[x1][y] = last_val;\n                }\n\n                for (int y = y_min; y < y_max; y++) {\n                    if (y < N - 1) {\n                        ver[x1][y] = true;\n                    }\n                }\n                for (int y = y_min; y < y_max; y++) {\n                    if (y < N - 1) {\n                        ver[x2][y] = true;\n                    }\n                }\n                for (int x = x_min; x < x_max; x++) {\n                    if (x < N - 1) {\n                        hor[x][y_min] = true;\n                    }\n                }\n                for (int x = x_min; x < x_max; x++) {\n                    if (x < N - 1) {\n                        hor[x][y_max] = true;\n                    }\n                }\n\n                found = true;\n                break;\n            }\n            if (found) break;\n        }\n    }\n\n    cout << operations.size() << endl;\n    for (auto& op : operations) {\n        for (int i = 0; i < 8; i++) {\n            if (i > 0) cout << \" \";\n            cout << op[i];\n        }\n        cout << endl;\n    }\n\n    return 0;\n}","ahc015":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <climits>\n#include <algorithm>\nusing namespace std;\n\nvector<vector<int>> simulate_tilt(vector<vector<int>> grid, char dir) {\n    vector<vector<int>> new_grid(10, vector<int>(10, 0));\n    if (dir == 'L' || dir == 'R') {\n        for (int i = 0; i < 10; i++) {\n            vector<int> non_empty;\n            for (int j = 0; j < 10; j++) {\n                if (grid[i][j] != 0) {\n                    non_empty.push_back(grid[i][j]);\n                }\n            }\n            if (dir == 'L') {\n                for (int j = 0; j < non_empty.size(); j++) {\n                    new_grid[i][j] = non_empty[j];\n                }\n            } else {\n                int start = 10 - non_empty.size();\n                for (int j = 0; j < non_empty.size(); j++) {\n                    new_grid[i][start + j] = non_empty[j];\n                }\n            }\n        }\n    } else {\n        for (int j = 0; j < 10; j++) {\n            vector<int> non_empty;\n            for (int i = 0; i < 10; i++) {\n                if (grid[i][j] != 0) {\n                    non_empty.push_back(grid[i][j]);\n                }\n            }\n            if (dir == 'F') {\n                for (int i = 0; i < non_empty.size(); i++) {\n                    new_grid[i][j] = non_empty[i];\n                }\n            } else {\n                int start = 10 - non_empty.size();\n                for (int i = 0; i < non_empty.size(); i++) {\n                    new_grid[start + i][j] = non_empty[i];\n                }\n            }\n        }\n    }\n    return new_grid;\n}\n\nint compute_manhattan(vector<vector<int>>& grid, const vector<int>& penalty_type) {\n    int penalty = 0;\n    for (int i = 0; i < 10; i++) {\n        for (int j = 0; j < 10; j++) {\n            if (grid[i][j] != 0) {\n                int f = grid[i][j];\n                int t = penalty_type[f];\n                if (t == 0) {\n                    penalty += i + j;\n                } else if (t == 1) {\n                    penalty += i + (9 - j);\n                } else {\n                    penalty += (9 - i) + j;\n                }\n            }\n        }\n    }\n    return penalty;\n}\n\nint compute_score(vector<vector<int>>& grid) {\n    int n = 10;\n    vector<vector<bool>> visited(n, vector<bool>(n, false));\n    int total_score = 0;\n    vector<int> dr = {1, -1, 0, 0};\n    vector<int> dc = {0, 0, 1, -1};\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            if (!visited[i][j] && grid[i][j] != 0) {\n                int flavor = grid[i][j];\n                int count = 0;\n                queue<pair<int, int>> q;\n                q.push({i, j});\n                visited[i][j] = true;\n                while (!q.empty()) {\n                    auto cell = q.front();\n                    q.pop();\n                    count++;\n                    int r = cell.first;\n                    int c = cell.second;\n                    for (int d = 0; d < 4; d++) {\n                        int nr = r + dr[d];\n                        int nc = c + dc[d];\n                        if (nr >= 0 && nr < n && nc >= 0 && nc < n && !visited[nr][nc] && grid[nr][nc] == flavor) {\n                            visited[nr][nc] = true;\n                            q.push({nr, nc});\n                        }\n                    }\n                }\n                total_score += count * count;\n            }\n        }\n    }\n    return total_score;\n}\n\nint compute_pairs(vector<vector<int>>& grid) {\n    int pairs = 0;\n    for (int i = 0; i < 10; i++) {\n        for (int j = 0; j < 10; j++) {\n            if (grid[i][j] != 0) {\n                if (j < 9 && grid[i][j] == grid[i][j+1]) {\n                    pairs++;\n                }\n                if (i < 9 && grid[i][j] == grid[i+1][j]) {\n                    pairs++;\n                }\n            }\n        }\n    }\n    return pairs;\n}\n\nint main() {\n    vector<int> flavors;\n    for (int i = 0; i < 100; i++) {\n        int f;\n        cin >> f;\n        flavors.push_back(f);\n    }\n\n    vector<int> flavor_counts(4, 0);\n    for (int f : flavors) {\n        if (f >= 1 && f <= 3) {\n            flavor_counts[f]++;\n        }\n    }\n    vector<int> order = {1, 2, 3};\n    sort(order.begin(), order.end(), [&](int a, int b) {\n        return flavor_counts[a] > flavor_counts[b];\n    });\n    vector<int> penalty_type(4);\n    penalty_type[order[0]] = 0;\n    penalty_type[order[1]] = 1;\n    penalty_type[order[2]] = 2;\n\n    vector<vector<int>> grid(10, vector<int>(10, 0));\n    vector<char> dirs = {'L', 'R', 'F', 'B'};\n\n    for (int t = 0; t < 100; t++) {\n        int p;\n        cin >> p;\n\n        vector<pair<int, int>> emptyCells;\n        for (int i = 0; i < 10; i++) {\n            for (int j = 0; j < 10; j++) {\n                if (grid[i][j] == 0) {\n                    emptyCells.push_back(make_pair(i, j));\n                }\n            }\n        }\n        sort(emptyCells.begin(), emptyCells.end(), [](const pair<int, int>& a, const pair<int, int>& b) {\n            if (a.first != b.first) return a.first < b.first;\n            return a.second < b.second;\n        });\n        int r = emptyCells[p-1].first;\n        int c = emptyCells[p-1].second;\n        grid[r][c] = flavors[t];\n\n        int best_score = -1;\n        int best_pairs = -1;\n        int best_penalty = INT_MAX;\n        char best_dir = 'L';\n\n        for (char dir : dirs) {\n            vector<vector<int>> new_grid = simulate_tilt(grid, dir);\n            int score = compute_score(new_grid);\n            int pairs = compute_pairs(new_grid);\n            int penalty = compute_manhattan(new_grid, penalty_type);\n            \n            if (score > best_score) {\n                best_score = score;\n                best_pairs = pairs;\n                best_penalty = penalty;\n                best_dir = dir;\n            } else if (score == best_score) {\n                if (pairs > best_pairs) {\n                    best_pairs = pairs;\n                    best_penalty = penalty;\n                    best_dir = dir;\n                } else if (pairs == best_pairs) {\n                    if (penalty < best_penalty) {\n                        best_penalty = penalty;\n                        best_dir = dir;\n                    }\n                }\n            }\n        }\n\n        cout << best_dir << endl;\n        cout.flush();\n\n        grid = simulate_tilt(grid, best_dir);\n    }\n\n    return 0;\n}","ahc016":"#include <iostream>\n#include <vector>\n#include <cstdlib>\n#include <algorithm>\n#include <cmath>\nusing namespace std;\n\nint main() {\n    int M;\n    double eps;\n    cin >> M >> eps;\n\n    srand(42);\n    int n = 80;\n    int L = n * (n - 1) / 2;\n\n    cout << n << endl;\n\n    vector<vector<int>> stored_degrees(M, vector<int>(n));\n    vector<vector<double>> expected_degrees(M, vector<double>(n));\n\n    for (int i = 0; i < M; i++) {\n        double p_i = 0.1 + 0.8 * (static_cast<double>(i) / (M - 1));\n        string s = \"\";\n        for (int j = 0; j < L; j++) {\n            double r = static_cast<double>(rand()) / RAND_MAX;\n            s += (r < p_i) ? '1' : '0';\n        }\n        cout << s << endl;\n\n        vector<int> deg(n, 0);\n        int idx = 0;\n        for (int u = 0; u < n; u++) {\n            for (int v = u + 1; v < n; v++) {\n                if (s[idx] == '1') {\n                    deg[u]++;\n                    deg[v]++;\n                }\n                idx++;\n            }\n        }\n        sort(deg.begin(), deg.end());\n        stored_degrees[i] = deg;\n        \n        for (int j = 0; j < n; j++) {\n            expected_degrees[i][j] = deg[j] * (1 - 2*eps) + eps*(n-1);\n        }\n    }\n    cout.flush();\n\n    for (int q = 0; q < 100; q++) {\n        string H;\n        cin >> H;\n\n        vector<int> degH(n, 0);\n        int idx = 0;\n        for (int u = 0; u < n; u++) {\n            for (int v = u + 1; v < n; v++) {\n                if (H[idx] == '1') {\n                    degH[u]++;\n                    degH[v]++;\n                }\n                idx++;\n            }\n        }\n        sort(degH.begin(), degH.end());\n\n        double minDist = 1e18;\n        int best_i = 0;\n        for (int i = 0; i < M; i++) {\n            double dist = 0.0;\n            for (int j = 0; j < n; j++) {\n                double diff = expected_degrees[i][j] - degH[j];\n                dist += diff * diff;\n            }\n            if (dist < minDist) {\n                minDist = dist;\n                best_i = i;\n            }\n        }\n\n        cout << best_i << endl;\n        cout.flush();\n    }\n\n    return 0;\n}","ahc017":"#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, D, K;\n    cin >> N >> M >> D >> K;\n    vector<tuple<int, int, int>> edges;\n    vector<vector<tuple<int, int, int>>> adj(N);\n    map<pair<int, int>, int> edgeMap;\n\n    for (int i = 0; i < M; i++) {\n        int u, v, w;\n        cin >> u >> v >> w;\n        u--; v--;\n        edges.push_back({u, v, w});\n        adj[u].push_back({v, w, i});\n        adj[v].push_back({u, w, i});\n        int a = min(u, v), b = max(u, v);\n        edgeMap[{a, b}] = i;\n    }\n    for (int i = 0; i < N; i++) {\n        int x, y;\n        cin >> x >> y;\n    }\n\n    vector<double> bc(M, 0.0);\n\n    for (int s = 0; s < N; s++) {\n        vector<ll> dist(N, 1e18);\n        vector<int> numPaths(N, 0);\n        vector<vector<int>> pred(N);\n        vector<int> order;\n\n        dist[s] = 0;\n        numPaths[s] = 1;\n        priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;\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            order.push_back(u);\n            for (auto [v, w, eid] : adj[u]) {\n                ll nd = d + w;\n                if (nd < dist[v]) {\n                    dist[v] = nd;\n                    numPaths[v] = numPaths[u];\n                    pred[v] = {u};\n                    pq.push({nd, v});\n                } else if (nd == dist[v]) {\n                    numPaths[v] += numPaths[u];\n                    pred[v].push_back(u);\n                }\n            }\n        }\n\n        vector<double> dep(N, 0.0);\n        for (int i = order.size() - 1; i >= 0; i--) {\n            int w = order[i];\n            for (int u : pred[w]) {\n                if (numPaths[w] == 0) continue;\n                double frac = static_cast<double>(numPaths[u]) / numPaths[w] * (1.0 + dep[w]);\n                int a = min(u, w);\n                int b = max(u, w);\n                auto it = edgeMap.find({a, b});\n                if (it == edgeMap.end()) continue;\n                int eid = it->second;\n                bc[eid] += frac;\n                dep[u] += frac;\n            }\n        }\n    }\n\n    vector<int> assignment(M, -1);\n    vector<int> day_count(D, 0);\n    vector<double> day_sum(D, 0.0);\n\n    vector<int> idx_edges(M);\n    iota(idx_edges.begin(), idx_edges.end(), 0);\n    sort(idx_edges.begin(), idx_edges.end(), [&](int i, int j) { return bc[i] > bc[j]; });\n\n    for (int i : idx_edges) {\n        int best_d = -1;\n        double min_sum = 1e18;\n        for (int d = 0; d < D; d++) {\n            if (day_count[d] < K) {\n                if (day_sum[d] < min_sum) {\n                    min_sum = day_sum[d];\n                    best_d = d;\n                }\n            }\n        }\n        if (best_d == -1) {\n            for (int d = 0; d < D; d++) {\n                if (day_count[d] < K) {\n                    best_d = d;\n                    break;\n                }\n            }\n            if (best_d == -1) {\n                best_d = 0;\n            }\n        }\n        assignment[i] = best_d;\n        day_count[best_d]++;\n        day_sum[best_d] += bc[i];\n    }\n\n    vector<vector<int>> adj_uw(N);\n    for (int i = 0; i < M; i++) {\n        int u = get<0>(edges[i]);\n        int v = get<1>(edges[i]);\n        adj_uw[u].push_back(v);\n        adj_uw[v].push_back(u);\n    }\n\n    random_device rd;\n    mt19937 gen(rd());\n\n    for (int d = 0; d < D; d++) {\n        vector<vector<int>> adj_d(N);\n        for (int u = 0; u < N; u++) {\n            for (int v : adj_uw[u]) {\n                int a = min(u, v);\n                int b = max(u, v);\n                int eid = edgeMap[{a, b}];\n                if (assignment[eid] == d) continue;\n                adj_d[u].push_back(v);\n            }\n        }\n\n        vector<bool> vis(N, false);\n        queue<int> q;\n        q.push(0);\n        vis[0] = true;\n        int cnt = 0;\n        while (!q.empty()) {\n            int u = q.front(); q.pop();\n            cnt++;\n            for (int v : adj_d[u]) {\n                if (!vis[v]) {\n                    vis[v] = true;\n                    q.push(v);\n                }\n            }\n        }\n        if (cnt == N) continue;\n\n        vector<int> edges_d;\n        for (int i = 0; i < M; i++) {\n            if (assignment[i] == d) {\n                edges_d.push_back(i);\n            }\n        }\n\n        bool fixed = false;\n        if (edges_d.empty()) continue;\n\n        for (int attempt = 0; attempt < 100; attempt++) {\n            uniform_int_distribution<int> dist_e(0, edges_d.size()-1);\n            int idx_e = dist_e(gen);\n            int e = edges_d[idx_e];\n\n            int d2 = gen() % D;\n            if (d2 == d) continue;\n\n            vector<int> edges_d2;\n            for (int i = 0; i < M; i++) {\n                if (assignment[i] == d2) {\n                    edges_d2.push_back(i);\n                }\n            }\n            if (edges_d2.empty()) continue;\n\n            uniform_int_distribution<int> dist_f(0, edges_d2.size()-1);\n            int idx_f = dist_f(gen);\n            int f = edges_d2[idx_f];\n\n            assignment[e] = d2;\n            assignment[f] = d;\n\n            vector<vector<int>> adj_d1(N);\n            for (int u = 0; u < N; u++) {\n                for (int v : adj_uw[u]) {\n                    int a = min(u, v);\n                    int b = max(u, v);\n                    int eid_t = edgeMap[{a, b}];\n                    if (assignment[eid_t] == d) continue;\n                    adj_d1[u].push_back(v);\n                }\n            }\n            vector<bool> vis1(N, false);\n            queue<int> q1;\n            q1.push(0);\n            vis1[0] = true;\n            int cnt1 = 0;\n            while (!q1.empty()) {\n                int u = q1.front(); q1.pop();\n                cnt1++;\n                for (int v : adj_d1[u]) {\n                    if (!vis1[v]) {\n                        vis1[v] = true;\n                        q1.push(v);\n                    }\n                }\n            }\n            if (cnt1 != N) {\n                assignment[e] = d;\n                assignment[f] = d2;\n                continue;\n            }\n\n            vector<vector<int>> adj_d2(N);\n            for (int u = 0; u < N; u++) {\n                for (int v : adj_uw[u]) {\n                    int a = min(u, v);\n                    int b = max(u, v);\n                    int eid_t = edgeMap[{a, b}];\n                    if (assignment[eid_t] == d2) continue;\n                    adj_d2[u].push_back(v);\n                }\n            }\n            vector<bool> vis2(N, false);\n            queue<int> q2;\n            q2.push(0);\n            vis2[0] = true;\n            int cnt2 = 0;\n            while (!q2.empty()) {\n                int u = q2.front(); q2.pop();\n                cnt2++;\n                for (int v : adj_d2[u]) {\n                    if (!vis2[v]) {\n                        vis2[v] = true;\n                        q2.push(v);\n                    }\n                }\n            }\n            if (cnt2 != N) {\n                assignment[e] = d;\n                assignment[f] = d2;\n                continue;\n            }\n\n            fixed = true;\n            break;\n        }\n    }\n\n    for (int i = 0; i < M; i++) {\n        cout << assignment[i] + 1;\n        if (i < M-1) cout << \" \";\n    }\n    cout << endl;\n\n    return 0;\n}","ahc019":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <tuple>\n#include <algorithm>\n#include <set>\n#include <map>\n#include <deque>\nusing namespace std;\n\nconst int MAX_VOL = 8;\n\nstruct Rotation {\n    int perm[3];\n    int sign[3];\n};\n\nvector<Rotation> generate_rotations() {\n    vector<int> perm = {0,1,2};\n    vector<Rotation> rots;\n    do {\n        int inv = 0;\n        for (int i=0; i<3; i++)\n            for (int j=i+1; j<3; j++)\n                if (perm[i] > perm[j]) inv++;\n        int perm_sign = (inv%2==0) ? 1 : -1;\n\n        for (int s=0; s<8; s++) {\n            int sx = (s&1) ? -1 : 1;\n            int sy = (s&2) ? -1 : 1;\n            int sz = (s&4) ? -1 : 1;\n            if (perm_sign * sx * sy * sz == 1) {\n                Rotation r;\n                r.perm[0] = perm[0]; r.sign[0] = sx;\n                r.perm[1] = perm[1]; r.sign[1] = sy;\n                r.perm[2] = perm[2]; r.sign[2] = sz;\n                rots.push_back(r);\n            }\n        }\n    } while (next_permutation(perm.begin(), perm.end()));\n    return rots;\n}\n\nint main() {\n    vector<Rotation> rots = generate_rotations();\n\n    int D;\n    cin >> D;\n    vector<vector<string>> f(2, vector<string>(D));\n    vector<vector<string>> r(2, vector<string>(D));\n    for (int i=0; i<2; i++) {\n        for (int j=0; j<D; j++) {\n            cin >> f[i][j];\n        }\n        for (int j=0; j<D; j++) {\n            cin >> r[i][j];\n        }\n    }\n\n    vector<vector<vector<vector<bool>>>> Allowed(2, vector<vector<vector<bool>>>(D, vector<vector<bool>>(D, vector<bool>(D, false))));\n    vector<vector<vector<vector<bool>>>> Visited(2, vector<vector<vector<bool>>>(D, vector<vector<bool>>(D, vector<bool>(D, false))));\n    vector<vector<vector<tuple<int,int,int>>>> blocks(2);\n\n    vector<tuple<int,int,int>> dirs = {{1,0,0}, {-1,0,0}, {0,1,0}, {0,-1,0}, {0,0,1}, {0,0,-1}};\n\n    for (int obj=0; obj<2; obj++) {\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                    if (f[obj][z][x]=='1' && r[obj][z][y]=='1') {\n                        Allowed[obj][z][x][y] = true;\n                    }\n                }\n            }\n        }\n\n        vector<vector<tuple<int,int,int>>> components;\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 (Allowed[obj][z][x][y] && !Visited[obj][z][x][y]) {\n                        vector<tuple<int,int,int>> comp;\n                        queue<tuple<int,int,int>> q;\n                        q.push({x,y,z});\n                        Visited[obj][z][x][y] = true;\n                        while (!q.empty()) {\n                            auto [cx, cy, cz] = q.front();\n                            q.pop();\n                            comp.push_back({cx, cy, cz});\n                            for (auto [dx,dy,dz] : dirs) {\n                                int nx = cx + dx;\n                                int ny = cy + dy;\n                                int nz = cz + dz;\n                                if (nx>=0 && nx<D && ny>=0 && ny<D && nz>=0 && nz<D) {\n                                    if (Allowed[obj][nz][nx][ny] && !Visited[obj][nz][nx][ny]) {\n                                        Visited[obj][nz][nx][ny] = true;\n                                        q.push({nx, ny, nz});\n                                    }\n                                }\n                            }\n                        }\n                        components.push_back(comp);\n                    }\n                }\n            }\n        }\n\n        for (auto &comp : components) {\n            set<tuple<int,int,int>> comp_set(comp.begin(), comp.end());\n            set<tuple<int,int,int>> chosen_in_comp;\n\n            while (!comp_set.empty()) {\n                int best_count = -1;\n                tuple<int,int,int> best_point = *comp_set.begin();\n                for (auto &p : comp_set) {\n                    int count = 0;\n                    auto [x,y,z] = p;\n                    for (auto [dx,dy,dz] : dirs) {\n                        int nx = x + dx;\n                        int ny = y + dy;\n                        int nz = z + dz;\n                        tuple<int,int,int> np = {nx, ny, nz};\n                        if (nx>=0 && nx<D && ny>=0 && ny<D && nz>=0 && nz<D) {\n                            if (chosen_in_comp.find(np) != chosen_in_comp.end()) {\n                                count++;\n                            }\n                        }\n                    }\n                    if (count > best_count) {\n                        best_count = count;\n                        best_point = p;\n                    }\n                }\n\n                vector<tuple<int,int,int>> block;\n                queue<tuple<int,int,int>> q;\n                q.push(best_point);\n                comp_set.erase(best_point);\n                chosen_in_comp.insert(best_point);\n                block.push_back(best_point);\n\n                while (!q.empty() && block.size() < MAX_VOL) {\n                    auto [x,y,z] = q.front();\n                    q.pop();\n                    for (auto [dx,dy,dz] : dirs) {\n                        int nx = x + dx;\n                        int ny = y + dy;\n                        int nz = z + dz;\n                        tuple<int,int,int> np = {nx, ny, nz};\n                        if (comp_set.find(np) != comp_set.end()) {\n                            comp_set.erase(np);\n                            chosen_in_comp.insert(np);\n                            block.push_back(np);\n                            q.push(np);\n                            if (block.size() >= MAX_VOL) break;\n                        }\n                    }\n                }\n                blocks[obj].push_back(block);\n            }\n        }\n    }\n\n    vector<vector<vector<tuple<int,int,int>>>> canonical_blocks(2);\n    for (int obj=0; obj<2; obj++) {\n        for (auto &block : blocks[obj]) {\n            vector<vector<tuple<int,int,int>>> candidates;\n            for (auto &rot : rots) {\n                vector<tuple<int,int,int>> transformed;\n                for (auto [x,y,z] : block) {\n                    int coords[3] = {x, y, z};\n                    int nx = rot.sign[0] * coords[rot.perm[0]];\n                    int ny = rot.sign[1] * coords[rot.perm[1]];\n                    int nz = rot.sign[2] * coords[rot.perm[2]];\n                    transformed.push_back(make_tuple(nx, ny, nz));\n                }\n                int min_x = get<0>(transformed[0]), min_y = get<1>(transformed[0]), min_z = get<2>(transformed[0]);\n                for (auto &p : transformed) {\n                    min_x = min(min_x, get<0>(p));\n                    min_y = min(min_y, get<1>(p));\n                    min_z = min(min_z, get<2>(p));\n                }\n                vector<tuple<int,int,int>> shifted;\n                for (auto p : transformed) {\n                    shifted.push_back(make_tuple(get<0>(p)-min_x, get<1>(p)-min_y, get<2>(p)-min_z));\n                }\n                sort(shifted.begin(), shifted.end());\n                candidates.push_back(shifted);\n            }\n            sort(candidates.begin(), candidates.end());\n            canonical_blocks[obj].push_back(candidates[0]);\n        }\n    }\n\n    vector<tuple<int, vector<tuple<int,int,int>>, int>> list0;\n    for (size_t i=0; i<blocks[0].size(); i++) {\n        list0.push_back(make_tuple(blocks[0][i].size(), canonical_blocks[0][i], i));\n    }\n    vector<tuple<int, vector<tuple<int,int,int>>, int>> list1;\n    for (size_t i=0; i<blocks[1].size(); i++) {\n        list1.push_back(make_tuple(blocks[1][i].size(), canonical_blocks[1][i], i));\n    }\n\n    sort(list0.rbegin(), list0.rend());\n    sort(list1.rbegin(), list1.rend());\n\n    vector<int> global_id_counter = {1};\n    vector<int> mapping0(blocks[0].size(), -1);\n    vector<int> mapping1(blocks[1].size(), -1);\n\n    for (size_t i=0; i<list0.size(); i++) {\n        auto [vol0, canon0, idx0] = list0[i];\n        if (mapping0[idx0] != -1) continue;\n        for (size_t j=0; j<list1.size(); j++) {\n            auto [vol1, canon1, idx1] = list1[j];\n            if (mapping1[idx1] != -1) continue;\n            if (canon0 == canon1) {\n                mapping0[idx0] = global_id_counter[0];\n                mapping1[idx1] = global_id_counter[0];\n                global_id_counter[0]++;\n                break;\n            }\n        }\n    }\n\n    for (size_t i=0; i<blocks[0].size(); i++) {\n        if (mapping0[i] == -1) {\n            mapping0[i] = global_id_counter[0];\n            global_id_counter[0]++;\n        }\n    }\n    for (size_t i=0; i<blocks[1].size(); i++) {\n        if (mapping1[i] == -1) {\n            mapping1[i] = global_id_counter[0];\n            global_id_counter[0]++;\n        }\n    }\n\n    int n_blocks = global_id_counter[0] - 1;\n\n    vector<vector<vector<int>>> grid0(D, vector<vector<int>>(D, vector<int>(D,0)));\n    for (size_t idx=0; idx<blocks[0].size(); idx++) {\n        int gid = mapping0[idx];\n        for (auto [x,y,z] : blocks[0][idx]) {\n            grid0[x][y][z] = gid;\n        }\n    }\n\n    vector<vector<vector<int>>> grid1(D, vector<vector<int>>(D, vector<int>(D,0)));\n    for (size_t idx=0; idx<blocks[1].size(); idx++) {\n        int gid = mapping1[idx];\n        for (auto [x,y,z] : blocks[1][idx]) {\n            grid1[x][y][z] = gid;\n        }\n    }\n\n    vector<int> flat0, flat1;\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                flat0.push_back(grid0[x][y][z]);\n                flat1.push_back(grid1[x][y][z]);\n            }\n        }\n    }\n\n    cout << n_blocks << endl;\n    for (size_t i=0; i<flat0.size(); i++) {\n        if (i>0) cout << \" \";\n        cout << flat0[i];\n    }\n    cout << endl;\n    for (size_t i=0; i<flat1.size(); i++) {\n        if (i>0) cout << \" \";\n        cout << flat1[i];\n    }\n    cout << endl;\n\n    return 0;\n}","ahc020":"#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef tuple<int, ll, int> EdgeTuple;\n\nstruct Edge {\n    int u, v, w, id;\n};\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, K;\n    cin >> N >> M >> K;\n\n    vector<ll> x(N), y(N);\n    for (int i = 0; i < N; i++) {\n        cin >> x[i] >> y[i];\n    }\n\n    vector<Edge> edges;\n    for (int j = 0; j < M; j++) {\n        int u, v, w;\n        cin >> u >> v >> w;\n        u--; v--;\n        edges.push_back({u, v, w, j});\n    }\n\n    vector<ll> a(K), b(K);\n    for (int k = 0; k < K; k++) {\n        cin >> a[k] >> b[k];\n    }\n\n    vector<int> assigned_station(K, -1);\n    vector<vector<int>> cluster(N);\n\n    for (int k = 0; k < K; k++) {\n        ll min_d_sq = 1e18;\n        int best_i = -1;\n        for (int i = 0; i < N; i++) {\n            ll dx = a[k] - x[i];\n            ll dy = b[k] - y[i];\n            ll d_sq = dx*dx + dy*dy;\n            if (d_sq < min_d_sq) {\n                min_d_sq = d_sq;\n                best_i = i;\n            }\n        }\n        assigned_station[k] = best_i;\n        cluster[best_i].push_back(k);\n    }\n\n    vector<ll> P(N, 0);\n    vector<bool> in_S(N, false);\n\n    for (int i = 0; i < N; i++) {\n        if (cluster[i].empty()) {\n            P[i] = 0;\n        } else {\n            in_S[i] = true;\n            ll max_d_sq = 0;\n            for (int k : cluster[i]) {\n                ll dx = a[k] - x[i];\n                ll dy = b[k] - y[i];\n                ll d_sq = dx*dx + dy*dy;\n                if (d_sq > max_d_sq) max_d_sq = d_sq;\n            }\n            if (max_d_sq == 0) {\n                P[i] = 0;\n            } else {\n                ll candidate = sqrt(max_d_sq);\n                if (candidate * candidate < max_d_sq) {\n                    candidate++;\n                }\n                P[i] = candidate;\n                if (P[i] > 5000) {\n                    P[i] = 5000;\n                }\n            }\n        }\n    }\n\n    set<int> T_set;\n    for (int i = 0; i < N; i++) {\n        if (in_S[i]) {\n            T_set.insert(i);\n        }\n    }\n    T_set.insert(0);\n\n    vector<vector<EdgeTuple>> adj(N);\n    for (const Edge& e : edges) {\n        int u = e.u, v = e.v;\n        ll w = e.w;\n        int id = e.id;\n        adj[u].push_back(make_tuple(v, w, id));\n        adj[v].push_back(make_tuple(u, w, id));\n    }\n\n    vector<bool> in_tree(N, false);\n    vector<bool> edge_used(M, false);\n\n    in_tree[0] = true;\n    set<int> T_remaining = T_set;\n    T_remaining.erase(0);\n\n    while (!T_remaining.empty()) {\n        vector<ll> dist(N, 1e18);\n        vector<int> parent_node(N, -1);\n        vector<int> parent_edge_id(N, -1);\n        priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<>> pq;\n\n        for (int i = 0; i < N; i++) {\n            if (in_tree[i]) {\n                dist[i] = 0;\n                pq.push({0, i});\n            }\n        }\n\n        bool found = false;\n        int found_node = -1;\n\n        while (!pq.empty()) {\n            auto [d, u] = pq.top();\n            pq.pop();\n            if (d != dist[u]) continue;\n\n            if (T_remaining.find(u) != T_remaining.end()) {\n                found = true;\n                found_node = u;\n                break;\n            }\n\n            for (auto &edge : adj[u]) {\n                int v = get<0>(edge);\n                ll w = get<1>(edge);\n                int eid = get<2>(edge);\n                if (dist[v] > dist[u] + w) {\n                    dist[v] = dist[u] + w;\n                    parent_node[v] = u;\n                    parent_edge_id[v] = eid;\n                    pq.push({dist[v], v});\n                }\n            }\n        }\n\n        if (!found) {\n            break;\n        }\n\n        int cur = found_node;\n        while (!in_tree[cur]) {\n            edge_used[parent_edge_id[cur]] = true;\n            in_tree[cur] = true;\n            cur = parent_node[cur];\n        }\n\n        T_remaining.erase(found_node);\n    }\n\n    vector<bool> in_steiner(N, false);\n    {\n        queue<int> q;\n        q.push(0);\n        in_steiner[0] = true;\n        while (!q.empty()) {\n            int u = q.front(); q.pop();\n            for (auto &edge : adj[u]) {\n                int v = get<0>(edge);\n                int id = get<2>(edge);\n                if (edge_used[id] && !in_steiner[v]) {\n                    in_steiner[v] = true;\n                    q.push(v);\n                }\n            }\n        }\n    }\n\n    vector<ll> P_reassign(N, 0);\n    {\n        vector<pair<ll, int>> resident_min_dist;\n        for (int k = 0; k < K; k++) {\n            ll min_d_sq = 1e18;\n            for (int i = 0; i < N; i++) {\n                if (!in_steiner[i]) continue;\n                ll dx = x[i] - a[k];\n                ll dy = y[i] - b[k];\n                ll d_sq = dx*dx + dy*dy;\n                if (d_sq < min_d_sq) {\n                    min_d_sq = d_sq;\n                }\n            }\n            resident_min_dist.push_back({min_d_sq, k});\n        }\n        sort(resident_min_dist.begin(), resident_min_dist.end());\n\n        for (auto [d_sq_val, k] : resident_min_dist) {\n            ll best_increase = 1e18;\n            int best_station = -1;\n            for (int i = 0; i < N; i++) {\n                if (!in_steiner[i]) continue;\n                ll dx = x[i] - a[k];\n                ll dy = y[i] - b[k];\n                ll d_sq = dx*dx + dy*dy;\n                ll d = sqrt(d_sq);\n                if (d*d < d_sq) d++;\n\n                ll current_power = P_reassign[i];\n                if (d <= current_power) {\n                    if (0 < best_increase) {\n                        best_increase = 0;\n                        best_station = i;\n                    }\n                } else {\n                    ll increase = d*d - current_power*current_power;\n                    if (increase < best_increase) {\n                        best_increase = increase;\n                        best_station = i;\n                    }\n                }\n            }\n            if (best_station != -1) {\n                ll dx = x[best_station] - a[k];\n                ll dy = y[best_station] - b[k];\n                ll d_sq = dx*dx + dy*dy;\n                ll d = sqrt(d_sq);\n                if (d*d < d_sq) d++;\n                if (d > P_reassign[best_station]) {\n                    P_reassign[best_station] = d;\n                }\n            }\n        }\n    }\n\n    vector<set<pair<int, int>>> tree_adj(N);\n    vector<int> degree(N, 0);\n    for (int j = 0; j < M; j++) {\n        if (edge_used[j]) {\n            int u = edges[j].u;\n            int v = edges[j].v;\n            tree_adj[u].insert({v, j});\n            tree_adj[v].insert({u, j});\n            degree[u]++;\n            degree[v]++;\n        }\n    }\n\n    queue<int> q_prune;\n    for (int i = 0; i < N; i++) {\n        if (in_steiner[i] && degree[i] == 1 && i != 0 && P_reassign[i] == 0) {\n            q_prune.push(i);\n        }\n    }\n\n    while (!q_prune.empty()) {\n        int u = q_prune.front(); q_prune.pop();\n        if (!in_steiner[u] || degree[u] != 1 || u == 0 || P_reassign[u] != 0) continue;\n\n        in_steiner[u] = false;\n        if (tree_adj[u].empty()) continue;\n        auto it = tree_adj[u].begin();\n        int v = it->first;\n        int eid = it->second;\n        tree_adj[u].erase(it);\n        tree_adj[v].erase({u, eid});\n        degree[u]--;\n        degree[v]--;\n        edge_used[eid] = false;\n\n        if (degree[v] == 1 && v != 0 && P_reassign[v] == 0) {\n            q_prune.push(v);\n        }\n    }\n\n    fill(P_reassign.begin(), P_reassign.end(), 0);\n    {\n        vector<pair<ll, int>> resident_min_dist;\n        for (int k = 0; k < K; k++) {\n            ll min_d_sq = 1e18;\n            for (int i = 0; i < N; i++) {\n                if (!in_steiner[i]) continue;\n                ll dx = x[i] - a[k];\n                ll dy = y[i] - b[k];\n                ll d_sq = dx*dx + dy*dy;\n                if (d_sq < min_d_sq) {\n                    min_d_sq = d_sq;\n                }\n            }\n            resident_min_dist.push_back({min_d_sq, k});\n        }\n        sort(resident_min_dist.begin(), resident_min_dist.end());\n\n        for (auto [d_sq_val, k] : resident_min_dist) {\n            ll best_increase = 1e18;\n            int best_station = -1;\n            for (int i = 0; i < N; i++) {\n                if (!in_steiner[i]) continue;\n                ll dx = x[i] - a[k];\n                ll dy = y[i] - b[k];\n                ll d_sq = dx*dx + dy*dy;\n                ll d = sqrt(d_sq);\n                if (d*d < d_sq) d++;\n\n                ll current_power = P_reassign[i];\n                if (d <= current_power) {\n                    if (0 < best_increase) {\n                        best_increase = 0;\n                        best_station = i;\n                    }\n                } else {\n                    ll increase = d*d - current_power*current_power;\n                    if (increase < best_increase) {\n                        best_increase = increase;\n                        best_station = i;\n                    }\n                }\n            }\n            if (best_station != -1) {\n                ll dx = x[best_station] - a[k];\n                ll dy = y[best_station] - b[k];\n                ll d_sq = dx*dx + dy*dy;\n                ll d = sqrt(d_sq);\n                if (d*d < d_sq) d++;\n                if (d > P_reassign[best_station]) {\n                    P_reassign[best_station] = d;\n                }\n            }\n        }\n    }\n\n    for (int i = 0; i < N; i++) {\n        if (in_steiner[i]) {\n            if (P_reassign[i] > 5000) {\n                P[i] = 5000;\n            } else {\n                P[i] = P_reassign[i];\n            }\n        } else {\n            P[i] = 0;\n        }\n    }\n\n    for (int i = 0; i < N; i++) {\n        cout << P[i];\n        if (i < N-1) cout << \" \";\n    }\n    cout << endl;\n\n    for (int j = 0; j < M; j++) {\n        cout << (edge_used[j] ? 1 : 0);\n        if (j < M-1) cout << \" \";\n    }\n    cout << endl;\n\n    return 0;\n}","ahc021":"#include <iostream>\n#include <vector>\n#include <tuple>\n#include <algorithm>\nusing namespace std;\n\nint main() {\n    int grid[30][30];\n    pair<int, int> pos[465];\n\n    for (int i = 0; i < 30; i++) {\n        for (int j = 0; j <= i; j++) {\n            cin >> grid[i][j];\n            pos[grid[i][j]] = make_pair(i, j);\n        }\n    }\n\n    vector<int> nums;\n    for (int i = 464; i >= 0; i--) {\n        nums.push_back(i);\n    }\n\n    vector<tuple<int, int, int, int>> ops;\n\n    for (int num : nums) {\n        int cx = pos[num].first;\n        int cy = pos[num].second;\n        while (cx < 29) {\n            int child1_val = grid[cx+1][cy];\n            int child2_val = grid[cx+1][cy+1];\n            int min_child_val = min(child1_val, child2_val);\n            \n            if (min_child_val >= num) {\n                break;\n            }\n            \n            int child_x = cx + 1;\n            int child_y = (min_child_val == child1_val) ? cy : cy + 1;\n            int child_num = min_child_val;\n            \n            ops.push_back(make_tuple(cx, cy, child_x, child_y));\n            \n            swap(grid[cx][cy], grid[child_x][child_y]);\n            swap(pos[num], pos[child_num]);\n            \n            cx = child_x;\n            cy = child_y;\n        }\n    }\n\n    cout << ops.size() << endl;\n    for (auto op : ops) {\n        cout << get<0>(op) << \" \" << get<1>(op) << \" \" << get<2>(op) << \" \" << get<3>(op) << endl;\n    }\n\n    return 0;\n}","toyota2023summer-final":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\n#include <functional>\n#include <tuple>\n#include <climits>\n#include <cmath>\n\nusing namespace std;\n\nint main() {\n    int D = 9;\n    int N;\n    cin >> D >> N;\n\n    vector<vector<int>> grid(D, vector<int>(D, -3));\n    grid[0][(D-1)/2] = -2;\n\n    for (int i = 0; i < N; i++) {\n        int ri, rj;\n        cin >> ri >> rj;\n        grid[ri][rj] = -1;\n    }\n\n    vector<vector<int>> parent_i(D, vector<int>(D, -1));\n    vector<vector<int>> parent_j(D, vector<int>(D, -1));\n    vector<vector<vector<pair<int, int>>>> children(D, vector<vector<pair<int, int>>>(D));\n\n    vector<int> dx = {0, 1, 0, -1};\n    vector<int> dy = {1, 0, -1, 0};\n\n    queue<pair<int, int>> q_bfs_parent;\n    q_bfs_parent.push({0, (D-1)/2});\n    parent_i[0][(D-1)/2] = -1;\n    parent_j[0][(D-1)/2] = -1;\n\n    while (!q_bfs_parent.empty()) {\n        auto [i, j] = q_bfs_parent.front();\n        q_bfs_parent.pop();\n        for (int d = 0; d < 4; d++) {\n            int ni = i + dx[d];\n            int nj = j + dy[d];\n            if (ni >= 0 && ni < D && nj >= 0 && nj < D) {\n                if (parent_i[ni][nj] == -1) {\n                    if (grid[ni][nj] != -1) {\n                        parent_i[ni][nj] = i;\n                        parent_j[ni][nj] = j;\n                        q_bfs_parent.push({ni, nj});\n                    }\n                }\n            }\n        }\n    }\n\n    for (int i = 0; i < D; i++) {\n        for (int j = 0; j < D; j++) {\n            if (i == 0 && j == (D-1)/2) continue;\n            if (parent_i[i][j] != -1) {\n                int pi = parent_i[i][j];\n                int pj = parent_j[i][j];\n                children[pi][pj].push_back({i, j});\n            }\n        }\n    }\n\n    for (int i = 0; i < D; i++) {\n        for (int j = 0; j < D; j++) {\n            sort(children[i][j].begin(), children[i][j].end());\n        }\n    }\n\n    vector<vector<int>> post_index(D, vector<int>(D, -1));\n    int index_counter = 0;\n\n    function<void(int, int)> dfs = [&](int i, int j) {\n        for (auto& child : children[i][j]) {\n            dfs(child.first, child.second);\n        }\n        if (!(i == 0 && j == (D-1)/2)) {\n            post_index[i][j] = index_counter;\n            index_counter++;\n        }\n    };\n\n    for (auto& child : children[0][(D-1)/2]) {\n        dfs(child.first, child.second);\n    }\n\n    int M = D * D - 1 - N;\n\n    for (int d = 0; d < M; d++) {\n        int t_d;\n        cin >> t_d;\n\n        vector<vector<bool>> visited(D, vector<bool>(D, false));\n        vector<pair<int, int>> free_list;\n        queue<pair<int, int>> q_bfs;\n        q_bfs.push({0, (D-1)/2});\n        visited[0][(D-1)/2] = true;\n\n        while (!q_bfs.empty()) {\n            auto [i, j] = q_bfs.front();\n            q_bfs.pop();\n            if (grid[i][j] == -3) {\n                free_list.push_back({i, j});\n            }\n            for (int dir = 0; dir < 4; dir++) {\n                int ni = i + dx[dir];\n                int nj = j + dy[dir];\n                if (ni >= 0 && ni < D && nj >= 0 && nj < D && !visited[ni][nj]) {\n                    if (grid[ni][nj] == -1 || grid[ni][nj] >= 0) continue;\n                    visited[ni][nj] = true;\n                    q_bfs.push({ni, nj});\n                }\n            }\n        }\n\n        int best_i = -1, best_j = -1;\n        int best_diff = INT_MAX;\n        for (auto [i, j] : free_list) {\n            if (post_index[i][j] == -1) continue;\n            int diff = abs(post_index[i][j] - t_d);\n            if (diff < best_diff || (diff == best_diff && (i < best_i || (i == best_i && j < best_j)))) {\n                best_diff = diff;\n                best_i = i;\n                best_j = j;\n            }\n        }\n\n        if (best_i == -1) {\n            for (auto [i, j] : free_list) {\n                best_i = i;\n                best_j = j;\n                break;\n            }\n        }\n\n        cout << best_i << \" \" << best_j << endl;\n        cout.flush();\n        grid[best_i][best_j] = t_d;\n    }\n\n    vector<pair<int, int>> removal_sequence;\n\n    for (int step = 0; step < M; step++) {\n        vector<vector<bool>> visited(D, vector<bool>(D, false));\n        queue<pair<int, int>> q_bfs;\n        q_bfs.push({0, (D-1)/2});\n        visited[0][(D-1)/2] = true;\n\n        while (!q_bfs.empty()) {\n            auto [i, j] = q_bfs.front();\n            q_bfs.pop();\n            for (int dir = 0; dir < 4; dir++) {\n                int ni = i + dx[dir];\n                int nj = j + dy[dir];\n                if (ni >= 0 && ni < D && nj >= 0 && nj < D && !visited[ni][nj]) {\n                    if (grid[ni][nj] == -1) continue;\n                    if (grid[ni][nj] >= 0) continue;\n                    visited[ni][nj] = true;\n                    q_bfs.push({ni, nj});\n                }\n            }\n        }\n\n        vector<tuple<int, int, int>> removable;\n        for (int i = 0; i < D; i++) {\n            for (int j = 0; j < D; j++) {\n                if (grid[i][j] >= 0) {\n                    bool flag = false;\n                    for (int dir = 0; dir < 4; dir++) {\n                        int ni = i + dx[dir];\n                        int nj = j + dy[dir];\n                        if (ni >= 0 && ni < D && nj >= 0 && nj < D) {\n                            if (visited[ni][nj]) {\n                                flag = true;\n                                break;\n                            }\n                        }\n                    }\n                    if (flag) {\n                        removable.push_back(make_tuple(grid[i][j], i, j));\n                    }\n                }\n            }\n        }\n\n        if (removable.empty()) {\n            for (int i = 0; i < D; i++) {\n                for (int j = 0; j < D; j++) {\n                    if (grid[i][j] >= 0) {\n                        removable.push_back(make_tuple(grid[i][j], i, j));\n                    }\n                }\n            }\n            sort(removable.begin(), removable.end());\n        } else {\n            sort(removable.begin(), removable.end());\n        }\n\n        if (removable.empty()) {\n            break;\n        }\n\n        auto [num, i, j] = removable[0];\n        removal_sequence.push_back({i, j});\n        grid[i][j] = -3;\n    }\n\n    for (auto [i, j] : removal_sequence) {\n        cout << i << \" \" << j << endl;\n    }\n    cout.flush();\n\n    return 0;\n}","ahc024":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\n#include <cstring>\nusing namespace std;\n\ntypedef pair<int, int> pii;\n\nconst int dx[4] = {1, 0, -1, 0};\nconst int dy[4] = {0, 1, 0, -1};\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int n, m;\n    cin >> n >> m;\n    vector<vector<int>> grid(n, vector<int>(n));\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            cin >> grid[i][j];\n        }\n    }\n\n    vector<vector<bool>> orig_adj(101, vector<bool>(101, false));\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            if (i == 0 || i == n-1 || j == 0 || j == n-1) {\n                int c = grid[i][j];\n                if (c > 0) {\n                    orig_adj[0][c] = true;\n                }\n            }\n        }\n    }\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            if (i < n-1 && grid[i][j] != grid[i+1][j] && grid[i][j] > 0 && grid[i+1][j] > 0) {\n                int c1 = min(grid[i][j], grid[i+1][j]);\n                int c2 = max(grid[i][j], grid[i+1][j]);\n                orig_adj[c1][c2] = true;\n            }\n            if (j < n-1 && grid[i][j] != grid[i][j+1] && grid[i][j] > 0 && grid[i][j+1] > 0) {\n                int c1 = min(grid[i][j], grid[i][j+1]);\n                int c2 = max(grid[i][j], grid[i][j+1]);\n                orig_adj[c1][c2] = true;\n            }\n        }\n    }\n\n    vector<vector<int>> current_adj_count(101, vector<int>(101, 0));\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            if (i == 0 || i == n-1 || j == 0 || j == n-1) {\n                int c = grid[i][j];\n                if (c > 0) {\n                    current_adj_count[0][c]++;\n                }\n            }\n        }\n    }\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            if (i < n-1 && grid[i][j] != grid[i+1][j] && grid[i][j] > 0 && grid[i+1][j] > 0) {\n                int c1 = min(grid[i][j], grid[i+1][j]);\n                int c2 = max(grid[i][j], grid[i+1][j]);\n                current_adj_count[c1][c2]++;\n            }\n            if (j < n-1 && grid[i][j] != grid[i][j+1] && grid[i][j] > 0 && grid[i][j+1] > 0) {\n                int c1 = min(grid[i][j], grid[i][j+1]);\n                int c2 = max(grid[i][j], grid[i][j+1]);\n                current_adj_count[c1][c2]++;\n            }\n        }\n    }\n\n    vector<int> ward_size(m+1, 0);\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            int c = grid[i][j];\n            if (c > 0) {\n                ward_size[c]++;\n            }\n        }\n    }\n\n    int num_zeros = 0;\n    vector<vector<bool>> in_queue(n, vector<bool>(n, true));\n    queue<pii> candidate_queue;\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            candidate_queue.push({i, j});\n        }\n    }\n\n    while (!candidate_queue.empty()) {\n        auto [i, j] = candidate_queue.front();\n        candidate_queue.pop();\n        in_queue[i][j] = false;\n        if (grid[i][j] == 0) continue;\n\n        int c = grid[i][j];\n        if (ward_size[c] == 1) {\n            continue;\n        }\n\n        vector<pii> neighbors_in_c;\n        for (int d = 0; d < 4; d++) {\n            int ni = i + dx[d];\n            int nj = j + dy[d];\n            if (ni < 0 || ni >= n || nj < 0 || nj >= n) continue;\n            if (grid[ni][nj] == c) {\n                neighbors_in_c.push_back({ni, nj});\n            }\n        }\n        if (neighbors_in_c.empty()) {\n            continue;\n        }\n\n        int total_after = ward_size[c] - 1;\n        vector<vector<bool>> visited(n, vector<bool>(n, false));\n        queue<pii> q_bfs;\n        auto [start_i, start_j] = neighbors_in_c[0];\n        q_bfs.push({start_i, start_j});\n        visited[start_i][start_j] = true;\n        int count_visited = 1;\n\n        while (!q_bfs.empty()) {\n            auto [x, y] = q_bfs.front();\n            q_bfs.pop();\n            for (int d = 0; d < 4; d++) {\n                int nx = x + dx[d];\n                int ny = y + dy[d];\n                if (nx < 0 || nx >= n || ny < 0 || ny >= n) continue;\n                if (grid[nx][ny] != c) continue;\n                if (visited[nx][ny]) continue;\n                if (nx == i && ny == j) continue;\n                visited[nx][ny] = true;\n                count_visited++;\n                q_bfs.push({nx, ny});\n            }\n        }\n\n        if (count_visited != total_after) {\n            continue;\n        }\n\n        vector<int> break_count(m+1, 0);\n        for (int d = 0; d < 4; d++) {\n            int ni = i + dx[d];\n            int nj = j + dy[d];\n            if (ni < 0 || ni >= n || nj < 0 || nj >= n) continue;\n            int d_val = grid[ni][nj];\n            if (d_val == c) continue;\n            if (d_val == 0) {\n                break_count[0]++;\n            } else {\n                break_count[d_val]++;\n            }\n        }\n        if (i == 0 || i == n-1 || j == 0 || j == n-1) {\n            break_count[0]++;\n        }\n\n        bool skip = false;\n        for (int d = 0; d <= m; d++) {\n            if (break_count[d] == 0) continue;\n            if (d == 0) {\n                if (orig_adj[0][c] && current_adj_count[0][c] <= break_count[0]) {\n                    skip = true;\n                    break;\n                }\n            } else {\n                int c1 = min(c, d);\n                int c2 = max(c, d);\n                if (orig_adj[c1][c2] && current_adj_count[c1][c2] <= break_count[d]) {\n                    skip = true;\n                    break;\n                }\n            }\n        }\n        if (skip) continue;\n\n        for (int d = 0; d < 4; d++) {\n            int ni = i + dx[d];\n            int nj = j + dy[d];\n            if (ni < 0 || ni >= n || nj < 0 || nj >= n) continue;\n            int d_val = grid[ni][nj];\n            if (d_val > 0 && d_val != c) {\n                if (!orig_adj[0][d_val]) {\n                    skip = true;\n                    break;\n                }\n            }\n        }\n        if (skip) continue;\n\n        if (num_zeros == 0) {\n            if (i != 0 && i != n-1 && j != 0 && j != n-1) {\n                continue;\n            }\n        } else {\n            if (i != 0 && i != n-1 && j != 0 && j != n-1) {\n                bool has_zero_neighbor = false;\n                for (int d = 0; d < 4; d++) {\n                    int ni = i + dx[d];\n                    int nj = j + dy[d];\n                    if (ni < 0 || ni >= n || nj < 0 || nj >= n) continue;\n                    if (grid[ni][nj] == 0) {\n                        has_zero_neighbor = true;\n                        break;\n                    }\n                }\n                if (!has_zero_neighbor) {\n                    continue;\n                }\n            }\n        }\n\n        current_adj_count[0][c] -= break_count[0];\n        for (int d = 1; d <= m; d++) {\n            if (break_count[d] > 0) {\n                int c1 = min(c, d);\n                int c2 = max(c, d);\n                current_adj_count[c1][c2] -= break_count[d];\n            }\n        }\n\n        grid[i][j] = 0;\n        num_zeros++;\n        ward_size[c]--;\n\n        for (int d = 0; d < 4; d++) {\n            int ni = i + dx[d];\n            int nj = j + dy[d];\n            if (ni < 0 || ni >= n || nj < 0 || nj >= n) continue;\n            int d_val = grid[ni][nj];\n            if (d_val > 0) {\n                current_adj_count[0][d_val]++;\n            }\n        }\n\n        for (int d = 0; d < 4; d++) {\n            int ni = i + dx[d];\n            int nj = j + dy[d];\n            if (ni < 0 || ni >= n || nj < 0 || nj >= n) continue;\n            if (grid[ni][nj] != 0 && !in_queue[ni][nj]) {\n                candidate_queue.push({ni, nj});\n                in_queue[ni][nj] = true;\n            }\n        }\n    }\n\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            if (j > 0) cout << \" \";\n            cout << grid[i][j];\n        }\n        cout << endl;\n    }\n\n    return 0;\n}","ahc025":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <random>\n#include <cstdlib>\n#include <ctime>\n#include <numeric>\n#include <cmath>\n\nusing namespace std;\n\nint calc_merge_queries(int n) {\n    if (n <= 1) return 0;\n    int n1 = n / 2;\n    int n2 = n - n1;\n    return calc_merge_queries(n1) + calc_merge_queries(n2) + n - 1;\n}\n\nvector<int> merge(vector<int> left, vector<int> right, int &query_count) {\n    vector<int> res;\n    int i = 0, j = 0;\n    while (i < left.size() && j < right.size()) {\n        cout << \"1 1 \" << left[i] << \" \" << right[j] << endl;\n        string cmp;\n        cin >> cmp;\n        query_count++;\n        if (cmp == \"<\") {\n            res.push_back(left[i]);\n            i++;\n        } else if (cmp == \">\") {\n            res.push_back(right[j]);\n            j++;\n        } else {\n            res.push_back(left[i]);\n            i++;\n        }\n    }\n    while (i < left.size()) {\n        res.push_back(left[i]);\n        i++;\n    }\n    while (j < right.size()) {\n        res.push_back(right[j]);\n        j++;\n    }\n    return res;\n}\n\nvector<int> merge_sort(vector<int> v, int &query_count) {\n    if (v.size() <= 1) {\n        return v;\n    }\n    int mid = v.size() / 2;\n    vector<int> left(v.begin(), v.begin() + mid);\n    vector<int> right(v.begin() + mid, v.end());\n    left = merge_sort(left, query_count);\n    right = merge_sort(right, query_count);\n    return merge(left, right, query_count);\n}\n\nint main() {\n    random_device rd;\n    mt19937 g(rd());\n\n    int N, D, Q;\n    cin >> N >> D >> Q;\n\n    int T_sort = calc_merge_queries(N);\n    int T_assign = (N - D) * (D - 1);\n    bool use_methodA = (Q >= T_sort + T_assign);\n\n    vector<int> items;\n    for (int i = 0; i < N; i++) {\n        items.push_back(i);\n    }\n    shuffle(items.begin(), items.end(), g);\n\n    vector<int> methodA_items;\n    int query_count = 0;\n\n    if (use_methodA) {\n        methodA_items = merge_sort(items, query_count);\n        reverse(methodA_items.begin(), methodA_items.end());\n        items = methodA_items;\n    }\n\n    vector<vector<int>> groups(D);\n    for (int i = 0; i < D; i++) {\n        groups[i].push_back(items[i]);\n    }\n\n    if (use_methodA) {\n        for (int i = D; i < N; i++) {\n            vector<int> candidates(D);\n            iota(candidates.begin(), candidates.end(), 0);\n            while (candidates.size() > 1) {\n                int g1 = candidates[0];\n                int g2 = candidates[1];\n                cout << groups[g1].size() << \" \" << groups[g2].size();\n                for (int item : groups[g1]) {\n                    cout << \" \" << item;\n                }\n                for (int item : groups[g2]) {\n                    cout << \" \" << item;\n                }\n                cout << endl;\n\n                string res;\n                cin >> res;\n                query_count++;\n\n                if (res == \"<\") {\n                    candidates.erase(candidates.begin() + 1);\n                } else if (res == \">\") {\n                    candidates.erase(candidates.begin());\n                } else {\n                    candidates.erase(candidates.begin() + 1);\n                }\n            }\n            int group_index = candidates[0];\n            groups[group_index].push_back(items[i]);\n        }\n    } else {\n        auto tournament = [&](vector<int> candidate_groups) -> int {\n            while (candidate_groups.size() > 1) {\n                vector<int> next_candidates;\n                for (int j = 0; j < candidate_groups.size(); j += 2) {\n                    if (j + 1 == candidate_groups.size()) {\n                        next_candidates.push_back(candidate_groups[j]);\n                    } else {\n                        int g1 = candidate_groups[j];\n                        int g2 = candidate_groups[j+1];\n                        cout << groups[g1].size() << \" \" << groups[g2].size();\n                        for (int item : groups[g1]) {\n                            cout << \" \" << item;\n                        }\n                        for (int item : groups[g2]) {\n                            cout << \" \" << item;\n                        }\n                        cout << endl;\n\n                        string res;\n                        cin >> res;\n                        query_count++;\n\n                        if (res == \"<\") {\n                            next_candidates.push_back(g1);\n                        } else if (res == \">\") {\n                            next_candidates.push_back(g2);\n                        } else {\n                            next_candidates.push_back(g1);\n                        }\n                    }\n                }\n                candidate_groups = next_candidates;\n            }\n            return candidate_groups[0];\n        };\n\n        vector<int> all_groups(D);\n        iota(all_groups.begin(), all_groups.end(), 0);\n\n        for (int i = D; i < N; i++) {\n            int remaining_queries = Q - query_count;\n            int remaining_items = N - i;\n\n            if (remaining_queries <= 0) {\n                uniform_int_distribution<int> dist(0, D-1);\n                int group_index = dist(g);\n                groups[group_index].push_back(items[i]);\n            } else {\n                int k = min(D, 1 + remaining_queries / remaining_items);\n                k = max(2, k);\n                shuffle(all_groups.begin(), all_groups.end(), g);\n                vector<int> candidate_groups(all_groups.begin(), all_groups.begin() + k);\n                int group_index = tournament(candidate_groups);\n                groups[group_index].push_back(items[i]);\n            }\n        }\n    }\n\n    while (query_count < Q) {\n        cout << \"1 1 \" << items[0] << \" \" << items[1] << endl;\n        string res;\n        cin >> res;\n        query_count++;\n    }\n\n    vector<int> ans(N);\n    for (int group_id = 0; group_id < D; group_id++) {\n        for (int item : groups[group_id]) {\n            ans[item] = group_id;\n        }\n    }\n\n    for (int i = 0; i < N; i++) {\n        if (i > 0) cout << \" \";\n        cout << ans[i];\n    }\n    cout << endl;\n\n    return 0;\n}","ahc026":"#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int n, m;\n    cin >> n >> m;\n    vector<vector<int>> stacks(m);\n    for (int i = 0; i < m; i++) {\n        for (int j = 0; j < n/m; j++) {\n            int x;\n            cin >> x;\n            stacks[i].push_back(x);\n        }\n    }\n\n    vector<pair<int, int>> operations;\n\n    for (int v = 1; v <= n; v++) {\n        int s = -1, pos = -1;\n        for (int i = 0; i < m; i++) {\n            for (int j = 0; j < stacks[i].size(); j++) {\n                if (stacks[i][j] == v) {\n                    s = i;\n                    pos = j;\n                    break;\n                }\n            }\n            if (s != -1) break;\n        }\n\n        if (s == -1) {\n            continue;\n        }\n\n        if (pos == (int)stacks[s].size() - 1) {\n            operations.push_back({v, 0});\n            stacks[s].pop_back();\n        } else {\n            int w = stacks[s][pos+1];\n            int d = -1;\n            int best_top = -1;\n\n            for (int i = 0; i < m; i++) {\n                if (i == s) continue;\n                if (stacks[i].empty()) {\n                    if (1000 > best_top) {\n                        best_top = 1000;\n                        d = i;\n                    }\n                } else {\n                    int top_val = stacks[i].back();\n                    if (top_val > best_top) {\n                        best_top = top_val;\n                        d = i;\n                    }\n                }\n            }\n\n            if (d == -1) {\n                d = (s == 0) ? 1 : 0;\n            }\n\n            vector<int> seg;\n            for (int j = pos+1; j < stacks[s].size(); j++) {\n                seg.push_back(stacks[s][j]);\n            }\n            stacks[s].resize(pos+1);\n            for (int box : seg) {\n                stacks[d].push_back(box);\n            }\n\n            operations.push_back({w, d+1});\n            operations.push_back({v, 0});\n            stacks[s].pop_back();\n        }\n    }\n\n    for (auto &op : operations) {\n        cout << op.first << \" \" << op.second << '\\n';\n    }\n\n    return 0;\n}","ahc027":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\n#include <cmath>\n#include <unordered_map>\n#include <cctype>\n#include <climits>\n#include <functional>\n#include <set>\n#include <iomanip>\n\nusing namespace std;\n\nconst int dx[4] = {0, 1, 0, -1};\nconst string dir_str = \"RDLU\";\nconst int dy[4] = {1, 0, -1, 0};\n\nstruct Cell {\n    int i, j;\n    Cell(int i=0, int j=0) : i(i), j(j) {}\n    bool operator==(const Cell& other) const {\n        return i == other.i && j == other.j;\n    }\n    bool operator!=(const Cell& other) const {\n        return !(*this == other);\n    }\n};\n\nnamespace std {\n    template <>\n    struct hash<Cell> {\n        size_t operator()(const Cell& c) const {\n            return c.i * 100 + c.j;\n        }\n    };\n}\n\nint get_index(int i, int j, int N) {\n    return i * N + j;\n}\n\nCell get_cell(int idx, int N) {\n    return Cell(idx / N, idx % N);\n}\n\nchar get_move_char(Cell from, Cell to) {\n    if (to.i == from.i + 1) return 'D';\n    if (to.i == from.i - 1) return 'U';\n    if (to.j == from.j + 1) return 'R';\n    if (to.j == from.j - 1) return 'L';\n    return '?';\n}\n\nchar reverse_move(char m) {\n    if (m == 'U') return 'D';\n    if (m == 'D') return 'U';\n    if (m == 'L') return 'R';\n    if (m == 'R') return 'L';\n    return m;\n}\n\nvector<vector<int>> precompute_all_pairs_shortest_path(int N, const vector<vector<bool>>& down_wall, const vector<vector<bool>>& right_wall) {\n    int total_cells = N * N;\n    vector<vector<int>> dist(total_cells, vector<int>(total_cells, INT_MAX));\n\n    for (int idx = 0; idx < total_cells; idx++) {\n        Cell src = get_cell(idx, N);\n        vector<int>& d_vec = dist[idx];\n        d_vec[idx] = 0;\n        queue<int> q;\n        q.push(idx);\n\n        while (!q.empty()) {\n            int u_idx = q.front();\n            q.pop();\n            Cell u = get_cell(u_idx, N);\n\n            for (int d = 0; d < 4; d++) {\n                Cell v = Cell(u.i + dx[d], u.j + dy[d]);\n                if (v.i < 0 || v.i >= N || v.j < 0 || v.j >= N) continue;\n                if (d == 0) {\n                    if (right_wall[u.i][u.j]) continue;\n                } else if (d == 1) {\n                    if (down_wall[u.i][u.j]) continue;\n                } else if (d == 2) {\n                    if (v.j < u.j && (u.j-1>=0) && right_wall[u.i][u.j-1]) continue;\n                } else if (d == 3) {\n                    if (v.i < u.i && (u.i-1>=0) && down_wall[u.i-1][u.j]) continue;\n                }\n\n                int v_idx = get_index(v.i, v.j, N);\n                if (d_vec[v_idx] == INT_MAX) {\n                    d_vec[v_idx] = d_vec[u_idx] + 1;\n                    q.push(v_idx);\n                }\n            }\n        }\n    }\n\n    return dist;\n}\n\nvector<vector<int>> precompute_parents_all(int N, const vector<vector<bool>>& down_wall, const vector<vector<bool>>& right_wall) {\n    int total_cells = N * N;\n    vector<vector<int>> parent_all(total_cells, vector<int>(total_cells, -1));\n\n    for (int src_idx = 0; src_idx < total_cells; src_idx++) {\n        vector<int>& parent = parent_all[src_idx];\n        vector<int> dist(total_cells, INT_MAX);\n        dist[src_idx] = 0;\n        parent[src_idx] = src_idx;\n        queue<int> q;\n        q.push(src_idx);\n\n        while (!q.empty()) {\n            int u_idx = q.front();\n            q.pop();\n            Cell u = get_cell(u_idx, N);\n\n            for (int d = 0; d < 4; d++) {\n                Cell v = Cell(u.i + dx[d], u.j + dy[d]);\n                if (v.i < 0 || v.i >= N || v.j < 0 || v.j >= N) continue;\n                if (d == 0) {\n                    if (right_wall[u.i][u.j]) continue;\n                } else if (d == 1) {\n                    if (down_wall[u.i][u.j]) continue;\n                } else if (d == 2) {\n                    if (v.j < u.j && (u.j-1>=0) && right_wall[u.i][u.j-1]) continue;\n                } else if (d == 3) {\n                    if (v.i < u.i && (u.i-1>=0) && down_wall[u.i-1][u.j]) continue;\n                }\n\n                int v_idx = get_index(v.i, v.j, N);\n                if (dist[v_idx] == INT_MAX) {\n                    dist[v_idx] = dist[u_idx] + 1;\n                    parent[v_idx] = u_idx;\n                    q.push(v_idx);\n                }\n            }\n        }\n    }\n\n    return parent_all;\n}\n\nstring generate_base_route(int N, const vector<vector<bool>>& down_wall, const vector<vector<bool>>& right_wall, const vector<vector<int>>& d) {\n    int total_cells = N * N;\n    vector<int> parent_tree(total_cells, -1);\n    vector<int> dist(total_cells, INT_MAX);\n    vector<vector<int>> children(total_cells);\n\n    queue<int> q;\n    dist[0] = 0;\n    parent_tree[0] = 0;\n    q.push(0);\n\n    while (!q.empty()) {\n        int u_idx = q.front(); q.pop();\n        Cell u = get_cell(u_idx, N);\n        for (int d = 0; d < 4; d++) {\n            Cell v = Cell(u.i + dx[d], u.j + dy[d]);\n            if (v.i < 0 || v.i >= N || v.j < 0 || v.j >= N) continue;\n            if (d == 0) {\n                if (right_wall[u.i][u.j]) continue;\n            } else if (d == 1) {\n                if (down_wall[u.i][u.j]) continue;\n            } else if (d == 2) {\n                if (v.j < u.j && (u.j-1>=0) && right_wall[u.i][u.j-1]) continue;\n            } else if (d == 3) {\n                if (v.i < u.i && (u.i-1>=0) && down_wall[u.i-1][u.j]) continue;\n            }\n\n            int v_idx = get_index(v.i, v.j, N);\n            if (dist[v_idx] == INT_MAX) {\n                dist[v_idx] = dist[u_idx] + 1;\n                parent_tree[v_idx] = u_idx;\n                q.push(v_idx);\n                children[u_idx].push_back(v_idx);\n            }\n        }\n    }\n\n    for (int i = 0; i < total_cells; i++) {\n        if (dist[i] == INT_MAX) {\n            cerr << \"Cell not reached: \" << i/N << \", \" << i%N << endl;\n        }\n    }\n\n    function<string(int)> dfs = [&](int u_idx) {\n        string route = \"\";\n        Cell u = get_cell(u_idx, N);\n        vector<int>& child_list = children[u_idx];\n        sort(child_list.begin(), child_list.end(), [&](int a, int b) {\n            return d[get_cell(a, N).i][get_cell(a, N).j] > d[get_cell(b, N).i][get_cell(b, N).j];\n        });\n\n        for (int v_idx : child_list) {\n            Cell v = get_cell(v_idx, N);\n            char move_char = get_move_char(u, v);\n            route += move_char;\n            route += dfs(v_idx);\n            route += reverse_move(move_char);\n        }\n        return route;\n    };\n\n    string base_route = dfs(0);\n    return base_route;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N;\n    cin >> N;\n    vector<string> h_lines(N-1);\n    vector<string> v_lines(N);\n    vector<vector<int>> d(N, vector<int>(N));\n\n    for (int i = 0; i < N-1; i++) {\n        cin >> h_lines[i];\n    }\n    for (int i = 0; i < N; i++) {\n        cin >> v_lines[i];\n    }\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cin >> d[i][j];\n        }\n    }\n\n    vector<vector<bool>> down_wall(N-1, vector<bool>(N, false));\n    vector<vector<bool>> right_wall(N, vector<bool>(N-1, false));\n\n    for (int i = 0; i < N-1; i++) {\n        for (int j = 0; j < N; j++) {\n            down_wall[i][j] = (h_lines[i][j] == '1');\n        }\n    }\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N-1; j++) {\n            right_wall[i][j] = (v_lines[i][j] == '1');\n        }\n    }\n\n    vector<vector<int>> all_dist = precompute_all_pairs_shortest_path(N, down_wall, right_wall);\n    vector<vector<int>> parent_all = precompute_parents_all(N, down_wall, right_wall);\n\n    string base_route = generate_base_route(N, down_wall, right_wall, d);\n    string current_route = base_route;\n\n    int total_cells = N * N;\n    vector<vector<int>> occ(total_cells);\n    vector<Cell> positions;\n    Cell cur(0, 0);\n    positions.push_back(cur);\n    for (int i = 0; i < current_route.size(); i++) {\n        char m = current_route[i];\n        Cell next = cur;\n        if (m == 'R') next.j++;\n        else if (m == 'D') next.i++;\n        else if (m == 'L') next.j--;\n        else if (m == 'U') next.i--;\n        positions.push_back(next);\n        cur = next;\n    }\n\n    for (int idx = 0; idx < positions.size(); idx++) {\n        Cell cell = positions[idx];\n        if (idx > 0) {\n            int cell_idx = get_index(cell.i, cell.j, N);\n            occ[cell_idx].push_back(idx-1);\n        }\n    }\n\n    int L0 = current_route.size();\n    vector<int> freq(total_cells, 0);\n    for (int i = 0; i < total_cells; i++) {\n        freq[i] = occ[i].size();\n    }\n\n    vector<double> gap_est(total_cells, 0.0);\n    for (int i = 0; i < total_cells; i++) {\n        if (freq[i] > 0) {\n            gap_est[i] = L0 * 1.0 / freq[i];\n        }\n    }\n\n    using pdi = pair<double, int>;\n    priority_queue<pdi> pq;\n    for (int i = 0; i < total_cells; i++) {\n        if (freq[i] > 0) {\n            double priority_val = d[get_cell(i, N).i][get_cell(i, N).j] * gap_est[i] * gap_est[i];\n            pq.push({priority_val, i});\n        }\n    }\n\n    int insertion_count = 0;\n    const int MAX_INSERTION = 1000;\n    vector<int> dummy_occ_count(total_cells, 0);\n\n    while (insertion_count < MAX_INSERTION && !pq.empty()) {\n        double priority_val = pq.top().first;\n        int c_idx = pq.top().second;\n        pq.pop();\n        if (freq[c_idx] == 0) continue;\n\n        int best_u_idx = -1;\n        int best_dist = INT_MAX;\n        for (int u_idx = 0; u_idx < total_cells; u_idx++) {\n            if (freq[u_idx] == 0) continue;\n            if (all_dist[u_idx][c_idx] < best_dist) {\n                best_dist = all_dist[u_idx][c_idx];\n                best_u_idx = u_idx;\n            }\n        }\n\n        if (best_u_idx == -1 || best_dist == INT_MAX) {\n            continue;\n        }\n\n        int detour_length = 2 * best_dist;\n        if (L0 + detour_length > 100000) {\n            continue;\n        }\n\n        if (occ[best_u_idx].empty()) {\n            continue;\n        }\n        int insert_move_index = occ[best_u_idx][0];\n\n        Cell best_u = get_cell(best_u_idx, N);\n        Cell c = get_cell(c_idx, N);\n        string moves_to_c = \"\";\n        int current_idx = best_u_idx;\n        while (current_idx != c_idx) {\n            int next_idx = parent_all[c_idx][current_idx];\n            if (next_idx == -1) break;\n            Cell cur_cell = get_cell(current_idx, N);\n            Cell next_cell = get_cell(next_idx, N);\n            char m = get_move_char(cur_cell, next_cell);\n            moves_to_c += m;\n            current_idx = next_idx;\n        }\n\n        string moves_back = \"\";\n        for (int i = moves_to_c.size() - 1; i >= 0; i--) {\n            moves_back += reverse_move(moves_to_c[i]);\n        }\n        string detour = moves_to_c + moves_back;\n\n        string new_route = current_route.substr(0, insert_move_index+1) + detour + current_route.substr(insert_move_index+1);\n        int new_L = new_route.size();\n\n        vector<Cell> new_positions;\n        vector<vector<int>> new_occ(total_cells);\n        Cell pos(0,0);\n        new_positions.push_back(pos);\n        for (int i = 0; i < new_route.size(); i++) {\n            char m = new_route[i];\n            Cell next = pos;\n            if (m == 'R') next.j++;\n            else if (m == 'D') next.i++;\n            else if (m == 'L') next.j--;\n            else if (m == 'U') next.i--;\n            new_positions.push_back(next);\n            int cell_idx = get_index(next.i, next.j, N);\n            new_occ[cell_idx].push_back(i);\n            pos = next;\n        }\n\n        current_route = new_route;\n        L0 = new_L;\n        for (int i = 0; i < total_cells; i++) {\n            freq[i] = new_occ[i].size();\n            if (freq[i] > 0) {\n                gap_est[i] = L0 * 1.0 / freq[i];\n            }\n        }\n        occ = new_occ;\n\n        priority_queue<pdi> new_pq;\n        for (int i = 0; i < total_cells; i++) {\n            if (freq[i] > 0) {\n                double new_priority = d[get_cell(i, N).i][get_cell(i, N).j] * gap_est[i] * gap_est[i];\n                new_pq.push({new_priority, i});\n            }\n        }\n        pq = new_pq;\n\n        insertion_count++;\n    }\n\n    cout << current_route << endl;\n\n    return 0;\n}","ahc028":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <unordered_set>\n#include <climits>\n#include <tuple>\n#include <cmath>\n\nusing namespace std;\n\nconst int INF = 1e8;\nconst int N_grid = 15;\n\nvoid manhattan_transform(vector<vector<int>>& grid) {\n    int n = grid.size();\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            if (i > 0) grid[i][j] = min(grid[i][j], grid[i-1][j] + 1);\n            if (j > 0) grid[i][j] = min(grid[i][j], grid[i][j-1] + 1);\n        }\n    }\n    for (int i = n-1; i >=0; i--) {\n        for (int j = n-1; j >=0; j--) {\n            if (i+1 < n) grid[i][j] = min(grid[i][j], grid[i+1][j] + 1);\n            if (j+1 < n) grid[i][j] = min(grid[i][j], grid[i][j+1] + 1);\n        }\n    }\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M;\n    cin >> N >> M;\n    int s_i, s_j;\n    cin >> s_i >> s_j;\n\n    vector<string> grid(N);\n    for (int i = 0; i < N; i++) {\n        cin >> grid[i];\n    }\n\n    vector<string> targets;\n    for (int i = 0; i < M; i++) {\n        string t;\n        cin >> t;\n        targets.push_back(t);\n    }\n\n    unordered_set<string> target_set(targets.begin(), targets.end());\n    vector<vector<pair<int, int>>> positions(26);\n\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            char c = grid[i][j];\n            positions[c-'A'].push_back({i, j});\n        }\n    }\n\n    vector<vector<int>> d_table(26, vector<int>(26, INF));\n\n    for (int b = 0; b < 26; b++) {\n        vector<vector<int>> dist(N, vector<int>(N, INF));\n        for (auto [x, y] : positions[b]) {\n            dist[x][y] = 0;\n        }\n        manhattan_transform(dist);\n\n        for (int a = 0; a < 26; a++) {\n            int min_val = INF;\n            for (auto [x, y] : positions[a]) {\n                if (dist[x][y] < min_val) {\n                    min_val = dist[x][y];\n                }\n            }\n            d_table[a][b] = min_val;\n        }\n    }\n\n    string S = \"\";\n    unordered_set<string> covered;\n    int cur_x = s_i, cur_y = s_j;\n    vector<pair<int, int>> operations;\n\n    while (covered.size() < M && operations.size() < 5000) {\n        vector<int> min_dist_letter(26, INF);\n        for (int c = 0; c < 26; c++) {\n            for (auto [x, y] : positions[c]) {\n                int d = abs(cur_x - x) + abs(cur_y - y);\n                if (d < min_dist_letter[c]) {\n                    min_dist_letter[c] = d;\n                }\n            }\n        }\n\n        vector<tuple<double, int, string, double>> candidate_list;\n        for (string t : targets) {\n            if (covered.find(t) != covered.end()) continue;\n            int lb = min_dist_letter[t[0]-'A'] \n                     + d_table[t[0]-'A'][t[1]-'A'] \n                     + d_table[t[1]-'A'][t[2]-'A'] \n                     + d_table[t[2]-'A'][t[3]-'A'] \n                     + d_table[t[3]-'A'][t[4]-'A'] \n                     + 5;\n            string last_part = (S.length() >= 4) ? S.substr(S.length()-4) : S;\n            string candidate_string = last_part + t;\n            unordered_set<string> new_grams;\n            for (int i = 0; i <= (int)candidate_string.length()-5; i++) {\n                string gram = candidate_string.substr(i, 5);\n                if (target_set.find(gram) != target_set.end() && covered.find(gram) == covered.end()) {\n                    new_grams.insert(gram);\n                }\n            }\n            int new_count = new_grams.size();\n            if (new_count == 0) continue;\n            double score_lb = (double)lb / new_count;\n            candidate_list.push_back({score_lb, new_count, t, (double)lb});\n        }\n\n        if (candidate_list.empty()) {\n            break;\n        }\n\n        sort(candidate_list.begin(), candidate_list.end());\n        int K_candidate = min(10, (int)candidate_list.size());\n        vector<tuple<double, int, string, double>> top_candidates(candidate_list.begin(), candidate_list.begin() + K_candidate);\n\n        string best_string = \"\";\n        vector<tuple<int, int>> best_path;\n        double best_score = 1e18;\n        int best_new_count = 0;\n\n        for (auto [score_lb, new_count, t, lb] : top_candidates) {\n            vector<vector<vector<int>>> dp(5, vector<vector<int>>(N, vector<int>(N, INF)));\n            for (int x = 0; x < N; x++) {\n                for (int y = 0; y < N; y++) {\n                    if (grid[x][y] == t[0]) {\n                        dp[0][x][y] = abs(cur_x - x) + abs(cur_y - y) + 1;\n                    }\n                }\n            }\n\n            for (int k = 1; k < 5; k++) {\n                vector<vector<int>> g = dp[k-1];\n                manhattan_transform(g);\n                for (int x = 0; x < N; x++) {\n                    for (int y = 0; y < N; y++) {\n                        if (grid[x][y] == t[k]) {\n                            dp[k][x][y] = g[x][y] + 1;\n                        }\n                    }\n                }\n            }\n\n            int min_cost = INF;\n            int best_x4 = -1, best_y4 = -1;\n            for (int x = 0; x < N; x++) {\n                for (int y = 0; y < N; y++) {\n                    if (dp[4][x][y] < min_cost) {\n                        min_cost = dp[4][x][y];\n                        best_x4 = x;\n                        best_y4 = y;\n                    }\n                }\n            }\n\n            if (min_cost == INF) continue;\n\n            double actual_score = (double)min_cost / new_count;\n            if (actual_score < best_score) {\n                best_score = actual_score;\n                best_string = t;\n                best_new_count = new_count;\n\n                int cx = best_x4, cy = best_y4;\n                vector<tuple<int, int>> path(5);\n                path[4] = {cx, cy};\n                for (int k = 3; k >= 0; k--) {\n                    int best_val = INF;\n                    int best_x = -1, best_y = -1;\n                    for (int x = 0; x < N; x++) {\n                        for (int y = 0; y < N; y++) {\n                            if (dp[k][x][y] >= INF) continue;\n                            int cost_here = dp[k][x][y] + abs(x - cx) + abs(y - cy);\n                            if (cost_here < best_val) {\n                                best_val = cost_here;\n                                best_x = x;\n                                best_y = y;\n                            }\n                        }\n                    }\n                    if (best_val == INF) {\n                        for (int x = 0; x < N; x++) {\n                            for (int y = 0; y < N; y++) {\n                                if (dp[k][x][y] < INF) {\n                                    best_x = x;\n                                    best_y = y;\n                                    best_val = dp[k][x][y] + abs(x-cx) + abs(y-cy);\n                                    break;\n                                }\n                            }\n                            if (best_x != -1) break;\n                        }\n                    }\n                    path[k] = {best_x, best_y};\n                    cx = best_x;\n                    cy = best_y;\n                }\n                best_path = path;\n            }\n        }\n\n        if (best_string.empty()) {\n            break;\n        }\n\n        for (int k = 0; k < 5; k++) {\n            int x = get<0>(best_path[k]);\n            int y = get<1>(best_path[k]);\n            operations.push_back({x, y});\n            S += grid[x][y];\n            cur_x = x;\n            cur_y = y;\n        }\n\n        int L = S.length();\n        int start_index = max(0, L - 9);\n        int end_index = L - 5;\n        for (int i = start_index; i <= end_index; i++) {\n            string gram = S.substr(i, 5);\n            if (target_set.find(gram) != target_set.end()) {\n                covered.insert(gram);\n            }\n        }\n\n        if (covered.size() >= M) break;\n        if (operations.size() >= 5000) break;\n    }\n\n    for (auto [x, y] : operations) {\n        cout << x << \" \" << y << \"\\n\";\n    }\n\n    return 0;\n}","ahc030":"#include <iostream>\n#include <vector>\n#include <cmath>\n#include <algorithm>\nusing namespace std;\n\nint main() {\n    int N, M;\n    double eps;\n    cin >> N >> M >> eps;\n\n    vector<vector<pair<int, int>>> fields;\n    for (int k = 0; k < M; k++) {\n        int d;\n        cin >> d;\n        vector<pair<int, int>> field;\n        for (int i = 0; i < d; i++) {\n            int x, y;\n            cin >> x >> y;\n            field.push_back({x, y});\n        }\n        fields.push_back(field);\n    }\n\n    vector<vector<int>> grid(N, vector<int>(N, -1));\n    vector<pair<int, int>> has_oil;\n    const int block_size = 5;\n\n    for (int i0 = 0; i0 < N; i0 += block_size) {\n        for (int j0 = 0; j0 < N; j0 += block_size) {\n            int i1 = min(i0 + block_size, N);\n            int j1 = min(j0 + block_size, N);\n            vector<pair<int, int>> block;\n            for (int i = i0; i < i1; i++) {\n                for (int j = j0; j < j1; j++) {\n                    block.push_back({i, j});\n                }\n            }\n            int k = block.size();\n            bool nonempty = false;\n            for (int trial = 0; trial < 3; trial++) {\n                cout << \"q \" << k;\n                for (auto [i, j] : block) {\n                    cout << \" \" << i << \" \" << j;\n                }\n                cout << endl;\n                int X;\n                cin >> X;\n                if (X >= 1) {\n                    nonempty = true;\n                    break;\n                }\n            }\n            if (nonempty) {\n                for (auto [i, j] : block) {\n                    cout << \"q 1 \" << i << \" \" << j << endl;\n                    int v;\n                    cin >> v;\n                    grid[i][j] = v;\n                    if (v > 0) {\n                        has_oil.push_back({i, j});\n                    }\n                }\n            } else {\n                for (auto [i, j] : block) {\n                    grid[i][j] = 0;\n                }\n            }\n        }\n    }\n\n    cout << \"a \" << has_oil.size();\n    for (auto [i, j] : has_oil) {\n        cout << \" \" << i << \" \" << j;\n    }\n    cout << endl;\n    int resp;\n    cin >> resp;\n\n    return 0;\n}","ahc031":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <cmath>\n#include <tuple>\n#include <climits>\nusing namespace std;\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int W, D, N;\n    cin >> W >> D >> N;\n    vector<vector<int>> a(D, vector<int>(N));\n    for (int d = 0; d < D; ++d) {\n        for (int k = 0; k < N; ++k) {\n            cin >> a[d][k];\n        }\n    }\n\n    for (int d = 0; d < D; ++d) {\n        vector<int> skyline(W, 0);\n        vector<tuple<int, int, int, int>> placements(N);\n\n        vector<pair<int, int>> events;\n        for (int k = 0; k < N; ++k) {\n            events.push_back({a[d][k], k});\n        }\n        sort(events.begin(), events.end(), [](const auto &x, const auto &y) {\n            return x.first > y.first;\n        });\n\n        for (const auto& event : events) {\n            int area = event.first;\n            int idx = event.second;\n            int w = ceil(sqrt(area));\n            int h = (area + w - 1) / w;\n\n            bool placed = false;\n            for (int orientation = 0; orientation < 2; ++orientation) {\n                for (int start_col = 0; start_col <= W - w; ++start_col) {\n                    int base = 0;\n                    for (int col = start_col; col < start_col + w; ++col) {\n                        if (skyline[col] > base) {\n                            base = skyline[col];\n                        }\n                    }\n                    if (base + h <= W) {\n                        for (int col = start_col; col < start_col + w; ++col) {\n                            skyline[col] = base + h;\n                        }\n                        placements[idx] = make_tuple(base, start_col, base + h, start_col + w);\n                        placed = true;\n                        break;\n                    }\n                }\n                if (placed) break;\n                swap(w, h);\n            }\n\n            if (!placed) {\n                int h_emerg = (area + W - 1) / W;\n                int w_emerg = W;\n                int base = 0;\n                for (int col = 0; col < W; ++col) {\n                    if (skyline[col] > base) {\n                        base = skyline[col];\n                    }\n                }\n                if (base + h_emerg > W) {\n                    h_emerg = W - base;\n                }\n                for (int col = 0; col < W; ++col) {\n                    skyline[col] = base + h_emerg;\n                }\n                placements[idx] = make_tuple(base, 0, base + h_emerg, w_emerg);\n            }\n        }\n\n        for (int k = 0; k < N; ++k) {\n            auto [i0, j0, i1, j1] = placements[k];\n            cout << i0 << \" \" << j0 << \" \" << i1 << \" \" << j1 << \"\\n\";\n        }\n    }\n\n    return 0;\n}","ahc032":"#include <iostream>\n#include <vector>\n#include <tuple>\n#include <algorithm>\n#include <climits>\n#include <cmath>\nusing namespace std;\n\nconst long long mod = 998244353;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, K;\n    cin >> N >> M >> K;\n\n    vector<vector<long long>> grid(N, vector<long long>(N));\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cin >> grid[i][j];\n        }\n    }\n\n    vector<vector<vector<long long>>> stamps(M, vector<vector<long long>>(3, vector<long long>(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 >> stamps[m][i][j];\n            }\n        }\n    }\n\n    vector<tuple<int, int, int>> operations;\n    long long total_score = 0;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            total_score += grid[i][j];\n        }\n    }\n\n    while (operations.size() < K) {\n        long long best_delta = LLONG_MIN;\n        int best_m = -1, best_p = -1, best_q = -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                    long long delta = 0;\n                    for (int i = 0; i < 3; i++) {\n                        for (int j = 0; j < 3; j++) {\n                            long long old_val = grid[p+i][q+j];\n                            long long add = stamps[m][i][j];\n                            long long new_val = old_val + add;\n                            if (new_val >= mod) {\n                                new_val -= mod;\n                            }\n                            delta += (new_val - old_val);\n                        }\n                    }\n                    if (delta > best_delta) {\n                        best_delta = delta;\n                        best_m = m;\n                        best_p = p;\n                        best_q = q;\n                    }\n                }\n            }\n        }\n\n        if (best_delta <= 0) {\n            break;\n        }\n\n        for (int i = 0; i < 3; i++) {\n            for (int j = 0; j < 3; j++) {\n                long long old_val = grid[best_p+i][best_q+j];\n                long long new_val = old_val + stamps[best_m][i][j];\n                if (new_val >= mod) {\n                    new_val -= mod;\n                }\n                grid[best_p+i][best_q+j] = new_val;\n                total_score += (new_val - old_val);\n            }\n        }\n\n        operations.push_back(make_tuple(best_m, best_p, best_q));\n    }\n\n    int passes = 0;\n    const int MAX_PASSES = 10;\n    while (passes < MAX_PASSES) {\n        bool improved = false;\n        vector<tuple<int, int, int>> current_ops = operations;\n        vector<long long> removal_effects(current_ops.size());\n        vector<int> indices(current_ops.size());\n        for (int idx = 0; idx < current_ops.size(); idx++) {\n            auto [m, p, q] = current_ops[idx];\n            long long effect = 0;\n            for (int i = 0; i < 3; i++) {\n                for (int j = 0; j < 3; j++) {\n                    long long old_val = grid[p+i][q+j];\n                    long long s_val = stamps[m][i][j];\n                    if (old_val < s_val) {\n                        effect += (mod - s_val);\n                    } else {\n                        effect -= s_val;\n                    }\n                }\n            }\n            removal_effects[idx] = effect;\n            indices[idx] = idx;\n        }\n\n        sort(indices.begin(), indices.end(), [&](int a, int b) {\n            return removal_effects[a] > removal_effects[b];\n        });\n\n        for (int idx : indices) {\n            vector<vector<long long>> grid_backup = grid;\n            long long total_score_backup = total_score;\n\n            auto [m_remove, p_remove, q_remove] = current_ops[idx];\n            for (int i = 0; i < 3; i++) {\n                for (int j = 0; j < 3; j++) {\n                    long long old_val = grid[p_remove+i][q_remove+j];\n                    long long s_val = stamps[m_remove][i][j];\n                    long long new_val = old_val - s_val;\n                    if (new_val < 0) {\n                        new_val += mod;\n                    }\n                    grid[p_remove+i][q_remove+j] = new_val;\n                    total_score += (new_val - old_val);\n                }\n            }\n\n            vector<tuple<int, int, int>> new_ops;\n            int max_add = K - (current_ops.size() - 1);\n            for (int op_count = 0; op_count < max_add; op_count++) {\n                long long best_delta = LLONG_MIN;\n                int best_m = -1, best_p = -1, best_q = -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                            long long delta = 0;\n                            for (int i = 0; i < 3; i++) {\n                                for (int j = 0; j < 3; j++) {\n                                    long long old_val = grid[p+i][q+j];\n                                    long long add = stamps[m][i][j];\n                                    long long new_val = old_val + add;\n                                    if (new_val >= mod) {\n                                        new_val -= mod;\n                                    }\n                                    delta += (new_val - old_val);\n                                }\n                            }\n                            if (delta > best_delta) {\n                                best_delta = delta;\n                                best_m = m;\n                                best_p = p;\n                                best_q = q;\n                            }\n                        }\n                    }\n                }\n\n                if (best_delta <= 0) {\n                    break;\n                }\n\n                for (int i = 0; i < 3; i++) {\n                    for (int j = 0; j < 3; j++) {\n                        long long old_val = grid[best_p+i][best_q+j];\n                        long long new_val = old_val + stamps[best_m][i][j];\n                        if (new_val >= mod) {\n                            new_val -= mod;\n                        }\n                        grid[best_p+i][best_q+j] = new_val;\n                        total_score += (new_val - old_val);\n                    }\n                }\n                new_ops.push_back(make_tuple(best_m, best_p, best_q));\n            }\n\n            if (total_score > total_score_backup) {\n                operations = current_ops;\n                operations.erase(operations.begin() + idx);\n                operations.insert(operations.end(), new_ops.begin(), new_ops.end());\n                improved = true;\n                break;\n            } else {\n                grid = grid_backup;\n                total_score = total_score_backup;\n            }\n        }\n\n        if (!improved) {\n            break;\n        }\n        passes++;\n    }\n\n    cout << operations.size() << '\\n';\n    for (const auto& op : operations) {\n        cout << get<0>(op) << ' ' << get<1>(op) << ' ' << get<2>(op) << '\\n';\n    }\n\n    return 0;\n}","ahc033":"#include <iostream>\n#include <vector>\n#include <string>\n#include <algorithm>\n#include <utility>\nusing namespace std;\n\nvoid move_to(string& s, int& cr, int& cc, int tr, int tc, int& turn_count) {\n    while (cr < tr) {\n        s += 'D';\n        cr++;\n        turn_count++;\n    }\n    while (cr > tr) {\n        s += 'U';\n        cr--;\n        turn_count++;\n    }\n    while (cc < tc) {\n        s += 'R';\n        cc++;\n        turn_count++;\n    }\n    while (cc > tc) {\n        s += 'L';\n        cc--;\n        turn_count++;\n    }\n}\n\nint main() {\n    int N;\n    cin >> N;\n    vector<vector<int>> A(5, vector<int>(5));\n    for (int i = 0; i < 5; i++) {\n        for (int j = 0; j < 5; j++) {\n            cin >> A[i][j];\n        }\n    }\n\n    vector<string> crane_actions(5);\n    for (int i = 1; i < 5; i++) {\n        crane_actions[i] = \"RB\";\n    }\n\n    string large_actions = \"R\";\n    int turn_count = 1;\n    int cr = 0, cc = 1;\n\n    vector<int> next_exp = {0, 5, 10, 15, 20};\n    vector<vector<bool>> buffer_occupied(5, vector<bool>(5, false));\n    vector<vector<int>> buffer_contents(5, vector<int>(5, -1));\n\n    for (int i = 0; i < 5; i++) {\n        for (int j = 0; j < 5; j++) {\n            move_to(large_actions, cr, cc, i, 0, turn_count);\n            large_actions += 'P';\n            turn_count++;\n\n            int x = A[i][j];\n            int r = x / 5;\n            int target_col;\n\n            if (x == next_exp[r]) {\n                target_col = 4;\n            } else {\n                if (!buffer_occupied[r][2]) {\n                    target_col = 2;\n                    buffer_occupied[r][2] = true;\n                    buffer_contents[r][2] = x;\n                } else if (!buffer_occupied[r][3]) {\n                    target_col = 3;\n                    buffer_occupied[r][3] = true;\n                    buffer_contents[r][3] = x;\n                } else {\n                    target_col = 4;\n                }\n            }\n\n            move_to(large_actions, cr, cc, r, target_col, turn_count);\n            large_actions += 'Q';\n            turn_count++;\n\n            if (target_col == 4) {\n                next_exp[r]++;\n            }\n\n            bool cleared;\n            do {\n                cleared = false;\n                if (buffer_occupied[r][2] && buffer_contents[r][2] == next_exp[r]) {\n                    move_to(large_actions, cr, cc, r, 2, turn_count);\n                    large_actions += 'P';\n                    turn_count++;\n                    move_to(large_actions, cr, cc, r, 4, turn_count);\n                    large_actions += 'Q';\n                    turn_count++;\n                    next_exp[r]++;\n                    buffer_occupied[r][2] = false;\n                    cleared = true;\n                } else if (buffer_occupied[r][3] && buffer_contents[r][3] == next_exp[r]) {\n                    move_to(large_actions, cr, cc, r, 3, turn_count);\n                    large_actions += 'P';\n                    turn_count++;\n                    move_to(large_actions, cr, cc, r, 4, turn_count);\n                    large_actions += 'Q';\n                    turn_count++;\n                    next_exp[r]++;\n                    buffer_occupied[r][3] = false;\n                    cleared = true;\n                }\n            } while (cleared);\n\n            move_to(large_actions, cr, cc, i, 1, turn_count);\n        }\n    }\n\n    for (int r = 0; r < 5; r++) {\n        vector<pair<int, int>> bufs;\n        if (buffer_occupied[r][2]) {\n            bufs.push_back({buffer_contents[r][2], 2});\n        }\n        if (buffer_occupied[r][3]) {\n            bufs.push_back({buffer_contents[r][3], 3});\n        }\n        sort(bufs.begin(), bufs.end());\n        for (auto [num, col] : bufs) {\n            move_to(large_actions, cr, cc, r, col, turn_count);\n            large_actions += 'P';\n            turn_count++;\n            move_to(large_actions, cr, cc, r, 4, turn_count);\n            large_actions += 'Q';\n            turn_count++;\n        }\n    }\n\n    crane_actions[0] = large_actions;\n    size_t max_len = crane_actions[0].size();\n    for (int i = 1; i < 5; i++) {\n        if (crane_actions[i].size() < max_len) {\n            crane_actions[i] += string(max_len - crane_actions[i].size(), '.');\n        }\n    }\n\n    for (int i = 0; i < 5; i++) {\n        cout << crane_actions[i] << endl;\n    }\n\n    return 0;\n}","ahc034":"#include <iostream>\n#include <vector>\n#include <string>\nusing namespace std;\n\nint main() {\n    int N;\n    cin >> N;\n    vector<vector<int>> grid(N, vector<int>(N));\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cin >> grid[i][j];\n        }\n    }\n\n    vector<string> ops;\n    int x = 0, y = 0;\n    int load = 0;\n\n    if (grid[0][0] < 0) {\n        int need = -grid[0][0];\n        if (load >= need) {\n            ops.push_back(\"-\" + to_string(need));\n            load -= need;\n            grid[0][0] = 0;\n        } else if (load > 0) {\n            ops.push_back(\"-\" + to_string(load));\n            grid[0][0] += load;\n            load = 0;\n        }\n    }\n    if (grid[0][0] > 0) {\n        ops.push_back(\"+\" + to_string(grid[0][0]));\n        load += grid[0][0];\n        grid[0][0] = 0;\n    }\n\n    for (int i = 0; i < N; i++) {\n        if (i % 2 == 0) {\n            if (i > 0) {\n                if (grid[i][0] < 0) {\n                    int need = -grid[i][0];\n                    if (load >= need) {\n                        ops.push_back(\"-\" + to_string(need));\n                        load -= need;\n                        grid[i][0] = 0;\n                    } else if (load > 0) {\n                        ops.push_back(\"-\" + to_string(load));\n                        grid[i][0] += load;\n                        load = 0;\n                    }\n                }\n                if (grid[i][0] > 0) {\n                    ops.push_back(\"+\" + to_string(grid[i][0]));\n                    load += grid[i][0];\n                    grid[i][0] = 0;\n                }\n            }\n            for (int j = (i == 0 ? 1 : 1); j < N; j++) {\n                ops.push_back(\"R\");\n                y++;\n                if (grid[i][j] < 0) {\n                    int need = -grid[i][j];\n                    if (load >= need) {\n                        ops.push_back(\"-\" + to_string(need));\n                        load -= need;\n                        grid[i][j] = 0;\n                    } else if (load > 0) {\n                        ops.push_back(\"-\" + to_string(load));\n                        grid[i][j] += load;\n                        load = 0;\n                    }\n                }\n                if (grid[i][j] > 0) {\n                    ops.push_back(\"+\" + to_string(grid[i][j]));\n                    load += grid[i][j];\n                    grid[i][j] = 0;\n                }\n            }\n            if (i < N-1) {\n                ops.push_back(\"D\");\n                x++;\n            }\n        } else {\n            if (grid[i][N-1] < 0) {\n                int need = -grid[i][N-1];\n                if (load >= need) {\n                    ops.push_back(\"-\" + to_string(need));\n                    load -= need;\n                    grid[i][N-1] = 0;\n                } else if (load > 0) {\n                    ops.push_back(\"-\" + to_string(load));\n                    grid[i][N-1] += load;\n                    load = 0;\n                }\n            }\n            if (grid[i][N-1] > 0) {\n                ops.push_back(\"+\" + to_string(grid[i][N-1]));\n                load += grid[i][N-1];\n                grid[i][N-1] = 0;\n            }\n            for (int j = N-2; j >= 0; j--) {\n                ops.push_back(\"L\");\n                y--;\n                if (grid[i][j] < 0) {\n                    int need = -grid[i][j];\n                    if (load >= need) {\n                        ops.push_back(\"-\" + to_string(need));\n                        load -= need;\n                        grid[i][j] = 0;\n                    } else if (load > 0) {\n                        ops.push_back(\"-\" + to_string(load));\n                        grid[i][j] += load;\n                        load = 0;\n                    }\n                }\n                if (grid[i][j] > 0) {\n                    ops.push_back(\"+\" + to_string(grid[i][j]));\n                    load += grid[i][j];\n                    grid[i][j] = 0;\n                }\n            }\n            if (i < N-1) {\n                ops.push_back(\"D\");\n                x++;\n            }\n        }\n    }\n\n    while (x > 0) {\n        ops.push_back(\"U\");\n        x--;\n    }\n    while (y > 0) {\n        ops.push_back(\"L\");\n        y--;\n    }\n\n    if (grid[0][0] < 0) {\n        int d = -grid[0][0];\n        ops.push_back(\"-\" + to_string(d));\n        load -= d;\n        grid[0][0] = 0;\n    }\n\n    x = 0;\n    y = 0;\n    for (int i = 0; i < N; i++) {\n        if (i % 2 == 0) {\n            if (i > 0) {\n                if (grid[i][0] < 0) {\n                    int d = -grid[i][0];\n                    ops.push_back(\"-\" + to_string(d));\n                    load -= d;\n                    grid[i][0] = 0;\n                }\n            }\n            for (int j = (i == 0 ? 1 : 1); j < N; j++) {\n                ops.push_back(\"R\");\n                y++;\n                if (grid[i][j] < 0) {\n                    int d = -grid[i][j];\n                    ops.push_back(\"-\" + to_string(d));\n                    load -= d;\n                    grid[i][j] = 0;\n                }\n            }\n            if (i < N-1) {\n                ops.push_back(\"D\");\n                x++;\n            }\n        } else {\n            if (grid[i][N-1] < 0) {\n                int d = -grid[i][N-1];\n                ops.push_back(\"-\" + to_string(d));\n                load -= d;\n                grid[i][N-1] = 0;\n            }\n            for (int j = N-2; j >= 0; j--) {\n                ops.push_back(\"L\");\n                y--;\n                if (grid[i][j] < 0) {\n                    int d = -grid[i][j];\n                    ops.push_back(\"-\" + to_string(d));\n                    load -= d;\n                    grid[i][j] = 0;\n                }\n            }\n            if (i < N-1) {\n                ops.push_back(\"D\");\n                x++;\n            }\n        }\n    }\n\n    for (const string& op : ops) {\n        cout << op << endl;\n    }\n\n    return 0;\n}","ahc035":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <set>\n#include <utility>\n\nusing namespace std;\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    const int fixed_N = 6;\n    vector<vector<int>> weight(fixed_N, vector<int>(fixed_N, 0));\n    vector<pair<int, int>> all_positions;\n    vector<pair<int, int>> even_positions, odd_positions;\n\n    for (int i = 0; i < fixed_N; i++) {\n        for (int j = 0; j < fixed_N; j++) {\n            int w = 0;\n            if (i > 0) w++;\n            if (i < fixed_N-1) w++;\n            if (j > 0) w++;\n            if (j < fixed_N-1) w++;\n            weight[i][j] = w;\n            all_positions.push_back({i, j});\n            if ((i+j) % 2 == 0) {\n                even_positions.push_back({i, j});\n            } else {\n                odd_positions.push_back({i, j});\n            }\n        }\n    }\n\n    sort(even_positions.begin(), even_positions.end(), [&](const pair<int, int>& a, const pair<int, int>& b) {\n        return weight[a.first][a.second] > weight[b.first][b.second];\n    });\n    sort(odd_positions.begin(), odd_positions.end(), [&](const pair<int, int>& a, const pair<int, int>& b) {\n        return weight[a.first][a.second] > weight[b.first][b.second];\n    });\n\n    int N, M, T;\n    cin >> N >> M >> T;\n\n    const int seed_count = 2 * N * (N - 1);\n    vector<vector<int>> seeds(seed_count, vector<int>(M, 0));\n\n    for (int i = 0; i < seed_count; i++) {\n        for (int j = 0; j < M; j++) {\n            cin >> seeds[i][j];\n        }\n    }\n\n    for (int t = 0; t < T; t++) {\n        vector<int> current_max(M, -1);\n        for (int l = 0; l < M; l++) {\n            for (int k = 0; k < seed_count; k++) {\n                if (seeds[k][l] > current_max[l]) {\n                    current_max[l] = seeds[k][l];\n                }\n            }\n        }\n\n        vector<int> V(seed_count, 0);\n        for (int k = 0; k < seed_count; k++) {\n            for (int l = 0; l < M; l++) {\n                V[k] += seeds[k][l];\n            }\n        }\n\n        vector<vector<bool>> trait(seed_count, vector<bool>(M, false));\n        for (int k = 0; k < seed_count; k++) {\n            for (int l = 0; l < M; l++) {\n                if (seeds[k][l] == current_max[l]) {\n                    trait[k][l] = true;\n                }\n            }\n        }\n\n        vector<int> coverage_count(M, 0);\n        set<int> chosen_seeds;\n\n        while (true) {\n            bool all_covered = true;\n            for (int l = 0; l < M; l++) {\n                if (coverage_count[l] < 2) {\n                    all_covered = false;\n                    break;\n                }\n            }\n            if (all_covered) break;\n\n            int best_seed = -1;\n            int best_cover = -1;\n            int best_V = -1;\n\n            for (int k = 0; k < seed_count; k++) {\n                if (chosen_seeds.find(k) != chosen_seeds.end()) continue;\n\n                int count = 0;\n                for (int l = 0; l < M; l++) {\n                    if (trait[k][l] && coverage_count[l] < 2) {\n                        count++;\n                    }\n                }\n\n                if (count == 0) continue;\n\n                if (count > best_cover || (count == best_cover && V[k] > best_V)) {\n                    best_cover = count;\n                    best_V = V[k];\n                    best_seed = k;\n                }\n            }\n\n            if (best_seed == -1) break;\n\n            chosen_seeds.insert(best_seed);\n            for (int l = 0; l < M; l++) {\n                if (trait[best_seed][l]) {\n                    coverage_count[l]++;\n                }\n            }\n        }\n\n        if (chosen_seeds.size() < N * N) {\n            vector<pair<int, int>> candidates;\n            for (int k = 0; k < seed_count; k++) {\n                if (chosen_seeds.find(k) == chosen_seeds.end()) {\n                    candidates.push_back({V[k], k});\n                }\n            }\n            sort(candidates.begin(), candidates.end(), [](const pair<int, int>& a, const pair<int, int>& b) {\n                return a.first > b.first;\n            });\n            int rem = N * N - chosen_seeds.size();\n            for (int i = 0; i < rem; i++) {\n                chosen_seeds.insert(candidates[i].second);\n            }\n        }\n\n        int half1 = 8;\n        vector<pair<int, int>> seeds_with_signature;\n        for (int k : chosen_seeds) {\n            int score1 = 0, score2 = 0;\n            for (int l = 0; l < half1; l++) {\n                if (trait[k][l]) score1++;\n            }\n            for (int l = half1; l < M; l++) {\n                if (trait[k][l]) score2++;\n            }\n            seeds_with_signature.push_back({score1 - score2, k});\n        }\n\n        sort(seeds_with_signature.begin(), seeds_with_signature.end(), [](const pair<int, int>& a, const pair<int, int>& b) {\n            return a.first > b.first;\n        });\n\n        vector<int> groupA, groupB;\n        for (int i = 0; i < seeds_with_signature.size(); i++) {\n            if (i < 18) {\n                groupA.push_back(seeds_with_signature[i].second);\n            } else {\n                groupB.push_back(seeds_with_signature[i].second);\n            }\n        }\n\n        sort(groupA.begin(), groupA.end(), [&](int a, int b) { return V[a] > V[b]; });\n        sort(groupB.begin(), groupB.end(), [&](int a, int b) { return V[a] > V[b]; });\n\n        vector<vector<int>> grid(N, vector<int>(N, -1));\n        for (int idx = 0; idx < groupA.size(); idx++) {\n            int i = even_positions[idx].first;\n            int j = even_positions[idx].second;\n            grid[i][j] = groupA[idx];\n        }\n        for (int idx = 0; idx < groupB.size(); idx++) {\n            int i = odd_positions[idx].first;\n            int j = odd_positions[idx].second;\n            grid[i][j] = groupB[idx];\n        }\n\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                cout << grid[i][j];\n                if (j < N - 1) {\n                    cout << \" \";\n                } else {\n                    cout << endl;\n                }\n            }\n        }\n        cout.flush();\n\n        for (int i = 0; i < seed_count; i++) {\n            for (int j = 0; j < M; j++) {\n                cin >> seeds[i][j];\n            }\n        }\n    }\n\n    return 0;\n}","ahc038":"#include <iostream>\n#include <vector>\n#include <cstdlib>\n#include <algorithm>\n#include <climits>\n#include <tuple>\nusing namespace std;\n\nint main() {\n    int N, M, V;\n    cin >> N >> M >> V;\n\n    vector<string> s_grid(N), t_grid(N);\n    for (int i = 0; i < N; i++) {\n        cin >> s_grid[i];\n    }\n    for (int i = 0; i < N; i++) {\n        cin >> t_grid[i];\n    }\n\n    int L0 = (N - 1) / 2;\n\n    cout << V << endl;\n    for (int i = 1; i < V; i++) {\n        cout << \"0 \" << L0 << endl;\n    }\n\n    int rx0 = N / 2;\n    int ry0 = N / 2;\n    cout << rx0 << \" \" << ry0 << endl;\n\n    int cur_x = rx0, cur_y = ry0;\n    vector<int> dirs(V, 0);\n    vector<bool> holding(V, false);\n\n    vector<pair<int, int>> initial_list;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (s_grid[i][j] == '1') {\n                initial_list.push_back({i, j});\n            }\n        }\n    }\n\n    vector<pair<int, int>> target_list;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (t_grid[i][j] == '1') {\n                target_list.push_back({i, j});\n            }\n        }\n    }\n\n    vector<string> outputs;\n\n    while (!initial_list.empty()) {\n        int F_available = V - 1;\n        int batch_size = min(F_available, (int)initial_list.size());\n        vector<tuple<int, int, int, int, int, int>> pickup_batch;\n        vector<int> available_fingertips;\n        for (int i = 1; i < V; i++) {\n            available_fingertips.push_back(i);\n        }\n\n        int current_start_x = cur_x;\n        int current_start_y = cur_y;\n\n        for (int idx = 0; idx < batch_size; idx++) {\n            int best_cost = INT_MAX;\n            int best_i = -1, best_j = -1, best_cx = -1, best_cy = -1, best_d = -1, best_f = -1;\n            for (int k = 0; k < initial_list.size(); k++) {\n                int i = initial_list[k].first;\n                int j = initial_list[k].second;\n                vector<tuple<int, int, int>> candidates;\n                if (j - L0 >= 0) {\n                    candidates.push_back(make_tuple(i, j - L0, 0));\n                }\n                if (i - L0 >= 0) {\n                    candidates.push_back(make_tuple(i - L0, j, 1));\n                }\n                if (j + L0 < N) {\n                    candidates.push_back(make_tuple(i, j + L0, 2));\n                }\n                if (i + L0 < N) {\n                    candidates.push_back(make_tuple(i + L0, j, 3));\n                }\n                for (auto &cand : candidates) {\n                    int cx = get<0>(cand);\n                    int cy = get<1>(cand);\n                    int d = get<2>(cand);\n                    for (int f_index = 0; f_index < available_fingertips.size(); f_index++) {\n                        int f = available_fingertips[f_index];\n                        int dist = abs(cx - current_start_x) + abs(cy - current_start_y);\n                        int cur_dir = dirs[f];\n                        int diff = (d - cur_dir + 4) % 4;\n                        int rot_cost = min(diff, 4 - diff);\n                        int cost = dist + rot_cost;\n                        if (cost < best_cost) {\n                            best_cost = cost;\n                            best_i = i;\n                            best_j = j;\n                            best_cx = cx;\n                            best_cy = cy;\n                            best_d = d;\n                            best_f = f;\n                        }\n                    }\n                }\n            }\n            if (best_cost == INT_MAX) {\n                batch_size = idx;\n                break;\n            }\n\n            for (auto it = initial_list.begin(); it != initial_list.end(); it++) {\n                if (it->first == best_i && it->second == best_j) {\n                    initial_list.erase(it);\n                    break;\n                }\n            }\n\n            for (auto it = available_fingertips.begin(); it != available_fingertips.end(); it++) {\n                if (*it == best_f) {\n                    available_fingertips.erase(it);\n                    break;\n                }\n            }\n\n            pickup_batch.push_back(make_tuple(best_i, best_j, best_cx, best_cy, best_d, best_f));\n            current_start_x = best_cx;\n            current_start_y = best_cy;\n        }\n\n        for (auto &item : pickup_batch) {\n            int i = get<0>(item);\n            int j = get<1>(item);\n            int cx = get<2>(item);\n            int cy = get<3>(item);\n            int d = get<4>(item);\n            int f = get<5>(item);\n\n            int dx = cx - cur_x;\n            int dy = cy - cur_y;\n\n            vector<char> moves;\n            if (dx > 0) {\n                for (int k = 0; k < dx; k++) moves.push_back('R');\n            } else if (dx < 0) {\n                for (int k = 0; k < -dx; k++) moves.push_back('L');\n            }\n            if (dy > 0) {\n                for (int k = 0; k < dy; k++) moves.push_back('D');\n            } else if (dy < 0) {\n                for (int k = 0; k < -dy; k++) moves.push_back('U');\n            }\n\n            int cur_dir = dirs[f];\n            int diff = (d - cur_dir + 4) % 4;\n            vector<char> rots;\n            if (diff == 1) {\n                rots.push_back('R');\n            } else if (diff == 2) {\n                rots.push_back('R');\n                rots.push_back('R');\n            } else if (diff == 3) {\n                rots.push_back('L');\n            }\n\n            int T = max(moves.size(), rots.size());\n            for (int t = 0; t < T; t++) {\n                string op = \"\";\n                if (t < moves.size()) {\n                    op += moves[t];\n                } else {\n                    op += '.';\n                }\n\n                for (int v = 1; v < V; v++) {\n                    if (v == f && t < rots.size()) {\n                        op += rots[t];\n                    } else {\n                        op += '.';\n                    }\n                }\n\n                for (int v = 0; v < V; v++) {\n                    if (v == f && t == T - 1) {\n                        op += 'P';\n                        holding[f] = true;\n                    } else {\n                        op += '.';\n                    }\n                }\n                outputs.push_back(op);\n            }\n\n            cur_x = cx;\n            cur_y = cy;\n            dirs[f] = d;\n        }\n\n        vector<pair<int, int>> available_targets = target_list;\n        vector<tuple<int, int, int, int, int, int>> delivery_batch;\n        current_start_x = cur_x;\n        current_start_y = cur_y;\n\n        for (int idx = 0; idx < batch_size; idx++) {\n            int best_cost = INT_MAX;\n            int best_i = -1, best_j = -1, best_cx = -1, best_cy = -1, best_d = -1, best_f = -1;\n            for (int k = 0; k < available_targets.size(); k++) {\n                int i = available_targets[k].first;\n                int j = available_targets[k].second;\n                vector<tuple<int, int, int>> candidates;\n                if (j - L0 >= 0) {\n                    candidates.push_back(make_tuple(i, j - L0, 0));\n                }\n                if (i - L0 >= 0) {\n                    candidates.push_back(make_tuple(i - L0, j, 1));\n                }\n                if (j + L0 < N) {\n                    candidates.push_back(make_tuple(i, j + L0, 2));\n                }\n                if (i + L0 < N) {\n                    candidates.push_back(make_tuple(i + L0, j, 3));\n                }\n                for (auto &cand : candidates) {\n                    int cx = get<0>(cand);\n                    int cy = get<1>(cand);\n                    int d = get<2>(cand);\n                    for (auto &p_item : pickup_batch) {\n                        int f = get<5>(p_item);\n                        if (holding[f]) {\n                            int dist = abs(cx - current_start_x) + abs(cy - current_start_y);\n                            int cur_dir = dirs[f];\n                            int diff = (d - cur_dir + 4) % 4;\n                            int rot_cost = min(diff, 4 - diff);\n                            int cost = dist + rot_cost;\n                            if (cost < best_cost) {\n                                best_cost = cost;\n                                best_i = i;\n                                best_j = j;\n                                best_cx = cx;\n                                best_cy = cy;\n                                best_d = d;\n                                best_f = f;\n                            }\n                        }\n                    }\n                }\n            }\n            if (best_cost == INT_MAX) {\n                batch_size = idx;\n                break;\n            }\n\n            for (auto it = available_targets.begin(); it != available_targets.end(); it++) {\n                if (it->first == best_i && it->second == best_j) {\n                    available_targets.erase(it);\n                    break;\n                }\n            }\n\n            delivery_batch.push_back(make_tuple(best_i, best_j, best_cx, best_cy, best_d, best_f));\n            current_start_x = best_cx;\n            current_start_y = best_cy;\n        }\n\n        for (auto &item : delivery_batch) {\n            int i = get<0>(item);\n            int j = get<1>(item);\n            int cx = get<2>(item);\n            int cy = get<3>(item);\n            int d = get<4>(item);\n            int f = get<5>(item);\n\n            int dx = cx - cur_x;\n            int dy = cy - cur_y;\n\n            vector<char> moves;\n            if (dx > 0) {\n                for (int k = 0; k < dx; k++) moves.push_back('R');\n            } else if (dx < 0) {\n                for (int k = 0; k < -dx; k++) moves.push_back('L');\n            }\n            if (dy > 0) {\n                for (int k = 0; k < dy; k++) moves.push_back('D');\n            } else if (dy < 0) {\n                for (int k = 0; k < -dy; k++) moves.push_back('U');\n            }\n\n            int cur_dir = dirs[f];\n            int diff = (d - cur_dir + 4) % 4;\n            vector<char> rots;\n            if (diff == 1) {\n                rots.push_back('R');\n            } else if (diff == 2) {\n                rots.push_back('R');\n                rots.push_back('R');\n            } else if (diff == 3) {\n                rots.push_back('L');\n            }\n\n            int T = max(moves.size(), rots.size());\n            for (int t = 0; t < T; t++) {\n                string op = \"\";\n                if (t < moves.size()) {\n                    op += moves[t];\n                } else {\n                    op += '.';\n                }\n\n                for (int v = 1; v < V; v++) {\n                    if (v == f && t < rots.size()) {\n                        op += rots[t];\n                    } else {\n                        op += '.';\n                    }\n                }\n\n                for (int v = 0; v < V; v++) {\n                    if (v == f && t == T - 1) {\n                        op += 'P';\n                        holding[f] = false;\n                    } else {\n                        op += '.';\n                    }\n                }\n                outputs.push_back(op);\n            }\n\n            cur_x = cx;\n            cur_y = cy;\n            dirs[f] = d;\n        }\n\n        for (auto &item : delivery_batch) {\n            int i = get<0>(item);\n            int j = get<1>(item);\n            for (auto it = target_list.begin(); it != target_list.end(); it++) {\n                if (it->first == i && it->second == j) {\n                    target_list.erase(it);\n                    break;\n                }\n            }\n        }\n    }\n\n    for (string &s : outputs) {\n        cout << s << endl;\n    }\n\n    return 0;\n}","ahc039":"#include <iostream>\n#include <vector>\n#include <set>\n#include <algorithm>\n#include <cctype>\n#include <cmath>\n#include <queue>\n#include <map>\n#include <unordered_set>\nusing namespace std;\n\nstruct Point {\n    int x, y;\n    Point(int x=0, int y=0) : x(x), y(y) {}\n};\n\nconst int GRID = 15;\n\nint main() {\n    int N;\n    cin >> N;\n    vector<Point> mackerels;\n    vector<Point> sardines;\n    for (int i = 0; i < 2*N; i++) {\n        int x, y;\n        cin >> x >> y;\n        if (i < N) {\n            mackerels.push_back(Point(x, y));\n        } else {\n            sardines.push_back(Point(x, y));\n        }\n    }\n\n    set<pair<int, int>> sardine_set;\n    for (auto& p : sardines) {\n        sardine_set.insert({p.x, p.y});\n    }\n\n    vector<int> x_grid(GRID+1);\n    vector<int> y_grid(GRID+1);\n    for (int i = 0; i <= GRID; i++) {\n        x_grid[i] = (long long)i * 100000 / GRID;\n        y_grid[i] = (long long)i * 100000 / GRID;\n    }\n    x_grid[GRID] = 100000;\n    y_grid[GRID] = 100000;\n\n    vector<vector<int>> grid_m(GRID, vector<int>(GRID, 0));\n    vector<vector<int>> grid_s(GRID, vector<int>(GRID, 0));\n    vector<vector<int>> grid_count(GRID, vector<int>(GRID, 0));\n\n    for (auto& p : mackerels) {\n        int i = lower_bound(x_grid.begin(), x_grid.end(), p.x + 1) - x_grid.begin() - 1;\n        int j = lower_bound(y_grid.begin(), y_grid.end(), p.y + 1) - y_grid.begin() - 1;\n        if (i < 0) i = 0;\n        if (i >= GRID) i = GRID-1;\n        if (j < 0) j = 0;\n        if (j >= GRID) j = GRID-1;\n        grid_m[i][j]++;\n    }\n\n    for (auto& p : sardines) {\n        int i = lower_bound(x_grid.begin(), x_grid.end(), p.x + 1) - x_grid.begin() - 1;\n        int j = lower_bound(y_grid.begin(), y_grid.end(), p.y + 1) - y_grid.begin() - 1;\n        if (i < 0) i = 0;\n        if (i >= GRID) i = GRID-1;\n        if (j < 0) j = 0;\n        if (j >= GRID) j = GRID-1;\n        grid_s[i][j]++;\n    }\n\n    for (int i = 0; i < GRID; i++) {\n        for (int j = 0; j < GRID; j++) {\n            grid_count[i][j] = grid_m[i][j] - grid_s[i][j];\n        }\n    }\n\n    int best_score = -1000000;\n    int best_i = -1, best_j = -1;\n    for (int i = 0; i < GRID; i++) {\n        for (int j = 0; j < GRID; j++) {\n            if (grid_count[i][j] > best_score) {\n                best_score = grid_count[i][j];\n                best_i = i;\n                best_j = j;\n            }\n        }\n    }\n\n    if (best_score <= 0) {\n        int best_cnt = 10;\n        Point best_point;\n        bool found = false;\n        for (auto& p : mackerels) {\n            int cnt = 0;\n            for (int dx = -1; dx <= 1; dx++) {\n                for (int dy = -1; dy <= 1; dy++) {\n                    int x1 = p.x + dx;\n                    int y1 = p.y + dy;\n                    if (x1 < 0 || x1 > 100000 || y1 < 0 || y1 > 100000) continue;\n                    if (sardine_set.find({x1, y1}) != sardine_set.end()) {\n                        cnt++;\n                    }\n                }\n            }\n            if (cnt < best_cnt) {\n                best_cnt = cnt;\n                best_point = p;\n                found = true;\n            }\n        }\n        if (!found) {\n            best_point = mackerels[0];\n        }\n        int x0 = best_point.x;\n        int y0 = best_point.y;\n        vector<Point> poly;\n        poly.push_back(Point(x0-1, y0-1));\n        poly.push_back(Point(x0-1, y0+1));\n        poly.push_back(Point(x0+1, y0+1));\n        poly.push_back(Point(x0+1, y0-1));\n        cout << 4 << \"\\n\";\n        for (auto& p : poly) {\n            cout << p.x << \" \" << p.y << \"\\n\";\n        }\n        return 0;\n    }\n\n    set<pair<int, int>> R;\n    set<pair<int, int>> frontier;\n    R.insert({best_i, best_j});\n    int total_perimeter = 0;\n    const int dx4[4] = {0, 1, 0, -1};\n    const int dy4[4] = {1, 0, -1, 0};\n\n    int dx_cell = x_grid[best_i+1] - x_grid[best_i];\n    int dy_cell = y_grid[best_j+1] - y_grid[best_j];\n    total_perimeter = 2 * (dx_cell + dy_cell);\n\n    for (int d = 0; d < 4; d++) {\n        int ni = best_i + dx4[d];\n        int nj = best_j + dy4[d];\n        if (ni < 0 || ni >= GRID || nj < 0 || nj >= GRID) continue;\n        if (R.find({ni, nj}) == R.end()) {\n            frontier.insert({ni, nj});\n        }\n    }\n\n    while (!frontier.empty()) {\n        int best_cell_i = -1, best_cell_j = -1;\n        int best_cell_score = -1000000;\n        int best_change_perimeter = 0;\n        vector<pair<int, int>> to_remove;\n        for (auto cell : frontier) {\n            int i = cell.first;\n            int j = cell.second;\n            if (R.find(cell) != R.end()) {\n                to_remove.push_back(cell);\n                continue;\n            }\n            int dx_local = x_grid[i+1] - x_grid[i];\n            int dy_local = y_grid[j+1] - y_grid[j];\n            int change = 0;\n            for (int d = 0; d < 4; d++) {\n                int ni = i + dx4[d];\n                int nj = j + dy4[d];\n                if (ni < 0 || ni >= GRID || nj < 0 || nj >= GRID) {\n                    if (d == 0) change += dy_local;\n                    else if (d == 1) change += dx_local;\n                    else if (d == 2) change += dy_local;\n                    else if (d == 3) change += dx_local;\n                    continue;\n                }\n                if (R.find({ni, nj}) != R.end()) {\n                    if (d == 0) change -= dy_local;\n                    else if (d == 1) change -= dx_local;\n                    else if (d == 2) change -= dy_local;\n                    else if (d == 3) change -= dx_local;\n                } else {\n                    if (d == 0) change += dy_local;\n                    else if (d == 1) change += dx_local;\n                    else if (d == 2) change += dy_local;\n                    else if (d == 3) change += dx_local;\n                }\n            }\n            int new_perimeter = total_perimeter + change;\n            if (new_perimeter > 400000) continue;\n            if (grid_count[i][j] > best_cell_score) {\n                best_cell_score = grid_count[i][j];\n                best_cell_i = i;\n                best_cell_j = j;\n                best_change_perimeter = change;\n            }\n        }\n        for (auto& cell : to_remove) {\n            frontier.erase(cell);\n        }\n        if (best_cell_score <= 0) {\n            break;\n        }\n        if (best_cell_i == -1) {\n            break;\n        }\n        R.insert({best_cell_i, best_cell_j});\n        total_perimeter += best_change_perimeter;\n        frontier.erase({best_cell_i, best_cell_j});\n        for (int d = 0; d < 4; d++) {\n            int ni = best_cell_i + dx4[d];\n            int nj = best_cell_j + dy4[d];\n            if (ni < 0 || ni >= GRID || nj < 0 || nj >= GRID) continue;\n            if (R.find({ni, nj}) == R.end()) {\n                frontier.insert({ni, nj});\n            }\n        }\n    }\n\n    int min_x = 100000, max_x = 0, min_y = 100000, max_y = 0;\n    for (auto cell : R) {\n        int i = cell.first;\n        int j = cell.second;\n        min_x = min(min_x, x_grid[i]);\n        max_x = max(max_x, x_grid[i+1]);\n        min_y = min(min_y, y_grid[j]);\n        max_y = max(max_y, y_grid[j+1]);\n    }\n    vector<Point> poly;\n    poly.push_back(Point(min_x, max_y));\n    poly.push_back(Point(max_x, max_y));\n    poly.push_back(Point(max_x, min_y));\n    poly.push_back(Point(min_x, min_y));\n    cout << 4 << \"\\n\";\n    for (auto& p : poly) {\n        cout << p.x << \" \" << p.y << \"\\n\";\n    }\n\n    return 0;\n}","ahc040":"#include <iostream>\n#include <vector>\n#include <set>\n#include <map>\n#include <algorithm>\n#include <cmath>\n#include <cctype>\n#include <tuple>\n#include <iomanip>\n#include <climits>\n#include <random>\n#include <chrono>\n\nusing namespace std;\n\nset<int> get_reference_set(int strategy, const vector<int>& placed_list, const vector<tuple<long long, long long, long long, long long>>& rect_pos) {\n    set<int> refs;\n    refs.insert(-1);\n    if (placed_list.empty()) {\n        return refs;\n    }\n    if (strategy == 0) {\n        refs.insert(placed_list.back());\n    } else if (strategy == 1) {\n        int count = 0;\n        for (auto it = placed_list.rbegin(); it != placed_list.rend() && count < 5; it++) {\n            refs.insert(*it);\n            count++;\n        }\n    } else if (strategy == 2) {\n        vector<pair<long long, int>> areas;\n        for (int j : placed_list) {\n            auto [x, y, w, h] = rect_pos[j];\n            areas.push_back({w * h, j});\n        }\n        sort(areas.rbegin(), areas.rend());\n        for (int i = 0; i < min(5, (int)areas.size()); i++) {\n            refs.insert(areas[i].second);\n        }\n    } else if (strategy == 3) {\n        refs.insert(placed_list.back());\n        long long max_area = 0;\n        int best_j = -1;\n        for (int j : placed_list) {\n            auto [x, y, w, h] = rect_pos[j];\n            long long area = w * (long long)h;\n            if (area > max_area) {\n                max_area = area;\n                best_j = j;\n            }\n        }\n        if (best_j != -1) {\n            refs.insert(best_j);\n        }\n    }\n    return refs;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, T;\n    double sigma;\n    cin >> N >> T >> sigma;\n\n    vector<double> w_est(N);\n    vector<double> h_est(N);\n    vector<double> w_sum(N, 0.0);\n    vector<double> h_sum(N, 0.0);\n    vector<int> count(N, 0);\n\n    for (int i = 0; i < N; i++) {\n        cin >> w_est[i] >> h_est[i];\n    }\n\n    int exploration_turns = min(N, T);\n    vector<int> exploration_order(N);\n    for (int i = 0; i < N; i++) {\n        exploration_order[i] = i;\n    }\n    sort(exploration_order.begin(), exploration_order.end(), [&](int i, int j) {\n        return (w_est[i] * h_est[i]) > (w_est[j] * h_est[j]);\n    });\n\n    for (int turn = 0; turn < exploration_turns; turn++) {\n        int idx = exploration_order[turn % N];\n        cout << 1 << '\\n';\n        cout << idx << \" \" << 0 << \" \" << \"U\" << \" \" << -1 << '\\n';\n        cout.flush();\n\n        long long Wp, Hp;\n        cin >> Wp >> Hp;\n\n        w_sum[idx] += Wp;\n        h_sum[idx] += Hp;\n        count[idx]++;\n    }\n\n    for (int i = 0; i < N; i++) {\n        if (count[i] > 0) {\n            w_est[i] = w_sum[i] / count[i];\n            h_est[i] = h_sum[i] / count[i];\n        }\n    }\n\n    int packing_turns = T - exploration_turns;\n    packing_turns = min(packing_turns, 500);\n\n    vector<long long> w_sim(N), h_sim(N);\n    for (int i = 0; i < N; i++) {\n        w_sim[i] = static_cast<long long>(round(w_est[i]));\n        h_sim[i] = static_cast<long long>(round(h_est[i]));\n        if (w_sim[i] < 1) w_sim[i] = 1;\n        if (w_sim[i] > 1000000000) w_sim[i] = 1000000000;\n        if (h_sim[i] < 1) h_sim[i] = 1;\n        if (h_sim[i] > 1000000000) h_sim[i] = 1000000000;\n    }\n\n    vector<tuple<vector<tuple<int, int, char, int>>, long long>> candidates;\n    if (packing_turns > 0) {\n        auto rng = mt19937(chrono::steady_clock::now().time_since_epoch().count());\n        uniform_real_distribution<double> dist_th(0.5, 2.0);\n        uniform_int_distribution<int> dist_ref(0, 3);\n\n        for (int cand_idx = 0; cand_idx < packing_turns; cand_idx++) {\n            double threshold = dist_th(rng);\n            int ref_strategy = dist_ref(rng);\n\n            vector<tuple<int, int, char, int>> sequence;\n            vector<bool> placed(N, false);\n            vector<tuple<long long, long long, long long, long long>> rect_pos(N);\n            vector<int> placed_list;\n            long long current_max_x = 0;\n            long long current_max_y = 0;\n\n            for (int idx = 0; idx < N; idx++) {\n                set<int> refs = get_reference_set(ref_strategy, placed_list, rect_pos);\n                vector<tuple<long long, int, char, int, long long, long long, long long, long long>> candidates_for_rect;\n\n                for (int rot = 0; rot < 2; rot++) {\n                    long long w = (rot == 0) ? w_sim[idx] : h_sim[idx];\n                    long long h = (rot == 0) ? h_sim[idx] : w_sim[idx];\n                    for (char d : {'U', 'L'}) {\n                        for (int b_ref : refs) {\n                            long long x0 = 0, y0 = 0;\n                            if (d == 'U') {\n                                if (b_ref == -1) {\n                                    x0 = 0;\n                                } else {\n                                    auto [x_pl, y_pl, w_pl, h_pl] = rect_pos[b_ref];\n                                    x0 = x_pl + w_pl;\n                                }\n                                for (int j : placed_list) {\n                                    auto [x_pl, y_pl, w_pl, h_pl] = rect_pos[j];\n                                    if (x0 < x_pl + w_pl && x0 + w > x_pl) {\n                                        y0 = max(y0, y_pl + h_pl);\n                                    }\n                                }\n                            } else {\n                                if (b_ref == -1) {\n                                    y0 = 0;\n                                } else {\n                                    auto [x_pl, y_pl, w_pl, h_pl] = rect_pos[b_ref];\n                                    y0 = y_pl + h_pl;\n                                }\n                                for (int j : placed_list) {\n                                    auto [x_pl, y_pl, w_pl, h_pl] = rect_pos[j];\n                                    if (y0 < y_pl + h_pl && y0 + h > y_pl) {\n                                        x0 = max(x0, x_pl + w_pl);\n                                    }\n                                }\n                            }\n\n                            long long new_max_x = max(current_max_x, x0 + w);\n                            long long new_max_y = max(current_max_y, y0 + h);\n                            long long increase = (new_max_x + new_max_y) - (current_max_x + current_max_y);\n                            candidates_for_rect.push_back(make_tuple(increase, rot, d, b_ref, new_max_x, new_max_y, x0, y0));\n                        }\n                    }\n                }\n\n                if (candidates_for_rect.empty()) {\n                    continue;\n                }\n\n                sort(candidates_for_rect.begin(), candidates_for_rect.end());\n                auto [min_inc, rot_choice, d_choice, b_ref_choice, new_max_x, new_max_y, x0, y0] = candidates_for_rect[0];\n\n                if (min_inc <= threshold * (w_sim[idx] + h_sim[idx])) {\n                    placed[idx] = true;\n                    placed_list.push_back(idx);\n                    long long w_used = (rot_choice == 0) ? w_sim[idx] : h_sim[idx];\n                    long long h_used = (rot_choice == 0) ? h_sim[idx] : w_sim[idx];\n                    rect_pos[idx] = make_tuple(x0, y0, w_used, h_used);\n                    current_max_x = new_max_x;\n                    current_max_y = new_max_y;\n                    sequence.push_back(make_tuple(idx, rot_choice, d_choice, b_ref_choice));\n                }\n            }\n\n            long long sim_score = current_max_x + current_max_y;\n            for (int i = 0; i < N; i++) {\n                if (!placed[i]) {\n                    sim_score += w_sim[i] + h_sim[i];\n                }\n            }\n            candidates.push_back(make_tuple(sequence, sim_score));\n        }\n\n        sort(candidates.begin(), candidates.end(), [](const auto& a, const auto& b) {\n            return get<1>(a) < get<1>(b);\n        });\n    }\n\n    for (int turn = exploration_turns; turn < T; turn++) {\n        int cand_idx = turn - exploration_turns;\n        if (cand_idx < candidates.size()) {\n            auto& [seq, score] = candidates[cand_idx];\n            cout << seq.size() << '\\n';\n            for (auto [p, r, d, b] : seq) {\n                cout << p << \" \" << r << \" \" << d << \" \" << b << '\\n';\n            }\n        } else {\n            cout << 0 << '\\n';\n        }\n        cout.flush();\n\n        long long Wp, Hp;\n        cin >> Wp >> Hp;\n    }\n\n    return 0;\n}","ahc041":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\n#include <utility>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, H;\n    cin >> N >> M >> H;\n\n    vector<int> A(N);\n    for (int i = 0; i < N; i++) {\n        cin >> A[i];\n    }\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    vector<int> x(N), y(N);\n    for (int i = 0; i < N; i++) {\n        cin >> x[i] >> y[i];\n    }\n\n    vector<int> depth(N, -1);\n    vector<int> parent(N, -1);\n    vector<bool> finalized(N, false);\n\n    auto cmp = greater<pair<int, int>>();\n    priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(cmp)> unassigned_pq(cmp);\n    priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(cmp)> bfs_pq(cmp);\n\n    for (int i = 0; i < N; i++) {\n        unassigned_pq.push({A[i], i});\n    }\n\n    int finalized_count = 0;\n\n    while (finalized_count < N) {\n        int v = -1;\n        if (!bfs_pq.empty()) {\n            while (!bfs_pq.empty() && v == -1) {\n                auto [a_val, node] = bfs_pq.top();\n                bfs_pq.pop();\n                if (!finalized[node]) {\n                    v = node;\n                }\n            }\n        }\n        if (v == -1) {\n            while (!unassigned_pq.empty() && v == -1) {\n                auto [a_val, node] = unassigned_pq.top();\n                unassigned_pq.pop();\n                if (!finalized[node]) {\n                    v = node;\n                }\n            }\n        }\n        if (v == -1) break;\n\n        finalized[v] = true;\n        finalized_count++;\n\n        if (depth[v] == -1) {\n            depth[v] = 0;\n            parent[v] = -1;\n        }\n\n        if (depth[v] < H) {\n            for (int w : adj[v]) {\n                if (finalized[w]) continue;\n                int new_depth = depth[v] + 1;\n                if (depth[w] == -1 || new_depth > depth[w]) {\n                    depth[w] = new_depth;\n                    parent[w] = v;\n                    bfs_pq.push({A[w], w});\n                }\n            }\n        }\n    }\n\n    for (int i = 0; i < N; i++) {\n        if (i > 0) cout << \" \";\n        cout << parent[i];\n    }\n    cout << endl;\n\n    return 0;\n}","ahc042":"#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\nint main() {\n    const int N = 20;\n    vector<string> board(N);\n    for (int i = 0; i < N; i++) {\n        cin >> board[i];\n    }\n\n    vector<pair<int, int>> onis;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (board[i][j] == 'x') {\n                onis.push_back({i, j});\n            }\n        }\n    }\n\n    vector<pair<char, int>> ans;\n\n    for (auto& oni : onis) {\n        int i = oni.first;\n        int j = oni.second;\n        vector<pair<int, char>> options;\n\n        bool valid = true;\n        for (int k = 0; k < i; k++) {\n            if (board[k][j] == 'o') {\n                valid = false;\n                break;\n            }\n        }\n        if (valid) {\n            options.push_back({2 * (i + 1), 'U'});\n        }\n\n        valid = true;\n        for (int k = i + 1; k < N; k++) {\n            if (board[k][j] == 'o') {\n                valid = false;\n                break;\n            }\n        }\n        if (valid) {\n            options.push_back({2 * (N - i), 'D'});\n        }\n\n        valid = true;\n        for (int k = 0; k < j; k++) {\n            if (board[i][k] == 'o') {\n                valid = false;\n                break;\n            }\n        }\n        if (valid) {\n            options.push_back({2 * (j + 1), 'L'});\n        }\n\n        valid = true;\n        for (int k = j + 1; k < N; k++) {\n            if (board[i][k] == 'o') {\n                valid = false;\n                break;\n            }\n        }\n        if (valid) {\n            options.push_back({2 * (N - j), 'R'});\n        }\n\n        if (options.empty()) {\n            vector<pair<int, char>> all_options = {\n                {2*(i+1), 'U'},\n                {2*(N-i), 'D'},\n                {2*(j+1), 'L'},\n                {2*(N-j), 'R'}\n            };\n            sort(all_options.begin(), all_options.end());\n            char chosen_dir = all_options[0].second;\n            if (chosen_dir == 'U') {\n                for (int k = 0; k <= i; k++) ans.push_back({'U', j});\n                for (int k = 0; k <= i; k++) ans.push_back({'D', j});\n            } else if (chosen_dir == 'D') {\n                for (int k = 0; k < N - i; k++) ans.push_back({'D', j});\n                for (int k = 0; k < N - i; k++) ans.push_back({'U', j});\n            } else if (chosen_dir == 'L') {\n                for (int k = 0; k <= j; k++) ans.push_back({'L', i});\n                for (int k = 0; k <= j; k++) ans.push_back({'R', i});\n            } else if (chosen_dir == 'R') {\n                for (int k = 0; k < N - j; k++) ans.push_back({'R', i});\n                for (int k = 0; k < N - j; k++) ans.push_back({'L', i});\n            }\n        } else {\n            sort(options.begin(), options.end());\n            char chosen_dir = options[0].second;\n            if (chosen_dir == 'U') {\n                for (int k = 0; k <= i; k++) ans.push_back({'U', j});\n                for (int k = 0; k <= i; k++) ans.push_back({'D', j});\n            } else if (chosen_dir == 'D') {\n                for (int k = 0; k < N - i; k++) ans.push_back({'D', j});\n                for (int k = 0; k < N - i; k++) ans.push_back({'U', j});\n            } else if (chosen_dir == 'L') {\n                for (int k = 0; k <= j; k++) ans.push_back({'L', i});\n                for (int k = 0; k <= j; k++) ans.push_back({'R', i});\n            } else if (chosen_dir == 'R') {\n                for (int k = 0; k < N - j; k++) ans.push_back({'R', i});\n                for (int k = 0; k < N - j; k++) ans.push_back({'L', i});\n            }\n        }\n    }\n\n    for (auto& move : ans) {\n        cout << move.first << \" \" << move.second << '\\n';\n    }\n\n    return 0;\n}","ahc044":"#include <iostream>\n#include <vector>\n#include <random>\n#include <cmath>\n#include <algorithm>\nusing namespace std;\n\nlong long simulate(const vector<int>& a, const vector<int>& b, const vector<int>& T, int L, int N) {\n    int total_count[100] = {0};\n    char parity[100] = {0};\n    int next_table[2][100];\n\n    for (int i = 0; i < N; i++) {\n        next_table[0][i] = b[i];\n        next_table[1][i] = a[i];\n    }\n\n    total_count[0] = 1;\n    parity[0] = 1;\n    int current = 0;\n\n    for (int week = 2; week <= L; week++) {\n        int p = parity[current];\n        int next_emp = next_table[p][current];\n        total_count[next_emp]++;\n        parity[next_emp] ^= 1;\n        current = next_emp;\n    }\n\n    long long error = 0;\n    for (int i = 0; i < N; i++) {\n        error += abs(total_count[i] - T[i]);\n    }\n    return error;\n}\n\nint main() {\n    int N, L;\n    cin >> N >> L;\n    vector<int> T(N);\n    for (int i = 0; i < N; i++) {\n        cin >> T[i];\n    }\n\n    random_device rd;\n    mt19937 rng(rd());\n    vector<int> a(N), b(N);\n    uniform_int_distribution<int> emp_dist(0, N-1);\n    for (int i = 0; i < N; i++) {\n        a[i] = emp_dist(rng);\n        b[i] = emp_dist(rng);\n    }\n\n    long long current_error = simulate(a, b, T, L, N);\n    vector<int> best_a = a;\n    vector<int> best_b = b;\n    long long best_error = current_error;\n\n    int iterations = 800;\n    double max_temp = 10000.0;\n    double min_temp = 1.0;\n    uniform_real_distribution<double> real_dist(0.0, 1.0);\n    uniform_int_distribution<int> bit_dist(0, 1);\n    uniform_int_distribution<int> count_dist(1, 10);\n\n    for (int iter = 0; iter < iterations; iter++) {\n        double temp = max_temp * pow(min_temp / max_temp, static_cast<double>(iter) / iterations);\n        vector<int> new_a = a;\n        vector<int> new_b = b;\n\n        int change_count = count_dist(rng) == 1 ? 2 : 1;\n        for (int c = 0; c < change_count; c++) {\n            int i = emp_dist(rng);\n            int which = bit_dist(rng);\n            int old_val;\n            int new_val;\n            if (which == 0) {\n                old_val = new_a[i];\n                do {\n                    new_val = emp_dist(rng);\n                } while (new_val == old_val);\n                new_a[i] = new_val;\n            } else {\n                old_val = new_b[i];\n                do {\n                    new_val = emp_dist(rng);\n                } while (new_val == old_val);\n                new_b[i] = new_val;\n            }\n        }\n\n        long long new_error = simulate(new_a, new_b, T, L, N);\n\n        if (new_error < current_error || \n            real_dist(rng) < exp((current_error - new_error) / temp)) {\n            a = new_a;\n            b = new_b;\n            current_error = new_error;\n            if (new_error < best_error) {\n                best_error = new_error;\n                best_a = a;\n                best_b = b;\n            }\n        }\n    }\n\n    for (int i = 0; i < N; i++) {\n        cout << best_a[i] << \" \" << best_b[i] << endl;\n    }\n\n    return 0;\n}","ahc045":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <cstdint>\nusing namespace std;\n\nuint32_t xy2d(uint32_t n, uint32_t x, uint32_t y) {\n    uint32_t rx, ry, s, d = 0;\n    for (s = n / 2; s > 0; s /= 2) {\n        rx = (x & s) > 0;\n        ry = (y & s) > 0;\n        d += s * s * ((3 * rx) ^ ry);\n        if (ry == 0) {\n            if (rx == 1) {\n                x = s - 1 - x;\n                y = s - 1 - y;\n            }\n            swap(x, y);\n        }\n    }\n    return d;\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, Q_max, L, W;\n    cin >> N >> M >> Q_max >> L >> W;\n    vector<int> G(M);\n    for (int i = 0; i < M; i++) {\n        cin >> G[i];\n    }\n\n    vector<int> lx(N), rx(N), ly(N), ry(N);\n    vector<int> cx(N), cy(N);\n    for (int i = 0; i < N; i++) {\n        cin >> lx[i] >> rx[i] >> ly[i] >> ry[i];\n        cx[i] = (lx[i] + rx[i]) / 2;\n        cy[i] = (ly[i] + ry[i]) / 2;\n    }\n\n    const uint32_t n_hilbert = 1 << 16;\n    const long long scale_val = (1LL << 16) - 1;\n    vector<uint32_t> hilbert_index(N);\n    for (int i = 0; i < N; i++) {\n        long long nx_val = static_cast<long long>(cx[i]) * scale_val;\n        long long ny_val = static_cast<long long>(cy[i]) * scale_val;\n        uint32_t nx = static_cast<uint32_t>(nx_val / 10000);\n        uint32_t ny = static_cast<uint32_t>(ny_val / 10000);\n        hilbert_index[i] = xy2d(n_hilbert, nx, ny);\n    }\n\n    vector<int> cities(N);\n    for (int i = 0; i < N; i++) cities[i] = i;\n    sort(cities.begin(), cities.end(), [&](int i, int j) {\n        return hilbert_index[i] < hilbert_index[j];\n    });\n\n    vector<vector<int>> groups(M);\n    int idx = 0;\n    for (int k = 0; k < M; k++) {\n        groups[k].resize(G[k]);\n        for (int j = 0; j < G[k]; j++) {\n            groups[k][j] = cities[idx++];\n        }\n    }\n\n    int query_count = 0;\n    vector<vector<pair<int, int>>> group_edges(M);\n\n    auto query_cluster = [](const vector<int>& cluster) {\n        vector<int> sorted_cluster = cluster;\n        sort(sorted_cluster.begin(), sorted_cluster.end());\n        cout << \"? \" << sorted_cluster.size();\n        for (int c : sorted_cluster) {\n            cout << \" \" << c;\n        }\n        cout << endl;\n        cout.flush();\n\n        vector<pair<int, int>> edges;\n        for (int i = 0; i < sorted_cluster.size() - 1; i++) {\n            int a, b;\n            cin >> a >> b;\n            edges.push_back({a, b});\n        }\n        return edges;\n    };\n\n    for (int k = 0; k < M; k++) {\n        vector<int>& group = groups[k];\n        int n = group.size();\n        if (n == 1) {\n            continue;\n        }\n\n        vector<pair<int, int>> edges;\n        int i = 0;\n        while (i < n && query_count < Q_max) {\n            int take = min(L, n - i);\n            if (take < 2) break;\n\n            vector<int> cluster;\n            for (int j = 0; j < take; j++) {\n                cluster.push_back(group[i + j]);\n            }\n\n            vector<pair<int, int>> e = query_cluster(cluster);\n            query_count++;\n            for (const auto& edge : e) {\n                edges.push_back(edge);\n            }\n\n            i += (take - 1);\n        }\n\n        if (i < n - 1) {\n            edges.push_back({group[i], group[i + 1]});\n            for (int j = i + 1; j < n - 1; j++) {\n                edges.push_back({group[j], group[j + 1]});\n            }\n        }\n\n        group_edges[k] = edges;\n    }\n\n    cout << \"!\" << endl;\n    for (int k = 0; k < M; k++) {\n        vector<int>& group = groups[k];\n        for (int j = 0; j < group.size(); j++) {\n            if (j > 0) cout << \" \";\n            cout << group[j];\n        }\n        cout << endl;\n\n        vector<pair<int, int>>& edges = group_edges[k];\n        for (const auto& edge : edges) {\n            cout << edge.first << \" \" << edge.second << endl;\n        }\n    }\n\n    return 0;\n}","ahc046":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int N, M;\n    cin >> N >> M;\n    vector<pair<int, int>> points;\n    for (int i = 0; i < M; i++) {\n        int a, b;\n        cin >> a >> b;\n        points.push_back({a, b});\n    }\n\n    vector<vector<bool>> blocks(N, vector<bool>(N, false));\n    map<char, pair<int, int>> dirs;\n    dirs['U'] = {-1, 0};\n    dirs['D'] = {1, 0};\n    dirs['L'] = {0, -1};\n    dirs['R'] = {0, 1};\n\n    auto is_blocked = [&](int i, int j) -> bool {\n        if (i < 0 || i >= N || j < 0 || j >= N)\n            return true;\n        return blocks[i][j];\n    };\n\n    vector<string> outputs;\n    int ci = points[0].first, cj = points[0].second;\n\n    for (int idx = 1; idx < M; idx++) {\n        int ti = points[idx].first, tj = points[idx].second;\n        while (ci != ti || cj != tj) {\n            int current_manhattan = abs(ci - ti) + abs(cj - tj);\n            double best_score = -1e9;\n            char best_act = ' ';\n            char best_dir = ' ';\n\n            for (char d : {'U', 'D', 'L', 'R'}) {\n                int dx = dirs[d].first, dy = dirs[d].second;\n                int ni = ci + dx;\n                int nj = cj + dy;\n\n                if (ni >= 0 && ni < N && nj >= 0 && nj < N) {\n                    if (!blocks[ni][nj]) {\n                        int new_manhattan = abs(ni - ti) + abs(nj - tj);\n                        int reduction = current_manhattan - new_manhattan;\n                        if (reduction > best_score) {\n                            best_score = reduction;\n                            best_act = 'M';\n                            best_dir = d;\n                        }\n\n                        int slide_steps = 1;\n                        int nni = ni, nnj = nj;\n                        while (true) {\n                            nni += dx;\n                            nnj += dy;\n                            if (nni < 0 || nni >= N || nnj < 0 || nnj >= N || blocks[nni][nnj]) {\n                                break;\n                            }\n                            slide_steps++;\n                        }\n                        int slide_i = ci + slide_steps * dx;\n                        int slide_j = cj + slide_steps * dy;\n                        int new_manhattan_slide = abs(slide_i - ti) + abs(slide_j - tj);\n                        int reduction_slide = current_manhattan - new_manhattan_slide;\n                        if (reduction_slide > best_score) {\n                            best_score = reduction_slide;\n                            best_act = 'S';\n                            best_dir = d;\n                        }\n                    } else {\n                        int new_manhattan = abs(ni - ti) + abs(nj - tj);\n                        int reduction = current_manhattan - new_manhattan;\n                        if (reduction > best_score) {\n                            best_score = reduction;\n                            best_act = 'M';\n                            best_dir = d;\n                        }\n\n                        int slide_steps = 1;\n                        int nni = ni, nnj = nj;\n                        while (true) {\n                            nni += dx;\n                            nnj += dy;\n                            if (nni < 0 || nni >= N || nnj < 0 || nnj >= N || blocks[nni][nnj]) {\n                                break;\n                            }\n                            slide_steps++;\n                        }\n                        int slide_i = ci + slide_steps * dx;\n                        int slide_j = cj + slide_steps * dy;\n                        int new_manhattan_slide = abs(slide_i - ti) + abs(slide_j - tj);\n                        int reduction_slide = current_manhattan - new_manhattan_slide;\n                        if (reduction_slide > best_score) {\n                            best_score = reduction_slide;\n                            best_act = 'S';\n                            best_dir = d;\n                        }\n                    }\n                }\n            }\n\n            if (best_score == -1e9) {\n                double best_alter_score = -1e9;\n                char best_alter_dir = 'U';\n                for (char d : {'U', 'D', 'L', 'R'}) {\n                    int dx = dirs[d].first, dy = dirs[d].second;\n                    int ni = ci + dx, nj = cj + dy;\n                    if (ni < 0 || ni >= N || nj < 0 || nj >= N) continue;\n                    int dist_block = abs(ni - ti) + abs(nj - tj);\n                    double alter_score = -dist_block;\n                    if (alter_score > best_alter_score) {\n                        best_alter_score = alter_score;\n                        best_alter_dir = d;\n                    }\n                }\n                best_act = 'A';\n                best_dir = best_alter_dir;\n            }\n\n            outputs.push_back(string(1, best_act) + \" \" + best_dir);\n            int dx = dirs[best_dir].first, dy = dirs[best_dir].second;\n            if (best_act == 'M') {\n                ci = ci + dx;\n                cj = cj + dy;\n            } else if (best_act == 'S') {\n                int steps = 0;\n                int ni = ci, nj = cj;\n                while (true) {\n                    ni += dx;\n                    nj += dy;\n                    if (ni < 0 || ni >= N || nj < 0 || nj >= N || blocks[ni][nj]) {\n                        ni -= dx;\n                        nj -= dy;\n                        break;\n                    }\n                    steps++;\n                }\n                ci = ni;\n                cj = nj;\n            } else if (best_act == 'A') {\n                int ni = ci + dx, nj = cj + dy;\n                blocks[ni][nj] = !blocks[ni][nj];\n            }\n        }\n    }\n\n    for (const string& s : outputs) {\n        cout << s << '\\n';\n    }\n\n    return 0;\n}"},"8":{"ahc001":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <cmath>\n#include <cstdlib>\n#include <iomanip>\nusing namespace std;\n\nstruct Rect {\n    int a, b, c, d;\n    long long area;\n    long long r_val;\n};\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int n;\n    cin >> n;\n    vector<Rect> rects;\n    for (int i = 0; i < n; i++) {\n        int x, y, r;\n        cin >> x >> y >> r;\n        rects.push_back({x, y, x+1, y+1, 1LL, (long long)r});\n    }\n\n    bool changed = true;\n    int pass_count = 0;\n    const int MAX_PASS = 100;\n\n    while (changed && pass_count < MAX_PASS) {\n        pass_count++;\n        changed = false;\n        vector<int> indices(n);\n        for (int i = 0; i < n; i++) {\n            indices[i] = i;\n        }\n\n        sort(indices.begin(), indices.end(), [&](int i, int j) {\n            double sat_i = (double)rects[i].area / rects[i].r_val;\n            double sat_j = (double)rects[j].area / rects[j].r_val;\n            return sat_i < sat_j;\n        });\n\n        for (int idx = 0; idx < n; idx++) {\n            int i = indices[idx];\n            if (rects[i].area >= rects[i].r_val) \n                continue;\n\n            double best_improvement = -1e9;\n            int best_dir = -1;\n            int best_d_use = 0;\n\n            for (int dir = 0; dir < 4; dir++) {\n                int d_max;\n                if (dir == 0) {\n                    d_max = rects[i].a;\n                } else if (dir == 1) {\n                    d_max = 10000 - rects[i].c;\n                } else if (dir == 2) {\n                    d_max = rects[i].b;\n                } else {\n                    d_max = 10000 - rects[i].d;\n                }\n\n                if (d_max <= 0) continue;\n\n                int side_length;\n                if (dir < 2) {\n                    side_length = rects[i].d - rects[i].b;\n                } else {\n                    side_length = rects[i].c - rects[i].a;\n                }\n                if (side_length <= 0) continue;\n\n                for (int j = 0; j < n; j++) {\n                    if (i == j) continue;\n                    if (dir == 0) {\n                        if (rects[i].b < rects[j].d && rects[j].b < rects[i].d) {\n                            if (rects[j].c <= rects[i].a) {\n                                d_max = min(d_max, rects[i].a - rects[j].c);\n                            }\n                        }\n                    } else if (dir == 1) {\n                        if (rects[i].b < rects[j].d && rects[j].b < rects[i].d) {\n                            if (rects[j].a >= rects[i].c) {\n                                d_max = min(d_max, rects[j].a - rects[i].c);\n                            }\n                        }\n                    } else if (dir == 2) {\n                        if (rects[i].a < rects[j].c && rects[j].a < rects[i].c) {\n                            if (rects[j].d <= rects[i].b) {\n                                d_max = min(d_max, rects[i].b - rects[j].d);\n                            }\n                        }\n                    } else if (dir == 3) {\n                        if (rects[i].a < rects[j].c && rects[j].a < rects[i].c) {\n                            if (rects[j].b >= rects[i].d) {\n                                d_max = min(d_max, rects[j].b - rects[i].d);\n                            }\n                        }\n                    }\n                }\n\n                if (d_max <= 0) continue;\n\n                double d0 = (double)(rects[i].r_val - rects[i].area) / side_length;\n                if (d0 < 1e-5) continue;\n\n                vector<int> cands;\n                int d1 = (int)floor(d0);\n                int d2 = (int)ceil(d0);\n                d1 = min(d_max, d1);\n                d2 = min(d_max, d2);\n                if (d1 > 0) cands.push_back(d1);\n                if (d2 > 0 && d2 != d1) cands.push_back(d2);\n\n                double best_improve_dir = -1e9;\n                int best_d_use_dir = -1;\n\n                for (int d_candidate : cands) {\n                    long long new_area = rects[i].area + (long long)d_candidate * side_length;\n                    double min_val = min(rects[i].r_val, rects[i].area);\n                    double max_val = max(rects[i].r_val, rects[i].area);\n                    double old_sat = 1.0 - pow(1.0 - min_val / max_val, 2);\n\n                    min_val = min(rects[i].r_val, new_area);\n                    max_val = max(rects[i].r_val, new_area);\n                    double new_sat = 1.0 - pow(1.0 - min_val / max_val, 2);\n\n                    double improvement = new_sat - old_sat;\n                    if (improvement > best_improve_dir) {\n                        best_improve_dir = improvement;\n                        best_d_use_dir = d_candidate;\n                    }\n                }\n\n                if (best_improve_dir > best_improvement) {\n                    best_improvement = best_improve_dir;\n                    best_dir = dir;\n                    best_d_use = best_d_use_dir;\n                }\n            }\n\n            if (best_improvement > 1e-8) {\n                if (best_dir == 0) {\n                    rects[i].a -= best_d_use;\n                    rects[i].area += (long long)best_d_use * (rects[i].d - rects[i].b);\n                } else if (best_dir == 1) {\n                    rects[i].c += best_d_use;\n                    rects[i].area += (long long)best_d_use * (rects[i].d - rects[i].b);\n                } else if (best_dir == 2) {\n                    rects[i].b -= best_d_use;\n                    rects[i].area += (long long)best_d_use * (rects[i].c - rects[i].a);\n                } else if (best_dir == 3) {\n                    rects[i].d += best_d_use;\n                    rects[i].area += (long long)best_d_use * (rects[i].c - rects[i].a);\n                }\n                changed = true;\n            }\n        }\n    }\n\n    for (int i = 0; i < n; i++) {\n        cout << rects[i].a << \" \" << rects[i].b << \" \" << rects[i].c << \" \" << rects[i].d << \"\\n\";\n    }\n\n    return 0;\n}","ahc002":"#include <iostream>\n#include <vector>\n#include <set>\n#include <algorithm>\n#include <tuple>\nusing namespace std;\n\nint main() {\n    int si, sj;\n    cin >> si >> sj;\n    \n    vector<vector<int>> t(50, vector<int>(50));\n    vector<vector<int>> p(50, vector<int>(50));\n    \n    for (int i = 0; i < 50; i++) {\n        for (int j = 0; j < 50; j++) {\n            cin >> t[i][j];\n        }\n    }\n    for (int i = 0; i < 50; i++) {\n        for (int j = 0; j < 50; j++) {\n            cin >> p[i][j];\n        }\n    }\n    \n    int M = 0;\n    for (int i = 0; i < 50; i++) {\n        for (int j = 0; j < 50; j++) {\n            M = max(M, t[i][j] + 1);\n        }\n    }\n    \n    vector<set<int>> adj_tiles(M);\n    const int dx[4] = {1, -1, 0, 0};\n    const int dy[4] = {0, 0, 1, -1};\n    \n    for (int i = 0; i < 50; i++) {\n        for (int j = 0; j < 50; j++) {\n            for (int d = 0; d < 4; d++) {\n                int ni = i + dx[d];\n                int nj = j + dy[d];\n                if (ni >= 0 && ni < 50 && nj >= 0 && nj < 50 && t[i][j] != t[ni][nj]) {\n                    adj_tiles[t[i][j]].insert(t[ni][nj]);\n                }\n            }\n        }\n    }\n    \n    vector<int> current_degree(M);\n    for (int i = 0; i < M; i++) {\n        current_degree[i] = adj_tiles[i].size();\n    }\n    \n    vector<bool> visited_tile(M, false);\n    int cx = si, cy = sj;\n    int start_tile = t[si][sj];\n    visited_tile[start_tile] = true;\n    \n    for (int adj : adj_tiles[start_tile]) {\n        if (!visited_tile[adj]) {\n            current_degree[adj]--;\n        }\n    }\n    \n    string path = \"\";\n    const char dir_char[4] = {'D', 'U', 'R', 'L'};\n    \n    while (true) {\n        vector<tuple<int, char, int, int>> candidates;\n        for (int d1 = 0; d1 < 4; d1++) {\n            int nx1 = cx + dx[d1];\n            int ny1 = cy + dy[d1];\n            if (nx1 < 0 || nx1 >= 50 || ny1 < 0 || ny1 >= 50) continue;\n            int tile1 = t[nx1][ny1];\n            if (visited_tile[tile1]) continue;\n            \n            int score1 = p[nx1][ny1];\n            int base_score = score1 + 120 * current_degree[tile1];\n            int best_next = 0;\n            \n            for (int d2 = 0; d2 < 4; d2++) {\n                int nx2 = nx1 + dx[d2];\n                int ny2 = ny1 + dy[d2];\n                if (nx2 < 0 || nx2 >= 50 || ny2 < 0 || ny2 >= 50) continue;\n                int tile2 = t[nx2][ny2];\n                if (tile2 == tile1 || visited_tile[tile2]) continue;\n                \n                int deg2 = max(0, current_degree[tile2] - 1);\n                int score2 = p[nx2][ny2] + 100 * deg2;\n                int best_third = 0;\n                \n                for (int d3 = 0; d3 < 4; d3++) {\n                    int nx3 = nx2 + dx[d3];\n                    int ny3 = ny2 + dy[d3];\n                    if (nx3 < 0 || nx3 >= 50 || ny3 < 0 || ny3 >= 50) continue;\n                    int tile3 = t[nx3][ny3];\n                    if (tile3 == tile1 || tile3 == tile2 || visited_tile[tile3]) continue;\n                    \n                    int deg3 = max(0, current_degree[tile3] - 1);\n                    int score3 = p[nx3][ny3] + 60 * deg3;\n                    if (score3 > best_third) {\n                        best_third = score3;\n                    }\n                }\n                \n                int candidate_score = score2 + best_third;\n                if (candidate_score > best_next) {\n                    best_next = candidate_score;\n                }\n            }\n            \n            int total_score = base_score + best_next;\n            candidates.push_back(make_tuple(total_score, dir_char[d1], nx1, ny1));\n        }\n        \n        if (candidates.empty()) break;\n        \n        auto best = *max_element(candidates.begin(), candidates.end(),\n            [](const auto& a, const auto& b) { return get<0>(a) < get<0>(b); });\n        \n        char dir = get<1>(best);\n        int nx1 = get<2>(best);\n        int ny1 = get<3>(best);\n        int tile1 = t[nx1][ny1];\n        \n        path += dir;\n        cx = nx1;\n        cy = ny1;\n        visited_tile[tile1] = true;\n        \n        for (int adj : adj_tiles[tile1]) {\n            if (!visited_tile[adj]) {\n                current_degree[adj]--;\n            }\n        }\n    }\n    \n    cout << path << endl;\n    return 0;\n}","ahc003":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\n#include <tuple>\n#include <cmath>\n\nusing namespace std;\n\nconst int N = 30;\nconst double INF = 1e18;\nconst double EXPLORE_CONST = 500.0;\n\nvector<vector<double>> h_est(N, vector<double>(N-1, 5000.0));\nvector<vector<double>> v_est(N-1, vector<double>(N, 5000.0));\nvector<vector<int>> count_h(N, vector<int>(N-1, 0));\nvector<vector<int>> count_v(N-1, vector<int>(N, 0));\n\nconst int dx[4] = {0, 1, 0, -1};\nconst int dy[4] = {1, 0, -1, 0};\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    for (int k = 0; k < 1000; k++) {\n        int si, sj, ti, tj;\n        cin >> si >> sj >> ti >> tj;\n\n        vector<vector<double>> dist(N, vector<double>(N, INF));\n        vector<vector<pair<int, int>>> prev(N, vector<pair<int, int>>(N, {-1, -1}));\n        priority_queue<tuple<double, int, int>, vector<tuple<double, int, int>>, greater<tuple<double, int, int>>> pq;\n\n        dist[si][sj] = 0;\n        pq.push({0, si, sj});\n\n        while (!pq.empty()) {\n            auto [d, i, j] = pq.top();\n            pq.pop();\n            if (d > dist[i][j]) continue;\n            if (i == ti && j == tj) break;\n\n            for (int dir = 0; dir < 4; dir++) {\n                int ni = i + dx[dir];\n                int nj = j + dy[dir];\n                if (ni < 0 || ni >= N || nj < 0 || nj >= N) continue;\n\n                double base_weight, bonus, weight;\n                if (dir == 0) {\n                    base_weight = h_est[i][j];\n                    bonus = EXPLORE_CONST * sqrt(log(k+2) / (count_h[i][j] + 1));\n                    weight = max(1000.0, base_weight - bonus);\n                } else if (dir == 1) {\n                    base_weight = v_est[i][j];\n                    bonus = EXPLORE_CONST * sqrt(log(k+2) / (count_v[i][j] + 1));\n                    weight = max(1000.0, base_weight - bonus);\n                } else if (dir == 2) {\n                    base_weight = h_est[i][j-1];\n                    bonus = EXPLORE_CONST * sqrt(log(k+2) / (count_h[i][j-1] + 1));\n                    weight = max(1000.0, base_weight - bonus);\n                } else {\n                    base_weight = v_est[i-1][j];\n                    bonus = EXPLORE_CONST * sqrt(log(k+2) / (count_v[i-1][j] + 1));\n                    weight = max(1000.0, base_weight - bonus);\n                }\n\n                double new_dist = dist[i][j] + weight;\n                if (new_dist < dist[ni][nj]) {\n                    dist[ni][nj] = new_dist;\n                    prev[ni][nj] = {i, j};\n                    pq.push({new_dist, ni, nj});\n                }\n            }\n        }\n\n        vector<char> moves;\n        int ci = ti, cj = tj;\n        while (ci != si || cj != sj) {\n            auto [pi, pj] = prev[ci][cj];\n            if (pi == ci) moves.push_back(pj < cj ? 'R' : 'L');\n            else moves.push_back(pi < ci ? 'D' : 'U');\n            ci = pi;\n            cj = pj;\n        }\n        reverse(moves.begin(), moves.end());\n        cout << string(moves.begin(), moves.end()) << endl;\n        cout.flush();\n\n        long long z_k;\n        cin >> z_k;\n\n        double estimated_sum = dist[ti][tj];\n        double relative_error = (z_k - estimated_sum) / estimated_sum;\n        int ci2 = si, cj2 = sj;\n        \n        for (char move : moves) {\n            double* w_est = nullptr;\n            int* cnt = nullptr;\n            if (move == 'R') {\n                w_est = &h_est[ci2][cj2];\n                cnt = &count_h[ci2][cj2];\n                cj2++;\n            } else if (move == 'D') {\n                w_est = &v_est[ci2][cj2];\n                cnt = &count_v[ci2][cj2];\n                ci2++;\n            } else if (move == 'L') {\n                w_est = &h_est[ci2][cj2-1];\n                cnt = &count_h[ci2][cj2-1];\n                cj2--;\n            } else {\n                w_est = &v_est[ci2-1][cj2];\n                cnt = &count_v[ci2-1][cj2];\n                ci2--;\n            }\n            \n            double alpha = 1.0 / sqrt(1 + *cnt);\n            double new_est = *w_est * (1.0 + alpha * relative_error);\n            *w_est = max(1000.0, min(9000.0, new_est));\n            (*cnt)++;\n        }\n    }\n\n    return 0;\n}","ahc004":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <string>\n#include <cmath>\n#include <cstdlib>\n#include <ctime>\n#include <random>\n#include <tuple>\n#include <utility>\nusing namespace std;\n\nbool is_subsequence(vector<string> &mat, string s) {\n    int len = s.length();\n    int n = mat.size();\n    for (int dir = 0; dir < 2; dir++) {\n        for (int i = 0; i < n; i++) {\n            for (int j = 0; j < n; j++) {\n                bool match = true;\n                for (int p = 0; p < len; p++) {\n                    int ni = i, nj = j;\n                    if (dir == 0) {\n                        nj = (j + p) % n;\n                        if (nj < 0) nj += n;\n                    } else {\n                        ni = (i + p) % n;\n                        if (ni < 0) ni += n;\n                    }\n                    if (mat[ni][nj] != s[p]) {\n                        match = false;\n                        break;\n                    }\n                }\n                if (match) {\n                    return true;\n                }\n            }\n        }\n    }\n    return false;\n}\n\nvector<bool> compute_covered(vector<string> &mat, vector<string> &strings) {\n    int M = strings.size();\n    vector<bool> covered(M, false);\n    for (int idx = 0; idx < M; idx++) {\n        covered[idx] = is_subsequence(mat, strings[idx]);\n    }\n    return covered;\n}\n\nint main() {\n    mt19937 rng(time(0));\n    int N, M;\n    cin >> N >> M;\n    vector<string> strings(M);\n    for (int i = 0; i < M; i++) {\n        cin >> strings[i];\n    }\n\n    auto build_greedy = [&](bool desc) {\n        vector<string> mat(N, string(N, '.'));\n        vector<string> sorted = strings;\n        if (desc) {\n            sort(sorted.begin(), sorted.end(), [](const string& a, const string& b) {\n                return a.size() > b.size();\n            });\n        } else {\n            sort(sorted.begin(), sorted.end(), [](const string& a, const string& b) {\n                return a.size() < b.size();\n            });\n        }\n        for (const string& s : sorted) {\n            int len = s.size();\n            for (int dir = 0; dir < 2; dir++) {\n                for (int i = 0; i < N; i++) {\n                    for (int j = 0; j < N; j++) {\n                        bool ok = true;\n                        for (int p = 0; p < len; p++) {\n                            int ni = i, nj = j;\n                            if (dir == 0) {\n                                nj = (j + p) % N;\n                                if (nj < 0) nj += N;\n                            } else {\n                                ni = (i + p) % N;\n                                if (ni < 0) ni += N;\n                            }\n                            if (mat[ni][nj] != '.' && mat[ni][nj] != s[p]) {\n                                ok = false;\n                                break;\n                            }\n                        }\n                        if (ok) {\n                            for (int p = 0; p < len; p++) {\n                                int ni = i, nj = j;\n                                if (dir == 0) {\n                                    nj = (j + p) % N;\n                                    if (nj < 0) nj += N;\n                                } else {\n                                    ni = (i + p) % N;\n                                    if (ni < 0) ni += N;\n                                }\n                                if (mat[ni][nj] == '.') {\n                                    mat[ni][nj] = s[p];\n                                }\n                            }\n                            goto next_string;\n                        }\n                    }\n                }\n            }\n            next_string: ;\n        }\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                if (mat[i][j] == '.') {\n                    mat[i][j] = 'A';\n                }\n            }\n        }\n        return mat;\n    };\n\n    vector<string> mat1 = build_greedy(true);\n    vector<bool> covered1 = compute_covered(mat1, strings);\n    int c1 = 0;\n    for (bool b : covered1) c1 += b;\n\n    vector<string> mat2 = build_greedy(false);\n    vector<bool> covered2 = compute_covered(mat2, strings);\n    int c2 = 0;\n    for (bool b : covered2) c2 += b;\n\n    vector<string> current;\n    vector<bool> current_covered;\n    int current_cov;\n    if (c1 >= c2) {\n        current = mat1;\n        current_covered = covered1;\n        current_cov = c1;\n    } else {\n        current = mat2;\n        current_covered = covered2;\n        current_cov = c2;\n    }\n\n    vector<string> best_mat = current;\n    int best_cov = current_cov;\n\n    if (best_cov == M) {\n        for (int i = 0; i < N; i++) {\n            cout << best_mat[i] << endl;\n        }\n        return 0;\n    }\n\n    vector<int> uncovered_indices;\n    for (int i = 0; i < M; i++) {\n        if (!current_covered[i]) {\n            uncovered_indices.push_back(i);\n        }\n    }\n\n    vector<vector<vector<tuple<int, int, int, int, int>>>> cell_to_candidates(N, vector<vector<tuple<int, int, int, int, int>>>(N));\n    vector<vector<vector<vector<int>>>> mismatch_count(M, vector<vector<vector<int>>>(N, vector<vector<int>>(N, vector<int>(2, 0))));\n    vector<int> valid_count(M, 0);\n    int total_covered = 0;\n\n    for (int si = 0; si < M; si++) {\n        string s = strings[si];\n        int len = s.length();\n        for (int i0 = 0; i0 < N; i0++) {\n            for (int j0 = 0; j0 < N; j0++) {\n                for (int d = 0; d < 2; d++) {\n                    int cnt = 0;\n                    for (int p = 0; p < len; p++) {\n                        int ni = i0, nj = j0;\n                        if (d == 0) {\n                            nj = (j0 + p) % N;\n                            if (nj < 0) nj += N;\n                        } else {\n                            ni = (i0 + p) % N;\n                            if (ni < 0) ni += N;\n                        }\n                        if (current[ni][nj] != s[p]) {\n                            cnt++;\n                        }\n                        cell_to_candidates[ni][nj].push_back(make_tuple(si, i0, j0, d, p));\n                    }\n                    mismatch_count[si][i0][j0][d] = cnt;\n                    if (cnt == 0) {\n                        valid_count[si]++;\n                    }\n                }\n            }\n        }\n        if (valid_count[si] > 0) {\n            total_covered++;\n        }\n    }\n\n    current_cov = total_covered;\n    if (current_cov > best_cov) {\n        best_cov = current_cov;\n        best_mat = current;\n    }\n\n    const int MAX_ITER = 400;\n    const double T0 = 10.0;\n    uniform_int_distribution<int> rand_idx(0, N-1);\n    uniform_int_distribution<int> rand_char(0, 7);\n    uniform_int_distribution<int> rand_percent(0, 99);\n    uniform_real_distribution<double> rand_prob(0.0, 1.0);\n\n    for (int iter = 0; iter < MAX_ITER; iter++) {\n        double T = T0 * (1.0 - static_cast<double>(iter) / MAX_ITER);\n        vector<string> candidate_mat = current;\n        int i, j;\n        char old_char;\n        char new_char;\n\n        vector<int> uncovered;\n        for (int si = 0; si < M; si++) {\n            if (valid_count[si] == 0) {\n                uncovered.push_back(si);\n            }\n        }\n\n        if (uncovered.empty() || rand_percent(rng) < 30) {\n            i = rand_idx(rng);\n            j = rand_idx(rng);\n            old_char = current[i][j];\n            do {\n                new_char = 'A' + rand_char(rng);\n            } while (new_char == old_char);\n        } else {\n            int uidx = uncovered[rand_idx(rng) % uncovered.size()];\n            string s = strings[uidx];\n            int len = s.length();\n            int i0 = rand_idx(rng);\n            int j0 = rand_idx(rng);\n            int d = rand_percent(rng) % 2;\n            vector<pair<int, int>> mismatches;\n            vector<char> req_chars;\n            for (int p = 0; p < len; p++) {\n                int ni = i0, nj = j0;\n                if (d == 0) {\n                    nj = (j0 + p) % N;\n                    if (nj < 0) nj += N;\n                } else {\n                    ni = (i0 + p) % N;\n                    if (ni < 0) ni += N;\n                }\n                if (current[ni][nj] != s[p]) {\n                    mismatches.push_back({ni, nj});\n                    req_chars.push_back(s[p]);\n                }\n            }\n            if (mismatches.empty()) {\n                i = rand_idx(rng);\n                j = rand_idx(rng);\n                old_char = current[i][j];\n                do {\n                    new_char = 'A' + rand_char(rng);\n                } while (new_char == old_char);\n            } else {\n                int pos = rand_idx(rng) % mismatches.size();\n                i = mismatches[pos].first;\n                j = mismatches[pos].second;\n                new_char = req_chars[pos];\n                old_char = current[i][j];\n                if (new_char == old_char) {\n                    i = rand_idx(rng);\n                    j = rand_idx(rng);\n                    old_char = current[i][j];\n                    do {\n                        new_char = 'A' + rand_char(rng);\n                    } while (new_char == old_char);\n                }\n            }\n        }\n\n        if (old_char == new_char) {\n            continue;\n        }\n\n        candidate_mat[i][j] = new_char;\n        int old_current_cov = current_cov;\n        vector<int> old_valid_count = valid_count;\n        vector<tuple<int, int, int, int, int>> backup_mismatches;\n\n        for (auto &cand : cell_to_candidates[i][j]) {\n            int si = get<0>(cand);\n            int i0 = get<1>(cand);\n            int j0 = get<2>(cand);\n            int d = get<3>(cand);\n            int p = get<4>(cand);\n            char s_char = strings[si][p];\n            int old_mismatch = mismatch_count[si][i0][j0][d];\n            backup_mismatches.push_back(make_tuple(si, i0, j0, d, old_mismatch));\n            bool was_valid = (old_mismatch == 0);\n\n            if (old_char == s_char) {\n                if (new_char != s_char) {\n                    mismatch_count[si][i0][j0][d]++;\n                }\n            } else {\n                if (new_char == s_char) {\n                    mismatch_count[si][i0][j0][d]--;\n                }\n            }\n\n            int new_mismatch = mismatch_count[si][i0][j0][d];\n            bool now_valid = (new_mismatch == 0);\n\n            if (was_valid && !now_valid) {\n                valid_count[si]--;\n                if (valid_count[si] == 0) {\n                    current_cov--;\n                }\n            } else if (!was_valid && now_valid) {\n                valid_count[si]++;\n                if (valid_count[si] == 1) {\n                    current_cov++;\n                }\n            }\n        }\n\n        double delta = current_cov - old_current_cov;\n        bool accept_move = false;\n        if (delta > 0) {\n            accept_move = true;\n        } else if (delta < 0) {\n            double prob = exp(delta / T);\n            double r = rand_prob(rng);\n            accept_move = (r < prob);\n        } else {\n            accept_move = (rand_percent(rng) % 2 == 0);\n        }\n\n        if (accept_move) {\n            current = candidate_mat;\n            if (current_cov > best_cov) {\n                best_cov = current_cov;\n                best_mat = current;\n                if (best_cov == M) {\n                    break;\n                }\n            }\n        } else {\n            current_cov = old_current_cov;\n            valid_count = old_valid_count;\n            for (auto &tup : backup_mismatches) {\n                int si = get<0>(tup);\n                int i0 = get<1>(tup);\n                int j0 = get<2>(tup);\n                int d = get<3>(tup);\n                int old_mismatch = get<4>(tup);\n                mismatch_count[si][i0][j0][d] = old_mismatch;\n            }\n        }\n    }\n\n    for (int i = 0; i < N; i++) {\n        cout << best_mat[i] << endl;\n    }\n    return 0;\n}","ahc005":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int di[] = {-1, 1, 0, 0};\nconst int dj[] = {0, 0, -1, 1};\nconst char dir_char[] = {'U', 'D', 'L', 'R'};\nconst int INF = 1e9;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, si, sj;\n    cin >> N >> si >> sj;\n    vector<string> grid;\n    for (int i = 0; i < N; i++) {\n        string s;\n        cin >> s;\n        grid.push_back(s);\n    }\n\n    vector<vector<int>> h_segment_id(N, vector<int>(N, -1));\n    vector<vector<int>> v_segment_id(N, vector<int>(N, -1));\n    vector<vector<pair<int, int>>> horizontal_segments;\n    vector<vector<pair<int, int>>> vertical_segments;\n\n    for (int i = 0; i < N; i++) {\n        int j = 0;\n        while (j < N) {\n            if (grid[i][j] == '#') {\n                j++;\n                continue;\n            }\n            vector<pair<int, int>> seg;\n            while (j < N && grid[i][j] != '#') {\n                seg.push_back({i, j});\n                j++;\n            }\n            int seg_id = horizontal_segments.size();\n            horizontal_segments.push_back(seg);\n            for (const auto& p : seg) {\n                h_segment_id[p.first][p.second] = seg_id;\n            }\n        }\n    }\n\n    for (int j = 0; j < N; j++) {\n        int i = 0;\n        while (i < N) {\n            if (grid[i][j] == '#') {\n                i++;\n                continue;\n            }\n            vector<pair<int, int>> seg;\n            while (i < N && grid[i][j] != '#') {\n                seg.push_back({i, j});\n                i++;\n            }\n            int seg_id = vertical_segments.size();\n            vertical_segments.push_back(seg);\n            for (const auto& p : seg) {\n                v_segment_id[p.first][p.second] = seg_id;\n            }\n        }\n    }\n\n    int totalH = horizontal_segments.size();\n    int totalV = vertical_segments.size();\n    vector<bool> coveredH(totalH, false);\n    vector<bool> coveredV(totalV, false);\n    int cur_i = si, cur_j = sj;\n    string route = \"\";\n\n    auto coverCell = [&](int i, int j) {\n        int h_id = h_segment_id[i][j];\n        int v_id = v_segment_id[i][j];\n        if (h_id != -1) coveredH[h_id] = true;\n        if (v_id != -1) coveredV[v_id] = true;\n    };\n\n    coverCell(si, sj);\n\n    while (true) {\n        bool allCovered = true;\n        for (bool b : coveredH) if (!b) { allCovered = false; break; }\n        if (allCovered) {\n            for (bool b : coveredV) if (!b) { allCovered = false; break; }\n            if (allCovered) break;\n        }\n\n        vector<vector<int>> dist(N, vector<int>(N, INF));\n        vector<vector<pair<int, int>>> prev(N, vector<pair<int, int>>(N, {-1, -1}));\n        priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<tuple<int, int, int>>> pq;\n        dist[cur_i][cur_j] = 0;\n        pq.push({0, cur_i, cur_j});\n\n        while (!pq.empty()) {\n            auto [cost, i, j] = pq.top();\n            pq.pop();\n            if (cost != dist[i][j]) continue;\n            for (int d = 0; d < 4; d++) {\n                int ni = i + di[d];\n                int nj = j + dj[d];\n                if (ni < 0 || ni >= N || nj < 0 || nj >= N || grid[ni][nj] == '#') continue;\n                int weight = grid[ni][nj] - '0';\n                int new_cost = cost + weight;\n                if (new_cost < dist[ni][nj]) {\n                    dist[ni][nj] = new_cost;\n                    prev[ni][nj] = {i, j};\n                    pq.push({new_cost, ni, nj});\n                }\n            }\n        }\n\n        int min_dist = INF;\n        int target_i = -1, target_j = -1;\n        int target_seg_id = -1;\n        bool is_target_horizontal = false;\n\n        for (int idx = 0; idx < totalH; idx++) {\n            if (coveredH[idx]) continue;\n            for (const auto& cell : horizontal_segments[idx]) {\n                int i = cell.first, j = cell.second;\n                if (dist[i][j] < min_dist) {\n                    min_dist = dist[i][j];\n                    target_i = i;\n                    target_j = j;\n                    target_seg_id = idx;\n                    is_target_horizontal = true;\n                }\n            }\n        }\n\n        for (int idx = 0; idx < totalV; idx++) {\n            if (coveredV[idx]) continue;\n            for (const auto& cell : vertical_segments[idx]) {\n                int i = cell.first, j = cell.second;\n                if (dist[i][j] < min_dist) {\n                    min_dist = dist[i][j];\n                    target_i = i;\n                    target_j = j;\n                    target_seg_id = idx;\n                    is_target_horizontal = false;\n                }\n            }\n        }\n\n        if (min_dist == INF) break;\n\n        vector<pair<int, int>> path_cells;\n        int ci = target_i, cj = target_j;\n        while (ci != cur_i || cj != cur_j) {\n            path_cells.push_back({ci, cj});\n            auto p = prev[ci][cj];\n            ci = p.first;\n            cj = p.second;\n        }\n        reverse(path_cells.begin(), path_cells.end());\n\n        int now_i = cur_i, now_j = cur_j;\n        string path_str = \"\";\n        bool covered_target_segment = false;\n        for (const auto& cell : path_cells) {\n            int ni = cell.first, nj = cell.second;\n            char move;\n            if (ni == now_i + 1) move = 'D';\n            else if (ni == now_i - 1) move = 'U';\n            else if (nj == now_j + 1) move = 'R';\n            else if (nj == now_j - 1) move = 'L';\n            else continue;\n            \n            path_str += move;\n            now_i = ni;\n            now_j = nj;\n            coverCell(ni, nj);\n            \n            if (is_target_horizontal) {\n                if (h_segment_id[ni][nj] == target_seg_id) {\n                    covered_target_segment = true;\n                    break;\n                }\n            } else {\n                if (v_segment_id[ni][nj] == target_seg_id) {\n                    covered_target_segment = true;\n                    break;\n                }\n            }\n        }\n        route += path_str;\n        cur_i = now_i;\n        cur_j = now_j;\n    }\n\n    vector<vector<int>> dist_return(N, vector<int>(N, INF));\n    vector<vector<pair<int, int>>> prev_return(N, vector<pair<int, int>>(N, {-1, -1}));\n    priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<tuple<int, int, int>>> pq_return;\n    dist_return[cur_i][cur_j] = 0;\n    pq_return.push({0, cur_i, cur_j});\n    while (!pq_return.empty()) {\n        auto [cost, i, j] = pq_return.top();\n        pq_return.pop();\n        if (cost != dist_return[i][j]) continue;\n        if (i == si && j == sj) break;\n        for (int d = 0; d < 4; d++) {\n            int ni = i + di[d];\n            int nj = j + dj[d];\n            if (ni < 0 || ni >= N || nj < 0 || nj >= N || grid[ni][nj] == '#') continue;\n            int weight = grid[ni][nj] - '0';\n            int new_cost = cost + weight;\n            if (new_cost < dist_return[ni][nj]) {\n                dist_return[ni][nj] = new_cost;\n                prev_return[ni][nj] = {i, j};\n                pq_return.push({new_cost, ni, nj});\n            }\n        }\n    }\n\n    vector<pair<int, int>> return_path;\n    int ci = si, cj = sj;\n    while (ci != cur_i || cj != cur_j) {\n        return_path.push_back({ci, cj});\n        auto p = prev_return[ci][cj];\n        ci = p.first;\n        cj = p.second;\n    }\n    reverse(return_path.begin(), return_path.end());\n    int now_i = cur_i, now_j = cur_j;\n    string return_str = \"\";\n    for (const auto& cell : return_path) {\n        int ni = cell.first, nj = cell.second;\n        if (ni == now_i + 1) return_str += 'D';\n        else if (ni == now_i - 1) return_str += 'U';\n        else if (nj == now_j + 1) return_str += 'R';\n        else if (nj == now_j - 1) return_str += 'L';\n        now_i = ni;\n        now_j = nj;\n    }\n    route += return_str;\n\n    cout << route << endl;\n\n    return 0;\n}","future-contest-2022-qual":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <set>\n#include <algorithm>\n#include <cmath>\n#include <iomanip>\n#include <cctype>\n#include <cstdio>\n#include <cstring>\n#include <map>\n#include <unordered_map>\n#include <unordered_set>\n#include <stack>\n#include <deque>\n#include <string>\n#include <climits>\n#include <cfloat>\n#include <bitset>\nusing namespace std;\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, K, R;\n    cin >> N >> M >> K >> R;\n\n    vector<vector<int>> task_diff(N, vector<int>(K));\n    for (int i = 0; i < N; i++) {\n        for (int k = 0; k < K; k++) {\n            cin >> task_diff[i][k];\n        }\n    }\n\n    vector<vector<int>> children(N+1);\n    vector<vector<int>> parents(N+1);\n    vector<int> in_degree(N+1, 0);\n    for (int i = 0; i < R; i++) {\n        int u, v;\n        cin >> u >> v;\n        children[u].push_back(v);\n        parents[v].push_back(u);\n        in_degree[v]++;\n    }\n\n    vector<int> num_desc(N+1, 0);\n    for (int i = 1; i <= N; i++) {\n        vector<bool> visited(N+1, false);\n        queue<int> q;\n        q.push(i);\n        visited[i] = true;\n        int count = 0;\n        while (!q.empty()) {\n            int u = q.front(); q.pop();\n            count++;\n            for (int v : children[u]) {\n                if (!visited[v]) {\n                    visited[v] = true;\n                    q.push(v);\n                }\n            }\n        }\n        num_desc[i] = count;\n    }\n\n    vector<bool> completed(N+1, false);\n    vector<bool> assigned(N+1, false);\n    vector<int> task_start_day(N+1, -1);\n    vector<int> member_status(M+1, -1);\n    vector<vector<double>> s_hat(M+1, vector<double>(K, 10.0));\n    vector<vector<int>> min_skill_lower_bound(M+1, vector<int>(K, 0));\n    vector<vector<double>> momentum(M+1, vector<double>(K, 0.0));\n    set<int> available_set;\n    for (int i = 1; i <= N; i++) {\n        if (in_degree[i] == 0) {\n            available_set.insert(i);\n        }\n    }\n\n    int day = 0;\n    while (true) {\n        day++;\n        if (day > 1) {\n            int n_finished;\n            cin >> n_finished;\n            if (n_finished == -1) {\n                break;\n            }\n            vector<int> finished_members(n_finished);\n            for (int i = 0; i < n_finished; i++) {\n                cin >> finished_members[i];\n            }\n            for (int j : finished_members) {\n                int task_i = member_status[j];\n                completed[task_i] = true;\n                assigned[task_i] = false;\n                member_status[j] = -1;\n                for (int child : children[task_i]) {\n                    in_degree[child]--;\n                    if (in_degree[child] == 0 && !completed[child] && !assigned[child]) {\n                        available_set.insert(child);\n                    }\n                }\n                int start_day = task_start_day[task_i];\n                int t_days = day - start_day;\n                vector<int>& d_vec = task_diff[task_i-1];\n                if (t_days == 1) {\n                    for (int k = 0; k < K; k++) {\n                        if (d_vec[k] > min_skill_lower_bound[j][k]) {\n                            min_skill_lower_bound[j][k] = d_vec[k];\n                        }\n                        if (s_hat[j][k] < min_skill_lower_bound[j][k]) {\n                            s_hat[j][k] = min_skill_lower_bound[j][k];\n                        }\n                    }\n                } else {\n                    double w_hat = 0.0;\n                    for (int k = 0; k < K; k++) {\n                        if (d_vec[k] > s_hat[j][k]) {\n                            w_hat += (d_vec[k] - s_hat[j][k]);\n                        }\n                    }\n                    double w_obs = t_days;\n                    double e = w_obs - w_hat;\n                    double learning_rate = 0.1;\n                    double momentum_factor = 0.5;\n                    for (int k = 0; k < K; k++) {\n                        if (d_vec[k] > s_hat[j][k]) {\n                            double update = -learning_rate * e + momentum_factor * momentum[j][k];\n                            s_hat[j][k] += update;\n                            momentum[j][k] = update;\n                            if (s_hat[j][k] < min_skill_lower_bound[j][k]) {\n                                s_hat[j][k] = min_skill_lower_bound[j][k];\n                            }\n                        } else {\n                            if (s_hat[j][k] < min_skill_lower_bound[j][k]) {\n                                s_hat[j][k] = min_skill_lower_bound[j][k];\n                            }\n                        }\n                    }\n                }\n            }\n        }\n\n        vector<int> free_members;\n        for (int j = 1; j <= M; j++) {\n            if (member_status[j] == -1) {\n                free_members.push_back(j);\n            }\n        }\n\n        vector<vector<double>> time_est_full(N+1, vector<double>(M+1, 0.0));\n        vector<double> base_time(N+1, 0.0);\n        for (int i = 1; i <= N; i++) {\n            if (completed[i]) {\n                base_time[i] = 0.0;\n                continue;\n            }\n            double min_est = 1e18;\n            for (int j = 1; j <= M; j++) {\n                double w = 0.0;\n                for (int k = 0; k < K; k++) {\n                    if (task_diff[i-1][k] > s_hat[j][k]) {\n                        w += task_diff[i-1][k] - s_hat[j][k];\n                    }\n                }\n                if (w < 1e-5) w = 1.0;\n                time_est_full[i][j] = w;\n                if (w < min_est) min_est = w;\n            }\n            base_time[i] = min_est;\n        }\n\n        vector<double> earliest_start(N+1, 0.0);\n        vector<int> in_degree_temp = in_degree;\n        queue<int> q_forward;\n        for (int i = 1; i <= N; i++) {\n            if (in_degree_temp[i] == 0) {\n                q_forward.push(i);\n            }\n        }\n        while (!q_forward.empty()) {\n            int u = q_forward.front();\n            q_forward.pop();\n            for (int v : children[u]) {\n                if (earliest_start[v] < earliest_start[u] + base_time[u]) {\n                    earliest_start[v] = earliest_start[u] + base_time[u];\n                }\n                in_degree_temp[v]--;\n                if (in_degree_temp[v] == 0) {\n                    q_forward.push(v);\n                }\n            }\n        }\n\n        double total_project_time = 0.0;\n        for (int i = 1; i <= N; i++) {\n            total_project_time = max(total_project_time, earliest_start[i] + base_time[i]);\n        }\n\n        vector<double> latest_finish(N+1, total_project_time);\n        vector<double> latest_start(N+1, 0.0);\n        vector<int> out_degree(N+1, 0);\n        for (int i = 1; i <= N; i++) {\n            out_degree[i] = children[i].size();\n        }\n        vector<int> out_degree_temp = out_degree;\n        queue<int> q_backward;\n        for (int i = 1; i <= N; i++) {\n            if (out_degree_temp[i] == 0) {\n                q_backward.push(i);\n            }\n        }\n        while (!q_backward.empty()) {\n            int u = q_backward.front();\n            q_backward.pop();\n            latest_start[u] = latest_finish[u] - base_time[u];\n            for (int p : parents[u]) {\n                if (latest_finish[p] > latest_start[u]) {\n                    latest_finish[p] = latest_start[u];\n                }\n                out_degree_temp[p]--;\n                if (out_degree_temp[p] == 0) {\n                    q_backward.push(p);\n                }\n            }\n        }\n\n        vector<double> slack(N+1, 0.0);\n        for (int i = 1; i <= N; i++) {\n            slack[i] = latest_start[i] - earliest_start[i];\n        }\n\n        if (free_members.empty() || available_set.empty()) {\n            cout << 0 << endl;\n            for (int j = 1; j <= M; j++) {\n                cout << \"#s \" << j;\n                for (int k = 0; k < K; k++) {\n                    cout << \" \" << (int)round(s_hat[j][k]);\n                }\n                cout << endl;\n            }\n            cout.flush();\n            continue;\n        }\n\n        vector<int> available_tasks_list(available_set.begin(), available_set.end());\n        int n_tasks = available_tasks_list.size();\n        int n_free = free_members.size();\n        vector<vector<double>> time_est(n_tasks, vector<double>(n_free, 0.0));\n        for (int i = 0; i < n_tasks; i++) {\n            int task_id = available_tasks_list[i];\n            for (int j = 0; j < n_free; j++) {\n                int member_id = free_members[j];\n                time_est[i][j] = time_est_full[task_id][member_id];\n            }\n        }\n\n        vector<tuple<double, int, int>> task_priority;\n        for (int i = 0; i < n_tasks; i++) {\n            int task_id = available_tasks_list[i];\n            task_priority.push_back({slack[task_id], -num_desc[task_id], i});\n        }\n        sort(task_priority.begin(), task_priority.end());\n\n        vector<bool> free_flag(n_free, true);\n        vector<pair<int, int>> assignments;\n        vector<bool> task_assigned_flag(n_tasks, false);\n        for (auto& tpl : task_priority) {\n            int idx_task = get<2>(tpl);\n            if (task_assigned_flag[idx_task]) continue;\n            int task_id = available_tasks_list[idx_task];\n            double best_time = 1e18;\n            int best_j_index = -1;\n            for (int j = 0; j < n_free; j++) {\n                if (free_flag[j] && time_est[idx_task][j] < best_time) {\n                    best_time = time_est[idx_task][j];\n                    best_j_index = j;\n                }\n            }\n            if (best_j_index != -1) {\n                int member_id = free_members[best_j_index];\n                assignments.push_back({member_id, task_id});\n                free_flag[best_j_index] = false;\n                task_assigned_flag[idx_task] = true;\n                assigned[task_id] = true;\n                available_set.erase(task_id);\n                member_status[member_id] = task_id;\n                task_start_day[task_id] = day;\n            }\n        }\n\n        cout << assignments.size();\n        for (auto& a : assignments) {\n            cout << \" \" << a.first << \" \" << a.second;\n        }\n        cout << endl;\n        for (int j = 1; j <= M; j++) {\n            cout << \"#s \" << j;\n            for (int k = 0; k < K; k++) {\n                cout << \" \" << (int)round(s_hat[j][k]);\n            }\n            cout << endl;\n        }\n        cout.flush();\n    }\n\n    return 0;\n}","ahc006":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <cmath>\n#include <cstdlib>\nusing namespace std;\n\nstruct Point {\n    int x, y;\n    Point(int x=0, int y=0) : x(x), y(y) {}\n};\n\nint manhattan(const Point& a, const Point& b) {\n    return abs(a.x - b.x) + abs(a.y - b.y);\n}\n\nstruct Order {\n    int a, b, c, d;\n};\n\nint main() {\n    vector<Order> orders(1000);\n    for (int i = 0; i < 1000; i++) {\n        cin >> orders[i].a >> orders[i].b >> orders[i].c >> orders[i].d;\n    }\n\n    vector<Point> tour;\n    tour.push_back(Point(400, 400));\n    tour.push_back(Point(400, 400));\n\n    vector<int> point_to_order;\n    vector<int> point_type;\n    point_to_order.push_back(-1);\n    point_to_order.push_back(-1);\n    point_type.push_back(0);\n    point_type.push_back(0);\n\n    vector<int> rest_index(1000, -1);\n    vector<int> dest_index(1000, -1);\n    vector<bool> used(1000, false);\n    vector<int> chosen_ids;\n\n    int n = 2;\n    for (int step = 0; step < 50; step++) {\n        long long best_cost = (long long)1e18;\n        int best_order = -1;\n        int best_edge = -1;\n\n        for (int idx = 0; idx < 1000; idx++) {\n            if (used[idx]) continue;\n            Order& o = orders[idx];\n            Point R = {o.a, o.b};\n            Point D = {o.c, o.d};\n            for (int edge = 0; edge < n-1; edge++) {\n                Point A = tour[edge];\n                Point B = tour[edge+1];\n                long long cost_insert = (long long)manhattan(A, R) + manhattan(R, D) + manhattan(D, B) - manhattan(A, B);\n                if (cost_insert < best_cost) {\n                    best_cost = cost_insert;\n                    best_order = idx;\n                    best_edge = edge;\n                }\n            }\n        }\n\n        if (best_order == -1) {\n            break;\n        }\n\n        Order& o = orders[best_order];\n        Point R = {o.a, o.b};\n        Point D = {o.c, o.d};\n        tour.insert(tour.begin() + best_edge + 1, R);\n        tour.insert(tour.begin() + best_edge + 2, D);\n\n        point_to_order.insert(point_to_order.begin() + best_edge + 1, best_order);\n        point_to_order.insert(point_to_order.begin() + best_edge + 2, best_order);\n        point_type.insert(point_type.begin() + best_edge + 1, 1);\n        point_type.insert(point_type.begin() + best_edge + 2, 2);\n\n        rest_index[best_order] = best_edge + 1;\n        dest_index[best_order] = best_edge + 2;\n        used[best_order] = true;\n        chosen_ids.push_back(best_order + 1);\n\n        n += 2;\n    }\n\n    n = tour.size();\n    bool improved = true;\n    while (improved) {\n        improved = false;\n        for (int i = 0; i <= n - 4; i++) {\n            int j = i + 1;\n            int k = i + 2;\n            int id1 = point_to_order[j];\n            int id2 = point_to_order[k];\n            if (id1 == id2 && id1 != -1) {\n                continue;\n            }\n\n            Point A = tour[i];\n            Point B = tour[j];\n            Point C = tour[k];\n            Point D = tour[i + 3];\n            long long original_cost = (long long)manhattan(A, B) + manhattan(B, C) + manhattan(C, D);\n            long long new_cost = (long long)manhattan(A, C) + manhattan(C, B) + manhattan(B, D);\n            long long delta = new_cost - original_cost;\n            if (delta >= 0) {\n                continue;\n            }\n\n            bool valid = true;\n            if (id1 != -1) {\n                int r1 = rest_index[id1];\n                int d1 = dest_index[id1];\n                if (j == r1) {\n                    r1 = k;\n                } else if (j == d1) {\n                    d1 = k;\n                }\n                if (r1 >= d1) {\n                    valid = false;\n                }\n            }\n            if (valid && id2 != -1) {\n                int r2 = rest_index[id2];\n                int d2 = dest_index[id2];\n                if (k == r2) {\n                    r2 = j;\n                } else if (k == d2) {\n                    d2 = j;\n                }\n                if (r2 >= d2) {\n                    valid = false;\n                }\n            }\n\n            if (valid) {\n                swap(tour[j], tour[k]);\n                swap(point_to_order[j], point_to_order[k]);\n                swap(point_type[j], point_type[k]);\n\n                if (id1 != -1) {\n                    if (rest_index[id1] == j) {\n                        rest_index[id1] = k;\n                    } else if (dest_index[id1] == j) {\n                        dest_index[id1] = k;\n                    }\n                }\n                if (id2 != -1) {\n                    if (rest_index[id2] == k) {\n                        rest_index[id2] = j;\n                    } else if (dest_index[id2] == k) {\n                        dest_index[id2] = j;\n                    }\n                }\n                improved = true;\n            }\n        }\n    }\n\n    cout << \"50\";\n    for (int i = 0; i < 50; i++) {\n        cout << \" \" << chosen_ids[i];\n    }\n    cout << endl;\n    cout << tour.size();\n    for (int i = 0; i < tour.size(); i++) {\n        cout << \" \" << tour[i].x << \" \" << tour[i].y;\n    }\n    cout << endl;\n\n    return 0;\n}","ahc007":"#include <bits/stdc++.h>\n#include <cmath>\n#include <vector>\n#include <tuple>\n#include <algorithm>\n#include <atcoder/dsu>\n\nusing namespace std;\nusing namespace atcoder;\n\nint main() {\n    const int N = 400;\n    const int M = 1995;\n    vector<double> x(N), y(N);\n    for (int i = 0; i < N; i++) {\n        cin >> x[i] >> y[i];\n    }\n\n    vector<pair<int, int>> edges_list(M);\n    for (int i = 0; i < M; i++) {\n        cin >> edges_list[i].first >> edges_list[i].second;\n    }\n\n    vector<double> d_list(M);\n    for (int i = 0; i < M; i++) {\n        int u = edges_list[i].first;\n        int v = edges_list[i].second;\n        double dx = x[u] - x[v];\n        double dy = y[u] - y[v];\n        d_list[i] = round(sqrt(dx*dx + dy*dy));\n    }\n\n    vector<tuple<double, int, int, int>> edges_for_mst;\n    for (int i = 0; i < M; i++) {\n        edges_for_mst.push_back(make_tuple(d_list[i], edges_list[i].first, edges_list[i].second, i));\n    }\n    sort(edges_for_mst.begin(), edges_for_mst.end());\n\n    dsu dsu_mst(N);\n    vector<bool> in_mst_low(M, false);\n    for (auto &e : edges_for_mst) {\n        double cost = get<0>(e);\n        int u = get<1>(e);\n        int v = get<2>(e);\n        int idx = get<3>(e);\n        if (dsu_mst.same(u, v)) continue;\n        dsu_mst.merge(u, v);\n        in_mst_low[idx] = true;\n    }\n\n    dsu dsu_online(N);\n    int comp_count = N;\n\n    for (int i = 0; i < M; i++) {\n        int l_i;\n        cin >> l_i;\n\n        int u = edges_list[i].first;\n        int v = edges_list[i].second;\n\n        if (dsu_online.same(u, v)) {\n            cout << 0 << endl;\n            cout.flush();\n        } else {\n            dsu temp = dsu_online;\n            int temp_comp = comp_count;\n            for (int j = i+1; j < M; j++) {\n                int uj = edges_list[j].first;\n                int vj = edges_list[j].second;\n                if (!temp.same(uj, vj)) {\n                    temp.merge(uj, vj);\n                    temp_comp--;\n                }\n            }\n            if (temp_comp != 1) {\n                cout << 1 << endl;\n                cout.flush();\n                dsu_online.merge(u, v);\n                comp_count--;\n            } else {\n                double progress = (double) i / M;\n                double ratio = (comp_count - 1.0) / (M - i - 1);\n                double threshold_mst = 1.3 + 1.05 * (progress + ratio);\n                double threshold_non_mst = 1.1 + 1.25 * (progress + ratio);\n\n                if (in_mst_low[i]) {\n                    if (l_i <= threshold_mst * d_list[i]) {\n                        cout << 1 << endl;\n                        cout.flush();\n                        dsu_online.merge(u, v);\n                        comp_count--;\n                    } else {\n                        cout << 0 << endl;\n                        cout.flush();\n                    }\n                } else {\n                    if (l_i <= threshold_non_mst * d_list[i]) {\n                        cout << 1 << endl;\n                        cout.flush();\n                        dsu_online.merge(u, v);\n                        comp_count--;\n                    } else {\n                        cout << 0 << endl;\n                        cout.flush();\n                    }\n                }\n            }\n        }\n    }\n\n    return 0;\n}","ahc008":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\n#include <set>\n#include <tuple>\n#include <climits>\nusing namespace std;\n\nconst int GRID_SIZE = 32;\n\nstruct Pet {\n    int x, y, type;\n};\n\nstruct Human {\n    int x, y;\n};\n\nstruct BarrierTask {\n    int wall_x, wall_y;\n    int hx, hy;\n    char action;\n};\n\nint manhattan(int x1, int y1, int x2, int y2) {\n    return abs(x1 - x2) + abs(y1 - y2);\n}\n\nint main() {\n    int N;\n    cin >> N;\n    vector<Pet> pets(N);\n    for (int i = 0; i < N; i++) {\n        cin >> pets[i].x >> pets[i].y >> pets[i].type;\n    }\n    int M;\n    cin >> M;\n    vector<Human> humans(M);\n    for (int i = 0; i < M; i++) {\n        cin >> humans[i].x >> humans[i].y;\n    }\n\n    bool wall[GRID_SIZE][GRID_SIZE] = {false};\n    for (int i = 0; i < GRID_SIZE; i++) {\n        for (int j = 0; j < GRID_SIZE; j++) {\n            if (i == 0 || i == GRID_SIZE-1 || j == 0 || j == GRID_SIZE-1) {\n                wall[i][j] = true;\n            }\n        }\n    }\n\n    vector<BarrierTask> all_tasks;\n    for (int y = 2; y <= 29; y++) {\n        BarrierTask task;\n        task.wall_x = 2; task.wall_y = y;\n        task.hx = 3; task.hy = y;\n        task.action = 'u';\n        all_tasks.push_back(task);\n    }\n    for (int y = 2; y <= 29; y++) {\n        BarrierTask task;\n        task.wall_x = 29; task.wall_y = y;\n        task.hx = 28; task.hy = y;\n        task.action = 'd';\n        all_tasks.push_back(task);\n    }\n    for (int x = 3; x <= 28; x++) {\n        BarrierTask task;\n        task.wall_x = x; task.wall_y = 2;\n        task.hx = x; task.hy = 3;\n        task.action = 'l';\n        all_tasks.push_back(task);\n    }\n    for (int x = 3; x <= 28; x++) {\n        BarrierTask task;\n        task.wall_x = x; task.wall_y = 29;\n        task.hx = x; task.hy = 28;\n        task.action = 'r';\n        all_tasks.push_back(task);\n    }\n\n    BarrierTask gap_task = {2, 15, 3, 15, 'u'};\n    vector<BarrierTask> all_tasks_without_gap;\n    for (auto& task : all_tasks) {\n        if (task.wall_x == gap_task.wall_x && task.wall_y == gap_task.wall_y) {\n            continue;\n        }\n        all_tasks_without_gap.push_back(task);\n    }\n\n    vector<queue<BarrierTask>> tasks(M);\n    vector<int> current_x(M), current_y(M);\n    for (int i=0; i<M; i++) {\n        current_x[i] = humans[i].x;\n        current_y[i] = humans[i].y;\n    }\n\n    vector<BarrierTask> unassigned = all_tasks_without_gap;\n    while (!unassigned.empty()) {\n        int best_i = -1, best_j = -1;\n        int min_dist = INT_MAX;\n        for (int i=0; i<M; i++) {\n            for (int j=0; j<unassigned.size(); j++) {\n                BarrierTask& task = unassigned[j];\n                int d = manhattan(current_x[i], current_y[i], task.hx, task.hy);\n                if (d < min_dist) {\n                    min_dist = d;\n                    best_i = i;\n                    best_j = j;\n                }\n            }\n        }\n        if (best_i == -1) break;\n        tasks[best_i].push(unassigned[best_j]);\n        current_x[best_i] = unassigned[best_j].hx;\n        current_y[best_i] = unassigned[best_j].hy;\n        unassigned.erase(unassigned.begin() + best_j);\n    }\n\n    bool gap_assigned = false;\n\n    for (int turn = 0; turn < 300; turn++) {\n        set<pair<int, int>> planned_walls;\n        vector<string> actions(M, \".\");\n\n        for (int i = 0; i < M; i++) {\n            if (tasks[i].empty()) {\n                continue;\n            }\n            BarrierTask current_task = tasks[i].front();\n            if (wall[current_task.wall_x][current_task.wall_y]) {\n                tasks[i].pop();\n                continue;\n            }\n            if (humans[i].x == current_task.hx && humans[i].y == current_task.hy) {\n                bool can_build = true;\n                for (const auto& pet : pets) {\n                    if (pet.x == current_task.wall_x && pet.y == current_task.wall_y) {\n                        can_build = false;\n                        break;\n                    }\n                }\n                if (can_build) {\n                    for (int j = 0; j < M; j++) {\n                        if (humans[j].x == current_task.wall_x && humans[j].y == current_task.wall_y) {\n                            can_build = false;\n                            break;\n                        }\n                    }\n                }\n                if (can_build) {\n                    int dx[4] = {0,0,1,-1};\n                    int dy[4] = {1,-1,0,0};\n                    for (int d=0; d<4; d++) {\n                        int nx = current_task.wall_x + dx[d];\n                        int ny = current_task.wall_y + dy[d];\n                        if (nx < 1 || nx > 30 || ny < 1 || ny > 30) continue;\n                        for (const auto& pet : pets) {\n                            if (pet.x == nx && pet.y == ny) {\n                                can_build = false;\n                                break;\n                            }\n                        }\n                        if (!can_build) break;\n                    }\n                }\n                if (current_task.wall_x == gap_task.wall_x && current_task.wall_y == gap_task.wall_y) {\n                    if (turn < 290) {\n                        can_build = false;\n                    }\n                }\n                if (can_build) {\n                    actions[i] = string(1, current_task.action);\n                    planned_walls.insert({current_task.wall_x, current_task.wall_y});\n                }\n            } else {\n                actions[i] = \"?\";\n            }\n        }\n\n        if (turn >= 290 && !gap_assigned && !wall[gap_task.wall_x][gap_task.wall_y]) {\n            int best_i = -1;\n            int best_dist = INT_MAX;\n            for (int i=0; i<M; i++) {\n                int d = manhattan(humans[i].x, humans[i].y, gap_task.hx, gap_task.hy);\n                if (d < best_dist) {\n                    best_dist = d;\n                    best_i = i;\n                }\n            }\n            if (best_i != -1) {\n                tasks[best_i].push(gap_task);\n                gap_assigned = true;\n            }\n        }\n\n        for (int i = 0; i < M; i++) {\n            if (actions[i] == \"?\") {\n                if (tasks[i].empty()) {\n                    actions[i] = '.';\n                    continue;\n                }\n                BarrierTask current_task = tasks[i].front();\n                if (wall[current_task.wall_x][current_task.wall_y]) {\n                    tasks[i].pop();\n                    actions[i] = '.';\n                    continue;\n                }\n                int sx = humans[i].x;\n                int sy = humans[i].y;\n                int tx = current_task.hx;\n                int ty = current_task.hy;\n\n                vector<vector<bool>> visited(GRID_SIZE, vector<bool>(GRID_SIZE, false));\n                vector<vector<char>> first_step_dir(GRID_SIZE, vector<char>(GRID_SIZE, '.'));\n                queue<pair<int, int>> q;\n                q.push({sx, sy});\n                visited[sx][sy] = true;\n                bool found = false;\n\n                while (!q.empty()) {\n                    auto [x, y] = q.front(); q.pop();\n                    if (x == tx && y == ty) {\n                        found = true;\n                        break;\n                    }\n                    vector<tuple<int, int, char>> dirs = {{-1,0,'U'}, {1,0,'D'}, {0,-1,'L'}, {0,1,'R'}};\n                    for (auto [dx, dy, dir] : dirs) {\n                        int nx = x + dx;\n                        int ny = y + dy;\n                        if (nx < 1 || nx > 30 || ny < 1 || ny > 30) continue;\n                        if (visited[nx][ny]) continue;\n                        if (wall[nx][ny] || planned_walls.count({nx,ny})) {\n                            continue;\n                        }\n                        visited[nx][ny] = true;\n                        if (x == sx && y == sy) {\n                            first_step_dir[nx][ny] = dir;\n                        } else {\n                            first_step_dir[nx][ny] = first_step_dir[x][y];\n                        }\n                        q.push({nx, ny});\n                    }\n                }\n\n                if (found) {\n                    actions[i] = string(1, first_step_dir[tx][ty]);\n                } else {\n                    actions[i] = '.';\n                }\n            }\n        }\n\n        string output_line;\n        for (int i=0; i<M; i++) {\n            output_line += actions[i];\n        }\n        cout << output_line << endl;\n        cout.flush();\n\n        for (int i=0; i<M; i++) {\n            if (actions[i] == \"u\" || actions[i] == \"d\" || actions[i] == \"l\" || actions[i] == \"r\") {\n                BarrierTask current_task = tasks[i].front();\n                wall[current_task.wall_x][current_task.wall_y] = true;\n                tasks[i].pop();\n            } else if (actions[i] == \"U\") {\n                humans[i].x--;\n            } else if (actions[i] == \"D\") {\n                humans[i].x++;\n            } else if (actions[i] == \"L\") {\n                humans[i].y--;\n            } else if (actions[i] == \"R\") {\n                humans[i].y++;\n            }\n        }\n\n        for (int i=0; i<N; i++) {\n            string move;\n            cin >> move;\n            for (char c : move) {\n                if (c == 'U') pets[i].x--;\n                else if (c == 'D') pets[i].x++;\n                else if (c == 'L') pets[i].y--;\n                else if (c == 'R') pets[i].y++;\n            }\n        }\n    }\n\n    return 0;\n}","ahc009":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\n#include <tuple>\n#include <cmath>\nusing namespace std;\n\ndouble evaluate_route(const string& route, int s_i, int s_j, int t_i, int t_j, double p, \n                     const vector<string>& h, const vector<string>& v) {\n    int T = route.size();\n    vector<vector<double>> dp(20, vector<double>(20, 0.0));\n    for (int i = 0; i < 20; i++) {\n        for (int j = 0; j < 20; j++) {\n            if (i == t_i && j == t_j) {\n                dp[i][j] = 401 - T;\n            } else {\n                dp[i][j] = 0.0;\n            }\n        }\n    }\n\n    for (int t = T-1; t >= 0; t--) {\n        vector<vector<double>> new_dp(20, vector<double>(20, 0.0));\n        for (int i = 0; i < 20; i++) {\n            for (int j = 0; j < 20; j++) {\n                if (i == t_i && j == t_j) {\n                    new_dp[i][j] = 401 - t;\n                } else {\n                    char c = route[t];\n                    int ni = i, nj = j;\n                    bool blocked = false;\n                    if (c == 'U') {\n                        if (i > 0 && v[i-1][j] == '0') \n                            ni--;\n                        else \n                            blocked = true;\n                    } else if (c == 'D') {\n                        if (i < 19 && v[i][j] == '0') \n                            ni++;\n                        else \n                            blocked = true;\n                    } else if (c == 'L') {\n                        if (j > 0 && h[i][j-1] == '0') \n                            nj--;\n                        else \n                            blocked = true;\n                    } else if (c == 'R') {\n                        if (j < 19 && h[i][j] == '0') \n                            nj++;\n                        else \n                            blocked = true;\n                    }\n\n                    double val;\n                    if (ni == t_i && nj == t_j && !blocked) {\n                        val = p * dp[i][j] + (1-p) * (401 - (t+1));\n                    } else {\n                        if (blocked) {\n                            val = p * dp[i][j] + (1-p) * dp[i][j];\n                        } else {\n                            val = p * dp[i][j] + (1-p) * dp[ni][nj];\n                        }\n                    }\n                    new_dp[i][j] = val;\n                }\n            }\n        }\n        dp = new_dp;\n    }\n    return dp[s_i][s_j];\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int s_i, s_j, t_i, t_j;\n    double p;\n    cin >> s_i >> s_j >> t_i >> t_j >> p;\n\n    vector<string> h(20);\n    for (int i = 0; i < 20; i++) {\n        cin >> h[i];\n    }\n\n    vector<string> v(19);\n    for (int i = 0; i < 19; i++) {\n        cin >> v[i];\n    }\n\n    vector<vector<bool>> visited(20, vector<bool>(20, false));\n    vector<vector<pair<int, int>>> parent(20, vector<pair<int, int>>(20, {-1, -1}));\n    vector<vector<char>> parent_move(20, vector<char>(20, ' '));\n    queue<pair<int, int>> q;\n    q.push({s_i, s_j});\n    visited[s_i][s_j] = true;\n\n    while (!q.empty()) {\n        auto [i, j] = q.front(); q.pop();\n        if (i == t_i && j == t_j) break;\n\n        if (i > 0 && v[i-1][j] == '0' && !visited[i-1][j]) {\n            visited[i-1][j] = true;\n            parent[i-1][j] = {i, j};\n            parent_move[i-1][j] = 'U';\n            q.push({i-1, j});\n        }\n        if (i < 19 && v[i][j] == '0' && !visited[i+1][j]) {\n            visited[i+1][j] = true;\n            parent[i+1][j] = {i, j};\n            parent_move[i+1][j] = 'D';\n            q.push({i+1, j});\n        }\n        if (j > 0 && h[i][j-1] == '0' && !visited[i][j-1]) {\n            visited[i][j-1] = true;\n            parent[i][j-1] = {i, j};\n            parent_move[i][j-1] = 'L';\n            q.push({i, j-1});\n        }\n        if (j < 19 && h[i][j] == '0' && !visited[i][j+1]) {\n            visited[i][j+1] = true;\n            parent[i][j+1] = {i, j};\n            parent_move[i][j+1] = 'R';\n            q.push({i, j+1});\n        }\n    }\n\n    string bfs_route = \"\";\n    string extended_bfs = string(200, 'U');\n    if (visited[t_i][t_j]) {\n        int i = t_i, j = t_j;\n        while (!(i == s_i && j == s_j)) {\n            char m = parent_move[i][j];\n            bfs_route += m;\n            auto [pi, pj] = parent[i][j];\n            i = pi;\n            j = pj;\n        }\n        reverse(bfs_route.begin(), bfs_route.end());\n        int L = bfs_route.size();\n        extended_bfs = \"\";\n        for (int i = 0; i < 200; i++) {\n            extended_bfs += bfs_route[i % L];\n        }\n    }\n\n    vector<pair<string, double>> candidates;\n\n    if (visited[t_i][t_j]) {\n        char safe_move = ' ';\n        if (t_i == 19) safe_move = 'D';\n        else if (t_j == 19) safe_move = 'R';\n        else if (v[t_i][t_j] == '1') safe_move = 'D';\n        else if (h[t_i][t_j] == '1') safe_move = 'R';\n        else if (t_i > 0 && v[t_i-1][t_j] == '1') safe_move = 'U';\n        else if (t_j > 0 && h[t_i][t_j-1] == '1') safe_move = 'L';\n        \n        if (safe_move != ' ') {\n            int L = bfs_route.size();\n            string stay_route = bfs_route;\n            if (L < 200) {\n                stay_route += string(200 - L, safe_move);\n            } else {\n                stay_route = stay_route.substr(0, 200);\n            }\n            double score_stay = evaluate_route(stay_route, s_i, s_j, t_i, t_j, p, h, v);\n            candidates.push_back({stay_route, score_stay});\n        }\n    }\n\n    const int T = 200;\n    vector<vector<vector<char>>> best_move(T, vector<vector<char>>(20, vector<char>(20, 'U')));\n    vector<vector<double>> dp_next(20, vector<double>(20, 0.0));\n\n    for (int i = 0; i < 20; i++) {\n        for (int j = 0; j < 20; j++) {\n            if (i == t_i && j == t_j) {\n                dp_next[i][j] = 201.0;\n            } else {\n                dp_next[i][j] = 0.0;\n            }\n        }\n    }\n\n    for (int tt = T-1; tt >= 0; tt--) {\n        vector<vector<double>> dp_curr(20, vector<double>(20, 0.0));\n        for (int i = 0; i < 20; i++) {\n            for (int j = 0; j < 20; j++) {\n                if (i == t_i && j == t_j) {\n                    dp_curr[i][j] = 401.0 - tt;\n                    continue;\n                }\n                double best_val = -1e18;\n                char best_char = 'U';\n                for (char move : {'U','D','L','R'}) {\n                    int ni = i, nj = j;\n                    if (move == 'U') {\n                        if (i > 0 && v[i-1][j] == '0') {\n                            ni = i - 1;\n                        }\n                    } else if (move == 'D') {\n                        if (i < 19 && v[i][j] == '0') {\n                            ni = i + 1;\n                        }\n                    } else if (move == 'L') {\n                        if (j > 0 && h[i][j-1] == '0') {\n                            nj = j - 1;\n                        }\n                    } else if (move == 'R') {\n                        if (j < 19 && h[i][j] == '0') {\n                            nj = j + 1;\n                        }\n                    }\n\n                    double val = p * dp_next[i][j] + (1-p) * dp_next[ni][nj];\n                    if (val > best_val) {\n                        best_val = val;\n                        best_char = move;\n                    }\n                }\n                dp_curr[i][j] = best_val;\n                best_move[tt][i][j] = best_char;\n            }\n        }\n        dp_next = dp_curr;\n    }\n\n    int cur_i = s_i, cur_j = s_j;\n    string route_dp = \"\";\n    for (int t = 0; t < T; t++) {\n        if (cur_i == t_i && cur_j == t_j) {\n            int rem = T - t;\n            route_dp += string(rem, 'U');\n            break;\n        }\n        char move = best_move[t][cur_i][cur_j];\n        route_dp += move;\n\n        if (move == 'U') {\n            if (cur_i > 0 && v[cur_i-1][cur_j] == '0') {\n                cur_i--;\n            }\n        } else if (move == 'D') {\n            if (cur_i < 19 && v[cur_i][cur_j] == '0') {\n                cur_i++;\n            }\n        } else if (move == 'L') {\n            if (cur_j > 0 && h[cur_i][cur_j-1] == '0') {\n                cur_j--;\n            }\n        } else if (move == 'R') {\n            if (cur_j < 19 && h[cur_i][cur_j] == '0') {\n                cur_j++;\n            }\n        }\n    }\n\n    double score_dp = evaluate_route(route_dp, s_i, s_j, t_i, t_j, p, h, v);\n    candidates.push_back({route_dp, score_dp});\n\n    double score_bfs = evaluate_route(extended_bfs, s_i, s_j, t_i, t_j, p, h, v);\n    candidates.push_back({extended_bfs, score_bfs});\n\n    string best_route = extended_bfs;\n    double best_score = -1;\n    for (auto [route, score] : candidates) {\n        if (score > best_score) {\n            best_score = score;\n            best_route = route;\n        }\n    }\n\n    cout << best_route << endl;\n\n    return 0;\n}","ahc010":"#include <iostream>\n#include <vector>\n#include <algorithm>\n\nusing namespace std;\n\nconst vector<int> di = {0, -1, 0, 1};\nconst vector<int> dj = {-1, 0, 1, 0};\n\nconst vector<vector<int>> to_table = {\n    {1, 0, -1, -1},\n    {3, -1, -1, 0},\n    {-1, -1, 3, 2},\n    {-1, 2, 1, -1},\n    {1, 0, 3, 2},\n    {3, 2, 1, 0},\n    {2, -1, 0, -1},\n    {-1, 3, -1, 1}\n};\n\nlong long compute_score(const vector<vector<int>>& grid, const vector<vector<int>>& rot) {\n    int n = 30;\n    vector<vector<int>> eff(n, vector<int>(n));\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            int t = grid[i][j];\n            int r = rot[i][j];\n            if (t < 4) {\n                eff[i][j] = (t + r) % 4;\n            } else if (t < 6) {\n                eff[i][j] = (r % 2 == 0) ? t : (t ^ 1);\n            } else {\n                eff[i][j] = (r % 2 == 0) ? t : (t ^ 1);\n            }\n        }\n    }\n\n    vector<vector<vector<bool>>> visited(n, vector<vector<bool>>(n, vector<bool>(4, false)));\n    vector<long long> loops;\n\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            for (int d = 0; d < 4; d++) {\n                if (visited[i][j][d]) continue;\n\n                int ci = i, cj = j, cd = d;\n                long long steps = 0;\n                bool found_loop = false;\n                vector<vector<vector<bool>>> current_visited(n, vector<vector<bool>>(n, vector<bool>(4, false)));\n\n                while (true) {\n                    if (current_visited[ci][cj][cd]) break;\n                    current_visited[ci][cj][cd] = true;\n                    visited[ci][cj][cd] = true;\n\n                    int eff_tile = eff[ci][cj];\n                    int d2 = to_table[eff_tile][cd];\n                    if (d2 == -1) break;\n\n                    int ni = ci + di[d2];\n                    int nj = cj + dj[d2];\n                    if (ni < 0 || ni >= n || nj < 0 || nj >= n) break;\n\n                    int nd = (d2 + 2) % 4;\n                    steps++;\n\n                    if (ni == i && nj == j && nd == d) {\n                        found_loop = true;\n                        break;\n                    }\n\n                    ci = ni;\n                    cj = nj;\n                    cd = nd;\n                }\n\n                if (found_loop) {\n                    loops.push_back(steps);\n                }\n            }\n        }\n    }\n\n    if (loops.size() < 2) {\n        return 0;\n    }\n\n    sort(loops.rbegin(), loops.rend());\n    return (long long)loops[0] * loops[1];\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int num_test_cases = 100;\n    for (int tc = 0; tc < num_test_cases; tc++) {\n        vector<vector<int>> grid(30, vector<int>(30));\n        for (int i = 0; i < 30; i++) {\n            string s;\n            cin >> s;\n            for (int j = 0; j < 30; j++) {\n                grid[i][j] = s[j] - '0';\n            }\n        }\n\n        vector<vector<int>> rot(30, vector<int>(30));\n        for (int i = 0; i < 30; i++) {\n            for (int j = 0; j < 30; j++) {\n                rot[i][j] = (i + j) % 4;\n            }\n        }\n\n        for (int i = 0; i < 30; i++) {\n            for (int j = 0; j < 30; j++) {\n                cout << rot[i][j];\n            }\n        }\n        cout << '\\n';\n    }\n\n    return 0;\n}","ahc011":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\n#include <tuple>\n#include <map>\n#include <climits>\n#include <array>\nusing namespace std;\n\nconst int BEAM_WIDTH = 50;\nconst int dx[4] = {0, -1, 0, 1};\nconst int dy[4] = {1, 0, -1, 0};\nconst int opposite_dir[4] = {2, 3, 0, 1};\nconst int bitmask[4] = {4, 2, 1, 8};\n\nstruct State {\n    vector<vector<int>> board;\n    int empty_r, empty_c;\n    string moves;\n    char last_move;\n    int S, C, comp_count, dist;\n};\n\ntuple<int, int, int> evaluate(vector<vector<int>>& board, int N) {\n    array<array<bool, 10>, 10> visited = {};\n    array<array<bool, 10>, 10> in_comp = {};\n    int total_edges = 0;\n    int max_tree_size = 0;\n    int comp_count = 0;\n\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (board[i][j] == 0) {\n                visited[i][j] = true;\n            } else {\n                visited[i][j] = false;\n            }\n        }\n    }\n\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (board[i][j] == 0) continue;\n            if (j < N-1 && board[i][j+1] != 0) {\n                if ((board[i][j] & 4) && (board[i][j+1] & 1)) {\n                    total_edges++;\n                }\n            }\n            if (i < N-1 && board[i+1][j] != 0) {\n                if ((board[i][j] & 8) && (board[i+1][j] & 2)) {\n                    total_edges++;\n                }\n            }\n        }\n    }\n\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (!visited[i][j] && board[i][j] != 0) {\n                comp_count++;\n                queue<pair<int, int>> q;\n                vector<pair<int, int>> component;\n                q.push({i, j});\n                visited[i][j] = true;\n                in_comp[i][j] = true;\n                component.push_back({i, j});\n\n                while (!q.empty()) {\n                    auto [r, c] = q.front();\n                    q.pop();\n                    for (int d = 0; d < 4; d++) {\n                        int nr = r + dx[d];\n                        int nc = c + dy[d];\n                        if (nr < 0 || nr >= N || nc < 0 || nc >= N) continue;\n                        if (visited[nr][nc]) continue;\n                        if (board[nr][nc] == 0) continue;\n                        if (!(board[r][c] & bitmask[d])) continue;\n                        if (!(board[nr][nc] & bitmask[opposite_dir[d]])) continue;\n                        visited[nr][nc] = true;\n                        in_comp[nr][nc] = true;\n                        q.push({nr, nc});\n                        component.push_back({nr, nc});\n                    }\n                }\n\n                int edge_count = 0;\n                for (auto [r, c] : component) {\n                    for (int d = 0; d < 4; d++) {\n                        int nr = r + dx[d];\n                        int nc = c + dy[d];\n                        if (nr < 0 || nr >= N || nc < 0 || nc >= N) continue;\n                        if (in_comp[nr][nc] && (board[r][c] & bitmask[d])) {\n                            edge_count++;\n                        }\n                    }\n                }\n                edge_count /= 2;\n\n                int V = component.size();\n                if (edge_count == V - 1) {\n                    max_tree_size = max(max_tree_size, V);\n                }\n\n                for (auto [r, c] : component) {\n                    in_comp[r][c] = false;\n                }\n            }\n        }\n    }\n\n    if (max_tree_size == 0) {\n        max_tree_size = 1;\n    }\n\n    return {max_tree_size, total_edges, comp_count};\n}\n\nint main() {\n    int N, T_val;\n    cin >> N >> T_val;\n    vector<string> input_grid(N);\n    for (int i = 0; i < N; i++) {\n        cin >> input_grid[i];\n    }\n\n    vector<vector<int>> initial_board(N, vector<int>(N));\n    int empty_r = -1, empty_c = -1;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            char c = input_grid[i][j];\n            if (c >= '0' && c <= '9') {\n                initial_board[i][j] = c - '0';\n            } else if (c >= 'a' && c <= 'f') {\n                initial_board[i][j] = 10 + (c - 'a');\n            } else {\n                initial_board[i][j] = 0;\n            }\n            if (initial_board[i][j] == 0) {\n                empty_r = i;\n                empty_c = j;\n            }\n        }\n    }\n\n    auto [S0, C0, comp0] = evaluate(initial_board, N);\n    int dist0 = abs(empty_r - (N-1)) + abs(empty_c - (N-1));\n    State initial_state = {initial_board, empty_r, empty_c, \"\", ' ', S0, C0, comp0, dist0};\n\n    vector<State> beam;\n    beam.push_back(initial_state);\n\n    string final_sequence = \"\";\n    int min_full_moves = INT_MAX;\n    bool found_full_tree = false;\n\n    map<char, char> reverse_move = {{'U','D'}, {'D','U'}, {'L','R'}, {'R','L'}};\n    map<char, pair<int, int>> dir = {{'U',{-1,0}}, {'D',{1,0}}, {'L',{0,-1}}, {'R',{0,1}}};\n\n    for (int step = 0; step < T_val; step++) {\n        vector<State> candidates;\n        int min_in_step = INT_MAX;\n        string best_in_step = \"\";\n\n        for (const State& state : beam) {\n            for (const char move : {'U','D','L','R'}) {\n                if (state.last_move != ' ' && move == reverse_move[state.last_move]) {\n                    continue;\n                }\n\n                int dr = dir[move].first;\n                int dc = dir[move].second;\n                int new_r = state.empty_r + dr;\n                int new_c = state.empty_c + dc;\n\n                if (new_r < 0 || new_r >= N || new_c < 0 || new_c >= N) {\n                    continue;\n                }\n\n                State new_state = state;\n                swap(new_state.board[state.empty_r][state.empty_c], new_state.board[new_r][new_c]);\n                new_state.empty_r = new_r;\n                new_state.empty_c = new_c;\n                new_state.moves = state.moves + move;\n                new_state.last_move = move;\n                \n                tie(new_state.S, new_state.C, new_state.comp_count) = evaluate(new_state.board, N);\n                new_state.dist = abs(new_r - (N-1)) + abs(new_c - (N-1));\n                \n                candidates.push_back(new_state);\n\n                if (new_state.S == N*N-1) {\n                    int move_count = new_state.moves.size();\n                    if (move_count < min_in_step) {\n                        min_in_step = move_count;\n                        best_in_step = new_state.moves;\n                    }\n                }\n            }\n        }\n\n        if (min_in_step < min_full_moves) {\n            min_full_moves = min_in_step;\n            final_sequence = best_in_step;\n            found_full_tree = true;\n        }\n\n        if (candidates.empty()) {\n            break;\n        }\n\n        sort(candidates.begin(), candidates.end(), [](const State& a, const State& b) {\n            if (a.S != b.S) return a.S > b.S;\n            if (a.C != b.C) return a.C > b.C;\n            if (a.comp_count != b.comp_count) return a.comp_count < b.comp_count;\n            return a.dist < b.dist;\n        });\n\n        beam.clear();\n        int width = min(BEAM_WIDTH, (int)candidates.size());\n        for (int i = 0; i < width; i++) {\n            beam.push_back(candidates[i]);\n        }\n\n        if (found_full_tree) {\n            break;\n        }\n    }\n\n    if (!found_full_tree && !beam.empty()) {\n        sort(beam.begin(), beam.end(), [](const State& a, const State& b) {\n            if (a.S != b.S) return a.S > b.S;\n            if (a.C != b.C) return a.C > b.C;\n            if (a.comp_count != b.comp_count) return a.comp_count < b.comp_count;\n            return a.dist < b.dist;\n        });\n        final_sequence = beam[0].moves;\n    }\n\n    cout << final_sequence << endl;\n\n    return 0;\n}","ahc012":"#include <bits/stdc++.h>\n#include <random>\nusing namespace std;\ntypedef long long ll;\ntypedef __int128 i128;\n\nconst int B = 20000;\nconst ll R2 = 100000000;\n\nstruct Point {\n    ll x, y;\n    Point() {}\n    Point(ll x, ll y) : x(x), y(y) {}\n    bool operator==(const Point& other) const {\n        return x == other.x && y == other.y;\n    }\n};\n\nstruct Line {\n    Point p, q;\n    ll a, b, c;\n    Line(Point p1, Point p2) : p(p1), q(p2) {\n        a = p.y - q.y;\n        b = q.x - p.x;\n        c = p.x * q.y - q.x * p.y;\n    }\n};\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    random_device rd;\n    mt19937 gen(rd());\n    uniform_int_distribution<int> distrib(-B, B);\n\n    const int CANDIDATE_SIZE = 1000;\n    const int num_test_cases = 100;\n    for (int tc = 0; tc < num_test_cases; tc++) {\n        int N, K;\n        cin >> N >> K;\n        vector<int> a(11, 0);\n        for (int i = 1; i <= 10; i++) {\n            cin >> a[i];\n        }\n        vector<Point> points;\n        for (int i = 0; i < N; i++) {\n            ll x, y;\n            cin >> x >> y;\n            points.push_back(Point(x, y));\n        }\n\n        vector<Line> candidate_lines;\n        vector<bool> used;\n        for (int i = 0; i < CANDIDATE_SIZE; ) {\n            bool found = false;\n            for (int t = 0; t < 100; t++) {\n                Point p(distrib(gen), distrib(gen));\n                Point q(distrib(gen), distrib(gen));\n                if (p == q) continue;\n                Line L(p, q);\n                i128 a_val = L.a;\n                i128 b_val = L.b;\n                i128 c_val = L.c;\n                i128 lhs = c_val * c_val;\n                i128 rhs = R2 * (a_val * a_val + b_val * b_val);\n                if (lhs > rhs) continue;\n\n                bool valid = true;\n                for (const Point& s : points) {\n                    ll D = L.a * s.x + L.b * s.y + L.c;\n                    if (D == 0) {\n                        valid = false;\n                        break;\n                    }\n                }\n                if (valid) {\n                    candidate_lines.push_back(L);\n                    found = true;\n                    i++;\n                    break;\n                }\n            }\n            if (!found) {\n                break;\n            }\n        }\n        used.assign(candidate_lines.size(), false);\n\n        vector<vector<int>> current_groups;\n        if (N > 0) {\n            current_groups.push_back(vector<int>());\n            for (int i = 0; i < N; i++) {\n                current_groups[0].push_back(i);\n            }\n        }\n\n        vector<int> current_b_d(11, 0);\n        if (N >= 1 && N <= 10) {\n            current_b_d[N] = 1;\n        }\n        int current_score = 0;\n        for (int d = 1; d <= 10; d++) {\n            current_score += min(a[d], current_b_d[d]);\n        }\n\n        vector<Line> chosen_lines;\n\n        for (int step = 0; step < K; step++) {\n            int best_next_score = current_score;\n            int best_candidate = -1;\n\n            if (candidate_lines.empty()) break;\n\n            for (int idx = 0; idx < candidate_lines.size(); idx++) {\n                if (used[idx]) continue;\n                const Line& L = candidate_lines[idx];\n                vector<int> new_b_d(11, 0);\n                for (const vector<int>& group : current_groups) {\n                    int cnt0 = 0, cnt1 = 0;\n                    for (int sid : group) {\n                        const Point& s = points[sid];\n                        ll D = L.a * s.x + L.b * s.y + L.c;\n                        if (D > 0) cnt1++;\n                        else cnt0++;\n                    }\n                    if (cnt0 > 0 && cnt0 <= 10) new_b_d[cnt0]++;\n                    if (cnt1 > 0 && cnt1 <= 10) new_b_d[cnt1]++;\n                }\n                int score = 0;\n                for (int d = 1; d <= 10; d++) {\n                    score += min(a[d], new_b_d[d]);\n                }\n                if (score > best_next_score) {\n                    best_next_score = score;\n                    best_candidate = idx;\n                }\n            }\n\n            if (best_candidate == -1) break;\n\n            used[best_candidate] = true;\n            const Line& L = candidate_lines[best_candidate];\n            chosen_lines.push_back(L);\n\n            vector<vector<int>> new_groups;\n            for (const vector<int>& group : current_groups) {\n                vector<int> group0, group1;\n                for (int sid : group) {\n                    const Point& s = points[sid];\n                    ll D = L.a * s.x + L.b * s.y + L.c;\n                    if (D > 0) group1.push_back(sid);\n                    else group0.push_back(sid);\n                }\n                if (!group0.empty()) new_groups.push_back(group0);\n                if (!group1.empty()) new_groups.push_back(group1);\n            }\n            current_groups = move(new_groups);\n\n            current_b_d.assign(11, 0);\n            for (const auto& g : current_groups) {\n                int s = g.size();\n                if (s >= 1 && s <= 10) current_b_d[s]++;\n            }\n            current_score = best_next_score;\n        }\n\n        cout << chosen_lines.size() << endl;\n        for (const Line& L : chosen_lines) {\n            cout << L.p.x << \" \" << L.p.y << \" \" << L.q.x << \" \" << L.q.y << endl;\n        }\n    }\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    cin >> N >> M;\n    vector<vector<bool>> dot(N, vector<bool>(N, false));\n    vector<set<int>> rows(N);\n    vector<set<int>> cols(N);\n\n    vector<vector<int>> next_in_row(N, vector<int>(N, N));\n    vector<vector<int>> prev_in_row(N, vector<int>(N, -1));\n    vector<vector<int>> next_in_col(N, vector<int>(N, N));\n    vector<vector<int>> prev_in_col(N, vector<int>(N, -1));\n\n    vector<vector<bool>> hor(N, vector<bool>(N, false));\n    vector<vector<bool>> ver(N, vector<bool>(N, false));\n\n    for (int i = 0; i < M; i++) {\n        int x, y;\n        cin >> x >> y;\n        dot[x][y] = true;\n        rows[y].insert(x);\n        cols[x].insert(y);\n    }\n\n    for (int y = 0; y < N; y++) {\n        int last = -1;\n        for (int x = 0; x < N; x++) {\n            if (dot[x][y]) {\n                last = x;\n            }\n            prev_in_row[y][x] = last;\n        }\n        last = N;\n        for (int x = N-1; x >= 0; x--) {\n            if (dot[x][y]) {\n                last = x;\n            }\n            next_in_row[y][x] = last;\n        }\n    }\n\n    for (int x = 0; x < N; x++) {\n        int last = -1;\n        for (int y = 0; y < N; y++) {\n            if (dot[x][y]) {\n                last = y;\n            }\n            prev_in_col[x][y] = last;\n        }\n        last = N;\n        for (int y = N-1; y >= 0; y--) {\n            if (dot[x][y]) {\n                last = y;\n            }\n            next_in_col[x][y] = last;\n        }\n    }\n\n    int c = (N - 1) / 2;\n    priority_queue<tuple<int, int, int>> pq;\n    for (int x = 0; x < N; x++) {\n        for (int y = 0; y < N; y++) {\n            if (!dot[x][y]) {\n                int weight = (x - c) * (x - c) + (y - c) * (y - c) + 1;\n                pq.push(make_tuple(weight, x, y));\n            }\n        }\n    }\n\n    vector<array<int, 8>> operations;\n    auto start_time = chrono::high_resolution_clock::now();\n\n    while (!pq.empty()) {\n        auto now = chrono::high_resolution_clock::now();\n        auto duration = chrono::duration_cast<chrono::milliseconds>(now - start_time);\n        if (duration.count() > 4500) {\n            break;\n        }\n\n        auto top = pq.top();\n        int w = get<0>(top);\n        int x1 = get<1>(top);\n        int y1 = get<2>(top);\n        pq.pop();\n        if (dot[x1][y1]) {\n            continue;\n        }\n\n        bool found = false;\n        const auto& col_x1 = cols[x1];\n        for (int y2 : col_x1) {\n            if (y2 == y1) continue;\n            int y_min = min(y1, y2);\n            int y_max = max(y1, y2);\n\n            if (next_in_col[x1][y_min + 1] < y_max) {\n                continue;\n            }\n\n            set<int> common;\n            const set<int>& row_y1 = rows[y1];\n            const set<int>& row_y2 = rows[y2];\n            if (row_y1.size() < row_y2.size()) {\n                for (int x : row_y1) {\n                    if (row_y2.find(x) != row_y2.end()) {\n                        common.insert(x);\n                    }\n                }\n            } else {\n                for (int x : row_y2) {\n                    if (row_y1.find(x) != row_y1.end()) {\n                        common.insert(x);\n                    }\n                }\n            }\n\n            for (int x2 : common) {\n                if (x2 == x1) continue;\n                int x_min = min(x1, x2);\n                int x_max = max(x1, x2);\n\n                if (next_in_row[y1][x_min + 1] < x_max) {\n                    continue;\n                }\n                if (next_in_row[y2][x_min + 1] < x_max) {\n                    continue;\n                }\n                if (next_in_col[x2][y_min + 1] < y_max) {\n                    continue;\n                }\n\n                bool edgeValid = true;\n                for (int y = y_min; y < y_max; y++) {\n                    if (y < N - 1 && ver[x1][y]) {\n                        edgeValid = false;\n                        break;\n                    }\n                }\n                if (!edgeValid) continue;\n\n                for (int y = y_min; y < y_max; y++) {\n                    if (y < N - 1 && ver[x2][y]) {\n                        edgeValid = false;\n                        break;\n                    }\n                }\n                if (!edgeValid) continue;\n\n                for (int x = x_min; x < x_max; x++) {\n                    if (x < N - 1 && hor[x][y_min]) {\n                        edgeValid = false;\n                        break;\n                    }\n                }\n                if (!edgeValid) continue;\n\n                for (int x = x_min; x < x_max; x++) {\n                    if (x < N - 1 && hor[x][y_max]) {\n                        edgeValid = false;\n                        break;\n                    }\n                }\n                if (!edgeValid) continue;\n\n                operations.push_back({x1, y1, x1, y2, x2, y2, x2, y1});\n                dot[x1][y1] = true;\n                rows[y1].insert(x1);\n                cols[x1].insert(y1);\n\n                int last_val = -1;\n                for (int x = 0; x < N; x++) {\n                    if (dot[x][y1]) {\n                        last_val = x;\n                    }\n                    prev_in_row[y1][x] = last_val;\n                }\n                last_val = N;\n                for (int x = N - 1; x >= 0; x--) {\n                    if (dot[x][y1]) {\n                        last_val = x;\n                    }\n                    next_in_row[y1][x] = last_val;\n                }\n\n                last_val = -1;\n                for (int y = 0; y < N; y++) {\n                    if (dot[x1][y]) {\n                        last_val = y;\n                    }\n                    prev_in_col[x1][y] = last_val;\n                }\n                last_val = N;\n                for (int y = N - 1; y >= 0; y--) {\n                    if (dot[x1][y]) {\n                        last_val = y;\n                    }\n                    next_in_col[x1][y] = last_val;\n                }\n\n                for (int y = y_min; y < y_max; y++) {\n                    if (y < N - 1) {\n                        ver[x1][y] = true;\n                    }\n                }\n                for (int y = y_min; y < y_max; y++) {\n                    if (y < N - 1) {\n                        ver[x2][y] = true;\n                    }\n                }\n                for (int x = x_min; x < x_max; x++) {\n                    if (x < N - 1) {\n                        hor[x][y_min] = true;\n                    }\n                }\n                for (int x = x_min; x < x_max; x++) {\n                    if (x < N - 1) {\n                        hor[x][y_max] = true;\n                    }\n                }\n\n                found = true;\n                break;\n            }\n            if (found) break;\n        }\n    }\n\n    cout << operations.size() << endl;\n    for (auto& op : operations) {\n        for (int i = 0; i < 8; i++) {\n            if (i > 0) cout << \" \";\n            cout << op[i];\n        }\n        cout << endl;\n    }\n\n    return 0;\n}","ahc015":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <climits>\n#include <algorithm>\nusing namespace std;\n\nvector<vector<int>> simulate_tilt(vector<vector<int>> grid, char dir) {\n    vector<vector<int>> new_grid(10, vector<int>(10, 0));\n    if (dir == 'L' || dir == 'R') {\n        for (int i = 0; i < 10; i++) {\n            vector<int> non_empty;\n            for (int j = 0; j < 10; j++) {\n                if (grid[i][j] != 0) {\n                    non_empty.push_back(grid[i][j]);\n                }\n            }\n            if (dir == 'L') {\n                for (int j = 0; j < non_empty.size(); j++) {\n                    new_grid[i][j] = non_empty[j];\n                }\n            } else {\n                int start = 10 - non_empty.size();\n                for (int j = 0; j < non_empty.size(); j++) {\n                    new_grid[i][start + j] = non_empty[j];\n                }\n            }\n        }\n    } else {\n        for (int j = 0; j < 10; j++) {\n            vector<int> non_empty;\n            for (int i = 0; i < 10; i++) {\n                if (grid[i][j] != 0) {\n                    non_empty.push_back(grid[i][j]);\n                }\n            }\n            if (dir == 'F') {\n                for (int i = 0; i < non_empty.size(); i++) {\n                    new_grid[i][j] = non_empty[i];\n                }\n            } else {\n                int start = 10 - non_empty.size();\n                for (int i = 0; i < non_empty.size(); i++) {\n                    new_grid[start + i][j] = non_empty[i];\n                }\n            }\n        }\n    }\n    return new_grid;\n}\n\nint compute_manhattan(vector<vector<int>>& grid, const vector<int>& penalty_type) {\n    int penalty = 0;\n    for (int i = 0; i < 10; i++) {\n        for (int j = 0; j < 10; j++) {\n            if (grid[i][j] != 0) {\n                int f = grid[i][j];\n                int t = penalty_type[f];\n                if (t == 0) {\n                    penalty += i + j;\n                } else if (t == 1) {\n                    penalty += i + (9 - j);\n                } else {\n                    penalty += (9 - i) + j;\n                }\n            }\n        }\n    }\n    return penalty;\n}\n\nint compute_score(vector<vector<int>>& grid) {\n    int n = 10;\n    vector<vector<bool>> visited(n, vector<bool>(n, false));\n    int total_score = 0;\n    vector<int> dr = {1, -1, 0, 0};\n    vector<int> dc = {0, 0, 1, -1};\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            if (!visited[i][j] && grid[i][j] != 0) {\n                int flavor = grid[i][j];\n                int count = 0;\n                queue<pair<int, int>> q;\n                q.push({i, j});\n                visited[i][j] = true;\n                while (!q.empty()) {\n                    auto cell = q.front();\n                    q.pop();\n                    count++;\n                    int r = cell.first;\n                    int c = cell.second;\n                    for (int d = 0; d < 4; d++) {\n                        int nr = r + dr[d];\n                        int nc = c + dc[d];\n                        if (nr >= 0 && nr < n && nc >= 0 && nc < n && !visited[nr][nc] && grid[nr][nc] == flavor) {\n                            visited[nr][nc] = true;\n                            q.push({nr, nc});\n                        }\n                    }\n                }\n                total_score += count * count;\n            }\n        }\n    }\n    return total_score;\n}\n\nint compute_pairs(vector<vector<int>>& grid) {\n    int pairs = 0;\n    for (int i = 0; i < 10; i++) {\n        for (int j = 0; j < 10; j++) {\n            if (grid[i][j] != 0) {\n                if (j < 9 && grid[i][j] == grid[i][j+1]) {\n                    pairs++;\n                }\n                if (i < 9 && grid[i][j] == grid[i+1][j]) {\n                    pairs++;\n                }\n            }\n        }\n    }\n    return pairs;\n}\n\nint main() {\n    vector<int> flavors;\n    for (int i = 0; i < 100; i++) {\n        int f;\n        cin >> f;\n        flavors.push_back(f);\n    }\n\n    vector<int> flavor_counts(4, 0);\n    for (int f : flavors) {\n        if (f >= 1 && f <= 3) {\n            flavor_counts[f]++;\n        }\n    }\n    vector<int> order = {1, 2, 3};\n    sort(order.begin(), order.end(), [&](int a, int b) {\n        return flavor_counts[a] > flavor_counts[b];\n    });\n    vector<int> penalty_type(4);\n    penalty_type[order[0]] = 0;\n    penalty_type[order[1]] = 1;\n    penalty_type[order[2]] = 2;\n\n    vector<vector<int>> grid(10, vector<int>(10, 0));\n    vector<char> dirs = {'L', 'R', 'F', 'B'};\n\n    for (int t = 0; t < 100; t++) {\n        int p;\n        cin >> p;\n\n        vector<pair<int, int>> emptyCells;\n        for (int i = 0; i < 10; i++) {\n            for (int j = 0; j < 10; j++) {\n                if (grid[i][j] == 0) {\n                    emptyCells.push_back(make_pair(i, j));\n                }\n            }\n        }\n        sort(emptyCells.begin(), emptyCells.end(), [](const pair<int, int>& a, const pair<int, int>& b) {\n            if (a.first != b.first) return a.first < b.first;\n            return a.second < b.second;\n        });\n        int r = emptyCells[p-1].first;\n        int c = emptyCells[p-1].second;\n        grid[r][c] = flavors[t];\n\n        int best_score = -1;\n        int best_pairs = -1;\n        int best_penalty = INT_MAX;\n        char best_dir = 'L';\n\n        for (char dir : dirs) {\n            vector<vector<int>> new_grid = simulate_tilt(grid, dir);\n            int score = compute_score(new_grid);\n            int pairs = compute_pairs(new_grid);\n            int penalty = compute_manhattan(new_grid, penalty_type);\n            \n            if (score > best_score) {\n                best_score = score;\n                best_pairs = pairs;\n                best_penalty = penalty;\n                best_dir = dir;\n            } else if (score == best_score) {\n                if (pairs > best_pairs) {\n                    best_pairs = pairs;\n                    best_penalty = penalty;\n                    best_dir = dir;\n                } else if (pairs == best_pairs) {\n                    if (penalty < best_penalty) {\n                        best_penalty = penalty;\n                        best_dir = dir;\n                    }\n                }\n            }\n        }\n\n        cout << best_dir << endl;\n        cout.flush();\n\n        grid = simulate_tilt(grid, best_dir);\n    }\n\n    return 0;\n}","ahc016":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <cmath>\nusing namespace std;\n\nstruct CliquePair {\n    int a, b;\n    long long total_edges;\n};\n\nint main() {\n    int M;\n    double eps;\n    cin >> M >> eps;\n\n    const int n = 40;\n    const int L = n * (n - 1) / 2;\n\n    vector<CliquePair> allPairs;\n    for (int a = 1; a <= n; a++) {\n        for (int b = 1; b <= a; b++) {\n            if (a + b <= n) {\n                long long edges = (long long)a * (a - 1) / 2 + (long long)b * (b - 1) / 2;\n                allPairs.push_back({a, b, edges});\n            }\n        }\n    }\n\n    sort(allPairs.begin(), allPairs.end(), [](const CliquePair& x, const CliquePair& y) {\n        return x.total_edges < y.total_edges;\n    });\n\n    vector<CliquePair> selectedPairs;\n    if (M == 1) {\n        selectedPairs.push_back(allPairs[0]);\n    } else {\n        double step = (allPairs.size() - 1.0) / (M - 1);\n        for (int i = 0; i < M; i++) {\n            int idx = min(static_cast<int>(i * step + 0.5), static_cast<int>(allPairs.size() - 1));\n            selectedPairs.push_back(allPairs[idx]);\n        }\n    }\n\n    cout << n << endl;\n    vector<vector<double>> expected_degrees(M, vector<double>(n));\n\n    for (int i = 0; i < M; i++) {\n        int a = selectedPairs[i].a;\n        int b = selectedPairs[i].b;\n        int c = n - a - b;\n        string s = \"\";\n        \n        for (int u = 0; u < n; u++) {\n            for (int v = u + 1; v < n; v++) {\n                if ((u < a && v < a) || \n                    (u >= a && u < a+b && v >= a && v < a+b)) {\n                    s += '1';\n                } else {\n                    s += '0';\n                }\n            }\n        }\n        cout << s << endl;\n\n        vector<double> deg;\n        for (int j = 0; j < c; j++) \n            deg.push_back(eps * (n-1));\n        for (int j = 0; j < b; j++)\n            deg.push_back((b-1)*(1-2*eps) + eps*(n-1));\n        for (int j = 0; j < a; j++)\n            deg.push_back((a-1)*(1-2*eps) + eps*(n-1));\n        \n        expected_degrees[i] = deg;\n    }\n    cout.flush();\n\n    vector<int> degH(n);\n    for (int q = 0; q < 100; q++) {\n        string H;\n        cin >> H;\n\n        fill(degH.begin(), degH.end(), 0);\n        int idx = 0;\n        for (int u = 0; u < n; u++) {\n            for (int v = u + 1; v < n; v++) {\n                if (H[idx] == '1') {\n                    degH[u]++;\n                    degH[v]++;\n                }\n                idx++;\n            }\n        }\n        sort(degH.begin(), degH.end());\n\n        double minDist = 1e18;\n        int best_i = 0;\n        for (int i = 0; i < M; i++) {\n            double dist = 0.0;\n            for (int j = 0; j < n; j++) {\n                double diff = expected_degrees[i][j] - degH[j];\n                dist += diff * diff;\n            }\n            if (dist < minDist) {\n                minDist = dist;\n                best_i = i;\n            }\n        }\n\n        cout << best_i << endl;\n        cout.flush();\n    }\n\n    return 0;\n}","ahc017":"#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, D, K;\n    cin >> N >> M >> D >> K;\n    vector<tuple<int, int, int>> edges;\n    vector<vector<tuple<int, int, int>>> adj(N);\n    map<pair<int, int>, int> edgeMap;\n\n    for (int i = 0; i < M; i++) {\n        int u, v, w;\n        cin >> u >> v >> w;\n        u--; v--;\n        edges.push_back({u, v, w});\n        adj[u].push_back({v, w, i});\n        adj[v].push_back({u, w, i});\n        int a = min(u, v), b = max(u, v);\n        edgeMap[{a, b}] = i;\n    }\n    for (int i = 0; i < N; i++) {\n        int x, y;\n        cin >> x >> y;\n    }\n\n    vector<double> bc(M, 0.0);\n\n    for (int s = 0; s < N; s++) {\n        vector<ll> dist(N, 1e18);\n        vector<int> numPaths(N, 0);\n        vector<vector<int>> pred(N);\n        vector<int> order;\n\n        dist[s] = 0;\n        numPaths[s] = 1;\n        priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;\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            order.push_back(u);\n            for (auto [v, w, eid] : adj[u]) {\n                ll nd = d + w;\n                if (nd < dist[v]) {\n                    dist[v] = nd;\n                    numPaths[v] = numPaths[u];\n                    pred[v] = {u};\n                    pq.push({nd, v});\n                } else if (nd == dist[v]) {\n                    numPaths[v] += numPaths[u];\n                    pred[v].push_back(u);\n                }\n            }\n        }\n\n        vector<double> dep(N, 0.0);\n        for (int i = order.size() - 1; i >= 0; i--) {\n            int w = order[i];\n            for (int u : pred[w]) {\n                if (numPaths[w] == 0) continue;\n                double frac = static_cast<double>(numPaths[u]) / numPaths[w] * (1.0 + dep[w]);\n                int a = min(u, w);\n                int b = max(u, w);\n                auto it = edgeMap.find({a, b});\n                if (it == edgeMap.end()) continue;\n                int eid = it->second;\n                bc[eid] += frac;\n                dep[u] += frac;\n            }\n        }\n    }\n\n    vector<int> assignment(M, -1);\n    vector<int> day_count(D, 0);\n    vector<double> day_sum(D, 0.0);\n\n    vector<int> idx_edges(M);\n    iota(idx_edges.begin(), idx_edges.end(), 0);\n    sort(idx_edges.begin(), idx_edges.end(), [&](int i, int j) { return bc[i] > bc[j]; });\n\n    for (int i : idx_edges) {\n        int best_d = -1;\n        double min_sum = 1e18;\n        for (int d = 0; d < D; d++) {\n            if (day_count[d] < K) {\n                if (day_sum[d] < min_sum) {\n                    min_sum = day_sum[d];\n                    best_d = d;\n                }\n            }\n        }\n        if (best_d == -1) {\n            for (int d = 0; d < D; d++) {\n                if (day_count[d] < K) {\n                    best_d = d;\n                    break;\n                }\n            }\n            if (best_d == -1) {\n                best_d = 0;\n            }\n        }\n        assignment[i] = best_d;\n        day_count[best_d]++;\n        day_sum[best_d] += bc[i];\n    }\n\n    vector<vector<int>> adj_uw(N);\n    for (int i = 0; i < M; i++) {\n        int u = get<0>(edges[i]);\n        int v = get<1>(edges[i]);\n        adj_uw[u].push_back(v);\n        adj_uw[v].push_back(u);\n    }\n\n    random_device rd;\n    mt19937 gen(rd());\n\n    for (int d = 0; d < D; d++) {\n        vector<vector<int>> adj_d(N);\n        for (int u = 0; u < N; u++) {\n            for (int v : adj_uw[u]) {\n                int a = min(u, v);\n                int b = max(u, v);\n                int eid = edgeMap[{a, b}];\n                if (assignment[eid] == d) continue;\n                adj_d[u].push_back(v);\n            }\n        }\n\n        vector<bool> vis(N, false);\n        queue<int> q;\n        q.push(0);\n        vis[0] = true;\n        int cnt = 0;\n        while (!q.empty()) {\n            int u = q.front(); q.pop();\n            cnt++;\n            for (int v : adj_d[u]) {\n                if (!vis[v]) {\n                    vis[v] = true;\n                    q.push(v);\n                }\n            }\n        }\n        if (cnt == N) continue;\n\n        vector<int> edges_d;\n        for (int i = 0; i < M; i++) {\n            if (assignment[i] == d) {\n                edges_d.push_back(i);\n            }\n        }\n\n        bool fixed = false;\n        if (edges_d.empty()) continue;\n\n        for (int attempt = 0; attempt < 100; attempt++) {\n            uniform_int_distribution<int> dist_e(0, edges_d.size()-1);\n            int idx_e = dist_e(gen);\n            int e = edges_d[idx_e];\n\n            int d2 = gen() % D;\n            if (d2 == d) continue;\n\n            vector<int> edges_d2;\n            for (int i = 0; i < M; i++) {\n                if (assignment[i] == d2) {\n                    edges_d2.push_back(i);\n                }\n            }\n            if (edges_d2.empty()) continue;\n\n            uniform_int_distribution<int> dist_f(0, edges_d2.size()-1);\n            int idx_f = dist_f(gen);\n            int f = edges_d2[idx_f];\n\n            assignment[e] = d2;\n            assignment[f] = d;\n\n            vector<vector<int>> adj_d1(N);\n            for (int u = 0; u < N; u++) {\n                for (int v : adj_uw[u]) {\n                    int a = min(u, v);\n                    int b = max(u, v);\n                    int eid_t = edgeMap[{a, b}];\n                    if (assignment[eid_t] == d) continue;\n                    adj_d1[u].push_back(v);\n                }\n            }\n            vector<bool> vis1(N, false);\n            queue<int> q1;\n            q1.push(0);\n            vis1[0] = true;\n            int cnt1 = 0;\n            while (!q1.empty()) {\n                int u = q1.front(); q1.pop();\n                cnt1++;\n                for (int v : adj_d1[u]) {\n                    if (!vis1[v]) {\n                        vis1[v] = true;\n                        q1.push(v);\n                    }\n                }\n            }\n            if (cnt1 != N) {\n                assignment[e] = d;\n                assignment[f] = d2;\n                continue;\n            }\n\n            vector<vector<int>> adj_d2(N);\n            for (int u = 0; u < N; u++) {\n                for (int v : adj_uw[u]) {\n                    int a = min(u, v);\n                    int b = max(u, v);\n                    int eid_t = edgeMap[{a, b}];\n                    if (assignment[eid_t] == d2) continue;\n                    adj_d2[u].push_back(v);\n                }\n            }\n            vector<bool> vis2(N, false);\n            queue<int> q2;\n            q2.push(0);\n            vis2[0] = true;\n            int cnt2 = 0;\n            while (!q2.empty()) {\n                int u = q2.front(); q2.pop();\n                cnt2++;\n                for (int v : adj_d2[u]) {\n                    if (!vis2[v]) {\n                        vis2[v] = true;\n                        q2.push(v);\n                    }\n                }\n            }\n            if (cnt2 != N) {\n                assignment[e] = d;\n                assignment[f] = d2;\n                continue;\n            }\n\n            fixed = true;\n            break;\n        }\n    }\n\n    for (int i = 0; i < M; i++) {\n        cout << assignment[i] + 1;\n        if (i < M-1) cout << \" \";\n    }\n    cout << endl;\n\n    return 0;\n}","ahc019":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <tuple>\n#include <algorithm>\n#include <set>\n#include <map>\nusing namespace std;\n\nconst int MAX_VOL = 8;\n\nstruct Rotation {\n    int perm[3];\n    int sign[3];\n};\n\nvector<Rotation> generate_rotations() {\n    vector<int> perm = {0,1,2};\n    vector<Rotation> rots;\n    do {\n        int inv = 0;\n        for (int i=0; i<3; i++)\n            for (int j=i+1; j<3; j++)\n                if (perm[i] > perm[j]) inv++;\n        int perm_sign = (inv%2==0) ? 1 : -1;\n\n        for (int s=0; s<8; s++) {\n            int sx = (s&1) ? -1 : 1;\n            int sy = (s&2) ? -1 : 1;\n            int sz = (s&4) ? -1 : 1;\n            if (perm_sign * sx * sy * sz == 1) {\n                Rotation r;\n                r.perm[0] = perm[0]; r.sign[0] = sx;\n                r.perm[1] = perm[1]; r.sign[1] = sy;\n                r.perm[2] = perm[2]; r.sign[2] = sz;\n                rots.push_back(r);\n            }\n        }\n    } while (next_permutation(perm.begin(), perm.end()));\n    return rots;\n}\n\nint main() {\n    vector<Rotation> rots = generate_rotations();\n\n    int D;\n    cin >> D;\n    vector<vector<string>> f(2, vector<string>(D));\n    vector<vector<string>> r(2, vector<string>(D));\n    for (int i=0; i<2; i++) {\n        for (int j=0; j<D; j++) {\n            cin >> f[i][j];\n        }\n        for (int j=0; j<D; j++) {\n            cin >> r[i][j];\n        }\n    }\n\n    vector<vector<vector<vector<bool>>>> Allowed(2, vector<vector<vector<bool>>>(D, vector<vector<bool>>(D, vector<bool>(D, false))));\n    vector<vector<vector<vector<bool>>>> Visited(2, vector<vector<vector<bool>>>(D, vector<vector<bool>>(D, vector<bool>(D, false))));\n    vector<vector<vector<tuple<int,int,int>>>> blocks(2);\n\n    vector<tuple<int,int,int>> dirs = {{1,0,0}, {-1,0,0}, {0,1,0}, {0,-1,0}, {0,0,1}, {0,0,-1}};\n\n    for (int obj=0; obj<2; obj++) {\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                    if (f[obj][z][x]=='1' && r[obj][z][y]=='1') {\n                        Allowed[obj][z][x][y] = true;\n                    }\n                }\n            }\n        }\n\n        vector<vector<tuple<int,int,int>>> components;\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 (Allowed[obj][z][x][y] && !Visited[obj][z][x][y]) {\n                        vector<tuple<int,int,int>> comp;\n                        queue<tuple<int,int,int>> q;\n                        q.push({x,y,z});\n                        Visited[obj][z][x][y] = true;\n                        while (!q.empty()) {\n                            auto [cx, cy, cz] = q.front();\n                            q.pop();\n                            comp.push_back({cx, cy, cz});\n                            for (auto [dx,dy,dz] : dirs) {\n                                int nx = cx + dx;\n                                int ny = cy + dy;\n                                int nz = cz + dz;\n                                if (nx>=0 && nx<D && ny>=0 && ny<D && nz>=0 && nz<D) {\n                                    if (Allowed[obj][nz][nx][ny] && !Visited[obj][nz][nx][ny]) {\n                                        Visited[obj][nz][nx][ny] = true;\n                                        q.push({nx, ny, nz});\n                                    }\n                                }\n                            }\n                        }\n                        components.push_back(comp);\n                    }\n                }\n            }\n        }\n\n        for (auto &comp : components) {\n            set<tuple<int,int,int>> comp_set(comp.begin(), comp.end());\n            while (!comp_set.empty()) {\n                tuple<int,int,int> best_point = *comp_set.begin();\n\n                vector<tuple<int,int,int>> block;\n                queue<tuple<int,int,int>> q;\n                q.push(best_point);\n                comp_set.erase(best_point);\n                block.push_back(best_point);\n\n                while (!q.empty() && block.size() < MAX_VOL) {\n                    auto [x,y,z] = q.front();\n                    q.pop();\n                    for (auto [dx,dy,dz] : dirs) {\n                        int nx = x + dx;\n                        int ny = y + dy;\n                        int nz = z + dz;\n                        tuple<int,int,int> np = {nx, ny, nz};\n                        if (comp_set.find(np) != comp_set.end()) {\n                            comp_set.erase(np);\n                            block.push_back(np);\n                            q.push(np);\n                            if (block.size() >= MAX_VOL) break;\n                        }\n                    }\n                }\n                blocks[obj].push_back(block);\n            }\n        }\n    }\n\n    vector<vector<vector<tuple<int,int,int>>>> canonical_blocks(2);\n    for (int obj=0; obj<2; obj++) {\n        for (auto &block : blocks[obj]) {\n            vector<vector<tuple<int,int,int>>> candidates;\n            for (auto &rot : rots) {\n                vector<tuple<int,int,int>> transformed;\n                for (auto [x,y,z] : block) {\n                    int coords[3] = {x, y, z};\n                    int nx = rot.sign[0] * coords[rot.perm[0]];\n                    int ny = rot.sign[1] * coords[rot.perm[1]];\n                    int nz = rot.sign[2] * coords[rot.perm[2]];\n                    transformed.push_back(make_tuple(nx, ny, nz));\n                }\n                int min_x = get<0>(transformed[0]), min_y = get<1>(transformed[0]), min_z = get<2>(transformed[0]);\n                for (auto &p : transformed) {\n                    min_x = min(min_x, get<0>(p));\n                    min_y = min(min_y, get<1>(p));\n                    min_z = min(min_z, get<2>(p));\n                }\n                vector<tuple<int,int,int>> shifted;\n                for (auto p : transformed) {\n                    shifted.push_back(make_tuple(get<0>(p)-min_x, get<1>(p)-min_y, get<2>(p)-min_z));\n                }\n                sort(shifted.begin(), shifted.end());\n                candidates.push_back(shifted);\n            }\n            sort(candidates.begin(), candidates.end());\n            canonical_blocks[obj].push_back(candidates[0]);\n        }\n    }\n\n    vector<tuple<int, vector<tuple<int,int,int>>, int>> list0;\n    for (size_t i=0; i<blocks[0].size(); i++) {\n        list0.push_back(make_tuple(blocks[0][i].size(), canonical_blocks[0][i], i));\n    }\n    vector<tuple<int, vector<tuple<int,int,int>>, int>> list1;\n    for (size_t i=0; i<blocks[1].size(); i++) {\n        list1.push_back(make_tuple(blocks[1][i].size(), canonical_blocks[1][i], i));\n    }\n\n    sort(list0.rbegin(), list0.rend());\n    sort(list1.rbegin(), list1.rend());\n\n    vector<int> global_id_counter = {1};\n    vector<int> mapping0(blocks[0].size(), -1);\n    vector<int> mapping1(blocks[1].size(), -1);\n\n    for (size_t i=0; i<list0.size(); i++) {\n        auto [vol0, canon0, idx0] = list0[i];\n        if (mapping0[idx0] != -1) continue;\n        for (size_t j=0; j<list1.size(); j++) {\n            auto [vol1, canon1, idx1] = list1[j];\n            if (mapping1[idx1] != -1) continue;\n            if (canon0 == canon1) {\n                mapping0[idx0] = global_id_counter[0];\n                mapping1[idx1] = global_id_counter[0];\n                global_id_counter[0]++;\n                break;\n            }\n        }\n    }\n\n    for (size_t i=0; i<blocks[0].size(); i++) {\n        if (mapping0[i] == -1) {\n            mapping0[i] = global_id_counter[0];\n            global_id_counter[0]++;\n        }\n    }\n    for (size_t i=0; i<blocks[1].size(); i++) {\n        if (mapping1[i] == -1) {\n            mapping1[i] = global_id_counter[0];\n            global_id_counter[0]++;\n        }\n    }\n\n    int n_blocks = global_id_counter[0] - 1;\n\n    vector<vector<vector<int>>> grid0(D, vector<vector<int>>(D, vector<int>(D,0)));\n    for (size_t idx=0; idx<blocks[0].size(); idx++) {\n        int gid = mapping0[idx];\n        for (auto [x,y,z] : blocks[0][idx]) {\n            grid0[x][y][z] = gid;\n        }\n    }\n\n    vector<vector<vector<int>>> grid1(D, vector<vector<int>>(D, vector<int>(D,0)));\n    for (size_t idx=0; idx<blocks[1].size(); idx++) {\n        int gid = mapping1[idx];\n        for (auto [x,y,z] : blocks[1][idx]) {\n            grid1[x][y][z] = gid;\n        }\n    }\n\n    vector<int> flat0, flat1;\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                flat0.push_back(grid0[x][y][z]);\n                flat1.push_back(grid1[x][y][z]);\n            }\n        }\n    }\n\n    cout << n_blocks << endl;\n    for (size_t i=0; i<flat0.size(); i++) {\n        if (i>0) cout << \" \";\n        cout << flat0[i];\n    }\n    cout << endl;\n    for (size_t i=0; i<flat1.size(); i++) {\n        if (i>0) cout << \" \";\n        cout << flat1[i];\n    }\n    cout << endl;\n\n    return 0;\n}","ahc020":"#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef tuple<int, ll, int> EdgeTuple;\n\nstruct Edge {\n    int u, v, w, id;\n};\n\nvoid prune(vector<bool>& in_steiner, vector<bool>& edge_used, vector<set<pair<int,int>>>& tree_adj, vector<int>& degree, const vector<ll>& P_value, const vector<Edge>& edges, int N) {\n    queue<int> q;\n    for (int i = 0; i < N; i++) {\n        if (in_steiner[i]) {\n            degree[i] = tree_adj[i].size();\n            if (degree[i] == 1 && i != 0 && P_value[i] == 0) {\n                q.push(i);\n            }\n        }\n    }\n    while (!q.empty()) {\n        int u = q.front(); q.pop();\n        if (!in_steiner[u] || degree[u] != 1 || u == 0 || P_value[u] != 0) \n            continue;\n        in_steiner[u] = false;\n        if (tree_adj[u].empty()) continue;\n        auto it = tree_adj[u].begin();\n        int v = it->first;\n        int eid = it->second;\n        tree_adj[u].erase(it);\n        auto it2 = tree_adj[v].find({u, eid});\n        if (it2 != tree_adj[v].end()) {\n            tree_adj[v].erase(it2);\n        }\n        degree[u] = 0;\n        degree[v]--;\n        edge_used[eid] = false;\n        if (degree[v] == 1 && v != 0 && P_value[v] == 0) {\n            q.push(v);\n        }\n    }\n}\n\nvector<ll> reassign_residents(const vector<bool>& in_steiner, const vector<ll>& x, const vector<ll>& y, const vector<ll>& a, const vector<ll>& b, int N, int K) {\n    vector<ll> P_reassign(N, 0);\n    vector<pair<ll, int>> resident_min_dist;\n    for (int k = 0; k < K; k++) {\n        ll min_d_sq = 1e18;\n        for (int i = 0; i < N; i++) {\n            if (!in_steiner[i]) continue;\n            ll dx = x[i] - a[k];\n            ll dy = y[i] - b[k];\n            ll d_sq = dx*dx + dy*dy;\n            if (d_sq < min_d_sq) {\n                min_d_sq = d_sq;\n            }\n        }\n        resident_min_dist.push_back({min_d_sq, k});\n    }\n    sort(resident_min_dist.begin(), resident_min_dist.end());\n\n    for (auto [d_sq_val, k] : resident_min_dist) {\n        ll best_increase = 1e18;\n        int best_station = -1;\n        for (int i = 0; i < N; i++) {\n            if (!in_steiner[i]) continue;\n            ll dx = x[i] - a[k];\n            ll dy = y[i] - b[k];\n            ll d_sq = dx*dx + dy*dy;\n            ll d = sqrt(d_sq);\n            if (d*d < d_sq) d++;\n\n            ll current_power = P_reassign[i];\n            if (d <= current_power) {\n                if (0 < best_increase) {\n                    best_increase = 0;\n                    best_station = i;\n                }\n            } else {\n                ll increase = d*d - current_power*current_power;\n                if (increase < best_increase) {\n                    best_increase = increase;\n                    best_station = i;\n                }\n            }\n        }\n        if (best_station != -1) {\n            ll dx = x[best_station] - a[k];\n            ll dy = y[best_station] - b[k];\n            ll d_sq = dx*dx + dy*dy;\n            ll d = sqrt(d_sq);\n            if (d*d < d_sq) d++;\n            if (d > P_reassign[best_station]) {\n                P_reassign[best_station] = d;\n            }\n        }\n    }\n    return P_reassign;\n}\n\nvoid remove_redundant_stations(vector<ll>& P_final, const vector<bool>& in_steiner, const vector<ll>& x, const vector<ll>& y, const vector<ll>& a, const vector<ll>& b, int N, int K) {\n    vector<int> count_cover(K, 0);\n    vector<vector<int>> cover(N);\n\n    for (int i = 0; i < N; i++) {\n        if (!in_steiner[i] || P_final[i] == 0) \n            continue;\n        for (int k = 0; k < K; k++) {\n            ll dx = x[i] - a[k];\n            ll dy = y[i] - b[k];\n            ll d_sq = dx*dx + dy*dy;\n            ll p_sq = (ll)P_final[i] * P_final[i];\n            if (d_sq <= p_sq) {\n                count_cover[k]++;\n                cover[i].push_back(k);\n            }\n        }\n    }\n\n    vector<int> stations_to_check;\n    for (int i = 1; i < N; i++) {\n        if (in_steiner[i] && P_final[i] > 0) {\n            stations_to_check.push_back(i);\n        }\n    }\n\n    sort(stations_to_check.begin(), stations_to_check.end(), [&](int i, int j) {\n        return P_final[i] > P_final[j];\n    });\n\n    for (int i : stations_to_check) {\n        if (!in_steiner[i] || P_final[i] == 0) continue;\n        bool redundant = true;\n        for (int k : cover[i]) {\n            if (count_cover[k] < 2) {\n                redundant = false;\n                break;\n            }\n        }\n        if (redundant) {\n            for (int k : cover[i]) {\n                count_cover[k]--;\n            }\n            P_final[i] = 0;\n        }\n    }\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, K;\n    cin >> N >> M >> K;\n\n    vector<ll> x(N), y(N);\n    for (int i = 0; i < N; i++) {\n        cin >> x[i] >> y[i];\n    }\n\n    vector<Edge> edges;\n    for (int j = 0; j < M; j++) {\n        int u, v, w;\n        cin >> u >> v >> w;\n        u--; v--;\n        edges.push_back({u, v, w, j});\n    }\n\n    vector<ll> a(K), b(K);\n    for (int k = 0; k < K; k++) {\n        cin >> a[k] >> b[k];\n    }\n\n    vector<int> assigned_station(K, -1);\n    vector<vector<int>> cluster(N);\n\n    for (int k = 0; k < K; k++) {\n        ll min_d_sq = 1e18;\n        int best_i = -1;\n        for (int i = 0; i < N; i++) {\n            ll dx = a[k] - x[i];\n            ll dy = b[k] - y[i];\n            ll d_sq = dx*dx + dy*dy;\n            if (d_sq < min_d_sq) {\n                min_d_sq = d_sq;\n                best_i = i;\n            }\n        }\n        assigned_station[k] = best_i;\n        cluster[best_i].push_back(k);\n    }\n\n    vector<ll> P_initial(N, 0);\n    vector<bool> in_S(N, false);\n\n    for (int i = 0; i < N; i++) {\n        if (cluster[i].empty()) {\n            P_initial[i] = 0;\n        } else {\n            in_S[i] = true;\n            ll max_d_sq = 0;\n            for (int k : cluster[i]) {\n                ll dx = a[k] - x[i];\n                ll dy = b[k] - y[i];\n                ll d_sq = dx*dx + dy*dy;\n                if (d_sq > max_d_sq) max_d_sq = d_sq;\n            }\n            if (max_d_sq == 0) {\n                P_initial[i] = 0;\n            } else {\n                ll candidate = sqrt(max_d_sq);\n                if (candidate * candidate < max_d_sq) {\n                    candidate++;\n                }\n                P_initial[i] = candidate;\n                if (P_initial[i] > 5000) {\n                    P_initial[i] = 5000;\n                }\n            }\n        }\n    }\n\n    set<int> T_set;\n    for (int i = 0; i < N; i++) {\n        if (in_S[i]) {\n            T_set.insert(i);\n        }\n    }\n    T_set.insert(0);\n\n    vector<vector<EdgeTuple>> adj(N);\n    for (const Edge& e : edges) {\n        int u = e.u, v = e.v;\n        ll w = e.w;\n        int id = e.id;\n        adj[u].push_back(make_tuple(v, w, id));\n        adj[v].push_back(make_tuple(u, w, id));\n    }\n\n    vector<bool> in_steiner(N, false);\n    vector<bool> edge_used(M, false);\n\n    in_steiner[0] = true;\n    set<int> T_remaining = T_set;\n    T_remaining.erase(0);\n\n    while (!T_remaining.empty()) {\n        vector<ll> dist(N, 1e18);\n        vector<int> parent_node(N, -1);\n        vector<int> parent_edge_id(N, -1);\n        priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<>> pq;\n\n        for (int i = 0; i < N; i++) {\n            if (in_steiner[i]) {\n                dist[i] = 0;\n                pq.push({0, i});\n            }\n        }\n\n        bool found = false;\n        int found_node = -1;\n\n        while (!pq.empty()) {\n            auto [d, u] = pq.top();\n            pq.pop();\n            if (d != dist[u]) continue;\n\n            if (T_remaining.find(u) != T_remaining.end()) {\n                found = true;\n                found_node = u;\n                break;\n            }\n\n            for (auto &edge : adj[u]) {\n                int v = get<0>(edge);\n                ll w = get<1>(edge);\n                int eid = get<2>(edge);\n                if (dist[v] > dist[u] + w) {\n                    dist[v] = dist[u] + w;\n                    parent_node[v] = u;\n                    parent_edge_id[v] = eid;\n                    pq.push({dist[v], v});\n                }\n            }\n        }\n\n        if (!found) {\n            break;\n        }\n\n        int cur = found_node;\n        while (!in_steiner[cur]) {\n            edge_used[parent_edge_id[cur]] = true;\n            in_steiner[cur] = true;\n            cur = parent_node[cur];\n        }\n\n        T_remaining.erase(found_node);\n    }\n\n    vector<set<pair<int,int>>> tree_adj(N);\n    vector<int> degree(N, 0);\n    for (int j = 0; j < M; j++) {\n        if (edge_used[j]) {\n            int u = edges[j].u;\n            int v = edges[j].v;\n            tree_adj[u].insert({v, j});\n            tree_adj[v].insert({u, j});\n            degree[u]++;\n            degree[v]++;\n        }\n    }\n\n    prune(in_steiner, edge_used, tree_adj, degree, P_initial, edges, N);\n\n    vector<ll> P_reassign = reassign_residents(in_steiner, x, y, a, b, N, K);\n\n    tree_adj = vector<set<pair<int,int>>>(N);\n    degree = vector<int>(N, 0);\n    for (int j = 0; j < M; j++) {\n        if (edge_used[j]) {\n            int u = edges[j].u;\n            int v = edges[j].v;\n            tree_adj[u].insert({v, j});\n            tree_adj[v].insert({u, j});\n            degree[u]++;\n            degree[v]++;\n        }\n    }\n\n    prune(in_steiner, edge_used, tree_adj, degree, P_reassign, edges, N);\n\n    vector<ll> P_final = reassign_residents(in_steiner, x, y, a, b, N, K);\n\n    tree_adj = vector<set<pair<int,int>>>(N);\n    degree = vector<int>(N, 0);\n    for (int j = 0; j < M; j++) {\n        if (edge_used[j]) {\n            int u = edges[j].u;\n            int v = edges[j].v;\n            tree_adj[u].insert({v, j});\n            tree_adj[v].insert({u, j});\n            degree[u]++;\n            degree[v]++;\n        }\n    }\n\n    remove_redundant_stations(P_final, in_steiner, x, y, a, b, N, K);\n\n    prune(in_steiner, edge_used, tree_adj, degree, P_final, edges, N);\n\n    tree_adj = vector<set<pair<int,int>>>(N);\n    degree = vector<int>(N, 0);\n    for (int j = 0; j < M; j++) {\n        if (edge_used[j]) {\n            int u = edges[j].u;\n            int v = edges[j].v;\n            tree_adj[u].insert({v, j});\n            tree_adj[v].insert({u, j});\n            degree[u]++;\n            degree[v]++;\n        }\n    }\n\n    P_final = reassign_residents(in_steiner, x, y, a, b, N, K);\n\n    for (int i = 0; i < N; i++) {\n        if (P_final[i] > 5000) {\n            P_final[i] = 5000;\n        }\n        cout << P_final[i];\n        if (i < N-1) cout << \" \";\n    }\n    cout << endl;\n\n    for (int j = 0; j < M; j++) {\n        cout << (edge_used[j] ? 1 : 0);\n        if (j < M-1) cout << \" \";\n    }\n    cout << endl;\n\n    return 0;\n}","ahc021":"#include <iostream>\n#include <vector>\n#include <tuple>\n#include <algorithm>\nusing namespace std;\n\nint main() {\n    int grid[30][30];\n    pair<int, int> pos[465];\n\n    for (int i = 0; i < 30; i++) {\n        for (int j = 0; j <= i; j++) {\n            cin >> grid[i][j];\n            pos[grid[i][j]] = make_pair(i, j);\n        }\n    }\n\n    vector<int> nums;\n    for (int i = 464; i >= 0; i--) {\n        nums.push_back(i);\n    }\n\n    vector<tuple<int, int, int, int>> ops;\n\n    for (int num : nums) {\n        int cx = pos[num].first;\n        int cy = pos[num].second;\n        while (cx < 29) {\n            int child1_val = grid[cx+1][cy];\n            int child2_val = grid[cx+1][cy+1];\n            int min_child_val = min(child1_val, child2_val);\n            \n            if (min_child_val >= num) {\n                break;\n            }\n            \n            int child_x = cx + 1;\n            int child_y = (min_child_val == child1_val) ? cy : cy + 1;\n            int child_num = min_child_val;\n            \n            ops.push_back(make_tuple(cx, cy, child_x, child_y));\n            \n            swap(grid[cx][cy], grid[child_x][child_y]);\n            swap(pos[num], pos[child_num]);\n            \n            cx = child_x;\n            cy = child_y;\n        }\n    }\n\n    cout << ops.size() << endl;\n    for (auto op : ops) {\n        cout << get<0>(op) << \" \" << get<1>(op) << \" \" << get<2>(op) << \" \" << get<3>(op) << endl;\n    }\n\n    return 0;\n}","toyota2023summer-final":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\n#include <functional>\n#include <tuple>\n#include <climits>\n#include <cmath>\n\nusing namespace std;\n\nint main() {\n    int D = 9;\n    int N;\n    cin >> D >> N;\n\n    vector<vector<int>> grid(D, vector<int>(D, -3));\n    grid[0][(D-1)/2] = -2;\n\n    for (int i = 0; i < N; i++) {\n        int ri, rj;\n        cin >> ri >> rj;\n        grid[ri][rj] = -1;\n    }\n\n    vector<vector<int>> parent_i(D, vector<int>(D, -1));\n    vector<vector<int>> parent_j(D, vector<int>(D, -1));\n    vector<vector<vector<pair<int, int>>>> children(D, vector<vector<pair<int, int>>>(D));\n\n    vector<int> dx = {0, 1, 0, -1};\n    vector<int> dy = {1, 0, -1, 0};\n\n    queue<pair<int, int>> q_bfs_parent;\n    q_bfs_parent.push({0, (D-1)/2});\n    parent_i[0][(D-1)/2] = -1;\n    parent_j[0][(D-1)/2] = -1;\n\n    while (!q_bfs_parent.empty()) {\n        auto [i, j] = q_bfs_parent.front();\n        q_bfs_parent.pop();\n        for (int d = 0; d < 4; d++) {\n            int ni = i + dx[d];\n            int nj = j + dy[d];\n            if (ni >= 0 && ni < D && nj >= 0 && nj < D) {\n                if (parent_i[ni][nj] == -1) {\n                    if (grid[ni][nj] != -1) {\n                        parent_i[ni][nj] = i;\n                        parent_j[ni][nj] = j;\n                        q_bfs_parent.push({ni, nj});\n                    }\n                }\n            }\n        }\n    }\n\n    for (int i = 0; i < D; i++) {\n        for (int j = 0; j < D; j++) {\n            if (i == 0 && j == (D-1)/2) continue;\n            if (parent_i[i][j] != -1) {\n                int pi = parent_i[i][j];\n                int pj = parent_j[i][j];\n                children[pi][pj].push_back({i, j});\n            }\n        }\n    }\n\n    for (int i = 0; i < D; i++) {\n        for (int j = 0; j < D; j++) {\n            sort(children[i][j].begin(), children[i][j].end());\n        }\n    }\n\n    vector<vector<int>> post_index(D, vector<int>(D, -1));\n    int index_counter = 0;\n\n    function<void(int, int)> dfs = [&](int i, int j) {\n        for (auto& child : children[i][j]) {\n            dfs(child.first, child.second);\n        }\n        if (!(i == 0 && j == (D-1)/2)) {\n            post_index[i][j] = index_counter;\n            index_counter++;\n        }\n    };\n\n    for (auto& child : children[0][(D-1)/2]) {\n        dfs(child.first, child.second);\n    }\n\n    int M = D * D - 1 - N;\n\n    for (int d = 0; d < M; d++) {\n        int t_d;\n        cin >> t_d;\n\n        vector<vector<bool>> visited(D, vector<bool>(D, false));\n        vector<pair<int, int>> free_list;\n        queue<pair<int, int>> q_bfs;\n        q_bfs.push({0, (D-1)/2});\n        visited[0][(D-1)/2] = true;\n\n        while (!q_bfs.empty()) {\n            auto [i, j] = q_bfs.front();\n            q_bfs.pop();\n            if (grid[i][j] == -3) {\n                free_list.push_back({i, j});\n            }\n            for (int dir = 0; dir < 4; dir++) {\n                int ni = i + dx[dir];\n                int nj = j + dy[dir];\n                if (ni >= 0 && ni < D && nj >= 0 && nj < D && !visited[ni][nj]) {\n                    if (grid[ni][nj] == -1 || grid[ni][nj] >= 0) continue;\n                    visited[ni][nj] = true;\n                    q_bfs.push({ni, nj});\n                }\n            }\n        }\n\n        int best_i = -1, best_j = -1;\n        int best_diff = INT_MAX;\n        for (auto [i, j] : free_list) {\n            if (post_index[i][j] == -1) continue;\n            int diff = abs(post_index[i][j] - t_d);\n            if (diff < best_diff || (diff == best_diff && (i < best_i || (i == best_i && j < best_j)))) {\n                best_diff = diff;\n                best_i = i;\n                best_j = j;\n            }\n        }\n\n        if (best_i == -1) {\n            for (auto [i, j] : free_list) {\n                best_i = i;\n                best_j = j;\n                break;\n            }\n        }\n\n        cout << best_i << \" \" << best_j << endl;\n        cout.flush();\n        grid[best_i][best_j] = t_d;\n    }\n\n    vector<pair<int, int>> removal_sequence;\n\n    for (int step = 0; step < M; step++) {\n        vector<vector<bool>> visited(D, vector<bool>(D, false));\n        queue<pair<int, int>> q_bfs;\n        q_bfs.push({0, (D-1)/2});\n        visited[0][(D-1)/2] = true;\n\n        while (!q_bfs.empty()) {\n            auto [i, j] = q_bfs.front();\n            q_bfs.pop();\n            for (int dir = 0; dir < 4; dir++) {\n                int ni = i + dx[dir];\n                int nj = j + dy[dir];\n                if (ni >= 0 && ni < D && nj >= 0 && nj < D && !visited[ni][nj]) {\n                    if (grid[ni][nj] == -1) continue;\n                    if (grid[ni][nj] >= 0) continue;\n                    visited[ni][nj] = true;\n                    q_bfs.push({ni, nj});\n                }\n            }\n        }\n\n        vector<tuple<int, int, int>> removable;\n        for (int i = 0; i < D; i++) {\n            for (int j = 0; j < D; j++) {\n                if (grid[i][j] >= 0) {\n                    bool flag = false;\n                    for (int dir = 0; dir < 4; dir++) {\n                        int ni = i + dx[dir];\n                        int nj = j + dy[dir];\n                        if (ni >= 0 && ni < D && nj >= 0 && nj < D) {\n                            if (visited[ni][nj]) {\n                                flag = true;\n                                break;\n                            }\n                        }\n                    }\n                    if (flag) {\n                        removable.push_back(make_tuple(grid[i][j], i, j));\n                    }\n                }\n            }\n        }\n\n        if (removable.empty()) {\n            for (int i = 0; i < D; i++) {\n                for (int j = 0; j < D; j++) {\n                    if (grid[i][j] >= 0) {\n                        removable.push_back(make_tuple(grid[i][j], i, j));\n                    }\n                }\n            }\n            sort(removable.begin(), removable.end());\n        } else {\n            sort(removable.begin(), removable.end());\n        }\n\n        if (removable.empty()) {\n            break;\n        }\n\n        auto [num, i, j] = removable[0];\n        removal_sequence.push_back({i, j});\n        grid[i][j] = -3;\n    }\n\n    for (auto [i, j] : removal_sequence) {\n        cout << i << \" \" << j << endl;\n    }\n    cout.flush();\n\n    return 0;\n}","ahc024":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\n#include <cstring>\nusing namespace std;\n\ntypedef pair<int, int> pii;\n\nconst int dx[4] = {1, 0, -1, 0};\nconst int dy[4] = {0, 1, 0, -1};\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int n, m;\n    cin >> n >> m;\n    vector<vector<int>> grid(n, vector<int>(n));\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            cin >> grid[i][j];\n        }\n    }\n\n    vector<vector<bool>> orig_adj(101, vector<bool>(101, false));\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            if (i == 0 || i == n-1 || j == 0 || j == n-1) {\n                int c = grid[i][j];\n                if (c > 0) {\n                    orig_adj[0][c] = true;\n                }\n            }\n        }\n    }\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            if (i < n-1 && grid[i][j] != grid[i+1][j] && grid[i][j] > 0 && grid[i+1][j] > 0) {\n                int c1 = min(grid[i][j], grid[i+1][j]);\n                int c2 = max(grid[i][j], grid[i+1][j]);\n                orig_adj[c1][c2] = true;\n            }\n            if (j < n-1 && grid[i][j] != grid[i][j+1] && grid[i][j] > 0 && grid[i][j+1] > 0) {\n                int c1 = min(grid[i][j], grid[i][j+1]);\n                int c2 = max(grid[i][j], grid[i][j+1]);\n                orig_adj[c1][c2] = true;\n            }\n        }\n    }\n\n    vector<vector<int>> current_adj_count(101, vector<int>(101, 0));\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            if (i == 0 || i == n-1 || j == 0 || j == n-1) {\n                int c = grid[i][j];\n                if (c > 0) {\n                    current_adj_count[0][c]++;\n                }\n            }\n        }\n    }\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            if (i < n-1 && grid[i][j] != grid[i+1][j] && grid[i][j] > 0 && grid[i+1][j] > 0) {\n                int c1 = min(grid[i][j], grid[i+1][j]);\n                int c2 = max(grid[i][j], grid[i+1][j]);\n                current_adj_count[c1][c2]++;\n            }\n            if (j < n-1 && grid[i][j] != grid[i][j+1] && grid[i][j] > 0 && grid[i][j+1] > 0) {\n                int c1 = min(grid[i][j], grid[i][j+1]);\n                int c2 = max(grid[i][j], grid[i][j+1]);\n                current_adj_count[c1][c2]++;\n            }\n        }\n    }\n\n    vector<int> ward_size(m+1, 0);\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            int c = grid[i][j];\n            if (c > 0) {\n                ward_size[c]++;\n            }\n        }\n    }\n\n    int num_zeros = 0;\n    vector<vector<bool>> in_queue(n, vector<bool>(n, true));\n    queue<pii> candidate_queue;\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            candidate_queue.push({i, j});\n        }\n    }\n\n    while (!candidate_queue.empty()) {\n        auto [i, j] = candidate_queue.front();\n        candidate_queue.pop();\n        in_queue[i][j] = false;\n        if (grid[i][j] == 0) continue;\n\n        int c = grid[i][j];\n        if (ward_size[c] == 1) {\n            continue;\n        }\n\n        vector<pii> neighbors_in_c;\n        for (int d = 0; d < 4; d++) {\n            int ni = i + dx[d];\n            int nj = j + dy[d];\n            if (ni < 0 || ni >= n || nj < 0 || nj >= n) continue;\n            if (grid[ni][nj] == c) {\n                neighbors_in_c.push_back({ni, nj});\n            }\n        }\n        if (neighbors_in_c.empty()) {\n            continue;\n        }\n\n        int total_after = ward_size[c] - 1;\n        vector<vector<bool>> visited(n, vector<bool>(n, false));\n        queue<pii> q_bfs;\n        auto [start_i, start_j] = neighbors_in_c[0];\n        q_bfs.push({start_i, start_j});\n        visited[start_i][start_j] = true;\n        int count_visited = 1;\n\n        while (!q_bfs.empty()) {\n            auto [x, y] = q_bfs.front();\n            q_bfs.pop();\n            for (int d = 0; d < 4; d++) {\n                int nx = x + dx[d];\n                int ny = y + dy[d];\n                if (nx < 0 || nx >= n || ny < 0 || ny >= n) continue;\n                if (grid[nx][ny] != c) continue;\n                if (visited[nx][ny]) continue;\n                if (nx == i && ny == j) continue;\n                visited[nx][ny] = true;\n                count_visited++;\n                q_bfs.push({nx, ny});\n            }\n        }\n\n        if (count_visited != total_after) {\n            continue;\n        }\n\n        vector<int> break_count(m+1, 0);\n        for (int d = 0; d < 4; d++) {\n            int ni = i + dx[d];\n            int nj = j + dy[d];\n            if (ni < 0 || ni >= n || nj < 0 || nj >= n) continue;\n            int d_val = grid[ni][nj];\n            if (d_val == c) continue;\n            if (d_val == 0) {\n                break_count[0]++;\n            } else {\n                break_count[d_val]++;\n            }\n        }\n        if (i == 0 || i == n-1 || j == 0 || j == n-1) {\n            break_count[0]++;\n        }\n\n        bool skip = false;\n        for (int d = 0; d <= m; d++) {\n            if (break_count[d] == 0) continue;\n            if (d == 0) {\n                if (orig_adj[0][c] && current_adj_count[0][c] <= break_count[0]) {\n                    skip = true;\n                    break;\n                }\n            } else {\n                int c1 = min(c, d);\n                int c2 = max(c, d);\n                if (orig_adj[c1][c2] && current_adj_count[c1][c2] <= break_count[d]) {\n                    skip = true;\n                    break;\n                }\n            }\n        }\n        if (skip) continue;\n\n        for (int d = 0; d < 4; d++) {\n            int ni = i + dx[d];\n            int nj = j + dy[d];\n            if (ni < 0 || ni >= n || nj < 0 || nj >= n) continue;\n            int d_val = grid[ni][nj];\n            if (d_val > 0 && d_val != c) {\n                if (!orig_adj[0][d_val]) {\n                    skip = true;\n                    break;\n                }\n            }\n        }\n        if (skip) continue;\n\n        if (num_zeros == 0) {\n            if (i != 0 && i != n-1 && j != 0 && j != n-1) {\n                continue;\n            }\n        } else {\n            if (i != 0 && i != n-1 && j != 0 && j != n-1) {\n                bool has_zero_neighbor = false;\n                for (int d = 0; d < 4; d++) {\n                    int ni = i + dx[d];\n                    int nj = j + dy[d];\n                    if (ni < 0 || ni >= n || nj < 0 || nj >= n) continue;\n                    if (grid[ni][nj] == 0) {\n                        has_zero_neighbor = true;\n                        break;\n                    }\n                }\n                if (!has_zero_neighbor) {\n                    continue;\n                }\n            }\n        }\n\n        current_adj_count[0][c] -= break_count[0];\n        for (int d = 1; d <= m; d++) {\n            if (break_count[d] > 0) {\n                int c1 = min(c, d);\n                int c2 = max(c, d);\n                current_adj_count[c1][c2] -= break_count[d];\n            }\n        }\n\n        grid[i][j] = 0;\n        num_zeros++;\n        ward_size[c]--;\n\n        for (int d = 0; d < 4; d++) {\n            int ni = i + dx[d];\n            int nj = j + dy[d];\n            if (ni < 0 || ni >= n || nj < 0 || nj >= n) continue;\n            int d_val = grid[ni][nj];\n            if (d_val > 0) {\n                current_adj_count[0][d_val]++;\n            }\n        }\n\n        for (int d = 0; d < 4; d++) {\n            int ni = i + dx[d];\n            int nj = j + dy[d];\n            if (ni < 0 || ni >= n || nj < 0 || nj >= n) continue;\n            if (grid[ni][nj] != 0 && !in_queue[ni][nj]) {\n                candidate_queue.push({ni, nj});\n                in_queue[ni][nj] = true;\n            }\n        }\n    }\n\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            if (j > 0) cout << \" \";\n            cout << grid[i][j];\n        }\n        cout << endl;\n    }\n\n    return 0;\n}","ahc025":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <random>\n#include <cstdlib>\n#include <ctime>\n#include <numeric>\n\nusing namespace std;\n\nint calc_merge_queries(int n) {\n    if (n <= 1) return 0;\n    int n1 = n / 2;\n    int n2 = n - n1;\n    return calc_merge_queries(n1) + calc_merge_queries(n2) + n - 1;\n}\n\nvector<int> merge(vector<int> left, vector<int> right, int &query_count) {\n    vector<int> res;\n    int i = 0, j = 0;\n    while (i < left.size() && j < right.size()) {\n        cout << \"1 1 \" << left[i] << \" \" << right[j] << endl;\n        string cmp;\n        cin >> cmp;\n        query_count++;\n        if (cmp == \"<\") {\n            res.push_back(left[i]);\n            i++;\n        } else if (cmp == \">\") {\n            res.push_back(right[j]);\n            j++;\n        } else {\n            res.push_back(left[i]);\n            i++;\n        }\n    }\n    while (i < left.size()) {\n        res.push_back(left[i]);\n        i++;\n    }\n    while (j < right.size()) {\n        res.push_back(right[j]);\n        j++;\n    }\n    return res;\n}\n\nvector<int> merge_sort(vector<int> v, int &query_count) {\n    if (v.size() <= 1) return v;\n    int mid = v.size() / 2;\n    vector<int> left(v.begin(), v.begin() + mid);\n    vector<int> right(v.begin() + mid, v.end());\n    left = merge_sort(left, query_count);\n    right = merge_sort(right, query_count);\n    return merge(left, right, query_count);\n}\n\nint main() {\n    random_device rd;\n    mt19937 g(rd());\n\n    int N, D, Q;\n    cin >> N >> D >> Q;\n\n    vector<int> items(N);\n    iota(items.begin(), items.end(), 0);\n    shuffle(items.begin(), items.end(), g);\n\n    int T_sort_full = calc_merge_queries(N);\n    int T_assign = (N - D) * (D - 1);\n    int T_sortD = calc_merge_queries(D);\n    int query_count = 0;\n\n    vector<vector<int>> groups(D);\n\n    if (Q >= T_sort_full + T_assign) {\n        vector<int> sorted_items = merge_sort(items, query_count);\n        reverse(sorted_items.begin(), sorted_items.end());\n        for (int i = 0; i < D; i++) {\n            groups[i].push_back(sorted_items[i]);\n        }\n        for (int i = D; i < N; i++) {\n            vector<int> candidates(D);\n            iota(candidates.begin(), candidates.end(), 0);\n            while (candidates.size() > 1) {\n                int g1 = candidates[0];\n                int g2 = candidates[1];\n                cout << groups[g1].size() << \" \" << groups[g2].size();\n                for (int item : groups[g1]) cout << \" \" << item;\n                for (int item : groups[g2]) cout << \" \" << item;\n                cout << endl;\n                \n                string res;\n                cin >> res;\n                query_count++;\n                \n                if (res == \"<\") candidates.erase(candidates.begin() + 1);\n                else if (res == \">\") candidates.erase(candidates.begin());\n                else candidates.erase(candidates.begin() + 1);\n            }\n            groups[candidates[0]].push_back(sorted_items[i]);\n        }\n    }\n    else if (Q >= T_sortD + T_assign) {\n        vector<int> firstD(items.begin(), items.begin() + D);\n        int sort_queries = 0;\n        vector<int> sorted_firstD = merge_sort(firstD, sort_queries);\n        reverse(sorted_firstD.begin(), sorted_firstD.end());\n        query_count += sort_queries;\n        for (int i = 0; i < D; i++) {\n            groups[i].push_back(sorted_firstD[i]);\n        }\n\n        int T_sort_remaining = calc_merge_queries(N - D);\n        if (Q - query_count >= T_sort_remaining + T_assign) {\n            vector<int> remaining(items.begin() + D, items.end());\n            int rem_queries = 0;\n            vector<int> sorted_remaining = merge_sort(remaining, rem_queries);\n            reverse(sorted_remaining.begin(), sorted_remaining.end());\n            query_count += rem_queries;\n            for (int i = 0; i < N - D; i++) {\n                vector<int> candidates(D);\n                iota(candidates.begin(), candidates.end(), 0);\n                while (candidates.size() > 1) {\n                    int g1 = candidates[0];\n                    int g2 = candidates[1];\n                    cout << groups[g1].size() << \" \" << groups[g2].size();\n                    for (int item : groups[g1]) cout << \" \" << item;\n                    for (int item : groups[g2]) cout << \" \" << item;\n                    cout << endl;\n                    \n                    string res;\n                    cin >> res;\n                    query_count++;\n                    \n                    if (res == \"<\") candidates.erase(candidates.begin() + 1);\n                    else if (res == \">\") candidates.erase(candidates.begin());\n                    else candidates.erase(candidates.begin() + 1);\n                }\n                groups[candidates[0]].push_back(sorted_remaining[i]);\n            }\n        } else {\n            for (int i = N-1; i >= D; i--) {\n                vector<int> candidates(D);\n                iota(candidates.begin(), candidates.end(), 0);\n                while (candidates.size() > 1) {\n                    int g1 = candidates[0];\n                    int g2 = candidates[1];\n                    cout << groups[g1].size() << \" \" << groups[g2].size();\n                    for (int item : groups[g1]) cout << \" \" << item;\n                    for (int item : groups[g2]) cout << \" \" << item;\n                    cout << endl;\n                    \n                    string res;\n                    cin >> res;\n                    query_count++;\n                    \n                    if (res == \"<\") candidates.erase(candidates.begin() + 1);\n                    else if (res == \">\") candidates.erase(candidates.begin());\n                    else candidates.erase(candidates.begin() + 1);\n                }\n                groups[candidates[0]].push_back(items[i]);\n            }\n        }\n    }\n    else {\n        for (int i = 0; i < D; i++) {\n            groups[i].push_back(items[i]);\n        }\n        vector<int> all_groups(D);\n        iota(all_groups.begin(), all_groups.end(), 0);\n        for (int i = N-1; i >= D; i--) {\n            int remaining_queries = Q - query_count;\n            int remaining_items = i - D + 1;\n            if (remaining_queries <= 0) {\n                uniform_int_distribution<int> dist(0, D-1);\n                groups[dist(g)].push_back(items[i]);\n                continue;\n            }\n            int k = min(D, 1 + remaining_queries / remaining_items);\n            k = max(2, k);\n            shuffle(all_groups.begin(), all_groups.end(), g);\n            vector<int> candidate_groups(all_groups.begin(), all_groups.begin() + k);\n            while (candidate_groups.size() > 1) {\n                vector<int> next_candidates;\n                for (int j = 0; j < candidate_groups.size(); j += 2) {\n                    if (j+1 == candidate_groups.size()) {\n                        next_candidates.push_back(candidate_groups[j]);\n                    }\n                    else {\n                        int g1 = candidate_groups[j];\n                        int g2 = candidate_groups[j+1];\n                        cout << groups[g1].size() << \" \" << groups[g2].size();\n                        for (int item : groups[g1]) cout << \" \" << item;\n                        for (int item : groups[g2]) cout << \" \" << item;\n                        cout << endl;\n                        \n                        string res;\n                        cin >> res;\n                        query_count++;\n                        \n                        if (res == \"<\") next_candidates.push_back(g1);\n                        else if (res == \">\") next_candidates.push_back(g2);\n                        else next_candidates.push_back(g1);\n                    }\n                }\n                candidate_groups = next_candidates;\n            }\n            groups[candidate_groups[0]].push_back(items[i]);\n        }\n    }\n\n    while (query_count < Q) {\n        cout << \"1 1 \" << items[0] << \" \" << items[1] << endl;\n        string res;\n        cin >> res;\n        query_count++;\n    }\n\n    vector<int> ans(N);\n    for (int gid = 0; gid < D; gid++) {\n        for (int item : groups[gid]) {\n            ans[item] = gid;\n        }\n    }\n\n    for (int i = 0; i < N; i++) {\n        if (i) cout << \" \";\n        cout << ans[i];\n    }\n    cout << endl;\n\n    return 0;\n}","ahc026":"#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int n, m;\n    cin >> n >> m;\n    vector<vector<int>> stacks(m);\n    for (int i = 0; i < m; i++) {\n        for (int j = 0; j < n/m; j++) {\n            int x;\n            cin >> x;\n            stacks[i].push_back(x);\n        }\n    }\n\n    vector<pair<int, int>> operations;\n\n    for (int v = 1; v <= n; v++) {\n        int s = -1, pos = -1;\n        for (int i = 0; i < m; i++) {\n            for (int j = 0; j < stacks[i].size(); j++) {\n                if (stacks[i][j] == v) {\n                    s = i;\n                    pos = j;\n                    break;\n                }\n            }\n            if (s != -1) break;\n        }\n\n        if (s == -1) {\n            continue;\n        }\n\n        if (pos == (int)stacks[s].size() - 1) {\n            operations.push_back({v, 0});\n            stacks[s].pop_back();\n        } else {\n            int w = stacks[s][pos+1];\n            int d = -1;\n            int best_top = -1;\n\n            for (int i = 0; i < m; i++) {\n                if (i == s) continue;\n                if (stacks[i].empty()) {\n                    if (1000 > best_top) {\n                        best_top = 1000;\n                        d = i;\n                    }\n                } else {\n                    int top_val = stacks[i].back();\n                    if (top_val > best_top) {\n                        best_top = top_val;\n                        d = i;\n                    }\n                }\n            }\n\n            if (d == -1) {\n                d = (s == 0) ? 1 : 0;\n            }\n\n            vector<int> seg;\n            for (int j = pos+1; j < stacks[s].size(); j++) {\n                seg.push_back(stacks[s][j]);\n            }\n            stacks[s].resize(pos+1);\n            for (int box : seg) {\n                stacks[d].push_back(box);\n            }\n\n            operations.push_back({w, d+1});\n            operations.push_back({v, 0});\n            stacks[s].pop_back();\n        }\n    }\n\n    for (auto &op : operations) {\n        cout << op.first << \" \" << op.second << '\\n';\n    }\n\n    return 0;\n}","ahc027":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\n#include <cmath>\n#include <unordered_map>\n#include <cctype>\n#include <climits>\n#include <functional>\n#include <set>\n#include <iomanip>\n\nusing namespace std;\n\nconst int dx[4] = {0, 1, 0, -1};\nconst string dir_str = \"RDLU\";\nconst int dy[4] = {1, 0, -1, 0};\n\nstruct Cell {\n    int i, j;\n    Cell(int i=0, int j=0) : i(i), j(j) {}\n    bool operator==(const Cell& other) const {\n        return i == other.i && j == other.j;\n    }\n    bool operator!=(const Cell& other) const {\n        return !(*this == other);\n    }\n};\n\nnamespace std {\n    template <>\n    struct hash<Cell> {\n        size_t operator()(const Cell& c) const {\n            return c.i * 100 + c.j;\n        }\n    };\n}\n\nint get_index(int i, int j, int N) {\n    return i * N + j;\n}\n\nCell get_cell(int idx, int N) {\n    return Cell(idx / N, idx % N);\n}\n\nchar get_move_char(Cell from, Cell to) {\n    if (to.i == from.i + 1) return 'D';\n    if (to.i == from.i - 1) return 'U';\n    if (to.j == from.j + 1) return 'R';\n    if (to.j == from.j - 1) return 'L';\n    return '?';\n}\n\nchar reverse_move(char m) {\n    if (m == 'U') return 'D';\n    if (m == 'D') return 'U';\n    if (m == 'L') return 'R';\n    if (m == 'R') return 'L';\n    return m;\n}\n\nvector<vector<int>> precompute_all_pairs_shortest_path(int N, const vector<vector<bool>>& down_wall, const vector<vector<bool>>& right_wall) {\n    int total_cells = N * N;\n    vector<vector<int>> dist(total_cells, vector<int>(total_cells, INT_MAX));\n\n    for (int idx = 0; idx < total_cells; idx++) {\n        Cell src = get_cell(idx, N);\n        vector<int>& d_vec = dist[idx];\n        d_vec[idx] = 0;\n        queue<int> q;\n        q.push(idx);\n\n        while (!q.empty()) {\n            int u_idx = q.front();\n            q.pop();\n            Cell u = get_cell(u_idx, N);\n\n            for (int d = 0; d < 4; d++) {\n                Cell v = Cell(u.i + dx[d], u.j + dy[d]);\n                if (v.i < 0 || v.i >= N || v.j < 0 || v.j >= N) continue;\n                if (d == 0) {\n                    if (right_wall[u.i][u.j]) continue;\n                } else if (d == 1) {\n                    if (down_wall[u.i][u.j]) continue;\n                } else if (d == 2) {\n                    if (v.j < u.j && (u.j-1>=0) && right_wall[u.i][u.j-1]) continue;\n                } else if (d == 3) {\n                    if (v.i < u.i && (u.i-1>=0) && down_wall[u.i-1][u.j]) continue;\n                }\n\n                int v_idx = get_index(v.i, v.j, N);\n                if (d_vec[v_idx] == INT_MAX) {\n                    d_vec[v_idx] = d_vec[u_idx] + 1;\n                    q.push(v_idx);\n                }\n            }\n        }\n    }\n\n    return dist;\n}\n\nvector<vector<int>> precompute_parents_all(int N, const vector<vector<bool>>& down_wall, const vector<vector<bool>>& right_wall) {\n    int total_cells = N * N;\n    vector<vector<int>> parent_all(total_cells, vector<int>(total_cells, -1));\n\n    for (int src_idx = 0; src_idx < total_cells; src_idx++) {\n        vector<int>& parent = parent_all[src_idx];\n        vector<int> dist(total_cells, INT_MAX);\n        dist[src_idx] = 0;\n        parent[src_idx] = src_idx;\n        queue<int> q;\n        q.push(src_idx);\n\n        while (!q.empty()) {\n            int u_idx = q.front();\n            q.pop();\n            Cell u = get_cell(u_idx, N);\n\n            for (int d = 0; d < 4; d++) {\n                Cell v = Cell(u.i + dx[d], u.j + dy[d]);\n                if (v.i < 0 || v.i >= N || v.j < 0 || v.j >= N) continue;\n                if (d == 0) {\n                    if (right_wall[u.i][u.j]) continue;\n                } else if (d == 1) {\n                    if (down_wall[u.i][u.j]) continue;\n                } else if (d == 2) {\n                    if (v.j < u.j && (u.j-1>=0) && right_wall[u.i][u.j-1]) continue;\n                } else if (d == 3) {\n                    if (v.i < u.i && (u.i-1>=0) && down_wall[u.i-1][u.j]) continue;\n                }\n\n                int v_idx = get_index(v.i, v.j, N);\n                if (dist[v_idx] == INT_MAX) {\n                    dist[v_idx] = dist[u_idx] + 1;\n                    parent[v_idx] = u_idx;\n                    q.push(v_idx);\n                }\n            }\n        }\n    }\n\n    return parent_all;\n}\n\nstring generate_base_route(int N, const vector<vector<bool>>& down_wall, const vector<vector<bool>>& right_wall, const vector<vector<int>>& d) {\n    int total_cells = N * N;\n    vector<int> parent_tree(total_cells, -1);\n    vector<int> dist(total_cells, INT_MAX);\n    vector<vector<int>> children(total_cells);\n\n    queue<int> q;\n    dist[0] = 0;\n    parent_tree[0] = 0;\n    q.push(0);\n\n    while (!q.empty()) {\n        int u_idx = q.front(); q.pop();\n        Cell u = get_cell(u_idx, N);\n        for (int d = 0; d < 4; d++) {\n            Cell v = Cell(u.i + dx[d], u.j + dy[d]);\n            if (v.i < 0 || v.i >= N || v.j < 0 || v.j >= N) continue;\n            if (d == 0) {\n                if (right_wall[u.i][u.j]) continue;\n            } else if (d == 1) {\n                if (down_wall[u.i][u.j]) continue;\n            } else if (d == 2) {\n                if (v.j < u.j && (u.j-1>=0) && right_wall[u.i][u.j-1]) continue;\n            } else if (d == 3) {\n                if (v.i < u.i && (u.i-1>=0) && down_wall[u.i-1][u.j]) continue;\n            }\n\n            int v_idx = get_index(v.i, v.j, N);\n            if (dist[v_idx] == INT_MAX) {\n                dist[v_idx] = dist[u_idx] + 1;\n                parent_tree[v_idx] = u_idx;\n                q.push(v_idx);\n                children[u_idx].push_back(v_idx);\n            }\n        }\n    }\n\n    for (int i = 0; i < total_cells; i++) {\n        if (dist[i] == INT_MAX) {\n            cerr << \"Cell not reached: \" << i/N << \", \" << i%N << endl;\n        }\n    }\n\n    function<string(int)> dfs = [&](int u_idx) {\n        string route = \"\";\n        Cell u = get_cell(u_idx, N);\n        vector<int>& child_list = children[u_idx];\n        sort(child_list.begin(), child_list.end(), [&](int a, int b) {\n            return d[get_cell(a, N).i][get_cell(a, N).j] > d[get_cell(b, N).i][get_cell(b, N).j];\n        });\n\n        for (int v_idx : child_list) {\n            Cell v = get_cell(v_idx, N);\n            char move_char = get_move_char(u, v);\n            route += move_char;\n            route += dfs(v_idx);\n            route += reverse_move(move_char);\n        }\n        return route;\n    };\n\n    string base_route = dfs(0);\n    return base_route;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N;\n    cin >> N;\n    vector<string> h_lines(N-1);\n    vector<string> v_lines(N);\n    vector<vector<int>> d(N, vector<int>(N));\n\n    for (int i = 0; i < N-1; i++) {\n        cin >> h_lines[i];\n    }\n    for (int i = 0; i < N; i++) {\n        cin >> v_lines[i];\n    }\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cin >> d[i][j];\n        }\n    }\n\n    vector<vector<bool>> down_wall(N-1, vector<bool>(N, false));\n    vector<vector<bool>> right_wall(N, vector<bool>(N-1, false));\n\n    for (int i = 0; i < N-1; i++) {\n        for (int j = 0; j < N; j++) {\n            down_wall[i][j] = (h_lines[i][j] == '1');\n        }\n    }\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N-1; j++) {\n            right_wall[i][j] = (v_lines[i][j] == '1');\n        }\n    }\n\n    vector<vector<int>> all_dist = precompute_all_pairs_shortest_path(N, down_wall, right_wall);\n    vector<vector<int>> parent_all = precompute_parents_all(N, down_wall, right_wall);\n\n    string base_route = generate_base_route(N, down_wall, right_wall, d);\n    string current_route = base_route;\n\n    int total_cells = N * N;\n    vector<vector<int>> occ(total_cells);\n    vector<Cell> positions;\n    Cell cur(0, 0);\n    positions.push_back(cur);\n    for (int i = 0; i < current_route.size(); i++) {\n        char m = current_route[i];\n        Cell next = cur;\n        if (m == 'R') next.j++;\n        else if (m == 'D') next.i++;\n        else if (m == 'L') next.j--;\n        else if (m == 'U') next.i--;\n        positions.push_back(next);\n        cur = next;\n    }\n\n    for (int idx = 0; idx < positions.size(); idx++) {\n        Cell cell = positions[idx];\n        if (idx > 0) {\n            int cell_idx = get_index(cell.i, cell.j, N);\n            occ[cell_idx].push_back(idx-1);\n        }\n    }\n\n    int L0 = current_route.size();\n    vector<int> freq(total_cells, 0);\n    for (int i = 0; i < total_cells; i++) {\n        freq[i] = occ[i].size();\n    }\n\n    vector<double> gap_est(total_cells, 0.0);\n    for (int i = 0; i < total_cells; i++) {\n        if (freq[i] > 0) {\n            gap_est[i] = L0 * 1.0 / freq[i];\n        }\n    }\n\n    using pdi = pair<double, int>;\n    priority_queue<pdi> pq;\n    for (int i = 0; i < total_cells; i++) {\n        if (freq[i] > 0) {\n            double priority_val = d[get_cell(i, N).i][get_cell(i, N).j] * gap_est[i] * gap_est[i];\n            pq.push({priority_val, i});\n        }\n    }\n\n    int insertion_count = 0;\n    const int MAX_INSERTION = 1000;\n    vector<int> dummy_occ_count(total_cells, 0);\n\n    while (insertion_count < MAX_INSERTION && !pq.empty()) {\n        double priority_val = pq.top().first;\n        int c_idx = pq.top().second;\n        pq.pop();\n        if (freq[c_idx] == 0) continue;\n\n        int best_u_idx = -1;\n        int best_dist = INT_MAX;\n        for (int u_idx = 0; u_idx < total_cells; u_idx++) {\n            if (freq[u_idx] == 0) continue;\n            if (all_dist[u_idx][c_idx] < best_dist) {\n                best_dist = all_dist[u_idx][c_idx];\n                best_u_idx = u_idx;\n            }\n        }\n\n        if (best_u_idx == -1 || best_dist == INT_MAX) {\n            continue;\n        }\n\n        int detour_length = 2 * best_dist;\n        if (L0 + detour_length > 100000) {\n            continue;\n        }\n\n        if (occ[best_u_idx].empty()) {\n            continue;\n        }\n        int insert_move_index = occ[best_u_idx][0];\n\n        Cell best_u = get_cell(best_u_idx, N);\n        Cell c = get_cell(c_idx, N);\n        string moves_to_c = \"\";\n        int current_idx = best_u_idx;\n        while (current_idx != c_idx) {\n            int next_idx = parent_all[c_idx][current_idx];\n            if (next_idx == -1) break;\n            Cell cur_cell = get_cell(current_idx, N);\n            Cell next_cell = get_cell(next_idx, N);\n            char m = get_move_char(cur_cell, next_cell);\n            moves_to_c += m;\n            current_idx = next_idx;\n        }\n\n        string moves_back = \"\";\n        for (int i = moves_to_c.size() - 1; i >= 0; i--) {\n            moves_back += reverse_move(moves_to_c[i]);\n        }\n        string detour = moves_to_c + moves_back;\n\n        string new_route = current_route.substr(0, insert_move_index+1) + detour + current_route.substr(insert_move_index+1);\n        int new_L = new_route.size();\n\n        vector<Cell> new_positions;\n        vector<vector<int>> new_occ(total_cells);\n        Cell pos(0,0);\n        new_positions.push_back(pos);\n        for (int i = 0; i < new_route.size(); i++) {\n            char m = new_route[i];\n            Cell next = pos;\n            if (m == 'R') next.j++;\n            else if (m == 'D') next.i++;\n            else if (m == 'L') next.j--;\n            else if (m == 'U') next.i--;\n            new_positions.push_back(next);\n            int cell_idx = get_index(next.i, next.j, N);\n            new_occ[cell_idx].push_back(i);\n            pos = next;\n        }\n\n        current_route = new_route;\n        L0 = new_L;\n        for (int i = 0; i < total_cells; i++) {\n            freq[i] = new_occ[i].size();\n            if (freq[i] > 0) {\n                gap_est[i] = L0 * 1.0 / freq[i];\n            }\n        }\n        occ = new_occ;\n\n        priority_queue<pdi> new_pq;\n        for (int i = 0; i < total_cells; i++) {\n            if (freq[i] > 0) {\n                double new_priority = d[get_cell(i, N).i][get_cell(i, N).j] * gap_est[i] * gap_est[i];\n                new_pq.push({new_priority, i});\n            }\n        }\n        pq = new_pq;\n\n        insertion_count++;\n    }\n\n    cout << current_route << endl;\n\n    return 0;\n}","ahc028":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <unordered_set>\n#include <climits>\n#include <tuple>\n#include <deque>\n\nusing namespace std;\n\nconst int INF = 1e8;\nconst int N_grid = 15;\n\nvoid manhattan_transform(vector<vector<int>>& grid) {\n    int n = grid.size();\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            if (i > 0) grid[i][j] = min(grid[i][j], grid[i-1][j] + 1);\n            if (j > 0) grid[i][j] = min(grid[i][j], grid[i][j-1] + 1);\n        }\n    }\n    for (int i = n-1; i >= 0; i--) {\n        for (int j = n-1; j >= 0; j--) {\n            if (i+1 < n) grid[i][j] = min(grid[i][j], grid[i+1][j] + 1);\n            if (j+1 < n) grid[i][j] = min(grid[i][j], grid[i][j+1] + 1);\n        }\n    }\n}\n\nint compute_actual_cost(string& t, int cur_x, int cur_y, vector<vector<pair<int, int>>>& positions, int N) {\n    vector<vector<int>> dp_prev(N, vector<int>(N, INF));\n    for (auto [x, y] : positions[t[0]-'A']) {\n        dp_prev[x][y] = abs(cur_x - x) + abs(cur_y - y) + 1;\n    }\n\n    for (int idx = 1; idx < 5; idx++) {\n        vector<vector<int>> grid(N, vector<int>(N, INF));\n        for (auto [x, y] : positions[t[idx-1]-'A']) {\n            grid[x][y] = dp_prev[x][y];\n        }\n        manhattan_transform(grid);\n        vector<vector<int>> dp_curr(N, vector<int>(N, INF));\n        for (auto [x, y] : positions[t[idx]-'A']) {\n            dp_curr[x][y] = grid[x][y] + 1;\n        }\n        dp_prev = dp_curr;\n    }\n\n    int min_cost = INF;\n    for (auto [x, y] : positions[t[4]-'A']) {\n        if (dp_prev[x][y] < min_cost) {\n            min_cost = dp_prev[x][y];\n        }\n    }\n    return min_cost;\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M;\n    cin >> N >> M;\n    int s_i, s_j;\n    cin >> s_i >> s_j;\n\n    vector<string> grid_str(N);\n    for (int i = 0; i < N; i++) {\n        cin >> grid_str[i];\n    }\n\n    vector<string> targets;\n    for (int i = 0; i < M; i++) {\n        string t;\n        cin >> t;\n        targets.push_back(t);\n    }\n\n    unordered_set<string> target_set(targets.begin(), targets.end());\n    vector<vector<pair<int, int>>> positions(26);\n\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            char c = grid_str[i][j];\n            positions[c-'A'].push_back({i, j});\n        }\n    }\n\n    vector<vector<int>> d_table(26, vector<int>(26, INF));\n\n    for (int b = 0; b < 26; b++) {\n        vector<vector<int>> dist(N, vector<int>(N, INF));\n        for (auto [x, y] : positions[b]) {\n            dist[x][y] = 0;\n        }\n        manhattan_transform(dist);\n\n        for (int a = 0; a < 26; a++) {\n            int min_val = INF;\n            for (auto [x, y] : positions[a]) {\n                if (dist[x][y] < min_val) {\n                    min_val = dist[x][y];\n                }\n            }\n            d_table[a][b] = min_val;\n        }\n    }\n\n    string S = \"\";\n    int cur_x = s_i, cur_y = s_j;\n    vector<pair<int, int>> operations;\n    unordered_set<string> covered;\n\n    while (covered.size() < M && operations.size() < 5000) {\n        vector<int> min_dist_letter(26, INF);\n        for (int c = 0; c < 26; c++) {\n            for (auto [x, y] : positions[c]) {\n                int d = abs(cur_x - x) + abs(cur_y - y);\n                if (d < min_dist_letter[c]) {\n                    min_dist_letter[c] = d;\n                }\n            }\n        }\n\n        vector<tuple<double, string, int, double>> candidate_list;\n        for (string t : targets) {\n            if (covered.find(t) != covered.end()) continue;\n            int lb = min_dist_letter[t[0]-'A'] \n                     + d_table[t[0]-'A'][t[1]-'A'] \n                     + d_table[t[1]-'A'][t[2]-'A'] \n                     + d_table[t[2]-'A'][t[3]-'A'] \n                     + d_table[t[3]-'A'][t[4]-'A'] \n                     + 5;\n            string last_part = (S.length() >= 4) ? S.substr(S.length()-4) : S;\n            string candidate_string = last_part + t;\n            unordered_set<string> new_grams;\n            for (int i = 0; i <= (int)candidate_string.length()-5; i++) {\n                string gram = candidate_string.substr(i, 5);\n                if (target_set.find(gram) != target_set.end() && covered.find(gram) == covered.end()) {\n                    new_grams.insert(gram);\n                }\n            }\n            int new_count = new_grams.size();\n            if (new_count == 0) continue;\n            double score_lb = (double)lb / new_count;\n            candidate_list.push_back({score_lb, t, new_count, (double)lb});\n        }\n\n        if (candidate_list.empty()) {\n            break;\n        }\n\n        sort(candidate_list.begin(), candidate_list.end());\n        int K_candidate = min(50, (int)candidate_list.size());\n        vector<tuple<double, string, int, double>> top_candidates(candidate_list.begin(), candidate_list.begin() + K_candidate);\n\n        string best_string = \"\";\n        double best_score = 1e18;\n        int best_new_count = 0;\n\n        for (auto [score_lb, t, new_count, lb] : top_candidates) {\n            int actual_cost = compute_actual_cost(t, cur_x, cur_y, positions, N);\n            if (actual_cost == INF) continue;\n\n            double actual_score = (double)actual_cost / new_count;\n\n            string last_part = (S.length() >= 4) ? S.substr(S.length()-4) : S;\n            string candidate_string = last_part + t;\n            unordered_set<string> new_grams;\n            for (int i = 0; i <= (int)candidate_string.length()-5; i++) {\n                string gram = candidate_string.substr(i, 5);\n                if (target_set.find(gram) != target_set.end() && covered.find(gram) == covered.end()) {\n                    new_grams.insert(gram);\n                }\n            }\n\n            char last_char = t[4];\n            int min_next = INF;\n            for (const string& t_remain : targets) {\n                if (covered.find(t_remain) != covered.end()) continue;\n                if (new_grams.find(t_remain) != new_grams.end()) continue;\n                min_next = min(min_next, d_table[last_char-'A'][t_remain[0]-'A']);\n            }\n            if (min_next == INF) min_next = 0;\n\n            double adjusted_score = actual_score + 0.001 * min_next;\n\n            if (adjusted_score < best_score) {\n                best_score = adjusted_score;\n                best_string = t;\n                best_new_count = new_count;\n            }\n        }\n\n        if (best_string.empty()) {\n            break;\n        }\n\n        vector<vector<vector<int>>> dp(5, vector<vector<int>>(N, vector<int>(N, INF)));\n        vector<vector<vector<pair<int, int>>>> parent(5, vector<vector<pair<int, int>>>(N, vector<pair<int, int>>(N, {-1, -1})));\n\n        for (auto [x, y] : positions[best_string[0]-'A']) {\n            dp[0][x][y] = abs(cur_x - x) + abs(cur_y - y) + 1;\n        }\n\n        for (int k = 1; k < 5; k++) {\n            char prev_char = best_string[k-1];\n            char curr_char = best_string[k];\n            for (auto [x, y] : positions[curr_char-'A']) {\n                for (auto [px, py] : positions[prev_char-'A']) {\n                    if (dp[k-1][px][py] >= INF) continue;\n                    int move_cost = abs(x - px) + abs(y - py);\n                    int total_cost = dp[k-1][px][py] + move_cost + 1;\n                    if (total_cost < dp[k][x][y]) {\n                        dp[k][x][y] = total_cost;\n                        parent[k][x][y] = {px, py};\n                    }\n                }\n            }\n        }\n\n        int min_cost = INF;\n        pair<int, int> best_end = {-1, -1};\n        for (auto [x, y] : positions[best_string[4]-'A']) {\n            if (dp[4][x][y] < min_cost) {\n                min_cost = dp[4][x][y];\n                best_end = {x, y};\n            }\n        }\n\n        if (min_cost == INF) {\n            break;\n        }\n\n        vector<pair<int, int>> path(5);\n        int cx = best_end.first, cy = best_end.second;\n        path[4] = best_end;\n        for (int k = 3; k >= 0; k--) {\n            auto [px, py] = parent[k+1][cx][cy];\n            path[k] = {px, py};\n            cx = px;\n            cy = py;\n        }\n\n        for (int k = 0; k < 5; k++) {\n            int x = path[k].first;\n            int y = path[k].second;\n            operations.push_back({x, y});\n            S += grid_str[x][y];\n            cur_x = x;\n            cur_y = y;\n        }\n\n        int L = S.length();\n        int start_index = max(0, L - 9);\n        for (int i = start_index; i <= L - 5; i++) {\n            string gram = S.substr(i, 5);\n            if (target_set.find(gram) != target_set.end()) {\n                covered.insert(gram);\n            }\n        }\n\n        if (covered.size() >= M) break;\n        if (operations.size() >= 5000) break;\n    }\n\n    for (auto [x, y] : operations) {\n        cout << x << \" \" << y << \"\\n\";\n    }\n\n    return 0;\n}","ahc030":"#include <iostream>\n#include <vector>\n#include <cmath>\n#include <algorithm>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M;\n    double eps;\n    cin >> N >> M >> eps;\n\n    vector<vector<pair<int, int>>> fields;\n    for (int k = 0; k < M; k++) {\n        int d;\n        cin >> d;\n        vector<pair<int, int>> field;\n        for (int i = 0; i < d; i++) {\n            int x, y;\n            cin >> x >> y;\n            field.push_back({x, y});\n        }\n        fields.push_back(field);\n    }\n\n    const int BLOCK_SIZE = 5;\n    vector<pair<int, int>> oil_list;\n\n    for (int i0 = 0; i0 < N; i0 += BLOCK_SIZE) {\n        for (int j0 = 0; j0 < N; j0 += BLOCK_SIZE) {\n            int i1 = min(i0 + BLOCK_SIZE, N);\n            int j1 = min(j0 + BLOCK_SIZE, N);\n            vector<pair<int, int>> block;\n            for (int i = i0; i < i1; i++) {\n                for (int j = j0; j < j1; j++) {\n                    block.push_back({i, j});\n                }\n            }\n            int k = block.size();\n            bool non_empty = false;\n            for (int trial = 0; trial < 2; trial++) {\n                cout << \"q \" << k;\n                for (auto [i, j] : block) {\n                    cout << \" \" << i << \" \" << j;\n                }\n                cout << endl;\n                int resp;\n                cin >> resp;\n                if (resp >= 1) {\n                    non_empty = true;\n                    break;\n                }\n            }\n            if (non_empty) {\n                for (auto [i, j] : block) {\n                    cout << \"q 1 \" << i << \" \" << j << endl;\n                    int v;\n                    cin >> v;\n                    if (v > 0) {\n                        oil_list.push_back({i, j});\n                    }\n                }\n            }\n        }\n    }\n\n    cout << \"a \" << oil_list.size();\n    for (auto [i, j] : oil_list) {\n        cout << \" \" << i << \" \" << j;\n    }\n    cout << endl;\n    int resp;\n    cin >> resp;\n\n    return 0;\n}","ahc031":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <set>\n#include <climits>\n#include <tuple>\nusing namespace std;\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int W, D, N;\n    cin >> W >> D >> N;\n    vector<vector<int>> a(D, vector<int>(N));\n    for (int d = 0; d < D; ++d) {\n        for (int k = 0; k < N; ++k) {\n            cin >> a[d][k];\n        }\n    }\n\n    set<int> prev_boundaries;\n    vector<vector<tuple<int, int, int, int>>> all_placements(D);\n\n    for (int d = 0; d < D; ++d) {\n        vector<int> minH(N);\n        for (int k = 0; k < N; ++k) {\n            minH[k] = (a[d][k] + W - 1) / W;\n            if (minH[k] == 0) minH[k] = 1;\n        }\n\n        vector<tuple<int, int, int, int>> placements(N);\n        set<int> curr_boundaries;\n\n        if (N == 1) {\n            placements[0] = make_tuple(0, 0, W, W);\n        } else {\n            vector<int> dp(W+1, -1);\n            vector<vector<int>> parent_arr;\n\n            for (int y = minH[0]; y <= W; ++y) {\n                dp[y] = (d > 0 && prev_boundaries.find(y) != prev_boundaries.end()) ? 1 : 0;\n            }\n\n            for (int k_index = 0; k_index <= N-3; ++k_index) {\n                vector<int> new_dp(W+1, -1);\n                vector<int> new_parent(W+1, -1);\n\n                vector<int> best_val(W+1, -1);\n                vector<int> best_arg(W+1, -1);\n                best_val[0] = dp[0];\n                best_arg[0] = 0;\n                for (int z = 1; z <= W; ++z) {\n                    if (dp[z] > best_val[z-1]) {\n                        best_val[z] = dp[z];\n                        best_arg[z] = z;\n                    } else {\n                        best_val[z] = best_val[z-1];\n                        best_arg[z] = best_arg[z-1];\n                    }\n                }\n\n                int required = minH[k_index+1];\n                for (int z = required; z <= W; ++z) {\n                    int prev_z = z - required;\n                    if (prev_z < 0 || best_val[prev_z] == -1) continue;\n                    int common = (d > 0 && prev_boundaries.find(z) != prev_boundaries.end()) ? 1 : 0;\n                    new_dp[z] = best_val[prev_z] + common;\n                    new_parent[z] = best_arg[prev_z];\n                }\n\n                dp = new_dp;\n                parent_arr.push_back(new_parent);\n            }\n\n            int best_z = -1;\n            int best_val = -1;\n            for (int z = 0; z <= W; ++z) {\n                if (dp[z] != -1 && (W - z) >= minH[N-1]) {\n                    if (dp[z] > best_val) {\n                        best_val = dp[z];\n                        best_z = z;\n                    }\n                }\n            }\n\n            if (best_z == -1) {\n                for (int k = 0; k < N; ++k) {\n                    placements[k] = make_tuple(k, 0, k+1, W);\n                }\n                for (int i = 1; i < N; ++i) {\n                    if (i < W) curr_boundaries.insert(i);\n                }\n            } else {\n                vector<int> boundaries(N-1, 0);\n                boundaries[N-2] = best_z;\n                for (int idx = N-3; idx >= 0; --idx) {\n                    boundaries[idx] = parent_arr[idx][boundaries[idx+1]];\n                }\n                placements[0] = make_tuple(0, 0, boundaries[0], W);\n                for (int k = 1; k < N-1; ++k) {\n                    placements[k] = make_tuple(boundaries[k-1], 0, boundaries[k], W);\n                }\n                placements[N-1] = make_tuple(boundaries[N-2], 0, W, W);\n                for (int k = 0; k < N-1; ++k) {\n                    curr_boundaries.insert(boundaries[k]);\n                }\n            }\n        }\n\n        all_placements[d] = placements;\n        prev_boundaries = curr_boundaries;\n    }\n\n    for (int d = 0; d < D; ++d) {\n        for (int k = 0; k < N; ++k) {\n            auto [i0, j0, i1, j1] = all_placements[d][k];\n            cout << i0 << \" \" << j0 << \" \" << i1 << \" \" << j1 << \"\\n\";\n        }\n    }\n\n    return 0;\n}","ahc032":"#include <iostream>\n#include <vector>\n#include <tuple>\n#include <algorithm>\n#include <climits>\n#include <cmath>\nusing namespace std;\n\nconst long long mod = 998244353;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, K;\n    cin >> N >> M >> K;\n\n    vector<vector<long long>> grid(N, vector<long long>(N));\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cin >> grid[i][j];\n        }\n    }\n\n    vector<vector<vector<long long>>> stamps(M, vector<vector<long long>>(3, vector<long long>(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 >> stamps[m][i][j];\n            }\n        }\n    }\n\n    vector<tuple<int, int, int>> operations;\n    long long total_score = 0;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            total_score += grid[i][j];\n        }\n    }\n\n    while (operations.size() < K) {\n        long long best_delta = LLONG_MIN;\n        int best_m = -1, best_p = -1, best_q = -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                    long long delta = 0;\n                    for (int i = 0; i < 3; i++) {\n                        for (int j = 0; j < 3; j++) {\n                            long long old_val = grid[p+i][q+j];\n                            long long add = stamps[m][i][j];\n                            long long new_val = old_val + add;\n                            if (new_val >= mod) {\n                                new_val -= mod;\n                            }\n                            delta += (new_val - old_val);\n                        }\n                    }\n                    if (delta > best_delta) {\n                        best_delta = delta;\n                        best_m = m;\n                        best_p = p;\n                        best_q = q;\n                    }\n                }\n            }\n        }\n\n        if (best_delta <= 0) {\n            break;\n        }\n\n        for (int i = 0; i < 3; i++) {\n            for (int j = 0; j < 3; j++) {\n                long long old_val = grid[best_p+i][best_q+j];\n                long long new_val = old_val + stamps[best_m][i][j];\n                if (new_val >= mod) {\n                    new_val -= mod;\n                }\n                grid[best_p+i][best_q+j] = new_val;\n                total_score += (new_val - old_val);\n            }\n        }\n\n        operations.push_back(make_tuple(best_m, best_p, best_q));\n    }\n\n    int passes = 0;\n    const int MAX_PASSES = 10;\n    while (passes < MAX_PASSES) {\n        bool improved = false;\n        vector<tuple<int, int, int>> current_ops = operations;\n        vector<long long> removal_effects(current_ops.size());\n        vector<int> indices(current_ops.size());\n        for (int idx = 0; idx < current_ops.size(); idx++) {\n            auto [m, p, q] = current_ops[idx];\n            long long effect = 0;\n            for (int i = 0; i < 3; i++) {\n                for (int j = 0; j < 3; j++) {\n                    long long old_val = grid[p+i][q+j];\n                    long long s_val = stamps[m][i][j];\n                    if (old_val < s_val) {\n                        effect += (mod - s_val);\n                    } else {\n                        effect -= s_val;\n                    }\n                }\n            }\n            removal_effects[idx] = effect;\n            indices[idx] = idx;\n        }\n\n        sort(indices.begin(), indices.end(), [&](int a, int b) {\n            return removal_effects[a] > removal_effects[b];\n        });\n\n        for (int idx : indices) {\n            vector<vector<long long>> grid_backup = grid;\n            long long total_score_backup = total_score;\n\n            auto [m_remove, p_remove, q_remove] = current_ops[idx];\n            for (int i = 0; i < 3; i++) {\n                for (int j = 0; j < 3; j++) {\n                    long long old_val = grid[p_remove+i][q_remove+j];\n                    long long s_val = stamps[m_remove][i][j];\n                    long long new_val = old_val - s_val;\n                    if (new_val < 0) {\n                        new_val += mod;\n                    }\n                    grid[p_remove+i][q_remove+j] = new_val;\n                    total_score += (new_val - old_val);\n                }\n            }\n\n            vector<tuple<int, int, int>> new_ops;\n            int max_add = K - (current_ops.size() - 1);\n            for (int op_count = 0; op_count < max_add; op_count++) {\n                long long best_delta = LLONG_MIN;\n                int best_m = -1, best_p = -1, best_q = -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                            long long delta = 0;\n                            for (int i = 0; i < 3; i++) {\n                                for (int j = 0; j < 3; j++) {\n                                    long long old_val = grid[p+i][q+j];\n                                    long long add = stamps[m][i][j];\n                                    long long new_val = old_val + add;\n                                    if (new_val >= mod) {\n                                        new_val -= mod;\n                                    }\n                                    delta += (new_val - old_val);\n                                }\n                            }\n                            if (delta > best_delta) {\n                                best_delta = delta;\n                                best_m = m;\n                                best_p = p;\n                                best_q = q;\n                            }\n                        }\n                    }\n                }\n\n                if (best_delta <= 0) {\n                    break;\n                }\n\n                for (int i = 0; i < 3; i++) {\n                    for (int j = 0; j < 3; j++) {\n                        long long old_val = grid[best_p+i][best_q+j];\n                        long long new_val = old_val + stamps[best_m][i][j];\n                        if (new_val >= mod) {\n                            new_val -= mod;\n                        }\n                        grid[best_p+i][best_q+j] = new_val;\n                        total_score += (new_val - old_val);\n                    }\n                }\n                new_ops.push_back(make_tuple(best_m, best_p, best_q));\n            }\n\n            if (total_score > total_score_backup) {\n                operations = current_ops;\n                operations.erase(operations.begin() + idx);\n                operations.insert(operations.end(), new_ops.begin(), new_ops.end());\n                improved = true;\n                break;\n            } else {\n                grid = grid_backup;\n                total_score = total_score_backup;\n            }\n        }\n\n        if (!improved) {\n            break;\n        }\n        passes++;\n    }\n\n    cout << operations.size() << '\\n';\n    for (const auto& op : operations) {\n        cout << get<0>(op) << ' ' << get<1>(op) << ' ' << get<2>(op) << '\\n';\n    }\n\n    return 0;\n}","ahc033":"#include <iostream>\n#include <vector>\n#include <string>\n#include <algorithm>\n#include <utility>\nusing namespace std;\n\nvoid move_to(string& s, int& cr, int& cc, int tr, int tc) {\n    while (cr < tr) {\n        s += 'D';\n        cr++;\n    }\n    while (cr > tr) {\n        s += 'U';\n        cr--;\n    }\n    while (cc < tc) {\n        s += 'R';\n        cc++;\n    }\n    while (cc > tc) {\n        s += 'L';\n        cc--;\n    }\n}\n\nint main() {\n    int N;\n    cin >> N;\n    vector<vector<int>> A(5, vector<int>(5));\n    for (int i = 0; i < 5; i++) {\n        for (int j = 0; j < 5; j++) {\n            cin >> A[i][j];\n        }\n    }\n\n    vector<string> crane_actions(5);\n    for (int i = 1; i < 5; i++) {\n        crane_actions[i] = \"RB\";\n    }\n\n    string large_actions = \"\";\n    int cr = 0, cc = 0;\n\n    vector<int> next_exp = {0, 5, 10, 15, 20};\n    vector<vector<bool>> buffer_occupied(5, vector<bool>(5, false));\n    vector<vector<int>> buffer_contents(5, vector<int>(5, -1));\n\n    for (int i = 0; i < 5; i++) {\n        for (int j = 0; j < 5; j++) {\n            if (i == 0 && j == 0) {\n                large_actions += 'P';\n            } else {\n                move_to(large_actions, cr, cc, i, 0);\n                large_actions += 'P';\n            }\n\n            int x = A[i][j];\n            int r = x / 5;\n            int target_col;\n            if (x == next_exp[r]) {\n                target_col = 4;\n            } else {\n                if (!buffer_occupied[r][2]) {\n                    target_col = 2;\n                    buffer_occupied[r][2] = true;\n                    buffer_contents[r][2] = x;\n                } else if (!buffer_occupied[r][3]) {\n                    target_col = 3;\n                    buffer_occupied[r][3] = true;\n                    buffer_contents[r][3] = x;\n                } else {\n                    target_col = 4;\n                }\n            }\n\n            move_to(large_actions, cr, cc, r, target_col);\n            large_actions += 'Q';\n\n            if (target_col == 4) {\n                next_exp[r]++;\n                bool cleared;\n                do {\n                    cleared = false;\n                    if (buffer_occupied[r][2] && buffer_contents[r][2] == next_exp[r]) {\n                        move_to(large_actions, cr, cc, r, 2);\n                        large_actions += 'P';\n                        move_to(large_actions, cr, cc, r, 4);\n                        large_actions += 'Q';\n                        next_exp[r]++;\n                        buffer_occupied[r][2] = false;\n                        cleared = true;\n                    } else if (buffer_occupied[r][3] && buffer_contents[r][3] == next_exp[r]) {\n                        move_to(large_actions, cr, cc, r, 3);\n                        large_actions += 'P';\n                        move_to(large_actions, cr, cc, r, 4);\n                        large_actions += 'Q';\n                        next_exp[r]++;\n                        buffer_occupied[r][3] = false;\n                        cleared = true;\n                    }\n                } while (cleared);\n            }\n\n            if (j < 4) {\n                move_to(large_actions, cr, cc, i, 1);\n            } else if (i < 4) {\n                move_to(large_actions, cr, cc, i+1, 1);\n            }\n        }\n    }\n\n    for (int r = 0; r < 5; r++) {\n        vector<pair<int, int>> bufs;\n        if (buffer_occupied[r][2]) {\n            bufs.push_back({buffer_contents[r][2], 2});\n        }\n        if (buffer_occupied[r][3]) {\n            bufs.push_back({buffer_contents[r][3], 3});\n        }\n        sort(bufs.begin(), bufs.end());\n        for (auto [num_val, col] : bufs) {\n            move_to(large_actions, cr, cc, r, col);\n            large_actions += 'P';\n            move_to(large_actions, cr, cc, r, 4);\n            large_actions += 'Q';\n        }\n    }\n\n    crane_actions[0] = large_actions;\n    size_t max_len = crane_actions[0].size();\n    for (int i = 1; i < 5; i++) {\n        if (crane_actions[i].size() < max_len) {\n            crane_actions[i] += string(max_len - crane_actions[i].size(), '.');\n        }\n    }\n\n    for (int i = 0; i < 5; i++) {\n        cout << crane_actions[i] << endl;\n    }\n\n    return 0;\n}","ahc034":"#include <iostream>\n#include <vector>\n#include <string>\n#include <algorithm>\n#include <cmath>\nusing namespace std;\n\nint main() {\n    int N;\n    cin >> N;\n    vector<vector<int>> grid(N, vector<int>(N));\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cin >> grid[i][j];\n        }\n    }\n\n    vector<string> ops;\n    int x = 0, y = 0;\n    int load = 0;\n\n    if (grid[0][0] < 0) {\n        int need = -grid[0][0];\n        if (load >= need) {\n            ops.push_back(\"-\" + to_string(need));\n            load -= need;\n            grid[0][0] = 0;\n        } else if (load > 0) {\n            ops.push_back(\"-\" + to_string(load));\n            grid[0][0] += load;\n            load = 0;\n        }\n    }\n    if (grid[0][0] > 0) {\n        ops.push_back(\"+\" + to_string(grid[0][0]));\n        load += grid[0][0];\n        grid[0][0] = 0;\n    }\n\n    for (int i = 0; i < N; i++) {\n        if (i % 2 == 0) {\n            if (i > 0) {\n                if (grid[i][0] < 0) {\n                    int need = -grid[i][0];\n                    if (load >= need) {\n                        ops.push_back(\"-\" + to_string(need));\n                        load -= need;\n                        grid[i][0] = 0;\n                    } else if (load > 0) {\n                        ops.push_back(\"-\" + to_string(load));\n                        grid[i][0] += load;\n                        load = 0;\n                    }\n                }\n                if (grid[i][0] > 0) {\n                    ops.push_back(\"+\" + to_string(grid[i][0]));\n                    load += grid[i][0];\n                    grid[i][0] = 0;\n                }\n            }\n            for (int j = (i == 0 ? 1 : 1); j < N; j++) {\n                ops.push_back(\"R\");\n                y++;\n                if (grid[i][y] < 0) {\n                    int need = -grid[i][y];\n                    if (load >= need) {\n                        ops.push_back(\"-\" + to_string(need));\n                        load -= need;\n                        grid[i][y] = 0;\n                    } else if (load > 0) {\n                        ops.push_back(\"-\" + to_string(load));\n                        grid[i][y] += load;\n                        load = 0;\n                    }\n                }\n                if (grid[i][y] > 0) {\n                    ops.push_back(\"+\" + to_string(grid[i][y]));\n                    load += grid[i][y];\n                    grid[i][y] = 0;\n                }\n            }\n            if (i < N-1) {\n                ops.push_back(\"D\");\n                x++;\n            }\n        } else {\n            if (grid[i][N-1] < 0) {\n                int need = -grid[i][N-1];\n                if (load >= need) {\n                    ops.push_back(\"-\" + to_string(need));\n                    load -= need;\n                    grid[i][N-1] = 0;\n                } else if (load > 0) {\n                    ops.push_back(\"-\" + to_string(load));\n                    grid[i][N-1] += load;\n                    load = 0;\n                }\n            }\n            if (grid[i][N-1] > 0) {\n                ops.push_back(\"+\" + to_string(grid[i][N-1]));\n                load += grid[i][N-1];\n                grid[i][N-1] = 0;\n            }\n            for (int j = N-2; j >= 0; j--) {\n                ops.push_back(\"L\");\n                y--;\n                if (grid[i][y] < 0) {\n                    int need = -grid[i][y];\n                    if (load >= need) {\n                        ops.push_back(\"-\" + to_string(need));\n                        load -= need;\n                        grid[i][y] = 0;\n                    } else if (load > 0) {\n                        ops.push_back(\"-\" + to_string(load));\n                        grid[i][y] += load;\n                        load = 0;\n                    }\n                }\n                if (grid[i][y] > 0) {\n                    ops.push_back(\"+\" + to_string(grid[i][y]));\n                    load += grid[i][y];\n                    grid[i][y] = 0;\n                }\n            }\n            if (i < N-1) {\n                ops.push_back(\"D\");\n                x++;\n            }\n        }\n    }\n\n    vector<pair<int, int>> deficits;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (grid[i][j] < 0) {\n                deficits.push_back({i, j});\n            }\n        }\n    }\n\n    while (load > 0) {\n        if (deficits.empty()) break;\n        int best_dist = 1000000000;\n        int best_index = -1;\n        for (int idx = 0; idx < deficits.size(); idx++) {\n            int i0 = deficits[idx].first;\n            int j0 = deficits[idx].second;\n            int d = abs(i0 - x) + abs(j0 - y);\n            if (d < best_dist) {\n                best_dist = d;\n                best_index = idx;\n            }\n        }\n        if (best_index == -1) break;\n        int target_i = deficits[best_index].first;\n        int target_j = deficits[best_index].second;\n        deficits[best_index] = deficits.back();\n        deficits.pop_back();\n\n        while (y < target_j) {\n            ops.push_back(\"R\");\n            y++;\n        }\n        while (y > target_j) {\n            ops.push_back(\"L\");\n            y--;\n        }\n        while (x < target_i) {\n            ops.push_back(\"D\");\n            x++;\n        }\n        while (x > target_i) {\n            ops.push_back(\"U\");\n            x--;\n        }\n\n        int d_val = -grid[target_i][target_j];\n        ops.push_back(\"-\" + to_string(d_val));\n        load -= d_val;\n        grid[target_i][target_j] = 0;\n    }\n\n    for (const string& op : ops) {\n        cout << op << endl;\n    }\n\n    return 0;\n}","ahc035":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <set>\n#include <utility>\n\nusing namespace std;\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    const int fixed_N = 6;\n    vector<vector<int>> weight(fixed_N, vector<int>(fixed_N, 0));\n    vector<pair<int, int>> even_positions, odd_positions;\n\n    for (int i = 0; i < fixed_N; i++) {\n        for (int j = 0; j < fixed_N; j++) {\n            int w = 0;\n            if (i > 0) w++;\n            if (i < fixed_N-1) w++;\n            if (j > 0) w++;\n            if (j < fixed_N-1) w++;\n            weight[i][j] = w;\n            if ((i+j) % 2 == 0) {\n                even_positions.push_back({i, j});\n            } else {\n                odd_positions.push_back({i, j});\n            }\n        }\n    }\n\n    auto weight_compare = [&](const pair<int, int>& a, const pair<int, int>& b) {\n        return weight[a.first][a.second] > weight[b.first][b.second];\n    };\n    sort(even_positions.begin(), even_positions.end(), weight_compare);\n    sort(odd_positions.begin(), odd_positions.end(), weight_compare);\n\n    int N, M, T;\n    cin >> N >> M >> T;\n\n    const int seed_count = 2 * N * (N - 1);\n    vector<vector<int>> seeds(seed_count, vector<int>(M, 0));\n\n    for (int i = 0; i < seed_count; i++) {\n        for (int j = 0; j < M; j++) {\n            cin >> seeds[i][j];\n        }\n    }\n\n    for (int t = 0; t < T; t++) {\n        vector<int> current_max(M, -1);\n        for (int l = 0; l < M; l++) {\n            for (int k = 0; k < seed_count; k++) {\n                if (seeds[k][l] > current_max[l]) {\n                    current_max[l] = seeds[k][l];\n                }\n            }\n        }\n\n        vector<int> V(seed_count, 0);\n        vector<int> trait_count(seed_count, 0);\n        vector<vector<bool>> trait(seed_count, vector<bool>(M, false));\n        \n        for (int k = 0; k < seed_count; k++) {\n            for (int l = 0; l < M; l++) {\n                V[k] += seeds[k][l];\n                if (seeds[k][l] == current_max[l]) {\n                    trait[k][l] = true;\n                    trait_count[k]++;\n                }\n            }\n        }\n\n        vector<int> coverage_count(M, 0);\n        set<int> chosen_seeds;\n\n        while (true) {\n            bool all_covered = true;\n            for (int l = 0; l < M; l++) {\n                if (coverage_count[l] < 2) {\n                    all_covered = false;\n                    break;\n                }\n            }\n            if (all_covered) break;\n\n            int best_seed = -1;\n            int best_cover = -1;\n            int best_V = -1;\n\n            for (int k = 0; k < seed_count; k++) {\n                if (chosen_seeds.find(k) != chosen_seeds.end()) continue;\n\n                int count = 0;\n                for (int l = 0; l < M; l++) {\n                    if (trait[k][l] && coverage_count[l] < 2) {\n                        count++;\n                    }\n                }\n\n                if (count == 0) continue;\n\n                if (count > best_cover || (count == best_cover && V[k] > best_V)) {\n                    best_cover = count;\n                    best_V = V[k];\n                    best_seed = k;\n                }\n            }\n\n            if (best_seed == -1) break;\n\n            chosen_seeds.insert(best_seed);\n            for (int l = 0; l < M; l++) {\n                if (trait[best_seed][l]) {\n                    coverage_count[l]++;\n                }\n            }\n        }\n\n        if (chosen_seeds.size() < N * N) {\n            vector<pair<int, int>> candidates;\n            for (int k = 0; k < seed_count; k++) {\n                if (chosen_seeds.find(k) == chosen_seeds.end()) {\n                    candidates.push_back({V[k], k});\n                }\n            }\n            sort(candidates.begin(), candidates.end(), [](const auto& a, const auto& b) {\n                return a.first > b.first;\n            });\n            int rem = N * N - chosen_seeds.size();\n            for (int i = 0; i < rem && i < candidates.size(); i++) {\n                chosen_seeds.insert(candidates[i].second);\n            }\n        }\n\n        const int half1 = 8;\n        vector<int> trait_frequency(M, 0);\n        for (int l = 0; l < M; l++) {\n            for (int k : chosen_seeds) {\n                if (trait[k][l]) {\n                    trait_frequency[l]++;\n                }\n            }\n        }\n        \n        vector<int> trait_order(M);\n        for (int l = 0; l < M; l++) trait_order[l] = l;\n        sort(trait_order.begin(), trait_order.end(), [&](int a, int b) {\n            return trait_frequency[a] < trait_frequency[b];\n        });\n        \n        vector<bool> assigned(seed_count, false);\n        vector<int> groupA, groupB;\n        vector<bool> hasA(M, false), hasB(M, false);\n        \n        for (int l : trait_order) {\n            if (!hasA[l]) {\n                int best_seed = -1;\n                for (int k : chosen_seeds) {\n                    if (assigned[k]) continue;\n                    if (!trait[k][l]) continue;\n                    if (best_seed == -1 || V[k] > V[best_seed]) {\n                        best_seed = k;\n                    }\n                }\n                if (best_seed != -1) {\n                    assigned[best_seed] = true;\n                    groupA.push_back(best_seed);\n                    for (int ll = 0; ll < M; ll++) {\n                        if (trait[best_seed][ll]) hasA[ll] = true;\n                    }\n                }\n            }\n            \n            if (!hasB[l]) {\n                int best_seed = -1;\n                for (int k : chosen_seeds) {\n                    if (assigned[k]) continue;\n                    if (!trait[k][l]) continue;\n                    if (best_seed == -1 || V[k] > V[best_seed]) {\n                        best_seed = k;\n                    }\n                }\n                if (best_seed != -1) {\n                    assigned[best_seed] = true;\n                    groupB.push_back(best_seed);\n                    for (int ll = 0; ll < M; ll++) {\n                        if (trait[best_seed][ll]) hasB[ll] = true;\n                    }\n                }\n            }\n        }\n        \n        vector<pair<int, int>> remaining_signature;\n        for (int k : chosen_seeds) {\n            if (assigned[k]) continue;\n            int score1 = 0, score2 = 0;\n            for (int l = 0; l < half1; l++) {\n                if (trait[k][l]) score1++;\n            }\n            for (int l = half1; l < M; l++) {\n                if (trait[k][l]) score2++;\n            }\n            remaining_signature.push_back({score1 - score2, k});\n        }\n        sort(remaining_signature.begin(), remaining_signature.end(), [](const auto& a, const auto& b) {\n            return a.first > b.first;\n        });\n        \n        for (auto& p : remaining_signature) {\n            int k = p.second;\n            if (groupA.size() < 18) {\n                groupA.push_back(k);\n            } else if (groupB.size() < 18) {\n                groupB.push_back(k);\n            }\n        }\n\n        auto seed_compare = [&](int a, int b) {\n            if (V[a] != V[b]) return V[a] > V[b];\n            return trait_count[a] > trait_count[b];\n        };\n        sort(groupA.begin(), groupA.end(), seed_compare);\n        sort(groupB.begin(), groupB.end(), seed_compare);\n\n        vector<vector<int>> grid(N, vector<int>(N, -1));\n        for (int idx = 0; idx < groupA.size(); idx++) {\n            int i = even_positions[idx].first;\n            int j = even_positions[idx].second;\n            grid[i][j] = groupA[idx];\n        }\n        for (int idx = 0; idx < groupB.size(); idx++) {\n            int i = odd_positions[idx].first;\n            int j = odd_positions[idx].second;\n            grid[i][j] = groupB[idx];\n        }\n\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                cout << grid[i][j];\n                if (j < N - 1) cout << \" \";\n                else cout << endl;\n            }\n        }\n        cout.flush();\n\n        for (int i = 0; i < seed_count; i++) {\n            for (int j = 0; j < M; j++) {\n                cin >> seeds[i][j];\n            }\n        }\n    }\n\n    return 0;\n}","ahc038":"#include <iostream>\n#include <vector>\n#include <cstdlib>\n#include <algorithm>\n#include <climits>\n#include <tuple>\nusing namespace std;\n\nint main() {\n    int N, M, V;\n    cin >> N >> M >> V;\n\n    vector<string> s_grid(N), t_grid(N);\n    for (int i = 0; i < N; i++) {\n        cin >> s_grid[i];\n    }\n    for (int i = 0; i < N; i++) {\n        cin >> t_grid[i];\n    }\n\n    int L0 = (N - 1) / 2;\n\n    cout << V << endl;\n    for (int i = 1; i < V; i++) {\n        cout << \"0 \" << L0 << endl;\n    }\n\n    int rx0 = N / 2;\n    int ry0 = N / 2;\n    cout << rx0 << \" \" << ry0 << endl;\n\n    int cur_x = rx0, cur_y = ry0;\n    vector<int> dirs(V, 0);\n    vector<bool> holding(V, false);\n\n    vector<pair<int, int>> initial_list;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (s_grid[i][j] == '1') {\n                initial_list.push_back({i, j});\n            }\n        }\n    }\n\n    vector<pair<int, int>> target_list;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (t_grid[i][j] == '1') {\n                target_list.push_back({i, j});\n            }\n        }\n    }\n\n    vector<string> outputs;\n\n    while (!initial_list.empty()) {\n        int F_available = V - 1;\n        int batch_size = min(F_available, (int)initial_list.size());\n        vector<tuple<int, int, int, int, int, int>> pickup_batch;\n        vector<int> available_fingertips;\n        for (int i = 1; i < V; i++) {\n            available_fingertips.push_back(i);\n        }\n\n        int current_start_x = cur_x;\n        int current_start_y = cur_y;\n\n        for (int idx = 0; idx < batch_size; idx++) {\n            int best_cost = INT_MAX;\n            int best_i = -1, best_j = -1, best_cx = -1, best_cy = -1, best_d = -1, best_f = -1;\n            for (int k = 0; k < initial_list.size(); k++) {\n                int i = initial_list[k].first;\n                int j = initial_list[k].second;\n                vector<tuple<int, int, int>> candidates;\n                if (j - L0 >= 0) {\n                    candidates.push_back(make_tuple(i, j - L0, 0));\n                }\n                if (i - L0 >= 0) {\n                    candidates.push_back(make_tuple(i - L0, j, 1));\n                }\n                if (j + L0 < N) {\n                    candidates.push_back(make_tuple(i, j + L0, 2));\n                }\n                if (i + L0 < N) {\n                    candidates.push_back(make_tuple(i + L0, j, 3));\n                }\n                for (auto &cand : candidates) {\n                    int cx = get<0>(cand);\n                    int cy = get<1>(cand);\n                    int d = get<2>(cand);\n                    for (int f_index = 0; f_index < available_fingertips.size(); f_index++) {\n                        int f = available_fingertips[f_index];\n                        int dist = abs(cx - current_start_x) + abs(cy - current_start_y);\n                        int cur_dir = dirs[f];\n                        int diff = (d - cur_dir + 4) % 4;\n                        int rot_cost = min(diff, 4 - diff);\n                        int cost = dist + rot_cost;\n                        if (cost < best_cost) {\n                            best_cost = cost;\n                            best_i = i;\n                            best_j = j;\n                            best_cx = cx;\n                            best_cy = cy;\n                            best_d = d;\n                            best_f = f;\n                        }\n                    }\n                }\n            }\n            if (best_cost == INT_MAX) {\n                batch_size = idx;\n                break;\n            }\n\n            for (auto it = initial_list.begin(); it != initial_list.end(); it++) {\n                if (it->first == best_i && it->second == best_j) {\n                    initial_list.erase(it);\n                    break;\n                }\n            }\n\n            for (auto it = available_fingertips.begin(); it != available_fingertips.end(); it++) {\n                if (*it == best_f) {\n                    available_fingertips.erase(it);\n                    break;\n                }\n            }\n\n            pickup_batch.push_back(make_tuple(best_i, best_j, best_cx, best_cy, best_d, best_f));\n            current_start_x = best_cx;\n            current_start_y = best_cy;\n        }\n\n        for (auto &item : pickup_batch) {\n            int i = get<0>(item);\n            int j = get<1>(item);\n            int cx = get<2>(item);\n            int cy = get<3>(item);\n            int d = get<4>(item);\n            int f = get<5>(item);\n\n            int dx = cx - cur_x;\n            int dy = cy - cur_y;\n\n            vector<char> moves;\n            if (dx > 0) {\n                for (int k = 0; k < dx; k++) moves.push_back('R');\n            } else if (dx < 0) {\n                for (int k = 0; k < -dx; k++) moves.push_back('L');\n            }\n            if (dy > 0) {\n                for (int k = 0; k < dy; k++) moves.push_back('D');\n            } else if (dy < 0) {\n                for (int k = 0; k < -dy; k++) moves.push_back('U');\n            }\n\n            int cur_dir = dirs[f];\n            int diff = (d - cur_dir + 4) % 4;\n            vector<char> rots;\n            if (diff == 1) {\n                rots.push_back('R');\n            } else if (diff == 2) {\n                rots.push_back('R');\n                rots.push_back('R');\n            } else if (diff == 3) {\n                rots.push_back('L');\n            }\n\n            int T = max(moves.size(), rots.size());\n            for (int t = 0; t < T; t++) {\n                string op = \"\";\n                if (t < moves.size()) {\n                    op += moves[t];\n                } else {\n                    op += '.';\n                }\n\n                for (int v = 1; v < V; v++) {\n                    if (v == f && t < rots.size()) {\n                        op += rots[t];\n                    } else {\n                        op += '.';\n                    }\n                }\n\n                for (int v = 0; v < V; v++) {\n                    if (v == f && t == T - 1) {\n                        op += 'P';\n                        holding[f] = true;\n                    } else {\n                        op += '.';\n                    }\n                }\n                outputs.push_back(op);\n            }\n\n            cur_x = cx;\n            cur_y = cy;\n            dirs[f] = d;\n        }\n\n        vector<pair<int, int>> available_targets = target_list;\n        vector<tuple<int, int, int, int, int, int>> delivery_batch;\n        current_start_x = cur_x;\n        current_start_y = cur_y;\n\n        for (int idx = 0; idx < batch_size; idx++) {\n            int best_cost = INT_MAX;\n            int best_i = -1, best_j = -1, best_cx = -1, best_cy = -1, best_d = -1, best_f = -1;\n            for (int k = 0; k < available_targets.size(); k++) {\n                int i = available_targets[k].first;\n                int j = available_targets[k].second;\n                vector<tuple<int, int, int>> candidates;\n                if (j - L0 >= 0) {\n                    candidates.push_back(make_tuple(i, j - L0, 0));\n                }\n                if (i - L0 >= 0) {\n                    candidates.push_back(make_tuple(i - L0, j, 1));\n                }\n                if (j + L0 < N) {\n                    candidates.push_back(make_tuple(i, j + L0, 2));\n                }\n                if (i + L0 < N) {\n                    candidates.push_back(make_tuple(i + L0, j, 3));\n                }\n                for (auto &cand : candidates) {\n                    int cx = get<0>(cand);\n                    int cy = get<1>(cand);\n                    int d = get<2>(cand);\n                    for (auto &p_item : pickup_batch) {\n                        int f = get<5>(p_item);\n                        if (holding[f]) {\n                            int dist = abs(cx - current_start_x) + abs(cy - current_start_y);\n                            int cur_dir = dirs[f];\n                            int diff = (d - cur_dir + 4) % 4;\n                            int rot_cost = min(diff, 4 - diff);\n                            int cost = dist + rot_cost;\n                            if (cost < best_cost) {\n                                best_cost = cost;\n                                best_i = i;\n                                best_j = j;\n                                best_cx = cx;\n                                best_cy = cy;\n                                best_d = d;\n                                best_f = f;\n                            }\n                        }\n                    }\n                }\n            }\n            if (best_cost == INT_MAX) {\n                batch_size = idx;\n                break;\n            }\n\n            for (auto it = available_targets.begin(); it != available_targets.end(); it++) {\n                if (it->first == best_i && it->second == best_j) {\n                    available_targets.erase(it);\n                    break;\n                }\n            }\n\n            delivery_batch.push_back(make_tuple(best_i, best_j, best_cx, best_cy, best_d, best_f));\n            current_start_x = best_cx;\n            current_start_y = best_cy;\n        }\n\n        for (auto &item : delivery_batch) {\n            int i = get<0>(item);\n            int j = get<1>(item);\n            int cx = get<2>(item);\n            int cy = get<3>(item);\n            int d = get<4>(item);\n            int f = get<5>(item);\n\n            int dx = cx - cur_x;\n            int dy = cy - cur_y;\n\n            vector<char> moves;\n            if (dx > 0) {\n                for (int k = 0; k < dx; k++) moves.push_back('R');\n            } else if (dx < 0) {\n                for (int k = 0; k < -dx; k++) moves.push_back('L');\n            }\n            if (dy > 0) {\n                for (int k = 0; k < dy; k++) moves.push_back('D');\n            } else if (dy < 0) {\n                for (int k = 0; k < -dy; k++) moves.push_back('U');\n            }\n\n            int cur_dir = dirs[f];\n            int diff = (d - cur_dir + 4) % 4;\n            vector<char> rots;\n            if (diff == 1) {\n                rots.push_back('R');\n            } else if (diff == 2) {\n                rots.push_back('R');\n                rots.push_back('R');\n            } else if (diff == 3) {\n                rots.push_back('L');\n            }\n\n            int T = max(moves.size(), rots.size());\n            for (int t = 0; t < T; t++) {\n                string op = \"\";\n                if (t < moves.size()) {\n                    op += moves[t];\n                } else {\n                    op += '.';\n                }\n\n                for (int v = 1; v < V; v++) {\n                    if (v == f && t < rots.size()) {\n                        op += rots[t];\n                    } else {\n                        op += '.';\n                    }\n                }\n\n                for (int v = 0; v < V; v++) {\n                    if (v == f && t == T - 1) {\n                        op += 'P';\n                        holding[f] = false;\n                    } else {\n                        op += '.';\n                    }\n                }\n                outputs.push_back(op);\n            }\n\n            cur_x = cx;\n            cur_y = cy;\n            dirs[f] = d;\n        }\n\n        for (auto &item : delivery_batch) {\n            int i = get<0>(item);\n            int j = get<1>(item);\n            for (auto it = target_list.begin(); it != target_list.end(); it++) {\n                if (it->first == i && it->second == j) {\n                    target_list.erase(it);\n                    break;\n                }\n            }\n        }\n    }\n\n    for (string &s : outputs) {\n        cout << s << endl;\n    }\n\n    return 0;\n}","ahc039":"#include <iostream>\n#include <vector>\n#include <set>\n#include <algorithm>\n#include <climits>\n#include <tuple>\n#include <map>\n#include <queue>\n#include <unordered_map>\n#include <cmath>\nusing namespace std;\n\nstruct Point {\n    int x, y;\n    Point(int x=0, int y=0) : x(x), y(y) {}\n    bool operator<(const Point& other) const {\n        if (x != other.x) return x < other.x;\n        return y < other.y;\n    }\n    bool operator==(const Point& other) const {\n        return x == other.x && y == other.y;\n    }\n};\n\nnamespace std {\n    template<> struct hash<Point> {\n        size_t operator()(const Point& p) const {\n            return (size_t)p.x * 1000000 + p.y;\n        }\n    };\n}\n\nconst int GRID = 15;\n\nint main() {\n    int N;\n    cin >> N;\n    vector<Point> mackerels;\n    vector<Point> sardines;\n    for (int i = 0; i < 2*N; i++) {\n        int x, y;\n        cin >> x >> y;\n        if (i < N) {\n            mackerels.push_back(Point(x, y));\n        } else {\n            sardines.push_back(Point(x, y));\n        }\n    }\n\n    set<pair<int, int>> sardine_set;\n    for (auto& p : sardines) {\n        sardine_set.insert({p.x, p.y});\n    }\n\n    vector<int> x_grid(GRID+1);\n    vector<int> y_grid(GRID+1);\n    for (int i = 0; i <= GRID; i++) {\n        x_grid[i] = (long long)i * 100000 / GRID;\n        y_grid[i] = (long long)i * 100000 / GRID;\n    }\n    x_grid[GRID] = 100000;\n    y_grid[GRID] = 100000;\n\n    vector<vector<int>> grid_m(GRID, vector<int>(GRID, 0));\n    vector<vector<int>> grid_s(GRID, vector<int>(GRID, 0));\n    vector<vector<int>> grid_count(GRID, vector<int>(GRID, 0));\n\n    for (auto& p : mackerels) {\n        int i = lower_bound(x_grid.begin(), x_grid.end(), p.x + 1) - x_grid.begin() - 1;\n        int j = lower_bound(y_grid.begin(), y_grid.end(), p.y + 1) - y_grid.begin() - 1;\n        if (i < 0) i = 0;\n        if (i >= GRID) i = GRID-1;\n        if (j < 0) j = 0;\n        if (j >= GRID) j = GRID-1;\n        grid_m[i][j]++;\n    }\n\n    for (auto& p : sardines) {\n        int i = lower_bound(x_grid.begin(), x_grid.end(), p.x + 1) - x_grid.begin() - 1;\n        int j = lower_bound(y_grid.begin(), y_grid.end(), p.y + 1) - y_grid.begin() - 1;\n        if (i < 0) i = 0;\n        if (i >= GRID) i = GRID-1;\n        if (j < 0) j = 0;\n        if (j >= GRID) j = GRID-1;\n        grid_s[i][j]++;\n    }\n\n    for (int i = 0; i < GRID; i++) {\n        for (int j = 0; j < GRID; j++) {\n            grid_count[i][j] = grid_m[i][j] - grid_s[i][j];\n        }\n    }\n\n    int best_score = -1000000;\n    int best_i = -1, best_j = -1;\n    for (int i = 0; i < GRID; i++) {\n        for (int j = 0; j < GRID; j++) {\n            if (grid_count[i][j] > best_score) {\n                best_score = grid_count[i][j];\n                best_i = i;\n                best_j = j;\n            }\n        }\n    }\n\n    if (best_score <= 0) {\n        int best_cnt = 10;\n        Point best_point;\n        bool found = false;\n        for (auto& p : mackerels) {\n            int cnt = 0;\n            for (int dx = -1; dx <= 1; dx++) {\n                for (int dy = -1; dy <= 1; dy++) {\n                    int x1 = p.x + dx;\n                    int y1 = p.y + dy;\n                    if (x1 < 0 || x1 > 100000 || y1 < 0 || y1 > 100000) continue;\n                    if (sardine_set.find({x1, y1}) != sardine_set.end()) {\n                        cnt++;\n                    }\n                }\n            }\n            if (cnt < best_cnt) {\n                best_cnt = cnt;\n                best_point = p;\n                found = true;\n            }\n        }\n        if (!found) {\n            best_point = mackerels[0];\n        }\n\n        int best_value_fallback = -1000000;\n        int best_x1 = 0, best_y1 = 0, best_x2 = 0, best_y2 = 0;\n\n        for (int s = 1; s <= 10; s++) {\n            for (int dx = 0; dx <= s; dx++) {\n                for (int dy = 0; dy <= s; dy++) {\n                    int x0 = best_point.x;\n                    int y0 = best_point.y;\n                    int x1 = max(0, x0 - dx);\n                    int y1 = max(0, y0 - dy);\n                    int x2 = min(100000, x1 + s);\n                    int y2 = min(100000, y1 + s);\n\n                    int m = 0, sard = 0;\n                    for (auto& p : mackerels) {\n                        if (p.x >= x1 && p.x <= x2 && p.y >= y1 && p.y <= y2) {\n                            m++;\n                        }\n                    }\n                    for (auto& p : sardines) {\n                        if (p.x >= x1 && p.x <= x2 && p.y >= y1 && p.y <= y2) {\n                            sard++;\n                        }\n                    }\n                    int value = m - sard;\n                    if (value > best_value_fallback) {\n                        best_value_fallback = value;\n                        best_x1 = x1;\n                        best_y1 = y1;\n                        best_x2 = x2;\n                        best_y2 = y2;\n                    }\n                }\n            }\n        }\n\n        vector<Point> poly;\n        poly.push_back(Point(best_x1, best_y2));\n        poly.push_back(Point(best_x2, best_y2));\n        poly.push_back(Point(best_x2, best_y1));\n        poly.push_back(Point(best_x1, best_y1));\n        cout << 4 << \"\\n\";\n        for (auto& p : poly) {\n            cout << p.x << \" \" << p.y << \"\\n\";\n        }\n        return 0;\n    }\n\n    set<pair<int, int>> R;\n    set<pair<int, int>> frontier;\n    R.insert({best_i, best_j});\n    long long total_perimeter = 0;\n    const int dx4[4] = {0, 1, 0, -1};\n    const int dy4[4] = {1, 0, -1, 0};\n\n    int w0 = x_grid[best_i+1] - x_grid[best_i];\n    int h0 = y_grid[best_j+1] - y_grid[best_j];\n    total_perimeter = 2LL * (w0 + h0);\n\n    for (int d = 0; d < 4; d++) {\n        int ni = best_i + dx4[d];\n        int nj = best_j + dy4[d];\n        if (ni < 0 || ni >= GRID || nj < 0 || nj >= GRID) continue;\n        if (R.find({ni, nj}) == R.end()) {\n            frontier.insert({ni, nj});\n        }\n    }\n\n    while (!frontier.empty()) {\n        int best_cell_i = -1, best_cell_j = -1;\n        int best_cell_score = -1000000;\n        long long best_change_perimeter = 0;\n        vector<pair<int, int>> to_remove;\n        for (auto cell : frontier) {\n            int i = cell.first;\n            int j = cell.second;\n            if (R.find(cell) != R.end()) {\n                to_remove.push_back(cell);\n                continue;\n            }\n            int w = x_grid[i+1] - x_grid[i];\n            int h = y_grid[j+1] - y_grid[j];\n            long long shared_edges = 0;\n            for (int d = 0; d < 4; d++) {\n                int ni = i + dx4[d];\n                int nj = j + dy4[d];\n                if (ni < 0 || ni >= GRID || nj < 0 || nj >= GRID) continue;\n                if (R.find({ni, nj}) != R.end()) {\n                    if (d == 0 || d == 2) {\n                        shared_edges += h;\n                    } else {\n                        shared_edges += w;\n                    }\n                }\n            }\n            long long change_perimeter = 2LL * (w + h) - 2 * shared_edges;\n            if (total_perimeter + change_perimeter > 400000) continue;\n            if (grid_count[i][j] > best_cell_score) {\n                best_cell_score = grid_count[i][j];\n                best_cell_i = i;\n                best_cell_j = j;\n                best_change_perimeter = change_perimeter;\n            }\n        }\n        for (auto& cell : to_remove) {\n            frontier.erase(cell);\n        }\n        if (best_cell_score <= 0) {\n            break;\n        }\n        if (best_cell_i == -1) {\n            break;\n        }\n        R.insert({best_cell_i, best_cell_j});\n        total_perimeter += best_change_perimeter;\n        frontier.erase({best_cell_i, best_cell_j});\n        for (int d = 0; d < 4; d++) {\n            int ni = best_cell_i + dx4[d];\n            int nj = best_cell_j + dy4[d];\n            if (ni < 0 || ni >= GRID || nj < 0 || nj >= GRID) continue;\n            if (R.find({ni, nj}) == R.end()) {\n                frontier.insert({ni, nj});\n            }\n        }\n    }\n\n    vector<vector<bool>> grid_selected(GRID, vector<bool>(GRID, false));\n    for (auto& cell : R) {\n        grid_selected[cell.first][cell.second] = true;\n    }\n\n    vector<vector<bool>> visited(GRID, vector<bool>(GRID, false));\n    queue<pair<int, int>> q;\n    for (int i = 0; i < GRID; i++) {\n        for (int j = 0; j < GRID; j++) {\n            if (i == 0 || i == GRID-1 || j == 0 || j == GRID-1) {\n                if (!grid_selected[i][j]) {\n                    visited[i][j] = true;\n                    q.push({i, j});\n                }\n            }\n        }\n    }\n\n    const int di[4] = {0, 1, 0, -1};\n    const int dj[4] = {1, 0, -1, 0};\n    while (!q.empty()) {\n        auto cell = q.front(); q.pop();\n        int i = cell.first;\n        int j = cell.second;\n        for (int d = 0; d < 4; d++) {\n            int ni = i + di[d];\n            int nj = j + dj[d];\n            if (ni < 0 || ni >= GRID || nj < 0 || nj >= GRID) continue;\n            if (visited[ni][nj]) continue;\n            if (grid_selected[ni][nj]) continue;\n            visited[ni][nj] = true;\n            q.push({ni, nj});\n        }\n    }\n\n    bool hasHole = false;\n    for (int i = 0; i < GRID; i++) {\n        for (int j = 0; j < GRID; j++) {\n            if (!grid_selected[i][j] && !visited[i][j]) {\n                hasHole = true;\n                break;\n            }\n        }\n        if (hasHole) break;\n    }\n\n    if (!hasHole && total_perimeter <= 400000) {\n        set<tuple<int, int, int, int>> edgeSet;\n        for (auto& cell : R) {\n            int i = cell.first;\n            int j = cell.second;\n            tuple<int, int, int, int> top = make_tuple(x_grid[i], y_grid[j+1], x_grid[i+1], y_grid[j+1]);\n            tuple<int, int, int, int> right = make_tuple(x_grid[i+1], y_grid[j], x_grid[i+1], y_grid[j+1]);\n            tuple<int, int, int, int> bottom = make_tuple(x_grid[i], y_grid[j], x_grid[i+1], y_grid[j]);\n            tuple<int, int, int, int> left = make_tuple(x_grid[i], y_grid[j], x_grid[i], y_grid[j+1]);\n            if (edgeSet.find(top) != edgeSet.end()) {\n                edgeSet.erase(top);\n            } else {\n                edgeSet.insert(top);\n            }\n            if (edgeSet.find(right) != edgeSet.end()) {\n                edgeSet.erase(right);\n            } else {\n                edgeSet.insert(right);\n            }\n            if (edgeSet.find(bottom) != edgeSet.end()) {\n                edgeSet.erase(bottom);\n            } else {\n                edgeSet.insert(bottom);\n            }\n            if (edgeSet.find(left) != edgeSet.end()) {\n                edgeSet.erase(left);\n            } else {\n                edgeSet.insert(left);\n            }\n        }\n\n        map<Point, vector<Point>> graph;\n        for (auto& edge : edgeSet) {\n            int x1 = get<0>(edge);\n            int y1 = get<1>(edge);\n            int x2 = get<2>(edge);\n            int y2 = get<3>(edge);\n            Point p1, p2;\n            if (y1 == y2) {\n                p1 = Point(x1, y1);\n                p2 = Point(x2, y1);\n            } else {\n                p1 = Point(x1, y1);\n                p2 = Point(x1, y2);\n            }\n            graph[p1].push_back(p2);\n            graph[p2].push_back(p1);\n        }\n\n        Point start(100000, 0);\n        for (auto& node : graph) {\n            Point p = node.first;\n            if (p.x < start.x || (p.x == start.x && p.y > start.y)) {\n                start = p;\n            }\n        }\n\n        vector<Point> poly;\n        poly.push_back(start);\n        Point prev = Point(-1, -1);\n        Point cur = start;\n        Point next(-1, -1);\n        vector<Point> neighbors = graph[start];\n        for (Point p : neighbors) {\n            if (p.y == start.y && p.x > start.x) {\n                next = p;\n                break;\n            }\n        }\n        if (next.x == -1) {\n            for (Point p : neighbors) {\n                if (p.x == start.x && p.y < start.y) {\n                    next = p;\n                    break;\n                }\n            }\n        }\n        if (next.x == -1) {\n            next = neighbors[0];\n        }\n        poly.push_back(next);\n        prev = cur;\n        cur = next;\n\n        while (cur != start) {\n            neighbors = graph[cur];\n            Point next_candidate(-1, -1);\n            for (Point p : neighbors) {\n                if (p == prev) continue;\n                next_candidate = p;\n                break;\n            }\n            if (next_candidate.x == -1) {\n                break;\n            }\n            poly.push_back(next_candidate);\n            prev = cur;\n            cur = next_candidate;\n        }\n\n        vector<Point> cleaned_poly;\n        cleaned_poly.push_back(poly[0]);\n        for (int i = 1; i < poly.size(); i++) {\n            if (poly[i].x == cleaned_poly.back().x && poly[i].y == cleaned_poly.back().y) {\n                continue;\n            }\n            cleaned_poly.push_back(poly[i]);\n        }\n        if (cleaned_poly.size() >= 2 && cleaned_poly[0].x == cleaned_poly.back().x && cleaned_poly[0].y == cleaned_poly.back().y) {\n            cleaned_poly.pop_back();\n        }\n\n        if (cleaned_poly.size() >= 4) {\n            long long actual_perim = 0;\n            int m = cleaned_poly.size();\n            for (int i = 0; i < m; i++) {\n                int j = (i+1) % m;\n                actual_perim += abs(cleaned_poly[i].x - cleaned_poly[j].x) + abs(cleaned_poly[i].y - cleaned_poly[j].y);\n            }\n\n            if (actual_perim <= 400000) {\n                cout << cleaned_poly.size() << \"\\n\";\n                for (auto& p : cleaned_poly) {\n                    cout << p.x << \" \" << p.y << \"\\n\";\n                }\n                return 0;\n            }\n        }\n    }\n\n    int min_x = 100000, max_x = 0, min_y = 100000, max_y = 0;\n    for (auto cell : R) {\n        int i = cell.first;\n        int j = cell.second;\n        min_x = min(min_x, x_grid[i]);\n        max_x = max(max_x, x_grid[i+1]);\n        min_y = min(min_y, y_grid[j]);\n        max_y = max(max_y, y_grid[j+1]);\n    }\n\n    long long bbox_perim = 2LL * ((max_x - min_x) + (max_y - min_y));\n    if (bbox_perim > 400000) {\n        min_x = x_grid[best_i];\n        max_x = x_grid[best_i+1];\n        min_y = y_grid[best_j];\n        max_y = y_grid[best_j+1];\n    }\n\n    vector<Point> poly;\n    poly.push_back(Point(min_x, max_y));\n    poly.push_back(Point(max_x, max_y));\n    poly.push_back(Point(max_x, min_y));\n    poly.push_back(Point(min_x, min_y));\n    cout << 4 << \"\\n\";\n    for (auto& p : poly) {\n        cout << p.x << \" \" << p.y << \"\\n\";\n    }\n\n    return 0;\n}","ahc040":"#include <iostream>\n#include <vector>\n#include <set>\n#include <map>\n#include <algorithm>\n#include <cmath>\n#include <cctype>\n#include <tuple>\n#include <iomanip>\n#include <climits>\n#include <random>\n#include <chrono>\n\nusing namespace std;\n\nset<int> get_reference_set(int strategy, const vector<int>& placed_list, const vector<tuple<long long, long long, long long, long long>>& rect_pos) {\n    set<int> refs;\n    refs.insert(-1);\n    if (placed_list.empty()) {\n        return refs;\n    }\n    \n    if (strategy == 0) {\n        refs.insert(placed_list.back());\n    } else if (strategy == 1) {\n        int count = 0;\n        for (auto it = placed_list.rbegin(); it != placed_list.rend() && count < 5; it++) {\n            refs.insert(*it);\n            count++;\n        }\n    } else if (strategy == 2) {\n        vector<pair<long long, int>> areas;\n        for (int j : placed_list) {\n            auto [x, y, w, h] = rect_pos[j];\n            areas.push_back({w * h, j});\n        }\n        sort(areas.rbegin(), areas.rend());\n        for (int i = 0; i < min(5, (int)areas.size()); i++) {\n            refs.insert(areas[i].second);\n        }\n    } else if (strategy == 3) {\n        refs.insert(placed_list.back());\n        long long max_area = 0;\n        int best_j = -1;\n        for (int j : placed_list) {\n            auto [x, y, w, h] = rect_pos[j];\n            long long area = w * h;\n            if (area > max_area) {\n                max_area = area;\n                best_j = j;\n            }\n        }\n        if (best_j != -1) {\n            refs.insert(best_j);\n        }\n    } else if (strategy == 4) {\n        long long max_right = -1;\n        int best_j = -1;\n        for (int j : placed_list) {\n            auto [x, y, w, h] = rect_pos[j];\n            if (x + w > max_right) {\n                max_right = x + w;\n                best_j = j;\n            }\n        }\n        if (best_j != -1) {\n            refs.insert(best_j);\n        }\n    } else if (strategy == 5) {\n        long long max_bottom = -1;\n        int best_j = -1;\n        for (int j : placed_list) {\n            auto [x, y, w, h] = rect_pos[j];\n            if (y + h > max_bottom) {\n                max_bottom = y + h;\n                best_j = j;\n            }\n        }\n        if (best_j != -1) {\n            refs.insert(best_j);\n        }\n    } else if (strategy == 6) {\n        refs.insert(placed_list.back());\n        long long max_right = -1, max_bottom = -1;\n        int best_right = -1, best_bottom = -1;\n        for (int j : placed_list) {\n            auto [x, y, w, h] = rect_pos[j];\n            if (x + w > max_right) {\n                max_right = x + w;\n                best_right = j;\n            }\n            if (y + h > max_bottom) {\n                max_bottom = y + h;\n                best_bottom = j;\n            }\n        }\n        if (best_right != -1) refs.insert(best_right);\n        if (best_bottom != -1) refs.insert(best_bottom);\n    } else if (strategy == 7) {\n        refs.insert(placed_list.back());\n        vector<pair<long long, int>> areas;\n        for (int j : placed_list) {\n            auto [x, y, w, h] = rect_pos[j];\n            areas.push_back({w * h, j});\n        }\n        sort(areas.rbegin(), areas.rend());\n        for (int i = 0; i < min(3, (int)areas.size()); i++) {\n            refs.insert(areas[i].second);\n        }\n    }\n    return refs;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, T;\n    double sigma;\n    cin >> N >> T >> sigma;\n\n    vector<double> w_est(N);\n    vector<double> h_est(N);\n    vector<double> w_sum(N, 0.0);\n    vector<double> h_sum(N, 0.0);\n    vector<int> count(N, 0);\n\n    for (int i = 0; i < N; i++) {\n        cin >> w_est[i] >> h_est[i];\n    }\n\n    int exploration_turns = min(N, T);\n    vector<int> exploration_order(N);\n    for (int i = 0; i < N; i++) {\n        exploration_order[i] = i;\n    }\n    sort(exploration_order.begin(), exploration_order.end(), [&](int i, int j) {\n        return (w_est[i] * h_est[i]) > (w_est[j] * h_est[j]);\n    });\n\n    for (int turn = 0; turn < exploration_turns; turn++) {\n        int idx = exploration_order[turn % N];\n        cout << 1 << '\\n';\n        cout << idx << \" \" << 0 << \" \" << \"U\" << \" \" << -1 << '\\n';\n        cout.flush();\n\n        long long Wp, Hp;\n        cin >> Wp >> Hp;\n\n        w_sum[idx] += Wp;\n        h_sum[idx] += Hp;\n        count[idx]++;\n    }\n\n    for (int i = 0; i < N; i++) {\n        if (count[i] > 0) {\n            w_est[i] = w_sum[i] / count[i];\n            h_est[i] = h_sum[i] / count[i];\n        }\n    }\n\n    int packing_turns = T - exploration_turns;\n    vector<tuple<vector<tuple<int, int, char, int>>, long long>> candidates;\n    const int NUM_CANDIDATES = 200;\n\n    vector<long long> w_sim(N), h_sim(N);\n    for (int i = 0; i < N; i++) {\n        w_sim[i] = static_cast<long long>(round(w_est[i]));\n        h_sim[i] = static_cast<long long>(round(h_est[i]));\n        if (w_sim[i] < 1) w_sim[i] = 1;\n        if (w_sim[i] > 1000000000) w_sim[i] = 1000000000;\n        if (h_sim[i] < 1) h_sim[i] = 1;\n        if (h_sim[i] > 1000000000) h_sim[i] = 1000000000;\n    }\n\n    if (packing_turns > 0) {\n        auto seed = chrono::steady_clock::now().time_since_epoch().count();\n        auto rng = mt19937(seed);\n        vector<double> thresholds = {0.8, 0.9, 1.0, 1.1, 1.2};\n        uniform_int_distribution<int> dist_ref(0, 7);\n\n        for (int cand_idx = 0; cand_idx < NUM_CANDIDATES; cand_idx++) {\n            double threshold = thresholds[cand_idx % thresholds.size()];\n            int ref_strategy = dist_ref(rng);\n\n            vector<tuple<int, int, char, int>> sequence;\n            vector<bool> placed(N, false);\n            vector<tuple<long long, long long, long long, long long>> rect_pos(N);\n            vector<int> placed_list;\n            long long current_max_x = 0;\n            long long current_max_y = 0;\n\n            for (int idx = 0; idx < N; idx++) {\n                set<int> refs = get_reference_set(ref_strategy, placed_list, rect_pos);\n                vector<tuple<long long, int, char, int, long long, long long, long long, long long>> candidates_for_rect;\n\n                for (int rot = 0; rot < 2; rot++) {\n                    long long w = (rot == 0) ? w_sim[idx] : h_sim[idx];\n                    long long h = (rot == 0) ? h_sim[idx] : w_sim[idx];\n                    for (char d : {'U', 'L'}) {\n                        for (int b_ref : refs) {\n                            long long x0 = 0, y0 = 0;\n                            if (d == 'U') {\n                                if (b_ref == -1) {\n                                    x0 = 0;\n                                } else {\n                                    if (placed[b_ref]) {\n                                        auto [x_pl, y_pl, w_pl, h_pl] = rect_pos[b_ref];\n                                        x0 = x_pl + w_pl;\n                                    } else {\n                                        continue;\n                                    }\n                                }\n                                for (int j : placed_list) {\n                                    auto [x_pl, y_pl, w_pl, h_pl] = rect_pos[j];\n                                    if (x0 < x_pl + w_pl && x0 + w > x_pl && y_pl + h_pl > y0) {\n                                        y0 = y_pl + h_pl;\n                                    }\n                                }\n                            } else {\n                                if (b_ref == -1) {\n                                    y0 = 0;\n                                } else {\n                                    if (placed[b_ref]) {\n                                        auto [x_pl, y_pl, w_pl, h_pl] = rect_pos[b_ref];\n                                        y0 = y_pl + h_pl;\n                                    } else {\n                                        continue;\n                                    }\n                                }\n                                for (int j : placed_list) {\n                                    auto [x_pl, y_pl, w_pl, h_pl] = rect_pos[j];\n                                    if (y0 < y_pl + h_pl && y0 + h > y_pl && x_pl + w_pl > x0) {\n                                        x0 = x_pl + w_pl;\n                                    }\n                                }\n                            }\n\n                            long long new_max_x = max(current_max_x, x0 + w);\n                            long long new_max_y = max(current_max_y, y0 + h);\n                            long long increase = (new_max_x + new_max_y) - (current_max_x + current_max_y);\n                            candidates_for_rect.push_back(make_tuple(increase, rot, d, b_ref, new_max_x, new_max_y, x0, y0));\n                        }\n                    }\n                }\n\n                if (candidates_for_rect.empty()) {\n                    continue;\n                }\n\n                sort(candidates_for_rect.begin(), candidates_for_rect.end());\n                auto [min_inc, rot_choice, d_choice, b_ref_choice, new_max_x, new_max_y, x0, y0] = candidates_for_rect[0];\n\n                if (min_inc <= threshold * (w_sim[idx] + h_sim[idx])) {\n                    placed[idx] = true;\n                    placed_list.push_back(idx);\n                    long long w_used = (rot_choice == 0) ? w_sim[idx] : h_sim[idx];\n                    long long h_used = (rot_choice == 0) ? h_sim[idx] : w_sim[idx];\n                    rect_pos[idx] = make_tuple(x0, y0, w_used, h_used);\n                    current_max_x = new_max_x;\n                    current_max_y = new_max_y;\n                    sequence.push_back(make_tuple(idx, rot_choice, d_choice, b_ref_choice));\n                }\n            }\n\n            long long sim_score = current_max_x + current_max_y;\n            for (int i = 0; i < N; i++) {\n                if (!placed[i]) {\n                    sim_score += w_sim[i] + h_sim[i];\n                }\n            }\n            candidates.push_back(make_tuple(sequence, sim_score));\n        }\n\n        sort(candidates.begin(), candidates.end(), [](const auto& a, const auto& b) {\n            return get<1>(a) < get<1>(b);\n        });\n    }\n\n    for (int turn = exploration_turns; turn < T; turn++) {\n        int cand_idx = turn - exploration_turns;\n        if (!candidates.empty()) {\n            int output_index = min(cand_idx, (int)candidates.size() - 1);\n            auto& seq = get<0>(candidates[output_index]);\n            cout << seq.size() << '\\n';\n            for (auto [p, r, d, b] : seq) {\n                cout << p << \" \" << r << \" \" << d << \" \" << b << '\\n';\n            }\n        } else {\n            cout << 0 << '\\n';\n        }\n        cout.flush();\n\n        long long Wp, Hp;\n        cin >> Wp >> Hp;\n    }\n\n    return 0;\n}","ahc041":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\n#include <utility>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, H;\n    cin >> N >> M >> H;\n\n    vector<int> A(N);\n    for (int i = 0; i < N; i++) {\n        cin >> A[i];\n    }\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    vector<int> x(N), y(N);\n    for (int i = 0; i < N; i++) {\n        cin >> x[i] >> y[i];\n    }\n\n    vector<int> depth(N, -1);\n    vector<int> parent(N, -1);\n    vector<bool> finalized(N, false);\n\n    auto cmp = greater<pair<int, int>>();\n    priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(cmp)> unassigned_pq(cmp);\n    priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(cmp)> bfs_pq(cmp);\n\n    for (int i = 0; i < N; i++) {\n        unassigned_pq.push({A[i], i});\n    }\n\n    int finalized_count = 0;\n\n    while (finalized_count < N) {\n        int v = -1;\n        if (!bfs_pq.empty()) {\n            while (!bfs_pq.empty() && v == -1) {\n                auto [a_val, node] = bfs_pq.top();\n                bfs_pq.pop();\n                if (!finalized[node]) {\n                    v = node;\n                }\n            }\n        }\n        if (v == -1) {\n            while (!unassigned_pq.empty() && v == -1) {\n                auto [a_val, node] = unassigned_pq.top();\n                unassigned_pq.pop();\n                if (!finalized[node]) {\n                    v = node;\n                }\n            }\n        }\n        if (v == -1) break;\n\n        finalized[v] = true;\n        finalized_count++;\n\n        if (depth[v] == -1) {\n            depth[v] = 0;\n            parent[v] = -1;\n        }\n\n        if (depth[v] < H) {\n            for (int w : adj[v]) {\n                if (finalized[w]) continue;\n                int new_depth = depth[v] + 1;\n                if (depth[w] == -1 || new_depth > depth[w]) {\n                    depth[w] = new_depth;\n                    parent[w] = v;\n                    bfs_pq.push({A[w], w});\n                }\n            }\n        }\n    }\n\n    for (int i = 0; i < N; i++) {\n        if (i > 0) cout << \" \";\n        cout << parent[i];\n    }\n    cout << endl;\n\n    return 0;\n}","ahc042":"#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\nint main() {\n    const int N = 20;\n    vector<string> board(N);\n    for (int i = 0; i < N; i++) {\n        cin >> board[i];\n    }\n\n    vector<pair<int, int>> onis;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (board[i][j] == 'x') {\n                onis.push_back({i, j});\n            }\n        }\n    }\n\n    vector<pair<char, int>> ans;\n\n    for (auto& oni : onis) {\n        int i = oni.first;\n        int j = oni.second;\n        vector<pair<int, char>> options;\n\n        bool valid = true;\n        for (int k = 0; k < i; k++) {\n            if (board[k][j] == 'o') {\n                valid = false;\n                break;\n            }\n        }\n        if (valid) {\n            options.push_back({2 * (i + 1), 'U'});\n        }\n\n        valid = true;\n        for (int k = i + 1; k < N; k++) {\n            if (board[k][j] == 'o') {\n                valid = false;\n                break;\n            }\n        }\n        if (valid) {\n            options.push_back({2 * (N - i), 'D'});\n        }\n\n        valid = true;\n        for (int k = 0; k < j; k++) {\n            if (board[i][k] == 'o') {\n                valid = false;\n                break;\n            }\n        }\n        if (valid) {\n            options.push_back({2 * (j + 1), 'L'});\n        }\n\n        valid = true;\n        for (int k = j + 1; k < N; k++) {\n            if (board[i][k] == 'o') {\n                valid = false;\n                break;\n            }\n        }\n        if (valid) {\n            options.push_back({2 * (N - j), 'R'});\n        }\n\n        if (options.empty()) {\n            vector<pair<int, char>> all_options = {\n                {2*(i+1), 'U'},\n                {2*(N-i), 'D'},\n                {2*(j+1), 'L'},\n                {2*(N-j), 'R'}\n            };\n            sort(all_options.begin(), all_options.end());\n            char chosen_dir = all_options[0].second;\n            if (chosen_dir == 'U') {\n                for (int k = 0; k <= i; k++) ans.push_back({'U', j});\n                for (int k = 0; k <= i; k++) ans.push_back({'D', j});\n            } else if (chosen_dir == 'D') {\n                for (int k = 0; k < N - i; k++) ans.push_back({'D', j});\n                for (int k = 0; k < N - i; k++) ans.push_back({'U', j});\n            } else if (chosen_dir == 'L') {\n                for (int k = 0; k <= j; k++) ans.push_back({'L', i});\n                for (int k = 0; k <= j; k++) ans.push_back({'R', i});\n            } else if (chosen_dir == 'R') {\n                for (int k = 0; k < N - j; k++) ans.push_back({'R', i});\n                for (int k = 0; k < N - j; k++) ans.push_back({'L', i});\n            }\n        } else {\n            sort(options.begin(), options.end());\n            char chosen_dir = options[0].second;\n            if (chosen_dir == 'U') {\n                for (int k = 0; k <= i; k++) ans.push_back({'U', j});\n                for (int k = 0; k <= i; k++) ans.push_back({'D', j});\n            } else if (chosen_dir == 'D') {\n                for (int k = 0; k < N - i; k++) ans.push_back({'D', j});\n                for (int k = 0; k < N - i; k++) ans.push_back({'U', j});\n            } else if (chosen_dir == 'L') {\n                for (int k = 0; k <= j; k++) ans.push_back({'L', i});\n                for (int k = 0; k <= j; k++) ans.push_back({'R', i});\n            } else if (chosen_dir == 'R') {\n                for (int k = 0; k < N - j; k++) ans.push_back({'R', i});\n                for (int k = 0; k < N - j; k++) ans.push_back({'L', i});\n            }\n        }\n    }\n\n    for (auto& move : ans) {\n        cout << move.first << \" \" << move.second << '\\n';\n    }\n\n    return 0;\n}","ahc044":"#include <iostream>\n#include <vector>\n#include <random>\n#include <cmath>\n#include <algorithm>\nusing namespace std;\n\nlong long simulate(const vector<int>& a, const vector<int>& b, const vector<int>& T, int L, int N) {\n    int total_count[100] = {0};\n    char parity[100] = {0};\n    int next_table[2][100];\n\n    for (int i = 0; i < N; i++) {\n        next_table[0][i] = b[i];\n        next_table[1][i] = a[i];\n    }\n\n    total_count[0] = 1;\n    parity[0] = 1;\n    int current = 0;\n\n    for (int week = 2; week <= L; week++) {\n        int p = parity[current];\n        int next_emp = next_table[p][current];\n        total_count[next_emp]++;\n        parity[next_emp] ^= 1;\n        current = next_emp;\n    }\n\n    long long error = 0;\n    for (int i = 0; i < N; i++) {\n        error += abs(total_count[i] - T[i]);\n    }\n    return error;\n}\n\nint main() {\n    int N, L;\n    cin >> N >> L;\n    vector<int> T(N);\n    for (int i = 0; i < N; i++) {\n        cin >> T[i];\n    }\n\n    random_device rd;\n    mt19937 rng(rd());\n    vector<int> a(N), b(N);\n    uniform_int_distribution<int> emp_dist(0, N-1);\n    for (int i = 0; i < N; i++) {\n        a[i] = emp_dist(rng);\n        b[i] = emp_dist(rng);\n    }\n\n    long long current_error = simulate(a, b, T, L, N);\n    vector<int> best_a = a;\n    vector<int> best_b = b;\n    long long best_error = current_error;\n\n    int iterations = 800;\n    double max_temp = 10000.0;\n    double min_temp = 1.0;\n    uniform_real_distribution<double> real_dist(0.0, 1.0);\n    uniform_int_distribution<int> bit_dist(0, 1);\n    uniform_int_distribution<int> count_dist(1, 10);\n\n    for (int iter = 0; iter < iterations; iter++) {\n        double temp = max_temp * pow(min_temp / max_temp, static_cast<double>(iter) / iterations);\n        vector<int> new_a = a;\n        vector<int> new_b = b;\n\n        int change_count = count_dist(rng) == 1 ? 2 : 1;\n        for (int c = 0; c < change_count; c++) {\n            int i = emp_dist(rng);\n            int which = bit_dist(rng);\n            int old_val;\n            int new_val;\n            if (which == 0) {\n                old_val = new_a[i];\n                do {\n                    new_val = emp_dist(rng);\n                } while (new_val == old_val);\n                new_a[i] = new_val;\n            } else {\n                old_val = new_b[i];\n                do {\n                    new_val = emp_dist(rng);\n                } while (new_val == old_val);\n                new_b[i] = new_val;\n            }\n        }\n\n        long long new_error = simulate(new_a, new_b, T, L, N);\n\n        if (new_error < current_error || \n            real_dist(rng) < exp((current_error - new_error) / temp)) {\n            a = new_a;\n            b = new_b;\n            current_error = new_error;\n            if (new_error < best_error) {\n                best_error = new_error;\n                best_a = a;\n                best_b = b;\n            }\n        }\n    }\n\n    for (int i = 0; i < N; i++) {\n        cout << best_a[i] << \" \" << best_b[i] << endl;\n    }\n\n    return 0;\n}","ahc045":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <cstdint>\nusing namespace std;\n\nuint32_t xy2d(uint32_t n, uint32_t x, uint32_t y) {\n    uint32_t rx, ry, s, d = 0;\n    for (s = n / 2; s > 0; s /= 2) {\n        rx = (x & s) > 0;\n        ry = (y & s) > 0;\n        d += s * s * ((3 * rx) ^ ry);\n        if (ry == 0) {\n            if (rx == 1) {\n                x = s - 1 - x;\n                y = s - 1 - y;\n            }\n            swap(x, y);\n        }\n    }\n    return d;\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, Q_max, L, W;\n    cin >> N >> M >> Q_max >> L >> W;\n    vector<int> G(M);\n    for (int i = 0; i < M; i++) {\n        cin >> G[i];\n    }\n\n    vector<int> lx(N), rx(N), ly(N), ry(N);\n    vector<int> cx(N), cy(N);\n    for (int i = 0; i < N; i++) {\n        cin >> lx[i] >> rx[i] >> ly[i] >> ry[i];\n        cx[i] = (lx[i] + rx[i]) / 2;\n        cy[i] = (ly[i] + ry[i]) / 2;\n    }\n\n    const uint32_t n_hilbert = 1 << 16;\n    const long long scale_val = (1LL << 16) - 1;\n    vector<uint32_t> hilbert_index(N);\n    for (int i = 0; i < N; i++) {\n        long long nx_val = static_cast<long long>(cx[i]) * scale_val;\n        long long ny_val = static_cast<long long>(cy[i]) * scale_val;\n        uint32_t nx = static_cast<uint32_t>(nx_val / 10000);\n        uint32_t ny = static_cast<uint32_t>(ny_val / 10000);\n        hilbert_index[i] = xy2d(n_hilbert, nx, ny);\n    }\n\n    vector<int> cities(N);\n    for (int i = 0; i < N; i++) cities[i] = i;\n    sort(cities.begin(), cities.end(), [&](int i, int j) {\n        return hilbert_index[i] < hilbert_index[j];\n    });\n\n    vector<vector<int>> groups(M);\n    int idx = 0;\n    for (int k = 0; k < M; k++) {\n        groups[k].resize(G[k]);\n        for (int j = 0; j < G[k]; j++) {\n            groups[k][j] = cities[idx++];\n        }\n    }\n\n    int query_count = 0;\n    vector<vector<pair<int, int>>> group_edges(M);\n\n    auto query_cluster = [](const vector<int>& cluster) {\n        vector<int> sorted_cluster = cluster;\n        sort(sorted_cluster.begin(), sorted_cluster.end());\n        cout << \"? \" << sorted_cluster.size();\n        for (int c : sorted_cluster) {\n            cout << \" \" << c;\n        }\n        cout << endl;\n        cout.flush();\n\n        vector<pair<int, int>> edges;\n        for (int i = 0; i < sorted_cluster.size() - 1; i++) {\n            int a, b;\n            cin >> a >> b;\n            edges.push_back({a, b});\n        }\n        return edges;\n    };\n\n    for (int k = 0; k < M; k++) {\n        vector<int>& group = groups[k];\n        int n = group.size();\n        if (n == 1) {\n            continue;\n        }\n\n        vector<pair<int, int>> edges;\n        int i = 0;\n        while (i < n && query_count < Q_max) {\n            int take = min(L, n - i);\n            if (take < 2) break;\n\n            vector<int> cluster;\n            for (int j = 0; j < take; j++) {\n                cluster.push_back(group[i + j]);\n            }\n\n            vector<pair<int, int>> e = query_cluster(cluster);\n            query_count++;\n            for (const auto& edge : e) {\n                edges.push_back(edge);\n            }\n\n            i += (take - 1);\n        }\n\n        if (i < n - 1) {\n            edges.push_back({group[i], group[i + 1]});\n            for (int j = i + 1; j < n - 1; j++) {\n                edges.push_back({group[j], group[j + 1]});\n            }\n        }\n\n        group_edges[k] = edges;\n    }\n\n    cout << \"!\" << endl;\n    for (int k = 0; k < M; k++) {\n        vector<int>& group = groups[k];\n        for (int j = 0; j < group.size(); j++) {\n            if (j > 0) cout << \" \";\n            cout << group[j];\n        }\n        cout << endl;\n\n        vector<pair<int, int>>& edges = group_edges[k];\n        for (const auto& edge : edges) {\n            cout << edge.first << \" \" << edge.second << endl;\n        }\n    }\n\n    return 0;\n}","ahc046":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int N, M;\n    cin >> N >> M;\n    vector<pair<int, int>> points;\n    for (int i = 0; i < M; i++) {\n        int a, b;\n        cin >> a >> b;\n        points.push_back({a, b});\n    }\n\n    vector<vector<bool>> blocks(N, vector<bool>(N, false));\n    map<char, pair<int, int>> dirs;\n    dirs['U'] = {-1, 0};\n    dirs['D'] = {1, 0};\n    dirs['L'] = {0, -1};\n    dirs['R'] = {0, 1};\n\n    auto is_blocked = [&](int i, int j) -> bool {\n        if (i < 0 || i >= N || j < 0 || j >= N)\n            return true;\n        return blocks[i][j];\n    };\n\n    vector<string> outputs;\n    int ci = points[0].first, cj = points[0].second;\n\n    for (int idx = 1; idx < M; idx++) {\n        int ti = points[idx].first, tj = points[idx].second;\n        while (ci != ti || cj != tj) {\n            int current_manhattan = abs(ci - ti) + abs(cj - tj);\n            double best_score = -1e9;\n            char best_act = ' ';\n            char best_dir = ' ';\n\n            for (char d : {'U', 'D', 'L', 'R'}) {\n                int dx = dirs[d].first, dy = dirs[d].second;\n                int ni = ci + dx;\n                int nj = cj + dy;\n\n                if (ni >= 0 && ni < N && nj >= 0 && nj < N) {\n                    if (!blocks[ni][nj]) {\n                        int new_manhattan = abs(ni - ti) + abs(nj - tj);\n                        int reduction = current_manhattan - new_manhattan;\n                        if (reduction > best_score) {\n                            best_score = reduction;\n                            best_act = 'M';\n                            best_dir = d;\n                        }\n\n                        int slide_steps = 1;\n                        int nni = ni, nnj = nj;\n                        while (true) {\n                            nni += dx;\n                            nnj += dy;\n                            if (nni < 0 || nni >= N || nnj < 0 || nnj >= N || blocks[nni][nnj]) {\n                                break;\n                            }\n                            slide_steps++;\n                        }\n                        int slide_i = ci + slide_steps * dx;\n                        int slide_j = cj + slide_steps * dy;\n                        int new_manhattan_slide = abs(slide_i - ti) + abs(slide_j - tj);\n                        int reduction_slide = current_manhattan - new_manhattan_slide;\n                        if (reduction_slide > best_score) {\n                            best_score = reduction_slide;\n                            best_act = 'S';\n                            best_dir = d;\n                        }\n                    } else {\n                        int new_manhattan = abs(ni - ti) + abs(nj - tj);\n                        int reduction = current_manhattan - new_manhattan;\n                        if (reduction > best_score) {\n                            best_score = reduction;\n                            best_act = 'M';\n                            best_dir = d;\n                        }\n\n                        int slide_steps = 1;\n                        int nni = ni, nnj = nj;\n                        while (true) {\n                            nni += dx;\n                            nnj += dy;\n                            if (nni < 0 || nni >= N || nnj < 0 || nnj >= N || blocks[nni][nnj]) {\n                                break;\n                            }\n                            slide_steps++;\n                        }\n                        int slide_i = ci + slide_steps * dx;\n                        int slide_j = cj + slide_steps * dy;\n                        int new_manhattan_slide = abs(slide_i - ti) + abs(slide_j - tj);\n                        int reduction_slide = current_manhattan - new_manhattan_slide;\n                        if (reduction_slide > best_score) {\n                            best_score = reduction_slide;\n                            best_act = 'S';\n                            best_dir = d;\n                        }\n                    }\n                }\n            }\n\n            if (best_score == -1e9) {\n                double best_alter_score = -1e9;\n                char best_alter_dir = 'U';\n                for (char d : {'U', 'D', 'L', 'R'}) {\n                    int dx = dirs[d].first, dy = dirs[d].second;\n                    int ni = ci + dx, nj = cj + dy;\n                    if (ni < 0 || ni >= N || nj < 0 || nj >= N) continue;\n                    int dist_block = abs(ni - ti) + abs(nj - tj);\n                    double alter_score = -dist_block;\n                    if (alter_score > best_alter_score) {\n                        best_alter_score = alter_score;\n                        best_alter_dir = d;\n                    }\n                }\n                best_act = 'A';\n                best_dir = best_alter_dir;\n            }\n\n            outputs.push_back(string(1, best_act) + \" \" + best_dir);\n            int dx = dirs[best_dir].first, dy = dirs[best_dir].second;\n            if (best_act == 'M') {\n                ci = ci + dx;\n                cj = cj + dy;\n            } else if (best_act == 'S') {\n                int steps = 0;\n                int ni = ci, nj = cj;\n                while (true) {\n                    ni += dx;\n                    nj += dy;\n                    if (ni < 0 || ni >= N || nj < 0 || nj >= N || blocks[ni][nj]) {\n                        ni -= dx;\n                        nj -= dy;\n                        break;\n                    }\n                    steps++;\n                }\n                ci = ni;\n                cj = nj;\n            } else if (best_act == 'A') {\n                int ni = ci + dx, nj = cj + dy;\n                blocks[ni][nj] = !blocks[ni][nj];\n            }\n        }\n    }\n\n    for (const string& s : outputs) {\n        cout << s << '\\n';\n    }\n\n    return 0;\n}"},"16":{"ahc001":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <cmath>\n#include <cstdlib>\n#include <iomanip>\nusing namespace std;\n\nstruct Rect {\n    int a, b, c, d;\n    long long area;\n    long long r_val;\n};\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int n;\n    cin >> n;\n    vector<Rect> rects;\n    for (int i = 0; i < n; i++) {\n        int x, y, r;\n        cin >> x >> y >> r;\n        rects.push_back({x, y, x+1, y+1, 1LL, (long long)r});\n    }\n\n    bool changed = true;\n    int pass_count = 0;\n    const int MAX_PASS = 100;\n\n    while (changed && pass_count < MAX_PASS) {\n        pass_count++;\n        changed = false;\n        vector<int> indices(n);\n        for (int i = 0; i < n; i++) {\n            indices[i] = i;\n        }\n\n        sort(indices.begin(), indices.end(), [&](int i, int j) {\n            double sat_i = (double)rects[i].area / rects[i].r_val;\n            double sat_j = (double)rects[j].area / rects[j].r_val;\n            return sat_i < sat_j;\n        });\n\n        for (int idx = 0; idx < n; idx++) {\n            int i = indices[idx];\n            if (rects[i].area >= rects[i].r_val) \n                continue;\n\n            double best_improvement = -1e9;\n            int best_dir = -1;\n            int best_d_use = 0;\n\n            for (int dir = 0; dir < 4; dir++) {\n                int d_max;\n                if (dir == 0) {\n                    d_max = rects[i].a;\n                } else if (dir == 1) {\n                    d_max = 10000 - rects[i].c;\n                } else if (dir == 2) {\n                    d_max = rects[i].b;\n                } else {\n                    d_max = 10000 - rects[i].d;\n                }\n\n                if (d_max <= 0) continue;\n\n                int side_length;\n                if (dir < 2) {\n                    side_length = rects[i].d - rects[i].b;\n                } else {\n                    side_length = rects[i].c - rects[i].a;\n                }\n                if (side_length <= 0) continue;\n\n                for (int j = 0; j < n; j++) {\n                    if (i == j) continue;\n                    if (dir == 0) {\n                        if (rects[i].b < rects[j].d && rects[j].b < rects[i].d) {\n                            if (rects[j].c <= rects[i].a) {\n                                d_max = min(d_max, rects[i].a - rects[j].c);\n                            }\n                        }\n                    } else if (dir == 1) {\n                        if (rects[i].b < rects[j].d && rects[j].b < rects[i].d) {\n                            if (rects[j].a >= rects[i].c) {\n                                d_max = min(d_max, rects[j].a - rects[i].c);\n                            }\n                        }\n                    } else if (dir == 2) {\n                        if (rects[i].a < rects[j].c && rects[j].a < rects[i].c) {\n                            if (rects[j].d <= rects[i].b) {\n                                d_max = min(d_max, rects[i].b - rects[j].d);\n                            }\n                        }\n                    } else if (dir == 3) {\n                        if (rects[i].a < rects[j].c && rects[j].a < rects[i].c) {\n                            if (rects[j].b >= rects[i].d) {\n                                d_max = min(d_max, rects[j].b - rects[i].d);\n                            }\n                        }\n                    }\n                }\n\n                if (d_max <= 0) continue;\n\n                double d0 = (double)(rects[i].r_val - rects[i].area) / side_length;\n                if (d0 < 1e-5) continue;\n\n                vector<int> cands;\n                int d1 = (int)floor(d0);\n                int d2 = (int)ceil(d0);\n                d1 = min(d_max, d1);\n                d2 = min(d_max, d2);\n                if (d1 > 0) cands.push_back(d1);\n                if (d2 > 0 && d2 != d1) cands.push_back(d2);\n\n                double best_improve_dir = -1e9;\n                int best_d_use_dir = -1;\n\n                for (int d_candidate : cands) {\n                    long long new_area = rects[i].area + (long long)d_candidate * side_length;\n                    double min_val = min(rects[i].r_val, rects[i].area);\n                    double max_val = max(rects[i].r_val, rects[i].area);\n                    double old_sat = 1.0 - pow(1.0 - min_val / max_val, 2);\n\n                    min_val = min(rects[i].r_val, new_area);\n                    max_val = max(rects[i].r_val, new_area);\n                    double new_sat = 1.0 - pow(1.0 - min_val / max_val, 2);\n\n                    double improvement = new_sat - old_sat;\n                    if (improvement > best_improve_dir) {\n                        best_improve_dir = improvement;\n                        best_d_use_dir = d_candidate;\n                    }\n                }\n\n                if (best_improve_dir > best_improvement) {\n                    best_improvement = best_improve_dir;\n                    best_dir = dir;\n                    best_d_use = best_d_use_dir;\n                }\n            }\n\n            if (best_improvement > 1e-8) {\n                if (best_dir == 0) {\n                    rects[i].a -= best_d_use;\n                    rects[i].area += (long long)best_d_use * (rects[i].d - rects[i].b);\n                } else if (best_dir == 1) {\n                    rects[i].c += best_d_use;\n                    rects[i].area += (long long)best_d_use * (rects[i].d - rects[i].b);\n                } else if (best_dir == 2) {\n                    rects[i].b -= best_d_use;\n                    rects[i].area += (long long)best_d_use * (rects[i].c - rects[i].a);\n                } else if (best_dir == 3) {\n                    rects[i].d += best_d_use;\n                    rects[i].area += (long long)best_d_use * (rects[i].c - rects[i].a);\n                }\n                changed = true;\n            }\n        }\n    }\n\n    for (int i = 0; i < n; i++) {\n        cout << rects[i].a << \" \" << rects[i].b << \" \" << rects[i].c << \" \" << rects[i].d << \"\\n\";\n    }\n\n    return 0;\n}","ahc002":"#include <iostream>\n#include <vector>\n#include <set>\n#include <algorithm>\n#include <tuple>\nusing namespace std;\n\nint main() {\n    int si, sj;\n    cin >> si >> sj;\n    \n    vector<vector<int>> t(50, vector<int>(50));\n    vector<vector<int>> p(50, vector<int>(50));\n    \n    for (int i = 0; i < 50; i++) {\n        for (int j = 0; j < 50; j++) {\n            cin >> t[i][j];\n        }\n    }\n    for (int i = 0; i < 50; i++) {\n        for (int j = 0; j < 50; j++) {\n            cin >> p[i][j];\n        }\n    }\n    \n    int M = 0;\n    for (int i = 0; i < 50; i++) {\n        for (int j = 0; j < 50; j++) {\n            M = max(M, t[i][j] + 1);\n        }\n    }\n    \n    vector<set<int>> adj_tiles(M);\n    const int dx[4] = {1, -1, 0, 0};\n    const int dy[4] = {0, 0, 1, -1};\n    \n    for (int i = 0; i < 50; i++) {\n        for (int j = 0; j < 50; j++) {\n            for (int d = 0; d < 4; d++) {\n                int ni = i + dx[d];\n                int nj = j + dy[d];\n                if (ni >= 0 && ni < 50 && nj >= 0 && nj < 50 && t[i][j] != t[ni][nj]) {\n                    adj_tiles[t[i][j]].insert(t[ni][nj]);\n                }\n            }\n        }\n    }\n    \n    vector<int> current_degree(M);\n    for (int i = 0; i < M; i++) {\n        current_degree[i] = adj_tiles[i].size();\n    }\n    \n    vector<bool> visited_tile(M, false);\n    int cx = si, cy = sj;\n    int start_tile = t[si][sj];\n    visited_tile[start_tile] = true;\n    \n    for (int adj : adj_tiles[start_tile]) {\n        if (!visited_tile[adj]) {\n            current_degree[adj]--;\n        }\n    }\n    \n    string path = \"\";\n    const char dir_char[4] = {'D', 'U', 'R', 'L'};\n    \n    while (true) {\n        vector<tuple<int, char, int, int>> candidates;\n        for (int d1 = 0; d1 < 4; d1++) {\n            int nx1 = cx + dx[d1];\n            int ny1 = cy + dy[d1];\n            if (nx1 < 0 || nx1 >= 50 || ny1 < 0 || ny1 >= 50) continue;\n            int tile1 = t[nx1][ny1];\n            if (visited_tile[tile1]) continue;\n            \n            int score1 = p[nx1][ny1];\n            int base_score = score1 + 120 * current_degree[tile1];\n            int best_next = 0;\n            \n            for (int d2 = 0; d2 < 4; d2++) {\n                int nx2 = nx1 + dx[d2];\n                int ny2 = ny1 + dy[d2];\n                if (nx2 < 0 || nx2 >= 50 || ny2 < 0 || ny2 >= 50) continue;\n                int tile2 = t[nx2][ny2];\n                if (tile2 == tile1 || visited_tile[tile2]) continue;\n                \n                int deg2 = max(0, current_degree[tile2] - 1);\n                int score2 = p[nx2][ny2] + 100 * deg2;\n                int best_third = 0;\n                \n                for (int d3 = 0; d3 < 4; d3++) {\n                    int nx3 = nx2 + dx[d3];\n                    int ny3 = ny2 + dy[d3];\n                    if (nx3 < 0 || nx3 >= 50 || ny3 < 0 || ny3 >= 50) continue;\n                    int tile3 = t[nx3][ny3];\n                    if (tile3 == tile1 || tile3 == tile2 || visited_tile[tile3]) continue;\n                    \n                    int deg3 = max(0, current_degree[tile3] - 1);\n                    int score3 = p[nx3][ny3] + 60 * deg3;\n                    if (score3 > best_third) {\n                        best_third = score3;\n                    }\n                }\n                \n                int candidate_score = score2 + best_third;\n                if (candidate_score > best_next) {\n                    best_next = candidate_score;\n                }\n            }\n            \n            int total_score = base_score + best_next;\n            candidates.push_back(make_tuple(total_score, dir_char[d1], nx1, ny1));\n        }\n        \n        if (candidates.empty()) break;\n        \n        auto best = *max_element(candidates.begin(), candidates.end(),\n            [](const auto& a, const auto& b) { return get<0>(a) < get<0>(b); });\n        \n        char dir = get<1>(best);\n        int nx1 = get<2>(best);\n        int ny1 = get<3>(best);\n        int tile1 = t[nx1][ny1];\n        \n        path += dir;\n        cx = nx1;\n        cy = ny1;\n        visited_tile[tile1] = true;\n        \n        for (int adj : adj_tiles[tile1]) {\n            if (!visited_tile[adj]) {\n                current_degree[adj]--;\n            }\n        }\n    }\n    \n    cout << path << endl;\n    return 0;\n}","ahc003":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\n#include <tuple>\n#include <cmath>\n\nusing namespace std;\n\nconst int N = 30;\nconst double INF = 1e18;\nconst double EXPLORE_CONST = 500.0;\n\nvector<vector<double>> h_est(N, vector<double>(N-1, 5000.0));\nvector<vector<double>> v_est(N-1, vector<double>(N, 5000.0));\nvector<vector<int>> count_h(N, vector<int>(N-1, 0));\nvector<vector<int>> count_v(N-1, vector<int>(N, 0));\n\nconst int dx[4] = {0, 1, 0, -1};\nconst int dy[4] = {1, 0, -1, 0};\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    for (int k = 0; k < 1000; k++) {\n        int si, sj, ti, tj;\n        cin >> si >> sj >> ti >> tj;\n\n        vector<vector<double>> dist(N, vector<double>(N, INF));\n        vector<vector<pair<int, int>>> prev(N, vector<pair<int, int>>(N, {-1, -1}));\n        priority_queue<tuple<double, int, int>, vector<tuple<double, int, int>>, greater<tuple<double, int, int>>> pq;\n\n        dist[si][sj] = 0;\n        pq.push({0, si, sj});\n\n        while (!pq.empty()) {\n            auto [d, i, j] = pq.top();\n            pq.pop();\n            if (d > dist[i][j]) continue;\n            if (i == ti && j == tj) break;\n\n            for (int dir = 0; dir < 4; dir++) {\n                int ni = i + dx[dir];\n                int nj = j + dy[dir];\n                if (ni < 0 || ni >= N || nj < 0 || nj >= N) continue;\n\n                double base_weight, bonus, weight;\n                if (dir == 0) {\n                    base_weight = h_est[i][j];\n                    bonus = EXPLORE_CONST * sqrt(log(k+2) / (count_h[i][j] + 1));\n                    weight = max(1000.0, base_weight - bonus);\n                } else if (dir == 1) {\n                    base_weight = v_est[i][j];\n                    bonus = EXPLORE_CONST * sqrt(log(k+2) / (count_v[i][j] + 1));\n                    weight = max(1000.0, base_weight - bonus);\n                } else if (dir == 2) {\n                    base_weight = h_est[i][j-1];\n                    bonus = EXPLORE_CONST * sqrt(log(k+2) / (count_h[i][j-1] + 1));\n                    weight = max(1000.0, base_weight - bonus);\n                } else {\n                    base_weight = v_est[i-1][j];\n                    bonus = EXPLORE_CONST * sqrt(log(k+2) / (count_v[i-1][j] + 1));\n                    weight = max(1000.0, base_weight - bonus);\n                }\n\n                double new_dist = dist[i][j] + weight;\n                if (new_dist < dist[ni][nj]) {\n                    dist[ni][nj] = new_dist;\n                    prev[ni][nj] = {i, j};\n                    pq.push({new_dist, ni, nj});\n                }\n            }\n        }\n\n        vector<char> moves;\n        int ci = ti, cj = tj;\n        while (ci != si || cj != sj) {\n            auto [pi, pj] = prev[ci][cj];\n            if (pi == ci) moves.push_back(pj < cj ? 'R' : 'L');\n            else moves.push_back(pi < ci ? 'D' : 'U');\n            ci = pi;\n            cj = pj;\n        }\n        reverse(moves.begin(), moves.end());\n        cout << string(moves.begin(), moves.end()) << endl;\n        cout.flush();\n\n        long long z_k;\n        cin >> z_k;\n\n        double estimated_sum = dist[ti][tj];\n        double relative_error = (z_k - estimated_sum) / estimated_sum;\n        int ci2 = si, cj2 = sj;\n        \n        for (char move : moves) {\n            double* w_est = nullptr;\n            int* cnt = nullptr;\n            if (move == 'R') {\n                w_est = &h_est[ci2][cj2];\n                cnt = &count_h[ci2][cj2];\n                cj2++;\n            } else if (move == 'D') {\n                w_est = &v_est[ci2][cj2];\n                cnt = &count_v[ci2][cj2];\n                ci2++;\n            } else if (move == 'L') {\n                w_est = &h_est[ci2][cj2-1];\n                cnt = &count_h[ci2][cj2-1];\n                cj2--;\n            } else {\n                w_est = &v_est[ci2-1][cj2];\n                cnt = &count_v[ci2-1][cj2];\n                ci2--;\n            }\n            \n            double alpha = 1.0 / sqrt(1 + *cnt);\n            double new_est = *w_est * (1.0 + alpha * relative_error);\n            *w_est = max(1000.0, min(9000.0, new_est));\n            (*cnt)++;\n        }\n    }\n\n    return 0;\n}","ahc004":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <string>\n#include <cmath>\n#include <random>\n#include <tuple>\n#include <climits>\nusing namespace std;\n\nbool is_subsequence(vector<string> &mat, string s) {\n    int len = s.length();\n    int n = mat.size();\n    for (int dir = 0; dir < 2; dir++) {\n        for (int i = 0; i < n; i++) {\n            for (int j = 0; j < n; j++) {\n                bool match = true;\n                for (int p = 0; p < len; p++) {\n                    int ni = i, nj = j;\n                    if (dir == 0) {\n                        nj = (j + p) % n;\n                        if (nj < 0) nj += n;\n                    } else {\n                        ni = (i + p) % n;\n                        if (ni < 0) ni += n;\n                    }\n                    if (mat[ni][nj] != s[p]) {\n                        match = false;\n                        break;\n                    }\n                }\n                if (match) {\n                    return true;\n                }\n            }\n        }\n    }\n    return false;\n}\n\nvector<bool> compute_covered(vector<string> &mat, vector<string> &strings) {\n    int M = strings.size();\n    vector<bool> covered(M, false);\n    for (int idx = 0; idx < M; idx++) {\n        covered[idx] = is_subsequence(mat, strings[idx]);\n    }\n    return covered;\n}\n\nint main() {\n    mt19937 rng(time(0));\n    int N, M;\n    cin >> N >> M;\n    vector<string> strings(M);\n    for (int i = 0; i < M; i++) {\n        cin >> strings[i];\n    }\n\n    auto build_greedy = [&](bool desc) {\n        vector<string> mat(N, string(N, '.'));\n        vector<string> sorted = strings;\n        if (desc) {\n            sort(sorted.begin(), sorted.end(), [](const string& a, const string& b) {\n                return a.size() > b.size();\n            });\n        } else {\n            sort(sorted.begin(), sorted.end(), [](const string& a, const string& b) {\n                return a.size() < b.size();\n            });\n        }\n        for (const string& s : sorted) {\n            int len = s.size();\n            for (int dir = 0; dir < 2; dir++) {\n                for (int i = 0; i < N; i++) {\n                    for (int j = 0; j < N; j++) {\n                        bool ok = true;\n                        for (int p = 0; p < len; p++) {\n                            int ni = i, nj = j;\n                            if (dir == 0) {\n                                nj = (j + p) % N;\n                                if (nj < 0) nj += N;\n                            } else {\n                                ni = (i + p) % N;\n                                if (ni < 0) ni += N;\n                            }\n                            if (mat[ni][nj] != '.' && mat[ni][nj] != s[p]) {\n                                ok = false;\n                                break;\n                            }\n                        }\n                        if (ok) {\n                            for (int p = 0; p < len; p++) {\n                                int ni = i, nj = j;\n                                if (dir == 0) {\n                                    nj = (j + p) % N;\n                                    if (nj < 0) nj += N;\n                                } else {\n                                    ni = (i + p) % N;\n                                    if (ni < 0) ni += N;\n                                }\n                                if (mat[ni][nj] == '.') {\n                                    mat[ni][nj] = s[p];\n                                }\n                            }\n                            goto next_string;\n                        }\n                    }\n                }\n            }\n            next_string: ;\n        }\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                if (mat[i][j] == '.') {\n                    mat[i][j] = 'A';\n                }\n            }\n        }\n        return mat;\n    };\n\n    vector<string> mat1 = build_greedy(true);\n    vector<bool> covered1 = compute_covered(mat1, strings);\n    int c1 = 0;\n    for (bool b : covered1) c1 += b;\n\n    vector<string> mat2 = build_greedy(false);\n    vector<bool> covered2 = compute_covered(mat2, strings);\n    int c2 = 0;\n    for (bool b : covered2) c2 += b;\n\n    vector<string> current;\n    vector<bool> current_covered;\n    int current_cov;\n    if (c1 >= c2) {\n        current = mat1;\n        current_covered = covered1;\n        current_cov = c1;\n    } else {\n        current = mat2;\n        current_covered = covered2;\n        current_cov = c2;\n    }\n\n    vector<string> best_mat = current;\n    int best_cov = current_cov;\n\n    if (best_cov == M) {\n        for (int i = 0; i < N; i++) {\n            cout << best_mat[i] << endl;\n        }\n        return 0;\n    }\n\n    vector<int> uncovered_indices;\n    for (int i = 0; i < M; i++) {\n        if (!current_covered[i]) {\n            uncovered_indices.push_back(i);\n        }\n    }\n\n    vector<vector<vector<tuple<int, int, int, int, int>>>> cell_to_candidates(N, vector<vector<tuple<int, int, int, int, int>>>(N));\n    vector<vector<vector<vector<int>>>> mismatch_count(M, vector<vector<vector<int>>>(N, vector<vector<int>>(N, vector<int>(2, 0))));\n    vector<int> valid_count(M, 0);\n    int total_covered = 0;\n\n    for (int si = 0; si < M; si++) {\n        string s = strings[si];\n        int len = s.length();\n        for (int i0 = 0; i0 < N; i0++) {\n            for (int j0 = 0; j0 < N; j0++) {\n                for (int d = 0; d < 2; d++) {\n                    int cnt = 0;\n                    for (int p = 0; p < len; p++) {\n                        int ni = i0, nj = j0;\n                        if (d == 0) {\n                            nj = (j0 + p) % N;\n                            if (nj < 0) nj += N;\n                        } else {\n                            ni = (i0 + p) % N;\n                            if (ni < 0) ni += N;\n                        }\n                        if (current[ni][nj] != s[p]) {\n                            cnt++;\n                        }\n                        cell_to_candidates[ni][nj].push_back(make_tuple(si, i0, j0, d, p));\n                    }\n                    mismatch_count[si][i0][j0][d] = cnt;\n                    if (cnt == 0) {\n                        valid_count[si]++;\n                    }\n                }\n            }\n        }\n        if (valid_count[si] > 0) {\n            total_covered++;\n        }\n    }\n\n    current_cov = total_covered;\n    if (current_cov > best_cov) {\n        best_cov = current_cov;\n        best_mat = current;\n    }\n\n    const int MAX_ITER = 2000;\n    double T = 10.0;\n    const double cooling_rate = 0.998;\n    uniform_int_distribution<int> rand_idx(0, N-1);\n    uniform_int_distribution<int> rand_char(0, 7);\n    uniform_real_distribution<double> rand_prob(0.0, 1.0);\n    uniform_int_distribution<int> rand_percent(0, 99);\n\n    for (int iter = 0; iter < MAX_ITER; iter++) {\n        vector<string> candidate_mat = current;\n        int i, j;\n        char old_char;\n        char new_char;\n\n        vector<int> uncovered;\n        for (int si = 0; si < M; si++) {\n            if (valid_count[si] == 0) {\n                uncovered.push_back(si);\n            }\n        }\n\n        bool use_random = true;\n        if (!uncovered.empty() && rand_percent(rng) < 90) {\n            uniform_int_distribution<int> rand_uncovered_idx(0, uncovered.size()-1);\n            int uidx = uncovered[rand_uncovered_idx(rng)];\n            int si = uidx;\n\n            int min_mismatch = INT_MAX;\n            vector<tuple<int, int, int>> best_candidates;\n            for (int i0 = 0; i0 < N; i0++) {\n                for (int j0 = 0; j0 < N; j0++) {\n                    for (int d = 0; d < 2; d++) {\n                        int mc = mismatch_count[si][i0][j0][d];\n                        if (mc > 0) {\n                            if (mc < min_mismatch) {\n                                min_mismatch = mc;\n                                best_candidates.clear();\n                                best_candidates.push_back(make_tuple(i0, j0, d));\n                            } else if (mc == min_mismatch) {\n                                best_candidates.push_back(make_tuple(i0, j0, d));\n                            }\n                        }\n                    }\n                }\n            }\n\n            if (!best_candidates.empty()) {\n                uniform_int_distribution<int> rand_best(0, best_candidates.size()-1);\n                auto cand = best_candidates[rand_best(rng)];\n                int i0 = get<0>(cand);\n                int j0 = get<1>(cand);\n                int d = get<2>(cand);\n                string s = strings[si];\n                int len = s.length();\n                vector<tuple<int, int, char>> mismatches;\n                for (int p = 0; p < len; p++) {\n                    int ni = i0, nj = j0;\n                    if (d == 0) {\n                        nj = (j0 + p) % N;\n                        if (nj < 0) nj += N;\n                    } else {\n                        ni = (i0 + p) % N;\n                        if (ni < 0) ni += N;\n                    }\n                    if (current[ni][nj] != s[p]) {\n                        mismatches.push_back(make_tuple(ni, nj, s[p]));\n                    }\n                }\n                if (!mismatches.empty()) {\n                    uniform_int_distribution<int> rand_mismatch(0, mismatches.size()-1);\n                    auto mis = mismatches[rand_mismatch(rng)];\n                    i = get<0>(mis);\n                    j = get<1>(mis);\n                    new_char = get<2>(mis);\n                    old_char = current[i][j];\n                    if (old_char != new_char) {\n                        use_random = false;\n                    }\n                }\n            }\n        }\n\n        if (use_random) {\n            i = rand_idx(rng);\n            j = rand_idx(rng);\n            old_char = current[i][j];\n            do {\n                new_char = 'A' + rand_char(rng);\n            } while (new_char == old_char);\n        }\n\n        candidate_mat[i][j] = new_char;\n        int old_current_cov = current_cov;\n        vector<int> old_valid_count = valid_count;\n        vector<tuple<int, int, int, int, int>> backup_mismatches;\n\n        for (auto &cand : cell_to_candidates[i][j]) {\n            int si = get<0>(cand);\n            int i0 = get<1>(cand);\n            int j0 = get<2>(cand);\n            int d = get<3>(cand);\n            int p = get<4>(cand);\n            char s_char = strings[si][p];\n            int old_mismatch = mismatch_count[si][i0][j0][d];\n            backup_mismatches.push_back(make_tuple(si, i0, j0, d, old_mismatch));\n            bool was_valid = (old_mismatch == 0);\n\n            if (old_char == s_char) {\n                if (new_char != s_char) {\n                    mismatch_count[si][i0][j0][d]++;\n                }\n            } else {\n                if (new_char == s_char) {\n                    mismatch_count[si][i0][j0][d]--;\n                }\n            }\n\n            int new_mismatch = mismatch_count[si][i0][j0][d];\n            bool now_valid = (new_mismatch == 0);\n\n            if (was_valid && !now_valid) {\n                valid_count[si]--;\n                if (valid_count[si] == 0) {\n                    current_cov--;\n                }\n            } else if (!was_valid && now_valid) {\n                valid_count[si]++;\n                if (valid_count[si] == 1) {\n                    current_cov++;\n                }\n            }\n        }\n\n        double delta = current_cov - old_current_cov;\n        bool accept_move = false;\n        if (delta > 0) {\n            accept_move = true;\n        } else if (delta < 0) {\n            double prob = exp(delta / T);\n            double r = rand_prob(rng);\n            accept_move = (r < prob);\n        } else {\n            accept_move = (rand_percent(rng) % 2 == 0);\n        }\n\n        if (accept_move) {\n            current = candidate_mat;\n            if (current_cov > best_cov) {\n                best_cov = current_cov;\n                best_mat = current;\n                if (best_cov == M) {\n                    break;\n                }\n            }\n        } else {\n            current_cov = old_current_cov;\n            valid_count = old_valid_count;\n            for (auto &tup : backup_mismatches) {\n                int si = get<0>(tup);\n                int i0 = get<1>(tup);\n                int j0 = get<2>(tup);\n                int d = get<3>(tup);\n                int old_mismatch = get<4>(tup);\n                mismatch_count[si][i0][j0][d] = old_mismatch;\n            }\n        }\n        T *= cooling_rate;\n    }\n\n    for (int i = 0; i < N; i++) {\n        cout << best_mat[i] << endl;\n    }\n    return 0;\n}","ahc005":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int di[] = {-1, 1, 0, 0};\nconst int dj[] = {0, 0, -1, 1};\nconst char dir_char[] = {'U', 'D', 'L', 'R'};\nconst int INF = 1e9;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, si, sj;\n    cin >> N >> si >> sj;\n    vector<string> grid;\n    for (int i = 0; i < N; i++) {\n        string s;\n        cin >> s;\n        grid.push_back(s);\n    }\n\n    vector<vector<int>> h_segment_id(N, vector<int>(N, -1));\n    vector<vector<int>> v_segment_id(N, vector<int>(N, -1));\n    vector<vector<pair<int, int>>> horizontal_segments;\n    vector<vector<pair<int, int>>> vertical_segments;\n\n    for (int i = 0; i < N; i++) {\n        int j = 0;\n        while (j < N) {\n            if (grid[i][j] == '#') {\n                j++;\n                continue;\n            }\n            vector<pair<int, int>> seg;\n            while (j < N && grid[i][j] != '#') {\n                seg.push_back({i, j});\n                j++;\n            }\n            int seg_id = horizontal_segments.size();\n            horizontal_segments.push_back(seg);\n            for (const auto& p : seg) {\n                h_segment_id[p.first][p.second] = seg_id;\n            }\n        }\n    }\n\n    for (int j = 0; j < N; j++) {\n        int i = 0;\n        while (i < N) {\n            if (grid[i][j] == '#') {\n                i++;\n                continue;\n            }\n            vector<pair<int, int>> seg;\n            while (i < N && grid[i][j] != '#') {\n                seg.push_back({i, j});\n                i++;\n            }\n            int seg_id = vertical_segments.size();\n            vertical_segments.push_back(seg);\n            for (const auto& p : seg) {\n                v_segment_id[p.first][p.second] = seg_id;\n            }\n        }\n    }\n\n    int totalH = horizontal_segments.size();\n    int totalV = vertical_segments.size();\n    vector<bool> coveredH(totalH, false);\n    vector<bool> coveredV(totalV, false);\n    int cur_i = si, cur_j = sj;\n    string route = \"\";\n\n    auto coverCell = [&](int i, int j) {\n        int h_id = h_segment_id[i][j];\n        int v_id = v_segment_id[i][j];\n        if (h_id != -1) coveredH[h_id] = true;\n        if (v_id != -1) coveredV[v_id] = true;\n    };\n\n    coverCell(si, sj);\n\n    while (true) {\n        bool allCovered = true;\n        for (bool b : coveredH) if (!b) { allCovered = false; break; }\n        if (allCovered) {\n            for (bool b : coveredV) if (!b) { allCovered = false; break; }\n            if (allCovered) break;\n        }\n\n        vector<vector<int>> dist(N, vector<int>(N, INF));\n        vector<vector<pair<int, int>>> prev(N, vector<pair<int, int>>(N, {-1, -1}));\n        priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<tuple<int, int, int>>> pq;\n        dist[cur_i][cur_j] = 0;\n        pq.push({0, cur_i, cur_j});\n\n        while (!pq.empty()) {\n            auto [cost, i, j] = pq.top();\n            pq.pop();\n            if (cost != dist[i][j]) continue;\n            for (int d = 0; d < 4; d++) {\n                int ni = i + di[d];\n                int nj = j + dj[d];\n                if (ni < 0 || ni >= N || nj < 0 || nj >= N || grid[ni][nj] == '#') continue;\n                int weight = grid[ni][nj] - '0';\n                int new_cost = cost + weight;\n                if (new_cost < dist[ni][nj]) {\n                    dist[ni][nj] = new_cost;\n                    prev[ni][nj] = {i, j};\n                    pq.push({new_cost, ni, nj});\n                }\n            }\n        }\n\n        int min_dist = INF;\n        int target_i = -1, target_j = -1;\n        int target_seg_id = -1;\n        bool is_target_horizontal = false;\n\n        for (int idx = 0; idx < totalH; idx++) {\n            if (coveredH[idx]) continue;\n            for (const auto& cell : horizontal_segments[idx]) {\n                int i = cell.first, j = cell.second;\n                if (dist[i][j] < min_dist) {\n                    min_dist = dist[i][j];\n                    target_i = i;\n                    target_j = j;\n                    target_seg_id = idx;\n                    is_target_horizontal = true;\n                }\n            }\n        }\n\n        for (int idx = 0; idx < totalV; idx++) {\n            if (coveredV[idx]) continue;\n            for (const auto& cell : vertical_segments[idx]) {\n                int i = cell.first, j = cell.second;\n                if (dist[i][j] < min_dist) {\n                    min_dist = dist[i][j];\n                    target_i = i;\n                    target_j = j;\n                    target_seg_id = idx;\n                    is_target_horizontal = false;\n                }\n            }\n        }\n\n        if (min_dist == INF) break;\n\n        vector<pair<int, int>> path_cells;\n        int ci = target_i, cj = target_j;\n        while (ci != cur_i || cj != cur_j) {\n            path_cells.push_back({ci, cj});\n            auto p = prev[ci][cj];\n            ci = p.first;\n            cj = p.second;\n        }\n        reverse(path_cells.begin(), path_cells.end());\n\n        int now_i = cur_i, now_j = cur_j;\n        string path_str = \"\";\n        bool covered_target_segment = false;\n        for (const auto& cell : path_cells) {\n            int ni = cell.first, nj = cell.second;\n            char move;\n            if (ni == now_i + 1) move = 'D';\n            else if (ni == now_i - 1) move = 'U';\n            else if (nj == now_j + 1) move = 'R';\n            else if (nj == now_j - 1) move = 'L';\n            else continue;\n            \n            path_str += move;\n            now_i = ni;\n            now_j = nj;\n            coverCell(ni, nj);\n            \n            if (is_target_horizontal) {\n                if (h_segment_id[ni][nj] == target_seg_id) {\n                    covered_target_segment = true;\n                    break;\n                }\n            } else {\n                if (v_segment_id[ni][nj] == target_seg_id) {\n                    covered_target_segment = true;\n                    break;\n                }\n            }\n        }\n        route += path_str;\n        cur_i = now_i;\n        cur_j = now_j;\n    }\n\n    vector<vector<int>> dist_return(N, vector<int>(N, INF));\n    vector<vector<pair<int, int>>> prev_return(N, vector<pair<int, int>>(N, {-1, -1}));\n    priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<tuple<int, int, int>>> pq_return;\n    dist_return[cur_i][cur_j] = 0;\n    pq_return.push({0, cur_i, cur_j});\n    while (!pq_return.empty()) {\n        auto [cost, i, j] = pq_return.top();\n        pq_return.pop();\n        if (cost != dist_return[i][j]) continue;\n        if (i == si && j == sj) break;\n        for (int d = 0; d < 4; d++) {\n            int ni = i + di[d];\n            int nj = j + dj[d];\n            if (ni < 0 || ni >= N || nj < 0 || nj >= N || grid[ni][nj] == '#') continue;\n            int weight = grid[ni][nj] - '0';\n            int new_cost = cost + weight;\n            if (new_cost < dist_return[ni][nj]) {\n                dist_return[ni][nj] = new_cost;\n                prev_return[ni][nj] = {i, j};\n                pq_return.push({new_cost, ni, nj});\n            }\n        }\n    }\n\n    vector<pair<int, int>> return_path;\n    int ci = si, cj = sj;\n    while (ci != cur_i || cj != cur_j) {\n        return_path.push_back({ci, cj});\n        auto p = prev_return[ci][cj];\n        ci = p.first;\n        cj = p.second;\n    }\n    reverse(return_path.begin(), return_path.end());\n    int now_i = cur_i, now_j = cur_j;\n    string return_str = \"\";\n    for (const auto& cell : return_path) {\n        int ni = cell.first, nj = cell.second;\n        if (ni == now_i + 1) return_str += 'D';\n        else if (ni == now_i - 1) return_str += 'U';\n        else if (nj == now_j + 1) return_str += 'R';\n        else if (nj == now_j - 1) return_str += 'L';\n        now_i = ni;\n        now_j = nj;\n    }\n    route += return_str;\n\n    cout << route << endl;\n\n    return 0;\n}","future-contest-2022-qual":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <set>\n#include <algorithm>\n#include <cmath>\n#include <iomanip>\n#include <cctype>\n#include <cstdio>\n#include <cstring>\n#include <map>\n#include <unordered_map>\n#include <unordered_set>\n#include <stack>\n#include <deque>\n#include <string>\n#include <climits>\n#include <cfloat>\n#include <bitset>\nusing namespace std;\n\ndouble expected_time(double w) {\n    if (w < 0.5) return 1.0;\n    else if (w < 1.5) return 13.0/7.0;\n    else if (w < 2.5) return 17.0/7.0;\n    else if (w < 3.5) return 22.0/7.0;\n    else return w;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, K, R;\n    cin >> N >> M >> K >> R;\n\n    vector<vector<int>> task_diff(N, vector<int>(K));\n    for (int i = 0; i < N; i++) {\n        for (int k = 0; k < K; k++) {\n            cin >> task_diff[i][k];\n        }\n    }\n\n    vector<vector<int>> children(N+1);\n    vector<vector<int>> parents(N+1);\n    vector<int> in_degree(N+1, 0);\n    for (int i = 0; i < R; i++) {\n        int u, v;\n        cin >> u >> v;\n        children[u].push_back(v);\n        parents[v].push_back(u);\n        in_degree[v]++;\n    }\n\n    vector<int> num_desc(N+1, 0);\n    for (int i = 1; i <= N; i++) {\n        vector<bool> visited(N+1, false);\n        queue<int> q;\n        q.push(i);\n        visited[i] = true;\n        int count = 0;\n        while (!q.empty()) {\n            int u = q.front(); q.pop();\n            count++;\n            for (int v : children[u]) {\n                if (!visited[v]) {\n                    visited[v] = true;\n                    q.push(v);\n                }\n            }\n        }\n        num_desc[i] = count;\n    }\n\n    vector<bool> completed(N+1, false);\n    vector<bool> assigned(N+1, false);\n    vector<int> task_start_day(N+1, -1);\n    vector<int> member_status(M+1, -1);\n    vector<vector<double>> s_hat(M+1, vector<double>(K, 10.0));\n    vector<vector<int>> min_skill_lower_bound(M+1, vector<int>(K, 0));\n    vector<vector<double>> momentum(M+1, vector<double>(K, 0.0));\n    set<int> available_set;\n    for (int i = 1; i <= N; i++) {\n        if (in_degree[i] == 0) {\n            available_set.insert(i);\n        }\n    }\n\n    vector<vector<double>> time_est_full(N+1, vector<double>(M+1, 0.0));\n    vector<double> base_time(N+1, 0.0);\n    vector<int> best_member(N+1, -1);\n    vector<int> second_best_member(N+1, -1);\n    vector<double> second_best_time(N+1, 1e18);\n\n    for (int i = 1; i <= N; i++) {\n        if (completed[i]) {\n            base_time[i] = 0.0;\n            best_member[i] = -1;\n            second_best_member[i] = -1;\n            second_best_time[i] = 1e18;\n        } else {\n            base_time[i] = 1e18;\n            second_best_time[i] = 1e18;\n            best_member[i] = -1;\n            second_best_member[i] = -1;\n            \n            for (int j = 1; j <= M; j++) {\n                double w_val = 0.0;\n                for (int k = 0; k < K; k++) {\n                    if (task_diff[i-1][k] > s_hat[j][k]) {\n                        w_val += task_diff[i-1][k] - s_hat[j][k];\n                    }\n                }\n                time_est_full[i][j] = expected_time(w_val);\n                \n                if (time_est_full[i][j] < base_time[i]) {\n                    second_best_time[i] = base_time[i];\n                    second_best_member[i] = best_member[i];\n                    base_time[i] = time_est_full[i][j];\n                    best_member[i] = j;\n                } else if (time_est_full[i][j] < second_best_time[i]) {\n                    second_best_time[i] = time_est_full[i][j];\n                    second_best_member[i] = j;\n                }\n            }\n        }\n    }\n\n    int day = 0;\n    while (true) {\n        day++;\n        if (day > 1) {\n            int n_finished;\n            cin >> n_finished;\n            if (n_finished == -1) {\n                break;\n            }\n            vector<int> finished_members(n_finished);\n            for (int i = 0; i < n_finished; i++) {\n                cin >> finished_members[i];\n            }\n            for (int j : finished_members) {\n                int task_i = member_status[j];\n                completed[task_i] = true;\n                assigned[task_i] = false;\n                member_status[j] = -1;\n                base_time[task_i] = 0.0;\n                best_member[task_i] = -1;\n                second_best_member[task_i] = -1;\n                second_best_time[task_i] = 1e18;\n                \n                for (int child : children[task_i]) {\n                    in_degree[child]--;\n                    if (in_degree[child] == 0 && !completed[child] && !assigned[child]) {\n                        available_set.insert(child);\n                    }\n                }\n                \n                int start_day = task_start_day[task_i];\n                int t_days = day - start_day;\n                vector<int>& d_vec = task_diff[task_i-1];\n                \n                if (t_days == 1) {\n                    for (int k = 0; k < K; k++) {\n                        if (d_vec[k] > min_skill_lower_bound[j][k]) {\n                            min_skill_lower_bound[j][k] = d_vec[k];\n                        }\n                        if (s_hat[j][k] < min_skill_lower_bound[j][k]) {\n                            s_hat[j][k] = min_skill_lower_bound[j][k];\n                        }\n                    }\n                } else {\n                    double w_hat = 0.0;\n                    for (int k = 0; k < K; k++) {\n                        if (d_vec[k] > s_hat[j][k]) {\n                            w_hat += (d_vec[k] - s_hat[j][k]);\n                        }\n                    }\n                    double w_obs = t_days;\n                    double e = w_obs - w_hat;\n                    double learning_rate = 0.1;\n                    double momentum_factor = 0.8;\n                    for (int k = 0; k < K; k++) {\n                        if (d_vec[k] > s_hat[j][k]) {\n                            double update = -learning_rate * e + momentum_factor * momentum[j][k];\n                            s_hat[j][k] += update;\n                            momentum[j][k] = update;\n                            if (s_hat[j][k] < min_skill_lower_bound[j][k]) {\n                                s_hat[j][k] = min_skill_lower_bound[j][k];\n                            }\n                        } else {\n                            if (s_hat[j][k] < min_skill_lower_bound[j][k]) {\n                                s_hat[j][k] = min_skill_lower_bound[j][k];\n                            }\n                        }\n                    }\n                }\n                \n                for (int i = 1; i <= N; i++) {\n                    if (completed[i]) continue;\n                    \n                    double old_val = time_est_full[i][j];\n                    double w_val = 0.0;\n                    for (int k = 0; k < K; k++) {\n                        if (task_diff[i-1][k] > s_hat[j][k]) {\n                            w_val += task_diff[i-1][k] - s_hat[j][k];\n                        }\n                    }\n                    double new_val = expected_time(w_val);\n                    time_est_full[i][j] = new_val;\n                    \n                    if (j == best_member[i]) {\n                        if (new_val <= base_time[i]) {\n                            base_time[i] = new_val;\n                        } else {\n                            if (new_val <= second_best_time[i]) {\n                                base_time[i] = new_val;\n                                swap(best_member[i], second_best_member[i]);\n                                swap(base_time[i], second_best_time[i]);\n                            } else {\n                                base_time[i] = second_best_time[i];\n                                best_member[i] = second_best_member[i];\n                                second_best_time[i] = 1e18;\n                                second_best_member[i] = -1;\n                                for (int j2 = 1; j2 <= M; j2++) {\n                                    if (j2 == best_member[i]) continue;\n                                    if (time_est_full[i][j2] < second_best_time[i]) {\n                                        second_best_time[i] = time_est_full[i][j2];\n                                        second_best_member[i] = j2;\n                                    }\n                                }\n                            }\n                        }\n                    } else if (j == second_best_member[i]) {\n                        if (new_val < second_best_time[i]) {\n                            second_best_time[i] = new_val;\n                            if (second_best_time[i] < base_time[i]) {\n                                swap(best_member[i], second_best_member[i]);\n                                swap(base_time[i], second_best_time[i]);\n                            }\n                        } else {\n                            second_best_time[i] = 1e18;\n                            second_best_member[i] = -1;\n                            for (int j2 = 1; j2 <= M; j2++) {\n                                if (j2 == best_member[i]) continue;\n                                if (time_est_full[i][j2] < second_best_time[i]) {\n                                    second_best_time[i] = time_est_full[i][j2];\n                                    second_best_member[i] = j2;\n                                }\n                            }\n                        }\n                    } else {\n                        if (new_val < base_time[i]) {\n                            second_best_time[i] = base_time[i];\n                            second_best_member[i] = best_member[i];\n                            base_time[i] = new_val;\n                            best_member[i] = j;\n                        } else if (new_val < second_best_time[i]) {\n                            second_best_time[i] = new_val;\n                            second_best_member[i] = j;\n                        }\n                    }\n                }\n            }\n        }\n\n        vector<int> free_members;\n        for (int j = 1; j <= M; j++) {\n            if (member_status[j] == -1) {\n                free_members.push_back(j);\n            }\n        }\n\n        vector<double> earliest_start(N+1, 0.0);\n        vector<int> in_degree_temp = in_degree;\n        queue<int> q_forward;\n        for (int i = 1; i <= N; i++) {\n            if (in_degree_temp[i] == 0) {\n                q_forward.push(i);\n            }\n        }\n        while (!q_forward.empty()) {\n            int u = q_forward.front();\n            q_forward.pop();\n            for (int v : children[u]) {\n                if (earliest_start[v] < earliest_start[u] + base_time[u]) {\n                    earliest_start[v] = earliest_start[u] + base_time[u];\n                }\n                in_degree_temp[v]--;\n                if (in_degree_temp[v] == 0) {\n                    q_forward.push(v);\n                }\n            }\n        }\n\n        double total_project_time = 0.0;\n        for (int i = 1; i <= N; i++) {\n            total_project_time = max(total_project_time, earliest_start[i] + base_time[i]);\n        }\n\n        vector<double> latest_finish(N+1, total_project_time);\n        vector<double> latest_start(N+1, 0.0);\n        vector<int> out_degree(N+1, 0);\n        for (int i = 1; i <= N; i++) {\n            out_degree[i] = children[i].size();\n        }\n        vector<int> out_degree_temp = out_degree;\n        queue<int> q_backward;\n        for (int i = 1; i <= N; i++) {\n            if (out_degree_temp[i] == 0) {\n                q_backward.push(i);\n            }\n        }\n        while (!q_backward.empty()) {\n            int u = q_backward.front();\n            q_backward.pop();\n            latest_start[u] = latest_finish[u] - base_time[u];\n            for (int p : parents[u]) {\n                if (latest_finish[p] > latest_start[u]) {\n                    latest_finish[p] = latest_start[u];\n                }\n                out_degree_temp[p]--;\n                if (out_degree_temp[p] == 0) {\n                    q_backward.push(p);\n                }\n            }\n        }\n\n        vector<double> slack(N+1, 0.0);\n        for (int i = 1; i <= N; i++) {\n            slack[i] = latest_start[i] - earliest_start[i];\n        }\n\n        if (free_members.empty() || available_set.empty()) {\n            cout << 0 << endl;\n            for (int j = 1; j <= M; j++) {\n                cout << \"#s \" << j;\n                for (int k = 0; k < K; k++) {\n                    cout << \" \" << (int)round(s_hat[j][k]);\n                }\n                cout << endl;\n            }\n            cout.flush();\n            continue;\n        }\n\n        vector<int> available_tasks_list(available_set.begin(), available_set.end());\n        int n_tasks = available_tasks_list.size();\n        int n_free = free_members.size();\n        vector<vector<double>> time_est(n_tasks, vector<double>(n_free, 0.0));\n        for (int i = 0; i < n_tasks; i++) {\n            int task_id = available_tasks_list[i];\n            for (int j = 0; j < n_free; j++) {\n                int member_id = free_members[j];\n                time_est[i][j] = time_est_full[task_id][member_id];\n            }\n        }\n\n        vector<tuple<double, double, int>> task_priority;\n        for (int i = 0; i < n_tasks; i++) {\n            int task_id = available_tasks_list[i];\n            double min_time = 1e18;\n            for (int j = 0; j < n_free; j++) {\n                if (time_est[i][j] < min_time) {\n                    min_time = time_est[i][j];\n                }\n            }\n            double priority_val;\n            if (slack[task_id] < 0) {\n                priority_val = 1e18;\n            } else {\n                priority_val = (double)num_desc[task_id] / (slack[task_id] + 1.0);\n            }\n            task_priority.push_back({-slack[task_id], -min_time, i});\n        }\n        sort(task_priority.rbegin(), task_priority.rend());\n\n        vector<bool> free_flag(n_free, true);\n        vector<pair<int, int>> assignments;\n        for (auto& tpl : task_priority) {\n            int idx_task = get<2>(tpl);\n            int task_id = available_tasks_list[idx_task];\n            double best_time = 1e18;\n            int best_j_index = -1;\n            for (int j = 0; j < n_free; j++) {\n                if (free_flag[j] && time_est[idx_task][j] < best_time) {\n                    best_time = time_est[idx_task][j];\n                    best_j_index = j;\n                }\n            }\n            if (best_j_index != -1) {\n                int member_id = free_members[best_j_index];\n                assignments.push_back({member_id, task_id});\n                free_flag[best_j_index] = false;\n                assigned[task_id] = true;\n                available_set.erase(task_id);\n                member_status[member_id] = task_id;\n                task_start_day[task_id] = day;\n            }\n        }\n\n        cout << assignments.size();\n        for (auto& a : assignments) {\n            cout << \" \" << a.first << \" \" << a.second;\n        }\n        cout << endl;\n        for (int j = 1; j <= M; j++) {\n            cout << \"#s \" << j;\n            for (int k = 0; k < K; k++) {\n                cout << \" \" << (int)round(s_hat[j][k]);\n            }\n            cout << endl;\n        }\n        cout.flush();\n    }\n\n    return 0;\n}","ahc006":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <cmath>\n#include <cstdlib>\nusing namespace std;\n\nstruct Point {\n    int x, y;\n    Point(int x=0, int y=0) : x(x), y(y) {}\n};\n\nint manhattan(const Point& a, const Point& b) {\n    return abs(a.x - b.x) + abs(a.y - b.y);\n}\n\nstruct Order {\n    int a, b, c, d;\n};\n\nint main() {\n    vector<Order> orders(1000);\n    for (int i = 0; i < 1000; i++) {\n        cin >> orders[i].a >> orders[i].b >> orders[i].c >> orders[i].d;\n    }\n\n    vector<Point> tour;\n    tour.push_back(Point(400, 400));\n    tour.push_back(Point(400, 400));\n\n    vector<int> point_to_order;\n    vector<int> point_type;\n    point_to_order.push_back(-1);\n    point_to_order.push_back(-1);\n    point_type.push_back(0);\n    point_type.push_back(0);\n\n    vector<int> rest_index(1000, -1);\n    vector<int> dest_index(1000, -1);\n    vector<bool> used(1000, false);\n    vector<int> chosen_ids;\n\n    int n = 2;\n    for (int step = 0; step < 50; step++) {\n        long long best_cost = (long long)1e18;\n        int best_order = -1;\n        int best_edge = -1;\n\n        for (int idx = 0; idx < 1000; idx++) {\n            if (used[idx]) continue;\n            Order& o = orders[idx];\n            Point R = {o.a, o.b};\n            Point D = {o.c, o.d};\n            for (int edge = 0; edge < n-1; edge++) {\n                Point A = tour[edge];\n                Point B = tour[edge+1];\n                long long cost_insert = (long long)manhattan(A, R) + manhattan(R, D) + manhattan(D, B) - manhattan(A, B);\n                if (cost_insert < best_cost) {\n                    best_cost = cost_insert;\n                    best_order = idx;\n                    best_edge = edge;\n                }\n            }\n        }\n\n        if (best_order == -1) {\n            break;\n        }\n\n        Order& o = orders[best_order];\n        Point R = {o.a, o.b};\n        Point D = {o.c, o.d};\n        tour.insert(tour.begin() + best_edge + 1, R);\n        tour.insert(tour.begin() + best_edge + 2, D);\n\n        point_to_order.insert(point_to_order.begin() + best_edge + 1, best_order);\n        point_to_order.insert(point_to_order.begin() + best_edge + 2, best_order);\n        point_type.insert(point_type.begin() + best_edge + 1, 1);\n        point_type.insert(point_type.begin() + best_edge + 2, 2);\n\n        rest_index[best_order] = best_edge + 1;\n        dest_index[best_order] = best_edge + 2;\n        used[best_order] = true;\n        chosen_ids.push_back(best_order + 1);\n\n        n += 2;\n    }\n\n    n = tour.size();\n    bool improved = true;\n    while (improved) {\n        improved = false;\n        for (int i = 0; i <= n - 4; i++) {\n            int j = i + 1;\n            int k = i + 2;\n            int id1 = point_to_order[j];\n            int id2 = point_to_order[k];\n            if (id1 == id2 && id1 != -1) {\n                continue;\n            }\n\n            Point A = tour[i];\n            Point B = tour[j];\n            Point C = tour[k];\n            Point D = tour[i + 3];\n            long long original_cost = (long long)manhattan(A, B) + manhattan(B, C) + manhattan(C, D);\n            long long new_cost = (long long)manhattan(A, C) + manhattan(C, B) + manhattan(B, D);\n            long long delta = new_cost - original_cost;\n            if (delta >= 0) {\n                continue;\n            }\n\n            bool valid = true;\n            if (id1 != -1) {\n                int r1 = rest_index[id1];\n                int d1 = dest_index[id1];\n                if (j == r1) {\n                    r1 = k;\n                } else if (j == d1) {\n                    d1 = k;\n                }\n                if (r1 >= d1) {\n                    valid = false;\n                }\n            }\n            if (valid && id2 != -1) {\n                int r2 = rest_index[id2];\n                int d2 = dest_index[id2];\n                if (k == r2) {\n                    r2 = j;\n                } else if (k == d2) {\n                    d2 = j;\n                }\n                if (r2 >= d2) {\n                    valid = false;\n                }\n            }\n\n            if (valid) {\n                swap(tour[j], tour[k]);\n                swap(point_to_order[j], point_to_order[k]);\n                swap(point_type[j], point_type[k]);\n\n                if (id1 != -1) {\n                    if (rest_index[id1] == j) {\n                        rest_index[id1] = k;\n                    } else if (dest_index[id1] == j) {\n                        dest_index[id1] = k;\n                    }\n                }\n                if (id2 != -1) {\n                    if (rest_index[id2] == k) {\n                        rest_index[id2] = j;\n                    } else if (dest_index[id2] == k) {\n                        dest_index[id2] = j;\n                    }\n                }\n                improved = true;\n            }\n        }\n    }\n\n    cout << \"50\";\n    for (int i = 0; i < 50; i++) {\n        cout << \" \" << chosen_ids[i];\n    }\n    cout << endl;\n    cout << tour.size();\n    for (int i = 0; i < tour.size(); i++) {\n        cout << \" \" << tour[i].x << \" \" << tour[i].y;\n    }\n    cout << endl;\n\n    return 0;\n}","ahc007":"#include <bits/stdc++.h>\n#include <cmath>\n#include <vector>\n#include <tuple>\n#include <algorithm>\n#include <atcoder/dsu>\n\nusing namespace std;\nusing namespace atcoder;\n\nint main() {\n    const int N = 400;\n    const int M = 1995;\n    vector<double> x(N), y(N);\n    for (int i = 0; i < N; i++) {\n        cin >> x[i] >> y[i];\n    }\n\n    vector<pair<int, int>> edges_list(M);\n    for (int i = 0; i < M; i++) {\n        cin >> edges_list[i].first >> edges_list[i].second;\n    }\n\n    vector<double> d_list(M);\n    for (int i = 0; i < M; i++) {\n        int u = edges_list[i].first;\n        int v = edges_list[i].second;\n        double dx = x[u] - x[v];\n        double dy = y[u] - y[v];\n        d_list[i] = round(sqrt(dx*dx + dy*dy));\n    }\n\n    vector<tuple<double, int, int, int>> edges_for_mst;\n    for (int i = 0; i < M; i++) {\n        edges_for_mst.push_back(make_tuple(d_list[i], edges_list[i].first, edges_list[i].second, i));\n    }\n    sort(edges_for_mst.begin(), edges_for_mst.end());\n\n    dsu dsu_mst(N);\n    vector<bool> in_mst_low(M, false);\n    for (auto &e : edges_for_mst) {\n        double cost = get<0>(e);\n        int u = get<1>(e);\n        int v = get<2>(e);\n        int idx = get<3>(e);\n        if (dsu_mst.same(u, v)) continue;\n        dsu_mst.merge(u, v);\n        in_mst_low[idx] = true;\n    }\n\n    dsu dsu_online(N);\n    int comp_count = N;\n\n    for (int i = 0; i < M; i++) {\n        int l_i;\n        cin >> l_i;\n\n        int u = edges_list[i].first;\n        int v = edges_list[i].second;\n\n        if (dsu_online.same(u, v)) {\n            cout << 0 << endl;\n            cout.flush();\n        } else {\n            dsu temp = dsu_online;\n            int temp_comp = comp_count;\n            for (int j = i+1; j < M; j++) {\n                int uj = edges_list[j].first;\n                int vj = edges_list[j].second;\n                if (!temp.same(uj, vj)) {\n                    temp.merge(uj, vj);\n                    temp_comp--;\n                }\n            }\n            if (temp_comp != 1) {\n                cout << 1 << endl;\n                cout.flush();\n                dsu_online.merge(u, v);\n                comp_count--;\n            } else {\n                double progress = (double) i / M;\n                double ratio = (comp_count - 1.0) / (M - i - 1);\n                double threshold_mst = 1.05 + 0.4 * progress + 1.8 * ratio;\n                double threshold_non_mst = 0.8 + 0.6 * progress + 2.0 * ratio;\n\n                if (in_mst_low[i]) {\n                    if (l_i <= threshold_mst * d_list[i]) {\n                        cout << 1 << endl;\n                        cout.flush();\n                        dsu_online.merge(u, v);\n                        comp_count--;\n                    } else {\n                        cout << 0 << endl;\n                        cout.flush();\n                    }\n                } else {\n                    if (l_i <= threshold_non_mst * d_list[i]) {\n                        cout << 1 << endl;\n                        cout.flush();\n                        dsu_online.merge(u, v);\n                        comp_count--;\n                    } else {\n                        cout << 0 << endl;\n                        cout.flush();\n                    }\n                }\n            }\n        }\n    }\n\n    return 0;\n}","ahc008":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\n#include <set>\n#include <tuple>\n#include <climits>\nusing namespace std;\n\nconst int GRID_SIZE = 32;\n\nstruct Pet {\n    int x, y, type;\n};\n\nstruct Human {\n    int x, y;\n};\n\nstruct BarrierTask {\n    int wall_x, wall_y;\n    int hx, hy;\n    char action;\n};\n\nint manhattan(int x1, int y1, int x2, int y2) {\n    return abs(x1 - x2) + abs(y1 - y2);\n}\n\nint main() {\n    int N;\n    cin >> N;\n    vector<Pet> pets(N);\n    for (int i = 0; i < N; i++) {\n        cin >> pets[i].x >> pets[i].y >> pets[i].type;\n    }\n    int M;\n    cin >> M;\n    vector<Human> humans(M);\n    for (int i = 0; i < M; i++) {\n        cin >> humans[i].x >> humans[i].y;\n    }\n\n    bool wall[GRID_SIZE][GRID_SIZE] = {false};\n    for (int i = 0; i < GRID_SIZE; i++) {\n        for (int j = 0; j < GRID_SIZE; j++) {\n            if (i == 0 || i == GRID_SIZE-1 || j == 0 || j == GRID_SIZE-1) {\n                wall[i][j] = true;\n            }\n        }\n    }\n\n    vector<BarrierTask> barrier_tasks;\n    for (int y = 2; y <= 14; y++) {\n        barrier_tasks.push_back({2, y, 3, y, 'u'});\n    }\n    for (int y = 16; y <= 29; y++) {\n        barrier_tasks.push_back({2, y, 3, y, 'u'});\n    }\n    for (int x = 3; x <= 14; x++) {\n        barrier_tasks.push_back({x, 29, x, 28, 'r'});\n    }\n    for (int x = 16; x <= 28; x++) {\n        barrier_tasks.push_back({x, 29, x, 28, 'r'});\n    }\n    for (int y = 29; y >= 16; y--) {\n        barrier_tasks.push_back({29, y, 28, y, 'd'});\n    }\n    for (int y = 14; y >= 2; y--) {\n        barrier_tasks.push_back({29, y, 28, y, 'd'});\n    }\n    for (int x = 28; x >= 16; x--) {\n        barrier_tasks.push_back({x, 2, x, 3, 'l'});\n    }\n    for (int x = 14; x >= 3; x--) {\n        barrier_tasks.push_back({x, 2, x, 3, 'l'});\n    }\n\n    vector<BarrierTask> gap_tasks = {\n        {2, 15, 3, 15, 'u'},\n        {15, 29, 15, 28, 'r'},\n        {29, 15, 28, 15, 'd'},\n        {15, 2, 15, 3, 'l'}\n    };\n    vector<bool> gap_built(4, false);\n    vector<bool> gap_assigned(4, false);\n    vector<int> human_gap_index(M, -1);\n\n    int total_tasks = barrier_tasks.size();\n    int segment_size = total_tasks / M;\n    int remainder = total_tasks % M;\n    vector<queue<BarrierTask>> tasks(M);\n    vector<vector<BarrierTask>> best_assignment;\n    int best_start_index = 0;\n    int min_max_dist = INT_MAX;\n\n    for (int start = 0; start < total_tasks; start++) {\n        vector<vector<BarrierTask>> temp_tasks(M);\n        int current = start;\n        for (int i = 0; i < M; i++) {\n            int num_tasks = segment_size + (i < remainder ? 1 : 0);\n            for (int j = 0; j < num_tasks; j++) {\n                temp_tasks[i].push_back(barrier_tasks[current]);\n                current = (current + 1) % total_tasks;\n            }\n        }\n\n        int max_dist = 0;\n        for (int i = 0; i < M; i++) {\n            if (!temp_tasks[i].empty()) {\n                BarrierTask& first_task = temp_tasks[i][0];\n                int d = manhattan(humans[i].x, humans[i].y, first_task.hx, first_task.hy);\n                if (d > max_dist) max_dist = d;\n            }\n        }\n\n        if (max_dist < min_max_dist) {\n            min_max_dist = max_dist;\n            best_start_index = start;\n            best_assignment = temp_tasks;\n        }\n    }\n\n    for (int i = 0; i < M; i++) {\n        for (BarrierTask task : best_assignment[i]) {\n            tasks[i].push(task);\n        }\n    }\n\n    int barrier_built_count = 0;\n\n    for (int turn = 0; turn < 300; turn++) {\n        bool no_pet_in_inner = true;\n        for (const auto& pet : pets) {\n            if (pet.x >= 3 && pet.x <= 28 && pet.y >= 3 && pet.y <= 28) {\n                no_pet_in_inner = false;\n                break;\n            }\n        }\n\n        vector<bool> available(M, false);\n        for (int i = 0; i < M; i++) {\n            if (tasks[i].empty() && human_gap_index[i] == -1) {\n                available[i] = true;\n            }\n        }\n\n        if (turn >= 200) {\n            for (int g = 0; g < 4; g++) {\n                if (gap_built[g] || gap_assigned[g]) continue;\n                int best_human = -1;\n                int best_dist = INT_MAX;\n                for (int i = 0; i < M; i++) {\n                    if (!available[i]) continue;\n                    int d = manhattan(humans[i].x, humans[i].y, gap_tasks[g].hx, gap_tasks[g].hy);\n                    if (d < best_dist) {\n                        best_dist = d;\n                        best_human = i;\n                    }\n                }\n                if (best_human != -1) {\n                    human_gap_index[best_human] = g;\n                    gap_assigned[g] = true;\n                    available[best_human] = false;\n                }\n            }\n        }\n\n        set<pair<int, int>> planned_walls;\n        vector<string> actions(M, \".\");\n\n        for (int i = 0; i < M; i++) {\n            if (human_gap_index[i] != -1) {\n                BarrierTask& gap_task = gap_tasks[human_gap_index[i]];\n                if (wall[gap_task.wall_x][gap_task.wall_y]) {\n                    human_gap_index[i] = -1;\n                    continue;\n                }\n                if (humans[i].x == gap_task.hx && humans[i].y == gap_task.hy) {\n                    bool can_build = true;\n                    for (const auto& pet : pets) {\n                        if (pet.x == gap_task.wall_x && pet.y == gap_task.wall_y) {\n                            can_build = false;\n                            break;\n                        }\n                    }\n                    if (can_build) {\n                        for (int j = 0; j < M; j++) {\n                            if (humans[j].x == gap_task.wall_x && humans[j].y == gap_task.wall_y) {\n                                can_build = false;\n                                break;\n                            }\n                        }\n                    }\n                    if (can_build) {\n                        int dx[4] = {0, 0, 1, -1};\n                        int dy[4] = {1, -1, 0, 0};\n                        for (int d = 0; d < 4; d++) {\n                            int nx = gap_task.wall_x + dx[d];\n                            int ny = gap_task.wall_y + dy[d];\n                            if (nx < 1 || nx > 30 || ny < 1 || ny > 30) continue;\n                            for (const auto& pet : pets) {\n                                if (pet.x == nx && pet.y == ny) {\n                                    can_build = false;\n                                    break;\n                                }\n                            }\n                            if (!can_build) break;\n                        }\n                    }\n                    if (can_build) {\n                        actions[i] = string(1, gap_task.action);\n                        planned_walls.insert({gap_task.wall_x, gap_task.wall_y});\n                    }\n                } else {\n                    actions[i] = \"?\";\n                }\n            } else if (!tasks[i].empty()) {\n                BarrierTask task = tasks[i].front();\n                if (wall[task.wall_x][task.wall_y]) {\n                    tasks[i].pop();\n                    continue;\n                }\n                if (humans[i].x == task.hx && humans[i].y == task.hy) {\n                    bool can_build = true;\n                    for (const auto& pet : pets) {\n                        if (pet.x == task.wall_x && pet.y == task.wall_y) {\n                            can_build = false;\n                            break;\n                        }\n                    }\n                    if (can_build) {\n                        for (int j = 0; j < M; j++) {\n                            if (humans[j].x == task.wall_x && humans[j].y == task.wall_y) {\n                                can_build = false;\n                                break;\n                            }\n                        }\n                    }\n                    if (can_build) {\n                        int dx[4] = {0, 0, 1, -1};\n                        int dy[4] = {1, -1, 0, 0};\n                        for (int d = 0; d < 4; d++) {\n                            int nx = task.wall_x + dx[d];\n                            int ny = task.wall_y + dy[d];\n                            if (nx < 1 || nx > 30 || ny < 1 || ny > 30) continue;\n                            for (const auto& pet : pets) {\n                                if (pet.x == nx && pet.y == ny) {\n                                    can_build = false;\n                                    break;\n                                }\n                            }\n                            if (!can_build) break;\n                        }\n                    }\n                    if (can_build) {\n                        actions[i] = string(1, task.action);\n                        planned_walls.insert({task.wall_x, task.wall_y});\n                    }\n                } else {\n                    actions[i] = \"?\";\n                }\n            }\n        }\n\n        for (int i = 0; i < M; i++) {\n            if (actions[i] == \"?\") {\n                int target_x, target_y;\n                if (human_gap_index[i] != -1) {\n                    BarrierTask& task = gap_tasks[human_gap_index[i]];\n                    target_x = task.hx;\n                    target_y = task.hy;\n                } else if (!tasks[i].empty()) {\n                    BarrierTask& task = tasks[i].front();\n                    target_x = task.hx;\n                    target_y = task.hy;\n                } else {\n                    actions[i] = '.';\n                    continue;\n                }\n\n                int sx = humans[i].x;\n                int sy = humans[i].y;\n                vector<vector<bool>> visited(GRID_SIZE, vector<bool>(GRID_SIZE, false));\n                vector<vector<char>> first_step_dir(GRID_SIZE, vector<char>(GRID_SIZE, '.'));\n                queue<pair<int, int>> q;\n                q.push({sx, sy});\n                visited[sx][sy] = true;\n                bool found = false;\n\n                while (!q.empty()) {\n                    auto [x, y] = q.front(); q.pop();\n                    if (x == target_x && y == target_y) {\n                        found = true;\n                        break;\n                    }\n                    vector<tuple<int, int, char>> dirs = {{-1,0,'U'}, {1,0,'D'}, {0,-1,'L'}, {0,1,'R'}};\n                    for (auto [dx, dy, dir] : dirs) {\n                        int nx = x + dx;\n                        int ny = y + dy;\n                        if (nx < 1 || nx > 30 || ny < 1 || ny > 30) continue;\n                        if (visited[nx][ny]) continue;\n                        if (wall[nx][ny] || planned_walls.count({nx, ny})) {\n                            continue;\n                        }\n                        visited[nx][ny] = true;\n                        if (x == sx && y == sy) {\n                            first_step_dir[nx][ny] = dir;\n                        } else {\n                            first_step_dir[nx][ny] = first_step_dir[x][y];\n                        }\n                        q.push({nx, ny});\n                    }\n                }\n\n                if (found) {\n                    actions[i] = string(1, first_step_dir[target_x][target_y]);\n                } else {\n                    actions[i] = '.';\n                }\n            }\n        }\n\n        string output_line;\n        for (int i = 0; i < M; i++) {\n            output_line += actions[i];\n        }\n        cout << output_line << endl;\n        cout.flush();\n\n        for (int i = 0; i < M; i++) {\n            if (actions[i] == \"u\" || actions[i] == \"d\" || actions[i] == \"l\" || actions[i] == \"r\") {\n                if (human_gap_index[i] != -1) {\n                    int g = human_gap_index[i];\n                    wall[gap_tasks[g].wall_x][gap_tasks[g].wall_y] = true;\n                    gap_built[g] = true;\n                    gap_assigned[g] = false;\n                    human_gap_index[i] = -1;\n                } else if (!tasks[i].empty()) {\n                    BarrierTask task = tasks[i].front();\n                    wall[task.wall_x][task.wall_y] = true;\n                    barrier_built_count++;\n                    tasks[i].pop();\n                }\n            } else if (actions[i] == \"U\") {\n                humans[i].x--;\n            } else if (actions[i] == \"D\") {\n                humans[i].x++;\n            } else if (actions[i] == \"L\") {\n                humans[i].y--;\n            } else if (actions[i] == \"R\") {\n                humans[i].y++;\n            }\n        }\n\n        for (int i = 0; i < N; i++) {\n            string move;\n            cin >> move;\n            for (char c : move) {\n                if (c == 'U') pets[i].x--;\n                else if (c == 'D') pets[i].x++;\n                else if (c == 'L') pets[i].y--;\n                else if (c == 'R') pets[i].y++;\n            }\n        }\n    }\n\n    return 0;\n}","ahc009":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\n#include <tuple>\n#include <cmath>\nusing namespace std;\n\ndouble evaluate_route(const string& route, int s_i, int s_j, int t_i, int t_j, double p, \n                     const vector<string>& h, const vector<string>& v) {\n    int T = route.size();\n    vector<vector<double>> dp(20, vector<double>(20, 0.0));\n    for (int i = 0; i < 20; i++) {\n        for (int j = 0; j < 20; j++) {\n            if (i == t_i && j == t_j) {\n                dp[i][j] = 401 - T;\n            } else {\n                dp[i][j] = 0.0;\n            }\n        }\n    }\n\n    for (int t = T-1; t >= 0; t--) {\n        vector<vector<double>> new_dp(20, vector<double>(20, 0.0));\n        for (int i = 0; i < 20; i++) {\n            for (int j = 0; j < 20; j++) {\n                if (i == t_i && j == t_j) {\n                    new_dp[i][j] = 401 - t;\n                } else {\n                    char c = route[t];\n                    int ni = i, nj = j;\n                    bool blocked = false;\n                    if (c == 'U') {\n                        if (i > 0 && v[i-1][j] == '0') \n                            ni--;\n                        else \n                            blocked = true;\n                    } else if (c == 'D') {\n                        if (i < 19 && v[i][j] == '0') \n                            ni++;\n                        else \n                            blocked = true;\n                    } else if (c == 'L') {\n                        if (j > 0 && h[i][j-1] == '0') \n                            nj--;\n                        else \n                            blocked = true;\n                    } else if (c == 'R') {\n                        if (j < 19 && h[i][j] == '0') \n                            nj++;\n                        else \n                            blocked = true;\n                    }\n\n                    double val;\n                    if (ni == t_i && nj == t_j && !blocked) {\n                        val = p * dp[i][j] + (1-p) * (401 - (t+1));\n                    } else {\n                        if (blocked) {\n                            val = p * dp[i][j] + (1-p) * dp[i][j];\n                        } else {\n                            val = p * dp[i][j] + (1-p) * dp[ni][nj];\n                        }\n                    }\n                    new_dp[i][j] = val;\n                }\n            }\n        }\n        dp = new_dp;\n    }\n    return dp[s_i][s_j];\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int s_i, s_j, t_i, t_j;\n    double p;\n    cin >> s_i >> s_j >> t_i >> t_j >> p;\n\n    vector<string> h(20);\n    for (int i = 0; i < 20; i++) {\n        cin >> h[i];\n    }\n\n    vector<string> v(19);\n    for (int i = 0; i < 19; i++) {\n        cin >> v[i];\n    }\n\n    vector<vector<bool>> visited(20, vector<bool>(20, false));\n    vector<vector<pair<int, int>>> parent(20, vector<pair<int, int>>(20, {-1, -1}));\n    vector<vector<char>> parent_move(20, vector<char>(20, ' '));\n    queue<pair<int, int>> q;\n    q.push({s_i, s_j});\n    visited[s_i][s_j] = true;\n\n    while (!q.empty()) {\n        auto [i, j] = q.front(); q.pop();\n        if (i == t_i && j == t_j) break;\n\n        if (i > 0 && v[i-1][j] == '0' && !visited[i-1][j]) {\n            visited[i-1][j] = true;\n            parent[i-1][j] = {i, j};\n            parent_move[i-1][j] = 'U';\n            q.push({i-1, j});\n        }\n        if (i < 19 && v[i][j] == '0' && !visited[i+1][j]) {\n            visited[i+1][j] = true;\n            parent[i+1][j] = {i, j};\n            parent_move[i+1][j] = 'D';\n            q.push({i+1, j});\n        }\n        if (j > 0 && h[i][j-1] == '0' && !visited[i][j-1]) {\n            visited[i][j-1] = true;\n            parent[i][j-1] = {i, j};\n            parent_move[i][j-1] = 'L';\n            q.push({i, j-1});\n        }\n        if (j < 19 && h[i][j] == '0' && !visited[i][j+1]) {\n            visited[i][j+1] = true;\n            parent[i][j+1] = {i, j};\n            parent_move[i][j+1] = 'R';\n            q.push({i, j+1});\n        }\n    }\n\n    string bfs_route = \"\";\n    string extended_bfs = string(200, 'U');\n    if (visited[t_i][t_j]) {\n        int i = t_i, j = t_j;\n        while (!(i == s_i && j == s_j)) {\n            char m = parent_move[i][j];\n            bfs_route += m;\n            auto [pi, pj] = parent[i][j];\n            i = pi;\n            j = pj;\n        }\n        reverse(bfs_route.begin(), bfs_route.end());\n        int L = bfs_route.size();\n        extended_bfs = \"\";\n        for (int i = 0; i < 200; i++) {\n            extended_bfs += bfs_route[i % L];\n        }\n    }\n\n    vector<pair<string, double>> candidates;\n\n    if (visited[t_i][t_j]) {\n        char safe_move = ' ';\n        if (t_i == 19) safe_move = 'D';\n        else if (t_j == 19) safe_move = 'R';\n        else if (v[t_i][t_j] == '1') safe_move = 'D';\n        else if (h[t_i][t_j] == '1') safe_move = 'R';\n        else if (t_i > 0 && v[t_i-1][t_j] == '1') safe_move = 'U';\n        else if (t_j > 0 && h[t_i][t_j-1] == '1') safe_move = 'L';\n        \n        if (safe_move != ' ') {\n            int L = bfs_route.size();\n            string stay_route = bfs_route;\n            if (L < 200) {\n                stay_route += string(200 - L, safe_move);\n            } else {\n                stay_route = stay_route.substr(0, 200);\n            }\n            double score_stay = evaluate_route(stay_route, s_i, s_j, t_i, t_j, p, h, v);\n            candidates.push_back({stay_route, score_stay});\n        }\n    }\n\n    const int T = 200;\n    vector<vector<vector<char>>> best_move(T, vector<vector<char>>(20, vector<char>(20, 'U')));\n    vector<vector<double>> dp_next(20, vector<double>(20, 0.0));\n\n    for (int i = 0; i < 20; i++) {\n        for (int j = 0; j < 20; j++) {\n            if (i == t_i && j == t_j) {\n                dp_next[i][j] = 201.0;\n            } else {\n                dp_next[i][j] = 0.0;\n            }\n        }\n    }\n\n    for (int tt = T-1; tt >= 0; tt--) {\n        vector<vector<double>> dp_curr(20, vector<double>(20, 0.0));\n        for (int i = 0; i < 20; i++) {\n            for (int j = 0; j < 20; j++) {\n                if (i == t_i && j == t_j) {\n                    dp_curr[i][j] = 401.0 - tt;\n                    continue;\n                }\n                double best_val = -1e18;\n                char best_char = 'U';\n                for (char move : {'U','D','L','R'}) {\n                    int ni = i, nj = j;\n                    if (move == 'U') {\n                        if (i > 0 && v[i-1][j] == '0') {\n                            ni = i - 1;\n                        }\n                    } else if (move == 'D') {\n                        if (i < 19 && v[i][j] == '0') {\n                            ni = i + 1;\n                        }\n                    } else if (move == 'L') {\n                        if (j > 0 && h[i][j-1] == '0') {\n                            nj = j - 1;\n                        }\n                    } else if (move == 'R') {\n                        if (j < 19 && h[i][j] == '0') {\n                            nj = j + 1;\n                        }\n                    }\n\n                    double val = p * dp_next[i][j] + (1-p) * dp_next[ni][nj];\n                    if (val > best_val) {\n                        best_val = val;\n                        best_char = move;\n                    }\n                }\n                dp_curr[i][j] = best_val;\n                best_move[tt][i][j] = best_char;\n            }\n        }\n        dp_next = dp_curr;\n    }\n\n    int cur_i = s_i, cur_j = s_j;\n    string route_dp = \"\";\n    for (int t = 0; t < T; t++) {\n        if (cur_i == t_i && cur_j == t_j) {\n            int rem = T - t;\n            route_dp += string(rem, 'U');\n            break;\n        }\n        char move = best_move[t][cur_i][cur_j];\n        route_dp += move;\n\n        if (move == 'U') {\n            if (cur_i > 0 && v[cur_i-1][cur_j] == '0') {\n                cur_i--;\n            }\n        } else if (move == 'D') {\n            if (cur_i < 19 && v[cur_i][cur_j] == '0') {\n                cur_i++;\n            }\n        } else if (move == 'L') {\n            if (cur_j > 0 && h[cur_i][cur_j-1] == '0') {\n                cur_j--;\n            }\n        } else if (move == 'R') {\n            if (cur_j < 19 && h[cur_i][cur_j] == '0') {\n                cur_j++;\n            }\n        }\n    }\n\n    double score_dp = evaluate_route(route_dp, s_i, s_j, t_i, t_j, p, h, v);\n    candidates.push_back({route_dp, score_dp});\n\n    double score_bfs = evaluate_route(extended_bfs, s_i, s_j, t_i, t_j, p, h, v);\n    candidates.push_back({extended_bfs, score_bfs});\n\n    string best_route = extended_bfs;\n    double best_score = -1;\n    for (auto [route, score] : candidates) {\n        if (score > best_score) {\n            best_score = score;\n            best_route = route;\n        }\n    }\n\n    cout << best_route << endl;\n\n    return 0;\n}","ahc010":"#include <iostream>\n#include <vector>\n#include <algorithm>\n\nusing namespace std;\n\nconst vector<int> di = {0, -1, 0, 1};\nconst vector<int> dj = {-1, 0, 1, 0};\n\nconst vector<vector<int>> to_table = {\n    {1, 0, -1, -1},\n    {3, -1, -1, 0},\n    {-1, -1, 3, 2},\n    {-1, 2, 1, -1},\n    {1, 0, 3, 2},\n    {3, 2, 1, 0},\n    {2, -1, 0, -1},\n    {-1, 3, -1, 1}\n};\n\nlong long compute_score(const vector<vector<int>>& grid, const vector<vector<int>>& rot) {\n    int n = 30;\n    vector<vector<int>> eff(n, vector<int>(n));\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            int t = grid[i][j];\n            int r = rot[i][j];\n            if (t < 4) {\n                eff[i][j] = (t + r) % 4;\n            } else if (t < 6) {\n                eff[i][j] = (r % 2 == 0) ? t : (t ^ 1);\n            } else {\n                eff[i][j] = (r % 2 == 0) ? t : (t ^ 1);\n            }\n        }\n    }\n\n    vector<vector<vector<bool>>> visited(n, vector<vector<bool>>(n, vector<bool>(4, false)));\n    vector<long long> loops;\n\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            for (int d = 0; d < 4; d++) {\n                if (visited[i][j][d]) continue;\n\n                int ci = i, cj = j, cd = d;\n                long long steps = 0;\n                bool found_loop = false;\n                vector<vector<vector<bool>>> current_visited(n, vector<vector<bool>>(n, vector<bool>(4, false)));\n\n                while (true) {\n                    if (current_visited[ci][cj][cd]) break;\n                    current_visited[ci][cj][cd] = true;\n                    visited[ci][cj][cd] = true;\n\n                    int eff_tile = eff[ci][cj];\n                    int d2 = to_table[eff_tile][cd];\n                    if (d2 == -1) break;\n\n                    int ni = ci + di[d2];\n                    int nj = cj + dj[d2];\n                    if (ni < 0 || ni >= n || nj < 0 || nj >= n) break;\n\n                    int nd = (d2 + 2) % 4;\n                    steps++;\n\n                    if (ni == i && nj == j && nd == d) {\n                        found_loop = true;\n                        break;\n                    }\n\n                    ci = ni;\n                    cj = nj;\n                    cd = nd;\n                }\n\n                if (found_loop) {\n                    loops.push_back(steps);\n                }\n            }\n        }\n    }\n\n    if (loops.size() < 2) {\n        return 0;\n    }\n\n    sort(loops.rbegin(), loops.rend());\n    return (long long)loops[0] * loops[1];\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int num_test_cases = 100;\n    for (int tc = 0; tc < num_test_cases; tc++) {\n        vector<vector<int>> grid(30, vector<int>(30));\n        for (int i = 0; i < 30; i++) {\n            string s;\n            cin >> s;\n            for (int j = 0; j < 30; j++) {\n                grid[i][j] = s[j] - '0';\n            }\n        }\n\n        vector<vector<int>> rot(30, vector<int>(30));\n        for (int i = 0; i < 30; i++) {\n            for (int j = 0; j < 30; j++) {\n                rot[i][j] = (i + j) % 4;\n            }\n        }\n\n        for (int i = 0; i < 30; i++) {\n            for (int j = 0; j < 30; j++) {\n                cout << rot[i][j];\n            }\n        }\n        cout << '\\n';\n    }\n\n    return 0;\n}","ahc011":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\n#include <tuple>\n#include <map>\n#include <climits>\n#include <array>\n#include <ctime>\n#include <unordered_map>\nusing namespace std;\n\nconst int dx[4] = {0, -1, 0, 1};\nconst int dy[4] = {1, 0, -1, 0};\nconst int opposite_dir[4] = {2, 3, 0, 1};\nconst int bitmask[4] = {4, 2, 1, 8};\n\nstruct State {\n    vector<vector<int>> board;\n    int empty_r, empty_c;\n    string moves;\n    char last_move;\n    int S, C, comp_count, dist;\n};\n\nunordered_map<size_t, tuple<int, int, int>> cache;\n\nsize_t hash_board(vector<vector<int>>& board, int N) {\n    size_t h = 0;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            h = h * 31 + board[i][j];\n        }\n    }\n    return h;\n}\n\ntuple<int, int, int> evaluate(vector<vector<int>>& board, int N) {\n    size_t h = hash_board(board, N);\n    if (cache.find(h) != cache.end()) {\n        return cache[h];\n    }\n    \n    array<array<bool, 10>, 10> visited = {};\n    array<array<bool, 10>, 10> in_comp = {};\n    int total_edges = 0;\n    int max_tree_size = 0;\n    int comp_count = 0;\n\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (board[i][j] == 0) {\n                visited[i][j] = true;\n            } else {\n                visited[i][j] = false;\n            }\n        }\n    }\n\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (board[i][j] == 0) continue;\n            if (j < N-1 && board[i][j+1] != 0) {\n                if ((board[i][j] & 4) && (board[i][j+1] & 1)) {\n                    total_edges++;\n                }\n            }\n            if (i < N-1 && board[i+1][j] != 0) {\n                if ((board[i][j] & 8) && (board[i+1][j] & 2)) {\n                    total_edges++;\n                }\n            }\n        }\n    }\n\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (!visited[i][j] && board[i][j] != 0) {\n                comp_count++;\n                queue<pair<int, int>> q;\n                vector<pair<int, int>> component;\n                q.push({i, j});\n                visited[i][j] = true;\n                in_comp[i][j] = true;\n                component.push_back({i, j});\n\n                while (!q.empty()) {\n                    auto [r, c] = q.front();\n                    q.pop();\n                    for (int d = 0; d < 4; d++) {\n                        int nr = r + dx[d];\n                        int nc = c + dy[d];\n                        if (nr < 0 || nr >= N || nc < 0 || nc >= N) continue;\n                        if (visited[nr][nc]) continue;\n                        if (board[nr][nc] == 0) continue;\n                        if (!(board[r][c] & bitmask[d])) continue;\n                        if (!(board[nr][nc] & bitmask[opposite_dir[d]])) continue;\n                        visited[nr][nc] = true;\n                        in_comp[nr][nc] = true;\n                        q.push({nr, nc});\n                        component.push_back({nr, nc});\n                    }\n                }\n\n                int edge_count = 0;\n                for (auto [r, c] : component) {\n                    for (int d = 0; d < 4; d++) {\n                        int nr = r + dx[d];\n                        int nc = c + dy[d];\n                        if (nr < 0 || nr >= N || nc < 0 || nc >= N) continue;\n                        if (in_comp[nr][nc] && (board[r][c] & bitmask[d])) {\n                            edge_count++;\n                        }\n                    }\n                }\n                edge_count /= 2;\n\n                int V = component.size();\n                if (edge_count == V - 1) {\n                    max_tree_size = max(max_tree_size, V);\n                }\n\n                for (auto [r, c] : component) {\n                    in_comp[r][c] = false;\n                }\n            }\n        }\n    }\n\n    if (max_tree_size == 0) {\n        max_tree_size = 1;\n    }\n\n    auto result = make_tuple(max_tree_size, total_edges, comp_count);\n    cache[h] = result;\n    return result;\n}\n\nint main() {\n    clock_t start_time = clock();\n    int N, T_val;\n    cin >> N >> T_val;\n    vector<string> input_grid(N);\n    for (int i = 0; i < N; i++) {\n        cin >> input_grid[i];\n    }\n\n    int BEAM_WIDTH;\n    if (N == 6) BEAM_WIDTH = 200;\n    else if (N == 7) BEAM_WIDTH = 150;\n    else if (N == 8) BEAM_WIDTH = 100;\n    else if (N == 9) BEAM_WIDTH = 60;\n    else BEAM_WIDTH = 40;\n\n    vector<vector<int>> initial_board(N, vector<int>(N));\n    int empty_r = -1, empty_c = -1;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            char c = input_grid[i][j];\n            if (c >= '0' && c <= '9') {\n                initial_board[i][j] = c - '0';\n            } else if (c >= 'a' && c <= 'f') {\n                initial_board[i][j] = 10 + (c - 'a');\n            } else {\n                initial_board[i][j] = 0;\n            }\n            if (initial_board[i][j] == 0) {\n                empty_r = i;\n                empty_c = j;\n            }\n        }\n    }\n\n    cache.clear();\n    auto [S0, C0, comp0] = evaluate(initial_board, N);\n    int dist0 = abs(empty_r - (N-1)) + abs(empty_c - (N-1));\n    State initial_state = {initial_board, empty_r, empty_c, \"\", ' ', S0, C0, comp0, dist0};\n\n    vector<State> beam;\n    beam.push_back(initial_state);\n\n    string final_sequence = \"\";\n    int min_full_moves = INT_MAX;\n    bool found_full_tree = false;\n\n    map<char, char> reverse_move = {{'U','D'}, {'D','U'}, {'L','R'}, {'R','L'}};\n    map<char, pair<int, int>> dir = {{'U',{-1,0}}, {'D',{1,0}}, {'L',{0,-1}}, {'R',{0,1}}};\n\n    for (int step = 0; step < T_val; step++) {\n        double elapsed = double(clock() - start_time) / CLOCKS_PER_SEC;\n        if (elapsed > 2.9) {\n            break;\n        }\n\n        vector<State> candidates;\n        int min_in_step = INT_MAX;\n        string best_in_step = \"\";\n\n        for (const State& state : beam) {\n            for (const char move : {'U','D','L','R'}) {\n                if (state.last_move != ' ' && move == reverse_move[state.last_move]) {\n                    continue;\n                }\n\n                int dr = dir[move].first;\n                int dc = dir[move].second;\n                int new_r = state.empty_r + dr;\n                int new_c = state.empty_c + dc;\n\n                if (new_r < 0 || new_r >= N || new_c < 0 || new_c >= N) {\n                    continue;\n                }\n\n                State new_state = state;\n                swap(new_state.board[state.empty_r][state.empty_c], new_state.board[new_r][new_c]);\n                new_state.empty_r = new_r;\n                new_state.empty_c = new_c;\n                new_state.moves = state.moves + move;\n                new_state.last_move = move;\n                \n                tie(new_state.S, new_state.C, new_state.comp_count) = evaluate(new_state.board, N);\n                new_state.dist = abs(new_r - (N-1)) + abs(new_c - (N-1));\n                \n                candidates.push_back(new_state);\n\n                if (new_state.S == N*N-1) {\n                    int move_count = new_state.moves.size();\n                    if (move_count < min_in_step) {\n                        min_in_step = move_count;\n                        best_in_step = new_state.moves;\n                    }\n                }\n            }\n        }\n\n        if (min_in_step < min_full_moves) {\n            min_full_moves = min_in_step;\n            final_sequence = best_in_step;\n            found_full_tree = true;\n        }\n\n        if (candidates.empty()) {\n            break;\n        }\n\n        sort(candidates.begin(), candidates.end(), [](const State& a, const State& b) {\n            if (a.S != b.S) return a.S > b.S;\n            if (a.C != b.C) return a.C > b.C;\n            if (a.comp_count != b.comp_count) return a.comp_count < b.comp_count;\n            return a.dist < b.dist;\n        });\n\n        beam.clear();\n        int width = min(BEAM_WIDTH, (int)candidates.size());\n        for (int i = 0; i < width; i++) {\n            beam.push_back(candidates[i]);\n        }\n\n        if (found_full_tree) {\n            break;\n        }\n    }\n\n    if (!found_full_tree && !beam.empty()) {\n        sort(beam.begin(), beam.end(), [](const State& a, const State& b) {\n            if (a.S != b.S) return a.S > b.S;\n            if (a.C != b.C) return a.C > b.C;\n            if (a.comp_count != b.comp_count) return a.comp_count < b.comp_count;\n            return a.dist < b.dist;\n        });\n        final_sequence = beam[0].moves;\n    }\n\n    cout << final_sequence << endl;\n\n    return 0;\n}","ahc012":"#include <bits/stdc++.h>\n#include <random>\nusing namespace std;\ntypedef long long ll;\ntypedef __int128 i128;\n\nconst int B = 20000;\nconst ll R2 = 100000000;\n\nstruct Point {\n    ll x, y;\n    Point() {}\n    Point(ll x, ll y) : x(x), y(y) {}\n    bool operator==(const Point& other) const {\n        return x == other.x && y == other.y;\n    }\n};\n\nstruct Line {\n    Point p, q;\n    ll a, b, c;\n    Line(Point p1, Point p2) : p(p1), q(p2) {\n        a = p.y - q.y;\n        b = q.x - p.x;\n        c = p.x * q.y - q.x * p.y;\n    }\n};\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    random_device rd;\n    mt19937 gen(rd());\n    uniform_int_distribution<int> distrib(-B, B);\n\n    const int CANDIDATE_SIZE = 1000;\n    const int num_test_cases = 100;\n    for (int tc = 0; tc < num_test_cases; tc++) {\n        int N, K;\n        cin >> N >> K;\n        vector<int> a(11, 0);\n        for (int i = 1; i <= 10; i++) {\n            cin >> a[i];\n        }\n        vector<Point> points;\n        for (int i = 0; i < N; i++) {\n            ll x, y;\n            cin >> x >> y;\n            points.push_back(Point(x, y));\n        }\n\n        vector<Line> candidate_lines;\n        vector<bool> used;\n        for (int i = 0; i < CANDIDATE_SIZE; ) {\n            bool found = false;\n            for (int t = 0; t < 100; t++) {\n                Point p(distrib(gen), distrib(gen));\n                Point q(distrib(gen), distrib(gen));\n                if (p == q) continue;\n                Line L(p, q);\n                i128 a_val = L.a;\n                i128 b_val = L.b;\n                i128 c_val = L.c;\n                i128 lhs = c_val * c_val;\n                i128 rhs = R2 * (a_val * a_val + b_val * b_val);\n                if (lhs > rhs) continue;\n\n                bool valid = true;\n                for (const Point& s : points) {\n                    ll D = L.a * s.x + L.b * s.y + L.c;\n                    if (D == 0) {\n                        valid = false;\n                        break;\n                    }\n                }\n                if (valid) {\n                    candidate_lines.push_back(L);\n                    found = true;\n                    i++;\n                    break;\n                }\n            }\n            if (!found) {\n                break;\n            }\n        }\n        used.assign(candidate_lines.size(), false);\n\n        vector<vector<int>> current_groups;\n        if (N > 0) {\n            current_groups.push_back(vector<int>());\n            for (int i = 0; i < N; i++) {\n                current_groups[0].push_back(i);\n            }\n        }\n\n        vector<int> current_b_d(11, 0);\n        if (N >= 1 && N <= 10) {\n            current_b_d[N] = 1;\n        }\n        int current_score = 0;\n        for (int d = 1; d <= 10; d++) {\n            current_score += min(a[d], current_b_d[d]);\n        }\n\n        vector<Line> chosen_lines;\n\n        for (int step = 0; step < K; step++) {\n            int best_next_score = current_score;\n            int best_candidate = -1;\n\n            if (candidate_lines.empty()) break;\n\n            for (int idx = 0; idx < candidate_lines.size(); idx++) {\n                if (used[idx]) continue;\n                const Line& L = candidate_lines[idx];\n                vector<int> new_b_d(11, 0);\n                for (const vector<int>& group : current_groups) {\n                    int cnt0 = 0, cnt1 = 0;\n                    for (int sid : group) {\n                        const Point& s = points[sid];\n                        ll D = L.a * s.x + L.b * s.y + L.c;\n                        if (D > 0) cnt1++;\n                        else cnt0++;\n                    }\n                    if (cnt0 > 0 && cnt0 <= 10) new_b_d[cnt0]++;\n                    if (cnt1 > 0 && cnt1 <= 10) new_b_d[cnt1]++;\n                }\n                int score = 0;\n                for (int d = 1; d <= 10; d++) {\n                    score += min(a[d], new_b_d[d]);\n                }\n                if (score > best_next_score) {\n                    best_next_score = score;\n                    best_candidate = idx;\n                }\n            }\n\n            if (best_candidate == -1) break;\n\n            used[best_candidate] = true;\n            const Line& L = candidate_lines[best_candidate];\n            chosen_lines.push_back(L);\n\n            vector<vector<int>> new_groups;\n            for (const vector<int>& group : current_groups) {\n                vector<int> group0, group1;\n                for (int sid : group) {\n                    const Point& s = points[sid];\n                    ll D = L.a * s.x + L.b * s.y + L.c;\n                    if (D > 0) group1.push_back(sid);\n                    else group0.push_back(sid);\n                }\n                if (!group0.empty()) new_groups.push_back(group0);\n                if (!group1.empty()) new_groups.push_back(group1);\n            }\n            current_groups = move(new_groups);\n\n            current_b_d.assign(11, 0);\n            for (const auto& g : current_groups) {\n                int s = g.size();\n                if (s >= 1 && s <= 10) current_b_d[s]++;\n            }\n            current_score = best_next_score;\n        }\n\n        cout << chosen_lines.size() << endl;\n        for (const Line& L : chosen_lines) {\n            cout << L.p.x << \" \" << L.p.y << \" \" << L.q.x << \" \" << L.q.y << endl;\n        }\n    }\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    cin >> N >> M;\n    vector<vector<bool>> dot(N, vector<bool>(N, false));\n    vector<set<int>> rows(N);\n    vector<set<int>> cols(N);\n\n    vector<vector<int>> next_in_row(N, vector<int>(N, N));\n    vector<vector<int>> prev_in_row(N, vector<int>(N, -1));\n    vector<vector<int>> next_in_col(N, vector<int>(N, N));\n    vector<vector<int>> prev_in_col(N, vector<int>(N, -1));\n\n    vector<vector<bool>> hor(N, vector<bool>(N, false));\n    vector<vector<bool>> ver(N, vector<bool>(N, false));\n\n    for (int i = 0; i < M; i++) {\n        int x, y;\n        cin >> x >> y;\n        dot[x][y] = true;\n        rows[y].insert(x);\n        cols[x].insert(y);\n    }\n\n    for (int y = 0; y < N; y++) {\n        int last = -1;\n        for (int x = 0; x < N; x++) {\n            if (dot[x][y]) {\n                last = x;\n            }\n            prev_in_row[y][x] = last;\n        }\n        last = N;\n        for (int x = N-1; x >= 0; x--) {\n            if (dot[x][y]) {\n                last = x;\n            }\n            next_in_row[y][x] = last;\n        }\n    }\n\n    for (int x = 0; x < N; x++) {\n        int last = -1;\n        for (int y = 0; y < N; y++) {\n            if (dot[x][y]) {\n                last = y;\n            }\n            prev_in_col[x][y] = last;\n        }\n        last = N;\n        for (int y = N-1; y >= 0; y--) {\n            if (dot[x][y]) {\n                last = y;\n            }\n            next_in_col[x][y] = last;\n        }\n    }\n\n    int c = (N - 1) / 2;\n    priority_queue<tuple<int, int, int>> pq;\n    for (int x = 0; x < N; x++) {\n        for (int y = 0; y < N; y++) {\n            if (!dot[x][y]) {\n                int weight = (x - c) * (x - c) + (y - c) * (y - c) + 1;\n                pq.push(make_tuple(weight, x, y));\n            }\n        }\n    }\n\n    vector<array<int, 8>> operations;\n    auto start_time = chrono::high_resolution_clock::now();\n\n    while (!pq.empty()) {\n        auto now = chrono::high_resolution_clock::now();\n        auto duration = chrono::duration_cast<chrono::milliseconds>(now - start_time);\n        if (duration.count() > 4500) {\n            break;\n        }\n\n        auto top = pq.top();\n        int w = get<0>(top);\n        int x1 = get<1>(top);\n        int y1 = get<2>(top);\n        pq.pop();\n        if (dot[x1][y1]) {\n            continue;\n        }\n\n        bool found = false;\n        const auto& col_x1 = cols[x1];\n        for (int y2 : col_x1) {\n            if (y2 == y1) continue;\n            int y_min = min(y1, y2);\n            int y_max = max(y1, y2);\n\n            if (next_in_col[x1][y_min + 1] < y_max) {\n                continue;\n            }\n\n            set<int> common;\n            const set<int>& row_y1 = rows[y1];\n            const set<int>& row_y2 = rows[y2];\n            if (row_y1.size() < row_y2.size()) {\n                for (int x : row_y1) {\n                    if (row_y2.find(x) != row_y2.end()) {\n                        common.insert(x);\n                    }\n                }\n            } else {\n                for (int x : row_y2) {\n                    if (row_y1.find(x) != row_y1.end()) {\n                        common.insert(x);\n                    }\n                }\n            }\n\n            for (int x2 : common) {\n                if (x2 == x1) continue;\n                int x_min = min(x1, x2);\n                int x_max = max(x1, x2);\n\n                if (next_in_row[y1][x_min + 1] < x_max) {\n                    continue;\n                }\n                if (next_in_row[y2][x_min + 1] < x_max) {\n                    continue;\n                }\n                if (next_in_col[x2][y_min + 1] < y_max) {\n                    continue;\n                }\n\n                bool edgeValid = true;\n                for (int y = y_min; y < y_max; y++) {\n                    if (y < N - 1 && ver[x1][y]) {\n                        edgeValid = false;\n                        break;\n                    }\n                }\n                if (!edgeValid) continue;\n\n                for (int y = y_min; y < y_max; y++) {\n                    if (y < N - 1 && ver[x2][y]) {\n                        edgeValid = false;\n                        break;\n                    }\n                }\n                if (!edgeValid) continue;\n\n                for (int x = x_min; x < x_max; x++) {\n                    if (x < N - 1 && hor[x][y_min]) {\n                        edgeValid = false;\n                        break;\n                    }\n                }\n                if (!edgeValid) continue;\n\n                for (int x = x_min; x < x_max; x++) {\n                    if (x < N - 1 && hor[x][y_max]) {\n                        edgeValid = false;\n                        break;\n                    }\n                }\n                if (!edgeValid) continue;\n\n                operations.push_back({x1, y1, x1, y2, x2, y2, x2, y1});\n                dot[x1][y1] = true;\n                rows[y1].insert(x1);\n                cols[x1].insert(y1);\n\n                int last_val = -1;\n                for (int x = 0; x < N; x++) {\n                    if (dot[x][y1]) {\n                        last_val = x;\n                    }\n                    prev_in_row[y1][x] = last_val;\n                }\n                last_val = N;\n                for (int x = N - 1; x >= 0; x--) {\n                    if (dot[x][y1]) {\n                        last_val = x;\n                    }\n                    next_in_row[y1][x] = last_val;\n                }\n\n                last_val = -1;\n                for (int y = 0; y < N; y++) {\n                    if (dot[x1][y]) {\n                        last_val = y;\n                    }\n                    prev_in_col[x1][y] = last_val;\n                }\n                last_val = N;\n                for (int y = N - 1; y >= 0; y--) {\n                    if (dot[x1][y]) {\n                        last_val = y;\n                    }\n                    next_in_col[x1][y] = last_val;\n                }\n\n                for (int y = y_min; y < y_max; y++) {\n                    if (y < N - 1) {\n                        ver[x1][y] = true;\n                    }\n                }\n                for (int y = y_min; y < y_max; y++) {\n                    if (y < N - 1) {\n                        ver[x2][y] = true;\n                    }\n                }\n                for (int x = x_min; x < x_max; x++) {\n                    if (x < N - 1) {\n                        hor[x][y_min] = true;\n                    }\n                }\n                for (int x = x_min; x < x_max; x++) {\n                    if (x < N - 1) {\n                        hor[x][y_max] = true;\n                    }\n                }\n\n                found = true;\n                break;\n            }\n            if (found) break;\n        }\n    }\n\n    cout << operations.size() << endl;\n    for (auto& op : operations) {\n        for (int i = 0; i < 8; i++) {\n            if (i > 0) cout << \" \";\n            cout << op[i];\n        }\n        cout << endl;\n    }\n\n    return 0;\n}","ahc015":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <climits>\n#include <algorithm>\nusing namespace std;\n\nvector<vector<int>> simulate_tilt(vector<vector<int>> grid, char dir) {\n    vector<vector<int>> new_grid(10, vector<int>(10, 0));\n    if (dir == 'L' || dir == 'R') {\n        for (int i = 0; i < 10; i++) {\n            vector<int> non_empty;\n            for (int j = 0; j < 10; j++) {\n                if (grid[i][j] != 0) {\n                    non_empty.push_back(grid[i][j]);\n                }\n            }\n            if (dir == 'L') {\n                for (int j = 0; j < non_empty.size(); j++) {\n                    new_grid[i][j] = non_empty[j];\n                }\n            } else {\n                int start = 10 - non_empty.size();\n                for (int j = 0; j < non_empty.size(); j++) {\n                    new_grid[i][start + j] = non_empty[j];\n                }\n            }\n        }\n    } else {\n        for (int j = 0; j < 10; j++) {\n            vector<int> non_empty;\n            for (int i = 0; i < 10; i++) {\n                if (grid[i][j] != 0) {\n                    non_empty.push_back(grid[i][j]);\n                }\n            }\n            if (dir == 'F') {\n                for (int i = 0; i < non_empty.size(); i++) {\n                    new_grid[i][j] = non_empty[i];\n                }\n            } else {\n                int start = 10 - non_empty.size();\n                for (int i = 0; i < non_empty.size(); i++) {\n                    new_grid[start + i][j] = non_empty[i];\n                }\n            }\n        }\n    }\n    return new_grid;\n}\n\nint compute_manhattan(vector<vector<int>>& grid, const vector<int>& penalty_type) {\n    int penalty = 0;\n    for (int i = 0; i < 10; i++) {\n        for (int j = 0; j < 10; j++) {\n            if (grid[i][j] != 0) {\n                int f = grid[i][j];\n                int t = penalty_type[f];\n                if (t == 0) {\n                    penalty += i + j;\n                } else if (t == 1) {\n                    penalty += i + (9 - j);\n                } else {\n                    penalty += (9 - i) + j;\n                }\n            }\n        }\n    }\n    return penalty;\n}\n\nint compute_score(vector<vector<int>>& grid) {\n    int n = 10;\n    vector<vector<bool>> visited(n, vector<bool>(n, false));\n    int total_score = 0;\n    vector<int> dr = {1, -1, 0, 0};\n    vector<int> dc = {0, 0, 1, -1};\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            if (!visited[i][j] && grid[i][j] != 0) {\n                int flavor = grid[i][j];\n                int count = 0;\n                queue<pair<int, int>> q;\n                q.push({i, j});\n                visited[i][j] = true;\n                while (!q.empty()) {\n                    auto cell = q.front();\n                    q.pop();\n                    count++;\n                    int r = cell.first;\n                    int c = cell.second;\n                    for (int d = 0; d < 4; d++) {\n                        int nr = r + dr[d];\n                        int nc = c + dc[d];\n                        if (nr >= 0 && nr < n && nc >= 0 && nc < n && !visited[nr][nc] && grid[nr][nc] == flavor) {\n                            visited[nr][nc] = true;\n                            q.push({nr, nc});\n                        }\n                    }\n                }\n                total_score += count * count;\n            }\n        }\n    }\n    return total_score;\n}\n\nint compute_pairs(vector<vector<int>>& grid) {\n    int pairs = 0;\n    for (int i = 0; i < 10; i++) {\n        for (int j = 0; j < 10; j++) {\n            if (grid[i][j] != 0) {\n                if (j < 9 && grid[i][j] == grid[i][j+1]) {\n                    pairs++;\n                }\n                if (i < 9 && grid[i][j] == grid[i+1][j]) {\n                    pairs++;\n                }\n            }\n        }\n    }\n    return pairs;\n}\n\nint main() {\n    vector<int> flavors;\n    for (int i = 0; i < 100; i++) {\n        int f;\n        cin >> f;\n        flavors.push_back(f);\n    }\n\n    vector<int> flavor_counts(4, 0);\n    for (int f : flavors) {\n        if (f >= 1 && f <= 3) {\n            flavor_counts[f]++;\n        }\n    }\n    vector<int> order = {1, 2, 3};\n    sort(order.begin(), order.end(), [&](int a, int b) {\n        return flavor_counts[a] > flavor_counts[b];\n    });\n    vector<int> penalty_type(4);\n    penalty_type[order[0]] = 0;\n    penalty_type[order[1]] = 1;\n    penalty_type[order[2]] = 2;\n\n    vector<vector<int>> grid(10, vector<int>(10, 0));\n    vector<char> dirs = {'L', 'R', 'F', 'B'};\n\n    for (int t = 0; t < 100; t++) {\n        int p;\n        cin >> p;\n\n        vector<pair<int, int>> emptyCells;\n        for (int i = 0; i < 10; i++) {\n            for (int j = 0; j < 10; j++) {\n                if (grid[i][j] == 0) {\n                    emptyCells.push_back(make_pair(i, j));\n                }\n            }\n        }\n        sort(emptyCells.begin(), emptyCells.end(), [](const pair<int, int>& a, const pair<int, int>& b) {\n            if (a.first != b.first) return a.first < b.first;\n            return a.second < b.second;\n        });\n        int r = emptyCells[p-1].first;\n        int c = emptyCells[p-1].second;\n        grid[r][c] = flavors[t];\n\n        int best_score = -1;\n        int best_pairs = -1;\n        int best_penalty = INT_MAX;\n        char best_dir = 'L';\n\n        for (char dir : dirs) {\n            vector<vector<int>> new_grid = simulate_tilt(grid, dir);\n            int score = compute_score(new_grid);\n            int pairs = compute_pairs(new_grid);\n            int penalty = compute_manhattan(new_grid, penalty_type);\n            \n            if (score > best_score) {\n                best_score = score;\n                best_pairs = pairs;\n                best_penalty = penalty;\n                best_dir = dir;\n            } else if (score == best_score) {\n                if (pairs > best_pairs) {\n                    best_pairs = pairs;\n                    best_penalty = penalty;\n                    best_dir = dir;\n                } else if (pairs == best_pairs) {\n                    if (penalty < best_penalty) {\n                        best_penalty = penalty;\n                        best_dir = dir;\n                    }\n                }\n            }\n        }\n\n        cout << best_dir << endl;\n        cout.flush();\n\n        grid = simulate_tilt(grid, best_dir);\n    }\n\n    return 0;\n}","ahc016":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <cmath>\nusing namespace std;\n\nconst int n = 40;\nvector<pair<int, int>> allPairs;\n\nvector<int> greedy_selection(int M, const vector<vector<double>>& points) {\n    int totalPoints = points.size();\n    if (M == 0) return {};\n    if (totalPoints == 0) return {};\n\n    vector<vector<double>> D_sq(totalPoints, vector<double>(totalPoints, 0.0));\n    for (int i = 0; i < totalPoints; i++) {\n        for (int j = 0; j < i; j++) {\n            double dist = 0.0;\n            for (int k = 0; k < n; k++) {\n                double diff = points[i][k] - points[j][k];\n                dist += diff * diff;\n            }\n            D_sq[i][j] = dist;\n            D_sq[j][i] = dist;\n        }\n    }\n\n    vector<bool> selected(totalPoints, false);\n    vector<int> selected_list;\n    selected_list.push_back(0);\n    selected[0] = true;\n\n    vector<double> minDist(totalPoints, 1e18);\n    for (int j = 0; j < totalPoints; j++) {\n        minDist[j] = D_sq[j][0];\n    }\n\n    for (int i = 1; i < M; i++) {\n        int best_j = -1;\n        double best_value = -1;\n        for (int j = 0; j < totalPoints; j++) {\n            if (!selected[j] && minDist[j] > best_value) {\n                best_value = minDist[j];\n                best_j = j;\n            }\n        }\n        if (best_j == -1) break;\n        selected_list.push_back(best_j);\n        selected[best_j] = true;\n        for (int j = 0; j < totalPoints; j++) {\n            if (!selected[j] && D_sq[j][best_j] < minDist[j]) {\n                minDist[j] = D_sq[j][best_j];\n            }\n        }\n    }\n    return selected_list;\n}\n\nint main() {\n    if (allPairs.empty()) {\n        for (int a = 1; a <= n; a++) {\n            for (int b = 1; b <= min(a, n - a); b++) {\n                allPairs.push_back({a, b});\n            }\n        }\n    }\n    int totalPoints = allPairs.size();\n\n    int M;\n    double eps;\n    cin >> M >> eps;\n\n    vector<vector<double>> points(totalPoints, vector<double>(n));\n    for (int idx = 0; idx < totalPoints; idx++) {\n        int a = allPairs[idx].first;\n        int b = allPairs[idx].second;\n        int c = n - a - b;\n        vector<double> deg;\n        for (int i = 0; i < c; i++) \n            deg.push_back(eps * (n-1));\n        for (int i = 0; i < b; i++)\n            deg.push_back((b-1)*(1-2*eps) + eps*(n-1));\n        for (int i = 0; i < a; i++)\n            deg.push_back((a-1)*(1-2*eps) + eps*(n-1));\n        points[idx] = deg;\n    }\n\n    vector<int> selected_indices;\n    if (M <= totalPoints) {\n        selected_indices = greedy_selection(M, points);\n    } else {\n        for (int i = 0; i < M; i++) {\n            selected_indices.push_back(i % totalPoints);\n        }\n    }\n\n    const int L = n * (n - 1) / 2;\n    cout << n << endl;\n\n    for (int i : selected_indices) {\n        int a = allPairs[i].first;\n        int b = allPairs[i].second;\n        string s = \"\";\n        for (int u = 0; u < n; u++) {\n            for (int v = u + 1; v < n; v++) {\n                if ((u < a && v < a) || \n                    (u >= a && u < a+b && v >= a && v < a+b)) {\n                    s += '1';\n                } else {\n                    s += '0';\n                }\n            }\n        }\n        cout << s << endl;\n    }\n    cout.flush();\n\n    vector<double> expected_degrees;\n    vector<int> degH(n);\n    for (int q = 0; q < 100; q++) {\n        string H;\n        cin >> H;\n\n        fill(degH.begin(), degH.end(), 0);\n        int idx = 0;\n        for (int u = 0; u < n; u++) {\n            for (int v = u + 1; v < n; v++) {\n                if (H[idx] == '1') {\n                    degH[u]++;\n                    degH[v]++;\n                }\n                idx++;\n            }\n        }\n        sort(degH.begin(), degH.end());\n\n        double minDist = 1e18;\n        int best_i = 0;\n        for (int i = 0; i < M; i++) {\n            int idx = selected_indices[i];\n            double dist = 0.0;\n            for (int j = 0; j < n; j++) {\n                double diff = points[idx][j] - degH[j];\n                dist += diff * diff;\n            }\n            if (dist < minDist) {\n                minDist = dist;\n                best_i = i;\n            }\n        }\n\n        cout << best_i << endl;\n        cout.flush();\n    }\n\n    return 0;\n}","ahc017":"#include <bits/stdc++.h>\n#include <Eigen/Dense>\nusing namespace std;\nusing namespace Eigen;\n\ntypedef long long ll;\n\nvector<int> get_components(int N, const vector<tuple<int, int, int>>& edges, const vector<bool>& remove_edge) {\n    vector<int> parent(N);\n    iota(parent.begin(), parent.end(), 0);\n    vector<int> rank(N, 0);\n    \n    function<int(int)> find = [&](int x) {\n        if (parent[x] != x) parent[x] = find(parent[x]);\n        return parent[x];\n    };\n    \n    auto unite = [&](int x, int y) {\n        x = find(x); y = find(y);\n        if (x == y) return false;\n        if (rank[x] < rank[y]) parent[x] = y;\n        else {\n            parent[y] = x;\n            if (rank[x] == rank[y]) rank[x]++;\n        }\n        return true;\n    };\n    \n    for (int i = 0; i < edges.size(); i++) {\n        if (remove_edge[i]) continue;\n        auto [u, v, w] = edges[i];\n        unite(u, v);\n    }\n    \n    vector<int> comp(N);\n    for (int i = 0; i < N; i++) {\n        comp[i] = find(i);\n    }\n    return comp;\n}\n\nbool is_connected(int N, const vector<tuple<int, int, int>>& edges, const vector<bool>& remove_edge) {\n    vector<int> comp = get_components(N, edges, remove_edge);\n    for (int i = 1; i < N; i++) {\n        if (comp[i] != comp[0]) {\n            return false;\n        }\n    }\n    return true;\n}\n\nstruct QuadtreeNode {\n    int x1, y1, x2, y2;\n    vector<int> point_indices;\n    QuadtreeNode* children[4] = {nullptr, nullptr, nullptr, nullptr};\n    \n    QuadtreeNode(int x1_, int y1_, int x2_, int y2_) : \n        x1(x1_), y1(y1_), x2(x2_), y2(y2_) {}\n};\n\nconst int MAX_POINTS_PER_NODE = 20;\n\nQuadtreeNode* build_quadtree(const vector<pair<int, int>>& points, int x1, int y1, int x2, int y2) {\n    QuadtreeNode* node = new QuadtreeNode(x1, y1, x2, y2);\n    for (int i = 0; i < points.size(); i++) {\n        int x = points[i].first;\n        int y = points[i].second;\n        if (x >= x1 && x <= x2 && y >= y1 && y <= y2) {\n            node->point_indices.push_back(i);\n        }\n    }\n    \n    if (node->point_indices.size() > MAX_POINTS_PER_NODE) {\n        int midx = (x1 + x2) / 2;\n        int midy = (y1 + y2) / 2;\n        node->children[0] = build_quadtree(points, x1, y1, midx, midy);\n        node->children[1] = build_quadtree(points, midx+1, y1, x2, midy);\n        node->children[2] = build_quadtree(points, x1, midy+1, midx, y2);\n        node->children[3] = build_quadtree(points, midx+1, midy+1, x2, y2);\n    }\n    return node;\n}\n\nvoid get_points_in_region(QuadtreeNode* node, int x1, int y1, int x2, int y2, vector<int>& result) {\n    if (!node) return;\n    if (x1 > node->x2 || x2 < node->x1 || y1 > node->y2 || y2 < node->y1) {\n        return;\n    }\n    if (x1 <= node->x1 && x2 >= node->x2 && y1 <= node->y1 && y2 >= node->y2) {\n        for (int idx : node->point_indices) {\n            result.push_back(idx);\n        }\n    } else {\n        for (int i = 0; i < 4; i++) {\n            get_points_in_region(node->children[i], x1, y1, x2, y2, result);\n        }\n    }\n}\n\nvector<vector<int>> spatial_partition(const vector<pair<int, int>>& points, int grid_size) {\n    int min_x = 0, max_x = 1000;\n    int min_y = 0, max_y = 1000;\n    QuadtreeNode* root = build_quadtree(points, min_x, min_y, max_x, max_y);\n    \n    vector<vector<int>> regions;\n    for (int i = 0; i < grid_size; i++) {\n        for (int j = 0; j < grid_size; j++) {\n            int x1 = min_x + i * (max_x - min_x) / grid_size;\n            int x2 = min_x + (i+1) * (max_x - min_x) / grid_size;\n            int y1 = min_y + j * (max_y - min_y) / grid_size;\n            int y2 = min_y + (j+1) * (max_y - min_y) / grid_size;\n            \n            vector<int> region_points;\n            get_points_in_region(root, x1, y1, x2, y2, region_points);\n            regions.push_back(region_points);\n        }\n    }\n    return regions;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, D, K;\n    cin >> N >> M >> D >> K;\n    vector<tuple<int, int, int>> edges;\n    vector<vector<tuple<int, int, int>>> adj(N);\n    map<pair<int, int>, int> edgeMap;\n    vector<pair<int, int>> points(N);\n\n    for (int i = 0; i < M; i++) {\n        int u, v, w;\n        cin >> u >> v >> w;\n        u--; v--;\n        edges.push_back({u, v, w});\n        adj[u].push_back({v, w, i});\n        adj[v].push_back({u, w, i});\n        int a = min(u, v), b = max(u, v);\n        edgeMap[{a, b}] = i;\n    }\n    for (int i = 0; i < N; i++) {\n        int x, y;\n        cin >> x >> y;\n        points[i] = {x, y};\n    }\n\n    vector<double> bc(M, 0.0);\n\n    for (int s = 0; s < N; s++) {\n        vector<ll> dist(N, 1e18);\n        vector<int> numPaths(N, 0);\n        vector<vector<int>> pred(N);\n        vector<int> order;\n\n        dist[s] = 0;\n        numPaths[s] = 1;\n        priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;\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            order.push_back(u);\n            for (auto [v, w, eid] : adj[u]) {\n                ll nd = d + w;\n                if (nd < dist[v]) {\n                    dist[v] = nd;\n                    numPaths[v] = numPaths[u];\n                    pred[v] = {u};\n                    pq.push({nd, v});\n                } else if (nd == dist[v]) {\n                    numPaths[v] += numPaths[u];\n                    pred[v].push_back(u);\n                }\n            }\n        }\n\n        vector<double> dep(N, 0.0);\n        for (int i = order.size() - 1; i >= 0; i--) {\n            int w = order[i];\n            for (int u : pred[w]) {\n                if (numPaths[w] == 0) continue;\n                double frac = static_cast<double>(numPaths[u]) / numPaths[w] * (1.0 + dep[w]);\n                int a = min(u, w);\n                int b = max(u, w);\n                auto it = edgeMap.find({a, b});\n                if (it == edgeMap.end()) continue;\n                int eid = it->second;\n                bc[eid] += frac;\n                dep[u] += frac;\n            }\n        }\n    }\n\n    vector<int> assignment(M, -1);\n    vector<int> day_count(D, 0);\n    vector<double> day_sum(D, 0.0);\n\n    int grid_size = min(10, static_cast<int>(sqrt(D * 4)));\n    vector<vector<int>> regions = spatial_partition(points, grid_size);\n    vector<vector<int>> region_edges(regions.size());\n\n    for (int i = 0; i < M; i++) {\n        auto [u, v, w] = edges[i];\n        for (int r = 0; r < regions.size(); r++) {\n            bool u_in = find(regions[r].begin(), regions[r].end(), u) != regions[r].end();\n            bool v_in = find(regions[r].begin(), regions[r].end(), v) != regions[r].end();\n            if (u_in || v_in) {\n                region_edges[r].push_back(i);\n            }\n        }\n    }\n\n    for (int r = 0; r < regions.size(); r++) {\n        sort(region_edges[r].begin(), region_edges[r].end(), [&](int i, int j) {\n            return bc[i] > bc[j];\n        });\n        for (int eid : region_edges[r]) {\n            if (assignment[eid] != -1) continue;\n            int best_d = -1;\n            double min_sum = 1e18;\n            for (int d = 0; d < D; d++) {\n                if (day_count[d] < K && day_sum[d] < min_sum) {\n                    min_sum = day_sum[d];\n                    best_d = d;\n                }\n            }\n            if (best_d == -1) {\n                for (int d = 0; d < D; d++) {\n                    if (day_count[d] < K) {\n                        best_d = d;\n                        break;\n                    }\n                }\n                if (best_d == -1) best_d = 0;\n            }\n            assignment[eid] = best_d;\n            day_count[best_d]++;\n            day_sum[best_d] += bc[eid];\n        }\n    }\n\n    vector<int> remaining_edges;\n    for (int i = 0; i < M; i++) {\n        if (assignment[i] == -1) {\n            remaining_edges.push_back(i);\n        }\n    }\n    sort(remaining_edges.begin(), remaining_edges.end(), [&](int i, int j) {\n        return bc[i] > bc[j];\n    });\n    for (int eid : remaining_edges) {\n        int best_d = -1;\n        double min_sum = 1e18;\n        for (int d = 0; d < D; d++) {\n            if (day_count[d] < K && day_sum[d] < min_sum) {\n                min_sum = day_sum[d];\n                best_d = d;\n            }\n        }\n        if (best_d == -1) {\n            for (int d = 0; d < D; d++) {\n                if (day_count[d] < K) {\n                    best_d = d;\n                    break;\n                }\n            }\n            if (best_d == -1) best_d = 0;\n        }\n        assignment[eid] = best_d;\n        day_count[best_d]++;\n        day_sum[best_d] += bc[eid];\n    }\n\n    random_device rd;\n    mt19937 gen(rd());\n\n    for (int d = 0; d < D; d++) {\n        vector<bool> remove_d(M, false);\n        for (int i = 0; i < M; i++) {\n            if (assignment[i] == d) {\n                remove_d[i] = true;\n            }\n        }\n\n        for (int swap_round = 0; swap_round < 20; swap_round++) {\n            if (is_connected(N, edges, remove_d)) {\n                break;\n            }\n\n            vector<int> comp = get_components(N, edges, remove_d);\n            vector<int> candidate_e;\n            vector<int> candidate_f;\n\n            for (int i = 0; i < M; i++) {\n                if (remove_d[i]) {\n                    int u = get<0>(edges[i]);\n                    int v = get<1>(edges[i]);\n                    if (comp[u] == comp[v]) {\n                        candidate_e.push_back(i);\n                    }\n                } else {\n                    int u = get<0>(edges[i]);\n                    int v = get<1>(edges[i]);\n                    if (comp[u] != comp[v]) {\n                        candidate_f.push_back(i);\n                    }\n                }\n            }\n\n            if (candidate_e.empty() || candidate_f.empty()) {\n                break;\n            }\n\n            sort(candidate_e.begin(), candidate_e.end(), [&](int i, int j) {\n                return bc[i] < bc[j];\n            });\n            sort(candidate_f.begin(), candidate_f.end(), [&](int i, int j) {\n                return bc[i] < bc[j];\n            });\n\n            bool swapped = false;\n            int attempts = min(500, min((int)candidate_e.size(), (int)candidate_f.size()));\n            for (int i = 0; i < attempts; i++) {\n                int e = candidate_e[i];\n                int f = candidate_f[i];\n                int d2 = assignment[f];\n\n                vector<bool> remove_d_new = remove_d;\n                remove_d_new[e] = false;\n                remove_d_new[f] = true;\n\n                vector<bool> remove_d2(M, false);\n                for (int j = 0; j < M; j++) {\n                    if (assignment[j] == d2) {\n                        remove_d2[j] = true;\n                    }\n                }\n                remove_d2[f] = false;\n                remove_d2[e] = true;\n\n                if (!is_connected(N, edges, remove_d_new) || !is_connected(N, edges, remove_d2)) {\n                    continue;\n                }\n\n                assignment[e] = d2;\n                assignment[f] = d;\n                remove_d[e] = false;\n                remove_d[f] = true;\n                swapped = true;\n                break;\n            }\n\n            if (!swapped) {\n                break;\n            }\n        }\n    }\n\n    for (int i = 0; i < M; i++) {\n        cout << assignment[i] + 1;\n        if (i < M - 1) cout << \" \";\n    }\n    cout << endl;\n\n    return 0;\n}","ahc019":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <tuple>\n#include <algorithm>\n#include <set>\n#include <map>\nusing namespace std;\n\nconst int MAX_VOL = 8;\n\nstruct Rotation {\n    int perm[3];\n    int sign[3];\n};\n\nvector<Rotation> generate_rotations() {\n    vector<int> perm = {0,1,2};\n    vector<Rotation> rots;\n    do {\n        int inv = 0;\n        for (int i=0; i<3; i++)\n            for (int j=i+1; j<3; j++)\n                if (perm[i] > perm[j]) inv++;\n        int perm_sign = (inv%2==0) ? 1 : -1;\n\n        for (int s=0; s<8; s++) {\n            int sx = (s&1) ? -1 : 1;\n            int sy = (s&2) ? -1 : 1;\n            int sz = (s&4) ? -1 : 1;\n            if (perm_sign * sx * sy * sz == 1) {\n                Rotation r;\n                r.perm[0] = perm[0]; r.sign[0] = sx;\n                r.perm[1] = perm[1]; r.sign[1] = sy;\n                r.perm[2] = perm[2]; r.sign[2] = sz;\n                rots.push_back(r);\n            }\n        }\n    } while (next_permutation(perm.begin(), perm.end()));\n    return rots;\n}\n\nvector<tuple<int, int, int>> canonical_form(vector<tuple<int, int, int>> block, vector<Rotation> &rots) {\n    vector<vector<tuple<int, int, int>>> candidates;\n    for (auto &rot : rots) {\n        vector<tuple<int, int, int>> transformed;\n        for (auto [x, y, z] : block) {\n            int coords[3] = {x, y, z};\n            int nx = rot.sign[0] * coords[rot.perm[0]];\n            int ny = rot.sign[1] * coords[rot.perm[1]];\n            int nz = rot.sign[2] * coords[rot.perm[2]];\n            transformed.push_back(make_tuple(nx, ny, nz));\n        }\n        int min_x = get<0>(transformed[0]), min_y = get<1>(transformed[0]), min_z = get<2>(transformed[0]);\n        for (auto &p : transformed) {\n            min_x = min(min_x, get<0>(p));\n            min_y = min(min_y, get<1>(p));\n            min_z = min(min_z, get<2>(p));\n        }\n        vector<tuple<int, int, int>> shifted;\n        for (auto p : transformed) {\n            shifted.push_back(make_tuple(get<0>(p) - min_x, get<1>(p) - min_y, get<2>(p) - min_z));\n        }\n        sort(shifted.begin(), shifted.end());\n        candidates.push_back(shifted);\n    }\n    sort(candidates.begin(), candidates.end());\n    return candidates[0];\n}\n\nint main() {\n    vector<Rotation> rots = generate_rotations();\n\n    int D;\n    cin >> D;\n    vector<vector<string>> f(2, vector<string>(D));\n    vector<vector<string>> r(2, vector<string>(D));\n    for (int i = 0; i < 2; i++) {\n        for (int j = 0; j < D; j++) {\n            cin >> f[i][j];\n        }\n        for (int j = 0; j < D; j++) {\n            cin >> r[i][j];\n        }\n    }\n\n    vector<vector<vector<vector<bool>>>> Allowed(2, vector<vector<vector<bool>>>(D, vector<vector<bool>>(D, vector<bool>(D, false))));\n    vector<vector<vector<vector<bool>>>> Visited(2, vector<vector<vector<bool>>>(D, vector<vector<bool>>(D, vector<bool>(D, false))));\n\n    vector<tuple<int, int, int>> dirs = {{1,0,0}, {-1,0,0}, {0,1,0}, {0,-1,0}, {0,0,1}, {0,0,-1}};\n\n    vector<vector<vector<tuple<int, int, int>>>> components(2);\n    for (int obj = 0; obj < 2; obj++) {\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                    if (f[obj][z][x] == '1' && r[obj][z][y] == '1') {\n                        Allowed[obj][z][x][y] = true;\n                    }\n                }\n            }\n        }\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                    if (Allowed[obj][z][x][y] && !Visited[obj][z][x][y]) {\n                        vector<tuple<int, int, int>> comp;\n                        queue<tuple<int, int, int>> q;\n                        q.push({x, y, z});\n                        Visited[obj][z][x][y] = true;\n                        while (!q.empty()) {\n                            auto [cx, cy, cz] = q.front();\n                            q.pop();\n                            comp.push_back({cx, cy, cz});\n                            for (auto [dx, dy, dz] : dirs) {\n                                int nx = cx + dx;\n                                int ny = cy + dy;\n                                int nz = cz + dz;\n                                if (nx >= 0 && nx < D && ny >= 0 && ny < D && nz >= 0 && nz < D) {\n                                    if (Allowed[obj][nz][nx][ny] && !Visited[obj][nz][nx][ny]) {\n                                        Visited[obj][nz][nx][ny] = true;\n                                        q.push({nx, ny, nz});\n                                    }\n                                }\n                            }\n                        }\n                        components[obj].push_back(comp);\n                    }\n                }\n            }\n        }\n    }\n\n    vector<vector<tuple<int, int, int>>> entire_canonical[2];\n    for (int obj = 0; obj < 2; obj++) {\n        for (auto &comp : components[obj]) {\n            entire_canonical[obj].push_back(canonical_form(comp, rots));\n        }\n    }\n\n    vector<bool> matched0(components[0].size(), false);\n    vector<bool> matched1(components[1].size(), false);\n    vector<int> global_id_counter = {1};\n    vector<int> comp0_block_id(components[0].size(), -1);\n    vector<int> comp1_block_id(components[1].size(), -1);\n\n    vector<tuple<int, int>> comp0_sorted;\n    for (size_t i = 0; i < components[0].size(); i++) {\n        comp0_sorted.push_back(make_tuple(components[0][i].size(), i));\n    }\n    sort(comp0_sorted.rbegin(), comp0_sorted.rend());\n\n    vector<tuple<int, int>> comp1_sorted;\n    for (size_t i = 0; i < components[1].size(); i++) {\n        comp1_sorted.push_back(make_tuple(components[1][i].size(), i));\n    }\n    sort(comp1_sorted.rbegin(), comp1_sorted.rend());\n\n    for (auto [size0, idx0] : comp0_sorted) {\n        if (matched0[idx0]) continue;\n        for (auto [size1, idx1] : comp1_sorted) {\n            if (matched1[idx1]) continue;\n            if (entire_canonical[0][idx0] == entire_canonical[1][idx1]) {\n                matched0[idx0] = true;\n                matched1[idx1] = true;\n                comp0_block_id[idx0] = global_id_counter[0];\n                comp1_block_id[idx1] = global_id_counter[0];\n                global_id_counter[0]++;\n                break;\n            }\n        }\n    }\n\n    vector<vector<tuple<int, int, int>>> small_blocks[2];\n    for (int obj = 0; obj < 2; obj++) {\n        for (size_t i = 0; i < components[obj].size(); i++) {\n            if ((obj == 0 && matched0[i]) || (obj == 1 && matched1[i])) {\n                continue;\n            }\n            auto comp = components[obj][i];\n            map<tuple<int, int, int>, int> original_degree;\n            for (auto p : comp) {\n                int cnt = 0;\n                auto [x, y, z] = p;\n                for (auto [dx, dy, dz] : dirs) {\n                    int nx = x + dx;\n                    int ny = y + dy;\n                    int nz = z + dz;\n                    tuple<int, int, int> np = {nx, ny, nz};\n                    for (auto p2 : comp) {\n                        if (p2 == np) {\n                            cnt++;\n                            break;\n                        }\n                    }\n                }\n                original_degree[p] = cnt;\n            }\n\n            set<tuple<int, int, int>> available_set(comp.begin(), comp.end());\n\n            while (!available_set.empty()) {\n                tuple<int, int, int> start;\n                int max_deg = -1;\n                for (auto p : available_set) {\n                    if (original_degree[p] > max_deg || (original_degree[p] == max_deg && p < start)) {\n                        max_deg = original_degree[p];\n                        start = p;\n                    }\n                }\n\n                set<tuple<int, int, int>> block_set;\n                block_set.insert(start);\n                available_set.erase(start);\n\n                set<tuple<int, int, int>> frontier_set;\n                for (auto [dx, dy, dz] : dirs) {\n                    int nx = get<0>(start) + dx;\n                    int ny = get<1>(start) + dy;\n                    int nz = get<2>(start) + dz;\n                    tuple<int, int, int> np = {nx, ny, nz};\n                    if (available_set.find(np) != available_set.end()) {\n                        frontier_set.insert(np);\n                    }\n                }\n\n                while (block_set.size() < MAX_VOL && !frontier_set.empty()) {\n                    vector<tuple<int, int, tuple<int, int, int>>> candidates;\n                    for (auto p : frontier_set) {\n                        int adj_count = 0;\n                        for (auto [dx, dy, dz] : dirs) {\n                            int nx = get<0>(p) + dx;\n                            int ny = get<1>(p) + dy;\n                            int nz = get<2>(p) + dz;\n                            tuple<int, int, int> np = {nx, ny, nz};\n                            if (block_set.find(np) != block_set.end()) {\n                                adj_count++;\n                            }\n                        }\n                        candidates.push_back(make_tuple(adj_count, original_degree[p], p));\n                    }\n                    sort(candidates.begin(), candidates.end(), [](auto a, auto b) {\n                        if (get<0>(a) != get<0>(b)) return get<0>(a) > get<0>(b);\n                        if (get<1>(a) != get<1>(b)) return get<1>(a) > get<1>(b);\n                        return get<2>(a) < get<2>(b);\n                    });\n                    tuple<int, int, int> chosen = get<2>(candidates[0]);\n\n                    block_set.insert(chosen);\n                    available_set.erase(chosen);\n                    frontier_set.erase(chosen);\n\n                    for (auto [dx, dy, dz] : dirs) {\n                        int nx = get<0>(chosen) + dx;\n                        int ny = get<1>(chosen) + dy;\n                        int nz = get<2>(chosen) + dz;\n                        tuple<int, int, int> np = {nx, ny, nz};\n                        if (available_set.find(np) != available_set.end() && block_set.find(np) == block_set.end()) {\n                            frontier_set.insert(np);\n                        }\n                    }\n                }\n\n                vector<tuple<int, int, int>> block_vec(block_set.begin(), block_set.end());\n                small_blocks[obj].push_back(block_vec);\n            }\n        }\n    }\n\n    vector<vector<tuple<int, int, int>>> small_canonical[2];\n    for (int obj = 0; obj < 2; obj++) {\n        for (auto &block : small_blocks[obj]) {\n            small_canonical[obj].push_back(canonical_form(block, rots));\n        }\n    }\n\n    vector<tuple<int, vector<tuple<int, int, int>>, int>> list0;\n    for (size_t i = 0; i < small_blocks[0].size(); i++) {\n        list0.push_back(make_tuple(small_blocks[0][i].size(), small_canonical[0][i], i));\n    }\n    vector<tuple<int, vector<tuple<int, int, int>>, int>> list1;\n    for (size_t i = 0; i < small_blocks[1].size(); i++) {\n        list1.push_back(make_tuple(small_blocks[1][i].size(), small_canonical[1][i], i));\n    }\n\n    sort(list0.rbegin(), list0.rend());\n    sort(list1.rbegin(), list1.rend());\n\n    vector<int> mapping_small0(small_blocks[0].size(), -1);\n    vector<int> mapping_small1(small_blocks[1].size(), -1);\n\n    for (size_t i = 0; i < list0.size(); i++) {\n        auto [vol0, canon0, idx0] = list0[i];\n        if (mapping_small0[idx0] != -1) continue;\n        for (size_t j = 0; j < list1.size(); j++) {\n            auto [vol1, canon1, idx1] = list1[j];\n            if (mapping_small1[idx1] != -1) continue;\n            if (canon0 == canon1) {\n                mapping_small0[idx0] = global_id_counter[0];\n                mapping_small1[idx1] = global_id_counter[0];\n                global_id_counter[0]++;\n                break;\n            }\n        }\n    }\n\n    for (size_t i = 0; i < small_blocks[0].size(); i++) {\n        if (mapping_small0[i] == -1) {\n            mapping_small0[i] = global_id_counter[0];\n            global_id_counter[0]++;\n        }\n    }\n    for (size_t i = 0; i < small_blocks[1].size(); i++) {\n        if (mapping_small1[i] == -1) {\n            mapping_small1[i] = global_id_counter[0];\n            global_id_counter[0]++;\n        }\n    }\n\n    int n_blocks = global_id_counter[0] - 1;\n\n    vector<vector<vector<int>>> grid0(D, vector<vector<int>>(D, vector<int>(D, 0)));\n    for (size_t idx = 0; idx < components[0].size(); idx++) {\n        if (matched0[idx]) {\n            int gid = comp0_block_id[idx];\n            for (auto [x, y, z] : components[0][idx]) {\n                grid0[x][y][z] = gid;\n            }\n        }\n    }\n    for (size_t i = 0; i < small_blocks[0].size(); i++) {\n        int gid = mapping_small0[i];\n        for (auto [x, y, z] : small_blocks[0][i]) {\n            grid0[x][y][z] = gid;\n        }\n    }\n\n    vector<vector<vector<int>>> grid1(D, vector<vector<int>>(D, vector<int>(D, 0)));\n    for (size_t idx = 0; idx < components[1].size(); idx++) {\n        if (matched1[idx]) {\n            int gid = comp1_block_id[idx];\n            for (auto [x, y, z] : components[1][idx]) {\n                grid1[x][y][z] = gid;\n            }\n        }\n    }\n    for (size_t i = 0; i < small_blocks[1].size(); i++) {\n        int gid = mapping_small1[i];\n        for (auto [x, y, z] : small_blocks[1][i]) {\n            grid1[x][y][z] = gid;\n        }\n    }\n\n    vector<int> flat0, flat1;\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                flat0.push_back(grid0[x][y][z]);\n                flat1.push_back(grid1[x][y][z]);\n            }\n        }\n    }\n\n    cout << n_blocks << endl;\n    for (size_t i = 0; i < flat0.size(); i++) {\n        if (i > 0) cout << \" \";\n        cout << flat0[i];\n    }\n    cout << endl;\n    for (size_t i = 0; i < flat1.size(); i++) {\n        if (i > 0) cout << \" \";\n        cout << flat1[i];\n    }\n    cout << endl;\n\n    return 0;\n}","ahc020":"#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef tuple<int, ll, int> EdgeTuple;\n\nstruct Edge {\n    int u, v, w, id;\n};\n\nvoid prune(vector<bool>& in_steiner, vector<bool>& edge_used, vector<set<pair<int,int>>>& tree_adj, vector<int>& degree, const vector<ll>& P_value, const vector<Edge>& edges, int N) {\n    queue<int> q;\n    for (int i = 0; i < N; i++) {\n        if (in_steiner[i]) {\n            degree[i] = tree_adj[i].size();\n            if (degree[i] == 1 && i != 0 && P_value[i] == 0) {\n                q.push(i);\n            }\n        }\n    }\n    while (!q.empty()) {\n        int u = q.front(); q.pop();\n        if (!in_steiner[u] || degree[u] != 1 || u == 0 || P_value[u] != 0) \n            continue;\n        in_steiner[u] = false;\n        if (tree_adj[u].empty()) continue;\n        auto it = tree_adj[u].begin();\n        int v = it->first;\n        int eid = it->second;\n        tree_adj[u].erase(it);\n        auto it2 = tree_adj[v].find({u, eid});\n        if (it2 != tree_adj[v].end()) {\n            tree_adj[v].erase(it2);\n        }\n        degree[u] = 0;\n        degree[v]--;\n        edge_used[eid] = false;\n        if (degree[v] == 1 && v != 0 && P_value[v] == 0) {\n            q.push(v);\n        }\n    }\n}\n\nvector<ll> reassign_residents(const vector<bool>& in_steiner, const vector<ll>& x, const vector<ll>& y, const vector<ll>& a, const vector<ll>& b, int N, int K, const vector<ll>& current_P) {\n    vector<ll> P_reassign = current_P;\n    vector<pair<ll, int>> resident_min_dist;\n    for (int k = 0; k < K; k++) {\n        ll min_d_sq = 1e18;\n        for (int i = 0; i < N; i++) {\n            if (!in_steiner[i]) continue;\n            ll dx = x[i] - a[k];\n            ll dy = y[i] - b[k];\n            ll d_sq = dx*dx + dy*dy;\n            if (d_sq < min_d_sq) {\n                min_d_sq = d_sq;\n            }\n        }\n        resident_min_dist.push_back({min_d_sq, k});\n    }\n    sort(resident_min_dist.begin(), resident_min_dist.end());\n\n    for (auto [d_sq_val, k] : resident_min_dist) {\n        ll best_increase = 1e18;\n        int best_station = -1;\n        for (int i = 0; i < N; i++) {\n            if (!in_steiner[i]) continue;\n            ll dx = x[i] - a[k];\n            ll dy = y[i] - b[k];\n            ll d_sq = dx*dx + dy*dy;\n            ll d = sqrt(d_sq);\n            if (d*d < d_sq) d++;\n\n            ll current_power = P_reassign[i];\n            if (d <= current_power) {\n                if (0 < best_increase) {\n                    best_increase = 0;\n                    best_station = i;\n                }\n            } else {\n                ll increase = d*d - current_power*current_power;\n                if (increase < best_increase) {\n                    best_increase = increase;\n                    best_station = i;\n                }\n            }\n        }\n        if (best_station != -1) {\n            ll dx = x[best_station] - a[k];\n            ll dy = y[best_station] - b[k];\n            ll d_sq = dx*dx + dy*dy;\n            ll d = sqrt(d_sq);\n            if (d*d < d_sq) d++;\n            if (d > P_reassign[best_station]) {\n                P_reassign[best_station] = d;\n            }\n        }\n    }\n    return P_reassign;\n}\n\nvoid remove_redundant_stations(vector<ll>& P_final, vector<bool>& in_steiner, const vector<ll>& x, const vector<ll>& y, const vector<ll>& a, const vector<ll>& b, int N, int K) {\n    vector<int> coverage_count(K, 0);\n    vector<vector<int>> station_cover(N);\n\n    for (int i = 0; i < N; i++) {\n        if (in_steiner[i] && P_final[i] > 0) {\n            for (int k = 0; k < K; k++) {\n                ll dx = x[i] - a[k];\n                ll dy = y[i] - b[k];\n                ll d_sq = dx*dx + dy*dy;\n                if (d_sq <= (ll)P_final[i] * P_final[i]) {\n                    coverage_count[k]++;\n                    station_cover[i].push_back(k);\n                }\n            }\n        }\n    }\n\n    vector<int> stations;\n    for (int i = 0; i < N; i++) {\n        if (in_steiner[i] && P_final[i] > 0) {\n            stations.push_back(i);\n        }\n    }\n    sort(stations.begin(), stations.end(), [&](int i, int j) {\n        return P_final[i] > P_final[j];\n    });\n\n    for (int i : stations) {\n        bool can_remove = true;\n        for (int k : station_cover[i]) {\n            if (coverage_count[k] <= 1) {\n                can_remove = false;\n                break;\n            }\n        }\n        if (can_remove) {\n            for (int k : station_cover[i]) {\n                coverage_count[k]--;\n            }\n            P_final[i] = 0;\n        }\n    }\n}\n\nvector<ll> reduce_powers(vector<ll>& P_final, vector<bool>& in_steiner, const vector<ll>& x, const vector<ll>& y, const vector<ll>& a, const vector<ll>& b, int N, int K) {\n    vector<vector<int>> cover_list(K);\n    for (int k = 0; k < K; k++) {\n        for (int i = 0; i < N; i++) {\n            if (in_steiner[i] && P_final[i] > 0) {\n                ll dx = x[i] - a[k];\n                ll dy = y[i] - b[k];\n                ll d_sq = dx*dx + dy*dy;\n                if (d_sq <= (ll)P_final[i] * P_final[i]) {\n                    cover_list[k].push_back(i);\n                }\n            }\n        }\n    }\n\n    vector<ll> new_P = P_final;\n    for (int i = 0; i < N; i++) {\n        if (!in_steiner[i] || P_final[i] == 0) \n            continue;\n\n        vector<int> critical_residents;\n        for (int k = 0; k < K; k++) {\n            if (find(cover_list[k].begin(), cover_list[k].end(), i) != cover_list[k].end()) {\n                bool covered_by_others = false;\n                for (int j : cover_list[k]) {\n                    if (j != i) {\n                        covered_by_others = true;\n                        break;\n                    }\n                }\n                if (!covered_by_others) {\n                    critical_residents.push_back(k);\n                }\n            }\n        }\n\n        if (critical_residents.empty()) {\n            new_P[i] = 0;\n        } else {\n            ll max_d_sq = 0;\n            for (int k : critical_residents) {\n                ll dx = x[i] - a[k];\n                ll dy = y[i] - b[k];\n                ll d_sq = dx*dx + dy*dy;\n                if (d_sq > max_d_sq) max_d_sq = d_sq;\n            }\n            ll d = sqrt(max_d_sq);\n            if (d*d < max_d_sq) d++;\n            new_P[i] = d;\n        }\n    }\n    return new_P;\n}\n\nvoid try_remove_edges(vector<bool>& in_steiner, vector<bool>& edge_used, const vector<Edge>& edges, const vector<ll>& P_final, int N, int M) {\n    vector<pair<int, int>> candidate_edges;\n    for (int j = 0; j < M; j++) {\n        if (edge_used[j]) {\n            candidate_edges.push_back({edges[j].w, j});\n        }\n    }\n    sort(candidate_edges.rbegin(), candidate_edges.rend());\n\n    set<int> important_nodes;\n    important_nodes.insert(0);\n    for (int i = 0; i < N; i++) {\n        if (in_steiner[i] && P_final[i] > 0) {\n            important_nodes.insert(i);\n        }\n    }\n\n    vector<bool> new_edge_used = edge_used;\n    vector<set<int>> graph(N);\n    for (int j = 0; j < M; j++) {\n        if (new_edge_used[j]) {\n            int u = edges[j].u;\n            int v = edges[j].v;\n            graph[u].insert(v);\n            graph[v].insert(u);\n        }\n    }\n\n    for (auto& [w, j] : candidate_edges) {\n        if (!new_edge_used[j]) continue;\n        int u = edges[j].u;\n        int v = edges[j].v;\n\n        graph[u].erase(v);\n        graph[v].erase(u);\n\n        vector<bool> visited(N, false);\n        queue<int> q;\n        q.push(0);\n        visited[0] = true;\n        while (!q.empty()) {\n            int node = q.front(); q.pop();\n            for (int neighbor : graph[node]) {\n                if (!visited[neighbor]) {\n                    visited[neighbor] = true;\n                    q.push(neighbor);\n                }\n            }\n        }\n\n        bool connected = true;\n        for (int node : important_nodes) {\n            if (!visited[node]) {\n                connected = false;\n                break;\n            }\n        }\n\n        if (connected) {\n            new_edge_used[j] = false;\n        } else {\n            graph[u].insert(v);\n            graph[v].insert(u);\n        }\n    }\n\n    edge_used = new_edge_used;\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, K;\n    cin >> N >> M >> K;\n\n    vector<ll> x(N), y(N);\n    for (int i = 0; i < N; i++) {\n        cin >> x[i] >> y[i];\n    }\n\n    vector<Edge> edges;\n    for (int j = 0; j < M; j++) {\n        int u, v, w;\n        cin >> u >> v >> w;\n        u--; v--;\n        edges.push_back({u, v, w, j});\n    }\n\n    vector<ll> a(K), b(K);\n    for (int k = 0; k < K; k++) {\n        cin >> a[k] >> b[k];\n    }\n\n    vector<int> assigned_station(K, -1);\n    vector<vector<int>> cluster(N);\n\n    for (int k = 0; k < K; k++) {\n        ll min_d_sq = 1e18;\n        int best_i = -1;\n        for (int i = 0; i < N; i++) {\n            ll dx = a[k] - x[i];\n            ll dy = b[k] - y[i];\n            ll d_sq = dx*dx + dy*dy;\n            if (d_sq < min_d_sq) {\n                min_d_sq = d_sq;\n                best_i = i;\n            }\n        }\n        assigned_station[k] = best_i;\n        cluster[best_i].push_back(k);\n    }\n\n    vector<ll> P_initial(N, 0);\n    vector<bool> in_S(N, false);\n\n    for (int i = 0; i < N; i++) {\n        if (cluster[i].empty()) {\n            P_initial[i] = 0;\n        } else {\n            in_S[i] = true;\n            ll max_d_sq = 0;\n            for (int k : cluster[i]) {\n                ll dx = a[k] - x[i];\n                ll dy = b[k] - y[i];\n                ll d_sq = dx*dx + dy*dy;\n                if (d_sq > max_d_sq) max_d_sq = d_sq;\n            }\n            if (max_d_sq == 0) {\n                P_initial[i] = 0;\n            } else {\n                ll candidate = sqrt(max_d_sq);\n                if (candidate * candidate < max_d_sq) {\n                    candidate++;\n                }\n                P_initial[i] = candidate;\n                if (P_initial[i] > 5000) {\n                    P_initial[i] = 5000;\n                }\n            }\n        }\n    }\n\n    set<int> T_set;\n    for (int i = 0; i < N; i++) {\n        if (in_S[i]) {\n            T_set.insert(i);\n        }\n    }\n    T_set.insert(0);\n\n    vector<vector<EdgeTuple>> adj(N);\n    for (const Edge& e : edges) {\n        int u = e.u, v = e.v;\n        ll w = e.w;\n        int id = e.id;\n        adj[u].push_back(make_tuple(v, w, id));\n        adj[v].push_back(make_tuple(u, w, id));\n    }\n\n    vector<bool> in_steiner(N, false);\n    vector<bool> edge_used(M, false);\n\n    in_steiner[0] = true;\n    set<int> T_remaining = T_set;\n    T_remaining.erase(0);\n\n    while (!T_remaining.empty()) {\n        vector<ll> dist(N, 1e18);\n        vector<int> parent_node(N, -1);\n        vector<int> parent_edge_id(N, -1);\n        priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<>> pq;\n\n        for (int i = 0; i < N; i++) {\n            if (in_steiner[i]) {\n                dist[i] = 0;\n                pq.push({0, i});\n            }\n        }\n\n        bool found = false;\n        int found_node = -1;\n\n        while (!pq.empty()) {\n            auto [d, u] = pq.top();\n            pq.pop();\n            if (d != dist[u]) continue;\n\n            if (T_remaining.find(u) != T_remaining.end()) {\n                found = true;\n                found_node = u;\n                break;\n            }\n\n            for (auto &edge : adj[u]) {\n                int v = get<0>(edge);\n                ll w = get<1>(edge);\n                int eid = get<2>(edge);\n                if (dist[v] > dist[u] + w) {\n                    dist[v] = dist[u] + w;\n                    parent_node[v] = u;\n                    parent_edge_id[v] = eid;\n                    pq.push({dist[v], v});\n                }\n            }\n        }\n\n        if (!found) {\n            break;\n        }\n\n        int cur = found_node;\n        while (!in_steiner[cur]) {\n            edge_used[parent_edge_id[cur]] = true;\n            in_steiner[cur] = true;\n            cur = parent_node[cur];\n        }\n\n        T_remaining.erase(found_node);\n    }\n\n    vector<set<pair<int,int>>> tree_adj(N);\n    vector<int> degree(N, 0);\n    for (int j = 0; j < M; j++) {\n        if (edge_used[j]) {\n            int u = edges[j].u;\n            int v = edges[j].v;\n            tree_adj[u].insert({v, j});\n            tree_adj[v].insert({u, j});\n            degree[u]++;\n            degree[v]++;\n        }\n    }\n\n    prune(in_steiner, edge_used, tree_adj, degree, P_initial, edges, N);\n\n    vector<ll> P_reassign = reassign_residents(in_steiner, x, y, a, b, N, K, P_initial);\n\n    tree_adj = vector<set<pair<int,int>>>(N);\n    degree = vector<int>(N, 0);\n    for (int j = 0; j < M; j++) {\n        if (edge_used[j]) {\n            int u = edges[j].u;\n            int v = edges[j].v;\n            tree_adj[u].insert({v, j});\n            tree_adj[v].insert({u, j});\n            degree[u]++;\n            degree[v]++;\n        }\n    }\n\n    prune(in_steiner, edge_used, tree_adj, degree, P_reassign, edges, N);\n\n    vector<ll> P_final = reassign_residents(in_steiner, x, y, a, b, N, K, P_reassign);\n\n    tree_adj = vector<set<pair<int,int>>>(N);\n    degree = vector<int>(N, 0);\n    for (int j = 0; j < M; j++) {\n        if (edge_used[j]) {\n            int u = edges[j].u;\n            int v = edges[j].v;\n            tree_adj[u].insert({v, j});\n            tree_adj[v].insert({u, j});\n            degree[u]++;\n            degree[v]++;\n        }\n    }\n\n    prune(in_steiner, edge_used, tree_adj, degree, P_final, edges, N);\n\n    const int num_iterations = 3;\n    for (int iter = 0; iter < num_iterations; iter++) {\n        vector<set<pair<int,int>>> new_tree_adj = tree_adj;\n        vector<int> new_degree = degree;\n\n        remove_redundant_stations(P_final, in_steiner, x, y, a, b, N, K);\n\n        prune(in_steiner, edge_used, new_tree_adj, new_degree, P_final, edges, N);\n\n        P_final = reassign_residents(in_steiner, x, y, a, b, N, K, P_final);\n\n        prune(in_steiner, edge_used, new_tree_adj, new_degree, P_final, edges, N);\n\n        vector<ll> new_P = reduce_powers(P_final, in_steiner, x, y, a, b, N, K);\n        P_final = new_P;\n\n        prune(in_steiner, edge_used, new_tree_adj, new_degree, P_final, edges, N);\n\n        P_final = reassign_residents(in_steiner, x, y, a, b, N, K, P_final);\n\n        prune(in_steiner, edge_used, new_tree_adj, new_degree, P_final, edges, N);\n\n        tree_adj = new_tree_adj;\n        degree = new_degree;\n    }\n\n    try_remove_edges(in_steiner, edge_used, edges, P_final, N, M);\n\n    {\n        vector<set<pair<int,int>>> new_tree_adj(N);\n        vector<int> new_degree(N, 0);\n        for (int j = 0; j < M; j++) {\n            if (edge_used[j]) {\n                int u = edges[j].u;\n                int v = edges[j].v;\n                new_tree_adj[u].insert({v, j});\n                new_tree_adj[v].insert({u, j});\n                new_degree[u]++;\n                new_degree[v]++;\n            }\n        }\n        tree_adj = new_tree_adj;\n        degree = new_degree;\n\n        prune(in_steiner, edge_used, tree_adj, degree, P_final, edges, N);\n\n        P_final = reduce_powers(P_final, in_steiner, x, y, a, b, N, K);\n\n        prune(in_steiner, edge_used, tree_adj, degree, P_final, edges, N);\n\n        P_final = reassign_residents(in_steiner, x, y, a, b, N, K, P_final);\n\n        prune(in_steiner, edge_used, tree_adj, degree, P_final, edges, N);\n    }\n\n    for (int i = 0; i < N; i++) {\n        if (P_final[i] > 5000) {\n            P_final[i] = 5000;\n        }\n        cout << P_final[i];\n        if (i < N-1) cout << \" \";\n    }\n    cout << endl;\n\n    for (int j = 0; j < M; j++) {\n        cout << (edge_used[j] ? 1 : 0);\n        if (j < M-1) cout << \" \";\n    }\n    cout << endl;\n\n    return 0;\n}","ahc021":"#include <iostream>\n#include <vector>\n#include <tuple>\n#include <algorithm>\nusing namespace std;\n\nint main() {\n    int grid[30][30];\n    pair<int, int> pos[465];\n\n    for (int i = 0; i < 30; i++) {\n        for (int j = 0; j <= i; j++) {\n            cin >> grid[i][j];\n            pos[grid[i][j]] = make_pair(i, j);\n        }\n    }\n\n    vector<int> nums;\n    for (int i = 464; i >= 0; i--) {\n        nums.push_back(i);\n    }\n\n    vector<tuple<int, int, int, int>> ops;\n\n    for (int num : nums) {\n        int cx = pos[num].first;\n        int cy = pos[num].second;\n        while (cx < 29) {\n            int child1_val = grid[cx+1][cy];\n            int child2_val = grid[cx+1][cy+1];\n            int min_child_val = min(child1_val, child2_val);\n            \n            if (min_child_val >= num) {\n                break;\n            }\n            \n            int child_x = cx + 1;\n            int child_y = (min_child_val == child1_val) ? cy : cy + 1;\n            int child_num = min_child_val;\n            \n            ops.push_back(make_tuple(cx, cy, child_x, child_y));\n            \n            swap(grid[cx][cy], grid[child_x][child_y]);\n            swap(pos[num], pos[child_num]);\n            \n            cx = child_x;\n            cy = child_y;\n        }\n    }\n\n    cout << ops.size() << endl;\n    for (auto op : ops) {\n        cout << get<0>(op) << \" \" << get<1>(op) << \" \" << get<2>(op) << \" \" << get<3>(op) << endl;\n    }\n\n    return 0;\n}","toyota2023summer-final":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\n#include <functional>\n#include <tuple>\n#include <climits>\n#include <cmath>\n\nusing namespace std;\n\nint main() {\n    int D = 9;\n    int N;\n    cin >> D >> N;\n\n    vector<vector<int>> grid(D, vector<int>(D, -3));\n    grid[0][(D-1)/2] = -2;\n\n    for (int i = 0; i < N; i++) {\n        int ri, rj;\n        cin >> ri >> rj;\n        grid[ri][rj] = -1;\n    }\n\n    vector<vector<int>> parent_i(D, vector<int>(D, -1));\n    vector<vector<int>> parent_j(D, vector<int>(D, -1));\n    vector<vector<vector<pair<int, int>>>> children(D, vector<vector<pair<int, int>>>(D));\n\n    vector<int> dx = {0, 1, 0, -1};\n    vector<int> dy = {1, 0, -1, 0};\n\n    queue<pair<int, int>> q_bfs_parent;\n    q_bfs_parent.push({0, (D-1)/2});\n    parent_i[0][(D-1)/2] = -1;\n    parent_j[0][(D-1)/2] = -1;\n\n    while (!q_bfs_parent.empty()) {\n        auto [i, j] = q_bfs_parent.front();\n        q_bfs_parent.pop();\n        for (int d = 0; d < 4; d++) {\n            int ni = i + dx[d];\n            int nj = j + dy[d];\n            if (ni >= 0 && ni < D && nj >= 0 && nj < D) {\n                if (parent_i[ni][nj] == -1) {\n                    if (grid[ni][nj] != -1) {\n                        parent_i[ni][nj] = i;\n                        parent_j[ni][nj] = j;\n                        q_bfs_parent.push({ni, nj});\n                    }\n                }\n            }\n        }\n    }\n\n    for (int i = 0; i < D; i++) {\n        for (int j = 0; j < D; j++) {\n            if (i == 0 && j == (D-1)/2) continue;\n            if (parent_i[i][j] != -1) {\n                int pi = parent_i[i][j];\n                int pj = parent_j[i][j];\n                children[pi][pj].push_back({i, j});\n            }\n        }\n    }\n\n    for (int i = 0; i < D; i++) {\n        for (int j = 0; j < D; j++) {\n            sort(children[i][j].begin(), children[i][j].end());\n        }\n    }\n\n    vector<vector<int>> post_index(D, vector<int>(D, -1));\n    int index_counter = 0;\n\n    function<void(int, int)> dfs = [&](int i, int j) {\n        for (auto& child : children[i][j]) {\n            dfs(child.first, child.second);\n        }\n        if (!(i == 0 && j == (D-1)/2)) {\n            post_index[i][j] = index_counter;\n            index_counter++;\n        }\n    };\n\n    for (auto& child : children[0][(D-1)/2]) {\n        dfs(child.first, child.second);\n    }\n\n    int M = D * D - 1 - N;\n\n    for (int d = 0; d < M; d++) {\n        int t_d;\n        cin >> t_d;\n\n        vector<vector<bool>> visited(D, vector<bool>(D, false));\n        vector<pair<int, int>> free_list;\n        queue<pair<int, int>> q_bfs;\n        q_bfs.push({0, (D-1)/2});\n        visited[0][(D-1)/2] = true;\n\n        while (!q_bfs.empty()) {\n            auto [i, j] = q_bfs.front();\n            q_bfs.pop();\n            if (grid[i][j] == -3) {\n                free_list.push_back({i, j});\n            }\n            for (int dir = 0; dir < 4; dir++) {\n                int ni = i + dx[dir];\n                int nj = j + dy[dir];\n                if (ni >= 0 && ni < D && nj >= 0 && nj < D && !visited[ni][nj]) {\n                    if (grid[ni][nj] == -1 || grid[ni][nj] >= 0) continue;\n                    visited[ni][nj] = true;\n                    q_bfs.push({ni, nj});\n                }\n            }\n        }\n\n        int best_i = -1, best_j = -1;\n        int best_diff = INT_MAX;\n        for (auto [i, j] : free_list) {\n            if (post_index[i][j] == -1) continue;\n            int diff = abs(post_index[i][j] - t_d);\n            if (diff < best_diff || (diff == best_diff && (i < best_i || (i == best_i && j < best_j)))) {\n                best_diff = diff;\n                best_i = i;\n                best_j = j;\n            }\n        }\n\n        if (best_i == -1) {\n            for (auto [i, j] : free_list) {\n                best_i = i;\n                best_j = j;\n                break;\n            }\n        }\n\n        cout << best_i << \" \" << best_j << endl;\n        cout.flush();\n        grid[best_i][best_j] = t_d;\n    }\n\n    vector<pair<int, int>> removal_sequence;\n\n    for (int step = 0; step < M; step++) {\n        vector<vector<bool>> visited(D, vector<bool>(D, false));\n        queue<pair<int, int>> q_bfs;\n        q_bfs.push({0, (D-1)/2});\n        visited[0][(D-1)/2] = true;\n\n        while (!q_bfs.empty()) {\n            auto [i, j] = q_bfs.front();\n            q_bfs.pop();\n            for (int dir = 0; dir < 4; dir++) {\n                int ni = i + dx[dir];\n                int nj = j + dy[dir];\n                if (ni >= 0 && ni < D && nj >= 0 && nj < D && !visited[ni][nj]) {\n                    if (grid[ni][nj] == -1) continue;\n                    if (grid[ni][nj] >= 0) continue;\n                    visited[ni][nj] = true;\n                    q_bfs.push({ni, nj});\n                }\n            }\n        }\n\n        vector<tuple<int, int, int>> removable;\n        for (int i = 0; i < D; i++) {\n            for (int j = 0; j < D; j++) {\n                if (grid[i][j] >= 0) {\n                    bool flag = false;\n                    for (int dir = 0; dir < 4; dir++) {\n                        int ni = i + dx[dir];\n                        int nj = j + dy[dir];\n                        if (ni >= 0 && ni < D && nj >= 0 && nj < D) {\n                            if (visited[ni][nj]) {\n                                flag = true;\n                                break;\n                            }\n                        }\n                    }\n                    if (flag) {\n                        removable.push_back(make_tuple(grid[i][j], i, j));\n                    }\n                }\n            }\n        }\n\n        if (removable.empty()) {\n            for (int i = 0; i < D; i++) {\n                for (int j = 0; j < D; j++) {\n                    if (grid[i][j] >= 0) {\n                        removable.push_back(make_tuple(grid[i][j], i, j));\n                    }\n                }\n            }\n            sort(removable.begin(), removable.end());\n        } else {\n            sort(removable.begin(), removable.end());\n        }\n\n        if (removable.empty()) {\n            break;\n        }\n\n        auto [num, i, j] = removable[0];\n        removal_sequence.push_back({i, j});\n        grid[i][j] = -3;\n    }\n\n    for (auto [i, j] : removal_sequence) {\n        cout << i << \" \" << j << endl;\n    }\n    cout.flush();\n\n    return 0;\n}","ahc024":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\n#include <cstring>\nusing namespace std;\n\ntypedef pair<int, int> pii;\n\nconst int dx[4] = {1, 0, -1, 0};\nconst int dy[4] = {0, 1, 0, -1};\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int n, m;\n    cin >> n >> m;\n    vector<vector<int>> grid(n, vector<int>(n));\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            cin >> grid[i][j];\n        }\n    }\n\n    vector<vector<bool>> orig_adj(101, vector<bool>(101, false));\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            if (i == 0 || i == n-1 || j == 0 || j == n-1) {\n                int c = grid[i][j];\n                if (c > 0) {\n                    orig_adj[0][c] = true;\n                }\n            }\n        }\n    }\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            if (i < n-1 && grid[i][j] != grid[i+1][j] && grid[i][j] > 0 && grid[i+1][j] > 0) {\n                int c1 = min(grid[i][j], grid[i+1][j]);\n                int c2 = max(grid[i][j], grid[i+1][j]);\n                orig_adj[c1][c2] = true;\n            }\n            if (j < n-1 && grid[i][j] != grid[i][j+1] && grid[i][j] > 0 && grid[i][j+1] > 0) {\n                int c1 = min(grid[i][j], grid[i][j+1]);\n                int c2 = max(grid[i][j], grid[i][j+1]);\n                orig_adj[c1][c2] = true;\n            }\n        }\n    }\n\n    vector<vector<int>> current_adj_count(101, vector<int>(101, 0));\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            if (i == 0 || i == n-1 || j == 0 || j == n-1) {\n                int c = grid[i][j];\n                if (c > 0) {\n                    current_adj_count[0][c]++;\n                }\n            }\n        }\n    }\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            if (i < n-1 && grid[i][j] != grid[i+1][j] && grid[i][j] > 0 && grid[i+1][j] > 0) {\n                int c1 = min(grid[i][j], grid[i+1][j]);\n                int c2 = max(grid[i][j], grid[i+1][j]);\n                current_adj_count[c1][c2]++;\n            }\n            if (j < n-1 && grid[i][j] != grid[i][j+1] && grid[i][j] > 0 && grid[i][j+1] > 0) {\n                int c1 = min(grid[i][j], grid[i][j+1]);\n                int c2 = max(grid[i][j], grid[i][j+1]);\n                current_adj_count[c1][c2]++;\n            }\n        }\n    }\n\n    vector<int> ward_size(m+1, 0);\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            int c = grid[i][j];\n            if (c > 0) {\n                ward_size[c]++;\n            }\n        }\n    }\n\n    int num_zeros = 0;\n    vector<vector<bool>> in_queue(n, vector<bool>(n, true));\n    queue<pii> candidate_queue;\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            candidate_queue.push({i, j});\n        }\n    }\n\n    while (!candidate_queue.empty()) {\n        auto [i, j] = candidate_queue.front();\n        candidate_queue.pop();\n        in_queue[i][j] = false;\n        if (grid[i][j] == 0) continue;\n\n        int c = grid[i][j];\n        if (ward_size[c] == 1) {\n            continue;\n        }\n\n        vector<pii> neighbors_in_c;\n        for (int d = 0; d < 4; d++) {\n            int ni = i + dx[d];\n            int nj = j + dy[d];\n            if (ni < 0 || ni >= n || nj < 0 || nj >= n) continue;\n            if (grid[ni][nj] == c) {\n                neighbors_in_c.push_back({ni, nj});\n            }\n        }\n        if (neighbors_in_c.empty()) {\n            continue;\n        }\n\n        int total_after = ward_size[c] - 1;\n        vector<vector<bool>> visited(n, vector<bool>(n, false));\n        queue<pii> q_bfs;\n        auto [start_i, start_j] = neighbors_in_c[0];\n        q_bfs.push({start_i, start_j});\n        visited[start_i][start_j] = true;\n        int count_visited = 1;\n\n        while (!q_bfs.empty()) {\n            auto [x, y] = q_bfs.front();\n            q_bfs.pop();\n            for (int d = 0; d < 4; d++) {\n                int nx = x + dx[d];\n                int ny = y + dy[d];\n                if (nx < 0 || nx >= n || ny < 0 || ny >= n) continue;\n                if (grid[nx][ny] != c) continue;\n                if (visited[nx][ny]) continue;\n                if (nx == i && ny == j) continue;\n                visited[nx][ny] = true;\n                count_visited++;\n                q_bfs.push({nx, ny});\n            }\n        }\n\n        if (count_visited != total_after) {\n            continue;\n        }\n\n        vector<int> break_count(m+1, 0);\n        for (int d = 0; d < 4; d++) {\n            int ni = i + dx[d];\n            int nj = j + dy[d];\n            if (ni < 0 || ni >= n || nj < 0 || nj >= n) continue;\n            int d_val = grid[ni][nj];\n            if (d_val == c) continue;\n            if (d_val == 0) {\n                break_count[0]++;\n            } else {\n                break_count[d_val]++;\n            }\n        }\n        if (i == 0 || i == n-1 || j == 0 || j == n-1) {\n            break_count[0]++;\n        }\n\n        bool skip = false;\n        for (int d = 0; d <= m; d++) {\n            if (break_count[d] == 0) continue;\n            if (d == 0) {\n                if (orig_adj[0][c] && current_adj_count[0][c] <= break_count[0]) {\n                    skip = true;\n                    break;\n                }\n            } else {\n                int c1 = min(c, d);\n                int c2 = max(c, d);\n                if (orig_adj[c1][c2] && current_adj_count[c1][c2] <= break_count[d]) {\n                    skip = true;\n                    break;\n                }\n            }\n        }\n        if (skip) continue;\n\n        for (int d = 0; d < 4; d++) {\n            int ni = i + dx[d];\n            int nj = j + dy[d];\n            if (ni < 0 || ni >= n || nj < 0 || nj >= n) continue;\n            int d_val = grid[ni][nj];\n            if (d_val > 0 && d_val != c) {\n                if (!orig_adj[0][d_val]) {\n                    skip = true;\n                    break;\n                }\n            }\n        }\n        if (skip) continue;\n\n        if (num_zeros == 0) {\n            if (i != 0 && i != n-1 && j != 0 && j != n-1) {\n                continue;\n            }\n        } else {\n            if (i != 0 && i != n-1 && j != 0 && j != n-1) {\n                bool has_zero_neighbor = false;\n                for (int d = 0; d < 4; d++) {\n                    int ni = i + dx[d];\n                    int nj = j + dy[d];\n                    if (ni < 0 || ni >= n || nj < 0 || nj >= n) continue;\n                    if (grid[ni][nj] == 0) {\n                        has_zero_neighbor = true;\n                        break;\n                    }\n                }\n                if (!has_zero_neighbor) {\n                    continue;\n                }\n            }\n        }\n\n        current_adj_count[0][c] -= break_count[0];\n        for (int d = 1; d <= m; d++) {\n            if (break_count[d] > 0) {\n                int c1 = min(c, d);\n                int c2 = max(c, d);\n                current_adj_count[c1][c2] -= break_count[d];\n            }\n        }\n\n        grid[i][j] = 0;\n        num_zeros++;\n        ward_size[c]--;\n\n        for (int d = 0; d < 4; d++) {\n            int ni = i + dx[d];\n            int nj = j + dy[d];\n            if (ni < 0 || ni >= n || nj < 0 || nj >= n) continue;\n            int d_val = grid[ni][nj];\n            if (d_val > 0) {\n                current_adj_count[0][d_val]++;\n            }\n        }\n\n        for (int d = 0; d < 4; d++) {\n            int ni = i + dx[d];\n            int nj = j + dy[d];\n            if (ni < 0 || ni >= n || nj < 0 || nj >= n) continue;\n            if (grid[ni][nj] != 0 && !in_queue[ni][nj]) {\n                candidate_queue.push({ni, nj});\n                in_queue[ni][nj] = true;\n            }\n        }\n    }\n\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            if (j > 0) cout << \" \";\n            cout << grid[i][j];\n        }\n        cout << endl;\n    }\n\n    return 0;\n}","ahc025":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <random>\n#include <cstdlib>\n#include <ctime>\n#include <numeric>\n\nusing namespace std;\n\nint calc_merge_queries(int n) {\n    if (n <= 1) return 0;\n    int n1 = n / 2;\n    int n2 = n - n1;\n    return calc_merge_queries(n1) + calc_merge_queries(n2) + n - 1;\n}\n\nvector<int> merge(vector<int> left, vector<int> right, int &query_count) {\n    vector<int> res;\n    int i = 0, j = 0;\n    while (i < left.size() && j < right.size()) {\n        cout << \"1 1 \" << left[i] << \" \" << right[j] << endl;\n        string cmp;\n        cin >> cmp;\n        query_count++;\n        if (cmp == \"<\") {\n            res.push_back(left[i]);\n            i++;\n        } else if (cmp == \">\") {\n            res.push_back(right[j]);\n            j++;\n        } else {\n            res.push_back(left[i]);\n            i++;\n        }\n    }\n    while (i < left.size()) {\n        res.push_back(left[i]);\n        i++;\n    }\n    while (j < right.size()) {\n        res.push_back(right[j]);\n        j++;\n    }\n    return res;\n}\n\nvector<int> merge_sort(vector<int> v, int &query_count) {\n    if (v.size() <= 1) return v;\n    int mid = v.size() / 2;\n    vector<int> left(v.begin(), v.begin() + mid);\n    vector<int> right(v.begin() + mid, v.end());\n    left = merge_sort(left, query_count);\n    right = merge_sort(right, query_count);\n    return merge(left, right, query_count);\n}\n\nint main() {\n    random_device rd;\n    mt19937 g(rd());\n\n    int N, D, Q;\n    cin >> N >> D >> Q;\n\n    vector<int> items(N);\n    iota(items.begin(), items.end(), 0);\n    shuffle(items.begin(), items.end(), g);\n\n    int T_sort_full = calc_merge_queries(N);\n    int T_assign = (N - D) * (D - 1);\n    int T_sortD = calc_merge_queries(D);\n    int query_count = 0;\n\n    vector<vector<int>> groups(D);\n\n    if (Q >= T_sort_full + T_assign) {\n        vector<int> sorted_items = merge_sort(items, query_count);\n        reverse(sorted_items.begin(), sorted_items.end());\n        for (int i = 0; i < D; i++) {\n            groups[i].push_back(sorted_items[i]);\n        }\n        for (int i = D; i < N; i++) {\n            vector<int> candidates(D);\n            iota(candidates.begin(), candidates.end(), 0);\n            while (candidates.size() > 1) {\n                int g1 = candidates[0];\n                int g2 = candidates[1];\n                cout << groups[g1].size() << \" \" << groups[g2].size();\n                for (int item : groups[g1]) cout << \" \" << item;\n                for (int item : groups[g2]) cout << \" \" << item;\n                cout << endl;\n                \n                string res;\n                cin >> res;\n                query_count++;\n                \n                if (res == \"<\") candidates.erase(candidates.begin() + 1);\n                else if (res == \">\") candidates.erase(candidates.begin());\n                else candidates.erase(candidates.begin() + 1);\n            }\n            groups[candidates[0]].push_back(sorted_items[i]);\n        }\n    }\n    else if (Q >= T_sortD + T_assign) {\n        vector<int> firstD(items.begin(), items.begin() + D);\n        int sort_queries = 0;\n        vector<int> sorted_firstD = merge_sort(firstD, sort_queries);\n        reverse(sorted_firstD.begin(), sorted_firstD.end());\n        query_count += sort_queries;\n        for (int i = 0; i < D; i++) {\n            groups[i].push_back(sorted_firstD[i]);\n        }\n\n        int T_sort_remaining = calc_merge_queries(N - D);\n        if (Q - query_count >= T_sort_remaining + T_assign) {\n            vector<int> remaining(items.begin() + D, items.end());\n            int rem_queries = 0;\n            vector<int> sorted_remaining = merge_sort(remaining, rem_queries);\n            reverse(sorted_remaining.begin(), sorted_remaining.end());\n            query_count += rem_queries;\n            for (int i = 0; i < N - D; i++) {\n                vector<int> candidates(D);\n                iota(candidates.begin(), candidates.end(), 0);\n                while (candidates.size() > 1) {\n                    int g1 = candidates[0];\n                    int g2 = candidates[1];\n                    cout << groups[g1].size() << \" \" << groups[g2].size();\n                    for (int item : groups[g1]) cout << \" \" << item;\n                    for (int item : groups[g2]) cout << \" \" << item;\n                    cout << endl;\n                    \n                    string res;\n                    cin >> res;\n                    query_count++;\n                    \n                    if (res == \"<\") candidates.erase(candidates.begin() + 1);\n                    else if (res == \">\") candidates.erase(candidates.begin());\n                    else candidates.erase(candidates.begin() + 1);\n                }\n                groups[candidates[0]].push_back(sorted_remaining[i]);\n            }\n        } else {\n            for (int i = N-1; i >= D; i--) {\n                vector<int> candidates(D);\n                iota(candidates.begin(), candidates.end(), 0);\n                while (candidates.size() > 1) {\n                    int g1 = candidates[0];\n                    int g2 = candidates[1];\n                    cout << groups[g1].size() << \" \" << groups[g2].size();\n                    for (int item : groups[g1]) cout << \" \" << item;\n                    for (int item : groups[g2]) cout << \" \" << item;\n                    cout << endl;\n                    \n                    string res;\n                    cin >> res;\n                    query_count++;\n                    \n                    if (res == \"<\") candidates.erase(candidates.begin() + 1);\n                    else if (res == \">\") candidates.erase(candidates.begin());\n                    else candidates.erase(candidates.begin() + 1);\n                }\n                groups[candidates[0]].push_back(items[i]);\n            }\n        }\n    }\n    else {\n        for (int i = 0; i < D; i++) {\n            groups[i].push_back(items[i]);\n        }\n        vector<int> all_groups(D);\n        iota(all_groups.begin(), all_groups.end(), 0);\n        for (int i = N-1; i >= D; i--) {\n            int remaining_queries = Q - query_count;\n            int remaining_items = i - D + 1;\n            if (remaining_queries <= 0) {\n                uniform_int_distribution<int> dist(0, D-1);\n                groups[dist(g)].push_back(items[i]);\n                continue;\n            }\n            int k = min(D, 1 + remaining_queries / remaining_items);\n            k = max(2, k);\n            shuffle(all_groups.begin(), all_groups.end(), g);\n            vector<int> candidate_groups(all_groups.begin(), all_groups.begin() + k);\n            while (candidate_groups.size() > 1) {\n                vector<int> next_candidates;\n                for (int j = 0; j < candidate_groups.size(); j += 2) {\n                    if (j+1 == candidate_groups.size()) {\n                        next_candidates.push_back(candidate_groups[j]);\n                    }\n                    else {\n                        int g1 = candidate_groups[j];\n                        int g2 = candidate_groups[j+1];\n                        cout << groups[g1].size() << \" \" << groups[g2].size();\n                        for (int item : groups[g1]) cout << \" \" << item;\n                        for (int item : groups[g2]) cout << \" \" << item;\n                        cout << endl;\n                        \n                        string res;\n                        cin >> res;\n                        query_count++;\n                        \n                        if (res == \"<\") next_candidates.push_back(g1);\n                        else if (res == \">\") next_candidates.push_back(g2);\n                        else next_candidates.push_back(g1);\n                    }\n                }\n                candidate_groups = next_candidates;\n            }\n            groups[candidate_groups[0]].push_back(items[i]);\n        }\n    }\n\n    while (query_count < Q) {\n        cout << \"1 1 \" << items[0] << \" \" << items[1] << endl;\n        string res;\n        cin >> res;\n        query_count++;\n    }\n\n    vector<int> ans(N);\n    for (int gid = 0; gid < D; gid++) {\n        for (int item : groups[gid]) {\n            ans[item] = gid;\n        }\n    }\n\n    for (int i = 0; i < N; i++) {\n        if (i) cout << \" \";\n        cout << ans[i];\n    }\n    cout << endl;\n\n    return 0;\n}","ahc026":"#include <iostream>\n#include <vector>\nusing namespace std;\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int n, m;\n    cin >> n >> m;\n    vector<vector<int>> stacks(m);\n    for (int i = 0; i < m; i++) {\n        for (int j = 0; j < n/m; j++) {\n            int x;\n            cin >> x;\n            stacks[i].push_back(x);\n        }\n    }\n\n    vector<pair<int, int>> operations;\n    int next_v = 1;\n\n    while (next_v <= n) {\n        bool removed_some = false;\n        while (true) {\n            bool found = false;\n            for (int i = 0; i < m; i++) {\n                if (!stacks[i].empty() && stacks[i].back() == next_v) {\n                    operations.push_back({next_v, 0});\n                    stacks[i].pop_back();\n                    next_v++;\n                    found = true;\n                    removed_some = true;\n                    break;\n                }\n            }\n            if (!found) break;\n        }\n\n        if (next_v > n) break;\n\n        int s = -1;\n        int pos = -1;\n        for (int i = 0; i < m; i++) {\n            for (int j = 0; j < stacks[i].size(); j++) {\n                if (stacks[i][j] == next_v) {\n                    s = i;\n                    pos = j;\n                    break;\n                }\n            }\n            if (s != -1) break;\n        }\n\n        if (s == -1) {\n            next_v++;\n            continue;\n        }\n\n        int L = stacks[s].size() - pos - 1;\n        int w = (L > 0) ? stacks[s][pos+1] : -1;\n        int d = -1;\n        long long best_score = -1000000000000000;\n\n        for (int i = 0; i < m; i++) {\n            if (i == s) continue;\n            \n            if (stacks[i].empty()) {\n                long long score_val = 1000000000;\n                if (score_val > best_score) {\n                    best_score = score_val;\n                    d = i;\n                }\n            } else {\n                int min_future = 1000;\n                for (int box : stacks[i]) {\n                    if (box >= next_v + 1 && box <= next_v + 20) {\n                        if (box < min_future) {\n                            min_future = box;\n                        }\n                    }\n                }\n\n                long long score_val;\n                if (min_future != 1000) {\n                    score_val = min_future;\n                } else {\n                    int top_val = stacks[i].back();\n                    score_val = (long long)top_val * 100000LL - 1000LL * (stacks[i].size() + L);\n                }\n\n                if (score_val > best_score) {\n                    best_score = score_val;\n                    d = i;\n                }\n            }\n        }\n\n        if (d == -1) {\n            for (int i = 0; i < m; i++) {\n                if (i != s) {\n                    d = i;\n                    break;\n                }\n            }\n        }\n\n        vector<int> seg;\n        for (int j = pos+1; j < stacks[s].size(); j++) {\n            seg.push_back(stacks[s][j]);\n        }\n        stacks[s].resize(pos+1);\n        for (int box : seg) {\n            stacks[d].push_back(box);\n        }\n\n        if (L > 0) {\n            operations.push_back({w, d+1});\n        }\n        operations.push_back({next_v, 0});\n        stacks[s].pop_back();\n        next_v++;\n    }\n\n    for (auto &op : operations) {\n        cout << op.first << \" \" << op.second << '\\n';\n    }\n\n    return 0;\n}","ahc027":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\n#include <cmath>\n#include <unordered_map>\n#include <cctype>\n#include <climits>\n#include <functional>\n#include <set>\n#include <iomanip>\n\nusing namespace std;\n\nconst int dx[4] = {0, 1, 0, -1};\nconst string dir_str = \"RDLU\";\nconst int dy[4] = {1, 0, -1, 0};\n\nstruct Cell {\n    int i, j;\n    Cell(int i=0, int j=0) : i(i), j(j) {}\n    bool operator==(const Cell& other) const {\n        return i == other.i && j == other.j;\n    }\n    bool operator!=(const Cell& other) const {\n        return !(*this == other);\n    }\n};\n\nnamespace std {\n    template <>\n    struct hash<Cell> {\n        size_t operator()(const Cell& c) const {\n            return c.i * 100 + c.j;\n        }\n    };\n}\n\nint get_index(int i, int j, int N) {\n    return i * N + j;\n}\n\nCell get_cell(int idx, int N) {\n    return Cell(idx / N, idx % N);\n}\n\nchar get_move_char(Cell from, Cell to) {\n    if (to.i == from.i + 1) return 'D';\n    if (to.i == from.i - 1) return 'U';\n    if (to.j == from.j + 1) return 'R';\n    if (to.j == from.j - 1) return 'L';\n    return '?';\n}\n\nchar reverse_move(char m) {\n    if (m == 'U') return 'D';\n    if (m == 'D') return 'U';\n    if (m == 'L') return 'R';\n    if (m == 'R') return 'L';\n    return m;\n}\n\nvector<vector<int>> precompute_all_pairs_shortest_path(int N, const vector<vector<bool>>& down_wall, const vector<vector<bool>>& right_wall) {\n    int total_cells = N * N;\n    vector<vector<int>> dist(total_cells, vector<int>(total_cells, INT_MAX));\n\n    for (int idx = 0; idx < total_cells; idx++) {\n        Cell src = get_cell(idx, N);\n        vector<int>& d_vec = dist[idx];\n        d_vec[idx] = 0;\n        queue<int> q;\n        q.push(idx);\n\n        while (!q.empty()) {\n            int u_idx = q.front();\n            q.pop();\n            Cell u = get_cell(u_idx, N);\n\n            for (int d = 0; d < 4; d++) {\n                Cell v = Cell(u.i + dx[d], u.j + dy[d]);\n                if (v.i < 0 || v.i >= N || v.j < 0 || v.j >= N) continue;\n                if (d == 0) {\n                    if (right_wall[u.i][u.j]) continue;\n                } else if (d == 1) {\n                    if (down_wall[u.i][u.j]) continue;\n                } else if (d == 2) {\n                    if (v.j < u.j && (u.j-1>=0) && right_wall[u.i][u.j-1]) continue;\n                } else if (d == 3) {\n                    if (v.i < u.i && (u.i-1>=0) && down_wall[u.i-1][u.j]) continue;\n                }\n\n                int v_idx = get_index(v.i, v.j, N);\n                if (d_vec[v_idx] == INT_MAX) {\n                    d_vec[v_idx] = d_vec[u_idx] + 1;\n                    q.push(v_idx);\n                }\n            }\n        }\n    }\n\n    return dist;\n}\n\nvector<vector<int>> precompute_parents_all(int N, const vector<vector<bool>>& down_wall, const vector<vector<bool>>& right_wall) {\n    int total_cells = N * N;\n    vector<vector<int>> parent_all(total_cells, vector<int>(total_cells, -1));\n\n    for (int src_idx = 0; src_idx < total_cells; src_idx++) {\n        vector<int>& parent = parent_all[src_idx];\n        vector<int> dist(total_cells, INT_MAX);\n        dist[src_idx] = 0;\n        parent[src_idx] = src_idx;\n        queue<int> q;\n        q.push(src_idx);\n\n        while (!q.empty()) {\n            int u_idx = q.front();\n            q.pop();\n            Cell u = get_cell(u_idx, N);\n\n            for (int d = 0; d < 4; d++) {\n                Cell v = Cell(u.i + dx[d], u.j + dy[d]);\n                if (v.i < 0 || v.i >= N || v.j < 0 || v.j >= N) continue;\n                if (d == 0) {\n                    if (right_wall[u.i][u.j]) continue;\n                } else if (d == 1) {\n                    if (down_wall[u.i][u.j]) continue;\n                } else if (d == 2) {\n                    if (v.j < u.j && (u.j-1>=0) && right_wall[u.i][u.j-1]) continue;\n                } else if (d == 3) {\n                    if (v.i < u.i && (u.i-1>=0) && down_wall[u.i-1][u.j]) continue;\n                }\n\n                int v_idx = get_index(v.i, v.j, N);\n                if (dist[v_idx] == INT_MAX) {\n                    dist[v_idx] = dist[u_idx] + 1;\n                    parent[v_idx] = u_idx;\n                    q.push(v_idx);\n                }\n            }\n        }\n    }\n\n    return parent_all;\n}\n\nstring generate_base_route(int N, const vector<vector<bool>>& down_wall, const vector<vector<bool>>& right_wall, const vector<vector<int>>& d) {\n    int total_cells = N * N;\n    vector<int> parent_tree(total_cells, -1);\n    vector<int> dist(total_cells, INT_MAX);\n    vector<vector<int>> children(total_cells);\n\n    queue<int> q;\n    dist[0] = 0;\n    parent_tree[0] = 0;\n    q.push(0);\n\n    while (!q.empty()) {\n        int u_idx = q.front(); q.pop();\n        Cell u = get_cell(u_idx, N);\n        for (int d = 0; d < 4; d++) {\n            Cell v = Cell(u.i + dx[d], u.j + dy[d]);\n            if (v.i < 0 || v.i >= N || v.j < 0 || v.j >= N) continue;\n            if (d == 0) {\n                if (right_wall[u.i][u.j]) continue;\n            } else if (d == 1) {\n                if (down_wall[u.i][u.j]) continue;\n            } else if (d == 2) {\n                if (v.j < u.j && (u.j-1>=0) && right_wall[u.i][u.j-1]) continue;\n            } else if (d == 3) {\n                if (v.i < u.i && (u.i-1>=0) && down_wall[u.i-1][u.j]) continue;\n            }\n\n            int v_idx = get_index(v.i, v.j, N);\n            if (dist[v_idx] == INT_MAX) {\n                dist[v_idx] = dist[u_idx] + 1;\n                parent_tree[v_idx] = u_idx;\n                q.push(v_idx);\n                children[u_idx].push_back(v_idx);\n            }\n        }\n    }\n\n    for (int i = 0; i < total_cells; i++) {\n        if (dist[i] == INT_MAX) {\n            cerr << \"Cell not reached: \" << i/N << \", \" << i%N << endl;\n        }\n    }\n\n    function<string(int)> dfs = [&](int u_idx) {\n        string route = \"\";\n        Cell u = get_cell(u_idx, N);\n        vector<int>& child_list = children[u_idx];\n        sort(child_list.begin(), child_list.end(), [&](int a, int b) {\n            return d[get_cell(a, N).i][get_cell(a, N).j] > d[get_cell(b, N).i][get_cell(b, N).j];\n        });\n\n        for (int v_idx : child_list) {\n            Cell v = get_cell(v_idx, N);\n            char move_char = get_move_char(u, v);\n            route += move_char;\n            route += dfs(v_idx);\n            route += reverse_move(move_char);\n        }\n        return route;\n    };\n\n    string base_route = dfs(0);\n    return base_route;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N;\n    cin >> N;\n    vector<string> h_lines(N-1);\n    vector<string> v_lines(N);\n    vector<vector<int>> d(N, vector<int>(N));\n\n    for (int i = 0; i < N-1; i++) {\n        cin >> h_lines[i];\n    }\n    for (int i = 0; i < N; i++) {\n        cin >> v_lines[i];\n    }\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cin >> d[i][j];\n        }\n    }\n\n    vector<vector<bool>> down_wall(N-1, vector<bool>(N, false));\n    vector<vector<bool>> right_wall(N, vector<bool>(N-1, false));\n\n    for (int i = 0; i < N-1; i++) {\n        for (int j = 0; j < N; j++) {\n            down_wall[i][j] = (h_lines[i][j] == '1');\n        }\n    }\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N-1; j++) {\n            right_wall[i][j] = (v_lines[i][j] == '1');\n        }\n    }\n\n    vector<vector<int>> all_dist = precompute_all_pairs_shortest_path(N, down_wall, right_wall);\n    vector<vector<int>> parent_all = precompute_parents_all(N, down_wall, right_wall);\n\n    string base_route = generate_base_route(N, down_wall, right_wall, d);\n    string current_route = base_route;\n\n    int total_cells = N * N;\n    vector<vector<int>> occ(total_cells);\n    vector<Cell> positions;\n    Cell cur(0, 0);\n    positions.push_back(cur);\n    for (int i = 0; i < current_route.size(); i++) {\n        char m = current_route[i];\n        Cell next = cur;\n        if (m == 'R') next.j++;\n        else if (m == 'D') next.i++;\n        else if (m == 'L') next.j--;\n        else if (m == 'U') next.i--;\n        positions.push_back(next);\n        cur = next;\n    }\n\n    for (int idx = 0; idx < positions.size(); idx++) {\n        Cell cell = positions[idx];\n        if (idx > 0) {\n            int cell_idx = get_index(cell.i, cell.j, N);\n            occ[cell_idx].push_back(idx-1);\n        }\n    }\n\n    int L0 = current_route.size();\n    vector<int> freq(total_cells, 0);\n    for (int i = 0; i < total_cells; i++) {\n        freq[i] = occ[i].size();\n    }\n\n    vector<double> gap_est(total_cells, 0.0);\n    for (int i = 0; i < total_cells; i++) {\n        if (freq[i] > 0) {\n            gap_est[i] = L0 * 1.0 / freq[i];\n        }\n    }\n\n    using pdi = pair<double, int>;\n    priority_queue<pdi> pq;\n    for (int i = 0; i < total_cells; i++) {\n        if (freq[i] > 0) {\n            double priority_val = d[get_cell(i, N).i][get_cell(i, N).j] * gap_est[i] * gap_est[i];\n            pq.push({priority_val, i});\n        }\n    }\n\n    int insertion_count = 0;\n    const int MAX_INSERTION = 1000;\n    vector<int> dummy_occ_count(total_cells, 0);\n\n    while (insertion_count < MAX_INSERTION && !pq.empty()) {\n        double priority_val = pq.top().first;\n        int c_idx = pq.top().second;\n        pq.pop();\n        if (freq[c_idx] == 0) continue;\n\n        int best_u_idx = -1;\n        int best_dist = INT_MAX;\n        for (int u_idx = 0; u_idx < total_cells; u_idx++) {\n            if (freq[u_idx] == 0) continue;\n            if (all_dist[u_idx][c_idx] < best_dist) {\n                best_dist = all_dist[u_idx][c_idx];\n                best_u_idx = u_idx;\n            }\n        }\n\n        if (best_u_idx == -1 || best_dist == INT_MAX) {\n            continue;\n        }\n\n        int detour_length = 2 * best_dist;\n        if (L0 + detour_length > 100000) {\n            continue;\n        }\n\n        if (occ[best_u_idx].empty()) {\n            continue;\n        }\n        int insert_move_index = occ[best_u_idx][0];\n\n        Cell best_u = get_cell(best_u_idx, N);\n        Cell c = get_cell(c_idx, N);\n        string moves_to_c = \"\";\n        int current_idx = best_u_idx;\n        while (current_idx != c_idx) {\n            int next_idx = parent_all[c_idx][current_idx];\n            if (next_idx == -1) break;\n            Cell cur_cell = get_cell(current_idx, N);\n            Cell next_cell = get_cell(next_idx, N);\n            char m = get_move_char(cur_cell, next_cell);\n            moves_to_c += m;\n            current_idx = next_idx;\n        }\n\n        string moves_back = \"\";\n        for (int i = moves_to_c.size() - 1; i >= 0; i--) {\n            moves_back += reverse_move(moves_to_c[i]);\n        }\n        string detour = moves_to_c + moves_back;\n\n        string new_route = current_route.substr(0, insert_move_index+1) + detour + current_route.substr(insert_move_index+1);\n        int new_L = new_route.size();\n\n        vector<Cell> new_positions;\n        vector<vector<int>> new_occ(total_cells);\n        Cell pos(0,0);\n        new_positions.push_back(pos);\n        for (int i = 0; i < new_route.size(); i++) {\n            char m = new_route[i];\n            Cell next = pos;\n            if (m == 'R') next.j++;\n            else if (m == 'D') next.i++;\n            else if (m == 'L') next.j--;\n            else if (m == 'U') next.i--;\n            new_positions.push_back(next);\n            int cell_idx = get_index(next.i, next.j, N);\n            new_occ[cell_idx].push_back(i);\n            pos = next;\n        }\n\n        current_route = new_route;\n        L0 = new_L;\n        for (int i = 0; i < total_cells; i++) {\n            freq[i] = new_occ[i].size();\n            if (freq[i] > 0) {\n                gap_est[i] = L0 * 1.0 / freq[i];\n            }\n        }\n        occ = new_occ;\n\n        priority_queue<pdi> new_pq;\n        for (int i = 0; i < total_cells; i++) {\n            if (freq[i] > 0) {\n                double new_priority = d[get_cell(i, N).i][get_cell(i, N).j] * gap_est[i] * gap_est[i];\n                new_pq.push({new_priority, i});\n            }\n        }\n        pq = new_pq;\n\n        insertion_count++;\n    }\n\n    cout << current_route << endl;\n\n    return 0;\n}","ahc028":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <unordered_set>\n#include <climits>\n#include <tuple>\n#include <deque>\n\nusing namespace std;\n\nconst int INF = 1e8;\nconst int N_grid = 15;\n\nvoid manhattan_transform(vector<vector<int>>& grid) {\n    int n = grid.size();\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            if (i > 0) grid[i][j] = min(grid[i][j], grid[i-1][j] + 1);\n            if (j > 0) grid[i][j] = min(grid[i][j], grid[i][j-1] + 1);\n        }\n    }\n    for (int i = n-1; i >= 0; i--) {\n        for (int j = n-1; j >= 0; j--) {\n            if (i+1 < n) grid[i][j] = min(grid[i][j], grid[i+1][j] + 1);\n            if (j+1 < n) grid[i][j] = min(grid[i][j], grid[i][j+1] + 1);\n        }\n    }\n}\n\nint compute_actual_cost(string& t, int cur_x, int cur_y, vector<vector<pair<int, int>>>& positions, int N) {\n    vector<vector<int>> dp_prev(N, vector<int>(N, INF));\n    for (auto [x, y] : positions[t[0]-'A']) {\n        dp_prev[x][y] = abs(cur_x - x) + abs(cur_y - y) + 1;\n    }\n\n    for (int idx = 1; idx < 5; idx++) {\n        vector<vector<int>> grid(N, vector<int>(N, INF));\n        for (auto [x, y] : positions[t[idx-1]-'A']) {\n            grid[x][y] = dp_prev[x][y];\n        }\n        manhattan_transform(grid);\n        vector<vector<int>> dp_curr(N, vector<int>(N, INF));\n        for (auto [x, y] : positions[t[idx]-'A']) {\n            dp_curr[x][y] = grid[x][y] + 1;\n        }\n        dp_prev = dp_curr;\n    }\n\n    int min_cost = INF;\n    for (auto [x, y] : positions[t[4]-'A']) {\n        if (dp_prev[x][y] < min_cost) {\n            min_cost = dp_prev[x][y];\n        }\n    }\n    return min_cost;\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M;\n    cin >> N >> M;\n    int s_i, s_j;\n    cin >> s_i >> s_j;\n\n    vector<string> grid_str(N);\n    for (int i = 0; i < N; i++) {\n        cin >> grid_str[i];\n    }\n\n    vector<string> targets;\n    for (int i = 0; i < M; i++) {\n        string t;\n        cin >> t;\n        targets.push_back(t);\n    }\n\n    unordered_set<string> target_set(targets.begin(), targets.end());\n    vector<vector<pair<int, int>>> positions(26);\n\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            char c = grid_str[i][j];\n            positions[c-'A'].push_back({i, j});\n        }\n    }\n\n    vector<vector<int>> d_table(26, vector<int>(26, INF));\n\n    for (int b = 0; b < 26; b++) {\n        vector<vector<int>> dist(N, vector<int>(N, INF));\n        for (auto [x, y] : positions[b]) {\n            dist[x][y] = 0;\n        }\n        manhattan_transform(dist);\n\n        for (int a = 0; a < 26; a++) {\n            int min_val = INF;\n            for (auto [x, y] : positions[a]) {\n                if (dist[x][y] < min_val) {\n                    min_val = dist[x][y];\n                }\n            }\n            d_table[a][b] = min_val;\n        }\n    }\n\n    string S = \"\";\n    int cur_x = s_i, cur_y = s_j;\n    vector<pair<int, int>> operations;\n    unordered_set<string> covered;\n\n    while (covered.size() < M && operations.size() < 5000) {\n        vector<int> min_dist_letter(26, INF);\n        for (int c = 0; c < 26; c++) {\n            for (auto [x, y] : positions[c]) {\n                int d = abs(cur_x - x) + abs(cur_y - y);\n                if (d < min_dist_letter[c]) {\n                    min_dist_letter[c] = d;\n                }\n            }\n        }\n\n        vector<tuple<double, string, int, double>> candidate_list;\n        for (string t : targets) {\n            if (covered.find(t) != covered.end()) continue;\n            int lb = min_dist_letter[t[0]-'A'] \n                     + d_table[t[0]-'A'][t[1]-'A'] \n                     + d_table[t[1]-'A'][t[2]-'A'] \n                     + d_table[t[2]-'A'][t[3]-'A'] \n                     + d_table[t[3]-'A'][t[4]-'A'] \n                     + 5;\n            string last_part = (S.length() >= 4) ? S.substr(S.length()-4) : S;\n            string candidate_string = last_part + t;\n            unordered_set<string> new_grams;\n            for (int i = 0; i <= (int)candidate_string.length()-5; i++) {\n                string gram = candidate_string.substr(i, 5);\n                if (target_set.find(gram) != target_set.end() && covered.find(gram) == covered.end()) {\n                    new_grams.insert(gram);\n                }\n            }\n            int new_count = new_grams.size();\n            if (new_count == 0) continue;\n            double score_lb = (double)lb / new_count;\n            candidate_list.push_back({score_lb, t, new_count, (double)lb});\n        }\n\n        if (candidate_list.empty()) {\n            break;\n        }\n\n        sort(candidate_list.begin(), candidate_list.end());\n        int K_candidate = min(50, (int)candidate_list.size());\n        vector<tuple<double, string, int, double>> top_candidates(candidate_list.begin(), candidate_list.begin() + K_candidate);\n\n        string best_string = \"\";\n        double best_score = 1e18;\n        int best_new_count = 0;\n\n        for (auto [score_lb, t, new_count, lb] : top_candidates) {\n            int actual_cost = compute_actual_cost(t, cur_x, cur_y, positions, N);\n            if (actual_cost == INF) continue;\n\n            double actual_score = (double)actual_cost / new_count;\n\n            string last_part = (S.length() >= 4) ? S.substr(S.length()-4) : S;\n            string candidate_string = last_part + t;\n            unordered_set<string> new_grams;\n            for (int i = 0; i <= (int)candidate_string.length()-5; i++) {\n                string gram = candidate_string.substr(i, 5);\n                if (target_set.find(gram) != target_set.end() && covered.find(gram) == covered.end()) {\n                    new_grams.insert(gram);\n                }\n            }\n\n            char last_char = t[4];\n            int min_next = INF;\n            for (const string& t_remain : targets) {\n                if (covered.find(t_remain) != covered.end()) continue;\n                if (new_grams.find(t_remain) != new_grams.end()) continue;\n                min_next = min(min_next, d_table[last_char-'A'][t_remain[0]-'A']);\n            }\n            if (min_next == INF) min_next = 0;\n\n            double adjusted_score = actual_score + 0.001 * min_next;\n\n            if (adjusted_score < best_score) {\n                best_score = adjusted_score;\n                best_string = t;\n                best_new_count = new_count;\n            }\n        }\n\n        if (best_string.empty()) {\n            break;\n        }\n\n        vector<vector<vector<int>>> dp(5, vector<vector<int>>(N, vector<int>(N, INF)));\n        vector<vector<vector<pair<int, int>>>> parent(5, vector<vector<pair<int, int>>>(N, vector<pair<int, int>>(N, {-1, -1})));\n\n        for (auto [x, y] : positions[best_string[0]-'A']) {\n            dp[0][x][y] = abs(cur_x - x) + abs(cur_y - y) + 1;\n        }\n\n        for (int k = 1; k < 5; k++) {\n            char prev_char = best_string[k-1];\n            char curr_char = best_string[k];\n            for (auto [x, y] : positions[curr_char-'A']) {\n                for (auto [px, py] : positions[prev_char-'A']) {\n                    if (dp[k-1][px][py] >= INF) continue;\n                    int move_cost = abs(x - px) + abs(y - py);\n                    int total_cost = dp[k-1][px][py] + move_cost + 1;\n                    if (total_cost < dp[k][x][y]) {\n                        dp[k][x][y] = total_cost;\n                        parent[k][x][y] = {px, py};\n                    }\n                }\n            }\n        }\n\n        int min_cost = INF;\n        pair<int, int> best_end = {-1, -1};\n        for (auto [x, y] : positions[best_string[4]-'A']) {\n            if (dp[4][x][y] < min_cost) {\n                min_cost = dp[4][x][y];\n                best_end = {x, y};\n            }\n        }\n\n        if (min_cost == INF) {\n            break;\n        }\n\n        vector<pair<int, int>> path(5);\n        int cx = best_end.first, cy = best_end.second;\n        path[4] = best_end;\n        for (int k = 3; k >= 0; k--) {\n            auto [px, py] = parent[k+1][cx][cy];\n            path[k] = {px, py};\n            cx = px;\n            cy = py;\n        }\n\n        for (int k = 0; k < 5; k++) {\n            int x = path[k].first;\n            int y = path[k].second;\n            operations.push_back({x, y});\n            S += grid_str[x][y];\n            cur_x = x;\n            cur_y = y;\n        }\n\n        int L = S.length();\n        int start_index = max(0, L - 9);\n        for (int i = start_index; i <= L - 5; i++) {\n            string gram = S.substr(i, 5);\n            if (target_set.find(gram) != target_set.end()) {\n                covered.insert(gram);\n            }\n        }\n\n        if (covered.size() >= M) break;\n        if (operations.size() >= 5000) break;\n    }\n\n    for (auto [x, y] : operations) {\n        cout << x << \" \" << y << \"\\n\";\n    }\n\n    return 0;\n}","ahc030":"#include <iostream>\n#include <vector>\n#include <cmath>\n#include <algorithm>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M;\n    double eps;\n    cin >> N >> M >> eps;\n\n    vector<vector<pair<int, int>>> fields;\n    for (int k = 0; k < M; k++) {\n        int d;\n        cin >> d;\n        vector<pair<int, int>> field;\n        for (int i = 0; i < d; i++) {\n            int x, y;\n            cin >> x >> y;\n            field.push_back({x, y});\n        }\n        fields.push_back(field);\n    }\n\n    const int BLOCK_SIZE = 5;\n    vector<pair<int, int>> oil_list;\n    int trials = (eps > 0.15) ? 1 : 2;\n\n    for (int i0 = 0; i0 < N; i0 += BLOCK_SIZE) {\n        for (int j0 = 0; j0 < N; j0 += BLOCK_SIZE) {\n            int i1 = min(i0 + BLOCK_SIZE, N);\n            int j1 = min(j0 + BLOCK_SIZE, N);\n            vector<pair<int, int>> block_coords;\n            for (int i = i0; i < i1; i++) {\n                for (int j = j0; j < j1; j++) {\n                    block_coords.push_back({i, j});\n                }\n            }\n            int k = block_coords.size();\n            bool found_oil = false;\n            \n            for (int t = 0; t < trials; t++) {\n                cout << \"q \" << k;\n                for (auto [i, j] : block_coords) {\n                    cout << \" \" << i << \" \" << j;\n                }\n                cout << endl;\n                int resp;\n                cin >> resp;\n                if (resp >= 1) {\n                    found_oil = true;\n                    break;\n                }\n            }\n            \n            if (found_oil) {\n                for (auto [i, j] : block_coords) {\n                    cout << \"q 1 \" << i << \" \" << j << endl;\n                    int v;\n                    cin >> v;\n                    if (v > 0) {\n                        oil_list.push_back({i, j});\n                    }\n                }\n            }\n        }\n    }\n\n    cout << \"a \" << oil_list.size();\n    for (auto [i, j] : oil_list) {\n        cout << \" \" << i << \" \" << j;\n    }\n    cout << endl;\n    int resp;\n    cin >> resp;\n\n    return 0;\n}","ahc031":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <set>\n#include <climits>\n#include <tuple>\n#include <cmath>\nusing namespace std;\n\nconst int ORIENTATION_HORIZONTAL = 0;\nconst int ORIENTATION_VERTICAL = 1;\nconst int ORIENTATION_GRID = 2;\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int W, D, N;\n    cin >> W >> D >> N;\n    vector<vector<int>> a(D, vector<int>(N));\n    for (int d = 0; d < D; ++d) {\n        for (int k = 0; k < N; ++k) {\n            cin >> a[d][k];\n        }\n    }\n\n    int prev_orientation = -1;\n    set<int> prev_boundaries;\n    vector<vector<tuple<int, int, int, int>>> all_placements(D);\n\n    for (int d = 0; d < D; ++d) {\n        vector<int> minH(N), minW(N);\n        for (int k = 0; k < N; ++k) {\n            minH[k] = (a[d][k] + W - 1) / W;\n            if (minH[k] == 0) minH[k] = 1;\n            minW[k] = minH[k];\n        }\n\n        long long total_minH = 0;\n        for (int k = 0; k < N; ++k) total_minH += minH[k];\n\n        long long cost_horiz = LLONG_MAX;\n        long long cost_vert = LLONG_MAX;\n        set<int> horiz_boundaries, vert_boundaries;\n        vector<tuple<int, int, int, int>> horiz_placements(N), vert_placements(N), grid_placements(N);\n\n        bool skip_horiz = (total_minH > W);\n        bool skip_vert = (total_minH > W);\n\n        if (!skip_horiz) {\n            if (N == 1) {\n                horiz_placements[0] = make_tuple(0, 0, W, W);\n                horiz_boundaries = set<int>();\n            } else {\n                vector<int> dp(W+1, -1);\n                vector<vector<int>> parent_arr;\n\n                for (int y = minH[0]; y <= W; ++y) {\n                    if (d == 0) {\n                        dp[y] = 0;\n                    } else {\n                        dp[y] = (prev_orientation == ORIENTATION_HORIZONTAL && prev_boundaries.find(y) != prev_boundaries.end()) ? 1 : 0;\n                    }\n                }\n\n                for (int k_index = 0; k_index <= N-3; ++k_index) {\n                    vector<int> new_dp(W+1, -1);\n                    vector<int> new_parent(W+1, -1);\n\n                    vector<int> best_val(W+1, -1);\n                    vector<int> best_arg(W+1, -1);\n                    best_val[0] = dp[0];\n                    best_arg[0] = 0;\n                    for (int z = 1; z <= W; ++z) {\n                        if (dp[z] > best_val[z-1]) {\n                            best_val[z] = dp[z];\n                            best_arg[z] = z;\n                        } else {\n                            best_val[z] = best_val[z-1];\n                            best_arg[z] = best_arg[z-1];\n                        }\n                    }\n\n                    int required = minH[k_index+1];\n                    for (int z = required; z <= W; ++z) {\n                        int prev_z = z - required;\n                        if (prev_z < 0 || best_val[prev_z] == -1) continue;\n                        int common = (d > 0 && prev_orientation == ORIENTATION_HORIZONTAL && prev_boundaries.find(z) != prev_boundaries.end()) ? 1 : 0;\n                        new_dp[z] = best_val[prev_z] + common;\n                        new_parent[z] = best_arg[prev_z];\n                    }\n\n                    dp = new_dp;\n                    parent_arr.push_back(new_parent);\n                }\n\n                int best_z = -1;\n                int best_val = -1;\n                for (int z = 0; z <= W; ++z) {\n                    if (dp[z] != -1 && (W - z) >= minH[N-1]) {\n                        if (dp[z] > best_val) {\n                            best_val = dp[z];\n                            best_z = z;\n                        }\n                    }\n                }\n\n                if (best_z == -1) {\n                    skip_horiz = true;\n                } else {\n                    vector<int> boundaries(N-1, 0);\n                    boundaries[N-2] = best_z;\n                    for (int idx = N-3; idx >= 0; --idx) {\n                        boundaries[idx] = parent_arr[idx][boundaries[idx+1]];\n                    }\n                    horiz_placements[0] = make_tuple(0, 0, boundaries[0], W);\n                    for (int k = 1; k < N-1; ++k) {\n                        horiz_placements[k] = make_tuple(boundaries[k-1], 0, boundaries[k], W);\n                    }\n                    horiz_placements[N-1] = make_tuple(boundaries[N-2], 0, W, W);\n                    for (int k = 0; k < N-1; ++k) {\n                        horiz_boundaries.insert(boundaries[k]);\n                    }\n                }\n            }\n        }\n\n        if (!skip_vert) {\n            if (N == 1) {\n                vert_placements[0] = make_tuple(0, 0, W, W);\n                vert_boundaries = set<int>();\n            } else {\n                vector<int> dp(W+1, -1);\n                vector<vector<int>> parent_arr;\n\n                for (int x = minW[0]; x <= W; ++x) {\n                    if (d == 0) {\n                        dp[x] = 0;\n                    } else {\n                        dp[x] = (prev_orientation == ORIENTATION_VERTICAL && prev_boundaries.find(x) != prev_boundaries.end()) ? 1 : 0;\n                    }\n                }\n\n                for (int k_index = 0; k_index <= N-3; ++k_index) {\n                    vector<int> new_dp(W+1, -1);\n                    vector<int> new_parent(W+1, -1);\n\n                    vector<int> best_val(W+1, -1);\n                    vector<int> best_arg(W+1, -1);\n                    best_val[0] = dp[0];\n                    best_arg[0] = 0;\n                    for (int z = 1; z <= W; ++z) {\n                        if (dp[z] > best_val[z-1]) {\n                            best_val[z] = dp[z];\n                            best_arg[z] = z;\n                        } else {\n                            best_val[z] = best_val[z-1];\n                            best_arg[z] = best_arg[z-1];\n                        }\n                    }\n\n                    int required = minW[k_index+1];\n                    for (int z = required; z <= W; ++z) {\n                        int prev_z = z - required;\n                        if (prev_z < 0 || best_val[prev_z] == -1) continue;\n                        int common = (d > 0 && prev_orientation == ORIENTATION_VERTICAL && prev_boundaries.find(z) != prev_boundaries.end()) ? 1 : 0;\n                        new_dp[z] = best_val[prev_z] + common;\n                        new_parent[z] = best_arg[prev_z];\n                    }\n\n                    dp = new_dp;\n                    parent_arr.push_back(new_parent);\n                }\n\n                int best_z = -1;\n                int best_val = -1;\n                for (int z = 0; z <= W; ++z) {\n                    if (dp[z] != -1 && (W - z) >= minW[N-1]) {\n                        if (dp[z] > best_val) {\n                            best_val = dp[z];\n                            best_z = z;\n                        }\n                    }\n                }\n\n                if (best_z == -1) {\n                    skip_vert = true;\n                } else {\n                    vector<int> boundaries(N-1, 0);\n                    boundaries[N-2] = best_z;\n                    for (int idx = N-3; idx >= 0; --idx) {\n                        boundaries[idx] = parent_arr[idx][boundaries[idx+1]];\n                    }\n                    vert_placements[0] = make_tuple(0, 0, W, boundaries[0]);\n                    for (int k = 1; k < N-1; ++k) {\n                        vert_placements[k] = make_tuple(0, boundaries[k-1], W, boundaries[k]);\n                    }\n                    vert_placements[N-1] = make_tuple(0, boundaries[N-2], W, W);\n                    for (int k = 0; k < N-1; ++k) {\n                        vert_boundaries.insert(boundaries[k]);\n                    }\n                }\n            }\n        }\n\n        if (!skip_horiz) {\n            long long reuse_count = 0;\n            if (d > 0) {\n                if (prev_orientation == ORIENTATION_HORIZONTAL) {\n                    for (int b : horiz_boundaries) {\n                        if (prev_boundaries.find(b) != prev_boundaries.end()) reuse_count++;\n                    }\n                    cost_horiz = (prev_boundaries.size() + horiz_boundaries.size() - 2 * reuse_count) * (long long)W;\n                } else {\n                    cost_horiz = ((long long)prev_boundaries.size() + (long long)horiz_boundaries.size()) * W;\n                }\n            } else {\n                cost_horiz = horiz_boundaries.size() * (long long)W;\n            }\n        }\n\n        if (!skip_vert) {\n            long long reuse_count = 0;\n            if (d > 0) {\n                if (prev_orientation == ORIENTATION_VERTICAL) {\n                    for (int b : vert_boundaries) {\n                        if (prev_boundaries.find(b) != prev_boundaries.end()) reuse_count++;\n                    }\n                    cost_vert = (prev_boundaries.size() + vert_boundaries.size() - 2 * reuse_count) * (long long)W;\n                } else {\n                    cost_vert = ((long long)prev_boundaries.size() + (long long)vert_boundaries.size()) * W;\n                }\n            } else {\n                cost_vert = vert_boundaries.size() * (long long)W;\n            }\n        }\n\n        if (skip_horiz && skip_vert) {\n            vector<int> skyline(W, 0);\n            vector<vector<bool>> used(W, vector<bool>(W, false));\n            vector<pair<int, int>> events;\n            for (int k = 0; k < N; ++k) {\n                events.push_back({a[d][k], k});\n            }\n            sort(events.begin(), events.end(), greater<pair<int, int>>());\n\n            for (const auto& event : events) {\n                int area = event.first;\n                int idx = event.second;\n                int w0 = ceil(sqrt(area));\n                int h0 = (area + w0 - 1) / w0;\n\n                bool placed = false;\n                for (int orientation = 0; orientation < 2; orientation++) {\n                    int w = w0, h = h0;\n                    if (orientation == 1) {\n                        swap(w, h);\n                    }\n                    if (w > W || h > W) continue;\n\n                    for (int start_col = 0; start_col <= W - w; start_col++) {\n                        int base = 0;\n                        for (int col = start_col; col < start_col + w; col++) {\n                            if (skyline[col] > base) {\n                                base = skyline[col];\n                            }\n                        }\n                        if (base + h > W) continue;\n\n                        bool ok = true;\n                        for (int r = base; r < base + h; r++) {\n                            for (int c = start_col; c < start_col + w; c++) {\n                                if (r < W && c < W) {\n                                    if (used[r][c]) {\n                                        ok = false;\n                                        break;\n                                    }\n                                } else {\n                                    ok = false;\n                                    break;\n                                }\n                            }\n                            if (!ok) break;\n                        }\n                        if (!ok) continue;\n\n                        for (int r = base; r < base + h; r++) {\n                            for (int c = start_col; c < start_col + w; c++) {\n                                if (r < W && c < W) {\n                                    used[r][c] = true;\n                                }\n                            }\n                        }\n                        for (int col = start_col; col < start_col + w; col++) {\n                            if (col < W) {\n                                skyline[col] = max(skyline[col], base + h);\n                            }\n                        }\n                        grid_placements[idx] = make_tuple(base, start_col, base + h, start_col + w);\n                        placed = true;\n                        break;\n                    }\n                    if (placed) break;\n                }\n\n                if (!placed) {\n                    int base = 0;\n                    for (int col = 0; col < W; col++) {\n                        if (skyline[col] > base) {\n                            base = skyline[col];\n                        }\n                    }\n                    int h_emerg = (area + W - 1) / W;\n                    if (base + h_emerg > W) {\n                        h_emerg = W - base;\n                    }\n                    if (h_emerg > 0) {\n                        int w_emerg = (area + h_emerg - 1) / h_emerg;\n                        w_emerg = min(w_emerg, W);\n                        bool placed_emerg = false;\n                        for (int start_col = 0; start_col <= W - w_emerg; start_col++) {\n                            bool ok = true;\n                            for (int r = base; r < base + h_emerg; r++) {\n                                if (r >= W) {\n                                    ok = false;\n                                    break;\n                                }\n                                for (int c = start_col; c < start_col + w_emerg; c++) {\n                                    if (c >= W) {\n                                        ok = false;\n                                        break;\n                                    }\n                                    if (used[r][c]) {\n                                        ok = false;\n                                        break;\n                                    }\n                                }\n                                if (!ok) break;\n                            }\n                            if (ok) {\n                                int current_h = h_emerg;\n                                int current_w = w_emerg;\n                                int current_area = current_h * current_w;\n                                \n                                while (current_area < area && start_col + current_w < W) {\n                                    bool free = true;\n                                    for (int r = base; r < base + current_h; r++) {\n                                        if (r >= W || start_col + current_w >= W) {\n                                            free = false;\n                                            break;\n                                        }\n                                        if (used[r][start_col + current_w]) {\n                                            free = false;\n                                            break;\n                                        }\n                                    }\n                                    if (!free) break;\n                                    current_w++;\n                                    current_area += current_h;\n                                }\n                                \n                                while (current_area < area && base + current_h < W) {\n                                    bool free = true;\n                                    for (int c = start_col; c < start_col + current_w; c++) {\n                                        if (c >= W || base + current_h >= W) {\n                                            free = false;\n                                            break;\n                                        }\n                                        if (used[base + current_h][c]) {\n                                            free = false;\n                                            break;\n                                        }\n                                    }\n                                    if (!free) break;\n                                    current_h++;\n                                    current_area += current_w;\n                                }\n                                \n                                for (int r = base; r < base + current_h; r++) {\n                                    for (int c = start_col; c < start_col + current_w; c++) {\n                                        if (r < W && c < W) {\n                                            used[r][c] = true;\n                                        }\n                                    }\n                                }\n                                for (int col = start_col; col < start_col + current_w; col++) {\n                                    if (col < W) {\n                                        skyline[col] = max(skyline[col], base + current_h);\n                                    }\n                                }\n                                grid_placements[idx] = make_tuple(base, start_col, base + current_h, start_col + current_w);\n                                placed_emerg = true;\n                                break;\n                            }\n                        }\n                        if (!placed_emerg) {\n                            bool found = false;\n                            for (int r = 0; r < W; r++) {\n                                for (int c = 0; c < W; c++) {\n                                    if (!used[r][c]) {\n                                        int current_h = 1;\n                                        int current_w = 1;\n                                        int current_area = 1;\n                                        \n                                        while (current_area < area && c + current_w < W) {\n                                            bool free = true;\n                                            for (int i = r; i < r + current_h; i++) {\n                                                if (i >= W || c + current_w >= W) {\n                                                    free = false;\n                                                    break;\n                                                }\n                                                if (used[i][c + current_w]) {\n                                                    free = false;\n                                                    break;\n                                                }\n                                            }\n                                            if (!free) break;\n                                            current_w++;\n                                            current_area += current_h;\n                                        }\n                                        \n                                        while (current_area < area && r + current_h < W) {\n                                            bool free = true;\n                                            for (int j = c; j < c + current_w; j++) {\n                                                if (j >= W || r + current_h >= W) {\n                                                    free = false;\n                                                    break;\n                                                }\n                                                if (used[r + current_h][j]) {\n                                                    free = false;\n                                                    break;\n                                                }\n                                            }\n                                            if (!free) break;\n                                            current_h++;\n                                            current_area += current_w;\n                                        }\n                                        \n                                        for (int i = r; i < r + current_h; i++) {\n                                            for (int j = c; j < c + current_w; j++) {\n                                                if (i < W && j < W) {\n                                                    used[i][j] = true;\n                                                }\n                                            }\n                                        }\n                                        for (int j = c; j < c + current_w; j++) {\n                                            if (j < W) {\n                                                skyline[j] = max(skyline[j], r + current_h);\n                                            }\n                                        }\n                                        grid_placements[idx] = make_tuple(r, c, r + current_h, c + current_w);\n                                        found = true;\n                                        break;\n                                    }\n                                }\n                                if (found) break;\n                            }\n                            if (!found) {\n                                grid_placements[idx] = make_tuple(0, 0, 1, 1);\n                            }\n                        }\n                    } else {\n                        bool found = false;\n                        for (int r = 0; r < W; r++) {\n                            for (int c = 0; c < W; c++) {\n                                if (!used[r][c]) {\n                                    int current_h = 1;\n                                    int current_w = 1;\n                                    int current_area = 1;\n                                    \n                                    while (current_area < area && c + current_w < W) {\n                                        bool free = true;\n                                        for (int i = r; i < r + current_h; i++) {\n                                            if (i >= W || c + current_w >= W) {\n                                                free = false;\n                                                break;\n                                            }\n                                            if (used[i][c + current_w]) {\n                                                free = false;\n                                                break;\n                                            }\n                                        }\n                                        if (!free) break;\n                                        current_w++;\n                                        current_area += current_h;\n                                    }\n                                    \n                                    while (current_area < area && r + current_h < W) {\n                                        bool free = true;\n                                        for (int j = c; j < c + current_w; j++) {\n                                            if (j >= W || r + current_h >= W) {\n                                                free = false;\n                                                break;\n                                            }\n                                            if (used[r + current_h][j]) {\n                                                free = false;\n                                                break;\n                                            }\n                                        }\n                                        if (!free) break;\n                                        current_h++;\n                                        current_area += current_w;\n                                    }\n                                    \n                                    for (int i = r; i < r + current_h; i++) {\n                                        for (int j = c; j < c + current_w; j++) {\n                                            if (i < W && j < W) {\n                                                used[i][j] = true;\n                                            }\n                                        }\n                                    }\n                                    for (int j = c; j < c + current_w; j++) {\n                                        if (j < W) {\n                                            skyline[j] = max(skyline[j], r + current_h);\n                                        }\n                                    }\n                                    grid_placements[idx] = make_tuple(r, c, r + current_h, c + current_w);\n                                    found = true;\n                                    break;\n                                }\n                            }\n                            if (found) break;\n                        }\n                        if (!found) {\n                            grid_placements[idx] = make_tuple(0, 0, 1, 1);\n                        }\n                    }\n                }\n            }\n\n            all_placements[d] = grid_placements;\n            prev_orientation = ORIENTATION_GRID;\n            prev_boundaries.clear();\n        } else {\n            if (cost_horiz <= cost_vert) {\n                all_placements[d] = horiz_placements;\n                prev_orientation = ORIENTATION_HORIZONTAL;\n                prev_boundaries = horiz_boundaries;\n            } else {\n                all_placements[d] = vert_placements;\n                prev_orientation = ORIENTATION_VERTICAL;\n                prev_boundaries = vert_boundaries;\n            }\n        }\n    }\n\n    for (int d = 0; d < D; ++d) {\n        for (int k = 0; k < N; ++k) {\n            auto [i0, j0, i1, j1] = all_placements[d][k];\n            i1 = min(i1, W);\n            j1 = min(j1, W);\n            if (i0 >= i1) i1 = i0 + 1;\n            if (j0 >= j1) j1 = j0 + 1;\n            cout << i0 << \" \" << j0 << \" \" << i1 << \" \" << j1 << \"\\n\";\n        }\n    }\n\n    return 0;\n}","ahc032":"#include <iostream>\n#include <vector>\n#include <tuple>\n#include <algorithm>\n#include <climits>\n#include <set>\n#include <cmath>\nusing namespace std;\n\nconst long long mod = 998244353;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, K;\n    cin >> N >> M >> K;\n\n    vector<vector<long long>> grid(N, vector<long long>(N));\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cin >> grid[i][j];\n        }\n    }\n\n    vector<vector<vector<long long>>> stamps(M, vector<vector<long long>>(3, vector<long long>(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 >> stamps[m][i][j];\n            }\n        }\n    }\n\n    vector<tuple<int, int, int>> operations;\n    long long total_score = 0;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            total_score += grid[i][j];\n        }\n    }\n\n    while (operations.size() < K) {\n        long long best_delta = LLONG_MIN;\n        int best_m = -1, best_p = -1, best_q = -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                    long long delta = 0;\n                    for (int i = 0; i < 3; i++) {\n                        for (int j = 0; j < 3; j++) {\n                            long long old_val = grid[p+i][q+j];\n                            long long add = stamps[m][i][j];\n                            long long new_val = old_val + add;\n                            if (new_val >= mod) {\n                                new_val -= mod;\n                            }\n                            delta += (new_val - old_val);\n                        }\n                    }\n                    if (delta > best_delta) {\n                        best_delta = delta;\n                        best_m = m;\n                        best_p = p;\n                        best_q = q;\n                    }\n                }\n            }\n        }\n\n        if (best_delta <= 0) {\n            break;\n        }\n\n        for (int i = 0; i < 3; i++) {\n            for (int j = 0; j < 3; j++) {\n                long long old_val = grid[best_p+i][best_q+j];\n                long long new_val = old_val + stamps[best_m][i][j];\n                if (new_val >= mod) {\n                    new_val -= mod;\n                }\n                grid[best_p+i][best_q+j] = new_val;\n                total_score += (new_val - old_val);\n            }\n        }\n\n        operations.push_back(make_tuple(best_m, best_p, best_q));\n    }\n\n    int passes = 0;\n    const int MAX_PASSES = 10;\n    while (passes < MAX_PASSES) {\n        bool improved = false;\n        vector<tuple<int, int, int>> current_ops = operations;\n        vector<long long> removal_effects(current_ops.size());\n        vector<int> indices(current_ops.size());\n        bool found_positive = false;\n        int positive_index = -1;\n        for (int idx = 0; idx < current_ops.size(); idx++) {\n            auto [m, p, q] = current_ops[idx];\n            removal_effects[idx] = 0;\n            for (int i = 0; i < 3; i++) {\n                for (int j = 0; j < 3; j++) {\n                    long long old_val = grid[p+i][q+j];\n                    long long s_val = stamps[m][i][j];\n                    if (old_val < s_val) {\n                        removal_effects[idx] += (mod - s_val);\n                    } else {\n                        removal_effects[idx] -= s_val;\n                    }\n                }\n            }\n            if (removal_effects[idx] > 0) {\n                found_positive = true;\n                positive_index = idx;\n                break;\n            }\n            indices[idx] = idx;\n        }\n\n        if (found_positive) {\n            int idx = positive_index;\n            auto [m_remove, p_remove, q_remove] = current_ops[idx];\n            for (int i = 0; i < 3; i++) {\n                for (int j = 0; j < 3; j++) {\n                    long long old_val = grid[p_remove+i][q_remove+j];\n                    long long s_val = stamps[m_remove][i][j];\n                    long long new_val = old_val - s_val;\n                    if (new_val < 0) {\n                        new_val += mod;\n                    }\n                    grid[p_remove+i][q_remove+j] = new_val;\n                    total_score += (new_val - old_val);\n                }\n            }\n            operations.erase(operations.begin() + idx);\n            improved = true;\n        } else {\n            sort(indices.begin(), indices.end(), [&](int a, int b) {\n                return removal_effects[a] > removal_effects[b];\n            });\n\n            bool one_removed = false;\n            for (int idx : indices) {\n                vector<vector<long long>> grid_backup = grid;\n                long long total_score_backup = total_score;\n\n                auto [m_remove, p_remove, q_remove] = current_ops[idx];\n                for (int i = 0; i < 3; i++) {\n                    for (int j = 0; j < 3; j++) {\n                        long long old_val = grid[p_remove+i][q_remove+j];\n                        long long s_val = stamps[m_remove][i][j];\n                        long long new_val = old_val - s_val;\n                        if (new_val < 0) {\n                            new_val += mod;\n                        }\n                        grid[p_remove+i][q_remove+j] = new_val;\n                        total_score += (new_val - old_val);\n                    }\n                }\n\n                vector<tuple<int, int, int>> new_ops;\n                int max_add = K - (current_ops.size() - 1);\n                for (int op_count = 0; op_count < max_add; op_count++) {\n                    long long best_delta = LLONG_MIN;\n                    int best_m = -1, best_p = -1, best_q = -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                                long long delta = 0;\n                                for (int i = 0; i < 3; i++) {\n                                    for (int j = 0; j < 3; j++) {\n                                        long long old_val = grid[p+i][q+j];\n                                        long long add = stamps[m][i][j];\n                                        long long new_val = old_val + add;\n                                        if (new_val >= mod) {\n                                            new_val -= mod;\n                                        }\n                                        delta += (new_val - old_val);\n                                    }\n                                }\n                                if (delta > best_delta) {\n                                    best_delta = delta;\n                                    best_m = m;\n                                    best_p = p;\n                                    best_q = q;\n                                }\n                            }\n                        }\n                    }\n\n                    if (best_delta <= 0) {\n                        break;\n                    }\n\n                    for (int i = 0; i < 3; i++) {\n                        for (int j = 0; j < 3; j++) {\n                            long long old_val = grid[best_p+i][best_q+j];\n                            long long new_val = old_val + stamps[best_m][i][j];\n                            if (new_val >= mod) {\n                                new_val -= mod;\n                            }\n                            grid[best_p+i][best_q+j] = new_val;\n                            total_score += (new_val - old_val);\n                        }\n                    }\n                    new_ops.push_back(make_tuple(best_m, best_p, best_q));\n                }\n\n                if (total_score > total_score_backup) {\n                    operations = current_ops;\n                    operations.erase(operations.begin() + idx);\n                    operations.insert(operations.end(), new_ops.begin(), new_ops.end());\n                    improved = true;\n                    one_removed = true;\n                    break;\n                } else {\n                    grid = grid_backup;\n                    total_score = total_score_backup;\n                }\n            }\n\n            if (!one_removed) {\n                const int TOP_SIZE = 10;\n                vector<int> top_indices;\n                for (int i = 0; i < min(TOP_SIZE, (int)indices.size()); i++) {\n                    top_indices.push_back(indices[i]);\n                }\n                bool two_improved = false;\n                for (int i = 0; i < top_indices.size(); i++) {\n                    for (int j = i+1; j < top_indices.size(); j++) {\n                        int idx1 = top_indices[i];\n                        int idx2 = top_indices[j];\n                        vector<vector<long long>> grid_backup = grid;\n                        long long total_score_backup = total_score;\n\n                        vector<int> rm_indices = {idx1, idx2};\n                        sort(rm_indices.rbegin(), rm_indices.rend());\n                        for (int idx : rm_indices) {\n                            auto [m_remove, p_remove, q_remove] = current_ops[idx];\n                            for (int dx = 0; dx < 3; dx++) {\n                                for (int dy = 0; dy < 3; dy++) {\n                                    long long old_val = grid[p_remove+dx][q_remove+dy];\n                                    long long s_val = stamps[m_remove][dx][dy];\n                                    long long new_val = old_val - s_val;\n                                    if (new_val < 0) new_val += mod;\n                                    grid[p_remove+dx][q_remove+dy] = new_val;\n                                    total_score += (new_val - old_val);\n                                }\n                            }\n                        }\n\n                        vector<tuple<int, int, int>> new_ops;\n                        int max_add = K - (current_ops.size() - 2);\n                        for (int op_count = 0; op_count < max_add; op_count++) {\n                            long long best_delta = LLONG_MIN;\n                            int best_m = -1, best_p = -1, best_q = -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                                        long long delta = 0;\n                                        for (int dx = 0; dx < 3; dx++) {\n                                            for (int dy = 0; dy < 3; dy++) {\n                                                long long old_val = grid[p+dx][q+dy];\n                                                long long add = stamps[m][dx][dy];\n                                                long long new_val = old_val + add;\n                                                if (new_val >= mod) new_val -= mod;\n                                                delta += (new_val - old_val);\n                                            }\n                                        }\n                                        if (delta > best_delta) {\n                                            best_delta = delta;\n                                            best_m = m;\n                                            best_p = p;\n                                            best_q = q;\n                                        }\n                                    }\n                                }\n                            }\n\n                            if (best_delta <= 0) {\n                                break;\n                            }\n\n                            for (int dx = 0; dx < 3; dx++) {\n                                for (int dy = 0; dy < 3; dy++) {\n                                    long long old_val = grid[best_p+dx][best_q+dy];\n                                    long long new_val = old_val + stamps[best_m][dx][dy];\n                                    if (new_val >= mod) new_val -= mod;\n                                    grid[best_p+dx][best_q+dy] = new_val;\n                                    total_score += (new_val - old_val);\n                                }\n                            }\n                            new_ops.push_back(make_tuple(best_m, best_p, best_q));\n                        }\n\n                        if (total_score > total_score_backup) {\n                            operations = current_ops;\n                            if (rm_indices[0] > rm_indices[1]) {\n                                operations.erase(operations.begin() + rm_indices[0]);\n                                operations.erase(operations.begin() + rm_indices[1]);\n                            } else {\n                                operations.erase(operations.begin() + rm_indices[1]);\n                                operations.erase(operations.begin() + rm_indices[0]);\n                            }\n                            operations.insert(operations.end(), new_ops.begin(), new_ops.end());\n                            improved = true;\n                            two_improved = true;\n                            break;\n                        } else {\n                            grid = grid_backup;\n                            total_score = total_score_backup;\n                        }\n                    }\n                    if (two_improved) break;\n                }\n\n                if (!two_improved) {\n                    bool three_improved = false;\n                    for (int i = 0; i < top_indices.size(); i++) {\n                        for (int j = i+1; j < top_indices.size(); j++) {\n                            for (int k = j+1; k < top_indices.size(); k++) {\n                                int idx1 = top_indices[i];\n                                int idx2 = top_indices[j];\n                                int idx3 = top_indices[k];\n                                vector<vector<long long>> grid_backup = grid;\n                                long long total_score_backup = total_score;\n\n                                vector<int> rm_indices = {idx1, idx2, idx3};\n                                sort(rm_indices.rbegin(), rm_indices.rend());\n                                for (int idx : rm_indices) {\n                                    auto [m_remove, p_remove, q_remove] = current_ops[idx];\n                                    for (int dx = 0; dx < 3; dx++) {\n                                        for (int dy = 0; dy < 3; dy++) {\n                                            long long old_val = grid[p_remove+dx][q_remove+dy];\n                                            long long s_val = stamps[m_remove][dx][dy];\n                                            long long new_val = old_val - s_val;\n                                            if (new_val < 0) new_val += mod;\n                                            grid[p_remove+dx][q_remove+dy] = new_val;\n                                            total_score += (new_val - old_val);\n                                        }\n                                    }\n                                }\n\n                                vector<tuple<int, int, int>> new_ops;\n                                int max_add = K - (current_ops.size() - 3);\n                                for (int op_count = 0; op_count < max_add; op_count++) {\n                                    long long best_delta = LLONG_MIN;\n                                    int best_m = -1, best_p = -1, best_q = -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                                                long long delta = 0;\n                                                for (int dx = 0; dx < 3; dx++) {\n                                                    for (int dy = 0; dy < 3; dy++) {\n                                                        long long old_val = grid[p+dx][q+dy];\n                                                        long long add = stamps[m][dx][dy];\n                                                        long long new_val = old_val + add;\n                                                        if (new_val >= mod) new_val -= mod;\n                                                        delta += (new_val - old_val);\n                                                    }\n                                                }\n                                                if (delta > best_delta) {\n                                                    best_delta = delta;\n                                                    best_m = m;\n                                                    best_p = p;\n                                                    best_q = q;\n                                                }\n                                            }\n                                        }\n                                    }\n\n                                    if (best_delta <= 0) {\n                                        break;\n                                    }\n\n                                    for (int dx = 0; dx < 3; dx++) {\n                                        for (int dy = 0; dy < 3; dy++) {\n                                            long long old_val = grid[best_p+dx][best_q+dy];\n                                            long long new_val = old_val + stamps[best_m][dx][dy];\n                                            if (new_val >= mod) new_val -= mod;\n                                            grid[best_p+dx][best_q+dy] = new_val;\n                                            total_score += (new_val - old_val);\n                                        }\n                                    }\n                                    new_ops.push_back(make_tuple(best_m, best_p, best_q));\n                                }\n\n                                if (total_score > total_score_backup) {\n                                    operations = current_ops;\n                                    sort(rm_indices.rbegin(), rm_indices.rend());\n                                    for (int idx : rm_indices) {\n                                        operations.erase(operations.begin() + idx);\n                                    }\n                                    operations.insert(operations.end(), new_ops.begin(), new_ops.end());\n                                    improved = true;\n                                    three_improved = true;\n                                    goto after_triple;\n                                } else {\n                                    grid = grid_backup;\n                                    total_score = total_score_backup;\n                                }\n                            }\n                        }\n                    }\n                    after_triple:\n                    if (!three_improved) {\n                        bool four_improved = false;\n                        for (int i = 0; i < top_indices.size(); i++) {\n                            for (int j = i+1; j < top_indices.size(); j++) {\n                                for (int k = j+1; k < top_indices.size(); k++) {\n                                    for (int l = k+1; l < top_indices.size(); l++) {\n                                        int idx1 = top_indices[i];\n                                        int idx2 = top_indices[j];\n                                        int idx3 = top_indices[k];\n                                        int idx4 = top_indices[l];\n                                        vector<vector<long long>> grid_backup = grid;\n                                        long long total_score_backup = total_score;\n\n                                        vector<int> rm_indices = {idx1, idx2, idx3, idx4};\n                                        sort(rm_indices.rbegin(), rm_indices.rend());\n                                        for (int idx : rm_indices) {\n                                            auto [m_remove, p_remove, q_remove] = current_ops[idx];\n                                            for (int dx = 0; dx < 3; dx++) {\n                                                for (int dy = 0; dy < 3; dy++) {\n                                                    long long old_val = grid[p_remove+dx][q_remove+dy];\n                                                    long long s_val = stamps[m_remove][dx][dy];\n                                                    long long new_val = old_val - s_val;\n                                                    if (new_val < 0) new_val += mod;\n                                                    grid[p_remove+dx][q_remove+dy] = new_val;\n                                                    total_score += (new_val - old_val);\n                                                }\n                                            }\n                                        }\n\n                                        vector<tuple<int, int, int>> new_ops;\n                                        int max_add = K - (current_ops.size() - 4);\n                                        for (int op_count = 0; op_count < max_add; op_count++) {\n                                            long long best_delta = LLONG_MIN;\n                                            int best_m = -1, best_p = -1, best_q = -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                                                        long long delta = 0;\n                                                        for (int dx = 0; dx < 3; dx++) {\n                                                            for (int dy = 0; dy < 3; dy++) {\n                                                                long long old_val = grid[p+dx][q+dy];\n                                                                long long add = stamps[m][dx][dy];\n                                                                long long new_val = old_val + add;\n                                                                if (new_val >= mod) new_val -= mod;\n                                                                delta += (new_val - old_val);\n                                                            }\n                                                        }\n                                                        if (delta > best_delta) {\n                                                            best_delta = delta;\n                                                            best_m = m;\n                                                            best_p = p;\n                                                            best_q = q;\n                                                        }\n                                                    }\n                                                }\n                                            }\n\n                                            if (best_delta <= 0) {\n                                                break;\n                                            }\n\n                                            for (int dx = 0; dx < 3; dx++) {\n                                                for (int dy = 0; dy < 3; dy++) {\n                                                    long long old_val = grid[best_p+dx][best_q+dy];\n                                                    long long new_val = old_val + stamps[best_m][dx][dy];\n                                                    if (new_val >= mod) new_val -= mod;\n                                                    grid[best_p+dx][best_q+dy] = new_val;\n                                                    total_score += (new_val - old_val);\n                                                }\n                                            }\n                                            new_ops.push_back(make_tuple(best_m, best_p, best_q));\n                                        }\n\n                                        if (total_score > total_score_backup) {\n                                            operations = current_ops;\n                                            sort(rm_indices.rbegin(), rm_indices.rend());\n                                            for (int idx : rm_indices) {\n                                                operations.erase(operations.begin() + idx);\n                                            }\n                                            operations.insert(operations.end(), new_ops.begin(), new_ops.end());\n                                            improved = true;\n                                            four_improved = true;\n                                            goto after_quad;\n                                        } else {\n                                            grid = grid_backup;\n                                            total_score = total_score_backup;\n                                        }\n                                    }\n                                }\n                            }\n                        }\n                        after_quad:\n                        if (!four_improved) {\n                            bool five_improved = false;\n                            for (int i = 0; i < top_indices.size(); i++) {\n                                for (int j = i+1; j < top_indices.size(); j++) {\n                                    for (int k = j+1; k < top_indices.size(); k++) {\n                                        for (int l = k+1; l < top_indices.size(); l++) {\n                                            for (int n = l+1; n < top_indices.size(); n++) {\n                                                int idx1 = top_indices[i];\n                                                int idx2 = top_indices[j];\n                                                int idx3 = top_indices[k];\n                                                int idx4 = top_indices[l];\n                                                int idx5 = top_indices[n];\n                                                vector<vector<long long>> grid_backup = grid;\n                                                long long total_score_backup = total_score;\n\n                                                vector<int> rm_indices = {idx1, idx2, idx3, idx4, idx5};\n                                                sort(rm_indices.rbegin(), rm_indices.rend());\n                                                for (int idx : rm_indices) {\n                                                    auto [m_remove, p_remove, q_remove] = current_ops[idx];\n                                                    for (int dx = 0; dx < 3; dx++) {\n                                                        for (int dy = 0; dy < 3; dy++) {\n                                                            long long old_val = grid[p_remove+dx][q_remove+dy];\n                                                            long long s_val = stamps[m_remove][dx][dy];\n                                                            long long new_val = old_val - s_val;\n                                                            if (new_val < 0) new_val += mod;\n                                                            grid[p_remove+dx][q_remove+dy] = new_val;\n                                                            total_score += (new_val - old_val);\n                                                        }\n                                                    }\n                                                }\n\n                                                vector<tuple<int, int, int>> new_ops;\n                                                int max_add = K - (current_ops.size() - 5);\n                                                for (int op_count = 0; op_count < max_add; op_count++) {\n                                                    long long best_delta = LLONG_MIN;\n                                                    int best_m = -1, best_p = -1, best_q = -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                                                                long long delta = 0;\n                                                                for (int dx = 0; dx < 3; dx++) {\n                                                                    for (int dy = 0; dy < 3; dy++) {\n                                                                        long long old_val = grid[p+dx][q+dy];\n                                                                        long long add = stamps[m][dx][dy];\n                                                                        long long new_val = old_val + add;\n                                                                        if (new_val >= mod) new_val -= mod;\n                                                                        delta += (new_val - old_val);\n                                                                    }\n                                                                }\n                                                                if (delta > best_delta) {\n                                                                    best_delta = delta;\n                                                                    best_m = m;\n                                                                    best_p = p;\n                                                                    best_q = q;\n                                                                }\n                                                            }\n                                                        }\n                                                    }\n\n                                                    if (best_delta <= 0) {\n                                                        break;\n                                                    }\n\n                                                    for (int dx = 0; dx < 3; dx++) {\n                                                        for (int dy = 0; dy < 3; dy++) {\n                                                            long long old_val = grid[best_p+dx][best_q+dy];\n                                                            long long new_val = old_val + stamps[best_m][dx][dy];\n                                                            if (new_val >= mod) new_val -= mod;\n                                                            grid[best_p+dx][best_q+dy] = new_val;\n                                                            total_score += (new_val - old_val);\n                                                        }\n                                                    }\n                                                    new_ops.push_back(make_tuple(best_m, best_p, best_q));\n                                                }\n\n                                                if (total_score > total_score_backup) {\n                                                    operations = current_ops;\n                                                    sort(rm_indices.rbegin(), rm_indices.rend());\n                                                    for (int idx : rm_indices) {\n                                                        operations.erase(operations.begin() + idx);\n                                                    }\n                                                    operations.insert(operations.end(), new_ops.begin(), new_ops.end());\n                                                    improved = true;\n                                                    five_improved = true;\n                                                    goto after_five;\n                                                } else {\n                                                    grid = grid_backup;\n                                                    total_score = total_score_backup;\n                                                }\n                                            }\n                                        }\n                                    }\n                                }\n                            }\n                            after_five:\n                            if (!five_improved) {\n                                bool six_improved = false;\n                                for (int i = 0; i < top_indices.size(); i++) {\n                                    for (int j = i+1; j < top_indices.size(); j++) {\n                                        for (int k = j+1; k < top_indices.size(); k++) {\n                                            for (int l = k+1; l < top_indices.size(); l++) {\n                                                for (int n = l+1; n < top_indices.size(); n++) {\n                                                    for (int o = n+1; o < top_indices.size(); o++) {\n                                                        int idx1 = top_indices[i];\n                                                        int idx2 = top_indices[j];\n                                                        int idx3 = top_indices[k];\n                                                        int idx4 = top_indices[l];\n                                                        int idx5 = top_indices[n];\n                                                        int idx6 = top_indices[o];\n                                                        vector<vector<long long>> grid_backup = grid;\n                                                        long long total_score_backup = total_score;\n\n                                                        vector<int> rm_indices = {idx1, idx2, idx3, idx4, idx5, idx6};\n                                                        sort(rm_indices.rbegin(), rm_indices.rend());\n                                                        for (int idx : rm_indices) {\n                                                            auto [m_remove, p_remove, q_remove] = current_ops[idx];\n                                                            for (int dx = 0; dx < 3; dx++) {\n                                                                for (int dy = 0; dy < 3; dy++) {\n                                                                    long long old_val = grid[p_remove+dx][q_remove+dy];\n                                                                    long long s_val = stamps[m_remove][dx][dy];\n                                                                    long long new_val = old_val - s_val;\n                                                                    if (new_val < 0) new_val += mod;\n                                                                    grid[p_remove+dx][q_remove+dy] = new_val;\n                                                                    total_score += (new_val - old_val);\n                                                                }\n                                                            }\n                                                        }\n\n                                                        vector<tuple<int, int, int>> new_ops;\n                                                        int max_add = K - (current_ops.size() - 6);\n                                                        for (int op_count = 0; op_count < max_add; op_count++) {\n                                                            long long best_delta = LLONG_MIN;\n                                                            int best_m = -1, best_p = -1, best_q = -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                                                                        long long delta = 0;\n                                                                        for (int dx = 0; dx < 3; dx++) {\n                                                                            for (int dy = 0; dy < 3; dy++) {\n                                                                                long long old_val = grid[p+dx][q+dy];\n                                                                                long long add = stamps[m][dx][dy];\n                                                                                long long new_val = old_val + add;\n                                                                                if (new_val >= mod) new_val -= mod;\n                                                                                delta += (new_val - old_val);\n                                                                            }\n                                                                        }\n                                                                        if (delta > best_delta) {\n                                                                            best_delta = delta;\n                                                                            best_m = m;\n                                                                            best_p = p;\n                                                                            best_q = q;\n                                                                        }\n                                                                    }\n                                                                }\n                                                            }\n\n                                                            if (best_delta <= 0) {\n                                                                break;\n                                                            }\n\n                                                            for (int dx = 0; dx < 3; dx++) {\n                                                                for (int dy = 0; dy < 3; dy++) {\n                                                                    long long old_val = grid[best_p+dx][best_q+dy];\n                                                                    long long new_val = old_val + stamps[best_m][dx][dy];\n                                                                    if (new_val >= mod) new_val -= mod;\n                                                                    grid[best_p+dx][best_q+dy] = new_val;\n                                                                    total_score += (new_val - old_val);\n                                                                }\n                                                            }\n                                                            new_ops.push_back(make_tuple(best_m, best_p, best_q));\n                                                        }\n\n                                                        if (total_score > total_score_backup) {\n                                                            operations = current_ops;\n                                                            sort(rm_indices.rbegin(), rm_indices.rend());\n                                                            for (int idx : rm_indices) {\n                                                                operations.erase(operations.begin() + idx);\n                                                            }\n                                                            operations.insert(operations.end(), new_ops.begin(), new_ops.end());\n                                                            improved = true;\n                                                            six_improved = true;\n                                                            goto after_six;\n                                                        } else {\n                                                            grid = grid_backup;\n                                                            total_score = total_score_backup;\n                                                        }\n                                                    }\n                                                }\n                                            }\n                                        }\n                                    }\n                                }\n                                after_six:;\n                            }\n                        }\n                    }\n                }\n            }\n        }\n\n        if (!improved) {\n            break;\n        }\n        passes++;\n    }\n\n    int swap_passes = 0;\n    const int MAX_SWAP_PASSES = 20;\n    while (swap_passes < MAX_SWAP_PASSES) {\n        long long best_improve = 0;\n        int best_i = -1;\n        int best_m = -1, best_p = -1, best_q = -1;\n\n        for (int idx = 0; idx < operations.size(); idx++) {\n            auto [m_i, p_i, q_i] = operations[idx];\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                        set<pair<int, int>> cells;\n                        for (int dx = 0; dx < 3; dx++) {\n                            for (int dy = 0; dy < 3; dy++) {\n                                cells.insert({p_i + dx, q_i + dy});\n                                cells.insert({p + dx, q + dy});\n                            }\n                        }\n                        long long net_effect = 0;\n                        for (auto& cell : cells) {\n                            int x = cell.first;\n                            int y = cell.second;\n                            long long v0 = grid[x][y];\n                            long long temp = v0;\n                            if (x >= p_i && x < p_i+3 && y >= q_i && y < q_i+3) {\n                                int dx_i = x - p_i;\n                                int dy_i = y - q_i;\n                                temp -= stamps[m_i][dx_i][dy_i];\n                            }\n                            if (x >= p && x < p+3 && y >= q && y < q+3) {\n                                int dx_n = x - p;\n                                int dy_n = y - q;\n                                temp += stamps[m][dx_n][dy_n];\n                            }\n                            long long new_val = temp % mod;\n                            if (new_val < 0) new_val += mod;\n                            net_effect += (new_val - v0);\n                        }\n                        if (net_effect > best_improve) {\n                            best_improve = net_effect;\n                            best_i = idx;\n                            best_m = m;\n                            best_p = p;\n                            best_q = q;\n                        }\n                    }\n                }\n            }\n        }\n\n        if (best_improve > 0) {\n            auto [m_remove, p_remove, q_remove] = operations[best_i];\n            for (int dx = 0; dx < 3; dx++) {\n                for (int dy = 0; dy < 3; dy++) {\n                    int x = p_remove + dx;\n                    int y = q_remove + dy;\n                    long long old_val = grid[x][y];\n                    long long new_val = old_val - stamps[m_remove][dx][dy];\n                    if (new_val < 0) new_val += mod;\n                    grid[x][y] = new_val;\n                    total_score += (new_val - old_val);\n                }\n            }\n            for (int dx = 0; dx < 3; dx++) {\n                for (int dy = 0; dy < 3; dy++) {\n                    int x = best_p + dx;\n                    int y = best_q + dy;\n                    long long old_val = grid[x][y];\n                    long long new_val = old_val + stamps[best_m][dx][dy];\n                    if (new_val >= mod) new_val -= mod;\n                    grid[x][y] = new_val;\n                    total_score += (new_val - old_val);\n                }\n            }\n            operations[best_i] = make_tuple(best_m, best_p, best_q);\n            swap_passes++;\n        } else {\n            break;\n        }\n    }\n\n    cout << operations.size() << '\\n';\n    for (const auto& op : operations) {\n        cout << get<0>(op) << ' ' << get<1>(op) << ' ' << get<2>(op) << '\\n';\n    }\n\n    return 0;\n}","ahc033":"#include <iostream>\n#include <vector>\n#include <string>\n#include <algorithm>\n#include <utility>\nusing namespace std;\n\nvoid move_to(string& s, int& cr, int& cc, int tr, int tc) {\n    while (cr < tr) {\n        s += 'D';\n        cr++;\n    }\n    while (cr > tr) {\n        s += 'U';\n        cr--;\n    }\n    while (cc < tc) {\n        s += 'R';\n        cc++;\n    }\n    while (cc > tc) {\n        s += 'L';\n        cc--;\n    }\n}\n\nint main() {\n    int N;\n    cin >> N;\n    vector<vector<int>> A(5, vector<int>(5));\n    for (int i = 0; i < 5; i++) {\n        for (int j = 0; j < 5; j++) {\n            cin >> A[i][j];\n        }\n    }\n\n    vector<string> crane_actions(5);\n    for (int i = 1; i < 5; i++) {\n        crane_actions[i] = \"RB\";\n    }\n\n    string large_actions = \"\";\n    int cr = 0, cc = 0;\n\n    vector<int> next_exp = {0, 5, 10, 15, 20};\n    vector<vector<bool>> buffer_occupied(5, vector<bool>(5, false));\n    vector<vector<int>> buffer_contents(5, vector<int>(5, -1));\n\n    for (int i = 0; i < 5; i++) {\n        for (int j = 0; j < 5; j++) {\n            if (i == 0 && j == 0) {\n                large_actions += 'P';\n            } else {\n                move_to(large_actions, cr, cc, i, 0);\n                large_actions += 'P';\n            }\n\n            int x = A[i][j];\n            int r = x / 5;\n            int target_col;\n            if (x == next_exp[r]) {\n                target_col = 4;\n            } else {\n                if (!buffer_occupied[r][2]) {\n                    target_col = 2;\n                    buffer_occupied[r][2] = true;\n                    buffer_contents[r][2] = x;\n                } else if (!buffer_occupied[r][3]) {\n                    target_col = 3;\n                    buffer_occupied[r][3] = true;\n                    buffer_contents[r][3] = x;\n                } else {\n                    target_col = 4;\n                }\n            }\n\n            move_to(large_actions, cr, cc, r, target_col);\n            large_actions += 'Q';\n\n            if (target_col == 4) {\n                next_exp[r]++;\n                bool cleared;\n                do {\n                    cleared = false;\n                    if (buffer_occupied[r][2] && buffer_contents[r][2] == next_exp[r]) {\n                        move_to(large_actions, cr, cc, r, 2);\n                        large_actions += 'P';\n                        move_to(large_actions, cr, cc, r, 4);\n                        large_actions += 'Q';\n                        next_exp[r]++;\n                        buffer_occupied[r][2] = false;\n                        cleared = true;\n                    } else if (buffer_occupied[r][3] && buffer_contents[r][3] == next_exp[r]) {\n                        move_to(large_actions, cr, cc, r, 3);\n                        large_actions += 'P';\n                        move_to(large_actions, cr, cc, r, 4);\n                        large_actions += 'Q';\n                        next_exp[r]++;\n                        buffer_occupied[r][3] = false;\n                        cleared = true;\n                    }\n                } while (cleared);\n            }\n\n            if (j < 4) {\n                move_to(large_actions, cr, cc, i, 1);\n            } else if (i < 4) {\n                move_to(large_actions, cr, cc, i+1, 1);\n            }\n        }\n    }\n\n    for (int r = 0; r < 5; r++) {\n        vector<pair<int, int>> bufs;\n        if (buffer_occupied[r][2]) {\n            bufs.push_back({buffer_contents[r][2], 2});\n        }\n        if (buffer_occupied[r][3]) {\n            bufs.push_back({buffer_contents[r][3], 3});\n        }\n        sort(bufs.begin(), bufs.end());\n        for (auto [num_val, col] : bufs) {\n            move_to(large_actions, cr, cc, r, col);\n            large_actions += 'P';\n            move_to(large_actions, cr, cc, r, 4);\n            large_actions += 'Q';\n        }\n    }\n\n    crane_actions[0] = large_actions;\n    size_t max_len = crane_actions[0].size();\n    for (int i = 1; i < 5; i++) {\n        if (crane_actions[i].size() < max_len) {\n            crane_actions[i] += string(max_len - crane_actions[i].size(), '.');\n        }\n    }\n\n    for (int i = 0; i < 5; i++) {\n        cout << crane_actions[i] << endl;\n    }\n\n    return 0;\n}","ahc034":"#include <iostream>\n#include <vector>\n#include <string>\n#include <algorithm>\nusing namespace std;\n\nint main() {\n    int N;\n    cin >> N;\n    vector<vector<int>> grid(N, vector<int>(N));\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cin >> grid[i][j];\n        }\n    }\n\n    vector<string> ops;\n    int x = 0, y = 0;\n    int load = 0;\n\n    // Process starting cell\n    if (grid[x][y] < 0) {\n        int need = -grid[x][y];\n        if (load >= need) {\n            ops.push_back(\"-\" + to_string(need));\n            load -= need;\n            grid[x][y] = 0;\n        } else if (load > 0) {\n            ops.push_back(\"-\" + to_string(load));\n            grid[x][y] += load;\n            load = 0;\n        }\n    }\n    if (grid[x][y] > 0) {\n        ops.push_back(\"+\" + to_string(grid[x][y]));\n        load += grid[x][y];\n        grid[x][y] = 0;\n    }\n\n    // Forward snake traversal\n    for (int i = 0; i < N; i++) {\n        if (i % 2 == 0) {\n            if (i > 0) {\n                if (grid[x][y] < 0) {\n                    int need = -grid[x][y];\n                    if (load >= need) {\n                        ops.push_back(\"-\" + to_string(need));\n                        load -= need;\n                        grid[x][y] = 0;\n                    } else if (load > 0) {\n                        ops.push_back(\"-\" + to_string(load));\n                        grid[x][y] += load;\n                        load = 0;\n                    }\n                }\n                if (grid[x][y] > 0) {\n                    ops.push_back(\"+\" + to_string(grid[x][y]));\n                    load += grid[x][y];\n                    grid[x][y] = 0;\n                }\n            }\n            for (int j = (i == 0 ? 1 : 1); j < N; j++) {\n                ops.push_back(\"R\");\n                y++;\n                if (grid[x][y] < 0) {\n                    int need = -grid[x][y];\n                    if (load >= need) {\n                        ops.push_back(\"-\" + to_string(need));\n                        load -= need;\n                        grid[x][y] = 0;\n                    } else if (load > 0) {\n                        ops.push_back(\"-\" + to_string(load));\n                        grid[x][y] += load;\n                        load = 0;\n                    }\n                }\n                if (grid[x][y] > 0) {\n                    ops.push_back(\"+\" + to_string(grid[x][y]));\n                    load += grid[x][y];\n                    grid[x][y] = 0;\n                }\n            }\n            if (i < N-1) {\n                ops.push_back(\"D\");\n                x++;\n            }\n        } else {\n            if (grid[x][y] < 0) {\n                int need = -grid[x][y];\n                if (load >= need) {\n                    ops.push_back(\"-\" + to_string(need));\n                    load -= need;\n                    grid[x][y] = 0;\n                } else if (load > 0) {\n                    ops.push_back(\"-\" + to_string(load));\n                    grid[x][y] += load;\n                    load = 0;\n                }\n            }\n            if (grid[x][y] > 0) {\n                ops.push_back(\"+\" + to_string(grid[x][y]));\n                load += grid[x][y];\n                grid[x][y] = 0;\n            }\n            for (int j = N-2; j >= 0; j--) {\n                ops.push_back(\"L\");\n                y--;\n                if (grid[x][y] < 0) {\n                    int need = -grid[x][y];\n                    if (load >= need) {\n                        ops.push_back(\"-\" + to_string(need));\n                        load -= need;\n                        grid[x][y] = 0;\n                    } else if (load > 0) {\n                        ops.push_back(\"-\" + to_string(load));\n                        grid[x][y] += load;\n                        load = 0;\n                    }\n                }\n                if (grid[x][y] > 0) {\n                    ops.push_back(\"+\" + to_string(grid[x][y]));\n                    load += grid[x][y];\n                    grid[x][y] = 0;\n                }\n            }\n            if (i < N-1) {\n                ops.push_back(\"D\");\n                x++;\n            }\n        }\n    }\n\n    // Greedy TSP with intermediate unloading\n    while (load > 0) {\n        vector<pair<int, int>> deficits;\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                if (grid[i][j] < 0) {\n                    deficits.push_back({i, j});\n                }\n            }\n        }\n        if (deficits.empty()) break;\n        \n        int best_index = -1;\n        int best_dist = 1e9;\n        for (int idx = 0; idx < deficits.size(); idx++) {\n            int i0 = deficits[idx].first;\n            int j0 = deficits[idx].second;\n            int d = abs(i0 - x) + abs(j0 - y);\n            if (d < best_dist) {\n                best_dist = d;\n                best_index = idx;\n            }\n        }\n        if (best_index == -1) break;\n        int ti = deficits[best_index].first;\n        int tj = deficits[best_index].second;\n\n        // Move step-by-step to target, unloading at intermediate deficits\n        while (x != ti || y != tj) {\n            if (x < ti) {\n                ops.push_back(\"D\");\n                x++;\n            } else if (x > ti) {\n                ops.push_back(\"U\");\n                x--;\n            } else if (y < tj) {\n                ops.push_back(\"R\");\n                y++;\n            } else if (y > tj) {\n                ops.push_back(\"L\");\n                y--;\n            }\n            \n            // Unload at current cell if it's a deficit\n            if (grid[x][y] < 0 && load > 0) {\n                int d_val = min(load, -grid[x][y]);\n                ops.push_back(\"-\" + to_string(d_val));\n                load -= d_val;\n                grid[x][y] += d_val;\n            }\n        }\n    }\n\n    // Output operations\n    for (const string& op : ops) {\n        cout << op << endl;\n    }\n\n    return 0;\n}","ahc035":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <set>\n#include <utility>\n\nusing namespace std;\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    const int fixed_N = 6;\n    vector<vector<int>> weight(fixed_N, vector<int>(fixed_N, 0));\n    vector<pair<int, int>> even_positions, odd_positions;\n\n    for (int i = 0; i < fixed_N; i++) {\n        for (int j = 0; j < fixed_N; j++) {\n            int w = 0;\n            if (i > 0) w++;\n            if (i < fixed_N-1) w++;\n            if (j > 0) w++;\n            if (j < fixed_N-1) w++;\n            weight[i][j] = w;\n            if ((i+j) % 2 == 0) {\n                even_positions.push_back({i, j});\n            } else {\n                odd_positions.push_back({i, j});\n            }\n        }\n    }\n\n    auto weight_compare = [&](const pair<int, int>& a, const pair<int, int>& b) {\n        return weight[a.first][a.second] > weight[b.first][b.second];\n    };\n    sort(even_positions.begin(), even_positions.end(), weight_compare);\n    sort(odd_positions.begin(), odd_positions.end(), weight_compare);\n\n    int N, M, T;\n    cin >> N >> M >> T;\n\n    const int seed_count = 2 * N * (N - 1);\n    vector<vector<int>> seeds(seed_count, vector<int>(M, 0));\n\n    for (int i = 0; i < seed_count; i++) {\n        for (int j = 0; j < M; j++) {\n            cin >> seeds[i][j];\n        }\n    }\n\n    for (int t = 0; t < T; t++) {\n        vector<int> current_max(M, -1);\n        for (int l = 0; l < M; l++) {\n            for (int k = 0; k < seed_count; k++) {\n                if (seeds[k][l] > current_max[l]) {\n                    current_max[l] = seeds[k][l];\n                }\n            }\n        }\n\n        vector<int> V(seed_count, 0);\n        vector<int> trait_count(seed_count, 0);\n        vector<vector<bool>> trait(seed_count, vector<bool>(M, false));\n        \n        for (int k = 0; k < seed_count; k++) {\n            for (int l = 0; l < M; l++) {\n                V[k] += seeds[k][l];\n                if (seeds[k][l] == current_max[l]) {\n                    trait[k][l] = true;\n                    trait_count[k]++;\n                }\n            }\n        }\n\n        vector<int> coverage_count(M, 0);\n        set<int> chosen_seeds;\n\n        while (true) {\n            bool all_covered = true;\n            for (int l = 0; l < M; l++) {\n                if (coverage_count[l] < 2) {\n                    all_covered = false;\n                    break;\n                }\n            }\n            if (all_covered) break;\n\n            int best_seed = -1;\n            int best_cover = -1;\n            int best_V = -1;\n\n            for (int k = 0; k < seed_count; k++) {\n                if (chosen_seeds.find(k) != chosen_seeds.end()) continue;\n\n                int count = 0;\n                for (int l = 0; l < M; l++) {\n                    if (trait[k][l] && coverage_count[l] < 2) {\n                        count++;\n                    }\n                }\n\n                if (count == 0) continue;\n\n                if (count > best_cover || (count == best_cover && V[k] > best_V)) {\n                    best_cover = count;\n                    best_V = V[k];\n                    best_seed = k;\n                }\n            }\n\n            if (best_seed == -1) break;\n\n            chosen_seeds.insert(best_seed);\n            for (int l = 0; l < M; l++) {\n                if (trait[best_seed][l]) {\n                    coverage_count[l]++;\n                }\n            }\n        }\n\n        if (chosen_seeds.size() < N * N) {\n            vector<pair<int, int>> candidates;\n            for (int k = 0; k < seed_count; k++) {\n                if (chosen_seeds.find(k) == chosen_seeds.end()) {\n                    candidates.push_back({V[k], k});\n                }\n            }\n            sort(candidates.begin(), candidates.end(), [](const auto& a, const auto& b) {\n                return a.first > b.first;\n            });\n            int rem = N * N - chosen_seeds.size();\n            for (int i = 0; i < rem && i < candidates.size(); i++) {\n                chosen_seeds.insert(candidates[i].second);\n            }\n        }\n\n        const int half1 = 8;\n        vector<int> trait_frequency(M, 0);\n        for (int l = 0; l < M; l++) {\n            for (int k : chosen_seeds) {\n                if (trait[k][l]) {\n                    trait_frequency[l]++;\n                }\n            }\n        }\n        \n        vector<int> trait_order(M);\n        for (int l = 0; l < M; l++) trait_order[l] = l;\n        sort(trait_order.begin(), trait_order.end(), [&](int a, int b) {\n            return trait_frequency[a] < trait_frequency[b];\n        });\n        \n        vector<bool> assigned(seed_count, false);\n        vector<int> groupA, groupB;\n        vector<bool> hasA(M, false), hasB(M, false);\n        \n        for (int l : trait_order) {\n            if (!hasA[l]) {\n                int best_seed = -1;\n                for (int k : chosen_seeds) {\n                    if (assigned[k]) continue;\n                    if (!trait[k][l]) continue;\n                    if (best_seed == -1 || V[k] > V[best_seed]) {\n                        best_seed = k;\n                    }\n                }\n                if (best_seed != -1) {\n                    assigned[best_seed] = true;\n                    groupA.push_back(best_seed);\n                    for (int ll = 0; ll < M; ll++) {\n                        if (trait[best_seed][ll]) hasA[ll] = true;\n                    }\n                }\n            }\n            \n            if (!hasB[l]) {\n                int best_seed = -1;\n                for (int k : chosen_seeds) {\n                    if (assigned[k]) continue;\n                    if (!trait[k][l]) continue;\n                    if (best_seed == -1 || V[k] > V[best_seed]) {\n                        best_seed = k;\n                    }\n                }\n                if (best_seed != -1) {\n                    assigned[best_seed] = true;\n                    groupB.push_back(best_seed);\n                    for (int ll = 0; ll < M; ll++) {\n                        if (trait[best_seed][ll]) hasB[ll] = true;\n                    }\n                }\n            }\n        }\n        \n        vector<pair<int, int>> remaining_signature;\n        for (int k : chosen_seeds) {\n            if (assigned[k]) continue;\n            int score1 = 0, score2 = 0;\n            for (int l = 0; l < half1; l++) {\n                if (trait[k][l]) score1++;\n            }\n            for (int l = half1; l < M; l++) {\n                if (trait[k][l]) score2++;\n            }\n            remaining_signature.push_back({score1 - score2, k});\n        }\n        sort(remaining_signature.begin(), remaining_signature.end(), [](const auto& a, const auto& b) {\n            return a.first > b.first;\n        });\n        \n        for (auto& p : remaining_signature) {\n            int k = p.second;\n            if (groupA.size() < 18) {\n                groupA.push_back(k);\n            } else if (groupB.size() < 18) {\n                groupB.push_back(k);\n            }\n        }\n\n        auto seed_compare = [&](int a, int b) {\n            if (V[a] != V[b]) return V[a] > V[b];\n            return trait_count[a] > trait_count[b];\n        };\n        sort(groupA.begin(), groupA.end(), seed_compare);\n        sort(groupB.begin(), groupB.end(), seed_compare);\n\n        vector<vector<int>> grid(N, vector<int>(N, -1));\n        for (int idx = 0; idx < groupA.size(); idx++) {\n            int i = even_positions[idx].first;\n            int j = even_positions[idx].second;\n            grid[i][j] = groupA[idx];\n        }\n        for (int idx = 0; idx < groupB.size(); idx++) {\n            int i = odd_positions[idx].first;\n            int j = odd_positions[idx].second;\n            grid[i][j] = groupB[idx];\n        }\n\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                cout << grid[i][j];\n                if (j < N - 1) cout << \" \";\n                else cout << endl;\n            }\n        }\n        cout.flush();\n\n        for (int i = 0; i < seed_count; i++) {\n            for (int j = 0; j < M; j++) {\n                cin >> seeds[i][j];\n            }\n        }\n    }\n\n    return 0;\n}","ahc038":"#include <iostream>\n#include <vector>\n#include <cstdlib>\n#include <algorithm>\n#include <climits>\n#include <tuple>\nusing namespace std;\n\nint main() {\n    int N, M, V;\n    cin >> N >> M >> V;\n\n    vector<string> s_grid(N), t_grid(N);\n    for (int i = 0; i < N; i++) {\n        cin >> s_grid[i];\n    }\n    for (int i = 0; i < N; i++) {\n        cin >> t_grid[i];\n    }\n\n    int L0 = (N - 1) / 2;\n\n    cout << V << endl;\n    for (int i = 1; i < V; i++) {\n        cout << \"0 \" << L0 << endl;\n    }\n\n    int rx0 = N / 2;\n    int ry0 = N / 2;\n    cout << rx0 << \" \" << ry0 << endl;\n\n    int cur_x = rx0, cur_y = ry0;\n    vector<int> dirs(V, 0);\n    vector<bool> holding(V, false);\n\n    vector<pair<int, int>> initial_list;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (s_grid[i][j] == '1') {\n                initial_list.push_back({i, j});\n            }\n        }\n    }\n\n    vector<pair<int, int>> target_list;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (t_grid[i][j] == '1') {\n                target_list.push_back({i, j});\n            }\n        }\n    }\n\n    vector<string> outputs;\n\n    while (!initial_list.empty()) {\n        int F_available = V - 1;\n        int batch_size = min(F_available, (int)initial_list.size());\n        vector<tuple<int, int, int, int, int, int>> pickup_batch;\n        vector<int> available_fingertips;\n        for (int i = 1; i < V; i++) {\n            available_fingertips.push_back(i);\n        }\n\n        int current_start_x = cur_x;\n        int current_start_y = cur_y;\n\n        for (int idx = 0; idx < batch_size; idx++) {\n            int best_cost = INT_MAX;\n            int best_i = -1, best_j = -1, best_cx = -1, best_cy = -1, best_d = -1, best_f = -1;\n            for (int k = 0; k < initial_list.size(); k++) {\n                int i = initial_list[k].first;\n                int j = initial_list[k].second;\n                vector<tuple<int, int, int>> candidates;\n                if (j - L0 >= 0) {\n                    candidates.push_back(make_tuple(i, j - L0, 0));\n                }\n                if (i - L0 >= 0) {\n                    candidates.push_back(make_tuple(i - L0, j, 1));\n                }\n                if (j + L0 < N) {\n                    candidates.push_back(make_tuple(i, j + L0, 2));\n                }\n                if (i + L0 < N) {\n                    candidates.push_back(make_tuple(i + L0, j, 3));\n                }\n                for (auto &cand : candidates) {\n                    int cx = get<0>(cand);\n                    int cy = get<1>(cand);\n                    int d = get<2>(cand);\n                    for (int f_index = 0; f_index < available_fingertips.size(); f_index++) {\n                        int f = available_fingertips[f_index];\n                        int dist = abs(cx - current_start_x) + abs(cy - current_start_y);\n                        int cur_dir = dirs[f];\n                        int diff = (d - cur_dir + 4) % 4;\n                        int rot_cost = min(diff, 4 - diff);\n                        int cost = dist + rot_cost;\n                        if (cost < best_cost) {\n                            best_cost = cost;\n                            best_i = i;\n                            best_j = j;\n                            best_cx = cx;\n                            best_cy = cy;\n                            best_d = d;\n                            best_f = f;\n                        }\n                    }\n                }\n            }\n            if (best_cost == INT_MAX) {\n                batch_size = idx;\n                break;\n            }\n\n            for (auto it = initial_list.begin(); it != initial_list.end(); it++) {\n                if (it->first == best_i && it->second == best_j) {\n                    initial_list.erase(it);\n                    break;\n                }\n            }\n\n            for (auto it = available_fingertips.begin(); it != available_fingertips.end(); it++) {\n                if (*it == best_f) {\n                    available_fingertips.erase(it);\n                    break;\n                }\n            }\n\n            pickup_batch.push_back(make_tuple(best_i, best_j, best_cx, best_cy, best_d, best_f));\n            current_start_x = best_cx;\n            current_start_y = best_cy;\n        }\n\n        for (auto &item : pickup_batch) {\n            int i = get<0>(item);\n            int j = get<1>(item);\n            int cx = get<2>(item);\n            int cy = get<3>(item);\n            int d = get<4>(item);\n            int f = get<5>(item);\n\n            int dx = cx - cur_x;\n            int dy = cy - cur_y;\n\n            vector<char> moves;\n            if (dx > 0) {\n                for (int k = 0; k < dx; k++) moves.push_back('R');\n            } else if (dx < 0) {\n                for (int k = 0; k < -dx; k++) moves.push_back('L');\n            }\n            if (dy > 0) {\n                for (int k = 0; k < dy; k++) moves.push_back('D');\n            } else if (dy < 0) {\n                for (int k = 0; k < -dy; k++) moves.push_back('U');\n            }\n\n            int cur_dir = dirs[f];\n            int diff = (d - cur_dir + 4) % 4;\n            vector<char> rots;\n            if (diff == 1) {\n                rots.push_back('R');\n            } else if (diff == 2) {\n                rots.push_back('R');\n                rots.push_back('R');\n            } else if (diff == 3) {\n                rots.push_back('L');\n            }\n\n            int T = max(moves.size(), rots.size());\n            for (int t = 0; t < T; t++) {\n                string op = \"\";\n                if (t < moves.size()) {\n                    op += moves[t];\n                } else {\n                    op += '.';\n                }\n\n                for (int v = 1; v < V; v++) {\n                    if (v == f && t < rots.size()) {\n                        op += rots[t];\n                    } else {\n                        op += '.';\n                    }\n                }\n\n                for (int v = 0; v < V; v++) {\n                    if (v == f && t == T - 1) {\n                        op += 'P';\n                        holding[f] = true;\n                    } else {\n                        op += '.';\n                    }\n                }\n                outputs.push_back(op);\n            }\n\n            cur_x = cx;\n            cur_y = cy;\n            dirs[f] = d;\n        }\n\n        vector<pair<int, int>> available_targets = target_list;\n        vector<tuple<int, int, int, int, int, int>> delivery_batch;\n        current_start_x = cur_x;\n        current_start_y = cur_y;\n\n        for (int idx = 0; idx < batch_size; idx++) {\n            int best_cost = INT_MAX;\n            int best_i = -1, best_j = -1, best_cx = -1, best_cy = -1, best_d = -1, best_f = -1;\n            for (int k = 0; k < available_targets.size(); k++) {\n                int i = available_targets[k].first;\n                int j = available_targets[k].second;\n                vector<tuple<int, int, int>> candidates;\n                if (j - L0 >= 0) {\n                    candidates.push_back(make_tuple(i, j - L0, 0));\n                }\n                if (i - L0 >= 0) {\n                    candidates.push_back(make_tuple(i - L0, j, 1));\n                }\n                if (j + L0 < N) {\n                    candidates.push_back(make_tuple(i, j + L0, 2));\n                }\n                if (i + L0 < N) {\n                    candidates.push_back(make_tuple(i + L0, j, 3));\n                }\n                for (auto &cand : candidates) {\n                    int cx = get<0>(cand);\n                    int cy = get<1>(cand);\n                    int d = get<2>(cand);\n                    for (auto &p_item : pickup_batch) {\n                        int f = get<5>(p_item);\n                        if (holding[f]) {\n                            int dist = abs(cx - current_start_x) + abs(cy - current_start_y);\n                            int cur_dir = dirs[f];\n                            int diff = (d - cur_dir + 4) % 4;\n                            int rot_cost = min(diff, 4 - diff);\n                            int cost = dist + rot_cost;\n                            if (cost < best_cost) {\n                                best_cost = cost;\n                                best_i = i;\n                                best_j = j;\n                                best_cx = cx;\n                                best_cy = cy;\n                                best_d = d;\n                                best_f = f;\n                            }\n                        }\n                    }\n                }\n            }\n            if (best_cost == INT_MAX) {\n                batch_size = idx;\n                break;\n            }\n\n            for (auto it = available_targets.begin(); it != available_targets.end(); it++) {\n                if (it->first == best_i && it->second == best_j) {\n                    available_targets.erase(it);\n                    break;\n                }\n            }\n\n            delivery_batch.push_back(make_tuple(best_i, best_j, best_cx, best_cy, best_d, best_f));\n            current_start_x = best_cx;\n            current_start_y = best_cy;\n        }\n\n        for (auto &item : delivery_batch) {\n            int i = get<0>(item);\n            int j = get<1>(item);\n            int cx = get<2>(item);\n            int cy = get<3>(item);\n            int d = get<4>(item);\n            int f = get<5>(item);\n\n            int dx = cx - cur_x;\n            int dy = cy - cur_y;\n\n            vector<char> moves;\n            if (dx > 0) {\n                for (int k = 0; k < dx; k++) moves.push_back('R');\n            } else if (dx < 0) {\n                for (int k = 0; k < -dx; k++) moves.push_back('L');\n            }\n            if (dy > 0) {\n                for (int k = 0; k < dy; k++) moves.push_back('D');\n            } else if (dy < 0) {\n                for (int k = 0; k < -dy; k++) moves.push_back('U');\n            }\n\n            int cur_dir = dirs[f];\n            int diff = (d - cur_dir + 4) % 4;\n            vector<char> rots;\n            if (diff == 1) {\n                rots.push_back('R');\n            } else if (diff == 2) {\n                rots.push_back('R');\n                rots.push_back('R');\n            } else if (diff == 3) {\n                rots.push_back('L');\n            }\n\n            int T = max(moves.size(), rots.size());\n            for (int t = 0; t < T; t++) {\n                string op = \"\";\n                if (t < moves.size()) {\n                    op += moves[t];\n                } else {\n                    op += '.';\n                }\n\n                for (int v = 1; v < V; v++) {\n                    if (v == f && t < rots.size()) {\n                        op += rots[t];\n                    } else {\n                        op += '.';\n                    }\n                }\n\n                for (int v = 0; v < V; v++) {\n                    if (v == f && t == T - 1) {\n                        op += 'P';\n                        holding[f] = false;\n                    } else {\n                        op += '.';\n                    }\n                }\n                outputs.push_back(op);\n            }\n\n            cur_x = cx;\n            cur_y = cy;\n            dirs[f] = d;\n        }\n\n        for (auto &item : delivery_batch) {\n            int i = get<0>(item);\n            int j = get<1>(item);\n            for (auto it = target_list.begin(); it != target_list.end(); it++) {\n                if (it->first == i && it->second == j) {\n                    target_list.erase(it);\n                    break;\n                }\n            }\n        }\n    }\n\n    for (string &s : outputs) {\n        cout << s << endl;\n    }\n\n    return 0;\n}","ahc039":"#include <iostream>\n#include <vector>\n#include <set>\n#include <algorithm>\n#include <climits>\n#include <tuple>\n#include <map>\n#include <queue>\n#include <unordered_map>\n#include <cmath>\nusing namespace std;\n\nstruct Point {\n    int x, y;\n    Point(int x=0, int y=0) : x(x), y(y) {}\n    bool operator<(const Point& other) const {\n        if (x != other.x) return x < other.x;\n        return y < other.y;\n    }\n    bool operator==(const Point& other) const {\n        return x == other.x && y == other.y;\n    }\n};\n\nnamespace std {\n    template<> struct hash<Point> {\n        size_t operator()(const Point& p) const {\n            return (size_t)p.x * 1000000 + p.y;\n        }\n    };\n}\n\nconst int GRID = 15;\n\nint main() {\n    int N;\n    cin >> N;\n    vector<Point> mackerels;\n    vector<Point> sardines;\n    for (int i = 0; i < 2*N; i++) {\n        int x, y;\n        cin >> x >> y;\n        if (i < N) {\n            mackerels.push_back(Point(x, y));\n        } else {\n            sardines.push_back(Point(x, y));\n        }\n    }\n\n    set<pair<int, int>> sardine_set;\n    for (auto& p : sardines) {\n        sardine_set.insert({p.x, p.y});\n    }\n\n    vector<int> x_grid(GRID+1);\n    vector<int> y_grid(GRID+1);\n    for (int i = 0; i <= GRID; i++) {\n        x_grid[i] = (long long)i * 100000 / GRID;\n        y_grid[i] = (long long)i * 100000 / GRID;\n    }\n    x_grid[GRID] = 100000;\n    y_grid[GRID] = 100000;\n\n    vector<vector<int>> grid_m(GRID, vector<int>(GRID, 0));\n    vector<vector<int>> grid_s(GRID, vector<int>(GRID, 0));\n    vector<vector<int>> grid_count(GRID, vector<int>(GRID, 0));\n\n    for (auto& p : mackerels) {\n        int i = lower_bound(x_grid.begin(), x_grid.end(), p.x + 1) - x_grid.begin() - 1;\n        int j = lower_bound(y_grid.begin(), y_grid.end(), p.y + 1) - y_grid.begin() - 1;\n        if (i < 0) i = 0;\n        if (i >= GRID) i = GRID-1;\n        if (j < 0) j = 0;\n        if (j >= GRID) j = GRID-1;\n        grid_m[i][j]++;\n    }\n\n    for (auto& p : sardines) {\n        int i = lower_bound(x_grid.begin(), x_grid.end(), p.x + 1) - x_grid.begin() - 1;\n        int j = lower_bound(y_grid.begin(), y_grid.end(), p.y + 1) - y_grid.begin() - 1;\n        if (i < 0) i = 0;\n        if (i >= GRID) i = GRID-1;\n        if (j < 0) j = 0;\n        if (j >= GRID) j = GRID-1;\n        grid_s[i][j]++;\n    }\n\n    for (int i = 0; i < GRID; i++) {\n        for (int j = 0; j < GRID; j++) {\n            grid_count[i][j] = grid_m[i][j] - grid_s[i][j];\n        }\n    }\n\n    int best_score = -1000000;\n    int best_i = -1, best_j = -1;\n    for (int i = 0; i < GRID; i++) {\n        for (int j = 0; j < GRID; j++) {\n            if (grid_count[i][j] > best_score) {\n                best_score = grid_count[i][j];\n                best_i = i;\n                best_j = j;\n            }\n        }\n    }\n\n    if (best_score <= 0) {\n        int best_cnt = 10;\n        Point best_point;\n        bool found = false;\n        for (auto& p : mackerels) {\n            int cnt = 0;\n            for (int dx = -1; dx <= 1; dx++) {\n                for (int dy = -1; dy <= 1; dy++) {\n                    int x1 = p.x + dx;\n                    int y1 = p.y + dy;\n                    if (x1 < 0 || x1 > 100000 || y1 < 0 || y1 > 100000) continue;\n                    if (sardine_set.find({x1, y1}) != sardine_set.end()) {\n                        cnt++;\n                    }\n                }\n            }\n            if (cnt < best_cnt) {\n                best_cnt = cnt;\n                best_point = p;\n                found = true;\n            }\n        }\n        if (!found) {\n            best_point = mackerels[0];\n        }\n\n        int best_value_fallback = -1000000;\n        int best_x1 = 0, best_y1 = 0, best_x2 = 0, best_y2 = 0;\n\n        for (int s = 1; s <= 10; s++) {\n            for (int dx = 0; dx <= s; dx++) {\n                for (int dy = 0; dy <= s; dy++) {\n                    int x0 = best_point.x;\n                    int y0 = best_point.y;\n                    int x1 = max(0, x0 - dx);\n                    int y1 = max(0, y0 - dy);\n                    int x2 = min(100000, x1 + s);\n                    int y2 = min(100000, y1 + s);\n\n                    int m = 0, sard = 0;\n                    for (auto& p : mackerels) {\n                        if (p.x >= x1 && p.x <= x2 && p.y >= y1 && p.y <= y2) {\n                            m++;\n                        }\n                    }\n                    for (auto& p : sardines) {\n                        if (p.x >= x1 && p.x <= x2 && p.y >= y1 && p.y <= y2) {\n                            sard++;\n                        }\n                    }\n                    int value = m - sard;\n                    if (value > best_value_fallback) {\n                        best_value_fallback = value;\n                        best_x1 = x1;\n                        best_y1 = y1;\n                        best_x2 = x2;\n                        best_y2 = y2;\n                    }\n                }\n            }\n        }\n\n        vector<Point> poly;\n        poly.push_back(Point(best_x1, best_y2));\n        poly.push_back(Point(best_x2, best_y2));\n        poly.push_back(Point(best_x2, best_y1));\n        poly.push_back(Point(best_x1, best_y1));\n        cout << 4 << \"\\n\";\n        for (auto& p : poly) {\n            cout << p.x << \" \" << p.y << \"\\n\";\n        }\n        return 0;\n    }\n\n    set<pair<int, int>> R;\n    set<pair<int, int>> frontier;\n    R.insert({best_i, best_j});\n    long long total_perimeter = 0;\n    const int dx4[4] = {0, 1, 0, -1};\n    const int dy4[4] = {1, 0, -1, 0};\n\n    int w0 = x_grid[best_i+1] - x_grid[best_i];\n    int h0 = y_grid[best_j+1] - y_grid[best_j];\n    total_perimeter = 2LL * (w0 + h0);\n\n    for (int d = 0; d < 4; d++) {\n        int ni = best_i + dx4[d];\n        int nj = best_j + dy4[d];\n        if (ni < 0 || ni >= GRID || nj < 0 || nj >= GRID) continue;\n        if (R.find({ni, nj}) == R.end()) {\n            frontier.insert({ni, nj});\n        }\n    }\n\n    while (!frontier.empty()) {\n        int best_cell_i = -1, best_cell_j = -1;\n        int best_cell_score = -1000000;\n        long long best_change_perimeter = 0;\n        vector<pair<int, int>> to_remove;\n        for (auto cell : frontier) {\n            int i = cell.first;\n            int j = cell.second;\n            if (R.find(cell) != R.end()) {\n                to_remove.push_back(cell);\n                continue;\n            }\n            int w = x_grid[i+1] - x_grid[i];\n            int h = y_grid[j+1] - y_grid[j];\n            long long shared_edges = 0;\n            for (int d = 0; d < 4; d++) {\n                int ni = i + dx4[d];\n                int nj = j + dy4[d];\n                if (ni < 0 || ni >= GRID || nj < 0 || nj >= GRID) continue;\n                if (R.find({ni, nj}) != R.end()) {\n                    if (d == 0 || d == 2) {\n                        shared_edges += h;\n                    } else {\n                        shared_edges += w;\n                    }\n                }\n            }\n            long long change_perimeter = 2LL * (w + h) - 2 * shared_edges;\n            if (total_perimeter + change_perimeter > 400000) continue;\n            if (grid_count[i][j] > best_cell_score) {\n                best_cell_score = grid_count[i][j];\n                best_cell_i = i;\n                best_cell_j = j;\n                best_change_perimeter = change_perimeter;\n            }\n        }\n        for (auto& cell : to_remove) {\n            frontier.erase(cell);\n        }\n        if (best_cell_score <= 0) {\n            break;\n        }\n        if (best_cell_i == -1) {\n            break;\n        }\n        R.insert({best_cell_i, best_cell_j});\n        total_perimeter += best_change_perimeter;\n        frontier.erase({best_cell_i, best_cell_j});\n        for (int d = 0; d < 4; d++) {\n            int ni = best_cell_i + dx4[d];\n            int nj = best_cell_j + dy4[d];\n            if (ni < 0 || ni >= GRID || nj < 0 || nj >= GRID) continue;\n            if (R.find({ni, nj}) == R.end()) {\n                frontier.insert({ni, nj});\n            }\n        }\n    }\n\n    vector<vector<bool>> grid_selected(GRID, vector<bool>(GRID, false));\n    for (auto& cell : R) {\n        grid_selected[cell.first][cell.second] = true;\n    }\n\n    vector<vector<bool>> visited(GRID, vector<bool>(GRID, false));\n    queue<pair<int, int>> q;\n    for (int i = 0; i < GRID; i++) {\n        for (int j = 0; j < GRID; j++) {\n            if (i == 0 || i == GRID-1 || j == 0 || j == GRID-1) {\n                if (!grid_selected[i][j]) {\n                    visited[i][j] = true;\n                    q.push({i, j});\n                }\n            }\n        }\n    }\n\n    const int di[4] = {0, 1, 0, -1};\n    const int dj[4] = {1, 0, -1, 0};\n    while (!q.empty()) {\n        auto cell = q.front(); q.pop();\n        int i = cell.first;\n        int j = cell.second;\n        for (int d = 0; d < 4; d++) {\n            int ni = i + di[d];\n            int nj = j + dj[d];\n            if (ni < 0 || ni >= GRID || nj < 0 || nj >= GRID) continue;\n            if (visited[ni][nj]) continue;\n            if (grid_selected[ni][nj]) continue;\n            visited[ni][nj] = true;\n            q.push({ni, nj});\n        }\n    }\n\n    bool hasHole = false;\n    for (int i = 0; i < GRID; i++) {\n        for (int j = 0; j < GRID; j++) {\n            if (!grid_selected[i][j] && !visited[i][j]) {\n                hasHole = true;\n                break;\n            }\n        }\n        if (hasHole) break;\n    }\n\n    if (!hasHole && total_perimeter <= 400000) {\n        set<tuple<int, int, int, int>> edgeSet;\n        for (auto& cell : R) {\n            int i = cell.first;\n            int j = cell.second;\n            tuple<int, int, int, int> top = make_tuple(x_grid[i], y_grid[j+1], x_grid[i+1], y_grid[j+1]);\n            tuple<int, int, int, int> right = make_tuple(x_grid[i+1], y_grid[j], x_grid[i+1], y_grid[j+1]);\n            tuple<int, int, int, int> bottom = make_tuple(x_grid[i], y_grid[j], x_grid[i+1], y_grid[j]);\n            tuple<int, int, int, int> left = make_tuple(x_grid[i], y_grid[j], x_grid[i], y_grid[j+1]);\n            if (edgeSet.find(top) != edgeSet.end()) {\n                edgeSet.erase(top);\n            } else {\n                edgeSet.insert(top);\n            }\n            if (edgeSet.find(right) != edgeSet.end()) {\n                edgeSet.erase(right);\n            } else {\n                edgeSet.insert(right);\n            }\n            if (edgeSet.find(bottom) != edgeSet.end()) {\n                edgeSet.erase(bottom);\n            } else {\n                edgeSet.insert(bottom);\n            }\n            if (edgeSet.find(left) != edgeSet.end()) {\n                edgeSet.erase(left);\n            } else {\n                edgeSet.insert(left);\n            }\n        }\n\n        map<Point, vector<Point>> graph;\n        for (auto& edge : edgeSet) {\n            int x1 = get<0>(edge);\n            int y1 = get<1>(edge);\n            int x2 = get<2>(edge);\n            int y2 = get<3>(edge);\n            Point p1, p2;\n            if (y1 == y2) {\n                p1 = Point(x1, y1);\n                p2 = Point(x2, y1);\n            } else {\n                p1 = Point(x1, y1);\n                p2 = Point(x1, y2);\n            }\n            graph[p1].push_back(p2);\n            graph[p2].push_back(p1);\n        }\n\n        Point start(100000, 0);\n        for (auto& node : graph) {\n            Point p = node.first;\n            if (p.x < start.x || (p.x == start.x && p.y > start.y)) {\n                start = p;\n            }\n        }\n\n        vector<Point> poly;\n        poly.push_back(start);\n        Point prev = Point(-1, -1);\n        Point cur = start;\n        Point next(-1, -1);\n        vector<Point> neighbors = graph[start];\n        for (Point p : neighbors) {\n            if (p.y == start.y && p.x > start.x) {\n                next = p;\n                break;\n            }\n        }\n        if (next.x == -1) {\n            for (Point p : neighbors) {\n                if (p.x == start.x && p.y < start.y) {\n                    next = p;\n                    break;\n                }\n            }\n        }\n        if (next.x == -1) {\n            next = neighbors[0];\n        }\n        poly.push_back(next);\n        prev = cur;\n        cur = next;\n\n        while (cur != start) {\n            neighbors = graph[cur];\n            Point next_candidate(-1, -1);\n            for (Point p : neighbors) {\n                if (p == prev) continue;\n                next_candidate = p;\n                break;\n            }\n            if (next_candidate.x == -1) {\n                break;\n            }\n            poly.push_back(next_candidate);\n            prev = cur;\n            cur = next_candidate;\n        }\n\n        vector<Point> cleaned_poly;\n        cleaned_poly.push_back(poly[0]);\n        for (int i = 1; i < poly.size(); i++) {\n            if (poly[i].x == cleaned_poly.back().x && poly[i].y == cleaned_poly.back().y) {\n                continue;\n            }\n            cleaned_poly.push_back(poly[i]);\n        }\n        if (cleaned_poly.size() >= 2 && cleaned_poly[0].x == cleaned_poly.back().x && cleaned_poly[0].y == cleaned_poly.back().y) {\n            cleaned_poly.pop_back();\n        }\n\n        if (cleaned_poly.size() >= 4) {\n            long long actual_perim = 0;\n            int m = cleaned_poly.size();\n            for (int i = 0; i < m; i++) {\n                int j = (i+1) % m;\n                actual_perim += abs(cleaned_poly[i].x - cleaned_poly[j].x) + abs(cleaned_poly[i].y - cleaned_poly[j].y);\n            }\n\n            if (actual_perim <= 400000) {\n                cout << cleaned_poly.size() << \"\\n\";\n                for (auto& p : cleaned_poly) {\n                    cout << p.x << \" \" << p.y << \"\\n\";\n                }\n                return 0;\n            }\n        }\n    }\n\n    int min_x = 100000, max_x = 0, min_y = 100000, max_y = 0;\n    for (auto cell : R) {\n        int i = cell.first;\n        int j = cell.second;\n        min_x = min(min_x, x_grid[i]);\n        max_x = max(max_x, x_grid[i+1]);\n        min_y = min(min_y, y_grid[j]);\n        max_y = max(max_y, y_grid[j+1]);\n    }\n\n    long long bbox_perim = 2LL * ((max_x - min_x) + (max_y - min_y));\n    if (bbox_perim > 400000) {\n        min_x = x_grid[best_i];\n        max_x = x_grid[best_i+1];\n        min_y = y_grid[best_j];\n        max_y = y_grid[best_j+1];\n    }\n\n    vector<Point> poly;\n    poly.push_back(Point(min_x, max_y));\n    poly.push_back(Point(max_x, max_y));\n    poly.push_back(Point(max_x, min_y));\n    poly.push_back(Point(min_x, min_y));\n    cout << 4 << \"\\n\";\n    for (auto& p : poly) {\n        cout << p.x << \" \" << p.y << \"\\n\";\n    }\n\n    return 0;\n}","ahc040":"#include <iostream>\n#include <vector>\n#include <set>\n#include <map>\n#include <algorithm>\n#include <cmath>\n#include <cctype>\n#include <tuple>\n#include <iomanip>\n#include <climits>\n#include <random>\n#include <chrono>\n\nusing namespace std;\n\nset<int> get_reference_set(int strategy, const vector<int>& placed_list, const vector<tuple<long long, long long, long long, long long>>& rect_pos) {\n    set<int> refs;\n    refs.insert(-1);\n    if (placed_list.empty()) return refs;\n\n    if (strategy == 0) {\n        refs.insert(placed_list.back());\n    } else if (strategy == 1) {\n        for (int i = 0; i < min(3, (int)placed_list.size()); i++) {\n            refs.insert(placed_list[placed_list.size() - 1 - i]);\n        }\n    } else if (strategy == 2) {\n        vector<pair<long long, int>> areas;\n        for (int j : placed_list) {\n            auto [x, y, w, h] = rect_pos[j];\n            areas.push_back({w * h, j});\n        }\n        sort(areas.rbegin(), areas.rend());\n        for (int i = 0; i < min(3, (int)areas.size()); i++) {\n            refs.insert(areas[i].second);\n        }\n    } else if (strategy == 3) {\n        refs.insert(placed_list.back());\n        vector<pair<long long, int>> areas;\n        for (int j : placed_list) {\n            auto [x, y, w, h] = rect_pos[j];\n            areas.push_back({w * h, j});\n        }\n        sort(areas.rbegin(), areas.rend());\n        for (int i = 0; i < min(2, (int)areas.size()); i++) {\n            refs.insert(areas[i].second);\n        }\n    } else if (strategy == 4) {\n        long long max_right = 0;\n        int id_max_right = -1;\n        for (int j : placed_list) {\n            auto [x, y, w, h] = rect_pos[j];\n            if (x + w > max_right) {\n                max_right = x + w;\n                id_max_right = j;\n            }\n        }\n        if (id_max_right != -1) refs.insert(id_max_right);\n    } else if (strategy == 5) {\n        long long max_bottom = 0;\n        int id_max_bottom = -1;\n        for (int j : placed_list) {\n            auto [x, y, w, h] = rect_pos[j];\n            if (y + h > max_bottom) {\n                max_bottom = y + h;\n                id_max_bottom = j;\n            }\n        }\n        if (id_max_bottom != -1) refs.insert(id_max_bottom);\n    } else if (strategy == 6) {\n        refs.insert(placed_list.back());\n        long long max_right = 0, max_bottom = 0;\n        int id_max_right = -1, id_max_bottom = -1;\n        for (int j : placed_list) {\n            auto [x, y, w, h] = rect_pos[j];\n            if (x + w > max_right) {\n                max_right = x + w;\n                id_max_right = j;\n            }\n            if (y + h > max_bottom) {\n                max_bottom = y + h;\n                id_max_bottom = j;\n            }\n        }\n        if (id_max_right != -1) refs.insert(id_max_right);\n        if (id_max_bottom != -1) refs.insert(id_max_bottom);\n    }\n    return refs;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, T;\n    double sigma;\n    cin >> N >> T >> sigma;\n\n    vector<double> w_est(N);\n    vector<double> h_est(N);\n    vector<double> w_sum(N, 0.0);\n    vector<double> h_sum(N, 0.0);\n    vector<int> count(N, 0);\n\n    for (int i = 0; i < N; i++) {\n        cin >> w_est[i] >> h_est[i];\n    }\n\n    int exploration_turns = min(N, T);\n    vector<int> exploration_order(N);\n    for (int i = 0; i < N; i++) {\n        exploration_order[i] = i;\n    }\n    sort(exploration_order.begin(), exploration_order.end(), [&](int i, int j) {\n        return w_est[i] * h_est[i] > w_est[j] * h_est[j];\n    });\n\n    for (int turn = 0; turn < exploration_turns; turn++) {\n        int idx = exploration_order[turn % N];\n        cout << 1 << '\\n';\n        cout << idx << \" \" << 0 << \" \" << \"U\" << \" \" << -1 << '\\n';\n        cout.flush();\n\n        long long Wp, Hp;\n        cin >> Wp >> Hp;\n\n        w_sum[idx] += Wp;\n        h_sum[idx] += Hp;\n        count[idx]++;\n    }\n\n    for (int i = 0; i < N; i++) {\n        if (count[i] > 0) {\n            w_est[i] = w_sum[i] / count[i];\n            h_est[i] = h_sum[i] / count[i];\n        }\n    }\n\n    int packing_turns = T - exploration_turns;\n    vector<tuple<vector<tuple<int, int, char, int>>, long long>> candidates;\n    const int NUM_CANDIDATES = 120;\n\n    vector<long long> w_sim(N), h_sim(N);\n    for (int i = 0; i < N; i++) {\n        w_sim[i] = static_cast<long long>(round(w_est[i]));\n        h_sim[i] = static_cast<long long>(round(h_est[i]));\n        w_sim[i] = max(1LL, min(1000000000LL, w_sim[i]));\n        h_sim[i] = max(1LL, min(1000000000LL, h_sim[i]));\n    }\n\n    if (packing_turns > 0) {\n        auto seed = chrono::steady_clock::now().time_since_epoch().count();\n        auto rng = mt19937(seed);\n        vector<double> thresholds = {0.6, 0.7, 0.8, 0.9, 1.0};\n        uniform_int_distribution<int> dist_ref(0, 6);\n        \n        for (int cand_idx = 0; cand_idx < NUM_CANDIDATES; cand_idx++) {\n            int ref_strategy = dist_ref(rng);\n            double threshold = thresholds[cand_idx % thresholds.size()];\n\n            vector<tuple<int, int, char, int>> sequence;\n            vector<bool> placed(N, false);\n            vector<tuple<long long, long long, long long, long long>> rect_pos(N);\n            vector<int> placed_list;\n            long long current_max_x = 0;\n            long long current_max_y = 0;\n\n            for (int idx = 0; idx < N; idx++) {\n                set<int> refs = get_reference_set(ref_strategy, placed_list, rect_pos);\n                vector<tuple<long long, int, char, int, long long, long long>> candidates_for_rect;\n\n                for (int rot = 0; rot < 2; rot++) {\n                    long long w = (rot == 0) ? w_sim[idx] : h_sim[idx];\n                    long long h = (rot == 0) ? h_sim[idx] : w_sim[idx];\n                    for (char d : {'U', 'L'}) {\n                        for (int b_ref : refs) {\n                            long long x0 = 0, y0 = 0;\n                            \n                            if (b_ref == -1) {\n                                if (d == 'U') {\n                                    x0 = 0;\n                                    for (int j : placed_list) {\n                                        auto [x_pl, y_pl, w_pl, h_pl] = rect_pos[j];\n                                        if (x0 < x_pl + w_pl && x0 + w > x_pl) {\n                                            y0 = max(y0, y_pl + h_pl);\n                                        }\n                                    }\n                                } else {\n                                    y0 = 0;\n                                    for (int j : placed_list) {\n                                        auto [x_pl, y_pl, w_pl, h_pl] = rect_pos[j];\n                                        if (y0 < y_pl + h_pl && y0 + h > y_pl) {\n                                            x0 = max(x0, x_pl + w_pl);\n                                        }\n                                    }\n                                }\n                            } else {\n                                if (!placed[b_ref]) continue;\n                                auto [x_ref, y_ref, w_ref, h_ref] = rect_pos[b_ref];\n                                if (d == 'U') {\n                                    x0 = x_ref + w_ref;\n                                    for (int j : placed_list) {\n                                        auto [x_pl, y_pl, w_pl, h_pl] = rect_pos[j];\n                                        if (x0 < x_pl + w_pl && x0 + w > x_pl) {\n                                            y0 = max(y0, y_pl + h_pl);\n                                        }\n                                    }\n                                } else {\n                                    y0 = y_ref + h_ref;\n                                    for (int j : placed_list) {\n                                        auto [x_pl, y_pl, w_pl, h_pl] = rect_pos[j];\n                                        if (y0 < y_pl + h_pl && y0 + h > y_pl) {\n                                            x0 = max(x0, x_pl + w_pl);\n                                        }\n                                    }\n                                }\n                            }\n\n                            long long new_max_x = max(current_max_x, x0 + w);\n                            long long new_max_y = max(current_max_y, y0 + h);\n                            long long increase = (new_max_x + new_max_y) - (current_max_x + current_max_y);\n                            candidates_for_rect.push_back({increase, rot, d, b_ref, x0, y0});\n                        }\n                    }\n                }\n\n                if (candidates_for_rect.empty()) continue;\n                sort(candidates_for_rect.begin(), candidates_for_rect.end());\n                auto [min_inc, rot_choice, d_choice, b_ref_choice, x0, y0] = candidates_for_rect[0];\n                \n                long long w = (rot_choice == 0) ? w_sim[idx] : h_sim[idx];\n                long long h = (rot_choice == 0) ? h_sim[idx] : w_sim[idx];\n                long long penalty = w_sim[idx] + h_sim[idx];\n                double adjusted_threshold = threshold;\n                \n                if (penalty > current_max_x + current_max_y) {\n                    adjusted_threshold = 1e9;\n                }\n                else if (w_sim[idx] * h_sim[idx] < 0.1 * (current_max_x * current_max_y + 1)) {\n                    adjusted_threshold *= 0.8;\n                }\n                \n                if (min_inc <= adjusted_threshold * penalty) {\n                    placed[idx] = true;\n                    placed_list.push_back(idx);\n                    rect_pos[idx] = {x0, y0, w, h};\n                    current_max_x = max(current_max_x, x0 + w);\n                    current_max_y = max(current_max_y, y0 + h);\n                    sequence.push_back({idx, rot_choice, d_choice, b_ref_choice});\n                }\n            }\n\n            long long sim_score = current_max_x + current_max_y;\n            for (int i = 0; i < N; i++) {\n                if (!placed[i]) sim_score += w_sim[i] + h_sim[i];\n            }\n            candidates.push_back({sequence, sim_score});\n        }\n\n        sort(candidates.begin(), candidates.end(), [](const auto& a, const auto& b) {\n            return get<1>(a) < get<1>(b);\n        });\n    }\n\n    for (int turn = exploration_turns; turn < T; turn++) {\n        int cand_idx = turn - exploration_turns;\n        if (!candidates.empty()) {\n            int idx = min(cand_idx, (int)candidates.size() - 1);\n            auto& seq = get<0>(candidates[idx]);\n            cout << seq.size() << '\\n';\n            for (auto [p, r, d, b] : seq) {\n                cout << p << \" \" << r << \" \" << d << \" \" << b << '\\n';\n            }\n        } else {\n            cout << 0 << '\\n';\n        }\n        cout.flush();\n\n        long long Wp, Hp;\n        cin >> Wp >> Hp;\n    }\n\n    return 0;\n}","ahc041":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\n#include <utility>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, H;\n    cin >> N >> M >> H;\n\n    vector<int> A(N);\n    for (int i = 0; i < N; i++) {\n        cin >> A[i];\n    }\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    vector<int> x(N), y(N);\n    for (int i = 0; i < N; i++) {\n        cin >> x[i] >> y[i];\n    }\n\n    vector<int> depth(N, -1);\n    vector<int> parent(N, -1);\n    vector<bool> finalized(N, false);\n\n    auto cmp = greater<pair<int, int>>();\n    priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(cmp)> unassigned_pq(cmp);\n    priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(cmp)> bfs_pq(cmp);\n\n    for (int i = 0; i < N; i++) {\n        unassigned_pq.push({A[i], i});\n    }\n\n    int finalized_count = 0;\n\n    while (finalized_count < N) {\n        int v = -1;\n        if (!bfs_pq.empty()) {\n            while (!bfs_pq.empty() && v == -1) {\n                auto [a_val, node] = bfs_pq.top();\n                bfs_pq.pop();\n                if (!finalized[node]) {\n                    v = node;\n                }\n            }\n        }\n        if (v == -1) {\n            while (!unassigned_pq.empty() && v == -1) {\n                auto [a_val, node] = unassigned_pq.top();\n                unassigned_pq.pop();\n                if (!finalized[node]) {\n                    v = node;\n                }\n            }\n        }\n        if (v == -1) break;\n\n        finalized[v] = true;\n        finalized_count++;\n\n        if (depth[v] == -1) {\n            depth[v] = 0;\n            parent[v] = -1;\n        }\n\n        if (depth[v] < H) {\n            for (int w : adj[v]) {\n                if (finalized[w]) continue;\n                int new_depth = depth[v] + 1;\n                if (depth[w] == -1 || new_depth > depth[w]) {\n                    depth[w] = new_depth;\n                    parent[w] = v;\n                    bfs_pq.push({A[w], w});\n                }\n            }\n        }\n    }\n\n    for (int i = 0; i < N; i++) {\n        if (i > 0) cout << \" \";\n        cout << parent[i];\n    }\n    cout << endl;\n\n    return 0;\n}","ahc042":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <map>\n#include <climits>\nusing namespace std;\n\nint main() {\n    const int N = 20;\n    vector<string> board(N);\n    for (int i = 0; i < N; i++) {\n        cin >> board[i];\n    }\n\n    vector<pair<int, int>> onis;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (board[i][j] == 'x') {\n                onis.push_back({i, j});\n            }\n        }\n    }\n\n    map<pair<int, int>, vector<char>> safe_map;\n    for (auto& oni : onis) {\n        int i = oni.first, j = oni.second;\n        vector<char> dirs;\n\n        bool valid = true;\n        for (int r = 0; r < i; r++) {\n            if (board[r][j] == 'o') {\n                valid = false;\n                break;\n            }\n        }\n        if (valid) dirs.push_back('U');\n\n        valid = true;\n        for (int r = i+1; r < N; r++) {\n            if (board[r][j] == 'o') {\n                valid = false;\n                break;\n            }\n        }\n        if (valid) dirs.push_back('D');\n\n        valid = true;\n        for (int c = 0; c < j; c++) {\n            if (board[i][c] == 'o') {\n                valid = false;\n                break;\n            }\n        }\n        if (valid) dirs.push_back('L');\n\n        valid = true;\n        for (int c = j+1; c < N; c++) {\n            if (board[i][c] == 'o') {\n                valid = false;\n                break;\n            }\n        }\n        if (valid) dirs.push_back('R');\n\n        safe_map[oni] = dirs;\n    }\n\n    vector<pair<char, int>> moves;\n    vector<pair<int, int>> remaining_onis = onis;\n\n    while (!remaining_onis.empty()) {\n        map<int, vector<int>> col_up, col_down;\n        map<int, vector<int>> row_left, row_right;\n\n        for (auto& oni : remaining_onis) {\n            int i = oni.first, j = oni.second;\n            vector<char> dirs = safe_map[oni];\n            for (char dir : dirs) {\n                if (dir == 'U') {\n                    col_up[j].push_back(i);\n                } else if (dir == 'D') {\n                    col_down[j].push_back(i);\n                } else if (dir == 'L') {\n                    row_left[i].push_back(j);\n                } else if (dir == 'R') {\n                    row_right[i].push_back(j);\n                }\n            }\n        }\n\n        double best_eff = 1e18;\n        char best_dir = ' ';\n        int best_index = -1;\n        int best_cost = 0;\n        vector<pair<int, int>> best_group;\n\n        for (auto& kv : col_up) {\n            int j = kv.first;\n            vector<int>& rows = kv.second;\n            if (rows.empty()) continue;\n            int max_row = *max_element(rows.begin(), rows.end());\n            int count = rows.size();\n            int cost = 2 * (max_row + 1);\n            double eff = (double)cost / count;\n            if (eff < best_eff) {\n                best_eff = eff;\n                best_dir = 'U';\n                best_index = j;\n                best_cost = cost;\n                best_group.clear();\n                for (int i_val : rows) {\n                    best_group.push_back({i_val, j});\n                }\n            }\n        }\n\n        for (auto& kv : col_down) {\n            int j = kv.first;\n            vector<int>& rows = kv.second;\n            if (rows.empty()) continue;\n            int min_row = *min_element(rows.begin(), rows.end());\n            int count = rows.size();\n            int cost = 2 * (N - min_row);\n            double eff = (double)cost / count;\n            if (eff < best_eff) {\n                best_eff = eff;\n                best_dir = 'D';\n                best_index = j;\n                best_cost = cost;\n                best_group.clear();\n                for (int i_val : rows) {\n                    best_group.push_back({i_val, j});\n                }\n            }\n        }\n\n        for (auto& kv : row_left) {\n            int i = kv.first;\n            vector<int>& cols = kv.second;\n            if (cols.empty()) continue;\n            int max_col = *max_element(cols.begin(), cols.end());\n            int count = cols.size();\n            int cost = 2 * (max_col + 1);\n            double eff = (double)cost / count;\n            if (eff < best_eff) {\n                best_eff = eff;\n                best_dir = 'L';\n                best_index = i;\n                best_cost = cost;\n                best_group.clear();\n                for (int j_val : cols) {\n                    best_group.push_back({i, j_val});\n                }\n            }\n        }\n\n        for (auto& kv : row_right) {\n            int i = kv.first;\n            vector<int>& cols = kv.second;\n            if (cols.empty()) continue;\n            int min_col = *min_element(cols.begin(), cols.end());\n            int count = cols.size();\n            int cost = 2 * (N - min_col);\n            double eff = (double)cost / count;\n            if (eff < best_eff) {\n                best_eff = eff;\n                best_dir = 'R';\n                best_index = i;\n                best_cost = cost;\n                best_group.clear();\n                for (int j_val : cols) {\n                    best_group.push_back({i, j_val});\n                }\n            }\n        }\n\n        if (best_eff == 1e18) {\n            break;\n        }\n\n        if (best_dir == 'U' || best_dir == 'D') {\n            int k = (best_dir == 'U') ? (best_cost / 2) : (best_cost / 2);\n            for (int i = 0; i < k; i++) {\n                moves.push_back({best_dir, best_index});\n            }\n            for (int i = 0; i < k; i++) {\n                moves.push_back({(best_dir == 'U') ? 'D' : 'U', best_index});\n            }\n        } else {\n            int k = (best_dir == 'L') ? (best_cost / 2) : (best_cost / 2);\n            for (int i = 0; i < k; i++) {\n                moves.push_back({best_dir, best_index});\n            }\n            for (int i = 0; i < k; i++) {\n                moves.push_back({(best_dir == 'L') ? 'R' : 'L', best_index});\n            }\n        }\n\n        vector<pair<int, int>> new_remaining;\n        for (auto& oni : remaining_onis) {\n            bool in_group = false;\n            for (auto& g : best_group) {\n                if (oni == g) {\n                    in_group = true;\n                    break;\n                }\n            }\n            if (!in_group) {\n                new_remaining.push_back(oni);\n            }\n        }\n        remaining_onis = new_remaining;\n    }\n\n    for (auto& move : moves) {\n        cout << move.first << \" \" << move.second << \"\\n\";\n    }\n\n    return 0;\n}","ahc044":"#include <iostream>\n#include <vector>\n#include <random>\n#include <cmath>\n#include <algorithm>\nusing namespace std;\n\nlong long simulate(const vector<int>& a, const vector<int>& b, const vector<int>& T, int L, int N) {\n    int total_count[100] = {0};\n    char parity[100] = {0};\n    int next_table[2][100];\n\n    for (int i = 0; i < N; i++) {\n        next_table[0][i] = b[i];\n        next_table[1][i] = a[i];\n    }\n\n    total_count[0] = 1;\n    parity[0] = 1;\n    int current = 0;\n\n    for (int week = 2; week <= L; week++) {\n        int p = parity[current];\n        int next_emp = next_table[p][current];\n        total_count[next_emp]++;\n        parity[next_emp] ^= 1;\n        current = next_emp;\n    }\n\n    long long error = 0;\n    for (int i = 0; i < N; i++) {\n        error += abs(total_count[i] - T[i]);\n    }\n    return error;\n}\n\nint main() {\n    int N, L;\n    cin >> N >> L;\n    vector<int> T(N);\n    for (int i = 0; i < N; i++) {\n        cin >> T[i];\n    }\n\n    random_device rd;\n    mt19937 rng(rd());\n    vector<int> a(N), b(N);\n    uniform_int_distribution<int> emp_dist(0, N-1);\n    for (int i = 0; i < N; i++) {\n        a[i] = emp_dist(rng);\n        b[i] = emp_dist(rng);\n    }\n\n    long long current_error = simulate(a, b, T, L, N);\n    vector<int> best_a = a;\n    vector<int> best_b = b;\n    long long best_error = current_error;\n\n    int iterations = 800;\n    double max_temp = 10000.0;\n    double min_temp = 1.0;\n    uniform_real_distribution<double> real_dist(0.0, 1.0);\n    uniform_int_distribution<int> bit_dist(0, 1);\n    uniform_int_distribution<int> count_dist(1, 10);\n\n    for (int iter = 0; iter < iterations; iter++) {\n        double temp = max_temp * pow(min_temp / max_temp, static_cast<double>(iter) / iterations);\n        vector<int> new_a = a;\n        vector<int> new_b = b;\n\n        int change_count = count_dist(rng) == 1 ? 2 : 1;\n        for (int c = 0; c < change_count; c++) {\n            int i = emp_dist(rng);\n            int which = bit_dist(rng);\n            int old_val;\n            int new_val;\n            if (which == 0) {\n                old_val = new_a[i];\n                do {\n                    new_val = emp_dist(rng);\n                } while (new_val == old_val);\n                new_a[i] = new_val;\n            } else {\n                old_val = new_b[i];\n                do {\n                    new_val = emp_dist(rng);\n                } while (new_val == old_val);\n                new_b[i] = new_val;\n            }\n        }\n\n        long long new_error = simulate(new_a, new_b, T, L, N);\n\n        if (new_error < current_error || \n            real_dist(rng) < exp((current_error - new_error) / temp)) {\n            a = new_a;\n            b = new_b;\n            current_error = new_error;\n            if (new_error < best_error) {\n                best_error = new_error;\n                best_a = a;\n                best_b = b;\n            }\n        }\n    }\n\n    for (int i = 0; i < N; i++) {\n        cout << best_a[i] << \" \" << best_b[i] << endl;\n    }\n\n    return 0;\n}","ahc045":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <cstdint>\nusing namespace std;\n\nuint32_t xy2d(uint32_t n, uint32_t x, uint32_t y) {\n    uint32_t rx, ry, s, d = 0;\n    for (s = n / 2; s > 0; s /= 2) {\n        rx = (x & s) > 0;\n        ry = (y & s) > 0;\n        d += s * s * ((3 * rx) ^ ry);\n        if (ry == 0) {\n            if (rx == 1) {\n                x = s - 1 - x;\n                y = s - 1 - y;\n            }\n            swap(x, y);\n        }\n    }\n    return d;\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, Q_max, L, W;\n    cin >> N >> M >> Q_max >> L >> W;\n    vector<int> G(M);\n    for (int i = 0; i < M; i++) {\n        cin >> G[i];\n    }\n\n    vector<int> lx(N), rx(N), ly(N), ry(N);\n    vector<int> cx(N), cy(N);\n    for (int i = 0; i < N; i++) {\n        cin >> lx[i] >> rx[i] >> ly[i] >> ry[i];\n        cx[i] = (lx[i] + rx[i]) / 2;\n        cy[i] = (ly[i] + ry[i]) / 2;\n    }\n\n    const uint32_t n_hilbert = 1 << 16;\n    const long long scale_val = (1LL << 16) - 1;\n    vector<uint32_t> hilbert_index(N);\n    for (int i = 0; i < N; i++) {\n        long long nx_val = static_cast<long long>(cx[i]) * scale_val;\n        long long ny_val = static_cast<long long>(cy[i]) * scale_val;\n        uint32_t nx = static_cast<uint32_t>(nx_val / 10000);\n        uint32_t ny = static_cast<uint32_t>(ny_val / 10000);\n        hilbert_index[i] = xy2d(n_hilbert, nx, ny);\n    }\n\n    vector<int> cities(N);\n    for (int i = 0; i < N; i++) cities[i] = i;\n    sort(cities.begin(), cities.end(), [&](int i, int j) {\n        return hilbert_index[i] < hilbert_index[j];\n    });\n\n    vector<vector<int>> groups(M);\n    int idx = 0;\n    for (int k = 0; k < M; k++) {\n        groups[k].resize(G[k]);\n        for (int j = 0; j < G[k]; j++) {\n            groups[k][j] = cities[idx++];\n        }\n    }\n\n    int query_count = 0;\n    vector<vector<pair<int, int>>> group_edges(M);\n\n    auto query_cluster = [](const vector<int>& cluster) {\n        vector<int> sorted_cluster = cluster;\n        sort(sorted_cluster.begin(), sorted_cluster.end());\n        cout << \"? \" << sorted_cluster.size();\n        for (int c : sorted_cluster) {\n            cout << \" \" << c;\n        }\n        cout << endl;\n        cout.flush();\n\n        vector<pair<int, int>> edges;\n        for (int i = 0; i < sorted_cluster.size() - 1; i++) {\n            int a, b;\n            cin >> a >> b;\n            edges.push_back({a, b});\n        }\n        return edges;\n    };\n\n    for (int k = 0; k < M; k++) {\n        vector<int>& group = groups[k];\n        int n = group.size();\n        if (n == 1) {\n            continue;\n        }\n\n        vector<pair<int, int>> edges;\n        int i = 0;\n        while (i < n && query_count < Q_max) {\n            int take = min(L, n - i);\n            if (take < 2) break;\n\n            vector<int> cluster;\n            for (int j = 0; j < take; j++) {\n                cluster.push_back(group[i + j]);\n            }\n\n            vector<pair<int, int>> e = query_cluster(cluster);\n            query_count++;\n            for (const auto& edge : e) {\n                edges.push_back(edge);\n            }\n\n            i += (take - 1);\n        }\n\n        if (i < n - 1) {\n            edges.push_back({group[i], group[i + 1]});\n            for (int j = i + 1; j < n - 1; j++) {\n                edges.push_back({group[j], group[j + 1]});\n            }\n        }\n\n        group_edges[k] = edges;\n    }\n\n    cout << \"!\" << endl;\n    for (int k = 0; k < M; k++) {\n        vector<int>& group = groups[k];\n        for (int j = 0; j < group.size(); j++) {\n            if (j > 0) cout << \" \";\n            cout << group[j];\n        }\n        cout << endl;\n\n        vector<pair<int, int>>& edges = group_edges[k];\n        for (const auto& edge : edges) {\n            cout << edge.first << \" \" << edge.second << endl;\n        }\n    }\n\n    return 0;\n}","ahc046":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int N, M;\n    cin >> N >> M;\n    vector<pair<int, int>> points;\n    for (int i = 0; i < M; i++) {\n        int a, b;\n        cin >> a >> b;\n        points.push_back({a, b});\n    }\n\n    vector<vector<bool>> blocks(N, vector<bool>(N, false));\n    map<char, pair<int, int>> dirs;\n    dirs['U'] = {-1, 0};\n    dirs['D'] = {1, 0};\n    dirs['L'] = {0, -1};\n    dirs['R'] = {0, 1};\n\n    auto is_blocked = [&](int i, int j) -> bool {\n        if (i < 0 || i >= N || j < 0 || j >= N)\n            return true;\n        return blocks[i][j];\n    };\n\n    vector<string> outputs;\n    int ci = points[0].first, cj = points[0].second;\n\n    for (int idx = 1; idx < M; idx++) {\n        int ti = points[idx].first, tj = points[idx].second;\n        while (ci != ti || cj != tj) {\n            int current_manhattan = abs(ci - ti) + abs(cj - tj);\n            double best_score = -1e9;\n            char best_act = ' ';\n            char best_dir = ' ';\n\n            for (char d : {'U', 'D', 'L', 'R'}) {\n                int dx = dirs[d].first, dy = dirs[d].second;\n                int ni = ci + dx;\n                int nj = cj + dy;\n\n                if (ni >= 0 && ni < N && nj >= 0 && nj < N) {\n                    if (!blocks[ni][nj]) {\n                        int new_manhattan = abs(ni - ti) + abs(nj - tj);\n                        int reduction = current_manhattan - new_manhattan;\n                        if (reduction > best_score) {\n                            best_score = reduction;\n                            best_act = 'M';\n                            best_dir = d;\n                        }\n\n                        int slide_steps = 1;\n                        int nni = ni, nnj = nj;\n                        while (true) {\n                            nni += dx;\n                            nnj += dy;\n                            if (nni < 0 || nni >= N || nnj < 0 || nnj >= N || blocks[nni][nnj]) {\n                                break;\n                            }\n                            slide_steps++;\n                        }\n                        int slide_i = ci + slide_steps * dx;\n                        int slide_j = cj + slide_steps * dy;\n                        int new_manhattan_slide = abs(slide_i - ti) + abs(slide_j - tj);\n                        int reduction_slide = current_manhattan - new_manhattan_slide;\n                        if (reduction_slide > best_score) {\n                            best_score = reduction_slide;\n                            best_act = 'S';\n                            best_dir = d;\n                        }\n                    } else {\n                        int new_manhattan = abs(ni - ti) + abs(nj - tj);\n                        int reduction = current_manhattan - new_manhattan;\n                        if (reduction > best_score) {\n                            best_score = reduction;\n                            best_act = 'M';\n                            best_dir = d;\n                        }\n\n                        int slide_steps = 1;\n                        int nni = ni, nnj = nj;\n                        while (true) {\n                            nni += dx;\n                            nnj += dy;\n                            if (nni < 0 || nni >= N || nnj < 0 || nnj >= N || blocks[nni][nnj]) {\n                                break;\n                            }\n                            slide_steps++;\n                        }\n                        int slide_i = ci + slide_steps * dx;\n                        int slide_j = cj + slide_steps * dy;\n                        int new_manhattan_slide = abs(slide_i - ti) + abs(slide_j - tj);\n                        int reduction_slide = current_manhattan - new_manhattan_slide;\n                        if (reduction_slide > best_score) {\n                            best_score = reduction_slide;\n                            best_act = 'S';\n                            best_dir = d;\n                        }\n                    }\n                }\n            }\n\n            if (best_score == -1e9) {\n                double best_alter_score = -1e9;\n                char best_alter_dir = 'U';\n                for (char d : {'U', 'D', 'L', 'R'}) {\n                    int dx = dirs[d].first, dy = dirs[d].second;\n                    int ni = ci + dx, nj = cj + dy;\n                    if (ni < 0 || ni >= N || nj < 0 || nj >= N) continue;\n                    int dist_block = abs(ni - ti) + abs(nj - tj);\n                    double alter_score = -dist_block;\n                    if (alter_score > best_alter_score) {\n                        best_alter_score = alter_score;\n                        best_alter_dir = d;\n                    }\n                }\n                best_act = 'A';\n                best_dir = best_alter_dir;\n            }\n\n            outputs.push_back(string(1, best_act) + \" \" + best_dir);\n            int dx = dirs[best_dir].first, dy = dirs[best_dir].second;\n            if (best_act == 'M') {\n                ci = ci + dx;\n                cj = cj + dy;\n            } else if (best_act == 'S') {\n                int steps = 0;\n                int ni = ci, nj = cj;\n                while (true) {\n                    ni += dx;\n                    nj += dy;\n                    if (ni < 0 || ni >= N || nj < 0 || nj >= N || blocks[ni][nj]) {\n                        ni -= dx;\n                        nj -= dy;\n                        break;\n                    }\n                    steps++;\n                }\n                ci = ni;\n                cj = nj;\n            } else if (best_act == 'A') {\n                int ni = ci + dx, nj = cj + dy;\n                blocks[ni][nj] = !blocks[ni][nj];\n            }\n        }\n    }\n\n    for (const string& s : outputs) {\n        cout << s << '\\n';\n    }\n\n    return 0;\n}"}}}