{"model_name":"mistral-small-4","codes":{"1":{"ahc001":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int n;\n    cin >> n;\n    vector<tuple<int, int, int, int>> companies;\n    for (int i = 0; i < n; i++) {\n        int x, y, r;\n        cin >> x >> y >> r;\n        companies.emplace_back(x, y, r, i);\n    }\n\n    vector<vector<pair<int, int>>> all_candidates(n);\n    for (int i = 0; i < n; i++) {\n        auto [x, y, r_val, idx] = companies[i];\n        vector<pair<int, int>> cands;\n        int sw = max(1, (int)sqrt(r_val) - 20);\n        int ew = min(10000, (int)sqrt(r_val) + 20);\n        for (int w = sw; w <= ew; w++) {\n            int h1 = r_val / w;\n            int h2 = (r_val + w - 1) / w;\n            vector<int> hs = {h1 - 2, h1 - 1, h1, h1 + 1, h1 + 2, h2 - 2, h2 - 1, h2, h2 + 1, h2 + 2};\n            for (int h : hs) {\n                if (h < 1) h = 1;\n                if (h > 10000) h = 10000;\n                int area = w * h;\n                cands.emplace_back(w, h);\n                if (w != h) {\n                    cands.emplace_back(h, w);\n                }\n            }\n        }\n        cands.emplace_back(1, 1);\n        sort(cands.begin(), cands.end(), [&](const auto& a, const auto& b) {\n            int err_a = abs(a.first * a.second - r_val);\n            int err_b = abs(b.first * b.second - r_val);\n            if (err_a != err_b) {\n                return err_a < err_b;\n            }\n            return a.first * a.second < b.first * b.second;\n        });\n        auto last = unique(cands.begin(), cands.end(), [](const auto& a, const auto& b) {\n            return a.first == b.first && a.second == b.second;\n        });\n        cands.erase(last, cands.end());\n        if (cands.size() > 40) {\n            cands.resize(40);\n        }\n        all_candidates[i] = cands;\n    }\n\n    sort(companies.begin(), companies.end(), [](const auto& a, const auto& b) {\n        return get<2>(a) > get<2>(b);\n    });\n\n    vector<array<int, 4>> result(n);\n    vector<array<int, 4>> placed;\n\n    vector<int> offsets = {-3, -2, -1, 0, 1, 2, 3};\n\n    for (auto& comp : companies) {\n        int x = get<0>(comp);\n        int y = get<1>(comp);\n        int r_val = get<2>(comp);\n        int idx = get<3>(comp);\n        bool found = false;\n        for (auto [w, h] : all_candidates[idx]) {\n            int Lx = max(0, x + 1 - w);\n            int Rx = min(x, 10000 - w);\n            int Ly = max(0, y + 1 - h);\n            int Ry = min(y, 10000 - h);\n            if (Lx > Rx || Ly > Ry) {\n                continue;\n            }\n\n            double center_x = x + 0.5;\n            double center_y = y + 0.5;\n            double ideal_a = center_x - w / 2.0;\n            double ideal_b = center_y - h / 2.0;\n\n            for (int dx : offsets) {\n                int a_candidate = static_cast<int>(round(ideal_a)) + dx;\n                if (a_candidate < Lx) a_candidate = Lx;\n                if (a_candidate > Rx) a_candidate = Rx;\n                for (int dy : offsets) {\n                    int b_candidate = static_cast<int>(round(ideal_b)) + dy;\n                    if (b_candidate < Ly) b_candidate = Ly;\n                    if (b_candidate > Ry) b_candidate = Ry;\n\n                    bool valid = true;\n                    for (const auto& rect : placed) {\n                        if (max(a_candidate, rect[0]) < min(a_candidate + w, rect[2]) &&\n                            max(b_candidate, rect[1]) < min(b_candidate + h, rect[3])) {\n                            valid = false;\n                            break;\n                        }\n                    }\n                    if (valid) {\n                        result[idx] = {a_candidate, b_candidate, a_candidate + w, b_candidate + h};\n                        placed.push_back({a_candidate, b_candidate, a_candidate + w, b_candidate + h});\n                        found = true;\n                        break;\n                    }\n                }\n                if (found) break;\n            }\n            if (found) break;\n        }\n        if (!found) {\n            result[idx] = {x, y, x + 1, y + 1};\n            placed.push_back({x, y, x + 1, y + 1});\n        }\n    }\n\n    for (int i = 0; i < n; i++) {\n        auto [a, b, c, d] = result[i];\n        cout << a << \" \" << b << \" \" << c << \" \" << d << '\\n';\n    }\n\n    return 0;\n}","ahc002":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\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\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<bool> visited(M, false);\n    int cur_i = si, cur_j = sj;\n    visited[t[cur_i][cur_j]] = true;\n    string path;\n\n    const vector<tuple<int, int, char>> dirs = {\n        {-1, 0, 'U'},\n        {1, 0, 'D'},\n        {0, -1, 'L'},\n        {0, 1, 'R'}\n    };\n\n    while (true) {\n        int best_score = -1;\n        int best_p_val = -1;\n        char best_dir = '?';\n        int best_ni = -1, best_nj = -1;\n\n        for (const auto& [dx, dy, dchar] : dirs) {\n            int ni = cur_i + dx;\n            int nj = cur_j + dy;\n            if (ni < 0 || ni >= 50 || nj < 0 || nj >= 50) continue;\n\n            int tile_id = t[ni][nj];\n            if (visited[tile_id]) continue;\n\n            int next_val = -1;\n            for (const auto& [dx2, dy2, dchar2] : dirs) {\n                int ni2 = ni + dx2;\n                int nj2 = nj + dy2;\n                if (ni2 < 0 || ni2 >= 50 || nj2 < 0 || nj2 >= 50) continue;\n\n                int tile_id2 = t[ni2][nj2];\n                if (visited[tile_id2] || tile_id2 == tile_id) continue;\n\n                if (p[ni2][nj2] > next_val) {\n                    next_val = p[ni2][nj2];\n                }\n            }\n\n            int current_p = p[ni][nj];\n            int score = current_p;\n            if (next_val != -1) {\n                score += next_val;\n            }\n\n            if (score > best_score || (score == best_score && current_p > best_p_val)) {\n                best_score = score;\n                best_p_val = current_p;\n                best_dir = dchar;\n                best_ni = ni;\n                best_nj = nj;\n            }\n        }\n\n        if (best_score == -1) break;\n\n        path += best_dir;\n        cur_i = best_ni;\n        cur_j = best_nj;\n        visited[t[cur_i][cur_j]] = true;\n    }\n\n    cout << path << endl;\n    return 0;\n}","ahc003":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    const int N = 30;\n    double W_h[N][N-1];\n    double W_v[N-1][N];\n\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N-1; j++) {\n            W_h[i][j] = 5000.0;\n        }\n    }\n    for (int i = 0; i < N-1; i++) {\n        for (int j = 0; j < N; j++) {\n            W_v[i][j] = 5000.0;\n        }\n    }\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, 1e18));\n        vector<vector<int>> parent_i(N, vector<int>(N, -1));\n        vector<vector<int>> parent_j(N, vector<int>(N, -1));\n\n        priority_queue<tuple<double, int, int>, vector<tuple<double, int, int>>, greater<tuple<double, int, int>>> pq;\n        dist[si][sj] = 0.0;\n        pq.push(make_tuple(0.0, si, sj));\n\n        const int di[4] = {-1, 1, 0, 0};\n        const int dj[4] = {0, 0, -1, 1};\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 + di[dir];\n                int nj = j + dj[dir];\n                if (ni < 0 || ni >= N || nj < 0 || nj >= N) continue;\n\n                double weight;\n                if (dir == 0) {\n                    weight = W_v[i-1][j];\n                } else if (dir == 1) {\n                    weight = W_v[i][j];\n                } else if (dir == 2) {\n                    weight = W_h[i][j-1];\n                } else {\n                    weight = W_h[i][j];\n                }\n\n                double nd = d + weight;\n                if (nd < dist[ni][nj]) {\n                    dist[ni][nj] = nd;\n                    parent_i[ni][nj] = i;\n                    parent_j[ni][nj] = j;\n                    pq.push(make_tuple(nd, ni, nj));\n                }\n            }\n        }\n\n        string path_str = \"\";\n        vector<tuple<int, int, int>> edges_list;\n        int i = ti, j = tj;\n        while (i != si || j != sj) {\n            int pi = parent_i[i][j];\n            int pj = parent_j[i][j];\n            if (pi == i-1) {\n                path_str += 'D';\n            } else if (pi == i+1) {\n                path_str += 'U';\n            } else if (pj == j-1) {\n                path_str += 'R';\n            } else if (pj == j+1) {\n                path_str += 'L';\n            }\n\n            if (pi == i-1) {\n                edges_list.push_back(make_tuple(0, i-1, j));\n            } else if (pi == i+1) {\n                edges_list.push_back(make_tuple(0, i, j));\n            } else if (pj == j-1) {\n                edges_list.push_back(make_tuple(1, i, j-1));\n            } else if (pj == j+1) {\n                edges_list.push_back(make_tuple(1, i, j));\n            }\n\n            i = pi;\n            j = pj;\n        }\n        reverse(path_str.begin(), path_str.end());\n\n        cout << path_str << \"\\n\";\n        cout.flush();\n\n        long long r;\n        cin >> r;\n\n        int L = path_str.size();\n        double est_length = dist[ti][tj];\n        double error = static_cast<double>(r) - est_length;\n\n        double lr = 0.1 / (k + 1);\n        for (auto &edge : edges_list) {\n            int type = get<0>(edge);\n            int idx1 = get<1>(edge);\n            int idx2 = get<2>(edge);\n            double step = lr * error / static_cast<double>(L);\n\n            if (type == 0) {\n                W_v[idx1][idx2] += step;\n                if (W_v[idx1][idx2] < 1000.0) W_v[idx1][idx2] = 1000.0;\n                else if (W_v[idx1][idx2] > 9000.0) W_v[idx1][idx2] = 9000.0;\n            } else {\n                W_h[idx1][idx2] += step;\n                if (W_h[idx1][idx2] < 1000.0) W_h[idx1][idx2] = 1000.0;\n                else if (W_h[idx1][idx2] > 9000.0) W_h[idx1][idx2] = 9000.0;\n            }\n        }\n    }\n\n    return 0;\n}","ahc004":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int N, M;\n    cin >> N >> M;\n    vector<string> strs(M);\n    for (int i = 0; i < M; i++) {\n        cin >> strs[i];\n    }\n\n    vector<int> indices(M);\n    iota(indices.begin(), indices.end(), 0);\n    sort(indices.begin(), indices.end(), [&](int i, int j) {\n        return strs[i].size() < strs[j].size();\n    });\n\n    vector<string> mat(N, string(N, '.'));\n    vector<bool> covered(M, false);\n    int covered_count = 0;\n\n    for (int idx : indices) {\n        string& s = strs[idx];\n        int L = s.size();\n        int best_dots = -1;\n        int best_dir = -1;\n        int best_i = -1;\n        int best_j = -1;\n\n        for (int i = 0; i < N; i++) {\n            for (int sc = 0; sc < N; sc++) {\n                bool ok = true;\n                int dots = 0;\n                for (int p = 0; p < L; p++) {\n                    int col = (sc + p) % N;\n                    if (mat[i][col] != '.' && mat[i][col] != s[p]) {\n                        ok = false;\n                        break;\n                    }\n                    if (mat[i][col] == '.') {\n                        dots++;\n                    }\n                }\n                if (ok && dots > best_dots) {\n                    best_dots = dots;\n                    best_dir = 0;\n                    best_i = i;\n                    best_j = sc;\n                }\n            }\n        }\n\n        for (int j = 0; j < N; j++) {\n            for (int sr = 0; sr < N; sr++) {\n                bool ok = true;\n                int dots = 0;\n                for (int p = 0; p < L; p++) {\n                    int row = (sr + p) % N;\n                    if (mat[row][j] != '.' && mat[row][j] != s[p]) {\n                        ok = false;\n                        break;\n                    }\n                    if (mat[row][j] == '.') {\n                        dots++;\n                    }\n                }\n                if (ok && dots > best_dots) {\n                    best_dots = dots;\n                    best_dir = 1;\n                    best_i = sr;\n                    best_j = j;\n                }\n            }\n        }\n\n        if (best_dots != -1) {\n            covered[idx] = true;\n            covered_count++;\n            if (best_dir == 0) {\n                for (int p = 0; p < L; p++) {\n                    int col = (best_j + p) % N;\n                    mat[best_i][col] = s[p];\n                }\n            } else {\n                for (int p = 0; p < L; p++) {\n                    int row = (best_i + p) % N;\n                    mat[row][best_j] = s[p];\n                }\n            }\n        }\n    }\n\n    bool improved;\n    do {\n        improved = false;\n        for (int idx = 0; idx < M && !improved; idx++) {\n            if (covered[idx]) continue;\n            string& s = strs[idx];\n            int L = s.size();\n\n            for (int i = 0; i < N && !improved; i++) {\n                for (int sc = 0; sc < N && !improved; sc++) {\n                    bool ok = true;\n                    int dot_count = 0;\n                    int last_r = -1;\n                    int last_c = -1;\n                    for (int p = 0; p < L; p++) {\n                        int col = (sc + p) % N;\n                        if (mat[i][col] != '.' && mat[i][col] != s[p]) {\n                            ok = false;\n                            break;\n                        }\n                        if (mat[i][col] == '.') {\n                            dot_count++;\n                            last_r = i;\n                            last_c = col;\n                        }\n                    }\n                    if (ok && dot_count == 1) {\n                        int p_val = (last_c - sc + N) % N;\n                        mat[last_r][last_c] = s[p_val];\n                        covered[idx] = true;\n                        improved = true;\n                    }\n                }\n            }\n\n            if (improved) continue;\n\n            for (int j = 0; j < N && !improved; j++) {\n                for (int sr = 0; sr < N && !improved; sr++) {\n                    bool ok = true;\n                    int dot_count = 0;\n                    int last_r = -1;\n                    int last_c = -1;\n                    for (int p = 0; p < L; p++) {\n                        int row = (sr + p) % N;\n                        if (mat[row][j] != '.' && mat[row][j] != s[p]) {\n                            ok = false;\n                            break;\n                        }\n                        if (mat[row][j] == '.') {\n                            dot_count++;\n                            last_r = row;\n                            last_c = j;\n                        }\n                    }\n                    if (ok && dot_count == 1) {\n                        int p_val = (last_r - sr + N) % N;\n                        mat[last_r][last_c] = s[p_val];\n                        covered[idx] = true;\n                        improved = true;\n                    }\n                }\n            }\n        }\n    } while (improved);\n\n    if (covered_count == M) {\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    }\n\n    for (int i = 0; i < N; i++) {\n        cout << mat[i] << '\\n';\n    }\n    return 0;\n}","ahc005":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MAX_R = 5000;\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, si, sj;\n    cin >> N >> si >> sj;\n    vector<string> grid(N);\n    for (int i = 0; i < N; i++) {\n        cin >> grid[i];\n    }\n\n    vector<vector<bool>> is_road(N, vector<bool>(N, false));\n    vector<vector<int>> time_grid(N, vector<int>(N, 0));\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (grid[i][j] != '#') {\n                is_road[i][j] = true;\n                time_grid[i][j] = grid[i][j] - '0';\n            }\n        }\n    }\n\n    vector<pair<int, int>> road_cells;\n    map<pair<int, int>, int> cell_to_index;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (is_road[i][j]) {\n                cell_to_index[{i, j}] = road_cells.size();\n                road_cells.push_back({i, j});\n            }\n        }\n    }\n    int R = road_cells.size();\n    if (R == 0) {\n        cout << \"\" << endl;\n        return 0;\n    }\n\n    vector<vector<bitset<MAX_R>>> visible_set(N, vector<bitset<MAX_R>>(N));\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (!is_road[i][j]) continue;\n            int idx_center = cell_to_index[{i, j}];\n            visible_set[i][j].set(idx_center);\n\n            for (int k = j-1; k >= 0; k--) {\n                if (grid[i][k] == '#') break;\n                int idx = cell_to_index[{i, k}];\n                visible_set[i][j].set(idx);\n            }\n            for (int k = j+1; k < N; k++) {\n                if (grid[i][k] == '#') break;\n                int idx = cell_to_index[{i, k}];\n                visible_set[i][j].set(idx);\n            }\n            for (int k = i-1; k >= 0; k--) {\n                if (grid[k][j] == '#') break;\n                int idx = cell_to_index[{k, j}];\n                visible_set[i][j].set(idx);\n            }\n            for (int k = i+1; k < N; k++) {\n                if (grid[k][j] == '#') break;\n                int idx = cell_to_index[{k, j}];\n                visible_set[i][j].set(idx);\n            }\n        }\n    }\n\n    bitset<MAX_R> base_coverage;\n    int cur_i = si, cur_j = sj;\n    if (is_road[cur_i][cur_j]) {\n        int start_idx = cell_to_index[{cur_i, cur_j}];\n        base_coverage.set(start_idx);\n        base_coverage |= visible_set[cur_i][cur_j];\n    }\n\n    string path_so_far = \"\";\n    int total_time_so_far = 0;\n\n    const int D = 4;\n    while (base_coverage.count() < R) {\n        auto cmp = [&](const tuple<int, int, int, bitset<MAX_R>, int, int>& a, \n                       const tuple<int, int, int, bitset<MAX_R>, int, int>& b) {\n            auto [i1, j1, time1, cov1, pi1, pj1] = a;\n            auto [i2, j2, time2, cov2, pi2, pj2] = b;\n            int inc1 = cov1.count() - (base_coverage & cov1).count();\n            int inc2 = cov2.count() - (base_coverage & cov2).count();\n            if (inc1 != inc2) {\n                return inc1 < inc2;\n            }\n            return time1 > time2;\n        };\n        priority_queue<tuple<int, int, int, bitset<MAX_R>, int, int>, \n                       vector<tuple<int, int, int, bitset<MAX_R>, int, int>>, \n                       decltype(cmp)> pq(cmp);\n\n        bitset<MAX_R> start_cov;\n        pq.emplace(cur_i, cur_j, 0, start_cov, -1, -1);\n\n        tuple<int, int, int, bitset<MAX_R>, int, int> best_found = pq.top();\n        int best_incremental = -1;\n\n        while (!pq.empty()) {\n            auto [i, j, time_so_far, cov, pi, pj] = pq.top();\n            pq.pop();\n            int inc = cov.count() - (base_coverage & cov).count();\n            if (inc > best_incremental || (inc == best_incremental && time_so_far < get<2>(best_found))) {\n                best_incremental = inc;\n                best_found = make_tuple(i, j, time_so_far, cov, pi, pj);\n            }\n            if (get<0>(best_found) == i && get<1>(best_found) == j && \n                get<2>(best_found) == time_so_far && \n                get<3>(best_found) == cov) {\n                if (best_incremental == 0) {\n                    break;\n                }\n            }\n\n            if (pi == -2) {\n                continue;\n            }\n            if (pi != -1) {\n            } else {\n                pi = -2;\n            }\n\n            for (int d = 0; d < 4; d++) {\n                int ni = i, nj = j;\n                if (d == 0) ni--;\n                else if (d == 1) ni++;\n                else if (d == 2) nj--;\n                else if (d == 3) nj++;\n\n                if (ni < 0 || ni >= N || nj < 0 || nj >= N) continue;\n                if (!is_road[ni][nj]) continue;\n\n                int new_time = time_so_far + time_grid[ni][nj];\n                bitset<MAX_R> new_cov = cov;\n                new_cov |= visible_set[ni][nj];\n\n                pq.emplace(ni, nj, new_time, new_cov, i, j);\n            }\n        }\n\n        if (best_incremental <= 0) {\n            break;\n        }\n\n        auto [i_best, j_best, time_best, cov_best, pi_best, pj_best] = best_found;\n        vector<pair<int, int>> path_to_best;\n        int cur_i_temp = i_best, cur_j_temp = j_best;\n        int prev_i = pi_best, prev_j = pj_best;\n        while (prev_i != -1 || prev_j != -1) {\n            path_to_best.push_back({cur_i_temp, cur_j_temp});\n            if (prev_i == -1 && prev_j == -1) break;\n            int next_i = prev_i;\n            int next_j = prev_j;\n            cur_i_temp = prev_i;\n            cur_j_temp = prev_j;\n            prev_i = (path_to_best.size() == 1) ? -1 : get<4>(pq.top()); \n            prev_j = (path_to_best.size() == 1) ? -1 : get<5>(pq.top());\n            pq.pop();\n        }\n        reverse(path_to_best.begin(), path_to_best.end());\n\n        if (path_to_best.empty()) {\n            break;\n        }\n\n        int next_i = path_to_best[0].first;\n        int next_j = path_to_best[0].second;\n\n        char dir;\n        if (next_i == cur_i - 1) dir = 'U';\n        else if (next_i == cur_i + 1) dir = 'D';\n        else if (next_j == cur_j - 1) dir = 'L';\n        else if (next_j == cur_j + 1) dir = 'R';\n        else {\n            dir = 'R';\n        }\n\n        path_so_far += dir;\n        total_time_so_far += time_grid[next_i][next_j];\n        base_coverage |= visible_set[next_i][next_j];\n        cur_i = next_i;\n        cur_j = next_j;\n\n        if (base_coverage.count() == R) {\n            break;\n        }\n    }\n\n    vector<vector<int>> dist(N, vector<int>(N, INT_MAX));\n    vector<vector<pair<int, int>>> parent(N, vector<pair<int, int>>(N, {-1, -1}));\n    priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<>> pq_dijk;\n    dist[cur_i][cur_j] = 0;\n    pq_dijk.emplace(0, cur_i, cur_j);\n\n    while (!pq_dijk.empty()) {\n        auto [d, i, j] = pq_dijk.top();\n        pq_dijk.pop();\n        if (d != dist[i][j]) continue;\n        if (i == si && j == sj) break;\n\n        int dx[] = {-1, 1, 0, 0};\n        int dy[] = {0, 0, -1, 1};\n        for (int k = 0; k < 4; k++) {\n            int ni = i + dx[k];\n            int nj = j + dy[k];\n            if (ni < 0 || ni >= N || nj < 0 || nj >= N) continue;\n            if (!is_road[ni][nj]) continue;\n            int nd = d + time_grid[ni][nj];\n            if (nd < dist[ni][nj]) {\n                dist[ni][nj] = nd;\n                parent[ni][nj] = {i, j};\n                pq_dijk.emplace(nd, ni, nj);\n            }\n        }\n    }\n\n    string return_path = \"\";\n    int i = si, j = sj;\n    while (i != cur_i || j != cur_j) {\n        auto [pi, pj] = parent[i][j];\n        if (pi == i-1) return_path += 'D';\n        else if (pi == i+1) return_path += 'U';\n        else if (pj == j-1) return_path += 'R';\n        else if (pj == j+1) return_path += 'L';\n        i = pi;\n        j = pj;\n    }\n    reverse(return_path.begin(), return_path.end());\n\n    string full_path = path_so_far + return_path;\n    cout << full_path << endl;\n\n    return 0;\n}","future-contest-2022-qual":"#include <bits/stdc++.h>\nusing namespace std;\n\nvoid update_skill(vector<int>& s, const vector<int>& d_task, int t_actual, int K) {\n    int low_bound, high_bound;\n    if (t_actual == 1) {\n        low_bound = 0;\n        high_bound = 3;\n    } else {\n        low_bound = max(1, t_actual - 3);\n        high_bound = t_actual + 3;\n    }\n\n    int w_est = 0;\n    for (int k = 0; k < K; k++) {\n        if (d_task[k] > s[k]) {\n            w_est += (d_task[k] - s[k]);\n        }\n    }\n\n    if (w_est < low_bound) {\n        int needed = low_bound - w_est;\n        while (needed > 0) {\n            int best_k = -1;\n            int best_gain = 0;\n            for (int k = 0; k < K; k++) {\n                int def_old = max(0, d_task[k] - s[k]);\n                int def_new = max(0, d_task[k] - (s[k] - 1));\n                int gain = def_new - def_old;\n                if (gain > best_gain) {\n                    best_gain = gain;\n                    best_k = k;\n                }\n            }\n            if (best_k == -1) break;\n            s[best_k]--;\n            needed--;\n        }\n    } else if (w_est > high_bound) {\n        int needed = w_est - high_bound;\n        while (needed > 0) {\n            int best_k = -1;\n            int best_gain = 0;\n            for (int k = 0; k < K; k++) {\n                int def_old = max(0, d_task[k] - s[k]);\n                int def_new = max(0, d_task[k] - (s[k] + 1));\n                int gain = def_old - def_new;\n                if (gain > best_gain) {\n                    best_gain = gain;\n                    best_k = k;\n                }\n            }\n            if (best_k == -1) break;\n            s[best_k]++;\n            needed--;\n        }\n    }\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>> d(N, vector<int>(K));\n    for (int i = 0; i < N; i++) {\n        for (int k = 0; k < K; k++) {\n            cin >> d[i][k];\n        }\n    }\n\n    vector<vector<int>> children(N);\n    vector<vector<int>> parents(N);\n    for (int i = 0; i < R; i++) {\n        int u, v;\n        cin >> u >> v;\n        u--; v--;\n        children[u].push_back(v);\n        parents[v].push_back(u);\n    }\n\n    vector<int> size_vec(N, 1);\n    for (int i = N-1; i >= 0; i--) {\n        for (int child : children[i]) {\n            size_vec[i] += size_vec[child];\n        }\n    }\n\n    vector<int> in_degree_count(N, 0);\n    for (int i = 0; i < N; i++) {\n        in_degree_count[i] = parents[i].size();\n    }\n\n    vector<int> busy_until(M, 1);\n    vector<int> start_day(M, -1);\n    vector<int> current_task(M, -1);\n    vector<vector<int>> skill_est(M, vector<int>(K, 0));\n    vector<bool> completed(N, false);\n    vector<int> ready_vec;\n\n    for (int i = 0; i < N; i++) {\n        if (in_degree_count[i] == 0) {\n            ready_vec.push_back(i);\n        }\n    }\n\n    for (int day = 1; day <= 2000; day++) {\n        vector<int> free_members;\n        for (int j = 0; j < M; j++) {\n            if (busy_until[j] <= day) {\n                free_members.push_back(j);\n            }\n        }\n\n        sort(ready_vec.begin(), ready_vec.end(), [&](int a, int b) {\n            return size_vec[a] > size_vec[b];\n        });\n\n        vector<pair<int, int>> assignments;\n        vector<int> current_free = free_members;\n        vector<bool> free_flag(M, false);\n        for (int j : free_members) {\n            free_flag[j] = true;\n        }\n\n        int idx = 0;\n        while (idx < (int)ready_vec.size() && !current_free.empty()) {\n            int task = ready_vec[idx];\n            int best_member = -1;\n            int best_w = INT_MAX;\n\n            for (int j : current_free) {\n                int w = 0;\n                for (int k = 0; k < K; k++) {\n                    if (d[task][k] > skill_est[j][k]) {\n                        w += (d[task][k] - skill_est[j][k]);\n                    }\n                }\n                if (w < best_w) {\n                    best_w = w;\n                    best_member = j;\n                }\n            }\n\n            if (best_member == -1) {\n                idx++;\n                continue;\n            }\n\n            assignments.push_back({best_member, task});\n            auto it = find(current_free.begin(), current_free.end(), best_member);\n            if (it != current_free.end()) {\n                *it = current_free.back();\n                current_free.pop_back();\n            }\n            free_flag[best_member] = false;\n\n            start_day[best_member] = day;\n            current_task[best_member] = task;\n            int t_est = (best_w == 0) ? 1 : best_w;\n            busy_until[best_member] = day + t_est;\n\n            swap(ready_vec[idx], ready_vec.back());\n            ready_vec.pop_back();\n        }\n\n        cout << assignments.size();\n        for (auto &p : assignments) {\n            cout << \" \" << p.first + 1 << \" \" << p.second + 1;\n        }\n        cout << endl;\n        cout.flush();\n\n        int n_completed;\n        cin >> n_completed;\n        if (n_completed == -1) {\n            break;\n        }\n\n        vector<int> completed_members(n_completed);\n        for (int i = 0; i < n_completed; i++) {\n            cin >> completed_members[i];\n            completed_members[i]--;\n        }\n\n        for (int j : completed_members) {\n            if (current_task[j] == -1) {\n                continue;\n            }\n            int i_task = current_task[j];\n            int t_actual = day - start_day[j] + 1;\n            update_skill(skill_est[j], d[i_task], t_actual, K);\n\n            completed[i_task] = true;\n            for (int child : children[i_task]) {\n                in_degree_count[child]--;\n                if (in_degree_count[child] == 0) {\n                    ready_vec.push_back(child);\n                }\n            }\n\n            busy_until[j] = day + 1;\n            start_day[j] = -1;\n            current_task[j] = -1;\n        }\n    }\n\n    return 0;\n}","ahc006":"#include <bits/stdc++.h>\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 dist(const Point& a, const Point& b) {\n    return abs(a.x - b.x) + abs(a.y - b.y);\n}\n\nint main() {\n    vector<tuple<int, int, int, int>> orders;\n    for (int i = 0; i < 1000; i++) {\n        int a, b, c, d;\n        cin >> a >> b >> c >> d;\n        orders.emplace_back(a, b, c, d);\n    }\n\n    vector<Point> route = { Point(400, 400), Point(400, 400) };\n    vector<bool> selected(1000, false);\n    vector<int> pos_r(1000, -1);\n    vector<int> pos_d(1000, -1);\n\n    for (int iter = 0; iter < 50; iter++) {\n        long long best_delta = 1e18;\n        int best_order = -1;\n        int best_i = -1;\n        int best_j = -1;\n        int L_route = route.size();\n\n        for (int idx = 0; idx < 1000; idx++) {\n            if (selected[idx]) continue;\n            auto [a, b, c, d] = orders[idx];\n            long long min_delta = 1e18;\n            int min_i = -1;\n            int min_j = -1;\n\n            for (int i = 1; i < L_route; i++) {\n                int dr;\n                if (i == 1) {\n                    dr = dist(route[0], Point(a, b)) + dist(Point(a, b), route[1]) - dist(route[0], route[1]);\n                } else {\n                    dr = dist(route[i-1], Point(a, b)) + dist(Point(a, b), route[i]) - dist(route[i-1], route[i]);\n                }\n\n                for (int j = i+1; j < L_route; j++) {\n                    Point prev, next;\n                    if (j-1 < i) {\n                        prev = route[j-1];\n                    } else if (j-1 == i) {\n                        prev = Point(a, b);\n                    } else {\n                        prev = route[j-2];\n                    }\n\n                    if (j < i) {\n                        next = route[j];\n                    } else if (j == i) {\n                        next = Point(a, b);\n                    } else {\n                        next = route[j-1];\n                    }\n\n                    int dd = dist(prev, Point(c, d)) + dist(Point(c, d), next) - dist(prev, next);\n                    long long total_delta = (long long)dr + dd;\n                    if (total_delta < min_delta) {\n                        min_delta = total_delta;\n                        min_i = i;\n                        min_j = j;\n                    }\n                }\n            }\n\n            if (min_delta < best_delta) {\n                best_delta = min_delta;\n                best_order = idx;\n                best_i = min_i;\n                best_j = min_j;\n            }\n        }\n\n        if (best_order == -1) {\n            for (int idx = 0; idx < 1000; idx++) {\n                if (!selected[idx]) {\n                    best_order = idx;\n                    best_i = 1;\n                    best_j = 2;\n                    break;\n                }\n            }\n        }\n\n        auto [a, b, c, d] = orders[best_order];\n        selected[best_order] = true;\n        route.insert(route.begin() + best_i, Point(a, b));\n        route.insert(route.begin() + best_j, Point(c, d));\n        pos_r[best_order] = best_i;\n        pos_d[best_order] = best_j;\n        L_route = route.size();\n    }\n\n    auto compute_dist = [&](const vector<Point>& r) {\n        int d = 0;\n        for (int i = 0; i < (int)r.size() - 1; i++) {\n            d += dist(r[i], r[i+1]);\n        }\n        return d;\n    };\n    int current_dist = compute_dist(route);\n\n    bool improved;\n    int passes = 0;\n    do {\n        improved = false;\n        int n = route.size();\n        for (int i = 0; i < n-1; i++) {\n            for (int j = i+2; j < n-1; j++) {\n                vector<int> new_pos_r = pos_r;\n                vector<int> new_pos_d = pos_d;\n                bool valid = true;\n\n                for (int idx = 0; idx < 1000; idx++) {\n                    if (!selected[idx]) continue;\n                    if (new_pos_r[idx] >= i+1 && new_pos_r[idx] <= j) {\n                        new_pos_r[idx] = j - (new_pos_r[idx] - (i+1));\n                    }\n                    if (new_pos_d[idx] >= i+1 && new_pos_d[idx] <= j) {\n                        new_pos_d[idx] = j - (new_pos_d[idx] - (i+1));\n                    }\n                    if (new_pos_r[idx] >= new_pos_d[idx]) {\n                        valid = false;\n                        break;\n                    }\n                }\n                if (!valid) continue;\n\n                int old1 = dist(route[i], route[i+1]);\n                int old2 = dist(route[j], route[j+1]);\n                int new1 = dist(route[i], route[j]);\n                int new2 = dist(route[i+1], route[j+1]);\n                int change = (new1 + new2) - (old1 + old2);\n\n                if (change < 0) {\n                    reverse(route.begin() + i+1, route.begin() + j+1);\n                    pos_r = new_pos_r;\n                    pos_d = new_pos_d;\n                    current_dist += change;\n                    improved = true;\n                    i = n;\n                    break;\n                }\n            }\n            if (improved) break;\n        }\n        passes++;\n    } while (improved && passes < 1000);\n\n    vector<int> selected_indices;\n    for (int i = 0; i < 1000; i++) {\n        if (selected[i]) {\n            selected_indices.push_back(i+1);\n        }\n    }\n    sort(selected_indices.begin(), selected_indices.end());\n\n    cout << 50;\n    for (int idx : selected_indices) {\n        cout << \" \" << idx;\n    }\n    cout << \"\\n\";\n\n    int n_route = route.size();\n    cout << n_route;\n    for (int i = 0; i < n_route; i++) {\n        cout << \" \" << route[i].x << \" \" << route[i].y;\n    }\n    cout << \"\\n\";\n\n    return 0;\n}","ahc007":"#include <iostream>\n#include <vector>\n#include <cmath>\n#include <algorithm>\n#include <functional>\nusing namespace std;\n\nstruct Edge {\n    int u, v;\n};\n\nint main() {\n    const int N = 400;\n    const int M = 1995;\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<Edge> edges(M);\n    vector<int> d(M, 0);\n\n    for (int i = 0; i < M; i++) {\n        cin >> edges[i].u >> edges[i].v;\n    }\n\n    for (int i = 0; i < M; i++) {\n        int u = edges[i].u;\n        int v = edges[i].v;\n        long long dx = (long long)x[u] - x[v];\n        long long dy = (long long)y[u] - y[v];\n        double dist = sqrt(dx*dx + dy*dy);\n        d[i] = (int)(dist + 0.5);\n    }\n\n    vector<int> parent(N);\n    vector<int> size_vec(N, 1);\n    vector<int> mout_comp(N, 10000000);\n\n    for (int i = 0; i < N; i++) {\n        parent[i] = i;\n    }\n\n    for (int i = 0; i < M; i++) {\n        int u = edges[i].u;\n        int v = edges[i].v;\n        if (d[i] < mout_comp[u]) mout_comp[u] = d[i];\n        if (d[i] < mout_comp[v]) mout_comp[v] = d[i];\n    }\n\n    function<int(int)> find = [&](int x) -> int {\n        if (parent[x] != x) {\n            parent[x] = find(parent[x]);\n        }\n        return parent[x];\n    };\n\n    int count = 0;\n\n    for (int i = 0; i < M; i++) {\n        int l;\n        cin >> l;\n\n        int u = edges[i].u;\n        int v = edges[i].v;\n        int ru = find(u);\n        int rv = find(v);\n\n        if (ru == rv) {\n            cout << 0 << endl;\n            cout.flush();\n            continue;\n        }\n\n        if (count == N - 1) {\n            cout << 0 << endl;\n            cout.flush();\n            continue;\n        }\n\n        int m = min(mout_comp[ru], mout_comp[rv]);\n\n        if (l <= m) {\n            cout << 1 << endl;\n            cout.flush();\n            count++;\n\n            if (size_vec[ru] < size_vec[rv]) {\n                parent[ru] = rv;\n                mout_comp[rv] = m;\n                size_vec[rv] += size_vec[ru];\n            } else {\n                parent[rv] = ru;\n                mout_comp[ru] = m;\n                size_vec[ru] += size_vec[rv];\n            }\n        } else {\n            cout << 0 << endl;\n            cout.flush();\n        }\n    }\n\n    return 0;\n}","ahc008":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int H = 30;\nconst int W = 30;\nint dx[4] = {-1, 1, 0, 0};\nint dy[4] = {0, 0, -1, 1};\nchar move_dir[4] = {'U', 'D', 'L', 'R'};\nchar wall_dir[4] = {'u', 'd', 'l', 'r'};\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N;\n    cin >> N;\n    vector<pair<int, int>> pets;\n    for (int i = 0; i < N; i++) {\n        int x, y, t;\n        cin >> x >> y >> t;\n        pets.push_back({x, y});\n    }\n\n    int M;\n    cin >> M;\n    vector<pair<int, int>> humans;\n    for (int i = 0; i < M; i++) {\n        int x, y;\n        cin >> x >> y;\n        humans.push_back({x, y});\n    }\n\n    bool grid[H+1][W+1];\n    for (int i = 1; i <= H; i++) {\n        for (int j = 1; j <= W; j++) {\n            grid[i][j] = true;\n        }\n    }\n\n    for (int turn = 0; turn < 300; turn++) {\n        string actions = \"\";\n        for (int i = 0; i < M; i++) {\n            vector<char> candidates;\n            candidates.push_back('.');\n\n            for (int d = 0; d < 4; d++) {\n                int nx = humans[i].first + dx[d];\n                int ny = humans[i].second + dy[d];\n                if (nx >= 1 && nx <= H && ny >= 1 && ny <= W && grid[nx][ny]) {\n                    candidates.push_back(move_dir[d]);\n                }\n            }\n\n            for (int d = 0; d < 4; d++) {\n                int wx = humans[i].first + dx[d];\n                int wy = humans[i].second + dy[d];\n                if (wx < 1 || wx > H || wy < 1 || wy > W) continue;\n                if (!grid[wx][wy]) continue;\n\n                bool occupied = false;\n                for (int j = 0; j < M; j++) {\n                    if (humans[j].first == wx && humans[j].second == wy) {\n                        occupied = true;\n                        break;\n                    }\n                }\n                if (occupied) continue;\n                for (int j = 0; j < N; j++) {\n                    if (pets[j].first == wx && pets[j].second == wy) {\n                        occupied = true;\n                        break;\n                    }\n                }\n                if (occupied) continue;\n\n                bool adjacent_pet = false;\n                for (int dd = 0; dd < 4; dd++) {\n                    int nx = wx + dx[dd];\n                    int ny = wy + dy[dd];\n                    if (nx < 1 || nx > H || ny < 1 || ny > W) continue;\n                    for (int j = 0; j < N; j++) {\n                        if (pets[j].first == nx && pets[j].second == ny) {\n                            adjacent_pet = true;\n                            break;\n                        }\n                    }\n                    if (adjacent_pet) break;\n                }\n                if (adjacent_pet) continue;\n\n                candidates.push_back(wall_dir[d]);\n            }\n\n            double best_score = -1e18;\n            char best_act = '.';\n            for (char act : candidates) {\n                bool temp_grid[H+1][W+1];\n                for (int x = 1; x <= H; x++) {\n                    for (int y = 1; y <= W; y++) {\n                        temp_grid[x][y] = grid[x][y];\n                    }\n                }\n                pair<int, int> start_pos = humans[i];\n\n                if (act == '.') {\n                } else if (act >= 'A' && act <= 'Z') {\n                    int d;\n                    if (act == 'U') d = 0;\n                    else if (act == 'D') d = 1;\n                    else if (act == 'L') d = 2;\n                    else d = 3;\n                    int nx = start_pos.first + dx[d];\n                    int ny = start_pos.second + dy[d];\n                    start_pos = {nx, ny};\n                } else {\n                    int d;\n                    if (act == 'u') d = 0;\n                    else if (act == 'd') d = 1;\n                    else if (act == 'l') d = 2;\n                    else d = 3;\n                    int wx = start_pos.first + dx[d];\n                    int wy = start_pos.second + dy[d];\n                    temp_grid[wx][wy] = false;\n                }\n\n                bool visited[H+1][W+1] = {};\n                int comp_size = 0;\n                int pet_count = 0;\n                queue<pair<int, int>> q;\n                q.push(start_pos);\n                visited[start_pos.first][start_pos.second] = true;\n\n                while (!q.empty()) {\n                    auto [x, y] = q.front();\n                    q.pop();\n                    comp_size++;\n                    for (int j = 0; j < N; j++) {\n                        if (pets[j].first == x && pets[j].second == y) {\n                            pet_count++;\n                            break;\n                        }\n                    }\n                    for (int d = 0; d < 4; d++) {\n                        int nx = x + dx[d];\n                        int ny = y + dy[d];\n                        if (nx >= 1 && nx <= H && ny >= 1 && ny <= W && !visited[nx][ny] && temp_grid[nx][ny]) {\n                            visited[nx][ny] = true;\n                            q.push({nx, ny});\n                        }\n                    }\n                }\n\n                double factor = pow(2.0, -pet_count);\n                double score = comp_size * factor;\n\n                if (score > best_score) {\n                    best_score = score;\n                    best_act = act;\n                }\n            }\n            actions += best_act;\n        }\n\n        cout << actions << endl;\n        cout.flush();\n\n        vector<string> pet_moves(N);\n        for (int i = 0; i < N; i++) {\n            cin >> pet_moves[i];\n        }\n\n        for (int i = 0; i < M; i++) {\n            if (actions[i] >= 'a' && actions[i] <= 'z') {\n                int d;\n                if (actions[i] == 'u') d = 0;\n                else if (actions[i] == 'd') d = 1;\n                else if (actions[i] == 'l') d = 2;\n                else d = 3;\n                int wx = humans[i].first + dx[d];\n                int wy = humans[i].second + dy[d];\n                grid[wx][wy] = false;\n            }\n        }\n\n        for (int i = 0; i < M; i++) {\n            if (actions[i] >= 'A' && actions[i] <= 'Z') {\n                int d;\n                if (actions[i] == 'U') d = 0;\n                else if (actions[i] == 'D') d = 1;\n                else if (actions[i] == 'L') d = 2;\n                else d = 3;\n                int nx = humans[i].first + dx[d];\n                int ny = humans[i].second + dy[d];\n                if (nx >= 1 && nx <= H && ny >= 1 && ny <= W && grid[nx][ny]) {\n                    humans[i] = {nx, ny};\n                }\n            }\n        }\n\n        for (int i = 0; i < N; i++) {\n            int x = pets[i].first;\n            int y = pets[i].second;\n            string& moves = pet_moves[i];\n            for (char c : moves) {\n                int nx = x, ny = y;\n                if (c == 'U') nx--;\n                else if (c == 'D') nx++;\n                else if (c == 'L') ny--;\n                else if (c == 'R') ny++;\n                if (nx >= 1 && nx <= H && ny >= 1 && ny <= W && grid[nx][ny]) {\n                    x = nx;\n                    y = ny;\n                }\n            }\n            pets[i] = {x, y};\n        }\n    }\n\n    return 0;\n}","ahc009":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int si, sj, ti, tj;\n    double p;\n    cin >> si >> sj >> ti >> tj >> 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<int>> dist(20, vector<int>(20, -1));\n    vector<vector<pair<int, int>>> prev(20, vector<pair<int, int>>(20, make_pair(-1, -1)));\n    vector<vector<char>> move_from(20, vector<char>(20, '?'));\n\n    queue<pair<int, int>> q;\n    dist[si][sj] = 0;\n    q.push({si, sj});\n\n    const vector<int> di = {-1, 1, 0, 0};\n    const vector<int> dj = {0, 0, -1, 1};\n    const vector<char> dir_char = {'U', 'D', 'L', 'R'};\n\n    while (!q.empty()) {\n        auto [i, j] = q.front();\n        q.pop();\n        if (i == ti && j == tj) 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 >= 20 || nj < 0 || nj >= 20) \n                continue;\n\n            bool can_move = true;\n            if (d == 0) {\n                if (i == 0) can_move = false;\n                else if (v[i-1][j] == '1') can_move = false;\n            } else if (d == 1) {\n                if (i == 19) can_move = false;\n                else if (v[i][j] == '1') can_move = false;\n            } else if (d == 2) {\n                if (j == 0) can_move = false;\n                else if (h[i][j-1] == '1') can_move = false;\n            } else if (d == 3) {\n                if (j == 19) can_move = false;\n                else if (h[i][j] == '1') can_move = false;\n            }\n\n            if (!can_move) continue;\n\n            if (dist[ni][nj] == -1) {\n                dist[ni][nj] = dist[i][j] + 1;\n                prev[ni][nj] = {i, j};\n                move_from[ni][nj] = dir_char[d];\n                q.push({ni, nj});\n            }\n        }\n    }\n\n    string path = \"\";\n    int i = ti, j = tj;\n    while (i != si || j != sj) {\n        char c = move_from[i][j];\n        path += c;\n        auto [pi, pj] = prev[i][j];\n        i = pi;\n        j = pj;\n    }\n    reverse(path.begin(), path.end());\n\n    if (path.empty()) {\n        cout << \"\" << endl;\n        return 0;\n    }\n\n    int d = path.size();\n    int m = 200 / d;\n    string ans;\n    ans.reserve(d * m);\n    for (char c : path) {\n        ans.append(m, c);\n    }\n    cout << ans << endl;\n\n    return 0;\n}","ahc010":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int di[4] = {0, -1, 0, 1};\nconst int dj[4] = {-1, 0, 1, 0};\n\nconst vector<vector<int>> to = {\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\nint effective_type(int t, int r) {\n    if (t < 4) {\n        return (t + r) % 4;\n    } else if (t == 4 || t == 5) {\n        if (r % 2 == 0) \n            return t;\n        else \n            return (t == 4 ? 5 : 4);\n    } else {\n        if (r % 2 == 0) \n            return t;\n        else \n            return (t == 6 ? 7 : 6);\n    }\n}\n\nvector<int> compute_cycles(const vector<string>& initial_types, const vector<vector<int>>& rotations) {\n    int grid_type[30][30];\n    for (int i = 0; i < 30; i++) {\n        for (int j = 0; j < 30; j++) {\n            int t = initial_types[i][j] - '0';\n            grid_type[i][j] = effective_type(t, rotations[i][j]);\n        }\n    }\n\n    bool visited[30][30][4] = {};\n    vector<int> cycles;\n\n    for (int i = 0; i < 30; i++) {\n        for (int j = 0; j < 30; j++) {\n            for (int d = 0; d < 4; d++) {\n                if (visited[i][j][d]) \n                    continue;\n\n                vector<tuple<int, int, int>> path;\n                int ci = i, cj = j, cd = d;\n                bool broken = false;\n\n                while (!visited[ci][cj][cd]) {\n                    visited[ci][cj][cd] = true;\n                    path.push_back(make_tuple(ci, cj, cd));\n\n                    int s = grid_type[ci][cj];\n                    int nd = to[s][cd];\n                    if (nd == -1) {\n                        broken = true;\n                        break;\n                    }\n\n                    int ni = ci + di[nd];\n                    int nj = cj + dj[nd];\n                    if (ni < 0 || ni >= 30 || nj < 0 || nj >= 30) {\n                        broken = true;\n                        break;\n                    }\n\n                    cd = (nd + 2) % 4;\n                    ci = ni;\n                    cj = nj;\n                }\n\n                if (broken) {\n                    continue;\n                }\n\n                bool found = false;\n                int idx = -1;\n                for (int k = 0; k < path.size(); k++) {\n                    auto [x, y, dir] = path[k];\n                    if (x == ci && y == cj && dir == cd) {\n                        found = true;\n                        idx = k;\n                        break;\n                    }\n                }\n\n                if (found) {\n                    int cycle_length = static_cast<int>(path.size()) - idx;\n                    cycles.push_back(cycle_length);\n                }\n            }\n        }\n    }\n    return cycles;\n}\n\nlong long compute_score(const vector<int>& cycles) {\n    if (cycles.size() < 2) {\n        return 0;\n    }\n    vector<int> sorted = cycles;\n    sort(sorted.begin(), sorted.end(), greater<int>());\n    return 1LL * sorted[0] * sorted[1];\n}\n\nint main() {\n    vector<string> initial_types(30);\n    for (int i = 0; i < 30; i++) {\n        cin >> initial_types[i];\n    }\n\n    vector<vector<int>> rotations(30, vector<int>(30, 0));\n    auto cycles = compute_cycles(initial_types, rotations);\n    long long current_score = compute_score(cycles);\n\n    const int passes = 10;\n    for (int iter = 0; iter < passes; iter++) {\n        for (int i = 0; i < 30; i++) {\n            for (int j = 0; j < 30; j++) {\n                int old_r = rotations[i][j];\n                int best_r = old_r;\n                long long best_score = current_score;\n\n                for (int r = 0; r < 4; r++) {\n                    if (r == old_r) \n                        continue;\n\n                    rotations[i][j] = r;\n                    auto new_cycles = compute_cycles(initial_types, rotations);\n                    long long new_score = compute_score(new_cycles);\n                    rotations[i][j] = old_r;\n\n                    if (new_score > best_score) {\n                        best_score = new_score;\n                        best_r = r;\n                    }\n                }\n\n                if (best_r != old_r) {\n                    rotations[i][j] = best_r;\n                    current_score = best_score;\n                }\n            }\n        }\n    }\n\n    string output;\n    for (int i = 0; i < 30; i++) {\n        for (int j = 0; j < 30; j++) {\n            output += ('0' + rotations[i][j]);\n        }\n    }\n    cout << output << endl;\n    return 0;\n}","ahc011":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <random>\n#include <climits>\n#include <tuple>\n#include <cctype>\n#include <cmath>\nusing namespace std;\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, T;\n    cin >> N >> T;\n    vector<string> grid_str(N);\n    for (int i = 0; i < N; i++) {\n        cin >> grid_str[i];\n    }\n\n    vector<vector<int>> grid(N, vector<int>(N, 0));\n    vector<int> have(16, 0);\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            char c = grid_str[i][j];\n            int val;\n            if (c >= '0' && c <= '9') {\n                val = c - '0';\n            } else {\n                val = 10 + (c - 'a');\n            }\n            grid[i][j] = val;\n            if (val < 16) {\n                have[val]++;\n            }\n        }\n    }\n\n    vector<tuple<int, int, string>> candidates;\n    candidates.emplace_back(N-1, N-1, \"row_snake_even_left\");\n    candidates.emplace_back(N-1, N-1, \"row_snake_even_right\");\n    candidates.emplace_back(N-1, N-1, \"col_snake_even_top\");\n    candidates.emplace_back(N-1, N-1, \"col_snake_even_bottom\");\n    candidates.emplace_back(0, 0, \"row_snake_start_right\");\n    candidates.emplace_back(0, N-1, \"row_snake_start_left\");\n    candidates.emplace_back(N-1, 0, \"row_snake_end_left\");\n    candidates.emplace_back(N-1, N-1, \"row_snake_alt1\");\n    candidates.emplace_back(N-1, N-1, \"col_snake_alt1\");\n    candidates.emplace_back(0, 0, \"col_snake_start_bottom\");\n\n    vector<vector<int>> target_grid;\n    int target_empty_i = -1, target_empty_j = -1;\n    bool found_candidate = false;\n\n    random_device rd;\n    mt19937 rng(rd());\n\n    for (auto [e_i, e_j, pattern] : candidates) {\n        vector<pair<int, int>> path;\n        if (pattern == \"row_snake_even_left\" || pattern == \"row_snake_start_right\" || pattern == \"row_snake_end_left\") {\n            for (int i = 0; i < N; i++) {\n                if (i % 2 == 0) {\n                    for (int j = 0; j < N; j++) {\n                        if (i == e_i && j == e_j) continue;\n                        path.push_back({i, j});\n                    }\n                } else {\n                    for (int j = N-1; j >= 0; j--) {\n                        if (i == e_i && j == e_j) continue;\n                        path.push_back({i, j});\n                    }\n                }\n            }\n        } else if (pattern == \"row_snake_even_right\") {\n            for (int i = 0; i < N; i++) {\n                if (i % 2 == 0) {\n                    for (int j = N-1; j >= 0; j--) {\n                        if (i == e_i && j == e_j) continue;\n                        path.push_back({i, j});\n                    }\n                } else {\n                    for (int j = 0; j < N; j++) {\n                        if (i == e_i && j == e_j) continue;\n                        path.push_back({i, j});\n                    }\n                }\n            }\n        } else if (pattern.find(\"col_snake\") != string::npos) {\n            for (int j = 0; j < N; j++) {\n                if (j % 2 == 0) {\n                    for (int i = 0; i < N; i++) {\n                        if (i == e_i && j == e_j) continue;\n                        path.push_back({i, j});\n                    }\n                } else {\n                    for (int i = N-1; i >= 0; i--) {\n                        if (i == e_i && j == e_j) continue;\n                        path.push_back({i, j});\n                    }\n                }\n            }\n        } else if (pattern == \"row_snake_start_left\") {\n            for (int i = 0; i < N; i++) {\n                if (i % 2 == 0) {\n                    for (int j = N-2; j >= 0; j--) {\n                        if (i == e_i && j == e_j) continue;\n                        path.push_back({i, j});\n                    }\n                } else {\n                    for (int j = 0; j < N; j++) {\n                        if (i == e_i && j == e_j) continue;\n                        path.push_back({i, j});\n                    }\n                }\n            }\n        } else if (pattern == \"row_snake_alt1\") {\n            for (int i = 0; i < N; i++) {\n                if (i == 0) {\n                    for (int j = 0; j < N-1; j++) {\n                        if (0 == e_i && j == e_j) continue;\n                        path.push_back({0, j});\n                    }\n                    for (int j = 1; j < N; j++) {\n                        if (i == e_i && j == e_j) continue;\n                        path.push_back({i, j});\n                    }\n                } else {\n                    for (int j = 0; j < N; j++) {\n                        if (i == e_i && j == e_j) continue;\n                        path.push_back({i, j});\n                    }\n                }\n            }\n        } else if (pattern == \"col_snake_alt1\") {\n            for (int j = 0; j < N; j++) {\n                if (j == 0) {\n                    for (int i = 0; i < N-1; i++) {\n                        if (i == e_i && 0 == e_j) continue;\n                        path.push_back({i, 0});\n                    }\n                    for (int i = 1; i < N; i++) {\n                        if (i == e_i && j == e_j) continue;\n                        path.push_back({i, j});\n                    }\n                } else {\n                    for (int i = 0; i < N; i++) {\n                        if (i == e_i && j == e_j) continue;\n                        path.push_back({i, j});\n                    }\n                }\n            }\n        } else if (pattern == \"col_snake_start_bottom\") {\n            for (int j = 0; j < N; j++) {\n                if (j % 2 == 0) {\n                    for (int i = N-1; i >= 0; i--) {\n                        if (i == e_i && j == e_j) continue;\n                        path.push_back({i, j});\n                    }\n                } else {\n                    for (int i = 0; i < N; i++) {\n                        if (i == e_i && j == e_j) continue;\n                        path.push_back({i, j});\n                    }\n                }\n            }\n        }\n\n        vector<vector<int>> pos(N, vector<int>(N, -1));\n        for (int idx = 0; idx < path.size(); idx++) {\n            int i = path[idx].first;\n            int j = path[idx].second;\n            pos[i][j] = idx;\n        }\n\n        vector<vector<int>> req_bitmask(N, vector<int>(N, 0));\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                if (i == e_i && j == e_j) {\n                    req_bitmask[i][j] = 0;\n                } else if (pos[i][j] == -1) {\n                    continue;\n                } else {\n                    int bitmask = 0;\n                    int idx = pos[i][j];\n                    if (idx > 0) {\n                        auto [pi, pj] = path[idx-1];\n                        if (pi == i && pj == j-1) bitmask |= 1;\n                        else if (pi == i && pj == j+1) bitmask |= 4;\n                        else if (pi == i-1 && pj == j) bitmask |= 2;\n                        else if (pi == i+1 && pj == j) bitmask |= 8;\n                    }\n                    if (idx < (int)path.size()-1) {\n                        auto [ni, nj] = path[idx+1];\n                        if (ni == i && nj == j-1) bitmask |= 1;\n                        else if (ni == i && nj == j+1) bitmask |= 4;\n                        else if (ni == i-1 && nj == j) bitmask |= 2;\n                        else if (ni == i+1 && nj == j) bitmask |= 8;\n                    }\n                    req_bitmask[i][j] = bitmask;\n                }\n            }\n        }\n\n        vector<int> freq_req(16, 0);\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                int b = req_bitmask[i][j];\n                if (b >= 0 && b < 16) {\n                    freq_req[b]++;\n                }\n            }\n        }\n\n        bool match = true;\n        for (int b = 0; b < 16; b++) {\n            if (freq_req[b] != have[b]) {\n                match = false;\n                break;\n            }\n        }\n        if (match) {\n            found_candidate = true;\n            target_grid = req_bitmask;\n            target_empty_i = e_i;\n            target_empty_j = e_j;\n            break;\n        }\n    }\n\n    if (!found_candidate) {\n        target_empty_i = N-1;\n        target_empty_j = N-1;\n        for (int trial = 0; trial < 30; trial++) {\n            vector<vector<bool>> visited(N, vector<bool>(N, false));\n            visited[target_empty_i][target_empty_j] = true;\n\n            vector<pair<int, int>> V_cells;\n            for (int i = 0; i < N; i++) {\n                for (int j = 0; j < N; j++) {\n                    if (!visited[i][j]) {\n                        V_cells.push_back({i, j});\n                    }\n                }\n            }\n            if (V_cells.empty()) break;\n            shuffle(V_cells.begin(), V_cells.end(), rng);\n            int start_i = V_cells[0].first;\n            int start_j = V_cells[0].second;\n            visited[start_i][start_j] = true;\n\n            vector<tuple<pair<int, int>, pair<int, int>>> tree_edges;\n            vector<tuple<pair<int, int>, pair<int, int>>> frontier;\n\n            auto add_neighbors = [&](int i, int j) {\n                vector<pair<int, int>> neighbors = {{i-1, j}, {i+1, j}, {i, j-1}, {i, j+1}};\n                for (auto [ni, nj] : neighbors) {\n                    if (ni >= 0 && ni < N && nj >= 0 && nj < N && !visited[ni][nj]) {\n                        frontier.push_back({{i, j}, {ni, nj}});\n                    }\n                }\n            };\n            add_neighbors(start_i, start_j);\n\n            while (!frontier.empty()) {\n                int idx = uniform_int_distribution<int>(0, frontier.size()-1)(rng);\n                auto edge = frontier[idx];\n                frontier[idx] = frontier.back();\n                frontier.pop_back();\n                auto [u, v] = edge;\n                int ui = u.first, uj = u.second;\n                int vi = v.first, vj = v.second;\n                if (visited[vi][vj]) continue;\n                tree_edges.push_back(edge);\n                visited[vi][vj] = true;\n                add_neighbors(vi, vj);\n            }\n\n            vector<vector<int>> req_bitmask_rand(N, vector<int>(N, 0));\n            for (int i = 0; i < N; i++) {\n                for (int j = 0; j < N; j++) {\n                    if (i == target_empty_i && j == target_empty_j) {\n                        req_bitmask_rand[i][j] = 0;\n                        continue;\n                    }\n                    int bitmask = 0;\n                    if (i > 0 && visited[i-1][j]) bitmask |= 2;\n                    if (i < N-1 && visited[i+1][j]) bitmask |= 8;\n                    if (j > 0 && visited[i][j-1]) bitmask |= 1;\n                    if (j < N-1 && visited[i][j+1]) bitmask |= 4;\n                    req_bitmask_rand[i][j] = bitmask;\n                }\n            }\n\n            vector<int> freq_req_rand(16, 0);\n            for (int i = 0; i < N; i++) {\n                for (int j = 0; j < N; j++) {\n                    int b = req_bitmask_rand[i][j];\n                    if (b >= 0 && b < 16) {\n                        freq_req_rand[b]++;\n                    }\n                }\n            }\n\n            bool match = true;\n            for (int b = 0; b < 16; b++) {\n                if (freq_req_rand[b] != have[b]) {\n                    match = false;\n                    break;\n                }\n            }\n            if (match) {\n                found_candidate = true;\n                target_grid = req_bitmask_rand;\n                break;\n            }\n        }\n    }\n\n    int empty_i = -1, empty_j = -1;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (grid[i][j] == 0) {\n                empty_i = i;\n                empty_j = j;\n            }\n        }\n    }\n\n    string moves = \"\";\n    vector<pair<int, int>> dirs = {{-1,0}, {1,0}, {0,-1}, {0,1}};\n    char dir_chars[4] = {'U','D','L','R'};\n\n    for (int step = 0; step < T; step++) {\n        bool solved = true;\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                int expected;\n                if (i == target_empty_i && j == target_empty_j) {\n                    expected = 0;\n                } else {\n                    expected = target_grid[i][j];\n                }\n                int actual = grid[i][j];\n                if (actual != expected) {\n                    solved = false;\n                }\n            }\n        }\n        if (solved) {\n            break;\n        }\n\n        int best_cost = INT_MAX;\n        char best_move = '?';\n        for (int d = 0; d < 4; d++) {\n            int ni = empty_i + dirs[d].first;\n            int nj = empty_j + dirs[d].second;\n            if (ni < 0 || ni >= N || nj < 0 || nj >= N) {\n                continue;\n            }\n\n            int cost = 0;\n            for (int i = 0; i < N; i++) {\n                for (int j = 0; j < N; j++) {\n                    int actual_val;\n                    if (i == ni && j == nj) {\n                        actual_val = 0;\n                    } else if (i == empty_i && j == empty_j) {\n                        actual_val = grid[ni][nj];\n                    } else {\n                        actual_val = grid[i][j];\n                    }\n\n                    int expected_val;\n                    if (i == target_empty_i && j == target_empty_j) {\n                        expected_val = 0;\n                    } else {\n                        expected_val = target_grid[i][j];\n                    }\n\n                    if (actual_val != expected_val) {\n                        cost++;\n                    }\n                }\n            }\n            int manhattan = abs(ni - target_empty_i) + abs(nj - target_empty_j);\n            cost += 2 * manhattan;\n\n            if (cost < best_cost) {\n                best_cost = cost;\n                best_move = dir_chars[d];\n            }\n        }\n\n        if (best_move == '?') {\n            break;\n        }\n        moves += best_move;\n\n        int ni = empty_i + (best_move == 'U' ? -1 : (best_move == 'D' ? 1 : 0));\n        int nj = empty_j + (best_move == 'L' ? -1 : (best_move == 'R' ? 1 : 0));\n        swap(grid[empty_i][empty_j], grid[ni][nj]);\n        empty_i = ni;\n        empty_j = nj;\n    }\n\n    cout << moves << endl;\n\n    return 0;\n}","ahc012":"#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\n\nstruct Point {\n    ll x, y;\n    Point(ll x=0, ll y=0) : x(x), y(y) {}\n};\n\nstruct Line {\n    Point p1, p2;\n    Line(Point p1=Point(), Point p2=Point()) : p1(p1), p2(p2) {}\n};\n\nll side(Point p1, Point p2, Point p) {\n    return (p2.x - p1.x) * (p.y - p1.y) - (p2.y - p1.y) * (p.x - p1.x);\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(0);\n\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> straws(N);\n    for (int i = 0; i < N; i++) {\n        cin >> straws[i].x >> straws[i].y;\n    }\n\n    vector<vector<ll>> global_ts(4);\n    vector<pair<int, int>> dirs = {{1,0}, {0,1}, {1,1}, {1,-1}};\n\n    for (int dir_idx = 0; dir_idx < 4; dir_idx++) {\n        ll dx = dirs[dir_idx].first;\n        ll dy = dirs[dir_idx].second;\n        vector<ll> t_vec;\n        for (int i = 0; i < N; i++) {\n            t_vec.push_back(dx * straws[i].x + dy * straws[i].y);\n        }\n        sort(t_vec.begin(), t_vec.end());\n        global_ts[dir_idx] = t_vec;\n    }\n\n    struct Region {\n        vector<int> strawberries;\n    };\n    vector<Region> regions;\n    Region all;\n    for (int i = 0; i < N; i++) {\n        all.strawberries.push_back(i);\n    }\n    regions.push_back(all);\n\n    vector<int> b(11, 0);\n    if (N <= 10) {\n        b[N] = 1;\n    }\n\n    vector<Line> cuts;\n\n    for (int step = 0; step < K; step++) {\n        int best_delta = -1;\n        Line best_line;\n        Region* best_region = nullptr;\n        vector<int> best_new_b;\n\n        for (int i = 0; i < regions.size(); i++) {\n            Region& R = regions[i];\n            int c = R.strawberries.size();\n            if (c <= 1) continue;\n\n            for (int dir_idx = 0; dir_idx < 4; dir_idx++) {\n                ll dx = dirs[dir_idx].first;\n                ll dy = dirs[dir_idx].second;\n                vector<ll> t_region;\n                for (int idx : R.strawberries) {\n                    t_region.push_back(dx * straws[idx].x + dy * straws[idx].y);\n                }\n                sort(t_region.begin(), t_region.end());\n                int sz = t_region.size();\n                double median_val;\n                if (sz % 2 == 1) {\n                    median_val = t_region[sz/2];\n                } else {\n                    median_val = (t_region[sz/2 - 1] + t_region[sz/2]) / 2.0;\n                }\n\n                for (int offset = -2; offset <= 2; offset++) {\n                    ll c_val = (ll)(median_val) + offset;\n\n                    if (binary_search(global_ts[dir_idx].begin(), global_ts[dir_idx].end(), c_val)) {\n                        continue;\n                    }\n\n                    Point p1, p2;\n                    if (dir_idx == 0) {\n                        p1 = Point(c_val, 0);\n                        p2 = Point(c_val, 1);\n                    } else if (dir_idx == 1) {\n                        p1 = Point(0, c_val);\n                        p2 = Point(1, c_val);\n                    } else if (dir_idx == 2) {\n                        p1 = Point(c_val, 0);\n                        p2 = Point(0, c_val);\n                    } else {\n                        p1 = Point(c_val, 0);\n                        p2 = Point(0, -c_val);\n                    }\n                    Line cand(p1, p2);\n\n                    int c1 = 0;\n                    for (int idx : R.strawberries) {\n                        if (side(cand.p1, cand.p2, straws[idx]) > 0) {\n                            c1++;\n                        }\n                    }\n                    int c2 = c - c1;\n\n                    vector<int> new_b = b;\n                    if (c <= 10) {\n                        new_b[c]--;\n                    }\n                    if (c1 <= 10) {\n                        new_b[c1]++;\n                    }\n                    if (c2 <= 10) {\n                        new_b[c2]++;\n                    }\n\n                    int old_score = 0;\n                    for (int d = 1; d <= 10; d++) {\n                        old_score += min(a[d], b[d]);\n                    }\n                    int new_score = 0;\n                    for (int d = 1; d <= 10; d++) {\n                        new_score += min(a[d], new_b[d]);\n                    }\n                    int delta = new_score - old_score;\n\n                    if (delta > best_delta) {\n                        best_delta = delta;\n                        best_line = cand;\n                        best_region = &R;\n                        best_new_b = new_b;\n                    }\n                }\n            }\n        }\n\n        if (best_delta <= 0) {\n            break;\n        }\n\n        vector<int> left_idx, right_idx;\n        for (int idx : best_region->strawberries) {\n            if (side(best_line.p1, best_line.p2, straws[idx]) > 0) {\n                left_idx.push_back(idx);\n            } else {\n                right_idx.push_back(idx);\n            }\n        }\n\n        Region left_region, right_region;\n        left_region.strawberries = left_idx;\n        right_region.strawberries = right_idx;\n\n        vector<Region> new_regions;\n        for (int i = 0; i < regions.size(); i++) {\n            if (&regions[i] != best_region) {\n                new_regions.push_back(regions[i]);\n            }\n        }\n        new_regions.push_back(left_region);\n        new_regions.push_back(right_region);\n        regions = new_regions;\n\n        b = best_new_b;\n        cuts.push_back(best_line);\n    }\n\n    cout << cuts.size() << \"\\n\";\n    for (Line& line : cuts) {\n        cout << line.p1.x << \" \" << line.p1.y << \" \"\n             << line.p2.x << \" \" << line.p2.y << \"\\n\";\n    }\n\n    return 0;\n}","ahc014":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Operation {\n    int x1, y1, x2, y2, x3, y3, x4, y4;\n};\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M;\n    cin >> N >> M;\n    int c = (N - 1) / 2;\n\n    vector<vector<bool>> grid(N, vector<bool>(N, false));\n    vector<pair<int, int>> dots;\n    for (int i = 0; i < M; i++) {\n        int x, y;\n        cin >> x >> y;\n        grid[x][y] = true;\n        dots.emplace_back(x, y);\n    }\n\n    vector<tuple<long long, int, int>> candidate_list;\n    for (int x = 0; x < N; x++) {\n        for (int y = 0; y < N; y++) {\n            long long dx = x - c;\n            long long dy = y - c;\n            long long weight = dx * dx + dy * dy + 1;\n            candidate_list.emplace_back(-weight, x, y);\n        }\n    }\n    sort(candidate_list.begin(), candidate_list.end());\n\n    vector<vector<bool>> hor_used(N-1, vector<bool>(N, false));\n    vector<vector<bool>> ver_used(N, vector<bool>(N-1, false));\n    vector<vector<bool>> diag1_used(N-1, vector<bool>(N-1, false));\n    vector<vector<bool>> diag2_used(N-1, vector<bool>(N-1, false));\n\n    vector<Operation> operations;\n\n    bool added_something;\n    do {\n        added_something = false;\n        for (auto [neg_weight, x, y] : candidate_list) {\n            if (grid[x][y]) continue;\n\n            bool found = false;\n\n            auto check_axis = [&](int x1, int y1, int x2, int y2) -> bool {\n                if (x1 == x2 || y1 == y2) return false;\n                int x_min = min(x1, x2);\n                int x_max = max(x1, x2);\n                int y_min = min(y1, y2);\n                int y_max = max(y1, y2);\n\n                bool cond2 = true;\n                for (int yy = y_min + 1; yy < y_max; yy++) {\n                    if (grid[x_min][yy] || grid[x_max][yy]) {\n                        cond2 = false;\n                        break;\n                    }\n                }\n                if (!cond2) return false;\n                for (int xx = x_min + 1; xx < x_max; xx++) {\n                    if (grid[xx][y_min] || grid[xx][y_max]) {\n                        cond2 = false;\n                        break;\n                    }\n                }\n                if (!cond2) return false;\n\n                bool cond3 = true;\n                for (int yy = y_min; yy < y_max; yy++) {\n                    if (ver_used[x_min][yy] || ver_used[x_max][yy]) {\n                        cond3 = false;\n                        break;\n                    }\n                }\n                if (!cond3) return false;\n                for (int xx = x_min; xx < x_max; xx++) {\n                    if (hor_used[xx][y_min] || hor_used[xx][y_max]) {\n                        cond3 = false;\n                        break;\n                    }\n                }\n                return cond3;\n            };\n\n            auto mark_axis = [&](int x1, int y1, int x2, int y2) {\n                int x_min = min(x1, x2);\n                int x_max = max(x1, x2);\n                int y_min = min(y1, y2);\n                int y_max = max(y1, y2);\n                for (int yy = y_min; yy < y_max; yy++) {\n                    ver_used[x_min][yy] = true;\n                    ver_used[x_max][yy] = true;\n                }\n                for (int xx = x_min; xx < x_max; xx++) {\n                    hor_used[xx][y_min] = true;\n                    hor_used[xx][y_max] = true;\n                }\n            };\n\n            auto check_diag = [&](int x, int y) -> bool {\n                vector<pair<int, int>> d1_points;\n                vector<pair<int, int>> d2_points;\n                for (int dx = -N; dx <= N; dx++) {\n                    int nx = x + dx;\n                    int ny = y + dx;\n                    if (nx >= 0 && nx < N && ny >= 0 && ny < N && grid[nx][ny]) {\n                        d1_points.emplace_back(nx, ny);\n                    }\n                    nx = x + dx;\n                    ny = y - dx;\n                    if (nx >= 0 && nx < N && ny >= 0 && ny < N && grid[nx][ny]) {\n                        d2_points.emplace_back(nx, ny);\n                    }\n                }\n\n                for (auto& a : d1_points) {\n                    for (auto& b : d2_points) {\n                        int cx = a.first + b.first - x;\n                        int cy = a.second + b.second - y;\n                        if (cx < 0 || cx >= N || cy < 0 || cy >= N) continue;\n                        if (!grid[cx][cy]) continue;\n\n                        vector<pair<int, int>> corners = {{x,y}, a, {cx,cy}, b};\n                        bool cond2 = true;\n                        for (int i = 0; i < 4; i++) {\n                            int x1 = corners[i].first, y1 = corners[i].second;\n                            int x2 = corners[(i+1)%4].first, y2 = corners[(i+1)%4].second;\n                            int dx = x2 - x1, dy = y2 - y1;\n                            int steps = max(abs(dx), abs(dy));\n                            for (int step = 1; step < steps; step++) {\n                                int px = x1 + (dx * step) / steps;\n                                int py = y1 + (dy * step) / steps;\n                                bool is_corner = false;\n                                for (auto& corner : corners) {\n                                    if (px == corner.first && py == corner.second) {\n                                        is_corner = true;\n                                        break;\n                                    }\n                                }\n                                if (!is_corner && grid[px][py]) {\n                                    cond2 = false;\n                                    break;\n                                }\n                            }\n                            if (!cond2) break;\n                        }\n                        if (!cond2) continue;\n\n                        bool cond3 = true;\n                        for (int i = 0; i < 4; i++) {\n                            int x1 = corners[i].first, y1 = corners[i].second;\n                            int x2 = corners[(i+1)%4].first, y2 = corners[(i+1)%4].second;\n                            int dx = x2 - x1, dy = y2 - y1;\n                            int steps = max(abs(dx), abs(dy));\n                            for (int step = 1; step <= steps; step++) {\n                                int px = x1 + (dx * step) / steps;\n                                int py = y1 + (dy * step) / steps;\n                                int ex, ey;\n                                if (dx == 0) {\n                                    ex = px;\n                                    ey = min(y1, y2) + (step <= abs(dy) ? step-1 : abs(dy)-1);\n                                } else if (dy == 0) {\n                                    ex = min(x1, x2) + (step <= abs(dx) ? step-1 : abs(dx)-1);\n                                    ey = py;\n                                } else if (abs(dx) == abs(dy)) {\n                                    if (dx * dy > 0) {\n                                        if (dx > 0) {\n                                            ex = px;\n                                            ey = py;\n                                        } else {\n                                            ex = px + 1;\n                                            ey = py + 1;\n                                        }\n                                    } else {\n                                        if (dx > 0) {\n                                            ex = px;\n                                            ey = py + 1;\n                                        } else {\n                                            ex = px + 1;\n                                            ey = py;\n                                        }\n                                    }\n                                } else {\n                                    continue;\n                                }\n                                if (ex >= N-1 || ey >= N) continue;\n                                if (dx == 0 || dy == 0) {\n                                    if (dx == 0) {\n                                        if (ver_used[px][min(y1,y2) + step - 1]) {\n                                            cond3 = false;\n                                            break;\n                                        }\n                                    } else {\n                                        if (hor_used[min(x1,x2) + step - 1][py]) {\n                                            cond3 = false;\n                                            break;\n                                        }\n                                    }\n                                } else {\n                                    int type = (dx * dy > 0) ? 1 : 2;\n                                    int sx = px, sy = py;\n                                    if (type == 1) {\n                                        if (diag1_used[sx][sy]) {\n                                            cond3 = false;\n                                            break;\n                                        }\n                                    } else {\n                                        if (sx >= 0 && sy >= 0 && sx < N-1 && sy < N-1 && diag2_used[sx][sy]) {\n                                            cond3 = false;\n                                            break;\n                                        }\n                                    }\n                                }\n                            }\n                            if (!cond3) break;\n                        }\n                        if (cond2 && cond3) {\n                            grid[x][y] = true;\n                            dots.emplace_back(x, y);\n                            operations.push_back({x, y, a.first, a.second, cx, cy, b.first, b.second});\n                            for (int i = 0; i < 4; i++) {\n                                int x1 = corners[i].first, y1 = corners[i].second;\n                                int x2 = corners[(i+1)%4].first, y2 = corners[(i+1)%4].second;\n                                int dx = x2 - x1, dy = y2 - y1;\n                                int steps = max(abs(dx), abs(dy));\n                                for (int step = 1; step <= steps; step++) {\n                                    int px = x1 + (dx * step) / steps;\n                                    int py = y1 + (dy * step) / steps;\n                                    if (dx == 0) {\n                                        ver_used[x1][min(y1,y2) + step - 1] = true;\n                                    } else if (dy == 0) {\n                                        hor_used[min(x1,x2) + step - 1][y1] = true;\n                                    } else if (abs(dx) == abs(dy)) {\n                                        if (dx * dy > 0) {\n                                            if (dx > 0) diag1_used[px][py] = true;\n                                            else diag1_used[px][py] = true;\n                                        } else {\n                                            if (dx > 0) diag2_used[px][py] = true;\n                                            else diag2_used[px][py] = true;\n                                        }\n                                    }\n                                }\n                            }\n                            return true;\n                        }\n                    }\n                }\n                return false;\n            };\n\n            if (x > 0 && x < N-1 && y > 0 && y < N-1) {\n                if (check_diag(x, y)) {\n                    found = true;\n                }\n            }\n\n            if (!found) {\n                vector<int> up_dots, down_dots, left_dots, right_dots;\n                for (int yy = y+1; yy < N; yy++) if (grid[x][yy]) up_dots.push_back(yy);\n                for (int yy = 0; yy < y; yy++) if (grid[x][yy]) down_dots.push_back(yy);\n                for (int xx = 0; xx < x; xx++) if (grid[xx][y]) left_dots.push_back(xx);\n                for (int xx = x+1; xx < N; xx++) if (grid[xx][y]) right_dots.push_back(xx);\n\n                if (!up_dots.empty() && !right_dots.empty()) {\n                    for (int yA : up_dots) {\n                        for (int xB : right_dots) {\n                            if (check_axis(x, y, xB, yA)) {\n                                grid[x][y] = true;\n                                dots.emplace_back(x, y);\n                                operations.push_back({x, y, x, yA, xB, yA, xB, y});\n                                mark_axis(x, y, xB, yA);\n                                found = true;\n                                break;\n                            }\n                        }\n                        if (found) break;\n                    }\n                }\n                if (!found && !up_dots.empty() && !left_dots.empty()) {\n                    for (int yA : up_dots) {\n                        for (int xB : left_dots) {\n                            if (check_axis(x, y, xB, yA)) {\n                                grid[x][y] = true;\n                                dots.emplace_back(x, y);\n                                operations.push_back({x, y, x, yA, xB, yA, xB, y});\n                                mark_axis(x, y, xB, yA);\n                                found = true;\n                                break;\n                            }\n                        }\n                        if (found) break;\n                    }\n                }\n                if (!found && !down_dots.empty() && !right_dots.empty()) {\n                    for (int yA : down_dots) {\n                        for (int xB : right_dots) {\n                            if (check_axis(x, y, xB, yA)) {\n                                grid[x][y] = true;\n                                dots.emplace_back(x, y);\n                                operations.push_back({x, y, x, yA, xB, yA, xB, y});\n                                mark_axis(x, y, xB, yA);\n                                found = true;\n                                break;\n                            }\n                        }\n                        if (found) break;\n                    }\n                }\n                if (!found && !down_dots.empty() && !left_dots.empty()) {\n                    for (int yA : down_dots) {\n                        for (int xB : left_dots) {\n                            if (check_axis(x, y, xB, yA)) {\n                                grid[x][y] = true;\n                                dots.emplace_back(x, y);\n                                operations.push_back({x, y, x, yA, xB, yA, xB, y});\n                                mark_axis(x, y, xB, yA);\n                                found = true;\n                                break;\n                            }\n                        }\n                        if (found) break;\n                    }\n                }\n            }\n\n            if (found) {\n                added_something = true;\n                break;\n            }\n        }\n    } while (added_something);\n\n    cout << operations.size() << '\\n';\n    for (auto& op : operations) {\n        cout << op.x1 << ' ' << op.y1 << ' '\n             << op.x2 << ' ' << op.y2 << ' '\n             << op.x3 << ' ' << op.y3 << ' '\n             << op.x4 << ' ' << op.y4 << '\\n';\n    }\n\n    return 0;\n}","ahc015":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\nusing namespace std;\n\nint compute_sum_squares(int grid[10][10]) {\n    bool visited[10][10] = {{false}};\n    int total = 0;\n\n    for (int i = 0; i < 10; i++) {\n        for (int j = 0; j < 10; j++) {\n            if (grid[i][j] != 0 && !visited[i][j]) {\n                int size = 0;\n                queue<pair<int, int>> q;\n                q.push({i, j});\n                visited[i][j] = true;\n                int flavor = grid[i][j];\n\n                while (!q.empty()) {\n                    auto [x, y] = q.front();\n                    q.pop();\n                    size++;\n                    const int dx[4] = {-1, 1, 0, 0};\n                    const int dy[4] = {0, 0, -1, 1};\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 < 10 && ny >= 0 && ny < 10 && !visited[nx][ny] && grid[nx][ny] == flavor) {\n                            visited[nx][ny] = true;\n                            q.push({nx, ny});\n                        }\n                    }\n                }\n                total += size * size;\n            }\n        }\n    }\n    return total;\n}\n\nint main() {\n    vector<int> flavors(100);\n    for (int i = 0; i < 100; i++) {\n        cin >> flavors[i];\n    }\n\n    int grid[10][10] = {{0}};\n    vector<char> directions = {'F', 'B', 'L', 'R'};\n\n    for (int t = 0; t < 100; t++) {\n        int p;\n        cin >> p;\n\n        if (t == 99) {\n            break;\n        }\n\n        int r = (p - 1) / 10;\n        int c = (p - 1) % 10;\n        grid[r][c] = flavors[t];\n\n        char best_dir = 'F';\n        int best_score = -1;\n        int best_tilted[10][10] = {{0}};\n\n        for (char dir : directions) {\n            int tilted[10][10] = {{0}};\n\n            if (dir == 'F') {\n                for (int j = 0; j < 10; j++) {\n                    vector<int> candies;\n                    for (int i = 0; i < 10; i++) {\n                        if (grid[i][j] != 0) {\n                            candies.push_back(grid[i][j]);\n                        }\n                    }\n                    int k = candies.size();\n                    for (int i = 0; i < k; i++) {\n                        tilted[i][j] = candies[i];\n                    }\n                }\n            } else if (dir == 'B') {\n                for (int j = 0; j < 10; j++) {\n                    vector<int> candies;\n                    for (int i = 0; i < 10; i++) {\n                        if (grid[i][j] != 0) {\n                            candies.push_back(grid[i][j]);\n                        }\n                    }\n                    int k = candies.size();\n                    int start_row = 9 - k + 1;\n                    for (int i = 0; i < k; i++) {\n                        tilted[start_row + i][j] = candies[i];\n                    }\n                }\n            } else if (dir == 'L') {\n                for (int i = 0; i < 10; i++) {\n                    vector<int> candies;\n                    for (int j = 0; j < 10; j++) {\n                        if (grid[i][j] != 0) {\n                            candies.push_back(grid[i][j]);\n                        }\n                    }\n                    int k = candies.size();\n                    for (int j_idx = 0; j_idx < k; j_idx++) {\n                        tilted[i][j_idx] = candies[j_idx];\n                    }\n                }\n            } else if (dir == 'R') {\n                for (int i = 0; i < 10; i++) {\n                    vector<int> candies;\n                    for (int j = 0; j < 10; j++) {\n                        if (grid[i][j] != 0) {\n                            candies.push_back(grid[i][j]);\n                        }\n                    }\n                    int k = candies.size();\n                    int start_col = 9 - k + 1;\n                    for (int j_idx = 0; j_idx < k; j_idx++) {\n                        tilted[i][start_col + j_idx] = candies[j_idx];\n                    }\n                }\n            }\n\n            int score = compute_sum_squares(tilted);\n            if (score > best_score) {\n                best_score = score;\n                best_dir = dir;\n                for (int i = 0; i < 10; i++) {\n                    for (int j = 0; j < 10; j++) {\n                        best_tilted[i][j] = tilted[i][j];\n                    }\n                }\n            }\n        }\n\n        cout << best_dir << '\\n';\n        cout.flush();\n\n        for (int i = 0; i < 10; i++) {\n            for (int j = 0; j < 10; j++) {\n                grid[i][j] = best_tilted[i][j];\n            }\n        }\n    }\n\n    return 0;\n}","ahc016":"#include <bits/stdc++.h>\nusing namespace std;\n\n// Function to compute the index of edge (i,j) in the string, with i<j\nlong long get_index(int i, int j, int N) {\n    return 1LL * i * (2 * N - i - 1) / 2 + (j - i - 1);\n}\n\nint main() {\n    int M;\n    double eps;\n    cin >> M >> eps;\n\n    const int N = 100;\n    const int T = N * (N - 1) / 2;\n\n    cout << N << endl;\n    cout.flush();\n\n    for (int s = 0; s < M; s++) {\n        int k;\n        if (s < 96) {\n            k = 4 + s;\n        } else {\n            k = 100 - (s - 96);\n        }\n        k = max(4, min(k, 100));\n\n        string graph(T, '0');\n        for (int i = 0; i < k; i++) {\n            for (int j = i + 1; j < k; j++) {\n                long long idx = get_index(i, j, N);\n                graph[idx] = '1';\n            }\n        }\n        cout << graph << endl;\n    }\n    cout.flush();\n\n    for (int q = 0; q < 100; q++) {\n        string H;\n        cin >> H;\n\n        vector<int> deg(N, 0);\n        for (int i = 0; i < N; i++) {\n            for (int j = i + 1; j < N; j++) {\n                long long idx = get_index(i, j, N);\n                if (H[idx] == '1') {\n                    deg[i]++;\n                    deg[j]++;\n                }\n            }\n        }\n\n        vector<int> sorted_deg = deg;\n        sort(sorted_deg.begin(), sorted_deg.end());\n\n        double best_score = 1e300;\n        int best_s = 0;\n\n        for (int s = 0; s < M; s++) {\n            int k;\n            if (s < 96) {\n                k = 4 + s;\n            } else {\n                k = 100 - (s - 96);\n            }\n            k = max(4, min(k, 100));\n\n            int low_count = N - k;\n            int high_count = k;\n\n            double expected_low = 99.0 * eps;\n            double expected_high = (k - 1) + (101 - 2 * k) * eps;\n\n            double score = 0.0;\n            if (low_count > 0) {\n                double sum_low = 0.0;\n                for (int i = 0; i < low_count; i++) {\n                    sum_low += sorted_deg[i];\n                }\n                double mean_low = sum_low / low_count;\n                score += (mean_low - expected_low) * (mean_low - expected_low);\n            }\n            if (high_count > 0) {\n                double sum_high = 0.0;\n                for (int i = low_count; i < N; i++) {\n                    sum_high += sorted_deg[i];\n                }\n                double mean_high = sum_high / high_count;\n                score += (mean_high - expected_high) * (mean_high - expected_high);\n            }\n\n            if (score < best_score) {\n                best_score = score;\n                best_s = s;\n            }\n        }\n\n        cout << best_s << endl;\n        cout.flush();\n    }\n\n    return 0;\n}","ahc017":"#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\n\nstruct Edge {\n    int u, v;\n    ll w;\n};\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, D, K;\n    cin >> N >> M >> D >> K;\n\n    vector<Edge> edges(M);\n    vector<vector<pair<int, ll>>> adj(N);\n\n    for (int i = 0; i < M; i++) {\n        int u, v;\n        ll w;\n        cin >> u >> v >> w;\n        u--; v--;\n        edges[i] = {u, v, w};\n        adj[u].push_back({v, w});\n        adj[v].push_back({u, w});\n    }\n\n    for (int i = 0; i < N; i++) {\n        int x, y;\n        cin >> x >> y;\n    }\n\n    vector<double> betw(M, 0.0);\n\n    for (int s = 0; s < N; s++) {\n        vector<ll> dist(N, LLONG_MAX);\n        vector<double> num_paths(N, 0.0);\n        priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<>> pq;\n\n        dist[s] = 0;\n        num_paths[s] = 1.0;\n        pq.push({0, s});\n\n        while (!pq.empty()) {\n            auto [d, u] = pq.top();\n            pq.pop();\n            if (d != dist[u]) continue;\n            for (auto [v, w] : adj[u]) {\n                ll nd = d + w;\n                if (nd < dist[v]) {\n                    dist[v] = nd;\n                    num_paths[v] = num_paths[u];\n                    pq.push({nd, v});\n                } else if (nd == dist[v]) {\n                    num_paths[v] += num_paths[u];\n                }\n            }\n        }\n\n        for (int i = 0; i < M; i++) {\n            int u = edges[i].u;\n            int v = edges[i].v;\n            ll w = edges[i].w;\n            if (dist[u] != LLONG_MAX && dist[v] != LLONG_MAX) {\n                if (dist[u] + w == dist[v]) {\n                    betw[i] += num_paths[u];\n                }\n                if (dist[v] + w == dist[u]) {\n                    betw[i] += num_paths[v];\n                }\n            }\n        }\n    }\n\n    vector<pair<double, int>> vec;\n    for (int i = 0; i < M; i++) {\n        vec.push_back({betw[i], i});\n    }\n    sort(vec.begin(), vec.end(), [](const auto& a, const auto& b) {\n        return a.first > b.first;\n    });\n\n    vector<int> ans(M);\n    for (int i = 0; i < M; i++) {\n        ans[vec[i].second] = (i % D) + 1;\n    }\n\n    for (int i = 0; i < M; i++) {\n        if (i) cout << \" \";\n        cout << ans[i];\n    }\n    cout << '\\n';\n\n    return 0;\n}","ahc019":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\n#include <tuple>\n#include <climits>\n#include <cmath>\n#include <array>\n#include <set>\n#include <map>\nusing namespace std;\n\nconst vector<array<array<int, 3>, 3>> rotation_matrices = {\n    {{{1,0,0},{0,1,0},{0,0,1}}},\n    {{{1,0,0},{0,0,1},{0,-1,0}}},\n    {{{1,0,0},{0,-1,0},{0,0,-1}}},\n    {{{1,0,0},{0,0,-1},{0,1,0}}},\n    {{{0,0,1},{0,1,0},{-1,0,0}}},\n    {{{0,1,0},{1,0,0},{0,0,-1}}},\n    {{{0,0,-1},{0,1,0},{1,0,0}}},\n    {{{0,-1,0},{1,0,0},{0,0,1}}},\n    {{-1,0,0},{0,1,0},{0,0,-1}},\n    {{-1,0,0},{0,0,1},{0,1,0}},\n    {{-1,0,0},{0,-1,0},{0,0,1}},\n    {{-1,0,0},{0,0,-1},{0,-1,0}},\n    {{{0,0,-1},{0,-1,0},{-1,0,0}}},\n    {{{0,-1,0},{-1,0,0},{0,0,-1}}},\n    {{{0,0,1},{0,-1,0},{1,0,0}}},\n    {{{0,1,0},{-1,0,0},{0,0,1}}},\n    {{{0,0,1},{-1,0,0},{0,-1,0}}},\n    {{{0,1,0},{0,0,1},{-1,0,0}}},\n    {{{0,0,-1},{-1,0,0},{0,1,0}}},\n    {{{0,-1,0},{0,0,-1},{-1,0,0}}},\n    {{{0,0,1},{1,0,0},{0,1,0}}},\n    {{{0,1,0},{0,0,-1},{1,0,0}}},\n    {{{0,0,-1},{1,0,0},{0,-1,0}}},\n    {{{0,-1,0},{0,0,1},{1,0,0}}}\n};\n\nvector<vector<tuple<int,int,int>>> get_components(vector<vector<vector<bool>>>& grid, int D) {\n    vector<vector<vector<bool>>> visited(D, vector<vector<bool>>(D, vector<bool>(D, false)));\n    vector<vector<tuple<int,int,int>>> components;\n    const int dx[6] = {1, -1, 0, 0, 0, 0};\n    const int dy[6] = {0, 0, 1, -1, 0, 0};\n    const int dz[6] = {0, 0, 0, 0, 1, -1};\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 (grid[x][y][z] && !visited[x][y][z]) {\n                    vector<tuple<int,int,int>> comp;\n                    queue<tuple<int,int,int>> q;\n                    q.push({x, y, z});\n                    visited[x][y][z] = true;\n                    while (!q.empty()) {\n                        auto [cx, cy, cz] = q.front(); q.pop();\n                        comp.push_back({cx, cy, cz});\n                        for (int i = 0; i < 6; i++) {\n                            int nx = cx + dx[i];\n                            int ny = cy + dy[i];\n                            int nz = cz + dz[i];\n                            if (nx >= 0 && nx < D && ny >= 0 && ny < D && nz >= 0 && nz < D && grid[nx][ny][nz] && !visited[nx][ny][nz]) {\n                                visited[nx][ny][nz] = true;\n                                q.push({nx, ny, nz});\n                            }\n                        }\n                    }\n                    components.push_back(comp);\n                }\n            }\n        }\n    }\n    return components;\n}\n\nbool find_placement(const vector<tuple<int,int,int>>& shape, const vector<vector<vector<bool>>>& rem, int D, int& tx, int& ty, int& tz) {\n    int w = 0, h = 0, d = 0;\n    for (auto [x,y,z] : shape) {\n        w = max(w, x+1);\n        h = max(h, y+1);\n        d = max(d, z+1);\n    }\n    for (tx = 0; tx <= D - w; tx++) {\n        for (ty = 0; ty <= D - h; ty++) {\n            for (tz = 0; tz <= D - d; tz++) {\n                bool valid = true;\n                for (auto [x,y,z] : shape) {\n                    int gx = tx + x;\n                    int gy = ty + y;\n                    int gz = tz + z;\n                    if (gx < 0 || gx >= D || gy < 0 || gy >= D || gz < 0 || gz >= D) {\n                        valid = false;\n                        break;\n                    }\n                    if (!rem[gx][gy][gz]) {\n                        valid = false;\n                        break;\n                    }\n                }\n                if (valid) {\n                    return true;\n                }\n            }\n        }\n    }\n    return false;\n}\n\nvoid place_block(vector<vector<vector<int>>>& grid, const vector<tuple<int,int,int>>& shape, int tx, int ty, int tz, int idx) {\n    for (auto [x,y,z] : shape) {\n        int gx = tx + x;\n        int gy = ty + y;\n        int gz = tz + z;\n        if (gx >= 0 && gx < grid.size() && gy >=0 && gy < grid[0].size() && gz >=0 && gz < grid[0][0].size()) {\n            grid[gx][gy][gz] = idx;\n        }\n    }\n}\n\nvoid update_remaining(vector<vector<vector<bool>>>& rem, const vector<tuple<int,int,int>>& shape, int tx, int ty, int tz) {\n    for (auto [x,y,z] : shape) {\n        int gx = tx + x;\n        int gy = ty + y;\n        int gz = tz + z;\n        if (gx >= 0 && gx < rem.size() && gy >=0 && gy < rem[0].size() && gz >=0 && gz < rem[0][0].size()) {\n            rem[gx][gy][gz] = false;\n        }\n    }\n}\n\nbool try_place_shared_block(vector<tuple<int,int,int>>& comp, \n                          vector<vector<vector<bool>>>& R1_rem,\n                          vector<vector<vector<bool>>>& R2_rem,\n                          int D,\n                          int& next_block_idx,\n                          vector<vector<vector<int>>>& b1_grid,\n                          vector<vector<vector<int>>>& b2_grid) {\n    vector<tuple<int,int,int>> orig_comp = comp;\n    for (const auto& rot_mat : rotation_matrices) {\n        vector<tuple<int,int,int>> rotated;\n        for (auto [x,y,z] : orig_comp) {\n            int nx = rot_mat[0][0]*x + rot_mat[0][1]*y + rot_mat[0][2]*z;\n            int ny = rot_mat[1][0]*x + rot_mat[1][1]*y + rot_mat[1][2]*z;\n            int nz = rot_mat[2][0]*x + rot_mat[2][1]*y + rot_mat[2][2]*z;\n            rotated.push_back({nx, ny, nz});\n        }\n\n        int min_x = INT_MAX, max_x = INT_MIN;\n        int min_y = INT_MAX, max_y = INT_MIN;\n        int min_z = INT_MAX, max_z = INT_MIN;\n        for (auto [x,y,z] : rotated) {\n            min_x = min(min_x, x);\n            max_x = max(max_x, x);\n            min_y = min(min_y, y);\n            max_y = max(max_y, y);\n            min_z = min(min_z, z);\n            max_z = max(max_z, z);\n        }\n        int w = max_x - min_x + 1;\n        int h = max_y - min_y + 1;\n        int d = max_z - min_z + 1;\n        if (w <= 0 || h <= 0 || d <= 0) {\n            continue;\n        }\n\n        vector<tuple<int,int,int>> normalized;\n        for (auto [x,y,z] : rotated) {\n            normalized.push_back({x - min_x, y - min_y, z - min_z});\n        }\n\n        int tx1, ty1, tz1;\n        if (!find_placement(normalized, R1_rem, D, tx1, ty1, tz1)) {\n            continue;\n        }\n        int tx2, ty2, tz2;\n        if (!find_placement(normalized, R2_rem, D, tx2, ty2, tz2)) {\n            continue;\n        }\n\n        int idx = next_block_idx++;\n        place_block(b1_grid, normalized, tx1, ty1, tz1, idx);\n        place_block(b2_grid, normalized, tx2, ty2, tz2, idx);\n        update_remaining(R1_rem, normalized, tx1, ty1, tz1);\n        update_remaining(R2_rem, normalized, tx2, ty2, tz2);\n        return true;\n    }\n    return false;\n}\n\nbool try_place_non_shared_block(vector<tuple<int,int,int>>& comp, \n                              vector<vector<vector<bool>>>& target_rem,\n                              int D,\n                              int& next_block_idx,\n                              vector<vector<vector<int>>>& target_grid,\n                              bool for_b1,\n                              bool for_b2) {\n    vector<tuple<int,int,int>> orig_comp = comp;\n    for (const auto& rot_mat : rotation_matrices) {\n        vector<tuple<int,int,int>> rotated;\n        for (auto [x,y,z] : orig_comp) {\n            int nx = rot_mat[0][0]*x + rot_mat[0][1]*y + rot_mat[0][2]*z;\n            int ny = rot_mat[1][0]*x + rot_mat[1][1]*y + rot_mat[1][2]*z;\n            int nz = rot_mat[2][0]*x + rot_mat[2][1]*y + rot_mat[2][2]*z;\n            rotated.push_back({nx, ny, nz});\n        }\n\n        int min_x = INT_MAX, max_x = INT_MIN;\n        int min_y = INT_MAX, max_y = INT_MIN;\n        int min_z = INT_MAX, max_z = INT_MIN;\n        for (auto [x,y,z] : rotated) {\n            min_x = min(min_x, x);\n            max_x = max(max_x, x);\n            min_y = min(min_y, y);\n            max_y = max(max_y, y);\n            min_z = min(min_z, z);\n            max_z = max(max_z, z);\n        }\n        int w = max_x - min_x + 1;\n        int h = max_y - min_y + 1;\n        int d = max_z - min_z + 1;\n        if (w <= 0 || h <= 0 || d <= 0) {\n            continue;\n        }\n\n        vector<tuple<int,int,int>> normalized;\n        for (auto [x,y,z] : rotated) {\n            normalized.push_back({x - min_x, y - min_y, z - min_z});\n        }\n\n        int tx, ty, tz;\n        if (!find_placement(normalized, target_rem, D, tx, ty, tz)) {\n            continue;\n        }\n\n        int idx = next_block_idx++;\n        place_block(target_grid, normalized, tx, ty, tz, idx);\n        update_remaining(target_rem, normalized, tx, ty, tz);\n        return true;\n    }\n    return false;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int D;\n    cin >> D;\n\n    vector<string> f1_lines(D), r1_lines(D), f2_lines(D), r2_lines(D);\n    for (int i = 0; i < D; i++) {\n        cin >> f1_lines[i];\n    }\n    for (int i = 0; i < D; i++) {\n        cin >> r1_lines[i];\n    }\n    for (int i = 0; i < D; i++) {\n        cin >> f2_lines[i];\n    }\n    for (int i = 0; i < D; i++) {\n        cin >> r2_lines[i];\n    }\n\n    vector<vector<vector<bool>>> R1_rem(D, vector<vector<bool>>(D, vector<bool>(D, false)));\n    vector<vector<vector<bool>>> R2_rem(D, vector<vector<bool>>(D, vector<bool>(D, false)));\n\n    for (int z = 0; z < D; z++) {\n        for (int x = 0; x < D; x++) {\n            for (int y = 0; y < D; y++) {\n                if (f1_lines[z][x] == '1' && r1_lines[z][y] == '1') {\n                    R1_rem[x][y][z] = true;\n                }\n                if (f2_lines[z][x] == '1' && r2_lines[z][y] == '1') {\n                    R2_rem[x][y][z] = true;\n                }\n            }\n        }\n    }\n\n    vector<vector<vector<int>>> b1_grid(D, vector<vector<int>>(D, vector<int>(D, 0)));\n    vector<vector<vector<int>>> b2_grid(D, vector<vector<int>>(D, vector<int>(D, 0)));\n\n    int next_block_idx = 1;\n\n    vector<vector<vector<bool>>> R_grid = R1_rem;\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                R_grid[x][y][z] = R_grid[x][y][z] && R2_rem[x][y][z];\n            }\n        }\n    }\n\n    auto shared_components = get_components(R_grid, D);\n    sort(shared_components.begin(), shared_components.end(), [](const auto& a, const auto& b) {\n        return a.size() > b.size();\n    });\n\n    for (auto& comp : shared_components) {\n        if (comp.empty()) continue;\n        if (try_place_shared_block(comp, R1_rem, R2_rem, D, next_block_idx, b1_grid, b2_grid)) {\n        }\n    }\n\n    auto comps1 = get_components(R1_rem, D);\n    sort(comps1.begin(), comps1.end(), [](const auto& a, const auto& b) {\n        return a.size() > b.size();\n    });\n    for (auto& comp : comps1) {\n        if (comp.empty()) continue;\n        if (try_place_non_shared_block(comp, R1_rem, D, next_block_idx, b1_grid, true, false)) {\n        }\n    }\n\n    auto comps2 = get_components(R2_rem, D);\n    sort(comps2.begin(), comps2.end(), [](const auto& a, const auto& b) {\n        return a.size() > b.size();\n    });\n    for (auto& comp : comps2) {\n        if (comp.empty()) continue;\n        if (try_place_non_shared_block(comp, R2_rem, D, next_block_idx, b2_grid, false, true)) {\n        }\n    }\n\n    cout << next_block_idx - 1 << \"\\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                cout << b1_grid[x][y][z];\n                if (x == D-1 && y == D-1 && z == D-1) {\n                } else {\n                    cout << \" \";\n                }\n            }\n        }\n    }\n    cout << \"\\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                cout << b2_grid[x][y][z];\n                if (x == D-1 && y == D-1 && z == D-1) {\n                } else {\n                    cout << \" \";\n                }\n            }\n        }\n    }\n    cout << endl;\n\n    return 0;\n}","ahc020":"#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, K;\n    cin >> N >> M >> K;\n\n    vector<ll> stations_x(N), stations_y(N);\n    for (int i = 0; i < N; i++) {\n        cin >> stations_x[i] >> stations_y[i];\n    }\n\n    vector<tuple<int, int, ll>> edges;\n    for (int i = 0; i < M; i++) {\n        int u, v;\n        ll w;\n        cin >> u >> v >> w;\n        u--; v--;\n        edges.push_back({u, v, w});\n    }\n\n    vector<ll> residents_x(K), residents_y(K);\n    for (int i = 0; i < K; i++) {\n        cin >> residents_x[i] >> residents_y[i];\n    }\n\n    vector<vector<ll>> D_mat(N, vector<ll>(K, 0));\n    for (int i = 0; i < N; i++) {\n        for (int k = 0; k < K; k++) {\n            ll dx = stations_x[i] - residents_x[k];\n            ll dy = stations_y[i] - residents_y[k];\n            D_mat[i][k] = dx*dx + dy*dy;\n        }\n    }\n\n    vector<vector<ll>> d_graph(N, vector<ll>(N, LLONG_MAX/2));\n    vector<vector<int>> next_node(N, vector<int>(N, -1));\n    for (int i = 0; i < N; i++) {\n        d_graph[i][i] = 0;\n        next_node[i][i] = i;\n    }\n    for (auto &e : edges) {\n        int u = get<0>(e);\n        int v = get<1>(e);\n        ll w = get<2>(e);\n        if (w < d_graph[u][v]) {\n            d_graph[u][v] = w;\n            d_graph[v][u] = w;\n            next_node[u][v] = v;\n            next_node[v][u] = u;\n        }\n    }\n\n    for (int k = 0; k < N; k++) {\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                if (d_graph[i][k] + d_graph[k][j] < d_graph[i][j]) {\n                    d_graph[i][j] = d_graph[i][k] + d_graph[k][j];\n                    next_node[i][j] = next_node[i][k];\n                }\n            }\n        }\n    }\n\n    auto compute_mst_cost = [&](const vector<int>& T) -> ll {\n        int n = T.size();\n        if (n <= 1) return 0;\n        vector<ll> min_edge(n, LLONG_MAX);\n        vector<bool> used(n, false);\n        min_edge[0] = 0;\n        ll total = 0;\n        for (int iter = 0; iter < n; iter++) {\n            int u = -1;\n            for (int i = 0; i < n; i++) {\n                if (!used[i] && (u == -1 || min_edge[i] < min_edge[u])) {\n                    u = i;\n                }\n            }\n            if (u == -1) break;\n            used[u] = true;\n            total += min_edge[u];\n            for (int v = 0; v < n; v++) {\n                if (!used[v]) {\n                    ll w = d_graph[T[u]][T[v]];\n                    if (w < min_edge[v]) {\n                        min_edge[v] = w;\n                    }\n                }\n            }\n        }\n        return total;\n    };\n\n    auto compute_station_cost = [&](const vector<int>& T) -> ll {\n        vector<ll> cluster_max(N, 0);\n        for (int k = 0; k < K; k++) {\n            ll minD = LLONG_MAX;\n            int best_station = -1;\n            for (int s : T) {\n                if (D_mat[s][k] < minD) {\n                    minD = D_mat[s][k];\n                    best_station = s;\n                }\n            }\n            if (D_mat[best_station][k] > cluster_max[best_station]) {\n                cluster_max[best_station] = D_mat[best_station][k];\n            }\n        }\n        ll cost = 0;\n        for (int s : T) {\n            ll maxD = cluster_max[s];\n            if (maxD == 0) continue;\n            ll p = sqrt(maxD);\n            if (p * p < maxD) {\n                p++;\n            }\n            if (p > 5000) p = 5000;\n            cost += p * p;\n        }\n        return cost;\n    };\n\n    vector<int> current_T;\n    current_T.push_back(0);\n\n    while (true) {\n        int uncovered_k = -1;\n        for (int k = 0; k < K; k++) {\n            ll minD = LLONG_MAX;\n            for (int s : current_T) {\n                if (D_mat[s][k] < minD) {\n                    minD = D_mat[s][k];\n                }\n            }\n            if (minD > 50000000LL) {\n                uncovered_k = k;\n                break;\n            }\n        }\n        if (uncovered_k == -1) break;\n\n        int best_j = -1;\n        ll best_d_euclid = LLONG_MAX;\n        ll best_d_graph_val = LLONG_MAX;\n        for (int j = 0; j < N; j++) {\n            if (find(current_T.begin(), current_T.end(), j) != current_T.end()) \n                continue;\n\n            ll d_euclid = D_mat[j][uncovered_k];\n            ll d_graph_val = LLONG_MAX;\n            for (int s : current_T) {\n                if (d_graph[s][j] < d_graph_val) {\n                    d_graph_val = d_graph[s][j];\n                }\n            }\n            if (best_j == -1 || d_euclid < best_d_euclid || (d_euclid == best_d_euclid && d_graph_val < best_d_graph_val)) {\n                best_j = j;\n                best_d_euclid = d_euclid;\n                best_d_graph_val = d_graph_val;\n            }\n        }\n        if (best_j == -1) break;\n        current_T.push_back(best_j);\n    }\n\n    for (int iter = 0; iter < 10; iter++) {\n        bool improved = false;\n        ll best_total_after_removal = LLONG_MAX;\n        vector<int> candidate_T_after_removal;\n\n        for (int idx = 1; idx < current_T.size(); idx++) {\n            int i = current_T[idx];\n            bool valid = true;\n            for (int k = 0; k < K; k++) {\n                ll minD_orig = LLONG_MAX;\n                ll minD_after = LLONG_MAX;\n                for (int s : current_T) {\n                    if (D_mat[s][k] < minD_orig) minD_orig = D_mat[s][k];\n                    if (s == i) continue;\n                    if (D_mat[s][k] < minD_after) minD_after = D_mat[s][k];\n                }\n                if (minD_orig == D_mat[i][k] && minD_after > 50000000LL) {\n                    valid = false;\n                    break;\n                }\n            }\n            if (!valid) continue;\n\n            vector<int> candidate_T;\n            for (int s : current_T) {\n                if (s != i) candidate_T.push_back(s);\n            }\n\n            ll edge_cost_candidate = compute_mst_cost(candidate_T);\n            ll station_cost_candidate = compute_station_cost(candidate_T);\n            ll total_candidate = edge_cost_candidate + station_cost_candidate;\n\n            ll current_total = compute_mst_cost(current_T) + compute_station_cost(current_T);\n            if (total_candidate < current_total) {\n                if (total_candidate < best_total_after_removal) {\n                    best_total_after_removal = total_candidate;\n                    candidate_T_after_removal = candidate_T;\n                }\n            }\n        }\n\n        if (best_total_after_removal != LLONG_MAX) {\n            current_T = candidate_T_after_removal;\n            improved = true;\n        }\n\n        if (improved) continue;\n\n        best_total_after_removal = LLONG_MAX;\n        vector<int> candidate_T_after_add;\n\n        for (int j = 0; j < N; j++) {\n            if (find(current_T.begin(), current_T.end(), j) != current_T.end()) \n                continue;\n\n            vector<int> candidate_T = current_T;\n            candidate_T.push_back(j);\n\n            ll edge_cost_candidate = compute_mst_cost(candidate_T);\n            ll station_cost_candidate = compute_station_cost(candidate_T);\n            ll total_candidate = edge_cost_candidate + station_cost_candidate;\n\n            ll current_total = compute_mst_cost(current_T) + compute_station_cost(current_T);\n            if (total_candidate < current_total) {\n                if (total_candidate < best_total_after_removal) {\n                    best_total_after_removal = total_candidate;\n                    candidate_T_after_add = candidate_T;\n                }\n            }\n        }\n\n        if (best_total_after_removal != LLONG_MAX) {\n            current_T = candidate_T_after_add;\n            improved = true;\n        }\n\n        if (!improved) break;\n    }\n\n    vector<int> assignment(K, -1);\n    vector<ll> maxD_in_cluster(N, 0);\n    for (int k = 0; k < K; k++) {\n        ll minD = LLONG_MAX;\n        int best_station = -1;\n        for (int s : current_T) {\n            if (D_mat[s][k] < minD) {\n                minD = D_mat[s][k];\n                best_station = s;\n            }\n        }\n        assignment[k] = best_station;\n        if (D_mat[best_station][k] > maxD_in_cluster[best_station]) {\n            maxD_in_cluster[best_station] = D_mat[best_station][k];\n        }\n    }\n\n    vector<int> P(N, 0);\n    for (int i = 0; i < N; i++) {\n        if (find(current_T.begin(), current_T.end(), i) == current_T.end()) {\n            P[i] = 0;\n        } else {\n            ll maxD = maxD_in_cluster[i];\n            if (maxD == 0) {\n                P[i] = 0;\n            } else {\n                ll p = sqrt(maxD);\n                if (p * p < maxD) {\n                    p++;\n                }\n                if (p > 5000) p = 5000;\n                P[i] = (int)p;\n            }\n        }\n    }\n\n    auto compute_mst_edges = [&](const vector<int>& T) -> vector<pair<int, int>> {\n        int n = T.size();\n        if (n <= 1) return {};\n        vector<ll> min_edge(n, LLONG_MAX);\n        vector<int> parent(n, -1);\n        vector<bool> used(n, false);\n        min_edge[0] = 0;\n        for (int iter = 0; iter < n; iter++) {\n            int u = -1;\n            for (int i = 0; i < n; i++) {\n                if (!used[i] && (u == -1 || min_edge[i] < min_edge[u])) {\n                    u = i;\n                }\n            }\n            if (u == -1) break;\n            used[u] = true;\n            for (int v = 0; v < n; v++) {\n                if (!used[v]) {\n                    ll w = d_graph[T[u]][T[v]];\n                    if (w < min_edge[v]) {\n                        min_edge[v] = w;\n                        parent[v] = u;\n                    }\n                }\n            }\n        }\n        vector<pair<int, int>> edges;\n        for (int i = 1; i < n; i++) {\n            int u_idx = parent[i];\n            edges.push_back({T[u_idx], T[i]});\n        }\n        return edges;\n    };\n\n    auto get_path = [&](int start, int end) -> vector<int> {\n        vector<int> path;\n        int u = start;\n        while (u != end) {\n            path.push_back(u);\n            u = next_node[u][end];\n        }\n        path.push_back(end);\n        return path;\n    };\n\n    vector<pair<int, int>> mst_edges = compute_mst_edges(current_T);\n    vector<bool> edge_used(M, false);\n    for (auto &e : mst_edges) {\n        int u = e.first;\n        int v = e.second;\n        vector<int> path = get_path(u, v);\n        for (int idx = 0; idx < (int)path.size()-1; idx++) {\n            int a = path[idx];\n            int b = path[idx+1];\n            for (int j = 0; j < M; j++) {\n                int u1 = get<0>(edges[j]);\n                int v1 = get<1>(edges[j]);\n                if ((u1 == a && v1 == b) || (u1 == b && v1 == a)) {\n                    edge_used[j] = true;\n                    break;\n                }\n            }\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    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 <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    vector<vector<int>> pyramid(30);\n    for (int x = 0; x < 30; x++) {\n        string line;\n        getline(cin, line);\n        stringstream ss(line);\n        for (int y = 0; y <= x; y++) {\n            int val;\n            ss >> val;\n            pyramid[x].push_back(val);\n        }\n    }\n\n    vector<array<int, 4>> swaps;\n\n    for (int x = 28; x >= 0; x--) {\n        for (int y = 0; y <= x; y++) {\n            int cx = x, cy = y;\n            while (cx < 29) {\n                int left_x = cx + 1;\n                int left_y = cy;\n                int right_x = cx + 1;\n                int right_y = cy + 1;\n\n                int left_val = pyramid[left_x][left_y];\n                int right_val = pyramid[right_x][right_y];\n                int current_val = pyramid[cx][cy];\n\n                if (current_val <= left_val && current_val <= right_val) {\n                    break;\n                }\n\n                int swap_x, swap_y;\n                if (left_val <= right_val) {\n                    swap_x = left_x;\n                    swap_y = left_y;\n                } else {\n                    swap_x = right_x;\n                    swap_y = right_y;\n                }\n\n                swaps.push_back({cx, cy, swap_x, swap_y});\n                swap(pyramid[cx][cy], pyramid[swap_x][swap_y]);\n                cx = swap_x;\n                cy = swap_y;\n            }\n        }\n    }\n\n    cout << swaps.size() << '\\n';\n    for (auto& s : swaps) {\n        cout << s[0] << ' ' << s[1] << ' ' << s[2] << ' ' << s[3] << '\\n';\n    }\n    return 0;\n}","toyota2023summer-final":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\n#include <tuple>\n#include <set>\n#include <cctype>\n#include <cmath>\nusing namespace std;\n\nint main() {\n    int D, N;\n    cin >> D >> N;\n    vector<vector<bool>> is_obstacle(9, vector<bool>(9, false));\n    set<pair<int, int>> non_storage = {{0, 4}};\n\n    for (int idx = 0; idx < N; idx++) {\n        int ri, rj;\n        cin >> ri >> rj;\n        is_obstacle[ri][rj] = true;\n    }\n\n    vector<pair<int, int>> storage_cells;\n    for (int i = 0; i < 9; i++) {\n        for (int j = 0; j < 9; j++) {\n            if (non_storage.find({i, j}) != non_storage.end()) continue;\n            if (is_obstacle[i][j]) continue;\n            storage_cells.push_back({i, j});\n        }\n    }\n\n    vector<vector<int>> dist(9, vector<int>(9, -1));\n    queue<pair<int, int>> q_bfs;\n    dist[0][4] = 0;\n    q_bfs.push({0, 4});\n    const vector<int> dx = {1, -1, 0, 0};\n    const vector<int> dy = {0, 0, 1, -1};\n\n    while (!q_bfs.empty()) {\n        auto [i, j] = q_bfs.front();\n        q_bfs.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 < 9 && nj >= 0 && nj < 9) {\n                if (dist[ni][nj] == -1 && !is_obstacle[ni][nj]) {\n                    dist[ni][nj] = dist[i][j] + 1;\n                    q_bfs.push({ni, nj});\n                }\n            }\n        }\n    }\n\n    sort(storage_cells.begin(), storage_cells.end(), [&](const pair<int, int>& a, const pair<int, int>& b) {\n        if (dist[a.first][a.second] != dist[b.first][b.second]) {\n            return dist[a.first][a.second] > dist[b.first][b.second];\n        }\n        if (a.first != b.first) {\n            return a.first < b.first;\n        }\n        return a.second < b.second;\n    });\n\n    int M = storage_cells.size();\n    vector<vector<int>> container_id(9, vector<int>(9, -1));\n\n    for (int d = 0; d < M; d++) {\n        int t;\n        cin >> t;\n        int index = M - 1 - t;\n        auto [i, j] = storage_cells[index];\n        cout << i << \" \" << j << endl;\n        container_id[i][j] = t;\n    }\n\n    vector<vector<bool>> is_storage(9, vector<bool>(9, false));\n    for (auto [i, j] : storage_cells) {\n        is_storage[i][j] = true;\n    }\n\n    vector<vector<bool>> is_removed(9, vector<bool>(9, false));\n    vector<pair<int, int>> transport_order;\n\n    for (int step = 0; step < M; step++) {\n        vector<vector<bool>> visited(9, vector<bool>(9, false));\n        queue<pair<int, int>> q;\n        if (!is_obstacle[0][4]) {\n            visited[0][4] = true;\n            q.push({0, 4});\n        }\n\n        while (!q.empty()) {\n            auto [i, j] = q.front();\n            q.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 >= 9 || nj < 0 || nj >= 9) continue;\n                if (visited[ni][nj]) continue;\n                if (is_obstacle[ni][nj]) continue;\n                if (is_storage[ni][nj] && !is_removed[ni][nj]) {\n                    continue;\n                }\n                visited[ni][nj] = true;\n                q.push({ni, nj});\n            }\n        }\n\n        vector<tuple<int, int, int>> candidates;\n        for (int i = 0; i < 9; i++) {\n            for (int j = 0; j < 9; j++) {\n                if (!is_storage[i][j]) continue;\n                if (is_removed[i][j]) continue;\n                bool has_visited_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 < 9 && nj >= 0 && nj < 9) {\n                        if (visited[ni][nj]) {\n                            has_visited_neighbor = true;\n                            break;\n                        }\n                    }\n                }\n                if (has_visited_neighbor) {\n                    candidates.push_back(make_tuple(container_id[i][j], i, j));\n                }\n            }\n        }\n\n        sort(candidates.begin(), candidates.end());\n        auto [id_val, i_val, j_val] = candidates[0];\n        transport_order.push_back({i_val, j_val});\n        is_removed[i_val][j_val] = true;\n    }\n\n    for (auto [i, j] : transport_order) {\n        cout << i << \" \" << j << endl;\n    }\n\n    return 0;\n}","ahc024":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int n = 50;\nconst int m = 100;\nconst vector<pair<int, int>> directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};\n\nint main() {\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<set<int>> adj(m + 1);\n    vector<bool> boundary(m + 1, false);\n\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            int c = grid[i][j];\n            if (i == 0 || i == n - 1 || j == 0 || j == n - 1) {\n                boundary[c] = true;\n            }\n            for (auto [dx, dy] : directions) {\n                int ni = i + dx;\n                int nj = j + dy;\n                if (ni >= 0 && ni < n && nj >= 0 && nj < n) {\n                    int d = grid[ni][nj];\n                    if (d != c) {\n                        adj[c].insert(d);\n                        adj[d].insert(c);\n                    }\n                }\n            }\n        }\n    }\n\n    vector<pair<int, int>> boundary_positions;\n    for (int j = 0; j < n; j++) boundary_positions.push_back({n - 1, j});\n    for (int i = n - 2; i >= 0; i--) boundary_positions.push_back({i, n - 1});\n    for (int j = n - 2; j >= 0; j--) boundary_positions.push_back({0, j});\n    for (int i = 1; i < n - 1; i++) boundary_positions.push_back({i, 0});\n\n    vector<set<pair<int, int>>> regions(m + 1);\n    vector<queue<pair<int, int>>> frontiers(m + 1);\n    vector<vector<int>> ans(n, vector<int>(n, 0));\n    vector<bool> placed(m + 1, false);\n\n    int pos_index = 0;\n    for (int c = 1; c <= m; c++) {\n        if (boundary[c]) {\n            if (pos_index >= (int)boundary_positions.size()) break;\n            auto [i, j] = boundary_positions[pos_index++];\n            ans[i][j] = c;\n            regions[c].insert({i, j});\n            frontiers[c].push({i, j});\n            placed[c] = true;\n        }\n    }\n\n    vector<vector<int>> adj_list(m + 1);\n    for (int c = 1; c <= m; c++) {\n        for (int d : adj[c]) {\n            if (d >= 1 && d <= m) {\n                adj_list[c].push_back(d);\n            }\n        }\n    }\n\n    queue<int> q;\n    for (int c = 1; c <= m; c++) {\n        if (placed[c]) {\n            q.push(c);\n        }\n    }\n\n    auto try_place_edge = [&](int c, int d) -> bool {\n        while (!frontiers[c].empty()) {\n            auto cell = frontiers[c].front();\n            vector<pair<int, int>> free_dirs;\n            for (auto [dx, dy] : directions) {\n                int ni = cell.first + dx;\n                int nj = cell.second + dy;\n                if (ni >= 0 && ni < n && nj >= 0 && nj < n && ans[ni][nj] == 0) {\n                    free_dirs.push_back({dx, dy});\n                }\n            }\n            if (!free_dirs.empty()) {\n                auto [dx, dy] = free_dirs[0];\n                int ni = cell.first + dx;\n                int nj = cell.second + dy;\n                ans[ni][nj] = d;\n                regions[d].insert({ni, nj});\n                frontiers[d].push({ni, nj});\n                return true;\n            } else {\n                frontiers[c].pop();\n            }\n        }\n\n        for (auto cell : regions[c]) {\n            for (auto [dx, dy] : directions) {\n                int ni = cell.first + dx;\n                int nj = cell.second + dy;\n                if (ni >= 0 && ni < n && nj >= 0 && nj < n && ans[ni][nj] == 0) {\n                    ans[ni][nj] = c;\n                    regions[c].insert({ni, nj});\n                    frontiers[c].push({ni, nj});\n                    return try_place_edge(c, d);\n                }\n            }\n        }\n        return false;\n    };\n\n    while (!q.empty()) {\n        int c = q.front();\n        q.pop();\n        for (int d : adj_list[c]) {\n            if (placed[d]) continue;\n            if (try_place_edge(c, d)) {\n                placed[d] = true;\n                q.push(d);\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 << ans[i][j];\n        }\n        cout << '\\n';\n    }\n\n    return 0;\n}","ahc025":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(0);\n    cin.tie(0);\n\n    int N, D, Q;\n    cin >> N >> D >> Q;\n\n    vector<vector<int>> cache(N, vector<int>(N, -2));\n\n    auto compare = [&](int i, int j) -> int {\n        if (cache[i][j] != -2) {\n            return cache[i][j];\n        }\n        cout << \"1 1 \" << i << \" \" << j << endl;\n        string res;\n        cin >> res;\n        if (res == \"<\") {\n            cache[i][j] = -1;\n            cache[j][i] = 1;\n        } else if (res == \">\") {\n            cache[i][j] = 1;\n            cache[j][i] = -1;\n        } else {\n            cache[i][j] = 0;\n            cache[j][i] = 0;\n        }\n        return cache[i][j];\n    };\n\n    int queries_used = 0;\n    for (int i = 0; i < N && queries_used < Q; i++) {\n        for (int j = i + 1; j < N && queries_used < Q; j++) {\n            compare(i, j);\n            queries_used++;\n        }\n    }\n\n    while (queries_used < Q) {\n        cout << \"1 1 0 1\" << endl;\n        string res;\n        cin >> res;\n        queries_used++;\n    }\n\n    vector<int> score(N, 1);\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (i == j) continue;\n            if (cache[i][j] >= 0) {\n                score[i]++;\n            }\n        }\n    }\n\n    vector<int> indices(N);\n    iota(indices.begin(), indices.end(), 0);\n    sort(indices.begin(), indices.end(), [&](int a, int b) {\n        return score[a] > score[b];\n    });\n\n    vector<long long> set_load(D, 0);\n    vector<int> assignment(N, -1);\n\n    for (int idx = 0; idx < N; idx++) {\n        int item = indices[idx];\n        int best_set = 0;\n        for (int s = 1; s < D; s++) {\n            if (set_load[s] < set_load[best_set]) {\n                best_set = s;\n            }\n        }\n        assignment[item] = best_set;\n        set_load[best_set] += score[item];\n    }\n\n    for (int i = 0; i < N; i++) {\n        if (i) cout << \" \";\n        cout << assignment[i];\n    }\n    cout << endl;\n\n    return 0;\n}","ahc026":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int n, m;\n    cin >> n >> m;\n    int k = n / m;\n\n    vector<vector<int>> stacks(m);\n    for (int i = 0; i < m; i++) {\n        stacks[i].resize(k);\n        for (int j = 0; j < k; j++) {\n            cin >> stacks[i][j];\n        }\n    }\n\n    vector<int> empty_stacks;\n    vector<pair<int, int>> ops;\n\n    int cur = 1;\n\n    while (cur <= n) {\n        bool carried = false;\n        for (int s = 0; s < m; s++) {\n            if (stacks[s].empty()) continue;\n            if (stacks[s].back() == cur) {\n                ops.push_back({cur, 0});\n                stacks[s].pop_back();\n                if (stacks[s].empty()) {\n                    empty_stacks.push_back(s);\n                }\n                cur++;\n                carried = true;\n                break;\n            }\n        }\n        if (carried) {\n            continue;\n        }\n\n        int src = -1;\n        for (int s = 0; s < m; s++) {\n            if (stacks[s].empty()) continue;\n            auto it = find(stacks[s].begin(), stacks[s].end(), cur);\n            if (it != stacks[s].end()) {\n                src = s;\n                break;\n            }\n        }\n        if (src == -1) {\n            break;\n        }\n\n        auto it = find(stacks[src].begin(), stacks[src].end(), cur);\n        int pos = it - stacks[src].begin();\n        int v = *(it + 1);\n        int dest;\n\n        if (!empty_stacks.empty()) {\n            dest = empty_stacks.back();\n            empty_stacks.pop_back();\n        } else {\n            int z = stacks[src].back();\n            int group1_best = -1;\n            int group2_best = -1;\n\n            for (int s = 0; s < m; s++) {\n                if (s == src) continue;\n                if (stacks[s].empty()) continue;\n                int top_val = stacks[s].back();\n                if (top_val < z) {\n                    if (group1_best == -1 || top_val > stacks[group1_best].back()) {\n                        group1_best = s;\n                    }\n                } else {\n                    if (group2_best == -1 || top_val < stacks[group2_best].back()) {\n                        group2_best = s;\n                    }\n                }\n            }\n\n            if (group1_best != -1) {\n                dest = group1_best;\n            } else if (group2_best != -1) {\n                dest = group2_best;\n            } else {\n                dest = 0;\n            }\n        }\n\n        vector<int> segment(stacks[src].begin() + pos + 1, stacks[src].end());\n        stacks[src].resize(pos + 1);\n        for (int box : segment) {\n            stacks[dest].push_back(box);\n        }\n        ops.push_back({v, dest + 1});\n    }\n\n    for (auto &op : ops) {\n        cout << op.first << \" \" << op.second << \"\\n\";\n    }\n\n    return 0;\n}","ahc027":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N;\n    cin >> N;\n\n    vector<string> h(N-1);\n    for (int i = 0; i < N-1; i++) {\n        cin >> h[i];\n    }\n\n    vector<string> v(N);\n    for (int i = 0; i < N; i++) {\n        cin >> v[i];\n    }\n\n    vector<vector<int>> d_mat(N, vector<int>(N));\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cin >> d_mat[i][j];\n        }\n    }\n\n    vector<vector<int>> dist(N, vector<int>(N, -1));\n    vector<vector<pair<int, int>>> parent(N, vector<pair<int, int>>(N, make_pair(-1, -1)));\n    queue<pair<int, int>> q;\n    dist[0][0] = 0;\n    q.push({0, 0});\n\n    while (!q.empty()) {\n        auto [i, j] = q.front();\n        q.pop();\n        if (i < N-1 && h[i][j] == '0' && dist[i+1][j] == -1) {\n            dist[i+1][j] = dist[i][j] + 1;\n            parent[i+1][j] = {i, j};\n            q.push({i+1, j});\n        }\n        if (i > 0 && h[i-1][j] == '0' && dist[i-1][j] == -1) {\n            dist[i-1][j] = dist[i][j] + 1;\n            parent[i-1][j] = {i, j};\n            q.push({i-1, j});\n        }\n        if (j < N-1 && v[i][j] == '0' && dist[i][j+1] == -1) {\n            dist[i][j+1] = dist[i][j] + 1;\n            parent[i][j+1] = {i, j};\n            q.push({i, j+1});\n        }\n        if (j > 0 && v[i][j-1] == '0' && dist[i][j-1] == -1) {\n            dist[i][j-1] = dist[i][j] + 1;\n            parent[i][j-1] = {i, j};\n            q.push({i, j-1});\n        }\n    }\n\n    int best_i = -1, best_j = -1;\n    double best_val = -1.0;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (dist[i][j] <= 0) continue;\n            double denom = static_cast<double>(dist[i][j]);\n            denom = denom * denom * denom;\n            double val = static_cast<double>(d_mat[i][j]) / denom;\n            if (val > best_val) {\n                best_val = val;\n                best_i = i;\n                best_j = j;\n            }\n        }\n    }\n    if (best_i == -1) {\n        best_i = 0;\n        best_j = 1;\n    }\n\n    vector<char> path_moves;\n    int cur_i = best_i, cur_j = best_j;\n    while (cur_i != 0 || cur_j != 0) {\n        auto [pi, pj] = parent[cur_i][cur_j];\n        int di = cur_i - pi;\n        int dj = cur_j - pj;\n        char c;\n        if (di == 1) c = 'D';\n        else if (di == -1) c = 'U';\n        else if (dj == 1) c = 'R';\n        else c = 'L';\n        path_moves.push_back(c);\n        cur_i = pi;\n        cur_j = pj;\n    }\n    reverse(path_moves.begin(), path_moves.end());\n    string path_str(path_moves.begin(), path_moves.end());\n\n    vector<vector<vector<tuple<int, int, char>>>> moves(N, vector<vector<tuple<int, int, char>>>(N));\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            vector<tuple<int, int, char>> list;\n            if (i < N-1 && h[i][j] == '0') {\n                list.push_back(make_tuple(i+1, j, 'D'));\n            }\n            if (i > 0 && h[i-1][j] == '0') {\n                list.push_back(make_tuple(i-1, j, 'U'));\n            }\n            if (j < N-1 && v[i][j] == '0') {\n                list.push_back(make_tuple(i, j+1, 'R'));\n            }\n            if (j > 0 && v[i][j-1] == '0') {\n                list.push_back(make_tuple(i, j-1, 'L'));\n            }\n            sort(list.begin(), list.end(), [&](const auto& a, const auto& b) {\n                int da = d_mat[get<0>(a)][get<1>(a)];\n                int db = d_mat[get<0>(b)][get<1>(b)];\n                return da > db;\n            });\n            moves[i][j] = list;\n        }\n    }\n\n    struct State {\n        int i, j, dir;\n    };\n    vector<State> stack;\n    vector<vector<bool>> visited(N, vector<bool>(N, false));\n    string base_route = \"\";\n\n    stack.push_back({0, 0, 0});\n    visited[0][0] = true;\n\n    while (!stack.empty()) {\n        State& top = stack.back();\n        int i = top.i;\n        int j = top.j;\n        int& dir_index = top.dir;\n        if (dir_index >= static_cast<int>(moves[i][j].size())) {\n            stack.pop_back();\n            if (!stack.empty()) {\n                State& prev = stack.back();\n                int di = i - prev.i;\n                int dj = j - prev.j;\n                char move_char;\n                if (di == 1) move_char = 'D';\n                else if (di == -1) move_char = 'U';\n                else if (dj == 1) move_char = 'R';\n                else move_char = 'L';\n                char rev_char;\n                if (move_char == 'D') rev_char = 'U';\n                else if (move_char == 'U') rev_char = 'D';\n                else if (move_char == 'R') rev_char = 'L';\n                else rev_char = 'R';\n                base_route += rev_char;\n            }\n        } else {\n            auto [ni, nj, move_char] = moves[i][j][dir_index];\n            dir_index++;\n            if (!visited[ni][nj]) {\n                visited[ni][nj] = true;\n                stack.push_back({ni, nj, 0});\n                base_route += move_char;\n            }\n        }\n    }\n\n    string rev_path;\n    for (int idx = path_str.size() - 1; idx >= 0; idx--) {\n        char c = path_str[idx];\n        if (c == 'R') rev_path += 'L';\n        else if (c == 'L') rev_path += 'R';\n        else if (c == 'U') rev_path += 'D';\n        else if (c == 'D') rev_path += 'U';\n    }\n    string detour = path_str + rev_path;\n\n    const int MAX_LEN = 100000;\n    int base_len = base_route.size();\n    int detour_len = detour.size();\n    if (base_len <= MAX_LEN) {\n        int available = MAX_LEN - base_len;\n        int K = (detour_len > 0) ? (available / detour_len) : 0;\n        base_route.reserve(base_len + K * detour_len);\n        for (int k = 0; k < K; k++) {\n            base_route += detour;\n        }\n    }\n    if (base_route.size() > MAX_LEN) {\n        base_route = base_route.substr(0, MAX_LEN);\n    }\n\n    cout << base_route << endl;\n\n    return 0;\n}","ahc028":"#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(0);\n\n    int N, M;\n    cin >> N >> M;\n    int s_i, s_j;\n    cin >> s_i >> s_j;\n    vector<string> grid(N);\n    for (int i = 0; i < N; i++) {\n        cin >> grid[i];\n    }\n    vector<string> t_list(M);\n    for (int i = 0; i < M; i++) {\n        cin >> t_list[i];\n    }\n\n    unordered_map<string, int> five_gram_map;\n    for (int i = 0; i < M; i++) {\n        five_gram_map[t_list[i]] = i;\n    }\n\n    unordered_map<string, int> prefix_count_map;\n    for (int i = 0; i < M; i++) {\n        string prefix = t_list[i].substr(0, 4);\n        prefix_count_map[prefix]++;\n    }\n\n    const int NUM_RESTARTS = 5;\n    const int MAX_STEPS_PER_RESTART = 3000;\n    int best_covered = -1;\n    ll best_cost = LLONG_MAX;\n    vector<pair<int, int>> best_path;\n\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n\n    for (int restart = 0; restart < NUM_RESTARTS; restart++) {\n        auto local_prefix_count_map = prefix_count_map;\n        vector<bool> local_covered(M, false);\n        int local_covered_count = 0;\n        string suffix(4, '#');\n        int cur_i = s_i;\n        int cur_j = s_j;\n        vector<pair<int, int>> path;\n        ll total_cost = 0;\n\n        for (int step = 0; step < MAX_STEPS_PER_RESTART; step++) {\n            if (local_covered_count == M) {\n                break;\n            }\n\n            vector<tuple<int, int, string, int>> gain_candidates;\n            int best_gain_cost = INT_MAX;\n\n            vector<tuple<int, int, string>> heuristic_candidates;\n            int best_heuristic_val = -1;\n            int best_heuristic_cost = INT_MAX;\n\n            for (int i = 0; i < N; i++) {\n                for (int j = 0; j < N; j++) {\n                    char c = grid[i][j];\n                    string new_suffix = suffix.substr(1) + c;\n\n                    bool forms_5gram = true;\n                    for (char ch : suffix) {\n                        if (ch == '#') {\n                            forms_5gram = false;\n                            break;\n                        }\n                    }\n\n                    int gain = 0;\n                    int gain_index = -1;\n                    if (forms_5gram) {\n                        string five_gram = suffix + string(1, c);\n                        auto it = five_gram_map.find(five_gram);\n                        if (it != five_gram_map.end()) {\n                            int idx = it->second;\n                            if (!local_covered[idx]) {\n                                gain = 1;\n                                gain_index = idx;\n                            }\n                        }\n                    }\n\n                    int move_cost = abs(i - cur_i) + abs(j - cur_j) + 1;\n\n                    if (gain > 0) {\n                        if (move_cost < best_gain_cost) {\n                            best_gain_cost = move_cost;\n                            gain_candidates.clear();\n                            gain_candidates.push_back(make_tuple(i, j, new_suffix, gain_index));\n                        } else if (move_cost == best_gain_cost) {\n                            gain_candidates.push_back(make_tuple(i, j, new_suffix, gain_index));\n                        }\n                    } else {\n                        int heuristic_val = 0;\n                        bool valid = true;\n                        for (char ch : new_suffix) {\n                            if (ch < 'A' || ch > 'Z') {\n                                valid = false;\n                                break;\n                            }\n                        }\n                        if (valid) {\n                            auto it = local_prefix_count_map.find(new_suffix);\n                            if (it != local_prefix_count_map.end()) {\n                                heuristic_val = it->second;\n                            }\n                        }\n\n                        if (heuristic_val > best_heuristic_val) {\n                            best_heuristic_val = heuristic_val;\n                            best_heuristic_cost = move_cost;\n                            heuristic_candidates.clear();\n                            heuristic_candidates.push_back(make_tuple(i, j, new_suffix));\n                        } else if (heuristic_val == best_heuristic_val) {\n                            if (move_cost < best_heuristic_cost) {\n                                best_heuristic_cost = move_cost;\n                                heuristic_candidates.clear();\n                                heuristic_candidates.push_back(make_tuple(i, j, new_suffix));\n                            } else if (move_cost == best_heuristic_cost) {\n                                heuristic_candidates.push_back(make_tuple(i, j, new_suffix));\n                            }\n                        }\n                    }\n                }\n            }\n\n            int chosen_i = -1, chosen_j = -1;\n            string chosen_suffix = \"\";\n            int gain_idx = -1;\n\n            if (!gain_candidates.empty()) {\n                uniform_int_distribution<int> dist(0, (int)gain_candidates.size() - 1);\n                auto candidate = gain_candidates[dist(rng)];\n                chosen_i = get<0>(candidate);\n                chosen_j = get<1>(candidate);\n                chosen_suffix = get<2>(candidate);\n                gain_idx = get<3>(candidate);\n            } else {\n                if (heuristic_candidates.empty()) {\n                    uniform_int_distribution<int> dist(0, N*N-1);\n                    int idx = dist(rng);\n                    chosen_i = idx / N;\n                    chosen_j = idx % N;\n                    chosen_suffix = suffix.substr(1) + grid[chosen_i][chosen_j];\n                } else {\n                    uniform_int_distribution<int> dist(0, (int)heuristic_candidates.size() - 1);\n                    auto candidate = heuristic_candidates[dist(rng)];\n                    chosen_i = get<0>(candidate);\n                    chosen_j = get<1>(candidate);\n                    chosen_suffix = get<2>(candidate);\n                }\n            }\n\n            int move_cost = abs(chosen_i - cur_i) + abs(chosen_j - cur_j) + 1;\n            total_cost += move_cost;\n\n            cur_i = chosen_i;\n            cur_j = chosen_j;\n            suffix = chosen_suffix;\n\n            if (gain_idx != -1) {\n                local_covered[gain_idx] = true;\n                local_covered_count++;\n                string prefix = t_list[gain_idx].substr(0, 4);\n                local_prefix_count_map[prefix]--;\n                if (local_prefix_count_map[prefix] == 0) {\n                    local_prefix_count_map.erase(prefix);\n                }\n            }\n\n            path.push_back({chosen_i, chosen_j});\n        }\n\n        if (local_covered_count > best_covered || (local_covered_count == best_covered && total_cost < best_cost)) {\n            best_covered = local_covered_count;\n            best_cost = total_cost;\n            best_path = path;\n        }\n    }\n\n    cout << best_path.size() << \"\\n\";\n    for (auto &p : best_path) {\n        cout << p.first << \" \" << p.second << \"\\n\";\n    }\n\n    return 0;\n}","ahc030":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <tuple>\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<tuple<vector<pair<int, int>>, int, int>> fields;\n    for (int k = 0; k < M; k++) {\n        int d;\n        cin >> d;\n        vector<pair<int, int>> shape;\n        int max_i = 0, max_j = 0;\n        for (int i = 0; i < d; i++) {\n            int a, b;\n            cin >> a >> b;\n            shape.emplace_back(a, b);\n            if (a > max_i) max_i = a;\n            if (b > max_j) max_j = b;\n        }\n        fields.emplace_back(shape, max_i, max_j);\n    }\n\n    priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<>> pq;\n    int center = N / 2;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            int dist = abs(i - center) + abs(j - center);\n            pq.emplace(dist, i, j);\n        }\n    }\n\n    vector<vector<int>> status(N, vector<int>(N, 0));\n    vector<pair<int, int>> positive_list;\n\n    while (!pq.empty()) {\n        auto [dist, i, j] = pq.top();\n        pq.pop();\n\n        if (status[i][j] != 0) {\n            continue;\n        }\n\n        cout << \"q 1 \" << i << \" \" << j << endl;\n        cout.flush();\n\n        int v;\n        cin >> v;\n\n        if (v > 0) {\n            status[i][j] = 1;\n            positive_list.emplace_back(i, j);\n\n            for (const auto& field : fields) {\n                const auto& shape = get<0>(field);\n                int max_i = get<1>(field);\n                int max_j = get<2>(field);\n\n                for (const auto& offset : shape) {\n                    int a = offset.first;\n                    int b = offset.second;\n                    int p = i - a;\n                    int q_val = j - b;\n                    if (p < 0 || q_val < 0) continue;\n                    if (p + max_i >= N || q_val + max_j >= N) continue;\n\n                    for (const auto& cell : shape) {\n                        int i2 = p + cell.first;\n                        int j2 = q_val + cell.second;\n                        if (i2 < N && j2 < N && status[i2][j2] == 0) {\n                            int dist2 = abs(i2 - center) + abs(j2 - center);\n                            pq.emplace(dist2, i2, j2);\n                        }\n                    }\n                }\n            }\n        } else {\n            status[i][j] = 2;\n        }\n    }\n\n    cout << \"a \" << positive_list.size();\n    for (const auto& pos : positive_list) {\n        cout << \" \" << pos.first << \" \" << pos.second;\n    }\n    cout << endl;\n    cout.flush();\n\n    int result;\n    cin >> result;\n    return 0;\n}","ahc031":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Rect {\n    int y1, x1, y2, x2;\n    int area() const { \n        return (y2 - y1) * (x2 - x1); \n    }\n};\n\nbool overlap(const Rect& a, const Rect& b) {\n    return (a.y1 < b.y2 && b.y1 < a.y2 && a.x1 < b.x2 && b.x1 < a.x2);\n}\n\nvector<Rect> compute_free_space(const vector<Rect>& rects, int W) {\n    vector<Rect> free = {{0, 0, W, W}};\n    for (const Rect& r : rects) {\n        vector<Rect> new_free;\n        for (const Rect& f : free) {\n            if (!overlap(r, f)) {\n                new_free.push_back(f);\n            } else {\n                if (f.y1 < r.y1) {\n                    new_free.push_back({f.y1, f.x1, r.y1, f.x2});\n                }\n                if (r.y2 < f.y2) {\n                    new_free.push_back({r.y2, f.x1, f.y2, f.x2});\n                }\n                if (f.x1 < r.x1) {\n                    new_free.push_back({max(f.y1, r.y1), f.x1, min(f.y2, r.y2), r.x1});\n                }\n                if (r.x2 < f.x2) {\n                    new_free.push_back({max(f.y1, r.y1), r.x2, min(f.y2, r.y2), f.x2});\n                }\n            }\n        }\n        free = new_free;\n    }\n    return free;\n}\n\nvector<Rect> pack_day(int W, const vector<int>& areas, vector<Rect> free_space) {\n    int N = areas.size();\n    vector<tuple<int, int, Rect>> list;\n    for (int i = 0; i < N; i++) {\n        list.push_back(make_tuple(i, areas[i], Rect()));\n    }\n    sort(list.begin(), list.end(), [](const auto& a, const auto& b) {\n        return get<1>(a) > get<1>(b);\n    });\n\n    auto cmp = [](const Rect& a, const Rect& b) {\n        return a.area() < b.area();\n    };\n    priority_queue<Rect, vector<Rect>, decltype(cmp)> gaps(cmp);\n    for (Rect& gap : free_space) {\n        gaps.push(gap);\n    }\n\n    vector<Rect> placed_rects;\n\n    for (auto& item : list) {\n        int orig_idx = get<0>(item);\n        int a = get<1>(item);\n        bool found = false;\n        vector<Rect> temp_gaps;\n\n        while (!gaps.empty()) {\n            Rect gap = gaps.top();\n            gaps.pop();\n            if (gap.area() < a) {\n                temp_gaps.push_back(gap);\n                continue;\n            }\n            int gap_height = gap.y2 - gap.y1;\n            int gap_width = gap.x2 - gap.x1;\n            int h0 = (a + gap_width - 1) / gap_width;\n            if (h0 > gap_height) {\n                temp_gaps.push_back(gap);\n                continue;\n            }\n            int chosen_h = -1;\n            int chosen_w = -1;\n            for (int h = h0; h <= gap_height; h++) {\n                int w = (a + h - 1) / h;\n                if (w <= gap_width) {\n                    chosen_h = h;\n                    chosen_w = w;\n                    break;\n                }\n            }\n            if (chosen_h == -1) {\n                temp_gaps.push_back(gap);\n                continue;\n            }\n\n            Rect rect = {gap.y1, gap.x1, gap.y1 + chosen_h, gap.x1 + chosen_w};\n            placed_rects.push_back(rect);\n\n            if (gap_width > chosen_w) {\n                gaps.push({gap.y1, gap.x1 + chosen_w, gap.y2, gap.x2});\n            }\n            if (gap_height > chosen_h) {\n                gaps.push({gap.y1 + chosen_h, gap.x1, gap.y2, gap.x1 + chosen_w});\n            }\n\n            for (Rect& g : temp_gaps) {\n                gaps.push(g);\n            }\n            found = true;\n            break;\n        }\n        if (!found) {\n            if (gaps.empty()) {\n                placed_rects.push_back({0, 0, 1, 1});\n            } else {\n                Rect gap = gaps.top();\n                gaps.pop();\n                int gap_height = gap.y2 - gap.y1;\n                int gap_width = gap.x2 - gap.x1;\n                int h = gap_height;\n                int w = (a + h - 1) / h;\n                if (w > gap_width) {\n                    w = gap_width;\n                    h = (a + w - 1) / w;\n                }\n                Rect rect = {gap.y1, gap.x1, gap.y1 + h, gap.x1 + w};\n                placed_rects.push_back(rect);\n                if (gap_width > w) {\n                    gaps.push({gap.y1, gap.x1 + w, gap.y2, gap.x2});\n                }\n                if (gap_height > h) {\n                    gaps.push({gap.y1 + h, gap.x1, gap.y2, gap.x1 + w});\n                }\n            }\n        }\n    }\n\n    vector<Rect> result(N);\n    for (int i = 0; i < placed_rects.size(); i++) {\n        int orig_idx = get<0>(list[i]);\n        result[orig_idx] = placed_rects[i];\n    }\n    return result;\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int W, D, N;\n    cin >> W >> D >> N;\n\n    vector<vector<int>> areas(D, vector<int>(N));\n    for (int d = 0; d < D; d++) {\n        for (int k = 0; k < N; k++) {\n            cin >> areas[d][k];\n        }\n    }\n\n    vector<vector<Rect>> all_rects(D);\n\n    vector<Rect> free_space = compute_free_space({}, W);\n    all_rects[0] = pack_day(W, areas[0], free_space);\n\n    for (int d = 1; d < D; d++) {\n        free_space = compute_free_space(all_rects[d-1], W);\n        all_rects[d] = pack_day(W, areas[d], free_space);\n    }\n\n    for (int d = 0; d < D; d++) {\n        for (int k = 0; k < N; k++) {\n            Rect r = all_rects[d][k];\n            cout << r.y1 << \" \" << r.x1 << \" \" << r.y2 << \" \" << r.x2 << '\\n';\n        }\n    }\n\n    return 0;\n}","ahc032":"#include <iostream>\n#include <vector>\n#include <tuple>\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>> a(N, vector<long long>(N));\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cin >> a[i][j];\n        }\n    }\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<vector<long long>> board(N, vector<long long>(N, 0));\n    vector<tuple<int, int, int>> operations;\n\n    for (int step = 0; step < K; step++) {\n        long long best_gain = 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 gain = 0;\n                    for (int di = 0; di < 3; di++) {\n                        for (int dj = 0; dj < 3; dj++) {\n                            int i = p + di;\n                            int j = q + dj;\n                            long long stamp_val = stamps[m][di][dj];\n                            long long old_val = a[i][j] + board[i][j];\n                            long long old_mod = old_val % MOD;\n                            long long new_val = old_val + stamp_val;\n                            long long new_mod = new_val % MOD;\n                            gain += (new_mod - old_mod);\n                        }\n                    }\n                    if (gain > best_gain) {\n                        best_gain = gain;\n                        best_m = m;\n                        best_p = p;\n                        best_q = q;\n                    }\n                }\n            }\n        }\n\n        if (best_gain <= 0) {\n            break;\n        }\n\n        operations.emplace_back(best_m, best_p, best_q);\n        for (int di = 0; di < 3; di++) {\n            for (int dj = 0; dj < 3; dj++) {\n                int i = best_p + di;\n                int j = best_q + dj;\n                board[i][j] += stamps[best_m][di][dj];\n            }\n        }\n    }\n\n    cout << operations.size() << '\\n';\n    for (auto [m, p, q] : operations) {\n        cout << m << ' ' << p << ' ' << q << '\\n';\n    }\n\n    return 0;\n}","ahc033":"#include <bits/stdc++.h>\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        return x == other.x && y == other.y;\n    }\n};\n\nchar compute_next_move(Point start, Point goal, const vector<vector<int>>& grid, bool is_carrying, bool is_large) {\n    if (start == goal) {\n        return '.';\n    }\n    \n    vector<vector<bool>> visited(5, vector<bool>(5, false));\n    vector<vector<Point>> parent(5, vector<Point>(5, Point(-1, -1)));\n    queue<Point> q;\n    q.push(start);\n    visited[start.x][start.y] = true;\n    \n    const vector<int> dx = {-1, 1, 0, 0};\n    const vector<int> dy = {0, 0, -1, 1};\n    const vector<char> dirs = {'U', 'D', 'L', 'R'};\n    bool found = false;\n    \n    while (!q.empty()) {\n        Point p = q.front();\n        q.pop();\n        if (p.x == goal.x && p.y == goal.y) {\n            found = true;\n            break;\n        }\n        \n        for (int d = 0; d < 4; d++) {\n            int nx = p.x + dx[d];\n            int ny = p.y + dy[d];\n            if (nx < 0 || nx >= 5 || ny < 0 || ny >= 5) continue;\n            if (visited[nx][ny]) continue;\n            \n            bool allowed;\n            if (is_carrying) {\n                if (is_large) {\n                    allowed = true;\n                } else {\n                    allowed = (grid[nx][ny] == -1);\n                }\n            } else {\n                allowed = true;\n            }\n            \n            if (allowed) {\n                visited[nx][ny] = true;\n                parent[nx][ny] = p;\n                q.push(Point(nx, ny));\n            }\n        }\n    }\n    \n    if (!found) {\n        return '.';\n    }\n    \n    Point p = goal;\n    while (!(parent[p.x][p.y] == start)) {\n        p = parent[p.x][p.y];\n    }\n    \n    int diff_x = p.x - start.x;\n    int diff_y = p.y - start.y;\n    if (diff_x == -1) return 'U';\n    if (diff_x == 1) return 'D';\n    if (diff_y == -1) return 'L';\n    if (diff_y == 1) return 'R';\n    return '.';\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \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<vector<int>> grid(5, vector<int>(5, -1));\n    vector<int> next_arrival_index(5, 0);\n    vector<int> dispatched(5, 0);\n    vector<int> next_expected(5);\n    for (int i = 0; i < 5; i++) {\n        next_expected[i] = 5 * i;\n    }\n    \n    struct Crane {\n        Point pos;\n        int hold;\n    };\n    vector<Crane> cranes(5);\n    for (int i = 0; i < 5; i++) {\n        cranes[i] = {{i, 0}, -1};\n    }\n    \n    vector<string> output_lines(5);\n    \n    for (int turn = 0; turn < 10000; turn++) {\n        for (int i = 0; i < 5; i++) {\n            if (next_arrival_index[i] < 5) {\n                bool can_place = true;\n                if (grid[i][0] != -1) {\n                    can_place = false;\n                } else {\n                    for (int k = 0; k < 5; k++) {\n                        if (cranes[k].pos.x == i && cranes[k].pos.y == 0 && cranes[k].hold != -1) {\n                            can_place = false;\n                            break;\n                        }\n                    }\n                }\n                if (can_place) {\n                    int b = A[i][next_arrival_index[i]];\n                    grid[i][0] = b;\n                    next_arrival_index[i]++;\n                }\n            }\n        }\n        \n        vector<Point> old_positions(5);\n        for (int i = 0; i < 5; i++) {\n            old_positions[i] = cranes[i].pos;\n        }\n        \n        vector<char> actions(5, '.');\n        vector<Point> new_positions(5);\n        for (int i = 0; i < 5; i++) {\n            new_positions[i] = old_positions[i];\n        }\n        \n        for (int i = 0; i < 5; i++) {\n            bool is_large = (i == 0);\n            bool is_carrying = (cranes[i].hold != -1);\n            \n            if (is_carrying) {\n                int b = cranes[i].hold;\n                int target_row = b / 5;\n                Point target(target_row, 4);\n                if (old_positions[i].x == target_row && old_positions[i].y == 4) {\n                    actions[i] = 'Q';\n                    new_positions[i] = old_positions[i];\n                } else {\n                    char dir = compute_next_move(old_positions[i], target, grid, is_carrying, is_large);\n                    if (dir != '.') {\n                        int nx = old_positions[i].x;\n                        int ny = old_positions[i].y;\n                        if (dir == 'U') nx--;\n                        else if (dir == 'D') nx++;\n                        else if (dir == 'L') ny--;\n                        else if (dir == 'R') ny++;\n                        new_positions[i] = Point(nx, ny);\n                        actions[i] = dir;\n                    } else {\n                        actions[i] = '.';\n                        new_positions[i] = old_positions[i];\n                    }\n                }\n            } else {\n                if (grid[old_positions[i].x][old_positions[i].y] != -1) {\n                    int b = grid[old_positions[i].x][old_positions[i].y];\n                    int target_row = b / 5;\n                    if (b == next_expected[target_row]) {\n                        actions[i] = 'P';\n                        new_positions[i] = old_positions[i];\n                    } else {\n                        actions[i] = 'P';\n                        new_positions[i] = old_positions[i];\n                    }\n                } else {\n                    if (is_large) {\n                        Point goal(-1, -1);\n                        for (int x = 0; x < 5; x++) {\n                            for (int y = 0; y < 5; y++) {\n                                if (grid[x][y] != -1) {\n                                    int b = grid[x][y];\n                                    int expected_b = 5 * (b / 5) + dispatched[b / 5];\n                                    if (b == expected_b) {\n                                        goal = Point(x, y);\n                                        break;\n                                    }\n                                }\n                            }\n                            if (goal.x != -1) break;\n                        }\n                        if (goal.x == -1) {\n                            goal = Point(2, 2);\n                        }\n                        char dir = compute_next_move(old_positions[i], goal, grid, is_carrying, is_large);\n                        if (dir != '.') {\n                            int nx = old_positions[i].x;\n                            int ny = old_positions[i].y;\n                            if (dir == 'U') nx--;\n                            else if (dir == 'D') nx++;\n                            else if (dir == 'L') ny--;\n                            else if (dir == 'R') ny++;\n                            new_positions[i] = Point(nx, ny);\n                            actions[i] = dir;\n                        } else {\n                            actions[i] = '.';\n                            new_positions[i] = old_positions[i];\n                        }\n                    } else {\n                        if (grid[old_positions[i].x][1] == -1 && old_positions[i].y < 4) {\n                            actions[i] = 'R';\n                            new_positions[i] = Point(old_positions[i].x, old_positions[i].y + 1);\n                        } else {\n                            actions[i] = '.';\n                            new_positions[i] = old_positions[i];\n                        }\n                    }\n                }\n            }\n            \n            bool valid = true;\n            for (int j = 0; j < i; j++) {\n                if (new_positions[i] == new_positions[j]) {\n                    valid = false;\n                    break;\n                }\n                if (new_positions[i] == old_positions[j] && new_positions[j] == old_positions[i]) {\n                    valid = false;\n                    break;\n                }\n            }\n            if (!valid) {\n                actions[i] = '.';\n                new_positions[i] = old_positions[i];\n            }\n        }\n        \n        for (int i = 0; i < 5; i++) {\n            output_lines[i] += actions[i];\n        }\n        \n        for (int i = 0; i < 5; i++) {\n            if (actions[i] == 'P' && grid[old_positions[i].x][old_positions[i].y] != -1) {\n                int b = grid[old_positions[i].x][old_positions[i].y];\n                cranes[i].hold = b;\n                grid[old_positions[i].x][old_positions[i].y] = -1;\n            } else if (actions[i] == 'Q' && cranes[i].hold != -1) {\n                int b = cranes[i].hold;\n                grid[old_positions[i].x][old_positions[i].y] = b;\n                cranes[i].hold = -1;\n            } else if (actions[i] == 'U' || actions[i] == 'D' || actions[i] == 'L' || actions[i] == 'R') {\n                int dx = 0, dy = 0;\n                if (actions[i] == 'U') dx = -1;\n                else if (actions[i] == 'D') dx = 1;\n                else if (actions[i] == 'L') dy = -1;\n                else if (actions[i] == 'R') dy = 1;\n                cranes[i].pos.x = old_positions[i].x + dx;\n                cranes[i].pos.y = old_positions[i].y + dy;\n            }\n        }\n        \n        for (int i = 0; i < 5; i++) {\n            if (grid[i][4] != -1) {\n                int b = grid[i][4];\n                int expected_val = 5 * i + dispatched[i];\n                if (b == expected_val) {\n                    dispatched[i]++;\n                    next_expected[i] = 5 * i + dispatched[i];\n                }\n                grid[i][4] = -1;\n            }\n        }\n        \n        int total_dispatched = 0;\n        for (int i = 0; i < 5; i++) {\n            total_dispatched += dispatched[i];\n        }\n        if (total_dispatched == 25) {\n            break;\n        }\n    }\n    \n    int max_len = 0;\n    for (int i = 0; i < 5; i++) {\n        if ((int)output_lines[i].size() > max_len) {\n            max_len = output_lines[i].size();\n        }\n    }\n    for (int i = 0; i < 5; i++) {\n        while ((int)output_lines[i].size() < max_len) {\n            output_lines[i] += '.';\n        }\n    }\n    \n    for (int i = 0; i < 5; i++) {\n        cout << output_lines[i] << '\\n';\n    }\n    \n    return 0;\n}","ahc034":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    const int N = 20;\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<int>> remaining_source(N, vector<int>(N, 0));\n    vector<vector<int>> remaining_sink(N, vector<int>(N, 0));\n\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (grid[i][j] > 0) {\n                remaining_source[i][j] = grid[i][j];\n            } else if (grid[i][j] < 0) {\n                remaining_sink[i][j] = -grid[i][j];\n            }\n        }\n    }\n\n    int cur_i = 0, cur_j = 0;\n    int cur_load = 0;\n\n    auto has_remaining = [&]() -> bool {\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                if (remaining_source[i][j] > 0 || remaining_sink[i][j] > 0) {\n                    return true;\n                }\n            }\n        }\n        return false;\n    };\n\n    while (has_remaining()) {\n        bool handled = false;\n\n        if (remaining_source[cur_i][cur_j] > 0) {\n            int d = remaining_source[cur_i][cur_j];\n            cout << \"+\" << d << \"\\n\";\n            cur_load += d;\n            remaining_source[cur_i][cur_j] = 0;\n            handled = true;\n        }\n        else if (remaining_sink[cur_i][cur_j] > 0 && cur_load > 0) {\n            int d = min(remaining_sink[cur_i][cur_j], cur_load);\n            cout << \"-\" << d << \"\\n\";\n            cur_load -= d;\n            remaining_sink[cur_i][cur_j] -= d;\n            handled = true;\n        }\n\n        if (handled) {\n            continue;\n        }\n\n        int best_i = -1, best_j = -1;\n        int min_score = INT_MAX;\n\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                if (remaining_source[i][j] > 0) {\n                    int dist = abs(i - cur_i) + abs(j - cur_j);\n                    int score = dist * (100 + cur_load);\n                    if (score < min_score) {\n                        min_score = score;\n                        best_i = i;\n                        best_j = j;\n                    }\n                }\n                if (remaining_sink[i][j] > 0 && cur_load > 0) {\n                    int dist = abs(i - cur_i) + abs(j - cur_j);\n                    int score = dist * (100 + cur_load);\n                    if (score < min_score) {\n                        min_score = score;\n                        best_i = i;\n                        best_j = j;\n                    }\n                }\n            }\n        }\n\n        if (best_i == -1) {\n            break;\n        }\n\n        int dx = best_i - cur_i;\n        int dy = best_j - cur_j;\n        string moves = \"\";\n        if (dx > 0) moves += string(dx, 'D');\n        else if (dx < 0) moves += string(-dx, 'U');\n        if (dy > 0) moves += string(dy, 'R');\n        else if (dy < 0) moves += string(-dy, 'L');\n\n        for (char c : moves) {\n            cout << c << \"\\n\";\n            if (c == 'U') cur_i--;\n            else if (c == 'D') cur_i++;\n            else if (c == 'L') cur_j--;\n            else if (c == 'R') cur_j++;\n        }\n    }\n\n    return 0;\n}","ahc035":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int N, M, T;\n    cin >> N >> M >> T;\n\n    vector<tuple<int, int, int>> positions;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            int deg = 4;\n            if (i == 0 || i == N-1) deg--;\n            if (j == 0 || j == N-1) deg--;\n            positions.emplace_back(i, j, deg);\n        }\n    }\n    sort(positions.begin(), positions.end(), [](const auto& a, const auto& b) {\n        if (get<2>(a) != get<2>(b)) \n            return get<2>(a) > get<2>(b);\n        if (get<0>(a) != get<0>(b))\n            return get<0>(a) < get<0>(b);\n        return get<1>(a) < get<1>(b);\n    });\n\n    int seed_count = 2 * N * (N - 1);\n    vector<vector<int>> seeds(seed_count, vector<int>(M));\n    for (int k = 0; k < seed_count; k++) {\n        for (int l = 0; l < M; l++) {\n            cin >> seeds[k][l];\n        }\n    }\n\n    for (int t = 0; t < T; t++) {\n        vector<pair<long long, int>> scores;\n        for (int k = 0; k < seed_count; k++) {\n            long long v = 0;\n            for (int l = 0; l < M; l++) {\n                v += seeds[k][l];\n            }\n            scores.emplace_back(v, k);\n        }\n        sort(scores.begin(), scores.end(), [](const auto& a, const auto& b) {\n            return a.first > b.first;\n        });\n\n        vector<int> selected;\n        for (int i = 0; i < N * N; i++) {\n            selected.push_back(scores[i].second);\n        }\n\n        vector<vector<int>> grid(N, vector<int>(N));\n        for (int idx = 0; idx < N * N; idx++) {\n            auto [i, j, deg] = positions[idx];\n            grid[i][j] = selected[idx];\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 << '\\n';\n        }\n        cout.flush();\n\n        for (int k = 0; k < seed_count; k++) {\n            for (int l = 0; l < M; l++) {\n                cin >> seeds[k][l];\n            }\n        }\n    }\n\n    return 0;\n}","ahc038":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <cctype>\n#include <cmath>\n#include <string>\n#include <map>\n#include <set>\n#include <queue>\n#include <stack>\n#include <utility>\n#include <climits>\nusing namespace std;\n\ntypedef pair<int, int> Point;\n\nvector<char> get_manhattan_path(Point from, Point to) {\n    vector<char> path;\n    int dx = to.first - from.first;\n    int dy = to.second - from.second;\n    char x_dir = 0;\n    char y_dir = 0;\n    if (dx > 0) x_dir = 'D';\n    else if (dx < 0) x_dir = 'U';\n    if (dy > 0) y_dir = 'R';\n    else if (dy < 0) y_dir = 'L';\n\n    for (int i = 0; i < abs(dx); i++) {\n        path.push_back(x_dir);\n    }\n    for (int i = 0; i < abs(dy); i++) {\n        path.push_back(y_dir);\n    }\n    return path;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, V;\n    cin >> N >> M >> V;\n\n    vector<string> s_grid(N);\n    vector<string> 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    vector<Point> A;\n    vector<Point> B;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (s_grid[i][j] == '1') {\n                A.push_back({i, j});\n            }\n            if (t_grid[i][j] == '1') {\n                B.push_back({i, j});\n            }\n        }\n    }\n\n    sort(A.begin(), A.end());\n    sort(B.begin(), B.end());\n\n    int V_prime = 5;\n    cout << V_prime << \"\\n\";\n    for (int u = 1; u < V_prime; u++) {\n        cout << 0 << \" \" << 1 << \"\\n\";\n    }\n    cout << 0 << \" \" << 0 << \"\\n\";\n\n    vector<string> operations;\n\n    operations.push_back(\".\" + string(\"RRRL\") + string(\".....\"));\n    operations.push_back(\".\" + string(\"..R.\") + string(\".....\"));\n\n    Point current_root = {0, 0};\n\n    vector<pair<Point, Point>> valid_pairs;\n    for (int i = 0; i < M; i++) {\n        if (A[i] != B[i]) {\n            valid_pairs.push_back({A[i], B[i]});\n        }\n    }\n\n    auto get_options = [&](int i, int j) -> vector<pair<int, Point>> {\n        vector<pair<int, Point>> options;\n        if (j - 1 >= 0) {\n            options.push_back({1, {i, j - 1}});\n        }\n        if (i - 1 >= 0) {\n            options.push_back({2, {i - 1, j}});\n        }\n        if (j + 1 < N) {\n            options.push_back({3, {i, j + 1}});\n        }\n        if (i + 1 < N) {\n            options.push_back({4, {i + 1, j}});\n        }\n        return options;\n    };\n\n    for (auto &p : valid_pairs) {\n        Point a = p.first;\n        Point b = p.second;\n\n        auto options_a = get_options(a.first, a.second);\n        if (options_a.empty()) {\n            continue;\n        }\n        int best_u = -1;\n        Point best_R_a;\n        int min_dist = INT_MAX;\n        for (auto &opt : options_a) {\n            int u_idx = opt.first;\n            Point R = opt.second;\n            int dist = abs(current_root.first - R.first) + abs(current_root.second - R.second);\n            if (dist < min_dist) {\n                min_dist = dist;\n                best_u = u_idx;\n                best_R_a = R;\n            }\n        }\n        Point R_a = best_R_a;\n\n        auto options_b = get_options(b.first, b.second);\n        if (options_b.empty()) {\n            continue;\n        }\n        auto best_b = options_b[0];\n        int v_rel = best_b.first;\n        Point R_b = best_b.second;\n\n        vector<char> path1 = get_manhattan_path(current_root, R_a);\n        if (path1.empty()) {\n            string action_str = \".....\";\n            action_str[0] = '.';\n            if (best_u == 1) action_str[1] = 'P';\n            else if (best_u == 2) action_str[2] = 'P';\n            else if (best_u == 3) action_str[3] = 'P';\n            else if (best_u == 4) action_str[4] = 'P';\n            operations.push_back(\".\" + string(\"....\") + action_str);\n        } else {\n            for (int idx = 0; idx < path1.size(); idx++) {\n                string rot_str = \"....\";\n                string action_str = \".....\";\n                if (idx == path1.size() - 1) {\n                    action_str[0] = '.';\n                    if (best_u == 1) action_str[1] = 'P';\n                    else if (best_u == 2) action_str[2] = 'P';\n                    else if (best_u == 3) action_str[3] = 'P';\n                    else if (best_u == 4) action_str[4] = 'P';\n                }\n                string op = string(1, path1[idx]) + rot_str + action_str;\n                operations.push_back(op);\n            }\n        }\n\n        vector<char> path2 = get_manhattan_path(R_a, R_b);\n        if (path2.empty()) {\n            string action_str = \".....\";\n            action_str[0] = '.';\n            if (v_rel == 1) action_str[1] = 'P';\n            else if (v_rel == 2) action_str[2] = 'P';\n            else if (v_rel == 3) action_str[3] = 'P';\n            else if (v_rel == 4) action_str[4] = 'P';\n            operations.push_back(\".\" + string(\"....\") + action_str);\n        } else {\n            for (int idx = 0; idx < path2.size(); idx++) {\n                string rot_str = \"....\";\n                string action_str = \".....\";\n                if (idx == path2.size() - 1) {\n                    action_str[0] = '.';\n                    if (v_rel == 1) action_str[1] = 'P';\n                    else if (v_rel == 2) action_str[2] = 'P';\n                    else if (v_rel == 3) action_str[3] = 'P';\n                    else if (v_rel == 4) action_str[4] = 'P';\n                }\n                string op = string(1, path2[idx]) + rot_str + action_str;\n                operations.push_back(op);\n            }\n        }\n\n        current_root = R_b;\n    }\n\n    for (string &op : operations) {\n        cout << op << \"\\n\";\n    }\n\n    return 0;\n}","ahc039":"#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\n\nstruct Point {\n    int x, y;\n};\n\nstruct Rect {\n    int x1, x2, y1, y2;\n    Rect() : x1(0), x2(0), y1(0), y2(0) {}\n    Rect(int a, int b, int c, int d) {\n        x1 = min(a, b); x2 = max(a, b);\n        y1 = min(c, d); y2 = max(c, d);\n    }\n};\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    const int N = 5000;\n    vector<Point> mackerels(N), sardines(N);\n    for (int i = 0; i < N; i++) {\n        cin >> mackerels[i].x >> mackerels[i].y;\n    }\n    for (int i = 0; i < N; i++) {\n        cin >> sardines[i].x >> sardines[i].y;\n    }\n\n    vector<int> xs, ys;\n    for (int i = 0; i < N; i++) {\n        xs.push_back(mackerels[i].x);\n        ys.push_back(mackerels[i].y);\n        xs.push_back(sardines[i].x);\n        ys.push_back(sardines[i].y);\n    }\n    sort(xs.begin(), xs.end());\n    xs.erase(unique(xs.begin(), xs.end()), xs.end());\n    sort(ys.begin(), ys.end());\n    ys.erase(unique(ys.begin(), ys.end()), ys.end());\n\n    int X = xs.size(), Y = ys.size();\n\n    vector<vector<int>> grid_val(X, vector<int>(Y, 0));\n    for (int i = 0; i < N; i++) {\n        auto p = mackerels[i];\n        int ix = lower_bound(xs.begin(), xs.end(), p.x) - xs.begin();\n        int iy = lower_bound(ys.begin(), ys.end(), p.y) - ys.begin();\n        if (ix < X && iy < Y) {\n            grid_val[ix][iy] = 1;\n        }\n    }\n    for (int i = 0; i < N; i++) {\n        auto p = sardines[i];\n        int ix = lower_bound(xs.begin(), xs.end(), p.x) - xs.begin();\n        int iy = lower_bound(ys.begin(), ys.end(), p.y) - ys.begin();\n        if (ix < X && iy < Y) {\n            grid_val[ix][iy] = -1;\n        }\n    }\n\n    vector<vector<int>> P(X+1, vector<int>(Y+1, 0));\n    for (int i = 0; i < X; i++) {\n        for (int j = 0; j < Y; j++) {\n            P[i+1][j+1] = P[i][j+1] + P[i+1][j] - P[i][j] + grid_val[i][j];\n        }\n    }\n\n    auto get_sum = [&](int x1, int x2, int y1, int y2) -> int {\n        int i1 = lower_bound(xs.begin(), xs.end(), x1) - xs.begin();\n        int i2 = upper_bound(xs.begin(), xs.end(), x2) - xs.begin();\n        if (i1 >= i2) return 0;\n        i2--;\n\n        int j1 = lower_bound(ys.begin(), ys.end(), y1) - ys.begin();\n        int j2 = upper_bound(ys.begin(), ys.end(), y2) - ys.begin();\n        if (j1 >= j2) return 0;\n        j2--;\n\n        return P[i2+1][j2+1] - P[i1][j2+1] - P[i2+1][j1] + P[i1][j1];\n    };\n\n    auto greedy_improve = [&](Rect r) -> Rect {\n        int x1 = r.x1, x2 = r.x2, y1 = r.y1, y2 = r.y2;\n        bool improved;\n        do {\n            improved = false;\n            while (x1 < x2) {\n                int strip_sum = get_sum(x1, x1, y1, y2);\n                if (strip_sum < 0) {\n                    x1++;\n                    improved = true;\n                } else {\n                    break;\n                }\n            }\n            while (x2 > x1) {\n                int strip_sum = get_sum(x2, x2, y1, y2);\n                if (strip_sum < 0) {\n                    x2--;\n                    improved = true;\n                } else {\n                    break;\n                }\n            }\n            while (y2 > y1) {\n                int strip_sum = get_sum(x1, x2, y2, y2);\n                if (strip_sum < 0) {\n                    y2--;\n                    improved = true;\n                } else {\n                    break;\n                }\n            }\n            while (y1 < y2) {\n                int strip_sum = get_sum(x1, x2, y1, y1);\n                if (strip_sum < 0) {\n                    y1++;\n                    improved = true;\n                } else {\n                    break;\n                }\n            }\n        } while (improved);\n        return Rect(x1, x2, y1, y2);\n    };\n\n    vector<Rect> candidates;\n\n    int min_x = 1000000, max_x = -1, min_y = 1000000, max_y = -1;\n    for (int i = 0; i < N; i++) {\n        min_x = min(min_x, mackerels[i].x);\n        max_x = max(max_x, mackerels[i].x);\n        min_y = min(min_y, mackerels[i].y);\n        max_y = max(max_y, mackerels[i].y);\n    }\n    candidates.emplace_back(min_x, max_x, min_y, max_y);\n\n    for (int i = 0; i < N; i++) {\n        int x = mackerels[i].x;\n        int y = mackerels[i].y;\n        int x1 = max(0, x - 5000);\n        int x2 = min(100000, x + 5000);\n        int y1 = max(0, y - 5000);\n        int y2 = min(100000, y + 5000);\n        candidates.emplace_back(x1, x2, y1, y2);\n    }\n\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n    uniform_int_distribution<int> dist(0, 100000);\n    for (int i = 0; i < 5000; i++) {\n        int x1 = dist(rng);\n        int x2 = dist(rng);\n        if (x1 > x2) swap(x1, x2);\n        int y1 = dist(rng);\n        int y2 = dist(rng);\n        if (y1 > y2) swap(y1, y2);\n        candidates.emplace_back(x1, x2, y1, y2);\n    }\n\n    int best_score = -1000000000;\n    Rect best_rect;\n\n    for (Rect r : candidates) {\n        Rect improved = greedy_improve(r);\n        int score = get_sum(improved.x1, improved.x2, improved.y1, improved.y2);\n        if (score > best_score) {\n            best_score = score;\n            best_rect = improved;\n        }\n    }\n\n    cout << 4 << '\\n';\n    cout << best_rect.x1 << \" \" << best_rect.y1 << '\\n';\n    cout << best_rect.x2 << \" \" << best_rect.y1 << '\\n';\n    cout << best_rect.x2 << \" \" << best_rect.y2 << '\\n';\n    cout << best_rect.x1 << \" \" << best_rect.y2 << '\\n';\n\n    return 0;\n}","ahc040":"#include <iostream>\n#include <vector>\n#include <tuple>\n#include <algorithm>\n#include <random>\n#include <chrono>\n#include <climits>\n#include <cctype>\n#include <cmath>\nusing namespace std;\n\nusing RectInstruction = tuple<int, int, char, int>;\n\npair<long long, long long> simulate_placement(const vector<RectInstruction>& instructions, const vector<double>& w_est, const vector<double>& h_est) {\n    struct Rect {\n        long long x, y, w, h;\n    };\n    vector<Rect> placed;\n    long long current_max_x = 0;\n    long long current_max_y = 0;\n\n    for (const auto& inst : instructions) {\n        int p = get<0>(inst);\n        int r = get<1>(inst);\n        char d = get<2>(inst);\n        int b = get<3>(inst);\n\n        long long w_rect = (long long)round((r == 0) ? w_est[p] : h_est[p]);\n        long long h_rect = (long long)round((r == 0) ? h_est[p] : w_est[p]);\n        w_rect = max(1LL, min(1000000000LL, w_rect));\n        h_rect = max(1LL, min(1000000000LL, h_rect));\n\n        long long x_start = 0;\n        long long y_start = 0;\n\n        if (d == 'U') {\n            if (b == -1) {\n                x_start = 0;\n            } else {\n                x_start = placed[b].x + placed[b].w;\n            }\n\n            y_start = 0;\n            for (const auto& rect : placed) {\n                if (rect.x < x_start + w_rect && rect.x + rect.w > x_start) {\n                    if (rect.y + rect.h > y_start) {\n                        y_start = rect.y + rect.h;\n                    }\n                }\n            }\n        } else if (d == 'L') {\n            if (b == -1) {\n                y_start = 0;\n            } else {\n                y_start = placed[b].y + placed[b].h;\n            }\n\n            x_start = 0;\n            for (const auto& rect : placed) {\n                if (rect.y < y_start + h_rect && rect.y + rect.h > y_start) {\n                    if (rect.x + rect.w > x_start) {\n                        x_start = rect.x + rect.w;\n                    }\n                }\n            }\n        }\n\n        placed.push_back({x_start, y_start, w_rect, h_rect});\n        if (x_start + w_rect > current_max_x) {\n            current_max_x = x_start + w_rect;\n        }\n        if (y_start + h_rect > current_max_y) {\n            current_max_y = y_start + h_rect;\n        }\n    }\n    return {current_max_x, current_max_y};\n}\n\nvector<RectInstruction> generate_vertical_minH(int N, const vector<double>& w_est, const vector<double>& h_est) {\n    vector<RectInstruction> res;\n    for (int i = 0; i < N; i++) {\n        double h0 = h_est[i];\n        double h1 = w_est[i];\n        int r = (h0 <= h1) ? 0 : 1;\n        char d = 'U';\n        int b = (i == 0) ? -1 : (i - 1);\n        res.push_back({i, r, d, b});\n    }\n    return res;\n}\n\nvector<RectInstruction> generate_vertical_minW(int N, const vector<double>& w_est, const vector<double>& h_est) {\n    vector<RectInstruction> res;\n    for (int i = 0; i < N; i++) {\n        double w0 = w_est[i];\n        double w1 = h_est[i];\n        int r = (w0 <= w1) ? 0 : 1;\n        char d = 'U';\n        int b = (i == 0) ? -1 : (i - 1);\n        res.push_back({i, r, d, b});\n    }\n    return res;\n}\n\nvector<RectInstruction> generate_horizontal_minW(int N, const vector<double>& w_est, const vector<double>& h_est) {\n    vector<RectInstruction> res;\n    for (int i = 0; i < N; i++) {\n        double w0 = w_est[i];\n        double w1 = h_est[i];\n        int r = (w0 <= w1) ? 0 : 1;\n        char d = 'L';\n        int b = (i == 0) ? -1 : (i - 1);\n        res.push_back({i, r, d, b});\n    }\n    return res;\n}\n\nvector<RectInstruction> generate_horizontal_minH(int N, const vector<double>& w_est, const vector<double>& h_est) {\n    vector<RectInstruction> res;\n    for (int i = 0; i < N; i++) {\n        double h0 = h_est[i];\n        double h1 = w_est[i];\n        int r = (h0 <= h1) ? 0 : 1;\n        char d = 'L';\n        int b = (i == 0) ? -1 : (i - 1);\n        res.push_back({i, r, d, b});\n    }\n    return res;\n}\n\nvector<RectInstruction> generate_random(int N, mt19937& rng) {\n    vector<RectInstruction> res;\n    for (int i = 0; i < N; i++) {\n        int r = uniform_int_distribution<int>(0, 1)(rng);\n        char d = uniform_int_distribution<int>(0, 1)(rng) ? 'U' : 'L';\n        int b = uniform_int_distribution<int>(-1, i - 1)(rng);\n        res.push_back({i, r, d, b});\n    }\n    return res;\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    for (int i = 0; i < N; i++) {\n        long long wp, hp;\n        cin >> wp >> hp;\n        w_est[i] = static_cast<double>(wp);\n        h_est[i] = static_cast<double>(hp);\n    }\n\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n\n    for (int turn = 0; turn < T; turn++) {\n        vector<vector<RectInstruction>> candidates;\n        candidates.push_back(generate_vertical_minH(N, w_est, h_est));\n        candidates.push_back(generate_vertical_minW(N, w_est, h_est));\n        candidates.push_back(generate_horizontal_minW(N, w_est, h_est));\n        candidates.push_back(generate_horizontal_minH(N, w_est, h_est));\n        candidates.push_back(generate_random(N, rng));\n\n        long long best_score = LLONG_MAX;\n        vector<RectInstruction> best_instructions;\n\n        for (auto& instructions : candidates) {\n            auto [W_est, H_est] = simulate_placement(instructions, w_est, h_est);\n            long long score_est = W_est + H_est;\n            if (score_est < best_score) {\n                best_score = score_est;\n                best_instructions = instructions;\n            }\n        }\n\n        cout << N << \"\\n\";\n        for (const auto& inst : best_instructions) {\n            cout << get<0>(inst) << \" \" << get<1>(inst) << \" \" << get<2>(inst) << \" \" << get<3>(inst) << \"\\n\";\n        }\n        cout.flush();\n\n        long long W_obs, H_obs;\n        cin >> W_obs >> H_obs;\n\n        auto [W_est_sim, H_est_sim] = simulate_placement(best_instructions, w_est, h_est);\n        double total_est = static_cast<double>(W_est_sim) + static_cast<double>(H_est_sim);\n        double total_obs = static_cast<double>(W_obs) + static_cast<double>(H_obs);\n        double scale = 1.0;\n        if (total_est > 1e-9) {\n            scale = total_obs / total_est;\n        }\n\n        for (int i = 0; i < N; i++) {\n            w_est[i] *= scale;\n            h_est[i] *= scale;\n            if (w_est[i] < 1) w_est[i] = 1;\n            if (w_est[i] > 1e9) w_est[i] = 1e9;\n            if (h_est[i] < 1) h_est[i] = 1;\n            if (h_est[i] > 1e9) h_est[i] = 1e9;\n        }\n    }\n\n    return 0;\n}","ahc041":"#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, H_input;\n    cin >> N >> M >> H_input;\n    const int H = H_input;\n\n    vector<ll> A(N);\n    for (int i = 0; i < N; i++) {\n        cin >> A[i];\n    }\n\n    vector<vector<int>> graph(N);\n    for (int i = 0; i < M; i++) {\n        int u, v;\n        cin >> u >> v;\n        graph[u].push_back(v);\n        graph[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<vector<pair<int, int>>> neighbors(N);\n    for (int v = 0; v < N; v++) {\n        vector<int> dist(N, -1);\n        queue<int> q;\n        dist[v] = 0;\n        q.push(v);\n        while (!q.empty()) {\n            int u = q.front(); q.pop();\n            if (dist[u] >= H) continue;\n            for (int w : graph[u]) {\n                if (dist[w] == -1) {\n                    dist[w] = dist[u] + 1;\n                    q.push(w);\n                }\n            }\n        }\n        for (int u = 0; u < N; u++) {\n            if (dist[u] != -1) {\n                neighbors[v].push_back({u, dist[u]});\n            }\n        }\n    }\n\n    vector<int> d(N, H+1);\n    vector<bool> inR(N, false);\n    vector<ll> best_marginal(N, 0);\n    priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<>> pq;\n\n    for (int v = 0; v < N; v++) {\n        ll marginal = 0;\n        for (auto [u, k] : neighbors[v]) {\n            marginal += (ll)(H + 1 - k) * A[u];\n        }\n        best_marginal[v] = marginal;\n        pq.push({marginal, v});\n    }\n\n    vector<int> R;\n    while (true) {\n        bool all_covered = true;\n        for (int u = 0; u < N; u++) {\n            if (d[u] > H) {\n                all_covered = false;\n                break;\n            }\n        }\n        if (all_covered) break;\n\n        ll candidate_marginal = -1;\n        int candidate_v = -1;\n        while (!pq.empty()) {\n            auto [marginal, v] = pq.top();\n            pq.pop();\n            if (inR[v] || marginal > best_marginal[v]) {\n                continue;\n            }\n            candidate_marginal = marginal;\n            candidate_v = v;\n            break;\n        }\n        if (candidate_v == -1) {\n            break;\n        }\n\n        inR[candidate_v] = true;\n        R.push_back(candidate_v);\n\n        for (auto [u, k] : neighbors[candidate_v]) {\n            if (k < d[u]) {\n                d[u] = k;\n            }\n        }\n\n        for (int w = 0; w < N; w++) {\n            if (inR[w]) continue;\n            ll new_marginal = 0;\n            for (auto [u, k] : neighbors[w]) {\n                if (d[u] > k) {\n                    new_marginal += (ll)(d[u] - k) * A[u];\n                }\n            }\n            if (new_marginal < best_marginal[w]) {\n                best_marginal[w] = new_marginal;\n                pq.push({new_marginal, w});\n            }\n        }\n    }\n\n    vector<int> parent(N, -2);\n    for (int r : R) {\n        parent[r] = -1;\n    }\n\n    vector<vector<int>> depth_nodes(H + 1);\n    for (int u = 0; u < N; u++) {\n        if (d[u] <= H) {\n            depth_nodes[d[u]].push_back(u);\n        }\n    }\n\n    for (int k = 1; k <= H; k++) {\n        for (int u : depth_nodes[k]) {\n            for (int v : graph[u]) {\n                if (d[v] == k - 1) {\n                    parent[u] = v;\n                    break;\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 <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int N;\n    cin >> N;\n    vector<string> grid(N);\n    for (int i = 0; i < N; i++) {\n        cin >> grid[i];\n    }\n\n    vector<pair<char, int>> ops;\n\n    while (true) {\n        int best_i = -1, best_j = -1;\n        char best_dir = ' ';\n        int best_p = -1;\n        int min_shifts = 100000000;\n\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                if (grid[i][j] != 'x') continue;\n\n                bool safe = true;\n                for (int k = 0; k < i; k++) {\n                    if (grid[k][j] == 'o') {\n                        safe = false;\n                        break;\n                    }\n                }\n                if (safe) {\n                    int shifts = i + 1;\n                    if (shifts < min_shifts) {\n                        min_shifts = shifts;\n                        best_dir = 'U';\n                        best_p = j;\n                        best_i = i;\n                        best_j = j;\n                    }\n                }\n\n                safe = true;\n                for (int k = i + 1; k < N; k++) {\n                    if (grid[k][j] == 'o') {\n                        safe = false;\n                        break;\n                    }\n                }\n                if (safe) {\n                    int shifts = N - i;\n                    if (shifts < min_shifts) {\n                        min_shifts = shifts;\n                        best_dir = 'D';\n                        best_p = j;\n                        best_i = i;\n                        best_j = j;\n                    }\n                }\n\n                safe = true;\n                for (int k = 0; k < j; k++) {\n                    if (grid[i][k] == 'o') {\n                        safe = false;\n                        break;\n                    }\n                }\n                if (safe) {\n                    int shifts = j + 1;\n                    if (shifts < min_shifts) {\n                        min_shifts = shifts;\n                        best_dir = 'L';\n                        best_p = i;\n                        best_i = i;\n                        best_j = j;\n                    }\n                }\n\n                safe = true;\n                for (int k = j + 1; k < N; k++) {\n                    if (grid[i][k] == 'o') {\n                        safe = false;\n                        break;\n                    }\n                }\n                if (safe) {\n                    int shifts = N - j;\n                    if (shifts < min_shifts) {\n                        min_shifts = shifts;\n                        best_dir = 'R';\n                        best_p = i;\n                        best_i = i;\n                        best_j = j;\n                    }\n                }\n            }\n        }\n\n        if (best_i == -1) {\n            break;\n        }\n\n        for (int s = 0; s < min_shifts; s++) {\n            ops.push_back({best_dir, best_p});\n            if (best_dir == 'U') {\n                for (int i = 0; i < N - 1; i++) {\n                    grid[i][best_p] = grid[i + 1][best_p];\n                }\n                grid[N - 1][best_p] = '.';\n            } else if (best_dir == 'D') {\n                for (int i = N - 1; i > 0; i--) {\n                    grid[i][best_p] = grid[i - 1][best_p];\n                }\n                grid[0][best_p] = '.';\n            } else if (best_dir == 'L') {\n                for (int j = 0; j < N - 1; j++) {\n                    grid[best_p][j] = grid[best_p][j + 1];\n                }\n                grid[best_p][N - 1] = '.';\n            } else if (best_dir == 'R') {\n                for (int j = N - 1; j > 0; j--) {\n                    grid[best_p][j] = grid[best_p][j - 1];\n                }\n                grid[best_p][0] = '.';\n            }\n        }\n    }\n\n    for (auto &op : ops) {\n        cout << op.first << \" \" << op.second << \"\\n\";\n    }\n\n    return 0;\n}","ahc044":"#include <iostream>\n#include <vector>\n#include <cstdlib>\n#include <cmath>\n#include <algorithm>\nusing namespace std;\n\nlong simulate(const vector<int>& a, const vector<int>& b, const vector<long>& T, int L, int N) {\n    vector<long> count(N, 0);\n    count[0] = 1;\n    int current = 0;\n    for (int week = 1; week < L; week++) {\n        long t = count[current];\n        int next;\n        if (t & 1) {\n            next = a[current];\n        } else {\n            next = b[current];\n        }\n        count[next]++;\n        current = next;\n    }\n    long error = 0;\n    for (int j = 0; j < N; j++) {\n        error += abs(count[j] - T[j]);\n    }\n    return error;\n}\n\nint main() {\n    const int N = 100;\n    int L;\n    cin >> N >> L;\n    vector<long> T(N);\n    for (int i = 0; i < N; i++) {\n        cin >> T[i];\n    }\n\n    vector<long> target(N);\n    for (int j = 0; j < N; j++) {\n        target[j] = 2 * T[j];\n    }\n\n    vector<long> S(N, 0);\n    vector<long> rem = target;\n    vector<int> a(N), b(N);\n\n    for (int i = 0; i < N; i++) {\n        int j1 = 0;\n        for (int j = 1; j < N; j++) {\n            if (rem[j] > rem[j1]) {\n                j1 = j;\n            }\n        }\n        int j2 = 0;\n        for (int j = 1; j < N; j++) {\n            if (rem[j] > rem[j2] && j != j1) {\n                j2 = j;\n            }\n        }\n        if (rem[j1] == rem[j2]) {\n            if (j2 == 0 && j1 != 0) {\n                j2 = j1;\n            } else if (j1 == j2) {\n                j2 = (j1 + 1) % N;\n            }\n        }\n        if (rem[j2] < rem[j1]) {\n            j2 = j1;\n        }\n        a[i] = j1;\n        b[i] = j2;\n        S[j1] += T[i];\n        S[j2] += T[i];\n        rem[j1] = target[j1] - S[j1];\n        rem[j2] = target[j2] - S[j2];\n    }\n\n    long best_error = simulate(a, b, T, L, N);\n    vector<int> best_a = a;\n    vector<int> best_b = b;\n\n    for (int i = 0; i < N; i++) {\n        swap(a[i], b[i]);\n        long error = simulate(a, b, T, L, N);\n        if (error < best_error) {\n            best_error = error;\n            best_a = a;\n            best_b = b;\n        } else {\n            swap(a[i], b[i]);\n        }\n    }\n\n    for (int i = 0; i < N; i++) {\n        cout << best_a[i] << \" \" << best_b[i] << '\\n';\n    }\n\n    return 0;\n}","ahc045":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, Q, L, W;\n    cin >> N >> M >> Q >> L >> W;\n\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    for (int i = 0; i < N; i++) {\n        cin >> lx[i] >> rx[i] >> ly[i] >> ry[i];\n    }\n\n    vector<int> cx2(N), cy2(N);\n    for (int i = 0; i < N; i++) {\n        cx2[i] = lx[i] + rx[i];\n        cy2[i] = ly[i] + ry[i];\n    }\n\n    vector<int> cities(N);\n    iota(cities.begin(), cities.end(), 0);\n    sort(cities.begin(), cities.end(), [&](int i, int j) {\n        if (cx2[i] != cx2[j]) \n            return cx2[i] < cx2[j];\n        if (cy2[i] != cy2[j])\n            return cy2[i] < cy2[j];\n        return i < j;\n    });\n\n    vector<vector<int>> groups;\n    int start = 0;\n    for (int i = 0; i < M; i++) {\n        int g = G[i];\n        groups.push_back(vector<int>(cities.begin() + start, cities.begin() + start + g));\n        start += g;\n    }\n\n    vector<vector<pair<int, int>>> group_edges(M);\n\n    for (int k = 0; k < M; k++) {\n        int g = groups[k].size();\n        for (int i = 0; i < g; ) {\n            if (i + 2 < g) {\n                cout << \"? 3\";\n                cout << \" \" << groups[k][i];\n                cout << \" \" << groups[k][i+1];\n                cout << \" \" << groups[k][i+2] << endl;\n                cout.flush();\n\n                vector<pair<int, int>> edges;\n                for (int j = 0; j < 2; j++) {\n                    int u, v;\n                    cin >> u >> v;\n                    edges.push_back({u, v});\n                }\n                for (auto &e : edges) {\n                    group_edges[k].push_back(e);\n                }\n                i += 2;\n            } else {\n                if (i + 1 < g) {\n                    group_edges[k].push_back({groups[k][i], groups[k][i+1]});\n                }\n                break;\n            }\n        }\n    }\n\n    cout << \"!\" << endl;\n    for (int k = 0; k < M; k++) {\n        for (int i = 0; i < groups[k].size(); i++) {\n            if (i > 0) cout << \" \";\n            cout << groups[k][i];\n        }\n        cout << endl;\n\n        for (auto &e : group_edges[k]) {\n            cout << e.first << \" \" << e.second << endl;\n        }\n    }\n    cout.flush();\n\n    return 0;\n}","ahc046":"#include <iostream>\n#include <vector>\n#include <cmath>\nusing namespace std;\n\nint main() {\n    int N, M;\n    cin >> N >> M;\n    vector<pair<int, int>> points(M);\n    for (int i = 0; i < M; i++) {\n        cin >> points[i].first >> points[i].second;\n    }\n\n    for (int i = 1; i < M; i++) {\n        int ci = points[i-1].first;\n        int cj = points[i-1].second;\n        int ti = points[i].first;\n        int tj = points[i].second;\n\n        if (ci == ti) {\n            if (cj < tj) {\n                if (tj == N-1) {\n                    cout << \"S R\\n\";\n                } else {\n                    for (int j = 0; j < tj - cj; j++) {\n                        cout << \"M R\\n\";\n                    }\n                }\n            } else {\n                if (tj == 0) {\n                    cout << \"S L\\n\";\n                } else {\n                    for (int j = 0; j < cj - tj; j++) {\n                        cout << \"M L\\n\";\n                    }\n                }\n            }\n        } else if (cj == tj) {\n            if (ci < ti) {\n                if (ti == N-1) {\n                    cout << \"S D\\n\";\n                } else {\n                    for (int k = 0; k < ti - ci; k++) {\n                        cout << \"M D\\n\";\n                    }\n                }\n            } else {\n                if (ti == 0) {\n                    cout << \"S U\\n\";\n                } else {\n                    for (int k = 0; k < ci - ti; k++) {\n                        cout << \"M U\\n\";\n                    }\n                }\n            }\n        } else {\n            int dr = ti - ci;\n            int dc = tj - cj;\n            if (abs(dr) >= abs(dc)) {\n                char vdir = (dr > 0) ? 'D' : 'U';\n                for (int k = 0; k < abs(dr); k++) {\n                    cout << \"M \" << vdir << '\\n';\n                }\n                if (dc > 0) {\n                    if (tj == N-1) {\n                        cout << \"S R\\n\";\n                    } else {\n                        for (int k = 0; k < dc; k++) {\n                            cout << \"M R\\n\";\n                        }\n                    }\n                } else {\n                    if (tj == 0) {\n                        cout << \"S L\\n\";\n                    } else {\n                        for (int k = 0; k < abs(dc); k++) {\n                            cout << \"M L\\n\";\n                        }\n                    }\n                }\n            } else {\n                char hdir = (dc > 0) ? 'R' : 'L';\n                for (int k = 0; k < abs(dc); k++) {\n                    cout << \"M \" << hdir << '\\n';\n                }\n                if (dr > 0) {\n                    if (ti == N-1) {\n                        cout << \"S D\\n\";\n                    } else {\n                        for (int k = 0; k < dr; k++) {\n                            cout << \"M D\\n\";\n                        }\n                    }\n                } else {\n                    if (ti == 0) {\n                        cout << \"S U\\n\";\n                    } else {\n                        for (int k = 0; k < abs(dr); k++) {\n                            cout << \"M U\\n\";\n                        }\n                    }\n                }\n            }\n        }\n    }\n    return 0;\n}"},"2":{"ahc001":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int n;\n    cin >> n;\n    vector<tuple<int, int, int, int>> companies;\n    for (int i = 0; i < n; i++) {\n        int x, y, r;\n        cin >> x >> y >> r;\n        companies.emplace_back(x, y, r, i);\n    }\n\n    vector<vector<pair<int, int>>> all_candidates(n);\n    for (int i = 0; i < n; i++) {\n        auto [x, y, r_val, idx] = companies[i];\n        vector<pair<int, int>> cands;\n        int sw = max(1, (int)sqrt(r_val) - 20);\n        int ew = min(10000, (int)sqrt(r_val) + 20);\n        for (int w = sw; w <= ew; w++) {\n            int h1 = r_val / w;\n            int h2 = (r_val + w - 1) / w;\n            vector<int> hs = {h1 - 2, h1 - 1, h1, h1 + 1, h1 + 2, h2 - 2, h2 - 1, h2, h2 + 1, h2 + 2};\n            for (int h : hs) {\n                if (h < 1) h = 1;\n                if (h > 10000) h = 10000;\n                int area = w * h;\n                cands.emplace_back(w, h);\n                if (w != h) {\n                    cands.emplace_back(h, w);\n                }\n            }\n        }\n        cands.emplace_back(1, 1);\n        sort(cands.begin(), cands.end(), [&](const auto& a, const auto& b) {\n            int err_a = abs(a.first * a.second - r_val);\n            int err_b = abs(b.first * b.second - r_val);\n            if (err_a != err_b) {\n                return err_a < err_b;\n            }\n            return a.first * a.second < b.first * b.second;\n        });\n        auto last = unique(cands.begin(), cands.end(), [](const auto& a, const auto& b) {\n            return a.first == b.first && a.second == b.second;\n        });\n        cands.erase(last, cands.end());\n        if (cands.size() > 40) {\n            cands.resize(40);\n        }\n        all_candidates[i] = cands;\n    }\n\n    sort(companies.begin(), companies.end(), [](const auto& a, const auto& b) {\n        return get<2>(a) > get<2>(b);\n    });\n\n    vector<array<int, 4>> result(n);\n    vector<array<int, 4>> placed;\n\n    vector<int> offsets = {-3, -2, -1, 0, 1, 2, 3};\n\n    for (auto& comp : companies) {\n        int x = get<0>(comp);\n        int y = get<1>(comp);\n        int r_val = get<2>(comp);\n        int idx = get<3>(comp);\n        bool found = false;\n        for (auto [w, h] : all_candidates[idx]) {\n            int Lx = max(0, x + 1 - w);\n            int Rx = min(x, 10000 - w);\n            int Ly = max(0, y + 1 - h);\n            int Ry = min(y, 10000 - h);\n            if (Lx > Rx || Ly > Ry) {\n                continue;\n            }\n\n            double center_x = x + 0.5;\n            double center_y = y + 0.5;\n            double ideal_a = center_x - w / 2.0;\n            double ideal_b = center_y - h / 2.0;\n\n            for (int dx : offsets) {\n                int a_candidate = static_cast<int>(round(ideal_a)) + dx;\n                if (a_candidate < Lx) a_candidate = Lx;\n                if (a_candidate > Rx) a_candidate = Rx;\n                for (int dy : offsets) {\n                    int b_candidate = static_cast<int>(round(ideal_b)) + dy;\n                    if (b_candidate < Ly) b_candidate = Ly;\n                    if (b_candidate > Ry) b_candidate = Ry;\n\n                    bool valid = true;\n                    for (const auto& rect : placed) {\n                        if (max(a_candidate, rect[0]) < min(a_candidate + w, rect[2]) &&\n                            max(b_candidate, rect[1]) < min(b_candidate + h, rect[3])) {\n                            valid = false;\n                            break;\n                        }\n                    }\n                    if (valid) {\n                        result[idx] = {a_candidate, b_candidate, a_candidate + w, b_candidate + h};\n                        placed.push_back({a_candidate, b_candidate, a_candidate + w, b_candidate + h});\n                        found = true;\n                        break;\n                    }\n                }\n                if (found) break;\n            }\n            if (found) break;\n        }\n        if (!found) {\n            result[idx] = {x, y, x + 1, y + 1};\n            placed.push_back({x, y, x + 1, y + 1});\n        }\n    }\n\n    for (int i = 0; i < n; i++) {\n        auto [a, b, c, d] = result[i];\n        cout << a << \" \" << b << \" \" << c << \" \" << d << '\\n';\n    }\n\n    return 0;\n}","ahc002":"#include <bits/stdc++.h>\n#include <boost/dynamic_bitset.hpp>\nusing namespace std;\n\nstruct State {\n    int i, j;\n    boost::dynamic_bitset<> visited;\n    long long score;\n    string path;\n    State(int i, int j, int M) : i(i), j(j), visited(M), score(0), path(\"\") {}\n};\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\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\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    const vector<tuple<int, int, char>> dirs = {\n        {-1, 0, 'U'},\n        {1, 0, 'D'},\n        {0, -1, 'L'},\n        {0, 1, 'R'}\n    };\n\n    const int BEAM_WIDTH = 200;\n    vector<State> current_beam;\n    State start_state(si, sj, M);\n    start_state.visited.set(t[si][sj]);\n    start_state.score = p[si][sj];\n    current_beam.push_back(start_state);\n\n    long long best_score = start_state.score;\n    string best_path = \"\";\n\n    while (!current_beam.empty()) {\n        vector<State> next_beam;\n        next_beam.reserve(current_beam.size() * 4);\n\n        for (State& state : current_beam) {\n            for (const auto& [dx, dy, dchar] : dirs) {\n                int ni = state.i + dx;\n                int nj = state.j + dy;\n                if (ni < 0 || ni >= 50 || nj < 0 || nj >= 50) continue;\n\n                int tile_id = t[ni][nj];\n                if (state.visited.test(tile_id)) continue;\n\n                State new_state(ni, nj, M);\n                new_state.visited = state.visited;\n                new_state.visited.set(tile_id);\n                new_state.score = state.score + p[ni][nj];\n                new_state.path = state.path;\n                new_state.path += dchar;\n\n                if (new_state.score > best_score) {\n                    best_score = new_state.score;\n                    best_path = new_state.path;\n                }\n\n                next_beam.push_back(new_state);\n            }\n        }\n\n        if (next_beam.empty()) break;\n\n        sort(next_beam.begin(), next_beam.end(), [](const State& a, const State& b) {\n            return a.score > b.score;\n        });\n\n        if (next_beam.size() > BEAM_WIDTH) {\n            current_beam.assign(next_beam.begin(), next_beam.begin() + BEAM_WIDTH);\n        } else {\n            current_beam = move(next_beam);\n        }\n    }\n\n    cout << best_path << endl;\n    return 0;\n}","ahc003":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    const int N = 30;\n    double W_h[N][N-1];\n    double W_v[N-1][N];\n\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N-1; j++) {\n            W_h[i][j] = 5000.0;\n        }\n    }\n    for (int i = 0; i < N-1; i++) {\n        for (int j = 0; j < N; j++) {\n            W_v[i][j] = 5000.0;\n        }\n    }\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, 1e18));\n        vector<vector<int>> parent_i(N, vector<int>(N, -1));\n        vector<vector<int>> parent_j(N, vector<int>(N, -1));\n\n        priority_queue<tuple<double, int, int>, vector<tuple<double, int, int>>, greater<tuple<double, int, int>>> pq;\n        dist[si][sj] = 0.0;\n        pq.push(make_tuple(0.0, si, sj));\n\n        const int di[4] = {-1, 1, 0, 0};\n        const int dj[4] = {0, 0, -1, 1};\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 + di[dir];\n                int nj = j + dj[dir];\n                if (ni < 0 || ni >= N || nj < 0 || nj >= N) continue;\n\n                double weight;\n                if (dir == 0) {\n                    weight = W_v[i-1][j];\n                } else if (dir == 1) {\n                    weight = W_v[i][j];\n                } else if (dir == 2) {\n                    weight = W_h[i][j-1];\n                } else {\n                    weight = W_h[i][j];\n                }\n\n                double nd = d + weight;\n                if (nd < dist[ni][nj]) {\n                    dist[ni][nj] = nd;\n                    parent_i[ni][nj] = i;\n                    parent_j[ni][nj] = j;\n                    pq.push(make_tuple(nd, ni, nj));\n                }\n            }\n        }\n\n        string path_str = \"\";\n        vector<tuple<int, int, int>> edges_list;\n        int i = ti, j = tj;\n        while (i != si || j != sj) {\n            int pi = parent_i[i][j];\n            int pj = parent_j[i][j];\n            if (pi == i-1) {\n                path_str += 'D';\n            } else if (pi == i+1) {\n                path_str += 'U';\n            } else if (pj == j-1) {\n                path_str += 'R';\n            } else if (pj == j+1) {\n                path_str += 'L';\n            }\n\n            if (pi == i-1) {\n                edges_list.push_back(make_tuple(0, i-1, j));\n            } else if (pi == i+1) {\n                edges_list.push_back(make_tuple(0, i, j));\n            } else if (pj == j-1) {\n                edges_list.push_back(make_tuple(1, i, j-1));\n            } else if (pj == j+1) {\n                edges_list.push_back(make_tuple(1, i, j));\n            }\n\n            i = pi;\n            j = pj;\n        }\n        reverse(path_str.begin(), path_str.end());\n\n        cout << path_str << \"\\n\";\n        cout.flush();\n\n        long long r;\n        cin >> r;\n\n        int L = path_str.size();\n        double est_length = dist[ti][tj];\n        double error = static_cast<double>(r) - est_length;\n\n        double lr = 0.1 / (k + 1);\n        for (auto &edge : edges_list) {\n            int type = get<0>(edge);\n            int idx1 = get<1>(edge);\n            int idx2 = get<2>(edge);\n            double step = lr * error / static_cast<double>(L);\n\n            if (type == 0) {\n                W_v[idx1][idx2] += step;\n                if (W_v[idx1][idx2] < 1000.0) W_v[idx1][idx2] = 1000.0;\n                else if (W_v[idx1][idx2] > 9000.0) W_v[idx1][idx2] = 9000.0;\n            } else {\n                W_h[idx1][idx2] += step;\n                if (W_h[idx1][idx2] < 1000.0) W_h[idx1][idx2] = 1000.0;\n                else if (W_h[idx1][idx2] > 9000.0) W_h[idx1][idx2] = 9000.0;\n            }\n        }\n    }\n\n    return 0;\n}","ahc004":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int N, M;\n    cin >> N >> M;\n    vector<string> strs(M);\n    for (int i = 0; i < M; i++) {\n        cin >> strs[i];\n    }\n\n    vector<int> indices(M);\n    iota(indices.begin(), indices.end(), 0);\n    sort(indices.begin(), indices.end(), [&](int i, int j) {\n        return strs[i].size() > strs[j].size();\n    });\n\n    vector<string> mat(N, string(N, '.'));\n    vector<bool> covered(M, false);\n    int covered_count = 0;\n\n    for (int idx : indices) {\n        string& s = strs[idx];\n        int L = s.size();\n        int best_dots = -1;\n        int best_dir = -1;\n        int best_i = -1;\n        int best_j = -1;\n\n        for (int i = 0; i < N; i++) {\n            for (int sc = 0; sc < N; sc++) {\n                bool ok = true;\n                int dots = 0;\n                for (int p = 0; p < L; p++) {\n                    int col = (sc + p) % N;\n                    if (mat[i][col] != '.' && mat[i][col] != s[p]) {\n                        ok = false;\n                        break;\n                    }\n                    if (mat[i][col] == '.') {\n                        dots++;\n                    }\n                }\n                if (ok && dots > best_dots) {\n                    best_dots = dots;\n                    best_dir = 0;\n                    best_i = i;\n                    best_j = sc;\n                }\n            }\n        }\n\n        for (int j = 0; j < N; j++) {\n            for (int sr = 0; sr < N; sr++) {\n                bool ok = true;\n                int dots = 0;\n                for (int p = 0; p < L; p++) {\n                    int row = (sr + p) % N;\n                    if (mat[row][j] != '.' && mat[row][j] != s[p]) {\n                        ok = false;\n                        break;\n                    }\n                    if (mat[row][j] == '.') {\n                        dots++;\n                    }\n                }\n                if (ok && dots > best_dots) {\n                    best_dots = dots;\n                    best_dir = 1;\n                    best_i = sr;\n                    best_j = j;\n                }\n            }\n        }\n\n        if (best_dots != -1) {\n            covered[idx] = true;\n            covered_count++;\n            if (best_dir == 0) {\n                for (int p = 0; p < L; p++) {\n                    int col = (best_j + p) % N;\n                    mat[best_i][col] = s[p];\n                }\n            } else {\n                for (int p = 0; p < L; p++) {\n                    int row = (best_i + p) % N;\n                    mat[row][best_j] = s[p];\n                }\n            }\n        }\n    }\n\n    bool improved;\n    do {\n        improved = false;\n        for (int idx = 0; idx < M && !improved; idx++) {\n            if (covered[idx]) continue;\n            string& s = strs[idx];\n            int L = s.size();\n\n            for (int i = 0; i < N && !improved; i++) {\n                for (int sc = 0; sc < N && !improved; sc++) {\n                    bool ok = true;\n                    for (int p = 0; p < L; p++) {\n                        int col = (sc + p) % N;\n                        if (mat[i][col] != '.' && mat[i][col] != s[p]) {\n                            ok = false;\n                            break;\n                        }\n                    }\n                    if (ok) {\n                        for (int p = 0; p < L; p++) {\n                            int col = (sc + p) % N;\n                            mat[i][col] = s[p];\n                        }\n                        covered[idx] = true;\n                        covered_count++;\n                        improved = true;\n                    }\n                }\n            }\n\n            if (improved) continue;\n\n            for (int j = 0; j < N && !improved; j++) {\n                for (int sr = 0; sr < N && !improved; sr++) {\n                    bool ok = true;\n                    for (int p = 0; p < L; p++) {\n                        int row = (sr + p) % N;\n                        if (mat[row][j] != '.' && mat[row][j] != s[p]) {\n                            ok = false;\n                            break;\n                        }\n                    }\n                    if (ok) {\n                        for (int p = 0; p < L; p++) {\n                            int row = (sr + p) % N;\n                            mat[row][j] = s[p];\n                        }\n                        covered[idx] = true;\n                        covered_count++;\n                        improved = true;\n                    }\n                }\n            }\n        }\n    } while (improved);\n\n    if (covered_count == M) {\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    }\n\n    for (int i = 0; i < N; i++) {\n        cout << mat[i] << '\\n';\n    }\n    return 0;\n}","ahc005":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MAX_R = 5000;\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, si, sj;\n    cin >> N >> si >> sj;\n    vector<string> grid(N);\n    for (int i = 0; i < N; i++) {\n        cin >> grid[i];\n    }\n\n    vector<vector<bool>> is_road(N, vector<bool>(N, false));\n    vector<vector<int>> time_grid(N, vector<int>(N, 0));\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (grid[i][j] != '#') {\n                is_road[i][j] = true;\n                time_grid[i][j] = grid[i][j] - '0';\n            }\n        }\n    }\n\n    vector<pair<int, int>> road_cells;\n    map<pair<int, int>, int> cell_to_index;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (is_road[i][j]) {\n                cell_to_index[{i, j}] = road_cells.size();\n                road_cells.push_back({i, j});\n            }\n        }\n    }\n    int R = road_cells.size();\n    if (R == 0) {\n        cout << \"\" << endl;\n        return 0;\n    }\n\n    vector<vector<bitset<MAX_R>>> visible_set(N, vector<bitset<MAX_R>>(N));\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (!is_road[i][j]) continue;\n            int idx_center = cell_to_index[{i, j}];\n            visible_set[i][j].set(idx_center);\n\n            for (int k = j-1; k >= 0; k--) {\n                if (grid[i][k] == '#') break;\n                int idx = cell_to_index[{i, k}];\n                visible_set[i][j].set(idx);\n            }\n            for (int k = j+1; k < N; k++) {\n                if (grid[i][k] == '#') break;\n                int idx = cell_to_index[{i, k}];\n                visible_set[i][j].set(idx);\n            }\n            for (int k = i-1; k >= 0; k--) {\n                if (grid[k][j] == '#') break;\n                int idx = cell_to_index[{k, j}];\n                visible_set[i][j].set(idx);\n            }\n            for (int k = i+1; k < N; k++) {\n                if (grid[k][j] == '#') break;\n                int idx = cell_to_index[{k, j}];\n                visible_set[i][j].set(idx);\n            }\n        }\n    }\n\n    bitset<MAX_R> base_coverage;\n    int cur_i = si, cur_j = sj;\n    if (is_road[cur_i][cur_j]) {\n        int start_idx = cell_to_index[{cur_i, cur_j}];\n        base_coverage.set(start_idx);\n        base_coverage |= visible_set[cur_i][cur_j];\n    }\n\n    string path_so_far = \"\";\n    int total_time_so_far = 0;\n\n    const int D = 4;\n    while (base_coverage.count() < R) {\n        auto cmp = [&](const tuple<int, int, int, bitset<MAX_R>, int, int>& a, \n                       const tuple<int, int, int, bitset<MAX_R>, int, int>& b) {\n            auto [i1, j1, time1, cov1, pi1, pj1] = a;\n            auto [i2, j2, time2, cov2, pi2, pj2] = b;\n            int inc1 = cov1.count() - (base_coverage & cov1).count();\n            int inc2 = cov2.count() - (base_coverage & cov2).count();\n            if (inc1 != inc2) {\n                return inc1 < inc2;\n            }\n            return time1 > time2;\n        };\n        priority_queue<tuple<int, int, int, bitset<MAX_R>, int, int>, \n                       vector<tuple<int, int, int, bitset<MAX_R>, int, int>>, \n                       decltype(cmp)> pq(cmp);\n\n        bitset<MAX_R> start_cov;\n        pq.emplace(cur_i, cur_j, 0, start_cov, -1, -1);\n\n        tuple<int, int, int, bitset<MAX_R>, int, int> best_found = pq.top();\n        int best_incremental = -1;\n\n        while (!pq.empty()) {\n            auto [i, j, time_so_far, cov, pi, pj] = pq.top();\n            pq.pop();\n            int inc = cov.count() - (base_coverage & cov).count();\n            if (inc > best_incremental || (inc == best_incremental && time_so_far < get<2>(best_found))) {\n                best_incremental = inc;\n                best_found = make_tuple(i, j, time_so_far, cov, pi, pj);\n            }\n            if (get<0>(best_found) == i && get<1>(best_found) == j && \n                get<2>(best_found) == time_so_far && \n                get<3>(best_found) == cov) {\n                if (best_incremental == 0) {\n                    break;\n                }\n            }\n\n            if (pi == -2) {\n                continue;\n            }\n            if (pi != -1) {\n            } else {\n                pi = -2;\n            }\n\n            for (int d = 0; d < 4; d++) {\n                int ni = i, nj = j;\n                if (d == 0) ni--;\n                else if (d == 1) ni++;\n                else if (d == 2) nj--;\n                else if (d == 3) nj++;\n\n                if (ni < 0 || ni >= N || nj < 0 || nj >= N) continue;\n                if (!is_road[ni][nj]) continue;\n\n                int new_time = time_so_far + time_grid[ni][nj];\n                bitset<MAX_R> new_cov = cov;\n                new_cov |= visible_set[ni][nj];\n\n                pq.emplace(ni, nj, new_time, new_cov, i, j);\n            }\n        }\n\n        if (best_incremental <= 0) {\n            break;\n        }\n\n        auto [i_best, j_best, time_best, cov_best, pi_best, pj_best] = best_found;\n        vector<pair<int, int>> path_to_best;\n        int cur_i_temp = i_best, cur_j_temp = j_best;\n        int prev_i = pi_best, prev_j = pj_best;\n        while (prev_i != -1 || prev_j != -1) {\n            path_to_best.push_back({cur_i_temp, cur_j_temp});\n            if (prev_i == -1 && prev_j == -1) break;\n            int next_i = prev_i;\n            int next_j = prev_j;\n            cur_i_temp = prev_i;\n            cur_j_temp = prev_j;\n            prev_i = (path_to_best.size() == 1) ? -1 : get<4>(pq.top()); \n            prev_j = (path_to_best.size() == 1) ? -1 : get<5>(pq.top());\n            pq.pop();\n        }\n        reverse(path_to_best.begin(), path_to_best.end());\n\n        if (path_to_best.empty()) {\n            break;\n        }\n\n        int next_i = path_to_best[0].first;\n        int next_j = path_to_best[0].second;\n\n        char dir;\n        if (next_i == cur_i - 1) dir = 'U';\n        else if (next_i == cur_i + 1) dir = 'D';\n        else if (next_j == cur_j - 1) dir = 'L';\n        else if (next_j == cur_j + 1) dir = 'R';\n        else {\n            dir = 'R';\n        }\n\n        path_so_far += dir;\n        total_time_so_far += time_grid[next_i][next_j];\n        base_coverage |= visible_set[next_i][next_j];\n        cur_i = next_i;\n        cur_j = next_j;\n\n        if (base_coverage.count() == R) {\n            break;\n        }\n    }\n\n    vector<vector<int>> dist(N, vector<int>(N, INT_MAX));\n    vector<vector<pair<int, int>>> parent(N, vector<pair<int, int>>(N, {-1, -1}));\n    priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<>> pq_dijk;\n    dist[cur_i][cur_j] = 0;\n    pq_dijk.emplace(0, cur_i, cur_j);\n\n    while (!pq_dijk.empty()) {\n        auto [d, i, j] = pq_dijk.top();\n        pq_dijk.pop();\n        if (d != dist[i][j]) continue;\n        if (i == si && j == sj) break;\n\n        int dx[] = {-1, 1, 0, 0};\n        int dy[] = {0, 0, -1, 1};\n        for (int k = 0; k < 4; k++) {\n            int ni = i + dx[k];\n            int nj = j + dy[k];\n            if (ni < 0 || ni >= N || nj < 0 || nj >= N) continue;\n            if (!is_road[ni][nj]) continue;\n            int nd = d + time_grid[ni][nj];\n            if (nd < dist[ni][nj]) {\n                dist[ni][nj] = nd;\n                parent[ni][nj] = {i, j};\n                pq_dijk.emplace(nd, ni, nj);\n            }\n        }\n    }\n\n    string return_path = \"\";\n    int i = si, j = sj;\n    while (i != cur_i || j != cur_j) {\n        auto [pi, pj] = parent[i][j];\n        if (pi == i-1) return_path += 'D';\n        else if (pi == i+1) return_path += 'U';\n        else if (pj == j-1) return_path += 'R';\n        else if (pj == j+1) return_path += 'L';\n        i = pi;\n        j = pj;\n    }\n    reverse(return_path.begin(), return_path.end());\n\n    string full_path = path_so_far + return_path;\n    cout << full_path << endl;\n\n    return 0;\n}","future-contest-2022-qual":"#include <bits/stdc++.h>\nusing namespace std;\n\nvoid update_skill(vector<int>& s, const vector<int>& d_task, int t_actual, int K) {\n    int low_bound, high_bound;\n    if (t_actual == 1) {\n        low_bound = 0;\n        high_bound = 3;\n    } else {\n        low_bound = max(1, t_actual - 3);\n        high_bound = t_actual + 3;\n    }\n\n    int w_est = 0;\n    for (int k = 0; k < K; k++) {\n        if (d_task[k] > s[k]) {\n            w_est += (d_task[k] - s[k]);\n        }\n    }\n\n    if (w_est < low_bound) {\n        int needed = low_bound - w_est;\n        while (needed > 0) {\n            int best_k = -1;\n            int best_gain = 0;\n            for (int k = 0; k < K; k++) {\n                int def_old = max(0, d_task[k] - s[k]);\n                int def_new = max(0, d_task[k] - (s[k] - 1));\n                int gain = def_new - def_old;\n                if (gain > best_gain) {\n                    best_gain = gain;\n                    best_k = k;\n                }\n            }\n            if (best_k == -1) break;\n            s[best_k]--;\n            needed--;\n        }\n    } else if (w_est > high_bound) {\n        int needed = w_est - high_bound;\n        while (needed > 0) {\n            int best_k = -1;\n            int best_gain = 0;\n            for (int k = 0; k < K; k++) {\n                int def_old = max(0, d_task[k] - s[k]);\n                int def_new = max(0, d_task[k] - (s[k] + 1));\n                int gain = def_old - def_new;\n                if (gain > best_gain) {\n                    best_gain = gain;\n                    best_k = k;\n                }\n            }\n            if (best_k == -1) break;\n            s[best_k]++;\n            needed--;\n        }\n    }\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>> d(N, vector<int>(K));\n    for (int i = 0; i < N; i++) {\n        for (int k = 0; k < K; k++) {\n            cin >> d[i][k];\n        }\n    }\n\n    vector<vector<int>> children(N);\n    vector<vector<int>> parents(N);\n    for (int i = 0; i < R; i++) {\n        int u, v;\n        cin >> u >> v;\n        u--; v--;\n        children[u].push_back(v);\n        parents[v].push_back(u);\n    }\n\n    vector<int> size_vec(N, 1);\n    for (int i = N-1; i >= 0; i--) {\n        for (int child : children[i]) {\n            size_vec[i] += size_vec[child];\n        }\n    }\n\n    vector<int> in_degree_count(N, 0);\n    for (int i = 0; i < N; i++) {\n        in_degree_count[i] = parents[i].size();\n    }\n\n    vector<int> busy_until(M, 1);\n    vector<int> start_day(M, -1);\n    vector<int> current_task(M, -1);\n    vector<vector<int>> skill_est(M, vector<int>(K, 0));\n    vector<bool> completed(N, false);\n    vector<int> ready_vec;\n\n    for (int i = 0; i < N; i++) {\n        if (in_degree_count[i] == 0) {\n            ready_vec.push_back(i);\n        }\n    }\n\n    for (int day = 1; day <= 2000; day++) {\n        vector<int> free_members;\n        for (int j = 0; j < M; j++) {\n            if (busy_until[j] <= day) {\n                free_members.push_back(j);\n            }\n        }\n\n        sort(ready_vec.begin(), ready_vec.end(), [&](int a, int b) {\n            return size_vec[a] > size_vec[b];\n        });\n\n        vector<pair<int, int>> assignments;\n        vector<int> current_free = free_members;\n        vector<bool> free_flag(M, false);\n        for (int j : free_members) {\n            free_flag[j] = true;\n        }\n\n        int idx = 0;\n        while (idx < (int)ready_vec.size() && !current_free.empty()) {\n            int task = ready_vec[idx];\n            int best_member = -1;\n            int best_w = INT_MAX;\n\n            for (int j : current_free) {\n                int w = 0;\n                for (int k = 0; k < K; k++) {\n                    if (d[task][k] > skill_est[j][k]) {\n                        w += (d[task][k] - skill_est[j][k]);\n                    }\n                }\n                if (w < best_w) {\n                    best_w = w;\n                    best_member = j;\n                }\n            }\n\n            if (best_member == -1) {\n                idx++;\n                continue;\n            }\n\n            assignments.push_back({best_member, task});\n            auto it = find(current_free.begin(), current_free.end(), best_member);\n            if (it != current_free.end()) {\n                *it = current_free.back();\n                current_free.pop_back();\n            }\n            free_flag[best_member] = false;\n\n            start_day[best_member] = day;\n            current_task[best_member] = task;\n            int t_est = (best_w == 0) ? 1 : best_w;\n            busy_until[best_member] = day + t_est;\n\n            swap(ready_vec[idx], ready_vec.back());\n            ready_vec.pop_back();\n        }\n\n        cout << assignments.size();\n        for (auto &p : assignments) {\n            cout << \" \" << p.first + 1 << \" \" << p.second + 1;\n        }\n        cout << endl;\n        cout.flush();\n\n        int n_completed;\n        cin >> n_completed;\n        if (n_completed == -1) {\n            break;\n        }\n\n        vector<int> completed_members(n_completed);\n        for (int i = 0; i < n_completed; i++) {\n            cin >> completed_members[i];\n            completed_members[i]--;\n        }\n\n        for (int j : completed_members) {\n            if (current_task[j] == -1) {\n                continue;\n            }\n            int i_task = current_task[j];\n            int t_actual = day - start_day[j] + 1;\n            update_skill(skill_est[j], d[i_task], t_actual, K);\n\n            completed[i_task] = true;\n            for (int child : children[i_task]) {\n                in_degree_count[child]--;\n                if (in_degree_count[child] == 0) {\n                    ready_vec.push_back(child);\n                }\n            }\n\n            busy_until[j] = day + 1;\n            start_day[j] = -1;\n            current_task[j] = -1;\n        }\n    }\n\n    return 0;\n}","ahc006":"#include <bits/stdc++.h>\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 dist(const Point& a, const Point& b) {\n    return abs(a.x - b.x) + abs(a.y - b.y);\n}\n\nint main() {\n    vector<tuple<int, int, int, int>> orders;\n    for (int i = 0; i < 1000; i++) {\n        int a, b, c, d;\n        cin >> a >> b >> c >> d;\n        orders.emplace_back(a, b, c, d);\n    }\n\n    vector<Point> route = { Point(400, 400), Point(400, 400) };\n    vector<bool> selected(1000, false);\n    vector<int> pos_r(1000, -1);\n    vector<int> pos_d(1000, -1);\n\n    for (int iter = 0; iter < 50; iter++) {\n        long long best_delta = 1e18;\n        int best_order = -1;\n        int best_i = -1;\n        int best_j = -1;\n        int L_route = route.size();\n\n        for (int idx = 0; idx < 1000; idx++) {\n            if (selected[idx]) continue;\n            auto [a, b, c, d] = orders[idx];\n            long long min_delta = 1e18;\n            int min_i = -1;\n            int min_j = -1;\n\n            for (int i = 1; i < L_route; i++) {\n                int dr;\n                if (i == 1) {\n                    dr = dist(route[0], Point(a, b)) + dist(Point(a, b), route[1]) - dist(route[0], route[1]);\n                } else {\n                    dr = dist(route[i-1], Point(a, b)) + dist(Point(a, b), route[i]) - dist(route[i-1], route[i]);\n                }\n\n                for (int j = i+1; j < L_route; j++) {\n                    Point prev, next;\n                    if (j-1 < i) {\n                        prev = route[j-1];\n                    } else if (j-1 == i) {\n                        prev = Point(a, b);\n                    } else {\n                        prev = route[j-2];\n                    }\n\n                    if (j < i) {\n                        next = route[j];\n                    } else if (j == i) {\n                        next = Point(a, b);\n                    } else {\n                        next = route[j-1];\n                    }\n\n                    int dd = dist(prev, Point(c, d)) + dist(Point(c, d), next) - dist(prev, next);\n                    long long total_delta = (long long)dr + dd;\n                    if (total_delta < min_delta) {\n                        min_delta = total_delta;\n                        min_i = i;\n                        min_j = j;\n                    }\n                }\n            }\n\n            if (min_delta < best_delta) {\n                best_delta = min_delta;\n                best_order = idx;\n                best_i = min_i;\n                best_j = min_j;\n            }\n        }\n\n        if (best_order == -1) {\n            for (int idx = 0; idx < 1000; idx++) {\n                if (!selected[idx]) {\n                    best_order = idx;\n                    best_i = 1;\n                    best_j = 2;\n                    break;\n                }\n            }\n        }\n\n        auto [a, b, c, d] = orders[best_order];\n        selected[best_order] = true;\n        route.insert(route.begin() + best_i, Point(a, b));\n        route.insert(route.begin() + best_j, Point(c, d));\n        pos_r[best_order] = best_i;\n        pos_d[best_order] = best_j;\n        L_route = route.size();\n    }\n\n    auto compute_dist = [&](const vector<Point>& r) {\n        int d = 0;\n        for (int i = 0; i < (int)r.size() - 1; i++) {\n            d += dist(r[i], r[i+1]);\n        }\n        return d;\n    };\n    int current_dist = compute_dist(route);\n\n    bool improved;\n    int passes = 0;\n    do {\n        improved = false;\n        int n = route.size();\n        for (int i = 0; i < n-1; i++) {\n            for (int j = i+2; j < n-1; j++) {\n                vector<int> new_pos_r = pos_r;\n                vector<int> new_pos_d = pos_d;\n                bool valid = true;\n\n                for (int idx = 0; idx < 1000; idx++) {\n                    if (!selected[idx]) continue;\n                    if (new_pos_r[idx] >= i+1 && new_pos_r[idx] <= j) {\n                        new_pos_r[idx] = j - (new_pos_r[idx] - (i+1));\n                    }\n                    if (new_pos_d[idx] >= i+1 && new_pos_d[idx] <= j) {\n                        new_pos_d[idx] = j - (new_pos_d[idx] - (i+1));\n                    }\n                    if (new_pos_r[idx] >= new_pos_d[idx]) {\n                        valid = false;\n                        break;\n                    }\n                }\n                if (!valid) continue;\n\n                int old1 = dist(route[i], route[i+1]);\n                int old2 = dist(route[j], route[j+1]);\n                int new1 = dist(route[i], route[j]);\n                int new2 = dist(route[i+1], route[j+1]);\n                int change = (new1 + new2) - (old1 + old2);\n\n                if (change < 0) {\n                    reverse(route.begin() + i+1, route.begin() + j+1);\n                    pos_r = new_pos_r;\n                    pos_d = new_pos_d;\n                    current_dist += change;\n                    improved = true;\n                    i = n;\n                    break;\n                }\n            }\n            if (improved) break;\n        }\n        passes++;\n    } while (improved && passes < 1000);\n\n    vector<int> selected_indices;\n    for (int i = 0; i < 1000; i++) {\n        if (selected[i]) {\n            selected_indices.push_back(i+1);\n        }\n    }\n    sort(selected_indices.begin(), selected_indices.end());\n\n    cout << 50;\n    for (int idx : selected_indices) {\n        cout << \" \" << idx;\n    }\n    cout << \"\\n\";\n\n    int n_route = route.size();\n    cout << n_route;\n    for (int i = 0; i < n_route; i++) {\n        cout << \" \" << route[i].x << \" \" << route[i].y;\n    }\n    cout << \"\\n\";\n\n    return 0;\n}","ahc007":"#include <iostream>\n#include <vector>\n#include <cmath>\n#include <algorithm>\n#include <functional>\nusing namespace std;\n\nstruct Edge {\n    int u, v;\n};\n\nint main() {\n    const int N = 400;\n    const int M = 1995;\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<Edge> edges(M);\n    vector<int> d(M, 0);\n\n    for (int i = 0; i < M; i++) {\n        cin >> edges[i].u >> edges[i].v;\n    }\n\n    for (int i = 0; i < M; i++) {\n        int u = edges[i].u;\n        int v = edges[i].v;\n        long long dx = (long long)x[u] - x[v];\n        long long dy = (long long)y[u] - y[v];\n        double dist = sqrt(dx*dx + dy*dy);\n        d[i] = (int)(dist + 0.5);\n    }\n\n    vector<int> parent(N);\n    vector<int> size_vec(N, 1);\n    vector<int> mout_comp(N, 10000000);\n\n    for (int i = 0; i < N; i++) {\n        parent[i] = i;\n    }\n\n    for (int i = 0; i < M; i++) {\n        int u = edges[i].u;\n        int v = edges[i].v;\n        if (d[i] < mout_comp[u]) mout_comp[u] = d[i];\n        if (d[i] < mout_comp[v]) mout_comp[v] = d[i];\n    }\n\n    function<int(int)> find = [&](int x) -> int {\n        if (parent[x] != x) {\n            parent[x] = find(parent[x]);\n        }\n        return parent[x];\n    };\n\n    int count = 0;\n\n    for (int i = 0; i < M; i++) {\n        int l;\n        cin >> l;\n\n        int u = edges[i].u;\n        int v = edges[i].v;\n        int ru = find(u);\n        int rv = find(v);\n\n        if (ru == rv) {\n            cout << 0 << endl;\n            cout.flush();\n            continue;\n        }\n\n        if (count == N - 1) {\n            cout << 0 << endl;\n            cout.flush();\n            continue;\n        }\n\n        int m = min(mout_comp[ru], mout_comp[rv]);\n\n        if (l <= m) {\n            cout << 1 << endl;\n            cout.flush();\n            count++;\n\n            if (size_vec[ru] < size_vec[rv]) {\n                parent[ru] = rv;\n                mout_comp[rv] = m;\n                size_vec[rv] += size_vec[ru];\n            } else {\n                parent[rv] = ru;\n                mout_comp[ru] = m;\n                size_vec[ru] += size_vec[rv];\n            }\n        } else {\n            cout << 0 << endl;\n            cout.flush();\n        }\n    }\n\n    return 0;\n}","ahc008":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int H = 30;\nconst int W = 30;\nint dx[4] = {-1, 1, 0, 0};\nint dy[4] = {0, 0, -1, 1};\nchar move_dir[4] = {'U', 'D', 'L', 'R'};\nchar wall_dir[4] = {'u', 'd', 'l', 'r'};\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N;\n    cin >> N;\n    vector<pair<int, int>> pets;\n    for (int i = 0; i < N; i++) {\n        int x, y, t;\n        cin >> x >> y >> t;\n        pets.push_back({x, y});\n    }\n\n    int M;\n    cin >> M;\n    vector<pair<int, int>> humans;\n    for (int i = 0; i < M; i++) {\n        int x, y;\n        cin >> x >> y;\n        humans.push_back({x, y});\n    }\n\n    bool grid[H+1][W+1];\n    for (int i = 1; i <= H; i++) {\n        for (int j = 1; j <= W; j++) {\n            grid[i][j] = true;\n        }\n    }\n\n    for (int turn = 0; turn < 300; turn++) {\n        string actions = \"\";\n        for (int i = 0; i < M; i++) {\n            vector<char> candidates;\n            candidates.push_back('.');\n\n            for (int d = 0; d < 4; d++) {\n                int nx = humans[i].first + dx[d];\n                int ny = humans[i].second + dy[d];\n                if (nx >= 1 && nx <= H && ny >= 1 && ny <= W && grid[nx][ny]) {\n                    candidates.push_back(move_dir[d]);\n                }\n            }\n\n            for (int d = 0; d < 4; d++) {\n                int wx = humans[i].first + dx[d];\n                int wy = humans[i].second + dy[d];\n                if (wx < 1 || wx > H || wy < 1 || wy > W) continue;\n                if (!grid[wx][wy]) continue;\n\n                bool occupied = false;\n                for (int j = 0; j < M; j++) {\n                    if (humans[j].first == wx && humans[j].second == wy) {\n                        occupied = true;\n                        break;\n                    }\n                }\n                if (occupied) continue;\n                for (int j = 0; j < N; j++) {\n                    if (pets[j].first == wx && pets[j].second == wy) {\n                        occupied = true;\n                        break;\n                    }\n                }\n                if (occupied) continue;\n\n                bool adjacent_pet = false;\n                for (int dd = 0; dd < 4; dd++) {\n                    int nx = wx + dx[dd];\n                    int ny = wy + dy[dd];\n                    if (nx < 1 || nx > H || ny < 1 || ny > W) continue;\n                    for (int j = 0; j < N; j++) {\n                        if (pets[j].first == nx && pets[j].second == ny) {\n                            adjacent_pet = true;\n                            break;\n                        }\n                    }\n                    if (adjacent_pet) break;\n                }\n                if (adjacent_pet) continue;\n\n                candidates.push_back(wall_dir[d]);\n            }\n\n            double best_score = -1e18;\n            char best_act = '.';\n            for (char act : candidates) {\n                bool temp_grid[H+1][W+1];\n                for (int x = 1; x <= H; x++) {\n                    for (int y = 1; y <= W; y++) {\n                        temp_grid[x][y] = grid[x][y];\n                    }\n                }\n                pair<int, int> start_pos = humans[i];\n\n                if (act == '.') {\n                } else if (act >= 'A' && act <= 'Z') {\n                    int d;\n                    if (act == 'U') d = 0;\n                    else if (act == 'D') d = 1;\n                    else if (act == 'L') d = 2;\n                    else d = 3;\n                    int nx = start_pos.first + dx[d];\n                    int ny = start_pos.second + dy[d];\n                    start_pos = {nx, ny};\n                } else {\n                    int d;\n                    if (act == 'u') d = 0;\n                    else if (act == 'd') d = 1;\n                    else if (act == 'l') d = 2;\n                    else d = 3;\n                    int wx = start_pos.first + dx[d];\n                    int wy = start_pos.second + dy[d];\n                    temp_grid[wx][wy] = false;\n                }\n\n                bool visited[H+1][W+1] = {};\n                int comp_size = 0;\n                int pet_count = 0;\n                queue<pair<int, int>> q;\n                q.push(start_pos);\n                visited[start_pos.first][start_pos.second] = true;\n\n                while (!q.empty()) {\n                    auto [x, y] = q.front();\n                    q.pop();\n                    comp_size++;\n                    for (int j = 0; j < N; j++) {\n                        if (pets[j].first == x && pets[j].second == y) {\n                            pet_count++;\n                            break;\n                        }\n                    }\n                    for (int d = 0; d < 4; d++) {\n                        int nx = x + dx[d];\n                        int ny = y + dy[d];\n                        if (nx >= 1 && nx <= H && ny >= 1 && ny <= W && !visited[nx][ny] && temp_grid[nx][ny]) {\n                            visited[nx][ny] = true;\n                            q.push({nx, ny});\n                        }\n                    }\n                }\n\n                double factor = pow(2.0, -pet_count);\n                double score = comp_size * factor;\n\n                if (score > best_score) {\n                    best_score = score;\n                    best_act = act;\n                }\n            }\n            actions += best_act;\n        }\n\n        cout << actions << endl;\n        cout.flush();\n\n        vector<string> pet_moves(N);\n        for (int i = 0; i < N; i++) {\n            cin >> pet_moves[i];\n        }\n\n        for (int i = 0; i < M; i++) {\n            if (actions[i] >= 'a' && actions[i] <= 'z') {\n                int d;\n                if (actions[i] == 'u') d = 0;\n                else if (actions[i] == 'd') d = 1;\n                else if (actions[i] == 'l') d = 2;\n                else d = 3;\n                int wx = humans[i].first + dx[d];\n                int wy = humans[i].second + dy[d];\n                grid[wx][wy] = false;\n            }\n        }\n\n        for (int i = 0; i < M; i++) {\n            if (actions[i] >= 'A' && actions[i] <= 'Z') {\n                int d;\n                if (actions[i] == 'U') d = 0;\n                else if (actions[i] == 'D') d = 1;\n                else if (actions[i] == 'L') d = 2;\n                else d = 3;\n                int nx = humans[i].first + dx[d];\n                int ny = humans[i].second + dy[d];\n                if (nx >= 1 && nx <= H && ny >= 1 && ny <= W && grid[nx][ny]) {\n                    humans[i] = {nx, ny};\n                }\n            }\n        }\n\n        for (int i = 0; i < N; i++) {\n            int x = pets[i].first;\n            int y = pets[i].second;\n            string& moves = pet_moves[i];\n            for (char c : moves) {\n                int nx = x, ny = y;\n                if (c == 'U') nx--;\n                else if (c == 'D') nx++;\n                else if (c == 'L') ny--;\n                else if (c == 'R') ny++;\n                if (nx >= 1 && nx <= H && ny >= 1 && ny <= W && grid[nx][ny]) {\n                    x = nx;\n                    y = ny;\n                }\n            }\n            pets[i] = {x, y};\n        }\n    }\n\n    return 0;\n}","ahc009":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int si, sj, ti, tj;\n    double p;\n    cin >> si >> sj >> ti >> tj >> 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<int>> dist(20, vector<int>(20, -1));\n    vector<vector<pair<int, int>>> prev(20, vector<pair<int, int>>(20, make_pair(-1, -1)));\n    vector<vector<char>> move_from(20, vector<char>(20, '?'));\n\n    queue<pair<int, int>> q;\n    dist[si][sj] = 0;\n    q.push({si, sj});\n\n    const vector<int> di = {-1, 1, 0, 0};\n    const vector<int> dj = {0, 0, -1, 1};\n    const vector<char> dir_char = {'U', 'D', 'L', 'R'};\n\n    while (!q.empty()) {\n        auto [i, j] = q.front();\n        q.pop();\n        if (i == ti && j == tj) 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 >= 20 || nj < 0 || nj >= 20) \n                continue;\n\n            bool can_move = true;\n            if (d == 0) {\n                if (i == 0) can_move = false;\n                else if (v[i-1][j] == '1') can_move = false;\n            } else if (d == 1) {\n                if (i == 19) can_move = false;\n                else if (v[i][j] == '1') can_move = false;\n            } else if (d == 2) {\n                if (j == 0) can_move = false;\n                else if (h[i][j-1] == '1') can_move = false;\n            } else if (d == 3) {\n                if (j == 19) can_move = false;\n                else if (h[i][j] == '1') can_move = false;\n            }\n\n            if (!can_move) continue;\n\n            if (dist[ni][nj] == -1) {\n                dist[ni][nj] = dist[i][j] + 1;\n                prev[ni][nj] = {i, j};\n                move_from[ni][nj] = dir_char[d];\n                q.push({ni, nj});\n            }\n        }\n    }\n\n    string path = \"\";\n    int i = ti, j = tj;\n    while (i != si || j != sj) {\n        char c = move_from[i][j];\n        path += c;\n        auto [pi, pj] = prev[i][j];\n        i = pi;\n        j = pj;\n    }\n    reverse(path.begin(), path.end());\n\n    if (path.empty()) {\n        cout << \"\" << endl;\n        return 0;\n    }\n\n    int d = path.size();\n    int m = 200 / d;\n    string ans;\n    ans.reserve(d * m);\n    for (char c : path) {\n        ans.append(m, c);\n    }\n    cout << ans << endl;\n\n    return 0;\n}","ahc010":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int di[4] = {0, -1, 0, 1};\nconst int dj[4] = {-1, 0, 1, 0};\nconst int to[8][4] = {\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\nvector<int> compute_cycles(const vector<string>& initial_types, const vector<vector<int>>& rotations) {\n    int grid[30][30];\n    for (int i = 0; i < 30; i++) {\n        for (int j = 0; j < 30; j++) {\n            int t = initial_types[i][j] - '0';\n            int r = rotations[i][j];\n            if (t < 4) {\n                grid[i][j] = (t + r) % 4;\n            } else {\n                grid[i][j] = t ^ (r & 1);\n            }\n        }\n    }\n\n    bool vis[30][30][4] = {};\n    vector<int> cycs;\n\n    for (int i = 0; i < 30; i++) {\n        for (int j = 0; j < 30; j++) {\n            for (int d = 0; d < 4; d++) {\n                if (vis[i][j][d]) continue;\n\n                int ci = i, cj = j, cd = d;\n                int steps = 0;\n                bool valid = true;\n\n                while (true) {\n                    int s = grid[ci][cj];\n                    int nd = to[s][cd];\n                    if (nd == -1) {\n                        valid = false;\n                        break;\n                    }\n\n                    int ni = ci + di[nd];\n                    int nj = cj + dj[nd];\n                    if (ni < 0 || ni >= 30 || nj < 0 || nj >= 30) {\n                        valid = false;\n                        break;\n                    }\n\n                    vis[ci][cj][cd] = true;\n\n                    cd = (nd + 2) % 4;\n                    ci = ni;\n                    cj = nj;\n                    steps++;\n\n                    if (ci == i && cj == j && cd == d) {\n                        cycs.push_back(steps);\n                        break;\n                    }\n\n                    if (vis[ci][cj][cd]) {\n                        valid = false;\n                        break;\n                    }\n                }\n            }\n        }\n    }\n    return cycs;\n}\n\nlong long compute_score(const vector<int>& cycles) {\n    if (cycles.size() < 2) {\n        return 0;\n    }\n    vector<int> sorted = cycles;\n    sort(sorted.begin(), sorted.end(), greater<int>());\n    return 1LL * sorted[0] * sorted[1];\n}\n\nint main() {\n    vector<string> initial_types(30);\n    for (int i = 0; i < 30; i++) {\n        cin >> initial_types[i];\n    }\n\n    vector<vector<int>> rotations(30, vector<int>(30, 0));\n    auto cycles = compute_cycles(initial_types, rotations);\n    long long current_score = compute_score(cycles);\n\n    const int passes = 2;\n    for (int iter = 0; iter < passes; iter++) {\n        for (int i = 0; i < 30; i++) {\n            for (int j = 0; j < 30; j++) {\n                int original_r = rotations[i][j];\n                long long best_score = current_score;\n                int best_r = original_r;\n\n                for (int offset = 0; offset < 2; offset++) {\n                    int r = (original_r + offset) % 4;\n                    rotations[i][j] = r;\n                    auto new_cycles = compute_cycles(initial_types, rotations);\n                    long long new_score = compute_score(new_cycles);\n                    rotations[i][j] = original_r;\n\n                    if (new_score > best_score) {\n                        best_score = new_score;\n                        best_r = r;\n                    }\n                }\n\n                if (best_r != original_r) {\n                    rotations[i][j] = best_r;\n                    current_score = best_score;\n                }\n            }\n        }\n    }\n\n    string output;\n    for (int i = 0; i < 30; i++) {\n        for (int j = 0; j < 30; j++) {\n            output += ('0' + rotations[i][j]);\n        }\n    }\n    cout << output << endl;\n    return 0;\n}","ahc011":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <random>\n#include <climits>\n#include <tuple>\n#include <cctype>\n#include <cmath>\nusing namespace std;\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, T;\n    cin >> N >> T;\n    vector<string> grid_str(N);\n    for (int i = 0; i < N; i++) {\n        cin >> grid_str[i];\n    }\n\n    vector<vector<int>> grid(N, vector<int>(N, 0));\n    vector<int> have(16, 0);\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            char c = grid_str[i][j];\n            int val;\n            if (c >= '0' && c <= '9') {\n                val = c - '0';\n            } else {\n                val = 10 + (c - 'a');\n            }\n            grid[i][j] = val;\n            if (val < 16) {\n                have[val]++;\n            }\n        }\n    }\n\n    vector<tuple<int, int, string>> candidates;\n    candidates.emplace_back(N-1, N-1, \"row_snake_even_left\");\n    candidates.emplace_back(N-1, N-1, \"row_snake_even_right\");\n    candidates.emplace_back(N-1, N-1, \"col_snake_even_top\");\n    candidates.emplace_back(N-1, N-1, \"col_snake_even_bottom\");\n    candidates.emplace_back(0, 0, \"row_snake_start_right\");\n    candidates.emplace_back(0, N-1, \"row_snake_start_left\");\n    candidates.emplace_back(N-1, 0, \"row_snake_end_left\");\n    candidates.emplace_back(N-1, N-1, \"row_snake_alt1\");\n    candidates.emplace_back(N-1, N-1, \"col_snake_alt1\");\n    candidates.emplace_back(0, 0, \"col_snake_start_bottom\");\n\n    vector<vector<int>> target_grid;\n    int target_empty_i = -1, target_empty_j = -1;\n    bool found_candidate = false;\n\n    random_device rd;\n    mt19937 rng(rd());\n\n    for (auto [e_i, e_j, pattern] : candidates) {\n        vector<pair<int, int>> path;\n        if (pattern == \"row_snake_even_left\" || pattern == \"row_snake_start_right\" || pattern == \"row_snake_end_left\") {\n            for (int i = 0; i < N; i++) {\n                if (i % 2 == 0) {\n                    for (int j = 0; j < N; j++) {\n                        if (i == e_i && j == e_j) continue;\n                        path.push_back({i, j});\n                    }\n                } else {\n                    for (int j = N-1; j >= 0; j--) {\n                        if (i == e_i && j == e_j) continue;\n                        path.push_back({i, j});\n                    }\n                }\n            }\n        } else if (pattern == \"row_snake_even_right\") {\n            for (int i = 0; i < N; i++) {\n                if (i % 2 == 0) {\n                    for (int j = N-1; j >= 0; j--) {\n                        if (i == e_i && j == e_j) continue;\n                        path.push_back({i, j});\n                    }\n                } else {\n                    for (int j = 0; j < N; j++) {\n                        if (i == e_i && j == e_j) continue;\n                        path.push_back({i, j});\n                    }\n                }\n            }\n        } else if (pattern.find(\"col_snake\") != string::npos) {\n            for (int j = 0; j < N; j++) {\n                if (j % 2 == 0) {\n                    for (int i = 0; i < N; i++) {\n                        if (i == e_i && j == e_j) continue;\n                        path.push_back({i, j});\n                    }\n                } else {\n                    for (int i = N-1; i >= 0; i--) {\n                        if (i == e_i && j == e_j) continue;\n                        path.push_back({i, j});\n                    }\n                }\n            }\n        } else if (pattern == \"row_snake_start_left\") {\n            for (int i = 0; i < N; i++) {\n                if (i % 2 == 0) {\n                    for (int j = N-2; j >= 0; j--) {\n                        if (i == e_i && j == e_j) continue;\n                        path.push_back({i, j});\n                    }\n                } else {\n                    for (int j = 0; j < N; j++) {\n                        if (i == e_i && j == e_j) continue;\n                        path.push_back({i, j});\n                    }\n                }\n            }\n        } else if (pattern == \"row_snake_alt1\") {\n            for (int i = 0; i < N; i++) {\n                if (i == 0) {\n                    for (int j = 0; j < N-1; j++) {\n                        if (0 == e_i && j == e_j) continue;\n                        path.push_back({0, j});\n                    }\n                    for (int j = 1; j < N; j++) {\n                        if (i == e_i && j == e_j) continue;\n                        path.push_back({i, j});\n                    }\n                } else {\n                    for (int j = 0; j < N; j++) {\n                        if (i == e_i && j == e_j) continue;\n                        path.push_back({i, j});\n                    }\n                }\n            }\n        } else if (pattern == \"col_snake_alt1\") {\n            for (int j = 0; j < N; j++) {\n                if (j == 0) {\n                    for (int i = 0; i < N-1; i++) {\n                        if (i == e_i && 0 == e_j) continue;\n                        path.push_back({i, 0});\n                    }\n                    for (int i = 1; i < N; i++) {\n                        if (i == e_i && j == e_j) continue;\n                        path.push_back({i, j});\n                    }\n                } else {\n                    for (int i = 0; i < N; i++) {\n                        if (i == e_i && j == e_j) continue;\n                        path.push_back({i, j});\n                    }\n                }\n            }\n        } else if (pattern == \"col_snake_start_bottom\") {\n            for (int j = 0; j < N; j++) {\n                if (j % 2 == 0) {\n                    for (int i = N-1; i >= 0; i--) {\n                        if (i == e_i && j == e_j) continue;\n                        path.push_back({i, j});\n                    }\n                } else {\n                    for (int i = 0; i < N; i++) {\n                        if (i == e_i && j == e_j) continue;\n                        path.push_back({i, j});\n                    }\n                }\n            }\n        }\n\n        vector<vector<int>> pos(N, vector<int>(N, -1));\n        for (int idx = 0; idx < path.size(); idx++) {\n            int i = path[idx].first;\n            int j = path[idx].second;\n            pos[i][j] = idx;\n        }\n\n        vector<vector<int>> req_bitmask(N, vector<int>(N, 0));\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                if (i == e_i && j == e_j) {\n                    req_bitmask[i][j] = 0;\n                } else if (pos[i][j] == -1) {\n                    continue;\n                } else {\n                    int bitmask = 0;\n                    int idx = pos[i][j];\n                    if (idx > 0) {\n                        auto [pi, pj] = path[idx-1];\n                        if (pi == i && pj == j-1) bitmask |= 1;\n                        else if (pi == i && pj == j+1) bitmask |= 4;\n                        else if (pi == i-1 && pj == j) bitmask |= 2;\n                        else if (pi == i+1 && pj == j) bitmask |= 8;\n                    }\n                    if (idx < (int)path.size()-1) {\n                        auto [ni, nj] = path[idx+1];\n                        if (ni == i && nj == j-1) bitmask |= 1;\n                        else if (ni == i && nj == j+1) bitmask |= 4;\n                        else if (ni == i-1 && nj == j) bitmask |= 2;\n                        else if (ni == i+1 && nj == j) bitmask |= 8;\n                    }\n                    req_bitmask[i][j] = bitmask;\n                }\n            }\n        }\n\n        vector<int> freq_req(16, 0);\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                int b = req_bitmask[i][j];\n                if (b >= 0 && b < 16) {\n                    freq_req[b]++;\n                }\n            }\n        }\n\n        bool match = true;\n        for (int b = 0; b < 16; b++) {\n            if (freq_req[b] != have[b]) {\n                match = false;\n                break;\n            }\n        }\n        if (match) {\n            found_candidate = true;\n            target_grid = req_bitmask;\n            target_empty_i = e_i;\n            target_empty_j = e_j;\n            break;\n        }\n    }\n\n    if (!found_candidate) {\n        target_empty_i = N-1;\n        target_empty_j = N-1;\n        for (int trial = 0; trial < 30; trial++) {\n            vector<vector<bool>> visited(N, vector<bool>(N, false));\n            visited[target_empty_i][target_empty_j] = true;\n\n            vector<pair<int, int>> V_cells;\n            for (int i = 0; i < N; i++) {\n                for (int j = 0; j < N; j++) {\n                    if (!visited[i][j]) {\n                        V_cells.push_back({i, j});\n                    }\n                }\n            }\n            if (V_cells.empty()) break;\n            shuffle(V_cells.begin(), V_cells.end(), rng);\n            int start_i = V_cells[0].first;\n            int start_j = V_cells[0].second;\n            visited[start_i][start_j] = true;\n\n            vector<tuple<pair<int, int>, pair<int, int>>> tree_edges;\n            vector<tuple<pair<int, int>, pair<int, int>>> frontier;\n\n            auto add_neighbors = [&](int i, int j) {\n                vector<pair<int, int>> neighbors = {{i-1, j}, {i+1, j}, {i, j-1}, {i, j+1}};\n                for (auto [ni, nj] : neighbors) {\n                    if (ni >= 0 && ni < N && nj >= 0 && nj < N && !visited[ni][nj]) {\n                        frontier.push_back({{i, j}, {ni, nj}});\n                    }\n                }\n            };\n            add_neighbors(start_i, start_j);\n\n            while (!frontier.empty()) {\n                int idx = uniform_int_distribution<int>(0, frontier.size()-1)(rng);\n                auto edge = frontier[idx];\n                frontier[idx] = frontier.back();\n                frontier.pop_back();\n                auto [u, v] = edge;\n                int ui = u.first, uj = u.second;\n                int vi = v.first, vj = v.second;\n                if (visited[vi][vj]) continue;\n                tree_edges.push_back(edge);\n                visited[vi][vj] = true;\n                add_neighbors(vi, vj);\n            }\n\n            vector<vector<int>> req_bitmask_rand(N, vector<int>(N, 0));\n            for (int i = 0; i < N; i++) {\n                for (int j = 0; j < N; j++) {\n                    if (i == target_empty_i && j == target_empty_j) {\n                        req_bitmask_rand[i][j] = 0;\n                        continue;\n                    }\n                    int bitmask = 0;\n                    if (i > 0 && visited[i-1][j]) bitmask |= 2;\n                    if (i < N-1 && visited[i+1][j]) bitmask |= 8;\n                    if (j > 0 && visited[i][j-1]) bitmask |= 1;\n                    if (j < N-1 && visited[i][j+1]) bitmask |= 4;\n                    req_bitmask_rand[i][j] = bitmask;\n                }\n            }\n\n            vector<int> freq_req_rand(16, 0);\n            for (int i = 0; i < N; i++) {\n                for (int j = 0; j < N; j++) {\n                    int b = req_bitmask_rand[i][j];\n                    if (b >= 0 && b < 16) {\n                        freq_req_rand[b]++;\n                    }\n                }\n            }\n\n            bool match = true;\n            for (int b = 0; b < 16; b++) {\n                if (freq_req_rand[b] != have[b]) {\n                    match = false;\n                    break;\n                }\n            }\n            if (match) {\n                found_candidate = true;\n                target_grid = req_bitmask_rand;\n                break;\n            }\n        }\n    }\n\n    int empty_i = -1, empty_j = -1;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (grid[i][j] == 0) {\n                empty_i = i;\n                empty_j = j;\n            }\n        }\n    }\n\n    string moves = \"\";\n    vector<pair<int, int>> dirs = {{-1,0}, {1,0}, {0,-1}, {0,1}};\n    char dir_chars[4] = {'U','D','L','R'};\n\n    for (int step = 0; step < T; step++) {\n        bool solved = true;\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                int expected;\n                if (i == target_empty_i && j == target_empty_j) {\n                    expected = 0;\n                } else {\n                    expected = target_grid[i][j];\n                }\n                int actual = grid[i][j];\n                if (actual != expected) {\n                    solved = false;\n                }\n            }\n        }\n        if (solved) {\n            break;\n        }\n\n        int best_cost = INT_MAX;\n        char best_move = '?';\n        for (int d = 0; d < 4; d++) {\n            int ni = empty_i + dirs[d].first;\n            int nj = empty_j + dirs[d].second;\n            if (ni < 0 || ni >= N || nj < 0 || nj >= N) {\n                continue;\n            }\n\n            int cost = 0;\n            for (int i = 0; i < N; i++) {\n                for (int j = 0; j < N; j++) {\n                    int actual_val;\n                    if (i == ni && j == nj) {\n                        actual_val = 0;\n                    } else if (i == empty_i && j == empty_j) {\n                        actual_val = grid[ni][nj];\n                    } else {\n                        actual_val = grid[i][j];\n                    }\n\n                    int expected_val;\n                    if (i == target_empty_i && j == target_empty_j) {\n                        expected_val = 0;\n                    } else {\n                        expected_val = target_grid[i][j];\n                    }\n\n                    if (actual_val != expected_val) {\n                        cost++;\n                    }\n                }\n            }\n            int manhattan = abs(ni - target_empty_i) + abs(nj - target_empty_j);\n            cost += 2 * manhattan;\n\n            if (cost < best_cost) {\n                best_cost = cost;\n                best_move = dir_chars[d];\n            }\n        }\n\n        if (best_move == '?') {\n            break;\n        }\n        moves += best_move;\n\n        int ni = empty_i + (best_move == 'U' ? -1 : (best_move == 'D' ? 1 : 0));\n        int nj = empty_j + (best_move == 'L' ? -1 : (best_move == 'R' ? 1 : 0));\n        swap(grid[empty_i][empty_j], grid[ni][nj]);\n        empty_i = ni;\n        empty_j = nj;\n    }\n\n    cout << moves << endl;\n\n    return 0;\n}","ahc012":"#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\n\nstruct Point {\n    ll x, y;\n    Point(ll x=0, ll y=0) : x(x), y(y) {}\n};\n\nstruct Line {\n    Point p1, p2;\n    Line(Point p1=Point(), Point p2=Point()) : p1(p1), p2(p2) {}\n};\n\nll side(Point p1, Point p2, Point p) {\n    return (p2.x - p1.x) * (p.y - p1.y) - (p2.y - p1.y) * (p.x - p1.x);\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(0);\n\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> straws(N);\n    for (int i = 0; i < N; i++) {\n        cin >> straws[i].x >> straws[i].y;\n    }\n\n    vector<vector<ll>> global_ts(4);\n    vector<pair<int, int>> dirs = {{1,0}, {0,1}, {1,1}, {1,-1}};\n\n    for (int dir_idx = 0; dir_idx < 4; dir_idx++) {\n        ll dx = dirs[dir_idx].first;\n        ll dy = dirs[dir_idx].second;\n        vector<ll> t_vec;\n        for (int i = 0; i < N; i++) {\n            t_vec.push_back(dx * straws[i].x + dy * straws[i].y);\n        }\n        sort(t_vec.begin(), t_vec.end());\n        global_ts[dir_idx] = t_vec;\n    }\n\n    struct Region {\n        vector<int> strawberries;\n    };\n    vector<Region> regions;\n    Region all;\n    for (int i = 0; i < N; i++) {\n        all.strawberries.push_back(i);\n    }\n    regions.push_back(all);\n\n    vector<int> b(11, 0);\n    if (N <= 10) {\n        b[N] = 1;\n    }\n\n    vector<Line> cuts;\n\n    for (int step = 0; step < K; step++) {\n        int best_delta = -1;\n        Line best_line;\n        Region* best_region = nullptr;\n        vector<int> best_new_b;\n\n        for (int i = 0; i < regions.size(); i++) {\n            Region& R = regions[i];\n            int c = R.strawberries.size();\n            if (c <= 1) continue;\n\n            for (int dir_idx = 0; dir_idx < 4; dir_idx++) {\n                ll dx = dirs[dir_idx].first;\n                ll dy = dirs[dir_idx].second;\n                vector<ll> t_region;\n                for (int idx : R.strawberries) {\n                    t_region.push_back(dx * straws[idx].x + dy * straws[idx].y);\n                }\n                sort(t_region.begin(), t_region.end());\n                int sz = t_region.size();\n                double median_val;\n                if (sz % 2 == 1) {\n                    median_val = t_region[sz/2];\n                } else {\n                    median_val = (t_region[sz/2 - 1] + t_region[sz/2]) / 2.0;\n                }\n\n                for (int offset = -2; offset <= 2; offset++) {\n                    ll c_val = (ll)(median_val) + offset;\n\n                    if (binary_search(global_ts[dir_idx].begin(), global_ts[dir_idx].end(), c_val)) {\n                        continue;\n                    }\n\n                    Point p1, p2;\n                    if (dir_idx == 0) {\n                        p1 = Point(c_val, 0);\n                        p2 = Point(c_val, 1);\n                    } else if (dir_idx == 1) {\n                        p1 = Point(0, c_val);\n                        p2 = Point(1, c_val);\n                    } else if (dir_idx == 2) {\n                        p1 = Point(c_val, 0);\n                        p2 = Point(0, c_val);\n                    } else {\n                        p1 = Point(c_val, 0);\n                        p2 = Point(0, -c_val);\n                    }\n                    Line cand(p1, p2);\n\n                    int c1 = 0;\n                    for (int idx : R.strawberries) {\n                        if (side(cand.p1, cand.p2, straws[idx]) > 0) {\n                            c1++;\n                        }\n                    }\n                    int c2 = c - c1;\n\n                    vector<int> new_b = b;\n                    if (c <= 10) {\n                        new_b[c]--;\n                    }\n                    if (c1 <= 10) {\n                        new_b[c1]++;\n                    }\n                    if (c2 <= 10) {\n                        new_b[c2]++;\n                    }\n\n                    int old_score = 0;\n                    for (int d = 1; d <= 10; d++) {\n                        old_score += min(a[d], b[d]);\n                    }\n                    int new_score = 0;\n                    for (int d = 1; d <= 10; d++) {\n                        new_score += min(a[d], new_b[d]);\n                    }\n                    int delta = new_score - old_score;\n\n                    if (delta > best_delta) {\n                        best_delta = delta;\n                        best_line = cand;\n                        best_region = &R;\n                        best_new_b = new_b;\n                    }\n                }\n            }\n        }\n\n        if (best_delta <= 0) {\n            break;\n        }\n\n        vector<int> left_idx, right_idx;\n        for (int idx : best_region->strawberries) {\n            if (side(best_line.p1, best_line.p2, straws[idx]) > 0) {\n                left_idx.push_back(idx);\n            } else {\n                right_idx.push_back(idx);\n            }\n        }\n\n        Region left_region, right_region;\n        left_region.strawberries = left_idx;\n        right_region.strawberries = right_idx;\n\n        vector<Region> new_regions;\n        for (int i = 0; i < regions.size(); i++) {\n            if (&regions[i] != best_region) {\n                new_regions.push_back(regions[i]);\n            }\n        }\n        new_regions.push_back(left_region);\n        new_regions.push_back(right_region);\n        regions = new_regions;\n\n        b = best_new_b;\n        cuts.push_back(best_line);\n    }\n\n    cout << cuts.size() << \"\\n\";\n    for (Line& line : cuts) {\n        cout << line.p1.x << \" \" << line.p1.y << \" \"\n             << line.p2.x << \" \" << line.p2.y << \"\\n\";\n    }\n\n    return 0;\n}","ahc014":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Operation {\n    int x1, y1, x2, y2, x3, y3, x4, y4;\n};\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M;\n    cin >> N >> M;\n    int c = (N - 1) / 2;\n\n    vector<vector<bool>> grid(N, vector<bool>(N, false));\n    vector<pair<int, int>> dots;\n    for (int i = 0; i < M; i++) {\n        int x, y;\n        cin >> x >> y;\n        grid[x][y] = true;\n        dots.emplace_back(x, y);\n    }\n\n    vector<tuple<long long, int, int>> candidate_list;\n    for (int x = 0; x < N; x++) {\n        for (int y = 0; y < N; y++) {\n            long long dx = x - c;\n            long long dy = y - c;\n            long long weight = dx * dx + dy * dy + 1;\n            candidate_list.emplace_back(-weight, x, y);\n        }\n    }\n    sort(candidate_list.begin(), candidate_list.end());\n\n    vector<vector<bool>> hor_used(N-1, vector<bool>(N, false));\n    vector<vector<bool>> ver_used(N, vector<bool>(N-1, false));\n    vector<vector<bool>> diag1_used(N-1, vector<bool>(N-1, false));\n    vector<vector<bool>> diag2_used(N-1, vector<bool>(N-1, false));\n\n    vector<Operation> operations;\n\n    bool added_something;\n    do {\n        added_something = false;\n        for (auto [neg_weight, x, y] : candidate_list) {\n            if (grid[x][y]) continue;\n\n            bool found = false;\n\n            auto check_axis = [&](int x1, int y1, int x2, int y2) -> bool {\n                if (x1 == x2 || y1 == y2) return false;\n                int x_min = min(x1, x2);\n                int x_max = max(x1, x2);\n                int y_min = min(y1, y2);\n                int y_max = max(y1, y2);\n\n                bool cond2 = true;\n                for (int yy = y_min + 1; yy < y_max; yy++) {\n                    if (grid[x_min][yy] || grid[x_max][yy]) {\n                        cond2 = false;\n                        break;\n                    }\n                }\n                if (!cond2) return false;\n                for (int xx = x_min + 1; xx < x_max; xx++) {\n                    if (grid[xx][y_min] || grid[xx][y_max]) {\n                        cond2 = false;\n                        break;\n                    }\n                }\n                if (!cond2) return false;\n\n                bool cond3 = true;\n                for (int yy = y_min; yy < y_max; yy++) {\n                    if (ver_used[x_min][yy] || ver_used[x_max][yy]) {\n                        cond3 = false;\n                        break;\n                    }\n                }\n                if (!cond3) return false;\n                for (int xx = x_min; xx < x_max; xx++) {\n                    if (hor_used[xx][y_min] || hor_used[xx][y_max]) {\n                        cond3 = false;\n                        break;\n                    }\n                }\n                return cond3;\n            };\n\n            auto mark_axis = [&](int x1, int y1, int x2, int y2) {\n                int x_min = min(x1, x2);\n                int x_max = max(x1, x2);\n                int y_min = min(y1, y2);\n                int y_max = max(y1, y2);\n                for (int yy = y_min; yy < y_max; yy++) {\n                    ver_used[x_min][yy] = true;\n                    ver_used[x_max][yy] = true;\n                }\n                for (int xx = x_min; xx < x_max; xx++) {\n                    hor_used[xx][y_min] = true;\n                    hor_used[xx][y_max] = true;\n                }\n            };\n\n            auto check_diag = [&](int x, int y) -> bool {\n                vector<pair<int, int>> d1_points;\n                vector<pair<int, int>> d2_points;\n                for (int dx = -N; dx <= N; dx++) {\n                    int nx = x + dx;\n                    int ny = y + dx;\n                    if (nx >= 0 && nx < N && ny >= 0 && ny < N && grid[nx][ny]) {\n                        d1_points.emplace_back(nx, ny);\n                    }\n                    nx = x + dx;\n                    ny = y - dx;\n                    if (nx >= 0 && nx < N && ny >= 0 && ny < N && grid[nx][ny]) {\n                        d2_points.emplace_back(nx, ny);\n                    }\n                }\n\n                for (auto& a : d1_points) {\n                    for (auto& b : d2_points) {\n                        int cx = a.first + b.first - x;\n                        int cy = a.second + b.second - y;\n                        if (cx < 0 || cx >= N || cy < 0 || cy >= N) continue;\n                        if (!grid[cx][cy]) continue;\n\n                        vector<pair<int, int>> corners = {{x,y}, a, {cx,cy}, b};\n                        bool cond2 = true;\n                        for (int i = 0; i < 4; i++) {\n                            int x1 = corners[i].first, y1 = corners[i].second;\n                            int x2 = corners[(i+1)%4].first, y2 = corners[(i+1)%4].second;\n                            int dx = x2 - x1, dy = y2 - y1;\n                            int steps = max(abs(dx), abs(dy));\n                            for (int step = 1; step < steps; step++) {\n                                int px = x1 + (dx * step) / steps;\n                                int py = y1 + (dy * step) / steps;\n                                bool is_corner = false;\n                                for (auto& corner : corners) {\n                                    if (px == corner.first && py == corner.second) {\n                                        is_corner = true;\n                                        break;\n                                    }\n                                }\n                                if (!is_corner && grid[px][py]) {\n                                    cond2 = false;\n                                    break;\n                                }\n                            }\n                            if (!cond2) break;\n                        }\n                        if (!cond2) continue;\n\n                        bool cond3 = true;\n                        for (int i = 0; i < 4; i++) {\n                            int x1 = corners[i].first, y1 = corners[i].second;\n                            int x2 = corners[(i+1)%4].first, y2 = corners[(i+1)%4].second;\n                            int dx = x2 - x1, dy = y2 - y1;\n                            int steps = max(abs(dx), abs(dy));\n                            for (int step = 1; step <= steps; step++) {\n                                int px = x1 + (dx * step) / steps;\n                                int py = y1 + (dy * step) / steps;\n                                int ex, ey;\n                                if (dx == 0) {\n                                    ex = px;\n                                    ey = min(y1, y2) + (step <= abs(dy) ? step-1 : abs(dy)-1);\n                                } else if (dy == 0) {\n                                    ex = min(x1, x2) + (step <= abs(dx) ? step-1 : abs(dx)-1);\n                                    ey = py;\n                                } else if (abs(dx) == abs(dy)) {\n                                    if (dx * dy > 0) {\n                                        if (dx > 0) {\n                                            ex = px;\n                                            ey = py;\n                                        } else {\n                                            ex = px + 1;\n                                            ey = py + 1;\n                                        }\n                                    } else {\n                                        if (dx > 0) {\n                                            ex = px;\n                                            ey = py + 1;\n                                        } else {\n                                            ex = px + 1;\n                                            ey = py;\n                                        }\n                                    }\n                                } else {\n                                    continue;\n                                }\n                                if (ex >= N-1 || ey >= N) continue;\n                                if (dx == 0 || dy == 0) {\n                                    if (dx == 0) {\n                                        if (ver_used[px][min(y1,y2) + step - 1]) {\n                                            cond3 = false;\n                                            break;\n                                        }\n                                    } else {\n                                        if (hor_used[min(x1,x2) + step - 1][py]) {\n                                            cond3 = false;\n                                            break;\n                                        }\n                                    }\n                                } else {\n                                    int type = (dx * dy > 0) ? 1 : 2;\n                                    int sx = px, sy = py;\n                                    if (type == 1) {\n                                        if (diag1_used[sx][sy]) {\n                                            cond3 = false;\n                                            break;\n                                        }\n                                    } else {\n                                        if (sx >= 0 && sy >= 0 && sx < N-1 && sy < N-1 && diag2_used[sx][sy]) {\n                                            cond3 = false;\n                                            break;\n                                        }\n                                    }\n                                }\n                            }\n                            if (!cond3) break;\n                        }\n                        if (cond2 && cond3) {\n                            grid[x][y] = true;\n                            dots.emplace_back(x, y);\n                            operations.push_back({x, y, a.first, a.second, cx, cy, b.first, b.second});\n                            for (int i = 0; i < 4; i++) {\n                                int x1 = corners[i].first, y1 = corners[i].second;\n                                int x2 = corners[(i+1)%4].first, y2 = corners[(i+1)%4].second;\n                                int dx = x2 - x1, dy = y2 - y1;\n                                int steps = max(abs(dx), abs(dy));\n                                for (int step = 1; step <= steps; step++) {\n                                    int px = x1 + (dx * step) / steps;\n                                    int py = y1 + (dy * step) / steps;\n                                    if (dx == 0) {\n                                        ver_used[x1][min(y1,y2) + step - 1] = true;\n                                    } else if (dy == 0) {\n                                        hor_used[min(x1,x2) + step - 1][y1] = true;\n                                    } else if (abs(dx) == abs(dy)) {\n                                        if (dx * dy > 0) {\n                                            if (dx > 0) diag1_used[px][py] = true;\n                                            else diag1_used[px][py] = true;\n                                        } else {\n                                            if (dx > 0) diag2_used[px][py] = true;\n                                            else diag2_used[px][py] = true;\n                                        }\n                                    }\n                                }\n                            }\n                            return true;\n                        }\n                    }\n                }\n                return false;\n            };\n\n            if (x > 0 && x < N-1 && y > 0 && y < N-1) {\n                if (check_diag(x, y)) {\n                    found = true;\n                }\n            }\n\n            if (!found) {\n                vector<int> up_dots, down_dots, left_dots, right_dots;\n                for (int yy = y+1; yy < N; yy++) if (grid[x][yy]) up_dots.push_back(yy);\n                for (int yy = 0; yy < y; yy++) if (grid[x][yy]) down_dots.push_back(yy);\n                for (int xx = 0; xx < x; xx++) if (grid[xx][y]) left_dots.push_back(xx);\n                for (int xx = x+1; xx < N; xx++) if (grid[xx][y]) right_dots.push_back(xx);\n\n                if (!up_dots.empty() && !right_dots.empty()) {\n                    for (int yA : up_dots) {\n                        for (int xB : right_dots) {\n                            if (check_axis(x, y, xB, yA)) {\n                                grid[x][y] = true;\n                                dots.emplace_back(x, y);\n                                operations.push_back({x, y, x, yA, xB, yA, xB, y});\n                                mark_axis(x, y, xB, yA);\n                                found = true;\n                                break;\n                            }\n                        }\n                        if (found) break;\n                    }\n                }\n                if (!found && !up_dots.empty() && !left_dots.empty()) {\n                    for (int yA : up_dots) {\n                        for (int xB : left_dots) {\n                            if (check_axis(x, y, xB, yA)) {\n                                grid[x][y] = true;\n                                dots.emplace_back(x, y);\n                                operations.push_back({x, y, x, yA, xB, yA, xB, y});\n                                mark_axis(x, y, xB, yA);\n                                found = true;\n                                break;\n                            }\n                        }\n                        if (found) break;\n                    }\n                }\n                if (!found && !down_dots.empty() && !right_dots.empty()) {\n                    for (int yA : down_dots) {\n                        for (int xB : right_dots) {\n                            if (check_axis(x, y, xB, yA)) {\n                                grid[x][y] = true;\n                                dots.emplace_back(x, y);\n                                operations.push_back({x, y, x, yA, xB, yA, xB, y});\n                                mark_axis(x, y, xB, yA);\n                                found = true;\n                                break;\n                            }\n                        }\n                        if (found) break;\n                    }\n                }\n                if (!found && !down_dots.empty() && !left_dots.empty()) {\n                    for (int yA : down_dots) {\n                        for (int xB : left_dots) {\n                            if (check_axis(x, y, xB, yA)) {\n                                grid[x][y] = true;\n                                dots.emplace_back(x, y);\n                                operations.push_back({x, y, x, yA, xB, yA, xB, y});\n                                mark_axis(x, y, xB, yA);\n                                found = true;\n                                break;\n                            }\n                        }\n                        if (found) break;\n                    }\n                }\n            }\n\n            if (found) {\n                added_something = true;\n                break;\n            }\n        }\n    } while (added_something);\n\n    cout << operations.size() << '\\n';\n    for (auto& op : operations) {\n        cout << op.x1 << ' ' << op.y1 << ' '\n             << op.x2 << ' ' << op.y2 << ' '\n             << op.x3 << ' ' << op.y3 << ' '\n             << op.x4 << ' ' << op.y4 << '\\n';\n    }\n\n    return 0;\n}","ahc015":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\nusing namespace std;\n\nint compute_sum_squares(int grid[10][10]) {\n    bool visited[10][10] = {{false}};\n    int total = 0;\n\n    for (int i = 0; i < 10; i++) {\n        for (int j = 0; j < 10; j++) {\n            if (grid[i][j] != 0 && !visited[i][j]) {\n                int size = 0;\n                queue<pair<int, int>> q;\n                q.push({i, j});\n                visited[i][j] = true;\n                int flavor = grid[i][j];\n\n                while (!q.empty()) {\n                    auto [x, y] = q.front();\n                    q.pop();\n                    size++;\n                    const int dx[4] = {-1, 1, 0, 0};\n                    const int dy[4] = {0, 0, -1, 1};\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 < 10 && ny >= 0 && ny < 10 && !visited[nx][ny] && grid[nx][ny] == flavor) {\n                            visited[nx][ny] = true;\n                            q.push({nx, ny});\n                        }\n                    }\n                }\n                total += size * size;\n            }\n        }\n    }\n    return total;\n}\n\nint main() {\n    vector<int> flavors(100);\n    for (int i = 0; i < 100; i++) {\n        cin >> flavors[i];\n    }\n\n    int grid[10][10] = {{0}};\n    vector<char> directions = {'F', 'B', 'L', 'R'};\n\n    for (int t = 0; t < 100; t++) {\n        int p;\n        cin >> p;\n\n        if (t == 99) {\n            break;\n        }\n\n        int r = (p - 1) / 10;\n        int c = (p - 1) % 10;\n        grid[r][c] = flavors[t];\n\n        char best_dir = 'F';\n        int best_score = -1;\n        int best_tilted[10][10] = {{0}};\n\n        for (char dir : directions) {\n            int tilted[10][10] = {{0}};\n\n            if (dir == 'F') {\n                for (int j = 0; j < 10; j++) {\n                    vector<int> candies;\n                    for (int i = 0; i < 10; i++) {\n                        if (grid[i][j] != 0) {\n                            candies.push_back(grid[i][j]);\n                        }\n                    }\n                    int k = candies.size();\n                    for (int i = 0; i < k; i++) {\n                        tilted[i][j] = candies[i];\n                    }\n                }\n            } else if (dir == 'B') {\n                for (int j = 0; j < 10; j++) {\n                    vector<int> candies;\n                    for (int i = 0; i < 10; i++) {\n                        if (grid[i][j] != 0) {\n                            candies.push_back(grid[i][j]);\n                        }\n                    }\n                    int k = candies.size();\n                    int start_row = 9 - k + 1;\n                    for (int i = 0; i < k; i++) {\n                        tilted[start_row + i][j] = candies[i];\n                    }\n                }\n            } else if (dir == 'L') {\n                for (int i = 0; i < 10; i++) {\n                    vector<int> candies;\n                    for (int j = 0; j < 10; j++) {\n                        if (grid[i][j] != 0) {\n                            candies.push_back(grid[i][j]);\n                        }\n                    }\n                    int k = candies.size();\n                    for (int j_idx = 0; j_idx < k; j_idx++) {\n                        tilted[i][j_idx] = candies[j_idx];\n                    }\n                }\n            } else if (dir == 'R') {\n                for (int i = 0; i < 10; i++) {\n                    vector<int> candies;\n                    for (int j = 0; j < 10; j++) {\n                        if (grid[i][j] != 0) {\n                            candies.push_back(grid[i][j]);\n                        }\n                    }\n                    int k = candies.size();\n                    int start_col = 9 - k + 1;\n                    for (int j_idx = 0; j_idx < k; j_idx++) {\n                        tilted[i][start_col + j_idx] = candies[j_idx];\n                    }\n                }\n            }\n\n            int score = compute_sum_squares(tilted);\n            if (score > best_score) {\n                best_score = score;\n                best_dir = dir;\n                for (int i = 0; i < 10; i++) {\n                    for (int j = 0; j < 10; j++) {\n                        best_tilted[i][j] = tilted[i][j];\n                    }\n                }\n            }\n        }\n\n        cout << best_dir << '\\n';\n        cout.flush();\n\n        for (int i = 0; i < 10; i++) {\n            for (int j = 0; j < 10; j++) {\n                grid[i][j] = best_tilted[i][j];\n            }\n        }\n    }\n\n    return 0;\n}","ahc016":"#include <bits/stdc++.h>\nusing namespace std;\n\n// Function to compute the index of edge (i,j) in the string, with i<j\nlong long get_index(int i, int j, int N) {\n    return 1LL * i * (2 * N - i - 1) / 2 + (j - i - 1);\n}\n\nint main() {\n    int M;\n    double eps;\n    cin >> M >> eps;\n\n    const int N = 100;\n    const int T = N * (N - 1) / 2;\n\n    cout << N << endl;\n    cout.flush();\n\n    for (int s = 0; s < M; s++) {\n        int k;\n        if (s < 96) {\n            k = 4 + s;\n        } else {\n            k = 100 - (s - 96);\n        }\n        k = max(4, min(k, 100));\n\n        string graph(T, '0');\n        for (int i = 0; i < k; i++) {\n            for (int j = i + 1; j < k; j++) {\n                long long idx = get_index(i, j, N);\n                graph[idx] = '1';\n            }\n        }\n        cout << graph << endl;\n    }\n    cout.flush();\n\n    for (int q = 0; q < 100; q++) {\n        string H;\n        cin >> H;\n\n        vector<int> deg(N, 0);\n        for (int i = 0; i < N; i++) {\n            for (int j = i + 1; j < N; j++) {\n                long long idx = get_index(i, j, N);\n                if (H[idx] == '1') {\n                    deg[i]++;\n                    deg[j]++;\n                }\n            }\n        }\n\n        vector<int> sorted_deg = deg;\n        sort(sorted_deg.begin(), sorted_deg.end());\n\n        double best_score = 1e300;\n        int best_s = 0;\n\n        for (int s = 0; s < M; s++) {\n            int k;\n            if (s < 96) {\n                k = 4 + s;\n            } else {\n                k = 100 - (s - 96);\n            }\n            k = max(4, min(k, 100));\n\n            int low_count = N - k;\n            int high_count = k;\n\n            double expected_low = 99.0 * eps;\n            double expected_high = (k - 1) + (101 - 2 * k) * eps;\n\n            double score = 0.0;\n            if (low_count > 0) {\n                double sum_low = 0.0;\n                for (int i = 0; i < low_count; i++) {\n                    sum_low += sorted_deg[i];\n                }\n                double mean_low = sum_low / low_count;\n                score += (mean_low - expected_low) * (mean_low - expected_low);\n            }\n            if (high_count > 0) {\n                double sum_high = 0.0;\n                for (int i = low_count; i < N; i++) {\n                    sum_high += sorted_deg[i];\n                }\n                double mean_high = sum_high / high_count;\n                score += (mean_high - expected_high) * (mean_high - expected_high);\n            }\n\n            if (score < best_score) {\n                best_score = score;\n                best_s = s;\n            }\n        }\n\n        cout << best_s << endl;\n        cout.flush();\n    }\n\n    return 0;\n}","ahc017":"#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\n\nstruct Edge {\n    int u, v;\n    ll w;\n};\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, D, K;\n    cin >> N >> M >> D >> K;\n\n    vector<Edge> edges(M);\n    vector<vector<pair<int, ll>>> adj(N);\n    vector<int> deg(N, 0);\n\n    for (int i = 0; i < M; i++) {\n        int u, v;\n        ll w;\n        cin >> u >> v >> w;\n        u--; v--;\n        edges[i] = {u, v, w};\n        adj[u].push_back({v, w});\n        adj[v].push_back({u, w});\n        deg[u]++;\n        deg[v]++;\n    }\n\n    for (int i = 0; i < N; i++) {\n        int x, y;\n        cin >> x >> y;\n    }\n\n    vector<double> betw(M, 0.0);\n\n    for (int s = 0; s < N; s++) {\n        vector<ll> dist(N, LLONG_MAX);\n        vector<double> num_paths(N, 0.0);\n        priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<>> pq;\n\n        dist[s] = 0;\n        num_paths[s] = 1.0;\n        pq.push({0, s});\n\n        while (!pq.empty()) {\n            auto [d, u] = pq.top();\n            pq.pop();\n            if (d != dist[u]) continue;\n            for (auto [v, w] : adj[u]) {\n                ll nd = d + w;\n                if (nd < dist[v]) {\n                    dist[v] = nd;\n                    num_paths[v] = num_paths[u];\n                    pq.push({nd, v});\n                } else if (nd == dist[v]) {\n                    num_paths[v] += num_paths[u];\n                }\n            }\n        }\n\n        for (int i = 0; i < M; i++) {\n            int u = edges[i].u;\n            int v = edges[i].v;\n            ll w = edges[i].w;\n            if (dist[u] != LLONG_MAX && dist[v] != LLONG_MAX) {\n                if (dist[u] + w == dist[v]) {\n                    betw[i] += num_paths[u];\n                }\n                if (dist[v] + w == dist[u]) {\n                    betw[i] += num_paths[v];\n                }\n            }\n        }\n    }\n\n    vector<double> score(M);\n    for (int i = 0; i < M; i++) {\n        int u = edges[i].u;\n        int v = edges[i].v;\n        score[i] = betw[i] * deg[u] * deg[v];\n    }\n\n    vector<pair<double, int>> vec;\n    for (int i = 0; i < M; i++) {\n        vec.push_back({score[i], i});\n    }\n    sort(vec.begin(), vec.end(), [](const auto& a, const auto& b) {\n        return a.first > b.first;\n    });\n\n    vector<double> day_sums(D, 0.0);\n    priority_queue<pair<double, int>, vector<pair<double, int>>, greater<>> pq;\n    for (int d = 0; d < D; d++) {\n        pq.push({0.0, d});\n    }\n\n    vector<int> ans(M);\n    for (auto [s_val, idx] : vec) {\n        auto [current_sum, day] = pq.top();\n        pq.pop();\n        ans[idx] = day + 1;\n        day_sums[day] = current_sum + s_val;\n        pq.push({day_sums[day], day});\n    }\n\n    for (int i = 0; i < M; i++) {\n        if (i) cout << \" \";\n        cout << ans[i];\n    }\n    cout << '\\n';\n\n    return 0;\n}","ahc019":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\n#include <tuple>\n#include <climits>\n#include <cmath>\n#include <array>\n#include <set>\n#include <map>\nusing namespace std;\n\nconst vector<array<array<int, 3>, 3>> rotation_matrices = {\n    {{{1,0,0},{0,1,0},{0,0,1}}},\n    {{{1,0,0},{0,0,1},{0,-1,0}}},\n    {{{1,0,0},{0,-1,0},{0,0,-1}}},\n    {{{1,0,0},{0,0,-1},{0,1,0}}},\n    {{{0,0,1},{0,1,0},{-1,0,0}}},\n    {{{0,1,0},{1,0,0},{0,0,-1}}},\n    {{{0,0,-1},{0,1,0},{1,0,0}}},\n    {{{0,-1,0},{1,0,0},{0,0,1}}},\n    {{-1,0,0},{0,1,0},{0,0,-1}},\n    {{-1,0,0},{0,0,1},{0,1,0}},\n    {{-1,0,0},{0,-1,0},{0,0,1}},\n    {{-1,0,0},{0,0,-1},{0,-1,0}},\n    {{{0,0,-1},{0,-1,0},{-1,0,0}}},\n    {{{0,-1,0},{-1,0,0},{0,0,-1}}},\n    {{{0,0,1},{0,-1,0},{1,0,0}}},\n    {{{0,1,0},{-1,0,0},{0,0,1}}},\n    {{{0,0,1},{-1,0,0},{0,-1,0}}},\n    {{{0,1,0},{0,0,1},{-1,0,0}}},\n    {{{0,0,-1},{-1,0,0},{0,1,0}}},\n    {{{0,-1,0},{0,0,-1},{-1,0,0}}},\n    {{{0,0,1},{1,0,0},{0,1,0}}},\n    {{{0,1,0},{0,0,-1},{1,0,0}}},\n    {{{0,0,-1},{1,0,0},{0,-1,0}}},\n    {{{0,-1,0},{0,0,1},{1,0,0}}}\n};\n\nvector<vector<tuple<int,int,int>>> get_components(vector<vector<vector<bool>>>& grid, int D) {\n    vector<vector<vector<bool>>> visited(D, vector<vector<bool>>(D, vector<bool>(D, false)));\n    vector<vector<tuple<int,int,int>>> components;\n    const int dx[6] = {1, -1, 0, 0, 0, 0};\n    const int dy[6] = {0, 0, 1, -1, 0, 0};\n    const int dz[6] = {0, 0, 0, 0, 1, -1};\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 (grid[x][y][z] && !visited[x][y][z]) {\n                    vector<tuple<int,int,int>> comp;\n                    queue<tuple<int,int,int>> q;\n                    q.push({x, y, z});\n                    visited[x][y][z] = true;\n                    while (!q.empty()) {\n                        auto [cx, cy, cz] = q.front(); q.pop();\n                        comp.push_back({cx, cy, cz});\n                        for (int i = 0; i < 6; i++) {\n                            int nx = cx + dx[i];\n                            int ny = cy + dy[i];\n                            int nz = cz + dz[i];\n                            if (nx >= 0 && nx < D && ny >= 0 && ny < D && nz >= 0 && nz < D && grid[nx][ny][nz] && !visited[nx][ny][nz]) {\n                                visited[nx][ny][nz] = true;\n                                q.push({nx, ny, nz});\n                            }\n                        }\n                    }\n                    components.push_back(comp);\n                }\n            }\n        }\n    }\n    return components;\n}\n\nbool find_placement(const vector<tuple<int,int,int>>& shape, const vector<vector<vector<bool>>>& rem, int D, int& tx, int& ty, int& tz) {\n    int w = 0, h = 0, d = 0;\n    for (auto [x,y,z] : shape) {\n        w = max(w, x+1);\n        h = max(h, y+1);\n        d = max(d, z+1);\n    }\n    for (tx = 0; tx <= D - w; tx++) {\n        for (ty = 0; ty <= D - h; ty++) {\n            for (tz = 0; tz <= D - d; tz++) {\n                bool valid = true;\n                for (auto [x,y,z] : shape) {\n                    int gx = tx + x;\n                    int gy = ty + y;\n                    int gz = tz + z;\n                    if (gx < 0 || gx >= D || gy < 0 || gy >= D || gz < 0 || gz >= D) {\n                        valid = false;\n                        break;\n                    }\n                    if (!rem[gx][gy][gz]) {\n                        valid = false;\n                        break;\n                    }\n                }\n                if (valid) {\n                    return true;\n                }\n            }\n        }\n    }\n    return false;\n}\n\nvoid place_block(vector<vector<vector<int>>>& grid, const vector<tuple<int,int,int>>& shape, int tx, int ty, int tz, int idx) {\n    for (auto [x,y,z] : shape) {\n        int gx = tx + x;\n        int gy = ty + y;\n        int gz = tz + z;\n        if (gx >= 0 && gx < grid.size() && gy >=0 && gy < grid[0].size() && gz >=0 && gz < grid[0][0].size()) {\n            grid[gx][gy][gz] = idx;\n        }\n    }\n}\n\nvoid update_remaining(vector<vector<vector<bool>>>& rem, const vector<tuple<int,int,int>>& shape, int tx, int ty, int tz) {\n    for (auto [x,y,z] : shape) {\n        int gx = tx + x;\n        int gy = ty + y;\n        int gz = tz + z;\n        if (gx >= 0 && gx < rem.size() && gy >=0 && gy < rem[0].size() && gz >=0 && gz < rem[0][0].size()) {\n            rem[gx][gy][gz] = false;\n        }\n    }\n}\n\nbool try_place_shared_block(vector<tuple<int,int,int>>& comp, \n                          vector<vector<vector<bool>>>& R1_rem,\n                          vector<vector<vector<bool>>>& R2_rem,\n                          int D,\n                          int& next_block_idx,\n                          vector<vector<vector<int>>>& b1_grid,\n                          vector<vector<vector<int>>>& b2_grid) {\n    vector<tuple<int,int,int>> orig_comp = comp;\n    for (const auto& rot_mat : rotation_matrices) {\n        vector<tuple<int,int,int>> rotated;\n        for (auto [x,y,z] : orig_comp) {\n            int nx = rot_mat[0][0]*x + rot_mat[0][1]*y + rot_mat[0][2]*z;\n            int ny = rot_mat[1][0]*x + rot_mat[1][1]*y + rot_mat[1][2]*z;\n            int nz = rot_mat[2][0]*x + rot_mat[2][1]*y + rot_mat[2][2]*z;\n            rotated.push_back({nx, ny, nz});\n        }\n\n        int min_x = INT_MAX, max_x = INT_MIN;\n        int min_y = INT_MAX, max_y = INT_MIN;\n        int min_z = INT_MAX, max_z = INT_MIN;\n        for (auto [x,y,z] : rotated) {\n            min_x = min(min_x, x);\n            max_x = max(max_x, x);\n            min_y = min(min_y, y);\n            max_y = max(max_y, y);\n            min_z = min(min_z, z);\n            max_z = max(max_z, z);\n        }\n        int w = max_x - min_x + 1;\n        int h = max_y - min_y + 1;\n        int d = max_z - min_z + 1;\n        if (w <= 0 || h <= 0 || d <= 0) {\n            continue;\n        }\n\n        vector<tuple<int,int,int>> normalized;\n        for (auto [x,y,z] : rotated) {\n            normalized.push_back({x - min_x, y - min_y, z - min_z});\n        }\n\n        int tx1, ty1, tz1;\n        if (!find_placement(normalized, R1_rem, D, tx1, ty1, tz1)) {\n            continue;\n        }\n        int tx2, ty2, tz2;\n        if (!find_placement(normalized, R2_rem, D, tx2, ty2, tz2)) {\n            continue;\n        }\n\n        int idx = next_block_idx++;\n        place_block(b1_grid, normalized, tx1, ty1, tz1, idx);\n        place_block(b2_grid, normalized, tx2, ty2, tz2, idx);\n        update_remaining(R1_rem, normalized, tx1, ty1, tz1);\n        update_remaining(R2_rem, normalized, tx2, ty2, tz2);\n        return true;\n    }\n    return false;\n}\n\nbool try_place_non_shared_block(vector<tuple<int,int,int>>& comp, \n                              vector<vector<vector<bool>>>& target_rem,\n                              int D,\n                              int& next_block_idx,\n                              vector<vector<vector<int>>>& target_grid,\n                              bool for_b1,\n                              bool for_b2) {\n    vector<tuple<int,int,int>> orig_comp = comp;\n    for (const auto& rot_mat : rotation_matrices) {\n        vector<tuple<int,int,int>> rotated;\n        for (auto [x,y,z] : orig_comp) {\n            int nx = rot_mat[0][0]*x + rot_mat[0][1]*y + rot_mat[0][2]*z;\n            int ny = rot_mat[1][0]*x + rot_mat[1][1]*y + rot_mat[1][2]*z;\n            int nz = rot_mat[2][0]*x + rot_mat[2][1]*y + rot_mat[2][2]*z;\n            rotated.push_back({nx, ny, nz});\n        }\n\n        int min_x = INT_MAX, max_x = INT_MIN;\n        int min_y = INT_MAX, max_y = INT_MIN;\n        int min_z = INT_MAX, max_z = INT_MIN;\n        for (auto [x,y,z] : rotated) {\n            min_x = min(min_x, x);\n            max_x = max(max_x, x);\n            min_y = min(min_y, y);\n            max_y = max(max_y, y);\n            min_z = min(min_z, z);\n            max_z = max(max_z, z);\n        }\n        int w = max_x - min_x + 1;\n        int h = max_y - min_y + 1;\n        int d = max_z - min_z + 1;\n        if (w <= 0 || h <= 0 || d <= 0) {\n            continue;\n        }\n\n        vector<tuple<int,int,int>> normalized;\n        for (auto [x,y,z] : rotated) {\n            normalized.push_back({x - min_x, y - min_y, z - min_z});\n        }\n\n        int tx, ty, tz;\n        if (!find_placement(normalized, target_rem, D, tx, ty, tz)) {\n            continue;\n        }\n\n        int idx = next_block_idx++;\n        place_block(target_grid, normalized, tx, ty, tz, idx);\n        update_remaining(target_rem, normalized, tx, ty, tz);\n        return true;\n    }\n    return false;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int D;\n    cin >> D;\n\n    vector<string> f1_lines(D), r1_lines(D), f2_lines(D), r2_lines(D);\n    for (int i = 0; i < D; i++) {\n        cin >> f1_lines[i];\n    }\n    for (int i = 0; i < D; i++) {\n        cin >> r1_lines[i];\n    }\n    for (int i = 0; i < D; i++) {\n        cin >> f2_lines[i];\n    }\n    for (int i = 0; i < D; i++) {\n        cin >> r2_lines[i];\n    }\n\n    vector<vector<vector<bool>>> R1_rem(D, vector<vector<bool>>(D, vector<bool>(D, false)));\n    vector<vector<vector<bool>>> R2_rem(D, vector<vector<bool>>(D, vector<bool>(D, false)));\n\n    for (int z = 0; z < D; z++) {\n        for (int x = 0; x < D; x++) {\n            for (int y = 0; y < D; y++) {\n                if (f1_lines[z][x] == '1' && r1_lines[z][y] == '1') {\n                    R1_rem[x][y][z] = true;\n                }\n                if (f2_lines[z][x] == '1' && r2_lines[z][y] == '1') {\n                    R2_rem[x][y][z] = true;\n                }\n            }\n        }\n    }\n\n    vector<vector<vector<int>>> b1_grid(D, vector<vector<int>>(D, vector<int>(D, 0)));\n    vector<vector<vector<int>>> b2_grid(D, vector<vector<int>>(D, vector<int>(D, 0)));\n\n    int next_block_idx = 1;\n\n    vector<vector<vector<bool>>> R_grid = R1_rem;\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                R_grid[x][y][z] = R_grid[x][y][z] && R2_rem[x][y][z];\n            }\n        }\n    }\n\n    auto shared_components = get_components(R_grid, D);\n    sort(shared_components.begin(), shared_components.end(), [](const auto& a, const auto& b) {\n        return a.size() > b.size();\n    });\n\n    for (auto& comp : shared_components) {\n        if (comp.empty()) continue;\n        if (try_place_shared_block(comp, R1_rem, R2_rem, D, next_block_idx, b1_grid, b2_grid)) {\n        }\n    }\n\n    auto comps1 = get_components(R1_rem, D);\n    sort(comps1.begin(), comps1.end(), [](const auto& a, const auto& b) {\n        return a.size() > b.size();\n    });\n    for (auto& comp : comps1) {\n        if (comp.empty()) continue;\n        if (try_place_non_shared_block(comp, R1_rem, D, next_block_idx, b1_grid, true, false)) {\n        }\n    }\n\n    auto comps2 = get_components(R2_rem, D);\n    sort(comps2.begin(), comps2.end(), [](const auto& a, const auto& b) {\n        return a.size() > b.size();\n    });\n    for (auto& comp : comps2) {\n        if (comp.empty()) continue;\n        if (try_place_non_shared_block(comp, R2_rem, D, next_block_idx, b2_grid, false, true)) {\n        }\n    }\n\n    cout << next_block_idx - 1 << \"\\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                cout << b1_grid[x][y][z];\n                if (x == D-1 && y == D-1 && z == D-1) {\n                } else {\n                    cout << \" \";\n                }\n            }\n        }\n    }\n    cout << \"\\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                cout << b2_grid[x][y][z];\n                if (x == D-1 && y == D-1 && z == D-1) {\n                } else {\n                    cout << \" \";\n                }\n            }\n        }\n    }\n    cout << endl;\n\n    return 0;\n}","ahc020":"#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, K;\n    cin >> N >> M >> K;\n\n    vector<ll> stations_x(N), stations_y(N);\n    for (int i = 0; i < N; i++) {\n        cin >> stations_x[i] >> stations_y[i];\n    }\n\n    vector<tuple<int, int, ll>> edges;\n    for (int i = 0; i < M; i++) {\n        int u, v;\n        ll w;\n        cin >> u >> v >> w;\n        u--; v--;\n        edges.push_back({u, v, w});\n    }\n\n    vector<ll> residents_x(K), residents_y(K);\n    for (int i = 0; i < K; i++) {\n        cin >> residents_x[i] >> residents_y[i];\n    }\n\n    vector<vector<ll>> D_mat(N, vector<ll>(K, 0));\n    for (int i = 0; i < N; i++) {\n        for (int k = 0; k < K; k++) {\n            ll dx = stations_x[i] - residents_x[k];\n            ll dy = stations_y[i] - residents_y[k];\n            D_mat[i][k] = dx*dx + dy*dy;\n        }\n    }\n\n    vector<vector<ll>> d_graph(N, vector<ll>(N, LLONG_MAX/2));\n    vector<vector<int>> next_node(N, vector<int>(N, -1));\n    for (int i = 0; i < N; i++) {\n        d_graph[i][i] = 0;\n        next_node[i][i] = i;\n    }\n    for (auto &e : edges) {\n        int u = get<0>(e);\n        int v = get<1>(e);\n        ll w = get<2>(e);\n        if (w < d_graph[u][v]) {\n            d_graph[u][v] = w;\n            d_graph[v][u] = w;\n            next_node[u][v] = v;\n            next_node[v][u] = u;\n        }\n    }\n\n    for (int k = 0; k < N; k++) {\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                if (d_graph[i][k] + d_graph[k][j] < d_graph[i][j]) {\n                    d_graph[i][j] = d_graph[i][k] + d_graph[k][j];\n                    next_node[i][j] = next_node[i][k];\n                }\n            }\n        }\n    }\n\n    auto compute_mst_cost = [&](const vector<int>& T) -> ll {\n        int n = T.size();\n        if (n <= 1) return 0;\n        vector<ll> min_edge(n, LLONG_MAX);\n        vector<bool> used(n, false);\n        min_edge[0] = 0;\n        ll total = 0;\n        for (int iter = 0; iter < n; iter++) {\n            int u = -1;\n            for (int i = 0; i < n; i++) {\n                if (!used[i] && (u == -1 || min_edge[i] < min_edge[u])) {\n                    u = i;\n                }\n            }\n            if (u == -1) break;\n            used[u] = true;\n            total += min_edge[u];\n            for (int v = 0; v < n; v++) {\n                if (!used[v]) {\n                    ll w = d_graph[T[u]][T[v]];\n                    if (w < min_edge[v]) {\n                        min_edge[v] = w;\n                    }\n                }\n            }\n        }\n        return total;\n    };\n\n    auto compute_station_cost = [&](const vector<int>& T) -> ll {\n        vector<ll> cluster_max(N, 0);\n        for (int k = 0; k < K; k++) {\n            ll minD = LLONG_MAX;\n            int best_station = -1;\n            for (int s : T) {\n                if (D_mat[s][k] < minD) {\n                    minD = D_mat[s][k];\n                    best_station = s;\n                }\n            }\n            if (D_mat[best_station][k] > cluster_max[best_station]) {\n                cluster_max[best_station] = D_mat[best_station][k];\n            }\n        }\n        ll cost = 0;\n        for (int s : T) {\n            ll maxD = cluster_max[s];\n            if (maxD == 0) continue;\n            ll p = sqrt(maxD);\n            if (p * p < maxD) {\n                p++;\n            }\n            if (p > 5000) p = 5000;\n            cost += p * p;\n        }\n        return cost;\n    };\n\n    vector<int> current_T;\n    current_T.push_back(0);\n\n    while (true) {\n        int uncovered_k = -1;\n        for (int k = 0; k < K; k++) {\n            ll minD = LLONG_MAX;\n            for (int s : current_T) {\n                if (D_mat[s][k] < minD) {\n                    minD = D_mat[s][k];\n                }\n            }\n            if (minD > 50000000LL) {\n                uncovered_k = k;\n                break;\n            }\n        }\n        if (uncovered_k == -1) break;\n\n        int best_j = -1;\n        ll best_d_euclid = LLONG_MAX;\n        ll best_d_graph_val = LLONG_MAX;\n        for (int j = 0; j < N; j++) {\n            if (find(current_T.begin(), current_T.end(), j) != current_T.end()) \n                continue;\n\n            ll d_euclid = D_mat[j][uncovered_k];\n            ll d_graph_val = LLONG_MAX;\n            for (int s : current_T) {\n                if (d_graph[s][j] < d_graph_val) {\n                    d_graph_val = d_graph[s][j];\n                }\n            }\n            if (best_j == -1 || d_euclid < best_d_euclid || (d_euclid == best_d_euclid && d_graph_val < best_d_graph_val)) {\n                best_j = j;\n                best_d_euclid = d_euclid;\n                best_d_graph_val = d_graph_val;\n            }\n        }\n        if (best_j == -1) break;\n        current_T.push_back(best_j);\n    }\n\n    for (int iter = 0; iter < 10; iter++) {\n        bool improved = false;\n        ll best_total_after_removal = LLONG_MAX;\n        vector<int> candidate_T_after_removal;\n\n        for (int idx = 1; idx < current_T.size(); idx++) {\n            int i = current_T[idx];\n            bool valid = true;\n            for (int k = 0; k < K; k++) {\n                ll minD_orig = LLONG_MAX;\n                ll minD_after = LLONG_MAX;\n                for (int s : current_T) {\n                    if (D_mat[s][k] < minD_orig) minD_orig = D_mat[s][k];\n                    if (s == i) continue;\n                    if (D_mat[s][k] < minD_after) minD_after = D_mat[s][k];\n                }\n                if (minD_orig == D_mat[i][k] && minD_after > 50000000LL) {\n                    valid = false;\n                    break;\n                }\n            }\n            if (!valid) continue;\n\n            vector<int> candidate_T;\n            for (int s : current_T) {\n                if (s != i) candidate_T.push_back(s);\n            }\n\n            ll edge_cost_candidate = compute_mst_cost(candidate_T);\n            ll station_cost_candidate = compute_station_cost(candidate_T);\n            ll total_candidate = edge_cost_candidate + station_cost_candidate;\n\n            ll current_total = compute_mst_cost(current_T) + compute_station_cost(current_T);\n            if (total_candidate < current_total) {\n                if (total_candidate < best_total_after_removal) {\n                    best_total_after_removal = total_candidate;\n                    candidate_T_after_removal = candidate_T;\n                }\n            }\n        }\n\n        if (best_total_after_removal != LLONG_MAX) {\n            current_T = candidate_T_after_removal;\n            improved = true;\n        }\n\n        if (improved) continue;\n\n        best_total_after_removal = LLONG_MAX;\n        vector<int> candidate_T_after_add;\n\n        for (int j = 0; j < N; j++) {\n            if (find(current_T.begin(), current_T.end(), j) != current_T.end()) \n                continue;\n\n            vector<int> candidate_T = current_T;\n            candidate_T.push_back(j);\n\n            ll edge_cost_candidate = compute_mst_cost(candidate_T);\n            ll station_cost_candidate = compute_station_cost(candidate_T);\n            ll total_candidate = edge_cost_candidate + station_cost_candidate;\n\n            ll current_total = compute_mst_cost(current_T) + compute_station_cost(current_T);\n            if (total_candidate < current_total) {\n                if (total_candidate < best_total_after_removal) {\n                    best_total_after_removal = total_candidate;\n                    candidate_T_after_add = candidate_T;\n                }\n            }\n        }\n\n        if (best_total_after_removal != LLONG_MAX) {\n            current_T = candidate_T_after_add;\n            improved = true;\n        }\n\n        if (!improved) break;\n    }\n\n    vector<int> assignment(K, -1);\n    vector<ll> maxD_in_cluster(N, 0);\n    for (int k = 0; k < K; k++) {\n        ll minD = LLONG_MAX;\n        int best_station = -1;\n        for (int s : current_T) {\n            if (D_mat[s][k] < minD) {\n                minD = D_mat[s][k];\n                best_station = s;\n            }\n        }\n        assignment[k] = best_station;\n        if (D_mat[best_station][k] > maxD_in_cluster[best_station]) {\n            maxD_in_cluster[best_station] = D_mat[best_station][k];\n        }\n    }\n\n    vector<int> P(N, 0);\n    for (int i = 0; i < N; i++) {\n        if (find(current_T.begin(), current_T.end(), i) == current_T.end()) {\n            P[i] = 0;\n        } else {\n            ll maxD = maxD_in_cluster[i];\n            if (maxD == 0) {\n                P[i] = 0;\n            } else {\n                ll p = sqrt(maxD);\n                if (p * p < maxD) {\n                    p++;\n                }\n                if (p > 5000) p = 5000;\n                P[i] = (int)p;\n            }\n        }\n    }\n\n    auto compute_mst_edges = [&](const vector<int>& T) -> vector<pair<int, int>> {\n        int n = T.size();\n        if (n <= 1) return {};\n        vector<ll> min_edge(n, LLONG_MAX);\n        vector<int> parent(n, -1);\n        vector<bool> used(n, false);\n        min_edge[0] = 0;\n        for (int iter = 0; iter < n; iter++) {\n            int u = -1;\n            for (int i = 0; i < n; i++) {\n                if (!used[i] && (u == -1 || min_edge[i] < min_edge[u])) {\n                    u = i;\n                }\n            }\n            if (u == -1) break;\n            used[u] = true;\n            for (int v = 0; v < n; v++) {\n                if (!used[v]) {\n                    ll w = d_graph[T[u]][T[v]];\n                    if (w < min_edge[v]) {\n                        min_edge[v] = w;\n                        parent[v] = u;\n                    }\n                }\n            }\n        }\n        vector<pair<int, int>> edges;\n        for (int i = 1; i < n; i++) {\n            int u_idx = parent[i];\n            edges.push_back({T[u_idx], T[i]});\n        }\n        return edges;\n    };\n\n    auto get_path = [&](int start, int end) -> vector<int> {\n        vector<int> path;\n        int u = start;\n        while (u != end) {\n            path.push_back(u);\n            u = next_node[u][end];\n        }\n        path.push_back(end);\n        return path;\n    };\n\n    vector<pair<int, int>> mst_edges = compute_mst_edges(current_T);\n    vector<bool> edge_used(M, false);\n    for (auto &e : mst_edges) {\n        int u = e.first;\n        int v = e.second;\n        vector<int> path = get_path(u, v);\n        for (int idx = 0; idx < (int)path.size()-1; idx++) {\n            int a = path[idx];\n            int b = path[idx+1];\n            for (int j = 0; j < M; j++) {\n                int u1 = get<0>(edges[j]);\n                int v1 = get<1>(edges[j]);\n                if ((u1 == a && v1 == b) || (u1 == b && v1 == a)) {\n                    edge_used[j] = true;\n                    break;\n                }\n            }\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    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 <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    vector<vector<int>> pyramid(30);\n    for (int x = 0; x < 30; x++) {\n        string line;\n        getline(cin, line);\n        stringstream ss(line);\n        for (int y = 0; y <= x; y++) {\n            int val;\n            ss >> val;\n            pyramid[x].push_back(val);\n        }\n    }\n\n    vector<array<int, 4>> swaps;\n\n    for (int x = 28; x >= 0; x--) {\n        for (int y = 0; y <= x; y++) {\n            int cx = x, cy = y;\n            while (cx < 29) {\n                int left_x = cx + 1;\n                int left_y = cy;\n                int right_x = cx + 1;\n                int right_y = cy + 1;\n\n                int left_val = pyramid[left_x][left_y];\n                int right_val = pyramid[right_x][right_y];\n                int current_val = pyramid[cx][cy];\n\n                if (current_val <= left_val && current_val <= right_val) {\n                    break;\n                }\n\n                int swap_x, swap_y;\n                if (left_val <= right_val) {\n                    swap_x = left_x;\n                    swap_y = left_y;\n                } else {\n                    swap_x = right_x;\n                    swap_y = right_y;\n                }\n\n                swaps.push_back({cx, cy, swap_x, swap_y});\n                swap(pyramid[cx][cy], pyramid[swap_x][swap_y]);\n                cx = swap_x;\n                cy = swap_y;\n            }\n        }\n    }\n\n    cout << swaps.size() << '\\n';\n    for (auto& s : swaps) {\n        cout << s[0] << ' ' << s[1] << ' ' << s[2] << ' ' << s[3] << '\\n';\n    }\n    return 0;\n}","toyota2023summer-final":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\n#include <tuple>\n#include <set>\n#include <cctype>\n#include <cmath>\nusing namespace std;\n\nint main() {\n    int D, N;\n    cin >> D >> N;\n    vector<vector<bool>> is_obstacle(9, vector<bool>(9, false));\n    set<pair<int, int>> non_storage = {{0, 4}};\n\n    for (int idx = 0; idx < N; idx++) {\n        int ri, rj;\n        cin >> ri >> rj;\n        is_obstacle[ri][rj] = true;\n    }\n\n    vector<pair<int, int>> storage_cells;\n    for (int i = 0; i < 9; i++) {\n        for (int j = 0; j < 9; j++) {\n            if (non_storage.find({i, j}) != non_storage.end()) continue;\n            if (is_obstacle[i][j]) continue;\n            storage_cells.push_back({i, j});\n        }\n    }\n\n    vector<vector<int>> dist(9, vector<int>(9, -1));\n    queue<pair<int, int>> q_bfs;\n    dist[0][4] = 0;\n    q_bfs.push({0, 4});\n    const vector<int> dx = {1, -1, 0, 0};\n    const vector<int> dy = {0, 0, 1, -1};\n\n    while (!q_bfs.empty()) {\n        auto [i, j] = q_bfs.front();\n        q_bfs.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 < 9 && nj >= 0 && nj < 9) {\n                if (dist[ni][nj] == -1 && !is_obstacle[ni][nj]) {\n                    dist[ni][nj] = dist[i][j] + 1;\n                    q_bfs.push({ni, nj});\n                }\n            }\n        }\n    }\n\n    sort(storage_cells.begin(), storage_cells.end(), [&](const pair<int, int>& a, const pair<int, int>& b) {\n        if (dist[a.first][a.second] != dist[b.first][b.second]) {\n            return dist[a.first][a.second] > dist[b.first][b.second];\n        }\n        if (a.first != b.first) {\n            return a.first < b.first;\n        }\n        return a.second < b.second;\n    });\n\n    int M = storage_cells.size();\n    vector<vector<int>> container_id(9, vector<int>(9, -1));\n\n    for (int d = 0; d < M; d++) {\n        int t;\n        cin >> t;\n        int index = M - 1 - t;\n        auto [i, j] = storage_cells[index];\n        cout << i << \" \" << j << endl;\n        container_id[i][j] = t;\n    }\n\n    vector<vector<bool>> is_storage(9, vector<bool>(9, false));\n    for (auto [i, j] : storage_cells) {\n        is_storage[i][j] = true;\n    }\n\n    vector<vector<bool>> is_removed(9, vector<bool>(9, false));\n    vector<pair<int, int>> transport_order;\n\n    for (int step = 0; step < M; step++) {\n        vector<vector<bool>> visited(9, vector<bool>(9, false));\n        queue<pair<int, int>> q;\n        if (!is_obstacle[0][4]) {\n            visited[0][4] = true;\n            q.push({0, 4});\n        }\n\n        while (!q.empty()) {\n            auto [i, j] = q.front();\n            q.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 >= 9 || nj < 0 || nj >= 9) continue;\n                if (visited[ni][nj]) continue;\n                if (is_obstacle[ni][nj]) continue;\n                if (is_storage[ni][nj] && !is_removed[ni][nj]) {\n                    continue;\n                }\n                visited[ni][nj] = true;\n                q.push({ni, nj});\n            }\n        }\n\n        vector<tuple<int, int, int>> candidates;\n        for (int i = 0; i < 9; i++) {\n            for (int j = 0; j < 9; j++) {\n                if (!is_storage[i][j]) continue;\n                if (is_removed[i][j]) continue;\n                bool has_visited_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 < 9 && nj >= 0 && nj < 9) {\n                        if (visited[ni][nj]) {\n                            has_visited_neighbor = true;\n                            break;\n                        }\n                    }\n                }\n                if (has_visited_neighbor) {\n                    candidates.push_back(make_tuple(container_id[i][j], i, j));\n                }\n            }\n        }\n\n        sort(candidates.begin(), candidates.end());\n        auto [id_val, i_val, j_val] = candidates[0];\n        transport_order.push_back({i_val, j_val});\n        is_removed[i_val][j_val] = true;\n    }\n\n    for (auto [i, j] : transport_order) {\n        cout << i << \" \" << j << endl;\n    }\n\n    return 0;\n}","ahc024":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int n = 50;\nconst int m = 100;\nconst vector<pair<int, int>> directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};\n\nint main() {\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<set<int>> adj(m + 1);\n    vector<bool> boundary(m + 1, false);\n\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            int c = grid[i][j];\n            if (i == 0 || i == n - 1 || j == 0 || j == n - 1) {\n                boundary[c] = true;\n            }\n            for (auto [dx, dy] : directions) {\n                int ni = i + dx;\n                int nj = j + dy;\n                if (ni >= 0 && ni < n && nj >= 0 && nj < n) {\n                    int d = grid[ni][nj];\n                    if (d != c) {\n                        adj[c].insert(d);\n                        adj[d].insert(c);\n                    }\n                }\n            }\n        }\n    }\n\n    vector<pair<int, int>> boundary_positions;\n    for (int j = 0; j < n; j++) boundary_positions.push_back({n - 1, j});\n    for (int i = n - 2; i >= 0; i--) boundary_positions.push_back({i, n - 1});\n    for (int j = n - 2; j >= 0; j--) boundary_positions.push_back({0, j});\n    for (int i = 1; i < n - 1; i++) boundary_positions.push_back({i, 0});\n\n    vector<set<pair<int, int>>> regions(m + 1);\n    vector<queue<pair<int, int>>> frontiers(m + 1);\n    vector<vector<int>> ans(n, vector<int>(n, 0));\n    vector<bool> placed(m + 1, false);\n\n    int pos_index = 0;\n    for (int c = 1; c <= m; c++) {\n        if (boundary[c]) {\n            if (pos_index >= (int)boundary_positions.size()) break;\n            auto [i, j] = boundary_positions[pos_index++];\n            ans[i][j] = c;\n            regions[c].insert({i, j});\n            frontiers[c].push({i, j});\n            placed[c] = true;\n        }\n    }\n\n    vector<vector<int>> adj_list(m + 1);\n    for (int c = 1; c <= m; c++) {\n        for (int d : adj[c]) {\n            if (d >= 1 && d <= m) {\n                adj_list[c].push_back(d);\n            }\n        }\n    }\n\n    queue<int> q;\n    for (int c = 1; c <= m; c++) {\n        if (placed[c]) {\n            q.push(c);\n        }\n    }\n\n    auto try_place_edge = [&](int c, int d) -> bool {\n        while (!frontiers[c].empty()) {\n            auto cell = frontiers[c].front();\n            vector<pair<int, int>> free_dirs;\n            for (auto [dx, dy] : directions) {\n                int ni = cell.first + dx;\n                int nj = cell.second + dy;\n                if (ni >= 0 && ni < n && nj >= 0 && nj < n && ans[ni][nj] == 0) {\n                    free_dirs.push_back({dx, dy});\n                }\n            }\n            if (!free_dirs.empty()) {\n                auto [dx, dy] = free_dirs[0];\n                int ni = cell.first + dx;\n                int nj = cell.second + dy;\n                ans[ni][nj] = d;\n                regions[d].insert({ni, nj});\n                frontiers[d].push({ni, nj});\n                return true;\n            } else {\n                frontiers[c].pop();\n            }\n        }\n\n        for (auto cell : regions[c]) {\n            for (auto [dx, dy] : directions) {\n                int ni = cell.first + dx;\n                int nj = cell.second + dy;\n                if (ni >= 0 && ni < n && nj >= 0 && nj < n && ans[ni][nj] == 0) {\n                    ans[ni][nj] = c;\n                    regions[c].insert({ni, nj});\n                    frontiers[c].push({ni, nj});\n                    return try_place_edge(c, d);\n                }\n            }\n        }\n        return false;\n    };\n\n    while (!q.empty()) {\n        int c = q.front();\n        q.pop();\n        for (int d : adj_list[c]) {\n            if (placed[d]) continue;\n            if (try_place_edge(c, d)) {\n                placed[d] = true;\n                q.push(d);\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 << ans[i][j];\n        }\n        cout << '\\n';\n    }\n\n    return 0;\n}","ahc025":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(0);\n    cin.tie(0);\n\n    int N, D, Q;\n    cin >> N >> D >> Q;\n\n    vector<vector<int>> cache(N, vector<int>(N, -2));\n\n    auto compare = [&](int i, int j) -> int {\n        if (cache[i][j] != -2) {\n            return cache[i][j];\n        }\n        cout << \"1 1 \" << i << \" \" << j << endl;\n        string res;\n        cin >> res;\n        if (res == \"<\") {\n            cache[i][j] = -1;\n            cache[j][i] = 1;\n        } else if (res == \">\") {\n            cache[i][j] = 1;\n            cache[j][i] = -1;\n        } else {\n            cache[i][j] = 0;\n            cache[j][i] = 0;\n        }\n        return cache[i][j];\n    };\n\n    int queries_used = 0;\n    for (int i = 0; i < N && queries_used < Q; i++) {\n        for (int j = i + 1; j < N && queries_used < Q; j++) {\n            compare(i, j);\n            queries_used++;\n        }\n    }\n\n    while (queries_used < Q) {\n        cout << \"1 1 0 1\" << endl;\n        string res;\n        cin >> res;\n        queries_used++;\n    }\n\n    vector<int> score(N, 1);\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (i == j) continue;\n            if (cache[i][j] >= 0) {\n                score[i]++;\n            }\n        }\n    }\n\n    vector<int> indices(N);\n    iota(indices.begin(), indices.end(), 0);\n    sort(indices.begin(), indices.end(), [&](int a, int b) {\n        return score[a] > score[b];\n    });\n\n    vector<long long> set_load(D, 0);\n    vector<int> assignment(N, -1);\n\n    for (int idx = 0; idx < N; idx++) {\n        int item = indices[idx];\n        int best_set = 0;\n        for (int s = 1; s < D; s++) {\n            if (set_load[s] < set_load[best_set]) {\n                best_set = s;\n            }\n        }\n        assignment[item] = best_set;\n        set_load[best_set] += score[item];\n    }\n\n    for (int i = 0; i < N; i++) {\n        if (i) cout << \" \";\n        cout << assignment[i];\n    }\n    cout << endl;\n\n    return 0;\n}","ahc026":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int n, m;\n    cin >> n >> m;\n    int k = n / m;\n\n    vector<vector<int>> stacks(m);\n    for (int i = 0; i < m; i++) {\n        stacks[i].resize(k);\n        for (int j = 0; j < k; j++) {\n            cin >> stacks[i][j];\n        }\n    }\n\n    vector<int> empty_stacks;\n    vector<pair<int, int>> ops;\n    int cur = 1;\n\n    while (cur <= n) {\n        bool carried = false;\n        for (int s = 0; s < m; s++) {\n            if (stacks[s].empty()) continue;\n            if (stacks[s].back() == cur) {\n                ops.push_back({cur, 0});\n                stacks[s].pop_back();\n                if (stacks[s].empty()) {\n                    empty_stacks.push_back(s);\n                }\n                cur++;\n                carried = true;\n                break;\n            }\n        }\n        if (carried) {\n            continue;\n        }\n\n        int src = -1;\n        int pos = -1;\n        for (int s = 0; s < m; s++) {\n            auto it = find(stacks[s].begin(), stacks[s].end(), cur);\n            if (it != stacks[s].end()) {\n                src = s;\n                pos = it - stacks[s].begin();\n                break;\n            }\n        }\n        if (src == -1) {\n            break;\n        }\n\n        vector<int> segment(stacks[src].begin() + pos + 1, stacks[src].end());\n        int T = segment.back();\n        int dest = -1;\n\n        if (!empty_stacks.empty()) {\n            dest = empty_stacks.back();\n            empty_stacks.pop_back();\n        } else {\n            int candidate_large = -1;\n            int candidate_small = -1;\n            for (int s = 0; s < m; s++) {\n                if (stacks[s].empty()) continue;\n                int top_val = stacks[s].back();\n                if (top_val > T) {\n                    if (candidate_large == -1 || top_val > stacks[candidate_large].back()) {\n                        candidate_large = s;\n                    }\n                }\n                if (candidate_small == -1 || top_val < stacks[candidate_small].back()) {\n                    candidate_small = s;\n                }\n            }\n            if (candidate_large != -1) {\n                dest = candidate_large;\n            } else if (candidate_small != -1) {\n                dest = candidate_small;\n            } else {\n                dest = 0;\n            }\n        }\n\n        int v = segment[0];\n        stacks[src].resize(pos + 1);\n        for (int box : segment) {\n            stacks[dest].push_back(box);\n        }\n        ops.push_back({v, dest + 1});\n    }\n\n    for (auto &op : ops) {\n        cout << op.first << \" \" << op.second << \"\\n\";\n    }\n\n    return 0;\n}","ahc027":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N;\n    cin >> N;\n\n    vector<string> h(N-1);\n    for (int i = 0; i < N-1; i++) {\n        cin >> h[i];\n    }\n\n    vector<string> v(N);\n    for (int i = 0; i < N; i++) {\n        cin >> v[i];\n    }\n\n    vector<vector<int>> d_mat(N, vector<int>(N));\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cin >> d_mat[i][j];\n        }\n    }\n\n    vector<vector<int>> dist(N, vector<int>(N, -1));\n    vector<vector<pair<int, int>>> parent(N, vector<pair<int, int>>(N, make_pair(-1, -1)));\n    queue<pair<int, int>> q;\n    dist[0][0] = 0;\n    q.push({0, 0});\n\n    while (!q.empty()) {\n        auto [i, j] = q.front();\n        q.pop();\n        if (i < N-1 && h[i][j] == '0' && dist[i+1][j] == -1) {\n            dist[i+1][j] = dist[i][j] + 1;\n            parent[i+1][j] = {i, j};\n            q.push({i+1, j});\n        }\n        if (i > 0 && h[i-1][j] == '0' && dist[i-1][j] == -1) {\n            dist[i-1][j] = dist[i][j] + 1;\n            parent[i-1][j] = {i, j};\n            q.push({i-1, j});\n        }\n        if (j < N-1 && v[i][j] == '0' && dist[i][j+1] == -1) {\n            dist[i][j+1] = dist[i][j] + 1;\n            parent[i][j+1] = {i, j};\n            q.push({i, j+1});\n        }\n        if (j > 0 && v[i][j-1] == '0' && dist[i][j-1] == -1) {\n            dist[i][j-1] = dist[i][j] + 1;\n            parent[i][j-1] = {i, j};\n            q.push({i, j-1});\n        }\n    }\n\n    int best_i = -1, best_j = -1;\n    double best_val = -1.0;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (dist[i][j] <= 0) continue;\n            double denom = static_cast<double>(dist[i][j]);\n            denom = denom * denom * denom;\n            double val = static_cast<double>(d_mat[i][j]) / denom;\n            if (val > best_val) {\n                best_val = val;\n                best_i = i;\n                best_j = j;\n            }\n        }\n    }\n    if (best_i == -1) {\n        best_i = 0;\n        best_j = 1;\n    }\n\n    vector<char> path_moves;\n    int cur_i = best_i, cur_j = best_j;\n    while (cur_i != 0 || cur_j != 0) {\n        auto [pi, pj] = parent[cur_i][cur_j];\n        int di = cur_i - pi;\n        int dj = cur_j - pj;\n        char c;\n        if (di == 1) c = 'D';\n        else if (di == -1) c = 'U';\n        else if (dj == 1) c = 'R';\n        else c = 'L';\n        path_moves.push_back(c);\n        cur_i = pi;\n        cur_j = pj;\n    }\n    reverse(path_moves.begin(), path_moves.end());\n    string path_str(path_moves.begin(), path_moves.end());\n\n    vector<vector<vector<tuple<int, int, char>>>> moves(N, vector<vector<tuple<int, int, char>>>(N));\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            vector<tuple<int, int, char>> list;\n            if (i < N-1 && h[i][j] == '0') {\n                list.push_back(make_tuple(i+1, j, 'D'));\n            }\n            if (i > 0 && h[i-1][j] == '0') {\n                list.push_back(make_tuple(i-1, j, 'U'));\n            }\n            if (j < N-1 && v[i][j] == '0') {\n                list.push_back(make_tuple(i, j+1, 'R'));\n            }\n            if (j > 0 && v[i][j-1] == '0') {\n                list.push_back(make_tuple(i, j-1, 'L'));\n            }\n            sort(list.begin(), list.end(), [&](const auto& a, const auto& b) {\n                int da = d_mat[get<0>(a)][get<1>(a)];\n                int db = d_mat[get<0>(b)][get<1>(b)];\n                return da > db;\n            });\n            moves[i][j] = list;\n        }\n    }\n\n    struct State {\n        int i, j, dir;\n    };\n    vector<State> stack;\n    vector<vector<bool>> visited(N, vector<bool>(N, false));\n    string base_route = \"\";\n\n    stack.push_back({0, 0, 0});\n    visited[0][0] = true;\n\n    while (!stack.empty()) {\n        State& top = stack.back();\n        int i = top.i;\n        int j = top.j;\n        int& dir_index = top.dir;\n        if (dir_index >= static_cast<int>(moves[i][j].size())) {\n            stack.pop_back();\n            if (!stack.empty()) {\n                State& prev = stack.back();\n                int di = i - prev.i;\n                int dj = j - prev.j;\n                char move_char;\n                if (di == 1) move_char = 'D';\n                else if (di == -1) move_char = 'U';\n                else if (dj == 1) move_char = 'R';\n                else move_char = 'L';\n                char rev_char;\n                if (move_char == 'D') rev_char = 'U';\n                else if (move_char == 'U') rev_char = 'D';\n                else if (move_char == 'R') rev_char = 'L';\n                else rev_char = 'R';\n                base_route += rev_char;\n            }\n        } else {\n            auto [ni, nj, move_char] = moves[i][j][dir_index];\n            dir_index++;\n            if (!visited[ni][nj]) {\n                visited[ni][nj] = true;\n                stack.push_back({ni, nj, 0});\n                base_route += move_char;\n            }\n        }\n    }\n\n    string rev_path;\n    for (int idx = path_str.size() - 1; idx >= 0; idx--) {\n        char c = path_str[idx];\n        if (c == 'R') rev_path += 'L';\n        else if (c == 'L') rev_path += 'R';\n        else if (c == 'U') rev_path += 'D';\n        else if (c == 'D') rev_path += 'U';\n    }\n    string detour = path_str + rev_path;\n\n    const int MAX_LEN = 100000;\n    int base_len = base_route.size();\n    int detour_len = detour.size();\n    if (base_len <= MAX_LEN) {\n        int available = MAX_LEN - base_len;\n        int K = (detour_len > 0) ? (available / detour_len) : 0;\n        base_route.reserve(base_len + K * detour_len);\n        for (int k = 0; k < K; k++) {\n            base_route += detour;\n        }\n    }\n    if (base_route.size() > MAX_LEN) {\n        base_route = base_route.substr(0, MAX_LEN);\n    }\n\n    cout << base_route << endl;\n\n    return 0;\n}","ahc028":"#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(0);\n\n    int N, M;\n    cin >> N >> M;\n    int s_i, s_j;\n    cin >> s_i >> s_j;\n    vector<string> grid(N);\n    for (int i = 0; i < N; i++) {\n        cin >> grid[i];\n    }\n    vector<string> t_list(M);\n    for (int i = 0; i < M; i++) {\n        cin >> t_list[i];\n    }\n\n    unordered_map<string, int> five_gram_map;\n    for (int i = 0; i < M; i++) {\n        five_gram_map[t_list[i]] = i;\n    }\n\n    unordered_map<string, int> prefix_count_map;\n    for (int i = 0; i < M; i++) {\n        string prefix = t_list[i].substr(0, 4);\n        prefix_count_map[prefix]++;\n    }\n\n    const int NUM_RESTARTS = 5;\n    const int MAX_STEPS_PER_RESTART = 3000;\n    int best_covered = -1;\n    ll best_cost = LLONG_MAX;\n    vector<pair<int, int>> best_path;\n\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n\n    for (int restart = 0; restart < NUM_RESTARTS; restart++) {\n        auto local_prefix_count_map = prefix_count_map;\n        vector<bool> local_covered(M, false);\n        int local_covered_count = 0;\n        string suffix(4, '#');\n        int cur_i = s_i;\n        int cur_j = s_j;\n        vector<pair<int, int>> path;\n        ll total_cost = 0;\n\n        for (int step = 0; step < MAX_STEPS_PER_RESTART; step++) {\n            if (local_covered_count == M) {\n                break;\n            }\n\n            vector<tuple<int, int, string, int>> gain_candidates;\n            int best_gain_cost = INT_MAX;\n\n            vector<tuple<int, int, string>> heuristic_candidates;\n            int best_heuristic_val = -1;\n            int best_heuristic_cost = INT_MAX;\n\n            for (int i = 0; i < N; i++) {\n                for (int j = 0; j < N; j++) {\n                    char c = grid[i][j];\n                    string new_suffix = suffix.substr(1) + c;\n\n                    bool forms_5gram = true;\n                    for (char ch : suffix) {\n                        if (ch == '#') {\n                            forms_5gram = false;\n                            break;\n                        }\n                    }\n\n                    int gain = 0;\n                    int gain_index = -1;\n                    if (forms_5gram) {\n                        string five_gram = suffix + string(1, c);\n                        auto it = five_gram_map.find(five_gram);\n                        if (it != five_gram_map.end()) {\n                            int idx = it->second;\n                            if (!local_covered[idx]) {\n                                gain = 1;\n                                gain_index = idx;\n                            }\n                        }\n                    }\n\n                    int move_cost = abs(i - cur_i) + abs(j - cur_j) + 1;\n\n                    if (gain > 0) {\n                        if (move_cost < best_gain_cost) {\n                            best_gain_cost = move_cost;\n                            gain_candidates.clear();\n                            gain_candidates.push_back(make_tuple(i, j, new_suffix, gain_index));\n                        } else if (move_cost == best_gain_cost) {\n                            gain_candidates.push_back(make_tuple(i, j, new_suffix, gain_index));\n                        }\n                    } else {\n                        int heuristic_val = 0;\n                        bool valid = true;\n                        for (char ch : new_suffix) {\n                            if (ch < 'A' || ch > 'Z') {\n                                valid = false;\n                                break;\n                            }\n                        }\n                        if (valid) {\n                            auto it = local_prefix_count_map.find(new_suffix);\n                            if (it != local_prefix_count_map.end()) {\n                                heuristic_val = it->second;\n                            }\n                        }\n\n                        if (heuristic_val > best_heuristic_val) {\n                            best_heuristic_val = heuristic_val;\n                            best_heuristic_cost = move_cost;\n                            heuristic_candidates.clear();\n                            heuristic_candidates.push_back(make_tuple(i, j, new_suffix));\n                        } else if (heuristic_val == best_heuristic_val) {\n                            if (move_cost < best_heuristic_cost) {\n                                best_heuristic_cost = move_cost;\n                                heuristic_candidates.clear();\n                                heuristic_candidates.push_back(make_tuple(i, j, new_suffix));\n                            } else if (move_cost == best_heuristic_cost) {\n                                heuristic_candidates.push_back(make_tuple(i, j, new_suffix));\n                            }\n                        }\n                    }\n                }\n            }\n\n            int chosen_i = -1, chosen_j = -1;\n            string chosen_suffix = \"\";\n            int gain_idx = -1;\n\n            if (!gain_candidates.empty()) {\n                uniform_int_distribution<int> dist(0, (int)gain_candidates.size() - 1);\n                auto candidate = gain_candidates[dist(rng)];\n                chosen_i = get<0>(candidate);\n                chosen_j = get<1>(candidate);\n                chosen_suffix = get<2>(candidate);\n                gain_idx = get<3>(candidate);\n            } else {\n                if (heuristic_candidates.empty()) {\n                    uniform_int_distribution<int> dist(0, N*N-1);\n                    int idx = dist(rng);\n                    chosen_i = idx / N;\n                    chosen_j = idx % N;\n                    chosen_suffix = suffix.substr(1) + grid[chosen_i][chosen_j];\n                } else {\n                    uniform_int_distribution<int> dist(0, (int)heuristic_candidates.size() - 1);\n                    auto candidate = heuristic_candidates[dist(rng)];\n                    chosen_i = get<0>(candidate);\n                    chosen_j = get<1>(candidate);\n                    chosen_suffix = get<2>(candidate);\n                }\n            }\n\n            int move_cost = abs(chosen_i - cur_i) + abs(chosen_j - cur_j) + 1;\n            total_cost += move_cost;\n\n            cur_i = chosen_i;\n            cur_j = chosen_j;\n            suffix = chosen_suffix;\n\n            if (gain_idx != -1) {\n                local_covered[gain_idx] = true;\n                local_covered_count++;\n                string prefix = t_list[gain_idx].substr(0, 4);\n                local_prefix_count_map[prefix]--;\n                if (local_prefix_count_map[prefix] == 0) {\n                    local_prefix_count_map.erase(prefix);\n                }\n            }\n\n            path.push_back({chosen_i, chosen_j});\n        }\n\n        if (local_covered_count > best_covered || (local_covered_count == best_covered && total_cost < best_cost)) {\n            best_covered = local_covered_count;\n            best_cost = total_cost;\n            best_path = path;\n        }\n    }\n\n    cout << best_path.size() << \"\\n\";\n    for (auto &p : best_path) {\n        cout << p.first << \" \" << p.second << \"\\n\";\n    }\n\n    return 0;\n}","ahc030":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <tuple>\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<tuple<vector<pair<int, int>>, int, int>> fields;\n    for (int k = 0; k < M; k++) {\n        int d;\n        cin >> d;\n        vector<pair<int, int>> shape;\n        int max_i = 0, max_j = 0;\n        for (int i = 0; i < d; i++) {\n            int a, b;\n            cin >> a >> b;\n            shape.emplace_back(a, b);\n            if (a > max_i) max_i = a;\n            if (b > max_j) max_j = b;\n        }\n        fields.emplace_back(shape, max_i, max_j);\n    }\n\n    priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<>> pq;\n    int center = N / 2;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            int dist = abs(i - center) + abs(j - center);\n            pq.emplace(dist, i, j);\n        }\n    }\n\n    vector<vector<int>> status(N, vector<int>(N, 0));\n    vector<pair<int, int>> positive_list;\n\n    while (!pq.empty()) {\n        auto [dist, i, j] = pq.top();\n        pq.pop();\n\n        if (status[i][j] != 0) {\n            continue;\n        }\n\n        cout << \"q 1 \" << i << \" \" << j << endl;\n        cout.flush();\n\n        int v;\n        cin >> v;\n\n        if (v > 0) {\n            status[i][j] = 1;\n            positive_list.emplace_back(i, j);\n\n            for (const auto& field : fields) {\n                const auto& shape = get<0>(field);\n                int max_i = get<1>(field);\n                int max_j = get<2>(field);\n\n                for (const auto& offset : shape) {\n                    int a = offset.first;\n                    int b = offset.second;\n                    int p = i - a;\n                    int q_val = j - b;\n                    if (p < 0 || q_val < 0) continue;\n                    if (p + max_i >= N || q_val + max_j >= N) continue;\n\n                    for (const auto& cell : shape) {\n                        int i2 = p + cell.first;\n                        int j2 = q_val + cell.second;\n                        if (i2 < N && j2 < N && status[i2][j2] == 0) {\n                            int dist2 = abs(i2 - center) + abs(j2 - center);\n                            pq.emplace(dist2, i2, j2);\n                        }\n                    }\n                }\n            }\n        } else {\n            status[i][j] = 2;\n        }\n    }\n\n    cout << \"a \" << positive_list.size();\n    for (const auto& pos : positive_list) {\n        cout << \" \" << pos.first << \" \" << pos.second;\n    }\n    cout << endl;\n    cout.flush();\n\n    int result;\n    cin >> result;\n    return 0;\n}","ahc031":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Rect {\n    int y1, x1, y2, x2;\n    int area() const { \n        return (y2 - y1) * (x2 - x1); \n    }\n};\n\nbool overlap(const Rect& a, const Rect& b) {\n    return (a.y1 < b.y2 && b.y1 < a.y2 && a.x1 < b.x2 && b.x1 < a.x2);\n}\n\nvector<Rect> compute_free_space(const vector<Rect>& rects, int W) {\n    vector<Rect> free = {{0, 0, W, W}};\n    for (const Rect& r : rects) {\n        vector<Rect> new_free;\n        for (const Rect& f : free) {\n            if (!overlap(r, f)) {\n                new_free.push_back(f);\n            } else {\n                if (f.y1 < r.y1) {\n                    new_free.push_back({f.y1, f.x1, r.y1, f.x2});\n                }\n                if (r.y2 < f.y2) {\n                    new_free.push_back({r.y2, f.x1, f.y2, f.x2});\n                }\n                if (f.x1 < r.x1) {\n                    new_free.push_back({max(f.y1, r.y1), f.x1, min(f.y2, r.y2), r.x1});\n                }\n                if (r.x2 < f.x2) {\n                    new_free.push_back({max(f.y1, r.y1), r.x2, min(f.y2, r.y2), f.x2});\n                }\n            }\n        }\n        free = new_free;\n    }\n    return free;\n}\n\nvector<Rect> pack_day(int W, const vector<int>& areas, vector<Rect> free_space) {\n    int N = areas.size();\n    vector<tuple<int, int, Rect>> list;\n    for (int i = 0; i < N; i++) {\n        list.push_back(make_tuple(i, areas[i], Rect()));\n    }\n    sort(list.begin(), list.end(), [](const auto& a, const auto& b) {\n        return get<1>(a) > get<1>(b);\n    });\n\n    auto cmp = [](const Rect& a, const Rect& b) {\n        return a.area() < b.area();\n    };\n    priority_queue<Rect, vector<Rect>, decltype(cmp)> gaps(cmp);\n    for (Rect& gap : free_space) {\n        gaps.push(gap);\n    }\n\n    vector<Rect> placed_rects;\n\n    for (auto& item : list) {\n        int orig_idx = get<0>(item);\n        int a = get<1>(item);\n        bool found = false;\n        vector<Rect> temp_gaps;\n\n        while (!gaps.empty()) {\n            Rect gap = gaps.top();\n            gaps.pop();\n            if (gap.area() < a) {\n                temp_gaps.push_back(gap);\n                continue;\n            }\n            int gap_height = gap.y2 - gap.y1;\n            int gap_width = gap.x2 - gap.x1;\n            int h0 = (a + gap_width - 1) / gap_width;\n            if (h0 > gap_height) {\n                temp_gaps.push_back(gap);\n                continue;\n            }\n            int chosen_h = -1;\n            int chosen_w = -1;\n            for (int h = h0; h <= gap_height; h++) {\n                int w = (a + h - 1) / h;\n                if (w <= gap_width) {\n                    chosen_h = h;\n                    chosen_w = w;\n                    break;\n                }\n            }\n            if (chosen_h == -1) {\n                temp_gaps.push_back(gap);\n                continue;\n            }\n\n            Rect rect = {gap.y1, gap.x1, gap.y1 + chosen_h, gap.x1 + chosen_w};\n            placed_rects.push_back(rect);\n\n            if (gap_width > chosen_w) {\n                gaps.push({gap.y1, gap.x1 + chosen_w, gap.y2, gap.x2});\n            }\n            if (gap_height > chosen_h) {\n                gaps.push({gap.y1 + chosen_h, gap.x1, gap.y2, gap.x1 + chosen_w});\n            }\n\n            for (Rect& g : temp_gaps) {\n                gaps.push(g);\n            }\n            found = true;\n            break;\n        }\n        if (!found) {\n            if (gaps.empty()) {\n                placed_rects.push_back({0, 0, 1, 1});\n            } else {\n                Rect gap = gaps.top();\n                gaps.pop();\n                int gap_height = gap.y2 - gap.y1;\n                int gap_width = gap.x2 - gap.x1;\n                int h = gap_height;\n                int w = (a + h - 1) / h;\n                if (w > gap_width) {\n                    w = gap_width;\n                    h = (a + w - 1) / w;\n                }\n                Rect rect = {gap.y1, gap.x1, gap.y1 + h, gap.x1 + w};\n                placed_rects.push_back(rect);\n                if (gap_width > w) {\n                    gaps.push({gap.y1, gap.x1 + w, gap.y2, gap.x2});\n                }\n                if (gap_height > h) {\n                    gaps.push({gap.y1 + h, gap.x1, gap.y2, gap.x1 + w});\n                }\n            }\n        }\n    }\n\n    vector<Rect> result(N);\n    for (int i = 0; i < placed_rects.size(); i++) {\n        int orig_idx = get<0>(list[i]);\n        result[orig_idx] = placed_rects[i];\n    }\n    return result;\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int W, D, N;\n    cin >> W >> D >> N;\n\n    vector<vector<int>> areas(D, vector<int>(N));\n    for (int d = 0; d < D; d++) {\n        for (int k = 0; k < N; k++) {\n            cin >> areas[d][k];\n        }\n    }\n\n    vector<vector<Rect>> all_rects(D);\n\n    vector<Rect> free_space = compute_free_space({}, W);\n    all_rects[0] = pack_day(W, areas[0], free_space);\n\n    for (int d = 1; d < D; d++) {\n        free_space = compute_free_space(all_rects[d-1], W);\n        all_rects[d] = pack_day(W, areas[d], free_space);\n    }\n\n    for (int d = 0; d < D; d++) {\n        for (int k = 0; k < N; k++) {\n            Rect r = all_rects[d][k];\n            cout << r.y1 << \" \" << r.x1 << \" \" << r.y2 << \" \" << r.x2 << '\\n';\n        }\n    }\n\n    return 0;\n}","ahc032":"#include <iostream>\n#include <vector>\n#include <tuple>\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>> a(N, vector<long long>(N));\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cin >> a[i][j];\n        }\n    }\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<vector<long long>> board(N, vector<long long>(N, 0));\n    vector<tuple<int, int, int>> operations;\n\n    for (int step = 0; step < K; step++) {\n        long long best_gain = 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 gain = 0;\n                    for (int di = 0; di < 3; di++) {\n                        for (int dj = 0; dj < 3; dj++) {\n                            int i = p + di;\n                            int j = q + dj;\n                            long long stamp_val = stamps[m][di][dj];\n                            long long old_val = a[i][j] + board[i][j];\n                            long long old_mod = old_val % MOD;\n                            long long new_val = old_val + stamp_val;\n                            long long new_mod = new_val % MOD;\n                            gain += (new_mod - old_mod);\n                        }\n                    }\n                    if (gain > best_gain) {\n                        best_gain = gain;\n                        best_m = m;\n                        best_p = p;\n                        best_q = q;\n                    }\n                }\n            }\n        }\n\n        if (best_gain <= 0) {\n            break;\n        }\n\n        operations.emplace_back(best_m, best_p, best_q);\n        for (int di = 0; di < 3; di++) {\n            for (int dj = 0; dj < 3; dj++) {\n                int i = best_p + di;\n                int j = best_q + dj;\n                board[i][j] += stamps[best_m][di][dj];\n            }\n        }\n    }\n\n    cout << operations.size() << '\\n';\n    for (auto [m, p, q] : operations) {\n        cout << m << ' ' << p << ' ' << q << '\\n';\n    }\n\n    return 0;\n}","ahc033":"#include <bits/stdc++.h>\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        return x == other.x && y == other.y;\n    }\n};\n\nchar compute_next_move(Point start, Point goal, const vector<vector<int>>& grid, bool is_carrying, bool is_large) {\n    if (start == goal) {\n        return '.';\n    }\n    \n    vector<vector<bool>> visited(5, vector<bool>(5, false));\n    vector<vector<Point>> parent(5, vector<Point>(5, Point(-1, -1)));\n    queue<Point> q;\n    q.push(start);\n    visited[start.x][start.y] = true;\n    \n    const vector<int> dx = {-1, 1, 0, 0};\n    const vector<int> dy = {0, 0, -1, 1};\n    const vector<char> dirs = {'U', 'D', 'L', 'R'};\n    bool found = false;\n    \n    while (!q.empty()) {\n        Point p = q.front();\n        q.pop();\n        if (p.x == goal.x && p.y == goal.y) {\n            found = true;\n            break;\n        }\n        \n        for (int d = 0; d < 4; d++) {\n            int nx = p.x + dx[d];\n            int ny = p.y + dy[d];\n            if (nx < 0 || nx >= 5 || ny < 0 || ny >= 5) continue;\n            if (visited[nx][ny]) continue;\n            \n            bool allowed;\n            if (is_carrying) {\n                if (is_large) {\n                    allowed = true;\n                } else {\n                    allowed = (grid[nx][ny] == -1);\n                }\n            } else {\n                allowed = true;\n            }\n            \n            if (allowed) {\n                visited[nx][ny] = true;\n                parent[nx][ny] = p;\n                q.push(Point(nx, ny));\n            }\n        }\n    }\n    \n    if (!found) {\n        return '.';\n    }\n    \n    Point p = goal;\n    while (!(parent[p.x][p.y] == start)) {\n        p = parent[p.x][p.y];\n    }\n    \n    int diff_x = p.x - start.x;\n    int diff_y = p.y - start.y;\n    if (diff_x == -1) return 'U';\n    if (diff_x == 1) return 'D';\n    if (diff_y == -1) return 'L';\n    if (diff_y == 1) return 'R';\n    return '.';\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \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<vector<int>> grid(5, vector<int>(5, -1));\n    vector<int> next_arrival_index(5, 0);\n    vector<int> dispatched(5, 0);\n    vector<int> next_expected(5);\n    for (int i = 0; i < 5; i++) {\n        next_expected[i] = 5 * i;\n    }\n    \n    struct Crane {\n        Point pos;\n        int hold;\n    };\n    vector<Crane> cranes(5);\n    for (int i = 0; i < 5; i++) {\n        cranes[i] = {{i, 0}, -1};\n    }\n    \n    vector<string> output_lines(5);\n    \n    for (int turn = 0; turn < 10000; turn++) {\n        for (int i = 0; i < 5; i++) {\n            if (next_arrival_index[i] < 5) {\n                bool can_place = true;\n                if (grid[i][0] != -1) {\n                    can_place = false;\n                } else {\n                    for (int k = 0; k < 5; k++) {\n                        if (cranes[k].pos.x == i && cranes[k].pos.y == 0 && cranes[k].hold != -1) {\n                            can_place = false;\n                            break;\n                        }\n                    }\n                }\n                if (can_place) {\n                    int b = A[i][next_arrival_index[i]];\n                    grid[i][0] = b;\n                    next_arrival_index[i]++;\n                }\n            }\n        }\n        \n        vector<Point> old_positions(5);\n        for (int i = 0; i < 5; i++) {\n            old_positions[i] = cranes[i].pos;\n        }\n        \n        vector<char> actions(5, '.');\n        vector<Point> new_positions(5);\n        for (int i = 0; i < 5; i++) {\n            new_positions[i] = old_positions[i];\n        }\n        \n        for (int i = 0; i < 5; i++) {\n            bool is_large = (i == 0);\n            bool is_carrying = (cranes[i].hold != -1);\n            \n            if (is_carrying) {\n                int b = cranes[i].hold;\n                int target_row = b / 5;\n                Point target(target_row, 4);\n                if (old_positions[i].x == target_row && old_positions[i].y == 4) {\n                    actions[i] = 'Q';\n                    new_positions[i] = old_positions[i];\n                } else {\n                    char dir = compute_next_move(old_positions[i], target, grid, is_carrying, is_large);\n                    if (dir != '.') {\n                        int nx = old_positions[i].x;\n                        int ny = old_positions[i].y;\n                        if (dir == 'U') nx--;\n                        else if (dir == 'D') nx++;\n                        else if (dir == 'L') ny--;\n                        else if (dir == 'R') ny++;\n                        new_positions[i] = Point(nx, ny);\n                        actions[i] = dir;\n                    } else {\n                        actions[i] = '.';\n                        new_positions[i] = old_positions[i];\n                    }\n                }\n            } else {\n                if (grid[old_positions[i].x][old_positions[i].y] != -1) {\n                    int b = grid[old_positions[i].x][old_positions[i].y];\n                    int target_row = b / 5;\n                    if (b == next_expected[target_row]) {\n                        actions[i] = 'P';\n                        new_positions[i] = old_positions[i];\n                    } else {\n                        actions[i] = 'P';\n                        new_positions[i] = old_positions[i];\n                    }\n                } else {\n                    if (is_large) {\n                        Point goal(-1, -1);\n                        for (int x = 0; x < 5; x++) {\n                            for (int y = 0; y < 5; y++) {\n                                if (grid[x][y] != -1) {\n                                    int b = grid[x][y];\n                                    int expected_b = 5 * (b / 5) + dispatched[b / 5];\n                                    if (b == expected_b) {\n                                        goal = Point(x, y);\n                                        break;\n                                    }\n                                }\n                            }\n                            if (goal.x != -1) break;\n                        }\n                        if (goal.x == -1) {\n                            goal = Point(2, 2);\n                        }\n                        char dir = compute_next_move(old_positions[i], goal, grid, is_carrying, is_large);\n                        if (dir != '.') {\n                            int nx = old_positions[i].x;\n                            int ny = old_positions[i].y;\n                            if (dir == 'U') nx--;\n                            else if (dir == 'D') nx++;\n                            else if (dir == 'L') ny--;\n                            else if (dir == 'R') ny++;\n                            new_positions[i] = Point(nx, ny);\n                            actions[i] = dir;\n                        } else {\n                            actions[i] = '.';\n                            new_positions[i] = old_positions[i];\n                        }\n                    } else {\n                        if (grid[old_positions[i].x][1] == -1 && old_positions[i].y < 4) {\n                            actions[i] = 'R';\n                            new_positions[i] = Point(old_positions[i].x, old_positions[i].y + 1);\n                        } else {\n                            actions[i] = '.';\n                            new_positions[i] = old_positions[i];\n                        }\n                    }\n                }\n            }\n            \n            bool valid = true;\n            for (int j = 0; j < i; j++) {\n                if (new_positions[i] == new_positions[j]) {\n                    valid = false;\n                    break;\n                }\n                if (new_positions[i] == old_positions[j] && new_positions[j] == old_positions[i]) {\n                    valid = false;\n                    break;\n                }\n            }\n            if (!valid) {\n                actions[i] = '.';\n                new_positions[i] = old_positions[i];\n            }\n        }\n        \n        for (int i = 0; i < 5; i++) {\n            output_lines[i] += actions[i];\n        }\n        \n        for (int i = 0; i < 5; i++) {\n            if (actions[i] == 'P' && grid[old_positions[i].x][old_positions[i].y] != -1) {\n                int b = grid[old_positions[i].x][old_positions[i].y];\n                cranes[i].hold = b;\n                grid[old_positions[i].x][old_positions[i].y] = -1;\n            } else if (actions[i] == 'Q' && cranes[i].hold != -1) {\n                int b = cranes[i].hold;\n                grid[old_positions[i].x][old_positions[i].y] = b;\n                cranes[i].hold = -1;\n            } else if (actions[i] == 'U' || actions[i] == 'D' || actions[i] == 'L' || actions[i] == 'R') {\n                int dx = 0, dy = 0;\n                if (actions[i] == 'U') dx = -1;\n                else if (actions[i] == 'D') dx = 1;\n                else if (actions[i] == 'L') dy = -1;\n                else if (actions[i] == 'R') dy = 1;\n                cranes[i].pos.x = old_positions[i].x + dx;\n                cranes[i].pos.y = old_positions[i].y + dy;\n            }\n        }\n        \n        for (int i = 0; i < 5; i++) {\n            if (grid[i][4] != -1) {\n                int b = grid[i][4];\n                int expected_val = 5 * i + dispatched[i];\n                if (b == expected_val) {\n                    dispatched[i]++;\n                    next_expected[i] = 5 * i + dispatched[i];\n                }\n                grid[i][4] = -1;\n            }\n        }\n        \n        int total_dispatched = 0;\n        for (int i = 0; i < 5; i++) {\n            total_dispatched += dispatched[i];\n        }\n        if (total_dispatched == 25) {\n            break;\n        }\n    }\n    \n    int max_len = 0;\n    for (int i = 0; i < 5; i++) {\n        if ((int)output_lines[i].size() > max_len) {\n            max_len = output_lines[i].size();\n        }\n    }\n    for (int i = 0; i < 5; i++) {\n        while ((int)output_lines[i].size() < max_len) {\n            output_lines[i] += '.';\n        }\n    }\n    \n    for (int i = 0; i < 5; i++) {\n        cout << output_lines[i] << '\\n';\n    }\n    \n    return 0;\n}","ahc034":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    const int N = 20;\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<int>> remaining_source(N, vector<int>(N, 0));\n    vector<vector<int>> remaining_sink(N, vector<int>(N, 0));\n\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (grid[i][j] > 0) {\n                remaining_source[i][j] = grid[i][j];\n            } else if (grid[i][j] < 0) {\n                remaining_sink[i][j] = -grid[i][j];\n            }\n        }\n    }\n\n    int cur_i = 0, cur_j = 0;\n    int cur_load = 0;\n\n    auto has_remaining = [&]() -> bool {\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                if (remaining_source[i][j] > 0 || remaining_sink[i][j] > 0) {\n                    return true;\n                }\n            }\n        }\n        return false;\n    };\n\n    while (has_remaining()) {\n        bool handled = false;\n\n        if (remaining_source[cur_i][cur_j] > 0) {\n            int d = remaining_source[cur_i][cur_j];\n            cout << \"+\" << d << \"\\n\";\n            cur_load += d;\n            remaining_source[cur_i][cur_j] = 0;\n            handled = true;\n        }\n        else if (remaining_sink[cur_i][cur_j] > 0 && cur_load > 0) {\n            int d = min(remaining_sink[cur_i][cur_j], cur_load);\n            cout << \"-\" << d << \"\\n\";\n            cur_load -= d;\n            remaining_sink[cur_i][cur_j] -= d;\n            handled = true;\n        }\n\n        if (handled) {\n            continue;\n        }\n\n        int best_i = -1, best_j = -1;\n        int min_score = INT_MAX;\n\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                if (remaining_source[i][j] > 0) {\n                    int dist = abs(i - cur_i) + abs(j - cur_j);\n                    int score = dist * (100 + cur_load);\n                    if (score < min_score) {\n                        min_score = score;\n                        best_i = i;\n                        best_j = j;\n                    }\n                }\n                if (remaining_sink[i][j] > 0 && cur_load > 0) {\n                    int dist = abs(i - cur_i) + abs(j - cur_j);\n                    int score = dist * (100 + cur_load);\n                    if (score < min_score) {\n                        min_score = score;\n                        best_i = i;\n                        best_j = j;\n                    }\n                }\n            }\n        }\n\n        if (best_i == -1) {\n            break;\n        }\n\n        int dx = best_i - cur_i;\n        int dy = best_j - cur_j;\n        string moves = \"\";\n        if (dx > 0) moves += string(dx, 'D');\n        else if (dx < 0) moves += string(-dx, 'U');\n        if (dy > 0) moves += string(dy, 'R');\n        else if (dy < 0) moves += string(-dy, 'L');\n\n        for (char c : moves) {\n            cout << c << \"\\n\";\n            if (c == 'U') cur_i--;\n            else if (c == 'D') cur_i++;\n            else if (c == 'L') cur_j--;\n            else if (c == 'R') cur_j++;\n        }\n    }\n\n    return 0;\n}","ahc035":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, T;\n    cin >> N >> M >> T;\n\n    int seed_count = 2 * N * (N - 1);\n    vector<vector<int>> seeds(seed_count, vector<int>(M));\n\n    // Read initial seeds\n    for (int k = 0; k < seed_count; k++) {\n        for (int l = 0; l < M; l++) {\n            cin >> seeds[k][l];\n        }\n    }\n\n    // Precompute X: max for each criterion over initial seeds\n    vector<int> X(M, 0);\n    for (int k = 0; k < seed_count; k++) {\n        for (int l = 0; l < M; l++) {\n            if (seeds[k][l] > X[l]) {\n                X[l] = seeds[k][l];\n            }\n        }\n    }\n\n    // Precompute grid positions: (i, j, degree)\n    vector<tuple<int, int, int>> positions;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            int deg = 4;\n            if (i == 0 || i == N-1) deg--;\n            if (j == 0 || j == N-1) deg--;\n            positions.emplace_back(i, j, deg);\n        }\n    }\n\n    // Sort positions: primary by degree descending, then by squared distance to center ascending\n    double center = (N - 1) / 2.0;\n    sort(positions.begin(), positions.end(), [&](const auto& a, const auto& b) {\n        if (get<2>(a) != get<2>(b)) \n            return get<2>(a) > get<2>(b);\n        double dist_a = (get<0>(a) - center) * (get<0>(a) - center) + (get<1>(a) - center) * (get<1>(a) - center);\n        double dist_b = (get<0>(b) - center) * (get<0>(b) - center) + (get<1>(b) - center) * (get<1>(b) - center);\n        if (abs(dist_a - dist_b) > 1e-9)\n            return dist_a < dist_b;\n        if (get<0>(a) != get<0>(b))\n            return get<0>(a) < get<0>(b);\n        return get<1>(a) < get<1>(b);\n    });\n\n    for (int t = 0; t < T; t++) {\n        // Compute score and V for each current seed\n        vector<tuple<double, long long, int>> seed_scores;\n        for (int k = 0; k < seed_count; k++) {\n            double score = 0.0;\n            long long V = 0;\n            for (int l = 0; l < M; l++) {\n                V += seeds[k][l];\n                if (X[l] > 0) {\n                    score += static_cast<double>(seeds[k][l]) / X[l];\n                }\n            }\n            seed_scores.emplace_back(score, V, k);\n        }\n\n        // Sort seeds by score descending, then V descending\n        sort(seed_scores.begin(), seed_scores.end(), [](const auto& a, const auto& b) {\n            if (get<0>(a) != get<0>(b))\n                return get<0>(a) > get<0>(b);\n            return get<1>(a) > get<1>(b);\n        });\n\n        // Prepare grid: assign top N*N seeds to the sorted positions\n        vector<vector<int>> grid(N, vector<int>(N));\n        for (int idx = 0; idx < N * N; idx++) {\n            auto [i, j, deg] = positions[idx];\n            grid[i][j] = get<2>(seed_scores[idx]);\n        }\n\n        // Output the grid\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 << '\\n';\n        }\n        cout.flush();\n\n        // Read new seeds for the next turn\n        for (int k = 0; k < seed_count; k++) {\n            for (int l = 0; l < M; l++) {\n                cin >> seeds[k][l];\n            }\n        }\n    }\n\n    return 0;\n}","ahc038":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <cctype>\n#include <cmath>\n#include <string>\n#include <map>\n#include <set>\n#include <queue>\n#include <stack>\n#include <utility>\n#include <climits>\nusing namespace std;\n\ntypedef pair<int, int> Point;\n\nvector<char> get_manhattan_path(Point from, Point to) {\n    vector<char> path;\n    int dx = to.first - from.first;\n    int dy = to.second - from.second;\n    char x_dir = 0;\n    char y_dir = 0;\n    if (dx > 0) x_dir = 'D';\n    else if (dx < 0) x_dir = 'U';\n    if (dy > 0) y_dir = 'R';\n    else if (dy < 0) y_dir = 'L';\n\n    for (int i = 0; i < abs(dx); i++) {\n        path.push_back(x_dir);\n    }\n    for (int i = 0; i < abs(dy); i++) {\n        path.push_back(y_dir);\n    }\n    return path;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, V;\n    cin >> N >> M >> V;\n\n    vector<string> s_grid(N);\n    vector<string> 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    vector<Point> A;\n    vector<Point> B;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (s_grid[i][j] == '1') {\n                A.push_back({i, j});\n            }\n            if (t_grid[i][j] == '1') {\n                B.push_back({i, j});\n            }\n        }\n    }\n\n    sort(A.begin(), A.end());\n    sort(B.begin(), B.end());\n\n    int V_prime = 5;\n    cout << V_prime << \"\\n\";\n    for (int u = 1; u < V_prime; u++) {\n        cout << 0 << \" \" << 1 << \"\\n\";\n    }\n    cout << 0 << \" \" << 0 << \"\\n\";\n\n    vector<string> operations;\n\n    operations.push_back(\".\" + string(\"RRRL\") + string(\".....\"));\n    operations.push_back(\".\" + string(\"..R.\") + string(\".....\"));\n\n    Point current_root = {0, 0};\n\n    vector<pair<Point, Point>> valid_pairs;\n    for (int i = 0; i < M; i++) {\n        if (A[i] != B[i]) {\n            valid_pairs.push_back({A[i], B[i]});\n        }\n    }\n\n    auto get_options = [&](int i, int j) -> vector<pair<int, Point>> {\n        vector<pair<int, Point>> options;\n        if (j - 1 >= 0) {\n            options.push_back({1, {i, j - 1}});\n        }\n        if (i - 1 >= 0) {\n            options.push_back({2, {i - 1, j}});\n        }\n        if (j + 1 < N) {\n            options.push_back({3, {i, j + 1}});\n        }\n        if (i + 1 < N) {\n            options.push_back({4, {i + 1, j}});\n        }\n        return options;\n    };\n\n    for (auto &p : valid_pairs) {\n        Point a = p.first;\n        Point b = p.second;\n\n        auto options_a = get_options(a.first, a.second);\n        if (options_a.empty()) {\n            continue;\n        }\n        int best_u = -1;\n        Point best_R_a;\n        int min_dist = INT_MAX;\n        for (auto &opt : options_a) {\n            int u_idx = opt.first;\n            Point R = opt.second;\n            int dist = abs(current_root.first - R.first) + abs(current_root.second - R.second);\n            if (dist < min_dist) {\n                min_dist = dist;\n                best_u = u_idx;\n                best_R_a = R;\n            }\n        }\n        Point R_a = best_R_a;\n\n        auto options_b = get_options(b.first, b.second);\n        if (options_b.empty()) {\n            continue;\n        }\n        auto best_b = options_b[0];\n        int v_rel = best_b.first;\n        Point R_b = best_b.second;\n\n        vector<char> path1 = get_manhattan_path(current_root, R_a);\n        if (path1.empty()) {\n            string action_str = \".....\";\n            action_str[0] = '.';\n            if (best_u == 1) action_str[1] = 'P';\n            else if (best_u == 2) action_str[2] = 'P';\n            else if (best_u == 3) action_str[3] = 'P';\n            else if (best_u == 4) action_str[4] = 'P';\n            operations.push_back(\".\" + string(\"....\") + action_str);\n        } else {\n            for (int idx = 0; idx < path1.size(); idx++) {\n                string rot_str = \"....\";\n                string action_str = \".....\";\n                if (idx == path1.size() - 1) {\n                    action_str[0] = '.';\n                    if (best_u == 1) action_str[1] = 'P';\n                    else if (best_u == 2) action_str[2] = 'P';\n                    else if (best_u == 3) action_str[3] = 'P';\n                    else if (best_u == 4) action_str[4] = 'P';\n                }\n                string op = string(1, path1[idx]) + rot_str + action_str;\n                operations.push_back(op);\n            }\n        }\n\n        vector<char> path2 = get_manhattan_path(R_a, R_b);\n        if (path2.empty()) {\n            string action_str = \".....\";\n            action_str[0] = '.';\n            if (v_rel == 1) action_str[1] = 'P';\n            else if (v_rel == 2) action_str[2] = 'P';\n            else if (v_rel == 3) action_str[3] = 'P';\n            else if (v_rel == 4) action_str[4] = 'P';\n            operations.push_back(\".\" + string(\"....\") + action_str);\n        } else {\n            for (int idx = 0; idx < path2.size(); idx++) {\n                string rot_str = \"....\";\n                string action_str = \".....\";\n                if (idx == path2.size() - 1) {\n                    action_str[0] = '.';\n                    if (v_rel == 1) action_str[1] = 'P';\n                    else if (v_rel == 2) action_str[2] = 'P';\n                    else if (v_rel == 3) action_str[3] = 'P';\n                    else if (v_rel == 4) action_str[4] = 'P';\n                }\n                string op = string(1, path2[idx]) + rot_str + action_str;\n                operations.push_back(op);\n            }\n        }\n\n        current_root = R_b;\n    }\n\n    for (string &op : operations) {\n        cout << op << \"\\n\";\n    }\n\n    return 0;\n}","ahc039":"#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\n\nstruct Point {\n    int x, y;\n};\n\nstruct Rect {\n    int x1, x2, y1, y2;\n    Rect() : x1(0), x2(0), y1(0), y2(0) {}\n    Rect(int a, int b, int c, int d) {\n        x1 = min(a, b); x2 = max(a, b);\n        y1 = min(c, d); y2 = max(c, d);\n    }\n};\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    const int N = 5000;\n    vector<Point> mackerels(N), sardines(N);\n    for (int i = 0; i < N; i++) {\n        cin >> mackerels[i].x >> mackerels[i].y;\n    }\n    for (int i = 0; i < N; i++) {\n        cin >> sardines[i].x >> sardines[i].y;\n    }\n\n    vector<int> xs, ys;\n    for (int i = 0; i < N; i++) {\n        xs.push_back(mackerels[i].x);\n        ys.push_back(mackerels[i].y);\n        xs.push_back(sardines[i].x);\n        ys.push_back(sardines[i].y);\n    }\n    sort(xs.begin(), xs.end());\n    xs.erase(unique(xs.begin(), xs.end()), xs.end());\n    sort(ys.begin(), ys.end());\n    ys.erase(unique(ys.begin(), ys.end()), ys.end());\n\n    int X = xs.size(), Y = ys.size();\n\n    vector<vector<int>> grid_val(X, vector<int>(Y, 0));\n    for (int i = 0; i < N; i++) {\n        auto p = mackerels[i];\n        int ix = lower_bound(xs.begin(), xs.end(), p.x) - xs.begin();\n        int iy = lower_bound(ys.begin(), ys.end(), p.y) - ys.begin();\n        if (ix < X && iy < Y) {\n            grid_val[ix][iy] = 1;\n        }\n    }\n    for (int i = 0; i < N; i++) {\n        auto p = sardines[i];\n        int ix = lower_bound(xs.begin(), xs.end(), p.x) - xs.begin();\n        int iy = lower_bound(ys.begin(), ys.end(), p.y) - ys.begin();\n        if (ix < X && iy < Y) {\n            grid_val[ix][iy] = -1;\n        }\n    }\n\n    vector<vector<int>> P(X+1, vector<int>(Y+1, 0));\n    for (int i = 0; i < X; i++) {\n        for (int j = 0; j < Y; j++) {\n            P[i+1][j+1] = P[i][j+1] + P[i+1][j] - P[i][j] + grid_val[i][j];\n        }\n    }\n\n    auto get_sum = [&](int x1, int x2, int y1, int y2) -> int {\n        int i1 = lower_bound(xs.begin(), xs.end(), x1) - xs.begin();\n        int i2 = upper_bound(xs.begin(), xs.end(), x2) - xs.begin();\n        if (i1 >= i2) return 0;\n        i2--;\n\n        int j1 = lower_bound(ys.begin(), ys.end(), y1) - ys.begin();\n        int j2 = upper_bound(ys.begin(), ys.end(), y2) - ys.begin();\n        if (j1 >= j2) return 0;\n        j2--;\n\n        return P[i2+1][j2+1] - P[i1][j2+1] - P[i2+1][j1] + P[i1][j1];\n    };\n\n    auto greedy_improve = [&](Rect r) -> Rect {\n        int x1 = r.x1, x2 = r.x2, y1 = r.y1, y2 = r.y2;\n        bool improved;\n        do {\n            improved = false;\n            while (x1 < x2) {\n                int strip_sum = get_sum(x1, x1, y1, y2);\n                if (strip_sum < 0) {\n                    x1++;\n                    improved = true;\n                } else {\n                    break;\n                }\n            }\n            while (x2 > x1) {\n                int strip_sum = get_sum(x2, x2, y1, y2);\n                if (strip_sum < 0) {\n                    x2--;\n                    improved = true;\n                } else {\n                    break;\n                }\n            }\n            while (y2 > y1) {\n                int strip_sum = get_sum(x1, x2, y2, y2);\n                if (strip_sum < 0) {\n                    y2--;\n                    improved = true;\n                } else {\n                    break;\n                }\n            }\n            while (y1 < y2) {\n                int strip_sum = get_sum(x1, x2, y1, y1);\n                if (strip_sum < 0) {\n                    y1++;\n                    improved = true;\n                } else {\n                    break;\n                }\n            }\n        } while (improved);\n        return Rect(x1, x2, y1, y2);\n    };\n\n    vector<Rect> candidates;\n\n    int min_x = 1000000, max_x = -1, min_y = 1000000, max_y = -1;\n    for (int i = 0; i < N; i++) {\n        min_x = min(min_x, mackerels[i].x);\n        max_x = max(max_x, mackerels[i].x);\n        min_y = min(min_y, mackerels[i].y);\n        max_y = max(max_y, mackerels[i].y);\n    }\n    candidates.emplace_back(min_x, max_x, min_y, max_y);\n\n    for (int i = 0; i < N; i++) {\n        int x = mackerels[i].x;\n        int y = mackerels[i].y;\n        int x1 = max(0, x - 5000);\n        int x2 = min(100000, x + 5000);\n        int y1 = max(0, y - 5000);\n        int y2 = min(100000, y + 5000);\n        candidates.emplace_back(x1, x2, y1, y2);\n    }\n\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n    uniform_int_distribution<int> dist(0, 100000);\n    for (int i = 0; i < 5000; i++) {\n        int x1 = dist(rng);\n        int x2 = dist(rng);\n        if (x1 > x2) swap(x1, x2);\n        int y1 = dist(rng);\n        int y2 = dist(rng);\n        if (y1 > y2) swap(y1, y2);\n        candidates.emplace_back(x1, x2, y1, y2);\n    }\n\n    int best_score = -1000000000;\n    Rect best_rect;\n\n    for (Rect r : candidates) {\n        Rect improved = greedy_improve(r);\n        int score = get_sum(improved.x1, improved.x2, improved.y1, improved.y2);\n        if (score > best_score) {\n            best_score = score;\n            best_rect = improved;\n        }\n    }\n\n    cout << 4 << '\\n';\n    cout << best_rect.x1 << \" \" << best_rect.y1 << '\\n';\n    cout << best_rect.x2 << \" \" << best_rect.y1 << '\\n';\n    cout << best_rect.x2 << \" \" << best_rect.y2 << '\\n';\n    cout << best_rect.x1 << \" \" << best_rect.y2 << '\\n';\n\n    return 0;\n}","ahc040":"#include <iostream>\n#include <vector>\n#include <tuple>\n#include <algorithm>\n#include <random>\n#include <chrono>\n#include <climits>\n#include <cmath>\n#include <cctype>\nusing namespace std;\n\nstruct Rect {\n    long long x, y, w, h;\n};\n\nstruct Instruction {\n    int p, r, b;\n    char d;\n};\n\ndouble rand_double(double min_val, double max_val, mt19937& rng) {\n    uniform_real_distribution<double> dist(min_val, max_val);\n    return dist(rng);\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    for (int i = 0; i < N; i++) {\n        long long wp, hp;\n        cin >> wp >> hp;\n        w_est[i] = static_cast<double>(wp);\n        h_est[i] = static_cast<double>(hp);\n    }\n\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n\n    const int M = 50;\n\n    for (int turn = 0; turn < T; turn++) {\n        vector<Instruction> best_instructions;\n        long long best_score = LLONG_MAX;\n\n        for (int cand_idx = 0; cand_idx < M; cand_idx++) {\n            vector<Instruction> inst_seq;\n            vector<Rect> layout;\n            long long cur_max_x = 0;\n            long long cur_max_y = 0;\n\n            vector<double> w_cand = w_est;\n            vector<double> h_cand = h_est;\n            if (cand_idx > 0) {\n                double noise_factor = 0.1 * (1.0 - static_cast<double>(cand_idx) / M);\n                for (int i = 0; i < N; i++) {\n                    double noise = 1.0 + rand_double(-noise_factor, noise_factor, rng);\n                    w_cand[i] *= noise;\n                    h_cand[i] *= noise;\n                }\n            }\n\n            bool valid_placement = true;\n            for (int i = 0; i < N; i++) {\n                vector<tuple<int, int, char, int, long long, long long, long long, long long>> options;\n\n                for (int rotation : {0, 1}) {\n                    double w_val = (rotation == 0) ? w_cand[i] : h_cand[i];\n                    double h_val = (rotation == 0) ? h_cand[i] : w_cand[i];\n                    long long w_rect = llround(w_val);\n                    long long h_rect = llround(h_val);\n                    w_rect = max(1LL, min(1000000000LL, w_rect));\n                    h_rect = max(1LL, min(1000000000LL, h_rect));\n\n                    vector<tuple<char, int>> dir_b_opts;\n                    dir_b_opts.push_back({'U', -1});\n                    if (!layout.empty()) {\n                        dir_b_opts.push_back({'U', static_cast<int>(layout.size() - 1)});\n                    }\n                    dir_b_opts.push_back({'L', -1});\n                    if (!layout.empty()) {\n                        dir_b_opts.push_back({'L', static_cast<int>(layout.size() - 1)});\n                    }\n\n                    for (auto [d, b] : dir_b_opts) {\n                        long long x_fixed, y_fixed;\n                        if (d == 'U') {\n                            if (b == -1) {\n                                x_fixed = 0;\n                            } else {\n                                x_fixed = layout[b].x + layout[b].w;\n                            }\n                            y_fixed = 0;\n                            for (const auto& rect : layout) {\n                                if (rect.x < x_fixed + w_rect && rect.x + rect.w > x_fixed) {\n                                    y_fixed = max(y_fixed, rect.y + rect.h);\n                                }\n                            }\n                        } else {\n                            if (b == -1) {\n                                y_fixed = 0;\n                            } else {\n                                y_fixed = layout[b].y + layout[b].h;\n                            }\n                            x_fixed = 0;\n                            for (const auto& rect : layout) {\n                                if (rect.y < y_fixed + h_rect && rect.y + rect.h > y_fixed) {\n                                    x_fixed = max(x_fixed, rect.x + rect.w);\n                                }\n                            }\n                        }\n                        long long new_max_x = max(cur_max_x, x_fixed + w_rect);\n                        long long new_max_y = max(cur_max_y, y_fixed + h_rect);\n                        long long score_val = new_max_x + new_max_y;\n                        options.emplace_back(rotation, b, d, score_val, x_fixed, y_fixed, w_rect, h_rect);\n                    }\n                }\n\n                if (options.empty()) {\n                    valid_placement = false;\n                    break;\n                }\n\n                auto best_opt = *min_element(options.begin(), options.end(),\n                    [](const auto& a, const auto& b) {\n                        return get<3>(a) < get<3>(b);\n                    });\n\n                int r = get<0>(best_opt);\n                int b_val = get<1>(best_opt);\n                char d_val = get<2>(best_opt);\n                long long x_val = get<4>(best_opt);\n                long long y_val = get<5>(best_opt);\n                long long w_val = get<6>(best_opt);\n                long long h_val = get<7>(best_opt);\n\n                inst_seq.push_back({i, r, b_val, d_val});\n                layout.push_back({x_val, y_val, w_val, h_val});\n                cur_max_x = max(cur_max_x, x_val + w_val);\n                cur_max_y = max(cur_max_y, y_val + h_val);\n            }\n\n            if (!valid_placement) {\n                continue;\n            }\n\n            long long total_est = cur_max_x + cur_max_y;\n            if (total_est < best_score) {\n                best_score = total_est;\n                best_instructions = inst_seq;\n            }\n        }\n\n        if (best_instructions.empty()) {\n            for (int i = 0; i < N; i++) {\n                best_instructions.push_back({i, 0, -1, 'U'});\n            }\n        }\n\n        cout << N << \"\\n\";\n        for (const auto& inst : best_instructions) {\n            cout << inst.p << \" \" << inst.r << \" \" << inst.d << \" \" << inst.b << \"\\n\";\n        }\n        cout.flush();\n\n        long long W_obs, H_obs;\n        cin >> W_obs >> H_obs;\n\n        double total_est_val = static_cast<double>(best_score);\n        double total_obs_val = static_cast<double>(W_obs) + static_cast<double>(H_obs);\n        double scale = 1.0;\n        if (total_est_val > 1e-9) {\n            scale = total_obs_val / total_est_val;\n        }\n\n        for (int i = 0; i < N; i++) {\n            w_est[i] *= scale;\n            h_est[i] *= scale;\n            if (w_est[i] < 1.0) w_est[i] = 1.0;\n            if (w_est[i] > 1e9) w_est[i] = 1e9;\n            if (h_est[i] < 1.0) h_est[i] = 1.0;\n            if (h_est[i] > 1e9) h_est[i] = 1e9;\n        }\n    }\n\n    return 0;\n}","ahc041":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, H_val;\n    cin >> N >> M >> H_val;\n    const int max_layer = H_val + 1;\n\n    vector<long long> A(N);\n    for (int i = 0; i < N; i++) {\n        cin >> A[i];\n    }\n\n    vector<vector<int>> graph(N);\n    for (int i = 0; i < M; i++) {\n        int u, v;\n        cin >> u >> v;\n        graph[u].push_back(v);\n        graph[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> indices(N);\n    iota(indices.begin(), indices.end(), 0);\n    sort(indices.begin(), indices.end(), [&](int i, int j) {\n        return A[i] < A[j];\n    });\n\n    vector<int> layer(N, -1);\n    vector<int> parent(N, -1);\n\n    for (int v : indices) {\n        for (int d = max_layer; d >= 1; d--) {\n            if (d == 1) {\n                layer[v] = 1;\n                parent[v] = -1;\n                break;\n            }\n            for (int u : graph[v]) {\n                if (layer[u] == d - 1) {\n                    layer[v] = d;\n                    parent[v] = u;\n                    break;\n                }\n            }\n            if (layer[v] != -1) {\n                break;\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 <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int N;\n    cin >> N;\n    vector<string> grid(N);\n    for (int i = 0; i < N; i++) {\n        cin >> grid[i];\n    }\n\n    vector<pair<char, int>> ops;\n\n    while (true) {\n        int best_i = -1, best_j = -1;\n        char best_dir = ' ';\n        int best_p = -1;\n        int min_shifts = 100000000;\n\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                if (grid[i][j] != 'x') continue;\n\n                bool safe = true;\n                for (int k = 0; k < i; k++) {\n                    if (grid[k][j] == 'o') {\n                        safe = false;\n                        break;\n                    }\n                }\n                if (safe) {\n                    int shifts = i + 1;\n                    if (shifts < min_shifts) {\n                        min_shifts = shifts;\n                        best_dir = 'U';\n                        best_p = j;\n                        best_i = i;\n                        best_j = j;\n                    }\n                }\n\n                safe = true;\n                for (int k = i + 1; k < N; k++) {\n                    if (grid[k][j] == 'o') {\n                        safe = false;\n                        break;\n                    }\n                }\n                if (safe) {\n                    int shifts = N - i;\n                    if (shifts < min_shifts) {\n                        min_shifts = shifts;\n                        best_dir = 'D';\n                        best_p = j;\n                        best_i = i;\n                        best_j = j;\n                    }\n                }\n\n                safe = true;\n                for (int k = 0; k < j; k++) {\n                    if (grid[i][k] == 'o') {\n                        safe = false;\n                        break;\n                    }\n                }\n                if (safe) {\n                    int shifts = j + 1;\n                    if (shifts < min_shifts) {\n                        min_shifts = shifts;\n                        best_dir = 'L';\n                        best_p = i;\n                        best_i = i;\n                        best_j = j;\n                    }\n                }\n\n                safe = true;\n                for (int k = j + 1; k < N; k++) {\n                    if (grid[i][k] == 'o') {\n                        safe = false;\n                        break;\n                    }\n                }\n                if (safe) {\n                    int shifts = N - j;\n                    if (shifts < min_shifts) {\n                        min_shifts = shifts;\n                        best_dir = 'R';\n                        best_p = i;\n                        best_i = i;\n                        best_j = j;\n                    }\n                }\n            }\n        }\n\n        if (best_i == -1) {\n            break;\n        }\n\n        for (int s = 0; s < min_shifts; s++) {\n            ops.push_back({best_dir, best_p});\n            if (best_dir == 'U') {\n                for (int i = 0; i < N - 1; i++) {\n                    grid[i][best_p] = grid[i + 1][best_p];\n                }\n                grid[N - 1][best_p] = '.';\n            } else if (best_dir == 'D') {\n                for (int i = N - 1; i > 0; i--) {\n                    grid[i][best_p] = grid[i - 1][best_p];\n                }\n                grid[0][best_p] = '.';\n            } else if (best_dir == 'L') {\n                for (int j = 0; j < N - 1; j++) {\n                    grid[best_p][j] = grid[best_p][j + 1];\n                }\n                grid[best_p][N - 1] = '.';\n            } else if (best_dir == 'R') {\n                for (int j = N - 1; j > 0; j--) {\n                    grid[best_p][j] = grid[best_p][j - 1];\n                }\n                grid[best_p][0] = '.';\n            }\n        }\n    }\n\n    for (auto &op : ops) {\n        cout << op.first << \" \" << op.second << \"\\n\";\n    }\n\n    return 0;\n}","ahc044":"#include <iostream>\n#include <vector>\n#include <cstdlib>\n#include <cmath>\n#include <algorithm>\nusing namespace std;\n\nlong simulate(const vector<int>& a, const vector<int>& b, const vector<long>& T, int L, int N) {\n    vector<long> count(N, 0);\n    count[0] = 1;\n    int current = 0;\n    for (int week = 1; week < L; week++) {\n        long t = count[current];\n        int next;\n        if (t & 1) {\n            next = a[current];\n        } else {\n            next = b[current];\n        }\n        count[next]++;\n        current = next;\n    }\n    long error = 0;\n    for (int j = 0; j < N; j++) {\n        error += abs(count[j] - T[j]);\n    }\n    return error;\n}\n\nint main() {\n    const int N = 100;\n    int L;\n    cin >> N >> L;\n    vector<long> T(N);\n    for (int i = 0; i < N; i++) {\n        cin >> T[i];\n    }\n\n    vector<long> target(N);\n    for (int j = 0; j < N; j++) {\n        target[j] = 2 * T[j];\n    }\n\n    vector<long> S(N, 0);\n    vector<long> rem = target;\n    vector<int> a(N), b(N);\n\n    for (int i = 0; i < N; i++) {\n        int j1 = 0;\n        for (int j = 1; j < N; j++) {\n            if (rem[j] > rem[j1]) {\n                j1 = j;\n            }\n        }\n        int j2 = 0;\n        for (int j = 1; j < N; j++) {\n            if (rem[j] > rem[j2] && j != j1) {\n                j2 = j;\n            }\n        }\n        if (rem[j1] == rem[j2]) {\n            if (j2 == 0 && j1 != 0) {\n                j2 = j1;\n            } else if (j1 == j2) {\n                j2 = (j1 + 1) % N;\n            }\n        }\n        if (rem[j2] < rem[j1]) {\n            j2 = j1;\n        }\n        a[i] = j1;\n        b[i] = j2;\n        S[j1] += T[i];\n        S[j2] += T[i];\n        rem[j1] = target[j1] - S[j1];\n        rem[j2] = target[j2] - S[j2];\n    }\n\n    long best_error = simulate(a, b, T, L, N);\n    vector<int> best_a = a;\n    vector<int> best_b = b;\n\n    for (int i = 0; i < N; i++) {\n        swap(a[i], b[i]);\n        long error = simulate(a, b, T, L, N);\n        if (error < best_error) {\n            best_error = error;\n            best_a = a;\n            best_b = b;\n        } else {\n            swap(a[i], b[i]);\n        }\n    }\n\n    for (int i = 0; i < N; i++) {\n        cout << best_a[i] << \" \" << best_b[i] << '\\n';\n    }\n\n    return 0;\n}","ahc045":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, Q, L, W;\n    cin >> N >> M >> Q >> L >> W;\n\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    for (int i = 0; i < N; i++) {\n        cin >> lx[i] >> rx[i] >> ly[i] >> ry[i];\n    }\n\n    vector<int> cx2(N), cy2(N);\n    for (int i = 0; i < N; i++) {\n        cx2[i] = lx[i] + rx[i];\n        cy2[i] = ly[i] + ry[i];\n    }\n\n    vector<int> cities(N);\n    iota(cities.begin(), cities.end(), 0);\n    sort(cities.begin(), cities.end(), [&](int i, int j) {\n        if (cx2[i] != cx2[j]) \n            return cx2[i] < cx2[j];\n        if (cy2[i] != cy2[j])\n            return cy2[i] < cy2[j];\n        return i < j;\n    });\n\n    vector<vector<int>> groups;\n    int start = 0;\n    for (int i = 0; i < M; i++) {\n        int g = G[i];\n        groups.push_back(vector<int>(cities.begin() + start, cities.begin() + start + g));\n        start += g;\n    }\n\n    vector<vector<pair<int, int>>> group_edges(M);\n\n    for (int k = 0; k < M; k++) {\n        int g = groups[k].size();\n        for (int i = 0; i < g; ) {\n            if (i + 2 < g) {\n                cout << \"? 3\";\n                cout << \" \" << groups[k][i];\n                cout << \" \" << groups[k][i+1];\n                cout << \" \" << groups[k][i+2] << endl;\n                cout.flush();\n\n                vector<pair<int, int>> edges;\n                for (int j = 0; j < 2; j++) {\n                    int u, v;\n                    cin >> u >> v;\n                    edges.push_back({u, v});\n                }\n                for (auto &e : edges) {\n                    group_edges[k].push_back(e);\n                }\n                i += 2;\n            } else {\n                if (i + 1 < g) {\n                    group_edges[k].push_back({groups[k][i], groups[k][i+1]});\n                }\n                break;\n            }\n        }\n    }\n\n    cout << \"!\" << endl;\n    for (int k = 0; k < M; k++) {\n        for (int i = 0; i < groups[k].size(); i++) {\n            if (i > 0) cout << \" \";\n            cout << groups[k][i];\n        }\n        cout << endl;\n\n        for (auto &e : group_edges[k]) {\n            cout << e.first << \" \" << e.second << endl;\n        }\n    }\n    cout.flush();\n\n    return 0;\n}","ahc046":"#include <iostream>\n#include <vector>\n#include <cstdlib>\nusing namespace std;\n\nint main() {\n    int N, M;\n    cin >> N >> M;\n    vector<pair<int, int>> points(M);\n    for (int i = 0; i < M; i++) {\n        cin >> points[i].first >> points[i].second;\n    }\n\n    for (int idx = 0; idx < M-1; idx++) {\n        int ci = points[idx].first;\n        int cj = points[idx].second;\n        int ti = points[idx+1].first;\n        int tj = points[idx+1].second;\n\n        int dr = ti - ci;\n        int dc = tj - cj;\n\n        if (ci == ti) {\n            if (cj < tj) {\n                int d_direct = tj - cj;\n                int d_alt = 1 + (N - 1 - tj);\n                if (d_alt < d_direct) {\n                    cout << \"S R\\n\";\n                    for (int k = 0; k < N-1-tj; k++) {\n                        cout << \"M L\\n\";\n                    }\n                } else {\n                    for (int k = 0; k < d_direct; k++) {\n                        cout << \"M R\\n\";\n                    }\n                }\n            } else if (cj > tj) {\n                int d_direct = cj - tj;\n                int d_alt = 1 + tj;\n                if (d_alt < d_direct) {\n                    cout << \"S L\\n\";\n                    for (int k = 0; k < tj; k++) {\n                        cout << \"M R\\n\";\n                    }\n                } else {\n                    for (int k = 0; k < d_direct; k++) {\n                        cout << \"M L\\n\";\n                    }\n                }\n            }\n        }\n        else if (cj == tj) {\n            if (ci < ti) {\n                int d_direct = ti - ci;\n                int d_alt = 1 + (N - 1 - ti);\n                if (d_alt < d_direct) {\n                    cout << \"S D\\n\";\n                    for (int k = 0; k < N-1-ti; k++) {\n                        cout << \"M U\\n\";\n                    }\n                } else {\n                    for (int k = 0; k < d_direct; k++) {\n                        cout << \"M D\\n\";\n                    }\n                }\n            } else if (ci > ti) {\n                int d_direct = ci - ti;\n                int d_alt = 1 + ti;\n                if (d_alt < d_direct) {\n                    cout << \"S U\\n\";\n                    for (int k = 0; k < ti; k++) {\n                        cout << \"M D\\n\";\n                    }\n                } else {\n                    for (int k = 0; k < d_direct; k++) {\n                        cout << \"M U\\n\";\n                    }\n                }\n            }\n        }\n        else {\n            if (abs(dr) >= abs(dc)) {\n                if (dr > 0) {\n                    int d_direct = dr;\n                    int d_alt = 1 + (N - 1 - ti);\n                    if (d_alt < d_direct) {\n                        cout << \"S D\\n\";\n                        for (int k = 0; k < N-1-ti; k++) {\n                            cout << \"M U\\n\";\n                        }\n                    } else {\n                        for (int k = 0; k < d_direct; k++) {\n                            cout << \"M D\\n\";\n                        }\n                    }\n                } else {\n                    int d_direct = -dr;\n                    int d_alt = 1 + ti;\n                    if (d_alt < d_direct) {\n                        cout << \"S U\\n\";\n                        for (int k = 0; k < ti; k++) {\n                            cout << \"M D\\n\";\n                        }\n                    } else {\n                        for (int k = 0; k < d_direct; k++) {\n                            cout << \"M U\\n\";\n                        }\n                    }\n                }\n                if (cj < tj) {\n                    int d_direct = tj - cj;\n                    int d_alt = 1 + (N - 1 - tj);\n                    if (d_alt < d_direct) {\n                        cout << \"S R\\n\";\n                        for (int k = 0; k < N-1-tj; k++) {\n                            cout << \"M L\\n\";\n                        }\n                    } else {\n                        for (int k = 0; k < d_direct; k++) {\n                            cout << \"M R\\n\";\n                        }\n                    }\n                } else {\n                    int d_direct = cj - tj;\n                    int d_alt = 1 + tj;\n                    if (d_alt < d_direct) {\n                        cout << \"S L\\n\";\n                        for (int k = 0; k < tj; k++) {\n                            cout << \"M R\\n\";\n                        }\n                    } else {\n                        for (int k = 0; k < d_direct; k++) {\n                            cout << \"M L\\n\";\n                        }\n                    }\n                }\n            } else {\n                if (dc > 0) {\n                    int d_direct = dc;\n                    int d_alt = 1 + (N - 1 - tj);\n                    if (d_alt < d_direct) {\n                        cout << \"S R\\n\";\n                        for (int k = 0; k < N-1-tj; k++) {\n                            cout << \"M L\\n\";\n                        }\n                    } else {\n                        for (int k = 0; k < d_direct; k++) {\n                            cout << \"M R\\n\";\n                        }\n                    }\n                } else {\n                    int d_direct = -dc;\n                    int d_alt = 1 + tj;\n                    if (d_alt < d_direct) {\n                        cout << \"S L\\n\";\n                        for (int k = 0; k < tj; k++) {\n                            cout << \"M R\\n\";\n                        }\n                    } else {\n                        for (int k = 0; k < d_direct; k++) {\n                            cout << \"M L\\n\";\n                        }\n                    }\n                }\n                if (ci < ti) {\n                    int d_direct = ti - ci;\n                    int d_alt = 1 + (N - 1 - ti);\n                    if (d_alt < d_direct) {\n                        cout << \"S D\\n\";\n                        for (int k = 0; k < N-1-ti; k++) {\n                            cout << \"M U\\n\";\n                        }\n                    } else {\n                        for (int k = 0; k < d_direct; k++) {\n                            cout << \"M D\\n\";\n                        }\n                    }\n                } else {\n                    int d_direct = ci - ti;\n                    int d_alt = 1 + ti;\n                    if (d_alt < d_direct) {\n                        cout << \"S U\\n\";\n                        for (int k = 0; k < ti; k++) {\n                            cout << \"M D\\n\";\n                        }\n                    } else {\n                        for (int k = 0; k < d_direct; k++) {\n                            cout << \"M U\\n\";\n                        }\n                    }\n                }\n            }\n        }\n    }\n\n    return 0;\n}"},"4":{"ahc001":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int n;\n    cin >> n;\n    vector<tuple<int, int, int, int>> companies;\n    for (int i = 0; i < n; i++) {\n        int x, y, r;\n        cin >> x >> y >> r;\n        companies.emplace_back(x, y, r, i);\n    }\n\n    vector<vector<pair<int, int>>> all_candidates(n);\n    for (int i = 0; i < n; i++) {\n        auto [x, y, r_val, idx] = companies[i];\n        vector<pair<int, int>> cands;\n        int sw = max(1, (int)sqrt(r_val) - 20);\n        int ew = min(10000, (int)sqrt(r_val) + 20);\n        for (int w = sw; w <= ew; w++) {\n            int h1 = r_val / w;\n            int h2 = (r_val + w - 1) / w;\n            vector<int> hs = {h1 - 2, h1 - 1, h1, h1 + 1, h1 + 2, h2 - 2, h2 - 1, h2, h2 + 1, h2 + 2};\n            for (int h : hs) {\n                if (h < 1) h = 1;\n                if (h > 10000) h = 10000;\n                int area = w * h;\n                cands.emplace_back(w, h);\n                if (w != h) {\n                    cands.emplace_back(h, w);\n                }\n            }\n        }\n        cands.emplace_back(1, 1);\n        sort(cands.begin(), cands.end(), [&](const auto& a, const auto& b) {\n            int err_a = abs(a.first * a.second - r_val);\n            int err_b = abs(b.first * b.second - r_val);\n            if (err_a != err_b) {\n                return err_a < err_b;\n            }\n            return a.first * a.second < b.first * b.second;\n        });\n        auto last = unique(cands.begin(), cands.end(), [](const auto& a, const auto& b) {\n            return a.first == b.first && a.second == b.second;\n        });\n        cands.erase(last, cands.end());\n        if (cands.size() > 40) {\n            cands.resize(40);\n        }\n        all_candidates[i] = cands;\n    }\n\n    sort(companies.begin(), companies.end(), [](const auto& a, const auto& b) {\n        return get<2>(a) > get<2>(b);\n    });\n\n    vector<array<int, 4>> result(n);\n    vector<array<int, 4>> placed;\n\n    vector<int> offsets = {-3, -2, -1, 0, 1, 2, 3};\n\n    for (auto& comp : companies) {\n        int x = get<0>(comp);\n        int y = get<1>(comp);\n        int r_val = get<2>(comp);\n        int idx = get<3>(comp);\n        bool found = false;\n        for (auto [w, h] : all_candidates[idx]) {\n            int Lx = max(0, x + 1 - w);\n            int Rx = min(x, 10000 - w);\n            int Ly = max(0, y + 1 - h);\n            int Ry = min(y, 10000 - h);\n            if (Lx > Rx || Ly > Ry) {\n                continue;\n            }\n\n            double center_x = x + 0.5;\n            double center_y = y + 0.5;\n            double ideal_a = center_x - w / 2.0;\n            double ideal_b = center_y - h / 2.0;\n\n            for (int dx : offsets) {\n                int a_candidate = static_cast<int>(round(ideal_a)) + dx;\n                if (a_candidate < Lx) a_candidate = Lx;\n                if (a_candidate > Rx) a_candidate = Rx;\n                for (int dy : offsets) {\n                    int b_candidate = static_cast<int>(round(ideal_b)) + dy;\n                    if (b_candidate < Ly) b_candidate = Ly;\n                    if (b_candidate > Ry) b_candidate = Ry;\n\n                    bool valid = true;\n                    for (const auto& rect : placed) {\n                        if (max(a_candidate, rect[0]) < min(a_candidate + w, rect[2]) &&\n                            max(b_candidate, rect[1]) < min(b_candidate + h, rect[3])) {\n                            valid = false;\n                            break;\n                        }\n                    }\n                    if (valid) {\n                        result[idx] = {a_candidate, b_candidate, a_candidate + w, b_candidate + h};\n                        placed.push_back({a_candidate, b_candidate, a_candidate + w, b_candidate + h});\n                        found = true;\n                        break;\n                    }\n                }\n                if (found) break;\n            }\n            if (found) break;\n        }\n        if (!found) {\n            result[idx] = {x, y, x + 1, y + 1};\n            placed.push_back({x, y, x + 1, y + 1});\n        }\n    }\n\n    for (int i = 0; i < n; i++) {\n        auto [a, b, c, d] = result[i];\n        cout << a << \" \" << b << \" \" << c << \" \" << d << '\\n';\n    }\n\n    return 0;\n}","ahc002":"#include <bits/stdc++.h>\n#include <boost/dynamic_bitset.hpp>\nusing namespace std;\n\nstruct PathNode {\n    char dir;\n    shared_ptr<PathNode> prev;\n    PathNode(char d, shared_ptr<PathNode> p) : dir(d), prev(p) {}\n};\n\nstruct State {\n    int i, j;\n    boost::dynamic_bitset<> visited;\n    long long score;\n    shared_ptr<PathNode> path_tail;\n    long long value;\n\n    State(int i, int j, int M) : i(i), j(j), visited(M), score(0), path_tail(nullptr), value(0) {}\n};\n\nstring get_path(shared_ptr<PathNode> tail) {\n    if (!tail) return \"\";\n    string s;\n    while (tail) {\n        s += tail->dir;\n        tail = tail->prev;\n    }\n    reverse(s.begin(), s.end());\n    return s;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\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\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    const vector<tuple<int, int, char>> dirs = {\n        {-1, 0, 'U'},\n        {1, 0, 'D'},\n        {0, -1, 'L'},\n        {0, 1, 'R'}\n    };\n\n    const int BEAM_WIDTH = 1000;\n    vector<State> current_beam;\n    State start_state(si, sj, M);\n    start_state.visited.set(t[si][sj]);\n    start_state.score = p[si][sj];\n\n    // Compute initial potential\n    vector<int> neighbors;\n    for (const auto& [dx, dy, _] : dirs) {\n        int ni = si + dx;\n        int nj = sj + dy;\n        if (ni >= 0 && ni < 50 && nj >= 0 && nj < 50) {\n            if (!start_state.visited[t[ni][nj]]) {\n                neighbors.push_back(p[ni][nj]);\n            }\n        }\n    }\n    sort(neighbors.rbegin(), neighbors.rend());\n    int potential = 0;\n    for (int k = 0; k < min(3, (int)neighbors.size()); k++) {\n        potential += neighbors[k] * (1.0 / (k + 1));\n    }\n    start_state.value = start_state.score + potential;\n\n    current_beam.push_back(start_state);\n\n    long long best_score = start_state.score;\n    string best_path = \"\";\n\n    while (!current_beam.empty()) {\n        vector<State> next_beam;\n        next_beam.reserve(current_beam.size() * 4);\n\n        for (State& state : current_beam) {\n            for (const auto& [dx, dy, dchar] : dirs) {\n                int ni = state.i + dx;\n                int nj = state.j + dy;\n                if (ni < 0 || ni >= 50 || nj < 0 || nj >= 50) continue;\n\n                int tile_id = t[ni][nj];\n                if (state.visited.test(tile_id)) continue;\n\n                State new_state(ni, nj, M);\n                new_state.visited = state.visited;\n                new_state.visited.set(tile_id);\n                new_state.score = state.score + p[ni][nj];\n\n                // Compute potential for new_state\n                vector<int> neighbors;\n                for (const auto& [dx2, dy2, _] : dirs) {\n                    int ni2 = ni + dx2;\n                    int nj2 = nj + dy2;\n                    if (ni2 >= 0 && ni2 < 50 && nj2 >= 0 && nj2 < 50) {\n                        int tid = t[ni2][nj2];\n                        if (!new_state.visited[tid]) {\n                            neighbors.push_back(p[ni2][nj2]);\n                        }\n                    }\n                }\n                sort(neighbors.rbegin(), neighbors.rend());\n                int potential_val = 0;\n                for (int k = 0; k < min(3, (int)neighbors.size()); k++) {\n                    potential_val += neighbors[k] * (1.0 / (k + 1));\n                }\n                new_state.value = new_state.score + potential_val;\n\n                new_state.path_tail = make_shared<PathNode>(dchar, state.path_tail);\n\n                if (new_state.score > best_score) {\n                    best_score = new_state.score;\n                    best_path = get_path(new_state.path_tail);\n                }\n\n                next_beam.push_back(move(new_state));\n            }\n        }\n\n        if (next_beam.empty()) break;\n\n        // Sort by value descending, then by score descending\n        sort(next_beam.begin(), next_beam.end(), [](const State& a, const State& b) {\n            if (a.value != b.value) return a.value > b.value;\n            return a.score > b.score;\n        });\n\n        if ((int)next_beam.size() > BEAM_WIDTH) {\n            current_beam.assign(next_beam.begin(), next_beam.begin() + BEAM_WIDTH);\n        } else {\n            current_beam = move(next_beam);\n        }\n    }\n\n    cout << best_path << '\\n';\n    return 0;\n}","ahc003":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    const int N = 30;\n    double W_h[N][N-1];\n    double W_v[N-1][N];\n\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N-1; j++) {\n            W_h[i][j] = 5000.0;\n        }\n    }\n    for (int i = 0; i < N-1; i++) {\n        for (int j = 0; j < N; j++) {\n            W_v[i][j] = 5000.0;\n        }\n    }\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, 1e18));\n        vector<vector<int>> parent_i(N, vector<int>(N, -1));\n        vector<vector<int>> parent_j(N, vector<int>(N, -1));\n\n        priority_queue<tuple<double, int, int>, vector<tuple<double, int, int>>, greater<tuple<double, int, int>>> pq;\n        dist[si][sj] = 0.0;\n        pq.push(make_tuple(0.0, si, sj));\n\n        const int di[4] = {-1, 1, 0, 0};\n        const int dj[4] = {0, 0, -1, 1};\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 + di[dir];\n                int nj = j + dj[dir];\n                if (ni < 0 || ni >= N || nj < 0 || nj >= N) continue;\n\n                double weight;\n                if (dir == 0) {\n                    weight = W_v[i-1][j];\n                } else if (dir == 1) {\n                    weight = W_v[i][j];\n                } else if (dir == 2) {\n                    weight = W_h[i][j-1];\n                } else {\n                    weight = W_h[i][j];\n                }\n\n                double nd = d + weight;\n                if (nd < dist[ni][nj]) {\n                    dist[ni][nj] = nd;\n                    parent_i[ni][nj] = i;\n                    parent_j[ni][nj] = j;\n                    pq.push(make_tuple(nd, ni, nj));\n                }\n            }\n        }\n\n        string path_str = \"\";\n        vector<tuple<int, int, int>> edges_list;\n        int i = ti, j = tj;\n        while (i != si || j != sj) {\n            int pi = parent_i[i][j];\n            int pj = parent_j[i][j];\n            if (pi == i-1) {\n                path_str += 'D';\n            } else if (pi == i+1) {\n                path_str += 'U';\n            } else if (pj == j-1) {\n                path_str += 'R';\n            } else if (pj == j+1) {\n                path_str += 'L';\n            }\n\n            if (pi == i-1) {\n                edges_list.push_back(make_tuple(0, i-1, j));\n            } else if (pi == i+1) {\n                edges_list.push_back(make_tuple(0, i, j));\n            } else if (pj == j-1) {\n                edges_list.push_back(make_tuple(1, i, j-1));\n            } else if (pj == j+1) {\n                edges_list.push_back(make_tuple(1, i, j));\n            }\n\n            i = pi;\n            j = pj;\n        }\n        reverse(path_str.begin(), path_str.end());\n\n        cout << path_str << \"\\n\";\n        cout.flush();\n\n        long long r;\n        cin >> r;\n\n        int L = path_str.size();\n        double est_length = dist[ti][tj];\n        double error = static_cast<double>(r) - est_length;\n\n        double lr = 0.1 / (k + 1);\n        for (auto &edge : edges_list) {\n            int type = get<0>(edge);\n            int idx1 = get<1>(edge);\n            int idx2 = get<2>(edge);\n            double step = lr * error / static_cast<double>(L);\n\n            if (type == 0) {\n                W_v[idx1][idx2] += step;\n                if (W_v[idx1][idx2] < 1000.0) W_v[idx1][idx2] = 1000.0;\n                else if (W_v[idx1][idx2] > 9000.0) W_v[idx1][idx2] = 9000.0;\n            } else {\n                W_h[idx1][idx2] += step;\n                if (W_h[idx1][idx2] < 1000.0) W_h[idx1][idx2] = 1000.0;\n                else if (W_h[idx1][idx2] > 9000.0) W_h[idx1][idx2] = 9000.0;\n            }\n        }\n    }\n\n    return 0;\n}","ahc004":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int N, M;\n    cin >> N >> M;\n    vector<string> strs(M);\n    for (int i = 0; i < M; i++) {\n        cin >> strs[i];\n    }\n\n    vector<int> indices(M);\n    iota(indices.begin(), indices.end(), 0);\n    sort(indices.begin(), indices.end(), [&](int i, int j) {\n        return strs[i].size() > strs[j].size();\n    });\n\n    vector<string> mat(N, string(N, '.'));\n    vector<bool> covered(M, false);\n    int covered_count = 0;\n\n    for (int idx : indices) {\n        string& s = strs[idx];\n        int L = s.size();\n        int best_dots = -1;\n        int best_dir = -1;\n        int best_i = -1;\n        int best_j = -1;\n\n        for (int i = 0; i < N; i++) {\n            for (int sc = 0; sc < N; sc++) {\n                bool ok = true;\n                int dots = 0;\n                for (int p = 0; p < L; p++) {\n                    int col = (sc + p) % N;\n                    if (mat[i][col] != '.' && mat[i][col] != s[p]) {\n                        ok = false;\n                        break;\n                    }\n                    if (mat[i][col] == '.') {\n                        dots++;\n                    }\n                }\n                if (ok && dots > best_dots) {\n                    best_dots = dots;\n                    best_dir = 0;\n                    best_i = i;\n                    best_j = sc;\n                }\n            }\n        }\n\n        for (int j = 0; j < N; j++) {\n            for (int sr = 0; sr < N; sr++) {\n                bool ok = true;\n                int dots = 0;\n                for (int p = 0; p < L; p++) {\n                    int row = (sr + p) % N;\n                    if (mat[row][j] != '.' && mat[row][j] != s[p]) {\n                        ok = false;\n                        break;\n                    }\n                    if (mat[row][j] == '.') {\n                        dots++;\n                    }\n                }\n                if (ok && dots > best_dots) {\n                    best_dots = dots;\n                    best_dir = 1;\n                    best_i = sr;\n                    best_j = j;\n                }\n            }\n        }\n\n        if (best_dots != -1) {\n            covered[idx] = true;\n            covered_count++;\n            if (best_dir == 0) {\n                for (int p = 0; p < L; p++) {\n                    int col = (best_j + p) % N;\n                    mat[best_i][col] = s[p];\n                }\n            } else {\n                for (int p = 0; p < L; p++) {\n                    int row = (best_i + p) % N;\n                    mat[row][best_j] = s[p];\n                }\n            }\n        }\n    }\n\n    bool improved;\n    do {\n        improved = false;\n        for (int idx = 0; idx < M && !improved; idx++) {\n            if (covered[idx]) continue;\n            string& s = strs[idx];\n            int L = s.size();\n\n            for (int i = 0; i < N && !improved; i++) {\n                for (int sc = 0; sc < N && !improved; sc++) {\n                    bool ok = true;\n                    for (int p = 0; p < L; p++) {\n                        int col = (sc + p) % N;\n                        if (mat[i][col] != '.' && mat[i][col] != s[p]) {\n                            ok = false;\n                            break;\n                        }\n                    }\n                    if (ok) {\n                        for (int p = 0; p < L; p++) {\n                            int col = (sc + p) % N;\n                            mat[i][col] = s[p];\n                        }\n                        covered[idx] = true;\n                        covered_count++;\n                        improved = true;\n                    }\n                }\n            }\n\n            if (improved) continue;\n\n            for (int j = 0; j < N && !improved; j++) {\n                for (int sr = 0; sr < N && !improved; sr++) {\n                    bool ok = true;\n                    for (int p = 0; p < L; p++) {\n                        int row = (sr + p) % N;\n                        if (mat[row][j] != '.' && mat[row][j] != s[p]) {\n                            ok = false;\n                            break;\n                        }\n                    }\n                    if (ok) {\n                        for (int p = 0; p < L; p++) {\n                            int row = (sr + p) % N;\n                            mat[row][j] = s[p];\n                        }\n                        covered[idx] = true;\n                        covered_count++;\n                        improved = true;\n                    }\n                }\n            }\n        }\n    } while (improved);\n\n    if (covered_count == M) {\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    }\n\n    for (int i = 0; i < N; i++) {\n        cout << mat[i] << '\\n';\n    }\n    return 0;\n}","ahc005":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MAX_R = 5000;\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, si, sj;\n    cin >> N >> si >> sj;\n    vector<string> grid(N);\n    for (int i = 0; i < N; i++) {\n        cin >> grid[i];\n    }\n\n    vector<vector<bool>> is_road(N, vector<bool>(N, false));\n    vector<vector<int>> time_grid(N, vector<int>(N, 0));\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (grid[i][j] != '#') {\n                is_road[i][j] = true;\n                time_grid[i][j] = grid[i][j] - '0';\n            }\n        }\n    }\n\n    vector<pair<int, int>> road_cells;\n    map<pair<int, int>, int> cell_to_index;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (is_road[i][j]) {\n                cell_to_index[{i, j}] = road_cells.size();\n                road_cells.push_back({i, j});\n            }\n        }\n    }\n    int R = road_cells.size();\n    if (R == 0) {\n        cout << \"\" << endl;\n        return 0;\n    }\n\n    vector<vector<bitset<MAX_R>>> visible_set(N, vector<bitset<MAX_R>>(N));\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (!is_road[i][j]) continue;\n            int idx_center = cell_to_index[{i, j}];\n            visible_set[i][j].set(idx_center);\n\n            for (int k = j-1; k >= 0; k--) {\n                if (grid[i][k] == '#') break;\n                int idx = cell_to_index[{i, k}];\n                visible_set[i][j].set(idx);\n            }\n            for (int k = j+1; k < N; k++) {\n                if (grid[i][k] == '#') break;\n                int idx = cell_to_index[{i, k}];\n                visible_set[i][j].set(idx);\n            }\n            for (int k = i-1; k >= 0; k--) {\n                if (grid[k][j] == '#') break;\n                int idx = cell_to_index[{k, j}];\n                visible_set[i][j].set(idx);\n            }\n            for (int k = i+1; k < N; k++) {\n                if (grid[k][j] == '#') break;\n                int idx = cell_to_index[{k, j}];\n                visible_set[i][j].set(idx);\n            }\n        }\n    }\n\n    bitset<MAX_R> base_coverage;\n    int cur_i = si, cur_j = sj;\n    if (is_road[cur_i][cur_j]) {\n        int start_idx = cell_to_index[{cur_i, cur_j}];\n        base_coverage.set(start_idx);\n        base_coverage |= visible_set[cur_i][cur_j];\n    }\n\n    string path_so_far = \"\";\n    int total_time_so_far = 0;\n\n    const int D = 4;\n    while (base_coverage.count() < R) {\n        auto cmp = [&](const tuple<int, int, int, bitset<MAX_R>, int, int>& a, \n                       const tuple<int, int, int, bitset<MAX_R>, int, int>& b) {\n            auto [i1, j1, time1, cov1, pi1, pj1] = a;\n            auto [i2, j2, time2, cov2, pi2, pj2] = b;\n            int inc1 = cov1.count() - (base_coverage & cov1).count();\n            int inc2 = cov2.count() - (base_coverage & cov2).count();\n            if (inc1 != inc2) {\n                return inc1 < inc2;\n            }\n            return time1 > time2;\n        };\n        priority_queue<tuple<int, int, int, bitset<MAX_R>, int, int>, \n                       vector<tuple<int, int, int, bitset<MAX_R>, int, int>>, \n                       decltype(cmp)> pq(cmp);\n\n        bitset<MAX_R> start_cov;\n        pq.emplace(cur_i, cur_j, 0, start_cov, -1, -1);\n\n        tuple<int, int, int, bitset<MAX_R>, int, int> best_found = pq.top();\n        int best_incremental = -1;\n\n        while (!pq.empty()) {\n            auto [i, j, time_so_far, cov, pi, pj] = pq.top();\n            pq.pop();\n            int inc = cov.count() - (base_coverage & cov).count();\n            if (inc > best_incremental || (inc == best_incremental && time_so_far < get<2>(best_found))) {\n                best_incremental = inc;\n                best_found = make_tuple(i, j, time_so_far, cov, pi, pj);\n            }\n            if (get<0>(best_found) == i && get<1>(best_found) == j && \n                get<2>(best_found) == time_so_far && \n                get<3>(best_found) == cov) {\n                if (best_incremental == 0) {\n                    break;\n                }\n            }\n\n            if (pi == -2) {\n                continue;\n            }\n            if (pi != -1) {\n            } else {\n                pi = -2;\n            }\n\n            for (int d = 0; d < 4; d++) {\n                int ni = i, nj = j;\n                if (d == 0) ni--;\n                else if (d == 1) ni++;\n                else if (d == 2) nj--;\n                else if (d == 3) nj++;\n\n                if (ni < 0 || ni >= N || nj < 0 || nj >= N) continue;\n                if (!is_road[ni][nj]) continue;\n\n                int new_time = time_so_far + time_grid[ni][nj];\n                bitset<MAX_R> new_cov = cov;\n                new_cov |= visible_set[ni][nj];\n\n                pq.emplace(ni, nj, new_time, new_cov, i, j);\n            }\n        }\n\n        if (best_incremental <= 0) {\n            break;\n        }\n\n        auto [i_best, j_best, time_best, cov_best, pi_best, pj_best] = best_found;\n        vector<pair<int, int>> path_to_best;\n        int cur_i_temp = i_best, cur_j_temp = j_best;\n        int prev_i = pi_best, prev_j = pj_best;\n        while (prev_i != -1 || prev_j != -1) {\n            path_to_best.push_back({cur_i_temp, cur_j_temp});\n            if (prev_i == -1 && prev_j == -1) break;\n            int next_i = prev_i;\n            int next_j = prev_j;\n            cur_i_temp = prev_i;\n            cur_j_temp = prev_j;\n            prev_i = (path_to_best.size() == 1) ? -1 : get<4>(pq.top()); \n            prev_j = (path_to_best.size() == 1) ? -1 : get<5>(pq.top());\n            pq.pop();\n        }\n        reverse(path_to_best.begin(), path_to_best.end());\n\n        if (path_to_best.empty()) {\n            break;\n        }\n\n        int next_i = path_to_best[0].first;\n        int next_j = path_to_best[0].second;\n\n        char dir;\n        if (next_i == cur_i - 1) dir = 'U';\n        else if (next_i == cur_i + 1) dir = 'D';\n        else if (next_j == cur_j - 1) dir = 'L';\n        else if (next_j == cur_j + 1) dir = 'R';\n        else {\n            dir = 'R';\n        }\n\n        path_so_far += dir;\n        total_time_so_far += time_grid[next_i][next_j];\n        base_coverage |= visible_set[next_i][next_j];\n        cur_i = next_i;\n        cur_j = next_j;\n\n        if (base_coverage.count() == R) {\n            break;\n        }\n    }\n\n    vector<vector<int>> dist(N, vector<int>(N, INT_MAX));\n    vector<vector<pair<int, int>>> parent(N, vector<pair<int, int>>(N, {-1, -1}));\n    priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<>> pq_dijk;\n    dist[cur_i][cur_j] = 0;\n    pq_dijk.emplace(0, cur_i, cur_j);\n\n    while (!pq_dijk.empty()) {\n        auto [d, i, j] = pq_dijk.top();\n        pq_dijk.pop();\n        if (d != dist[i][j]) continue;\n        if (i == si && j == sj) break;\n\n        int dx[] = {-1, 1, 0, 0};\n        int dy[] = {0, 0, -1, 1};\n        for (int k = 0; k < 4; k++) {\n            int ni = i + dx[k];\n            int nj = j + dy[k];\n            if (ni < 0 || ni >= N || nj < 0 || nj >= N) continue;\n            if (!is_road[ni][nj]) continue;\n            int nd = d + time_grid[ni][nj];\n            if (nd < dist[ni][nj]) {\n                dist[ni][nj] = nd;\n                parent[ni][nj] = {i, j};\n                pq_dijk.emplace(nd, ni, nj);\n            }\n        }\n    }\n\n    string return_path = \"\";\n    int i = si, j = sj;\n    while (i != cur_i || j != cur_j) {\n        auto [pi, pj] = parent[i][j];\n        if (pi == i-1) return_path += 'D';\n        else if (pi == i+1) return_path += 'U';\n        else if (pj == j-1) return_path += 'R';\n        else if (pj == j+1) return_path += 'L';\n        i = pi;\n        j = pj;\n    }\n    reverse(return_path.begin(), return_path.end());\n\n    string full_path = path_so_far + return_path;\n    cout << full_path << endl;\n\n    return 0;\n}","future-contest-2022-qual":"#include <bits/stdc++.h>\nusing namespace std;\n\nvoid update_skill(vector<int>& s, const vector<int>& d_task, int t_actual, int K) {\n    int low_bound, high_bound;\n    if (t_actual == 1) {\n        low_bound = 0;\n        high_bound = 3;\n    } else {\n        low_bound = max(1, t_actual - 3);\n        high_bound = t_actual + 3;\n    }\n\n    int w_est = 0;\n    for (int k = 0; k < K; k++) {\n        if (d_task[k] > s[k]) {\n            w_est += (d_task[k] - s[k]);\n        }\n    }\n\n    if (w_est < low_bound) {\n        int needed = low_bound - w_est;\n        while (needed > 0) {\n            int best_k = -1;\n            int best_gain = 0;\n            for (int k = 0; k < K; k++) {\n                int def_old = max(0, d_task[k] - s[k]);\n                int def_new = max(0, d_task[k] - (s[k] - 1));\n                int gain = def_new - def_old;\n                if (gain > best_gain) {\n                    best_gain = gain;\n                    best_k = k;\n                }\n            }\n            if (best_k == -1) break;\n            s[best_k]--;\n            needed--;\n        }\n    } else if (w_est > high_bound) {\n        int needed = w_est - high_bound;\n        while (needed > 0) {\n            int best_k = -1;\n            int best_gain = 0;\n            for (int k = 0; k < K; k++) {\n                int def_old = max(0, d_task[k] - s[k]);\n                int def_new = max(0, d_task[k] - (s[k] + 1));\n                int gain = def_old - def_new;\n                if (gain > best_gain) {\n                    best_gain = gain;\n                    best_k = k;\n                }\n            }\n            if (best_k == -1) break;\n            s[best_k]++;\n            needed--;\n        }\n    }\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>> d(N, vector<int>(K));\n    for (int i = 0; i < N; i++) {\n        for (int k = 0; k < K; k++) {\n            cin >> d[i][k];\n        }\n    }\n\n    vector<vector<int>> children(N);\n    vector<vector<int>> parents(N);\n    for (int i = 0; i < R; i++) {\n        int u, v;\n        cin >> u >> v;\n        u--; v--;\n        children[u].push_back(v);\n        parents[v].push_back(u);\n    }\n\n    vector<int> size_vec(N, 1);\n    for (int i = N-1; i >= 0; i--) {\n        for (int child : children[i]) {\n            size_vec[i] += size_vec[child];\n        }\n    }\n\n    vector<int> in_degree_count(N, 0);\n    for (int i = 0; i < N; i++) {\n        in_degree_count[i] = parents[i].size();\n    }\n\n    vector<int> busy_until(M, 1);\n    vector<int> start_day(M, -1);\n    vector<int> current_task(M, -1);\n    vector<vector<int>> skill_est(M, vector<int>(K, 0));\n    vector<bool> completed(N, false);\n    vector<int> ready_vec;\n\n    for (int i = 0; i < N; i++) {\n        if (in_degree_count[i] == 0) {\n            ready_vec.push_back(i);\n        }\n    }\n\n    for (int day = 1; day <= 2000; day++) {\n        vector<int> free_members;\n        for (int j = 0; j < M; j++) {\n            if (busy_until[j] <= day) {\n                free_members.push_back(j);\n            }\n        }\n\n        sort(ready_vec.begin(), ready_vec.end(), [&](int a, int b) {\n            return size_vec[a] > size_vec[b];\n        });\n\n        vector<pair<int, int>> assignments;\n        vector<int> current_free = free_members;\n        vector<bool> free_flag(M, false);\n        for (int j : free_members) {\n            free_flag[j] = true;\n        }\n\n        int idx = 0;\n        while (idx < (int)ready_vec.size() && !current_free.empty()) {\n            int task = ready_vec[idx];\n            int best_member = -1;\n            int best_w = INT_MAX;\n\n            for (int j : current_free) {\n                int w = 0;\n                for (int k = 0; k < K; k++) {\n                    if (d[task][k] > skill_est[j][k]) {\n                        w += (d[task][k] - skill_est[j][k]);\n                    }\n                }\n                if (w < best_w) {\n                    best_w = w;\n                    best_member = j;\n                }\n            }\n\n            if (best_member == -1) {\n                idx++;\n                continue;\n            }\n\n            assignments.push_back({best_member, task});\n            auto it = find(current_free.begin(), current_free.end(), best_member);\n            if (it != current_free.end()) {\n                *it = current_free.back();\n                current_free.pop_back();\n            }\n            free_flag[best_member] = false;\n\n            start_day[best_member] = day;\n            current_task[best_member] = task;\n            int t_est = (best_w == 0) ? 1 : best_w;\n            busy_until[best_member] = day + t_est;\n\n            swap(ready_vec[idx], ready_vec.back());\n            ready_vec.pop_back();\n        }\n\n        cout << assignments.size();\n        for (auto &p : assignments) {\n            cout << \" \" << p.first + 1 << \" \" << p.second + 1;\n        }\n        cout << endl;\n        cout.flush();\n\n        int n_completed;\n        cin >> n_completed;\n        if (n_completed == -1) {\n            break;\n        }\n\n        vector<int> completed_members(n_completed);\n        for (int i = 0; i < n_completed; i++) {\n            cin >> completed_members[i];\n            completed_members[i]--;\n        }\n\n        for (int j : completed_members) {\n            if (current_task[j] == -1) {\n                continue;\n            }\n            int i_task = current_task[j];\n            int t_actual = day - start_day[j] + 1;\n            update_skill(skill_est[j], d[i_task], t_actual, K);\n\n            completed[i_task] = true;\n            for (int child : children[i_task]) {\n                in_degree_count[child]--;\n                if (in_degree_count[child] == 0) {\n                    ready_vec.push_back(child);\n                }\n            }\n\n            busy_until[j] = day + 1;\n            start_day[j] = -1;\n            current_task[j] = -1;\n        }\n    }\n\n    return 0;\n}","ahc006":"#include <bits/stdc++.h>\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        return x == other.x && y == other.y;\n    }\n};\n\nint dist(const Point& a, const Point& b) {\n    return abs(a.x - b.x) + abs(a.y - b.y);\n}\n\nint main() {\n    vector<tuple<int, int, int, int>> orders;\n    for (int i = 0; i < 1000; i++) {\n        int a, b, c, d;\n        cin >> a >> b >> c >> d;\n        orders.emplace_back(a, b, c, d);\n    }\n\n    vector<Point> route = { Point(400, 400), Point(400, 400) };\n    vector<bool> selected(1000, false);\n    vector<int> pos_r(1000, -1);\n    vector<int> pos_d(1000, -1);\n\n    auto compute_total_distance = [&](const vector<Point>& r) {\n        int d = 0;\n        for (int i = 0; i < (int)r.size() - 1; i++) {\n            d += dist(r[i], r[i+1]);\n        }\n        return d;\n    };\n    int current_dist = compute_total_distance(route);\n\n    for (int iter = 0; iter < 50; iter++) {\n        long long best_delta = 1e18;\n        int best_order = -1;\n        int best_k = -1;\n        int L_route = route.size();\n\n        for (int idx = 0; idx < 1000; idx++) {\n            if (selected[idx]) continue;\n            auto [a, b, c, d] = orders[idx];\n            long long min_delta = 1e18;\n            int min_k = -1;\n\n            for (int k = 1; k < L_route; k++) {\n                Point prev = route[k-1];\n                Point next = route[k];\n                int delta = dist(prev, Point(a, b)) + dist(Point(a, b), Point(c, d)) + dist(Point(c, d), next) - dist(prev, next);\n                if (delta < min_delta) {\n                    min_delta = delta;\n                    min_k = k;\n                }\n            }\n\n            if (min_delta < best_delta) {\n                best_delta = min_delta;\n                best_order = idx;\n                best_k = min_k;\n            }\n        }\n\n        if (best_order == -1) {\n            for (int idx = 0; idx < 1000; idx++) {\n                if (!selected[idx]) {\n                    best_order = idx;\n                    best_k = 1;\n                    break;\n                }\n            }\n        }\n\n        auto [a, b, c, d] = orders[best_order];\n        selected[best_order] = true;\n        route.insert(route.begin() + best_k, Point(a, b));\n        route.insert(route.begin() + best_k + 1, Point(c, d));\n        pos_r[best_order] = best_k;\n        pos_d[best_order] = best_k + 1;\n        current_dist += best_delta;\n        L_route = route.size();\n    }\n\n    bool improved;\n    int passes = 0;\n    do {\n        improved = false;\n        int n = route.size();\n        for (int i = 0; i < n-1 && !improved; i++) {\n            for (int j = i+2; j < n-1 && !improved; j++) {\n                Point A = route[i];\n                Point B = route[i+1];\n                Point K = route[j];\n                Point L = route[j+1];\n                int delta = dist(A, K) + dist(B, L) - dist(A, B) - dist(K, L);\n                if (delta >= 0) continue;\n\n                vector<Point> candidate = route;\n                reverse(candidate.begin() + i+1, candidate.begin() + j+1);\n\n                vector<int> candidate_pos_r(1000, -1);\n                vector<int> candidate_pos_d(1000, -1);\n                for (int p = 0; p < candidate.size(); p++) {\n                    Point pt = candidate[p];\n                    for (int idx = 0; idx < 1000; idx++) {\n                        if (!selected[idx]) continue;\n                        auto [a_val, b_val, c_val, d_val] = orders[idx];\n                        if (candidate_pos_r[idx] == -1 && pt.x == a_val && pt.y == b_val) {\n                            candidate_pos_r[idx] = p;\n                        } else if (candidate_pos_d[idx] == -1 && pt.x == c_val && pt.y == d_val) {\n                            candidate_pos_d[idx] = p;\n                        }\n                    }\n                }\n\n                bool valid = true;\n                for (int idx = 0; idx < 1000; idx++) {\n                    if (!selected[idx]) continue;\n                    if (candidate_pos_r[idx] == -1 || candidate_pos_d[idx] == -1 || candidate_pos_r[idx] >= candidate_pos_d[idx]) {\n                        valid = false;\n                        break;\n                    }\n                }\n                if (!valid) continue;\n\n                route = move(candidate);\n                current_dist += delta;\n                pos_r = move(candidate_pos_r);\n                pos_d = move(candidate_pos_d);\n                improved = true;\n            }\n        }\n        passes++;\n    } while (improved && passes < 1000);\n\n    vector<int> selected_indices;\n    for (int i = 0; i < 1000; i++) {\n        if (selected[i]) {\n            selected_indices.push_back(i+1);\n        }\n    }\n    sort(selected_indices.begin(), selected_indices.end());\n\n    cout << 50;\n    for (int idx : selected_indices) {\n        cout << \" \" << idx;\n    }\n    cout << \"\\n\";\n\n    int n_route = route.size();\n    cout << n_route;\n    for (int i = 0; i < n_route; i++) {\n        cout << \" \" << route[i].x << \" \" << route[i].y;\n    }\n    cout << \"\\n\";\n\n    return 0;\n}","ahc007":"#include <iostream>\n#include <vector>\n#include <cmath>\n#include <algorithm>\n#include <functional>\nusing namespace std;\n\nstruct Edge {\n    int u, v;\n};\n\nint main() {\n    const int N = 400;\n    const int M = 1995;\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<Edge> edges(M);\n    vector<int> d(M, 0);\n\n    for (int i = 0; i < M; i++) {\n        cin >> edges[i].u >> edges[i].v;\n    }\n\n    for (int i = 0; i < M; i++) {\n        int u = edges[i].u;\n        int v = edges[i].v;\n        long long dx = (long long)x[u] - x[v];\n        long long dy = (long long)y[u] - y[v];\n        double dist = sqrt(dx*dx + dy*dy);\n        d[i] = (int)(dist + 0.5);\n    }\n\n    vector<int> parent(N);\n    vector<int> size_vec(N, 1);\n    vector<int> mout_comp(N, 10000000);\n\n    for (int i = 0; i < N; i++) {\n        parent[i] = i;\n    }\n\n    for (int i = 0; i < M; i++) {\n        int u = edges[i].u;\n        int v = edges[i].v;\n        if (d[i] < mout_comp[u]) mout_comp[u] = d[i];\n        if (d[i] < mout_comp[v]) mout_comp[v] = d[i];\n    }\n\n    function<int(int)> find = [&](int x) -> int {\n        if (parent[x] != x) {\n            parent[x] = find(parent[x]);\n        }\n        return parent[x];\n    };\n\n    int count = 0;\n\n    for (int i = 0; i < M; i++) {\n        int l;\n        cin >> l;\n\n        int u = edges[i].u;\n        int v = edges[i].v;\n        int ru = find(u);\n        int rv = find(v);\n\n        if (ru == rv) {\n            cout << 0 << endl;\n            cout.flush();\n            continue;\n        }\n\n        if (count == N - 1) {\n            cout << 0 << endl;\n            cout.flush();\n            continue;\n        }\n\n        int m = min(mout_comp[ru], mout_comp[rv]);\n\n        if (l <= m) {\n            cout << 1 << endl;\n            cout.flush();\n            count++;\n\n            if (size_vec[ru] < size_vec[rv]) {\n                parent[ru] = rv;\n                mout_comp[rv] = m;\n                size_vec[rv] += size_vec[ru];\n            } else {\n                parent[rv] = ru;\n                mout_comp[ru] = m;\n                size_vec[ru] += size_vec[rv];\n            }\n        } else {\n            cout << 0 << endl;\n            cout.flush();\n        }\n    }\n\n    return 0;\n}","ahc008":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int H = 30;\nconst int W = 30;\nint dx[4] = {-1, 1, 0, 0};\nint dy[4] = {0, 0, -1, 1};\nchar move_dir[4] = {'U', 'D', 'L', 'R'};\nchar wall_dir[4] = {'u', 'd', 'l', 'r'};\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N;\n    cin >> N;\n    vector<pair<int, int>> pets;\n    for (int i = 0; i < N; i++) {\n        int x, y, t;\n        cin >> x >> y >> t;\n        pets.push_back({x, y});\n    }\n\n    int M;\n    cin >> M;\n    vector<pair<int, int>> humans;\n    for (int i = 0; i < M; i++) {\n        int x, y;\n        cin >> x >> y;\n        humans.push_back({x, y});\n    }\n\n    bool grid[H+1][W+1];\n    for (int i = 1; i <= H; i++) {\n        for (int j = 1; j <= W; j++) {\n            grid[i][j] = true;\n        }\n    }\n\n    for (int turn = 0; turn < 300; turn++) {\n        string actions = \"\";\n        for (int i = 0; i < M; i++) {\n            vector<char> candidates;\n            candidates.push_back('.');\n\n            for (int d = 0; d < 4; d++) {\n                int nx = humans[i].first + dx[d];\n                int ny = humans[i].second + dy[d];\n                if (nx >= 1 && nx <= H && ny >= 1 && ny <= W && grid[nx][ny]) {\n                    candidates.push_back(move_dir[d]);\n                }\n            }\n\n            for (int d = 0; d < 4; d++) {\n                int wx = humans[i].first + dx[d];\n                int wy = humans[i].second + dy[d];\n                if (wx < 1 || wx > H || wy < 1 || wy > W) continue;\n                if (!grid[wx][wy]) continue;\n\n                bool occupied = false;\n                for (int j = 0; j < M; j++) {\n                    if (humans[j].first == wx && humans[j].second == wy) {\n                        occupied = true;\n                        break;\n                    }\n                }\n                if (occupied) continue;\n                for (int j = 0; j < N; j++) {\n                    if (pets[j].first == wx && pets[j].second == wy) {\n                        occupied = true;\n                        break;\n                    }\n                }\n                if (occupied) continue;\n\n                bool adjacent_pet = false;\n                for (int dd = 0; dd < 4; dd++) {\n                    int nx = wx + dx[dd];\n                    int ny = wy + dy[dd];\n                    if (nx < 1 || nx > H || ny < 1 || ny > W) continue;\n                    for (int j = 0; j < N; j++) {\n                        if (pets[j].first == nx && pets[j].second == ny) {\n                            adjacent_pet = true;\n                            break;\n                        }\n                    }\n                    if (adjacent_pet) break;\n                }\n                if (adjacent_pet) continue;\n\n                candidates.push_back(wall_dir[d]);\n            }\n\n            double best_score = -1e18;\n            char best_act = '.';\n            for (char act : candidates) {\n                bool temp_grid[H+1][W+1];\n                for (int x = 1; x <= H; x++) {\n                    for (int y = 1; y <= W; y++) {\n                        temp_grid[x][y] = grid[x][y];\n                    }\n                }\n                pair<int, int> start_pos = humans[i];\n\n                if (act == '.') {\n                } else if (act >= 'A' && act <= 'Z') {\n                    int d;\n                    if (act == 'U') d = 0;\n                    else if (act == 'D') d = 1;\n                    else if (act == 'L') d = 2;\n                    else d = 3;\n                    int nx = start_pos.first + dx[d];\n                    int ny = start_pos.second + dy[d];\n                    start_pos = {nx, ny};\n                } else {\n                    int d;\n                    if (act == 'u') d = 0;\n                    else if (act == 'd') d = 1;\n                    else if (act == 'l') d = 2;\n                    else d = 3;\n                    int wx = start_pos.first + dx[d];\n                    int wy = start_pos.second + dy[d];\n                    temp_grid[wx][wy] = false;\n                }\n\n                bool visited[H+1][W+1] = {};\n                int comp_size = 0;\n                int pet_count = 0;\n                queue<pair<int, int>> q;\n                q.push(start_pos);\n                visited[start_pos.first][start_pos.second] = true;\n\n                while (!q.empty()) {\n                    auto [x, y] = q.front();\n                    q.pop();\n                    comp_size++;\n                    for (int j = 0; j < N; j++) {\n                        if (pets[j].first == x && pets[j].second == y) {\n                            pet_count++;\n                            break;\n                        }\n                    }\n                    for (int d = 0; d < 4; d++) {\n                        int nx = x + dx[d];\n                        int ny = y + dy[d];\n                        if (nx >= 1 && nx <= H && ny >= 1 && ny <= W && !visited[nx][ny] && temp_grid[nx][ny]) {\n                            visited[nx][ny] = true;\n                            q.push({nx, ny});\n                        }\n                    }\n                }\n\n                double factor = pow(2.0, -pet_count);\n                double score = comp_size * factor;\n\n                if (score > best_score) {\n                    best_score = score;\n                    best_act = act;\n                }\n            }\n            actions += best_act;\n        }\n\n        cout << actions << endl;\n        cout.flush();\n\n        vector<string> pet_moves(N);\n        for (int i = 0; i < N; i++) {\n            cin >> pet_moves[i];\n        }\n\n        for (int i = 0; i < M; i++) {\n            if (actions[i] >= 'a' && actions[i] <= 'z') {\n                int d;\n                if (actions[i] == 'u') d = 0;\n                else if (actions[i] == 'd') d = 1;\n                else if (actions[i] == 'l') d = 2;\n                else d = 3;\n                int wx = humans[i].first + dx[d];\n                int wy = humans[i].second + dy[d];\n                grid[wx][wy] = false;\n            }\n        }\n\n        for (int i = 0; i < M; i++) {\n            if (actions[i] >= 'A' && actions[i] <= 'Z') {\n                int d;\n                if (actions[i] == 'U') d = 0;\n                else if (actions[i] == 'D') d = 1;\n                else if (actions[i] == 'L') d = 2;\n                else d = 3;\n                int nx = humans[i].first + dx[d];\n                int ny = humans[i].second + dy[d];\n                if (nx >= 1 && nx <= H && ny >= 1 && ny <= W && grid[nx][ny]) {\n                    humans[i] = {nx, ny};\n                }\n            }\n        }\n\n        for (int i = 0; i < N; i++) {\n            int x = pets[i].first;\n            int y = pets[i].second;\n            string& moves = pet_moves[i];\n            for (char c : moves) {\n                int nx = x, ny = y;\n                if (c == 'U') nx--;\n                else if (c == 'D') nx++;\n                else if (c == 'L') ny--;\n                else if (c == 'R') ny++;\n                if (nx >= 1 && nx <= H && ny >= 1 && ny <= W && grid[nx][ny]) {\n                    x = nx;\n                    y = ny;\n                }\n            }\n            pets[i] = {x, y};\n        }\n    }\n\n    return 0;\n}","ahc009":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int INF = 1e9;\n\nstruct State {\n    int steps;\n    int dir_changes;\n    int i;\n    int j;\n    int last_dir;\n    State(int s, int dc, int i, int j, int ld) \n        : steps(s), dir_changes(dc), i(i), j(j), last_dir(ld) {}\n    bool operator<(const State& other) const {\n        if (steps != other.steps) return steps > other.steps;\n        if (dir_changes != other.dir_changes) return dir_changes > other.dir_changes;\n        return false;\n    }\n};\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int si, sj, ti, tj;\n    double p;\n    cin >> si >> sj >> ti >> tj >> 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    const vector<int> di = {-1, 1, 0, 0};\n    const vector<int> dj = {0, 0, -1, 1};\n    const vector<char> dir_char = {'U', 'D', 'L', 'R'};\n\n    vector<vector<vector<pair<int, int>>>> best(20, vector<vector<pair<int, int>>>(20, vector<pair<int, int>>(5, {INF, INF})));\n    vector<vector<vector<array<int, 3>>>> parent(20, vector<vector<array<int, 3>>>(20, vector<array<int, 3>>(5, {-1, -1, -1})));\n\n    priority_queue<State> pq;\n    best[si][sj][4] = {0, 0};\n    pq.push(State(0, 0, si, sj, 4));\n\n    while (!pq.empty()) {\n        State s = pq.top();\n        pq.pop();\n\n        if (s.steps != best[s.i][s.j][s.last_dir].first || s.dir_changes != best[s.i][s.j][s.last_dir].second) {\n            continue;\n        }\n\n        for (int d = 0; d < 4; d++) {\n            int ni = s.i + di[d];\n            int nj = s.j + dj[d];\n            if (ni < 0 || ni >= 20 || nj < 0 || nj >= 20) continue;\n\n            bool wall = false;\n            if (d == 0) {\n                if (s.i == 0) wall = true;\n                else if (v[s.i-1][s.j] == '1') wall = true;\n            } else if (d == 1) {\n                if (s.i == 19) wall = true;\n                else if (v[s.i][s.j] == '1') wall = true;\n            } else if (d == 2) {\n                if (s.j == 0) wall = true;\n                else if (h[s.i][s.j-1] == '1') wall = true;\n            } else if (d == 3) {\n                if (s.j == 19) wall = true;\n                else if (h[s.i][s.j] == '1') wall = true;\n            }\n            if (wall) continue;\n\n            int new_dir_changes = s.dir_changes;\n            if (s.last_dir != 4 && s.last_dir != d) {\n                new_dir_changes = s.dir_changes + 1;\n            }\n\n            int new_steps = s.steps + 1;\n            if (new_steps < best[ni][nj][d].first || (new_steps == best[ni][nj][d].first && new_dir_changes < best[ni][nj][d].second)) {\n                best[ni][nj][d] = {new_steps, new_dir_changes};\n                parent[ni][nj][d] = {s.i, s.j, s.last_dir};\n                pq.push(State(new_steps, new_dir_changes, ni, nj, d));\n            }\n        }\n    }\n\n    int best_steps = INF;\n    int best_dir_changes = INF;\n    int best_last_dir = -1;\n    for (int d = 0; d < 5; d++) {\n        if (best[ti][tj][d].first < best_steps || (best[ti][tj][d].first == best_steps && best[ti][tj][d].second < best_dir_changes)) {\n            best_steps = best[ti][tj][d].first;\n            best_dir_changes = best[ti][tj][d].second;\n            best_last_dir = d;\n        }\n    }\n\n    if (best_steps == INF) {\n        cout << \"\" << endl;\n        return 0;\n    }\n\n    vector<int> directions;\n    int i = ti, j = tj, last_dir = best_last_dir;\n    while (!(i == si && j == sj && last_dir == 4)) {\n        auto [pi, pj, pd] = parent[i][j][last_dir];\n        directions.push_back(last_dir);\n        i = pi;\n        j = pj;\n        last_dir = pd;\n    }\n    reverse(directions.begin(), directions.end());\n\n    if (directions.empty()) {\n        cout << \"\" << endl;\n        return 0;\n    }\n\n    int L = directions.size();\n    int k = 200 / L;\n    string ans;\n    for (int idx = 0; idx < k; idx++) {\n        for (int d : directions) {\n            ans += dir_char[d];\n        }\n    }\n\n    cout << ans << endl;\n\n    return 0;\n}","ahc010":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int di[4] = {0, -1, 0, 1};\nconst int dj[4] = {-1, 0, 1, 0};\nconst int to[8][4] = {\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\npair<int, int> get_top_two_cycles(const vector<string>& initial_types, const vector<vector<int>>& rotations) {\n    int grid[30][30];\n    for (int i = 0; i < 30; ++i) {\n        for (int j = 0; j < 30; ++j) {\n            int t = initial_types[i][j] - '0';\n            int r = rotations[i][j];\n            if (t < 4) {\n                grid[i][j] = (t + r) % 4;\n            } else {\n                grid[i][j] = t ^ (r & 1);\n            }\n        }\n    }\n\n    bool vis[30][30][4] = {};\n    int max1 = 0, max2 = 0;\n\n    for (int i = 0; i < 30; ++i) {\n        for (int j = 0; j < 30; ++j) {\n            for (int d = 0; d < 4; ++d) {\n                if (vis[i][j][d]) continue;\n\n                int ci = i, cj = j, cd = d;\n                int steps = 0;\n\n                while (true) {\n                    if (vis[ci][cj][cd]) {\n                        break;\n                    }\n                    vis[ci][cj][cd] = true;\n\n                    int s = grid[ci][cj];\n                    int nd = to[s][cd];\n                    if (nd == -1) {\n                        break;\n                    }\n\n                    int ni = ci + di[nd];\n                    int nj = cj + dj[nd];\n                    if (ni < 0 || ni >= 30 || nj < 0 || nj >= 30) {\n                        break;\n                    }\n\n                    cd = (nd + 2) % 4;\n                    ci = ni;\n                    cj = nj;\n                    steps++;\n\n                    if (ci == i && cj == j && cd == d) {\n                        if (steps > max1) {\n                            max2 = max1;\n                            max1 = steps;\n                        } else if (steps > max2) {\n                            max2 = steps;\n                        }\n                        break;\n                    }\n                }\n            }\n        }\n    }\n    return make_pair(max1, max2);\n}\n\nint main() {\n    vector<string> initial_types(30);\n    for (int i = 0; i < 30; i++) {\n        cin >> initial_types[i];\n    }\n\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n    uniform_int_distribution<int> dist(0, 3);\n\n    vector<vector<int>> best_rotations(30, vector<int>(30, 0));\n    long long best_score = 0;\n\n    for (int trial = 0; trial < 5; trial++) {\n        vector<vector<int>> rotations(30, vector<int>(30));\n        for (int i = 0; i < 30; i++) {\n            for (int j = 0; j < 30; j++) {\n                rotations[i][j] = dist(rng);\n            }\n        }\n\n        auto top_two = get_top_two_cycles(initial_types, rotations);\n        long long current_score = (top_two.second > 0) ? 1LL * top_two.first * top_two.second : 0;\n\n        if (current_score > best_score) {\n            best_score = current_score;\n            best_rotations = rotations;\n        }\n\n        bool improved;\n        for (int pass = 0; pass < 3; pass++) {\n            improved = false;\n            vector<pair<int, int>> tiles;\n            for (int i = 0; i < 30; i++) {\n                for (int j = 0; j < 30; j++) {\n                    tiles.emplace_back(i, j);\n                }\n            }\n            shuffle(tiles.begin(), tiles.end(), rng);\n\n            for (auto [i, j] : tiles) {\n                int original_r = rotations[i][j];\n                long long original_score = current_score;\n                int best_r = original_r;\n                long long best_score_for_tile = original_score;\n\n                for (int r = 0; r < 4; r++) {\n                    if (r == original_r) continue;\n                    rotations[i][j] = r;\n                    auto new_top = get_top_two_cycles(initial_types, rotations);\n                    long long new_score = (new_top.second > 0) ? 1LL * new_top.first * new_top.second : 0;\n                    rotations[i][j] = original_r;\n\n                    if (new_score > best_score_for_tile) {\n                        best_score_for_tile = new_score;\n                        best_r = r;\n                    }\n                }\n\n                if (best_r != original_r) {\n                    rotations[i][j] = best_r;\n                    current_score = best_score_for_tile;\n                    improved = true;\n                    if (current_score > best_score) {\n                        best_score = current_score;\n                        best_rotations = rotations;\n                    }\n                }\n            }\n            if (!improved) break;\n        }\n    }\n\n    string output;\n    for (int i = 0; i < 30; i++) {\n        for (int j = 0; j < 30; j++) {\n            output += ('0' + best_rotations[i][j]);\n        }\n    }\n    cout << output << endl;\n    return 0;\n}","ahc011":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <random>\n#include <queue>\n#include <climits>\n#include <tuple>\n#include <cctype>\nusing namespace std;\n\nstruct State {\n    vector<vector<int>> grid;\n    int empty_i, empty_j;\n    string moves;\n    vector<vector<bool>> fixed;\n\n    State(vector<vector<int>> g, int ei, int ej, string m = \"\")\n        : grid(g), empty_i(ei), empty_j(ej), moves(m) {\n        fixed = vector<vector<bool>>(grid.size(), vector<bool>(grid.size(), false));\n    }\n\n    bool is_goal(int N, const vector<vector<int>>& target) const {\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                if (grid[i][j] != target[i][j]) return false;\n            }\n        }\n        return true;\n    }\n\n    int heuristic(int N, const vector<vector<int>>& target) const {\n        int h = 0;\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                if (fixed[i][j]) continue;\n                if (grid[i][j] != target[i][j]) h++;\n            }\n        }\n        h += abs(empty_i - N/2) + abs(empty_j - N/2);\n        return h;\n    }\n\n    void generate_moves(vector<tuple<char, int, int, int>>& moves_list, int N) {\n        vector<pair<int, int>> dirs = {{-1,0}, {1,0}, {0,-1}, {0,1}};\n        char dir_chars[4] = {'U','D','L','R'};\n\n        for (int d = 0; d < 4; d++) {\n            int ni = empty_i + dirs[d].first;\n            int nj = empty_j + dirs[d].second;\n            if (ni < 0 || ni >= N || nj < 0 || nj >= N) continue;\n            moves_list.emplace_back(dir_chars[d], ni, nj, d);\n        }\n    }\n};\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, T;\n    cin >> N >> T;\n    vector<string> grid_str(N);\n    for (int i = 0; i < N; i++) {\n        cin >> grid_str[i];\n    }\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            char c = grid_str[i][j];\n            int val;\n            if (isdigit(c)) val = c - '0';\n            else val = 10 + (c - 'a');\n            grid[i][j] = val;\n        }\n    }\n\n    int empty_i = -1, empty_j = -1;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (grid[i][j] == 0) {\n                empty_i = i;\n                empty_j = j;\n            }\n        }\n    }\n\n    random_device rd;\n    mt19937 rng(rd());\n\n    vector<vector<int>> target_grid(N, vector<int>(N, 0));\n    int target_empty_i = N-1, target_empty_j = N-1;\n\n    // Generate a valid spanning tree configuration\n    vector<vector<bool>> visited(N, vector<bool>(N, false));\n    vector<pair<int, int>> tree_cells;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (i == target_empty_i && j == target_empty_j) continue;\n            tree_cells.push_back({i, j});\n            visited[i][j] = true;\n        }\n    }\n    shuffle(tree_cells.begin(), tree_cells.end(), rng);\n\n    // Build spanning tree using BFS\n    vector<vector<int>> parent(N, vector<int>(N, -1));\n    queue<pair<int, int>> q;\n    q.push({0, 0});\n    visited.assign(N, vector<bool>(N, false));\n    visited[0][0] = true;\n    parent[0][0] = -1;\n\n    while (!q.empty()) {\n        auto [i, j] = q.front(); q.pop();\n        vector<pair<int, int>> neighbors = {{i-1,j}, {i+1,j}, {i,j-1}, {i,j+1}};\n        shuffle(neighbors.begin(), neighbors.end(), rng);\n        for (auto [ni, nj] : neighbors) {\n            if (ni >= 0 && ni < N && nj >= 0 && nj < N && !visited[ni][nj] && !(ni == target_empty_i && nj == target_empty_j)) {\n                visited[ni][nj] = true;\n                parent[ni][nj] = i * N + j;\n                q.push({ni, nj});\n            }\n        }\n    }\n\n    // Assign bitmasks based on parent relationships\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (i == target_empty_i && j == target_empty_j) {\n                target_grid[i][j] = 0;\n                continue;\n            }\n            int bitmask = 0;\n            if (i > 0 && parent[i-1][j] == i*N+j) bitmask |= 8; // down from parent\n            if (i < N-1 && parent[i+1][j] == i*N+j) bitmask |= 2; // up from parent\n            if (j > 0 && parent[i][j-1] == i*N+j) bitmask |= 4; // right from parent\n            if (j < N-1 && parent[i][j+1] == i*N+j) bitmask |= 1; // left from parent\n            target_grid[i][j] = bitmask;\n        }\n    }\n\n    // Initialize state\n    State current(grid, empty_i, empty_j);\n    string best_moves = \"\";\n    int best_cost = INT_MAX;\n\n    // Beam search parameters\n    const int beam_width = 100;\n    vector<State> beam;\n    beam.push_back(current);\n\n    for (int step = 0; step < T; step++) {\n        vector<State> next_beam;\n\n        for (size_t b = 0; b < min(static_cast<size_t>(beam_width), beam.size()); b++) {\n            State state = beam[b];\n            vector<tuple<char, int, int, int>> moves;\n            state.generate_moves(moves, N);\n\n            for (auto [move_char, ni, nj, d] : moves) {\n                State next = state;\n                swap(next.grid[state.empty_i][state.empty_j], next.grid[ni][nj]);\n                next.empty_i = ni;\n                next.empty_j = nj;\n                next.moves += move_char;\n\n                if (next.is_goal(N, target_grid)) {\n                    cout << next.moves << endl;\n                    return 0;\n                }\n\n                int h = next.heuristic(N, target_grid);\n                if (static_cast<int>(next.moves.size()) + h < best_cost) {\n                    best_moves = next.moves;\n                    best_cost = next.moves.size() + h;\n                }\n\n                next_beam.push_back(next);\n                if (next_beam.size() > static_cast<size_t>(beam_width * 2)) break;\n            }\n            if (next_beam.size() > static_cast<size_t>(beam_width * 2)) break;\n        }\n\n        if (next_beam.empty()) break;\n\n        // Select top beam_width states\n        sort(next_beam.begin(), next_beam.end(), [&](const State& a, const State& b) {\n            return a.moves.size() + a.heuristic(N, target_grid) < b.moves.size() + b.heuristic(N, target_grid);\n        });\n        beam.clear();\n        for (size_t i = 0; i < min(static_cast<size_t>(beam_width), next_beam.size()); i++) {\n            beam.push_back(next_beam[i]);\n        }\n\n        // Random perturbation to escape local optima\n        if (beam.size() < static_cast<size_t>(beam_width/2) && step % 5 == 0) {\n            shuffle(next_beam.begin(), next_beam.end(), rng);\n            for (size_t i = 0; i < min(static_cast<size_t>(beam_width/2), next_beam.size()); i++) {\n                beam.push_back(next_beam[i]);\n            }\n        }\n    }\n\n    // If we didn't find the exact solution, output the best we have\n    cout << best_moves << endl;\n\n    return 0;\n}","ahc012":"#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\n\nstruct Point {\n    ll x, y;\n    Point(ll x=0, ll y=0) : x(x), y(y) {}\n};\n\nstruct Line {\n    Point p1, p2;\n    Line(Point p1=Point(), Point p2=Point()) : p1(p1), p2(p2) {}\n};\n\nll side(Point p1, Point p2, Point p) {\n    return (p2.x - p1.x) * (p.y - p1.y) - (p2.y - p1.y) * (p.x - p1.x);\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(0);\n\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> straws(N);\n    for (int i = 0; i < N; i++) {\n        cin >> straws[i].x >> straws[i].y;\n    }\n\n    vector<vector<ll>> global_ts(4);\n    vector<pair<int, int>> dirs = {{1,0}, {0,1}, {1,1}, {1,-1}};\n\n    for (int dir_idx = 0; dir_idx < 4; dir_idx++) {\n        ll dx = dirs[dir_idx].first;\n        ll dy = dirs[dir_idx].second;\n        vector<ll> t_vec;\n        for (int i = 0; i < N; i++) {\n            t_vec.push_back(dx * straws[i].x + dy * straws[i].y);\n        }\n        sort(t_vec.begin(), t_vec.end());\n        global_ts[dir_idx] = t_vec;\n    }\n\n    struct Region {\n        vector<int> strawberries;\n    };\n    vector<Region> regions;\n    Region all;\n    for (int i = 0; i < N; i++) {\n        all.strawberries.push_back(i);\n    }\n    regions.push_back(all);\n\n    vector<int> b(11, 0);\n    if (N <= 10) {\n        b[N] = 1;\n    }\n\n    vector<Line> cuts;\n\n    for (int step = 0; step < K; step++) {\n        int best_delta = -1;\n        Line best_line;\n        Region* best_region = nullptr;\n        vector<int> best_new_b;\n\n        for (int i = 0; i < regions.size(); i++) {\n            Region& R = regions[i];\n            int c = R.strawberries.size();\n            if (c <= 1) continue;\n\n            for (int dir_idx = 0; dir_idx < 4; dir_idx++) {\n                ll dx = dirs[dir_idx].first;\n                ll dy = dirs[dir_idx].second;\n                vector<ll> t_region;\n                for (int idx : R.strawberries) {\n                    t_region.push_back(dx * straws[idx].x + dy * straws[idx].y);\n                }\n                sort(t_region.begin(), t_region.end());\n                int sz = t_region.size();\n                double median_val;\n                if (sz % 2 == 1) {\n                    median_val = t_region[sz/2];\n                } else {\n                    median_val = (t_region[sz/2 - 1] + t_region[sz/2]) / 2.0;\n                }\n\n                for (int offset = -2; offset <= 2; offset++) {\n                    ll c_val = (ll)(median_val) + offset;\n\n                    if (binary_search(global_ts[dir_idx].begin(), global_ts[dir_idx].end(), c_val)) {\n                        continue;\n                    }\n\n                    Point p1, p2;\n                    if (dir_idx == 0) {\n                        p1 = Point(c_val, 0);\n                        p2 = Point(c_val, 1);\n                    } else if (dir_idx == 1) {\n                        p1 = Point(0, c_val);\n                        p2 = Point(1, c_val);\n                    } else if (dir_idx == 2) {\n                        p1 = Point(c_val, 0);\n                        p2 = Point(0, c_val);\n                    } else {\n                        p1 = Point(c_val, 0);\n                        p2 = Point(0, -c_val);\n                    }\n                    Line cand(p1, p2);\n\n                    int c1 = 0;\n                    for (int idx : R.strawberries) {\n                        if (side(cand.p1, cand.p2, straws[idx]) > 0) {\n                            c1++;\n                        }\n                    }\n                    int c2 = c - c1;\n\n                    vector<int> new_b = b;\n                    if (c <= 10) {\n                        new_b[c]--;\n                    }\n                    if (c1 <= 10) {\n                        new_b[c1]++;\n                    }\n                    if (c2 <= 10) {\n                        new_b[c2]++;\n                    }\n\n                    int old_score = 0;\n                    for (int d = 1; d <= 10; d++) {\n                        old_score += min(a[d], b[d]);\n                    }\n                    int new_score = 0;\n                    for (int d = 1; d <= 10; d++) {\n                        new_score += min(a[d], new_b[d]);\n                    }\n                    int delta = new_score - old_score;\n\n                    if (delta > best_delta) {\n                        best_delta = delta;\n                        best_line = cand;\n                        best_region = &R;\n                        best_new_b = new_b;\n                    }\n                }\n            }\n        }\n\n        if (best_delta <= 0) {\n            break;\n        }\n\n        vector<int> left_idx, right_idx;\n        for (int idx : best_region->strawberries) {\n            if (side(best_line.p1, best_line.p2, straws[idx]) > 0) {\n                left_idx.push_back(idx);\n            } else {\n                right_idx.push_back(idx);\n            }\n        }\n\n        Region left_region, right_region;\n        left_region.strawberries = left_idx;\n        right_region.strawberries = right_idx;\n\n        vector<Region> new_regions;\n        for (int i = 0; i < regions.size(); i++) {\n            if (&regions[i] != best_region) {\n                new_regions.push_back(regions[i]);\n            }\n        }\n        new_regions.push_back(left_region);\n        new_regions.push_back(right_region);\n        regions = new_regions;\n\n        b = best_new_b;\n        cuts.push_back(best_line);\n    }\n\n    cout << cuts.size() << \"\\n\";\n    for (Line& line : cuts) {\n        cout << line.p1.x << \" \" << line.p1.y << \" \"\n             << line.p2.x << \" \" << line.p2.y << \"\\n\";\n    }\n\n    return 0;\n}","ahc014":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Operation {\n    int x1, y1, x2, y2, x3, y3, x4, y4;\n};\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M;\n    cin >> N >> M;\n    int c = (N - 1) / 2;\n\n    vector<vector<bool>> grid(N, vector<bool>(N, false));\n    vector<pair<int, int>> dots;\n    for (int i = 0; i < M; i++) {\n        int x, y;\n        cin >> x >> y;\n        grid[x][y] = true;\n        dots.emplace_back(x, y);\n    }\n\n    vector<tuple<long long, int, int>> candidate_list;\n    for (int x = 0; x < N; x++) {\n        for (int y = 0; y < N; y++) {\n            long long dx = x - c;\n            long long dy = y - c;\n            long long weight = dx * dx + dy * dy + 1;\n            candidate_list.emplace_back(-weight, x, y);\n        }\n    }\n    sort(candidate_list.begin(), candidate_list.end());\n\n    vector<vector<bool>> hor_used(N-1, vector<bool>(N, false));\n    vector<vector<bool>> ver_used(N, vector<bool>(N-1, false));\n    vector<vector<bool>> diag1_used(N-1, vector<bool>(N-1, false));\n    vector<vector<bool>> diag2_used(N-1, vector<bool>(N-1, false));\n\n    vector<Operation> operations;\n\n    bool added_something;\n    do {\n        added_something = false;\n        for (auto [neg_weight, x, y] : candidate_list) {\n            if (grid[x][y]) continue;\n\n            bool found = false;\n\n            auto check_axis = [&](int x1, int y1, int x2, int y2) -> bool {\n                if (x1 == x2 || y1 == y2) return false;\n                int x_min = min(x1, x2);\n                int x_max = max(x1, x2);\n                int y_min = min(y1, y2);\n                int y_max = max(y1, y2);\n\n                bool cond2 = true;\n                for (int yy = y_min + 1; yy < y_max; yy++) {\n                    if (grid[x_min][yy] || grid[x_max][yy]) {\n                        cond2 = false;\n                        break;\n                    }\n                }\n                if (!cond2) return false;\n                for (int xx = x_min + 1; xx < x_max; xx++) {\n                    if (grid[xx][y_min] || grid[xx][y_max]) {\n                        cond2 = false;\n                        break;\n                    }\n                }\n                if (!cond2) return false;\n\n                bool cond3 = true;\n                for (int yy = y_min; yy < y_max; yy++) {\n                    if (ver_used[x_min][yy] || ver_used[x_max][yy]) {\n                        cond3 = false;\n                        break;\n                    }\n                }\n                if (!cond3) return false;\n                for (int xx = x_min; xx < x_max; xx++) {\n                    if (hor_used[xx][y_min] || hor_used[xx][y_max]) {\n                        cond3 = false;\n                        break;\n                    }\n                }\n                return cond3;\n            };\n\n            auto mark_axis = [&](int x1, int y1, int x2, int y2) {\n                int x_min = min(x1, x2);\n                int x_max = max(x1, x2);\n                int y_min = min(y1, y2);\n                int y_max = max(y1, y2);\n                for (int yy = y_min; yy < y_max; yy++) {\n                    ver_used[x_min][yy] = true;\n                    ver_used[x_max][yy] = true;\n                }\n                for (int xx = x_min; xx < x_max; xx++) {\n                    hor_used[xx][y_min] = true;\n                    hor_used[xx][y_max] = true;\n                }\n            };\n\n            auto check_diag = [&](int x, int y) -> bool {\n                vector<pair<int, int>> d1_points;\n                vector<pair<int, int>> d2_points;\n                for (int dx = -N; dx <= N; dx++) {\n                    int nx = x + dx;\n                    int ny = y + dx;\n                    if (nx >= 0 && nx < N && ny >= 0 && ny < N && grid[nx][ny]) {\n                        d1_points.emplace_back(nx, ny);\n                    }\n                    nx = x + dx;\n                    ny = y - dx;\n                    if (nx >= 0 && nx < N && ny >= 0 && ny < N && grid[nx][ny]) {\n                        d2_points.emplace_back(nx, ny);\n                    }\n                }\n\n                for (auto& a : d1_points) {\n                    for (auto& b : d2_points) {\n                        int cx = a.first + b.first - x;\n                        int cy = a.second + b.second - y;\n                        if (cx < 0 || cx >= N || cy < 0 || cy >= N) continue;\n                        if (!grid[cx][cy]) continue;\n\n                        vector<pair<int, int>> corners = {{x,y}, a, {cx,cy}, b};\n                        bool cond2 = true;\n                        for (int i = 0; i < 4; i++) {\n                            int x1 = corners[i].first, y1 = corners[i].second;\n                            int x2 = corners[(i+1)%4].first, y2 = corners[(i+1)%4].second;\n                            int dx = x2 - x1, dy = y2 - y1;\n                            int steps = max(abs(dx), abs(dy));\n                            for (int step = 1; step < steps; step++) {\n                                int px = x1 + (dx * step) / steps;\n                                int py = y1 + (dy * step) / steps;\n                                bool is_corner = false;\n                                for (auto& corner : corners) {\n                                    if (px == corner.first && py == corner.second) {\n                                        is_corner = true;\n                                        break;\n                                    }\n                                }\n                                if (!is_corner && grid[px][py]) {\n                                    cond2 = false;\n                                    break;\n                                }\n                            }\n                            if (!cond2) break;\n                        }\n                        if (!cond2) continue;\n\n                        bool cond3 = true;\n                        for (int i = 0; i < 4; i++) {\n                            int x1 = corners[i].first, y1 = corners[i].second;\n                            int x2 = corners[(i+1)%4].first, y2 = corners[(i+1)%4].second;\n                            int dx = x2 - x1, dy = y2 - y1;\n                            int steps = max(abs(dx), abs(dy));\n                            for (int step = 1; step <= steps; step++) {\n                                int px = x1 + (dx * step) / steps;\n                                int py = y1 + (dy * step) / steps;\n                                int ex, ey;\n                                if (dx == 0) {\n                                    ex = px;\n                                    ey = min(y1, y2) + (step <= abs(dy) ? step-1 : abs(dy)-1);\n                                } else if (dy == 0) {\n                                    ex = min(x1, x2) + (step <= abs(dx) ? step-1 : abs(dx)-1);\n                                    ey = py;\n                                } else if (abs(dx) == abs(dy)) {\n                                    if (dx * dy > 0) {\n                                        if (dx > 0) {\n                                            ex = px;\n                                            ey = py;\n                                        } else {\n                                            ex = px + 1;\n                                            ey = py + 1;\n                                        }\n                                    } else {\n                                        if (dx > 0) {\n                                            ex = px;\n                                            ey = py + 1;\n                                        } else {\n                                            ex = px + 1;\n                                            ey = py;\n                                        }\n                                    }\n                                } else {\n                                    continue;\n                                }\n                                if (ex >= N-1 || ey >= N) continue;\n                                if (dx == 0 || dy == 0) {\n                                    if (dx == 0) {\n                                        if (ver_used[px][min(y1,y2) + step - 1]) {\n                                            cond3 = false;\n                                            break;\n                                        }\n                                    } else {\n                                        if (hor_used[min(x1,x2) + step - 1][py]) {\n                                            cond3 = false;\n                                            break;\n                                        }\n                                    }\n                                } else {\n                                    int type = (dx * dy > 0) ? 1 : 2;\n                                    int sx = px, sy = py;\n                                    if (type == 1) {\n                                        if (diag1_used[sx][sy]) {\n                                            cond3 = false;\n                                            break;\n                                        }\n                                    } else {\n                                        if (sx >= 0 && sy >= 0 && sx < N-1 && sy < N-1 && diag2_used[sx][sy]) {\n                                            cond3 = false;\n                                            break;\n                                        }\n                                    }\n                                }\n                            }\n                            if (!cond3) break;\n                        }\n                        if (cond2 && cond3) {\n                            grid[x][y] = true;\n                            dots.emplace_back(x, y);\n                            operations.push_back({x, y, a.first, a.second, cx, cy, b.first, b.second});\n                            for (int i = 0; i < 4; i++) {\n                                int x1 = corners[i].first, y1 = corners[i].second;\n                                int x2 = corners[(i+1)%4].first, y2 = corners[(i+1)%4].second;\n                                int dx = x2 - x1, dy = y2 - y1;\n                                int steps = max(abs(dx), abs(dy));\n                                for (int step = 1; step <= steps; step++) {\n                                    int px = x1 + (dx * step) / steps;\n                                    int py = y1 + (dy * step) / steps;\n                                    if (dx == 0) {\n                                        ver_used[x1][min(y1,y2) + step - 1] = true;\n                                    } else if (dy == 0) {\n                                        hor_used[min(x1,x2) + step - 1][y1] = true;\n                                    } else if (abs(dx) == abs(dy)) {\n                                        if (dx * dy > 0) {\n                                            if (dx > 0) diag1_used[px][py] = true;\n                                            else diag1_used[px][py] = true;\n                                        } else {\n                                            if (dx > 0) diag2_used[px][py] = true;\n                                            else diag2_used[px][py] = true;\n                                        }\n                                    }\n                                }\n                            }\n                            return true;\n                        }\n                    }\n                }\n                return false;\n            };\n\n            if (x > 0 && x < N-1 && y > 0 && y < N-1) {\n                if (check_diag(x, y)) {\n                    found = true;\n                }\n            }\n\n            if (!found) {\n                vector<int> up_dots, down_dots, left_dots, right_dots;\n                for (int yy = y+1; yy < N; yy++) if (grid[x][yy]) up_dots.push_back(yy);\n                for (int yy = 0; yy < y; yy++) if (grid[x][yy]) down_dots.push_back(yy);\n                for (int xx = 0; xx < x; xx++) if (grid[xx][y]) left_dots.push_back(xx);\n                for (int xx = x+1; xx < N; xx++) if (grid[xx][y]) right_dots.push_back(xx);\n\n                if (!up_dots.empty() && !right_dots.empty()) {\n                    for (int yA : up_dots) {\n                        for (int xB : right_dots) {\n                            if (check_axis(x, y, xB, yA)) {\n                                grid[x][y] = true;\n                                dots.emplace_back(x, y);\n                                operations.push_back({x, y, x, yA, xB, yA, xB, y});\n                                mark_axis(x, y, xB, yA);\n                                found = true;\n                                break;\n                            }\n                        }\n                        if (found) break;\n                    }\n                }\n                if (!found && !up_dots.empty() && !left_dots.empty()) {\n                    for (int yA : up_dots) {\n                        for (int xB : left_dots) {\n                            if (check_axis(x, y, xB, yA)) {\n                                grid[x][y] = true;\n                                dots.emplace_back(x, y);\n                                operations.push_back({x, y, x, yA, xB, yA, xB, y});\n                                mark_axis(x, y, xB, yA);\n                                found = true;\n                                break;\n                            }\n                        }\n                        if (found) break;\n                    }\n                }\n                if (!found && !down_dots.empty() && !right_dots.empty()) {\n                    for (int yA : down_dots) {\n                        for (int xB : right_dots) {\n                            if (check_axis(x, y, xB, yA)) {\n                                grid[x][y] = true;\n                                dots.emplace_back(x, y);\n                                operations.push_back({x, y, x, yA, xB, yA, xB, y});\n                                mark_axis(x, y, xB, yA);\n                                found = true;\n                                break;\n                            }\n                        }\n                        if (found) break;\n                    }\n                }\n                if (!found && !down_dots.empty() && !left_dots.empty()) {\n                    for (int yA : down_dots) {\n                        for (int xB : left_dots) {\n                            if (check_axis(x, y, xB, yA)) {\n                                grid[x][y] = true;\n                                dots.emplace_back(x, y);\n                                operations.push_back({x, y, x, yA, xB, yA, xB, y});\n                                mark_axis(x, y, xB, yA);\n                                found = true;\n                                break;\n                            }\n                        }\n                        if (found) break;\n                    }\n                }\n            }\n\n            if (found) {\n                added_something = true;\n                break;\n            }\n        }\n    } while (added_something);\n\n    cout << operations.size() << '\\n';\n    for (auto& op : operations) {\n        cout << op.x1 << ' ' << op.y1 << ' '\n             << op.x2 << ' ' << op.y2 << ' '\n             << op.x3 << ' ' << op.y3 << ' '\n             << op.x4 << ' ' << op.y4 << '\\n';\n    }\n\n    return 0;\n}","ahc015":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\nusing namespace std;\n\nint compute_sum_squares(int grid[10][10]) {\n    bool visited[10][10] = {{false}};\n    int total = 0;\n\n    for (int i = 0; i < 10; i++) {\n        for (int j = 0; j < 10; j++) {\n            if (grid[i][j] != 0 && !visited[i][j]) {\n                int size = 0;\n                queue<pair<int, int>> q;\n                q.push({i, j});\n                visited[i][j] = true;\n                int flavor = grid[i][j];\n\n                while (!q.empty()) {\n                    auto [x, y] = q.front();\n                    q.pop();\n                    size++;\n                    const int dx[4] = {-1, 1, 0, 0};\n                    const int dy[4] = {0, 0, -1, 1};\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 < 10 && ny >= 0 && ny < 10 && !visited[nx][ny] && grid[nx][ny] == flavor) {\n                            visited[nx][ny] = true;\n                            q.push({nx, ny});\n                        }\n                    }\n                }\n                total += size * size;\n            }\n        }\n    }\n    return total;\n}\n\nint get_flavor_cluster_size(int grid[10][10], int flavor) {\n    bool visited[10][10] = {{false}};\n    int max_size = 0;\n\n    for (int i = 0; i < 10; i++) {\n        for (int j = 0; j < 10; j++) {\n            if (grid[i][j] == flavor && !visited[i][j]) {\n                int size = 0;\n                queue<pair<int, int>> q;\n                q.push({i, j});\n                visited[i][j] = true;\n\n                while (!q.empty()) {\n                    auto [x, y] = q.front();\n                    q.pop();\n                    size++;\n                    const int dx[4] = {-1, 1, 0, 0};\n                    const int dy[4] = {0, 0, -1, 1};\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 < 10 && ny >= 0 && ny < 10 && !visited[nx][ny] && grid[nx][ny] == flavor) {\n                            visited[nx][ny] = true;\n                            q.push({nx, ny});\n                        }\n                    }\n                }\n                max_size = max(max_size, size);\n            }\n        }\n    }\n    return max_size;\n}\n\nvoid tilt(int grid[10][10], char dir) {\n    int new_grid[10][10] = {{0}};\n\n    if (dir == 'F') { // Move candies toward front (row 0)\n        for (int j = 0; j < 10; j++) {\n            int write_pos = 0;\n            for (int i = 0; i < 10; i++) {\n                if (grid[i][j] != 0) {\n                    new_grid[write_pos][j] = grid[i][j];\n                    write_pos++;\n                }\n            }\n        }\n    } else if (dir == 'B') { // Move candies toward back (row 9)\n        for (int j = 0; j < 10; j++) {\n            int write_pos = 9;\n            for (int i = 9; i >= 0; i--) {\n                if (grid[i][j] != 0) {\n                    new_grid[write_pos][j] = grid[i][j];\n                    write_pos--;\n                }\n            }\n        }\n    } else if (dir == 'L') { // Move candies toward left (column 0)\n        for (int i = 0; i < 10; i++) {\n            int write_pos = 0;\n            for (int j = 0; j < 10; j++) {\n                if (grid[i][j] != 0) {\n                    new_grid[i][write_pos] = grid[i][j];\n                    write_pos++;\n                }\n            }\n        }\n    } else if (dir == 'R') { // Move candies toward right (column 9)\n        for (int i = 0; i < 10; i++) {\n            int write_pos = 9;\n            for (int j = 9; j >= 0; j--) {\n                if (grid[i][j] != 0) {\n                    new_grid[i][write_pos] = grid[i][j];\n                    write_pos--;\n                }\n            }\n        }\n    }\n\n    for (int i = 0; i < 10; i++) {\n        for (int j = 0; j < 10; j++) {\n            grid[i][j] = new_grid[i][j];\n        }\n    }\n}\n\nint main() {\n    vector<int> flavors(100);\n    for (int i = 0; i < 100; i++) {\n        cin >> flavors[i];\n    }\n\n    int grid[10][10] = {{0}};\n    vector<char> directions = {'F', 'B', 'L', 'R'};\n\n    for (int t = 0; t < 100; t++) {\n        int p;\n        cin >> p;\n\n        if (t == 99) {\n            break;\n        }\n\n        int r = (p - 1) / 10;\n        int c = (p - 1) % 10;\n        int new_flavor = flavors[t];\n        grid[r][c] = new_flavor;\n\n        char best_dir = 'F';\n        int best_score = -1;\n        int best_cluster_score = -1;\n\n        for (char dir : directions) {\n            int temp_grid[10][10];\n            for (int i = 0; i < 10; i++) {\n                for (int j = 0; j < 10; j++) {\n                    temp_grid[i][j] = grid[i][j];\n                }\n            }\n            tilt(temp_grid, dir);\n            int score = compute_sum_squares(temp_grid);\n            int cluster_score = get_flavor_cluster_size(temp_grid, new_flavor);\n\n            if (score > best_score || (score == best_score && cluster_score > best_cluster_score)) {\n                best_score = score;\n                best_cluster_score = cluster_score;\n                best_dir = dir;\n            }\n        }\n\n        cout << best_dir << '\\n';\n        cout.flush();\n        tilt(grid, best_dir);\n    }\n\n    return 0;\n}","ahc016":"#include <bits/stdc++.h>\nusing namespace std;\n\n// Function to compute the index of edge (i,j) in the string, with i<j\nlong long get_index(int i, int j, int N) {\n    return 1LL * i * (2 * N - i - 1) / 2 + (j - i - 1);\n}\n\nint main() {\n    int M;\n    double eps;\n    cin >> M >> eps;\n\n    const int N = 100;\n    const int T = N * (N - 1) / 2;\n\n    cout << N << endl;\n    cout.flush();\n\n    for (int s = 0; s < M; s++) {\n        int k;\n        if (s < 96) {\n            k = 4 + s;\n        } else {\n            k = 100 - (s - 96);\n        }\n        k = max(4, min(k, 100));\n\n        string graph(T, '0');\n        for (int i = 0; i < k; i++) {\n            for (int j = i + 1; j < k; j++) {\n                long long idx = get_index(i, j, N);\n                graph[idx] = '1';\n            }\n        }\n        cout << graph << endl;\n    }\n    cout.flush();\n\n    for (int q = 0; q < 100; q++) {\n        string H;\n        cin >> H;\n\n        vector<int> deg(N, 0);\n        for (int i = 0; i < N; i++) {\n            for (int j = i + 1; j < N; j++) {\n                long long idx = get_index(i, j, N);\n                if (H[idx] == '1') {\n                    deg[i]++;\n                    deg[j]++;\n                }\n            }\n        }\n\n        vector<int> sorted_deg = deg;\n        sort(sorted_deg.begin(), sorted_deg.end());\n\n        double best_score = 1e300;\n        int best_s = 0;\n\n        for (int s = 0; s < M; s++) {\n            int k;\n            if (s < 96) {\n                k = 4 + s;\n            } else {\n                k = 100 - (s - 96);\n            }\n            k = max(4, min(k, 100));\n\n            int low_count = N - k;\n            int high_count = k;\n\n            double expected_low = 99.0 * eps;\n            double expected_high = (k - 1) + (101 - 2 * k) * eps;\n\n            double score = 0.0;\n            if (low_count > 0) {\n                double sum_low = 0.0;\n                for (int i = 0; i < low_count; i++) {\n                    sum_low += sorted_deg[i];\n                }\n                double mean_low = sum_low / low_count;\n                score += (mean_low - expected_low) * (mean_low - expected_low);\n            }\n            if (high_count > 0) {\n                double sum_high = 0.0;\n                for (int i = low_count; i < N; i++) {\n                    sum_high += sorted_deg[i];\n                }\n                double mean_high = sum_high / high_count;\n                score += (mean_high - expected_high) * (mean_high - expected_high);\n            }\n\n            if (score < best_score) {\n                best_score = score;\n                best_s = s;\n            }\n        }\n\n        cout << best_s << endl;\n        cout.flush();\n    }\n\n    return 0;\n}","ahc017":"#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\n\nstruct Edge {\n    int u, v;\n    ll w;\n};\n\nstruct DayState {\n    int count;\n    double score;\n    int id;\n    bool operator>(const DayState& other) const {\n        if (count != other.count) return count > other.count;\n        return score > other.score;\n    }\n};\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, D, K;\n    cin >> N >> M >> D >> K;\n\n    vector<Edge> edges(M);\n    vector<vector<tuple<int, ll, int>>> adj(N);\n\n    for (int i = 0; i < M; i++) {\n        int u, v;\n        ll w;\n        cin >> u >> v >> w;\n        u--; v--;\n        edges[i] = {u, v, w};\n        adj[u].push_back({v, w, i});\n        adj[v].push_back({u, w, i});\n    }\n\n    for (int i = 0; i < N; i++) {\n        int x, y;\n        cin >> x >> y;\n    }\n\n    const ll INF = 1e18;\n    vector<double> edge_betw(M, 0.0);\n\n    for (int s = 0; s < N; s++) {\n        vector<ll> dist(N, INF);\n        vector<ll> sigma(N, 0);\n        vector<vector<pair<int, int>>> pred(N);\n        vector<double> delta(N, 0.0);\n\n        priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<>> pq;\n        dist[s] = 0;\n        sigma[s] = 1;\n        pq.push({0, s});\n\n        while (!pq.empty()) {\n            auto [d, u] = pq.top();\n            pq.pop();\n            if (d != dist[u]) continue;\n\n            for (auto [v, w, idx] : adj[u]) {\n                ll nd = d + w;\n                if (nd < dist[v]) {\n                    dist[v] = nd;\n                    sigma[v] = sigma[u];\n                    pred[v] = {{u, idx}};\n                    pq.push({nd, v});\n                } else if (nd == dist[v]) {\n                    sigma[v] += sigma[u];\n                    pred[v].push_back({u, idx});\n                }\n            }\n        }\n\n        vector<int> order;\n        for (int i = 0; i < N; i++) {\n            if (i != s && dist[i] != INF) {\n                order.push_back(i);\n            }\n        }\n        sort(order.begin(), order.end(), [&](int a, int b) {\n            return dist[a] > dist[b];\n        });\n\n        for (int w : order) {\n            for (auto [v, idx] : pred[w]) {\n                double c = static_cast<double>(sigma[v]) / sigma[w] * (1.0 + delta[w]);\n                edge_betw[idx] += c;\n                delta[v] += c;\n            }\n        }\n    }\n\n    vector<pair<double, int>> vec;\n    for (int i = 0; i < M; i++) {\n        vec.push_back({edge_betw[i], i});\n    }\n    sort(vec.begin(), vec.end(), [](const auto& a, const auto& b) {\n        return a.first > b.first;\n    });\n\n    priority_queue<DayState, vector<DayState>, greater<DayState>> pq;\n    for (int d = 0; d < D; d++) {\n        pq.push({0, 0.0, d});\n    }\n\n    vector<int> ans(M);\n    for (auto [s_val, idx] : vec) {\n        DayState day = pq.top();\n        pq.pop();\n        ans[idx] = day.id + 1;\n        day.count++;\n        day.score += s_val;\n        pq.push(day);\n    }\n\n    for (int i = 0; i < M; i++) {\n        if (i) cout << \" \";\n        cout << ans[i];\n    }\n    cout << '\\n';\n\n    return 0;\n}","ahc019":"#include<iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\n#include <tuple>\n#include <climits>\n#include <cmath>\n#include <array>\n#include <set>\n#include <map>\nusing namespace std;\n\nconst vector<array<array<int, 3>, 3>> rotation_matrices = {\n    {{\n        {1, 0, 0},\n        {0, 1, 0},\n        {0, 0, 1}\n    }},\n    {{\n        {1, 0, 0},\n        {0, 0, -1},\n        {0, 1, 0}\n    }},\n    {{\n        {1, 0, 0},\n        {0, -1, 0},\n        {0, 0, -1}\n    }},\n    {{\n        {1, 0, 0},\n        {0, 0, 1},\n        {0, -1, 0}\n    }},\n    {{\n        {0, 0, 1},\n        {0, 1, 0},\n        {-1, 0, 0}\n    }},\n    {{\n        {0, 1, 0},\n        {-1, 0, 0},\n        {0, 0, 1}\n    }},\n    {{\n        {0, 0, -1},\n        {-1, 0, 0},\n        {0, 0, -1}\n    }},\n    {{\n        {0, -1, 0},\n        {0, 0, 1},\n        {-1, 0, 0}\n    }},\n    {{\n        {0, 0, -1},\n        {0, -1, 0},\n        {1, 0, 0}\n    }},\n    {{\n        {0, 1, 0},\n        {0, 0, -1},\n        {1, 0, 0}\n    }},\n    {{\n        {0, 0, 1},\n        {1, 0, 0},\n        {0, -1, 0}\n    }},\n    {{\n        {0, -1, 0},\n        {0, 0, -1},\n        {-1, 0, 0}\n    }},\n    {{\n        {-1, 0, 0},\n        {0, 1, 0},\n        {0, 0, -1}\n    }},\n    {{\n        {-1, 0, 0},\n        {0, 0, 1},\n        {0, 1, 0}\n    }},\n    {{\n        {-1, 0, 0},\n        {0, -1, 0},\n        {0, 0, 1}\n    }},\n    {{\n        {-1, 0, 0},\n        {0, 0, -1},\n        {0, -1, 0}\n    }},\n    {{\n        {0, -1, 0},\n        {1, 0, 0},\n        {0, 0, 1}\n    }},\n    {{\n        {0, 0, -1},\n        {1, 0, 0},\n        {0, 1, 0}\n    }},\n    {{\n        {0, 1, 0},\n        {1, 0, 0},\n        {0, 0, -1}\n    }},\n    {{\n        {0, 0, 1},\n        {1, 0, 0},\n        {0, 1, 0}\n    }},\n    {{\n        {0, -1, 0},\n        {0, 0, -1},\n        {1, 0, 0}\n    }},\n    {{\n        {0, 0, -1},\n        {-1, 0, 0},\n        {0, 1, 0}\n    }},\n    {{\n        {0, 1, 0},\n        {0, 0, 1},\n        {-1, 0, 0}\n    }},\n    {{\n        {0, 0, 1},\n        {-1, 0, 0},\n        {0, -1, 0}\n    }}\n};\n\nvector<vector<tuple<int,int,int>>> get_components(vector<vector<vector<bool>>>& grid, int D) {\n    vector<vector<vector<bool>>> visited(D, vector<vector<bool>>(D, vector<bool>(D, false)));\n    vector<vector<tuple<int,int,int>>> components;\n    const int dx[6] = {1, -1, 0, 0, 0, 0};\n    const int dy[6] = {0, 0, 1, -1, 0, 0};\n    const int dz[6] = {0, 0, 0, 0, 1, -1};\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 (grid[x][y][z] && !visited[x][y][z]) {\n                    vector<tuple<int,int,int>> comp;\n                    queue<tuple<int,int,int>> q;\n                    q.push({x, y, z});\n                    visited[x][y][z] = true;\n                    while (!q.empty()) {\n                        auto [cx, cy, cz] = q.front(); q.pop();\n                        comp.push_back({cx, cy, cz});\n                        for (int i = 0; i < 6; i++) {\n                            int nx = cx + dx[i];\n                            int ny = cy + dy[i];\n                            int nz = cz + dz[i];\n                            if (nx >= 0 && nx < D && ny >= 0 && ny < D && nz >= 0 && nz < D && grid[nx][ny][nz] && !visited[nx][ny][nz]) {\n                                visited[nx][ny][nz] = true;\n                                q.push({nx, ny, nz});\n                            }\n                        }\n                    }\n                    components.push_back(comp);\n                }\n            }\n        }\n    }\n    return components;\n}\n\nbool find_placement(const vector<tuple<int,int,int>>& shape, const vector<vector<vector<bool>>>& rem, int D, int& tx, int& ty, int& tz) {\n    int w = 0, h = 0, d = 0;\n    for (auto [x,y,z] : shape) {\n        w = max(w, x+1);\n        h = max(h, y+1);\n        d = max(d, z+1);\n    }\n    for (tx = 0; tx <= D - w; tx++) {\n        for (ty = 0; ty <= D - h; ty++) {\n            for (tz = 0; tz <= D - d; tz++) {\n                bool valid = true;\n                for (auto [x,y,z] : shape) {\n                    int gx = tx + x;\n                    int gy = ty + y;\n                    int gz = tz + z;\n                    if (gx < 0 || gx >= D || gy < 0 || gy >= D || gz < 0 || gz >= D) {\n                        valid = false;\n                        break;\n                    }\n                    if (!rem[gx][gy][gz]) {\n                        valid = false;\n                        break;\n                    }\n                }\n                if (valid) {\n                    return true;\n                }\n            }\n        }\n    }\n    return false;\n}\n\nvoid place_block(vector<vector<vector<int>>>& grid, const vector<tuple<int,int,int>>& shape, int tx, int ty, int tz, int idx) {\n    for (auto [x,y,z] : shape) {\n        int gx = tx + x;\n        int gy = ty + y;\n        int gz = tz + z;\n        if (gx >= 0 && gx < (int)grid.size() && gy >=0 && gy < (int)grid[0].size() && gz >=0 && gz < (int)grid[0][0].size()) {\n            grid[gx][gy][gz] = idx;\n        }\n    }\n}\n\nvoid update_remaining(vector<vector<vector<bool>>>& rem, const vector<tuple<int,int,int>>& shape, int tx, int ty, int tz) {\n    for (auto [x,y,z] : shape) {\n        int gx = tx + x;\n        int gy = ty + y;\n        int gz = tz + z;\n        if (gx >= 0 && gx < (int)rem.size() && gy >=0 && gy < (int)rem[0].size() && gz >=0 && gz < (int)rem[0][0].size()) {\n            rem[gx][gy][gz] = false;\n        }\n    }\n}\n\nbool try_place_shared_block(vector<tuple<int,int,int>>& comp, \n                          vector<vector<vector<bool>>>& R1_rem,\n                          vector<vector<vector<bool>>>& R2_rem,\n                          int D,\n                          int& next_block_idx,\n                          vector<vector<vector<int>>>& b1_grid,\n                          vector<vector<vector<int>>>& b2_grid) {\n    vector<tuple<int,int,int>> orig_comp = comp;\n    for (const auto& rot_mat : rotation_matrices) {\n        vector<tuple<int,int,int>> rotated;\n        for (auto [x,y,z] : orig_comp) {\n            int nx = rot_mat[0][0]*x + rot_mat[0][1]*y + rot_mat[0][2]*z;\n            int ny = rot_mat[1][0]*x + rot_mat[1][1]*y + rot_mat[1][2]*z;\n            int nz = rot_mat[2][0]*x + rot_mat[2][1]*y + rot_mat[2][2]*z;\n            rotated.push_back({nx, ny, nz});\n        }\n\n        int min_x = INT_MAX, max_x = INT_MIN;\n        int min_y = INT_MAX, max_y = INT_MIN;\n        int min_z = INT_MAX, max_z = INT_MIN;\n        for (auto [x,y,z] : rotated) {\n            min_x = min(min_x, x);\n            max_x = max(max_x, x);\n            min_y = min(min_y, y);\n            max_y = max(max_y, y);\n            min_z = min(min_z, z);\n            max_z = max(max_z, z);\n        }\n        int w = max_x - min_x + 1;\n        int h = max_y - min_y + 1;\n        int d = max_z - min_z + 1;\n        if (w <= 0 || h <= 0 || d <= 0) {\n            continue;\n        }\n\n        vector<tuple<int,int,int>> normalized;\n        for (auto [x,y,z] : rotated) {\n            normalized.push_back({x - min_x, y - min_y, z - min_z});\n        }\n\n        int tx1, ty1, tz1;\n        if (!find_placement(normalized, R1_rem, D, tx1, ty1, tz1)) {\n            continue;\n        }\n        int tx2, ty2, tz2;\n        if (!find_placement(normalized, R2_rem, D, tx2, ty2, tz2)) {\n            continue;\n        }\n\n        int idx = next_block_idx++;\n        place_block(b1_grid, normalized, tx1, ty1, tz1, idx);\n        place_block(b2_grid, normalized, tx2, ty2, tz2, idx);\n        update_remaining(R1_rem, normalized, tx1, ty1, tz1);\n        update_remaining(R2_rem, normalized, tx2, ty2, tz2);\n        return true;\n    }\n    return false;\n}\n\nbool try_place_non_shared_block(vector<tuple<int,int,int>>& comp, \n                              vector<vector<vector<bool>>>& target_rem,\n                              int D,\n                              int& next_block_idx,\n                              vector<vector<vector<int>>>& target_grid) {\n    vector<tuple<int,int,int>> orig_comp = comp;\n    for (const auto& rot_mat : rotation_matrices) {\n        vector<tuple<int,int,int>> rotated;\n        for (auto [x,y,z] : orig_comp) {\n            int nx = rot_mat[0][0]*x + rot_mat[0][1]*y + rot_mat[0][2]*z;\n            int ny = rot_mat[1][0]*x + rot_mat[1][1]*y + rot_mat[1][2]*z;\n            int nz = rot_mat[2][0]*x + rot_mat[2][1]*y + rot_mat[2][2]*z;\n            rotated.push_back({nx, ny, nz});\n        }\n\n        int min_x = INT_MAX, max_x = INT_MIN;\n        int min_y = INT_MAX, max_y = INT_MIN;\n        int min_z = INT_MAX, max_z = INT_MIN;\n        for (auto [x,y,z] : rotated) {\n            min_x = min(min_x, x);\n            max_x = max(max_x, x);\n            min_y = min(min_y, y);\n            max_y = max(max_y, y);\n            min_z = min(min_z, z);\n            max_z = max(max_z, z);\n        }\n        int w = max_x - min_x + 1;\n        int h = max_y - min_y + 1;\n        int d = max_z - min_z + 1;\n        if (w <= 0 || h <= 0 || d <= 0) {\n            continue;\n        }\n\n        vector<tuple<int,int,int>> normalized;\n        for (auto [x,y,z] : rotated) {\n            normalized.push_back({x - min_x, y - min_y, z - min_z});\n        }\n\n        int tx, ty, tz;\n        if (!find_placement(normalized, target_rem, D, tx, ty, tz)) {\n            continue;\n        }\n\n        int idx = next_block_idx++;\n        place_block(target_grid, normalized, tx, ty, tz, idx);\n        update_remaining(target_rem, normalized, tx, ty, tz);\n        return true;\n    }\n    return false;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int D;\n    cin >> D;\n\n    vector<string> f1_lines(D), r1_lines(D), f2_lines(D), r2_lines(D);\n    for (int i = 0; i < D; i++) {\n        cin >> f1_lines[i];\n    }\n    for (int i = 0; i < D; i++) {\n        cin >> r1_lines[i];\n    }\n    for (int i = 0; i < D; i++) {\n        cin >> f2_lines[i];\n    }\n    for (int i = 0; i < D; i++) {\n        cin >> r2_lines[i];\n    }\n\n    vector<vector<vector<bool>>> R1_rem(D, vector<vector<bool>>(D, vector<bool>(D, false)));\n    vector<vector<vector<bool>>> R2_rem(D, vector<vector<bool>>(D, vector<bool>(D, false)));\n\n    for (int z = 0; z < D; z++) {\n        for (int x = 0; x < D; x++) {\n            for (int y = 0; y < D; y++) {\n                if (f1_lines[z][x] == '1' && r1_lines[z][y] == '1') {\n                    R1_rem[x][y][z] = true;\n                }\n                if (f2_lines[z][x] == '1' && r2_lines[z][y] == '1') {\n                    R2_rem[x][y][z] = true;\n                }\n            }\n        }\n    }\n\n    vector<vector<vector<int>>> b1_grid(D, vector<vector<int>>(D, vector<int>(D, 0)));\n    vector<vector<vector<int>>> b2_grid(D, vector<vector<int>>(D, vector<int>(D, 0)));\n\n    int next_block_idx = 1;\n\n    vector<vector<vector<bool>>> R_grid(D, vector<vector<bool>>(D, vector<bool>(D, false)));\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                R_grid[x][y][z] = R1_rem[x][y][z] && R2_rem[x][y][z];\n            }\n        }\n    }\n\n    auto shared_components = get_components(R_grid, D);\n    sort(shared_components.begin(), shared_components.end(), [](const auto& a, const auto& b) {\n        return a.size() > b.size();\n    });\n\n    for (auto& comp : shared_components) {\n        if (comp.empty()) continue;\n        if (try_place_shared_block(comp, R1_rem, R2_rem, D, next_block_idx, b1_grid, b2_grid)) {\n        }\n    }\n\n    auto comps1 = get_components(R1_rem, D);\n    sort(comps1.begin(), comps1.end(), [](const auto& a, const auto& b) {\n        return a.size() > b.size();\n    });\n    for (auto& comp : comps1) {\n        if (comp.empty()) continue;\n        if (try_place_non_shared_block(comp, R1_rem, D, next_block_idx, b1_grid)) {\n        }\n    }\n\n    auto comps2 = get_components(R2_rem, D);\n    sort(comps2.begin(), comps2.end(), [](const auto& a, const auto& b) {\n        return a.size() > b.size();\n    });\n    for (auto& comp : comps2) {\n        if (comp.empty()) continue;\n        if (try_place_non_shared_block(comp, R2_rem, D, next_block_idx, b2_grid)) {\n        }\n    }\n\n    cout << next_block_idx - 1 << \"\\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                cout << b1_grid[x][y][z];\n                if (!(x == D-1 && y == D-1 && z == D-1)) {\n                    cout << \" \";\n                }\n            }\n        }\n    }\n    cout << \"\\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                cout << b2_grid[x][y][z];\n                if (!(x == D-1 && y == D-1 && z == D-1)) {\n                    cout << \" \";\n                }\n            }\n        }\n    }\n    cout << endl;\n\n    return 0;\n}","ahc020":"#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, K;\n    cin >> N >> M >> K;\n\n    vector<ll> stations_x(N), stations_y(N);\n    for (int i = 0; i < N; i++) {\n        cin >> stations_x[i] >> stations_y[i];\n    }\n\n    vector<tuple<int, int, ll>> edges;\n    for (int i = 0; i < M; i++) {\n        int u, v;\n        ll w;\n        cin >> u >> v >> w;\n        u--; v--;\n        edges.push_back({u, v, w});\n    }\n\n    vector<ll> residents_x(K), residents_y(K);\n    for (int i = 0; i < K; i++) {\n        cin >> residents_x[i] >> residents_y[i];\n    }\n\n    vector<vector<ll>> D_mat(N, vector<ll>(K, 0));\n    for (int i = 0; i < N; i++) {\n        for (int k = 0; k < K; k++) {\n            ll dx = stations_x[i] - residents_x[k];\n            ll dy = stations_y[i] - residents_y[k];\n            D_mat[i][k] = dx*dx + dy*dy;\n        }\n    }\n\n    vector<vector<ll>> d_graph(N, vector<ll>(N, LLONG_MAX/2));\n    vector<vector<int>> next_node(N, vector<int>(N, -1));\n    for (int i = 0; i < N; i++) {\n        d_graph[i][i] = 0;\n        next_node[i][i] = i;\n    }\n    for (auto &e : edges) {\n        int u = get<0>(e);\n        int v = get<1>(e);\n        ll w = get<2>(e);\n        if (w < d_graph[u][v]) {\n            d_graph[u][v] = w;\n            d_graph[v][u] = w;\n            next_node[u][v] = v;\n            next_node[v][u] = u;\n        }\n    }\n\n    for (int k = 0; k < N; k++) {\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                if (d_graph[i][k] + d_graph[k][j] < d_graph[i][j]) {\n                    d_graph[i][j] = d_graph[i][k] + d_graph[k][j];\n                    next_node[i][j] = next_node[i][k];\n                }\n            }\n        }\n    }\n\n    auto compute_mst_cost = [&](const vector<int>& T) -> ll {\n        int n = T.size();\n        if (n <= 1) return 0;\n        vector<ll> min_edge(n, LLONG_MAX);\n        vector<bool> used(n, false);\n        min_edge[0] = 0;\n        ll total = 0;\n        for (int iter = 0; iter < n; iter++) {\n            int u = -1;\n            for (int i = 0; i < n; i++) {\n                if (!used[i] && (u == -1 || min_edge[i] < min_edge[u])) {\n                    u = i;\n                }\n            }\n            if (u == -1) break;\n            used[u] = true;\n            total += min_edge[u];\n            for (int v = 0; v < n; v++) {\n                if (!used[v]) {\n                    ll w = d_graph[T[u]][T[v]];\n                    if (w < min_edge[v]) {\n                        min_edge[v] = w;\n                    }\n                }\n            }\n        }\n        return total;\n    };\n\n    auto compute_station_cost = [&](const vector<int>& T) -> ll {\n        vector<ll> cluster_max(N, 0);\n        for (int k = 0; k < K; k++) {\n            ll minD = LLONG_MAX;\n            int best_station = -1;\n            for (int s : T) {\n                if (D_mat[s][k] < minD) {\n                    minD = D_mat[s][k];\n                    best_station = s;\n                }\n            }\n            if (D_mat[best_station][k] > cluster_max[best_station]) {\n                cluster_max[best_station] = D_mat[best_station][k];\n            }\n        }\n        ll cost = 0;\n        for (int s : T) {\n            ll maxD = cluster_max[s];\n            if (maxD == 0) continue;\n            ll p = sqrt(maxD);\n            if (p * p < maxD) {\n                p++;\n            }\n            if (p > 5000) p = 5000;\n            cost += p * p;\n        }\n        return cost;\n    };\n\n    vector<int> current_T;\n    current_T.push_back(0);\n\n    while (true) {\n        int uncovered_k = -1;\n        for (int k = 0; k < K; k++) {\n            ll minD = LLONG_MAX;\n            for (int s : current_T) {\n                if (D_mat[s][k] < minD) {\n                    minD = D_mat[s][k];\n                }\n            }\n            if (minD > 50000000LL) {\n                uncovered_k = k;\n                break;\n            }\n        }\n        if (uncovered_k == -1) break;\n\n        int best_j = -1;\n        ll best_d_euclid = LLONG_MAX;\n        ll best_d_graph_val = LLONG_MAX;\n        for (int j = 0; j < N; j++) {\n            if (find(current_T.begin(), current_T.end(), j) != current_T.end()) \n                continue;\n\n            ll d_euclid = D_mat[j][uncovered_k];\n            ll d_graph_val = LLONG_MAX;\n            for (int s : current_T) {\n                if (d_graph[s][j] < d_graph_val) {\n                    d_graph_val = d_graph[s][j];\n                }\n            }\n            if (best_j == -1 || d_euclid < best_d_euclid || (d_euclid == best_d_euclid && d_graph_val < best_d_graph_val)) {\n                best_j = j;\n                best_d_euclid = d_euclid;\n                best_d_graph_val = d_graph_val;\n            }\n        }\n        if (best_j == -1) break;\n        current_T.push_back(best_j);\n    }\n\n    for (int iter = 0; iter < 10; iter++) {\n        bool improved = false;\n        ll best_total_after_removal = LLONG_MAX;\n        vector<int> candidate_T_after_removal;\n\n        for (int idx = 1; idx < current_T.size(); idx++) {\n            int i = current_T[idx];\n            bool valid = true;\n            for (int k = 0; k < K; k++) {\n                ll minD_orig = LLONG_MAX;\n                ll minD_after = LLONG_MAX;\n                for (int s : current_T) {\n                    if (D_mat[s][k] < minD_orig) minD_orig = D_mat[s][k];\n                    if (s == i) continue;\n                    if (D_mat[s][k] < minD_after) minD_after = D_mat[s][k];\n                }\n                if (minD_orig == D_mat[i][k] && minD_after > 50000000LL) {\n                    valid = false;\n                    break;\n                }\n            }\n            if (!valid) continue;\n\n            vector<int> candidate_T;\n            for (int s : current_T) {\n                if (s != i) candidate_T.push_back(s);\n            }\n\n            ll edge_cost_candidate = compute_mst_cost(candidate_T);\n            ll station_cost_candidate = compute_station_cost(candidate_T);\n            ll total_candidate = edge_cost_candidate + station_cost_candidate;\n\n            ll current_total = compute_mst_cost(current_T) + compute_station_cost(current_T);\n            if (total_candidate < current_total) {\n                if (total_candidate < best_total_after_removal) {\n                    best_total_after_removal = total_candidate;\n                    candidate_T_after_removal = candidate_T;\n                }\n            }\n        }\n\n        if (best_total_after_removal != LLONG_MAX) {\n            current_T = candidate_T_after_removal;\n            improved = true;\n        }\n\n        if (improved) continue;\n\n        best_total_after_removal = LLONG_MAX;\n        vector<int> candidate_T_after_add;\n\n        for (int j = 0; j < N; j++) {\n            if (find(current_T.begin(), current_T.end(), j) != current_T.end()) \n                continue;\n\n            vector<int> candidate_T = current_T;\n            candidate_T.push_back(j);\n\n            ll edge_cost_candidate = compute_mst_cost(candidate_T);\n            ll station_cost_candidate = compute_station_cost(candidate_T);\n            ll total_candidate = edge_cost_candidate + station_cost_candidate;\n\n            ll current_total = compute_mst_cost(current_T) + compute_station_cost(current_T);\n            if (total_candidate < current_total) {\n                if (total_candidate < best_total_after_removal) {\n                    best_total_after_removal = total_candidate;\n                    candidate_T_after_add = candidate_T;\n                }\n            }\n        }\n\n        if (best_total_after_removal != LLONG_MAX) {\n            current_T = candidate_T_after_add;\n            improved = true;\n        }\n\n        if (!improved) break;\n    }\n\n    vector<int> assignment(K, -1);\n    vector<ll> maxD_in_cluster(N, 0);\n    for (int k = 0; k < K; k++) {\n        ll minD = LLONG_MAX;\n        int best_station = -1;\n        for (int s : current_T) {\n            if (D_mat[s][k] < minD) {\n                minD = D_mat[s][k];\n                best_station = s;\n            }\n        }\n        assignment[k] = best_station;\n        if (D_mat[best_station][k] > maxD_in_cluster[best_station]) {\n            maxD_in_cluster[best_station] = D_mat[best_station][k];\n        }\n    }\n\n    vector<int> P(N, 0);\n    for (int i = 0; i < N; i++) {\n        if (find(current_T.begin(), current_T.end(), i) == current_T.end()) {\n            P[i] = 0;\n        } else {\n            ll maxD = maxD_in_cluster[i];\n            if (maxD == 0) {\n                P[i] = 0;\n            } else {\n                ll p = sqrt(maxD);\n                if (p * p < maxD) {\n                    p++;\n                }\n                if (p > 5000) p = 5000;\n                P[i] = (int)p;\n            }\n        }\n    }\n\n    auto compute_mst_edges = [&](const vector<int>& T) -> vector<pair<int, int>> {\n        int n = T.size();\n        if (n <= 1) return {};\n        vector<ll> min_edge(n, LLONG_MAX);\n        vector<int> parent(n, -1);\n        vector<bool> used(n, false);\n        min_edge[0] = 0;\n        for (int iter = 0; iter < n; iter++) {\n            int u = -1;\n            for (int i = 0; i < n; i++) {\n                if (!used[i] && (u == -1 || min_edge[i] < min_edge[u])) {\n                    u = i;\n                }\n            }\n            if (u == -1) break;\n            used[u] = true;\n            for (int v = 0; v < n; v++) {\n                if (!used[v]) {\n                    ll w = d_graph[T[u]][T[v]];\n                    if (w < min_edge[v]) {\n                        min_edge[v] = w;\n                        parent[v] = u;\n                    }\n                }\n            }\n        }\n        vector<pair<int, int>> edges;\n        for (int i = 1; i < n; i++) {\n            int u_idx = parent[i];\n            edges.push_back({T[u_idx], T[i]});\n        }\n        return edges;\n    };\n\n    auto get_path = [&](int start, int end) -> vector<int> {\n        vector<int> path;\n        int u = start;\n        while (u != end) {\n            path.push_back(u);\n            u = next_node[u][end];\n        }\n        path.push_back(end);\n        return path;\n    };\n\n    vector<pair<int, int>> mst_edges = compute_mst_edges(current_T);\n    vector<bool> edge_used(M, false);\n    for (auto &e : mst_edges) {\n        int u = e.first;\n        int v = e.second;\n        vector<int> path = get_path(u, v);\n        for (int idx = 0; idx < (int)path.size()-1; idx++) {\n            int a = path[idx];\n            int b = path[idx+1];\n            for (int j = 0; j < M; j++) {\n                int u1 = get<0>(edges[j]);\n                int v1 = get<1>(edges[j]);\n                if ((u1 == a && v1 == b) || (u1 == b && v1 == a)) {\n                    edge_used[j] = true;\n                    break;\n                }\n            }\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    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 <bits/stdc++.h>\nusing namespace std;\n\ninline int get_idx(int x, int y) {\n    return x * (x + 1) / 2 + y;\n}\n\nint main() {\n    vector<int> pyramid(465);\n    vector<array<int, 2>> pos(465);\n\n    int idx = 0;\n    for (int x = 0; x < 30; x++) {\n        for (int y = 0; y <= x; y++) {\n            cin >> pyramid[idx];\n            pos[pyramid[idx]] = {x, y};\n            idx++;\n        }\n    }\n\n    vector<array<int, 4>> swaps;\n\n    for (int val = 464; val >= 0; val--) {\n        int x = pos[val][0];\n        int y = pos[val][1];\n        if (x == 29) continue;\n\n        while (true) {\n            int left_x = x + 1;\n            int left_y = y;\n            int right_x = x + 1;\n            int right_y = y + 1;\n\n            int left_idx = get_idx(left_x, left_y);\n            int right_idx = get_idx(right_x, right_y);\n            int left_val = pyramid[left_idx];\n            int right_val = pyramid[right_idx];\n\n            if (val <= left_val && val <= right_val) {\n                break;\n            }\n\n            int swap_x, swap_y;\n            if (left_val <= right_val) {\n                swap_x = left_x;\n                swap_y = left_y;\n            } else {\n                swap_x = right_x;\n                swap_y = right_y;\n            }\n            int swap_idx = get_idx(swap_x, swap_y);\n            int swapped_val = pyramid[swap_idx];\n\n            swaps.push_back({x, y, swap_x, swap_y});\n            swap(pyramid[get_idx(x, y)], pyramid[swap_idx]);\n\n            pos[val] = {swap_x, swap_y};\n            pos[swapped_val] = {x, y};\n\n            x = swap_x;\n            y = swap_y;\n            if (x == 29) break;\n        }\n    }\n\n    cout << swaps.size() << '\\n';\n    for (auto& s : swaps) {\n        cout << s[0] << ' ' << s[1] << ' ' << s[2] << ' ' << s[3] << '\\n';\n    }\n    return 0;\n}","toyota2023summer-final":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\n#include <tuple>\n#include <set>\n#include <cctype>\n#include <cmath>\nusing namespace std;\n\nint main() {\n    int D, N;\n    cin >> D >> N;\n    vector<vector<bool>> is_obstacle(9, vector<bool>(9, false));\n    set<pair<int, int>> non_storage = {{0, 4}};\n\n    for (int idx = 0; idx < N; idx++) {\n        int ri, rj;\n        cin >> ri >> rj;\n        is_obstacle[ri][rj] = true;\n    }\n\n    vector<pair<int, int>> storage_cells;\n    for (int i = 0; i < 9; i++) {\n        for (int j = 0; j < 9; j++) {\n            if (non_storage.find({i, j}) != non_storage.end()) continue;\n            if (is_obstacle[i][j]) continue;\n            storage_cells.push_back({i, j});\n        }\n    }\n\n    vector<vector<int>> dist(9, vector<int>(9, -1));\n    queue<pair<int, int>> q_bfs;\n    dist[0][4] = 0;\n    q_bfs.push({0, 4});\n    const vector<int> dx = {1, -1, 0, 0};\n    const vector<int> dy = {0, 0, 1, -1};\n\n    while (!q_bfs.empty()) {\n        auto [i, j] = q_bfs.front();\n        q_bfs.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 < 9 && nj >= 0 && nj < 9) {\n                if (dist[ni][nj] == -1 && !is_obstacle[ni][nj]) {\n                    dist[ni][nj] = dist[i][j] + 1;\n                    q_bfs.push({ni, nj});\n                }\n            }\n        }\n    }\n\n    sort(storage_cells.begin(), storage_cells.end(), [&](const pair<int, int>& a, const pair<int, int>& b) {\n        if (dist[a.first][a.second] != dist[b.first][b.second]) {\n            return dist[a.first][a.second] > dist[b.first][b.second];\n        }\n        if (a.first != b.first) {\n            return a.first < b.first;\n        }\n        return a.second < b.second;\n    });\n\n    int M = storage_cells.size();\n    vector<vector<int>> container_id(9, vector<int>(9, -1));\n\n    for (int d = 0; d < M; d++) {\n        int t;\n        cin >> t;\n        int index = M - 1 - t;\n        auto [i, j] = storage_cells[index];\n        cout << i << \" \" << j << endl;\n        container_id[i][j] = t;\n    }\n\n    vector<vector<bool>> is_storage(9, vector<bool>(9, false));\n    for (auto [i, j] : storage_cells) {\n        is_storage[i][j] = true;\n    }\n\n    vector<vector<bool>> is_removed(9, vector<bool>(9, false));\n    vector<pair<int, int>> transport_order;\n\n    for (int step = 0; step < M; step++) {\n        vector<vector<bool>> visited(9, vector<bool>(9, false));\n        queue<pair<int, int>> q;\n        if (!is_obstacle[0][4]) {\n            visited[0][4] = true;\n            q.push({0, 4});\n        }\n\n        while (!q.empty()) {\n            auto [i, j] = q.front();\n            q.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 >= 9 || nj < 0 || nj >= 9) continue;\n                if (visited[ni][nj]) continue;\n                if (is_obstacle[ni][nj]) continue;\n                if (is_storage[ni][nj] && !is_removed[ni][nj]) {\n                    continue;\n                }\n                visited[ni][nj] = true;\n                q.push({ni, nj});\n            }\n        }\n\n        vector<tuple<int, int, int>> candidates;\n        for (int i = 0; i < 9; i++) {\n            for (int j = 0; j < 9; j++) {\n                if (!is_storage[i][j]) continue;\n                if (is_removed[i][j]) continue;\n                bool has_visited_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 < 9 && nj >= 0 && nj < 9) {\n                        if (visited[ni][nj]) {\n                            has_visited_neighbor = true;\n                            break;\n                        }\n                    }\n                }\n                if (has_visited_neighbor) {\n                    candidates.push_back(make_tuple(container_id[i][j], i, j));\n                }\n            }\n        }\n\n        sort(candidates.begin(), candidates.end());\n        auto [id_val, i_val, j_val] = candidates[0];\n        transport_order.push_back({i_val, j_val});\n        is_removed[i_val][j_val] = true;\n    }\n\n    for (auto [i, j] : transport_order) {\n        cout << i << \" \" << j << endl;\n    }\n\n    return 0;\n}","ahc024":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int n = 50;\nconst int m = 100;\nconst vector<pair<int, int>> directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};\n\nint main() {\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<set<int>> adj(m + 1);\n    vector<bool> boundary(m + 1, false);\n\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            int c = grid[i][j];\n            if (i == 0 || i == n - 1 || j == 0 || j == n - 1) {\n                boundary[c] = true;\n            }\n            for (auto [dx, dy] : directions) {\n                int ni = i + dx;\n                int nj = j + dy;\n                if (ni >= 0 && ni < n && nj >= 0 && nj < n) {\n                    int d = grid[ni][nj];\n                    if (d != c) {\n                        adj[c].insert(d);\n                        adj[d].insert(c);\n                    }\n                }\n            }\n        }\n    }\n\n    vector<pair<int, int>> boundary_positions;\n    for (int j = 0; j < n; j++) boundary_positions.push_back({n - 1, j});\n    for (int i = n - 2; i >= 0; i--) boundary_positions.push_back({i, n - 1});\n    for (int j = n - 2; j >= 0; j--) boundary_positions.push_back({0, j});\n    for (int i = 1; i < n - 1; i++) boundary_positions.push_back({i, 0});\n\n    vector<set<pair<int, int>>> regions(m + 1);\n    vector<queue<pair<int, int>>> frontiers(m + 1);\n    vector<vector<int>> ans(n, vector<int>(n, 0));\n    vector<bool> placed(m + 1, false);\n\n    int pos_index = 0;\n    for (int c = 1; c <= m; c++) {\n        if (boundary[c]) {\n            if (pos_index >= (int)boundary_positions.size()) break;\n            auto [i, j] = boundary_positions[pos_index++];\n            ans[i][j] = c;\n            regions[c].insert({i, j});\n            frontiers[c].push({i, j});\n            placed[c] = true;\n        }\n    }\n\n    vector<vector<int>> adj_list(m + 1);\n    for (int c = 1; c <= m; c++) {\n        for (int d : adj[c]) {\n            if (d >= 1 && d <= m) {\n                adj_list[c].push_back(d);\n            }\n        }\n    }\n\n    queue<int> q;\n    for (int c = 1; c <= m; c++) {\n        if (placed[c]) {\n            q.push(c);\n        }\n    }\n\n    auto try_place_edge = [&](int c, int d) -> bool {\n        while (!frontiers[c].empty()) {\n            auto cell = frontiers[c].front();\n            vector<pair<int, int>> free_dirs;\n            for (auto [dx, dy] : directions) {\n                int ni = cell.first + dx;\n                int nj = cell.second + dy;\n                if (ni >= 0 && ni < n && nj >= 0 && nj < n && ans[ni][nj] == 0) {\n                    free_dirs.push_back({dx, dy});\n                }\n            }\n            if (!free_dirs.empty()) {\n                auto [dx, dy] = free_dirs[0];\n                int ni = cell.first + dx;\n                int nj = cell.second + dy;\n                ans[ni][nj] = d;\n                regions[d].insert({ni, nj});\n                frontiers[d].push({ni, nj});\n                return true;\n            } else {\n                frontiers[c].pop();\n            }\n        }\n\n        for (auto cell : regions[c]) {\n            for (auto [dx, dy] : directions) {\n                int ni = cell.first + dx;\n                int nj = cell.second + dy;\n                if (ni >= 0 && ni < n && nj >= 0 && nj < n && ans[ni][nj] == 0) {\n                    ans[ni][nj] = c;\n                    regions[c].insert({ni, nj});\n                    frontiers[c].push({ni, nj});\n                    return try_place_edge(c, d);\n                }\n            }\n        }\n        return false;\n    };\n\n    while (!q.empty()) {\n        int c = q.front();\n        q.pop();\n        for (int d : adj_list[c]) {\n            if (placed[d]) continue;\n            if (try_place_edge(c, d)) {\n                placed[d] = true;\n                q.push(d);\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 << ans[i][j];\n        }\n        cout << '\\n';\n    }\n\n    return 0;\n}","ahc025":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(0);\n    cin.tie(0);\n\n    int N, D, Q;\n    cin >> N >> D >> Q;\n\n    vector<double> weight_est(N, 1.0);\n    vector<vector<int>> comparisons;\n    int queries_used = 0;\n\n    auto make_query = [&](const vector<int>& L, const vector<int>& R) -> string {\n        cout << L.size() << \" \" << R.size();\n        for (int x : L) cout << \" \" << x;\n        for (int x : R) cout << \" \" << x;\n        cout << endl;\n        string res;\n        cin >> res;\n        queries_used++;\n        return res;\n    };\n\n    auto update_weights = [&](const vector<int>& L, const vector<int>& R, const string& res) {\n        if (res == \"=\") return;\n        double factor = 1.01;\n        if (res == \"<\") {\n            for (int x : L) weight_est[x] *= 0.99;\n            for (int x : R) weight_est[x] *= 1.01;\n        } else {\n            for (int x : L) weight_est[x] *= 1.01;\n            for (int x : R) weight_est[x] *= 0.99;\n        }\n    };\n\n    for (int i = 0; i < N && queries_used < Q; i++) {\n        for (int j = i + 1; j < N && queries_used < Q; j++) {\n            vector<int> L = {i}, R = {j};\n            string res = make_query(L, R);\n            update_weights(L, R, res);\n        }\n    }\n\n    while (queries_used < Q) {\n        vector<int> L = {0}, R = {1};\n        string res = make_query(L, R);\n        update_weights(L, R, res);\n    }\n\n    vector<int> indices(N);\n    iota(indices.begin(), indices.end(), 0);\n    sort(indices.begin(), indices.end(), [&](int a, int b) {\n        return weight_est[a] > weight_est[b];\n    });\n\n    vector<long long> set_sum(D, 0);\n    vector<int> assignment(N, -1);\n\n    for (int idx = 0; idx < N; idx++) {\n        int item = indices[idx];\n        int best_set = 0;\n        for (int s = 1; s < D; s++) {\n            if (set_sum[s] < set_sum[best_set]) {\n                best_set = s;\n            }\n        }\n        assignment[item] = best_set;\n        set_sum[best_set] += (long long)(weight_est[item] * 1000000);\n    }\n\n    for (int i = 0; i < N; i++) {\n        if (i) cout << \" \";\n        cout << assignment[i];\n    }\n    cout << endl;\n\n    return 0;\n}","ahc026":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int n, m;\n    cin >> n >> m;\n    int k = n / m;\n\n    vector<vector<int>> stacks(m);\n    for (int i = 0; i < m; i++) {\n        stacks[i].resize(k);\n        for (int j = 0; j < k; j++) {\n            cin >> stacks[i][j];\n        }\n    }\n\n    vector<int> empty_stacks;\n    vector<pair<int, int>> ops;\n    int cur = 1;\n\n    while (cur <= n) {\n        bool carried = false;\n        for (int s = 0; s < m; s++) {\n            if (stacks[s].empty()) continue;\n            if (stacks[s].back() == cur) {\n                ops.push_back({cur, 0});\n                stacks[s].pop_back();\n                if (stacks[s].empty()) {\n                    empty_stacks.push_back(s);\n                }\n                cur++;\n                carried = true;\n                break;\n            }\n        }\n        if (carried) {\n            continue;\n        }\n\n        int src = -1;\n        int pos = -1;\n        for (int s = 0; s < m; s++) {\n            auto it = find(stacks[s].begin(), stacks[s].end(), cur);\n            if (it != stacks[s].end()) {\n                src = s;\n                pos = it - stacks[s].begin();\n                break;\n            }\n        }\n        if (src == -1) {\n            break;\n        }\n\n        vector<int> segment(stacks[src].begin() + pos + 1, stacks[src].end());\n        int T = segment.back();\n        int dest = -1;\n\n        if (!empty_stacks.empty()) {\n            dest = empty_stacks.back();\n            empty_stacks.pop_back();\n        } else {\n            int candidate_large = -1;\n            int candidate_small = -1;\n            for (int s = 0; s < m; s++) {\n                if (stacks[s].empty()) continue;\n                int top_val = stacks[s].back();\n                if (top_val > T) {\n                    if (candidate_large == -1 || top_val > stacks[candidate_large].back()) {\n                        candidate_large = s;\n                    }\n                }\n                if (candidate_small == -1 || top_val < stacks[candidate_small].back()) {\n                    candidate_small = s;\n                }\n            }\n            if (candidate_large != -1) {\n                dest = candidate_large;\n            } else if (candidate_small != -1) {\n                dest = candidate_small;\n            } else {\n                dest = 0;\n            }\n        }\n\n        int v = segment[0];\n        stacks[src].resize(pos + 1);\n        for (int box : segment) {\n            stacks[dest].push_back(box);\n        }\n        ops.push_back({v, dest + 1});\n    }\n\n    for (auto &op : ops) {\n        cout << op.first << \" \" << op.second << \"\\n\";\n    }\n\n    return 0;\n}","ahc027":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N;\n    cin >> N;\n\n    vector<string> h(N-1);\n    for (int i = 0; i < N-1; i++) {\n        cin >> h[i];\n    }\n\n    vector<string> v(N);\n    for (int i = 0; i < N; i++) {\n        cin >> v[i];\n    }\n\n    vector<vector<int>> d(N, vector<int>(N));\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cin >> d[i][j];\n        }\n    }\n\n    vector<vector<vector<tuple<int, int, char>>>> neighbors(N, vector<vector<tuple<int, int, char>>>(N));\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (j < N-1 && v[i][j] == '0') {\n                neighbors[i][j].emplace_back(i, j+1, 'R');\n            }\n            if (i < N-1 && h[i][j] == '0') {\n                neighbors[i][j].emplace_back(i+1, j, 'D');\n            }\n            if (j > 0 && v[i][j-1] == '0') {\n                neighbors[i][j].emplace_back(i, j-1, 'L');\n            }\n            if (i > 0 && h[i-1][j] == '0') {\n                neighbors[i][j].emplace_back(i-1, j, 'U');\n            }\n            sort(neighbors[i][j].begin(), neighbors[i][j].end(), [&](const auto& a, const auto& b) {\n                int da = d[get<0>(a)][get<1>(a)];\n                int db = d[get<0>(b)][get<1>(b)];\n                return da > db;\n            });\n        }\n    }\n\n    vector<vector<bool>> visited(N, vector<bool>(N, false));\n    visited[0][0] = true;\n\n    stack<tuple<int, int, int>> st;\n    st.emplace(0, 0, 0);\n    string route;\n\n    while (!st.empty()) {\n        auto& [i, j, idx] = st.top();\n        if (idx < (int)neighbors[i][j].size()) {\n            auto [ni, nj, move_char] = neighbors[i][j][idx];\n            idx++;\n            if (!visited[ni][nj]) {\n                visited[ni][nj] = true;\n                st.emplace(ni, nj, 0);\n                route += move_char;\n            }\n        } else {\n            st.pop();\n            if (!st.empty()) {\n                auto [pi, pj, _] = st.top();\n                int di = i - pi;\n                int dj = j - pj;\n                char rev_char;\n                if (di == 1) rev_char = 'U';\n                else if (di == -1) rev_char = 'D';\n                else if (dj == 1) rev_char = 'L';\n                else rev_char = 'R';\n                route += rev_char;\n            }\n        }\n    }\n\n    cout << route << endl;\n\n    return 0;\n}","ahc028":"#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(0);\n\n    int N, M;\n    cin >> N >> M;\n    int s_i, s_j;\n    cin >> s_i >> s_j;\n    vector<string> grid(N);\n    for (int i = 0; i < N; i++) {\n        cin >> grid[i];\n    }\n    vector<string> t_list(M);\n    for (int i = 0; i < M; i++) {\n        cin >> t_list[i];\n    }\n\n    unordered_map<string, int> five_gram_map;\n    for (int i = 0; i < M; i++) {\n        five_gram_map[t_list[i]] = i;\n    }\n\n    unordered_map<string, int> prefix_count_map;\n    for (int i = 0; i < M; i++) {\n        string prefix = t_list[i].substr(0, 4);\n        prefix_count_map[prefix]++;\n    }\n\n    const int NUM_RESTARTS = 5;\n    const int MAX_STEPS_PER_RESTART = 3000;\n    int best_covered = -1;\n    ll best_cost = LLONG_MAX;\n    vector<pair<int, int>> best_path;\n\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n\n    for (int restart = 0; restart < NUM_RESTARTS; restart++) {\n        auto local_prefix_count_map = prefix_count_map;\n        vector<bool> local_covered(M, false);\n        int local_covered_count = 0;\n        string suffix(4, '#');\n        int cur_i = s_i;\n        int cur_j = s_j;\n        vector<pair<int, int>> path;\n        ll total_cost = 0;\n\n        for (int step = 0; step < MAX_STEPS_PER_RESTART; step++) {\n            if (local_covered_count == M) {\n                break;\n            }\n\n            vector<tuple<int, int, string, int>> gain_candidates;\n            int best_gain_cost = INT_MAX;\n\n            vector<tuple<int, int, string>> heuristic_candidates;\n            int best_heuristic_val = -1;\n            int best_heuristic_cost = INT_MAX;\n\n            for (int i = 0; i < N; i++) {\n                for (int j = 0; j < N; j++) {\n                    char c = grid[i][j];\n                    string new_suffix = suffix.substr(1) + c;\n\n                    bool forms_5gram = true;\n                    for (char ch : suffix) {\n                        if (ch == '#') {\n                            forms_5gram = false;\n                            break;\n                        }\n                    }\n\n                    int gain = 0;\n                    int gain_index = -1;\n                    if (forms_5gram) {\n                        string five_gram = suffix + string(1, c);\n                        auto it = five_gram_map.find(five_gram);\n                        if (it != five_gram_map.end()) {\n                            int idx = it->second;\n                            if (!local_covered[idx]) {\n                                gain = 1;\n                                gain_index = idx;\n                            }\n                        }\n                    }\n\n                    int move_cost = abs(i - cur_i) + abs(j - cur_j) + 1;\n\n                    if (gain > 0) {\n                        if (move_cost < best_gain_cost) {\n                            best_gain_cost = move_cost;\n                            gain_candidates.clear();\n                            gain_candidates.push_back(make_tuple(i, j, new_suffix, gain_index));\n                        } else if (move_cost == best_gain_cost) {\n                            gain_candidates.push_back(make_tuple(i, j, new_suffix, gain_index));\n                        }\n                    } else {\n                        int heuristic_val = 0;\n                        bool valid = true;\n                        for (char ch : new_suffix) {\n                            if (ch < 'A' || ch > 'Z') {\n                                valid = false;\n                                break;\n                            }\n                        }\n                        if (valid) {\n                            auto it = local_prefix_count_map.find(new_suffix);\n                            if (it != local_prefix_count_map.end()) {\n                                heuristic_val = it->second;\n                            }\n                        }\n\n                        if (heuristic_val > best_heuristic_val) {\n                            best_heuristic_val = heuristic_val;\n                            best_heuristic_cost = move_cost;\n                            heuristic_candidates.clear();\n                            heuristic_candidates.push_back(make_tuple(i, j, new_suffix));\n                        } else if (heuristic_val == best_heuristic_val) {\n                            if (move_cost < best_heuristic_cost) {\n                                best_heuristic_cost = move_cost;\n                                heuristic_candidates.clear();\n                                heuristic_candidates.push_back(make_tuple(i, j, new_suffix));\n                            } else if (move_cost == best_heuristic_cost) {\n                                heuristic_candidates.push_back(make_tuple(i, j, new_suffix));\n                            }\n                        }\n                    }\n                }\n            }\n\n            int chosen_i = -1, chosen_j = -1;\n            string chosen_suffix = \"\";\n            int gain_idx = -1;\n\n            if (!gain_candidates.empty()) {\n                uniform_int_distribution<int> dist(0, (int)gain_candidates.size() - 1);\n                auto candidate = gain_candidates[dist(rng)];\n                chosen_i = get<0>(candidate);\n                chosen_j = get<1>(candidate);\n                chosen_suffix = get<2>(candidate);\n                gain_idx = get<3>(candidate);\n            } else {\n                if (heuristic_candidates.empty()) {\n                    uniform_int_distribution<int> dist(0, N*N-1);\n                    int idx = dist(rng);\n                    chosen_i = idx / N;\n                    chosen_j = idx % N;\n                    chosen_suffix = suffix.substr(1) + grid[chosen_i][chosen_j];\n                } else {\n                    uniform_int_distribution<int> dist(0, (int)heuristic_candidates.size() - 1);\n                    auto candidate = heuristic_candidates[dist(rng)];\n                    chosen_i = get<0>(candidate);\n                    chosen_j = get<1>(candidate);\n                    chosen_suffix = get<2>(candidate);\n                }\n            }\n\n            int move_cost = abs(chosen_i - cur_i) + abs(chosen_j - cur_j) + 1;\n            total_cost += move_cost;\n\n            cur_i = chosen_i;\n            cur_j = chosen_j;\n            suffix = chosen_suffix;\n\n            if (gain_idx != -1) {\n                local_covered[gain_idx] = true;\n                local_covered_count++;\n                string prefix = t_list[gain_idx].substr(0, 4);\n                local_prefix_count_map[prefix]--;\n                if (local_prefix_count_map[prefix] == 0) {\n                    local_prefix_count_map.erase(prefix);\n                }\n            }\n\n            path.push_back({chosen_i, chosen_j});\n        }\n\n        if (local_covered_count > best_covered || (local_covered_count == best_covered && total_cost < best_cost)) {\n            best_covered = local_covered_count;\n            best_cost = total_cost;\n            best_path = path;\n        }\n    }\n\n    cout << best_path.size() << \"\\n\";\n    for (auto &p : best_path) {\n        cout << p.first << \" \" << p.second << \"\\n\";\n    }\n\n    return 0;\n}","ahc030":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <tuple>\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<tuple<vector<pair<int, int>>, int, int>> fields;\n    for (int k = 0; k < M; k++) {\n        int d;\n        cin >> d;\n        vector<pair<int, int>> shape;\n        int max_i = 0, max_j = 0;\n        for (int i = 0; i < d; i++) {\n            int a, b;\n            cin >> a >> b;\n            shape.emplace_back(a, b);\n            if (a > max_i) max_i = a;\n            if (b > max_j) max_j = b;\n        }\n        fields.emplace_back(shape, max_i, max_j);\n    }\n\n    priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<>> pq;\n    int center = N / 2;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            int dist = abs(i - center) + abs(j - center);\n            pq.emplace(dist, i, j);\n        }\n    }\n\n    vector<vector<int>> status(N, vector<int>(N, 0));\n    vector<pair<int, int>> positive_list;\n\n    while (!pq.empty()) {\n        auto [dist, i, j] = pq.top();\n        pq.pop();\n\n        if (status[i][j] != 0) {\n            continue;\n        }\n\n        cout << \"q 1 \" << i << \" \" << j << endl;\n        cout.flush();\n\n        int v;\n        cin >> v;\n\n        if (v > 0) {\n            status[i][j] = 1;\n            positive_list.emplace_back(i, j);\n\n            for (const auto& field : fields) {\n                const auto& shape = get<0>(field);\n                int max_i = get<1>(field);\n                int max_j = get<2>(field);\n\n                for (const auto& offset : shape) {\n                    int a = offset.first;\n                    int b = offset.second;\n                    int p = i - a;\n                    int q_val = j - b;\n                    if (p < 0 || q_val < 0) continue;\n                    if (p + max_i >= N || q_val + max_j >= N) continue;\n\n                    for (const auto& cell : shape) {\n                        int i2 = p + cell.first;\n                        int j2 = q_val + cell.second;\n                        if (i2 < N && j2 < N && status[i2][j2] == 0) {\n                            int dist2 = abs(i2 - center) + abs(j2 - center);\n                            pq.emplace(dist2, i2, j2);\n                        }\n                    }\n                }\n            }\n        } else {\n            status[i][j] = 2;\n        }\n    }\n\n    cout << \"a \" << positive_list.size();\n    for (const auto& pos : positive_list) {\n        cout << \" \" << pos.first << \" \" << pos.second;\n    }\n    cout << endl;\n    cout.flush();\n\n    int result;\n    cin >> result;\n    return 0;\n}","ahc031":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Rect {\n    int y1, x1, y2, x2;\n    int area() const { return (y2 - y1) * (x2 - x1); }\n};\n\nbool overlap(const Rect& a, const Rect& b) {\n    return !(a.y2 <= b.y1 || b.y2 <= a.y1 || a.x2 <= b.x1 || b.x1 >= a.x2);\n}\n\nvector<Rect> split_free_space(const Rect& placed, const Rect& free_space) {\n    vector<Rect> splits;\n    if (free_space.y1 < placed.y1) {\n        splits.push_back({free_space.y1, free_space.x1, placed.y1, free_space.x2});\n    }\n    if (placed.y2 < free_space.y2) {\n        splits.push_back({placed.y2, free_space.x1, free_space.y2, free_space.x2});\n    }\n    if (free_space.x1 < placed.x1) {\n        int y1 = max(free_space.y1, placed.y1);\n        int y2 = min(free_space.y2, placed.y2);\n        if (y1 < y2) {\n            splits.push_back({y1, free_space.x1, y2, placed.x1});\n        }\n    }\n    if (placed.x2 < free_space.x2) {\n        int y1 = max(free_space.y1, placed.y1);\n        int y2 = min(free_space.y2, placed.y2);\n        if (y1 < y2) {\n            splits.push_back({y1, placed.x2, y2, free_space.x2});\n        }\n    }\n    return splits;\n}\n\nvector<Rect> pack_day(int W, const vector<int>& areas, const vector<Rect>& prev_rects) {\n    int N = areas.size();\n    vector<Rect> free_space = {{0, 0, W, W}};\n    vector<Rect> result(N, {0, 0, 1, 1});\n    vector<bool> placed(N, false);\n\n    for (int k = 0; k < N; k++) {\n        int req_area = areas[k];\n        Rect best_fit;\n        int best_fit_idx = -1;\n        double best_score = -1.0;\n\n        for (int i = 0; i < (int)free_space.size(); i++) {\n            Rect f = free_space[i];\n            if (f.area() < req_area) continue;\n\n            double score = 0.0;\n            if (k < (int)prev_rects.size()) {\n                Rect p = prev_rects[k];\n                int overlap_y1 = max(f.y1, p.y1);\n                int overlap_y2 = min(f.y2, p.y2);\n                int overlap_x1 = max(f.x1, p.x1);\n                int overlap_x2 = min(f.x2, p.x2);\n                if (overlap_y1 < overlap_y2 && overlap_x1 < overlap_x2) {\n                    score = -1.0 * (overlap_y2 - overlap_y1) * (overlap_x2 - overlap_x1);\n                } else {\n                    int center_dist = abs((f.y1 + f.y2)/2 - (p.y1 + p.y2)/2) +\n                                    abs((f.x1 + f.x2)/2 - (p.x1 + p.x2)/2);\n                    score = -1.0 * center_dist;\n                }\n            } else {\n                score = f.area();\n            }\n\n            if (score > best_score) {\n                best_score = score;\n                best_fit = f;\n                best_fit_idx = i;\n            }\n        }\n\n        if (best_fit_idx == -1) {\n            for (int i = 0; i < (int)free_space.size(); i++) {\n                if (free_space[i].area() > (int)best_score) {\n                    best_score = free_space[i].area();\n                    best_fit = free_space[i];\n                    best_fit_idx = i;\n                }\n            }\n            if (best_fit_idx == -1) {\n                best_fit = {0, 0, min(W, 1), min(W, 1)};\n            }\n        }\n\n        int h = 1, w = best_fit.area();\n        if (req_area > 0) {\n            h = max(1, (int)sqrt(req_area));\n            while (h > 0) {\n                w = (req_area + h - 1) / h;\n                if (w <= best_fit.x2 - best_fit.x1) break;\n                h--;\n            }\n            if (h == 0) {\n                h = best_fit.y2 - best_fit.y1;\n                w = best_fit.x2 - best_fit.x1;\n            }\n            h = min(h, best_fit.y2 - best_fit.y1);\n            w = min(w, best_fit.x2 - best_fit.x1);\n        }\n\n        int base_y = best_fit.y1;\n        int base_x = best_fit.x1;\n        if (k < (int)prev_rects.size()) {\n            base_y = max(base_y, prev_rects[k].y1);\n            base_x = max(base_x, prev_rects[k].x1);\n            if (base_y + h > best_fit.y2) base_y = best_fit.y2 - h;\n            if (base_x + w > best_fit.x2) base_x = best_fit.x2 - w;\n        }\n\n        result[k] = {base_y, base_x, base_y + h, base_x + w};\n        placed[k] = true;\n\n        free_space.erase(free_space.begin() + best_fit_idx);\n        vector<Rect> new_spaces = split_free_space(result[k], best_fit);\n        for (Rect& ns : new_spaces) {\n            if (ns.area() > 0) {\n                free_space.push_back(ns);\n            }\n        }\n    }\n\n    return result;\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int W = 1000, D, N;\n    cin >> W >> D >> N;\n\n    vector<vector<int>> areas(D, vector<int>(N));\n    for (int d = 0; d < D; d++) {\n        for (int k = 0; k < N; k++) {\n            cin >> areas[d][k];\n        }\n    }\n\n    vector<vector<Rect>> all_rects(D);\n\n    all_rects[0] = pack_day(W, areas[0], vector<Rect>());\n\n    for (int d = 1; d < D; d++) {\n        all_rects[d] = pack_day(W, areas[d], all_rects[d-1]);\n    }\n\n    for (int d = 0; d < D; d++) {\n        for (int k = 0; k < N; k++) {\n            Rect r = all_rects[d][k];\n            cout << r.y1 << \" \" << r.x1 << \" \" << r.y2 << \" \" << r.x2 << '\\n';\n        }\n    }\n\n    return 0;\n}","ahc032":"#include <iostream>\n#include <vector>\n#include <tuple>\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>> a(N, vector<long long>(N));\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cin >> a[i][j];\n        }\n    }\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<vector<long long>> board(N, vector<long long>(N, 0));\n    vector<tuple<int, int, int>> operations;\n\n    for (int step = 0; step < K; step++) {\n        long long best_gain = 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 gain = 0;\n                    for (int di = 0; di < 3; di++) {\n                        for (int dj = 0; dj < 3; dj++) {\n                            int i = p + di;\n                            int j = q + dj;\n                            long long stamp_val = stamps[m][di][dj];\n                            long long old_val = a[i][j] + board[i][j];\n                            long long old_mod = old_val % MOD;\n                            long long new_val = old_val + stamp_val;\n                            long long new_mod = new_val % MOD;\n                            gain += (new_mod - old_mod);\n                        }\n                    }\n                    if (gain > best_gain) {\n                        best_gain = gain;\n                        best_m = m;\n                        best_p = p;\n                        best_q = q;\n                    }\n                }\n            }\n        }\n\n        if (best_gain <= 0) {\n            break;\n        }\n\n        operations.emplace_back(best_m, best_p, best_q);\n        for (int di = 0; di < 3; di++) {\n            for (int dj = 0; dj < 3; dj++) {\n                int i = best_p + di;\n                int j = best_q + dj;\n                board[i][j] += stamps[best_m][di][dj];\n            }\n        }\n    }\n\n    cout << operations.size() << '\\n';\n    for (auto [m, p, q] : operations) {\n        cout << m << ' ' << p << ' ' << q << '\\n';\n    }\n\n    return 0;\n}","ahc033":"#include <bits/stdc++.h>\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        return x == other.x && y == other.y;\n    }\n};\n\nchar compute_next_move(Point start, Point goal, const vector<vector<int>>& grid, bool is_carrying, bool is_large) {\n    if (start == goal) {\n        return '.';\n    }\n    \n    vector<vector<bool>> visited(5, vector<bool>(5, false));\n    vector<vector<Point>> parent(5, vector<Point>(5, Point(-1, -1)));\n    queue<Point> q;\n    q.push(start);\n    visited[start.x][start.y] = true;\n    \n    const vector<int> dx = {-1, 1, 0, 0};\n    const vector<int> dy = {0, 0, -1, 1};\n    const vector<char> dirs = {'U', 'D', 'L', 'R'};\n    bool found = false;\n    \n    while (!q.empty()) {\n        Point p = q.front();\n        q.pop();\n        if (p.x == goal.x && p.y == goal.y) {\n            found = true;\n            break;\n        }\n        \n        for (int d = 0; d < 4; d++) {\n            int nx = p.x + dx[d];\n            int ny = p.y + dy[d];\n            if (nx < 0 || nx >= 5 || ny < 0 || ny >= 5) continue;\n            if (visited[nx][ny]) continue;\n            \n            bool allowed;\n            if (is_carrying) {\n                if (is_large) {\n                    allowed = true;\n                } else {\n                    allowed = (grid[nx][ny] == -1);\n                }\n            } else {\n                allowed = true;\n            }\n            \n            if (allowed) {\n                visited[nx][ny] = true;\n                parent[nx][ny] = p;\n                q.push(Point(nx, ny));\n            }\n        }\n    }\n    \n    if (!found) {\n        return '.';\n    }\n    \n    Point p = goal;\n    while (!(parent[p.x][p.y] == start)) {\n        p = parent[p.x][p.y];\n    }\n    \n    int diff_x = p.x - start.x;\n    int diff_y = p.y - start.y;\n    if (diff_x == -1) return 'U';\n    if (diff_x == 1) return 'D';\n    if (diff_y == -1) return 'L';\n    if (diff_y == 1) return 'R';\n    return '.';\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \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<vector<int>> grid(5, vector<int>(5, -1));\n    vector<int> next_arrival_index(5, 0);\n    vector<int> dispatched(5, 0);\n    vector<int> next_expected(5);\n    for (int i = 0; i < 5; i++) {\n        next_expected[i] = 5 * i;\n    }\n    \n    struct Crane {\n        Point pos;\n        int hold;\n    };\n    vector<Crane> cranes(5);\n    for (int i = 0; i < 5; i++) {\n        cranes[i] = {{i, 0}, -1};\n    }\n    \n    vector<string> output_lines(5);\n    \n    for (int turn = 0; turn < 10000; turn++) {\n        for (int i = 0; i < 5; i++) {\n            if (next_arrival_index[i] < 5) {\n                bool can_place = true;\n                if (grid[i][0] != -1) {\n                    can_place = false;\n                } else {\n                    for (int k = 0; k < 5; k++) {\n                        if (cranes[k].pos.x == i && cranes[k].pos.y == 0 && cranes[k].hold != -1) {\n                            can_place = false;\n                            break;\n                        }\n                    }\n                }\n                if (can_place) {\n                    int b = A[i][next_arrival_index[i]];\n                    grid[i][0] = b;\n                    next_arrival_index[i]++;\n                }\n            }\n        }\n        \n        vector<Point> old_positions(5);\n        for (int i = 0; i < 5; i++) {\n            old_positions[i] = cranes[i].pos;\n        }\n        \n        vector<char> actions(5, '.');\n        vector<Point> new_positions(5);\n        for (int i = 0; i < 5; i++) {\n            new_positions[i] = old_positions[i];\n        }\n        \n        for (int i = 0; i < 5; i++) {\n            bool is_large = (i == 0);\n            bool is_carrying = (cranes[i].hold != -1);\n            \n            if (is_carrying) {\n                int b = cranes[i].hold;\n                int target_row = b / 5;\n                Point target(target_row, 4);\n                if (old_positions[i].x == target_row && old_positions[i].y == 4) {\n                    actions[i] = 'Q';\n                    new_positions[i] = old_positions[i];\n                } else {\n                    char dir = compute_next_move(old_positions[i], target, grid, is_carrying, is_large);\n                    if (dir != '.') {\n                        int nx = old_positions[i].x;\n                        int ny = old_positions[i].y;\n                        if (dir == 'U') nx--;\n                        else if (dir == 'D') nx++;\n                        else if (dir == 'L') ny--;\n                        else if (dir == 'R') ny++;\n                        new_positions[i] = Point(nx, ny);\n                        actions[i] = dir;\n                    } else {\n                        actions[i] = '.';\n                        new_positions[i] = old_positions[i];\n                    }\n                }\n            } else {\n                if (grid[old_positions[i].x][old_positions[i].y] != -1) {\n                    int b = grid[old_positions[i].x][old_positions[i].y];\n                    int target_row = b / 5;\n                    if (b == next_expected[target_row]) {\n                        actions[i] = 'P';\n                        new_positions[i] = old_positions[i];\n                    } else {\n                        actions[i] = 'P';\n                        new_positions[i] = old_positions[i];\n                    }\n                } else {\n                    if (is_large) {\n                        Point goal(-1, -1);\n                        for (int x = 0; x < 5; x++) {\n                            for (int y = 0; y < 5; y++) {\n                                if (grid[x][y] != -1) {\n                                    int b = grid[x][y];\n                                    int expected_b = 5 * (b / 5) + dispatched[b / 5];\n                                    if (b == expected_b) {\n                                        goal = Point(x, y);\n                                        break;\n                                    }\n                                }\n                            }\n                            if (goal.x != -1) break;\n                        }\n                        if (goal.x == -1) {\n                            goal = Point(2, 2);\n                        }\n                        char dir = compute_next_move(old_positions[i], goal, grid, is_carrying, is_large);\n                        if (dir != '.') {\n                            int nx = old_positions[i].x;\n                            int ny = old_positions[i].y;\n                            if (dir == 'U') nx--;\n                            else if (dir == 'D') nx++;\n                            else if (dir == 'L') ny--;\n                            else if (dir == 'R') ny++;\n                            new_positions[i] = Point(nx, ny);\n                            actions[i] = dir;\n                        } else {\n                            actions[i] = '.';\n                            new_positions[i] = old_positions[i];\n                        }\n                    } else {\n                        if (grid[old_positions[i].x][1] == -1 && old_positions[i].y < 4) {\n                            actions[i] = 'R';\n                            new_positions[i] = Point(old_positions[i].x, old_positions[i].y + 1);\n                        } else {\n                            actions[i] = '.';\n                            new_positions[i] = old_positions[i];\n                        }\n                    }\n                }\n            }\n            \n            bool valid = true;\n            for (int j = 0; j < i; j++) {\n                if (new_positions[i] == new_positions[j]) {\n                    valid = false;\n                    break;\n                }\n                if (new_positions[i] == old_positions[j] && new_positions[j] == old_positions[i]) {\n                    valid = false;\n                    break;\n                }\n            }\n            if (!valid) {\n                actions[i] = '.';\n                new_positions[i] = old_positions[i];\n            }\n        }\n        \n        for (int i = 0; i < 5; i++) {\n            output_lines[i] += actions[i];\n        }\n        \n        for (int i = 0; i < 5; i++) {\n            if (actions[i] == 'P' && grid[old_positions[i].x][old_positions[i].y] != -1) {\n                int b = grid[old_positions[i].x][old_positions[i].y];\n                cranes[i].hold = b;\n                grid[old_positions[i].x][old_positions[i].y] = -1;\n            } else if (actions[i] == 'Q' && cranes[i].hold != -1) {\n                int b = cranes[i].hold;\n                grid[old_positions[i].x][old_positions[i].y] = b;\n                cranes[i].hold = -1;\n            } else if (actions[i] == 'U' || actions[i] == 'D' || actions[i] == 'L' || actions[i] == 'R') {\n                int dx = 0, dy = 0;\n                if (actions[i] == 'U') dx = -1;\n                else if (actions[i] == 'D') dx = 1;\n                else if (actions[i] == 'L') dy = -1;\n                else if (actions[i] == 'R') dy = 1;\n                cranes[i].pos.x = old_positions[i].x + dx;\n                cranes[i].pos.y = old_positions[i].y + dy;\n            }\n        }\n        \n        for (int i = 0; i < 5; i++) {\n            if (grid[i][4] != -1) {\n                int b = grid[i][4];\n                int expected_val = 5 * i + dispatched[i];\n                if (b == expected_val) {\n                    dispatched[i]++;\n                    next_expected[i] = 5 * i + dispatched[i];\n                }\n                grid[i][4] = -1;\n            }\n        }\n        \n        int total_dispatched = 0;\n        for (int i = 0; i < 5; i++) {\n            total_dispatched += dispatched[i];\n        }\n        if (total_dispatched == 25) {\n            break;\n        }\n    }\n    \n    int max_len = 0;\n    for (int i = 0; i < 5; i++) {\n        if ((int)output_lines[i].size() > max_len) {\n            max_len = output_lines[i].size();\n        }\n    }\n    for (int i = 0; i < 5; i++) {\n        while ((int)output_lines[i].size() < max_len) {\n            output_lines[i] += '.';\n        }\n    }\n    \n    for (int i = 0; i < 5; i++) {\n        cout << output_lines[i] << '\\n';\n    }\n    \n    return 0;\n}","ahc034":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    const int N = 20;\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<int>> remaining_source(N, vector<int>(N, 0));\n    vector<vector<int>> remaining_sink(N, vector<int>(N, 0));\n\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (grid[i][j] > 0) {\n                remaining_source[i][j] = grid[i][j];\n            } else if (grid[i][j] < 0) {\n                remaining_sink[i][j] = -grid[i][j];\n            }\n        }\n    }\n\n    int cur_i = 0, cur_j = 0;\n    int cur_load = 0;\n\n    auto has_remaining = [&]() -> bool {\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                if (remaining_source[i][j] > 0 || remaining_sink[i][j] > 0) {\n                    return true;\n                }\n            }\n        }\n        return false;\n    };\n\n    while (has_remaining()) {\n        bool handled = false;\n\n        if (remaining_source[cur_i][cur_j] > 0) {\n            int d = remaining_source[cur_i][cur_j];\n            cout << \"+\" << d << \"\\n\";\n            cur_load += d;\n            remaining_source[cur_i][cur_j] = 0;\n            handled = true;\n        }\n        else if (remaining_sink[cur_i][cur_j] > 0 && cur_load > 0) {\n            int d = min(remaining_sink[cur_i][cur_j], cur_load);\n            cout << \"-\" << d << \"\\n\";\n            cur_load -= d;\n            remaining_sink[cur_i][cur_j] -= d;\n            handled = true;\n        }\n\n        if (handled) {\n            continue;\n        }\n\n        int best_i = -1, best_j = -1;\n        int min_score = INT_MAX;\n\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                if (remaining_source[i][j] > 0) {\n                    int dist = abs(i - cur_i) + abs(j - cur_j);\n                    int score = dist * (100 + cur_load);\n                    if (score < min_score) {\n                        min_score = score;\n                        best_i = i;\n                        best_j = j;\n                    }\n                }\n                if (remaining_sink[i][j] > 0 && cur_load > 0) {\n                    int dist = abs(i - cur_i) + abs(j - cur_j);\n                    int score = dist * (100 + cur_load);\n                    if (score < min_score) {\n                        min_score = score;\n                        best_i = i;\n                        best_j = j;\n                    }\n                }\n            }\n        }\n\n        if (best_i == -1) {\n            break;\n        }\n\n        int dx = best_i - cur_i;\n        int dy = best_j - cur_j;\n        string moves = \"\";\n        if (dx > 0) moves += string(dx, 'D');\n        else if (dx < 0) moves += string(-dx, 'U');\n        if (dy > 0) moves += string(dy, 'R');\n        else if (dy < 0) moves += string(-dy, 'L');\n\n        for (char c : moves) {\n            cout << c << \"\\n\";\n            if (c == 'U') cur_i--;\n            else if (c == 'D') cur_i++;\n            else if (c == 'L') cur_j--;\n            else if (c == 'R') cur_j++;\n        }\n    }\n\n    return 0;\n}","ahc035":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, T;\n    cin >> N >> M >> T;\n\n    int seed_count = 2 * N * (N - 1);\n    vector<vector<int>> seeds(seed_count, vector<int>(M));\n\n    // Read initial seeds\n    for (int k = 0; k < seed_count; k++) {\n        for (int l = 0; l < M; l++) {\n            cin >> seeds[k][l];\n        }\n    }\n\n    // Precompute X: max for each criterion over initial seeds\n    vector<int> X(M, 0);\n    for (int k = 0; k < seed_count; k++) {\n        for (int l = 0; l < M; l++) {\n            if (seeds[k][l] > X[l]) {\n                X[l] = seeds[k][l];\n            }\n        }\n    }\n\n    // Precompute grid positions: (i, j, degree)\n    vector<tuple<int, int, int>> positions;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            int deg = 4;\n            if (i == 0 || i == N-1) deg--;\n            if (j == 0 || j == N-1) deg--;\n            positions.emplace_back(i, j, deg);\n        }\n    }\n\n    // Sort positions: primary by degree descending, then by squared distance to center ascending\n    double center = (N - 1) / 2.0;\n    sort(positions.begin(), positions.end(), [&](const auto& a, const auto& b) {\n        if (get<2>(a) != get<2>(b)) \n            return get<2>(a) > get<2>(b);\n        double dist_a = (get<0>(a) - center) * (get<0>(a) - center) + (get<1>(a) - center) * (get<1>(a) - center);\n        double dist_b = (get<0>(b) - center) * (get<0>(b) - center) + (get<1>(b) - center) * (get<1>(b) - center);\n        if (abs(dist_a - dist_b) > 1e-9)\n            return dist_a < dist_b;\n        if (get<0>(a) != get<0>(b))\n            return get<0>(a) < get<0>(b);\n        return get<1>(a) < get<1>(b);\n    });\n\n    for (int t = 0; t < T; t++) {\n        // Compute score and V for each current seed\n        vector<tuple<double, long long, int>> seed_scores;\n        for (int k = 0; k < seed_count; k++) {\n            double score = 0.0;\n            long long V = 0;\n            for (int l = 0; l < M; l++) {\n                V += seeds[k][l];\n                if (X[l] > 0) {\n                    score += static_cast<double>(seeds[k][l]) / X[l];\n                }\n            }\n            seed_scores.emplace_back(score, V, k);\n        }\n\n        // Sort seeds by score descending, then V descending\n        sort(seed_scores.begin(), seed_scores.end(), [](const auto& a, const auto& b) {\n            if (get<0>(a) != get<0>(b))\n                return get<0>(a) > get<0>(b);\n            return get<1>(a) > get<1>(b);\n        });\n\n        // Prepare grid: assign top N*N seeds to the sorted positions\n        vector<vector<int>> grid(N, vector<int>(N));\n        for (int idx = 0; idx < N * N; idx++) {\n            auto [i, j, deg] = positions[idx];\n            grid[i][j] = get<2>(seed_scores[idx]);\n        }\n\n        // Output the grid\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 << '\\n';\n        }\n        cout.flush();\n\n        // Read new seeds for the next turn\n        for (int k = 0; k < seed_count; k++) {\n            for (int l = 0; l < M; l++) {\n                cin >> seeds[k][l];\n            }\n        }\n    }\n\n    return 0;\n}","ahc038":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <cctype>\n#include <cmath>\n#include <string>\n#include <map>\n#include <set>\n#include <queue>\n#include <stack>\n#include <utility>\n#include <climits>\nusing namespace std;\n\ntypedef pair<int, int> Point;\n\nvector<char> get_manhattan_path(Point from, Point to) {\n    vector<char> path;\n    int dx = to.first - from.first;\n    int dy = to.second - from.second;\n    char x_dir = 0;\n    char y_dir = 0;\n    if (dx > 0) x_dir = 'D';\n    else if (dx < 0) x_dir = 'U';\n    if (dy > 0) y_dir = 'R';\n    else if (dy < 0) y_dir = 'L';\n\n    for (int i = 0; i < abs(dx); i++) {\n        path.push_back(x_dir);\n    }\n    for (int i = 0; i < abs(dy); i++) {\n        path.push_back(y_dir);\n    }\n    return path;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, V;\n    cin >> N >> M >> V;\n\n    vector<string> s_grid(N);\n    vector<string> 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    vector<Point> A;\n    vector<Point> B;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (s_grid[i][j] == '1') {\n                A.push_back({i, j});\n            }\n            if (t_grid[i][j] == '1') {\n                B.push_back({i, j});\n            }\n        }\n    }\n\n    sort(A.begin(), A.end());\n    sort(B.begin(), B.end());\n\n    int V_prime = 5;\n    cout << V_prime << \"\\n\";\n    for (int u = 1; u < V_prime; u++) {\n        cout << 0 << \" \" << 1 << \"\\n\";\n    }\n    cout << 0 << \" \" << 0 << \"\\n\";\n\n    vector<string> operations;\n\n    operations.push_back(\".\" + string(\"RRRL\") + string(\".....\"));\n    operations.push_back(\".\" + string(\"..R.\") + string(\".....\"));\n\n    Point current_root = {0, 0};\n\n    vector<pair<Point, Point>> valid_pairs;\n    for (int i = 0; i < M; i++) {\n        if (A[i] != B[i]) {\n            valid_pairs.push_back({A[i], B[i]});\n        }\n    }\n\n    auto get_options = [&](int i, int j) -> vector<pair<int, Point>> {\n        vector<pair<int, Point>> options;\n        if (j - 1 >= 0) {\n            options.push_back({1, {i, j - 1}});\n        }\n        if (i - 1 >= 0) {\n            options.push_back({2, {i - 1, j}});\n        }\n        if (j + 1 < N) {\n            options.push_back({3, {i, j + 1}});\n        }\n        if (i + 1 < N) {\n            options.push_back({4, {i + 1, j}});\n        }\n        return options;\n    };\n\n    for (auto &p : valid_pairs) {\n        Point a = p.first;\n        Point b = p.second;\n\n        auto options_a = get_options(a.first, a.second);\n        if (options_a.empty()) {\n            continue;\n        }\n        int best_u = -1;\n        Point best_R_a;\n        int min_dist = INT_MAX;\n        for (auto &opt : options_a) {\n            int u_idx = opt.first;\n            Point R = opt.second;\n            int dist = abs(current_root.first - R.first) + abs(current_root.second - R.second);\n            if (dist < min_dist) {\n                min_dist = dist;\n                best_u = u_idx;\n                best_R_a = R;\n            }\n        }\n        Point R_a = best_R_a;\n\n        auto options_b = get_options(b.first, b.second);\n        if (options_b.empty()) {\n            continue;\n        }\n        auto best_b = options_b[0];\n        int v_rel = best_b.first;\n        Point R_b = best_b.second;\n\n        vector<char> path1 = get_manhattan_path(current_root, R_a);\n        if (path1.empty()) {\n            string action_str = \".....\";\n            action_str[0] = '.';\n            if (best_u == 1) action_str[1] = 'P';\n            else if (best_u == 2) action_str[2] = 'P';\n            else if (best_u == 3) action_str[3] = 'P';\n            else if (best_u == 4) action_str[4] = 'P';\n            operations.push_back(\".\" + string(\"....\") + action_str);\n        } else {\n            for (int idx = 0; idx < path1.size(); idx++) {\n                string rot_str = \"....\";\n                string action_str = \".....\";\n                if (idx == path1.size() - 1) {\n                    action_str[0] = '.';\n                    if (best_u == 1) action_str[1] = 'P';\n                    else if (best_u == 2) action_str[2] = 'P';\n                    else if (best_u == 3) action_str[3] = 'P';\n                    else if (best_u == 4) action_str[4] = 'P';\n                }\n                string op = string(1, path1[idx]) + rot_str + action_str;\n                operations.push_back(op);\n            }\n        }\n\n        vector<char> path2 = get_manhattan_path(R_a, R_b);\n        if (path2.empty()) {\n            string action_str = \".....\";\n            action_str[0] = '.';\n            if (v_rel == 1) action_str[1] = 'P';\n            else if (v_rel == 2) action_str[2] = 'P';\n            else if (v_rel == 3) action_str[3] = 'P';\n            else if (v_rel == 4) action_str[4] = 'P';\n            operations.push_back(\".\" + string(\"....\") + action_str);\n        } else {\n            for (int idx = 0; idx < path2.size(); idx++) {\n                string rot_str = \"....\";\n                string action_str = \".....\";\n                if (idx == path2.size() - 1) {\n                    action_str[0] = '.';\n                    if (v_rel == 1) action_str[1] = 'P';\n                    else if (v_rel == 2) action_str[2] = 'P';\n                    else if (v_rel == 3) action_str[3] = 'P';\n                    else if (v_rel == 4) action_str[4] = 'P';\n                }\n                string op = string(1, path2[idx]) + rot_str + action_str;\n                operations.push_back(op);\n            }\n        }\n\n        current_root = R_b;\n    }\n\n    for (string &op : operations) {\n        cout << op << \"\\n\";\n    }\n\n    return 0;\n}","ahc039":"#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\n\nstruct Point {\n    int x, y;\n};\n\nstruct Rect {\n    int x1, x2, y1, y2;\n    Rect() : x1(0), x2(0), y1(0), y2(0) {}\n    Rect(int a, int b, int c, int d) {\n        x1 = min(a, b); x2 = max(a, b);\n        y1 = min(c, d); y2 = max(c, d);\n    }\n};\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    const int N = 5000;\n    vector<Point> mackerels(N), sardines(N);\n    for (int i = 0; i < N; i++) {\n        cin >> mackerels[i].x >> mackerels[i].y;\n    }\n    for (int i = 0; i < N; i++) {\n        cin >> sardines[i].x >> sardines[i].y;\n    }\n\n    vector<int> xs, ys;\n    for (int i = 0; i < N; i++) {\n        xs.push_back(mackerels[i].x);\n        ys.push_back(mackerels[i].y);\n        xs.push_back(sardines[i].x);\n        ys.push_back(sardines[i].y);\n    }\n    sort(xs.begin(), xs.end());\n    xs.erase(unique(xs.begin(), xs.end()), xs.end());\n    sort(ys.begin(), ys.end());\n    ys.erase(unique(ys.begin(), ys.end()), ys.end());\n\n    int X = xs.size(), Y = ys.size();\n\n    vector<vector<int>> grid_val(X, vector<int>(Y, 0));\n    for (int i = 0; i < N; i++) {\n        auto p = mackerels[i];\n        int ix = lower_bound(xs.begin(), xs.end(), p.x) - xs.begin();\n        int iy = lower_bound(ys.begin(), ys.end(), p.y) - ys.begin();\n        if (ix < X && iy < Y) {\n            grid_val[ix][iy] = 1;\n        }\n    }\n    for (int i = 0; i < N; i++) {\n        auto p = sardines[i];\n        int ix = lower_bound(xs.begin(), xs.end(), p.x) - xs.begin();\n        int iy = lower_bound(ys.begin(), ys.end(), p.y) - ys.begin();\n        if (ix < X && iy < Y) {\n            grid_val[ix][iy] = -1;\n        }\n    }\n\n    vector<vector<int>> P(X+1, vector<int>(Y+1, 0));\n    for (int i = 0; i < X; i++) {\n        for (int j = 0; j < Y; j++) {\n            P[i+1][j+1] = P[i][j+1] + P[i+1][j] - P[i][j] + grid_val[i][j];\n        }\n    }\n\n    auto get_sum = [&](int x1, int x2, int y1, int y2) -> int {\n        int i1 = lower_bound(xs.begin(), xs.end(), x1) - xs.begin();\n        int i2 = upper_bound(xs.begin(), xs.end(), x2) - xs.begin();\n        if (i1 >= i2) return 0;\n        i2--;\n\n        int j1 = lower_bound(ys.begin(), ys.end(), y1) - ys.begin();\n        int j2 = upper_bound(ys.begin(), ys.end(), y2) - ys.begin();\n        if (j1 >= j2) return 0;\n        j2--;\n\n        return P[i2+1][j2+1] - P[i1][j2+1] - P[i2+1][j1] + P[i1][j1];\n    };\n\n    auto greedy_improve = [&](Rect r) -> Rect {\n        int x1 = r.x1, x2 = r.x2, y1 = r.y1, y2 = r.y2;\n        bool improved;\n        do {\n            improved = false;\n            while (x1 < x2) {\n                int strip_sum = get_sum(x1, x1, y1, y2);\n                if (strip_sum < 0) {\n                    x1++;\n                    improved = true;\n                } else {\n                    break;\n                }\n            }\n            while (x2 > x1) {\n                int strip_sum = get_sum(x2, x2, y1, y2);\n                if (strip_sum < 0) {\n                    x2--;\n                    improved = true;\n                } else {\n                    break;\n                }\n            }\n            while (y2 > y1) {\n                int strip_sum = get_sum(x1, x2, y2, y2);\n                if (strip_sum < 0) {\n                    y2--;\n                    improved = true;\n                } else {\n                    break;\n                }\n            }\n            while (y1 < y2) {\n                int strip_sum = get_sum(x1, x2, y1, y1);\n                if (strip_sum < 0) {\n                    y1++;\n                    improved = true;\n                } else {\n                    break;\n                }\n            }\n        } while (improved);\n        return Rect(x1, x2, y1, y2);\n    };\n\n    vector<Rect> candidates;\n\n    int min_x = 1000000, max_x = -1, min_y = 1000000, max_y = -1;\n    for (int i = 0; i < N; i++) {\n        min_x = min(min_x, mackerels[i].x);\n        max_x = max(max_x, mackerels[i].x);\n        min_y = min(min_y, mackerels[i].y);\n        max_y = max(max_y, mackerels[i].y);\n    }\n    candidates.emplace_back(min_x, max_x, min_y, max_y);\n\n    for (int i = 0; i < N; i++) {\n        int x = mackerels[i].x;\n        int y = mackerels[i].y;\n        int x1 = max(0, x - 5000);\n        int x2 = min(100000, x + 5000);\n        int y1 = max(0, y - 5000);\n        int y2 = min(100000, y + 5000);\n        candidates.emplace_back(x1, x2, y1, y2);\n    }\n\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n    uniform_int_distribution<int> dist(0, 100000);\n    for (int i = 0; i < 5000; i++) {\n        int x1 = dist(rng);\n        int x2 = dist(rng);\n        if (x1 > x2) swap(x1, x2);\n        int y1 = dist(rng);\n        int y2 = dist(rng);\n        if (y1 > y2) swap(y1, y2);\n        candidates.emplace_back(x1, x2, y1, y2);\n    }\n\n    int best_score = -1000000000;\n    Rect best_rect;\n\n    for (Rect r : candidates) {\n        Rect improved = greedy_improve(r);\n        int score = get_sum(improved.x1, improved.x2, improved.y1, improved.y2);\n        if (score > best_score) {\n            best_score = score;\n            best_rect = improved;\n        }\n    }\n\n    cout << 4 << '\\n';\n    cout << best_rect.x1 << \" \" << best_rect.y1 << '\\n';\n    cout << best_rect.x2 << \" \" << best_rect.y1 << '\\n';\n    cout << best_rect.x2 << \" \" << best_rect.y2 << '\\n';\n    cout << best_rect.x1 << \" \" << best_rect.y2 << '\\n';\n\n    return 0;\n}","ahc040":"#include <iostream>\n#include <vector>\n#include <tuple>\n#include <algorithm>\n#include <random>\n#include <chrono>\n#include <climits>\n#include <cmath>\n#include <cctype>\nusing namespace std;\n\nstruct Rect {\n    long long x, y, w, h;\n};\n\nstruct Instruction {\n    int p, r, b;\n    char d;\n};\n\ndouble rand_double(double min_val, double max_val, mt19937& rng) {\n    uniform_real_distribution<double> dist(min_val, max_val);\n    return dist(rng);\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    for (int i = 0; i < N; i++) {\n        long long wp, hp;\n        cin >> wp >> hp;\n        w_est[i] = static_cast<double>(wp);\n        h_est[i] = static_cast<double>(hp);\n    }\n\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n\n    const int M = 50;\n\n    for (int turn = 0; turn < T; turn++) {\n        vector<Instruction> best_instructions;\n        long long best_score = LLONG_MAX;\n\n        for (int cand_idx = 0; cand_idx < M; cand_idx++) {\n            vector<Instruction> inst_seq;\n            vector<Rect> layout;\n            long long cur_max_x = 0;\n            long long cur_max_y = 0;\n\n            vector<double> w_cand = w_est;\n            vector<double> h_cand = h_est;\n            if (cand_idx > 0) {\n                double noise_factor = 0.1 * (1.0 - static_cast<double>(cand_idx) / M);\n                for (int i = 0; i < N; i++) {\n                    double noise = 1.0 + rand_double(-noise_factor, noise_factor, rng);\n                    w_cand[i] *= noise;\n                    h_cand[i] *= noise;\n                }\n            }\n\n            bool valid_placement = true;\n            for (int i = 0; i < N; i++) {\n                vector<tuple<int, int, char, int, long long, long long, long long, long long>> options;\n\n                for (int rotation : {0, 1}) {\n                    double w_val = (rotation == 0) ? w_cand[i] : h_cand[i];\n                    double h_val = (rotation == 0) ? h_cand[i] : w_cand[i];\n                    long long w_rect = llround(w_val);\n                    long long h_rect = llround(h_val);\n                    w_rect = max(1LL, min(1000000000LL, w_rect));\n                    h_rect = max(1LL, min(1000000000LL, h_rect));\n\n                    vector<tuple<char, int>> dir_b_opts;\n                    dir_b_opts.push_back({'U', -1});\n                    if (!layout.empty()) {\n                        dir_b_opts.push_back({'U', static_cast<int>(layout.size() - 1)});\n                    }\n                    dir_b_opts.push_back({'L', -1});\n                    if (!layout.empty()) {\n                        dir_b_opts.push_back({'L', static_cast<int>(layout.size() - 1)});\n                    }\n\n                    for (auto [d, b] : dir_b_opts) {\n                        long long x_fixed, y_fixed;\n                        if (d == 'U') {\n                            if (b == -1) {\n                                x_fixed = 0;\n                            } else {\n                                x_fixed = layout[b].x + layout[b].w;\n                            }\n                            y_fixed = 0;\n                            for (const auto& rect : layout) {\n                                if (rect.x < x_fixed + w_rect && rect.x + rect.w > x_fixed) {\n                                    y_fixed = max(y_fixed, rect.y + rect.h);\n                                }\n                            }\n                        } else {\n                            if (b == -1) {\n                                y_fixed = 0;\n                            } else {\n                                y_fixed = layout[b].y + layout[b].h;\n                            }\n                            x_fixed = 0;\n                            for (const auto& rect : layout) {\n                                if (rect.y < y_fixed + h_rect && rect.y + rect.h > y_fixed) {\n                                    x_fixed = max(x_fixed, rect.x + rect.w);\n                                }\n                            }\n                        }\n                        long long new_max_x = max(cur_max_x, x_fixed + w_rect);\n                        long long new_max_y = max(cur_max_y, y_fixed + h_rect);\n                        long long score_val = new_max_x + new_max_y;\n                        options.emplace_back(rotation, b, d, score_val, x_fixed, y_fixed, w_rect, h_rect);\n                    }\n                }\n\n                if (options.empty()) {\n                    valid_placement = false;\n                    break;\n                }\n\n                auto best_opt = *min_element(options.begin(), options.end(),\n                    [](const auto& a, const auto& b) {\n                        return get<3>(a) < get<3>(b);\n                    });\n\n                int r = get<0>(best_opt);\n                int b_val = get<1>(best_opt);\n                char d_val = get<2>(best_opt);\n                long long x_val = get<4>(best_opt);\n                long long y_val = get<5>(best_opt);\n                long long w_val = get<6>(best_opt);\n                long long h_val = get<7>(best_opt);\n\n                inst_seq.push_back({i, r, b_val, d_val});\n                layout.push_back({x_val, y_val, w_val, h_val});\n                cur_max_x = max(cur_max_x, x_val + w_val);\n                cur_max_y = max(cur_max_y, y_val + h_val);\n            }\n\n            if (!valid_placement) {\n                continue;\n            }\n\n            long long total_est = cur_max_x + cur_max_y;\n            if (total_est < best_score) {\n                best_score = total_est;\n                best_instructions = inst_seq;\n            }\n        }\n\n        if (best_instructions.empty()) {\n            for (int i = 0; i < N; i++) {\n                best_instructions.push_back({i, 0, -1, 'U'});\n            }\n        }\n\n        cout << N << \"\\n\";\n        for (const auto& inst : best_instructions) {\n            cout << inst.p << \" \" << inst.r << \" \" << inst.d << \" \" << inst.b << \"\\n\";\n        }\n        cout.flush();\n\n        long long W_obs, H_obs;\n        cin >> W_obs >> H_obs;\n\n        double total_est_val = static_cast<double>(best_score);\n        double total_obs_val = static_cast<double>(W_obs) + static_cast<double>(H_obs);\n        double scale = 1.0;\n        if (total_est_val > 1e-9) {\n            scale = total_obs_val / total_est_val;\n        }\n\n        for (int i = 0; i < N; i++) {\n            w_est[i] *= scale;\n            h_est[i] *= scale;\n            if (w_est[i] < 1.0) w_est[i] = 1.0;\n            if (w_est[i] > 1e9) w_est[i] = 1e9;\n            if (h_est[i] < 1.0) h_est[i] = 1.0;\n            if (h_est[i] > 1e9) h_est[i] = 1e9;\n        }\n    }\n\n    return 0;\n}","ahc041":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, H_val;\n    cin >> N >> M >> H_val;\n    const int max_layer = H_val + 1;\n\n    vector<long long> A(N);\n    for (int i = 0; i < N; i++) {\n        cin >> A[i];\n    }\n\n    vector<vector<int>> graph(N);\n    for (int i = 0; i < M; i++) {\n        int u, v;\n        cin >> u >> v;\n        graph[u].push_back(v);\n        graph[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> indices(N);\n    iota(indices.begin(), indices.end(), 0);\n    sort(indices.begin(), indices.end(), [&](int i, int j) {\n        return A[i] < A[j];\n    });\n\n    vector<int> layer(N, -1);\n    vector<int> parent(N, -1);\n\n    for (int v : indices) {\n        for (int d = max_layer; d >= 1; d--) {\n            if (d == 1) {\n                layer[v] = 1;\n                parent[v] = -1;\n                break;\n            }\n            for (int u : graph[v]) {\n                if (layer[u] == d - 1) {\n                    layer[v] = d;\n                    parent[v] = u;\n                    break;\n                }\n            }\n            if (layer[v] != -1) {\n                break;\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 <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int N;\n    cin >> N;\n    vector<string> grid(N);\n    for (int i = 0; i < N; i++) {\n        cin >> grid[i];\n    }\n\n    vector<pair<char, int>> ops;\n\n    while (true) {\n        int best_i = -1, best_j = -1;\n        char best_dir = ' ';\n        int best_p = -1;\n        int min_shifts = 100000000;\n\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                if (grid[i][j] != 'x') continue;\n\n                bool safe = true;\n                for (int k = 0; k < i; k++) {\n                    if (grid[k][j] == 'o') {\n                        safe = false;\n                        break;\n                    }\n                }\n                if (safe) {\n                    int shifts = i + 1;\n                    if (shifts < min_shifts) {\n                        min_shifts = shifts;\n                        best_dir = 'U';\n                        best_p = j;\n                        best_i = i;\n                        best_j = j;\n                    }\n                }\n\n                safe = true;\n                for (int k = i + 1; k < N; k++) {\n                    if (grid[k][j] == 'o') {\n                        safe = false;\n                        break;\n                    }\n                }\n                if (safe) {\n                    int shifts = N - i;\n                    if (shifts < min_shifts) {\n                        min_shifts = shifts;\n                        best_dir = 'D';\n                        best_p = j;\n                        best_i = i;\n                        best_j = j;\n                    }\n                }\n\n                safe = true;\n                for (int k = 0; k < j; k++) {\n                    if (grid[i][k] == 'o') {\n                        safe = false;\n                        break;\n                    }\n                }\n                if (safe) {\n                    int shifts = j + 1;\n                    if (shifts < min_shifts) {\n                        min_shifts = shifts;\n                        best_dir = 'L';\n                        best_p = i;\n                        best_i = i;\n                        best_j = j;\n                    }\n                }\n\n                safe = true;\n                for (int k = j + 1; k < N; k++) {\n                    if (grid[i][k] == 'o') {\n                        safe = false;\n                        break;\n                    }\n                }\n                if (safe) {\n                    int shifts = N - j;\n                    if (shifts < min_shifts) {\n                        min_shifts = shifts;\n                        best_dir = 'R';\n                        best_p = i;\n                        best_i = i;\n                        best_j = j;\n                    }\n                }\n            }\n        }\n\n        if (best_i == -1) {\n            break;\n        }\n\n        for (int s = 0; s < min_shifts; s++) {\n            ops.push_back({best_dir, best_p});\n            if (best_dir == 'U') {\n                for (int i = 0; i < N - 1; i++) {\n                    grid[i][best_p] = grid[i + 1][best_p];\n                }\n                grid[N - 1][best_p] = '.';\n            } else if (best_dir == 'D') {\n                for (int i = N - 1; i > 0; i--) {\n                    grid[i][best_p] = grid[i - 1][best_p];\n                }\n                grid[0][best_p] = '.';\n            } else if (best_dir == 'L') {\n                for (int j = 0; j < N - 1; j++) {\n                    grid[best_p][j] = grid[best_p][j + 1];\n                }\n                grid[best_p][N - 1] = '.';\n            } else if (best_dir == 'R') {\n                for (int j = N - 1; j > 0; j--) {\n                    grid[best_p][j] = grid[best_p][j - 1];\n                }\n                grid[best_p][0] = '.';\n            }\n        }\n    }\n\n    for (auto &op : ops) {\n        cout << op.first << \" \" << op.second << \"\\n\";\n    }\n\n    return 0;\n}","ahc044":"#include <iostream>\n#include <vector>\nusing namespace std;\n\nint main() {\n    int N, L;\n    cin >> N >> L;\n    vector<long> T(N);\n    for (int i = 0; i < N; i++) {\n        cin >> T[i];\n    }\n\n    vector<long> rem = T;\n    vector<long> count(N, 0);\n    count[0] = 1;\n    vector<int> a(N, 0);\n    vector<int> b(N, 0);\n\n    int last = 0;\n\n    for (int week = 1; week < L; week++) {\n        int current = last;\n        int parity = count[current] & 1;\n\n        int j = 0;\n        for (int idx = 1; idx < N; idx++) {\n            if (rem[idx] > rem[j] || (rem[idx] == rem[j] && idx > j)) {\n                j = idx;\n            }\n        }\n\n        if (parity == 1) {\n            a[current] = j;\n        } else {\n            b[current] = j;\n        }\n\n        rem[j]--;\n        count[j]++;\n        last = j;\n    }\n\n    for (int i = 0; i < N; i++) {\n        cout << a[i] << \" \" << b[i] << '\\n';\n    }\n\n    return 0;\n}","ahc045":"#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\n\nstruct DSU {\n    vector<int> parent;\n    DSU(int n) {\n        parent.resize(n);\n        iota(parent.begin(), parent.end(), 0);\n    }\n    int find(int x) {\n        if (parent[x] != x) \n            parent[x] = find(parent[x]);\n        return parent[x];\n    }\n    bool unite(int x, int y) {\n        x = find(x);\n        y = find(y);\n        if (x == y) return false;\n        parent[y] = x;\n        return true;\n    }\n};\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, Q_total, L, W;\n    cin >> N >> M >> Q_total >> L >> W;\n\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    for (int i = 0; i < N; i++) {\n        cin >> lx[i] >> rx[i] >> ly[i] >> ry[i];\n    }\n\n    vector<vector<ll>> min_sq(N, vector<ll>(N, 0));\n    for (int i = 0; i < N; i++) {\n        for (int j = i+1; j < N; j++) {\n            ll dx_min = max(0LL, (ll)max(lx[i], lx[j]) - (ll)min(rx[i], rx[j]));\n            ll dy_min = max(0LL, (ll)max(ly[i], ly[j]) - (ll)min(ry[i], ry[j]));\n            min_sq[i][j] = dx_min * dx_min + dy_min * dy_min;\n            min_sq[j][i] = min_sq[i][j];\n        }\n    }\n\n    vector<ll> min_edge(N, LLONG_MAX);\n    vector<int> parent(N, -1);\n    vector<bool> in_mst(N, false);\n    min_edge[0] = 0;\n\n    for (int i = 0; i < N; i++) {\n        int u = -1;\n        for (int j = 0; j < N; j++) {\n            if (!in_mst[j] && (u == -1 || min_edge[j] < min_edge[u])) {\n                u = j;\n            }\n        }\n        if (u == -1) break;\n        in_mst[u] = true;\n        for (int v = 0; v < N; v++) {\n            if (u == v) continue;\n            if (!in_mst[v] && min_sq[u][v] < min_edge[v]) {\n                min_edge[v] = min_sq[u][v];\n                parent[v] = u;\n            }\n        }\n    }\n\n    vector<vector<int>> adj(N);\n    for (int v = 1; v < N; v++) {\n        adj[parent[v]].push_back(v);\n        adj[v].push_back(parent[v]);\n    }\n\n    vector<int> tour;\n    vector<bool> visited(N, false);\n    stack<int> st;\n    st.push(0);\n    visited[0] = true;\n    while (!st.empty()) {\n        int u = st.top();\n        st.pop();\n        tour.push_back(u);\n        for (auto it = adj[u].rbegin(); it != adj[u].rend(); ++it) {\n            int v = *it;\n            if (!visited[v]) {\n                visited[v] = true;\n                st.push(v);\n            }\n        }\n    }\n\n    vector<vector<int>> groups(M);\n    int start_idx = 0;\n    for (int k = 0; k < M; k++) {\n        int g = G[k];\n        groups[k] = vector<int>(tour.begin() + start_idx, tour.begin() + start_idx + g);\n        start_idx += g;\n    }\n\n    vector<int> queries_per_group(M, 0);\n    vector<int> group_order(M);\n    iota(group_order.begin(), group_order.end(), 0);\n    sort(group_order.begin(), group_order.end(), [&](int i, int j) {\n        return G[i] > G[j];\n    });\n\n    int remaining_queries = Q_total;\n    for (int idx : group_order) {\n        int g = G[idx];\n        if (g <= 2) {\n            queries_per_group[idx] = 0;\n        } else if (g <= L) {\n            queries_per_group[idx] = 1;\n        } else {\n            queries_per_group[idx] = (g + L - 2) / (L - 1);\n        }\n        if (queries_per_group[idx] > remaining_queries) {\n            queries_per_group[idx] = remaining_queries;\n        }\n        remaining_queries -= queries_per_group[idx];\n        if (remaining_queries <= 0) {\n            break;\n        }\n    }\n\n    vector<vector<pair<int, int>>> candidate_edges(M);\n    remaining_queries = Q_total;\n\n    for (int k = 0; k < M; k++) {\n        int g = G[k];\n        if (g <= 2 || queries_per_group[k] == 0) {\n            continue;\n        }\n        int q_count = queries_per_group[k];\n        vector<vector<int>> windows;\n        if (g <= L) {\n            windows.push_back(groups[k]);\n        } else {\n            if (q_count == 1) {\n                windows.push_back(vector<int>(groups[k].begin(), groups[k].begin() + L));\n            } else {\n                int total_gap = g - L;\n                for (int i = 0; i < q_count; i++) {\n                    int start_index = (i * total_gap) / (q_count - 1);\n                    int end_index = start_index + L - 1;\n                    if (end_index >= g) {\n                        end_index = g - 1;\n                        start_index = g - L;\n                    }\n                    vector<int> window;\n                    for (int j = start_index; j <= end_index; j++) {\n                        window.push_back(groups[k][j]);\n                    }\n                    windows.push_back(window);\n                }\n            }\n        }\n\n        for (auto &window : windows) {\n            if (remaining_queries <= 0) break;\n            cout << \"? \" << window.size();\n            for (int city : window) {\n                cout << \" \" << city;\n            }\n            cout << endl;\n            cout.flush();\n            remaining_queries--;\n\n            for (int i = 0; i < (int)window.size() - 1; i++) {\n                int u, v;\n                cin >> u >> v;\n                candidate_edges[k].push_back({u, v});\n            }\n        }\n    }\n\n    vector<vector<pair<int, int>>> final_edges(M);\n    for (int k = 0; k < M; k++) {\n        int g = G[k];\n        if (g <= 1) {\n            final_edges[k] = vector<pair<int, int>>();\n            continue;\n        }\n        if (g == 2) {\n            final_edges[k].push_back({groups[k][0], groups[k][1]});\n            continue;\n        }\n\n        vector<int> group_cities = groups[k];\n        int num_cities = g;\n        vector<int> global_to_local(N, -1);\n        for (int i = 0; i < num_cities; i++) {\n            global_to_local[group_cities[i]] = i;\n        }\n\n        vector<tuple<ll, int, int>> all_edges;\n        for (int i = 0; i < num_cities; i++) {\n            for (int j = i+1; j < num_cities; j++) {\n                int u_global = group_cities[i];\n                int v_global = group_cities[j];\n                all_edges.push_back({min_sq[u_global][v_global], u_global, v_global});\n            }\n        }\n        sort(all_edges.begin(), all_edges.end());\n\n        vector<tuple<ll, int, int>> candidate_list;\n        for (auto &e : candidate_edges[k]) {\n            int u = e.first, v = e.second;\n            if (global_to_local[u] == -1 || global_to_local[v] == -1) continue;\n            candidate_list.push_back({min_sq[u][v], u, v});\n        }\n        sort(candidate_list.begin(), candidate_list.end());\n\n        DSU dsu_local(num_cities);\n        vector<pair<int, int>> edges_used;\n\n        auto add_edge = [&](int u_global, int v_global) {\n            int lu = global_to_local[u_global];\n            int lv = global_to_local[v_global];\n            if (dsu_local.unite(lu, lv)) {\n                edges_used.push_back({u_global, v_global});\n            }\n        };\n\n        for (auto &t : candidate_list) {\n            int u = get<1>(t);\n            int v = get<2>(t);\n            add_edge(u, v);\n            if ((int)edges_used.size() == num_cities - 1) break;\n        }\n\n        if ((int)edges_used.size() < num_cities - 1) {\n            for (auto &t : all_edges) {\n                int u = get<1>(t);\n                int v = get<2>(t);\n                add_edge(u, v);\n                if ((int)edges_used.size() == num_cities - 1) break;\n            }\n        }\n\n        final_edges[k] = edges_used;\n    }\n\n    cout << \"!\" << endl;\n    for (int k = 0; k < M; k++) {\n        for (int i = 0; i < G[k]; i++) {\n            if (i > 0) cout << \" \";\n            cout << groups[k][i];\n        }\n        cout << endl;\n\n        for (auto &e : final_edges[k]) {\n            cout << e.first << \" \" << e.second << endl;\n        }\n    }\n    cout.flush();\n\n    return 0;\n}","ahc046":"#include <iostream>\n#include <vector>\n#include <cstdlib>\nusing namespace std;\n\nint main() {\n    int N, M;\n    cin >> N >> M;\n    vector<pair<int, int>> points(M);\n    for (int i = 0; i < M; i++) {\n        cin >> points[i].first >> points[i].second;\n    }\n\n    for (int idx = 0; idx < M-1; idx++) {\n        int ci = points[idx].first;\n        int cj = points[idx].second;\n        int ti = points[idx+1].first;\n        int tj = points[idx+1].second;\n\n        int dr = ti - ci;\n        int dc = tj - cj;\n\n        if (ci == ti) {\n            if (cj < tj) {\n                int d_direct = tj - cj;\n                int d_alt = 1 + (N - 1 - tj);\n                if (d_alt < d_direct) {\n                    cout << \"S R\\n\";\n                    for (int k = 0; k < N-1-tj; k++) {\n                        cout << \"M L\\n\";\n                    }\n                } else {\n                    for (int k = 0; k < d_direct; k++) {\n                        cout << \"M R\\n\";\n                    }\n                }\n            } else if (cj > tj) {\n                int d_direct = cj - tj;\n                int d_alt = 1 + tj;\n                if (d_alt < d_direct) {\n                    cout << \"S L\\n\";\n                    for (int k = 0; k < tj; k++) {\n                        cout << \"M R\\n\";\n                    }\n                } else {\n                    for (int k = 0; k < d_direct; k++) {\n                        cout << \"M L\\n\";\n                    }\n                }\n            }\n        }\n        else if (cj == tj) {\n            if (ci < ti) {\n                int d_direct = ti - ci;\n                int d_alt = 1 + (N - 1 - ti);\n                if (d_alt < d_direct) {\n                    cout << \"S D\\n\";\n                    for (int k = 0; k < N-1-ti; k++) {\n                        cout << \"M U\\n\";\n                    }\n                } else {\n                    for (int k = 0; k < d_direct; k++) {\n                        cout << \"M D\\n\";\n                    }\n                }\n            } else if (ci > ti) {\n                int d_direct = ci - ti;\n                int d_alt = 1 + ti;\n                if (d_alt < d_direct) {\n                    cout << \"S U\\n\";\n                    for (int k = 0; k < ti; k++) {\n                        cout << \"M D\\n\";\n                    }\n                } else {\n                    for (int k = 0; k < d_direct; k++) {\n                        cout << \"M U\\n\";\n                    }\n                }\n            }\n        }\n        else {\n            if (abs(dr) >= abs(dc)) {\n                if (dr > 0) {\n                    int d_direct = dr;\n                    int d_alt = 1 + (N - 1 - ti);\n                    if (d_alt < d_direct) {\n                        cout << \"S D\\n\";\n                        for (int k = 0; k < N-1-ti; k++) {\n                            cout << \"M U\\n\";\n                        }\n                    } else {\n                        for (int k = 0; k < d_direct; k++) {\n                            cout << \"M D\\n\";\n                        }\n                    }\n                } else {\n                    int d_direct = -dr;\n                    int d_alt = 1 + ti;\n                    if (d_alt < d_direct) {\n                        cout << \"S U\\n\";\n                        for (int k = 0; k < ti; k++) {\n                            cout << \"M D\\n\";\n                        }\n                    } else {\n                        for (int k = 0; k < d_direct; k++) {\n                            cout << \"M U\\n\";\n                        }\n                    }\n                }\n                if (cj < tj) {\n                    int d_direct = tj - cj;\n                    int d_alt = 1 + (N - 1 - tj);\n                    if (d_alt < d_direct) {\n                        cout << \"S R\\n\";\n                        for (int k = 0; k < N-1-tj; k++) {\n                            cout << \"M L\\n\";\n                        }\n                    } else {\n                        for (int k = 0; k < d_direct; k++) {\n                            cout << \"M R\\n\";\n                        }\n                    }\n                } else {\n                    int d_direct = cj - tj;\n                    int d_alt = 1 + tj;\n                    if (d_alt < d_direct) {\n                        cout << \"S L\\n\";\n                        for (int k = 0; k < tj; k++) {\n                            cout << \"M R\\n\";\n                        }\n                    } else {\n                        for (int k = 0; k < d_direct; k++) {\n                            cout << \"M L\\n\";\n                        }\n                    }\n                }\n            } else {\n                if (dc > 0) {\n                    int d_direct = dc;\n                    int d_alt = 1 + (N - 1 - tj);\n                    if (d_alt < d_direct) {\n                        cout << \"S R\\n\";\n                        for (int k = 0; k < N-1-tj; k++) {\n                            cout << \"M L\\n\";\n                        }\n                    } else {\n                        for (int k = 0; k < d_direct; k++) {\n                            cout << \"M R\\n\";\n                        }\n                    }\n                } else {\n                    int d_direct = -dc;\n                    int d_alt = 1 + tj;\n                    if (d_alt < d_direct) {\n                        cout << \"S L\\n\";\n                        for (int k = 0; k < tj; k++) {\n                            cout << \"M R\\n\";\n                        }\n                    } else {\n                        for (int k = 0; k < d_direct; k++) {\n                            cout << \"M L\\n\";\n                        }\n                    }\n                }\n                if (ci < ti) {\n                    int d_direct = ti - ci;\n                    int d_alt = 1 + (N - 1 - ti);\n                    if (d_alt < d_direct) {\n                        cout << \"S D\\n\";\n                        for (int k = 0; k < N-1-ti; k++) {\n                            cout << \"M U\\n\";\n                        }\n                    } else {\n                        for (int k = 0; k < d_direct; k++) {\n                            cout << \"M D\\n\";\n                        }\n                    }\n                } else {\n                    int d_direct = ci - ti;\n                    int d_alt = 1 + ti;\n                    if (d_alt < d_direct) {\n                        cout << \"S U\\n\";\n                        for (int k = 0; k < ti; k++) {\n                            cout << \"M D\\n\";\n                        }\n                    } else {\n                        for (int k = 0; k < d_direct; k++) {\n                            cout << \"M U\\n\";\n                        }\n                    }\n                }\n            }\n        }\n    }\n\n    return 0;\n}"},"8":{"ahc001":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int n;\n    cin >> n;\n    vector<tuple<int, int, int, int>> companies;\n    for (int i = 0; i < n; i++) {\n        int x, y, r;\n        cin >> x >> y >> r;\n        companies.emplace_back(x, y, r, i);\n    }\n\n    vector<vector<pair<int, int>>> all_candidates(n);\n    for (int i = 0; i < n; i++) {\n        auto [x, y, r_val, idx] = companies[i];\n        vector<pair<int, int>> cands;\n        int sw = max(1, (int)sqrt(r_val) - 20);\n        int ew = min(10000, (int)sqrt(r_val) + 20);\n        for (int w = sw; w <= ew; w++) {\n            int h1 = r_val / w;\n            int h2 = (r_val + w - 1) / w;\n            vector<int> hs = {h1 - 2, h1 - 1, h1, h1 + 1, h1 + 2, h2 - 2, h2 - 1, h2, h2 + 1, h2 + 2};\n            for (int h : hs) {\n                if (h < 1) h = 1;\n                if (h > 10000) h = 10000;\n                int area = w * h;\n                cands.emplace_back(w, h);\n                if (w != h) {\n                    cands.emplace_back(h, w);\n                }\n            }\n        }\n        cands.emplace_back(1, 1);\n        sort(cands.begin(), cands.end(), [&](const auto& a, const auto& b) {\n            int err_a = abs(a.first * a.second - r_val);\n            int err_b = abs(b.first * b.second - r_val);\n            if (err_a != err_b) {\n                return err_a < err_b;\n            }\n            return a.first * a.second < b.first * b.second;\n        });\n        auto last = unique(cands.begin(), cands.end(), [](const auto& a, const auto& b) {\n            return a.first == b.first && a.second == b.second;\n        });\n        cands.erase(last, cands.end());\n        if (cands.size() > 40) {\n            cands.resize(40);\n        }\n        all_candidates[i] = cands;\n    }\n\n    sort(companies.begin(), companies.end(), [](const auto& a, const auto& b) {\n        return get<2>(a) > get<2>(b);\n    });\n\n    vector<array<int, 4>> result(n);\n    vector<array<int, 4>> placed;\n\n    vector<int> offsets = {-3, -2, -1, 0, 1, 2, 3};\n\n    for (auto& comp : companies) {\n        int x = get<0>(comp);\n        int y = get<1>(comp);\n        int r_val = get<2>(comp);\n        int idx = get<3>(comp);\n        bool found = false;\n        for (auto [w, h] : all_candidates[idx]) {\n            int Lx = max(0, x + 1 - w);\n            int Rx = min(x, 10000 - w);\n            int Ly = max(0, y + 1 - h);\n            int Ry = min(y, 10000 - h);\n            if (Lx > Rx || Ly > Ry) {\n                continue;\n            }\n\n            double center_x = x + 0.5;\n            double center_y = y + 0.5;\n            double ideal_a = center_x - w / 2.0;\n            double ideal_b = center_y - h / 2.0;\n\n            for (int dx : offsets) {\n                int a_candidate = static_cast<int>(round(ideal_a)) + dx;\n                if (a_candidate < Lx) a_candidate = Lx;\n                if (a_candidate > Rx) a_candidate = Rx;\n                for (int dy : offsets) {\n                    int b_candidate = static_cast<int>(round(ideal_b)) + dy;\n                    if (b_candidate < Ly) b_candidate = Ly;\n                    if (b_candidate > Ry) b_candidate = Ry;\n\n                    bool valid = true;\n                    for (const auto& rect : placed) {\n                        if (max(a_candidate, rect[0]) < min(a_candidate + w, rect[2]) &&\n                            max(b_candidate, rect[1]) < min(b_candidate + h, rect[3])) {\n                            valid = false;\n                            break;\n                        }\n                    }\n                    if (valid) {\n                        result[idx] = {a_candidate, b_candidate, a_candidate + w, b_candidate + h};\n                        placed.push_back({a_candidate, b_candidate, a_candidate + w, b_candidate + h});\n                        found = true;\n                        break;\n                    }\n                }\n                if (found) break;\n            }\n            if (found) break;\n        }\n        if (!found) {\n            result[idx] = {x, y, x + 1, y + 1};\n            placed.push_back({x, y, x + 1, y + 1});\n        }\n    }\n\n    for (int i = 0; i < n; i++) {\n        auto [a, b, c, d] = result[i];\n        cout << a << \" \" << b << \" \" << c << \" \" << d << '\\n';\n    }\n\n    return 0;\n}","ahc002":"#include <bits/stdc++.h>\n#include <boost/dynamic_bitset.hpp>\nusing namespace std;\n\nstruct PathNode {\n    char dir;\n    shared_ptr<PathNode> prev;\n    PathNode(char d, shared_ptr<PathNode> p) : dir(d), prev(p) {}\n};\n\nstruct State {\n    int i, j;\n    boost::dynamic_bitset<> visited;\n    long long score;\n    shared_ptr<PathNode> path_tail;\n    long long value;\n\n    State(int i, int j, int M) : i(i), j(j), visited(M), score(0), path_tail(nullptr), value(0) {}\n};\n\nstring get_path(shared_ptr<PathNode> tail) {\n    if (!tail) return \"\";\n    string s;\n    while (tail) {\n        s += tail->dir;\n        tail = tail->prev;\n    }\n    reverse(s.begin(), s.end());\n    return s;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\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\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    const vector<tuple<int, int, char>> dirs = {\n        {-1, 0, 'U'},\n        {1, 0, 'D'},\n        {0, -1, 'L'},\n        {0, 1, 'R'}\n    };\n\n    const int BEAM_WIDTH = 1000;\n    vector<State> current_beam;\n    State start_state(si, sj, M);\n    start_state.visited.set(t[si][sj]);\n    start_state.score = p[si][sj];\n\n    // Compute initial potential\n    vector<int> neighbors;\n    for (const auto& [dx, dy, _] : dirs) {\n        int ni = si + dx;\n        int nj = sj + dy;\n        if (ni >= 0 && ni < 50 && nj >= 0 && nj < 50) {\n            if (!start_state.visited[t[ni][nj]]) {\n                neighbors.push_back(p[ni][nj]);\n            }\n        }\n    }\n    sort(neighbors.rbegin(), neighbors.rend());\n    int potential = 0;\n    for (int k = 0; k < min(3, (int)neighbors.size()); k++) {\n        potential += neighbors[k] * (1.0 / (k + 1));\n    }\n    start_state.value = start_state.score + potential;\n\n    current_beam.push_back(start_state);\n\n    long long best_score = start_state.score;\n    string best_path = \"\";\n\n    while (!current_beam.empty()) {\n        vector<State> next_beam;\n        next_beam.reserve(current_beam.size() * 4);\n\n        for (State& state : current_beam) {\n            for (const auto& [dx, dy, dchar] : dirs) {\n                int ni = state.i + dx;\n                int nj = state.j + dy;\n                if (ni < 0 || ni >= 50 || nj < 0 || nj >= 50) continue;\n\n                int tile_id = t[ni][nj];\n                if (state.visited.test(tile_id)) continue;\n\n                State new_state(ni, nj, M);\n                new_state.visited = state.visited;\n                new_state.visited.set(tile_id);\n                new_state.score = state.score + p[ni][nj];\n\n                // Compute potential for new_state\n                vector<int> neighbors;\n                for (const auto& [dx2, dy2, _] : dirs) {\n                    int ni2 = ni + dx2;\n                    int nj2 = nj + dy2;\n                    if (ni2 >= 0 && ni2 < 50 && nj2 >= 0 && nj2 < 50) {\n                        int tid = t[ni2][nj2];\n                        if (!new_state.visited[tid]) {\n                            neighbors.push_back(p[ni2][nj2]);\n                        }\n                    }\n                }\n                sort(neighbors.rbegin(), neighbors.rend());\n                int potential_val = 0;\n                for (int k = 0; k < min(3, (int)neighbors.size()); k++) {\n                    potential_val += neighbors[k] * (1.0 / (k + 1));\n                }\n                new_state.value = new_state.score + potential_val;\n\n                new_state.path_tail = make_shared<PathNode>(dchar, state.path_tail);\n\n                if (new_state.score > best_score) {\n                    best_score = new_state.score;\n                    best_path = get_path(new_state.path_tail);\n                }\n\n                next_beam.push_back(move(new_state));\n            }\n        }\n\n        if (next_beam.empty()) break;\n\n        // Sort by value descending, then by score descending\n        sort(next_beam.begin(), next_beam.end(), [](const State& a, const State& b) {\n            if (a.value != b.value) return a.value > b.value;\n            return a.score > b.score;\n        });\n\n        if ((int)next_beam.size() > BEAM_WIDTH) {\n            current_beam.assign(next_beam.begin(), next_beam.begin() + BEAM_WIDTH);\n        } else {\n            current_beam = move(next_beam);\n        }\n    }\n\n    cout << best_path << '\\n';\n    return 0;\n}","ahc003":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    const int N = 30;\n    double W_h[N][N-1];\n    double W_v[N-1][N];\n\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N-1; j++) {\n            W_h[i][j] = 5000.0;\n        }\n    }\n    for (int i = 0; i < N-1; i++) {\n        for (int j = 0; j < N; j++) {\n            W_v[i][j] = 5000.0;\n        }\n    }\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, 1e18));\n        vector<vector<int>> parent_i(N, vector<int>(N, -1));\n        vector<vector<int>> parent_j(N, vector<int>(N, -1));\n\n        priority_queue<tuple<double, int, int>, vector<tuple<double, int, int>>, greater<tuple<double, int, int>>> pq;\n        dist[si][sj] = 0.0;\n        pq.push(make_tuple(0.0, si, sj));\n\n        const int di[4] = {-1, 1, 0, 0};\n        const int dj[4] = {0, 0, -1, 1};\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 + di[dir];\n                int nj = j + dj[dir];\n                if (ni < 0 || ni >= N || nj < 0 || nj >= N) continue;\n\n                double weight;\n                if (dir == 0) {\n                    weight = W_v[i-1][j];\n                } else if (dir == 1) {\n                    weight = W_v[i][j];\n                } else if (dir == 2) {\n                    weight = W_h[i][j-1];\n                } else {\n                    weight = W_h[i][j];\n                }\n\n                double nd = d + weight;\n                if (nd < dist[ni][nj]) {\n                    dist[ni][nj] = nd;\n                    parent_i[ni][nj] = i;\n                    parent_j[ni][nj] = j;\n                    pq.push(make_tuple(nd, ni, nj));\n                }\n            }\n        }\n\n        string path_str = \"\";\n        vector<tuple<int, int, int>> edges_list;\n        int i = ti, j = tj;\n        while (i != si || j != sj) {\n            int pi = parent_i[i][j];\n            int pj = parent_j[i][j];\n            if (pi == i-1) {\n                path_str += 'D';\n            } else if (pi == i+1) {\n                path_str += 'U';\n            } else if (pj == j-1) {\n                path_str += 'R';\n            } else if (pj == j+1) {\n                path_str += 'L';\n            }\n\n            if (pi == i-1) {\n                edges_list.push_back(make_tuple(0, i-1, j));\n            } else if (pi == i+1) {\n                edges_list.push_back(make_tuple(0, i, j));\n            } else if (pj == j-1) {\n                edges_list.push_back(make_tuple(1, i, j-1));\n            } else if (pj == j+1) {\n                edges_list.push_back(make_tuple(1, i, j));\n            }\n\n            i = pi;\n            j = pj;\n        }\n        reverse(path_str.begin(), path_str.end());\n\n        cout << path_str << \"\\n\";\n        cout.flush();\n\n        long long r;\n        cin >> r;\n\n        int L = path_str.size();\n        double est_length = dist[ti][tj];\n        double error = static_cast<double>(r) - est_length;\n\n        double lr = 0.1 / (k + 1);\n        for (auto &edge : edges_list) {\n            int type = get<0>(edge);\n            int idx1 = get<1>(edge);\n            int idx2 = get<2>(edge);\n            double step = lr * error / static_cast<double>(L);\n\n            if (type == 0) {\n                W_v[idx1][idx2] += step;\n                if (W_v[idx1][idx2] < 1000.0) W_v[idx1][idx2] = 1000.0;\n                else if (W_v[idx1][idx2] > 9000.0) W_v[idx1][idx2] = 9000.0;\n            } else {\n                W_h[idx1][idx2] += step;\n                if (W_h[idx1][idx2] < 1000.0) W_h[idx1][idx2] = 1000.0;\n                else if (W_h[idx1][idx2] > 9000.0) W_h[idx1][idx2] = 9000.0;\n            }\n        }\n    }\n\n    return 0;\n}","ahc004":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int N, M;\n    cin >> N >> M;\n    vector<string> strs(M);\n    for (int i = 0; i < M; i++) {\n        cin >> strs[i];\n    }\n\n    vector<int> indices(M);\n    iota(indices.begin(), indices.end(), 0);\n    sort(indices.begin(), indices.end(), [&](int i, int j) {\n        return strs[i].size() > strs[j].size();\n    });\n\n    vector<string> mat(N, string(N, '.'));\n    vector<bool> covered(M, false);\n    int covered_count = 0;\n\n    for (int idx : indices) {\n        string& s = strs[idx];\n        int L = s.size();\n        int best_dots = -1;\n        int best_dir = -1;\n        int best_i = -1;\n        int best_j = -1;\n\n        for (int i = 0; i < N; i++) {\n            for (int sc = 0; sc < N; sc++) {\n                bool ok = true;\n                int dots = 0;\n                for (int p = 0; p < L; p++) {\n                    int col = (sc + p) % N;\n                    if (mat[i][col] != '.' && mat[i][col] != s[p]) {\n                        ok = false;\n                        break;\n                    }\n                    if (mat[i][col] == '.') {\n                        dots++;\n                    }\n                }\n                if (ok && dots > best_dots) {\n                    best_dots = dots;\n                    best_dir = 0;\n                    best_i = i;\n                    best_j = sc;\n                }\n            }\n        }\n\n        for (int j = 0; j < N; j++) {\n            for (int sr = 0; sr < N; sr++) {\n                bool ok = true;\n                int dots = 0;\n                for (int p = 0; p < L; p++) {\n                    int row = (sr + p) % N;\n                    if (mat[row][j] != '.' && mat[row][j] != s[p]) {\n                        ok = false;\n                        break;\n                    }\n                    if (mat[row][j] == '.') {\n                        dots++;\n                    }\n                }\n                if (ok && dots > best_dots) {\n                    best_dots = dots;\n                    best_dir = 1;\n                    best_i = sr;\n                    best_j = j;\n                }\n            }\n        }\n\n        if (best_dots != -1) {\n            covered[idx] = true;\n            covered_count++;\n            if (best_dir == 0) {\n                for (int p = 0; p < L; p++) {\n                    int col = (best_j + p) % N;\n                    mat[best_i][col] = s[p];\n                }\n            } else {\n                for (int p = 0; p < L; p++) {\n                    int row = (best_i + p) % N;\n                    mat[row][best_j] = s[p];\n                }\n            }\n        }\n    }\n\n    bool improved;\n    do {\n        improved = false;\n        for (int idx = 0; idx < M && !improved; idx++) {\n            if (covered[idx]) continue;\n            string& s = strs[idx];\n            int L = s.size();\n\n            for (int i = 0; i < N && !improved; i++) {\n                for (int sc = 0; sc < N && !improved; sc++) {\n                    bool ok = true;\n                    for (int p = 0; p < L; p++) {\n                        int col = (sc + p) % N;\n                        if (mat[i][col] != '.' && mat[i][col] != s[p]) {\n                            ok = false;\n                            break;\n                        }\n                    }\n                    if (ok) {\n                        for (int p = 0; p < L; p++) {\n                            int col = (sc + p) % N;\n                            mat[i][col] = s[p];\n                        }\n                        covered[idx] = true;\n                        covered_count++;\n                        improved = true;\n                    }\n                }\n            }\n\n            if (improved) continue;\n\n            for (int j = 0; j < N && !improved; j++) {\n                for (int sr = 0; sr < N && !improved; sr++) {\n                    bool ok = true;\n                    for (int p = 0; p < L; p++) {\n                        int row = (sr + p) % N;\n                        if (mat[row][j] != '.' && mat[row][j] != s[p]) {\n                            ok = false;\n                            break;\n                        }\n                    }\n                    if (ok) {\n                        for (int p = 0; p < L; p++) {\n                            int row = (sr + p) % N;\n                            mat[row][j] = s[p];\n                        }\n                        covered[idx] = true;\n                        covered_count++;\n                        improved = true;\n                    }\n                }\n            }\n        }\n    } while (improved);\n\n    if (covered_count == M) {\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    }\n\n    for (int i = 0; i < N; i++) {\n        cout << mat[i] << '\\n';\n    }\n    return 0;\n}","ahc005":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MAX_R = 5000;\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, si, sj;\n    cin >> N >> si >> sj;\n    vector<string> grid(N);\n    for (int i = 0; i < N; i++) {\n        cin >> grid[i];\n    }\n\n    vector<pair<int, int>> road_cells;\n    map<pair<int, int>, int> cell_to_index;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (grid[i][j] != '#') {\n                cell_to_index[{i, j}] = road_cells.size();\n                road_cells.push_back({i, j});\n            }\n        }\n    }\n    int R = road_cells.size();\n    if (R == 0) {\n        cout << \"\" << endl;\n        return 0;\n    }\n\n    vector<bitset<MAX_R>> visibility(R);\n    for (int u = 0; u < R; u++) {\n        int i = road_cells[u].first;\n        int j = road_cells[u].second;\n        visibility[u].set(u);\n\n        for (int k = j - 1; k >= 0; k--) {\n            if (grid[i][k] == '#') break;\n            int idx = cell_to_index[{i, k}];\n            visibility[u].set(idx);\n        }\n        for (int k = j + 1; k < N; k++) {\n            if (grid[i][k] == '#') break;\n            int idx = cell_to_index[{i, k}];\n            visibility[u].set(idx);\n        }\n        for (int k = i - 1; k >= 0; k--) {\n            if (grid[k][j] == '#') break;\n            int idx = cell_to_index[{k, j}];\n            visibility[u].set(idx);\n        }\n        for (int k = i + 1; k < N; k++) {\n            if (grid[k][j] == '#') break;\n            int idx = cell_to_index[{k, j}];\n            visibility[u].set(idx);\n        }\n    }\n\n    int start_idx = cell_to_index[{si, sj}];\n    bitset<MAX_R> covered = visibility[start_idx];\n    int cur_i = si, cur_j = sj;\n    string route = \"\";\n\n    while (covered.count() < R) {\n        vector<vector<int>> dist(N, vector<int>(N, INT_MAX));\n        vector<vector<pair<int, int>>> parent(N, vector<pair<int, int>>(N, make_pair(-1, -1)));\n        priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<>> pq;\n\n        dist[cur_i][cur_j] = 0;\n        pq.emplace(0, cur_i, cur_j);\n\n        while (!pq.empty()) {\n            auto [d, i, j] = pq.top();\n            pq.pop();\n            if (d != dist[i][j]) continue;\n\n            const int dx[4] = {-1, 1, 0, 0};\n            const int dy[4] = {0, 0, -1, 1};\n            for (int k = 0; k < 4; k++) {\n                int ni = i + dx[k];\n                int nj = j + dy[k];\n                if (ni < 0 || ni >= N || nj < 0 || nj >= N) continue;\n                if (grid[ni][nj] == '#') continue;\n                int nd = d + (grid[ni][nj] - '0');\n                if (nd < dist[ni][nj]) {\n                    dist[ni][nj] = nd;\n                    parent[ni][nj] = {i, j};\n                    pq.emplace(nd, ni, nj);\n                }\n            }\n        }\n\n        int best_gain = -1;\n        int best_time = INT_MAX;\n        int best_next_idx = -1;\n\n        for (int next_idx = 0; next_idx < R; next_idx++) {\n            int ni = road_cells[next_idx].first;\n            int nj = road_cells[next_idx].second;\n            if (dist[ni][nj] == INT_MAX) continue;\n\n            bitset<MAX_R> new_cover = visibility[next_idx] & ~covered;\n            int gain = new_cover.count();\n\n            if (gain == 0) continue;\n\n            if (gain > best_gain || (gain == best_gain && dist[ni][nj] < best_time)) {\n                best_gain = gain;\n                best_time = dist[ni][nj];\n                best_next_idx = next_idx;\n            }\n        }\n\n        if (best_next_idx == -1) {\n            break;\n        }\n\n        int ni_target = road_cells[best_next_idx].first;\n        int nj_target = road_cells[best_next_idx].second;\n\n        vector<pair<int, int>> path_cells;\n        int ci = ni_target;\n        int cj = nj_target;\n        path_cells.push_back({ci, cj});\n        while (ci != cur_i || cj != cur_j) {\n            auto [pi, pj] = parent[ci][cj];\n            ci = pi;\n            cj = pj;\n            path_cells.push_back({ci, cj});\n        }\n        reverse(path_cells.begin(), path_cells.end());\n\n        for (int idx = 1; idx < path_cells.size(); idx++) {\n            int pi = path_cells[idx-1].first;\n            int pj = path_cells[idx-1].second;\n            int ci = path_cells[idx].first;\n            int cj = path_cells[idx].second;\n            if (ci == pi - 1) route += 'U';\n            else if (ci == pi + 1) route += 'D';\n            else if (cj == pj - 1) route += 'L';\n            else if (cj == pj + 1) route += 'R';\n        }\n\n        covered |= visibility[best_next_idx];\n        cur_i = ni_target;\n        cur_j = nj_target;\n    }\n\n    if (cur_i != si || cur_j != sj) {\n        vector<vector<int>> dist_end(N, vector<int>(N, INT_MAX));\n        vector<vector<pair<int, int>>> parent_end(N, vector<pair<int, int>>(N, make_pair(-1, -1)));\n        priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<>> pq_end;\n\n        dist_end[cur_i][cur_j] = 0;\n        pq_end.emplace(0, cur_i, cur_j);\n\n        while (!pq_end.empty()) {\n            auto [d, i, j] = pq_end.top();\n            pq_end.pop();\n            if (d != dist_end[i][j]) continue;\n\n            const int dx[4] = {-1, 1, 0, 0};\n            const int dy[4] = {0, 0, -1, 1};\n            for (int k = 0; k < 4; k++) {\n                int ni = i + dx[k];\n                int nj = j + dy[k];\n                if (ni < 0 || ni >= N || nj < 0 || nj >= N) continue;\n                if (grid[ni][nj] == '#') continue;\n                int nd = d + (grid[ni][nj] - '0');\n                if (nd < dist_end[ni][nj]) {\n                    dist_end[ni][nj] = nd;\n                    parent_end[ni][nj] = {i, j};\n                    pq_end.emplace(nd, ni, nj);\n                }\n            }\n        }\n\n        vector<pair<int, int>> return_path;\n        int ci = si;\n        int cj = sj;\n        return_path.push_back({ci, cj});\n        while (ci != cur_i || cj != cur_j) {\n            auto [pi, pj] = parent_end[ci][cj];\n            ci = pi;\n            cj = pj;\n            return_path.push_back({ci, cj});\n        }\n        reverse(return_path.begin(), return_path.end());\n\n        for (int idx = 1; idx < return_path.size(); idx++) {\n            int pi = return_path[idx-1].first;\n            int pj = return_path[idx-1].second;\n            int ci = return_path[idx].first;\n            int cj = return_path[idx].second;\n            if (ci == pi - 1) route += 'U';\n            else if (ci == pi + 1) route += 'D';\n            else if (cj == pj - 1) route += 'L';\n            else if (cj == pj + 1) route += 'R';\n        }\n    }\n\n    cout << route << endl;\n\n    return 0;\n}","future-contest-2022-qual":"#include <bits/stdc++.h>\nusing namespace std;\n\nmt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n\nstruct Task {\n    int id;\n    int priority;\n    bool operator<(const Task& other) const {\n        return priority < other.priority;\n    }\n};\n\nvoid update_skill(vector<int>& s, const vector<int>& d_task, int t_actual, int K, int day) {\n    int low_bound = max(0, t_actual - 3);\n    int high_bound = t_actual + 3;\n\n    int w_est = 0;\n    for (int k = 0; k < K; k++) {\n        if (d_task[k] > s[k]) {\n            w_est += (d_task[k] - s[k]);\n        }\n    }\n\n    if (w_est == 0) return;\n\n    double learning_rate = min(1.0, 0.1 + (500.0 / (500.0 + day)));\n    if (w_est < low_bound) {\n        int needed = static_cast<int>((low_bound - w_est) * learning_rate);\n        while (needed > 0) {\n            int best_k = -1;\n            int best_gain = 0;\n            for (int k = 0; k < K; k++) {\n                int def_old = max(0, d_task[k] - s[k]);\n                int def_new = max(0, d_task[k] - (s[k] - 1));\n                int gain = def_new - def_old;\n                if (gain > best_gain) {\n                    best_gain = gain;\n                    best_k = k;\n                }\n            }\n            if (best_k == -1) break;\n            s[best_k]--;\n            needed--;\n        }\n    } else if (w_est > high_bound) {\n        int needed = static_cast<int>((w_est - high_bound) * learning_rate);\n        while (needed > 0) {\n            int best_k = -1;\n            int best_gain = 0;\n            for (int k = 0; k < K; k++) {\n                int def_old = max(0, d_task[k] - s[k]);\n                int def_new = max(0, d_task[k] - (s[k] + 1));\n                int gain = def_old - def_new;\n                if (gain > best_gain) {\n                    best_gain = gain;\n                    best_k = k;\n                }\n            }\n            if (best_k == -1) break;\n            s[best_k]++;\n            needed--;\n        }\n    }\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>> d(N, vector<int>(K));\n    for (int i = 0; i < N; i++) {\n        for (int k = 0; k < K; k++) {\n            cin >> d[i][k];\n        }\n    }\n\n    vector<vector<int>> children(N);\n    vector<vector<int>> parents(N);\n    for (int i = 0; i < R; i++) {\n        int u, v;\n        cin >> u >> v;\n        u--; v--;\n        children[u].push_back(v);\n        parents[v].push_back(u);\n    }\n\n    vector<int> in_degree_count(N, 0);\n    for (int i = 0; i < N; i++) {\n        in_degree_count[i] = parents[i].size();\n    }\n\n    vector<int> busy_until(M, 1);\n    vector<int> start_day(M, -1);\n    vector<int> current_task(M, -1);\n    vector<vector<int>> skill_est(M, vector<int>(K, 0));\n    vector<bool> completed(N, false);\n    vector<bool> is_assigned(N, false);\n    priority_queue<Task> ready_queue;\n\n    for (int i = 0; i < N; i++) {\n        if (in_degree_count[i] == 0) {\n            ready_queue.push({i, (int)children[i].size()});\n        }\n    }\n\n    for (int day = 1; day <= 2000; day++) {\n        vector<int> free_members;\n        for (int j = 0; j < M; j++) {\n            if (busy_until[j] <= day) {\n                free_members.push_back(j);\n            }\n        }\n\n        sort(free_members.begin(), free_members.end(), [&](int a, int b) {\n            int sum_a = accumulate(skill_est[a].begin(), skill_est[a].end(), 0);\n            int sum_b = accumulate(skill_est[b].begin(), skill_est[b].end(), 0);\n            return sum_a > sum_b;\n        });\n\n        vector<pair<int, int>> assignments;\n\n        for (int j : free_members) {\n            if (ready_queue.empty()) break;\n\n            Task task = ready_queue.top();\n            ready_queue.pop();\n\n            int task_id = task.id;\n            if (completed[task_id] || is_assigned[task_id]) {\n                continue;\n            }\n\n            int best_member = -1;\n            int best_w = INT_MAX;\n            for (int member : free_members) {\n                if (current_task[member] != -1) continue;\n\n                int w = 0;\n                for (int skill = 0; skill < K; skill++) {\n                    if (d[task_id][skill] > skill_est[member][skill]) {\n                        w += (d[task_id][skill] - skill_est[member][skill]);\n                    }\n                }\n                if (w < best_w) {\n                    best_w = w;\n                    best_member = member;\n                }\n            }\n\n            if (best_member == -1) {\n                ready_queue.push(task);\n                continue;\n            }\n\n            assignments.push_back({best_member, task_id});\n            is_assigned[task_id] = true;\n            start_day[best_member] = day;\n            current_task[best_member] = task_id;\n\n            int t_est = (best_w == 0) ? 1 : max(1, best_w + uniform_int_distribution<int>(-3, 3)(rng));\n            busy_until[best_member] = day + t_est;\n        }\n\n        cout << assignments.size();\n        for (auto &p : assignments) {\n            cout << \" \" << p.first + 1 << \" \" << p.second + 1;\n        }\n        cout << endl;\n        cout.flush();\n\n        int n_completed;\n        cin >> n_completed;\n        if (n_completed == -1) {\n            break;\n        }\n\n        vector<int> completed_members(n_completed);\n        for (int i = 0; i < n_completed; i++) {\n            cin >> completed_members[i];\n            completed_members[i]--;\n        }\n\n        for (int j : completed_members) {\n            if (current_task[j] == -1) continue;\n\n            int task = current_task[j];\n            int t_actual = day - start_day[j] + 1;\n            update_skill(skill_est[j], d[task], t_actual, K, day);\n\n            completed[task] = true;\n            is_assigned[task] = false;\n\n            for (int child : children[task]) {\n                in_degree_count[child]--;\n                if (in_degree_count[child] == 0) {\n                    ready_queue.push({child, (int)children[child].size()});\n                }\n            }\n\n            busy_until[j] = day + 1;\n            start_day[j] = -1;\n            current_task[j] = -1;\n        }\n    }\n\n    return 0;\n}","ahc006":"#include <bits/stdc++.h>\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        return x == other.x && y == other.y;\n    }\n};\n\nint dist(const Point& a, const Point& b) {\n    return abs(a.x - b.x) + abs(a.y - b.y);\n}\n\nint main() {\n    vector<tuple<int, int, int, int>> orders;\n    for (int i = 0; i < 1000; i++) {\n        int a, b, c, d;\n        cin >> a >> b >> c >> d;\n        orders.emplace_back(a, b, c, d);\n    }\n\n    vector<Point> route = { Point(400, 400), Point(400, 400) };\n    vector<bool> selected(1000, false);\n    vector<int> pos_r(1000, -1);\n    vector<int> pos_d(1000, -1);\n\n    auto compute_total_distance = [&](const vector<Point>& r) {\n        int d = 0;\n        for (int i = 0; i < (int)r.size() - 1; i++) {\n            d += dist(r[i], r[i+1]);\n        }\n        return d;\n    };\n    int current_dist = compute_total_distance(route);\n\n    for (int iter = 0; iter < 50; iter++) {\n        long long best_delta = 1e18;\n        int best_order = -1;\n        int best_k = -1;\n        int L_route = route.size();\n\n        for (int idx = 0; idx < 1000; idx++) {\n            if (selected[idx]) continue;\n            auto [a, b, c, d] = orders[idx];\n            long long min_delta = 1e18;\n            int min_k = -1;\n\n            for (int k = 1; k < L_route; k++) {\n                Point prev = route[k-1];\n                Point next = route[k];\n                int delta = dist(prev, Point(a, b)) + dist(Point(a, b), Point(c, d)) + dist(Point(c, d), next) - dist(prev, next);\n                if (delta < min_delta) {\n                    min_delta = delta;\n                    min_k = k;\n                }\n            }\n\n            if (min_delta < best_delta) {\n                best_delta = min_delta;\n                best_order = idx;\n                best_k = min_k;\n            }\n        }\n\n        if (best_order == -1) {\n            for (int idx = 0; idx < 1000; idx++) {\n                if (!selected[idx]) {\n                    best_order = idx;\n                    best_k = 1;\n                    break;\n                }\n            }\n        }\n\n        auto [a, b, c, d] = orders[best_order];\n        selected[best_order] = true;\n        route.insert(route.begin() + best_k, Point(a, b));\n        route.insert(route.begin() + best_k + 1, Point(c, d));\n        pos_r[best_order] = best_k;\n        pos_d[best_order] = best_k + 1;\n        current_dist += best_delta;\n        L_route = route.size();\n    }\n\n    bool improved;\n    int passes = 0;\n    do {\n        improved = false;\n        int n = route.size();\n        for (int i = 0; i < n-1 && !improved; i++) {\n            for (int j = i+2; j < n-1 && !improved; j++) {\n                Point A = route[i];\n                Point B = route[i+1];\n                Point K = route[j];\n                Point L = route[j+1];\n                int delta = dist(A, K) + dist(B, L) - dist(A, B) - dist(K, L);\n                if (delta >= 0) continue;\n\n                vector<Point> candidate = route;\n                reverse(candidate.begin() + i+1, candidate.begin() + j+1);\n\n                vector<int> candidate_pos_r(1000, -1);\n                vector<int> candidate_pos_d(1000, -1);\n                for (int p = 0; p < candidate.size(); p++) {\n                    Point pt = candidate[p];\n                    for (int idx = 0; idx < 1000; idx++) {\n                        if (!selected[idx]) continue;\n                        auto [a_val, b_val, c_val, d_val] = orders[idx];\n                        if (candidate_pos_r[idx] == -1 && pt.x == a_val && pt.y == b_val) {\n                            candidate_pos_r[idx] = p;\n                        } else if (candidate_pos_d[idx] == -1 && pt.x == c_val && pt.y == d_val) {\n                            candidate_pos_d[idx] = p;\n                        }\n                    }\n                }\n\n                bool valid = true;\n                for (int idx = 0; idx < 1000; idx++) {\n                    if (!selected[idx]) continue;\n                    if (candidate_pos_r[idx] == -1 || candidate_pos_d[idx] == -1 || candidate_pos_r[idx] >= candidate_pos_d[idx]) {\n                        valid = false;\n                        break;\n                    }\n                }\n                if (!valid) continue;\n\n                route = move(candidate);\n                current_dist += delta;\n                pos_r = move(candidate_pos_r);\n                pos_d = move(candidate_pos_d);\n                improved = true;\n            }\n        }\n        passes++;\n    } while (improved && passes < 1000);\n\n    vector<int> selected_indices;\n    for (int i = 0; i < 1000; i++) {\n        if (selected[i]) {\n            selected_indices.push_back(i+1);\n        }\n    }\n    sort(selected_indices.begin(), selected_indices.end());\n\n    cout << 50;\n    for (int idx : selected_indices) {\n        cout << \" \" << idx;\n    }\n    cout << \"\\n\";\n\n    int n_route = route.size();\n    cout << n_route;\n    for (int i = 0; i < n_route; i++) {\n        cout << \" \" << route[i].x << \" \" << route[i].y;\n    }\n    cout << \"\\n\";\n\n    return 0;\n}","ahc007":"#include <iostream>\n#include <vector>\n#include <cmath>\n#include <algorithm>\n#include <functional>\nusing namespace std;\n\nstruct Edge {\n    int u, v;\n};\n\nint main() {\n    const int N = 400;\n    const int M = 1995;\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<Edge> edges(M);\n    vector<int> d(M, 0);\n\n    for (int i = 0; i < M; i++) {\n        cin >> edges[i].u >> edges[i].v;\n    }\n\n    for (int i = 0; i < M; i++) {\n        int u = edges[i].u;\n        int v = edges[i].v;\n        long long dx = (long long)x[u] - x[v];\n        long long dy = (long long)y[u] - y[v];\n        double dist = sqrt(dx*dx + dy*dy);\n        d[i] = (int)(dist + 0.5);\n    }\n\n    vector<int> parent(N);\n    vector<int> size_vec(N, 1);\n    vector<int> mout_comp(N, 10000000);\n\n    for (int i = 0; i < N; i++) {\n        parent[i] = i;\n    }\n\n    for (int i = 0; i < M; i++) {\n        int u = edges[i].u;\n        int v = edges[i].v;\n        if (d[i] < mout_comp[u]) mout_comp[u] = d[i];\n        if (d[i] < mout_comp[v]) mout_comp[v] = d[i];\n    }\n\n    function<int(int)> find = [&](int x) -> int {\n        if (parent[x] != x) {\n            parent[x] = find(parent[x]);\n        }\n        return parent[x];\n    };\n\n    int count = 0;\n\n    for (int i = 0; i < M; i++) {\n        int l;\n        cin >> l;\n\n        int u = edges[i].u;\n        int v = edges[i].v;\n        int ru = find(u);\n        int rv = find(v);\n\n        if (ru == rv) {\n            cout << 0 << endl;\n            cout.flush();\n            continue;\n        }\n\n        if (count == N - 1) {\n            cout << 0 << endl;\n            cout.flush();\n            continue;\n        }\n\n        int m = min(mout_comp[ru], mout_comp[rv]);\n\n        if (l <= m) {\n            cout << 1 << endl;\n            cout.flush();\n            count++;\n\n            if (size_vec[ru] < size_vec[rv]) {\n                parent[ru] = rv;\n                mout_comp[rv] = m;\n                size_vec[rv] += size_vec[ru];\n            } else {\n                parent[rv] = ru;\n                mout_comp[ru] = m;\n                size_vec[ru] += size_vec[rv];\n            }\n        } else {\n            cout << 0 << endl;\n            cout.flush();\n        }\n    }\n\n    return 0;\n}","ahc008":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int H = 30;\nconst int W = 30;\nint dx[4] = {-1, 1, 0, 0};\nint dy[4] = {0, 0, -1, 1};\nchar move_dir[4] = {'U', 'D', 'L', 'R'};\nchar wall_dir[4] = {'u', 'd', 'l', 'r'};\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N;\n    cin >> N;\n    vector<pair<int, int>> pets;\n    for (int i = 0; i < N; i++) {\n        int x, y, t;\n        cin >> x >> y >> t;\n        pets.push_back({x, y});\n    }\n\n    int M;\n    cin >> M;\n    vector<pair<int, int>> humans;\n    for (int i = 0; i < M; i++) {\n        int x, y;\n        cin >> x >> y;\n        humans.push_back({x, y});\n    }\n\n    bool grid[H+1][W+1];\n    for (int i = 1; i <= H; i++) {\n        for (int j = 1; j <= W; j++) {\n            grid[i][j] = true;\n        }\n    }\n\n    for (int turn = 0; turn < 300; turn++) {\n        string actions = \"\";\n        for (int i = 0; i < M; i++) {\n            vector<char> candidates;\n            candidates.push_back('.');\n\n            for (int d = 0; d < 4; d++) {\n                int nx = humans[i].first + dx[d];\n                int ny = humans[i].second + dy[d];\n                if (nx >= 1 && nx <= H && ny >= 1 && ny <= W && grid[nx][ny]) {\n                    candidates.push_back(move_dir[d]);\n                }\n            }\n\n            for (int d = 0; d < 4; d++) {\n                int wx = humans[i].first + dx[d];\n                int wy = humans[i].second + dy[d];\n                if (wx < 1 || wx > H || wy < 1 || wy > W) continue;\n                if (!grid[wx][wy]) continue;\n\n                bool occupied = false;\n                for (int j = 0; j < M; j++) {\n                    if (humans[j].first == wx && humans[j].second == wy) {\n                        occupied = true;\n                        break;\n                    }\n                }\n                if (occupied) continue;\n                for (int j = 0; j < N; j++) {\n                    if (pets[j].first == wx && pets[j].second == wy) {\n                        occupied = true;\n                        break;\n                    }\n                }\n                if (occupied) continue;\n\n                bool adjacent_pet = false;\n                for (int dd = 0; dd < 4; dd++) {\n                    int nx = wx + dx[dd];\n                    int ny = wy + dy[dd];\n                    if (nx < 1 || nx > H || ny < 1 || ny > W) continue;\n                    for (int j = 0; j < N; j++) {\n                        if (pets[j].first == nx && pets[j].second == ny) {\n                            adjacent_pet = true;\n                            break;\n                        }\n                    }\n                    if (adjacent_pet) break;\n                }\n                if (adjacent_pet) continue;\n\n                candidates.push_back(wall_dir[d]);\n            }\n\n            double best_score = -1e18;\n            char best_act = '.';\n            for (char act : candidates) {\n                bool temp_grid[H+1][W+1];\n                for (int x = 1; x <= H; x++) {\n                    for (int y = 1; y <= W; y++) {\n                        temp_grid[x][y] = grid[x][y];\n                    }\n                }\n                pair<int, int> start_pos = humans[i];\n\n                if (act == '.') {\n                } else if (act >= 'A' && act <= 'Z') {\n                    int d;\n                    if (act == 'U') d = 0;\n                    else if (act == 'D') d = 1;\n                    else if (act == 'L') d = 2;\n                    else d = 3;\n                    int nx = start_pos.first + dx[d];\n                    int ny = start_pos.second + dy[d];\n                    start_pos = {nx, ny};\n                } else {\n                    int d;\n                    if (act == 'u') d = 0;\n                    else if (act == 'd') d = 1;\n                    else if (act == 'l') d = 2;\n                    else d = 3;\n                    int wx = start_pos.first + dx[d];\n                    int wy = start_pos.second + dy[d];\n                    temp_grid[wx][wy] = false;\n                }\n\n                bool visited[H+1][W+1] = {};\n                int comp_size = 0;\n                int pet_count = 0;\n                queue<pair<int, int>> q;\n                q.push(start_pos);\n                visited[start_pos.first][start_pos.second] = true;\n\n                while (!q.empty()) {\n                    auto [x, y] = q.front();\n                    q.pop();\n                    comp_size++;\n                    for (int j = 0; j < N; j++) {\n                        if (pets[j].first == x && pets[j].second == y) {\n                            pet_count++;\n                            break;\n                        }\n                    }\n                    for (int d = 0; d < 4; d++) {\n                        int nx = x + dx[d];\n                        int ny = y + dy[d];\n                        if (nx >= 1 && nx <= H && ny >= 1 && ny <= W && !visited[nx][ny] && temp_grid[nx][ny]) {\n                            visited[nx][ny] = true;\n                            q.push({nx, ny});\n                        }\n                    }\n                }\n\n                double factor = pow(2.0, -pet_count);\n                double score = comp_size * factor;\n\n                if (score > best_score) {\n                    best_score = score;\n                    best_act = act;\n                }\n            }\n            actions += best_act;\n        }\n\n        cout << actions << endl;\n        cout.flush();\n\n        vector<string> pet_moves(N);\n        for (int i = 0; i < N; i++) {\n            cin >> pet_moves[i];\n        }\n\n        for (int i = 0; i < M; i++) {\n            if (actions[i] >= 'a' && actions[i] <= 'z') {\n                int d;\n                if (actions[i] == 'u') d = 0;\n                else if (actions[i] == 'd') d = 1;\n                else if (actions[i] == 'l') d = 2;\n                else d = 3;\n                int wx = humans[i].first + dx[d];\n                int wy = humans[i].second + dy[d];\n                grid[wx][wy] = false;\n            }\n        }\n\n        for (int i = 0; i < M; i++) {\n            if (actions[i] >= 'A' && actions[i] <= 'Z') {\n                int d;\n                if (actions[i] == 'U') d = 0;\n                else if (actions[i] == 'D') d = 1;\n                else if (actions[i] == 'L') d = 2;\n                else d = 3;\n                int nx = humans[i].first + dx[d];\n                int ny = humans[i].second + dy[d];\n                if (nx >= 1 && nx <= H && ny >= 1 && ny <= W && grid[nx][ny]) {\n                    humans[i] = {nx, ny};\n                }\n            }\n        }\n\n        for (int i = 0; i < N; i++) {\n            int x = pets[i].first;\n            int y = pets[i].second;\n            string& moves = pet_moves[i];\n            for (char c : moves) {\n                int nx = x, ny = y;\n                if (c == 'U') nx--;\n                else if (c == 'D') nx++;\n                else if (c == 'L') ny--;\n                else if (c == 'R') ny++;\n                if (nx >= 1 && nx <= H && ny >= 1 && ny <= W && grid[nx][ny]) {\n                    x = nx;\n                    y = ny;\n                }\n            }\n            pets[i] = {x, y};\n        }\n    }\n\n    return 0;\n}","ahc009":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int INF = 1e9;\n\nstruct State {\n    int steps;\n    int dir_changes;\n    int i;\n    int j;\n    int last_dir;\n    State(int s, int dc, int i, int j, int ld) \n        : steps(s), dir_changes(dc), i(i), j(j), last_dir(ld) {}\n    bool operator<(const State& other) const {\n        if (steps != other.steps) return steps > other.steps;\n        if (dir_changes != other.dir_changes) return dir_changes > other.dir_changes;\n        return false;\n    }\n};\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int si, sj, ti, tj;\n    double p;\n    cin >> si >> sj >> ti >> tj >> 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    const vector<int> di = {-1, 1, 0, 0};\n    const vector<int> dj = {0, 0, -1, 1};\n    const vector<char> dir_char = {'U', 'D', 'L', 'R'};\n\n    vector<vector<vector<pair<int, int>>>> best(20, vector<vector<pair<int, int>>>(20, vector<pair<int, int>>(5, {INF, INF})));\n    vector<vector<vector<array<int, 3>>>> parent(20, vector<vector<array<int, 3>>>(20, vector<array<int, 3>>(5, {-1, -1, -1})));\n\n    priority_queue<State> pq;\n    best[si][sj][4] = {0, 0};\n    pq.push(State(0, 0, si, sj, 4));\n\n    while (!pq.empty()) {\n        State s = pq.top();\n        pq.pop();\n\n        if (s.steps != best[s.i][s.j][s.last_dir].first || s.dir_changes != best[s.i][s.j][s.last_dir].second) {\n            continue;\n        }\n\n        for (int d = 0; d < 4; d++) {\n            int ni = s.i + di[d];\n            int nj = s.j + dj[d];\n            if (ni < 0 || ni >= 20 || nj < 0 || nj >= 20) continue;\n\n            bool wall = false;\n            if (d == 0) {\n                if (s.i == 0) wall = true;\n                else if (v[s.i-1][s.j] == '1') wall = true;\n            } else if (d == 1) {\n                if (s.i == 19) wall = true;\n                else if (v[s.i][s.j] == '1') wall = true;\n            } else if (d == 2) {\n                if (s.j == 0) wall = true;\n                else if (h[s.i][s.j-1] == '1') wall = true;\n            } else if (d == 3) {\n                if (s.j == 19) wall = true;\n                else if (h[s.i][s.j] == '1') wall = true;\n            }\n            if (wall) continue;\n\n            int new_dir_changes = s.dir_changes;\n            if (s.last_dir != 4 && s.last_dir != d) {\n                new_dir_changes = s.dir_changes + 1;\n            }\n\n            int new_steps = s.steps + 1;\n            if (new_steps < best[ni][nj][d].first || (new_steps == best[ni][nj][d].first && new_dir_changes < best[ni][nj][d].second)) {\n                best[ni][nj][d] = {new_steps, new_dir_changes};\n                parent[ni][nj][d] = {s.i, s.j, s.last_dir};\n                pq.push(State(new_steps, new_dir_changes, ni, nj, d));\n            }\n        }\n    }\n\n    int best_steps = INF;\n    int best_dir_changes = INF;\n    int best_last_dir = -1;\n    for (int d = 0; d < 5; d++) {\n        if (best[ti][tj][d].first < best_steps || (best[ti][tj][d].first == best_steps && best[ti][tj][d].second < best_dir_changes)) {\n            best_steps = best[ti][tj][d].first;\n            best_dir_changes = best[ti][tj][d].second;\n            best_last_dir = d;\n        }\n    }\n\n    if (best_steps == INF) {\n        cout << \"\" << endl;\n        return 0;\n    }\n\n    vector<int> directions;\n    int i = ti, j = tj, last_dir = best_last_dir;\n    while (!(i == si && j == sj && last_dir == 4)) {\n        auto [pi, pj, pd] = parent[i][j][last_dir];\n        directions.push_back(last_dir);\n        i = pi;\n        j = pj;\n        last_dir = pd;\n    }\n    reverse(directions.begin(), directions.end());\n\n    if (directions.empty()) {\n        cout << \"\" << endl;\n        return 0;\n    }\n\n    int L = directions.size();\n    int k = 200 / L;\n    string ans;\n    for (int idx = 0; idx < k; idx++) {\n        for (int d : directions) {\n            ans += dir_char[d];\n        }\n    }\n\n    cout << ans << endl;\n\n    return 0;\n}","ahc010":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int di[4] = {0, -1, 0, 1};\nconst int dj[4] = {-1, 0, 1, 0};\nconst int to[8][4] = {\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\npair<int, int> get_top_two_cycles(const vector<string>& initial_types, const vector<vector<int>>& rotations) {\n    int grid[30][30];\n    for (int i = 0; i < 30; ++i) {\n        for (int j = 0; j < 30; ++j) {\n            int t = initial_types[i][j] - '0';\n            int r = rotations[i][j];\n            if (t < 4) {\n                grid[i][j] = (t + r) % 4;\n            } else {\n                grid[i][j] = t ^ (r & 1);\n            }\n        }\n    }\n\n    bool vis[30][30][4] = {};\n    int max1 = 0, max2 = 0;\n\n    for (int i = 0; i < 30; ++i) {\n        for (int j = 0; j < 30; ++j) {\n            for (int d = 0; d < 4; ++d) {\n                if (vis[i][j][d]) continue;\n\n                int ci = i, cj = j, cd = d;\n                int steps = 0;\n\n                while (true) {\n                    if (vis[ci][cj][cd]) {\n                        break;\n                    }\n                    vis[ci][cj][cd] = true;\n\n                    int s = grid[ci][cj];\n                    int nd = to[s][cd];\n                    if (nd == -1) {\n                        break;\n                    }\n\n                    int ni = ci + di[nd];\n                    int nj = cj + dj[nd];\n                    if (ni < 0 || ni >= 30 || nj < 0 || nj >= 30) {\n                        break;\n                    }\n\n                    cd = (nd + 2) % 4;\n                    ci = ni;\n                    cj = nj;\n                    steps++;\n\n                    if (ci == i && cj == j && cd == d) {\n                        if (steps > max1) {\n                            max2 = max1;\n                            max1 = steps;\n                        } else if (steps > max2) {\n                            max2 = steps;\n                        }\n                        break;\n                    }\n                }\n            }\n        }\n    }\n    return make_pair(max1, max2);\n}\n\nint main() {\n    vector<string> initial_types(30);\n    for (int i = 0; i < 30; i++) {\n        cin >> initial_types[i];\n    }\n\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n    uniform_int_distribution<int> dist(0, 3);\n\n    vector<vector<int>> best_rotations(30, vector<int>(30, 0));\n    long long best_score = 0;\n\n    for (int trial = 0; trial < 5; trial++) {\n        vector<vector<int>> rotations(30, vector<int>(30));\n        for (int i = 0; i < 30; i++) {\n            for (int j = 0; j < 30; j++) {\n                rotations[i][j] = dist(rng);\n            }\n        }\n\n        auto top_two = get_top_two_cycles(initial_types, rotations);\n        long long current_score = (top_two.second > 0) ? 1LL * top_two.first * top_two.second : 0;\n\n        if (current_score > best_score) {\n            best_score = current_score;\n            best_rotations = rotations;\n        }\n\n        bool improved;\n        for (int pass = 0; pass < 3; pass++) {\n            improved = false;\n            vector<pair<int, int>> tiles;\n            for (int i = 0; i < 30; i++) {\n                for (int j = 0; j < 30; j++) {\n                    tiles.emplace_back(i, j);\n                }\n            }\n            shuffle(tiles.begin(), tiles.end(), rng);\n\n            for (auto [i, j] : tiles) {\n                int original_r = rotations[i][j];\n                long long original_score = current_score;\n                int best_r = original_r;\n                long long best_score_for_tile = original_score;\n\n                for (int r = 0; r < 4; r++) {\n                    if (r == original_r) continue;\n                    rotations[i][j] = r;\n                    auto new_top = get_top_two_cycles(initial_types, rotations);\n                    long long new_score = (new_top.second > 0) ? 1LL * new_top.first * new_top.second : 0;\n                    rotations[i][j] = original_r;\n\n                    if (new_score > best_score_for_tile) {\n                        best_score_for_tile = new_score;\n                        best_r = r;\n                    }\n                }\n\n                if (best_r != original_r) {\n                    rotations[i][j] = best_r;\n                    current_score = best_score_for_tile;\n                    improved = true;\n                    if (current_score > best_score) {\n                        best_score = current_score;\n                        best_rotations = rotations;\n                    }\n                }\n            }\n            if (!improved) break;\n        }\n    }\n\n    string output;\n    for (int i = 0; i < 30; i++) {\n        for (int j = 0; j < 30; j++) {\n            output += ('0' + best_rotations[i][j]);\n        }\n    }\n    cout << output << endl;\n    return 0;\n}","ahc011":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <random>\n#include <queue>\n#include <climits>\n#include <tuple>\n#include <cctype>\n#include <chrono>\n#include <set>\nusing namespace std;\n\nstruct State {\n    vector<vector<int>> grid;\n    int empty_i, empty_j;\n    string moves;\n    vector<vector<bool>> fixed;\n\n    State(vector<vector<int>> g, int ei, int ej, string m = \"\")\n        : grid(g), empty_i(ei), empty_j(ej), moves(m) {\n        fixed = vector<vector<bool>>(grid.size(), vector<bool>(grid.size(), false));\n    }\n\n    bool is_goal(int N, const vector<vector<int>>& target) const {\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                if (grid[i][j] != target[i][j]) return false;\n            }\n        }\n        return true;\n    }\n\n    int heuristic(int N, const vector<vector<int>>& target) const {\n        int h = 0;\n        int center_dist = abs(empty_i - N/2) + abs(empty_j - N/2);\n\n        int mismatch = 0;\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                if (grid[i][j] != target[i][j]) {\n                    mismatch++;\n                    if (grid[i][j] == 0) {\n                        h += abs(i - N/2) + abs(j - N/2);\n                    } else {\n                        int target_dist = abs(target[i][j] - grid[i][j]);\n                        h += min(target_dist, 16 - target_dist);\n                    }\n                }\n            }\n        }\n\n        return mismatch + center_dist + h/3;\n    }\n\n    void generate_moves(vector<tuple<char, int, int, int, int>>& moves_list, int N, const vector<vector<int>>& target) {\n        vector<pair<int, int>> dirs = {{-1,0}, {1,0}, {0,-1}, {0,1}};\n        char dir_chars[4] = {'U','D','L','R'};\n\n        for (int d = 0; d < 4; d++) {\n            int ni = empty_i + dirs[d].first;\n            int nj = empty_j + dirs[d].second;\n            if (ni < 0 || ni >= N || nj < 0 || nj >= N) continue;\n\n            int benefit = 0;\n            int moved_val = grid[ni][nj];\n            if (moved_val != 0) {\n                benefit = 1;\n                if (moved_val == target[ni][nj]) benefit += 2;\n                if (moved_val == target[empty_i][empty_j]) benefit += 1;\n            }\n\n            moves_list.emplace_back(dir_chars[d], ni, nj, d, benefit);\n        }\n\n        sort(moves_list.begin(), moves_list.end(), [](const auto& a, const auto& b) {\n            return get<4>(a) > get<4>(b);\n        });\n    }\n};\n\nvector<vector<int>> generate_spanning_tree(int N, int target_empty_i, int target_empty_j, mt19937& rng) {\n    vector<vector<int>> parent(N, vector<int>(N, -1));\n    vector<bool> edges_added(N*N, false);\n    vector<int> edges;\n\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (i == target_empty_i && j == target_empty_j) continue;\n            int idx = i * N + j;\n            if (i > 0 && !(i-1 == target_empty_i && j == target_empty_j)) {\n                edges.push_back(idx - N);\n            }\n            if (j > 0 && !(i == target_empty_i && j-1 == target_empty_j)) {\n                edges.push_back(idx - 1);\n            }\n        }\n    }\n\n    shuffle(edges.begin(), edges.end(), rng);\n\n    vector<int> dsu(N*N, -1);\n    auto find = [&](int x) {\n        while (dsu[x] >= 0) x = dsu[x];\n        return x;\n    };\n    auto unite = [&](int x, int y) {\n        x = find(x);\n        y = find(y);\n        if (x == y) return false;\n        if (dsu[x] > dsu[y]) swap(x, y);\n        dsu[x] += dsu[y];\n        dsu[y] = x;\n        return true;\n    };\n\n    for (int e : edges) {\n        int x = e / N;\n        int y = e % N;\n        int px = (x == 0) ? -1 : x - 1;\n        int py = y;\n        int qx = x;\n        int qy = (y == 0) ? -1 : y - 1;\n\n        bool added = false;\n        if (px >= 0 && !(px == target_empty_i && py == target_empty_j)) {\n            if (unite(e, px * N + py)) {\n                parent[x][y] = px * N + py;\n                added = true;\n            }\n        }\n        if (!added && py >= 0 && !(x == target_empty_i && py == target_empty_j)) {\n            if (unite(e, x * N + py)) {\n                parent[x][y] = x * N + py;\n                added = true;\n            }\n        }\n    }\n\n    vector<vector<int>> target_grid(N, vector<int>(N, 0));\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (i == target_empty_i && j == target_empty_j) {\n                target_grid[i][j] = 0;\n                continue;\n            }\n            int bitmask = 0;\n            if (i > 0 && parent[i-1][j] == i*N+j) bitmask |= 8;\n            if (i < N-1 && parent[i+1][j] == i*N+j) bitmask |= 2;\n            if (j > 0 && parent[i][j-1] == i*N+j) bitmask |= 4;\n            if (j < N-1 && parent[i][j+1] == i*N+j) bitmask |= 1;\n            target_grid[i][j] = bitmask;\n        }\n    }\n\n    return target_grid;\n}\n\nvector<vector<int>> generate_snake_pattern(int N, int target_empty_i, int target_empty_j, mt19937& rng) {\n    vector<vector<int>> target_grid(N, vector<int>(N, 0));\n    vector<vector<bool>> visited(N, vector<bool>(N, false));\n    visited[target_empty_i][target_empty_j] = true;\n\n    vector<pair<int, int>> path;\n    if (rng() % 2 == 0) {\n        for (int i = 0; i < N; i++) {\n            if (i % 2 == 0) {\n                for (int j = 0; j < N; j++) {\n                    if (i == target_empty_i && j == target_empty_j) continue;\n                    path.push_back({i, j});\n                    visited[i][j] = true;\n                }\n            } else {\n                for (int j = N-1; j >= 0; j--) {\n                    if (i == target_empty_i && j == target_empty_j) continue;\n                    path.push_back({i, j});\n                    visited[i][j] = true;\n                }\n            }\n        }\n    } else {\n        for (int j = 0; j < N; j++) {\n            if (j % 2 == 0) {\n                for (int i = 0; i < N; i++) {\n                    if (i == target_empty_i && j == target_empty_j) continue;\n                    path.push_back({i, j});\n                    visited[i][j] = true;\n                }\n            } else {\n                for (int i = N-1; i >= 0; i--) {\n                    if (i == target_empty_i && j == target_empty_j) continue;\n                    path.push_back({i, j});\n                    visited[i][j] = true;\n                }\n            }\n        }\n    }\n\n    for (size_t i = 1; i < path.size(); i++) {\n        auto [pi, pj] = path[i-1];\n        auto [ci, cj] = path[i];\n        if (pi == ci && pj == cj-1) {\n            target_grid[ci][cj] |= 1;\n            target_grid[pi][pj] |= 4;\n        } else if (pi == ci && pj == cj+1) {\n            target_grid[ci][cj] |= 4;\n            target_grid[pi][pj] |= 1;\n        } else if (pi == ci-1 && pj == cj) {\n            target_grid[ci][cj] |= 2;\n            target_grid[pi][pj] |= 8;\n        } else if (pi == ci+1 && pj == cj) {\n            target_grid[ci][cj] |= 8;\n            target_grid[pi][pj] |= 2;\n        }\n    }\n\n    return target_grid;\n}\n\nvoid local_search_improvement(vector<vector<int>>& grid, int& empty_i, int& empty_j,\n                             const vector<vector<int>>& target, string& moves, int N, mt19937& rng) {\n    bool improved;\n    int max_iter = 2000;\n    do {\n        improved = false;\n        max_iter--;\n        if (max_iter <= 0) break;\n\n        vector<pair<int, int>> mismatches;\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                if (grid[i][j] != target[i][j]) {\n                    mismatches.emplace_back(i, j);\n                }\n            }\n        }\n\n        if (mismatches.empty()) break;\n\n        shuffle(mismatches.begin(), mismatches.end(), rng);\n        auto [best_i, best_j] = mismatches[0];\n        int best_val = grid[best_i][best_j];\n\n        vector<pair<int, int>> targets;\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                if (target[i][j] == best_val) {\n                    targets.emplace_back(i, j);\n                }\n            }\n        }\n\n        if (targets.empty()) continue;\n\n        shuffle(targets.begin(), targets.end(), rng);\n        auto [target_i, target_j] = targets[0];\n\n        if (best_val == 0) {\n            int dist = abs(empty_i - target_i) + abs(empty_j - target_j);\n            if (dist <= 5) {\n                vector<pair<char, pair<int, int>>> path;\n                int ci = empty_i, cj = empty_j;\n                while (ci != target_i || cj != target_j) {\n                    if (ci < target_i) {\n                        path.push_back({'D', {ci+1, cj}});\n                        ci++;\n                    } else if (ci > target_i) {\n                        path.push_back({'U', {ci-1, cj}});\n                        ci--;\n                    } else if (cj < target_j) {\n                        path.push_back({'R', {ci, cj+1}});\n                        cj++;\n                    } else {\n                        path.push_back({'L', {ci, cj-1}});\n                        cj--;\n                    }\n                }\n\n                vector<vector<int>> temp_grid = grid;\n                string temp_moves = moves;\n                int temp_empty_i = empty_i, temp_empty_j = empty_j;\n\n                bool valid = true;\n                for (auto [dir, pos] : path) {\n                    int ni = pos.first, nj = pos.second;\n                    if (temp_grid[ni][nj] != 0) {\n                        valid = false;\n                        break;\n                    }\n                    temp_moves += dir;\n                    swap(temp_grid[temp_empty_i][temp_empty_j], temp_grid[ni][nj]);\n                    temp_empty_i = ni;\n                    temp_empty_j = nj;\n                }\n\n                if (valid) {\n                    grid = temp_grid;\n                    moves = temp_moves;\n                    empty_i = temp_empty_i;\n                    empty_j = temp_empty_j;\n                    improved = true;\n                    continue;\n                }\n            }\n        }\n\n        vector<pair<int, int>> dirs = {{-1,0}, {1,0}, {0,-1}, {0,1}};\n        char dir_chars[4] = {'U','D','L','R'};\n        vector<int> dir_order = {0,1,2,3};\n        shuffle(dir_order.begin(), dir_order.end(), rng);\n\n        for (int d : dir_order) {\n            int ni = empty_i + dirs[d].first;\n            int nj = empty_j + dirs[d].second;\n            if (ni < 0 || ni >= N || nj < 0 || nj >= N) continue;\n\n            vector<vector<int>> temp_grid = grid;\n            string temp_moves = moves;\n            int temp_empty_i = empty_i, temp_empty_j = empty_j;\n\n            swap(temp_grid[empty_i][empty_j], temp_grid[ni][nj]);\n            temp_moves += dir_chars[d];\n            temp_empty_i = ni;\n            temp_empty_j = nj;\n\n            if (temp_grid[best_i][best_j] == best_val) {\n                grid = temp_grid;\n                moves = temp_moves;\n                empty_i = temp_empty_i;\n                empty_j = temp_empty_j;\n                improved = true;\n                break;\n            }\n        }\n    } while (improved);\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    auto start_time = chrono::high_resolution_clock::now();\n\n    int N, T;\n    cin >> N >> T;\n    vector<string> grid_str(N);\n    for (int i = 0; i < N; i++) {\n        cin >> grid_str[i];\n    }\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            char c = grid_str[i][j];\n            int val;\n            if (isdigit(c)) val = c - '0';\n            else val = 10 + (c - 'a');\n            grid[i][j] = val;\n        }\n    }\n\n    int empty_i = -1, empty_j = -1;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (grid[i][j] == 0) {\n                empty_i = i;\n                empty_j = j;\n            }\n        }\n    }\n\n    random_device rd;\n    mt19937 rng(rd());\n\n    vector<vector<vector<int>>> target_candidates;\n    for (int attempt = 0; attempt < 5; attempt++) {\n        int target_empty_i = rng() % N;\n        int target_empty_j = rng() % N;\n        target_candidates.push_back(generate_spanning_tree(N, target_empty_i, target_empty_j, rng));\n    }\n\n    for (int attempt = 0; attempt < 3; attempt++) {\n        int target_empty_i = rng() % N;\n        int target_empty_j = rng() % N;\n        target_candidates.push_back(generate_snake_pattern(N, target_empty_i, target_empty_j, rng));\n    }\n\n    int center_i = N/2, center_j = N/2;\n    target_candidates.push_back(generate_spanning_tree(N, center_i, center_j, rng));\n    target_candidates.push_back(generate_snake_pattern(N, center_i, center_j, rng));\n\n    vector<vector<int>> target_grid = target_candidates[0];\n    int min_mismatch = INT_MAX;\n    for (auto& candidate : target_candidates) {\n        vector<int> freq(16, 0);\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                if (candidate[i][j] < 16) {\n                    freq[candidate[i][j]]++;\n                }\n            }\n        }\n\n        vector<int> have(16, 0);\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                int val = grid[i][j];\n                if (val < 16) {\n                    have[val]++;\n                }\n            }\n        }\n\n        int total_mismatch = 0;\n        for (int i = 0; i < 16; i++) {\n            total_mismatch += abs(have[i] - freq[i]);\n        }\n        if (total_mismatch < min_mismatch) {\n            min_mismatch = total_mismatch;\n            target_grid = candidate;\n        }\n    }\n\n    State current(grid, empty_i, empty_j);\n    string best_moves = \"\";\n    int best_cost = INT_MAX;\n\n    const int beam_width = 150;\n    const int max_steps = T;\n    vector<State> beam;\n    beam.push_back(current);\n\n    int step = 0;\n    while (step < max_steps) {\n        auto current_time = chrono::high_resolution_clock::now();\n        auto elapsed = chrono::duration_cast<chrono::milliseconds>(current_time - start_time).count();\n        if (elapsed > 2800) break;\n\n        vector<State> next_beam;\n\n        for (size_t b = 0; b < min(static_cast<size_t>(beam_width), beam.size()); b++) {\n            State state = beam[b];\n            vector<tuple<char, int, int, int, int>> moves;\n            state.generate_moves(moves, N, target_grid);\n\n            for (auto [move_char, ni, nj, d, benefit] : moves) {\n                State next = state;\n                swap(next.grid[state.empty_i][state.empty_j], next.grid[ni][nj]);\n                next.empty_i = ni;\n                next.empty_j = nj;\n                next.moves += move_char;\n\n                if (next.moves.size() > static_cast<size_t>(T)) continue;\n\n                if (next.is_goal(N, target_grid)) {\n                    cout << next.moves << endl;\n                    return 0;\n                }\n\n                int h = next.heuristic(N, target_grid);\n                if (static_cast<int>(next.moves.size()) + h < best_cost) {\n                    best_moves = next.moves;\n                    best_cost = next.moves.size() + h;\n                }\n\n                next_beam.push_back(next);\n                if (next_beam.size() > static_cast<size_t>(beam_width * 3)) break;\n            }\n            if (next_beam.size() > static_cast<size_t>(beam_width * 3)) break;\n        }\n\n        if (next_beam.empty()) break;\n\n        sort(next_beam.begin(), next_beam.end(), [&](const State& a, const State& b) {\n            int a_cost = static_cast<int>(a.moves.size()) + a.heuristic(N, target_grid);\n            int b_cost = static_cast<int>(b.moves.size()) + b.heuristic(N, target_grid);\n            return a_cost < b_cost;\n        });\n        beam.clear();\n        for (size_t i = 0; i < min(static_cast<size_t>(beam_width), next_beam.size()); i++) {\n            beam.push_back(next_beam[i]);\n        }\n\n        if (beam.size() < static_cast<size_t>(beam_width/3) && step % 3 == 0) {\n            shuffle(next_beam.begin(), next_beam.end(), rng);\n            for (size_t i = 0; i < min(static_cast<size_t>(beam_width/3), next_beam.size()); i++) {\n                beam.push_back(next_beam[i]);\n            }\n        }\n\n        step++;\n    }\n\n    State final_state(grid, empty_i, empty_j, best_moves);\n    local_search_improvement(final_state.grid, final_state.empty_i, final_state.empty_j,\n                           target_grid, final_state.moves, N, rng);\n\n    if (final_state.is_goal(N, target_grid)) {\n        cout << final_state.moves << endl;\n    } else {\n        cout << best_moves << endl;\n    }\n\n    return 0;\n}","ahc012":"#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\n\nstruct Point {\n    ll x, y;\n    Point(ll x=0, ll y=0) : x(x), y(y) {}\n    bool operator==(const Point& other) const {\n        return x == other.x && y == other.y;\n    }\n    bool operator<(const Point& other) const {\n        return x < other.x || (x == other.x && y < other.y);\n    }\n};\n\nstruct Line {\n    Point p1, p2;\n    Line(Point p1=Point(), Point p2=Point()) : p1(p1), p2(p2) {}\n};\n\nll cross(const Point& o, const Point& a, const Point& b) {\n    return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x);\n}\n\nvector<Point> convex_hull(vector<Point> pts) {\n    int n = pts.size();\n    if (n <= 1) return pts;\n    sort(pts.begin(), pts.end());\n    vector<Point> hull;\n    hull.reserve(n+1);\n    for (int i = 0; i < n; ++i) {\n        while (hull.size() >= 2 && cross(hull[hull.size()-2], hull.back(), pts[i]) <= 0) {\n            hull.pop_back();\n        }\n        hull.push_back(pts[i]);\n    }\n    int lower_len = hull.size();\n    for (int i = n-2; i >= 0; --i) {\n        while (hull.size() > lower_len && cross(hull[hull.size()-2], hull.back(), pts[i]) <= 0) {\n            hull.pop_back();\n        }\n        hull.push_back(pts[i]);\n    }\n    hull.pop_back();\n    return hull;\n}\n\nll side(const Point& p1, const Point& p2, const Point& p) {\n    return (p2.x - p1.x) * (p.y - p1.y) - (p2.y - p1.y) * (p.x - p1.x);\n}\n\nbool line_passes_through(const Line& line, const vector<Point>& straws) {\n    for (const Point& s : straws) {\n        if (side(line.p1, line.p2, s) == 0) {\n            return true;\n        }\n    }\n    return false;\n}\n\nint count_in_half(const vector<int>& indices, const vector<Point>& straws, const Line& line) {\n    int cnt = 0;\n    for (int idx : indices) {\n        if (side(line.p1, line.p2, straws[idx]) > 0) {\n            cnt++;\n        }\n    }\n    return cnt;\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\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> straws(N);\n    for (int i = 0; i < N; i++) {\n        cin >> straws[i].x >> straws[i].y;\n    }\n\n    struct Region {\n        vector<int> indices;\n        int count() const { return indices.size(); }\n    };\n    vector<Region> regions;\n    regions.push_back(Region());\n    for (int i = 0; i < N; i++) {\n        regions[0].indices.push_back(i);\n    }\n\n    vector<int> b(11, 0);\n    if (N <= 10) {\n        b[N] = 1;\n    }\n\n    vector<Line> cuts;\n\n    for (int cut_num = 0; cut_num < K; cut_num++) {\n        int best_delta = -1;\n        Line best_line;\n        int best_region_idx = -1;\n        int best_count1 = 0, best_count2 = 0;\n        vector<int> best_left_indices, best_right_indices;\n\n        for (int reg_idx = 0; reg_idx < regions.size(); reg_idx++) {\n            Region& reg = regions[reg_idx];\n            int m = reg.count();\n            if (m <= 1) continue;\n\n            // Generate candidate lines from convex hull edges\n            vector<Point> hull_pts;\n            for (int idx : reg.indices) {\n                hull_pts.push_back(straws[idx]);\n            }\n            vector<Point> hull = convex_hull(hull_pts);\n            if (hull.size() >= 2) {\n                for (int i = 0; i < (int)hull.size(); i++) {\n                    Point p1 = hull[i];\n                    Point p2 = hull[(i+1) % hull.size()];\n                    Line line(p1, p2);\n                    if (line_passes_through(line, hull_pts)) {\n                        continue;\n                    }\n\n                    int count1 = count_in_half(reg.indices, straws, line);\n                    int count2 = m - count1;\n\n                    vector<int> new_b = b;\n                    if (m <= 10) new_b[m]--;\n                    if (count1 <= 10) new_b[count1]++;\n                    if (count2 <= 10) new_b[count2]++;\n\n                    int old_score = 0, new_score = 0;\n                    for (int d = 1; d <= 10; d++) {\n                        old_score += min(a[d], b[d]);\n                        new_score += min(a[d], new_b[d]);\n                    }\n                    int delta = new_score - old_score;\n\n                    if (delta > best_delta) {\n                        best_delta = delta;\n                        best_line = line;\n                        best_region_idx = reg_idx;\n                        best_count1 = count1;\n                        best_count2 = count2;\n                        best_left_indices.clear();\n                        best_right_indices.clear();\n                        for (int idx : reg.indices) {\n                            if (side(line.p1, line.p2, straws[idx]) > 0) {\n                                best_left_indices.push_back(idx);\n                            } else {\n                                best_right_indices.push_back(idx);\n                            }\n                        }\n                    }\n                }\n            }\n\n            // Generate random lines\n            int num_random = min(50, m * m);\n            for (int iter = 0; iter < num_random; iter++) {\n                // Generate two random points in the bounding box of the region\n                ll min_x = 1e18, max_x = -1e18;\n                ll min_y = 1e18, max_y = -1e18;\n                for (int idx : reg.indices) {\n                    min_x = min(min_x, straws[idx].x);\n                    max_x = max(max_x, straws[idx].x);\n                    min_y = min(min_y, straws[idx].y);\n                    max_y = max(max_y, straws[idx].y);\n                }\n                // Expand the bounding box a bit\n                ll exp_x = max(1LL, (max_x - min_x) / 4);\n                ll exp_y = max(1LL, (max_y - min_y) / 4);\n                min_x -= exp_x; max_x += exp_x;\n                min_y -= exp_y; max_y += exp_y;\n\n                Point p1;\n                p1.x = min_x + (rand() % (max_x - min_x + 1));\n                p1.y = min_y + (rand() % (max_y - min_y + 1));\n                Point p2;\n                p2.x = min_x + (rand() % (max_x - min_x + 1));\n                p2.y = min_y + (rand() % (max_y - min_y + 1));\n\n                Line line(p1, p2);\n                if (line_passes_through(line, straws)) {\n                    continue;\n                }\n\n                int count1 = count_in_half(reg.indices, straws, line);\n                int count2 = m - count1;\n\n                vector<int> new_b = b;\n                if (m <= 10) new_b[m]--;\n                if (count1 <= 10) new_b[count1]++;\n                if (count2 <= 10) new_b[count2]++;\n\n                int old_score = 0, new_score = 0;\n                for (int d = 1; d <= 10; d++) {\n                    old_score += min(a[d], b[d]);\n                    new_score += min(a[d], new_b[d]);\n                }\n                int delta = new_score - old_score;\n\n                if (delta > best_delta) {\n                    best_delta = delta;\n                    best_line = line;\n                    best_region_idx = reg_idx;\n                    best_count1 = count1;\n                    best_count2 = count2;\n                    best_left_indices.clear();\n                    best_right_indices.clear();\n                    for (int idx : reg.indices) {\n                        if (side(line.p1, line.p2, straws[idx]) > 0) {\n                            best_left_indices.push_back(idx);\n                        } else {\n                            best_right_indices.push_back(idx);\n                        }\n                    }\n                }\n            }\n        }\n\n        if (best_delta <= 0) {\n            break;\n        }\n\n        // Update b counts\n        if (regions[best_region_idx].count() <= 10) {\n            b[regions[best_region_idx].count()]--;\n        }\n        if (best_count1 <= 10) {\n            b[best_count1]++;\n        }\n        if (best_count2 <= 10) {\n            b[best_count2]++;\n        }\n\n        Region left_reg, right_reg;\n        left_reg.indices = best_left_indices;\n        right_reg.indices = best_right_indices;\n\n        vector<Region> new_regions;\n        for (int i = 0; i < regions.size(); i++) {\n            if (i != best_region_idx) {\n                new_regions.push_back(regions[i]);\n            }\n        }\n        if (!left_reg.indices.empty()) {\n            new_regions.push_back(left_reg);\n        }\n        if (!right_reg.indices.empty()) {\n            new_regions.push_back(right_reg);\n        }\n        regions = new_regions;\n\n        cuts.push_back(best_line);\n    }\n\n    cout << cuts.size() << '\\n';\n    for (const Line& line : cuts) {\n        cout << line.p1.x << ' ' << line.p1.y << ' '\n             << line.p2.x << ' ' << line.p2.y << '\\n';\n    }\n\n    return 0;\n}","ahc014":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Operation {\n    int x1, y1, x2, y2, x3, y3, x4, y4;\n};\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M;\n    cin >> N >> M;\n    int c = (N - 1) / 2;\n\n    vector<vector<bool>> grid(N, vector<bool>(N, false));\n    vector<pair<int, int>> dots;\n    for (int i = 0; i < M; i++) {\n        int x, y;\n        cin >> x >> y;\n        grid[x][y] = true;\n        dots.emplace_back(x, y);\n    }\n\n    vector<tuple<long long, int, int>> candidate_list;\n    for (int x = 0; x < N; x++) {\n        for (int y = 0; y < N; y++) {\n            long long dx = x - c;\n            long long dy = y - c;\n            long long weight = dx * dx + dy * dy + 1;\n            candidate_list.emplace_back(-weight, x, y);\n        }\n    }\n    sort(candidate_list.begin(), candidate_list.end());\n\n    vector<vector<bool>> hor_used(N-1, vector<bool>(N, false));\n    vector<vector<bool>> ver_used(N, vector<bool>(N-1, false));\n    vector<vector<bool>> diag1_used(N-1, vector<bool>(N-1, false));\n    vector<vector<bool>> diag2_used(N-1, vector<bool>(N-1, false));\n\n    vector<Operation> operations;\n\n    bool added_something;\n    do {\n        added_something = false;\n        for (auto [neg_weight, x, y] : candidate_list) {\n            if (grid[x][y]) continue;\n\n            bool found = false;\n\n            auto check_axis = [&](int x1, int y1, int x2, int y2) -> bool {\n                if (x1 == x2 || y1 == y2) return false;\n                int x_min = min(x1, x2);\n                int x_max = max(x1, x2);\n                int y_min = min(y1, y2);\n                int y_max = max(y1, y2);\n\n                bool cond2 = true;\n                for (int yy = y_min + 1; yy < y_max; yy++) {\n                    if (grid[x_min][yy] || grid[x_max][yy]) {\n                        cond2 = false;\n                        break;\n                    }\n                }\n                if (!cond2) return false;\n                for (int xx = x_min + 1; xx < x_max; xx++) {\n                    if (grid[xx][y_min] || grid[xx][y_max]) {\n                        cond2 = false;\n                        break;\n                    }\n                }\n                if (!cond2) return false;\n\n                bool cond3 = true;\n                for (int yy = y_min; yy < y_max; yy++) {\n                    if (ver_used[x_min][yy] || ver_used[x_max][yy]) {\n                        cond3 = false;\n                        break;\n                    }\n                }\n                if (!cond3) return false;\n                for (int xx = x_min; xx < x_max; xx++) {\n                    if (hor_used[xx][y_min] || hor_used[xx][y_max]) {\n                        cond3 = false;\n                        break;\n                    }\n                }\n                return cond3;\n            };\n\n            auto mark_axis = [&](int x1, int y1, int x2, int y2) {\n                int x_min = min(x1, x2);\n                int x_max = max(x1, x2);\n                int y_min = min(y1, y2);\n                int y_max = max(y1, y2);\n                for (int yy = y_min; yy < y_max; yy++) {\n                    ver_used[x_min][yy] = true;\n                    ver_used[x_max][yy] = true;\n                }\n                for (int xx = x_min; xx < x_max; xx++) {\n                    hor_used[xx][y_min] = true;\n                    hor_used[xx][y_max] = true;\n                }\n            };\n\n            auto check_diag = [&](int x, int y) -> bool {\n                vector<pair<int, int>> d1_points;\n                vector<pair<int, int>> d2_points;\n                for (int dx = -N; dx <= N; dx++) {\n                    int nx = x + dx;\n                    int ny = y + dx;\n                    if (nx >= 0 && nx < N && ny >= 0 && ny < N && grid[nx][ny]) {\n                        d1_points.emplace_back(nx, ny);\n                    }\n                    nx = x + dx;\n                    ny = y - dx;\n                    if (nx >= 0 && nx < N && ny >= 0 && ny < N && grid[nx][ny]) {\n                        d2_points.emplace_back(nx, ny);\n                    }\n                }\n\n                for (auto& a : d1_points) {\n                    for (auto& b : d2_points) {\n                        int cx = a.first + b.first - x;\n                        int cy = a.second + b.second - y;\n                        if (cx < 0 || cx >= N || cy < 0 || cy >= N) continue;\n                        if (!grid[cx][cy]) continue;\n\n                        vector<pair<int, int>> corners = {{x,y}, a, {cx,cy}, b};\n                        bool cond2 = true;\n                        for (int i = 0; i < 4; i++) {\n                            int x1 = corners[i].first, y1 = corners[i].second;\n                            int x2 = corners[(i+1)%4].first, y2 = corners[(i+1)%4].second;\n                            int dx = x2 - x1, dy = y2 - y1;\n                            int steps = max(abs(dx), abs(dy));\n                            for (int step = 1; step < steps; step++) {\n                                int px = x1 + (dx * step) / steps;\n                                int py = y1 + (dy * step) / steps;\n                                bool is_corner = false;\n                                for (auto& corner : corners) {\n                                    if (px == corner.first && py == corner.second) {\n                                        is_corner = true;\n                                        break;\n                                    }\n                                }\n                                if (!is_corner && grid[px][py]) {\n                                    cond2 = false;\n                                    break;\n                                }\n                            }\n                            if (!cond2) break;\n                        }\n                        if (!cond2) continue;\n\n                        bool cond3 = true;\n                        for (int i = 0; i < 4; i++) {\n                            int x1 = corners[i].first, y1 = corners[i].second;\n                            int x2 = corners[(i+1)%4].first, y2 = corners[(i+1)%4].second;\n                            int dx = x2 - x1, dy = y2 - y1;\n                            int steps = max(abs(dx), abs(dy));\n                            for (int step = 1; step <= steps; step++) {\n                                int px = x1 + (dx * step) / steps;\n                                int py = y1 + (dy * step) / steps;\n                                int ex, ey;\n                                if (dx == 0) {\n                                    ex = px;\n                                    ey = min(y1, y2) + (step <= abs(dy) ? step-1 : abs(dy)-1);\n                                } else if (dy == 0) {\n                                    ex = min(x1, x2) + (step <= abs(dx) ? step-1 : abs(dx)-1);\n                                    ey = py;\n                                } else if (abs(dx) == abs(dy)) {\n                                    if (dx * dy > 0) {\n                                        if (dx > 0) {\n                                            ex = px;\n                                            ey = py;\n                                        } else {\n                                            ex = px + 1;\n                                            ey = py + 1;\n                                        }\n                                    } else {\n                                        if (dx > 0) {\n                                            ex = px;\n                                            ey = py + 1;\n                                        } else {\n                                            ex = px + 1;\n                                            ey = py;\n                                        }\n                                    }\n                                } else {\n                                    continue;\n                                }\n                                if (ex >= N-1 || ey >= N) continue;\n                                if (dx == 0 || dy == 0) {\n                                    if (dx == 0) {\n                                        if (ver_used[px][min(y1,y2) + step - 1]) {\n                                            cond3 = false;\n                                            break;\n                                        }\n                                    } else {\n                                        if (hor_used[min(x1,x2) + step - 1][py]) {\n                                            cond3 = false;\n                                            break;\n                                        }\n                                    }\n                                } else {\n                                    int type = (dx * dy > 0) ? 1 : 2;\n                                    int sx = px, sy = py;\n                                    if (type == 1) {\n                                        if (diag1_used[sx][sy]) {\n                                            cond3 = false;\n                                            break;\n                                        }\n                                    } else {\n                                        if (sx >= 0 && sy >= 0 && sx < N-1 && sy < N-1 && diag2_used[sx][sy]) {\n                                            cond3 = false;\n                                            break;\n                                        }\n                                    }\n                                }\n                            }\n                            if (!cond3) break;\n                        }\n                        if (cond2 && cond3) {\n                            grid[x][y] = true;\n                            dots.emplace_back(x, y);\n                            operations.push_back({x, y, a.first, a.second, cx, cy, b.first, b.second});\n                            for (int i = 0; i < 4; i++) {\n                                int x1 = corners[i].first, y1 = corners[i].second;\n                                int x2 = corners[(i+1)%4].first, y2 = corners[(i+1)%4].second;\n                                int dx = x2 - x1, dy = y2 - y1;\n                                int steps = max(abs(dx), abs(dy));\n                                for (int step = 1; step <= steps; step++) {\n                                    int px = x1 + (dx * step) / steps;\n                                    int py = y1 + (dy * step) / steps;\n                                    if (dx == 0) {\n                                        ver_used[x1][min(y1,y2) + step - 1] = true;\n                                    } else if (dy == 0) {\n                                        hor_used[min(x1,x2) + step - 1][y1] = true;\n                                    } else if (abs(dx) == abs(dy)) {\n                                        if (dx * dy > 0) {\n                                            if (dx > 0) diag1_used[px][py] = true;\n                                            else diag1_used[px][py] = true;\n                                        } else {\n                                            if (dx > 0) diag2_used[px][py] = true;\n                                            else diag2_used[px][py] = true;\n                                        }\n                                    }\n                                }\n                            }\n                            return true;\n                        }\n                    }\n                }\n                return false;\n            };\n\n            if (x > 0 && x < N-1 && y > 0 && y < N-1) {\n                if (check_diag(x, y)) {\n                    found = true;\n                }\n            }\n\n            if (!found) {\n                vector<int> up_dots, down_dots, left_dots, right_dots;\n                for (int yy = y+1; yy < N; yy++) if (grid[x][yy]) up_dots.push_back(yy);\n                for (int yy = 0; yy < y; yy++) if (grid[x][yy]) down_dots.push_back(yy);\n                for (int xx = 0; xx < x; xx++) if (grid[xx][y]) left_dots.push_back(xx);\n                for (int xx = x+1; xx < N; xx++) if (grid[xx][y]) right_dots.push_back(xx);\n\n                if (!up_dots.empty() && !right_dots.empty()) {\n                    for (int yA : up_dots) {\n                        for (int xB : right_dots) {\n                            if (check_axis(x, y, xB, yA)) {\n                                grid[x][y] = true;\n                                dots.emplace_back(x, y);\n                                operations.push_back({x, y, x, yA, xB, yA, xB, y});\n                                mark_axis(x, y, xB, yA);\n                                found = true;\n                                break;\n                            }\n                        }\n                        if (found) break;\n                    }\n                }\n                if (!found && !up_dots.empty() && !left_dots.empty()) {\n                    for (int yA : up_dots) {\n                        for (int xB : left_dots) {\n                            if (check_axis(x, y, xB, yA)) {\n                                grid[x][y] = true;\n                                dots.emplace_back(x, y);\n                                operations.push_back({x, y, x, yA, xB, yA, xB, y});\n                                mark_axis(x, y, xB, yA);\n                                found = true;\n                                break;\n                            }\n                        }\n                        if (found) break;\n                    }\n                }\n                if (!found && !down_dots.empty() && !right_dots.empty()) {\n                    for (int yA : down_dots) {\n                        for (int xB : right_dots) {\n                            if (check_axis(x, y, xB, yA)) {\n                                grid[x][y] = true;\n                                dots.emplace_back(x, y);\n                                operations.push_back({x, y, x, yA, xB, yA, xB, y});\n                                mark_axis(x, y, xB, yA);\n                                found = true;\n                                break;\n                            }\n                        }\n                        if (found) break;\n                    }\n                }\n                if (!found && !down_dots.empty() && !left_dots.empty()) {\n                    for (int yA : down_dots) {\n                        for (int xB : left_dots) {\n                            if (check_axis(x, y, xB, yA)) {\n                                grid[x][y] = true;\n                                dots.emplace_back(x, y);\n                                operations.push_back({x, y, x, yA, xB, yA, xB, y});\n                                mark_axis(x, y, xB, yA);\n                                found = true;\n                                break;\n                            }\n                        }\n                        if (found) break;\n                    }\n                }\n            }\n\n            if (found) {\n                added_something = true;\n                break;\n            }\n        }\n    } while (added_something);\n\n    cout << operations.size() << '\\n';\n    for (auto& op : operations) {\n        cout << op.x1 << ' ' << op.y1 << ' '\n             << op.x2 << ' ' << op.y2 << ' '\n             << op.x3 << ' ' << op.y3 << ' '\n             << op.x4 << ' ' << op.y4 << '\\n';\n    }\n\n    return 0;\n}","ahc015":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\nusing namespace std;\n\nint compute_sum_squares(int grid[10][10]) {\n    bool visited[10][10] = {{false}};\n    int total = 0;\n\n    for (int i = 0; i < 10; i++) {\n        for (int j = 0; j < 10; j++) {\n            if (grid[i][j] != 0 && !visited[i][j]) {\n                int size = 0;\n                queue<pair<int, int>> q;\n                q.push({i, j});\n                visited[i][j] = true;\n                int flavor = grid[i][j];\n\n                while (!q.empty()) {\n                    auto [x, y] = q.front();\n                    q.pop();\n                    size++;\n                    const int dx[4] = {-1, 1, 0, 0};\n                    const int dy[4] = {0, 0, -1, 1};\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 < 10 && ny >= 0 && ny < 10 && !visited[nx][ny] && grid[nx][ny] == flavor) {\n                            visited[nx][ny] = true;\n                            q.push({nx, ny});\n                        }\n                    }\n                }\n                total += size * size;\n            }\n        }\n    }\n    return total;\n}\n\nint get_flavor_cluster_size(int grid[10][10], int flavor) {\n    bool visited[10][10] = {{false}};\n    int max_size = 0;\n\n    for (int i = 0; i < 10; i++) {\n        for (int j = 0; j < 10; j++) {\n            if (grid[i][j] == flavor && !visited[i][j]) {\n                int size = 0;\n                queue<pair<int, int>> q;\n                q.push({i, j});\n                visited[i][j] = true;\n\n                while (!q.empty()) {\n                    auto [x, y] = q.front();\n                    q.pop();\n                    size++;\n                    const int dx[4] = {-1, 1, 0, 0};\n                    const int dy[4] = {0, 0, -1, 1};\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 < 10 && ny >= 0 && ny < 10 && !visited[nx][ny] && grid[nx][ny] == flavor) {\n                            visited[nx][ny] = true;\n                            q.push({nx, ny});\n                        }\n                    }\n                }\n                max_size = max(max_size, size);\n            }\n        }\n    }\n    return max_size;\n}\n\nvoid tilt(int grid[10][10], char dir) {\n    int new_grid[10][10] = {{0}};\n\n    if (dir == 'F') { // Move candies toward front (row 0)\n        for (int j = 0; j < 10; j++) {\n            int write_pos = 0;\n            for (int i = 0; i < 10; i++) {\n                if (grid[i][j] != 0) {\n                    new_grid[write_pos][j] = grid[i][j];\n                    write_pos++;\n                }\n            }\n        }\n    } else if (dir == 'B') { // Move candies toward back (row 9)\n        for (int j = 0; j < 10; j++) {\n            int write_pos = 9;\n            for (int i = 9; i >= 0; i--) {\n                if (grid[i][j] != 0) {\n                    new_grid[write_pos][j] = grid[i][j];\n                    write_pos--;\n                }\n            }\n        }\n    } else if (dir == 'L') { // Move candies toward left (column 0)\n        for (int i = 0; i < 10; i++) {\n            int write_pos = 0;\n            for (int j = 0; j < 10; j++) {\n                if (grid[i][j] != 0) {\n                    new_grid[i][write_pos] = grid[i][j];\n                    write_pos++;\n                }\n            }\n        }\n    } else if (dir == 'R') { // Move candies toward right (column 9)\n        for (int i = 0; i < 10; i++) {\n            int write_pos = 9;\n            for (int j = 9; j >= 0; j--) {\n                if (grid[i][j] != 0) {\n                    new_grid[i][write_pos] = grid[i][j];\n                    write_pos--;\n                }\n            }\n        }\n    }\n\n    for (int i = 0; i < 10; i++) {\n        for (int j = 0; j < 10; j++) {\n            grid[i][j] = new_grid[i][j];\n        }\n    }\n}\n\nint main() {\n    vector<int> flavors(100);\n    for (int i = 0; i < 100; i++) {\n        cin >> flavors[i];\n    }\n\n    int grid[10][10] = {{0}};\n    vector<char> directions = {'F', 'B', 'L', 'R'};\n\n    for (int t = 0; t < 100; t++) {\n        int p;\n        cin >> p;\n\n        if (t == 99) {\n            break;\n        }\n\n        int r = (p - 1) / 10;\n        int c = (p - 1) % 10;\n        int new_flavor = flavors[t];\n        grid[r][c] = new_flavor;\n\n        char best_dir = 'F';\n        int best_score = -1;\n        int best_cluster_score = -1;\n\n        for (char dir : directions) {\n            int temp_grid[10][10];\n            for (int i = 0; i < 10; i++) {\n                for (int j = 0; j < 10; j++) {\n                    temp_grid[i][j] = grid[i][j];\n                }\n            }\n            tilt(temp_grid, dir);\n            int score = compute_sum_squares(temp_grid);\n            int cluster_score = get_flavor_cluster_size(temp_grid, new_flavor);\n\n            if (score > best_score || (score == best_score && cluster_score > best_cluster_score)) {\n                best_score = score;\n                best_cluster_score = cluster_score;\n                best_dir = dir;\n            }\n        }\n\n        cout << best_dir << '\\n';\n        cout.flush();\n        tilt(grid, best_dir);\n    }\n\n    return 0;\n}","ahc016":"#include <bits/stdc++.h>\nusing namespace std;\n\n// Function to compute the index of edge (i,j) in the string, with i<j\nlong long get_index(int i, int j, int N) {\n    return 1LL * i * (2 * N - i - 1) / 2 + (j - i - 1);\n}\n\nint main() {\n    int M;\n    double eps;\n    cin >> M >> eps;\n\n    const int N = 100;\n    const int T = N * (N - 1) / 2;\n\n    cout << N << endl;\n    cout.flush();\n\n    for (int s = 0; s < M; s++) {\n        int k;\n        if (s < 96) {\n            k = 4 + s;\n        } else {\n            k = 100 - (s - 96);\n        }\n        k = max(4, min(k, 100));\n\n        string graph(T, '0');\n        for (int i = 0; i < k; i++) {\n            for (int j = i + 1; j < k; j++) {\n                long long idx = get_index(i, j, N);\n                graph[idx] = '1';\n            }\n        }\n        cout << graph << endl;\n    }\n    cout.flush();\n\n    for (int q = 0; q < 100; q++) {\n        string H;\n        cin >> H;\n\n        vector<int> deg(N, 0);\n        for (int i = 0; i < N; i++) {\n            for (int j = i + 1; j < N; j++) {\n                long long idx = get_index(i, j, N);\n                if (H[idx] == '1') {\n                    deg[i]++;\n                    deg[j]++;\n                }\n            }\n        }\n\n        vector<int> sorted_deg = deg;\n        sort(sorted_deg.begin(), sorted_deg.end());\n\n        double best_score = 1e300;\n        int best_s = 0;\n\n        for (int s = 0; s < M; s++) {\n            int k;\n            if (s < 96) {\n                k = 4 + s;\n            } else {\n                k = 100 - (s - 96);\n            }\n            k = max(4, min(k, 100));\n\n            int low_count = N - k;\n            int high_count = k;\n\n            double expected_low = 99.0 * eps;\n            double expected_high = (k - 1) + (101 - 2 * k) * eps;\n\n            double score = 0.0;\n            if (low_count > 0) {\n                double sum_low = 0.0;\n                for (int i = 0; i < low_count; i++) {\n                    sum_low += sorted_deg[i];\n                }\n                double mean_low = sum_low / low_count;\n                score += (mean_low - expected_low) * (mean_low - expected_low);\n            }\n            if (high_count > 0) {\n                double sum_high = 0.0;\n                for (int i = low_count; i < N; i++) {\n                    sum_high += sorted_deg[i];\n                }\n                double mean_high = sum_high / high_count;\n                score += (mean_high - expected_high) * (mean_high - expected_high);\n            }\n\n            if (score < best_score) {\n                best_score = score;\n                best_s = s;\n            }\n        }\n\n        cout << best_s << endl;\n        cout.flush();\n    }\n\n    return 0;\n}","ahc017":"#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\nconst ll INF = 1e18;\n\nstruct Edge {\n    int u, v, idx;\n    ll w;\n};\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, D, K;\n    cin >> N >> M >> D >> K;\n\n    vector<Edge> edges(M);\n    vector<vector<tuple<int, ll, int>>> adj(N);\n    vector<int> deg(N, 0);\n\n    for (int i = 0; i < M; i++) {\n        int u, v;\n        ll w;\n        cin >> u >> v >> w;\n        u--; v--;\n        edges[i] = {u, v, i, w};\n        adj[u].emplace_back(v, w, i);\n        adj[v].emplace_back(u, w, i);\n        deg[u]++;\n        deg[v]++;\n    }\n\n    for (int i = 0; i < N; i++) {\n        int x, y;\n        cin >> x >> y;\n    }\n\n    vector<double> edge_betw(M, 0.0);\n\n    for (int s = 0; s < N; s++) {\n        vector<ll> dist(N, INF);\n        vector<ll> sigma(N, 0);\n        vector<vector<pair<int, int>>> pred(N);\n        vector<double> delta(N, 0.0);\n\n        priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<>> pq;\n        dist[s] = 0;\n        sigma[s] = 1;\n        pq.push({0, s});\n\n        while (!pq.empty()) {\n            auto [d, u] = pq.top();\n            pq.pop();\n            if (d != dist[u]) continue;\n\n            for (auto [v, w, idx] : adj[u]) {\n                ll nd = d + w;\n                if (nd < dist[v]) {\n                    dist[v] = nd;\n                    sigma[v] = sigma[u];\n                    pred[v].emplace_back(u, idx);\n                    pq.push({nd, v});\n                } else if (nd == dist[v]) {\n                    sigma[v] += sigma[u];\n                    pred[v].emplace_back(u, idx);\n                }\n            }\n        }\n\n        vector<int> order;\n        for (int i = 0; i < N; i++) {\n            if (i != s && dist[i] != INF) {\n                order.push_back(i);\n            }\n        }\n        sort(order.begin(), order.end(), [&](int a, int b) {\n            return dist[a] > dist[b];\n        });\n\n        for (int w : order) {\n            for (auto [v, idx] : pred[w]) {\n                double c = static_cast<double>(sigma[v]) / sigma[w] * (1.0 + delta[w]);\n                edge_betw[idx] += c;\n                delta[v] += c;\n            }\n        }\n    }\n\n    vector<double> score(M);\n    for (int i = 0; i < M; i++) {\n        double weight_factor = 1.0 / sqrt(edges[i].w);\n        score[i] = edge_betw[i] * weight_factor * min(deg[edges[i].u], deg[edges[i].v]);\n    }\n\n    vector<pair<double, int>> vec;\n    for (int i = 0; i < M; i++) {\n        vec.emplace_back(score[i], i);\n    }\n    sort(vec.begin(), vec.end(), [](const auto& a, const auto& b) {\n        return a.first > b.first;\n    });\n\n    vector<int> day_count(D, 0);\n    vector<ll> day_load(D, 0);\n    vector<int> ans(M);\n\n    for (int i = 0; i < M; i++) {\n        int best_day = 0;\n        for (int d = 1; d < D; d++) {\n            if (day_count[d] < day_count[best_day] ||\n                (day_count[d] == day_count[best_day] && day_load[d] < day_load[best_day])) {\n                best_day = d;\n            }\n        }\n        ans[vec[i].second] = best_day + 1;\n        day_count[best_day]++;\n        day_load[best_day] += edges[vec[i].second].w;\n    }\n\n    for (int i = 0; i < M; i++) {\n        if (i) cout << \" \";\n        cout << ans[i];\n    }\n    cout << '\\n';\n\n    return 0;\n}","ahc019":"#include<iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\n#include <tuple>\n#include <climits>\n#include <cmath>\n#include <array>\n#include <set>\n#include <map>\nusing namespace std;\n\nconst vector<array<array<int, 3>, 3>> rotation_matrices = {\n    {{\n        {1, 0, 0},\n        {0, 1, 0},\n        {0, 0, 1}\n    }},\n    {{\n        {1, 0, 0},\n        {0, 0, -1},\n        {0, 1, 0}\n    }},\n    {{\n        {1, 0, 0},\n        {0, -1, 0},\n        {0, 0, -1}\n    }},\n    {{\n        {1, 0, 0},\n        {0, 0, 1},\n        {0, -1, 0}\n    }},\n    {{\n        {0, 0, 1},\n        {0, 1, 0},\n        {-1, 0, 0}\n    }},\n    {{\n        {0, 1, 0},\n        {-1, 0, 0},\n        {0, 0, 1}\n    }},\n    {{\n        {0, 0, -1},\n        {-1, 0, 0},\n        {0, 0, -1}\n    }},\n    {{\n        {0, -1, 0},\n        {0, 0, 1},\n        {-1, 0, 0}\n    }},\n    {{\n        {0, 0, -1},\n        {0, -1, 0},\n        {1, 0, 0}\n    }},\n    {{\n        {0, 1, 0},\n        {0, 0, -1},\n        {1, 0, 0}\n    }},\n    {{\n        {0, 0, 1},\n        {1, 0, 0},\n        {0, -1, 0}\n    }},\n    {{\n        {0, -1, 0},\n        {0, 0, -1},\n        {-1, 0, 0}\n    }},\n    {{\n        {-1, 0, 0},\n        {0, 1, 0},\n        {0, 0, -1}\n    }},\n    {{\n        {-1, 0, 0},\n        {0, 0, 1},\n        {0, 1, 0}\n    }},\n    {{\n        {-1, 0, 0},\n        {0, -1, 0},\n        {0, 0, 1}\n    }},\n    {{\n        {-1, 0, 0},\n        {0, 0, -1},\n        {0, -1, 0}\n    }},\n    {{\n        {0, -1, 0},\n        {1, 0, 0},\n        {0, 0, 1}\n    }},\n    {{\n        {0, 0, -1},\n        {1, 0, 0},\n        {0, 1, 0}\n    }},\n    {{\n        {0, 1, 0},\n        {1, 0, 0},\n        {0, 0, -1}\n    }},\n    {{\n        {0, 0, 1},\n        {1, 0, 0},\n        {0, 1, 0}\n    }},\n    {{\n        {0, -1, 0},\n        {0, 0, -1},\n        {1, 0, 0}\n    }},\n    {{\n        {0, 0, -1},\n        {-1, 0, 0},\n        {0, 1, 0}\n    }},\n    {{\n        {0, 1, 0},\n        {0, 0, 1},\n        {-1, 0, 0}\n    }},\n    {{\n        {0, 0, 1},\n        {-1, 0, 0},\n        {0, -1, 0}\n    }}\n};\n\nvector<vector<tuple<int,int,int>>> get_components(vector<vector<vector<bool>>>& grid, int D) {\n    vector<vector<vector<bool>>> visited(D, vector<vector<bool>>(D, vector<bool>(D, false)));\n    vector<vector<tuple<int,int,int>>> components;\n    const int dx[6] = {1, -1, 0, 0, 0, 0};\n    const int dy[6] = {0, 0, 1, -1, 0, 0};\n    const int dz[6] = {0, 0, 0, 0, 1, -1};\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 (grid[x][y][z] && !visited[x][y][z]) {\n                    vector<tuple<int,int,int>> comp;\n                    queue<tuple<int,int,int>> q;\n                    q.push({x, y, z});\n                    visited[x][y][z] = true;\n                    while (!q.empty()) {\n                        auto [cx, cy, cz] = q.front(); q.pop();\n                        comp.push_back({cx, cy, cz});\n                        for (int i = 0; i < 6; i++) {\n                            int nx = cx + dx[i];\n                            int ny = cy + dy[i];\n                            int nz = cz + dz[i];\n                            if (nx >= 0 && nx < D && ny >= 0 && ny < D && nz >= 0 && nz < D && grid[nx][ny][nz] && !visited[nx][ny][nz]) {\n                                visited[nx][ny][nz] = true;\n                                q.push({nx, ny, nz});\n                            }\n                        }\n                    }\n                    components.push_back(comp);\n                }\n            }\n        }\n    }\n    return components;\n}\n\nbool find_placement(const vector<tuple<int,int,int>>& shape, const vector<vector<vector<bool>>>& rem, int D, int& tx, int& ty, int& tz) {\n    int w = 0, h = 0, d = 0;\n    for (auto [x,y,z] : shape) {\n        w = max(w, x+1);\n        h = max(h, y+1);\n        d = max(d, z+1);\n    }\n    for (tx = 0; tx <= D - w; tx++) {\n        for (ty = 0; ty <= D - h; ty++) {\n            for (tz = 0; tz <= D - d; tz++) {\n                bool valid = true;\n                for (auto [x,y,z] : shape) {\n                    int gx = tx + x;\n                    int gy = ty + y;\n                    int gz = tz + z;\n                    if (gx < 0 || gx >= D || gy < 0 || gy >= D || gz < 0 || gz >= D) {\n                        valid = false;\n                        break;\n                    }\n                    if (!rem[gx][gy][gz]) {\n                        valid = false;\n                        break;\n                    }\n                }\n                if (valid) {\n                    return true;\n                }\n            }\n        }\n    }\n    return false;\n}\n\nvoid place_block(vector<vector<vector<int>>>& grid, const vector<tuple<int,int,int>>& shape, int tx, int ty, int tz, int idx) {\n    for (auto [x,y,z] : shape) {\n        int gx = tx + x;\n        int gy = ty + y;\n        int gz = tz + z;\n        if (gx >= 0 && gx < (int)grid.size() && gy >=0 && gy < (int)grid[0].size() && gz >=0 && gz < (int)grid[0][0].size()) {\n            grid[gx][gy][gz] = idx;\n        }\n    }\n}\n\nvoid update_remaining(vector<vector<vector<bool>>>& rem, const vector<tuple<int,int,int>>& shape, int tx, int ty, int tz) {\n    for (auto [x,y,z] : shape) {\n        int gx = tx + x;\n        int gy = ty + y;\n        int gz = tz + z;\n        if (gx >= 0 && gx < (int)rem.size() && gy >=0 && gy < (int)rem[0].size() && gz >=0 && gz < (int)rem[0][0].size()) {\n            rem[gx][gy][gz] = false;\n        }\n    }\n}\n\nbool try_place_shared_block(vector<tuple<int,int,int>>& comp, \n                          vector<vector<vector<bool>>>& R1_rem,\n                          vector<vector<vector<bool>>>& R2_rem,\n                          int D,\n                          int& next_block_idx,\n                          vector<vector<vector<int>>>& b1_grid,\n                          vector<vector<vector<int>>>& b2_grid) {\n    vector<tuple<int,int,int>> orig_comp = comp;\n    for (const auto& rot_mat : rotation_matrices) {\n        vector<tuple<int,int,int>> rotated;\n        for (auto [x,y,z] : orig_comp) {\n            int nx = rot_mat[0][0]*x + rot_mat[0][1]*y + rot_mat[0][2]*z;\n            int ny = rot_mat[1][0]*x + rot_mat[1][1]*y + rot_mat[1][2]*z;\n            int nz = rot_mat[2][0]*x + rot_mat[2][1]*y + rot_mat[2][2]*z;\n            rotated.push_back({nx, ny, nz});\n        }\n\n        int min_x = INT_MAX, max_x = INT_MIN;\n        int min_y = INT_MAX, max_y = INT_MIN;\n        int min_z = INT_MAX, max_z = INT_MIN;\n        for (auto [x,y,z] : rotated) {\n            min_x = min(min_x, x);\n            max_x = max(max_x, x);\n            min_y = min(min_y, y);\n            max_y = max(max_y, y);\n            min_z = min(min_z, z);\n            max_z = max(max_z, z);\n        }\n        int w = max_x - min_x + 1;\n        int h = max_y - min_y + 1;\n        int d = max_z - min_z + 1;\n        if (w <= 0 || h <= 0 || d <= 0) {\n            continue;\n        }\n\n        vector<tuple<int,int,int>> normalized;\n        for (auto [x,y,z] : rotated) {\n            normalized.push_back({x - min_x, y - min_y, z - min_z});\n        }\n\n        int tx1, ty1, tz1;\n        if (!find_placement(normalized, R1_rem, D, tx1, ty1, tz1)) {\n            continue;\n        }\n        int tx2, ty2, tz2;\n        if (!find_placement(normalized, R2_rem, D, tx2, ty2, tz2)) {\n            continue;\n        }\n\n        int idx = next_block_idx++;\n        place_block(b1_grid, normalized, tx1, ty1, tz1, idx);\n        place_block(b2_grid, normalized, tx2, ty2, tz2, idx);\n        update_remaining(R1_rem, normalized, tx1, ty1, tz1);\n        update_remaining(R2_rem, normalized, tx2, ty2, tz2);\n        return true;\n    }\n    return false;\n}\n\nbool try_place_non_shared_block(vector<tuple<int,int,int>>& comp, \n                              vector<vector<vector<bool>>>& target_rem,\n                              int D,\n                              int& next_block_idx,\n                              vector<vector<vector<int>>>& target_grid) {\n    vector<tuple<int,int,int>> orig_comp = comp;\n    for (const auto& rot_mat : rotation_matrices) {\n        vector<tuple<int,int,int>> rotated;\n        for (auto [x,y,z] : orig_comp) {\n            int nx = rot_mat[0][0]*x + rot_mat[0][1]*y + rot_mat[0][2]*z;\n            int ny = rot_mat[1][0]*x + rot_mat[1][1]*y + rot_mat[1][2]*z;\n            int nz = rot_mat[2][0]*x + rot_mat[2][1]*y + rot_mat[2][2]*z;\n            rotated.push_back({nx, ny, nz});\n        }\n\n        int min_x = INT_MAX, max_x = INT_MIN;\n        int min_y = INT_MAX, max_y = INT_MIN;\n        int min_z = INT_MAX, max_z = INT_MIN;\n        for (auto [x,y,z] : rotated) {\n            min_x = min(min_x, x);\n            max_x = max(max_x, x);\n            min_y = min(min_y, y);\n            max_y = max(max_y, y);\n            min_z = min(min_z, z);\n            max_z = max(max_z, z);\n        }\n        int w = max_x - min_x + 1;\n        int h = max_y - min_y + 1;\n        int d = max_z - min_z + 1;\n        if (w <= 0 || h <= 0 || d <= 0) {\n            continue;\n        }\n\n        vector<tuple<int,int,int>> normalized;\n        for (auto [x,y,z] : rotated) {\n            normalized.push_back({x - min_x, y - min_y, z - min_z});\n        }\n\n        int tx, ty, tz;\n        if (!find_placement(normalized, target_rem, D, tx, ty, tz)) {\n            continue;\n        }\n\n        int idx = next_block_idx++;\n        place_block(target_grid, normalized, tx, ty, tz, idx);\n        update_remaining(target_rem, normalized, tx, ty, tz);\n        return true;\n    }\n    return false;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int D;\n    cin >> D;\n\n    vector<string> f1_lines(D), r1_lines(D), f2_lines(D), r2_lines(D);\n    for (int i = 0; i < D; i++) {\n        cin >> f1_lines[i];\n    }\n    for (int i = 0; i < D; i++) {\n        cin >> r1_lines[i];\n    }\n    for (int i = 0; i < D; i++) {\n        cin >> f2_lines[i];\n    }\n    for (int i = 0; i < D; i++) {\n        cin >> r2_lines[i];\n    }\n\n    vector<vector<vector<bool>>> R1_rem(D, vector<vector<bool>>(D, vector<bool>(D, false)));\n    vector<vector<vector<bool>>> R2_rem(D, vector<vector<bool>>(D, vector<bool>(D, false)));\n\n    for (int z = 0; z < D; z++) {\n        for (int x = 0; x < D; x++) {\n            for (int y = 0; y < D; y++) {\n                if (f1_lines[z][x] == '1' && r1_lines[z][y] == '1') {\n                    R1_rem[x][y][z] = true;\n                }\n                if (f2_lines[z][x] == '1' && r2_lines[z][y] == '1') {\n                    R2_rem[x][y][z] = true;\n                }\n            }\n        }\n    }\n\n    vector<vector<vector<int>>> b1_grid(D, vector<vector<int>>(D, vector<int>(D, 0)));\n    vector<vector<vector<int>>> b2_grid(D, vector<vector<int>>(D, vector<int>(D, 0)));\n\n    int next_block_idx = 1;\n\n    vector<vector<vector<bool>>> R_grid(D, vector<vector<bool>>(D, vector<bool>(D, false)));\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                R_grid[x][y][z] = R1_rem[x][y][z] && R2_rem[x][y][z];\n            }\n        }\n    }\n\n    auto shared_components = get_components(R_grid, D);\n    sort(shared_components.begin(), shared_components.end(), [](const auto& a, const auto& b) {\n        return a.size() > b.size();\n    });\n\n    for (auto& comp : shared_components) {\n        if (comp.empty()) continue;\n        if (try_place_shared_block(comp, R1_rem, R2_rem, D, next_block_idx, b1_grid, b2_grid)) {\n        }\n    }\n\n    auto comps1 = get_components(R1_rem, D);\n    sort(comps1.begin(), comps1.end(), [](const auto& a, const auto& b) {\n        return a.size() > b.size();\n    });\n    for (auto& comp : comps1) {\n        if (comp.empty()) continue;\n        if (try_place_non_shared_block(comp, R1_rem, D, next_block_idx, b1_grid)) {\n        }\n    }\n\n    auto comps2 = get_components(R2_rem, D);\n    sort(comps2.begin(), comps2.end(), [](const auto& a, const auto& b) {\n        return a.size() > b.size();\n    });\n    for (auto& comp : comps2) {\n        if (comp.empty()) continue;\n        if (try_place_non_shared_block(comp, R2_rem, D, next_block_idx, b2_grid)) {\n        }\n    }\n\n    cout << next_block_idx - 1 << \"\\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                cout << b1_grid[x][y][z];\n                if (!(x == D-1 && y == D-1 && z == D-1)) {\n                    cout << \" \";\n                }\n            }\n        }\n    }\n    cout << \"\\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                cout << b2_grid[x][y][z];\n                if (!(x == D-1 && y == D-1 && z == D-1)) {\n                    cout << \" \";\n                }\n            }\n        }\n    }\n    cout << endl;\n\n    return 0;\n}","ahc020":"#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, K;\n    cin >> N >> M >> K;\n\n    vector<ll> stations_x(N), stations_y(N);\n    for (int i = 0; i < N; i++) {\n        cin >> stations_x[i] >> stations_y[i];\n    }\n\n    vector<tuple<int, int, ll>> edges;\n    for (int i = 0; i < M; i++) {\n        int u, v;\n        ll w;\n        cin >> u >> v >> w;\n        u--; v--;\n        edges.push_back({u, v, w});\n    }\n\n    vector<ll> residents_x(K), residents_y(K);\n    for (int i = 0; i < K; i++) {\n        cin >> residents_x[i] >> residents_y[i];\n    }\n\n    vector<vector<ll>> D_mat(N, vector<ll>(K, 0));\n    for (int i = 0; i < N; i++) {\n        for (int k = 0; k < K; k++) {\n            ll dx = stations_x[i] - residents_x[k];\n            ll dy = stations_y[i] - residents_y[k];\n            D_mat[i][k] = dx*dx + dy*dy;\n        }\n    }\n\n    vector<vector<ll>> d_graph(N, vector<ll>(N, LLONG_MAX/2));\n    vector<vector<int>> next_node(N, vector<int>(N, -1));\n    for (int i = 0; i < N; i++) {\n        d_graph[i][i] = 0;\n        next_node[i][i] = i;\n    }\n    for (auto &e : edges) {\n        int u = get<0>(e);\n        int v = get<1>(e);\n        ll w = get<2>(e);\n        if (w < d_graph[u][v]) {\n            d_graph[u][v] = w;\n            d_graph[v][u] = w;\n            next_node[u][v] = v;\n            next_node[v][u] = u;\n        }\n    }\n\n    for (int k = 0; k < N; k++) {\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                if (d_graph[i][k] + d_graph[k][j] < d_graph[i][j]) {\n                    d_graph[i][j] = d_graph[i][k] + d_graph[k][j];\n                    next_node[i][j] = next_node[i][k];\n                }\n            }\n        }\n    }\n\n    auto compute_mst_cost = [&](const vector<int>& T) -> ll {\n        int n = T.size();\n        if (n <= 1) return 0;\n        vector<ll> min_edge(n, LLONG_MAX);\n        vector<bool> used(n, false);\n        min_edge[0] = 0;\n        ll total = 0;\n        for (int iter = 0; iter < n; iter++) {\n            int u = -1;\n            for (int i = 0; i < n; i++) {\n                if (!used[i] && (u == -1 || min_edge[i] < min_edge[u])) {\n                    u = i;\n                }\n            }\n            if (u == -1) break;\n            used[u] = true;\n            total += min_edge[u];\n            for (int v = 0; v < n; v++) {\n                if (!used[v]) {\n                    ll w = d_graph[T[u]][T[v]];\n                    if (w < min_edge[v]) {\n                        min_edge[v] = w;\n                    }\n                }\n            }\n        }\n        return total;\n    };\n\n    auto compute_station_cost = [&](const vector<int>& T) -> ll {\n        vector<ll> cluster_max(N, 0);\n        for (int k = 0; k < K; k++) {\n            ll minD = LLONG_MAX;\n            int best_station = -1;\n            for (int s : T) {\n                if (D_mat[s][k] < minD) {\n                    minD = D_mat[s][k];\n                    best_station = s;\n                }\n            }\n            if (D_mat[best_station][k] > cluster_max[best_station]) {\n                cluster_max[best_station] = D_mat[best_station][k];\n            }\n        }\n        ll cost = 0;\n        for (int s : T) {\n            ll maxD = cluster_max[s];\n            if (maxD == 0) continue;\n            ll p = sqrt(maxD);\n            if (p * p < maxD) {\n                p++;\n            }\n            if (p > 5000) p = 5000;\n            cost += p * p;\n        }\n        return cost;\n    };\n\n    vector<int> current_T;\n    current_T.push_back(0);\n\n    while (true) {\n        int uncovered_k = -1;\n        for (int k = 0; k < K; k++) {\n            ll minD = LLONG_MAX;\n            for (int s : current_T) {\n                if (D_mat[s][k] < minD) {\n                    minD = D_mat[s][k];\n                }\n            }\n            if (minD > 50000000LL) {\n                uncovered_k = k;\n                break;\n            }\n        }\n        if (uncovered_k == -1) break;\n\n        int best_j = -1;\n        ll best_d_euclid = LLONG_MAX;\n        ll best_d_graph_val = LLONG_MAX;\n        for (int j = 0; j < N; j++) {\n            if (find(current_T.begin(), current_T.end(), j) != current_T.end()) \n                continue;\n\n            ll d_euclid = D_mat[j][uncovered_k];\n            ll d_graph_val = LLONG_MAX;\n            for (int s : current_T) {\n                if (d_graph[s][j] < d_graph_val) {\n                    d_graph_val = d_graph[s][j];\n                }\n            }\n            if (best_j == -1 || d_euclid < best_d_euclid || (d_euclid == best_d_euclid && d_graph_val < best_d_graph_val)) {\n                best_j = j;\n                best_d_euclid = d_euclid;\n                best_d_graph_val = d_graph_val;\n            }\n        }\n        if (best_j == -1) break;\n        current_T.push_back(best_j);\n    }\n\n    for (int iter = 0; iter < 10; iter++) {\n        bool improved = false;\n        ll best_total_after_removal = LLONG_MAX;\n        vector<int> candidate_T_after_removal;\n\n        for (int idx = 1; idx < current_T.size(); idx++) {\n            int i = current_T[idx];\n            bool valid = true;\n            for (int k = 0; k < K; k++) {\n                ll minD_orig = LLONG_MAX;\n                ll minD_after = LLONG_MAX;\n                for (int s : current_T) {\n                    if (D_mat[s][k] < minD_orig) minD_orig = D_mat[s][k];\n                    if (s == i) continue;\n                    if (D_mat[s][k] < minD_after) minD_after = D_mat[s][k];\n                }\n                if (minD_orig == D_mat[i][k] && minD_after > 50000000LL) {\n                    valid = false;\n                    break;\n                }\n            }\n            if (!valid) continue;\n\n            vector<int> candidate_T;\n            for (int s : current_T) {\n                if (s != i) candidate_T.push_back(s);\n            }\n\n            ll edge_cost_candidate = compute_mst_cost(candidate_T);\n            ll station_cost_candidate = compute_station_cost(candidate_T);\n            ll total_candidate = edge_cost_candidate + station_cost_candidate;\n\n            ll current_total = compute_mst_cost(current_T) + compute_station_cost(current_T);\n            if (total_candidate < current_total) {\n                if (total_candidate < best_total_after_removal) {\n                    best_total_after_removal = total_candidate;\n                    candidate_T_after_removal = candidate_T;\n                }\n            }\n        }\n\n        if (best_total_after_removal != LLONG_MAX) {\n            current_T = candidate_T_after_removal;\n            improved = true;\n        }\n\n        if (improved) continue;\n\n        best_total_after_removal = LLONG_MAX;\n        vector<int> candidate_T_after_add;\n\n        for (int j = 0; j < N; j++) {\n            if (find(current_T.begin(), current_T.end(), j) != current_T.end()) \n                continue;\n\n            vector<int> candidate_T = current_T;\n            candidate_T.push_back(j);\n\n            ll edge_cost_candidate = compute_mst_cost(candidate_T);\n            ll station_cost_candidate = compute_station_cost(candidate_T);\n            ll total_candidate = edge_cost_candidate + station_cost_candidate;\n\n            ll current_total = compute_mst_cost(current_T) + compute_station_cost(current_T);\n            if (total_candidate < current_total) {\n                if (total_candidate < best_total_after_removal) {\n                    best_total_after_removal = total_candidate;\n                    candidate_T_after_add = candidate_T;\n                }\n            }\n        }\n\n        if (best_total_after_removal != LLONG_MAX) {\n            current_T = candidate_T_after_add;\n            improved = true;\n        }\n\n        if (!improved) break;\n    }\n\n    vector<int> assignment(K, -1);\n    vector<ll> maxD_in_cluster(N, 0);\n    for (int k = 0; k < K; k++) {\n        ll minD = LLONG_MAX;\n        int best_station = -1;\n        for (int s : current_T) {\n            if (D_mat[s][k] < minD) {\n                minD = D_mat[s][k];\n                best_station = s;\n            }\n        }\n        assignment[k] = best_station;\n        if (D_mat[best_station][k] > maxD_in_cluster[best_station]) {\n            maxD_in_cluster[best_station] = D_mat[best_station][k];\n        }\n    }\n\n    vector<int> P(N, 0);\n    for (int i = 0; i < N; i++) {\n        if (find(current_T.begin(), current_T.end(), i) == current_T.end()) {\n            P[i] = 0;\n        } else {\n            ll maxD = maxD_in_cluster[i];\n            if (maxD == 0) {\n                P[i] = 0;\n            } else {\n                ll p = sqrt(maxD);\n                if (p * p < maxD) {\n                    p++;\n                }\n                if (p > 5000) p = 5000;\n                P[i] = (int)p;\n            }\n        }\n    }\n\n    auto compute_mst_edges = [&](const vector<int>& T) -> vector<pair<int, int>> {\n        int n = T.size();\n        if (n <= 1) return {};\n        vector<ll> min_edge(n, LLONG_MAX);\n        vector<int> parent(n, -1);\n        vector<bool> used(n, false);\n        min_edge[0] = 0;\n        for (int iter = 0; iter < n; iter++) {\n            int u = -1;\n            for (int i = 0; i < n; i++) {\n                if (!used[i] && (u == -1 || min_edge[i] < min_edge[u])) {\n                    u = i;\n                }\n            }\n            if (u == -1) break;\n            used[u] = true;\n            for (int v = 0; v < n; v++) {\n                if (!used[v]) {\n                    ll w = d_graph[T[u]][T[v]];\n                    if (w < min_edge[v]) {\n                        min_edge[v] = w;\n                        parent[v] = u;\n                    }\n                }\n            }\n        }\n        vector<pair<int, int>> edges;\n        for (int i = 1; i < n; i++) {\n            int u_idx = parent[i];\n            edges.push_back({T[u_idx], T[i]});\n        }\n        return edges;\n    };\n\n    auto get_path = [&](int start, int end) -> vector<int> {\n        vector<int> path;\n        int u = start;\n        while (u != end) {\n            path.push_back(u);\n            u = next_node[u][end];\n        }\n        path.push_back(end);\n        return path;\n    };\n\n    vector<pair<int, int>> mst_edges = compute_mst_edges(current_T);\n    vector<bool> edge_used(M, false);\n    for (auto &e : mst_edges) {\n        int u = e.first;\n        int v = e.second;\n        vector<int> path = get_path(u, v);\n        for (int idx = 0; idx < (int)path.size()-1; idx++) {\n            int a = path[idx];\n            int b = path[idx+1];\n            for (int j = 0; j < M; j++) {\n                int u1 = get<0>(edges[j]);\n                int v1 = get<1>(edges[j]);\n                if ((u1 == a && v1 == b) || (u1 == b && v1 == a)) {\n                    edge_used[j] = true;\n                    break;\n                }\n            }\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    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 <bits/stdc++.h>\nusing namespace std;\n\ninline int get_idx(int x, int y) {\n    return x * (x + 1) / 2 + y;\n}\n\nint main() {\n    vector<int> pyramid(465);\n    vector<array<int, 2>> pos(465);\n\n    int idx = 0;\n    for (int x = 0; x < 30; x++) {\n        for (int y = 0; y <= x; y++) {\n            cin >> pyramid[idx];\n            pos[pyramid[idx]] = {x, y};\n            idx++;\n        }\n    }\n\n    vector<array<int, 4>> swaps;\n\n    for (int val = 464; val >= 0; val--) {\n        int x = pos[val][0];\n        int y = pos[val][1];\n        if (x == 29) continue;\n\n        while (true) {\n            int left_x = x + 1;\n            int left_y = y;\n            int right_x = x + 1;\n            int right_y = y + 1;\n\n            int left_idx = get_idx(left_x, left_y);\n            int right_idx = get_idx(right_x, right_y);\n            int left_val = pyramid[left_idx];\n            int right_val = pyramid[right_idx];\n\n            if (val <= left_val && val <= right_val) {\n                break;\n            }\n\n            int swap_x, swap_y;\n            if (left_val <= right_val) {\n                swap_x = left_x;\n                swap_y = left_y;\n            } else {\n                swap_x = right_x;\n                swap_y = right_y;\n            }\n            int swap_idx = get_idx(swap_x, swap_y);\n            int swapped_val = pyramid[swap_idx];\n\n            swaps.push_back({x, y, swap_x, swap_y});\n            swap(pyramid[get_idx(x, y)], pyramid[swap_idx]);\n\n            pos[val] = {swap_x, swap_y};\n            pos[swapped_val] = {x, y};\n\n            x = swap_x;\n            y = swap_y;\n            if (x == 29) break;\n        }\n    }\n\n    cout << swaps.size() << '\\n';\n    for (auto& s : swaps) {\n        cout << s[0] << ' ' << s[1] << ' ' << s[2] << ' ' << s[3] << '\\n';\n    }\n    return 0;\n}","toyota2023summer-final":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\n#include <tuple>\n#include <set>\n#include <cctype>\n#include <cmath>\nusing namespace std;\n\nint main() {\n    int D, N;\n    cin >> D >> N;\n    vector<vector<bool>> is_obstacle(9, vector<bool>(9, false));\n    set<pair<int, int>> non_storage = {{0, 4}};\n\n    for (int idx = 0; idx < N; idx++) {\n        int ri, rj;\n        cin >> ri >> rj;\n        is_obstacle[ri][rj] = true;\n    }\n\n    vector<pair<int, int>> storage_cells;\n    for (int i = 0; i < 9; i++) {\n        for (int j = 0; j < 9; j++) {\n            if (non_storage.find({i, j}) != non_storage.end()) continue;\n            if (is_obstacle[i][j]) continue;\n            storage_cells.push_back({i, j});\n        }\n    }\n\n    vector<vector<int>> dist(9, vector<int>(9, -1));\n    queue<pair<int, int>> q_bfs;\n    dist[0][4] = 0;\n    q_bfs.push({0, 4});\n    const vector<int> dx = {1, -1, 0, 0};\n    const vector<int> dy = {0, 0, 1, -1};\n\n    while (!q_bfs.empty()) {\n        auto [i, j] = q_bfs.front();\n        q_bfs.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 < 9 && nj >= 0 && nj < 9) {\n                if (dist[ni][nj] == -1 && !is_obstacle[ni][nj]) {\n                    dist[ni][nj] = dist[i][j] + 1;\n                    q_bfs.push({ni, nj});\n                }\n            }\n        }\n    }\n\n    sort(storage_cells.begin(), storage_cells.end(), [&](const pair<int, int>& a, const pair<int, int>& b) {\n        if (dist[a.first][a.second] != dist[b.first][b.second]) {\n            return dist[a.first][a.second] > dist[b.first][b.second];\n        }\n        if (a.first != b.first) {\n            return a.first < b.first;\n        }\n        return a.second < b.second;\n    });\n\n    int M = storage_cells.size();\n    vector<vector<int>> container_id(9, vector<int>(9, -1));\n\n    for (int d = 0; d < M; d++) {\n        int t;\n        cin >> t;\n        int index = M - 1 - t;\n        auto [i, j] = storage_cells[index];\n        cout << i << \" \" << j << endl;\n        container_id[i][j] = t;\n    }\n\n    vector<vector<bool>> is_storage(9, vector<bool>(9, false));\n    for (auto [i, j] : storage_cells) {\n        is_storage[i][j] = true;\n    }\n\n    vector<vector<bool>> is_removed(9, vector<bool>(9, false));\n    vector<pair<int, int>> transport_order;\n\n    for (int step = 0; step < M; step++) {\n        vector<vector<bool>> visited(9, vector<bool>(9, false));\n        queue<pair<int, int>> q;\n        if (!is_obstacle[0][4]) {\n            visited[0][4] = true;\n            q.push({0, 4});\n        }\n\n        while (!q.empty()) {\n            auto [i, j] = q.front();\n            q.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 >= 9 || nj < 0 || nj >= 9) continue;\n                if (visited[ni][nj]) continue;\n                if (is_obstacle[ni][nj]) continue;\n                if (is_storage[ni][nj] && !is_removed[ni][nj]) {\n                    continue;\n                }\n                visited[ni][nj] = true;\n                q.push({ni, nj});\n            }\n        }\n\n        vector<tuple<int, int, int>> candidates;\n        for (int i = 0; i < 9; i++) {\n            for (int j = 0; j < 9; j++) {\n                if (!is_storage[i][j]) continue;\n                if (is_removed[i][j]) continue;\n                bool has_visited_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 < 9 && nj >= 0 && nj < 9) {\n                        if (visited[ni][nj]) {\n                            has_visited_neighbor = true;\n                            break;\n                        }\n                    }\n                }\n                if (has_visited_neighbor) {\n                    candidates.push_back(make_tuple(container_id[i][j], i, j));\n                }\n            }\n        }\n\n        sort(candidates.begin(), candidates.end());\n        auto [id_val, i_val, j_val] = candidates[0];\n        transport_order.push_back({i_val, j_val});\n        is_removed[i_val][j_val] = true;\n    }\n\n    for (auto [i, j] : transport_order) {\n        cout << i << \" \" << j << endl;\n    }\n\n    return 0;\n}","ahc024":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int n = 50;\nconst int m = 100;\nconst vector<pair<int, int>> directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};\n\nint main() {\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<set<int>> adj(m + 1);\n    vector<bool> boundary(m + 1, false);\n\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            int c = grid[i][j];\n            if (i == 0 || i == n - 1 || j == 0 || j == n - 1) {\n                boundary[c] = true;\n            }\n            for (auto [dx, dy] : directions) {\n                int ni = i + dx;\n                int nj = j + dy;\n                if (ni >= 0 && ni < n && nj >= 0 && nj < n) {\n                    int d = grid[ni][nj];\n                    if (d != c) {\n                        adj[c].insert(d);\n                        adj[d].insert(c);\n                    }\n                }\n            }\n        }\n    }\n\n    vector<pair<int, int>> boundary_positions;\n    for (int j = 0; j < n; j++) boundary_positions.push_back({n - 1, j});\n    for (int i = n - 2; i >= 0; i--) boundary_positions.push_back({i, n - 1});\n    for (int j = n - 2; j >= 0; j--) boundary_positions.push_back({0, j});\n    for (int i = 1; i < n - 1; i++) boundary_positions.push_back({i, 0});\n\n    vector<set<pair<int, int>>> regions(m + 1);\n    vector<queue<pair<int, int>>> frontiers(m + 1);\n    vector<vector<int>> ans(n, vector<int>(n, 0));\n    vector<bool> placed(m + 1, false);\n\n    int pos_index = 0;\n    for (int c = 1; c <= m; c++) {\n        if (boundary[c]) {\n            if (pos_index >= (int)boundary_positions.size()) break;\n            auto [i, j] = boundary_positions[pos_index++];\n            ans[i][j] = c;\n            regions[c].insert({i, j});\n            frontiers[c].push({i, j});\n            placed[c] = true;\n        }\n    }\n\n    vector<vector<int>> adj_list(m + 1);\n    for (int c = 1; c <= m; c++) {\n        for (int d : adj[c]) {\n            if (d >= 1 && d <= m) {\n                adj_list[c].push_back(d);\n            }\n        }\n    }\n\n    queue<int> q;\n    for (int c = 1; c <= m; c++) {\n        if (placed[c]) {\n            q.push(c);\n        }\n    }\n\n    auto try_place_edge = [&](int c, int d) -> bool {\n        while (!frontiers[c].empty()) {\n            auto cell = frontiers[c].front();\n            vector<pair<int, int>> free_dirs;\n            for (auto [dx, dy] : directions) {\n                int ni = cell.first + dx;\n                int nj = cell.second + dy;\n                if (ni >= 0 && ni < n && nj >= 0 && nj < n && ans[ni][nj] == 0) {\n                    free_dirs.push_back({dx, dy});\n                }\n            }\n            if (!free_dirs.empty()) {\n                auto [dx, dy] = free_dirs[0];\n                int ni = cell.first + dx;\n                int nj = cell.second + dy;\n                ans[ni][nj] = d;\n                regions[d].insert({ni, nj});\n                frontiers[d].push({ni, nj});\n                return true;\n            } else {\n                frontiers[c].pop();\n            }\n        }\n\n        for (auto cell : regions[c]) {\n            for (auto [dx, dy] : directions) {\n                int ni = cell.first + dx;\n                int nj = cell.second + dy;\n                if (ni >= 0 && ni < n && nj >= 0 && nj < n && ans[ni][nj] == 0) {\n                    ans[ni][nj] = c;\n                    regions[c].insert({ni, nj});\n                    frontiers[c].push({ni, nj});\n                    return try_place_edge(c, d);\n                }\n            }\n        }\n        return false;\n    };\n\n    while (!q.empty()) {\n        int c = q.front();\n        q.pop();\n        for (int d : adj_list[c]) {\n            if (placed[d]) continue;\n            if (try_place_edge(c, d)) {\n                placed[d] = true;\n                q.push(d);\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 << ans[i][j];\n        }\n        cout << '\\n';\n    }\n\n    return 0;\n}","ahc025":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(0);\n    cin.tie(0);\n\n    int N, D, Q;\n    cin >> N >> D >> Q;\n\n    vector<double> weight_est(N, 1.0);\n    vector<vector<int>> comparisons;\n    int queries_used = 0;\n\n    auto make_query = [&](const vector<int>& L, const vector<int>& R) -> string {\n        cout << L.size() << \" \" << R.size();\n        for (int x : L) cout << \" \" << x;\n        for (int x : R) cout << \" \" << x;\n        cout << endl;\n        string res;\n        cin >> res;\n        queries_used++;\n        return res;\n    };\n\n    auto update_weights = [&](const vector<int>& L, const vector<int>& R, const string& res) {\n        if (res == \"=\") return;\n        double factor = 1.01;\n        if (res == \"<\") {\n            for (int x : L) weight_est[x] *= 0.99;\n            for (int x : R) weight_est[x] *= 1.01;\n        } else {\n            for (int x : L) weight_est[x] *= 1.01;\n            for (int x : R) weight_est[x] *= 0.99;\n        }\n    };\n\n    for (int i = 0; i < N && queries_used < Q; i++) {\n        for (int j = i + 1; j < N && queries_used < Q; j++) {\n            vector<int> L = {i}, R = {j};\n            string res = make_query(L, R);\n            update_weights(L, R, res);\n        }\n    }\n\n    while (queries_used < Q) {\n        vector<int> L = {0}, R = {1};\n        string res = make_query(L, R);\n        update_weights(L, R, res);\n    }\n\n    vector<int> indices(N);\n    iota(indices.begin(), indices.end(), 0);\n    sort(indices.begin(), indices.end(), [&](int a, int b) {\n        return weight_est[a] > weight_est[b];\n    });\n\n    vector<long long> set_sum(D, 0);\n    vector<int> assignment(N, -1);\n\n    for (int idx = 0; idx < N; idx++) {\n        int item = indices[idx];\n        int best_set = 0;\n        for (int s = 1; s < D; s++) {\n            if (set_sum[s] < set_sum[best_set]) {\n                best_set = s;\n            }\n        }\n        assignment[item] = best_set;\n        set_sum[best_set] += (long long)(weight_est[item] * 1000000);\n    }\n\n    for (int i = 0; i < N; i++) {\n        if (i) cout << \" \";\n        cout << assignment[i];\n    }\n    cout << endl;\n\n    return 0;\n}","ahc026":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int n, m;\n    cin >> n >> m;\n    int k = n / m;\n\n    vector<vector<int>> stacks(m);\n    for (int i = 0; i < m; i++) {\n        stacks[i].resize(k);\n        for (int j = 0; j < k; j++) {\n            cin >> stacks[i][j];\n        }\n    }\n\n    vector<int> empty_stacks;\n    vector<pair<int, int>> ops;\n    int cur = 1;\n\n    while (cur <= n) {\n        bool carried = false;\n        for (int s = 0; s < m; s++) {\n            if (stacks[s].empty()) continue;\n            if (stacks[s].back() == cur) {\n                ops.push_back({cur, 0});\n                stacks[s].pop_back();\n                if (stacks[s].empty()) {\n                    empty_stacks.push_back(s);\n                }\n                cur++;\n                carried = true;\n                break;\n            }\n        }\n        if (carried) {\n            continue;\n        }\n\n        int src = -1;\n        int pos = -1;\n        for (int s = 0; s < m; s++) {\n            auto it = find(stacks[s].begin(), stacks[s].end(), cur);\n            if (it != stacks[s].end()) {\n                src = s;\n                pos = it - stacks[s].begin();\n                break;\n            }\n        }\n        if (src == -1) {\n            break;\n        }\n\n        vector<int> segment(stacks[src].begin() + pos + 1, stacks[src].end());\n        int T = segment.back();\n        int dest = -1;\n\n        if (!empty_stacks.empty()) {\n            dest = empty_stacks.back();\n            empty_stacks.pop_back();\n        } else {\n            int candidate_large = -1;\n            int candidate_small = -1;\n            for (int s = 0; s < m; s++) {\n                if (stacks[s].empty()) continue;\n                int top_val = stacks[s].back();\n                if (top_val > T) {\n                    if (candidate_large == -1 || top_val > stacks[candidate_large].back()) {\n                        candidate_large = s;\n                    }\n                }\n                if (candidate_small == -1 || top_val < stacks[candidate_small].back()) {\n                    candidate_small = s;\n                }\n            }\n            if (candidate_large != -1) {\n                dest = candidate_large;\n            } else if (candidate_small != -1) {\n                dest = candidate_small;\n            } else {\n                dest = 0;\n            }\n        }\n\n        int v = segment[0];\n        stacks[src].resize(pos + 1);\n        for (int box : segment) {\n            stacks[dest].push_back(box);\n        }\n        ops.push_back({v, dest + 1});\n    }\n\n    for (auto &op : ops) {\n        cout << op.first << \" \" << op.second << \"\\n\";\n    }\n\n    return 0;\n}","ahc027":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N;\n    cin >> N;\n\n    vector<string> h(N-1);\n    for (int i = 0; i < N-1; i++) {\n        cin >> h[i];\n    }\n\n    vector<string> v(N);\n    for (int i = 0; i < N; i++) {\n        cin >> v[i];\n    }\n\n    vector<vector<int>> d(N, vector<int>(N));\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cin >> d[i][j];\n        }\n    }\n\n    vector<vector<vector<tuple<int, int, char>>>> neighbors(N, vector<vector<tuple<int, int, char>>>(N));\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (j < N-1 && v[i][j] == '0') {\n                neighbors[i][j].emplace_back(i, j+1, 'R');\n            }\n            if (i < N-1 && h[i][j] == '0') {\n                neighbors[i][j].emplace_back(i+1, j, 'D');\n            }\n            if (j > 0 && v[i][j-1] == '0') {\n                neighbors[i][j].emplace_back(i, j-1, 'L');\n            }\n            if (i > 0 && h[i-1][j] == '0') {\n                neighbors[i][j].emplace_back(i-1, j, 'U');\n            }\n            sort(neighbors[i][j].begin(), neighbors[i][j].end(), [&](const auto& a, const auto& b) {\n                int da = d[get<0>(a)][get<1>(a)];\n                int db = d[get<0>(b)][get<1>(b)];\n                return da > db;\n            });\n        }\n    }\n\n    vector<vector<bool>> visited(N, vector<bool>(N, false));\n    visited[0][0] = true;\n\n    stack<tuple<int, int, int>> st;\n    st.emplace(0, 0, 0);\n    string route;\n\n    while (!st.empty()) {\n        auto& [i, j, idx] = st.top();\n        if (idx < (int)neighbors[i][j].size()) {\n            auto [ni, nj, move_char] = neighbors[i][j][idx];\n            idx++;\n            if (!visited[ni][nj]) {\n                visited[ni][nj] = true;\n                st.emplace(ni, nj, 0);\n                route += move_char;\n            }\n        } else {\n            st.pop();\n            if (!st.empty()) {\n                auto [pi, pj, _] = st.top();\n                int di = i - pi;\n                int dj = j - pj;\n                char rev_char;\n                if (di == 1) rev_char = 'U';\n                else if (di == -1) rev_char = 'D';\n                else if (dj == 1) rev_char = 'L';\n                else rev_char = 'R';\n                route += rev_char;\n            }\n        }\n    }\n\n    cout << route << endl;\n\n    return 0;\n}","ahc028":"#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(0);\n\n    int N, M;\n    cin >> N >> M;\n    int s_i, s_j;\n    cin >> s_i >> s_j;\n    vector<string> grid(N);\n    for (int i = 0; i < N; i++) {\n        cin >> grid[i];\n    }\n    vector<string> t_list(M);\n    for (int i = 0; i < M; i++) {\n        cin >> t_list[i];\n    }\n\n    unordered_map<string, int> five_gram_map;\n    for (int i = 0; i < M; i++) {\n        five_gram_map[t_list[i]] = i;\n    }\n\n    unordered_map<string, int> prefix_count_map;\n    for (int i = 0; i < M; i++) {\n        string prefix = t_list[i].substr(0, 4);\n        prefix_count_map[prefix]++;\n    }\n\n    const int NUM_RESTARTS = 5;\n    const int MAX_STEPS_PER_RESTART = 3000;\n    int best_covered = -1;\n    ll best_cost = LLONG_MAX;\n    vector<pair<int, int>> best_path;\n\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n\n    for (int restart = 0; restart < NUM_RESTARTS; restart++) {\n        auto local_prefix_count_map = prefix_count_map;\n        vector<bool> local_covered(M, false);\n        int local_covered_count = 0;\n        string suffix(4, '#');\n        int cur_i = s_i;\n        int cur_j = s_j;\n        vector<pair<int, int>> path;\n        ll total_cost = 0;\n\n        for (int step = 0; step < MAX_STEPS_PER_RESTART; step++) {\n            if (local_covered_count == M) {\n                break;\n            }\n\n            vector<tuple<int, int, string, int>> gain_candidates;\n            int best_gain_cost = INT_MAX;\n\n            vector<tuple<int, int, string>> heuristic_candidates;\n            int best_heuristic_val = -1;\n            int best_heuristic_cost = INT_MAX;\n\n            for (int i = 0; i < N; i++) {\n                for (int j = 0; j < N; j++) {\n                    char c = grid[i][j];\n                    string new_suffix = suffix.substr(1) + c;\n\n                    bool forms_5gram = true;\n                    for (char ch : suffix) {\n                        if (ch == '#') {\n                            forms_5gram = false;\n                            break;\n                        }\n                    }\n\n                    int gain = 0;\n                    int gain_index = -1;\n                    if (forms_5gram) {\n                        string five_gram = suffix + string(1, c);\n                        auto it = five_gram_map.find(five_gram);\n                        if (it != five_gram_map.end()) {\n                            int idx = it->second;\n                            if (!local_covered[idx]) {\n                                gain = 1;\n                                gain_index = idx;\n                            }\n                        }\n                    }\n\n                    int move_cost = abs(i - cur_i) + abs(j - cur_j) + 1;\n\n                    if (gain > 0) {\n                        if (move_cost < best_gain_cost) {\n                            best_gain_cost = move_cost;\n                            gain_candidates.clear();\n                            gain_candidates.push_back(make_tuple(i, j, new_suffix, gain_index));\n                        } else if (move_cost == best_gain_cost) {\n                            gain_candidates.push_back(make_tuple(i, j, new_suffix, gain_index));\n                        }\n                    } else {\n                        int heuristic_val = 0;\n                        bool valid = true;\n                        for (char ch : new_suffix) {\n                            if (ch < 'A' || ch > 'Z') {\n                                valid = false;\n                                break;\n                            }\n                        }\n                        if (valid) {\n                            auto it = local_prefix_count_map.find(new_suffix);\n                            if (it != local_prefix_count_map.end()) {\n                                heuristic_val = it->second;\n                            }\n                        }\n\n                        if (heuristic_val > best_heuristic_val) {\n                            best_heuristic_val = heuristic_val;\n                            best_heuristic_cost = move_cost;\n                            heuristic_candidates.clear();\n                            heuristic_candidates.push_back(make_tuple(i, j, new_suffix));\n                        } else if (heuristic_val == best_heuristic_val) {\n                            if (move_cost < best_heuristic_cost) {\n                                best_heuristic_cost = move_cost;\n                                heuristic_candidates.clear();\n                                heuristic_candidates.push_back(make_tuple(i, j, new_suffix));\n                            } else if (move_cost == best_heuristic_cost) {\n                                heuristic_candidates.push_back(make_tuple(i, j, new_suffix));\n                            }\n                        }\n                    }\n                }\n            }\n\n            int chosen_i = -1, chosen_j = -1;\n            string chosen_suffix = \"\";\n            int gain_idx = -1;\n\n            if (!gain_candidates.empty()) {\n                uniform_int_distribution<int> dist(0, (int)gain_candidates.size() - 1);\n                auto candidate = gain_candidates[dist(rng)];\n                chosen_i = get<0>(candidate);\n                chosen_j = get<1>(candidate);\n                chosen_suffix = get<2>(candidate);\n                gain_idx = get<3>(candidate);\n            } else {\n                if (heuristic_candidates.empty()) {\n                    uniform_int_distribution<int> dist(0, N*N-1);\n                    int idx = dist(rng);\n                    chosen_i = idx / N;\n                    chosen_j = idx % N;\n                    chosen_suffix = suffix.substr(1) + grid[chosen_i][chosen_j];\n                } else {\n                    uniform_int_distribution<int> dist(0, (int)heuristic_candidates.size() - 1);\n                    auto candidate = heuristic_candidates[dist(rng)];\n                    chosen_i = get<0>(candidate);\n                    chosen_j = get<1>(candidate);\n                    chosen_suffix = get<2>(candidate);\n                }\n            }\n\n            int move_cost = abs(chosen_i - cur_i) + abs(chosen_j - cur_j) + 1;\n            total_cost += move_cost;\n\n            cur_i = chosen_i;\n            cur_j = chosen_j;\n            suffix = chosen_suffix;\n\n            if (gain_idx != -1) {\n                local_covered[gain_idx] = true;\n                local_covered_count++;\n                string prefix = t_list[gain_idx].substr(0, 4);\n                local_prefix_count_map[prefix]--;\n                if (local_prefix_count_map[prefix] == 0) {\n                    local_prefix_count_map.erase(prefix);\n                }\n            }\n\n            path.push_back({chosen_i, chosen_j});\n        }\n\n        if (local_covered_count > best_covered || (local_covered_count == best_covered && total_cost < best_cost)) {\n            best_covered = local_covered_count;\n            best_cost = total_cost;\n            best_path = path;\n        }\n    }\n\n    cout << best_path.size() << \"\\n\";\n    for (auto &p : best_path) {\n        cout << p.first << \" \" << p.second << \"\\n\";\n    }\n\n    return 0;\n}","ahc030":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <tuple>\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<tuple<vector<pair<int, int>>, int, int>> fields;\n    for (int k = 0; k < M; k++) {\n        int d;\n        cin >> d;\n        vector<pair<int, int>> shape;\n        int max_i = 0, max_j = 0;\n        for (int i = 0; i < d; i++) {\n            int a, b;\n            cin >> a >> b;\n            shape.emplace_back(a, b);\n            if (a > max_i) max_i = a;\n            if (b > max_j) max_j = b;\n        }\n        fields.emplace_back(shape, max_i, max_j);\n    }\n\n    priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<>> pq;\n    int center = N / 2;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            int dist = abs(i - center) + abs(j - center);\n            pq.emplace(dist, i, j);\n        }\n    }\n\n    vector<vector<int>> status(N, vector<int>(N, 0));\n    vector<pair<int, int>> positive_list;\n\n    while (!pq.empty()) {\n        auto [dist, i, j] = pq.top();\n        pq.pop();\n\n        if (status[i][j] != 0) {\n            continue;\n        }\n\n        cout << \"q 1 \" << i << \" \" << j << endl;\n        cout.flush();\n\n        int v;\n        cin >> v;\n\n        if (v > 0) {\n            status[i][j] = 1;\n            positive_list.emplace_back(i, j);\n\n            for (const auto& field : fields) {\n                const auto& shape = get<0>(field);\n                int max_i = get<1>(field);\n                int max_j = get<2>(field);\n\n                for (const auto& offset : shape) {\n                    int a = offset.first;\n                    int b = offset.second;\n                    int p = i - a;\n                    int q_val = j - b;\n                    if (p < 0 || q_val < 0) continue;\n                    if (p + max_i >= N || q_val + max_j >= N) continue;\n\n                    for (const auto& cell : shape) {\n                        int i2 = p + cell.first;\n                        int j2 = q_val + cell.second;\n                        if (i2 < N && j2 < N && status[i2][j2] == 0) {\n                            int dist2 = abs(i2 - center) + abs(j2 - center);\n                            pq.emplace(dist2, i2, j2);\n                        }\n                    }\n                }\n            }\n        } else {\n            status[i][j] = 2;\n        }\n    }\n\n    cout << \"a \" << positive_list.size();\n    for (const auto& pos : positive_list) {\n        cout << \" \" << pos.first << \" \" << pos.second;\n    }\n    cout << endl;\n    cout.flush();\n\n    int result;\n    cin >> result;\n    return 0;\n}","ahc031":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Rect {\n    int y1, x1, y2, x2;\n    int area() const { return (y2 - y1) * (x2 - x1); }\n};\n\nbool overlap(const Rect& a, const Rect& b) {\n    return !(a.y2 <= b.y1 || b.y2 <= a.y1 || a.x2 <= b.x1 || b.x1 >= a.x2);\n}\n\nvector<Rect> split_free_space(const Rect& placed, const Rect& free_space) {\n    vector<Rect> splits;\n    if (free_space.y1 < placed.y1) {\n        splits.push_back({free_space.y1, free_space.x1, placed.y1, free_space.x2});\n    }\n    if (placed.y2 < free_space.y2) {\n        splits.push_back({placed.y2, free_space.x1, free_space.y2, free_space.x2});\n    }\n    if (free_space.x1 < placed.x1) {\n        int y1 = max(free_space.y1, placed.y1);\n        int y2 = min(free_space.y2, placed.y2);\n        if (y1 < y2) {\n            splits.push_back({y1, free_space.x1, y2, placed.x1});\n        }\n    }\n    if (placed.x2 < free_space.x2) {\n        int y1 = max(free_space.y1, placed.y1);\n        int y2 = min(free_space.y2, placed.y2);\n        if (y1 < y2) {\n            splits.push_back({y1, placed.x2, y2, free_space.x2});\n        }\n    }\n    return splits;\n}\n\nvector<Rect> pack_day(int W, const vector<int>& areas, const vector<Rect>& prev_rects) {\n    int N = areas.size();\n    vector<Rect> free_space = {{0, 0, W, W}};\n    vector<Rect> result(N, {0, 0, 1, 1});\n    vector<bool> placed(N, false);\n\n    for (int k = 0; k < N; k++) {\n        int req_area = areas[k];\n        Rect best_fit;\n        int best_fit_idx = -1;\n        double best_score = -1.0;\n\n        for (int i = 0; i < (int)free_space.size(); i++) {\n            Rect f = free_space[i];\n            if (f.area() < req_area) continue;\n\n            double score = 0.0;\n            if (k < (int)prev_rects.size()) {\n                Rect p = prev_rects[k];\n                int overlap_y1 = max(f.y1, p.y1);\n                int overlap_y2 = min(f.y2, p.y2);\n                int overlap_x1 = max(f.x1, p.x1);\n                int overlap_x2 = min(f.x2, p.x2);\n                if (overlap_y1 < overlap_y2 && overlap_x1 < overlap_x2) {\n                    score = -1.0 * (overlap_y2 - overlap_y1) * (overlap_x2 - overlap_x1);\n                } else {\n                    int center_dist = abs((f.y1 + f.y2)/2 - (p.y1 + p.y2)/2) +\n                                    abs((f.x1 + f.x2)/2 - (p.x1 + p.x2)/2);\n                    score = -1.0 * center_dist;\n                }\n            } else {\n                score = f.area();\n            }\n\n            if (score > best_score) {\n                best_score = score;\n                best_fit = f;\n                best_fit_idx = i;\n            }\n        }\n\n        if (best_fit_idx == -1) {\n            for (int i = 0; i < (int)free_space.size(); i++) {\n                if (free_space[i].area() > (int)best_score) {\n                    best_score = free_space[i].area();\n                    best_fit = free_space[i];\n                    best_fit_idx = i;\n                }\n            }\n            if (best_fit_idx == -1) {\n                best_fit = {0, 0, min(W, 1), min(W, 1)};\n            }\n        }\n\n        int h = 1, w = best_fit.area();\n        if (req_area > 0) {\n            h = max(1, (int)sqrt(req_area));\n            while (h > 0) {\n                w = (req_area + h - 1) / h;\n                if (w <= best_fit.x2 - best_fit.x1) break;\n                h--;\n            }\n            if (h == 0) {\n                h = best_fit.y2 - best_fit.y1;\n                w = best_fit.x2 - best_fit.x1;\n            }\n            h = min(h, best_fit.y2 - best_fit.y1);\n            w = min(w, best_fit.x2 - best_fit.x1);\n        }\n\n        int base_y = best_fit.y1;\n        int base_x = best_fit.x1;\n        if (k < (int)prev_rects.size()) {\n            base_y = max(base_y, prev_rects[k].y1);\n            base_x = max(base_x, prev_rects[k].x1);\n            if (base_y + h > best_fit.y2) base_y = best_fit.y2 - h;\n            if (base_x + w > best_fit.x2) base_x = best_fit.x2 - w;\n        }\n\n        result[k] = {base_y, base_x, base_y + h, base_x + w};\n        placed[k] = true;\n\n        free_space.erase(free_space.begin() + best_fit_idx);\n        vector<Rect> new_spaces = split_free_space(result[k], best_fit);\n        for (Rect& ns : new_spaces) {\n            if (ns.area() > 0) {\n                free_space.push_back(ns);\n            }\n        }\n    }\n\n    return result;\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int W = 1000, D, N;\n    cin >> W >> D >> N;\n\n    vector<vector<int>> areas(D, vector<int>(N));\n    for (int d = 0; d < D; d++) {\n        for (int k = 0; k < N; k++) {\n            cin >> areas[d][k];\n        }\n    }\n\n    vector<vector<Rect>> all_rects(D);\n\n    all_rects[0] = pack_day(W, areas[0], vector<Rect>());\n\n    for (int d = 1; d < D; d++) {\n        all_rects[d] = pack_day(W, areas[d], all_rects[d-1]);\n    }\n\n    for (int d = 0; d < D; d++) {\n        for (int k = 0; k < N; k++) {\n            Rect r = all_rects[d][k];\n            cout << r.y1 << \" \" << r.x1 << \" \" << r.y2 << \" \" << r.x2 << '\\n';\n        }\n    }\n\n    return 0;\n}","ahc032":"#include <iostream>\n#include <vector>\n#include <tuple>\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>> a(N, vector<long long>(N));\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cin >> a[i][j];\n        }\n    }\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<vector<long long>> board(N, vector<long long>(N, 0));\n    vector<tuple<int, int, int>> operations;\n\n    for (int step = 0; step < K; step++) {\n        long long best_gain = 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 gain = 0;\n                    for (int di = 0; di < 3; di++) {\n                        for (int dj = 0; dj < 3; dj++) {\n                            int i = p + di;\n                            int j = q + dj;\n                            long long stamp_val = stamps[m][di][dj];\n                            long long old_val = a[i][j] + board[i][j];\n                            long long old_mod = old_val % MOD;\n                            long long new_val = old_val + stamp_val;\n                            long long new_mod = new_val % MOD;\n                            gain += (new_mod - old_mod);\n                        }\n                    }\n                    if (gain > best_gain) {\n                        best_gain = gain;\n                        best_m = m;\n                        best_p = p;\n                        best_q = q;\n                    }\n                }\n            }\n        }\n\n        if (best_gain <= 0) {\n            break;\n        }\n\n        operations.emplace_back(best_m, best_p, best_q);\n        for (int di = 0; di < 3; di++) {\n            for (int dj = 0; dj < 3; dj++) {\n                int i = best_p + di;\n                int j = best_q + dj;\n                board[i][j] += stamps[best_m][di][dj];\n            }\n        }\n    }\n\n    cout << operations.size() << '\\n';\n    for (auto [m, p, q] : operations) {\n        cout << m << ' ' << p << ' ' << q << '\\n';\n    }\n\n    return 0;\n}","ahc033":"#include <bits/stdc++.h>\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        return x == other.x && y == other.y;\n    }\n};\n\nchar compute_next_move(Point start, Point goal, const vector<vector<int>>& grid, bool is_carrying, bool is_large) {\n    if (start == goal) {\n        return '.';\n    }\n    \n    vector<vector<bool>> visited(5, vector<bool>(5, false));\n    vector<vector<Point>> parent(5, vector<Point>(5, Point(-1, -1)));\n    queue<Point> q;\n    q.push(start);\n    visited[start.x][start.y] = true;\n    \n    const vector<int> dx = {-1, 1, 0, 0};\n    const vector<int> dy = {0, 0, -1, 1};\n    const vector<char> dirs = {'U', 'D', 'L', 'R'};\n    bool found = false;\n    \n    while (!q.empty()) {\n        Point p = q.front();\n        q.pop();\n        if (p.x == goal.x && p.y == goal.y) {\n            found = true;\n            break;\n        }\n        \n        for (int d = 0; d < 4; d++) {\n            int nx = p.x + dx[d];\n            int ny = p.y + dy[d];\n            if (nx < 0 || nx >= 5 || ny < 0 || ny >= 5) continue;\n            if (visited[nx][ny]) continue;\n            \n            bool allowed;\n            if (is_carrying) {\n                if (is_large) {\n                    allowed = true;\n                } else {\n                    allowed = (grid[nx][ny] == -1);\n                }\n            } else {\n                allowed = true;\n            }\n            \n            if (allowed) {\n                visited[nx][ny] = true;\n                parent[nx][ny] = p;\n                q.push(Point(nx, ny));\n            }\n        }\n    }\n    \n    if (!found) {\n        return '.';\n    }\n    \n    Point p = goal;\n    while (!(parent[p.x][p.y] == start)) {\n        p = parent[p.x][p.y];\n    }\n    \n    int diff_x = p.x - start.x;\n    int diff_y = p.y - start.y;\n    if (diff_x == -1) return 'U';\n    if (diff_x == 1) return 'D';\n    if (diff_y == -1) return 'L';\n    if (diff_y == 1) return 'R';\n    return '.';\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \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<vector<int>> grid(5, vector<int>(5, -1));\n    vector<int> next_arrival_index(5, 0);\n    vector<int> dispatched(5, 0);\n    vector<int> next_expected(5);\n    for (int i = 0; i < 5; i++) {\n        next_expected[i] = 5 * i;\n    }\n    \n    struct Crane {\n        Point pos;\n        int hold;\n    };\n    vector<Crane> cranes(5);\n    for (int i = 0; i < 5; i++) {\n        cranes[i] = {{i, 0}, -1};\n    }\n    \n    vector<string> output_lines(5);\n    \n    for (int turn = 0; turn < 10000; turn++) {\n        for (int i = 0; i < 5; i++) {\n            if (next_arrival_index[i] < 5) {\n                bool can_place = true;\n                if (grid[i][0] != -1) {\n                    can_place = false;\n                } else {\n                    for (int k = 0; k < 5; k++) {\n                        if (cranes[k].pos.x == i && cranes[k].pos.y == 0 && cranes[k].hold != -1) {\n                            can_place = false;\n                            break;\n                        }\n                    }\n                }\n                if (can_place) {\n                    int b = A[i][next_arrival_index[i]];\n                    grid[i][0] = b;\n                    next_arrival_index[i]++;\n                }\n            }\n        }\n        \n        vector<Point> old_positions(5);\n        for (int i = 0; i < 5; i++) {\n            old_positions[i] = cranes[i].pos;\n        }\n        \n        vector<char> actions(5, '.');\n        vector<Point> new_positions(5);\n        for (int i = 0; i < 5; i++) {\n            new_positions[i] = old_positions[i];\n        }\n        \n        for (int i = 0; i < 5; i++) {\n            bool is_large = (i == 0);\n            bool is_carrying = (cranes[i].hold != -1);\n            \n            if (is_carrying) {\n                int b = cranes[i].hold;\n                int target_row = b / 5;\n                Point target(target_row, 4);\n                if (old_positions[i].x == target_row && old_positions[i].y == 4) {\n                    actions[i] = 'Q';\n                    new_positions[i] = old_positions[i];\n                } else {\n                    char dir = compute_next_move(old_positions[i], target, grid, is_carrying, is_large);\n                    if (dir != '.') {\n                        int nx = old_positions[i].x;\n                        int ny = old_positions[i].y;\n                        if (dir == 'U') nx--;\n                        else if (dir == 'D') nx++;\n                        else if (dir == 'L') ny--;\n                        else if (dir == 'R') ny++;\n                        new_positions[i] = Point(nx, ny);\n                        actions[i] = dir;\n                    } else {\n                        actions[i] = '.';\n                        new_positions[i] = old_positions[i];\n                    }\n                }\n            } else {\n                if (grid[old_positions[i].x][old_positions[i].y] != -1) {\n                    int b = grid[old_positions[i].x][old_positions[i].y];\n                    int target_row = b / 5;\n                    if (b == next_expected[target_row]) {\n                        actions[i] = 'P';\n                        new_positions[i] = old_positions[i];\n                    } else {\n                        actions[i] = 'P';\n                        new_positions[i] = old_positions[i];\n                    }\n                } else {\n                    if (is_large) {\n                        Point goal(-1, -1);\n                        for (int x = 0; x < 5; x++) {\n                            for (int y = 0; y < 5; y++) {\n                                if (grid[x][y] != -1) {\n                                    int b = grid[x][y];\n                                    int expected_b = 5 * (b / 5) + dispatched[b / 5];\n                                    if (b == expected_b) {\n                                        goal = Point(x, y);\n                                        break;\n                                    }\n                                }\n                            }\n                            if (goal.x != -1) break;\n                        }\n                        if (goal.x == -1) {\n                            goal = Point(2, 2);\n                        }\n                        char dir = compute_next_move(old_positions[i], goal, grid, is_carrying, is_large);\n                        if (dir != '.') {\n                            int nx = old_positions[i].x;\n                            int ny = old_positions[i].y;\n                            if (dir == 'U') nx--;\n                            else if (dir == 'D') nx++;\n                            else if (dir == 'L') ny--;\n                            else if (dir == 'R') ny++;\n                            new_positions[i] = Point(nx, ny);\n                            actions[i] = dir;\n                        } else {\n                            actions[i] = '.';\n                            new_positions[i] = old_positions[i];\n                        }\n                    } else {\n                        if (grid[old_positions[i].x][1] == -1 && old_positions[i].y < 4) {\n                            actions[i] = 'R';\n                            new_positions[i] = Point(old_positions[i].x, old_positions[i].y + 1);\n                        } else {\n                            actions[i] = '.';\n                            new_positions[i] = old_positions[i];\n                        }\n                    }\n                }\n            }\n            \n            bool valid = true;\n            for (int j = 0; j < i; j++) {\n                if (new_positions[i] == new_positions[j]) {\n                    valid = false;\n                    break;\n                }\n                if (new_positions[i] == old_positions[j] && new_positions[j] == old_positions[i]) {\n                    valid = false;\n                    break;\n                }\n            }\n            if (!valid) {\n                actions[i] = '.';\n                new_positions[i] = old_positions[i];\n            }\n        }\n        \n        for (int i = 0; i < 5; i++) {\n            output_lines[i] += actions[i];\n        }\n        \n        for (int i = 0; i < 5; i++) {\n            if (actions[i] == 'P' && grid[old_positions[i].x][old_positions[i].y] != -1) {\n                int b = grid[old_positions[i].x][old_positions[i].y];\n                cranes[i].hold = b;\n                grid[old_positions[i].x][old_positions[i].y] = -1;\n            } else if (actions[i] == 'Q' && cranes[i].hold != -1) {\n                int b = cranes[i].hold;\n                grid[old_positions[i].x][old_positions[i].y] = b;\n                cranes[i].hold = -1;\n            } else if (actions[i] == 'U' || actions[i] == 'D' || actions[i] == 'L' || actions[i] == 'R') {\n                int dx = 0, dy = 0;\n                if (actions[i] == 'U') dx = -1;\n                else if (actions[i] == 'D') dx = 1;\n                else if (actions[i] == 'L') dy = -1;\n                else if (actions[i] == 'R') dy = 1;\n                cranes[i].pos.x = old_positions[i].x + dx;\n                cranes[i].pos.y = old_positions[i].y + dy;\n            }\n        }\n        \n        for (int i = 0; i < 5; i++) {\n            if (grid[i][4] != -1) {\n                int b = grid[i][4];\n                int expected_val = 5 * i + dispatched[i];\n                if (b == expected_val) {\n                    dispatched[i]++;\n                    next_expected[i] = 5 * i + dispatched[i];\n                }\n                grid[i][4] = -1;\n            }\n        }\n        \n        int total_dispatched = 0;\n        for (int i = 0; i < 5; i++) {\n            total_dispatched += dispatched[i];\n        }\n        if (total_dispatched == 25) {\n            break;\n        }\n    }\n    \n    int max_len = 0;\n    for (int i = 0; i < 5; i++) {\n        if ((int)output_lines[i].size() > max_len) {\n            max_len = output_lines[i].size();\n        }\n    }\n    for (int i = 0; i < 5; i++) {\n        while ((int)output_lines[i].size() < max_len) {\n            output_lines[i] += '.';\n        }\n    }\n    \n    for (int i = 0; i < 5; i++) {\n        cout << output_lines[i] << '\\n';\n    }\n    \n    return 0;\n}","ahc034":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    const int N = 20;\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<int>> remaining_source(N, vector<int>(N, 0));\n    vector<vector<int>> remaining_sink(N, vector<int>(N, 0));\n\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (grid[i][j] > 0) {\n                remaining_source[i][j] = grid[i][j];\n            } else if (grid[i][j] < 0) {\n                remaining_sink[i][j] = -grid[i][j];\n            }\n        }\n    }\n\n    int cur_i = 0, cur_j = 0;\n    int cur_load = 0;\n\n    auto has_remaining = [&]() -> bool {\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                if (remaining_source[i][j] > 0 || remaining_sink[i][j] > 0) {\n                    return true;\n                }\n            }\n        }\n        return false;\n    };\n\n    while (has_remaining()) {\n        bool handled = false;\n\n        if (remaining_source[cur_i][cur_j] > 0) {\n            int d = remaining_source[cur_i][cur_j];\n            cout << \"+\" << d << \"\\n\";\n            cur_load += d;\n            remaining_source[cur_i][cur_j] = 0;\n            handled = true;\n        }\n        else if (remaining_sink[cur_i][cur_j] > 0 && cur_load > 0) {\n            int d = min(remaining_sink[cur_i][cur_j], cur_load);\n            cout << \"-\" << d << \"\\n\";\n            cur_load -= d;\n            remaining_sink[cur_i][cur_j] -= d;\n            handled = true;\n        }\n\n        if (handled) {\n            continue;\n        }\n\n        int best_i = -1, best_j = -1;\n        int min_score = INT_MAX;\n\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                if (remaining_source[i][j] > 0) {\n                    int dist = abs(i - cur_i) + abs(j - cur_j);\n                    int score = dist * (100 + cur_load);\n                    if (score < min_score) {\n                        min_score = score;\n                        best_i = i;\n                        best_j = j;\n                    }\n                }\n                if (remaining_sink[i][j] > 0 && cur_load > 0) {\n                    int dist = abs(i - cur_i) + abs(j - cur_j);\n                    int score = dist * (100 + cur_load);\n                    if (score < min_score) {\n                        min_score = score;\n                        best_i = i;\n                        best_j = j;\n                    }\n                }\n            }\n        }\n\n        if (best_i == -1) {\n            break;\n        }\n\n        int dx = best_i - cur_i;\n        int dy = best_j - cur_j;\n        string moves = \"\";\n        if (dx > 0) moves += string(dx, 'D');\n        else if (dx < 0) moves += string(-dx, 'U');\n        if (dy > 0) moves += string(dy, 'R');\n        else if (dy < 0) moves += string(-dy, 'L');\n\n        for (char c : moves) {\n            cout << c << \"\\n\";\n            if (c == 'U') cur_i--;\n            else if (c == 'D') cur_i++;\n            else if (c == 'L') cur_j--;\n            else if (c == 'R') cur_j++;\n        }\n    }\n\n    return 0;\n}","ahc035":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Seed {\n    long long V;\n    double total_ratio;\n    int count_max;\n    vector<int> values;\n};\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, T;\n    cin >> N >> M >> T;\n\n    int seed_count = 2 * N * (N - 1);\n    vector<Seed> seeds(seed_count);\n\n    // Read initial seeds and compute X\n    vector<int> X(M, 0);\n    for (int k = 0; k < seed_count; k++) {\n        seeds[k].values.resize(M);\n        seeds[k].V = 0;\n        seeds[k].total_ratio = 0.0;\n        seeds[k].count_max = 0;\n        for (int l = 0; l < M; l++) {\n            cin >> seeds[k].values[l];\n            seeds[k].V += seeds[k].values[l];\n            if (seeds[k].values[l] > X[l]) {\n                X[l] = seeds[k].values[l];\n            }\n        }\n        // Compute initial ratios and count max\n        for (int l = 0; l < M; l++) {\n            double ratio = (X[l] == 0) ? 1.0 : (double)seeds[k].values[l] / X[l];\n            seeds[k].total_ratio += ratio;\n            if (seeds[k].values[l] == X[l]) {\n                seeds[k].count_max++;\n            }\n        }\n    }\n\n    // Grid positions with degree and distance to center\n    vector<tuple<int, int, int, double>> positions;\n    double center = (N - 1) / 2.0;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            int deg = 4;\n            if (i == 0 || i == N-1) deg--;\n            if (j == 0 || j == N-1) deg--;\n            double dist = (i - center) * (i - center) + (j - center) * (j - center);\n            positions.emplace_back(i, j, deg, dist);\n        }\n    }\n\n    // Sort positions: primary by degree descending, then by distance to center ascending\n    sort(positions.begin(), positions.end(), [](const auto& a, const auto& b) {\n        if (get<2>(a) != get<2>(b)) return get<2>(a) > get<2>(b);\n        return get<3>(a) < get<3>(b);\n    });\n\n    for (int t = 0; t < T; t++) {\n        // Compute comprehensive scores for each seed\n        vector<tuple<double, double, int, long long, int>> score_list;\n        for (int k = 0; k < seed_count; k++) {\n            score_list.emplace_back(\n                seeds[k].total_ratio,\n                (double)seeds[k].count_max / M,\n                seeds[k].count_max,\n                seeds[k].V,\n                k\n            );\n        }\n\n        // Sort seeds: primary by total_ratio, secondary by count_max ratio, tertiary by V\n        sort(score_list.begin(), score_list.end(), [](const auto& a, const 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<3>(a) > get<3>(b);\n        });\n\n        // Create a mapping from seed index to its rank\n        vector<int> seed_rank(seed_count);\n        for (int rank = 0; rank < seed_count; rank++) {\n            seed_rank[get<4>(score_list[rank])] = rank;\n        }\n\n        // Grid assignment: try to place complementary seeds near each other\n        vector<vector<int>> grid(N, vector<int>(N, -1));\n        vector<bool> used(seed_count, false);\n\n        // First assign seeds to high-degree positions\n        for (int idx = 0; idx < N * N; idx++) {\n            auto [i, j, deg, dist] = positions[idx];\n            int best_seed = -1;\n            double best_score = -1;\n\n            // First try to find a seed that complements existing neighbors\n            for (int k = 0; k < seed_count; k++) {\n                if (!used[k]) {\n                    double comp_score = 0.0;\n                    vector<pair<int, int>> neighbors = {{i-1, j}, {i+1, j}, {i, j-1}, {i, j+1}};\n                    for (auto [ni, nj] : neighbors) {\n                        if (ni >= 0 && ni < N && nj >= 0 && nj < N && grid[ni][nj] != -1) {\n                            int other_seed = grid[ni][nj];\n                            for (int l = 0; l < M; l++) {\n                                if (seeds[k].values[l] == X[l] && seeds[other_seed].values[l] < X[l]) {\n                                    comp_score += 1.0;\n                                } else if (seeds[other_seed].values[l] == X[l] && seeds[k].values[l] < X[l]) {\n                                    comp_score += 1.0;\n                                }\n                            }\n                        }\n                    }\n                    double total = comp_score / (M * 4.0 + 1e-9);\n                    if (total > best_score) {\n                        best_score = total;\n                        best_seed = k;\n                    }\n                }\n            }\n\n            // If no complementary seed found, pick the best available\n            if (best_seed == -1) {\n                for (int k = 0; k < seed_count; k++) {\n                    if (!used[k] && (best_seed == -1 || seed_rank[k] < seed_rank[best_seed])) {\n                        best_seed = k;\n                    }\n                }\n            }\n\n            grid[i][j] = best_seed;\n            used[best_seed] = true;\n        }\n\n        // Output the grid\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 << '\\n';\n        }\n        cout.flush();\n\n        // Read new seeds\n        for (int k = 0; k < seed_count; k++) {\n            seeds[k].V = 0;\n            seeds[k].total_ratio = 0.0;\n            seeds[k].count_max = 0;\n            for (int l = 0; l < M; l++) {\n                cin >> seeds[k].values[l];\n                seeds[k].V += seeds[k].values[l];\n                if (seeds[k].values[l] > X[l]) {\n                    X[l] = seeds[k].values[l];\n                }\n            }\n            // Update ratios and count max\n            for (int l = 0; l < M; l++) {\n                double ratio = (X[l] == 0) ? 1.0 : (double)seeds[k].values[l] / X[l];\n                seeds[k].total_ratio += ratio;\n                if (seeds[k].values[l] == X[l]) {\n                    seeds[k].count_max++;\n                }\n            }\n        }\n    }\n\n    return 0;\n}","ahc038":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <cctype>\n#include <cmath>\n#include <string>\n#include <map>\n#include <set>\n#include <queue>\n#include <stack>\n#include <utility>\n#include <climits>\nusing namespace std;\n\ntypedef pair<int, int> Point;\n\nvector<char> get_manhattan_path(Point from, Point to) {\n    vector<char> path;\n    int dx = to.first - from.first;\n    int dy = to.second - from.second;\n    char x_dir = 0;\n    char y_dir = 0;\n    if (dx > 0) x_dir = 'D';\n    else if (dx < 0) x_dir = 'U';\n    if (dy > 0) y_dir = 'R';\n    else if (dy < 0) y_dir = 'L';\n\n    for (int i = 0; i < abs(dx); i++) {\n        path.push_back(x_dir);\n    }\n    for (int i = 0; i < abs(dy); i++) {\n        path.push_back(y_dir);\n    }\n    return path;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, V;\n    cin >> N >> M >> V;\n\n    vector<string> s_grid(N);\n    vector<string> 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    vector<Point> A;\n    vector<Point> B;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (s_grid[i][j] == '1') {\n                A.push_back({i, j});\n            }\n            if (t_grid[i][j] == '1') {\n                B.push_back({i, j});\n            }\n        }\n    }\n\n    sort(A.begin(), A.end());\n    sort(B.begin(), B.end());\n\n    int V_prime = 5;\n    cout << V_prime << \"\\n\";\n    for (int u = 1; u < V_prime; u++) {\n        cout << 0 << \" \" << 1 << \"\\n\";\n    }\n    cout << 0 << \" \" << 0 << \"\\n\";\n\n    vector<string> operations;\n\n    operations.push_back(\".\" + string(\"RRRL\") + string(\".....\"));\n    operations.push_back(\".\" + string(\"..R.\") + string(\".....\"));\n\n    Point current_root = {0, 0};\n\n    vector<pair<Point, Point>> valid_pairs;\n    for (int i = 0; i < M; i++) {\n        if (A[i] != B[i]) {\n            valid_pairs.push_back({A[i], B[i]});\n        }\n    }\n\n    auto get_options = [&](int i, int j) -> vector<pair<int, Point>> {\n        vector<pair<int, Point>> options;\n        if (j - 1 >= 0) {\n            options.push_back({1, {i, j - 1}});\n        }\n        if (i - 1 >= 0) {\n            options.push_back({2, {i - 1, j}});\n        }\n        if (j + 1 < N) {\n            options.push_back({3, {i, j + 1}});\n        }\n        if (i + 1 < N) {\n            options.push_back({4, {i + 1, j}});\n        }\n        return options;\n    };\n\n    for (auto &p : valid_pairs) {\n        Point a = p.first;\n        Point b = p.second;\n\n        auto options_a = get_options(a.first, a.second);\n        if (options_a.empty()) {\n            continue;\n        }\n        int best_u = -1;\n        Point best_R_a;\n        int min_dist = INT_MAX;\n        for (auto &opt : options_a) {\n            int u_idx = opt.first;\n            Point R = opt.second;\n            int dist = abs(current_root.first - R.first) + abs(current_root.second - R.second);\n            if (dist < min_dist) {\n                min_dist = dist;\n                best_u = u_idx;\n                best_R_a = R;\n            }\n        }\n        Point R_a = best_R_a;\n\n        auto options_b = get_options(b.first, b.second);\n        if (options_b.empty()) {\n            continue;\n        }\n        auto best_b = options_b[0];\n        int v_rel = best_b.first;\n        Point R_b = best_b.second;\n\n        vector<char> path1 = get_manhattan_path(current_root, R_a);\n        if (path1.empty()) {\n            string action_str = \".....\";\n            action_str[0] = '.';\n            if (best_u == 1) action_str[1] = 'P';\n            else if (best_u == 2) action_str[2] = 'P';\n            else if (best_u == 3) action_str[3] = 'P';\n            else if (best_u == 4) action_str[4] = 'P';\n            operations.push_back(\".\" + string(\"....\") + action_str);\n        } else {\n            for (int idx = 0; idx < path1.size(); idx++) {\n                string rot_str = \"....\";\n                string action_str = \".....\";\n                if (idx == path1.size() - 1) {\n                    action_str[0] = '.';\n                    if (best_u == 1) action_str[1] = 'P';\n                    else if (best_u == 2) action_str[2] = 'P';\n                    else if (best_u == 3) action_str[3] = 'P';\n                    else if (best_u == 4) action_str[4] = 'P';\n                }\n                string op = string(1, path1[idx]) + rot_str + action_str;\n                operations.push_back(op);\n            }\n        }\n\n        vector<char> path2 = get_manhattan_path(R_a, R_b);\n        if (path2.empty()) {\n            string action_str = \".....\";\n            action_str[0] = '.';\n            if (v_rel == 1) action_str[1] = 'P';\n            else if (v_rel == 2) action_str[2] = 'P';\n            else if (v_rel == 3) action_str[3] = 'P';\n            else if (v_rel == 4) action_str[4] = 'P';\n            operations.push_back(\".\" + string(\"....\") + action_str);\n        } else {\n            for (int idx = 0; idx < path2.size(); idx++) {\n                string rot_str = \"....\";\n                string action_str = \".....\";\n                if (idx == path2.size() - 1) {\n                    action_str[0] = '.';\n                    if (v_rel == 1) action_str[1] = 'P';\n                    else if (v_rel == 2) action_str[2] = 'P';\n                    else if (v_rel == 3) action_str[3] = 'P';\n                    else if (v_rel == 4) action_str[4] = 'P';\n                }\n                string op = string(1, path2[idx]) + rot_str + action_str;\n                operations.push_back(op);\n            }\n        }\n\n        current_root = R_b;\n    }\n\n    for (string &op : operations) {\n        cout << op << \"\\n\";\n    }\n\n    return 0;\n}","ahc039":"#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\n\nstruct Point {\n    int x, y;\n    bool operator==(const Point& other) const {\n        return x == other.x && y == other.y;\n    }\n    bool operator<(const Point& other) const {\n        return x < other.x || (x == other.x && y < other.y);\n    }\n};\n\nstruct Rect {\n    int x1, x2, y1, y2;\n    Rect() : x1(0), x2(0), y1(0), y2(0) {}\n    Rect(int a, int b, int c, int d) {\n        x1 = min(a, b); x2 = max(a, b);\n        y1 = min(c, d); y2 = max(c, d);\n    }\n    int width() const { return x2 - x1; }\n    int height() const { return y2 - y1; }\n    bool valid() const { return x1 < x2 && y1 < y2; }\n};\n\nconst int N = 5000;\nvector<Point> mackerels(N), sardines(N);\nvector<int> xs, ys;\nvector<vector<int>> grid_val;\nvector<vector<ll>> P;\nvector<int> x_to_idx(100001, -1);\nvector<int> y_to_idx(100001, -1);\n\nvoid preprocess() {\n    xs.clear(); ys.clear();\n    for (int i = 0; i < N; i++) {\n        xs.push_back(mackerels[i].x);\n        ys.push_back(mackerels[i].y);\n        xs.push_back(sardines[i].x);\n        ys.push_back(sardines[i].y);\n    }\n    sort(xs.begin(), xs.end());\n    xs.erase(unique(xs.begin(), xs.end()), xs.end());\n    sort(ys.begin(), ys.end());\n    ys.erase(unique(ys.begin(), ys.end()), ys.end());\n\n    grid_val.assign(xs.size(), vector<int>(ys.size(), 0));\n    for (int i = 0; i < N; i++) {\n        int ix = lower_bound(xs.begin(), xs.end(), mackerels[i].x) - xs.begin();\n        int iy = lower_bound(ys.begin(), ys.end(), mackerels[i].y) - ys.begin();\n        if (ix < (int)xs.size() && iy < (int)ys.size()) grid_val[ix][iy] = 1;\n    }\n    for (int i = 0; i < N; i++) {\n        int ix = lower_bound(xs.begin(), xs.end(), sardines[i].x) - xs.begin();\n        int iy = lower_bound(ys.begin(), ys.end(), sardines[i].y) - ys.begin();\n        if (ix < (int)xs.size() && iy < (int)ys.size()) grid_val[ix][iy] -= 1;\n    }\n\n    P.assign(xs.size()+1, vector<ll>(ys.size()+1, 0));\n    for (int i = 0; i < (int)xs.size(); i++) {\n        for (int j = 0; j < (int)ys.size(); j++) {\n            P[i+1][j+1] = P[i][j+1] + P[i+1][j] - P[i][j] + grid_val[i][j];\n        }\n    }\n\n    for (int i = 0; i <= 100000; i++) {\n        x_to_idx[i] = lower_bound(xs.begin(), xs.end(), i) - xs.begin();\n        y_to_idx[i] = lower_bound(ys.begin(), ys.end(), i) - ys.begin();\n    }\n}\n\nll get_sum(int x1, int x2, int y1, int y2) {\n    if (x1 > x2 || y1 > y2) return 0;\n    int i1 = max(0, x_to_idx[x1]);\n    int i2 = min((int)xs.size()-1, x_to_idx[x2]);\n    int j1 = max(0, y_to_idx[y1]);\n    int j2 = min((int)ys.size()-1, y_to_idx[y2]);\n    if (i1 > i2 || j1 > j2) return 0;\n    return P[i2+1][j2+1] - P[i1][j2+1] - P[i2+1][j1] + P[i1][j1];\n}\n\nRect shrink_rect(Rect r) {\n    int x1 = r.x1, x2 = r.x2, y1 = r.y1, y2 = r.y2;\n    bool improved;\n    do {\n        improved = false;\n        if (x2 > x1) {\n            ll strip = get_sum(x1, x1, y1, y2);\n            if (strip < 0) { x1++; improved = true; }\n        }\n        if (!improved && x2 > x1) {\n            ll strip = get_sum(x2, x2, y1, y2);\n            if (strip < 0) { x2--; improved = true; }\n        }\n        if (!improved && y2 > y1) {\n            ll strip = get_sum(x1, x2, y1, y1);\n            if (strip < 0) { y1++; improved = true; }\n        }\n        if (!improved && y2 > y1) {\n            ll strip = get_sum(x1, x2, y2, y2);\n            if (strip < 0) { y2--; improved = true; }\n        }\n    } while (improved);\n    return Rect(x1, x2, y1, y2);\n}\n\nvector<Point> make_rect_polygon(Rect r) {\n    return {\n        {r.x1, r.y1},\n        {r.x2, r.y1},\n        {r.x2, r.y2},\n        {r.x1, r.y2}\n    };\n}\n\nvector<Point> make_notched_rect(Rect outer, int notch_dir, int notch_size) {\n    vector<Point> poly;\n    if (notch_dir == 0) { // Top notch\n        poly.push_back({outer.x1, outer.y1});\n        poly.push_back({outer.x2, outer.y1});\n        poly.push_back({outer.x2, outer.y1 + notch_size});\n        poly.push_back({outer.x1 + notch_size, outer.y1 + notch_size});\n        poly.push_back({outer.x1 + notch_size, outer.y2});\n        poly.push_back({outer.x1, outer.y2});\n    } else if (notch_dir == 1) { // Bottom notch\n        poly.push_back({outer.x1, outer.y1});\n        poly.push_back({outer.x2, outer.y1});\n        poly.push_back({outer.x2, outer.y2});\n        poly.push_back({outer.x1, outer.y2});\n        poly.push_back({outer.x1, outer.y2 - notch_size});\n        poly.push_back({outer.x2 - notch_size, outer.y2 - notch_size});\n    } else if (notch_dir == 2) { // Left notch\n        poly.push_back({outer.x1, outer.y1});\n        poly.push_back({outer.x2, outer.y1});\n        poly.push_back({outer.x2, outer.y2});\n        poly.push_back({outer.x2 - notch_size, outer.y2});\n        poly.push_back({outer.x2 - notch_size, outer.y1 + notch_size});\n        poly.push_back({outer.x1, outer.y1 + notch_size});\n    } else { // Right notch\n        poly.push_back({outer.x1, outer.y1});\n        poly.push_back({outer.x2 - notch_size, outer.y1});\n        poly.push_back({outer.x2 - notch_size, outer.y1 + notch_size});\n        poly.push_back({outer.x2, outer.y1 + notch_size});\n        poly.push_back({outer.x2, outer.y2});\n        poly.push_back({outer.x1, outer.y2});\n    }\n    return poly;\n}\n\nvector<Point> make_u_shape(Rect outer, int open_side, int notch_size) {\n    vector<Point> poly;\n    if (open_side == 0) { // Top open\n        poly.push_back({outer.x1, outer.y1 + notch_size});\n        poly.push_back({outer.x2, outer.y1 + notch_size});\n        poly.push_back({outer.x2, outer.y2});\n        poly.push_back({outer.x1, outer.y2});\n    } else if (open_side == 1) { // Bottom open\n        poly.push_back({outer.x1, outer.y1});\n        poly.push_back({outer.x2, outer.y1});\n        poly.push_back({outer.x2, outer.y2 - notch_size});\n        poly.push_back({outer.x1, outer.y2 - notch_size});\n    } else if (open_side == 2) { // Left open\n        poly.push_back({outer.x1 + notch_size, outer.y1});\n        poly.push_back({outer.x2, outer.y1});\n        poly.push_back({outer.x2, outer.y2});\n        poly.push_back({outer.x1 + notch_size, outer.y2});\n    } else { // Right open\n        poly.push_back({outer.x1, outer.y1});\n        poly.push_back({outer.x2 - notch_size, outer.y1});\n        poly.push_back({outer.x2 - notch_size, outer.y2});\n        poly.push_back({outer.x1, outer.y2});\n    }\n    return poly;\n}\n\nvector<Point> make_l_shape(Rect outer, Rect inner) {\n    if (!inner.valid()) return make_rect_polygon(outer);\n    vector<Point> poly;\n    poly.push_back({outer.x1, outer.y1});\n    poly.push_back({outer.x2, outer.y1});\n    poly.push_back({outer.x2, outer.y2});\n    poly.push_back({inner.x2, outer.y2});\n    poly.push_back({inner.x2, inner.y2});\n    poly.push_back({inner.x1, inner.y2});\n    poly.push_back({inner.x1, inner.y1});\n    poly.push_back({outer.x1, inner.y1});\n    return poly;\n}\n\nvector<Point> make_cross_shape(Rect outer, Rect inner) {\n    if (!inner.valid()) return make_rect_polygon(outer);\n    vector<Point> poly;\n    poly.push_back({outer.x1, outer.y1});\n    poly.push_back({outer.x2, outer.y1});\n    poly.push_back({outer.x2, inner.y1});\n    poly.push_back({inner.x2, inner.y1});\n    poly.push_back({inner.x2, outer.y1});\n    poly.push_back({inner.x2, outer.y2});\n    poly.push_back({inner.x1, outer.y2});\n    poly.push_back({inner.x1, inner.y2});\n    poly.push_back({outer.x1, inner.y2});\n    poly.push_back({outer.x1, outer.y2});\n    return poly;\n}\n\nll evaluate_polygon(const vector<Point>& poly) {\n    if (poly.size() < 3) return 0;\n    int xmin = poly[0].x, xmax = poly[0].x;\n    int ymin = poly[0].y, ymax = poly[0].y;\n    for (const auto& p : poly) {\n        xmin = min(xmin, p.x);\n        xmax = max(xmax, p.x);\n        ymin = min(ymin, p.y);\n        ymax = max(ymax, p.y);\n    }\n    return get_sum(xmin, xmax, ymin, ymax);\n}\n\nvector<Rect> find_high_value_rects() {\n    const int grid_size = 1000;\n    vector<vector<ll>> mack_count(grid_size, vector<ll>(grid_size, 0));\n    vector<vector<ll>> sard_count(grid_size, vector<ll>(grid_size, 0));\n\n    for (const auto& p : mackerels) {\n        int gx = p.x / grid_size;\n        int gy = p.y / grid_size;\n        if (gx >= 0 && gx < grid_size && gy >= 0 && gy < grid_size) {\n            mack_count[gx][gy]++;\n        }\n    }\n    for (const auto& p : sardines) {\n        int gx = p.x / grid_size;\n        int gy = p.y / grid_size;\n        if (gx >= 0 && gx < grid_size && gy >= 0 && gy < grid_size) {\n            sard_count[gx][gy]++;\n        }\n    }\n\n    vector<Rect> rects;\n    for (int gx = 0; gx < grid_size; gx++) {\n        for (int gy = 0; gy < grid_size; gy++) {\n            if (mack_count[gx][gy] > 0) {\n                ll net = mack_count[gx][gy] - sard_count[gx][gy];\n                if (net > 0) {\n                    int x1 = max(0, gx * grid_size - 1000);\n                    int x2 = min(100000, (gx + 1) * grid_size + 1000);\n                    int y1 = max(0, gy * grid_size - 1000);\n                    int y2 = min(100000, (gy + 1) * grid_size + 1000);\n                    rects.emplace_back(x1, x2, y1, y2);\n                }\n            }\n        }\n    }\n\n    // Also add some expanded versions\n    vector<Rect> expanded = rects;\n    for (auto& r : rects) {\n        int x1 = max(0, r.x1 - 2000);\n        int x2 = min(100000, r.x2 + 2000);\n        int y1 = max(0, r.y1 - 2000);\n        int y2 = min(100000, r.y2 + 2000);\n        expanded.emplace_back(x1, x2, y1, y2);\n    }\n    rects.insert(rects.end(), expanded.begin(), expanded.end());\n    return rects;\n}\n\nvector<Point> optimize_polygon(vector<Point> poly) {\n    int xmin = poly[0].x, xmax = poly[0].x;\n    int ymin = poly[0].y, ymax = poly[0].y;\n    for (const auto& p : poly) {\n        xmin = min(xmin, p.x);\n        xmax = max(xmax, p.x);\n        ymin = min(ymin, p.y);\n        ymax = max(ymax, p.y);\n    }\n\n    for (int dx = -1000; dx <= 1000; dx += 100) {\n        for (int dy = -1000; dy <= 1000; dy += 100) {\n            Rect new_rect(\n                max(0, xmin + dx),\n                min(100000, xmax + dx),\n                max(0, ymin + dy),\n                min(100000, ymax + dy)\n            );\n            if (new_rect.valid()) {\n                vector<Point> new_poly = make_rect_polygon(new_rect);\n                ll new_score = evaluate_polygon(new_poly);\n                ll orig_score = evaluate_polygon(poly);\n                if (new_score > orig_score) {\n                    poly = new_poly;\n                    xmin = new_rect.x1;\n                    xmax = new_rect.x2;\n                    ymin = new_rect.y1;\n                    ymax = new_rect.y2;\n                }\n            }\n        }\n    }\n\n    return poly;\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    for (int i = 0; i < N; i++) {\n        cin >> mackerels[i].x >> mackerels[i].y;\n    }\n    for (int i = 0; i < N; i++) {\n        cin >> sardines[i].x >> sardines[i].y;\n    }\n\n    preprocess();\n\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n\n    vector<Point> best_poly;\n    ll best_score = -1e18;\n\n    vector<Rect> candidates = find_high_value_rects();\n\n    // Add bounding box of all mackerels\n    int mx1 = 100000, mx2 = 0, my1 = 100000, my2 = 0;\n    for (auto& p : mackerels) {\n        mx1 = min(mx1, p.x);\n        mx2 = max(mx2, p.x);\n        my1 = min(my1, p.y);\n        my2 = max(my2, p.y);\n    }\n    candidates.emplace_back(mx1, mx2, my1, my2);\n\n    // Add random rectangles around mackerels\n    uniform_int_distribution<int> mackerel_dist(0, N-1);\n    uniform_int_distribution<int> size_dist(2000, 10000);\n    for (int i = 0; i < 100; i++) {\n        Point p = mackerels[mackerel_dist(rng)];\n        int size = size_dist(rng);\n        Rect r(max(0, p.x - size/2), min(100000, p.x + size/2),\n               max(0, p.y - size/2), min(100000, p.y + size/2));\n        if (r.valid()) candidates.push_back(r);\n    }\n\n    // Add random rectangles\n    uniform_int_distribution<int> coord_dist(0, 100000);\n    for (int i = 0; i < 1000; i++) {\n        int x1 = coord_dist(rng);\n        int x2 = coord_dist(rng);\n        int y1 = coord_dist(rng);\n        int y2 = coord_dist(rng);\n        if (x1 > x2) swap(x1, x2);\n        if (y1 > y2) swap(y1, y2);\n        Rect r(x1, x2, y1, y2);\n        if (r.valid()) candidates.push_back(r);\n    }\n\n    // Beam search with top 5 candidates\n    const int beam_width = 5;\n    vector<pair<ll, vector<Point>>> beam;\n\n    for (Rect r : candidates) {\n        Rect shrunk = shrink_rect(r);\n        vector<Point> poly = make_rect_polygon(shrunk);\n        ll score = evaluate_polygon(poly);\n\n        if (score > best_score) {\n            best_score = score;\n            best_poly = poly;\n        }\n\n        beam.emplace_back(score, poly);\n        if (beam.size() > beam_width) {\n            sort(beam.begin(), beam.end(), [](const auto& a, const auto& b) {\n                return a.first > b.first;\n            });\n            beam.pop_back();\n        }\n    }\n\n    // Generate more candidates from beam\n    for (int iter = 0; iter < 3; iter++) {\n        vector<pair<ll, vector<Point>>> new_beam;\n\n        for (auto& [score, poly] : beam) {\n            // Add L-shapes\n            int xmin = poly[0].x, xmax = poly[0].x;\n            int ymin = poly[0].y, ymax = poly[0].y;\n            for (const auto& p : poly) {\n                xmin = min(xmin, p.x);\n                xmax = max(xmax, p.x);\n                ymin = min(ymin, p.y);\n                ymax = max(ymax, p.y);\n            }\n            int cx = (xmin + xmax)/2;\n            int cy = (ymin + ymax)/2;\n            Rect inner(max(xmin, cx - 2000), min(xmax, cx + 2000),\n                       max(ymin, cy - 2000), min(ymax, cy + 2000));\n\n            vector<Point> l_poly = make_l_shape(Rect(xmin, xmax, ymin, ymax), inner);\n            ll l_score = evaluate_polygon(l_poly);\n            new_beam.emplace_back(l_score, l_poly);\n\n            // Add U-shapes\n            for (int open_side = 0; open_side < 4; open_side++) {\n                for (int notch_size : {1000, 2000, 3000}) {\n                    vector<Point> u_poly = make_u_shape(Rect(xmin, xmax, ymin, ymax), open_side, notch_size);\n                    ll u_score = evaluate_polygon(u_poly);\n                    new_beam.emplace_back(u_score, u_poly);\n                }\n            }\n\n            // Add cross shapes\n            vector<Point> c_poly = make_cross_shape(Rect(xmin, xmax, ymin, ymax), inner);\n            ll c_score = evaluate_polygon(c_poly);\n            new_beam.emplace_back(c_score, c_poly);\n\n            // Optimize current polygon\n            vector<Point> opt_poly = optimize_polygon(poly);\n            ll opt_score = evaluate_polygon(opt_poly);\n            new_beam.emplace_back(opt_score, opt_poly);\n        }\n\n        // Select top candidates\n        sort(new_beam.begin(), new_beam.end(), [](const auto& a, const auto& b) {\n            return a.first > b.first;\n        });\n        beam.clear();\n        for (int i = 0; i < min(beam_width, (int)new_beam.size()); i++) {\n            beam.push_back(new_beam[i]);\n            if (new_beam[i].first > best_score) {\n                best_score = new_beam[i].first;\n                best_poly = new_beam[i].second;\n            }\n        }\n    }\n\n    // Final optimization pass\n    vector<Point> final_poly = optimize_polygon(best_poly);\n    ll final_score = evaluate_polygon(final_poly);\n    if (final_score > best_score) {\n        best_poly = final_poly;\n        best_score = final_score;\n    }\n\n    // Output the best polygon found\n    cout << best_poly.size() << '\\n';\n    for (const auto& p : best_poly) {\n        cout << p.x << \" \" << p.y << '\\n';\n    }\n\n    return 0;\n}","ahc040":"#include <iostream>\n#include <vector>\n#include <tuple>\n#include <algorithm>\n#include <random>\n#include <chrono>\n#include <climits>\n#include <cmath>\n#include <cctype>\nusing namespace std;\n\nstruct Rect {\n    long long x, y, w, h;\n};\n\nstruct Instruction {\n    int p, r, b;\n    char d;\n};\n\ndouble rand_double(double min_val, double max_val, mt19937& rng) {\n    uniform_real_distribution<double> dist(min_val, max_val);\n    return dist(rng);\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    for (int i = 0; i < N; i++) {\n        long long wp, hp;\n        cin >> wp >> hp;\n        w_est[i] = static_cast<double>(wp);\n        h_est[i] = static_cast<double>(hp);\n    }\n\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n\n    const int M = 50;\n\n    for (int turn = 0; turn < T; turn++) {\n        vector<Instruction> best_instructions;\n        long long best_score = LLONG_MAX;\n\n        for (int cand_idx = 0; cand_idx < M; cand_idx++) {\n            vector<Instruction> inst_seq;\n            vector<Rect> layout;\n            long long cur_max_x = 0;\n            long long cur_max_y = 0;\n\n            vector<double> w_cand = w_est;\n            vector<double> h_cand = h_est;\n            if (cand_idx > 0) {\n                double noise_factor = 0.1 * (1.0 - static_cast<double>(cand_idx) / M);\n                for (int i = 0; i < N; i++) {\n                    double noise = 1.0 + rand_double(-noise_factor, noise_factor, rng);\n                    w_cand[i] *= noise;\n                    h_cand[i] *= noise;\n                }\n            }\n\n            bool valid_placement = true;\n            for (int i = 0; i < N; i++) {\n                vector<tuple<int, int, char, int, long long, long long, long long, long long>> options;\n\n                for (int rotation : {0, 1}) {\n                    double w_val = (rotation == 0) ? w_cand[i] : h_cand[i];\n                    double h_val = (rotation == 0) ? h_cand[i] : w_cand[i];\n                    long long w_rect = llround(w_val);\n                    long long h_rect = llround(h_val);\n                    w_rect = max(1LL, min(1000000000LL, w_rect));\n                    h_rect = max(1LL, min(1000000000LL, h_rect));\n\n                    vector<tuple<char, int>> dir_b_opts;\n                    dir_b_opts.push_back({'U', -1});\n                    if (!layout.empty()) {\n                        dir_b_opts.push_back({'U', static_cast<int>(layout.size() - 1)});\n                    }\n                    dir_b_opts.push_back({'L', -1});\n                    if (!layout.empty()) {\n                        dir_b_opts.push_back({'L', static_cast<int>(layout.size() - 1)});\n                    }\n\n                    for (auto [d, b] : dir_b_opts) {\n                        long long x_fixed, y_fixed;\n                        if (d == 'U') {\n                            if (b == -1) {\n                                x_fixed = 0;\n                            } else {\n                                x_fixed = layout[b].x + layout[b].w;\n                            }\n                            y_fixed = 0;\n                            for (const auto& rect : layout) {\n                                if (rect.x < x_fixed + w_rect && rect.x + rect.w > x_fixed) {\n                                    y_fixed = max(y_fixed, rect.y + rect.h);\n                                }\n                            }\n                        } else {\n                            if (b == -1) {\n                                y_fixed = 0;\n                            } else {\n                                y_fixed = layout[b].y + layout[b].h;\n                            }\n                            x_fixed = 0;\n                            for (const auto& rect : layout) {\n                                if (rect.y < y_fixed + h_rect && rect.y + rect.h > y_fixed) {\n                                    x_fixed = max(x_fixed, rect.x + rect.w);\n                                }\n                            }\n                        }\n                        long long new_max_x = max(cur_max_x, x_fixed + w_rect);\n                        long long new_max_y = max(cur_max_y, y_fixed + h_rect);\n                        long long score_val = new_max_x + new_max_y;\n                        options.emplace_back(rotation, b, d, score_val, x_fixed, y_fixed, w_rect, h_rect);\n                    }\n                }\n\n                if (options.empty()) {\n                    valid_placement = false;\n                    break;\n                }\n\n                auto best_opt = *min_element(options.begin(), options.end(),\n                    [](const auto& a, const auto& b) {\n                        return get<3>(a) < get<3>(b);\n                    });\n\n                int r = get<0>(best_opt);\n                int b_val = get<1>(best_opt);\n                char d_val = get<2>(best_opt);\n                long long x_val = get<4>(best_opt);\n                long long y_val = get<5>(best_opt);\n                long long w_val = get<6>(best_opt);\n                long long h_val = get<7>(best_opt);\n\n                inst_seq.push_back({i, r, b_val, d_val});\n                layout.push_back({x_val, y_val, w_val, h_val});\n                cur_max_x = max(cur_max_x, x_val + w_val);\n                cur_max_y = max(cur_max_y, y_val + h_val);\n            }\n\n            if (!valid_placement) {\n                continue;\n            }\n\n            long long total_est = cur_max_x + cur_max_y;\n            if (total_est < best_score) {\n                best_score = total_est;\n                best_instructions = inst_seq;\n            }\n        }\n\n        if (best_instructions.empty()) {\n            for (int i = 0; i < N; i++) {\n                best_instructions.push_back({i, 0, -1, 'U'});\n            }\n        }\n\n        cout << N << \"\\n\";\n        for (const auto& inst : best_instructions) {\n            cout << inst.p << \" \" << inst.r << \" \" << inst.d << \" \" << inst.b << \"\\n\";\n        }\n        cout.flush();\n\n        long long W_obs, H_obs;\n        cin >> W_obs >> H_obs;\n\n        double total_est_val = static_cast<double>(best_score);\n        double total_obs_val = static_cast<double>(W_obs) + static_cast<double>(H_obs);\n        double scale = 1.0;\n        if (total_est_val > 1e-9) {\n            scale = total_obs_val / total_est_val;\n        }\n\n        for (int i = 0; i < N; i++) {\n            w_est[i] *= scale;\n            h_est[i] *= scale;\n            if (w_est[i] < 1.0) w_est[i] = 1.0;\n            if (w_est[i] > 1e9) w_est[i] = 1e9;\n            if (h_est[i] < 1.0) h_est[i] = 1.0;\n            if (h_est[i] > 1e9) h_est[i] = 1e9;\n        }\n    }\n\n    return 0;\n}","ahc041":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, H_val;\n    cin >> N >> M >> H_val;\n    const int max_layer = H_val + 1;\n\n    vector<long long> A(N);\n    for (int i = 0; i < N; i++) {\n        cin >> A[i];\n    }\n\n    vector<vector<int>> graph(N);\n    for (int i = 0; i < M; i++) {\n        int u, v;\n        cin >> u >> v;\n        graph[u].push_back(v);\n        graph[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> indices(N);\n    iota(indices.begin(), indices.end(), 0);\n    sort(indices.begin(), indices.end(), [&](int i, int j) {\n        return A[i] < A[j];\n    });\n\n    vector<int> layer(N, -1);\n    vector<int> parent(N, -1);\n\n    for (int v : indices) {\n        for (int d = max_layer; d >= 1; d--) {\n            if (d == 1) {\n                layer[v] = 1;\n                parent[v] = -1;\n                break;\n            }\n            for (int u : graph[v]) {\n                if (layer[u] == d - 1) {\n                    layer[v] = d;\n                    parent[v] = u;\n                    break;\n                }\n            }\n            if (layer[v] != -1) {\n                break;\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 <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int N;\n    cin >> N;\n    vector<string> grid(N);\n    for (int i = 0; i < N; i++) {\n        cin >> grid[i];\n    }\n\n    vector<pair<char, int>> ops;\n\n    while (true) {\n        int best_i = -1, best_j = -1;\n        char best_dir = ' ';\n        int best_p = -1;\n        int min_shifts = 100000000;\n\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                if (grid[i][j] != 'x') continue;\n\n                bool safe = true;\n                for (int k = 0; k < i; k++) {\n                    if (grid[k][j] == 'o') {\n                        safe = false;\n                        break;\n                    }\n                }\n                if (safe) {\n                    int shifts = i + 1;\n                    if (shifts < min_shifts) {\n                        min_shifts = shifts;\n                        best_dir = 'U';\n                        best_p = j;\n                        best_i = i;\n                        best_j = j;\n                    }\n                }\n\n                safe = true;\n                for (int k = i + 1; k < N; k++) {\n                    if (grid[k][j] == 'o') {\n                        safe = false;\n                        break;\n                    }\n                }\n                if (safe) {\n                    int shifts = N - i;\n                    if (shifts < min_shifts) {\n                        min_shifts = shifts;\n                        best_dir = 'D';\n                        best_p = j;\n                        best_i = i;\n                        best_j = j;\n                    }\n                }\n\n                safe = true;\n                for (int k = 0; k < j; k++) {\n                    if (grid[i][k] == 'o') {\n                        safe = false;\n                        break;\n                    }\n                }\n                if (safe) {\n                    int shifts = j + 1;\n                    if (shifts < min_shifts) {\n                        min_shifts = shifts;\n                        best_dir = 'L';\n                        best_p = i;\n                        best_i = i;\n                        best_j = j;\n                    }\n                }\n\n                safe = true;\n                for (int k = j + 1; k < N; k++) {\n                    if (grid[i][k] == 'o') {\n                        safe = false;\n                        break;\n                    }\n                }\n                if (safe) {\n                    int shifts = N - j;\n                    if (shifts < min_shifts) {\n                        min_shifts = shifts;\n                        best_dir = 'R';\n                        best_p = i;\n                        best_i = i;\n                        best_j = j;\n                    }\n                }\n            }\n        }\n\n        if (best_i == -1) {\n            break;\n        }\n\n        for (int s = 0; s < min_shifts; s++) {\n            ops.push_back({best_dir, best_p});\n            if (best_dir == 'U') {\n                for (int i = 0; i < N - 1; i++) {\n                    grid[i][best_p] = grid[i + 1][best_p];\n                }\n                grid[N - 1][best_p] = '.';\n            } else if (best_dir == 'D') {\n                for (int i = N - 1; i > 0; i--) {\n                    grid[i][best_p] = grid[i - 1][best_p];\n                }\n                grid[0][best_p] = '.';\n            } else if (best_dir == 'L') {\n                for (int j = 0; j < N - 1; j++) {\n                    grid[best_p][j] = grid[best_p][j + 1];\n                }\n                grid[best_p][N - 1] = '.';\n            } else if (best_dir == 'R') {\n                for (int j = N - 1; j > 0; j--) {\n                    grid[best_p][j] = grid[best_p][j - 1];\n                }\n                grid[best_p][0] = '.';\n            }\n        }\n    }\n\n    for (auto &op : ops) {\n        cout << op.first << \" \" << op.second << \"\\n\";\n    }\n\n    return 0;\n}","ahc044":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <cstdlib>\n#include <ctime>\nusing namespace std;\n\nconst int N = 100;\nconst int L = 500000;\n\nlong simulate(const vector<int>& a, const vector<int>& b, const vector<long>& T) {\n    vector<long> count(N, 0);\n    count[0] = 1;\n    int current = 0;\n    for (int week = 1; week < L; week++) {\n        long t = count[current];\n        int next;\n        if (t & 1) {\n            next = a[current];\n        } else {\n            next = b[current];\n        }\n        count[next]++;\n        current = next;\n    }\n    long error = 0;\n    for (int j = 0; j < N; j++) {\n        error += abs(count[j] - T[j]);\n    }\n    return error;\n}\n\nint main() {\n    srand(time(0));\n\n    int N_val, L_val;\n    cin >> N_val >> L_val;\n    vector<long> T(N_val);\n    for (int i = 0; i < N_val; i++) {\n        cin >> T[i];\n    }\n\n    vector<pair<long, int>> sorted;\n    for (int i = 0; i < N_val; i++) {\n        sorted.emplace_back(T[i], i);\n    }\n    sort(sorted.begin(), sorted.end(), [](const auto& a, const auto& b) {\n        return a.first > b.first;\n    });\n\n    vector<int> a(N_val), b(N_val);\n    for (int i = 0; i < N_val; i++) {\n        a[i] = sorted[i].second;\n        b[i] = sorted[(i + 1) % N_val].second;\n    }\n\n    long best_error = simulate(a, b, T);\n    vector<int> best_a = a;\n    vector<int> best_b = b;\n\n    for (int iter = 0; iter < 100; iter++) {\n        int idx = rand() % N_val;\n        swap(a[idx], b[idx]);\n        long error = simulate(a, b, T);\n        if (error < best_error) {\n            best_error = error;\n            best_a = a;\n            best_b = b;\n        } else {\n            swap(a[idx], b[idx]);\n        }\n    }\n\n    for (int i = 0; i < N_val; i++) {\n        cout << best_a[i] << \" \" << best_b[i] << '\\n';\n    }\n\n    return 0;\n}","ahc045":"#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\n\nstruct DSU {\n    vector<int> parent;\n    DSU(int n) {\n        parent.resize(n);\n        iota(parent.begin(), parent.end(), 0);\n    }\n    int find(int x) {\n        if (parent[x] != x) \n            parent[x] = find(parent[x]);\n        return parent[x];\n    }\n    bool unite(int x, int y) {\n        x = find(x);\n        y = find(y);\n        if (x == y) return false;\n        parent[y] = x;\n        return true;\n    }\n};\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, Q_total, L, W;\n    cin >> N >> M >> Q_total >> L >> W;\n\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    for (int i = 0; i < N; i++) {\n        cin >> lx[i] >> rx[i] >> ly[i] >> ry[i];\n    }\n\n    vector<vector<ll>> min_sq(N, vector<ll>(N, 0));\n    for (int i = 0; i < N; i++) {\n        for (int j = i+1; j < N; j++) {\n            ll dx_min = max(0LL, (ll)max(lx[i], lx[j]) - (ll)min(rx[i], rx[j]));\n            ll dy_min = max(0LL, (ll)max(ly[i], ly[j]) - (ll)min(ry[i], ry[j]));\n            min_sq[i][j] = dx_min * dx_min + dy_min * dy_min;\n            min_sq[j][i] = min_sq[i][j];\n        }\n    }\n\n    vector<ll> min_edge(N, LLONG_MAX);\n    vector<int> parent(N, -1);\n    vector<bool> in_mst(N, false);\n    min_edge[0] = 0;\n\n    for (int i = 0; i < N; i++) {\n        int u = -1;\n        for (int j = 0; j < N; j++) {\n            if (!in_mst[j] && (u == -1 || min_edge[j] < min_edge[u])) {\n                u = j;\n            }\n        }\n        if (u == -1) break;\n        in_mst[u] = true;\n        for (int v = 0; v < N; v++) {\n            if (u == v) continue;\n            if (!in_mst[v] && min_sq[u][v] < min_edge[v]) {\n                min_edge[v] = min_sq[u][v];\n                parent[v] = u;\n            }\n        }\n    }\n\n    vector<vector<int>> adj(N);\n    for (int v = 1; v < N; v++) {\n        adj[parent[v]].push_back(v);\n        adj[v].push_back(parent[v]);\n    }\n\n    vector<int> tour;\n    vector<bool> visited(N, false);\n    stack<int> st;\n    st.push(0);\n    visited[0] = true;\n    while (!st.empty()) {\n        int u = st.top();\n        st.pop();\n        tour.push_back(u);\n        for (auto it = adj[u].rbegin(); it != adj[u].rend(); ++it) {\n            int v = *it;\n            if (!visited[v]) {\n                visited[v] = true;\n                st.push(v);\n            }\n        }\n    }\n\n    vector<vector<int>> groups(M);\n    int start_idx = 0;\n    for (int k = 0; k < M; k++) {\n        int g = G[k];\n        groups[k] = vector<int>(tour.begin() + start_idx, tour.begin() + start_idx + g);\n        start_idx += g;\n    }\n\n    vector<int> queries_per_group(M, 0);\n    vector<int> group_order(M);\n    iota(group_order.begin(), group_order.end(), 0);\n    sort(group_order.begin(), group_order.end(), [&](int i, int j) {\n        return G[i] > G[j];\n    });\n\n    int remaining_queries = Q_total;\n    for (int idx : group_order) {\n        int g = G[idx];\n        if (g <= 2) {\n            queries_per_group[idx] = 0;\n        } else if (g <= L) {\n            queries_per_group[idx] = 1;\n        } else {\n            queries_per_group[idx] = (g + L - 2) / (L - 1);\n        }\n        if (queries_per_group[idx] > remaining_queries) {\n            queries_per_group[idx] = remaining_queries;\n        }\n        remaining_queries -= queries_per_group[idx];\n        if (remaining_queries <= 0) {\n            break;\n        }\n    }\n\n    vector<vector<pair<int, int>>> candidate_edges(M);\n    remaining_queries = Q_total;\n\n    for (int k = 0; k < M; k++) {\n        int g = G[k];\n        if (g <= 2 || queries_per_group[k] == 0) {\n            continue;\n        }\n        int q_count = queries_per_group[k];\n        vector<vector<int>> windows;\n        if (g <= L) {\n            windows.push_back(groups[k]);\n        } else {\n            if (q_count == 1) {\n                windows.push_back(vector<int>(groups[k].begin(), groups[k].begin() + L));\n            } else {\n                int total_gap = g - L;\n                for (int i = 0; i < q_count; i++) {\n                    int start_index = (i * total_gap) / (q_count - 1);\n                    int end_index = start_index + L - 1;\n                    if (end_index >= g) {\n                        end_index = g - 1;\n                        start_index = g - L;\n                    }\n                    vector<int> window;\n                    for (int j = start_index; j <= end_index; j++) {\n                        window.push_back(groups[k][j]);\n                    }\n                    windows.push_back(window);\n                }\n            }\n        }\n\n        for (auto &window : windows) {\n            if (remaining_queries <= 0) break;\n            cout << \"? \" << window.size();\n            for (int city : window) {\n                cout << \" \" << city;\n            }\n            cout << endl;\n            cout.flush();\n            remaining_queries--;\n\n            for (int i = 0; i < (int)window.size() - 1; i++) {\n                int u, v;\n                cin >> u >> v;\n                candidate_edges[k].push_back({u, v});\n            }\n        }\n    }\n\n    vector<vector<pair<int, int>>> final_edges(M);\n    for (int k = 0; k < M; k++) {\n        int g = G[k];\n        if (g <= 1) {\n            final_edges[k] = vector<pair<int, int>>();\n            continue;\n        }\n        if (g == 2) {\n            final_edges[k].push_back({groups[k][0], groups[k][1]});\n            continue;\n        }\n\n        vector<int> group_cities = groups[k];\n        int num_cities = g;\n        vector<int> global_to_local(N, -1);\n        for (int i = 0; i < num_cities; i++) {\n            global_to_local[group_cities[i]] = i;\n        }\n\n        vector<tuple<ll, int, int>> all_edges;\n        for (int i = 0; i < num_cities; i++) {\n            for (int j = i+1; j < num_cities; j++) {\n                int u_global = group_cities[i];\n                int v_global = group_cities[j];\n                all_edges.push_back({min_sq[u_global][v_global], u_global, v_global});\n            }\n        }\n        sort(all_edges.begin(), all_edges.end());\n\n        vector<tuple<ll, int, int>> candidate_list;\n        for (auto &e : candidate_edges[k]) {\n            int u = e.first, v = e.second;\n            if (global_to_local[u] == -1 || global_to_local[v] == -1) continue;\n            candidate_list.push_back({min_sq[u][v], u, v});\n        }\n        sort(candidate_list.begin(), candidate_list.end());\n\n        DSU dsu_local(num_cities);\n        vector<pair<int, int>> edges_used;\n\n        auto add_edge = [&](int u_global, int v_global) {\n            int lu = global_to_local[u_global];\n            int lv = global_to_local[v_global];\n            if (dsu_local.unite(lu, lv)) {\n                edges_used.push_back({u_global, v_global});\n            }\n        };\n\n        for (auto &t : candidate_list) {\n            int u = get<1>(t);\n            int v = get<2>(t);\n            add_edge(u, v);\n            if ((int)edges_used.size() == num_cities - 1) break;\n        }\n\n        if ((int)edges_used.size() < num_cities - 1) {\n            for (auto &t : all_edges) {\n                int u = get<1>(t);\n                int v = get<2>(t);\n                add_edge(u, v);\n                if ((int)edges_used.size() == num_cities - 1) break;\n            }\n        }\n\n        final_edges[k] = edges_used;\n    }\n\n    cout << \"!\" << endl;\n    for (int k = 0; k < M; k++) {\n        for (int i = 0; i < G[k]; i++) {\n            if (i > 0) cout << \" \";\n            cout << groups[k][i];\n        }\n        cout << endl;\n\n        for (auto &e : final_edges[k]) {\n            cout << e.first << \" \" << e.second << endl;\n        }\n    }\n    cout.flush();\n\n    return 0;\n}","ahc046":"#include <iostream>\n#include <vector>\n#include <cstdlib>\nusing namespace std;\n\nint main() {\n    int N, M;\n    cin >> N >> M;\n    vector<pair<int, int>> points(M);\n    for (int i = 0; i < M; i++) {\n        cin >> points[i].first >> points[i].second;\n    }\n\n    for (int idx = 0; idx < M-1; idx++) {\n        int ci = points[idx].first;\n        int cj = points[idx].second;\n        int ti = points[idx+1].first;\n        int tj = points[idx+1].second;\n\n        int dr = ti - ci;\n        int dc = tj - cj;\n\n        if (ci == ti) {\n            if (cj < tj) {\n                int d_direct = tj - cj;\n                int d_alt = 1 + (N - 1 - tj);\n                if (d_alt < d_direct) {\n                    cout << \"S R\\n\";\n                    for (int k = 0; k < N-1-tj; k++) {\n                        cout << \"M L\\n\";\n                    }\n                } else {\n                    for (int k = 0; k < d_direct; k++) {\n                        cout << \"M R\\n\";\n                    }\n                }\n            } else if (cj > tj) {\n                int d_direct = cj - tj;\n                int d_alt = 1 + tj;\n                if (d_alt < d_direct) {\n                    cout << \"S L\\n\";\n                    for (int k = 0; k < tj; k++) {\n                        cout << \"M R\\n\";\n                    }\n                } else {\n                    for (int k = 0; k < d_direct; k++) {\n                        cout << \"M L\\n\";\n                    }\n                }\n            }\n        }\n        else if (cj == tj) {\n            if (ci < ti) {\n                int d_direct = ti - ci;\n                int d_alt = 1 + (N - 1 - ti);\n                if (d_alt < d_direct) {\n                    cout << \"S D\\n\";\n                    for (int k = 0; k < N-1-ti; k++) {\n                        cout << \"M U\\n\";\n                    }\n                } else {\n                    for (int k = 0; k < d_direct; k++) {\n                        cout << \"M D\\n\";\n                    }\n                }\n            } else if (ci > ti) {\n                int d_direct = ci - ti;\n                int d_alt = 1 + ti;\n                if (d_alt < d_direct) {\n                    cout << \"S U\\n\";\n                    for (int k = 0; k < ti; k++) {\n                        cout << \"M D\\n\";\n                    }\n                } else {\n                    for (int k = 0; k < d_direct; k++) {\n                        cout << \"M U\\n\";\n                    }\n                }\n            }\n        }\n        else {\n            if (abs(dr) >= abs(dc)) {\n                if (dr > 0) {\n                    int d_direct = dr;\n                    int d_alt = 1 + (N - 1 - ti);\n                    if (d_alt < d_direct) {\n                        cout << \"S D\\n\";\n                        for (int k = 0; k < N-1-ti; k++) {\n                            cout << \"M U\\n\";\n                        }\n                    } else {\n                        for (int k = 0; k < d_direct; k++) {\n                            cout << \"M D\\n\";\n                        }\n                    }\n                } else {\n                    int d_direct = -dr;\n                    int d_alt = 1 + ti;\n                    if (d_alt < d_direct) {\n                        cout << \"S U\\n\";\n                        for (int k = 0; k < ti; k++) {\n                            cout << \"M D\\n\";\n                        }\n                    } else {\n                        for (int k = 0; k < d_direct; k++) {\n                            cout << \"M U\\n\";\n                        }\n                    }\n                }\n                if (cj < tj) {\n                    int d_direct = tj - cj;\n                    int d_alt = 1 + (N - 1 - tj);\n                    if (d_alt < d_direct) {\n                        cout << \"S R\\n\";\n                        for (int k = 0; k < N-1-tj; k++) {\n                            cout << \"M L\\n\";\n                        }\n                    } else {\n                        for (int k = 0; k < d_direct; k++) {\n                            cout << \"M R\\n\";\n                        }\n                    }\n                } else {\n                    int d_direct = cj - tj;\n                    int d_alt = 1 + tj;\n                    if (d_alt < d_direct) {\n                        cout << \"S L\\n\";\n                        for (int k = 0; k < tj; k++) {\n                            cout << \"M R\\n\";\n                        }\n                    } else {\n                        for (int k = 0; k < d_direct; k++) {\n                            cout << \"M L\\n\";\n                        }\n                    }\n                }\n            } else {\n                if (dc > 0) {\n                    int d_direct = dc;\n                    int d_alt = 1 + (N - 1 - tj);\n                    if (d_alt < d_direct) {\n                        cout << \"S R\\n\";\n                        for (int k = 0; k < N-1-tj; k++) {\n                            cout << \"M L\\n\";\n                        }\n                    } else {\n                        for (int k = 0; k < d_direct; k++) {\n                            cout << \"M R\\n\";\n                        }\n                    }\n                } else {\n                    int d_direct = -dc;\n                    int d_alt = 1 + tj;\n                    if (d_alt < d_direct) {\n                        cout << \"S L\\n\";\n                        for (int k = 0; k < tj; k++) {\n                            cout << \"M R\\n\";\n                        }\n                    } else {\n                        for (int k = 0; k < d_direct; k++) {\n                            cout << \"M L\\n\";\n                        }\n                    }\n                }\n                if (ci < ti) {\n                    int d_direct = ti - ci;\n                    int d_alt = 1 + (N - 1 - ti);\n                    if (d_alt < d_direct) {\n                        cout << \"S D\\n\";\n                        for (int k = 0; k < N-1-ti; k++) {\n                            cout << \"M U\\n\";\n                        }\n                    } else {\n                        for (int k = 0; k < d_direct; k++) {\n                            cout << \"M D\\n\";\n                        }\n                    }\n                } else {\n                    int d_direct = ci - ti;\n                    int d_alt = 1 + ti;\n                    if (d_alt < d_direct) {\n                        cout << \"S U\\n\";\n                        for (int k = 0; k < ti; k++) {\n                            cout << \"M D\\n\";\n                        }\n                    } else {\n                        for (int k = 0; k < d_direct; k++) {\n                            cout << \"M U\\n\";\n                        }\n                    }\n                }\n            }\n        }\n    }\n\n    return 0;\n}"},"16":{"ahc001":"#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\n\nstruct Rect {\n    int a, b, c, d;\n    int idx;\n};\n\nstruct Company {\n    int x, y;\n    ll r;\n    int idx;\n};\n\nconst int GRID_SIZE = 100;\nvector<Rect> grid[GRID_SIZE][GRID_SIZE];\n\nvoid add_rect_to_grid(const Rect& r) {\n    int ga1 = r.a / GRID_SIZE, gb1 = r.b / GRID_SIZE;\n    int ga2 = (r.c - 1) / GRID_SIZE, gb2 = (r.d - 1) / GRID_SIZE;\n    for (int ga = ga1; ga <= ga2; ga++) {\n        for (int gb = gb1; gb <= gb2; gb++) {\n            grid[ga][gb].push_back(r);\n        }\n    }\n}\n\nbool overlaps(const Rect& r1, const Rect& r2) {\n    return !(r1.c <= r2.a || r2.c <= r1.a || r1.d <= r2.b || r2.d <= r1.b);\n}\n\nbool contains(const Rect& rect, int x, int y) {\n    return rect.a <= x && x < rect.c && rect.b <= y && y < rect.d;\n}\n\nvoid clear_grid() {\n    for (int i = 0; i < GRID_SIZE; i++) {\n        for (int j = 0; j < GRID_SIZE; j++) {\n            grid[i][j].clear();\n        }\n    }\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int n;\n    cin >> n;\n    vector<Company> companies;\n    for (int i = 0; i < n; i++) {\n        int x, y;\n        ll r;\n        cin >> x >> y >> r;\n        companies.push_back({x, y, r, i});\n    }\n\n    vector<vector<pair<int, int>>> all_candidates(n);\n    for (int i = 0; i < n; i++) {\n        ll r_val = companies[i].r;\n        set<pair<int, int>> candSet;\n        int base = sqrt(r_val);\n\n        for (int dw = -20; dw <= 20; dw++) {\n            int w = base + dw;\n            if (w < 1) w = 1;\n            if (w > 10000) w = 10000;\n            int h = (r_val + w - 1) / w;\n            if (h < 1) h = 1;\n            if (h > 10000) h = 10000;\n            candSet.insert({w, h});\n            candSet.insert({h, w});\n        }\n        candSet.insert({1, 1});\n\n        vector<pair<int, int>> cands;\n        for (auto p : candSet) {\n            cands.push_back(p);\n        }\n        sort(cands.begin(), cands.end(), [&](const pair<int, int>& a, const pair<int, int>& b) {\n            ll area_a = (ll)a.first * a.second;\n            ll area_b = (ll)b.first * b.second;\n            ll err_a = abs(area_a - r_val);\n            ll err_b = abs(area_b - r_val);\n            if (err_a != err_b) return err_a < err_b;\n            return area_a < area_b;\n        });\n        if ((int)cands.size() > 30) {\n            cands.resize(30);\n        }\n        all_candidates[i] = cands;\n    }\n\n    sort(companies.begin(), companies.end(), [](const Company& a, const Company& b) {\n        return a.r > b.r;\n    });\n\n    vector<Rect> result(n);\n    clear_grid();\n\n    for (const auto& comp : companies) {\n        int x = comp.x;\n        int y = comp.y;\n        int orig_idx = comp.idx;\n        bool placed = false;\n\n        for (auto [w, h] : all_candidates[orig_idx]) {\n            int Lx = max(0, x + 1 - w);\n            int Rx = min(x, 10000 - w);\n            int Ly = max(0, y + 1 - h);\n            int Ry = min(y, 10000 - h);\n            if (Lx > Rx || Ly > Ry) continue;\n\n            int ideal_a = (Lx + Rx) / 2;\n            int ideal_b = (Ly + Ry) / 2;\n\n            // Try positions in a spiral pattern around ideal position\n            for (int d = 0; d <= 20 && !placed; d++) {\n                for (int dx = -d; dx <= d && !placed; dx++) {\n                    int dy = d;\n                    int a_candidate = max(Lx, min(Rx, ideal_a + dx));\n                    int b_candidate = max(Ly, min(Ry, ideal_b + dy));\n                    Rect candidate = {a_candidate, b_candidate, a_candidate + w, b_candidate + h, orig_idx};\n\n                    if (!contains(candidate, x + 1, y + 1)) continue;\n\n                    bool valid = true;\n                    int ga1 = a_candidate / GRID_SIZE, gb1 = b_candidate / GRID_SIZE;\n                    int ga2 = (a_candidate + w - 1) / GRID_SIZE, gb2 = (b_candidate + h - 1) / GRID_SIZE;\n\n                    for (int ga = ga1; ga <= ga2 && valid; ga++) {\n                        for (int gb = gb1; gb <= gb2 && valid; gb++) {\n                            for (const auto& rect : grid[ga][gb]) {\n                                if (overlaps(candidate, rect)) {\n                                    valid = false;\n                                    break;\n                                }\n                            }\n                        }\n                    }\n\n                    if (valid) {\n                        result[orig_idx] = candidate;\n                        add_rect_to_grid(candidate);\n                        placed = true;\n                    }\n                }\n                for (int dy = -d + 1; dy < d && !placed; dy++) {\n                    int dx = d;\n                    int a_candidate = max(Lx, min(Rx, ideal_a + dx));\n                    int b_candidate = max(Ly, min(Ry, ideal_b + dy));\n                    Rect candidate = {a_candidate, b_candidate, a_candidate + w, b_candidate + h, orig_idx};\n\n                    if (!contains(candidate, x + 1, y + 1)) continue;\n\n                    bool valid = true;\n                    int ga1 = a_candidate / GRID_SIZE, gb1 = b_candidate / GRID_SIZE;\n                    int ga2 = (a_candidate + w - 1) / GRID_SIZE, gb2 = (b_candidate + h - 1) / GRID_SIZE;\n\n                    for (int ga = ga1; ga <= ga2 && valid; ga++) {\n                        for (int gb = gb1; gb <= gb2 && valid; gb++) {\n                            for (const auto& rect : grid[ga][gb]) {\n                                if (overlaps(candidate, rect)) {\n                                    valid = false;\n                                    break;\n                                }\n                            }\n                        }\n                    }\n\n                    if (valid) {\n                        result[orig_idx] = candidate;\n                        add_rect_to_grid(candidate);\n                        placed = true;\n                    }\n                }\n            }\n        }\n\n        if (!placed) {\n            // Try minimal size placement in free space\n            Rect candidate = {0, 0, 1, 1, orig_idx};\n            for (int ga = 0; ga < GRID_SIZE && !placed; ga++) {\n                for (int gb = 0; gb < GRID_SIZE && !placed; gb++) {\n                    for (auto rect : grid[ga][gb]) {\n                        if (rect.c - rect.a > 1 && rect.d - rect.b > 1) {\n                            candidate = {rect.a, rect.b, rect.a + 1, rect.b + 1, orig_idx};\n                            if (contains(candidate, x + 1, y + 1)) {\n                                result[orig_idx] = candidate;\n                                add_rect_to_grid(candidate);\n                                placed = true;\n                                break;\n                            }\n                        }\n                    }\n                }\n            }\n        }\n\n        if (!placed) {\n            // Emergency placement\n            result[orig_idx] = {0, 0, 1, 1, orig_idx};\n            add_rect_to_grid(result[orig_idx]);\n        }\n    }\n\n    for (int i = 0; i < n; i++) {\n        cout << result[i].a << \" \" << result[i].b << \" \"\n             << result[i].c << \" \" << result[i].d << '\\n';\n    }\n\n    return 0;\n}","ahc002":"#include <bits/stdc++.h>\n#include <boost/dynamic_bitset.hpp>\nusing namespace std;\n\nstruct PathNode {\n    char dir;\n    shared_ptr<PathNode> prev;\n    PathNode(char d, shared_ptr<PathNode> p) : dir(d), prev(p) {}\n};\n\nstruct State {\n    int i, j;\n    boost::dynamic_bitset<> visited;\n    long long score;\n    shared_ptr<PathNode> path_tail;\n    long long value;\n\n    State(int i, int j, int M) : i(i), j(j), visited(M), score(0), path_tail(nullptr), value(0) {}\n};\n\nstring get_path(shared_ptr<PathNode> tail) {\n    if (!tail) return \"\";\n    string s;\n    while (tail) {\n        s += tail->dir;\n        tail = tail->prev;\n    }\n    reverse(s.begin(), s.end());\n    return s;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\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\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    const vector<tuple<int, int, char>> dirs = {\n        {-1, 0, 'U'},\n        {1, 0, 'D'},\n        {0, -1, 'L'},\n        {0, 1, 'R'}\n    };\n\n    const int BEAM_WIDTH = 1000;\n    vector<State> current_beam;\n    State start_state(si, sj, M);\n    start_state.visited.set(t[si][sj]);\n    start_state.score = p[si][sj];\n\n    // Compute initial potential\n    vector<int> neighbors;\n    for (const auto& [dx, dy, _] : dirs) {\n        int ni = si + dx;\n        int nj = sj + dy;\n        if (ni >= 0 && ni < 50 && nj >= 0 && nj < 50) {\n            if (!start_state.visited[t[ni][nj]]) {\n                neighbors.push_back(p[ni][nj]);\n            }\n        }\n    }\n    sort(neighbors.rbegin(), neighbors.rend());\n    int potential = 0;\n    for (int k = 0; k < min(3, (int)neighbors.size()); k++) {\n        potential += neighbors[k] * (1.0 / (k + 1));\n    }\n    start_state.value = start_state.score + potential;\n\n    current_beam.push_back(start_state);\n\n    long long best_score = start_state.score;\n    string best_path = \"\";\n\n    while (!current_beam.empty()) {\n        vector<State> next_beam;\n        next_beam.reserve(current_beam.size() * 4);\n\n        for (State& state : current_beam) {\n            for (const auto& [dx, dy, dchar] : dirs) {\n                int ni = state.i + dx;\n                int nj = state.j + dy;\n                if (ni < 0 || ni >= 50 || nj < 0 || nj >= 50) continue;\n\n                int tile_id = t[ni][nj];\n                if (state.visited.test(tile_id)) continue;\n\n                State new_state(ni, nj, M);\n                new_state.visited = state.visited;\n                new_state.visited.set(tile_id);\n                new_state.score = state.score + p[ni][nj];\n\n                // Compute potential for new_state\n                vector<int> neighbors;\n                for (const auto& [dx2, dy2, _] : dirs) {\n                    int ni2 = ni + dx2;\n                    int nj2 = nj + dy2;\n                    if (ni2 >= 0 && ni2 < 50 && nj2 >= 0 && nj2 < 50) {\n                        int tid = t[ni2][nj2];\n                        if (!new_state.visited[tid]) {\n                            neighbors.push_back(p[ni2][nj2]);\n                        }\n                    }\n                }\n                sort(neighbors.rbegin(), neighbors.rend());\n                int potential_val = 0;\n                for (int k = 0; k < min(3, (int)neighbors.size()); k++) {\n                    potential_val += neighbors[k] * (1.0 / (k + 1));\n                }\n                new_state.value = new_state.score + potential_val;\n\n                new_state.path_tail = make_shared<PathNode>(dchar, state.path_tail);\n\n                if (new_state.score > best_score) {\n                    best_score = new_state.score;\n                    best_path = get_path(new_state.path_tail);\n                }\n\n                next_beam.push_back(move(new_state));\n            }\n        }\n\n        if (next_beam.empty()) break;\n\n        // Sort by value descending, then by score descending\n        sort(next_beam.begin(), next_beam.end(), [](const State& a, const State& b) {\n            if (a.value != b.value) return a.value > b.value;\n            return a.score > b.score;\n        });\n\n        if ((int)next_beam.size() > BEAM_WIDTH) {\n            current_beam.assign(next_beam.begin(), next_beam.begin() + BEAM_WIDTH);\n        } else {\n            current_beam = move(next_beam);\n        }\n    }\n\n    cout << best_path << '\\n';\n    return 0;\n}","ahc003":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    const int N = 30;\n    double W_h[N][N-1];\n    double W_v[N-1][N];\n\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N-1; j++) {\n            W_h[i][j] = 5000.0;\n        }\n    }\n    for (int i = 0; i < N-1; i++) {\n        for (int j = 0; j < N; j++) {\n            W_v[i][j] = 5000.0;\n        }\n    }\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, 1e18));\n        vector<vector<int>> parent_i(N, vector<int>(N, -1));\n        vector<vector<int>> parent_j(N, vector<int>(N, -1));\n\n        priority_queue<tuple<double, int, int>, vector<tuple<double, int, int>>, greater<tuple<double, int, int>>> pq;\n        dist[si][sj] = 0.0;\n        pq.push(make_tuple(0.0, si, sj));\n\n        const int di[4] = {-1, 1, 0, 0};\n        const int dj[4] = {0, 0, -1, 1};\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 + di[dir];\n                int nj = j + dj[dir];\n                if (ni < 0 || ni >= N || nj < 0 || nj >= N) continue;\n\n                double weight;\n                if (dir == 0) {\n                    weight = W_v[i-1][j];\n                } else if (dir == 1) {\n                    weight = W_v[i][j];\n                } else if (dir == 2) {\n                    weight = W_h[i][j-1];\n                } else {\n                    weight = W_h[i][j];\n                }\n\n                double nd = d + weight;\n                if (nd < dist[ni][nj]) {\n                    dist[ni][nj] = nd;\n                    parent_i[ni][nj] = i;\n                    parent_j[ni][nj] = j;\n                    pq.push(make_tuple(nd, ni, nj));\n                }\n            }\n        }\n\n        string path_str = \"\";\n        vector<tuple<int, int, int>> edges_list;\n        int i = ti, j = tj;\n        while (i != si || j != sj) {\n            int pi = parent_i[i][j];\n            int pj = parent_j[i][j];\n            if (pi == i-1) {\n                path_str += 'D';\n            } else if (pi == i+1) {\n                path_str += 'U';\n            } else if (pj == j-1) {\n                path_str += 'R';\n            } else if (pj == j+1) {\n                path_str += 'L';\n            }\n\n            if (pi == i-1) {\n                edges_list.push_back(make_tuple(0, i-1, j));\n            } else if (pi == i+1) {\n                edges_list.push_back(make_tuple(0, i, j));\n            } else if (pj == j-1) {\n                edges_list.push_back(make_tuple(1, i, j-1));\n            } else if (pj == j+1) {\n                edges_list.push_back(make_tuple(1, i, j));\n            }\n\n            i = pi;\n            j = pj;\n        }\n        reverse(path_str.begin(), path_str.end());\n\n        cout << path_str << \"\\n\";\n        cout.flush();\n\n        long long r;\n        cin >> r;\n\n        int L = path_str.size();\n        double est_length = dist[ti][tj];\n        double error = static_cast<double>(r) - est_length;\n\n        double lr = 0.1 / (k + 1);\n        for (auto &edge : edges_list) {\n            int type = get<0>(edge);\n            int idx1 = get<1>(edge);\n            int idx2 = get<2>(edge);\n            double step = lr * error / static_cast<double>(L);\n\n            if (type == 0) {\n                W_v[idx1][idx2] += step;\n                if (W_v[idx1][idx2] < 1000.0) W_v[idx1][idx2] = 1000.0;\n                else if (W_v[idx1][idx2] > 9000.0) W_v[idx1][idx2] = 9000.0;\n            } else {\n                W_h[idx1][idx2] += step;\n                if (W_h[idx1][idx2] < 1000.0) W_h[idx1][idx2] = 1000.0;\n                else if (W_h[idx1][idx2] > 9000.0) W_h[idx1][idx2] = 9000.0;\n            }\n        }\n    }\n\n    return 0;\n}","ahc004":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int N, M;\n    cin >> N >> M;\n    vector<string> strs(M);\n    for (int i = 0; i < M; i++) {\n        cin >> strs[i];\n    }\n\n    vector<int> indices(M);\n    iota(indices.begin(), indices.end(), 0);\n    sort(indices.begin(), indices.end(), [&](int i, int j) {\n        return strs[i].size() > strs[j].size();\n    });\n\n    vector<string> mat(N, string(N, '.'));\n    vector<bool> covered(M, false);\n    int covered_count = 0;\n\n    for (int idx : indices) {\n        string& s = strs[idx];\n        int L = s.size();\n        int best_dots = -1;\n        int best_dir = -1;\n        int best_i = -1;\n        int best_j = -1;\n\n        for (int i = 0; i < N; i++) {\n            for (int sc = 0; sc < N; sc++) {\n                bool ok = true;\n                int dots = 0;\n                for (int p = 0; p < L; p++) {\n                    int col = (sc + p) % N;\n                    if (mat[i][col] != '.' && mat[i][col] != s[p]) {\n                        ok = false;\n                        break;\n                    }\n                    if (mat[i][col] == '.') {\n                        dots++;\n                    }\n                }\n                if (ok && dots > best_dots) {\n                    best_dots = dots;\n                    best_dir = 0;\n                    best_i = i;\n                    best_j = sc;\n                }\n            }\n        }\n\n        for (int j = 0; j < N; j++) {\n            for (int sr = 0; sr < N; sr++) {\n                bool ok = true;\n                int dots = 0;\n                for (int p = 0; p < L; p++) {\n                    int row = (sr + p) % N;\n                    if (mat[row][j] != '.' && mat[row][j] != s[p]) {\n                        ok = false;\n                        break;\n                    }\n                    if (mat[row][j] == '.') {\n                        dots++;\n                    }\n                }\n                if (ok && dots > best_dots) {\n                    best_dots = dots;\n                    best_dir = 1;\n                    best_i = sr;\n                    best_j = j;\n                }\n            }\n        }\n\n        if (best_dots != -1) {\n            covered[idx] = true;\n            covered_count++;\n            if (best_dir == 0) {\n                for (int p = 0; p < L; p++) {\n                    int col = (best_j + p) % N;\n                    mat[best_i][col] = s[p];\n                }\n            } else {\n                for (int p = 0; p < L; p++) {\n                    int row = (best_i + p) % N;\n                    mat[row][best_j] = s[p];\n                }\n            }\n        }\n    }\n\n    bool improved;\n    do {\n        improved = false;\n        for (int idx = 0; idx < M && !improved; idx++) {\n            if (covered[idx]) continue;\n            string& s = strs[idx];\n            int L = s.size();\n\n            for (int i = 0; i < N && !improved; i++) {\n                for (int sc = 0; sc < N && !improved; sc++) {\n                    bool ok = true;\n                    for (int p = 0; p < L; p++) {\n                        int col = (sc + p) % N;\n                        if (mat[i][col] != '.' && mat[i][col] != s[p]) {\n                            ok = false;\n                            break;\n                        }\n                    }\n                    if (ok) {\n                        for (int p = 0; p < L; p++) {\n                            int col = (sc + p) % N;\n                            mat[i][col] = s[p];\n                        }\n                        covered[idx] = true;\n                        covered_count++;\n                        improved = true;\n                    }\n                }\n            }\n\n            if (improved) continue;\n\n            for (int j = 0; j < N && !improved; j++) {\n                for (int sr = 0; sr < N && !improved; sr++) {\n                    bool ok = true;\n                    for (int p = 0; p < L; p++) {\n                        int row = (sr + p) % N;\n                        if (mat[row][j] != '.' && mat[row][j] != s[p]) {\n                            ok = false;\n                            break;\n                        }\n                    }\n                    if (ok) {\n                        for (int p = 0; p < L; p++) {\n                            int row = (sr + p) % N;\n                            mat[row][j] = s[p];\n                        }\n                        covered[idx] = true;\n                        covered_count++;\n                        improved = true;\n                    }\n                }\n            }\n        }\n    } while (improved);\n\n    if (covered_count == M) {\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    }\n\n    for (int i = 0; i < N; i++) {\n        cout << mat[i] << '\\n';\n    }\n    return 0;\n}","ahc005":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MAX_R = 5000;\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, si, sj;\n    cin >> N >> si >> sj;\n    vector<string> grid(N);\n    for (int i = 0; i < N; i++) {\n        cin >> grid[i];\n    }\n\n    vector<pair<int, int>> road_cells;\n    map<pair<int, int>, int> cell_to_index;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (grid[i][j] != '#') {\n                cell_to_index[{i, j}] = road_cells.size();\n                road_cells.push_back({i, j});\n            }\n        }\n    }\n    int R = road_cells.size();\n    if (R == 0) {\n        cout << \"\" << endl;\n        return 0;\n    }\n\n    vector<bitset<MAX_R>> visibility(R);\n    for (int u = 0; u < R; u++) {\n        int i = road_cells[u].first;\n        int j = road_cells[u].second;\n        visibility[u].set(u);\n\n        for (int k = j - 1; k >= 0; k--) {\n            if (grid[i][k] == '#') break;\n            int idx = cell_to_index[{i, k}];\n            visibility[u].set(idx);\n        }\n        for (int k = j + 1; k < N; k++) {\n            if (grid[i][k] == '#') break;\n            int idx = cell_to_index[{i, k}];\n            visibility[u].set(idx);\n        }\n        for (int k = i - 1; k >= 0; k--) {\n            if (grid[k][j] == '#') break;\n            int idx = cell_to_index[{k, j}];\n            visibility[u].set(idx);\n        }\n        for (int k = i + 1; k < N; k++) {\n            if (grid[k][j] == '#') break;\n            int idx = cell_to_index[{k, j}];\n            visibility[u].set(idx);\n        }\n    }\n\n    int start_idx = cell_to_index[{si, sj}];\n    bitset<MAX_R> covered = visibility[start_idx];\n    int cur_i = si, cur_j = sj;\n    string route = \"\";\n\n    while (covered.count() < R) {\n        vector<vector<int>> dist(N, vector<int>(N, INT_MAX));\n        vector<vector<pair<int, int>>> parent(N, vector<pair<int, int>>(N, make_pair(-1, -1)));\n        priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<>> pq;\n\n        dist[cur_i][cur_j] = 0;\n        pq.emplace(0, cur_i, cur_j);\n\n        while (!pq.empty()) {\n            auto [d, i, j] = pq.top();\n            pq.pop();\n            if (d != dist[i][j]) continue;\n\n            const int dx[4] = {-1, 1, 0, 0};\n            const int dy[4] = {0, 0, -1, 1};\n            for (int k = 0; k < 4; k++) {\n                int ni = i + dx[k];\n                int nj = j + dy[k];\n                if (ni < 0 || ni >= N || nj < 0 || nj >= N) continue;\n                if (grid[ni][nj] == '#') continue;\n                int nd = d + (grid[ni][nj] - '0');\n                if (nd < dist[ni][nj]) {\n                    dist[ni][nj] = nd;\n                    parent[ni][nj] = {i, j};\n                    pq.emplace(nd, ni, nj);\n                }\n            }\n        }\n\n        int best_gain = -1;\n        int best_time = INT_MAX;\n        int best_next_idx = -1;\n\n        for (int next_idx = 0; next_idx < R; next_idx++) {\n            int ni = road_cells[next_idx].first;\n            int nj = road_cells[next_idx].second;\n            if (dist[ni][nj] == INT_MAX) continue;\n\n            bitset<MAX_R> new_cover = visibility[next_idx] & ~covered;\n            int gain = new_cover.count();\n\n            if (gain == 0) continue;\n\n            if (gain > best_gain || (gain == best_gain && dist[ni][nj] < best_time)) {\n                best_gain = gain;\n                best_time = dist[ni][nj];\n                best_next_idx = next_idx;\n            }\n        }\n\n        if (best_next_idx == -1) {\n            break;\n        }\n\n        int ni_target = road_cells[best_next_idx].first;\n        int nj_target = road_cells[best_next_idx].second;\n\n        vector<pair<int, int>> path_cells;\n        int ci = ni_target;\n        int cj = nj_target;\n        path_cells.push_back({ci, cj});\n        while (ci != cur_i || cj != cur_j) {\n            auto [pi, pj] = parent[ci][cj];\n            ci = pi;\n            cj = pj;\n            path_cells.push_back({ci, cj});\n        }\n        reverse(path_cells.begin(), path_cells.end());\n\n        for (int idx = 1; idx < path_cells.size(); idx++) {\n            int pi = path_cells[idx-1].first;\n            int pj = path_cells[idx-1].second;\n            int ci = path_cells[idx].first;\n            int cj = path_cells[idx].second;\n            if (ci == pi - 1) route += 'U';\n            else if (ci == pi + 1) route += 'D';\n            else if (cj == pj - 1) route += 'L';\n            else if (cj == pj + 1) route += 'R';\n        }\n\n        covered |= visibility[best_next_idx];\n        cur_i = ni_target;\n        cur_j = nj_target;\n    }\n\n    if (cur_i != si || cur_j != sj) {\n        vector<vector<int>> dist_end(N, vector<int>(N, INT_MAX));\n        vector<vector<pair<int, int>>> parent_end(N, vector<pair<int, int>>(N, make_pair(-1, -1)));\n        priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<>> pq_end;\n\n        dist_end[cur_i][cur_j] = 0;\n        pq_end.emplace(0, cur_i, cur_j);\n\n        while (!pq_end.empty()) {\n            auto [d, i, j] = pq_end.top();\n            pq_end.pop();\n            if (d != dist_end[i][j]) continue;\n\n            const int dx[4] = {-1, 1, 0, 0};\n            const int dy[4] = {0, 0, -1, 1};\n            for (int k = 0; k < 4; k++) {\n                int ni = i + dx[k];\n                int nj = j + dy[k];\n                if (ni < 0 || ni >= N || nj < 0 || nj >= N) continue;\n                if (grid[ni][nj] == '#') continue;\n                int nd = d + (grid[ni][nj] - '0');\n                if (nd < dist_end[ni][nj]) {\n                    dist_end[ni][nj] = nd;\n                    parent_end[ni][nj] = {i, j};\n                    pq_end.emplace(nd, ni, nj);\n                }\n            }\n        }\n\n        vector<pair<int, int>> return_path;\n        int ci = si;\n        int cj = sj;\n        return_path.push_back({ci, cj});\n        while (ci != cur_i || cj != cur_j) {\n            auto [pi, pj] = parent_end[ci][cj];\n            ci = pi;\n            cj = pj;\n            return_path.push_back({ci, cj});\n        }\n        reverse(return_path.begin(), return_path.end());\n\n        for (int idx = 1; idx < return_path.size(); idx++) {\n            int pi = return_path[idx-1].first;\n            int pj = return_path[idx-1].second;\n            int ci = return_path[idx].first;\n            int cj = return_path[idx].second;\n            if (ci == pi - 1) route += 'U';\n            else if (ci == pi + 1) route += 'D';\n            else if (cj == pj - 1) route += 'L';\n            else if (cj == pj + 1) route += 'R';\n        }\n    }\n\n    cout << route << endl;\n\n    return 0;\n}","future-contest-2022-qual":"#include <bits/stdc++.h>\nusing namespace std;\n\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\n\nstruct Task {\n    int id;\n    int priority;\n    bool operator<(const Task& other) const {\n        return priority < other.priority;\n    }\n};\n\nvoid update_skill(vector<int>& s, const vector<int>& d_task, int t_actual, int K, int day, int member_tasks) {\n    int low_bound = max(0, t_actual - 3);\n    int high_bound = t_actual + 3;\n\n    int w_est = 0;\n    for (int k = 0; k < K; k++) {\n        if (d_task[k] > s[k]) {\n            w_est += (d_task[k] - s[k]);\n        }\n    }\n\n    if (w_est == 0) return;\n\n    double learning_rate = min(1.0, 0.1 + (500.0 / (500.0 + day)));\n    learning_rate *= exp(-member_tasks / 5.0);\n\n    if (w_est < low_bound) {\n        int needed = static_cast<int>((low_bound - w_est) * learning_rate);\n        for (int i = 0; i < needed && needed > 0; i++) {\n            int best_k = -1;\n            int best_gain = 0;\n            for (int k = 0; k < K; k++) {\n                int def_old = max(0, d_task[k] - s[k]);\n                int def_new = max(0, d_task[k] - (s[k] - 1));\n                int gain = def_new - def_old;\n                if (gain > best_gain) {\n                    best_gain = gain;\n                    best_k = k;\n                }\n            }\n            if (best_k == -1) break;\n            s[best_k]--;\n        }\n    } else if (w_est > high_bound) {\n        int needed = static_cast<int>((w_est - high_bound) * learning_rate);\n        for (int i = 0; i < needed && needed > 0; i++) {\n            int best_k = -1;\n            int best_gain = 0;\n            for (int k = 0; k < K; k++) {\n                int def_old = max(0, d_task[k] - s[k]);\n                int def_new = max(0, d_task[k] - (s[k] + 1));\n                int gain = def_old - def_new;\n                if (gain > best_gain) {\n                    best_gain = gain;\n                    best_k = k;\n                }\n            }\n            if (best_k == -1) break;\n            s[best_k]++;\n        }\n    }\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N = 1000, M = 20, K, R;\n    cin >> N >> M >> K >> R;\n\n    vector<vector<int>> d(N, vector<int>(K));\n    vector<int> task_size(N, 0);\n    vector<int> task_dependencies(N, 0);\n    for (int i = 0; i < N; i++) {\n        for (int k = 0; k < K; k++) {\n            cin >> d[i][k];\n            task_size[i] += d[i][k];\n        }\n    }\n\n    vector<vector<int>> children(N);\n    vector<vector<int>> parents(N);\n    for (int i = 0; i < R; i++) {\n        int u, v;\n        cin >> u >> v;\n        u--; v--;\n        children[u].push_back(v);\n        parents[v].push_back(u);\n        task_dependencies[v]++;\n    }\n\n    vector<int> in_degree_count = task_dependencies;\n    vector<int> busy_until(M, 0);\n    vector<int> start_day(M, -1);\n    vector<int> current_task(M, -1);\n    vector<vector<int>> skill_est(M, vector<int>(K, 0));\n    vector<int> member_completed(M, 0);\n    vector<int> member_load(M, 0);\n    vector<bool> completed(N, false);\n    vector<bool> is_assigned(N, false);\n    priority_queue<Task> ready_queue;\n\n    vector<int> critical_path(N, 1);\n    for (int i = N-1; i >= 0; i--) {\n        for (int child : children[i]) {\n            critical_path[i] = max(critical_path[i], critical_path[child] + 1);\n        }\n    }\n\n    for (int i = 0; i < N; i++) {\n        if (in_degree_count[i] == 0) {\n            int priority = critical_path[i] * 10000 - task_size[i] * 10 - in_degree_count[i] * 50;\n            ready_queue.push({i, priority});\n        }\n    }\n\n    for (int day = 1; day <= 2000; day++) {\n        vector<int> free_members;\n        for (int j = 0; j < M; j++) {\n            if (busy_until[j] <= day) {\n                free_members.push_back(j);\n            }\n        }\n\n        sort(free_members.begin(), free_members.end(), [&](int a, int b) {\n            int sum_a = accumulate(skill_est[a].begin(), skill_est[a].end(), 0);\n            int sum_b = accumulate(skill_est[b].begin(), skill_est[b].end(), 0);\n            double eff_a = sum_a * 1.0 / max(1, member_load[a]);\n            double eff_b = sum_b * 1.0 / max(1, member_load[b]);\n            return eff_a > eff_b;\n        });\n\n        vector<pair<int, int>> assignments;\n\n        for (int j : free_members) {\n            if (ready_queue.empty()) break;\n\n            Task task = ready_queue.top();\n            ready_queue.pop();\n\n            int task_id = task.id;\n            if (completed[task_id] || is_assigned[task_id]) {\n                continue;\n            }\n\n            int best_member = -1;\n            int best_time = INT_MAX;\n            int best_w = INT_MAX;\n\n            for (int member : free_members) {\n                if (current_task[member] != -1) continue;\n\n                int w = 0;\n                for (int skill = 0; skill < K; skill++) {\n                    if (d[task_id][skill] > skill_est[member][skill]) {\n                        w += (d[task_id][skill] - skill_est[member][skill]);\n                    }\n                }\n\n                int base_time = (w == 0) ? 1 : max(1, w);\n                int actual_time = base_time + uniform_int_distribution<int>(-3, 3)(rng);\n\n                if (actual_time < best_time ||\n                    (actual_time == best_time && w < best_w)) {\n                    best_time = actual_time;\n                    best_w = w;\n                    best_member = member;\n                }\n            }\n\n            if (best_member == -1) {\n                ready_queue.push(task);\n                continue;\n            }\n\n            assignments.push_back({best_member, task_id});\n            is_assigned[task_id] = true;\n            start_day[best_member] = day;\n            current_task[best_member] = task_id;\n\n            busy_until[best_member] = day + best_time;\n            member_load[best_member] += best_time;\n            member_completed[best_member]++;\n        }\n\n        cout << assignments.size();\n        for (auto &p : assignments) {\n            cout << \" \" << p.first + 1 << \" \" << p.second + 1;\n        }\n        cout << endl;\n        cout.flush();\n\n        int n_completed;\n        cin >> n_completed;\n        if (n_completed == -1) {\n            break;\n        }\n\n        vector<int> completed_members(n_completed);\n        for (int i = 0; i < n_completed; i++) {\n            cin >> completed_members[i];\n            completed_members[i]--;\n        }\n\n        for (int j : completed_members) {\n            if (current_task[j] == -1) continue;\n\n            int task = current_task[j];\n            int t_actual = day - start_day[j] + 1;\n            update_skill(skill_est[j], d[task], t_actual, K, day, member_completed[j]);\n\n            completed[task] = true;\n            is_assigned[task] = false;\n\n            for (int child : children[task]) {\n                in_degree_count[child]--;\n                if (in_degree_count[child] == 0) {\n                    int priority = critical_path[child] * 10000 - task_size[child] * 10 - in_degree_count[child] * 50;\n                    ready_queue.push({child, priority});\n                }\n            }\n\n            member_load[j] = max(0, member_load[j] - (t_actual / 2));\n            busy_until[j] = day + 1;\n            start_day[j] = -1;\n            current_task[j] = -1;\n        }\n    }\n\n    return 0;\n}","ahc006":"#include <bits/stdc++.h>\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        return x == other.x && y == other.y;\n    }\n};\n\nint dist(const Point& a, const Point& b) {\n    return abs(a.x - b.x) + abs(a.y - b.y);\n}\n\nint main() {\n    vector<tuple<int, int, int, int>> orders;\n    for (int i = 0; i < 1000; i++) {\n        int a, b, c, d;\n        cin >> a >> b >> c >> d;\n        orders.emplace_back(a, b, c, d);\n    }\n\n    vector<Point> route = { Point(400, 400), Point(400, 400) };\n    vector<bool> selected(1000, false);\n    vector<int> pos_r(1000, -1);\n    vector<int> pos_d(1000, -1);\n\n    auto compute_total_distance = [&](const vector<Point>& r) {\n        int d = 0;\n        for (int i = 0; i < (int)r.size() - 1; i++) {\n            d += dist(r[i], r[i+1]);\n        }\n        return d;\n    };\n    int current_dist = compute_total_distance(route);\n\n    for (int iter = 0; iter < 50; iter++) {\n        long long best_delta = 1e18;\n        int best_order = -1;\n        int best_k = -1;\n        int L_route = route.size();\n\n        for (int idx = 0; idx < 1000; idx++) {\n            if (selected[idx]) continue;\n            auto [a, b, c, d] = orders[idx];\n            long long min_delta = 1e18;\n            int min_k = -1;\n\n            for (int k = 1; k < L_route; k++) {\n                Point prev = route[k-1];\n                Point next = route[k];\n                int delta = dist(prev, Point(a, b)) + dist(Point(a, b), Point(c, d)) + dist(Point(c, d), next) - dist(prev, next);\n                if (delta < min_delta) {\n                    min_delta = delta;\n                    min_k = k;\n                }\n            }\n\n            if (min_delta < best_delta) {\n                best_delta = min_delta;\n                best_order = idx;\n                best_k = min_k;\n            }\n        }\n\n        if (best_order == -1) {\n            for (int idx = 0; idx < 1000; idx++) {\n                if (!selected[idx]) {\n                    best_order = idx;\n                    best_k = 1;\n                    break;\n                }\n            }\n        }\n\n        auto [a, b, c, d] = orders[best_order];\n        selected[best_order] = true;\n        route.insert(route.begin() + best_k, Point(a, b));\n        route.insert(route.begin() + best_k + 1, Point(c, d));\n        pos_r[best_order] = best_k;\n        pos_d[best_order] = best_k + 1;\n        current_dist += best_delta;\n        L_route = route.size();\n    }\n\n    bool improved;\n    int passes = 0;\n    do {\n        improved = false;\n        int n = route.size();\n        for (int i = 0; i < n-1 && !improved; i++) {\n            for (int j = i+2; j < n-1 && !improved; j++) {\n                Point A = route[i];\n                Point B = route[i+1];\n                Point K = route[j];\n                Point L = route[j+1];\n                int delta = dist(A, K) + dist(B, L) - dist(A, B) - dist(K, L);\n                if (delta >= 0) continue;\n\n                vector<Point> candidate = route;\n                reverse(candidate.begin() + i+1, candidate.begin() + j+1);\n\n                vector<int> candidate_pos_r(1000, -1);\n                vector<int> candidate_pos_d(1000, -1);\n                for (int p = 0; p < candidate.size(); p++) {\n                    Point pt = candidate[p];\n                    for (int idx = 0; idx < 1000; idx++) {\n                        if (!selected[idx]) continue;\n                        auto [a_val, b_val, c_val, d_val] = orders[idx];\n                        if (candidate_pos_r[idx] == -1 && pt.x == a_val && pt.y == b_val) {\n                            candidate_pos_r[idx] = p;\n                        } else if (candidate_pos_d[idx] == -1 && pt.x == c_val && pt.y == d_val) {\n                            candidate_pos_d[idx] = p;\n                        }\n                    }\n                }\n\n                bool valid = true;\n                for (int idx = 0; idx < 1000; idx++) {\n                    if (!selected[idx]) continue;\n                    if (candidate_pos_r[idx] == -1 || candidate_pos_d[idx] == -1 || candidate_pos_r[idx] >= candidate_pos_d[idx]) {\n                        valid = false;\n                        break;\n                    }\n                }\n                if (!valid) continue;\n\n                route = move(candidate);\n                current_dist += delta;\n                pos_r = move(candidate_pos_r);\n                pos_d = move(candidate_pos_d);\n                improved = true;\n            }\n        }\n        passes++;\n    } while (improved && passes < 1000);\n\n    vector<int> selected_indices;\n    for (int i = 0; i < 1000; i++) {\n        if (selected[i]) {\n            selected_indices.push_back(i+1);\n        }\n    }\n    sort(selected_indices.begin(), selected_indices.end());\n\n    cout << 50;\n    for (int idx : selected_indices) {\n        cout << \" \" << idx;\n    }\n    cout << \"\\n\";\n\n    int n_route = route.size();\n    cout << n_route;\n    for (int i = 0; i < n_route; i++) {\n        cout << \" \" << route[i].x << \" \" << route[i].y;\n    }\n    cout << \"\\n\";\n\n    return 0;\n}","ahc007":"#include <iostream>\n#include <vector>\n#include <cmath>\n#include <algorithm>\n#include <functional>\nusing namespace std;\n\nstruct Edge {\n    int u, v;\n};\n\nint main() {\n    const int N = 400;\n    const int M = 1995;\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<Edge> edges(M);\n    vector<int> d(M, 0);\n\n    for (int i = 0; i < M; i++) {\n        cin >> edges[i].u >> edges[i].v;\n    }\n\n    for (int i = 0; i < M; i++) {\n        int u = edges[i].u;\n        int v = edges[i].v;\n        long long dx = (long long)x[u] - x[v];\n        long long dy = (long long)y[u] - y[v];\n        double dist = sqrt(dx*dx + dy*dy);\n        d[i] = (int)(dist + 0.5);\n    }\n\n    vector<int> parent(N);\n    vector<int> size_vec(N, 1);\n    vector<int> mout_comp(N, 10000000);\n\n    for (int i = 0; i < N; i++) {\n        parent[i] = i;\n    }\n\n    for (int i = 0; i < M; i++) {\n        int u = edges[i].u;\n        int v = edges[i].v;\n        if (d[i] < mout_comp[u]) mout_comp[u] = d[i];\n        if (d[i] < mout_comp[v]) mout_comp[v] = d[i];\n    }\n\n    function<int(int)> find = [&](int x) -> int {\n        if (parent[x] != x) {\n            parent[x] = find(parent[x]);\n        }\n        return parent[x];\n    };\n\n    int count = 0;\n\n    for (int i = 0; i < M; i++) {\n        int l;\n        cin >> l;\n\n        int u = edges[i].u;\n        int v = edges[i].v;\n        int ru = find(u);\n        int rv = find(v);\n\n        if (ru == rv) {\n            cout << 0 << endl;\n            cout.flush();\n            continue;\n        }\n\n        if (count == N - 1) {\n            cout << 0 << endl;\n            cout.flush();\n            continue;\n        }\n\n        int m = min(mout_comp[ru], mout_comp[rv]);\n\n        if (l <= m) {\n            cout << 1 << endl;\n            cout.flush();\n            count++;\n\n            if (size_vec[ru] < size_vec[rv]) {\n                parent[ru] = rv;\n                mout_comp[rv] = m;\n                size_vec[rv] += size_vec[ru];\n            } else {\n                parent[rv] = ru;\n                mout_comp[ru] = m;\n                size_vec[ru] += size_vec[rv];\n            }\n        } else {\n            cout << 0 << endl;\n            cout.flush();\n        }\n    }\n\n    return 0;\n}","ahc008":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int H = 30;\nconst int W = 30;\nint dx[4] = {-1, 1, 0, 0};\nint dy[4] = {0, 0, -1, 1};\nchar move_dir[4] = {'U', 'D', 'L', 'R'};\nchar wall_dir[4] = {'u', 'd', 'l', 'r'};\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N;\n    cin >> N;\n    vector<pair<int, int>> pets;\n    for (int i = 0; i < N; i++) {\n        int x, y, t;\n        cin >> x >> y >> t;\n        pets.push_back({x, y});\n    }\n\n    int M;\n    cin >> M;\n    vector<pair<int, int>> humans;\n    for (int i = 0; i < M; i++) {\n        int x, y;\n        cin >> x >> y;\n        humans.push_back({x, y});\n    }\n\n    bool grid[H+1][W+1];\n    for (int i = 1; i <= H; i++) {\n        for (int j = 1; j <= W; j++) {\n            grid[i][j] = true;\n        }\n    }\n\n    for (int turn = 0; turn < 300; turn++) {\n        string actions = \"\";\n        for (int i = 0; i < M; i++) {\n            vector<char> candidates;\n            candidates.push_back('.');\n\n            for (int d = 0; d < 4; d++) {\n                int nx = humans[i].first + dx[d];\n                int ny = humans[i].second + dy[d];\n                if (nx >= 1 && nx <= H && ny >= 1 && ny <= W && grid[nx][ny]) {\n                    candidates.push_back(move_dir[d]);\n                }\n            }\n\n            for (int d = 0; d < 4; d++) {\n                int wx = humans[i].first + dx[d];\n                int wy = humans[i].second + dy[d];\n                if (wx < 1 || wx > H || wy < 1 || wy > W) continue;\n                if (!grid[wx][wy]) continue;\n\n                bool occupied = false;\n                for (int j = 0; j < M; j++) {\n                    if (humans[j].first == wx && humans[j].second == wy) {\n                        occupied = true;\n                        break;\n                    }\n                }\n                if (occupied) continue;\n                for (int j = 0; j < N; j++) {\n                    if (pets[j].first == wx && pets[j].second == wy) {\n                        occupied = true;\n                        break;\n                    }\n                }\n                if (occupied) continue;\n\n                bool adjacent_pet = false;\n                for (int dd = 0; dd < 4; dd++) {\n                    int nx = wx + dx[dd];\n                    int ny = wy + dy[dd];\n                    if (nx < 1 || nx > H || ny < 1 || ny > W) continue;\n                    for (int j = 0; j < N; j++) {\n                        if (pets[j].first == nx && pets[j].second == ny) {\n                            adjacent_pet = true;\n                            break;\n                        }\n                    }\n                    if (adjacent_pet) break;\n                }\n                if (adjacent_pet) continue;\n\n                candidates.push_back(wall_dir[d]);\n            }\n\n            double best_score = -1e18;\n            char best_act = '.';\n            for (char act : candidates) {\n                bool temp_grid[H+1][W+1];\n                for (int x = 1; x <= H; x++) {\n                    for (int y = 1; y <= W; y++) {\n                        temp_grid[x][y] = grid[x][y];\n                    }\n                }\n                pair<int, int> start_pos = humans[i];\n\n                if (act == '.') {\n                } else if (act >= 'A' && act <= 'Z') {\n                    int d;\n                    if (act == 'U') d = 0;\n                    else if (act == 'D') d = 1;\n                    else if (act == 'L') d = 2;\n                    else d = 3;\n                    int nx = start_pos.first + dx[d];\n                    int ny = start_pos.second + dy[d];\n                    start_pos = {nx, ny};\n                } else {\n                    int d;\n                    if (act == 'u') d = 0;\n                    else if (act == 'd') d = 1;\n                    else if (act == 'l') d = 2;\n                    else d = 3;\n                    int wx = start_pos.first + dx[d];\n                    int wy = start_pos.second + dy[d];\n                    temp_grid[wx][wy] = false;\n                }\n\n                bool visited[H+1][W+1] = {};\n                int comp_size = 0;\n                int pet_count = 0;\n                queue<pair<int, int>> q;\n                q.push(start_pos);\n                visited[start_pos.first][start_pos.second] = true;\n\n                while (!q.empty()) {\n                    auto [x, y] = q.front();\n                    q.pop();\n                    comp_size++;\n                    for (int j = 0; j < N; j++) {\n                        if (pets[j].first == x && pets[j].second == y) {\n                            pet_count++;\n                            break;\n                        }\n                    }\n                    for (int d = 0; d < 4; d++) {\n                        int nx = x + dx[d];\n                        int ny = y + dy[d];\n                        if (nx >= 1 && nx <= H && ny >= 1 && ny <= W && !visited[nx][ny] && temp_grid[nx][ny]) {\n                            visited[nx][ny] = true;\n                            q.push({nx, ny});\n                        }\n                    }\n                }\n\n                double factor = pow(2.0, -pet_count);\n                double score = comp_size * factor;\n\n                if (score > best_score) {\n                    best_score = score;\n                    best_act = act;\n                }\n            }\n            actions += best_act;\n        }\n\n        cout << actions << endl;\n        cout.flush();\n\n        vector<string> pet_moves(N);\n        for (int i = 0; i < N; i++) {\n            cin >> pet_moves[i];\n        }\n\n        for (int i = 0; i < M; i++) {\n            if (actions[i] >= 'a' && actions[i] <= 'z') {\n                int d;\n                if (actions[i] == 'u') d = 0;\n                else if (actions[i] == 'd') d = 1;\n                else if (actions[i] == 'l') d = 2;\n                else d = 3;\n                int wx = humans[i].first + dx[d];\n                int wy = humans[i].second + dy[d];\n                grid[wx][wy] = false;\n            }\n        }\n\n        for (int i = 0; i < M; i++) {\n            if (actions[i] >= 'A' && actions[i] <= 'Z') {\n                int d;\n                if (actions[i] == 'U') d = 0;\n                else if (actions[i] == 'D') d = 1;\n                else if (actions[i] == 'L') d = 2;\n                else d = 3;\n                int nx = humans[i].first + dx[d];\n                int ny = humans[i].second + dy[d];\n                if (nx >= 1 && nx <= H && ny >= 1 && ny <= W && grid[nx][ny]) {\n                    humans[i] = {nx, ny};\n                }\n            }\n        }\n\n        for (int i = 0; i < N; i++) {\n            int x = pets[i].first;\n            int y = pets[i].second;\n            string& moves = pet_moves[i];\n            for (char c : moves) {\n                int nx = x, ny = y;\n                if (c == 'U') nx--;\n                else if (c == 'D') nx++;\n                else if (c == 'L') ny--;\n                else if (c == 'R') ny++;\n                if (nx >= 1 && nx <= H && ny >= 1 && ny <= W && grid[nx][ny]) {\n                    x = nx;\n                    y = ny;\n                }\n            }\n            pets[i] = {x, y};\n        }\n    }\n\n    return 0;\n}","ahc009":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int INF = 1e9;\nconst vector<int> di = {-1, 1, 0, 0};\nconst vector<int> dj = {0, 0, -1, 1};\nconst vector<char> dir_char = {'U', 'D', 'L', 'R'};\n\nstruct State {\n    int steps;\n    int i;\n    int j;\n    State(int s, int i, int j) : steps(s), i(i), j(j) {}\n    bool operator<(const State& other) const {\n        return steps > other.steps;\n    }\n};\n\nvector<char> get_shortest_path(int si, int sj, int ti, int tj,\n                             const vector<string>& h, const vector<string>& v) {\n    vector<vector<int>> dist(20, vector<int>(20, INF));\n    vector<vector<pair<int, int>>> prev(20, vector<pair<int, int>>(20, {-1, -1}));\n    vector<vector<char>> move_dir(20, vector<char>(20, '?'));\n\n    priority_queue<State> pq;\n    dist[si][sj] = 0;\n    pq.push(State(0, si, sj));\n\n    while (!pq.empty()) {\n        auto s = pq.top();\n        pq.pop();\n\n        if (s.i == ti && s.j == tj) break;\n        if (s.steps > dist[s.i][s.j]) continue;\n\n        vector<int> dir_order = {0, 1, 2, 3};\n        sort(dir_order.begin(), dir_order.end(), [&](int a, int b) {\n            int nai = s.i + di[a];\n            int naj = s.j + dj[a];\n            int nbi = s.i + di[b];\n            int nbj = s.j + dj[b];\n            int dist_a = abs(nai - ti) + abs(naj - tj);\n            int dist_b = abs(nbi - ti) + abs(nbj - tj);\n            return dist_a < dist_b;\n        });\n\n        for (int d : dir_order) {\n            int ni = s.i + di[d];\n            int nj = s.j + dj[d];\n            if (ni < 0 || ni >= 20 || nj < 0 || nj >= 20) continue;\n\n            bool wall = false;\n            if (d == 0) {\n                if (s.i == 0) wall = true;\n                else if (v[s.i-1][s.j] == '1') wall = true;\n            } else if (d == 1) {\n                if (s.i == 19) wall = true;\n                else if (v[s.i][s.j] == '1') wall = true;\n            } else if (d == 2) {\n                if (s.j == 0) wall = true;\n                else if (h[s.i][s.j-1] == '1') wall = true;\n            } else if (d == 3) {\n                if (s.j == 19) wall = true;\n                else if (h[s.i][s.j] == '1') wall = true;\n            }\n            if (wall) continue;\n\n            if (dist[ni][nj] > dist[s.i][s.j] + 1) {\n                dist[ni][nj] = dist[s.i][s.j] + 1;\n                prev[ni][nj] = {s.i, s.j};\n                move_dir[ni][nj] = dir_char[d];\n                pq.push(State(dist[ni][nj], ni, nj));\n            }\n        }\n    }\n\n    if (dist[ti][tj] == INF) return {};\n\n    vector<char> path;\n    int i = ti, j = tj;\n    while (i != si || j != sj) {\n        path.push_back(move_dir[i][j]);\n        auto [pi, pj] = prev[i][j];\n        i = pi;\n        j = pj;\n    }\n    reverse(path.begin(), path.end());\n    return path;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int si, sj, ti, tj;\n    double p;\n    cin >> si >> sj >> ti >> tj >> 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<char> path = get_shortest_path(si, sj, ti, tj, h, v);\n\n    if (path.empty()) {\n        cout << \"\" << endl;\n        return 0;\n    }\n\n    int L = path.size();\n    int k = 200 / L;\n    string ans;\n    ans.reserve(200);\n    for (int i = 0; i < k; i++) {\n        for (char c : path) {\n            ans += c;\n        }\n    }\n\n    int remaining = 200 - (int)ans.size();\n    if (remaining > 0) {\n        for (int i = 0; i < remaining; i++) {\n            ans += path[i % L];\n        }\n    }\n\n    cout << ans << endl;\n\n    return 0;\n}","ahc010":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int di[4] = {0, -1, 0, 1};\nconst int dj[4] = {-1, 0, 1, 0};\nconst int to[8][4] = {\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\npair<int, int> get_top_two_cycles(const vector<string>& initial_types, const vector<vector<int>>& rotations) {\n    int grid[30][30];\n    for (int i = 0; i < 30; ++i) {\n        for (int j = 0; j < 30; ++j) {\n            int t = initial_types[i][j] - '0';\n            int r = rotations[i][j];\n            if (t < 4) {\n                grid[i][j] = (t + r) % 4;\n            } else {\n                grid[i][j] = t ^ (r & 1);\n            }\n        }\n    }\n\n    bool vis[30][30][4] = {};\n    int max1 = 0, max2 = 0;\n\n    for (int i = 0; i < 30; ++i) {\n        for (int j = 0; j < 30; ++j) {\n            for (int d = 0; d < 4; ++d) {\n                if (vis[i][j][d]) continue;\n\n                int ci = i, cj = j, cd = d;\n                int steps = 0;\n\n                while (true) {\n                    if (vis[ci][cj][cd]) {\n                        break;\n                    }\n                    vis[ci][cj][cd] = true;\n\n                    int s = grid[ci][cj];\n                    int nd = to[s][cd];\n                    if (nd == -1) {\n                        break;\n                    }\n\n                    int ni = ci + di[nd];\n                    int nj = cj + dj[nd];\n                    if (ni < 0 || ni >= 30 || nj < 0 || nj >= 30) {\n                        break;\n                    }\n\n                    cd = (nd + 2) % 4;\n                    ci = ni;\n                    cj = nj;\n                    steps++;\n\n                    if (ci == i && cj == j && cd == d) {\n                        if (steps > max1) {\n                            max2 = max1;\n                            max1 = steps;\n                        } else if (steps > max2) {\n                            max2 = steps;\n                        }\n                        break;\n                    }\n                }\n            }\n        }\n    }\n    return make_pair(max1, max2);\n}\n\nint main() {\n    vector<string> initial_types(30);\n    for (int i = 0; i < 30; i++) {\n        cin >> initial_types[i];\n    }\n\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n    uniform_int_distribution<int> dist(0, 3);\n\n    vector<vector<int>> best_rotations(30, vector<int>(30, 0));\n    long long best_score = 0;\n\n    for (int trial = 0; trial < 5; trial++) {\n        vector<vector<int>> rotations(30, vector<int>(30));\n        for (int i = 0; i < 30; i++) {\n            for (int j = 0; j < 30; j++) {\n                rotations[i][j] = dist(rng);\n            }\n        }\n\n        auto top_two = get_top_two_cycles(initial_types, rotations);\n        long long current_score = (top_two.second > 0) ? 1LL * top_two.first * top_two.second : 0;\n\n        if (current_score > best_score) {\n            best_score = current_score;\n            best_rotations = rotations;\n        }\n\n        bool improved;\n        for (int pass = 0; pass < 3; pass++) {\n            improved = false;\n            vector<pair<int, int>> tiles;\n            for (int i = 0; i < 30; i++) {\n                for (int j = 0; j < 30; j++) {\n                    tiles.emplace_back(i, j);\n                }\n            }\n            shuffle(tiles.begin(), tiles.end(), rng);\n\n            for (auto [i, j] : tiles) {\n                int original_r = rotations[i][j];\n                long long original_score = current_score;\n                int best_r = original_r;\n                long long best_score_for_tile = original_score;\n\n                for (int r = 0; r < 4; r++) {\n                    if (r == original_r) continue;\n                    rotations[i][j] = r;\n                    auto new_top = get_top_two_cycles(initial_types, rotations);\n                    long long new_score = (new_top.second > 0) ? 1LL * new_top.first * new_top.second : 0;\n                    rotations[i][j] = original_r;\n\n                    if (new_score > best_score_for_tile) {\n                        best_score_for_tile = new_score;\n                        best_r = r;\n                    }\n                }\n\n                if (best_r != original_r) {\n                    rotations[i][j] = best_r;\n                    current_score = best_score_for_tile;\n                    improved = true;\n                    if (current_score > best_score) {\n                        best_score = current_score;\n                        best_rotations = rotations;\n                    }\n                }\n            }\n            if (!improved) break;\n        }\n    }\n\n    string output;\n    for (int i = 0; i < 30; i++) {\n        for (int j = 0; j < 30; j++) {\n            output += ('0' + best_rotations[i][j]);\n        }\n    }\n    cout << output << endl;\n    return 0;\n}","ahc011":"#include <iostream>\n#include <vector>\n#include<algorithm>\n#include <random>\n#include <climits>\nusing namespace std;\n\nstruct State {\n    int empty_i, empty_j;\n    string moves;\n    int heuristic;\n\n    State(int ei, int ej, string m, int h)\n        : empty_i(ei), empty_j(ej), moves(move(m)), heuristic(h) {}\n};\n\nvector<vector<int>> generate_snake_pattern(int N) {\n    vector<vector<int>> target(N, vector<int>(N, 0));\n    int empty_i = N/2, empty_j = N/2;\n    for (int i = 0; i < N; i++) {\n        if (i % 2 == 0) {\n            for (int j = 0; j < N; j++) {\n                if (i == empty_i && j == empty_j) continue;\n                if (j > 0) {\n                    target[i][j] |= 1;\n                    target[i][j-1] |= 4;\n                }\n            }\n        } else {\n            for (int j = N-1; j >= 0; j--) {\n                if (i == empty_i && j == empty_j) continue;\n                if (j < N-1) {\n                    target[i][j] |= 4;\n                    target[i][j+1] |= 1;\n                }\n            }\n        }\n    }\n    return target;\n}\n\nint calculate_heuristic(const vector<vector<int>>& grid, const vector<vector<int>>& target, int empty_i, int empty_j, int N) {\n    int mismatches = abs(empty_i - N/2) + abs(empty_j - N/2);\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (grid[i][j] != target[i][j]) {\n                mismatches++;\n            }\n        }\n    }\n    return mismatches;\n}\n\nvoid optimize_path(vector<vector<int>>& grid, int& empty_i, int& empty_j,\n                  const vector<vector<int>>& target, string& moves, int N, mt19937& rng) {\n    vector<pair<int, int>> dirs = {{-1,0}, {1,0}, {0,-1}, {0,1}};\n    char dir_chars[] = {'U','D','L','R'};\n\n    for (int iter = 0; iter < 30; iter++) {\n        bool improved = false;\n        vector<int> dir_order = {0,1,2,3};\n        shuffle(dir_order.begin(), dir_order.end(), rng);\n\n        for (int d : dir_order) {\n            int ni = empty_i + dirs[d].first;\n            int nj = empty_j + dirs[d].second;\n            if (ni < 0 || ni >= N || nj < 0 || nj >= N) continue;\n\n            if (grid[ni][nj] == target[empty_i][empty_j]) {\n                swap(grid[empty_i][empty_j], grid[ni][nj]);\n                moves += dir_chars[d];\n                empty_i = ni;\n                empty_j = nj;\n                improved = true;\n                break;\n            }\n        }\n        if (!improved) break;\n    }\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, T;\n    cin >> N >> T;\n    vector<string> grid_str(N);\n    for (int i = 0; i < N; i++) {\n        cin >> grid_str[i];\n    }\n\n    vector<vector<int>> grid(N, vector<int>(N));\n    int empty_i = -1, empty_j = -1;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            char c = tolower(grid_str[i][j]);\n            grid[i][j] = (c >= '0' && c <= '9') ? (c - '0') : (10 + (c - 'a'));\n            if (grid[i][j] == 0) {\n                empty_i = i;\n                empty_j = j;\n            }\n        }\n    }\n\n    mt19937 rng(42);\n\n    vector<vector<int>> target = generate_snake_pattern(N);\n\n    vector<State> beam;\n    beam.emplace_back(empty_i, empty_j, \"\", calculate_heuristic(grid, target, empty_i, empty_j, N));\n\n    string best_moves = \"\";\n    int best_cost = INT_MAX;\n    const int beam_width = 20;\n    const int max_steps = min(T, 800);\n\n    for (int step = 0; step < max_steps; step++) {\n        if (beam.empty()) break;\n\n        vector<State> next_beam;\n        next_beam.reserve(beam_width * 2);\n\n        for (const State& state : beam) {\n            if (state.heuristic == 0) {\n                best_moves = state.moves;\n                break;\n            }\n\n            int current_cost = static_cast<int>(state.moves.size()) + state.heuristic;\n            if (current_cost < best_cost) {\n                best_cost = current_cost;\n                best_moves = state.moves;\n            }\n\n            vector<pair<int, int>> dirs = {{-1,0}, {1,0}, {0,-1}, {0,1}};\n            char dir_chars[] = {'U','D','L','R'};\n\n            for (int d = 0; d < 4; d++) {\n                int ni = state.empty_i + dirs[d].first;\n                int nj = state.empty_j + dirs[d].second;\n                if (ni < 0 || ni >= N || nj < 0 || nj >= N) continue;\n\n                vector<vector<int>> new_grid = grid;\n                swap(new_grid[state.empty_i][state.empty_j], new_grid[ni][nj]);\n\n                string new_moves = state.moves + dir_chars[d];\n                int new_heuristic = calculate_heuristic(new_grid, target, ni, nj, N);\n\n                if (new_heuristic == 0) {\n                    cout << new_moves << endl;\n                    return 0;\n                }\n\n                if (new_moves.size() < static_cast<size_t>(T)) {\n                    next_beam.emplace_back(ni, nj, new_moves, new_heuristic);\n                }\n            }\n        }\n\n        if (next_beam.empty()) break;\n\n        sort(next_beam.begin(), next_beam.end(), [](const State& a, const State& b) {\n            return (a.heuristic + static_cast<int>(a.moves.size())) <\n                   (b.heuristic + static_cast<int>(b.moves.size()));\n        });\n\n        beam.clear();\n        int take = min(static_cast<int>(next_beam.size()), beam_width);\n        for (int i = 0; i < take; i++) {\n            beam.push_back(next_beam[i]);\n        }\n    }\n\n    optimize_path(grid, empty_i, empty_j, target, best_moves, N, rng);\n\n    cout << best_moves << endl;\n    return 0;\n}","ahc012":"#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\n\nconst double PI = acos(-1.0);\n\nstruct Point {\n    ll x, y;\n    Point(ll x=0, ll y=0) : x(x), y(y) {}\n    bool operator==(const Point& other) const {\n        return x == other.x && y == other.y;\n    }\n    bool operator<(const Point& other) const {\n        return x < other.x || (x == other.x && y < other.y);\n    }\n};\n\nstruct Line {\n    Point p1, p2;\n    Line(Point p1=Point(), Point p2=Point()) : p1(p1), p2(p2) {}\n};\n\nll cross(const Point& o, const Point& a, const Point& b) {\n    return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x);\n}\n\nvector<Point> convex_hull(vector<Point> pts) {\n    size_t n = pts.size();\n    if (n <= 1) return pts;\n    sort(pts.begin(), pts.end());\n    vector<Point> hull;\n    hull.reserve(n+1);\n    for (size_t i = 0; i < n; ++i) {\n        while (hull.size() >= 2 && cross(hull[hull.size()-2], hull.back(), pts[i]) <= 0) {\n            hull.pop_back();\n        }\n        hull.push_back(pts[i]);\n    }\n    size_t lower_len = hull.size();\n    for (size_t i = n-2; i < n; --i) {\n        while (hull.size() > lower_len && cross(hull[hull.size()-2], hull.back(), pts[i]) <= 0) {\n            hull.pop_back();\n        }\n        hull.push_back(pts[i]);\n    }\n    if (hull.size() > 1) hull.pop_back();\n    return hull;\n}\n\nll side(const Point& p1, const Point& p2, const Point& p) {\n    return (p2.x - p1.x) * (p.y - p1.y) - (p2.y - p1.y) * (p.x - p1.x);\n}\n\nbool line_passes_through(const Line& line, const vector<Point>& straws) {\n    for (const Point& s : straws) {\n        if (side(line.p1, line.p2, s) == 0) {\n            return true;\n        }\n    }\n    return false;\n}\n\nint count_in_half(const vector<int>& indices, const vector<Point>& straws, const Line& line) {\n    int cnt = 0;\n    for (int idx : indices) {\n        if (side(line.p1, line.p2, straws[idx]) > 0) {\n            cnt++;\n        }\n    }\n    return cnt;\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\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> straws(N);\n    for (int i = 0; i < N; i++) {\n        cin >> straws[i].x >> straws[i].y;\n    }\n\n    // Calculate target needs\n    vector<int> target_needs(11, 0);\n    for (int d = 1; d <= 10; d++) {\n        target_needs[d] = a[d];\n    }\n\n    struct Region {\n        vector<int> indices;\n        int count() const { return indices.size(); }\n    };\n    vector<Region> regions;\n    regions.push_back(Region());\n    for (int i = 0; i < N; i++) {\n        regions[0].indices.push_back(i);\n    }\n\n    vector<int> b(11, 0); // Current counts of pieces with d strawberries\n    vector<Line> cuts;\n\n    for (int cut_num = 0; cut_num < K; cut_num++) {\n        // Check if we've satisfied all requirements\n        bool satisfied = true;\n        for (int d = 1; d <= 10; d++) {\n            if (b[d] < a[d]) {\n                satisfied = false;\n                break;\n            }\n        }\n        if (satisfied) break;\n\n        int best_delta = 0;\n        Line best_line;\n        size_t best_region_idx = 0;\n        int best_count1 = 0, best_count2 = 0;\n        vector<int> best_left_indices, best_right_indices;\n\n        for (size_t reg_idx = 0; reg_idx < regions.size(); reg_idx++) {\n            Region& reg = regions[reg_idx];\n            int m = reg.count();\n            if (m <= 1) continue;\n\n            // Calculate bounding box\n            ll min_x = 1e18, max_x = -1e18;\n            ll min_y = 1e18, max_y = -1e18;\n            for (int idx : reg.indices) {\n                min_x = min(min_x, straws[idx].x);\n                max_x = max(max_x, straws[idx].x);\n                min_y = min(min_y, straws[idx].y);\n                max_y = max(max_y, straws[idx].y);\n            }\n\n            // Generate candidates\n            vector<Line> candidates;\n\n            // Priority 1: Lines that create exact needed sizes\n            for (int target_size : {1, 2, 3, 5, 7, 10}) {\n                if (target_needs[target_size] <= 0) continue;\n                if (m < target_size) continue;\n\n                // Try vertical split\n                vector<pair<ll, int>> x_sorted;\n                for (int idx : reg.indices) {\n                    x_sorted.emplace_back(straws[idx].x, idx);\n                }\n                sort(x_sorted.begin(), x_sorted.end());\n                if (target_size <= (int)x_sorted.size()) {\n                    ll split_x = x_sorted[target_size-1].first;\n                    candidates.emplace_back(Point(split_x+1, min_y-1), Point(split_x+1, max_y+1));\n                }\n\n                // Try horizontal split\n                vector<pair<ll, int>> y_sorted;\n                for (int idx : reg.indices) {\n                    y_sorted.emplace_back(straws[idx].y, idx);\n                }\n                sort(y_sorted.begin(), y_sorted.end());\n                if (target_size <= (int)y_sorted.size()) {\n                    ll split_y = y_sorted[target_size-1].first;\n                    candidates.emplace_back(Point(min_x-1, split_y+1), Point(max_x+1, split_y+1));\n                }\n            }\n\n            // Priority 2: Median splits\n            ll med_x = (min_x + max_x) / 2;\n            ll med_y = (min_y + max_y) / 2;\n            candidates.emplace_back(Point(med_x, min_y-1), Point(med_x, max_y+1));\n            candidates.emplace_back(Point(min_x-1, med_y), Point(max_x+1, med_y));\n\n            // Priority 3: Convex hull edges\n            vector<Point> hull_pts;\n            for (int idx : reg.indices) {\n                hull_pts.push_back(straws[idx]);\n            }\n            vector<Point> hull = convex_hull(hull_pts);\n            if (hull.size() >= 2) {\n                for (size_t i = 0; i < hull.size(); i++) {\n                    candidates.emplace_back(hull[i], hull[(i+1) % hull.size()]);\n                }\n            }\n\n            // Priority 4: Lines through extreme points (8 directions)\n            if (!hull.empty()) {\n                for (const Point& p : hull) {\n                    for (int angle_idx = 0; angle_idx < 8; angle_idx++) {\n                        double angle = angle_idx * PI / 4.0;\n                        Point dir((ll)(cos(angle) * 1e9), (ll)(sin(angle) * 1e9));\n                        candidates.emplace_back(p, Point(p.x + dir.x, p.y + dir.y));\n                    }\n                }\n            }\n\n            // Priority 5: Lines through pairs of points\n            for (size_t i = 0; i < min<size_t>(20, reg.indices.size()); i++) {\n                for (size_t j = i+1; j < min<size_t>(20, reg.indices.size()); j++) {\n                    candidates.emplace_back(straws[reg.indices[i]], straws[reg.indices[j]]);\n                }\n            }\n\n            // Evaluate candidates\n            for (const Line& line : candidates) {\n                if (line_passes_through(line, straws)) continue;\n\n                int count1 = count_in_half(reg.indices, straws, line);\n                int count2 = m - count1;\n\n                // Calculate potential gain\n                vector<int> new_b = b;\n                if (m <= 10) new_b[m]--;\n                if (count1 <= 10) new_b[count1]++;\n                if (count2 <= 10) new_b[count2]++;\n\n                int old_score = 0, new_score = 0;\n                for (int d = 1; d <= 10; d++) {\n                    old_score += min(a[d], b[d]);\n                    new_score += min(a[d], new_b[d]);\n                }\n                int delta = new_score - old_score;\n\n                if (delta > best_delta) {\n                    best_delta = delta;\n                    best_line = line;\n                    best_region_idx = reg_idx;\n                    best_count1 = count1;\n                    best_count2 = count2;\n                    best_left_indices.clear();\n                    best_right_indices.clear();\n                    for (int idx : reg.indices) {\n                        if (side(line.p1, line.p2, straws[idx]) > 0) {\n                            best_left_indices.push_back(idx);\n                        } else {\n                            best_right_indices.push_back(idx);\n                        }\n                    }\n                }\n            }\n        }\n\n        if (best_delta <= 0) break;\n\n        // Update counts\n        if (regions[best_region_idx].count() <= 10) {\n            b[regions[best_region_idx].count()]--;\n        }\n        if (best_count1 <= 10) b[best_count1]++;\n        if (best_count2 <= 10) b[best_count2]++;\n\n        // Update target needs\n        for (int d = 1; d <= 10; d++) {\n            target_needs[d] = max(0, a[d] - b[d]);\n        }\n\n        Region left_reg{best_left_indices};\n        Region right_reg{best_right_indices};\n        vector<Region> new_regions;\n        for (size_t i = 0; i < regions.size(); i++) {\n            if (i != best_region_idx) new_regions.push_back(regions[i]);\n        }\n        if (!left_reg.indices.empty()) new_regions.push_back(left_reg);\n        if (!right_reg.indices.empty()) new_regions.push_back(right_reg);\n        regions = move(new_regions);\n\n        cuts.push_back(best_line);\n    }\n\n    cout << cuts.size() << '\\n';\n    for (const Line& line : cuts) {\n        cout << line.p1.x << ' ' << line.p1.y << ' '\n             << line.p2.x << ' ' << line.p2.y << '\\n';\n    }\n\n    return 0;\n}","ahc014":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Operation {\n    int x1, y1, x2, y2, x3, y3, x4, y4;\n};\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M;\n    cin >> N >> M;\n    int c = (N - 1) / 2;\n\n    vector<vector<bool>> grid(N, vector<bool>(N, false));\n    vector<pair<int, int>> dots;\n    for (int i = 0; i < M; i++) {\n        int x, y;\n        cin >> x >> y;\n        grid[x][y] = true;\n        dots.emplace_back(x, y);\n    }\n\n    vector<tuple<long long, int, int>> candidate_list;\n    for (int x = 0; x < N; x++) {\n        for (int y = 0; y < N; y++) {\n            long long dx = x - c;\n            long long dy = y - c;\n            long long weight = dx * dx + dy * dy + 1;\n            candidate_list.emplace_back(-weight, x, y);\n        }\n    }\n    sort(candidate_list.begin(), candidate_list.end());\n\n    vector<vector<bool>> hor_used(N-1, vector<bool>(N, false));\n    vector<vector<bool>> ver_used(N, vector<bool>(N-1, false));\n    vector<vector<bool>> diag1_used(N-1, vector<bool>(N-1, false));\n    vector<vector<bool>> diag2_used(N-1, vector<bool>(N-1, false));\n\n    vector<Operation> operations;\n\n    bool added_something;\n    do {\n        added_something = false;\n        for (auto [neg_weight, x, y] : candidate_list) {\n            if (grid[x][y]) continue;\n\n            bool found = false;\n\n            auto check_axis = [&](int x1, int y1, int x2, int y2) -> bool {\n                if (x1 == x2 || y1 == y2) return false;\n                int x_min = min(x1, x2);\n                int x_max = max(x1, x2);\n                int y_min = min(y1, y2);\n                int y_max = max(y1, y2);\n\n                bool cond2 = true;\n                for (int yy = y_min + 1; yy < y_max; yy++) {\n                    if (grid[x_min][yy] || grid[x_max][yy]) {\n                        cond2 = false;\n                        break;\n                    }\n                }\n                if (!cond2) return false;\n                for (int xx = x_min + 1; xx < x_max; xx++) {\n                    if (grid[xx][y_min] || grid[xx][y_max]) {\n                        cond2 = false;\n                        break;\n                    }\n                }\n                if (!cond2) return false;\n\n                bool cond3 = true;\n                for (int yy = y_min; yy < y_max; yy++) {\n                    if (ver_used[x_min][yy] || ver_used[x_max][yy]) {\n                        cond3 = false;\n                        break;\n                    }\n                }\n                if (!cond3) return false;\n                for (int xx = x_min; xx < x_max; xx++) {\n                    if (hor_used[xx][y_min] || hor_used[xx][y_max]) {\n                        cond3 = false;\n                        break;\n                    }\n                }\n                return cond3;\n            };\n\n            auto mark_axis = [&](int x1, int y1, int x2, int y2) {\n                int x_min = min(x1, x2);\n                int x_max = max(x1, x2);\n                int y_min = min(y1, y2);\n                int y_max = max(y1, y2);\n                for (int yy = y_min; yy < y_max; yy++) {\n                    ver_used[x_min][yy] = true;\n                    ver_used[x_max][yy] = true;\n                }\n                for (int xx = x_min; xx < x_max; xx++) {\n                    hor_used[xx][y_min] = true;\n                    hor_used[xx][y_max] = true;\n                }\n            };\n\n            auto check_diag = [&](int x, int y) -> bool {\n                vector<pair<int, int>> d1_points;\n                vector<pair<int, int>> d2_points;\n                for (int dx = -N; dx <= N; dx++) {\n                    int nx = x + dx;\n                    int ny = y + dx;\n                    if (nx >= 0 && nx < N && ny >= 0 && ny < N && grid[nx][ny]) {\n                        d1_points.emplace_back(nx, ny);\n                    }\n                    nx = x + dx;\n                    ny = y - dx;\n                    if (nx >= 0 && nx < N && ny >= 0 && ny < N && grid[nx][ny]) {\n                        d2_points.emplace_back(nx, ny);\n                    }\n                }\n\n                for (auto& a : d1_points) {\n                    for (auto& b : d2_points) {\n                        int cx = a.first + b.first - x;\n                        int cy = a.second + b.second - y;\n                        if (cx < 0 || cx >= N || cy < 0 || cy >= N) continue;\n                        if (!grid[cx][cy]) continue;\n\n                        vector<pair<int, int>> corners = {{x,y}, a, {cx,cy}, b};\n                        bool cond2 = true;\n                        for (int i = 0; i < 4; i++) {\n                            int x1 = corners[i].first, y1 = corners[i].second;\n                            int x2 = corners[(i+1)%4].first, y2 = corners[(i+1)%4].second;\n                            int dx = x2 - x1, dy = y2 - y1;\n                            int steps = max(abs(dx), abs(dy));\n                            for (int step = 1; step < steps; step++) {\n                                int px = x1 + (dx * step) / steps;\n                                int py = y1 + (dy * step) / steps;\n                                bool is_corner = false;\n                                for (auto& corner : corners) {\n                                    if (px == corner.first && py == corner.second) {\n                                        is_corner = true;\n                                        break;\n                                    }\n                                }\n                                if (!is_corner && grid[px][py]) {\n                                    cond2 = false;\n                                    break;\n                                }\n                            }\n                            if (!cond2) break;\n                        }\n                        if (!cond2) continue;\n\n                        bool cond3 = true;\n                        for (int i = 0; i < 4; i++) {\n                            int x1 = corners[i].first, y1 = corners[i].second;\n                            int x2 = corners[(i+1)%4].first, y2 = corners[(i+1)%4].second;\n                            int dx = x2 - x1, dy = y2 - y1;\n                            int steps = max(abs(dx), abs(dy));\n                            for (int step = 1; step <= steps; step++) {\n                                int px = x1 + (dx * step) / steps;\n                                int py = y1 + (dy * step) / steps;\n                                int ex, ey;\n                                if (dx == 0) {\n                                    ex = px;\n                                    ey = min(y1, y2) + (step <= abs(dy) ? step-1 : abs(dy)-1);\n                                } else if (dy == 0) {\n                                    ex = min(x1, x2) + (step <= abs(dx) ? step-1 : abs(dx)-1);\n                                    ey = py;\n                                } else if (abs(dx) == abs(dy)) {\n                                    if (dx * dy > 0) {\n                                        if (dx > 0) {\n                                            ex = px;\n                                            ey = py;\n                                        } else {\n                                            ex = px + 1;\n                                            ey = py + 1;\n                                        }\n                                    } else {\n                                        if (dx > 0) {\n                                            ex = px;\n                                            ey = py + 1;\n                                        } else {\n                                            ex = px + 1;\n                                            ey = py;\n                                        }\n                                    }\n                                } else {\n                                    continue;\n                                }\n                                if (ex >= N-1 || ey >= N) continue;\n                                if (dx == 0 || dy == 0) {\n                                    if (dx == 0) {\n                                        if (ver_used[px][min(y1,y2) + step - 1]) {\n                                            cond3 = false;\n                                            break;\n                                        }\n                                    } else {\n                                        if (hor_used[min(x1,x2) + step - 1][py]) {\n                                            cond3 = false;\n                                            break;\n                                        }\n                                    }\n                                } else {\n                                    int type = (dx * dy > 0) ? 1 : 2;\n                                    int sx = px, sy = py;\n                                    if (type == 1) {\n                                        if (diag1_used[sx][sy]) {\n                                            cond3 = false;\n                                            break;\n                                        }\n                                    } else {\n                                        if (sx >= 0 && sy >= 0 && sx < N-1 && sy < N-1 && diag2_used[sx][sy]) {\n                                            cond3 = false;\n                                            break;\n                                        }\n                                    }\n                                }\n                            }\n                            if (!cond3) break;\n                        }\n                        if (cond2 && cond3) {\n                            grid[x][y] = true;\n                            dots.emplace_back(x, y);\n                            operations.push_back({x, y, a.first, a.second, cx, cy, b.first, b.second});\n                            for (int i = 0; i < 4; i++) {\n                                int x1 = corners[i].first, y1 = corners[i].second;\n                                int x2 = corners[(i+1)%4].first, y2 = corners[(i+1)%4].second;\n                                int dx = x2 - x1, dy = y2 - y1;\n                                int steps = max(abs(dx), abs(dy));\n                                for (int step = 1; step <= steps; step++) {\n                                    int px = x1 + (dx * step) / steps;\n                                    int py = y1 + (dy * step) / steps;\n                                    if (dx == 0) {\n                                        ver_used[x1][min(y1,y2) + step - 1] = true;\n                                    } else if (dy == 0) {\n                                        hor_used[min(x1,x2) + step - 1][y1] = true;\n                                    } else if (abs(dx) == abs(dy)) {\n                                        if (dx * dy > 0) {\n                                            if (dx > 0) diag1_used[px][py] = true;\n                                            else diag1_used[px][py] = true;\n                                        } else {\n                                            if (dx > 0) diag2_used[px][py] = true;\n                                            else diag2_used[px][py] = true;\n                                        }\n                                    }\n                                }\n                            }\n                            return true;\n                        }\n                    }\n                }\n                return false;\n            };\n\n            if (x > 0 && x < N-1 && y > 0 && y < N-1) {\n                if (check_diag(x, y)) {\n                    found = true;\n                }\n            }\n\n            if (!found) {\n                vector<int> up_dots, down_dots, left_dots, right_dots;\n                for (int yy = y+1; yy < N; yy++) if (grid[x][yy]) up_dots.push_back(yy);\n                for (int yy = 0; yy < y; yy++) if (grid[x][yy]) down_dots.push_back(yy);\n                for (int xx = 0; xx < x; xx++) if (grid[xx][y]) left_dots.push_back(xx);\n                for (int xx = x+1; xx < N; xx++) if (grid[xx][y]) right_dots.push_back(xx);\n\n                if (!up_dots.empty() && !right_dots.empty()) {\n                    for (int yA : up_dots) {\n                        for (int xB : right_dots) {\n                            if (check_axis(x, y, xB, yA)) {\n                                grid[x][y] = true;\n                                dots.emplace_back(x, y);\n                                operations.push_back({x, y, x, yA, xB, yA, xB, y});\n                                mark_axis(x, y, xB, yA);\n                                found = true;\n                                break;\n                            }\n                        }\n                        if (found) break;\n                    }\n                }\n                if (!found && !up_dots.empty() && !left_dots.empty()) {\n                    for (int yA : up_dots) {\n                        for (int xB : left_dots) {\n                            if (check_axis(x, y, xB, yA)) {\n                                grid[x][y] = true;\n                                dots.emplace_back(x, y);\n                                operations.push_back({x, y, x, yA, xB, yA, xB, y});\n                                mark_axis(x, y, xB, yA);\n                                found = true;\n                                break;\n                            }\n                        }\n                        if (found) break;\n                    }\n                }\n                if (!found && !down_dots.empty() && !right_dots.empty()) {\n                    for (int yA : down_dots) {\n                        for (int xB : right_dots) {\n                            if (check_axis(x, y, xB, yA)) {\n                                grid[x][y] = true;\n                                dots.emplace_back(x, y);\n                                operations.push_back({x, y, x, yA, xB, yA, xB, y});\n                                mark_axis(x, y, xB, yA);\n                                found = true;\n                                break;\n                            }\n                        }\n                        if (found) break;\n                    }\n                }\n                if (!found && !down_dots.empty() && !left_dots.empty()) {\n                    for (int yA : down_dots) {\n                        for (int xB : left_dots) {\n                            if (check_axis(x, y, xB, yA)) {\n                                grid[x][y] = true;\n                                dots.emplace_back(x, y);\n                                operations.push_back({x, y, x, yA, xB, yA, xB, y});\n                                mark_axis(x, y, xB, yA);\n                                found = true;\n                                break;\n                            }\n                        }\n                        if (found) break;\n                    }\n                }\n            }\n\n            if (found) {\n                added_something = true;\n                break;\n            }\n        }\n    } while (added_something);\n\n    cout << operations.size() << '\\n';\n    for (auto& op : operations) {\n        cout << op.x1 << ' ' << op.y1 << ' '\n             << op.x2 << ' ' << op.y2 << ' '\n             << op.x3 << ' ' << op.y3 << ' '\n             << op.x4 << ' ' << op.y4 << '\\n';\n    }\n\n    return 0;\n}","ahc015":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\nusing namespace std;\n\nint compute_sum_squares(int grid[10][10]) {\n    bool visited[10][10] = {{false}};\n    int total = 0;\n\n    for (int i = 0; i < 10; i++) {\n        for (int j = 0; j < 10; j++) {\n            if (grid[i][j] != 0 && !visited[i][j]) {\n                int size = 0;\n                queue<pair<int, int>> q;\n                q.push({i, j});\n                visited[i][j] = true;\n                int flavor = grid[i][j];\n\n                while (!q.empty()) {\n                    auto [x, y] = q.front();\n                    q.pop();\n                    size++;\n                    const int dx[4] = {-1, 1, 0, 0};\n                    const int dy[4] = {0, 0, -1, 1};\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 < 10 && ny >= 0 && ny < 10 && !visited[nx][ny] && grid[nx][ny] == flavor) {\n                            visited[nx][ny] = true;\n                            q.push({nx, ny});\n                        }\n                    }\n                }\n                total += size * size;\n            }\n        }\n    }\n    return total;\n}\n\nint get_flavor_cluster_size(int grid[10][10], int flavor) {\n    bool visited[10][10] = {{false}};\n    int max_size = 0;\n\n    for (int i = 0; i < 10; i++) {\n        for (int j = 0; j < 10; j++) {\n            if (grid[i][j] == flavor && !visited[i][j]) {\n                int size = 0;\n                queue<pair<int, int>> q;\n                q.push({i, j});\n                visited[i][j] = true;\n\n                while (!q.empty()) {\n                    auto [x, y] = q.front();\n                    q.pop();\n                    size++;\n                    const int dx[4] = {-1, 1, 0, 0};\n                    const int dy[4] = {0, 0, -1, 1};\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 < 10 && ny >= 0 && ny < 10 && !visited[nx][ny] && grid[nx][ny] == flavor) {\n                            visited[nx][ny] = true;\n                            q.push({nx, ny});\n                        }\n                    }\n                }\n                max_size = max(max_size, size);\n            }\n        }\n    }\n    return max_size;\n}\n\nvoid tilt(int grid[10][10], char dir) {\n    int new_grid[10][10] = {{0}};\n\n    if (dir == 'F') { // Move candies toward front (row 0)\n        for (int j = 0; j < 10; j++) {\n            int write_pos = 0;\n            for (int i = 0; i < 10; i++) {\n                if (grid[i][j] != 0) {\n                    new_grid[write_pos][j] = grid[i][j];\n                    write_pos++;\n                }\n            }\n        }\n    } else if (dir == 'B') { // Move candies toward back (row 9)\n        for (int j = 0; j < 10; j++) {\n            int write_pos = 9;\n            for (int i = 9; i >= 0; i--) {\n                if (grid[i][j] != 0) {\n                    new_grid[write_pos][j] = grid[i][j];\n                    write_pos--;\n                }\n            }\n        }\n    } else if (dir == 'L') { // Move candies toward left (column 0)\n        for (int i = 0; i < 10; i++) {\n            int write_pos = 0;\n            for (int j = 0; j < 10; j++) {\n                if (grid[i][j] != 0) {\n                    new_grid[i][write_pos] = grid[i][j];\n                    write_pos++;\n                }\n            }\n        }\n    } else if (dir == 'R') { // Move candies toward right (column 9)\n        for (int i = 0; i < 10; i++) {\n            int write_pos = 9;\n            for (int j = 9; j >= 0; j--) {\n                if (grid[i][j] != 0) {\n                    new_grid[i][write_pos] = grid[i][j];\n                    write_pos--;\n                }\n            }\n        }\n    }\n\n    for (int i = 0; i < 10; i++) {\n        for (int j = 0; j < 10; j++) {\n            grid[i][j] = new_grid[i][j];\n        }\n    }\n}\n\nint main() {\n    vector<int> flavors(100);\n    for (int i = 0; i < 100; i++) {\n        cin >> flavors[i];\n    }\n\n    int grid[10][10] = {{0}};\n    vector<char> directions = {'F', 'B', 'L', 'R'};\n\n    for (int t = 0; t < 100; t++) {\n        int p;\n        cin >> p;\n\n        if (t == 99) {\n            break;\n        }\n\n        int r = (p - 1) / 10;\n        int c = (p - 1) % 10;\n        int new_flavor = flavors[t];\n        grid[r][c] = new_flavor;\n\n        char best_dir = 'F';\n        int best_score = -1;\n        int best_cluster_score = -1;\n\n        for (char dir : directions) {\n            int temp_grid[10][10];\n            for (int i = 0; i < 10; i++) {\n                for (int j = 0; j < 10; j++) {\n                    temp_grid[i][j] = grid[i][j];\n                }\n            }\n            tilt(temp_grid, dir);\n            int score = compute_sum_squares(temp_grid);\n            int cluster_score = get_flavor_cluster_size(temp_grid, new_flavor);\n\n            if (score > best_score || (score == best_score && cluster_score > best_cluster_score)) {\n                best_score = score;\n                best_cluster_score = cluster_score;\n                best_dir = dir;\n            }\n        }\n\n        cout << best_dir << '\\n';\n        cout.flush();\n        tilt(grid, best_dir);\n    }\n\n    return 0;\n}","ahc016":"#include <bits/stdc++.h>\nusing namespace std;\n\n// Function to compute the index of edge (i,j) in the string, with i<j\nlong long get_index(int i, int j, int N) {\n    return 1LL * i * (2 * N - i - 1) / 2 + (j - i - 1);\n}\n\nint main() {\n    int M;\n    double eps;\n    cin >> M >> eps;\n\n    const int N = 100;\n    const int T = N * (N - 1) / 2;\n\n    cout << N << endl;\n    cout.flush();\n\n    for (int s = 0; s < M; s++) {\n        int k;\n        if (s < 96) {\n            k = 4 + s;\n        } else {\n            k = 100 - (s - 96);\n        }\n        k = max(4, min(k, 100));\n\n        string graph(T, '0');\n        for (int i = 0; i < k; i++) {\n            for (int j = i + 1; j < k; j++) {\n                long long idx = get_index(i, j, N);\n                graph[idx] = '1';\n            }\n        }\n        cout << graph << endl;\n    }\n    cout.flush();\n\n    for (int q = 0; q < 100; q++) {\n        string H;\n        cin >> H;\n\n        vector<int> deg(N, 0);\n        for (int i = 0; i < N; i++) {\n            for (int j = i + 1; j < N; j++) {\n                long long idx = get_index(i, j, N);\n                if (H[idx] == '1') {\n                    deg[i]++;\n                    deg[j]++;\n                }\n            }\n        }\n\n        vector<int> sorted_deg = deg;\n        sort(sorted_deg.begin(), sorted_deg.end());\n\n        double best_score = 1e300;\n        int best_s = 0;\n\n        for (int s = 0; s < M; s++) {\n            int k;\n            if (s < 96) {\n                k = 4 + s;\n            } else {\n                k = 100 - (s - 96);\n            }\n            k = max(4, min(k, 100));\n\n            int low_count = N - k;\n            int high_count = k;\n\n            double expected_low = 99.0 * eps;\n            double expected_high = (k - 1) + (101 - 2 * k) * eps;\n\n            double score = 0.0;\n            if (low_count > 0) {\n                double sum_low = 0.0;\n                for (int i = 0; i < low_count; i++) {\n                    sum_low += sorted_deg[i];\n                }\n                double mean_low = sum_low / low_count;\n                score += (mean_low - expected_low) * (mean_low - expected_low);\n            }\n            if (high_count > 0) {\n                double sum_high = 0.0;\n                for (int i = low_count; i < N; i++) {\n                    sum_high += sorted_deg[i];\n                }\n                double mean_high = sum_high / high_count;\n                score += (mean_high - expected_high) * (mean_high - expected_high);\n            }\n\n            if (score < best_score) {\n                best_score = score;\n                best_s = s;\n            }\n        }\n\n        cout << best_s << endl;\n        cout.flush();\n    }\n\n    return 0;\n}","ahc017":"#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\nconst ll INF = 1e18;\n\nstruct Edge {\n    int u, v, idx;\n    ll w;\n};\n\nvector<ll> dijkstra(const vector<vector<pair<int, ll>>>& adj, int start) {\n    int n = adj.size();\n    vector<ll> dist(n, INF);\n    priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<>> pq;\n    dist[start] = 0;\n    pq.push({0, start});\n    while (!pq.empty()) {\n        auto [d, u] = pq.top();\n        pq.pop();\n        if (d != dist[u]) continue;\n        for (auto [v, w] : adj[u]) {\n            ll nd = d + w;\n            if (nd < dist[v]) {\n                dist[v] = nd;\n                pq.push({nd, v});\n            }\n        }\n    }\n    return dist;\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, D, K;\n    cin >> N >> M >> D >> K;\n\n    vector<Edge> edges(M);\n    vector<vector<pair<int, ll>>> adj(N);\n    vector<int> deg(N, 0);\n\n    for (int i = 0; i < M; i++) {\n        int u, v;\n        ll w;\n        cin >> u >> v >> w;\n        u--; v--;\n        edges[i] = {u, v, i, w};\n        adj[u].emplace_back(v, w);\n        adj[v].emplace_back(u, w);\n        deg[u]++;\n        deg[v]++;\n    }\n\n    for (int i = 0; i < N; i++) {\n        int x, y;\n        cin >> x >> y;\n    }\n\n    // Simplified edge importance calculation using 3 key nodes\n    vector<double> edge_importance(M, 0.0);\n    vector<int> sample_nodes = {0, N/2, N-1};\n\n    for (int s : sample_nodes) {\n        auto dist = dijkstra(adj, s);\n\n        // Simple importance contribution: edges that are on shortest paths from s\n        for (int i = 0; i < M; i++) {\n            int u = edges[i].u;\n            int v = edges[i].v;\n            ll w = edges[i].w;\n            if (dist[u] != INF && dist[v] != INF) {\n                if (abs(dist[u] - dist[v]) == w) {\n                    edge_importance[i] += 1.0 / (1.0 + min(dist[u], dist[v]));\n                }\n            }\n        }\n    }\n\n    // Calculate final edge scores using simple heuristic\n    vector<double> score(M);\n    for (int i = 0; i < M; i++) {\n        double weight_factor = 1.0 / (1.0 + log(edges[i].w + 1));\n        double degree_factor = min(deg[edges[i].u], deg[edges[i].v]) / 100.0;\n        score[i] = edge_importance[i] * weight_factor * (1.0 + degree_factor);\n    }\n\n    // Sort edges by score descending\n    vector<pair<double, int>> vec;\n    for (int i = 0; i < M; i++) {\n        vec.emplace_back(score[i], i);\n    }\n    sort(vec.begin(), vec.end(), greater<>());\n\n    // Initialize day assignments\n    vector<int> day_count(D, 0);\n    vector<ll> day_weight(D, 0);\n    vector<int> ans(M);\n\n    // Greedy assignment respecting K constraint\n    for (auto [s, idx] : vec) {\n        int best_day = 0;\n        for (int d = 1; d < D; d++) {\n            if (day_count[d] < day_count[best_day] ||\n                (day_count[d] == day_count[best_day] && day_weight[d] < day_weight[best_day])) {\n                best_day = d;\n            }\n        }\n        ans[idx] = best_day + 1;\n        day_count[best_day]++;\n        day_weight[best_day] += edges[idx].w;\n    }\n\n    // Simple balancing: try to move edges from overloaded days\n    for (int iter = 0; iter < 200; iter++) {\n        int i = rand() % M;\n        int d1 = ans[i] - 1;\n        if (day_count[d1] <= 1) continue;\n\n        int best_day = -1;\n        for (int d = 0; d < D; d++) {\n            if (d == d1) continue;\n            if (day_count[d] >= K) continue;\n            if (best_day == -1 || day_weight[d] < day_weight[best_day]) {\n                best_day = d;\n            }\n        }\n\n        if (best_day != -1) {\n            ans[i] = best_day + 1;\n            day_count[d1]--;\n            day_count[best_day]++;\n            day_weight[d1] -= edges[i].w;\n            day_weight[best_day] += edges[i].w;\n        }\n    }\n\n    // Final verification\n    fill(day_count.begin(), day_count.end(), 0);\n    for (int i = 0; i < M; i++) {\n        day_count[ans[i]-1]++;\n    }\n\n    // Redistribute any overloaded days\n    for (int d = 0; d < D; d++) {\n        while (day_count[d] > K) {\n            for (int i = 0; i < M && day_count[d] > K; i++) {\n                if (ans[i] == d+1) {\n                    for (int new_day = 0; new_day < D && day_count[d] > K; new_day++) {\n                        if (new_day != d && day_count[new_day] < K) {\n                            ans[i] = new_day + 1;\n                            day_count[d]--;\n                            day_count[new_day]++;\n                        }\n                    }\n                }\n            }\n        }\n    }\n\n    // Output the result\n    for (int i = 0; i < M; i++) {\n        if (i) cout << \" \";\n        cout << ans[i];\n    }\n    cout << '\\n';\n\n    return 0;\n}","ahc019":"#include<iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\n#include <tuple>\n#include <climits>\n#include <cmath>\n#include <array>\n#include <set>\n#include <map>\nusing namespace std;\n\nconst vector<array<array<int, 3>, 3>> rotation_matrices = {\n    {{\n        {1, 0, 0},\n        {0, 1, 0},\n        {0, 0, 1}\n    }},\n    {{\n        {1, 0, 0},\n        {0, 0, -1},\n        {0, 1, 0}\n    }},\n    {{\n        {1, 0, 0},\n        {0, -1, 0},\n        {0, 0, -1}\n    }},\n    {{\n        {1, 0, 0},\n        {0, 0, 1},\n        {0, -1, 0}\n    }},\n    {{\n        {0, 0, 1},\n        {0, 1, 0},\n        {-1, 0, 0}\n    }},\n    {{\n        {0, 1, 0},\n        {-1, 0, 0},\n        {0, 0, 1}\n    }},\n    {{\n        {0, 0, -1},\n        {-1, 0, 0},\n        {0, 0, -1}\n    }},\n    {{\n        {0, -1, 0},\n        {0, 0, 1},\n        {-1, 0, 0}\n    }},\n    {{\n        {0, 0, -1},\n        {0, -1, 0},\n        {1, 0, 0}\n    }},\n    {{\n        {0, 1, 0},\n        {0, 0, -1},\n        {1, 0, 0}\n    }},\n    {{\n        {0, 0, 1},\n        {1, 0, 0},\n        {0, -1, 0}\n    }},\n    {{\n        {0, -1, 0},\n        {0, 0, -1},\n        {-1, 0, 0}\n    }},\n    {{\n        {-1, 0, 0},\n        {0, 1, 0},\n        {0, 0, -1}\n    }},\n    {{\n        {-1, 0, 0},\n        {0, 0, 1},\n        {0, 1, 0}\n    }},\n    {{\n        {-1, 0, 0},\n        {0, -1, 0},\n        {0, 0, 1}\n    }},\n    {{\n        {-1, 0, 0},\n        {0, 0, -1},\n        {0, -1, 0}\n    }},\n    {{\n        {0, -1, 0},\n        {1, 0, 0},\n        {0, 0, 1}\n    }},\n    {{\n        {0, 0, -1},\n        {1, 0, 0},\n        {0, 1, 0}\n    }},\n    {{\n        {0, 1, 0},\n        {1, 0, 0},\n        {0, 0, -1}\n    }},\n    {{\n        {0, 0, 1},\n        {1, 0, 0},\n        {0, 1, 0}\n    }},\n    {{\n        {0, -1, 0},\n        {0, 0, -1},\n        {1, 0, 0}\n    }},\n    {{\n        {0, 0, -1},\n        {-1, 0, 0},\n        {0, 1, 0}\n    }},\n    {{\n        {0, 1, 0},\n        {0, 0, 1},\n        {-1, 0, 0}\n    }},\n    {{\n        {0, 0, 1},\n        {-1, 0, 0},\n        {0, -1, 0}\n    }}\n};\n\nvector<vector<tuple<int,int,int>>> get_components(vector<vector<vector<bool>>>& grid, int D) {\n    vector<vector<vector<bool>>> visited(D, vector<vector<bool>>(D, vector<bool>(D, false)));\n    vector<vector<tuple<int,int,int>>> components;\n    const int dx[6] = {1, -1, 0, 0, 0, 0};\n    const int dy[6] = {0, 0, 1, -1, 0, 0};\n    const int dz[6] = {0, 0, 0, 0, 1, -1};\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 (grid[x][y][z] && !visited[x][y][z]) {\n                    vector<tuple<int,int,int>> comp;\n                    queue<tuple<int,int,int>> q;\n                    q.push({x, y, z});\n                    visited[x][y][z] = true;\n                    while (!q.empty()) {\n                        auto [cx, cy, cz] = q.front(); q.pop();\n                        comp.push_back({cx, cy, cz});\n                        for (int i = 0; i < 6; i++) {\n                            int nx = cx + dx[i];\n                            int ny = cy + dy[i];\n                            int nz = cz + dz[i];\n                            if (nx >= 0 && nx < D && ny >= 0 && ny < D && nz >= 0 && nz < D && grid[nx][ny][nz] && !visited[nx][ny][nz]) {\n                                visited[nx][ny][nz] = true;\n                                q.push({nx, ny, nz});\n                            }\n                        }\n                    }\n                    components.push_back(comp);\n                }\n            }\n        }\n    }\n    return components;\n}\n\nbool find_placement(const vector<tuple<int,int,int>>& shape, const vector<vector<vector<bool>>>& rem, int D, int& tx, int& ty, int& tz) {\n    int w = 0, h = 0, d = 0;\n    for (auto [x,y,z] : shape) {\n        w = max(w, x+1);\n        h = max(h, y+1);\n        d = max(d, z+1);\n    }\n    for (tx = 0; tx <= D - w; tx++) {\n        for (ty = 0; ty <= D - h; ty++) {\n            for (tz = 0; tz <= D - d; tz++) {\n                bool valid = true;\n                for (auto [x,y,z] : shape) {\n                    int gx = tx + x;\n                    int gy = ty + y;\n                    int gz = tz + z;\n                    if (gx < 0 || gx >= D || gy < 0 || gy >= D || gz < 0 || gz >= D) {\n                        valid = false;\n                        break;\n                    }\n                    if (!rem[gx][gy][gz]) {\n                        valid = false;\n                        break;\n                    }\n                }\n                if (valid) {\n                    return true;\n                }\n            }\n        }\n    }\n    return false;\n}\n\nvoid place_block(vector<vector<vector<int>>>& grid, const vector<tuple<int,int,int>>& shape, int tx, int ty, int tz, int idx) {\n    for (auto [x,y,z] : shape) {\n        int gx = tx + x;\n        int gy = ty + y;\n        int gz = tz + z;\n        if (gx >= 0 && gx < (int)grid.size() && gy >=0 && gy < (int)grid[0].size() && gz >=0 && gz < (int)grid[0][0].size()) {\n            grid[gx][gy][gz] = idx;\n        }\n    }\n}\n\nvoid update_remaining(vector<vector<vector<bool>>>& rem, const vector<tuple<int,int,int>>& shape, int tx, int ty, int tz) {\n    for (auto [x,y,z] : shape) {\n        int gx = tx + x;\n        int gy = ty + y;\n        int gz = tz + z;\n        if (gx >= 0 && gx < (int)rem.size() && gy >=0 && gy < (int)rem[0].size() && gz >=0 && gz < (int)rem[0][0].size()) {\n            rem[gx][gy][gz] = false;\n        }\n    }\n}\n\nbool try_place_shared_block(vector<tuple<int,int,int>>& comp, \n                          vector<vector<vector<bool>>>& R1_rem,\n                          vector<vector<vector<bool>>>& R2_rem,\n                          int D,\n                          int& next_block_idx,\n                          vector<vector<vector<int>>>& b1_grid,\n                          vector<vector<vector<int>>>& b2_grid) {\n    vector<tuple<int,int,int>> orig_comp = comp;\n    for (const auto& rot_mat : rotation_matrices) {\n        vector<tuple<int,int,int>> rotated;\n        for (auto [x,y,z] : orig_comp) {\n            int nx = rot_mat[0][0]*x + rot_mat[0][1]*y + rot_mat[0][2]*z;\n            int ny = rot_mat[1][0]*x + rot_mat[1][1]*y + rot_mat[1][2]*z;\n            int nz = rot_mat[2][0]*x + rot_mat[2][1]*y + rot_mat[2][2]*z;\n            rotated.push_back({nx, ny, nz});\n        }\n\n        int min_x = INT_MAX, max_x = INT_MIN;\n        int min_y = INT_MAX, max_y = INT_MIN;\n        int min_z = INT_MAX, max_z = INT_MIN;\n        for (auto [x,y,z] : rotated) {\n            min_x = min(min_x, x);\n            max_x = max(max_x, x);\n            min_y = min(min_y, y);\n            max_y = max(max_y, y);\n            min_z = min(min_z, z);\n            max_z = max(max_z, z);\n        }\n        int w = max_x - min_x + 1;\n        int h = max_y - min_y + 1;\n        int d = max_z - min_z + 1;\n        if (w <= 0 || h <= 0 || d <= 0) {\n            continue;\n        }\n\n        vector<tuple<int,int,int>> normalized;\n        for (auto [x,y,z] : rotated) {\n            normalized.push_back({x - min_x, y - min_y, z - min_z});\n        }\n\n        int tx1, ty1, tz1;\n        if (!find_placement(normalized, R1_rem, D, tx1, ty1, tz1)) {\n            continue;\n        }\n        int tx2, ty2, tz2;\n        if (!find_placement(normalized, R2_rem, D, tx2, ty2, tz2)) {\n            continue;\n        }\n\n        int idx = next_block_idx++;\n        place_block(b1_grid, normalized, tx1, ty1, tz1, idx);\n        place_block(b2_grid, normalized, tx2, ty2, tz2, idx);\n        update_remaining(R1_rem, normalized, tx1, ty1, tz1);\n        update_remaining(R2_rem, normalized, tx2, ty2, tz2);\n        return true;\n    }\n    return false;\n}\n\nbool try_place_non_shared_block(vector<tuple<int,int,int>>& comp, \n                              vector<vector<vector<bool>>>& target_rem,\n                              int D,\n                              int& next_block_idx,\n                              vector<vector<vector<int>>>& target_grid) {\n    vector<tuple<int,int,int>> orig_comp = comp;\n    for (const auto& rot_mat : rotation_matrices) {\n        vector<tuple<int,int,int>> rotated;\n        for (auto [x,y,z] : orig_comp) {\n            int nx = rot_mat[0][0]*x + rot_mat[0][1]*y + rot_mat[0][2]*z;\n            int ny = rot_mat[1][0]*x + rot_mat[1][1]*y + rot_mat[1][2]*z;\n            int nz = rot_mat[2][0]*x + rot_mat[2][1]*y + rot_mat[2][2]*z;\n            rotated.push_back({nx, ny, nz});\n        }\n\n        int min_x = INT_MAX, max_x = INT_MIN;\n        int min_y = INT_MAX, max_y = INT_MIN;\n        int min_z = INT_MAX, max_z = INT_MIN;\n        for (auto [x,y,z] : rotated) {\n            min_x = min(min_x, x);\n            max_x = max(max_x, x);\n            min_y = min(min_y, y);\n            max_y = max(max_y, y);\n            min_z = min(min_z, z);\n            max_z = max(max_z, z);\n        }\n        int w = max_x - min_x + 1;\n        int h = max_y - min_y + 1;\n        int d = max_z - min_z + 1;\n        if (w <= 0 || h <= 0 || d <= 0) {\n            continue;\n        }\n\n        vector<tuple<int,int,int>> normalized;\n        for (auto [x,y,z] : rotated) {\n            normalized.push_back({x - min_x, y - min_y, z - min_z});\n        }\n\n        int tx, ty, tz;\n        if (!find_placement(normalized, target_rem, D, tx, ty, tz)) {\n            continue;\n        }\n\n        int idx = next_block_idx++;\n        place_block(target_grid, normalized, tx, ty, tz, idx);\n        update_remaining(target_rem, normalized, tx, ty, tz);\n        return true;\n    }\n    return false;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int D;\n    cin >> D;\n\n    vector<string> f1_lines(D), r1_lines(D), f2_lines(D), r2_lines(D);\n    for (int i = 0; i < D; i++) {\n        cin >> f1_lines[i];\n    }\n    for (int i = 0; i < D; i++) {\n        cin >> r1_lines[i];\n    }\n    for (int i = 0; i < D; i++) {\n        cin >> f2_lines[i];\n    }\n    for (int i = 0; i < D; i++) {\n        cin >> r2_lines[i];\n    }\n\n    vector<vector<vector<bool>>> R1_rem(D, vector<vector<bool>>(D, vector<bool>(D, false)));\n    vector<vector<vector<bool>>> R2_rem(D, vector<vector<bool>>(D, vector<bool>(D, false)));\n\n    for (int z = 0; z < D; z++) {\n        for (int x = 0; x < D; x++) {\n            for (int y = 0; y < D; y++) {\n                if (f1_lines[z][x] == '1' && r1_lines[z][y] == '1') {\n                    R1_rem[x][y][z] = true;\n                }\n                if (f2_lines[z][x] == '1' && r2_lines[z][y] == '1') {\n                    R2_rem[x][y][z] = true;\n                }\n            }\n        }\n    }\n\n    vector<vector<vector<int>>> b1_grid(D, vector<vector<int>>(D, vector<int>(D, 0)));\n    vector<vector<vector<int>>> b2_grid(D, vector<vector<int>>(D, vector<int>(D, 0)));\n\n    int next_block_idx = 1;\n\n    vector<vector<vector<bool>>> R_grid(D, vector<vector<bool>>(D, vector<bool>(D, false)));\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                R_grid[x][y][z] = R1_rem[x][y][z] && R2_rem[x][y][z];\n            }\n        }\n    }\n\n    auto shared_components = get_components(R_grid, D);\n    sort(shared_components.begin(), shared_components.end(), [](const auto& a, const auto& b) {\n        return a.size() > b.size();\n    });\n\n    for (auto& comp : shared_components) {\n        if (comp.empty()) continue;\n        if (try_place_shared_block(comp, R1_rem, R2_rem, D, next_block_idx, b1_grid, b2_grid)) {\n        }\n    }\n\n    auto comps1 = get_components(R1_rem, D);\n    sort(comps1.begin(), comps1.end(), [](const auto& a, const auto& b) {\n        return a.size() > b.size();\n    });\n    for (auto& comp : comps1) {\n        if (comp.empty()) continue;\n        if (try_place_non_shared_block(comp, R1_rem, D, next_block_idx, b1_grid)) {\n        }\n    }\n\n    auto comps2 = get_components(R2_rem, D);\n    sort(comps2.begin(), comps2.end(), [](const auto& a, const auto& b) {\n        return a.size() > b.size();\n    });\n    for (auto& comp : comps2) {\n        if (comp.empty()) continue;\n        if (try_place_non_shared_block(comp, R2_rem, D, next_block_idx, b2_grid)) {\n        }\n    }\n\n    cout << next_block_idx - 1 << \"\\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                cout << b1_grid[x][y][z];\n                if (!(x == D-1 && y == D-1 && z == D-1)) {\n                    cout << \" \";\n                }\n            }\n        }\n    }\n    cout << \"\\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                cout << b2_grid[x][y][z];\n                if (!(x == D-1 && y == D-1 && z == D-1)) {\n                    cout << \" \";\n                }\n            }\n        }\n    }\n    cout << endl;\n\n    return 0;\n}","ahc020":"#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\n\nconst ll MAX_COVERAGE_SQ = 25000000LL; // 5000^2\nconst int MAX_LOCAL_ITER = 150;\nconst int MAX_STARTS = 8;\n\nstruct Resident {\n    ll x, y;\n};\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, K;\n    cin >> N >> M >> K;\n\n    vector<ll> stations_x(N), stations_y(N);\n    for (int i = 0; i < N; i++) {\n        cin >> stations_x[i] >> stations_y[i];\n    }\n\n    vector<tuple<int, int, ll>> edges;\n    vector<vector<pair<int, ll>>> adj(N);\n    for (int i = 0; i < M; i++) {\n        int u, v;\n        ll w;\n        cin >> u >> v >> w;\n        u--; v--;\n        edges.emplace_back(u, v, w);\n        adj[u].emplace_back(v, w);\n        adj[v].emplace_back(u, w);\n    }\n\n    vector<Resident> residents(K);\n    for (int i = 0; i < K; i++) {\n        cin >> residents[i].x >> residents[i].y;\n    }\n\n    // Precompute squared distances and coverage\n    vector<vector<ll>> dist_sq(N, vector<ll>(K));\n    vector<vector<bool>> can_cover(K, vector<bool>(N, false));\n    for (int i = 0; i < N; i++) {\n        for (int k = 0; k < K; k++) {\n            ll dx = stations_x[i] - residents[k].x;\n            ll dy = stations_y[i] - residents[k].y;\n            dist_sq[i][k] = dx*dx + dy*dy;\n            can_cover[k][i] = (dist_sq[i][k] <= MAX_COVERAGE_SQ);\n        }\n    }\n\n    // Compute shortest paths using Dijkstra\n    vector<vector<ll>> dist_graph(N, vector<ll>(N, LLONG_MAX/2));\n    for (int i = 0; i < N; i++) {\n        dist_graph[i][i] = 0;\n        priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<>> pq;\n        pq.emplace(0, i);\n\n        while (!pq.empty()) {\n            auto [d, u] = pq.top();\n            pq.pop();\n            if (d > dist_graph[i][u]) continue;\n\n            for (auto [v, w] : adj[u]) {\n                if (dist_graph[i][v] > dist_graph[i][u] + w) {\n                    dist_graph[i][v] = dist_graph[i][u] + w;\n                    pq.emplace(dist_graph[i][v], v);\n                }\n            }\n        }\n    }\n\n    auto covers_all = [&](const vector<int>& stations_set, const vector<bool>& covered) {\n        for (int k = 0; k < K; k++) {\n            if (!covered[k]) return false;\n        }\n        return true;\n    };\n\n    auto compute_total_cost = [&](const vector<int>& stations_set) {\n        ll mst = 0;\n        int n = stations_set.size();\n        if (n > 1) {\n            vector<ll> min_edge(n, LLONG_MAX);\n            vector<bool> visited(n, false);\n            min_edge[0] = 0;\n            for (int i = 0; i < n; i++) {\n                int u = -1;\n                for (int j = 0; j < n; j++) {\n                    if (!visited[j] && (u == -1 || min_edge[j] < min_edge[u])) {\n                        u = j;\n                    }\n                }\n                if (u == -1) break;\n                visited[u] = true;\n                mst += min_edge[u];\n                for (int v = 0; v < n; v++) {\n                    if (!visited[v]) {\n                        ll w = dist_graph[stations_set[u]][stations_set[v]];\n                        if (w < min_edge[v]) {\n                            min_edge[v] = w;\n                        }\n                    }\n                }\n            }\n        }\n\n        vector<ll> max_dist(N, 0);\n        for (int k = 0; k < K; k++) {\n            ll min_d = LLONG_MAX;\n            int best_s = -1;\n            for (int s : stations_set) {\n                if (dist_sq[s][k] < min_d) {\n                    min_d = dist_sq[s][k];\n                    best_s = s;\n                }\n            }\n            if (best_s != -1 && dist_sq[best_s][k] > max_dist[best_s]) {\n                max_dist[best_s] = dist_sq[best_s][k];\n            }\n        }\n\n        ll station = 0;\n        for (int s : stations_set) {\n            ll max_d = max_dist[s];\n            if (max_d == 0) continue;\n            ll p = sqrt(max_d);\n            if (p * p < max_d) p++;\n            if (p > 5000) p = 5000;\n            station += p * p;\n        }\n\n        return mst + station;\n    };\n\n    // Check for stations that can cover all residents alone\n    vector<int> single_covering_stations;\n    for (int i = 0; i < N; i++) {\n        bool covers_all = true;\n        for (int k = 0; k < K; k++) {\n            if (!can_cover[k][i]) {\n                covers_all = false;\n                break;\n            }\n        }\n        if (covers_all) {\n            single_covering_stations.push_back(i);\n        }\n    }\n\n    if (!single_covering_stations.empty()) {\n        // Find the station with minimal cost\n        int best_station = single_covering_stations[0];\n        ll min_cost = LLONG_MAX;\n        for (int s : single_covering_stations) {\n            ll cost = 0;\n            for (int k = 0; k < K; k++) {\n                ll max_d = dist_sq[s][k];\n                ll p = sqrt(max_d);\n                if (p * p < max_d) p++;\n                if (p > 5000) p = 5000;\n                cost += p * p;\n            }\n            if (cost < min_cost) {\n                min_cost = cost;\n                best_station = s;\n            }\n        }\n\n        // Compute P_i values\n        vector<int> P(N, 0);\n        for (int k = 0; k < K; k++) {\n            ll max_d = dist_sq[best_station][k];\n            ll p = sqrt(max_d);\n            if (p * p < max_d) p++;\n            if (p > 5000) p = 5000;\n            P[best_station] = (int)p;\n        }\n\n        // Select all edges connected to this station\n        vector<bool> edge_used(M, false);\n        for (int j = 0; j < M; j++) {\n            auto [u, v, w] = edges[j];\n            if (u == best_station || v == best_station) {\n                edge_used[j] = true;\n            }\n        }\n\n        // Output the solution\n        for (int i = 0; i < N; i++) {\n            cout << P[i];\n            if (i < N-1) cout << \" \";\n        }\n        cout << \"\\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        return 0;\n    }\n\n    // Select top stations by coverage as starting points\n    vector<pair<int, int>> coverage_counts;\n    for (int i = 0; i < N; i++) {\n        int cover = 0;\n        for (int k = 0; k < K; k++) {\n            if (can_cover[k][i]) cover++;\n        }\n        coverage_counts.emplace_back(cover, i);\n    }\n    sort(coverage_counts.rbegin(), coverage_counts.rend());\n    int start_candidates = min(MAX_STARTS, (int)coverage_counts.size());\n\n    vector<int> best_stations;\n    ll best_total_cost = LLONG_MAX;\n    vector<int> best_P(N, 0);\n    vector<bool> best_edge_used(M, false);\n\n    for (int c = 0; c < start_candidates; c++) {\n        int start = coverage_counts[c].second;\n        vector<int> current_stations = {start};\n        vector<bool> resident_covered(K, false);\n\n        for (int k = 0; k < K; k++) {\n            if (can_cover[k][start]) resident_covered[k] = true;\n        }\n\n        // Greedy expansion with improved heuristic\n        while (true) {\n            vector<int> uncovered;\n            for (int k = 0; k < K; k++) {\n                if (!resident_covered[k]) uncovered.push_back(k);\n            }\n            if (uncovered.empty()) break;\n\n            int best_add = -1;\n            double best_score = -1.0;\n\n            for (int j = 0; j < N; j++) {\n                if (find(current_stations.begin(), current_stations.end(), j) != current_stations.end()) continue;\n\n                int new_cover = 0;\n                for (int k : uncovered) {\n                    if (can_cover[k][j]) new_cover++;\n                }\n                if (new_cover == 0) continue;\n\n                // Improved scoring: prioritize stations that cover the most new residents\n                double score = new_cover * 1000.0 - (current_stations.size() * 1.0);\n                if (score > best_score) {\n                    best_score = score;\n                    best_add = j;\n                }\n            }\n\n            if (best_add == -1) break;\n\n            current_stations.push_back(best_add);\n            for (int k = 0; k < K; k++) {\n                if (can_cover[k][best_add]) resident_covered[k] = true;\n            }\n        }\n\n        // Local search optimization\n        ll current_total_cost = compute_total_cost(current_stations);\n        bool improved;\n        int iter = 0;\n\n        do {\n            improved = false;\n            iter++;\n            if (iter > MAX_LOCAL_ITER) break;\n\n            // Try removing stations\n            for (int i = 1; i < (int)current_stations.size(); i++) {\n                vector<int> candidate = current_stations;\n                candidate.erase(candidate.begin() + i);\n                if (!covers_all(candidate, resident_covered)) continue;\n\n                ll new_cost = compute_total_cost(candidate);\n                if (new_cost < current_total_cost) {\n                    current_stations = candidate;\n                    current_total_cost = new_cost;\n                    improved = true;\n                    break;\n                }\n            }\n            if (improved) continue;\n\n            // Try adding stations\n            for (int j = 0; j < N; j++) {\n                if (find(current_stations.begin(), current_stations.end(), j) != current_stations.end()) continue;\n                vector<int> candidate = current_stations;\n                candidate.push_back(j);\n                vector<bool> temp_covered = resident_covered;\n                for (int k = 0; k < K; k++) {\n                    if (can_cover[k][j]) temp_covered[k] = true;\n                }\n                if (!covers_all(candidate, temp_covered)) continue;\n\n                ll new_cost = compute_total_cost(candidate);\n                if (new_cost < current_total_cost) {\n                    current_stations = candidate;\n                    resident_covered = temp_covered;\n                    current_total_cost = new_cost;\n                    improved = true;\n                    break;\n                }\n            }\n\n        } while (improved);\n\n        // Compute P_i values\n        vector<ll> max_dist(N, 0);\n        for (int k = 0; k < K; k++) {\n            ll min_d = LLONG_MAX;\n            int best_s = -1;\n            for (int s : current_stations) {\n                if (dist_sq[s][k] < min_d) {\n                    min_d = dist_sq[s][k];\n                    best_s = s;\n                }\n            }\n            if (best_s != -1 && dist_sq[best_s][k] > max_dist[best_s]) {\n                max_dist[best_s] = dist_sq[best_s][k];\n            }\n        }\n\n        vector<int> P(N, 0);\n        for (int i = 0; i < N; i++) {\n            if (find(current_stations.begin(), current_stations.end(), i) == current_stations.end()) {\n                P[i] = 0;\n            } else {\n                ll max_d = max_dist[i];\n                if (max_d == 0) {\n                    P[i] = 0;\n                } else {\n                    ll p = sqrt(max_d);\n                    if (p * p < max_d) p++;\n                    if (p > 5000) p = 5000;\n                    P[i] = (int)p;\n                }\n            }\n        }\n\n        // Select MST edges using Prim's algorithm\n        vector<bool> edge_used(M, false);\n        vector<bool> in_set(N, false);\n        priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<>> pq;\n\n        for (int s : current_stations) {\n            in_set[s] = true;\n            for (auto [v, w] : adj[s]) {\n                if (in_set[v]) continue;\n                pq.emplace(w, v);\n            }\n        }\n\n        while (!pq.empty()) {\n            auto [w, u] = pq.top();\n            pq.pop();\n\n            if (in_set[u]) continue;\n\n            // Find the edge connecting u to the set\n            bool found = false;\n            for (int j = 0; j < M; j++) {\n                auto [u1, v1, w1] = edges[j];\n                if ((u1 == u && in_set[v1]) || (v1 == u && in_set[u1])) {\n                    edge_used[j] = true;\n                    found = true;\n                    break;\n                }\n            }\n            if (!found) continue;\n\n            in_set[u] = true;\n            for (auto [v, w] : adj[u]) {\n                if (!in_set[v]) {\n                    pq.emplace(w, v);\n                }\n            }\n        }\n\n        if (current_total_cost < best_total_cost) {\n            best_total_cost = current_total_cost;\n            best_stations = current_stations;\n            best_P = P;\n            best_edge_used = edge_used;\n        }\n    }\n\n    // Ensure all residents are covered\n    vector<bool> resident_covered(K, false);\n    for (int s : best_stations) {\n        for (int k = 0; k < K; k++) {\n            if (can_cover[k][s]) resident_covered[k] = true;\n        }\n    }\n\n    for (int k = 0; k < K; k++) {\n        if (!resident_covered[k]) {\n            ll min_dist = LLONG_MAX;\n            int best_add = -1;\n            for (int j = 0; j < N; j++) {\n                if (can_cover[k][j] && dist_sq[j][k] < min_dist) {\n                    min_dist = dist_sq[j][k];\n                    best_add = j;\n                }\n            }\n            if (best_add != -1) {\n                best_stations.push_back(best_add);\n            }\n        }\n    }\n\n    // Recompute final solution\n    vector<ll> max_dist(N, 0);\n    for (int k = 0; k < K; k++) {\n        ll min_d = LLONG_MAX;\n        int best_s = -1;\n        for (int s : best_stations) {\n            if (dist_sq[s][k] < min_d) {\n                min_d = dist_sq[s][k];\n                best_s = s;\n            }\n        }\n        if (best_s != -1 && dist_sq[best_s][k] > max_dist[best_s]) {\n            max_dist[best_s] = dist_sq[best_s][k];\n        }\n    }\n\n    for (int i = 0; i < N; i++) {\n        if (find(best_stations.begin(), best_stations.end(), i) == best_stations.end()) {\n            best_P[i] = 0;\n        } else {\n            ll max_d = max_dist[i];\n            if (max_d == 0) {\n                best_P[i] = 0;\n            } else {\n                ll p = sqrt(max_d);\n                if (p * p < max_d) p++;\n                if (p > 5000) p = 5000;\n                best_P[i] = (int)p;\n            }\n        }\n    }\n\n    // Output the solution\n    for (int i = 0; i < N; i++) {\n        cout << best_P[i];\n        if (i < N-1) cout << \" \";\n    }\n    cout << \"\\n\";\n    for (int j = 0; j < M; j++) {\n        cout << best_edge_used[j];\n        if (j < M-1) cout << \" \";\n    }\n    cout << endl;\n\n    return 0;\n}","ahc021":"#include <bits/stdc++.h>\nusing namespace std;\n\ninline int get_idx(int x, int y) {\n    return x * (x + 1) / 2 + y;\n}\n\nint main() {\n    vector<int> pyramid(465);\n    vector<array<int, 2>> pos(465);\n\n    int idx = 0;\n    for (int x = 0; x < 30; x++) {\n        for (int y = 0; y <= x; y++) {\n            cin >> pyramid[idx];\n            pos[pyramid[idx]] = {x, y};\n            idx++;\n        }\n    }\n\n    vector<array<int, 4>> swaps;\n\n    for (int val = 464; val >= 0; val--) {\n        int x = pos[val][0];\n        int y = pos[val][1];\n        if (x == 29) continue;\n\n        while (true) {\n            int left_x = x + 1;\n            int left_y = y;\n            int right_x = x + 1;\n            int right_y = y + 1;\n\n            int left_idx = get_idx(left_x, left_y);\n            int right_idx = get_idx(right_x, right_y);\n            int left_val = pyramid[left_idx];\n            int right_val = pyramid[right_idx];\n\n            if (val <= left_val && val <= right_val) {\n                break;\n            }\n\n            int swap_x, swap_y;\n            if (left_val <= right_val) {\n                swap_x = left_x;\n                swap_y = left_y;\n            } else {\n                swap_x = right_x;\n                swap_y = right_y;\n            }\n            int swap_idx = get_idx(swap_x, swap_y);\n            int swapped_val = pyramid[swap_idx];\n\n            swaps.push_back({x, y, swap_x, swap_y});\n            swap(pyramid[get_idx(x, y)], pyramid[swap_idx]);\n\n            pos[val] = {swap_x, swap_y};\n            pos[swapped_val] = {x, y};\n\n            x = swap_x;\n            y = swap_y;\n            if (x == 29) break;\n        }\n    }\n\n    cout << swaps.size() << '\\n';\n    for (auto& s : swaps) {\n        cout << s[0] << ' ' << s[1] << ' ' << s[2] << ' ' << s[3] << '\\n';\n    }\n    return 0;\n}","toyota2023summer-final":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\n#include <tuple>\n#include <set>\n#include <cctype>\n#include <cmath>\nusing namespace std;\n\nint main() {\n    int D, N;\n    cin >> D >> N;\n    vector<vector<bool>> is_obstacle(9, vector<bool>(9, false));\n    set<pair<int, int>> non_storage = {{0, 4}};\n\n    for (int idx = 0; idx < N; idx++) {\n        int ri, rj;\n        cin >> ri >> rj;\n        is_obstacle[ri][rj] = true;\n    }\n\n    vector<pair<int, int>> storage_cells;\n    for (int i = 0; i < 9; i++) {\n        for (int j = 0; j < 9; j++) {\n            if (non_storage.find({i, j}) != non_storage.end()) continue;\n            if (is_obstacle[i][j]) continue;\n            storage_cells.push_back({i, j});\n        }\n    }\n\n    vector<vector<int>> dist(9, vector<int>(9, -1));\n    queue<pair<int, int>> q_bfs;\n    dist[0][4] = 0;\n    q_bfs.push({0, 4});\n    const vector<int> dx = {1, -1, 0, 0};\n    const vector<int> dy = {0, 0, 1, -1};\n\n    while (!q_bfs.empty()) {\n        auto [i, j] = q_bfs.front();\n        q_bfs.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 < 9 && nj >= 0 && nj < 9) {\n                if (dist[ni][nj] == -1 && !is_obstacle[ni][nj]) {\n                    dist[ni][nj] = dist[i][j] + 1;\n                    q_bfs.push({ni, nj});\n                }\n            }\n        }\n    }\n\n    sort(storage_cells.begin(), storage_cells.end(), [&](const pair<int, int>& a, const pair<int, int>& b) {\n        if (dist[a.first][a.second] != dist[b.first][b.second]) {\n            return dist[a.first][a.second] > dist[b.first][b.second];\n        }\n        if (a.first != b.first) {\n            return a.first < b.first;\n        }\n        return a.second < b.second;\n    });\n\n    int M = storage_cells.size();\n    vector<vector<int>> container_id(9, vector<int>(9, -1));\n\n    for (int d = 0; d < M; d++) {\n        int t;\n        cin >> t;\n        int index = M - 1 - t;\n        auto [i, j] = storage_cells[index];\n        cout << i << \" \" << j << endl;\n        container_id[i][j] = t;\n    }\n\n    vector<vector<bool>> is_storage(9, vector<bool>(9, false));\n    for (auto [i, j] : storage_cells) {\n        is_storage[i][j] = true;\n    }\n\n    vector<vector<bool>> is_removed(9, vector<bool>(9, false));\n    vector<pair<int, int>> transport_order;\n\n    for (int step = 0; step < M; step++) {\n        vector<vector<bool>> visited(9, vector<bool>(9, false));\n        queue<pair<int, int>> q;\n        if (!is_obstacle[0][4]) {\n            visited[0][4] = true;\n            q.push({0, 4});\n        }\n\n        while (!q.empty()) {\n            auto [i, j] = q.front();\n            q.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 >= 9 || nj < 0 || nj >= 9) continue;\n                if (visited[ni][nj]) continue;\n                if (is_obstacle[ni][nj]) continue;\n                if (is_storage[ni][nj] && !is_removed[ni][nj]) {\n                    continue;\n                }\n                visited[ni][nj] = true;\n                q.push({ni, nj});\n            }\n        }\n\n        vector<tuple<int, int, int>> candidates;\n        for (int i = 0; i < 9; i++) {\n            for (int j = 0; j < 9; j++) {\n                if (!is_storage[i][j]) continue;\n                if (is_removed[i][j]) continue;\n                bool has_visited_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 < 9 && nj >= 0 && nj < 9) {\n                        if (visited[ni][nj]) {\n                            has_visited_neighbor = true;\n                            break;\n                        }\n                    }\n                }\n                if (has_visited_neighbor) {\n                    candidates.push_back(make_tuple(container_id[i][j], i, j));\n                }\n            }\n        }\n\n        sort(candidates.begin(), candidates.end());\n        auto [id_val, i_val, j_val] = candidates[0];\n        transport_order.push_back({i_val, j_val});\n        is_removed[i_val][j_val] = true;\n    }\n\n    for (auto [i, j] : transport_order) {\n        cout << i << \" \" << j << endl;\n    }\n\n    return 0;\n}","ahc024":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int n = 50;\nconst int m = 100;\nconst vector<pair<int, int>> directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};\n\nint main() {\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<set<int>> adj(m + 1);\n    vector<bool> boundary(m + 1, false);\n\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            int c = grid[i][j];\n            if (i == 0 || i == n - 1 || j == 0 || j == n - 1) {\n                boundary[c] = true;\n            }\n            for (auto [dx, dy] : directions) {\n                int ni = i + dx;\n                int nj = j + dy;\n                if (ni >= 0 && ni < n && nj >= 0 && nj < n) {\n                    int d = grid[ni][nj];\n                    if (d != c) {\n                        adj[c].insert(d);\n                        adj[d].insert(c);\n                    }\n                }\n            }\n        }\n    }\n\n    vector<pair<int, int>> boundary_positions;\n    for (int j = 0; j < n; j++) boundary_positions.push_back({n - 1, j});\n    for (int i = n - 2; i >= 0; i--) boundary_positions.push_back({i, n - 1});\n    for (int j = n - 2; j >= 0; j--) boundary_positions.push_back({0, j});\n    for (int i = 1; i < n - 1; i++) boundary_positions.push_back({i, 0});\n\n    vector<set<pair<int, int>>> regions(m + 1);\n    vector<queue<pair<int, int>>> frontiers(m + 1);\n    vector<vector<int>> ans(n, vector<int>(n, 0));\n    vector<bool> placed(m + 1, false);\n\n    int pos_index = 0;\n    for (int c = 1; c <= m; c++) {\n        if (boundary[c]) {\n            if (pos_index >= (int)boundary_positions.size()) break;\n            auto [i, j] = boundary_positions[pos_index++];\n            ans[i][j] = c;\n            regions[c].insert({i, j});\n            frontiers[c].push({i, j});\n            placed[c] = true;\n        }\n    }\n\n    vector<vector<int>> adj_list(m + 1);\n    for (int c = 1; c <= m; c++) {\n        for (int d : adj[c]) {\n            if (d >= 1 && d <= m) {\n                adj_list[c].push_back(d);\n            }\n        }\n    }\n\n    queue<int> q;\n    for (int c = 1; c <= m; c++) {\n        if (placed[c]) {\n            q.push(c);\n        }\n    }\n\n    auto try_place_edge = [&](int c, int d) -> bool {\n        while (!frontiers[c].empty()) {\n            auto cell = frontiers[c].front();\n            vector<pair<int, int>> free_dirs;\n            for (auto [dx, dy] : directions) {\n                int ni = cell.first + dx;\n                int nj = cell.second + dy;\n                if (ni >= 0 && ni < n && nj >= 0 && nj < n && ans[ni][nj] == 0) {\n                    free_dirs.push_back({dx, dy});\n                }\n            }\n            if (!free_dirs.empty()) {\n                auto [dx, dy] = free_dirs[0];\n                int ni = cell.first + dx;\n                int nj = cell.second + dy;\n                ans[ni][nj] = d;\n                regions[d].insert({ni, nj});\n                frontiers[d].push({ni, nj});\n                return true;\n            } else {\n                frontiers[c].pop();\n            }\n        }\n\n        for (auto cell : regions[c]) {\n            for (auto [dx, dy] : directions) {\n                int ni = cell.first + dx;\n                int nj = cell.second + dy;\n                if (ni >= 0 && ni < n && nj >= 0 && nj < n && ans[ni][nj] == 0) {\n                    ans[ni][nj] = c;\n                    regions[c].insert({ni, nj});\n                    frontiers[c].push({ni, nj});\n                    return try_place_edge(c, d);\n                }\n            }\n        }\n        return false;\n    };\n\n    while (!q.empty()) {\n        int c = q.front();\n        q.pop();\n        for (int d : adj_list[c]) {\n            if (placed[d]) continue;\n            if (try_place_edge(c, d)) {\n                placed[d] = true;\n                q.push(d);\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 << ans[i][j];\n        }\n        cout << '\\n';\n    }\n\n    return 0;\n}","ahc025":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(0);\n    cin.tie(0);\n\n    int N, D, Q;\n    cin >> N >> D >> Q;\n\n    vector<double> weight_est(N, 1.0);\n    vector<vector<int>> comparisons;\n    int queries_used = 0;\n\n    auto make_query = [&](const vector<int>& L, const vector<int>& R) -> string {\n        cout << L.size() << \" \" << R.size();\n        for (int x : L) cout << \" \" << x;\n        for (int x : R) cout << \" \" << x;\n        cout << endl;\n        string res;\n        cin >> res;\n        queries_used++;\n        return res;\n    };\n\n    auto update_weights = [&](const vector<int>& L, const vector<int>& R, const string& res) {\n        if (res == \"=\") return;\n        double factor = 1.01;\n        if (res == \"<\") {\n            for (int x : L) weight_est[x] *= 0.99;\n            for (int x : R) weight_est[x] *= 1.01;\n        } else {\n            for (int x : L) weight_est[x] *= 1.01;\n            for (int x : R) weight_est[x] *= 0.99;\n        }\n    };\n\n    for (int i = 0; i < N && queries_used < Q; i++) {\n        for (int j = i + 1; j < N && queries_used < Q; j++) {\n            vector<int> L = {i}, R = {j};\n            string res = make_query(L, R);\n            update_weights(L, R, res);\n        }\n    }\n\n    while (queries_used < Q) {\n        vector<int> L = {0}, R = {1};\n        string res = make_query(L, R);\n        update_weights(L, R, res);\n    }\n\n    vector<int> indices(N);\n    iota(indices.begin(), indices.end(), 0);\n    sort(indices.begin(), indices.end(), [&](int a, int b) {\n        return weight_est[a] > weight_est[b];\n    });\n\n    vector<long long> set_sum(D, 0);\n    vector<int> assignment(N, -1);\n\n    for (int idx = 0; idx < N; idx++) {\n        int item = indices[idx];\n        int best_set = 0;\n        for (int s = 1; s < D; s++) {\n            if (set_sum[s] < set_sum[best_set]) {\n                best_set = s;\n            }\n        }\n        assignment[item] = best_set;\n        set_sum[best_set] += (long long)(weight_est[item] * 1000000);\n    }\n\n    for (int i = 0; i < N; i++) {\n        if (i) cout << \" \";\n        cout << assignment[i];\n    }\n    cout << endl;\n\n    return 0;\n}","ahc026":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int n, m;\n    cin >> n >> m;\n    int k = n / m;\n\n    vector<vector<int>> stacks(m);\n    for (int i = 0; i < m; i++) {\n        stacks[i].resize(k);\n        for (int j = 0; j < k; j++) {\n            cin >> stacks[i][j];\n        }\n    }\n\n    vector<int> empty_stacks;\n    vector<pair<int, int>> ops;\n    int cur = 1;\n\n    while (cur <= n) {\n        bool carried = false;\n        for (int s = 0; s < m; s++) {\n            if (stacks[s].empty()) continue;\n            if (stacks[s].back() == cur) {\n                ops.push_back({cur, 0});\n                stacks[s].pop_back();\n                if (stacks[s].empty()) {\n                    empty_stacks.push_back(s);\n                }\n                cur++;\n                carried = true;\n                break;\n            }\n        }\n        if (carried) {\n            continue;\n        }\n\n        int src = -1;\n        int pos = -1;\n        for (int s = 0; s < m; s++) {\n            auto it = find(stacks[s].begin(), stacks[s].end(), cur);\n            if (it != stacks[s].end()) {\n                src = s;\n                pos = it - stacks[s].begin();\n                break;\n            }\n        }\n        if (src == -1) {\n            break;\n        }\n\n        vector<int> segment(stacks[src].begin() + pos + 1, stacks[src].end());\n        int T = segment.back();\n        int dest = -1;\n\n        if (!empty_stacks.empty()) {\n            dest = empty_stacks.back();\n            empty_stacks.pop_back();\n        } else {\n            int candidate_large = -1;\n            int candidate_small = -1;\n            for (int s = 0; s < m; s++) {\n                if (stacks[s].empty()) continue;\n                int top_val = stacks[s].back();\n                if (top_val > T) {\n                    if (candidate_large == -1 || top_val > stacks[candidate_large].back()) {\n                        candidate_large = s;\n                    }\n                }\n                if (candidate_small == -1 || top_val < stacks[candidate_small].back()) {\n                    candidate_small = s;\n                }\n            }\n            if (candidate_large != -1) {\n                dest = candidate_large;\n            } else if (candidate_small != -1) {\n                dest = candidate_small;\n            } else {\n                dest = 0;\n            }\n        }\n\n        int v = segment[0];\n        stacks[src].resize(pos + 1);\n        for (int box : segment) {\n            stacks[dest].push_back(box);\n        }\n        ops.push_back({v, dest + 1});\n    }\n\n    for (auto &op : ops) {\n        cout << op.first << \" \" << op.second << \"\\n\";\n    }\n\n    return 0;\n}","ahc027":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N;\n    cin >> N;\n\n    vector<string> h(N-1);\n    for (int i = 0; i < N-1; i++) {\n        cin >> h[i];\n    }\n\n    vector<string> v(N);\n    for (int i = 0; i < N; i++) {\n        cin >> v[i];\n    }\n\n    vector<vector<int>> d(N, vector<int>(N));\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cin >> d[i][j];\n        }\n    }\n\n    // Directions: R, D, L, U\n    const int di[4] = {0, 1, 0, -1};\n    const int dj[4] = {1, 0, -1, 0};\n    const char dir_char[4] = {'R', 'D', 'L', 'U'};\n    const char rev_char[4] = {'L', 'U', 'R', 'D'};\n\n    vector<vector<bool>> visited(N, vector<bool>(N, false));\n    stack<tuple<int, int, int>> st; // (i, j, next_direction)\n    st.push({0, 0, 0});\n    visited[0][0] = true;\n    string path;\n\n    while (!st.empty()) {\n        auto [i, j, dir_idx] = st.top();\n        bool moved = false;\n\n        for (; dir_idx < 4; dir_idx++) {\n            int ni = i + di[dir_idx];\n            int nj = j + dj[dir_idx];\n\n            if (ni < 0 || ni >= N || nj < 0 || nj >= N) continue;\n\n            // Check walls based on movement direction\n            bool can_move = false;\n            if (dir_char[dir_idx] == 'R') {\n                if (j < N-1 && v[i][j] == '0') can_move = true;\n            }\n            else if (dir_char[dir_idx] == 'D') {\n                if (i < N-1 && h[i][j] == '0') can_move = true;\n            }\n            else if (dir_char[dir_idx] == 'L') {\n                if (j > 0 && v[i][j-1] == '0') can_move = true;\n            }\n            else if (dir_char[dir_idx] == 'U') {\n                if (i > 0 && h[i-1][j] == '0') can_move = true;\n            }\n\n            if (can_move && !visited[ni][nj]) {\n                visited[ni][nj] = true;\n                path += dir_char[dir_idx];\n                st.top() = {i, j, dir_idx + 1};\n                st.push({ni, nj, 0});\n                moved = true;\n                break;\n            }\n        }\n\n        if (!moved) {\n            st.pop();\n            if (!st.empty()) {\n                auto [pi, pj, _] = st.top();\n                int di = i - pi;\n                int dj = j - pj;\n                char rev;\n                if (di == 1) rev = 'U';\n                else if (di == -1) rev = 'D';\n                else if (dj == 1) rev = 'L';\n                else rev = 'R';\n                path += rev;\n            }\n        }\n    }\n\n    // Ensure we return to (0,0)\n    int i = 0, j = 0;\n    for (char c : path) {\n        if (c == 'U') i--;\n        else if (c == 'D') i++;\n        else if (c == 'L') j--;\n        else if (c == 'R') j++;\n    }\n\n    // Add return path if needed\n    while (i != 0 || j != 0) {\n        if (i > 0 && (i == 1 || h[i-1][j] == '0')) {\n            path += 'U';\n            i--;\n        }\n        else if (i < 0) {\n            path += 'D';\n            i++;\n        }\n        else if (j > 0) {\n            path += 'L';\n            j--;\n        }\n        else if (j < 0 && v[i][j] == '0') {\n            path += 'R';\n            j++;\n        }\n        else {\n            // Find any valid move to return\n            if (i < N-1 && h[i][j] == '0') {\n                path += 'D';\n                i++;\n            }\n            else if (j < N-1 && v[i][j] == '0') {\n                path += 'R';\n                j++;\n            }\n            else if (i > 0 && h[i-1][j] == '0') {\n                path += 'U';\n                i--;\n            }\n            else if (j > 0 && v[i][j-1] == '0') {\n                path += 'L';\n                j--;\n            }\n            else {\n                // Shouldn't happen as all cells are reachable\n                path += 'R';\n                j++;\n            }\n        }\n    }\n\n    // Validate path\n    auto validate = [&]() {\n        int x = 0, y = 0;\n        for (char c : path) {\n            int nx = x, ny = y;\n            if (c == 'U') nx--;\n            else if (c == 'D') nx++;\n            else if (c == 'L') ny--;\n            else if (c == 'R') ny++;\n\n            if (nx < 0 || nx >= N || ny < 0 || ny >= N) return false;\n\n            if (c == 'R' && (y >= N-1 || v[x][y] == '1')) return false;\n            if (c == 'D' && (x >= N-1 || h[x][y] == '1')) return false;\n            if (c == 'L' && (y <= 0 || v[x][y-1] == '1')) return false;\n            if (c == 'U' && (x <= 0 || h[x-1][y] == '1')) return false;\n\n            x = nx;\n            y = ny;\n        }\n        return x == 0 && y == 0;\n    };\n\n    if (!validate()) {\n        // Fallback path that visits all cells (snake pattern)\n        path = \"\";\n        for (int row = 0; row < N; row++) {\n            if (row % 2 == 0) {\n                for (int col = 0; col < N-1; col++) {\n                    if (v[row][col] == '0') path += 'R';\n                }\n            } else {\n                for (int col = N-1; col > 0; col--) {\n                    if (v[row][col-1] == '0') path += 'L';\n                }\n            }\n            if (row < N-1 && h[row][N-1 == '0']) path += 'D';\n        }\n        // Return to start\n        for (int row = N-1; row > 0; row--) {\n            if (row % 2 == 0) {\n                for (int col = N-1; col > 0; col--) {\n                    if (v[row][col-1] == '0') path += 'L';\n                }\n            } else {\n                for (int col = 0; col < N-1; col++) {\n                    if (v[row][col] == '0') path += 'R';\n                }\n            }\n            if (row > 0 && h[row-1][N-1] == '0') path += 'U';\n        }\n    }\n\n    if (path.size() > 100000) {\n        path = path.substr(0, 100000);\n    }\n\n    cout << path << endl;\n\n    return 0;\n}","ahc028":"#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(0);\n\n    int N, M;\n    cin >> N >> M;\n    int s_i, s_j;\n    cin >> s_i >> s_j;\n    vector<string> grid(N);\n    for (int i = 0; i < N; i++) {\n        cin >> grid[i];\n    }\n    vector<string> t_list(M);\n    for (int i = 0; i < M; i++) {\n        cin >> t_list[i];\n    }\n\n    unordered_map<string, int> five_gram_map;\n    for (int i = 0; i < M; i++) {\n        five_gram_map[t_list[i]] = i;\n    }\n\n    unordered_map<string, int> prefix_count_map;\n    for (int i = 0; i < M; i++) {\n        string prefix = t_list[i].substr(0, 4);\n        prefix_count_map[prefix]++;\n    }\n\n    const int NUM_RESTARTS = 5;\n    const int MAX_STEPS_PER_RESTART = 3000;\n    int best_covered = -1;\n    ll best_cost = LLONG_MAX;\n    vector<pair<int, int>> best_path;\n\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n\n    for (int restart = 0; restart < NUM_RESTARTS; restart++) {\n        auto local_prefix_count_map = prefix_count_map;\n        vector<bool> local_covered(M, false);\n        int local_covered_count = 0;\n        string suffix(4, '#');\n        int cur_i = s_i;\n        int cur_j = s_j;\n        vector<pair<int, int>> path;\n        ll total_cost = 0;\n\n        for (int step = 0; step < MAX_STEPS_PER_RESTART; step++) {\n            if (local_covered_count == M) {\n                break;\n            }\n\n            vector<tuple<int, int, string, int>> gain_candidates;\n            int best_gain_cost = INT_MAX;\n\n            vector<tuple<int, int, string>> heuristic_candidates;\n            int best_heuristic_val = -1;\n            int best_heuristic_cost = INT_MAX;\n\n            for (int i = 0; i < N; i++) {\n                for (int j = 0; j < N; j++) {\n                    char c = grid[i][j];\n                    string new_suffix = suffix.substr(1) + c;\n\n                    bool forms_5gram = true;\n                    for (char ch : suffix) {\n                        if (ch == '#') {\n                            forms_5gram = false;\n                            break;\n                        }\n                    }\n\n                    int gain = 0;\n                    int gain_index = -1;\n                    if (forms_5gram) {\n                        string five_gram = suffix + string(1, c);\n                        auto it = five_gram_map.find(five_gram);\n                        if (it != five_gram_map.end()) {\n                            int idx = it->second;\n                            if (!local_covered[idx]) {\n                                gain = 1;\n                                gain_index = idx;\n                            }\n                        }\n                    }\n\n                    int move_cost = abs(i - cur_i) + abs(j - cur_j) + 1;\n\n                    if (gain > 0) {\n                        if (move_cost < best_gain_cost) {\n                            best_gain_cost = move_cost;\n                            gain_candidates.clear();\n                            gain_candidates.push_back(make_tuple(i, j, new_suffix, gain_index));\n                        } else if (move_cost == best_gain_cost) {\n                            gain_candidates.push_back(make_tuple(i, j, new_suffix, gain_index));\n                        }\n                    } else {\n                        int heuristic_val = 0;\n                        bool valid = true;\n                        for (char ch : new_suffix) {\n                            if (ch < 'A' || ch > 'Z') {\n                                valid = false;\n                                break;\n                            }\n                        }\n                        if (valid) {\n                            auto it = local_prefix_count_map.find(new_suffix);\n                            if (it != local_prefix_count_map.end()) {\n                                heuristic_val = it->second;\n                            }\n                        }\n\n                        if (heuristic_val > best_heuristic_val) {\n                            best_heuristic_val = heuristic_val;\n                            best_heuristic_cost = move_cost;\n                            heuristic_candidates.clear();\n                            heuristic_candidates.push_back(make_tuple(i, j, new_suffix));\n                        } else if (heuristic_val == best_heuristic_val) {\n                            if (move_cost < best_heuristic_cost) {\n                                best_heuristic_cost = move_cost;\n                                heuristic_candidates.clear();\n                                heuristic_candidates.push_back(make_tuple(i, j, new_suffix));\n                            } else if (move_cost == best_heuristic_cost) {\n                                heuristic_candidates.push_back(make_tuple(i, j, new_suffix));\n                            }\n                        }\n                    }\n                }\n            }\n\n            int chosen_i = -1, chosen_j = -1;\n            string chosen_suffix = \"\";\n            int gain_idx = -1;\n\n            if (!gain_candidates.empty()) {\n                uniform_int_distribution<int> dist(0, (int)gain_candidates.size() - 1);\n                auto candidate = gain_candidates[dist(rng)];\n                chosen_i = get<0>(candidate);\n                chosen_j = get<1>(candidate);\n                chosen_suffix = get<2>(candidate);\n                gain_idx = get<3>(candidate);\n            } else {\n                if (heuristic_candidates.empty()) {\n                    uniform_int_distribution<int> dist(0, N*N-1);\n                    int idx = dist(rng);\n                    chosen_i = idx / N;\n                    chosen_j = idx % N;\n                    chosen_suffix = suffix.substr(1) + grid[chosen_i][chosen_j];\n                } else {\n                    uniform_int_distribution<int> dist(0, (int)heuristic_candidates.size() - 1);\n                    auto candidate = heuristic_candidates[dist(rng)];\n                    chosen_i = get<0>(candidate);\n                    chosen_j = get<1>(candidate);\n                    chosen_suffix = get<2>(candidate);\n                }\n            }\n\n            int move_cost = abs(chosen_i - cur_i) + abs(chosen_j - cur_j) + 1;\n            total_cost += move_cost;\n\n            cur_i = chosen_i;\n            cur_j = chosen_j;\n            suffix = chosen_suffix;\n\n            if (gain_idx != -1) {\n                local_covered[gain_idx] = true;\n                local_covered_count++;\n                string prefix = t_list[gain_idx].substr(0, 4);\n                local_prefix_count_map[prefix]--;\n                if (local_prefix_count_map[prefix] == 0) {\n                    local_prefix_count_map.erase(prefix);\n                }\n            }\n\n            path.push_back({chosen_i, chosen_j});\n        }\n\n        if (local_covered_count > best_covered || (local_covered_count == best_covered && total_cost < best_cost)) {\n            best_covered = local_covered_count;\n            best_cost = total_cost;\n            best_path = path;\n        }\n    }\n\n    cout << best_path.size() << \"\\n\";\n    for (auto &p : best_path) {\n        cout << p.first << \" \" << p.second << \"\\n\";\n    }\n\n    return 0;\n}","ahc030":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <tuple>\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<tuple<vector<pair<int, int>>, int, int>> fields;\n    for (int k = 0; k < M; k++) {\n        int d;\n        cin >> d;\n        vector<pair<int, int>> shape;\n        int max_i = 0, max_j = 0;\n        for (int i = 0; i < d; i++) {\n            int a, b;\n            cin >> a >> b;\n            shape.emplace_back(a, b);\n            if (a > max_i) max_i = a;\n            if (b > max_j) max_j = b;\n        }\n        fields.emplace_back(shape, max_i, max_j);\n    }\n\n    priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<>> pq;\n    int center = N / 2;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            int dist = abs(i - center) + abs(j - center);\n            pq.emplace(dist, i, j);\n        }\n    }\n\n    vector<vector<int>> status(N, vector<int>(N, 0));\n    vector<pair<int, int>> positive_list;\n\n    while (!pq.empty()) {\n        auto [dist, i, j] = pq.top();\n        pq.pop();\n\n        if (status[i][j] != 0) {\n            continue;\n        }\n\n        cout << \"q 1 \" << i << \" \" << j << endl;\n        cout.flush();\n\n        int v;\n        cin >> v;\n\n        if (v > 0) {\n            status[i][j] = 1;\n            positive_list.emplace_back(i, j);\n\n            for (const auto& field : fields) {\n                const auto& shape = get<0>(field);\n                int max_i = get<1>(field);\n                int max_j = get<2>(field);\n\n                for (const auto& offset : shape) {\n                    int a = offset.first;\n                    int b = offset.second;\n                    int p = i - a;\n                    int q_val = j - b;\n                    if (p < 0 || q_val < 0) continue;\n                    if (p + max_i >= N || q_val + max_j >= N) continue;\n\n                    for (const auto& cell : shape) {\n                        int i2 = p + cell.first;\n                        int j2 = q_val + cell.second;\n                        if (i2 < N && j2 < N && status[i2][j2] == 0) {\n                            int dist2 = abs(i2 - center) + abs(j2 - center);\n                            pq.emplace(dist2, i2, j2);\n                        }\n                    }\n                }\n            }\n        } else {\n            status[i][j] = 2;\n        }\n    }\n\n    cout << \"a \" << positive_list.size();\n    for (const auto& pos : positive_list) {\n        cout << \" \" << pos.first << \" \" << pos.second;\n    }\n    cout << endl;\n    cout.flush();\n\n    int result;\n    cin >> result;\n    return 0;\n}","ahc031":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Rect {\n    int y1, x1, y2, x2;\n    int area() const { return (y2 - y1) * (x2 - x1); }\n};\n\nbool overlap(const Rect& a, const Rect& b) {\n    return !(a.y2 <= b.y1 || b.y2 <= a.y1 || a.x2 <= b.x1 || b.x1 >= a.x2);\n}\n\nvector<Rect> split_free_space(const Rect& placed, const Rect& free_space) {\n    vector<Rect> splits;\n    if (free_space.y1 < placed.y1) {\n        splits.push_back({free_space.y1, free_space.x1, placed.y1, free_space.x2});\n    }\n    if (placed.y2 < free_space.y2) {\n        splits.push_back({placed.y2, free_space.x1, free_space.y2, free_space.x2});\n    }\n    if (free_space.x1 < placed.x1) {\n        int y1 = max(free_space.y1, placed.y1);\n        int y2 = min(free_space.y2, placed.y2);\n        if (y1 < y2) {\n            splits.push_back({y1, free_space.x1, y2, placed.x1});\n        }\n    }\n    if (placed.x2 < free_space.x2) {\n        int y1 = max(free_space.y1, placed.y1);\n        int y2 = min(free_space.y2, placed.y2);\n        if (y1 < y2) {\n            splits.push_back({y1, placed.x2, y2, free_space.x2});\n        }\n    }\n    return splits;\n}\n\nvector<Rect> pack_day(int W, const vector<int>& areas, const vector<Rect>& prev_rects) {\n    int N = areas.size();\n    vector<Rect> free_space = {{0, 0, W, W}};\n    vector<Rect> result(N, {0, 0, 1, 1});\n    vector<bool> placed(N, false);\n\n    for (int k = 0; k < N; k++) {\n        int req_area = areas[k];\n        Rect best_fit;\n        int best_fit_idx = -1;\n        double best_score = -1.0;\n\n        for (int i = 0; i < (int)free_space.size(); i++) {\n            Rect f = free_space[i];\n            if (f.area() < req_area) continue;\n\n            double score = 0.0;\n            if (k < (int)prev_rects.size()) {\n                Rect p = prev_rects[k];\n                int overlap_y1 = max(f.y1, p.y1);\n                int overlap_y2 = min(f.y2, p.y2);\n                int overlap_x1 = max(f.x1, p.x1);\n                int overlap_x2 = min(f.x2, p.x2);\n                if (overlap_y1 < overlap_y2 && overlap_x1 < overlap_x2) {\n                    score = -1.0 * (overlap_y2 - overlap_y1) * (overlap_x2 - overlap_x1);\n                } else {\n                    int center_dist = abs((f.y1 + f.y2)/2 - (p.y1 + p.y2)/2) +\n                                    abs((f.x1 + f.x2)/2 - (p.x1 + p.x2)/2);\n                    score = -1.0 * center_dist;\n                }\n            } else {\n                score = f.area();\n            }\n\n            if (score > best_score) {\n                best_score = score;\n                best_fit = f;\n                best_fit_idx = i;\n            }\n        }\n\n        if (best_fit_idx == -1) {\n            for (int i = 0; i < (int)free_space.size(); i++) {\n                if (free_space[i].area() > (int)best_score) {\n                    best_score = free_space[i].area();\n                    best_fit = free_space[i];\n                    best_fit_idx = i;\n                }\n            }\n            if (best_fit_idx == -1) {\n                best_fit = {0, 0, min(W, 1), min(W, 1)};\n            }\n        }\n\n        int h = 1, w = best_fit.area();\n        if (req_area > 0) {\n            h = max(1, (int)sqrt(req_area));\n            while (h > 0) {\n                w = (req_area + h - 1) / h;\n                if (w <= best_fit.x2 - best_fit.x1) break;\n                h--;\n            }\n            if (h == 0) {\n                h = best_fit.y2 - best_fit.y1;\n                w = best_fit.x2 - best_fit.x1;\n            }\n            h = min(h, best_fit.y2 - best_fit.y1);\n            w = min(w, best_fit.x2 - best_fit.x1);\n        }\n\n        int base_y = best_fit.y1;\n        int base_x = best_fit.x1;\n        if (k < (int)prev_rects.size()) {\n            base_y = max(base_y, prev_rects[k].y1);\n            base_x = max(base_x, prev_rects[k].x1);\n            if (base_y + h > best_fit.y2) base_y = best_fit.y2 - h;\n            if (base_x + w > best_fit.x2) base_x = best_fit.x2 - w;\n        }\n\n        result[k] = {base_y, base_x, base_y + h, base_x + w};\n        placed[k] = true;\n\n        free_space.erase(free_space.begin() + best_fit_idx);\n        vector<Rect> new_spaces = split_free_space(result[k], best_fit);\n        for (Rect& ns : new_spaces) {\n            if (ns.area() > 0) {\n                free_space.push_back(ns);\n            }\n        }\n    }\n\n    return result;\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int W = 1000, D, N;\n    cin >> W >> D >> N;\n\n    vector<vector<int>> areas(D, vector<int>(N));\n    for (int d = 0; d < D; d++) {\n        for (int k = 0; k < N; k++) {\n            cin >> areas[d][k];\n        }\n    }\n\n    vector<vector<Rect>> all_rects(D);\n\n    all_rects[0] = pack_day(W, areas[0], vector<Rect>());\n\n    for (int d = 1; d < D; d++) {\n        all_rects[d] = pack_day(W, areas[d], all_rects[d-1]);\n    }\n\n    for (int d = 0; d < D; d++) {\n        for (int k = 0; k < N; k++) {\n            Rect r = all_rects[d][k];\n            cout << r.y1 << \" \" << r.x1 << \" \" << r.y2 << \" \" << r.x2 << '\\n';\n        }\n    }\n\n    return 0;\n}","ahc032":"#include <iostream>\n#include <vector>\n#include <tuple>\n#include <array>\n#include <algorithm>\n#include <random>\n#include <chrono>\n#include <climits>\nusing namespace std;\n\nconst long long MOD = 998244353;\nconst int SIZE = 9;\nconst int BEAM_WIDTH = 10;\nconst int RANDOM_ITERATIONS = 20;\nconst int EARLY_STOP_STEPS = 3;\n\nstruct Candidate {\n    long long score;\n    array<array<long long, SIZE>, SIZE> board;\n    vector<tuple<int, int, int>> ops;\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<vector<long long>> a(SIZE, vector<long long>(SIZE));\n    for (int i = 0; i < SIZE; i++) {\n        for (int j = 0; j < SIZE; j++) {\n            cin >> a[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    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n\n    Candidate global_best;\n    for (int i = 0; i < SIZE; i++) {\n        for (int j = 0; j < SIZE; j++) {\n            global_best.board[i][j] = a[i][j] % MOD;\n            global_best.score += global_best.board[i][j];\n        }\n    }\n\n    int steps_without_improvement = 0;\n\n    while (steps_without_improvement < EARLY_STOP_STEPS) {\n        vector<Candidate> beam;\n        Candidate initial;\n\n        for (int i = 0; i < SIZE; i++) {\n            for (int j = 0; j < SIZE; j++) {\n                initial.board[i][j] = a[i][j] % MOD;\n            }\n        }\n        initial.score = global_best.score;\n        beam.push_back(initial);\n\n        for (int step = 0; step < K; step++) {\n            vector<Candidate> new_candidates;\n            new_candidates.reserve(beam.size() * M * (SIZE - 2) * (SIZE - 2) + beam.size() * RANDOM_ITERATIONS);\n\n            for (const auto& cand : beam) {\n                // Exhaustive search for best operations\n                for (int m = 0; m < M; m++) {\n                    for (int p = 0; p <= SIZE - 3; p++) {\n                        for (int q = 0; q <= SIZE - 3; q++) {\n                            Candidate next = cand;\n                            long long gain = 0;\n\n                            for (int di = 0; di < 3; di++) {\n                                for (int dj = 0; dj < 3; dj++) {\n                                    int i = p + di;\n                                    int j = q + dj;\n                                    long long s_val = stamps[m][di][dj];\n                                    long long old_val = next.board[i][j];\n                                    long long new_val = old_val + s_val;\n                                    if (new_val >= MOD) new_val -= MOD;\n                                    gain += (new_val - old_val);\n                                    next.board[i][j] = new_val;\n                                }\n                            }\n                            next.score = cand.score + gain;\n                            next.ops.push_back(make_tuple(m, p, q));\n                            new_candidates.push_back(next);\n\n                            if (next.score > global_best.score) {\n                                global_best = next;\n                                steps_without_improvement = 0;\n                            }\n                        }\n                    }\n                }\n\n                // Random operations\n                uniform_int_distribution<int> stamp_dist(0, M-1);\n                uniform_int_distribution<int> pos_dist(0, SIZE-3);\n                for (int r = 0; r < RANDOM_ITERATIONS; r++) {\n                    int m = stamp_dist(rng);\n                    int p = pos_dist(rng);\n                    int q = pos_dist(rng);\n                    Candidate next = cand;\n                    long long gain = 0;\n                    for (int di = 0; di < 3; di++) {\n                        for (int dj = 0; dj < 3; dj++) {\n                            int i = p + di;\n                            int j = q + dj;\n                            long long s_val = stamps[m][di][dj];\n                            long long old_val = next.board[i][j];\n                            long long new_val = old_val + s_val;\n                            if (new_val >= MOD) new_val -= MOD;\n                            gain += (new_val - old_val);\n                            next.board[i][j] = new_val;\n                        }\n                    }\n                    next.score = cand.score + gain;\n                    next.ops.push_back(make_tuple(m, p, q));\n                    if (next.score > global_best.score) {\n                        global_best = next;\n                        steps_without_improvement = 0;\n                    }\n                    new_candidates.push_back(next);\n                }\n            }\n\n            if (new_candidates.empty()) break;\n\n            sort(new_candidates.begin(), new_candidates.end(), [](const Candidate& a, const Candidate& b) {\n                return a.score > b.score;\n            });\n\n            beam.clear();\n            int num_to_keep = min(BEAM_WIDTH, (int)new_candidates.size());\n            for (int i = 0; i < num_to_keep; i++) {\n                beam.push_back(new_candidates[i]);\n            }\n\n            // Check if we're making progress\n            if (!beam.empty() && !beam[0].ops.empty()) {\n                steps_without_improvement++;\n            } else {\n                steps_without_improvement = max(0, steps_without_improvement - 1);\n            }\n        }\n    }\n\n    // Final local search\n    Candidate final = global_best;\n    bool improved;\n    do {\n        improved = false;\n        Candidate current = final;\n\n        for (int m = 0; m < M; m++) {\n            for (int p = 0; p <= SIZE - 3; p++) {\n                for (int q = 0; q <= SIZE - 3; q++) {\n                    Candidate next = current;\n                    long long gain = 0;\n\n                    for (int di = 0; di < 3; di++) {\n                        for (int dj = 0; dj < 3; dj++) {\n                            int i = p + di;\n                            int j = q + dj;\n                            long long s_val = stamps[m][di][dj];\n                            long long old_val = next.board[i][j];\n                            long long new_val = old_val + s_val;\n                            if (new_val >= MOD) new_val -= MOD;\n                            gain += (new_val - old_val);\n                            next.board[i][j] = new_val;\n                        }\n                    }\n                    next.score = current.score + gain;\n                    if (next.score > final.score) {\n                        final = next;\n                        improved = true;\n                    }\n                }\n            }\n        }\n    } while (improved && static_cast<size_t>(K) > final.ops.size());\n\n    cout << final.ops.size() << '\\n';\n    for (const auto& op : final.ops) {\n        cout << get<0>(op) << ' ' << get<1>(op) << ' ' << get<2>(op) << '\\n';\n    }\n\n    return 0;\n}","ahc033":"#include <bits/stdc++.h>\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        return x == other.x && y == other.y;\n    }\n};\n\nstruct Crane {\n    Point pos;\n    int hold = -1;\n};\n\nbool is_valid_point(Point p) {\n    return p.x >= 0 && p.x < 5 && p.y >= 0 && p.y < 5;\n}\n\nbool check_collision(const vector<Point>& new_pos, int i, int j) {\n    // Same position\n    if (new_pos[i] == new_pos[j]) return true;\n    // Swapping positions\n    if (new_pos[i].x == new_pos[j].x && abs(new_pos[i].y - new_pos[j].y) == 1) return true;\n    if (new_pos[i].y == new_pos[j].y && abs(new_pos[i].x - new_pos[j].x) == 1) return true;\n    return false;\n}\n\nbool any_collision(const vector<Point>& new_pos) {\n    for (int i = 0; i < 5; i++) {\n        for (int j = i+1; j < 5; j++) {\n            if (check_collision(new_pos, i, j)) {\n                return true;\n            }\n        }\n    }\n    return false;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    const int N = 5;\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<vector<int>> grid(5, vector<int>(5, -1));\n    vector<int> next_arrival(5, 0);\n    vector<int> dispatched(5, 0);\n    vector<int> next_expected(5);\n    for (int i = 0; i < 5; i++) {\n        next_expected[i] = 5 * i;\n    }\n\n    vector<Crane> cranes(5);\n    for (int i = 0; i < 5; i++) {\n        cranes[i] = {{i, 0}, -1};\n    }\n\n    vector<string> output_lines(5);\n\n    for (int turn = 0; turn < 10000; turn++) {\n        // Place new containers at receiving gates\n        for (int i = 0; i < 5; i++) {\n            if (next_arrival[i] >= 5) continue;\n            if (grid[i][0] != -1) continue;\n\n            bool blocked = false;\n            for (int k = 0; k < 5; k++) {\n                if (cranes[k].pos.x == i && cranes[k].pos.y == 0 && cranes[k].hold != -1) {\n                    blocked = true;\n                    break;\n                }\n            }\n\n            if (!blocked) {\n                grid[i][0] = A[i][next_arrival[i]++];\n            }\n        }\n\n        // Plan moves with strict collision avoidance\n        vector<Point> old_pos(5);\n        for (int i = 0; i < 5; i++) {\n            old_pos[i] = cranes[i].pos;\n        }\n\n        vector<char> actions(5, '.');\n        vector<Point> new_pos(5);\n        for (int i = 0; i < 5; i++) {\n            new_pos[i] = old_pos[i];\n        }\n\n        // Process cranes in order 0-4\n        for (int i = 0; i < 5; i++) {\n            bool is_large = (i == 0);\n            bool carrying = (cranes[i].hold != -1);\n\n            if (carrying) {\n                int b = cranes[i].hold;\n                int target_row = b / 5;\n                Point target(target_row, 4);\n\n                if (old_pos[i] == target) {\n                    actions[i] = 'Q';\n                } else {\n                    // Move toward target in cardinal directions only\n                    if (old_pos[i].x < target.x && is_valid_point(Point(old_pos[i].x+1, old_pos[i].y))) {\n                        new_pos[i].x++;\n                        actions[i] = 'D';\n                    } else if (old_pos[i].x > target.x && is_valid_point(Point(old_pos[i].x-1, old_pos[i].y))) {\n                        new_pos[i].x--;\n                        actions[i] = 'U';\n                    } else if (old_pos[i].y < target.y && is_valid_point(Point(old_pos[i].x, old_pos[i].y+1))) {\n                        new_pos[i].y++;\n                        actions[i] = 'R';\n                    } else if (old_pos[i].y > target.y && is_valid_point(Point(old_pos[i].x, old_pos[i].y-1))) {\n                        new_pos[i].y--;\n                        actions[i] = 'L';\n                    }\n                }\n            } else {\n                if (grid[old_pos[i].x][old_pos[i].y] != -1) {\n                    actions[i] = 'P';\n                } else if (i == 0) {\n                    // Large crane looks for containers to move\n                    Point goal(-1, -1);\n                    for (int x = 0; x < 5; x++) {\n                        for (int y = 0; y < 5; y++) {\n                            if (grid[x][y] != -1) {\n                                int b = grid[x][y];\n                                int r = b / 5;\n                                if (b == next_expected[r]) {\n                                    goal = {x, y};\n                                    break;\n                                }\n                            }\n                        }\n                        if (goal.x != -1) break;\n                    }\n                    if (goal.x == -1) goal = {2, 2};\n\n                    // Move toward goal in cardinal directions only\n                    if (old_pos[i].x < goal.x && is_valid_point(Point(old_pos[i].x+1, old_pos[i].y))) {\n                        new_pos[i].x++;\n                        actions[i] = 'D';\n                    } else if (old_pos[i].x > goal.x && is_valid_point(Point(old_pos[i].x-1, old_pos[i].y))) {\n                        new_pos[i].x--;\n                        actions[i] = 'U';\n                    } else if (old_pos[i].y < goal.y && is_valid_point(Point(old_pos[i].x, old_pos[i].y+1))) {\n                        new_pos[i].y++;\n                        actions[i] = 'R';\n                    } else if (old_pos[i].y > goal.y && is_valid_point(Point(old_pos[i].x, old_pos[i].y-1))) {\n                        new_pos[i].y--;\n                        actions[i] = 'L';\n                    }\n                } else {\n                    // Small cranes only move right if completely safe\n                    if (old_pos[i].y < 4 && grid[old_pos[i].x][old_pos[i].y + 1] == -1) {\n                        bool path_clear = true;\n                        for (int k = 0; k < 5; k++) {\n                            if (k != i && cranes[k].pos.x == old_pos[i].x && cranes[k].pos.y == old_pos[i].y + 1) {\n                                path_clear = false;\n                                break;\n                            }\n                        }\n                        if (path_clear) {\n                            new_pos[i].y++;\n                            actions[i] = 'R';\n                        }\n                    }\n                }\n            }\n        }\n\n        // Cancel entire turn if any collision would occur\n        if (any_collision(new_pos)) {\n            fill(actions.begin(), actions.end(), '.');\n            for (int i = 0; i < 5; i++) {\n                new_pos[i] = old_pos[i];\n            }\n        }\n\n        // Execute actions\n        for (int i = 0; i < 5; i++) {\n            output_lines[i] += actions[i];\n        }\n\n        for (int i = 0; i < 5; i++) {\n            if (actions[i] == 'P') {\n                if (grid[old_pos[i].x][old_pos[i].y] != -1) {\n                    cranes[i].hold = grid[old_pos[i].x][old_pos[i].y];\n                    grid[old_pos[i].x][old_pos[i].y] = -1;\n                }\n            } else if (actions[i] == 'Q') {\n                if (cranes[i].hold != -1) {\n                    grid[old_pos[i].x][old_pos[i].y] = cranes[i].hold;\n                    cranes[i].hold = -1;\n                }\n            } else if (actions[i] == 'U') {\n                cranes[i].pos.x--;\n            } else if (actions[i] == 'D') {\n                cranes[i].pos.x++;\n            } else if (actions[i] == 'L') {\n                cranes[i].pos.y--;\n            } else if (actions[i] == 'R') {\n                cranes[i].pos.y++;\n            }\n            cranes[i].pos = new_pos[i];\n        }\n\n        // Dispatch containers\n        for (int i = 0; i < 5; i++) {\n            if (grid[i][4] != -1) {\n                int b = grid[i][4];\n                int r = b / 5;\n                if (b == next_expected[r]) {\n                    dispatched[r]++;\n                    next_expected[r] = 5 * r + dispatched[r];\n                    grid[i][4] = -1;\n                }\n            }\n        }\n\n        if (accumulate(dispatched.begin(), dispatched.end(), 0) == 25) {\n            break;\n        }\n    }\n\n    // Pad output strings\n    int max_len = 0;\n    for (const auto& s : output_lines) {\n        max_len = max(max_len, (int)s.size());\n    }\n    for (auto& s : output_lines) {\n        while ((int)s.size() < max_len) {\n            s += '.';\n        }\n    }\n\n    for (const auto& s : output_lines) {\n        cout << s << '\\n';\n    }\n\n    return 0;\n}","ahc034":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    const int N = 20;\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<int>> remaining_source(N, vector<int>(N, 0));\n    vector<vector<int>> remaining_sink(N, vector<int>(N, 0));\n\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (grid[i][j] > 0) {\n                remaining_source[i][j] = grid[i][j];\n            } else if (grid[i][j] < 0) {\n                remaining_sink[i][j] = -grid[i][j];\n            }\n        }\n    }\n\n    int cur_i = 0, cur_j = 0;\n    int cur_load = 0;\n\n    auto has_remaining = [&]() -> bool {\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                if (remaining_source[i][j] > 0 || remaining_sink[i][j] > 0) {\n                    return true;\n                }\n            }\n        }\n        return false;\n    };\n\n    while (has_remaining()) {\n        bool handled = false;\n\n        if (remaining_source[cur_i][cur_j] > 0) {\n            int d = remaining_source[cur_i][cur_j];\n            cout << \"+\" << d << \"\\n\";\n            cur_load += d;\n            remaining_source[cur_i][cur_j] = 0;\n            handled = true;\n        }\n        else if (remaining_sink[cur_i][cur_j] > 0 && cur_load > 0) {\n            int d = min(remaining_sink[cur_i][cur_j], cur_load);\n            cout << \"-\" << d << \"\\n\";\n            cur_load -= d;\n            remaining_sink[cur_i][cur_j] -= d;\n            handled = true;\n        }\n\n        if (handled) {\n            continue;\n        }\n\n        int best_i = -1, best_j = -1;\n        int min_score = INT_MAX;\n\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                if (remaining_source[i][j] > 0) {\n                    int dist = abs(i - cur_i) + abs(j - cur_j);\n                    int score = dist * (100 + cur_load);\n                    if (score < min_score) {\n                        min_score = score;\n                        best_i = i;\n                        best_j = j;\n                    }\n                }\n                if (remaining_sink[i][j] > 0 && cur_load > 0) {\n                    int dist = abs(i - cur_i) + abs(j - cur_j);\n                    int score = dist * (100 + cur_load);\n                    if (score < min_score) {\n                        min_score = score;\n                        best_i = i;\n                        best_j = j;\n                    }\n                }\n            }\n        }\n\n        if (best_i == -1) {\n            break;\n        }\n\n        int dx = best_i - cur_i;\n        int dy = best_j - cur_j;\n        string moves = \"\";\n        if (dx > 0) moves += string(dx, 'D');\n        else if (dx < 0) moves += string(-dx, 'U');\n        if (dy > 0) moves += string(dy, 'R');\n        else if (dy < 0) moves += string(-dy, 'L');\n\n        for (char c : moves) {\n            cout << c << \"\\n\";\n            if (c == 'U') cur_i--;\n            else if (c == 'D') cur_i++;\n            else if (c == 'L') cur_j--;\n            else if (c == 'R') cur_j++;\n        }\n    }\n\n    return 0;\n}","ahc035":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Seed {\n    long long V;\n    double ratio_score;\n    double potential_score;\n    int birth_turn;\n    int last_used;\n    vector<int> values;\n};\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N = 6, M = 15, T = 10;\n    cin >> N >> M >> T;\n\n    int seed_count = 2 * N * (N - 1);\n    vector<Seed> seeds(seed_count);\n    vector<int> X(M, 0);\n\n    // Read initial seeds\n    for (int k = 0; k < seed_count; k++) {\n        seeds[k].values.resize(M);\n        seeds[k].V = 0;\n        seeds[k].birth_turn = 0;\n        seeds[k].last_used = -1;\n        for (int l = 0; l < M; l++) {\n            cin >> seeds[k].values[l];\n            seeds[k].V += seeds[k].values[l];\n            if (seeds[k].values[l] > X[l]) {\n                X[l] = seeds[k].values[l];\n            }\n        }\n        // Initial scoring\n        seeds[k].ratio_score = 0.0;\n        seeds[k].potential_score = 0.0;\n        for (int l = 0; l < M; l++) {\n            double ratio = (X[l] == 0) ? 1.0 : static_cast<double>(seeds[k].values[l]) / X[l];\n            seeds[k].ratio_score += ratio;\n            seeds[k].potential_score += min(seeds[k].values[l], X[l]);\n        }\n        seeds[k].ratio_score /= M;\n        seeds[k].potential_score /= M;\n    }\n\n    // Grid positions with degree and distance to center\n    vector<tuple<int, int, int, double>> positions;\n    double center = (N - 1) / 2.0;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            int deg = 4;\n            if (i == 0 || i == N-1) deg--;\n            if (j == 0 || j == N-1) deg--;\n            double dist = (i - center) * (i - center) + (j - center) * (j - center);\n            positions.emplace_back(i, j, deg, dist);\n        }\n    }\n\n    // Sort positions\n    sort(positions.begin(), positions.end(), [](const auto& a, const auto& b) {\n        if (get<2>(a) != get<2>(b)) return get<2>(a) > get<2>(b);\n        return get<3>(a) < get<3>(b);\n    });\n\n    for (int t = 0; t < T; t++) {\n        // Update X and recompute scores\n        fill(X.begin(), X.end(), 0);\n        for (int k = 0; k < seed_count; k++) {\n            for (int l = 0; l < M; l++) {\n                if (seeds[k].values[l] > X[l]) {\n                    X[l] = seeds[k].values[l];\n                }\n            }\n        }\n\n        // recompute scores\n        for (int k = 0; k < seed_count; k++) {\n            seeds[k].ratio_score = 0.0;\n            seeds[k].potential_score = 0.0;\n            for (int l = 0; l < M; l++) {\n                double ratio = (X[l] == 0) ? 1.0 : static_cast<double>(seeds[k].values[l]) / X[l];\n                seeds[k].ratio_score += ratio;\n                seeds[k].potential_score += min(seeds[k].values[l], X[l]);\n            }\n            seeds[k].ratio_score /= M;\n            seeds[k].potential_score /= M;\n        }\n\n        // Compute fitness scores\n        vector<pair<double, int>> fitness_list;\n        for (int k = 0; k < seed_count; k++) {\n            double age_factor = 1.0 - (t - seeds[k].birth_turn)/20.0;\n            age_factor = max(0.5, age_factor);\n            double fitness = seeds[k].ratio_score * 0.6 + seeds[k].potential_score * 0.3 +\n                           (t > 0 && seeds[k].last_used == t-1 ? 0.1 : 0.0);\n            fitness_list.emplace_back(fitness * age_factor, k);\n        }\n        sort(fitness_list.begin(), fitness_list.end(), greater<>());\n\n        // Select seeds for grid\n        vector<vector<int>> grid(N, vector<int>(N, -1));\n        vector<bool> used(seed_count, false);\n\n        // Assign top seeds to best positions\n        for (int idx = 0; idx < min(12, seed_count); idx++) {\n            int seed_idx = fitness_list[idx].second;\n            used[seed_idx] = true;\n\n            // Find best position\n            double best_score = -1;\n            pair<int, int> best_pos = {0, 0};\n            for (int pos_idx = 0; pos_idx < N*N; pos_idx++) {\n                auto [i, j, deg, dist] = positions[pos_idx];\n                if (grid[i][j] != -1) continue;\n\n                double pos_score = 0.0;\n                int neighbors = 0;\n                vector<pair<int, int>> n = {{i-1,j}, {i+1,j}, {i,j-1}, {i,j+1}};\n                for (auto [ni, nj] : n) {\n                    if (ni >= 0 && ni < N && nj >= 0 && nj < N && grid[ni][nj] != -1) {\n                        neighbors++;\n                        int other = grid[ni][nj];\n                        for (int l = 0; l < M; l++) {\n                            pos_score += max(seeds[seed_idx].values[l], seeds[other].values[l]);\n                        }\n                    }\n                }\n                pos_score /= (neighbors * M + 1e-9);\n                if (pos_score > best_score) {\n                    best_score = pos_score;\n                    best_pos = {i, j};\n                }\n            }\n            grid[best_pos.first][best_pos.second] = seed_idx;\n            seeds[seed_idx].last_used = t;\n        }\n\n        // Fill remaining positions\n        for (int idx = 0; idx < N*N; idx++) {\n            auto [i, j, deg, dist] = positions[idx];\n            if (grid[i][j] != -1) continue;\n\n            int best_seed = -1;\n            double best_score = -1;\n\n            for (int k = 0; k < seed_count; k++) {\n                if (used[k]) continue;\n\n                // Calculate compatibility with existing neighbors\n                double comp_score = 0.0;\n                int neighbors = 0;\n                vector<pair<int, int>> n = {{i-1,j}, {i+1,j}, {i,j-1}, {i,j+1}};\n                for (auto [ni, nj] : n) {\n                    if (ni >= 0 && ni < N && nj >= 0 && nj < N && grid[ni][nj] != -1) {\n                        neighbors++;\n                        int other = grid[ni][nj];\n                        for (int l = 0; l < M; l++) {\n                            comp_score += max(seeds[k].values[l], seeds[other].values[l]);\n                        }\n                    }\n                }\n                comp_score /= (neighbors * M + 1e-9);\n                double total_score = seeds[k].ratio_score * 0.5 + comp_score * 0.5;\n\n                if (total_score > best_score) {\n                    best_score = total_score;\n                    best_seed = k;\n                }\n            }\n\n            if (best_seed == -1) {\n                for (int k = 0; k < seed_count; k++) {\n                    if (!used[k]) {\n                        best_seed = k;\n                        break;\n                    }\n                }\n            }\n\n            grid[i][j] = best_seed;\n            used[best_seed] = true;\n            seeds[best_seed].last_used = t;\n        }\n\n        // Simple local optimization\n        for (int opt = 0; opt < 15; opt++) {\n            int i = rand() % N;\n            int j = rand() % N;\n            int seed1 = grid[i][j];\n\n            vector<pair<int, int>> neighbors = {{i-1,j}, {i+1,j}, {i,j-1}, {i,j+1}};\n            for (auto [ni, nj] : neighbors) {\n                if (ni < 0 || ni >= N || nj < 0 || nj >= N) continue;\n                int seed2 = grid[ni][nj];\n\n                // Calculate current compatibility\n                double current = 0.0;\n                int c_neighbors = 0;\n                vector<pair<int, int>> n1 = {{i-1,j}, {i+1,j}, {i,j-1}, {i,j+1}};\n                for (auto [x, y] : n1) {\n                    if (x >= 0 && x < N && y >= 0 && y < N && grid[x][y] != -1 && !(x == ni && y == nj)) {\n                        c_neighbors++;\n                        int other = grid[x][y];\n                        for (int l = 0; l < M; l++) {\n                            current += max(seeds[seed1].values[l], seeds[other].values[l]);\n                        }\n                    }\n                }\n                vector<pair<int, int>> n2 = {{ni-1,nj}, {ni+1,nj}, {ni,nj-1}, {ni,nj+1}};\n                for (auto [x, y] : n2) {\n                    if (x >= 0 && x < N && y >= 0 && y < N && grid[x][y] != -1 && !(x == i && y == j)) {\n                        c_neighbors++;\n                        int other = grid[x][y];\n                        for (int l = 0; l < M; l++) {\n                            current += max(seeds[seed2].values[l], seeds[other].values[l]);\n                        }\n                    }\n                }\n                current /= (c_neighbors * M + 1e-9);\n\n                // Calculate swapped compatibility\n                double swapped = 0.0;\n                int s_neighbors = 0;\n                for (auto [x, y] : n1) {\n                    if (x >= 0 && x < N && y >= 0 && y < N && grid[x][y] != -1) {\n                        s_neighbors++;\n                        int other = grid[x][y];\n                        for (int l = 0; l < M; l++) {\n                            swapped += max(seeds[seed2].values[l], seeds[other].values[l]);\n                        }\n                    }\n                }\n                for (auto [x, y] : n2) {\n                    if (x >= 0 && x < N && y >= 0 && y < N && grid[x][y] != -1) {\n                        s_neighbors++;\n                        int other = grid[x][y];\n                        for (int l = 0; l < M; l++) {\n                            swapped += max(seeds[seed1].values[l], seeds[other].values[l]);\n                        }\n                    }\n                }\n                swapped /= (s_neighbors * M + 1e-9);\n\n                if (swapped > current) {\n                    swap(grid[i][j], grid[ni][nj]);\n                    seeds[seed1].last_used = t;\n                    seeds[seed2].last_used = t;\n                }\n            }\n        }\n\n        // Output grid\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 << '\\n';\n        }\n        cout.flush();\n\n        // Read new seeds\n        for (int k = 0; k < seed_count; k++) {\n            seeds[k].birth_turn++;\n            seeds[k].V = 0;\n            for (int l = 0; l < M; l++) {\n                cin >> seeds[k].values[l];\n                seeds[k].V += seeds[k].values[l];\n            }\n        }\n    }\n\n    return 0;\n}","ahc038":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <cctype>\n#include <cmath>\n#include <string>\n#include <map>\n#include <set>\n#include <queue>\n#include <stack>\n#include <utility>\n#include <climits>\nusing namespace std;\n\ntypedef pair<int, int> Point;\n\nvector<char> get_manhattan_path(Point from, Point to) {\n    vector<char> path;\n    int dx = to.first - from.first;\n    int dy = to.second - from.second;\n    char x_dir = 0;\n    char y_dir = 0;\n    if (dx > 0) x_dir = 'D';\n    else if (dx < 0) x_dir = 'U';\n    if (dy > 0) y_dir = 'R';\n    else if (dy < 0) y_dir = 'L';\n\n    for (int i = 0; i < abs(dx); i++) {\n        path.push_back(x_dir);\n    }\n    for (int i = 0; i < abs(dy); i++) {\n        path.push_back(y_dir);\n    }\n    return path;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, V;\n    cin >> N >> M >> V;\n\n    vector<string> s_grid(N);\n    vector<string> 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    vector<Point> A;\n    vector<Point> B;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (s_grid[i][j] == '1') {\n                A.push_back({i, j});\n            }\n            if (t_grid[i][j] == '1') {\n                B.push_back({i, j});\n            }\n        }\n    }\n\n    sort(A.begin(), A.end());\n    sort(B.begin(), B.end());\n\n    int V_prime = 5;\n    cout << V_prime << \"\\n\";\n    for (int u = 1; u < V_prime; u++) {\n        cout << 0 << \" \" << 1 << \"\\n\";\n    }\n    cout << 0 << \" \" << 0 << \"\\n\";\n\n    vector<string> operations;\n\n    operations.push_back(\".\" + string(\"RRRL\") + string(\".....\"));\n    operations.push_back(\".\" + string(\"..R.\") + string(\".....\"));\n\n    Point current_root = {0, 0};\n\n    vector<pair<Point, Point>> valid_pairs;\n    for (int i = 0; i < M; i++) {\n        if (A[i] != B[i]) {\n            valid_pairs.push_back({A[i], B[i]});\n        }\n    }\n\n    auto get_options = [&](int i, int j) -> vector<pair<int, Point>> {\n        vector<pair<int, Point>> options;\n        if (j - 1 >= 0) {\n            options.push_back({1, {i, j - 1}});\n        }\n        if (i - 1 >= 0) {\n            options.push_back({2, {i - 1, j}});\n        }\n        if (j + 1 < N) {\n            options.push_back({3, {i, j + 1}});\n        }\n        if (i + 1 < N) {\n            options.push_back({4, {i + 1, j}});\n        }\n        return options;\n    };\n\n    for (auto &p : valid_pairs) {\n        Point a = p.first;\n        Point b = p.second;\n\n        auto options_a = get_options(a.first, a.second);\n        if (options_a.empty()) {\n            continue;\n        }\n        int best_u = -1;\n        Point best_R_a;\n        int min_dist = INT_MAX;\n        for (auto &opt : options_a) {\n            int u_idx = opt.first;\n            Point R = opt.second;\n            int dist = abs(current_root.first - R.first) + abs(current_root.second - R.second);\n            if (dist < min_dist) {\n                min_dist = dist;\n                best_u = u_idx;\n                best_R_a = R;\n            }\n        }\n        Point R_a = best_R_a;\n\n        auto options_b = get_options(b.first, b.second);\n        if (options_b.empty()) {\n            continue;\n        }\n        auto best_b = options_b[0];\n        int v_rel = best_b.first;\n        Point R_b = best_b.second;\n\n        vector<char> path1 = get_manhattan_path(current_root, R_a);\n        if (path1.empty()) {\n            string action_str = \".....\";\n            action_str[0] = '.';\n            if (best_u == 1) action_str[1] = 'P';\n            else if (best_u == 2) action_str[2] = 'P';\n            else if (best_u == 3) action_str[3] = 'P';\n            else if (best_u == 4) action_str[4] = 'P';\n            operations.push_back(\".\" + string(\"....\") + action_str);\n        } else {\n            for (int idx = 0; idx < path1.size(); idx++) {\n                string rot_str = \"....\";\n                string action_str = \".....\";\n                if (idx == path1.size() - 1) {\n                    action_str[0] = '.';\n                    if (best_u == 1) action_str[1] = 'P';\n                    else if (best_u == 2) action_str[2] = 'P';\n                    else if (best_u == 3) action_str[3] = 'P';\n                    else if (best_u == 4) action_str[4] = 'P';\n                }\n                string op = string(1, path1[idx]) + rot_str + action_str;\n                operations.push_back(op);\n            }\n        }\n\n        vector<char> path2 = get_manhattan_path(R_a, R_b);\n        if (path2.empty()) {\n            string action_str = \".....\";\n            action_str[0] = '.';\n            if (v_rel == 1) action_str[1] = 'P';\n            else if (v_rel == 2) action_str[2] = 'P';\n            else if (v_rel == 3) action_str[3] = 'P';\n            else if (v_rel == 4) action_str[4] = 'P';\n            operations.push_back(\".\" + string(\"....\") + action_str);\n        } else {\n            for (int idx = 0; idx < path2.size(); idx++) {\n                string rot_str = \"....\";\n                string action_str = \".....\";\n                if (idx == path2.size() - 1) {\n                    action_str[0] = '.';\n                    if (v_rel == 1) action_str[1] = 'P';\n                    else if (v_rel == 2) action_str[2] = 'P';\n                    else if (v_rel == 3) action_str[3] = 'P';\n                    else if (v_rel == 4) action_str[4] = 'P';\n                }\n                string op = string(1, path2[idx]) + rot_str + action_str;\n                operations.push_back(op);\n            }\n        }\n\n        current_root = R_b;\n    }\n\n    for (string &op : operations) {\n        cout << op << \"\\n\";\n    }\n\n    return 0;\n}","ahc039":"#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\n\nstruct Point {\n    int x, y;\n    bool operator==(const Point& other) const {\n        return x == other.x && y == other.y;\n    }\n};\n\nstruct Rect {\n    int x1, x2, y1, y2;\n    Rect() : x1(0), x2(0), y1(0), y2(0) {}\n    Rect(int a, int b, int c, int d) {\n        x1 = min(a, b); x2 = max(a, b);\n        y1 = min(c, d); y2 = max(c, d);\n    }\n    int width() const { return x2 - x1; }\n    int height() const { return y2 - y1; }\n    bool valid() const { return x1 < x2 && y1 < y2; }\n};\n\nconst int N = 5000;\nvector<Point> mackerels(N), sardines(N);\nvector<int> xs, ys;\nvector<vector<int>> grid_val;\nvector<vector<ll>> P;\nvector<int> x_to_idx(100001, -1);\nvector<int> y_to_idx(100001, -1);\n\nvoid preprocess() {\n    xs.clear(); ys.clear();\n    for (int i = 0; i < N; i++) {\n        xs.push_back(mackerels[i].x);\n        ys.push_back(mackerels[i].y);\n        xs.push_back(sardines[i].x);\n        ys.push_back(sardines[i].y);\n    }\n    sort(xs.begin(), xs.end());\n    xs.erase(unique(xs.begin(), xs.end()), xs.end());\n    sort(ys.begin(), ys.end());\n    ys.erase(unique(ys.begin(), ys.end()), ys.end());\n\n    grid_val.assign(xs.size(), vector<int>(ys.size(), 0));\n    for (int i = 0; i < N; i++) {\n        int ix = lower_bound(xs.begin(), xs.end(), mackerels[i].x) - xs.begin();\n        int iy = lower_bound(ys.begin(), ys.end(), mackerels[i].y) - ys.begin();\n        if (ix < (int)xs.size() && iy < (int)ys.size()) grid_val[ix][iy] = 1;\n    }\n    for (int i = 0; i < N; i++) {\n        int ix = lower_bound(xs.begin(), xs.end(), sardines[i].x) - xs.begin();\n        int iy = lower_bound(ys.begin(), ys.end(), sardines[i].y) - ys.begin();\n        if (ix < (int)xs.size() && iy < (int)ys.size()) grid_val[ix][iy] -= 1;\n    }\n\n    P.assign(xs.size()+1, vector<ll>(ys.size()+1, 0));\n    for (int i = 0; i < (int)xs.size(); i++) {\n        for (int j = 0; j < (int)ys.size(); j++) {\n            P[i+1][j+1] = P[i][j+1] + P[i+1][j] - P[i][j] + grid_val[i][j];\n        }\n    }\n\n    for (int i = 0; i <= 100000; i++) {\n        x_to_idx[i] = lower_bound(xs.begin(), xs.end(), i) - xs.begin();\n        y_to_idx[i] = lower_bound(ys.begin(), ys.end(), i) - ys.begin();\n    }\n}\n\nll get_sum(int x1, int x2, int y1, int y2) {\n    if (x1 > x2 || y1 > y2) return 0;\n    int i1 = max(0, x_to_idx[x1]);\n    int i2 = min((int)xs.size()-1, x_to_idx[x2]);\n    int j1 = max(0, y_to_idx[y1]);\n    int j2 = min((int)ys.size()-1, y_to_idx[y2]);\n    if (i1 > i2 || j1 > j2) return 0;\n    return P[i2+1][j2+1] - P[i1][j2+1] - P[i2+1][j1] + P[i1][j1];\n}\n\nRect shrink_rect(Rect r) {\n    Rect best = r;\n    ll best_score = get_sum(r.x1, r.x2, r.y1, r.y2);\n\n    // Try shrinking from each side\n    if (r.width() > 1) {\n        Rect left = r;\n        left.x1++;\n        ll score = get_sum(left.x1, left.x2, left.y1, left.y2);\n        if (score > best_score) { best = left; best_score = score; }\n\n        Rect right = r;\n        right.x2--;\n        score = get_sum(right.x1, right.x2, right.y1, right.y2);\n        if (score > best_score) { best = right; best_score = score; }\n    }\n\n    if (r.height() > 1) {\n        Rect bottom = r;\n        bottom.y1++;\n        ll score = get_sum(bottom.x1, bottom.x2, bottom.y1, bottom.y2);\n        if (score > best_score) { best = bottom; best_score = score; }\n\n        Rect top = r;\n        top.y2--;\n        score = get_sum(top.x1, top.x2, top.y1, top.y2);\n        if (score > best_score) { best = top; best_score = score; }\n    }\n\n    return best;\n}\n\nvector<Point> make_rect_polygon(Rect r) {\n    return {\n        {r.x1, r.y1},\n        {r.x2, r.y1},\n        {r.x2, r.y2},\n        {r.x1, r.y2}\n    };\n}\n\nvector<Point> make_l_shape(Rect outer, Rect inner) {\n    if (!inner.valid()) return make_rect_polygon(outer);\n    vector<Point> poly;\n    poly.push_back({outer.x1, outer.y1});\n    poly.push_back({outer.x2, outer.y1});\n    poly.push_back({outer.x2, outer.y2});\n    poly.push_back({inner.x2, outer.y2});\n    poly.push_back({inner.x2, inner.y2});\n    poly.push_back({inner.x1, inner.y2});\n    poly.push_back({inner.x1, inner.y1});\n    poly.push_back({outer.x1, inner.y1});\n    return poly;\n}\n\nvector<Rect> find_candidate_rects() {\n    vector<Rect> rects;\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n\n    // 1. Bounding box of mackerels\n    int mx1 = 100000, mx2 = 0, my1 = 100000, my2 = 0;\n    for (auto& p : mackerels) {\n        mx1 = min(mx1, p.x);\n        mx2 = max(mx2, p.x);\n        my1 = min(my1, p.y);\n        my2 = max(my2, p.y);\n    }\n    rects.emplace_back(mx1, mx2, my1, my2);\n\n    // 2. Rectangles around each mackerel\n    uniform_int_distribution<int> mackerel_dist(0, N-1);\n    uniform_int_distribution<int> size_dist(2000, 10000);\n    for (int i = 0; i < 50 && rects.size() < 200; i++) {\n        Point p = mackerels[mackerel_dist(rng)];\n        int size = size_dist(rng);\n        Rect r(max(0, p.x - size/2), min(100000, p.x + size/2),\n               max(0, p.y - size/2), min(100000, p.y + size/2));\n        if (r.valid()) {\n            ll score = get_sum(r.x1, r.x2, r.y1, r.y2);\n            if (score >= 0) {\n                rects.push_back(r);\n            }\n        }\n    }\n\n    // 3. Some random rectangles\n    uniform_int_distribution<int> coord_dist(0, 100000);\n    for (int i = 0; i < 200 && rects.size() < 500; i++) {\n        int x1 = coord_dist(rng);\n        int x2 = coord_dist(rng);\n        int y1 = coord_dist(rng);\n        int y2 = coord_dist(rng);\n        if (x1 > x2) swap(x1, x2);\n        if (y1 > y2) swap(y1, y2);\n        Rect r(x1, x2, y1, y2);\n        if (r.valid() && get_sum(r.x1, r.x2, r.y1, r.y2) >= 0) {\n            rects.push_back(r);\n        }\n    }\n\n    return rects;\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    for (int i = 0; i < N; i++) {\n        cin >> mackerels[i].x >> mackerels[i].y;\n    }\n    for (int i = 0; i < N; i++) {\n        cin >> sardines[i].x >> sardines[i].y;\n    }\n\n    preprocess();\n\n    vector<Rect> candidates = find_candidate_rects();\n    vector<Point> best_poly;\n    ll best_score = LLONG_MIN;\n\n    // Evaluate all candidates\n    for (Rect r : candidates) {\n        Rect shrunk = shrink_rect(r);\n        ll score = get_sum(shrunk.x1, shrunk.x2, shrunk.y1, shrunk.y2);\n        if (score > best_score) {\n            best_score = score;\n            best_poly = make_rect_polygon(shrunk);\n        }\n\n        // Try L-shape variant\n        int cx = shrunk.x1 + shrunk.width()/2;\n        int cy = shrunk.y1 + shrunk.height()/2;\n        Rect inner(max(shrunk.x1, cx-2000), min(shrunk.x2, cx+2000),\n                   max(shrunk.y1, cy-2000), min(shrunk.y2, cy+2000));\n        if (inner.valid()) {\n            vector<Point> l_poly = make_l_shape(shrunk, inner);\n            ll l_score = get_sum(l_poly[0].x, l_poly[2].x, l_poly[0].y, l_poly[2].y);\n            if (l_score > best_score) {\n                best_score = l_score;\n                best_poly = l_poly;\n            }\n        }\n    }\n\n    // Fallback if no good polygon found\n    if (best_poly.empty()) {\n        best_poly = make_rect_polygon(Rect(0, 100000, 0, 100000));\n    }\n\n    // Output\n    cout << best_poly.size() << '\\n';\n    for (const auto& p : best_poly) {\n        cout << p.x << \" \" << p.y << '\\n';\n    }\n\n    return 0;\n}","ahc040":"#include <iostream>\n#include <vector>\n#include <tuple>\n#include <algorithm>\n#include <random>\n#include <chrono>\n#include <climits>\n#include <cmath>\n#include <cctype>\nusing namespace std;\n\nstruct Rect {\n    long long x, y, w, h;\n};\n\nstruct Instruction {\n    int p, r, b;\n    char d;\n};\n\ndouble rand_double(double min_val, double max_val, mt19937& rng) {\n    uniform_real_distribution<double> dist(min_val, max_val);\n    return dist(rng);\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    for (int i = 0; i < N; i++) {\n        long long wp, hp;\n        cin >> wp >> hp;\n        w_est[i] = static_cast<double>(wp);\n        h_est[i] = static_cast<double>(hp);\n    }\n\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n\n    const int M = 50;\n\n    for (int turn = 0; turn < T; turn++) {\n        vector<Instruction> best_instructions;\n        long long best_score = LLONG_MAX;\n\n        for (int cand_idx = 0; cand_idx < M; cand_idx++) {\n            vector<Instruction> inst_seq;\n            vector<Rect> layout;\n            long long cur_max_x = 0;\n            long long cur_max_y = 0;\n\n            vector<double> w_cand = w_est;\n            vector<double> h_cand = h_est;\n            if (cand_idx > 0) {\n                double noise_factor = 0.1 * (1.0 - static_cast<double>(cand_idx) / M);\n                for (int i = 0; i < N; i++) {\n                    double noise = 1.0 + rand_double(-noise_factor, noise_factor, rng);\n                    w_cand[i] *= noise;\n                    h_cand[i] *= noise;\n                }\n            }\n\n            bool valid_placement = true;\n            for (int i = 0; i < N; i++) {\n                vector<tuple<int, int, char, int, long long, long long, long long, long long>> options;\n\n                for (int rotation : {0, 1}) {\n                    double w_val = (rotation == 0) ? w_cand[i] : h_cand[i];\n                    double h_val = (rotation == 0) ? h_cand[i] : w_cand[i];\n                    long long w_rect = llround(w_val);\n                    long long h_rect = llround(h_val);\n                    w_rect = max(1LL, min(1000000000LL, w_rect));\n                    h_rect = max(1LL, min(1000000000LL, h_rect));\n\n                    vector<tuple<char, int>> dir_b_opts;\n                    dir_b_opts.push_back({'U', -1});\n                    if (!layout.empty()) {\n                        dir_b_opts.push_back({'U', static_cast<int>(layout.size() - 1)});\n                    }\n                    dir_b_opts.push_back({'L', -1});\n                    if (!layout.empty()) {\n                        dir_b_opts.push_back({'L', static_cast<int>(layout.size() - 1)});\n                    }\n\n                    for (auto [d, b] : dir_b_opts) {\n                        long long x_fixed, y_fixed;\n                        if (d == 'U') {\n                            if (b == -1) {\n                                x_fixed = 0;\n                            } else {\n                                x_fixed = layout[b].x + layout[b].w;\n                            }\n                            y_fixed = 0;\n                            for (const auto& rect : layout) {\n                                if (rect.x < x_fixed + w_rect && rect.x + rect.w > x_fixed) {\n                                    y_fixed = max(y_fixed, rect.y + rect.h);\n                                }\n                            }\n                        } else {\n                            if (b == -1) {\n                                y_fixed = 0;\n                            } else {\n                                y_fixed = layout[b].y + layout[b].h;\n                            }\n                            x_fixed = 0;\n                            for (const auto& rect : layout) {\n                                if (rect.y < y_fixed + h_rect && rect.y + rect.h > y_fixed) {\n                                    x_fixed = max(x_fixed, rect.x + rect.w);\n                                }\n                            }\n                        }\n                        long long new_max_x = max(cur_max_x, x_fixed + w_rect);\n                        long long new_max_y = max(cur_max_y, y_fixed + h_rect);\n                        long long score_val = new_max_x + new_max_y;\n                        options.emplace_back(rotation, b, d, score_val, x_fixed, y_fixed, w_rect, h_rect);\n                    }\n                }\n\n                if (options.empty()) {\n                    valid_placement = false;\n                    break;\n                }\n\n                auto best_opt = *min_element(options.begin(), options.end(),\n                    [](const auto& a, const auto& b) {\n                        return get<3>(a) < get<3>(b);\n                    });\n\n                int r = get<0>(best_opt);\n                int b_val = get<1>(best_opt);\n                char d_val = get<2>(best_opt);\n                long long x_val = get<4>(best_opt);\n                long long y_val = get<5>(best_opt);\n                long long w_val = get<6>(best_opt);\n                long long h_val = get<7>(best_opt);\n\n                inst_seq.push_back({i, r, b_val, d_val});\n                layout.push_back({x_val, y_val, w_val, h_val});\n                cur_max_x = max(cur_max_x, x_val + w_val);\n                cur_max_y = max(cur_max_y, y_val + h_val);\n            }\n\n            if (!valid_placement) {\n                continue;\n            }\n\n            long long total_est = cur_max_x + cur_max_y;\n            if (total_est < best_score) {\n                best_score = total_est;\n                best_instructions = inst_seq;\n            }\n        }\n\n        if (best_instructions.empty()) {\n            for (int i = 0; i < N; i++) {\n                best_instructions.push_back({i, 0, -1, 'U'});\n            }\n        }\n\n        cout << N << \"\\n\";\n        for (const auto& inst : best_instructions) {\n            cout << inst.p << \" \" << inst.r << \" \" << inst.d << \" \" << inst.b << \"\\n\";\n        }\n        cout.flush();\n\n        long long W_obs, H_obs;\n        cin >> W_obs >> H_obs;\n\n        double total_est_val = static_cast<double>(best_score);\n        double total_obs_val = static_cast<double>(W_obs) + static_cast<double>(H_obs);\n        double scale = 1.0;\n        if (total_est_val > 1e-9) {\n            scale = total_obs_val / total_est_val;\n        }\n\n        for (int i = 0; i < N; i++) {\n            w_est[i] *= scale;\n            h_est[i] *= scale;\n            if (w_est[i] < 1.0) w_est[i] = 1.0;\n            if (w_est[i] > 1e9) w_est[i] = 1e9;\n            if (h_est[i] < 1.0) h_est[i] = 1.0;\n            if (h_est[i] > 1e9) h_est[i] = 1e9;\n        }\n    }\n\n    return 0;\n}","ahc041":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, H_val;\n    cin >> N >> M >> H_val;\n    const int max_layer = H_val + 1;\n\n    vector<long long> A(N);\n    for (int i = 0; i < N; i++) {\n        cin >> A[i];\n    }\n\n    vector<vector<int>> graph(N);\n    for (int i = 0; i < M; i++) {\n        int u, v;\n        cin >> u >> v;\n        graph[u].push_back(v);\n        graph[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> indices(N);\n    iota(indices.begin(), indices.end(), 0);\n    sort(indices.begin(), indices.end(), [&](int i, int j) {\n        return A[i] < A[j];\n    });\n\n    vector<int> layer(N, -1);\n    vector<int> parent(N, -1);\n\n    for (int v : indices) {\n        for (int d = max_layer; d >= 1; d--) {\n            if (d == 1) {\n                layer[v] = 1;\n                parent[v] = -1;\n                break;\n            }\n            for (int u : graph[v]) {\n                if (layer[u] == d - 1) {\n                    layer[v] = d;\n                    parent[v] = u;\n                    break;\n                }\n            }\n            if (layer[v] != -1) {\n                break;\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 <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int N;\n    cin >> N;\n    vector<string> grid(N);\n    for (int i = 0; i < N; i++) {\n        cin >> grid[i];\n    }\n\n    vector<pair<char, int>> ops;\n\n    while (true) {\n        int best_i = -1, best_j = -1;\n        char best_dir = ' ';\n        int best_p = -1;\n        int min_shifts = 100000000;\n\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                if (grid[i][j] != 'x') continue;\n\n                bool safe = true;\n                for (int k = 0; k < i; k++) {\n                    if (grid[k][j] == 'o') {\n                        safe = false;\n                        break;\n                    }\n                }\n                if (safe) {\n                    int shifts = i + 1;\n                    if (shifts < min_shifts) {\n                        min_shifts = shifts;\n                        best_dir = 'U';\n                        best_p = j;\n                        best_i = i;\n                        best_j = j;\n                    }\n                }\n\n                safe = true;\n                for (int k = i + 1; k < N; k++) {\n                    if (grid[k][j] == 'o') {\n                        safe = false;\n                        break;\n                    }\n                }\n                if (safe) {\n                    int shifts = N - i;\n                    if (shifts < min_shifts) {\n                        min_shifts = shifts;\n                        best_dir = 'D';\n                        best_p = j;\n                        best_i = i;\n                        best_j = j;\n                    }\n                }\n\n                safe = true;\n                for (int k = 0; k < j; k++) {\n                    if (grid[i][k] == 'o') {\n                        safe = false;\n                        break;\n                    }\n                }\n                if (safe) {\n                    int shifts = j + 1;\n                    if (shifts < min_shifts) {\n                        min_shifts = shifts;\n                        best_dir = 'L';\n                        best_p = i;\n                        best_i = i;\n                        best_j = j;\n                    }\n                }\n\n                safe = true;\n                for (int k = j + 1; k < N; k++) {\n                    if (grid[i][k] == 'o') {\n                        safe = false;\n                        break;\n                    }\n                }\n                if (safe) {\n                    int shifts = N - j;\n                    if (shifts < min_shifts) {\n                        min_shifts = shifts;\n                        best_dir = 'R';\n                        best_p = i;\n                        best_i = i;\n                        best_j = j;\n                    }\n                }\n            }\n        }\n\n        if (best_i == -1) {\n            break;\n        }\n\n        for (int s = 0; s < min_shifts; s++) {\n            ops.push_back({best_dir, best_p});\n            if (best_dir == 'U') {\n                for (int i = 0; i < N - 1; i++) {\n                    grid[i][best_p] = grid[i + 1][best_p];\n                }\n                grid[N - 1][best_p] = '.';\n            } else if (best_dir == 'D') {\n                for (int i = N - 1; i > 0; i--) {\n                    grid[i][best_p] = grid[i - 1][best_p];\n                }\n                grid[0][best_p] = '.';\n            } else if (best_dir == 'L') {\n                for (int j = 0; j < N - 1; j++) {\n                    grid[best_p][j] = grid[best_p][j + 1];\n                }\n                grid[best_p][N - 1] = '.';\n            } else if (best_dir == 'R') {\n                for (int j = N - 1; j > 0; j--) {\n                    grid[best_p][j] = grid[best_p][j - 1];\n                }\n                grid[best_p][0] = '.';\n            }\n        }\n    }\n\n    for (auto &op : ops) {\n        cout << op.first << \" \" << op.second << \"\\n\";\n    }\n\n    return 0;\n}","ahc044":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <random>\n#include <chrono>\nusing namespace std;\n\nlong fast_error_estimate(const vector<int>& a, const vector<int>& b, const vector<long>& T, int N) {\n    vector<long> expected(N, 0);\n    vector<long> in_degree(N, 0);\n    for (int i = 0; i < N; i++) {\n        expected[a[i]] += T[i];\n        expected[b[i]] += T[i];\n        in_degree[a[i]]++;\n        in_degree[b[i]]++;\n    }\n    long error = 0;\n    for (int i = 0; i < N; i++) {\n        error += abs(expected[i] / 2 - T[i]);\n    }\n    return error;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, L;\n    cin >> N >> L;\n    vector<long> T(N);\n    for (int i = 0; i < N; i++) {\n        cin >> T[i];\n    }\n\n    vector<pair<long, int>> sorted;\n    for (int i = 0; i < N; i++) {\n        sorted.emplace_back(T[i], i);\n    }\n    sort(sorted.begin(), sorted.end(), [](const auto& a, const auto& b) {\n        return a.first > b.first;\n    });\n\n    vector<int> a(N), b(N);\n    for (int i = 0; i < N; i++) {\n        a[sorted[i].second] = sorted[(i + 1) % N].second;\n        b[sorted[i].second] = sorted[(i + 2) % N].second;\n    }\n\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n    uniform_int_distribution<int> dist(0, N - 1);\n\n    long best_error = fast_error_estimate(a, b, T, N);\n    vector<int> best_a = a;\n    vector<int> best_b = b;\n\n    for (int iter = 0; iter < 1000; iter++) {\n        int idx = dist(rng);\n        int which = rng() % 2;\n        int old_val = (which == 0) ? a[idx] : b[idx];\n        int new_val = dist(rng);\n\n        if (which == 0) a[idx] = new_val;\n        else b[idx] = new_val;\n\n        long error = fast_error_estimate(a, b, T, N);\n        if (error < best_error) {\n            best_error = error;\n            best_a = a;\n            best_b = b;\n        } else {\n            if (which == 0) a[idx] = old_val;\n            else b[idx] = old_val;\n        }\n    }\n\n    for (int i = 0; i < N; i++) {\n        cout << best_a[i] << \" \" << best_b[i] << '\\n';\n    }\n\n    return 0;\n}","ahc045":"#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\n\nstruct DSU {\n    vector<int> parent;\n    DSU(int n) {\n        parent.resize(n);\n        iota(parent.begin(), parent.end(), 0);\n    }\n    int find(int x) {\n        if (parent[x] != x) \n            parent[x] = find(parent[x]);\n        return parent[x];\n    }\n    bool unite(int x, int y) {\n        x = find(x);\n        y = find(y);\n        if (x == y) return false;\n        parent[y] = x;\n        return true;\n    }\n};\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, Q_total, L, W;\n    cin >> N >> M >> Q_total >> L >> W;\n\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    for (int i = 0; i < N; i++) {\n        cin >> lx[i] >> rx[i] >> ly[i] >> ry[i];\n    }\n\n    vector<vector<ll>> min_sq(N, vector<ll>(N, 0));\n    for (int i = 0; i < N; i++) {\n        for (int j = i+1; j < N; j++) {\n            ll dx_min = max(0LL, (ll)max(lx[i], lx[j]) - (ll)min(rx[i], rx[j]));\n            ll dy_min = max(0LL, (ll)max(ly[i], ly[j]) - (ll)min(ry[i], ry[j]));\n            min_sq[i][j] = dx_min * dx_min + dy_min * dy_min;\n            min_sq[j][i] = min_sq[i][j];\n        }\n    }\n\n    vector<ll> min_edge(N, LLONG_MAX);\n    vector<int> parent(N, -1);\n    vector<bool> in_mst(N, false);\n    min_edge[0] = 0;\n\n    for (int i = 0; i < N; i++) {\n        int u = -1;\n        for (int j = 0; j < N; j++) {\n            if (!in_mst[j] && (u == -1 || min_edge[j] < min_edge[u])) {\n                u = j;\n            }\n        }\n        if (u == -1) break;\n        in_mst[u] = true;\n        for (int v = 0; v < N; v++) {\n            if (u == v) continue;\n            if (!in_mst[v] && min_sq[u][v] < min_edge[v]) {\n                min_edge[v] = min_sq[u][v];\n                parent[v] = u;\n            }\n        }\n    }\n\n    vector<vector<int>> adj(N);\n    for (int v = 1; v < N; v++) {\n        adj[parent[v]].push_back(v);\n        adj[v].push_back(parent[v]);\n    }\n\n    vector<int> tour;\n    vector<bool> visited(N, false);\n    stack<int> st;\n    st.push(0);\n    visited[0] = true;\n    while (!st.empty()) {\n        int u = st.top();\n        st.pop();\n        tour.push_back(u);\n        for (auto it = adj[u].rbegin(); it != adj[u].rend(); ++it) {\n            int v = *it;\n            if (!visited[v]) {\n                visited[v] = true;\n                st.push(v);\n            }\n        }\n    }\n\n    vector<vector<int>> groups(M);\n    int start_idx = 0;\n    for (int k = 0; k < M; k++) {\n        int g = G[k];\n        groups[k] = vector<int>(tour.begin() + start_idx, tour.begin() + start_idx + g);\n        start_idx += g;\n    }\n\n    vector<int> queries_per_group(M, 0);\n    vector<int> group_order(M);\n    iota(group_order.begin(), group_order.end(), 0);\n    sort(group_order.begin(), group_order.end(), [&](int i, int j) {\n        return G[i] > G[j];\n    });\n\n    int remaining_queries = Q_total;\n    for (int idx : group_order) {\n        int g = G[idx];\n        if (g <= 2) {\n            queries_per_group[idx] = 0;\n        } else if (g <= L) {\n            queries_per_group[idx] = 1;\n        } else {\n            queries_per_group[idx] = (g + L - 2) / (L - 1);\n        }\n        if (queries_per_group[idx] > remaining_queries) {\n            queries_per_group[idx] = remaining_queries;\n        }\n        remaining_queries -= queries_per_group[idx];\n        if (remaining_queries <= 0) {\n            break;\n        }\n    }\n\n    vector<vector<pair<int, int>>> candidate_edges(M);\n    remaining_queries = Q_total;\n\n    for (int k = 0; k < M; k++) {\n        int g = G[k];\n        if (g <= 2 || queries_per_group[k] == 0) {\n            continue;\n        }\n        int q_count = queries_per_group[k];\n        vector<vector<int>> windows;\n        if (g <= L) {\n            windows.push_back(groups[k]);\n        } else {\n            if (q_count == 1) {\n                windows.push_back(vector<int>(groups[k].begin(), groups[k].begin() + L));\n            } else {\n                int total_gap = g - L;\n                for (int i = 0; i < q_count; i++) {\n                    int start_index = (i * total_gap) / (q_count - 1);\n                    int end_index = start_index + L - 1;\n                    if (end_index >= g) {\n                        end_index = g - 1;\n                        start_index = g - L;\n                    }\n                    vector<int> window;\n                    for (int j = start_index; j <= end_index; j++) {\n                        window.push_back(groups[k][j]);\n                    }\n                    windows.push_back(window);\n                }\n            }\n        }\n\n        for (auto &window : windows) {\n            if (remaining_queries <= 0) break;\n            cout << \"? \" << window.size();\n            for (int city : window) {\n                cout << \" \" << city;\n            }\n            cout << endl;\n            cout.flush();\n            remaining_queries--;\n\n            for (int i = 0; i < (int)window.size() - 1; i++) {\n                int u, v;\n                cin >> u >> v;\n                candidate_edges[k].push_back({u, v});\n            }\n        }\n    }\n\n    vector<vector<pair<int, int>>> final_edges(M);\n    for (int k = 0; k < M; k++) {\n        int g = G[k];\n        if (g <= 1) {\n            final_edges[k] = vector<pair<int, int>>();\n            continue;\n        }\n        if (g == 2) {\n            final_edges[k].push_back({groups[k][0], groups[k][1]});\n            continue;\n        }\n\n        vector<int> group_cities = groups[k];\n        int num_cities = g;\n        vector<int> global_to_local(N, -1);\n        for (int i = 0; i < num_cities; i++) {\n            global_to_local[group_cities[i]] = i;\n        }\n\n        vector<tuple<ll, int, int>> all_edges;\n        for (int i = 0; i < num_cities; i++) {\n            for (int j = i+1; j < num_cities; j++) {\n                int u_global = group_cities[i];\n                int v_global = group_cities[j];\n                all_edges.push_back({min_sq[u_global][v_global], u_global, v_global});\n            }\n        }\n        sort(all_edges.begin(), all_edges.end());\n\n        vector<tuple<ll, int, int>> candidate_list;\n        for (auto &e : candidate_edges[k]) {\n            int u = e.first, v = e.second;\n            if (global_to_local[u] == -1 || global_to_local[v] == -1) continue;\n            candidate_list.push_back({min_sq[u][v], u, v});\n        }\n        sort(candidate_list.begin(), candidate_list.end());\n\n        DSU dsu_local(num_cities);\n        vector<pair<int, int>> edges_used;\n\n        auto add_edge = [&](int u_global, int v_global) {\n            int lu = global_to_local[u_global];\n            int lv = global_to_local[v_global];\n            if (dsu_local.unite(lu, lv)) {\n                edges_used.push_back({u_global, v_global});\n            }\n        };\n\n        for (auto &t : candidate_list) {\n            int u = get<1>(t);\n            int v = get<2>(t);\n            add_edge(u, v);\n            if ((int)edges_used.size() == num_cities - 1) break;\n        }\n\n        if ((int)edges_used.size() < num_cities - 1) {\n            for (auto &t : all_edges) {\n                int u = get<1>(t);\n                int v = get<2>(t);\n                add_edge(u, v);\n                if ((int)edges_used.size() == num_cities - 1) break;\n            }\n        }\n\n        final_edges[k] = edges_used;\n    }\n\n    cout << \"!\" << endl;\n    for (int k = 0; k < M; k++) {\n        for (int i = 0; i < G[k]; i++) {\n            if (i > 0) cout << \" \";\n            cout << groups[k][i];\n        }\n        cout << endl;\n\n        for (auto &e : final_edges[k]) {\n            cout << e.first << \" \" << e.second << endl;\n        }\n    }\n    cout.flush();\n\n    return 0;\n}","ahc046":"#include <iostream>\n#include <vector>\n#include <cstdlib>\nusing namespace std;\n\nint main() {\n    int N, M;\n    cin >> N >> M;\n    vector<pair<int, int>> points(M);\n    for (int i = 0; i < M; i++) {\n        cin >> points[i].first >> points[i].second;\n    }\n\n    for (int idx = 0; idx < M-1; idx++) {\n        int ci = points[idx].first;\n        int cj = points[idx].second;\n        int ti = points[idx+1].first;\n        int tj = points[idx+1].second;\n\n        int dr = ti - ci;\n        int dc = tj - cj;\n\n        if (ci == ti) {\n            if (cj < tj) {\n                int d_direct = tj - cj;\n                int d_alt = 1 + (N - 1 - tj);\n                if (d_alt < d_direct) {\n                    cout << \"S R\\n\";\n                    for (int k = 0; k < N-1-tj; k++) {\n                        cout << \"M L\\n\";\n                    }\n                } else {\n                    for (int k = 0; k < d_direct; k++) {\n                        cout << \"M R\\n\";\n                    }\n                }\n            } else if (cj > tj) {\n                int d_direct = cj - tj;\n                int d_alt = 1 + tj;\n                if (d_alt < d_direct) {\n                    cout << \"S L\\n\";\n                    for (int k = 0; k < tj; k++) {\n                        cout << \"M R\\n\";\n                    }\n                } else {\n                    for (int k = 0; k < d_direct; k++) {\n                        cout << \"M L\\n\";\n                    }\n                }\n            }\n        }\n        else if (cj == tj) {\n            if (ci < ti) {\n                int d_direct = ti - ci;\n                int d_alt = 1 + (N - 1 - ti);\n                if (d_alt < d_direct) {\n                    cout << \"S D\\n\";\n                    for (int k = 0; k < N-1-ti; k++) {\n                        cout << \"M U\\n\";\n                    }\n                } else {\n                    for (int k = 0; k < d_direct; k++) {\n                        cout << \"M D\\n\";\n                    }\n                }\n            } else if (ci > ti) {\n                int d_direct = ci - ti;\n                int d_alt = 1 + ti;\n                if (d_alt < d_direct) {\n                    cout << \"S U\\n\";\n                    for (int k = 0; k < ti; k++) {\n                        cout << \"M D\\n\";\n                    }\n                } else {\n                    for (int k = 0; k < d_direct; k++) {\n                        cout << \"M U\\n\";\n                    }\n                }\n            }\n        }\n        else {\n            if (abs(dr) >= abs(dc)) {\n                if (dr > 0) {\n                    int d_direct = dr;\n                    int d_alt = 1 + (N - 1 - ti);\n                    if (d_alt < d_direct) {\n                        cout << \"S D\\n\";\n                        for (int k = 0; k < N-1-ti; k++) {\n                            cout << \"M U\\n\";\n                        }\n                    } else {\n                        for (int k = 0; k < d_direct; k++) {\n                            cout << \"M D\\n\";\n                        }\n                    }\n                } else {\n                    int d_direct = -dr;\n                    int d_alt = 1 + ti;\n                    if (d_alt < d_direct) {\n                        cout << \"S U\\n\";\n                        for (int k = 0; k < ti; k++) {\n                            cout << \"M D\\n\";\n                        }\n                    } else {\n                        for (int k = 0; k < d_direct; k++) {\n                            cout << \"M U\\n\";\n                        }\n                    }\n                }\n                if (cj < tj) {\n                    int d_direct = tj - cj;\n                    int d_alt = 1 + (N - 1 - tj);\n                    if (d_alt < d_direct) {\n                        cout << \"S R\\n\";\n                        for (int k = 0; k < N-1-tj; k++) {\n                            cout << \"M L\\n\";\n                        }\n                    } else {\n                        for (int k = 0; k < d_direct; k++) {\n                            cout << \"M R\\n\";\n                        }\n                    }\n                } else {\n                    int d_direct = cj - tj;\n                    int d_alt = 1 + tj;\n                    if (d_alt < d_direct) {\n                        cout << \"S L\\n\";\n                        for (int k = 0; k < tj; k++) {\n                            cout << \"M R\\n\";\n                        }\n                    } else {\n                        for (int k = 0; k < d_direct; k++) {\n                            cout << \"M L\\n\";\n                        }\n                    }\n                }\n            } else {\n                if (dc > 0) {\n                    int d_direct = dc;\n                    int d_alt = 1 + (N - 1 - tj);\n                    if (d_alt < d_direct) {\n                        cout << \"S R\\n\";\n                        for (int k = 0; k < N-1-tj; k++) {\n                            cout << \"M L\\n\";\n                        }\n                    } else {\n                        for (int k = 0; k < d_direct; k++) {\n                            cout << \"M R\\n\";\n                        }\n                    }\n                } else {\n                    int d_direct = -dc;\n                    int d_alt = 1 + tj;\n                    if (d_alt < d_direct) {\n                        cout << \"S L\\n\";\n                        for (int k = 0; k < tj; k++) {\n                            cout << \"M R\\n\";\n                        }\n                    } else {\n                        for (int k = 0; k < d_direct; k++) {\n                            cout << \"M L\\n\";\n                        }\n                    }\n                }\n                if (ci < ti) {\n                    int d_direct = ti - ci;\n                    int d_alt = 1 + (N - 1 - ti);\n                    if (d_alt < d_direct) {\n                        cout << \"S D\\n\";\n                        for (int k = 0; k < N-1-ti; k++) {\n                            cout << \"M U\\n\";\n                        }\n                    } else {\n                        for (int k = 0; k < d_direct; k++) {\n                            cout << \"M D\\n\";\n                        }\n                    }\n                } else {\n                    int d_direct = ci - ti;\n                    int d_alt = 1 + ti;\n                    if (d_alt < d_direct) {\n                        cout << \"S U\\n\";\n                        for (int k = 0; k < ti; k++) {\n                            cout << \"M D\\n\";\n                        }\n                    } else {\n                        for (int k = 0; k < d_direct; k++) {\n                            cout << \"M U\\n\";\n                        }\n                    }\n                }\n            }\n        }\n    }\n\n    return 0;\n}"}}}