{"model_name":"grok-code-fast-1","codes":{"1":{"ahc001":"#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n\ndouble calc_p(ll r, ll s) {\n    if (s == 0 || r == 0) return 0.0;\n    double mn = min(r, s);\n    double mx = max(r, s);\n    double ratio = mn / mx;\n    return 1.0 - (1.0 - ratio) * (1.0 - ratio);\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int n;\n    cin >> n;\n    vector<ll> x(n), y(n), r(n);\n    for (int i = 0; i < n; ++i) {\n        cin >> x[i] >> y[i] >> r[i];\n    }\n    \n    vector<ll> a(n), b(n), c(n), d(n), s(n);\n    for (int i = 0; i < n; ++i) {\n        a[i] = x[i];\n        c[i] = x[i] + 1;\n        b[i] = y[i];\n        d[i] = y[i] + 1;\n        s[i] = 1;\n    }\n    \n    bool changed = true;\n    while (changed) {\n        changed = false;\n        double max_gain = -1e9;\n        int best_i = -1;\n        int best_dir = -1;  // 0: width, 1: height\n        ll best_delta = 0;\n        \n        for (int i = 0; i < n; ++i) {\n            if (s[i] >= r[i]) continue;\n            \n            ll w = c[i] - a[i];\n            ll h = d[i] - b[i];\n            \n            // Compute max dw (expand right)\n            ll dw = 10000LL - c[i];\n            for (int j = 0; j < n; ++j) {\n                if (i == j) continue;\n                if (max(b[i], b[j]) < min(d[i], d[j])) {  // y overlap\n                    ll limit = c[j] - c[i] - 1;\n                    if (limit < dw) dw = limit;\n                }\n            }\n            if (dw < 0) dw = 0;\n            \n            // Compute max dh (expand up)\n            ll dh = 10000LL - d[i];\n            for (int j = 0; j < n; ++j) {\n                if (i == j) continue;\n                if (max(a[i], a[j]) < min(c[i], c[j])) {  // x overlap\n                    ll limit = d[j] - d[i] - 1;\n                    if (limit < dh) dh = limit;\n                }\n            }\n            if (dh < 0) dh = 0;\n            \n            // Check gain for dw\n            if (dw > 0) {\n                ll new_s = s[i] + dw * h;\n                double old_p = calc_p(r[i], s[i]);\n                double new_p = calc_p(r[i], new_s);\n                double gain = new_p - old_p;\n                if (gain > max_gain) {\n                    max_gain = gain;\n                    best_i = i;\n                    best_dir = 0;\n                    best_delta = dw;\n                }\n            }\n            \n            // Check gain for dh\n            if (dh > 0) {\n                ll new_s = s[i] + dh * w;\n                double old_p = calc_p(r[i], s[i]);\n                double new_p = calc_p(r[i], new_s);\n                double gain = new_p - old_p;\n                if (gain > max_gain) {\n                    max_gain = gain;\n                    best_i = i;\n                    best_dir = 1;\n                    best_delta = dh;\n                }\n            }\n        }\n        \n        if (best_i != -1) {\n            changed = true;\n            if (best_dir == 0) {  // Expand width\n                c[best_i] += best_delta;\n                s[best_i] += best_delta * (d[best_i] - b[best_i]);\n            } else {  // Expand height\n                d[best_i] += best_delta;\n                s[best_i] += best_delta * (c[best_i] - a[best_i]);\n            }\n        }\n    }\n    \n    // Output\n    for (int i = 0; i < n; ++i) {\n        cout << a[i] << \" \" << b[i] << \" \" << c[i] << \" \" << d[i] << \"\\n\";\n    }\n    \n    return 0;\n}","ahc002":"#include <bits/stdc++.h>\n\nusing namespace std;\n\nint main() {\n    int si, sj;\n    cin >> si >> sj;\n    vector<vector<int>> t(50, vector<int>(50));\n    for (int i = 0; i < 50; i++) for (int j = 0; j < 50; j++) cin >> t[i][j];\n    vector<vector<int>> p(50, vector<int>(50));\n    for (int i = 0; i < 50; i++) for (int j = 0; j < 50; j++) cin >> p[i][j];\n    \n    int M = 0;\n    for (int i = 0; i < 50; i++) for (int j = 0; j < 50; j++) M = max(M, t[i][j] + 1);\n    \n    vector<vector<int>> tile_squares(M);\n    vector<int> max_p(M, 0);\n    for (int i = 0; i < 50; i++) for (int j = 0; j < 50; j++) {\n        tile_squares[t[i][j]].push_back(i * 50 + j);\n        max_p[t[i][j]] = max(max_p[t[i][j]], p[i][j]);\n    }\n    \n    vector<set<int>> adj_tile(M);\n    int di[4] = {-1, 1, 0, 0};\n    int dj[4] = {0, 0, -1, 1};\n    for (int i = 0; i < 50; i++) for (int j = 0; j < 50; j++) {\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 < 50 && nj >= 0 && nj < 50 && t[i][j] != t[ni][nj]) {\n                adj_tile[t[i][j]].insert(t[ni][nj]);\n            }\n        }\n    }\n    \n    // Greedy path on tiles\n    set<int> visited;\n    vector<int> path_tiles;\n    int current = t[si][sj];\n    visited.insert(current);\n    path_tiles.push_back(current);\n    while (true) {\n        int best = -1;\n        int best_score = -1;\n        for (int nei : adj_tile[current]) {\n            if (visited.count(nei)) continue;\n            if (max_p[nei] > best_score) {\n                best_score = max_p[nei];\n                best = nei;\n            }\n        }\n        if (best == -1) break;\n        visited.insert(best);\n        path_tiles.push_back(best);\n        current = best;\n    }\n    \n    // Build path on squares\n    vector<pair<int, int>> path_squares;\n    path_squares.push_back({si, sj});\n    for (size_t k = 1; k < path_tiles.size(); k++) {\n        int curr_tile = path_tiles[k];\n        auto [ci, cj] = path_squares.back();\n        bool found = false;\n        for (int d = 0; d < 4; d++) {\n            int ni = ci + di[d];\n            int nj = cj + dj[d];\n            if (ni >= 0 && ni < 50 && nj >= 0 && nj < 50 && t[ni][nj] == curr_tile) {\n                path_squares.push_back({ni, nj});\n                found = true;\n                break;\n            }\n        }\n        if (!found) {\n            // Fallback: find any adjacent square in curr_tile from prev_tile\n            int prev_tile = path_tiles[k - 1];\n            for (int sq : tile_squares[prev_tile]) {\n                int pi = sq / 50;\n                int pj = sq % 50;\n                for (int d = 0; d < 4; d++) {\n                    int ni = pi + di[d];\n                    int nj = pj + dj[d];\n                    if (ni >= 0 && ni < 50 && nj >= 0 && nj < 50 && t[ni][nj] == curr_tile) {\n                        path_squares.push_back({ni, nj});\n                        found = true;\n                        goto next_k;\n                    }\n                }\n            }\n            next_k:;\n            if (!found) {\n                // Should not happen if tile graph is correct\n                assert(false);\n            }\n        }\n    }\n    \n    // Build move string\n    string moves = \"\";\n    for (size_t i = 1; i < path_squares.size(); i++) {\n        auto [pi, pj] = path_squares[i - 1];\n        auto [ci, cj] = path_squares[i];\n        if (ci == pi - 1) moves += 'U';\n        else if (ci == pi + 1) moves += 'D';\n        else if (cj == pj - 1) moves += 'L';\n        else if (cj == pj + 1) moves += 'R';\n        else assert(false);\n    }\n    cout << moves << endl;\n    return 0;\n}","ahc003":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    const int N = 30;\n    vector<vector<double>> h(N, vector<double>(N-1, 5000.0));  // h[i][j]: cost from (i,j) to (i,j+1)\n    vector<vector<double>> v(N-1, vector<double>(N, 5000.0));  // v[i][j]: cost from (i,j) to (i+1,j)\n\n    for (int query = 0; query < 1000; ++query) {\n        int si, sj, ti, tj;\n        cin >> si >> sj >> ti >> tj;\n\n        // Dijkstra to find shortest path with current estimated costs\n        vector<vector<double>> dist(N, vector<double>(N, 1e18));\n        vector<vector<pair<int, int>>> prev(N, vector<pair<int, int>>(N, {-1, -1}));\n        priority_queue<tuple<double, int, int>, vector<tuple<double, int, int>>, greater<tuple<double, int, int>>> pq;\n\n        dist[si][sj] = 0.0;\n        pq.emplace(0.0, si, sj);\n\n        while (!pq.empty()) {\n            auto [cost, i, j] = pq.top();\n            pq.pop();\n            if (cost > dist[i][j]) continue;\n\n            // Neighbors: up, down, left, right\n            vector<tuple<int, int, double, char>> neighbors;\n            if (i > 0) neighbors.emplace_back(i-1, j, v[i-1][j], 'U');  // up: from (i,j) to (i-1,j), cost v[i-1][j]\n            if (i < N-1) neighbors.emplace_back(i+1, j, v[i][j], 'D');  // down: from (i,j) to (i+1,j), cost v[i][j]\n            if (j > 0) neighbors.emplace_back(i, j-1, h[i][j-1], 'L');  // left: from (i,j) to (i,j-1), cost h[i][j-1]\n            if (j < N-1) neighbors.emplace_back(i, j+1, h[i][j], 'R');  // right: from (i,j) to (i,j+1), cost h[i][j]\n\n            for (auto [ni, nj, edge_cost, move] : neighbors) {\n                double new_cost = cost + edge_cost;\n                if (new_cost < dist[ni][nj]) {\n                    dist[ni][nj] = new_cost;\n                    prev[ni][nj] = {i, j};\n                    pq.emplace(new_cost, ni, nj);\n                }\n            }\n        }\n\n        // Reconstruct path from t to s\n        string path = \"\";\n        int ci = ti, cj = tj;\n        vector<pair<int, int>> path_nodes;\n        while (ci != si || cj != sj) {\n            path_nodes.emplace_back(ci, cj);\n            auto [pi, pj] = prev[ci][cj];\n            if (pi == -1) {  // Should not happen\n                assert(false);\n            }\n            // Determine move\n            if (pi == ci - 1) path += 'U';  // from (ci,cj) to (pi,pj) = (ci-1,cj), so 'U'\n            else if (pi == ci + 1) path += 'D';  // from (ci,cj) to (pi,pj) = (ci+1,cj), so 'D'\n            else if (pj == cj - 1) path += 'L';  // from (ci,cj) to (pi,pj) = (ci,cj-1), so 'L'\n            else if (pj == cj + 1) path += 'R';  // from (ci,cj) to (pi,pj) = (ci,cj+1), so 'R'\n            ci = pi;\n            cj = pj;\n        }\n        reverse(path.begin(), path.end());  // Reverse to get from s to t\n\n        // Output path and flush\n        cout << path << endl;\n        cout.flush();\n\n        // Read feedback F\n        int F;\n        cin >> F;\n\n        // Compute estimated path cost C\n        double C = 0.0;\n        ci = si, cj = sj;\n        for (char move : path) {\n            if (move == 'U') {\n                C += v[ci-1][cj];\n                ci--;\n            } else if (move == 'D') {\n                C += v[ci][cj];\n                ci++;\n            } else if (move == 'L') {\n                C += h[ci][cj-1];\n                cj--;\n            } else if (move == 'R') {\n                C += h[ci][cj];\n                cj++;\n            }\n        }\n\n        // Update estimated costs if C > 0\n        if (C > 1e-9) {\n            double factor = static_cast<double>(F) / C;\n            ci = si, cj = sj;\n            for (char move : path) {\n                if (move == 'U') {\n                    v[ci-1][cj] *= factor;\n                    ci--;\n                } else if (move == 'D') {\n                    v[ci][cj] *= factor;\n                    ci++;\n                } else if (move == 'L') {\n                    h[ci][cj-1] *= factor;\n                    cj--;\n                } else if (move == 'R') {\n                    h[ci][cj] *= factor;\n                    cj++;\n                }\n            }\n        }\n    }\n\n    return 0;\n}","ahc004":"#include <bits/stdc++.h>\n\nusing namespace std;\n\nint main() {\n    int N, M;\n    cin >> N >> M;\n    vector<string> strings(M);\n    for (int i = 0; i < M; i++) {\n        cin >> strings[i];\n    }\n\n    vector<vector<char>> grid(N, vector<char>(N, '.'));\n\n    for (const auto& s : strings) {\n        int k = s.size();\n        int best_fill = -1;\n        int best_i = -1, best_j = -1;\n        bool best_horiz = true;\n\n        // Check horizontal placements\n        for (int i = 0; i < N; i++) {  // Row\n            for (int j = 0; j < N; j++) {  // Starting column\n                bool valid = true;\n                int fill = 0;\n                for (int p = 0; p < k; p++) {\n                    int cj = (j + p) % N;\n                    char cell = grid[i][cj];\n                    if (cell != '.' && cell != s[p]) {\n                        valid = false;\n                        break;\n                    }\n                    if (cell == '.') fill++;\n                }\n                if (valid && fill > best_fill) {\n                    best_fill = fill;\n                    best_i = i;\n                    best_j = j;\n                    best_horiz = true;\n                }\n            }\n        }\n\n        // Check vertical placements\n        for (int j = 0; j < N; j++) {  // Column\n            for (int i = 0; i < N; i++) {  // Starting row\n                bool valid = true;\n                int fill = 0;\n                for (int p = 0; p < k; p++) {\n                    int ci = (i + p) % N;\n                    char cell = grid[ci][j];\n                    if (cell != '.' && cell != s[p]) {\n                        valid = false;\n                        break;\n                    }\n                    if (cell == '.') fill++;\n                }\n                if (valid && fill > best_fill) {\n                    best_fill = fill;\n                    best_i = i;\n                    best_j = j;\n                    best_horiz = false;\n                }\n            }\n        }\n\n        // Place the string if a valid position was found\n        if (best_fill >= 0) {\n            if (best_horiz) {\n                for (int p = 0; p < k; p++) {\n                    int cj = (best_j + p) % N;\n                    if (grid[best_i][cj] == '.') {\n                        grid[best_i][cj] = s[p];\n                    }\n                }\n            } else {\n                for (int p = 0; p < k; p++) {\n                    int ci = (best_i + p) % N;\n                    if (grid[ci][best_j] == '.') {\n                        grid[ci][best_j] = s[p];\n                    }\n                }\n            }\n        }\n    }\n\n    // Output the grid\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cout << grid[i][j];\n        }\n        cout << endl;\n    }\n\n    return 0;\n}","ahc005":"#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nconst ll INF = 1LL << 60;\n\nint main() {\n    int N, si, sj;\n    cin >> N >> si >> sj;\n    vector<string> c(N);\n    for(int i = 0; i < N; i++) cin >> c[i];\n    vector<vector<int>> cost(N, vector<int>(N, 0));\n    vector<pair<int, int>> roads;\n    for(int i = 0; i < N; i++) {\n        for(int j = 0; j < N; j++) {\n            if(c[i][j] != '#') {\n                cost[i][j] = c[i][j] - '0';\n                roads.emplace_back(i, j);\n            }\n        }\n    }\n    int R = roads.size();\n    \n    // Identify active rows and columns\n    vector<bool> active_row(N, false), active_col(N, false);\n    for(auto [i, j] : roads) {\n        active_row[i] = true;\n        active_col[j] = true;\n    }\n    \n    // Greedy selection for S\n    vector<bool> covered_row(N, false), covered_col(N, false);\n    vector<pair<int, int>> S;\n    set<pair<int, int>> road_set(roads.begin(), roads.end());\n    while(true) {\n        bool all_covered = true;\n        for(int i = 0; i < N; i++) {\n            if(active_row[i] && !covered_row[i]) all_covered = false;\n            if(active_col[i] && !covered_col[i]) all_covered = false;\n        }\n        if(all_covered) break;\n        \n        pair<int, int> best = {-1, -1};\n        int max_gain = -1;\n        int min_cost = INT_MAX;\n        for(auto p : roads) {\n            int i = p.first, j = p.second;\n            int gain = (active_row[i] && !covered_row[i]) + (active_col[j] && !covered_col[j]);\n            if(gain > max_gain || (gain == max_gain && cost[i][j] < min_cost)) {\n                max_gain = gain;\n                min_cost = cost[i][j];\n                best = p;\n            }\n        }\n        if(best.first == -1) break; // Should not happen\n        S.push_back(best);\n        covered_row[best.first] = true;\n        covered_col[best.second] = true;\n    }\n    // Ensure start is in S\n    bool has_start = false;\n    for(auto p : S) if(p == make_pair(si, sj)) has_start = true;\n    if(!has_start) S.push_back({si, sj});\n    \n    // Nearest neighbor to order S\n    vector<pair<int, int>> path;\n    set<pair<int, int>> visited;\n    path.push_back({si, sj});\n    visited.insert({si, sj});\n    while(path.size() < S.size()) {\n        auto curr = path.back();\n        pair<int, int> next = {-1, -1};\n        int min_dist = INT_MAX;\n        for(auto p : S) {\n            if(visited.count(p)) continue;\n            int d = abs(p.first - curr.first) + abs(p.second - curr.second);\n            if(d < min_dist) {\n                min_dist = d;\n                next = p;\n            }\n        }\n        if(next.first == -1) break;\n        path.push_back(next);\n        visited.insert(next);\n    }\n    path.push_back({si, sj});\n    \n    // Function to get path string from a to b\n    auto get_path = [&](pair<int, int> start, pair<int, int> end) -> string {\n        if(start == end) return \"\";\n        vector<vector<ll>> dist(N, vector<ll>(N, INF));\n        vector<vector<pair<int, int>>> prev(N, vector<pair<int, int>>(N, {-1, -1}));\n        priority_queue<tuple<ll, int, int>, vector<tuple<ll, int, int>>, greater<>> pq;\n        dist[start.first][start.second] = 0;\n        pq.push({0, start.first, start.second});\n        while(!pq.empty()) {\n            auto [d, i, j] = pq.top(); pq.pop();\n            if(d > dist[i][j]) continue;\n            for(auto [di, dj] : vector<pair<int, int>>{{0,1}, {0,-1}, {1,0}, {-1,0}}) {\n                int ni = i + di, nj = j + dj;\n                if(ni >= 0 && ni < N && nj >= 0 && nj < N && cost[ni][nj] > 0) {\n                    ll nd = d + cost[ni][nj];\n                    if(nd < dist[ni][nj]) {\n                        dist[ni][nj] = nd;\n                        prev[ni][nj] = {i, j};\n                        pq.push({nd, ni, nj});\n                    }\n                }\n            }\n        }\n        // Reconstruct path\n        vector<pair<int, int>> route;\n        pair<int, int> curr = end;\n        while(curr != start) {\n            route.push_back(curr);\n            curr = prev[curr.first][curr.second];\n        }\n        reverse(route.begin(), route.end());\n        // Build string\n        string s;\n        for(size_t k = 1; k < route.size(); k++) {\n            auto [i1, j1] = route[k-1];\n            auto [i2, j2] = route[k];\n            if(i2 == i1 + 1) s += 'D';\n            else if(i2 == i1 - 1) s += 'U';\n            else if(j2 == j1 + 1) s += 'R';\n            else if(j2 == j1 - 1) s += 'L';\n        }\n        return s;\n    };\n    \n    // Build full route\n    string ans;\n    for(size_t k = 1; k < path.size(); k++) {\n        ans += get_path(path[k-1], path[k]);\n    }\n    cout << ans << endl;\n    return 0;\n}","future-contest-2022-qual":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int N, M, K, R;\n    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 j = 0; j < K; ++j) {\n            cin >> d[i][j];\n        }\n    }\n    \n    vector<vector<int>> deps(N);\n    for (int i = 0; i < R; ++i) {\n        int u, v;\n        cin >> u >> v;\n        --u; --v; // 0-based\n        deps[v].push_back(u);\n    }\n    \n    vector<int> task_status(N, -1); // -1: not started, 0: in progress, 1: completed\n    vector<int> member_status(M, -1); // -1: free, else task index\n    vector<int> start_day(N, -1);\n    vector<vector<int>> est_s(M, vector<int>(K, 0)); // estimated skills\n    \n    int current_day = 0;\n    while (true) {\n        ++current_day;\n        \n        // Collect free members and ready tasks\n        vector<int> free_members;\n        for (int j = 0; j < M; ++j) {\n            if (member_status[j] == -1) free_members.push_back(j);\n        }\n        \n        vector<int> ready_tasks;\n        for (int i = 0; i < N; ++i) {\n            if (task_status[i] == -1) {\n                bool can_start = true;\n                for (int u : deps[i]) {\n                    if (task_status[u] != 1) {\n                        can_start = false;\n                        break;\n                    }\n                }\n                if (can_start) ready_tasks.push_back(i);\n            }\n        }\n        \n        // Compute est_w for each ready task and free member\n        vector<vector<long long>> est_w(ready_tasks.size(), vector<long long>(free_members.size()));\n        for (int ti = 0; ti < ready_tasks.size(); ++ti) {\n            int i = ready_tasks[ti];\n            for (int mj = 0; mj < free_members.size(); ++mj) {\n                int j = free_members[mj];\n                long long w = 0;\n                for (int k = 0; k < K; ++k) {\n                    w += max(0, d[i][k] - est_s[j][k]);\n                }\n                est_w[ti][mj] = w;\n            }\n        }\n        \n        // Greedy assignment: sort tasks by min est_w, assign to best member\n        vector<pair<long long, int>> task_order; // (min_est_w, task_index_in_ready)\n        for (int ti = 0; ti < ready_tasks.size(); ++ti) {\n            long long min_w = LLONG_MAX;\n            for (int mj = 0; mj < free_members.size(); ++mj) {\n                min_w = min(min_w, est_w[ti][mj]);\n            }\n            task_order.emplace_back(min_w, ti);\n        }\n        sort(task_order.begin(), task_order.end());\n        \n        vector<pair<int, int>> assignments; // (member, task) 0-based\n        set<int> used_members;\n        for (auto& p : task_order) {\n            int ti = p.second;\n            int i = ready_tasks[ti];\n            long long best_w = LLONG_MAX;\n            int best_j = -1;\n            for (int mj = 0; mj < free_members.size(); ++mj) {\n                int j = free_members[mj];\n                if (used_members.count(j)) continue;\n                if (est_w[ti][mj] < best_w) {\n                    best_w = est_w[ti][mj];\n                    best_j = j;\n                }\n            }\n            if (best_j != -1) {\n                assignments.emplace_back(best_j, i);\n                used_members.insert(best_j);\n            }\n        }\n        \n        // Output assignments\n        cout << assignments.size();\n        for (auto& p : assignments) {\n            cout << \" \" << (p.first + 1) << \" \" << (p.second + 1);\n            // Update states\n            task_status[p.second] = 0;\n            member_status[p.first] = p.second;\n            start_day[p.second] = current_day;\n        }\n        cout << \"\\n\";\n        cout.flush();\n        \n        // Read input\n        int n;\n        cin >> n;\n        if (n == -1) break;\n        \n        vector<int> completers(n);\n        for (int i = 0; i < n; ++i) {\n            cin >> completers[i];\n            --completers[i]; // 0-based\n        }\n        \n        // Process completions\n        for (int f : completers) {\n            int task = member_status[f];\n            if (task == -1) continue; // Should not happen\n            int t = current_day - start_day[task] + 1;\n            // Update est_s\n            if (t == 1) {\n                for (int k = 0; k < K; ++k) {\n                    est_s[f][k] = max(est_s[f][k], d[task][k]);\n                }\n            } else {\n                int count = 0;\n                for (int k = 0; k < K; ++k) {\n                    if (d[task][k] > est_s[f][k]) ++count;\n                }\n                if (count > 0) {\n                    int add = t / count;\n                    for (int k = 0; k < K; ++k) {\n                        if (d[task][k] > est_s[f][k]) est_s[f][k] += add;\n                    }\n                }\n            }\n            // Update states\n            task_status[task] = 1;\n            member_status[f] = -1;\n        }\n        \n        // Check if all tasks completed\n        bool all_done = true;\n        for (int i = 0; i < N; ++i) {\n            if (task_status[i] != 1) all_done = false;\n        }\n        if (all_done) break;\n    }\n    \n    return 0;\n}","ahc006":"#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing pii = pair<int, int>;\n\nstruct Order {\n    int a, b, c, d, idx;\n    ll cost;\n};\n\nll dist(int x1, int y1, int x2, int y2) {\n    return abs(x1 - x2) + abs(y1 - y2);\n}\n\nvector<pii> simulated_annealing(const vector<pii>& points, int office_x, int office_y, mt19937& rng) {\n    int n = points.size();\n    vector<int> order(n);\n    for (int i = 0; i < n; ++i) order[i] = i;\n    \n    // Compute initial distance\n    auto calc_dist = [&](const vector<int>& ord) -> ll {\n        ll total = 0;\n        total += dist(office_x, office_y, points[ord[0]].first, points[ord[0]].second);\n        for (int i = 0; i < n - 1; ++i) {\n            total += dist(points[ord[i]].first, points[ord[i]].second,\n                          points[ord[i+1]].first, points[ord[i+1]].second);\n        }\n        total += dist(points[ord.back()].first, points[ord.back()].second, office_x, office_y);\n        return total;\n    };\n    \n    ll current_dist = calc_dist(order);\n    vector<int> best_order = order;\n    ll best_dist = current_dist;\n    \n    double T = 1000.0;\n    double alpha = 0.995;\n    int max_iter = 100000;  // Adjust based on time\n    \n    uniform_int_distribution<int> dist_idx(0, n - 1);\n    uniform_real_distribution<double> dist_prob(0.0, 1.0);\n    \n    for (int iter = 0; iter < max_iter; ++iter) {\n        // Perturb: swap two random indices\n        int i = dist_idx(rng), j = dist_idx(rng);\n        swap(order[i], order[j]);\n        \n        ll new_dist = calc_dist(order);\n        ll delta = new_dist - current_dist;\n        \n        if (delta < 0 || dist_prob(rng) < exp(-delta / T)) {\n            current_dist = new_dist;\n            if (new_dist < best_dist) {\n                best_dist = new_dist;\n                best_order = order;\n            }\n        } else {\n            swap(order[i], order[j]);  // Revert\n        }\n        \n        T *= alpha;\n    }\n    \n    // Build route\n    vector<pii> route;\n    route.emplace_back(office_x, office_y);\n    for (int idx : best_order) {\n        route.push_back(points[idx]);\n    }\n    route.emplace_back(office_x, office_y);\n    return route;\n}\n\nint main() {\n    const int OFFICE_X = 400, OFFICE_Y = 400;\n    vector<Order> orders(1000);\n    for (int i = 0; i < 1000; ++i) {\n        cin >> orders[i].a >> orders[i].b >> orders[i].c >> orders[i].d;\n        orders[i].idx = i + 1;\n        orders[i].cost = dist(OFFICE_X, OFFICE_Y, orders[i].a, orders[i].b) +\n                         dist(orders[i].a, orders[i].b, orders[i].c, orders[i].d) +\n                         dist(orders[i].c, orders[i].d, OFFICE_X, OFFICE_Y);\n    }\n    \n    // Select 50 orders with lowest cost\n    sort(orders.begin(), orders.end(), [](const Order& o1, const Order& o2) {\n        return o1.cost < o2.cost;\n    });\n    vector<Order> selected(orders.begin(), orders.begin() + 50);\n    \n    // Prepare pickups and deliveries\n    vector<pii> pickups, deliveries;\n    for (const auto& ord : selected) {\n        pickups.emplace_back(ord.a, ord.b);\n        deliveries.emplace_back(ord.c, ord.d);\n    }\n    \n    // Optimize order using SA\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n    vector<pii> pickup_route = simulated_annealing(pickups, OFFICE_X, OFFICE_Y, rng);\n    vector<pii> delivery_route = simulated_annealing(deliveries, OFFICE_X, OFFICE_Y, rng);\n    \n    // Combine routes: office -> pickups -> deliveries -> office\n    vector<pii> full_route;\n    full_route.emplace_back(OFFICE_X, OFFICE_Y);\n    for (size_t i = 1; i < pickup_route.size() - 1; ++i) {  // Skip office at start/end\n        full_route.push_back(pickup_route[i]);\n    }\n    for (size_t i = 1; i < delivery_route.size() - 1; ++i) {\n        full_route.push_back(delivery_route[i]);\n    }\n    full_route.emplace_back(OFFICE_X, OFFICE_Y);\n    \n    // Output\n    cout << 50;\n    for (const auto& ord : selected) cout << \" \" << ord.idx;\n    cout << \"\\n\";\n    cout << full_route.size();\n    for (const auto& p : full_route) cout << \" \" << p.first << \" \" << p.second;\n    cout << \"\\n\";\n    \n    return 0;\n}","ahc007":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    const int N = 400;\n    const int M = 1995;\n    vector<int> x(N), y(N);\n    for (int i = 0; i < N; i++) {\n        cin >> x[i] >> y[i];\n    }\n    vector<int> u(M), v(M);\n    for (int i = 0; i < M; i++) {\n        cin >> u[i] >> v[i];\n    }\n    // Adjacency list: vector of {to, {edge_index, weight}}\n    vector<vector<pair<int, pair<int, int>>>> adj(N);\n    vector<bool> adopted(M, false);\n    vector<int> weights(M, 0);\n    \n    auto remove_edge = [&](int a, int b, int e) {\n        for (auto it = adj[a].begin(); it != adj[a].end(); ++it) {\n            if (it->second.first == e) {\n                adj[a].erase(it);\n                break;\n            }\n        }\n    };\n    \n    for (int i = 0; i < M; i++) {\n        int l;\n        cin >> l;\n        int uu = u[i], vv = v[i];\n        // BFS to check connectivity and find max on path\n        vector<int> dist(N, -1);\n        vector<int> parent(N, -1);\n        vector<int> edge_to_parent(N, -1);\n        queue<int> q;\n        q.push(uu);\n        dist[uu] = 0;\n        bool found = false;\n        int max_weight = -1;\n        int max_edge = -1;\n        while (!q.empty() && !found) {\n            int cur = q.front();\n            q.pop();\n            for (auto &[to, p] : adj[cur]) {\n                int eidx = p.first;\n                int w = p.second;\n                if (dist[to] == -1) {\n                    dist[to] = dist[cur] + 1;\n                    parent[to] = cur;\n                    edge_to_parent[to] = eidx;\n                    q.push(to);\n                    if (to == vv) {\n                        found = true;\n                        // Backtrack to find max on path\n                        int current = vv;\n                        while (current != uu) {\n                            int e = edge_to_parent[current];\n                            int ww = weights[e];\n                            if (ww > max_weight) {\n                                max_weight = ww;\n                                max_edge = e;\n                            }\n                            current = parent[current];\n                        }\n                        break;\n                    }\n                }\n            }\n        }\n        if (!found) {\n            // Not connected, adopt\n            adopted[i] = true;\n            adj[uu].emplace_back(vv, make_pair(i, l));\n            adj[vv].emplace_back(uu, make_pair(i, l));\n            weights[i] = l;\n            cout << 1 << endl;\n            fflush(stdout);\n        } else {\n            // Connected, check for replacement\n            if (l < max_weight) {\n                // Adopt and remove max_edge\n                adopted[i] = true;\n                adopted[max_edge] = false;\n                remove_edge(u[max_edge], v[max_edge], max_edge);\n                remove_edge(v[max_edge], u[max_edge], max_edge);\n                adj[uu].emplace_back(vv, make_pair(i, l));\n                adj[vv].emplace_back(uu, make_pair(i, l));\n                weights[i] = l;\n                cout << 1 << endl;\n                fflush(stdout);\n            } else {\n                cout << 0 << endl;\n                fflush(stdout);\n            }\n        }\n    }\n    return 0;\n}","ahc008":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int DX[4] = {-1, 0, 1, 0};\nconst int DY[4] = {0, 1, 0, -1};\n\ndouble compute_score(pair<int, int> pos, const vector<vector<bool>>& passable, const vector<pair<int, int>>& pets) {\n    vector<vector<bool>> vis(31, vector<bool>(31, false));\n    queue<pair<int, int>> q;\n    q.push(pos);\n    vis[pos.first][pos.second] = true;\n    int area = 1;\n    int num_pets = 0;\n    for (auto& p : pets) {\n        if (p == pos) num_pets++;\n    }\n    while (!q.empty()) {\n        auto [x, y] = q.front();\n        q.pop();\n        for (int d = 0; d < 4; d++) {\n            int nx = x + DX[d], ny = y + DY[d];\n            if (nx >= 1 && nx <= 30 && ny >= 1 && ny <= 30 && passable[nx][ny] && !vis[nx][ny]) {\n                vis[nx][ny] = true;\n                area++;\n                for (auto& p : pets) {\n                    if (p == make_pair(nx, ny)) num_pets++;\n                }\n                q.push({nx, ny});\n            }\n        }\n    }\n    return (area / 900.0) * pow(2.0, -num_pets);\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N;\n    cin >> N;\n    vector<pair<int, int>> pets(N);\n    vector<int> pt(N);\n    for (int i = 0; i < N; i++) {\n        int x, y, t;\n        cin >> x >> y >> t;\n        pets[i] = {x, y};\n        pt[i] = t;\n    }\n    int M;\n    cin >> M;\n    vector<pair<int, int>> humans(M);\n    for (int i = 0; i < M; i++) {\n        int x, y;\n        cin >> x >> y;\n        humans[i] = {x, y};\n    }\n\n    vector<vector<bool>> passable(31, vector<bool>(31, true));\n\n    for (int turn = 0; turn < 300; turn++) {\n        vector<string> actions(M, \".\");\n        for (int i = 0; i < M; i++) {\n            double max_s = -1e9;\n            string best_act = \".\";\n            pair<int, int> cur_pos = humans[i];\n\n            // Try stay\n            {\n                double s = compute_score(cur_pos, passable, pets);\n                if (s > max_s) {\n                    max_s = s;\n                    best_act = \".\";\n                }\n            }\n\n            // Try builds\n            vector<string> builds = {\"u\", \"d\", \"l\", \"r\"};\n            for (auto act : builds) {\n                int dx = 0, dy = 0;\n                if (act == \"u\") dx = -1;\n                else if (act == \"d\") dx = 1;\n                else if (act == \"l\") dy = -1;\n                else if (act == \"r\") dy = 1;\n                int nx = cur_pos.first + dx, ny = cur_pos.second + dy;\n                bool valid = true;\n                if (nx < 1 || nx > 30 || ny < 1 || ny > 30) valid = false;\n                else {\n                    // Check for entities\n                    bool has_entity = false;\n                    for (auto& p : pets) {\n                        if (p == make_pair(nx, ny)) has_entity = true;\n                    }\n                    for (int j = 0; j < M; j++) {\n                        if (humans[j] == make_pair(nx, ny)) has_entity = true;\n                    }\n                    if (has_entity) valid = false;\n                    // Check adjacent pets\n                    for (int d = 0; d < 4; d++) {\n                        int ax = nx + DX[d], ay = ny + DY[d];\n                        if (ax >= 1 && ax <= 30 && ay >= 1 && ay <= 30) {\n                            for (auto& p : pets) {\n                                if (p == make_pair(ax, ay)) {\n                                    valid = false;\n                                    break;\n                                }\n                            }\n                        }\n                        if (!valid) break;\n                    }\n                    if (valid) {\n                        passable[nx][ny] = false;\n                        double s = compute_score(cur_pos, passable, pets);\n                        if (s > max_s) {\n                            max_s = s;\n                            best_act = act;\n                        }\n                        passable[nx][ny] = true;\n                    }\n                }\n            }\n\n            // Try moves\n            vector<string> moves = {\"U\", \"D\", \"L\", \"R\"};\n            for (auto act : moves) {\n                int dx = 0, dy = 0;\n                if (act == \"U\") dx = -1;\n                else if (act == \"D\") dx = 1;\n                else if (act == \"L\") dy = -1;\n                else if (act == \"R\") dy = 1;\n                int nx = cur_pos.first + dx, ny = cur_pos.second + dy;\n                bool valid = true;\n                if (nx < 1 || nx > 30 || ny < 1 || ny > 30) valid = false;\n                else if (!passable[nx][ny]) valid = false;\n                else {\n                    pair<int, int> new_pos = {nx, ny};\n                    double s = compute_score(new_pos, passable, pets);\n                    if (s > max_s) {\n                        max_s = s;\n                        best_act = act;\n                    }\n                }\n            }\n            actions[i] = best_act;\n        }\n\n        // Apply human moves\n        for (int i = 0; i < M; i++) {\n            string act = actions[i];\n            if (act[0] >= 'A' && act[0] <= 'Z') {  // Move\n                int dx = 0, dy = 0;\n                if (act == \"U\") dx = -1;\n                else if (act == \"D\") dx = 1;\n                else if (act == \"L\") dy = -1;\n                else if (act == \"R\") dy = 1;\n                int nx = humans[i].first + dx, ny = humans[i].second + dy;\n                humans[i] = {nx, ny};\n            }\n        }\n\n        // Apply builds\n        for (int i = 0; i < M; i++) {\n            string act = actions[i];\n            if (act[0] >= 'a' && act[0] <= 'z') {  // Build\n                int dx = 0, dy = 0;\n                if (act == \"u\") dx = -1;\n                else if (act == \"d\") dx = 1;\n                else if (act == \"l\") dy = -1;\n                else if (act == \"r\") dy = 1;\n                int nx = humans[i].first + dx, ny = humans[i].second + dy;\n                passable[nx][ny] = false;\n            }\n        }\n\n        // Output actions\n        for (auto& a : actions) cout << a;\n        cout << endl;\n        cout.flush();\n\n        // Read and apply pet moves\n        for (int i = 0; i < N; i++) {\n            string s;\n            cin >> s;\n            if (s == \".\") continue;\n            pair<int, int> pos = pets[i];\n            for (char c : s) {\n                int dx = 0, dy = 0;\n                if (c == 'U') dx = -1;\n                else if (c == 'D') dx = 1;\n                else if (c == 'L') dy = -1;\n                else if (c == 'R') dy = 1;\n                int nx = pos.first + dx, ny = pos.second + dy;\n                if (nx >= 1 && nx <= 30 && ny >= 1 && ny <= 30 && passable[nx][ny]) {\n                    pos = {nx, ny};\n                }\n            }\n            pets[i] = pos;\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), v(20);\n    for (int i = 0; i < 20; i++) cin >> h[i];\n    for (int i = 0; i < 20; i++) cin >> v[i];\n    \n    // Build adjacency list for the grid\n    vector<vector<vector<pair<int, int>>>> adj(20, vector<vector<pair<int, int>>>(20));\n    for (int i = 0; i < 20; i++) {\n        for (int j = 0; j < 20; j++) {\n            // Right\n            if (j < 19 && h[i][j] == '0') adj[i][j].push_back({i, j + 1});\n            // Left\n            if (j > 0 && h[i][j - 1] == '0') adj[i][j].push_back({i, j - 1});\n            // Down\n            if (i < 19 && v[i][j] == '0') adj[i][j].push_back({i + 1, j});\n            // Up\n            if (i > 0 && v[i - 1][j] == '0') adj[i][j].push_back({i - 1, j});\n        }\n    }\n    \n    // BFS to find shortest path\n    vector<vector<int>> dist(20, vector<int>(20, -1));\n    vector<vector<pair<int, int>>> parent(20, vector<pair<int, int>>(20, {-1, -1}));\n    queue<pair<int, int>> q;\n    q.push({si, sj});\n    dist[si][sj] = 0;\n    \n    while (!q.empty()) {\n        auto [x, y] = q.front(); q.pop();\n        for (auto [nx, ny] : adj[x][y]) {\n            if (dist[nx][ny] == -1) {\n                dist[nx][ny] = dist[x][y] + 1;\n                parent[nx][ny] = {x, y};\n                q.push({nx, ny});\n            }\n        }\n    }\n    \n    // If unreachable (shouldn't happen), output empty\n    if (dist[ti][tj] == -1) {\n        cout << \"\" << endl;\n        return 0;\n    }\n    \n    // Reconstruct the path\n    vector<pair<int, int>> path;\n    pair<int, int> cur = {ti, tj};\n    while (cur != make_pair(si, sj)) {\n        path.push_back(cur);\n        cur = parent[cur.first][cur.second];\n    }\n    path.push_back({si, sj});\n    reverse(path.begin(), path.end());\n    \n    // Generate the move string\n    string full_moves = \"\";\n    for (size_t i = 1; i < path.size(); i++) {\n        auto [px, py] = path[i - 1];\n        auto [cx, cy] = path[i];\n        if (cx == px + 1) full_moves += 'D';\n        else if (cx == px - 1) full_moves += 'U';\n        else if (cy == py + 1) full_moves += 'R';\n        else if (cy == py - 1) full_moves += 'L';\n    }\n    \n    // Handle length <= 200 with repetition for robustness\n    int L = full_moves.size();\n    string output;\n    if (L <= 200) {\n        int repeat = 200 / L;\n        for (int i = 0; i < repeat; i++) output += full_moves;\n        int rem = 200 - output.size();\n        for (int i = 0; i < rem; i++) output += full_moves[i % L];\n    } else {\n        // Truncate to 200 moves\n        output = full_moves.substr(0, 200);\n    }\n    \n    cout << output << endl;\n    return 0;\n}","ahc010":"#include <bits/stdc++.h>\nusing namespace std;\n\nmt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n\nconst vector<vector<int>> to = {\n    {1, 0, -1, -1},  // 0\n    {3, -1, -1, 0},  // 1\n    {-1, -1, 3, 2},  // 2\n    {-1, 2, 1, -1},  // 3\n    {1, 0, 3, 2},    // 4\n    {3, 2, 1, 0},    // 5\n    {2, -1, 0, -1},  // 6\n    {-1, 3, -1, 1}   // 7\n};\n\nconst vector<int> di = {0, -1, 0, 1};\nconst vector<int> dj = {-1, 0, 1, 0};\n\nint get_type(int t, int r) {\n    if (t <= 3) return (t + r) % 4;\n    if (t <= 5) return 4 + (t - 4 + r) % 2;\n    return 6 + (t - 6 + r) % 2;\n}\n\nlong long compute_score(const vector<vector<int>>& tiles, const vector<int>& rot) {\n    vector<vector<int>> adj(900);\n    for (int i = 0; i < 30; i++) {\n        for (int j = 0; j < 30; j++) {\n            int idx = i * 30 + j;\n            int t = tiles[i][j];\n            int r = rot[idx];\n            int et = get_type(t, r);\n            for (int d = 0; d < 4; d++) {\n                if (to[et][d] != -1) {\n                    int d2 = to[et][d];\n                    int ni = i + di[d2];\n                    int nj = j + dj[d2];\n                    if (ni >= 0 && ni < 30 && nj >= 0 && nj < 30) {\n                        int nidx = ni * 30 + nj;\n                        if (ni > i || (ni == i && nj > j)) {\n                            adj[idx].push_back(nidx);\n                            adj[nidx].push_back(idx);\n                        }\n                    }\n                }\n            }\n        }\n    }\n    vector<int> cycle_lengths;\n    vector<bool> visited(900, false);\n    vector<int> disc(900, -1);\n    vector<int> parent(900, -1);\n    int timestamp = 0;\n    function<void(int)> dfs = [&](int u) {\n        visited[u] = true;\n        disc[u] = timestamp++;\n        for (int v : adj[u]) {\n            if (!visited[v]) {\n                parent[v] = u;\n                dfs(v);\n            } else if (v != parent[u] && disc[v] < disc[u]) {\n                int len = disc[u] - disc[v] + 1;\n                cycle_lengths.push_back(len);\n            }\n        }\n    };\n    for (int i = 0; i < 900; i++) if (!visited[i]) dfs(i);\n    sort(cycle_lengths.rbegin(), cycle_lengths.rend());\n    if (cycle_lengths.size() >= 2) {\n        return (long long)cycle_lengths[0] * cycle_lengths[1];\n    }\n    return 0;\n}\n\nint main() {\n    vector<vector<int>> tiles(30, vector<int>(30));\n    for (int i = 0; i < 30; i++) {\n        string s;\n        cin >> s;\n        for (int j = 0; j < 30; j++) tiles[i][j] = s[j] - '0';\n    }\n    vector<int> current(900);\n    for (int &x : current) x = rng() % 4;\n    long long current_score = compute_score(tiles, current);\n    double T = 1000.0;\n    const int iterations = 10000;\n    for (int it = 0; it < iterations; it++) {\n        int idx = rng() % 900;\n        int old_r = current[idx];\n        int new_r = rng() % 4;\n        if (new_r == old_r) continue;\n        current[idx] = new_r;\n        long long new_score = compute_score(tiles, current);\n        long long delta = new_score - current_score;\n        if (delta > 0 || exp(delta / T) > (double)rng() / rng.max()) {\n            current_score = new_score;\n        } else {\n            current[idx] = old_r;\n        }\n        T *= 0.999;\n    }\n    string output;\n    for (int x : current) output += '0' + x;\n    cout << output << endl;\n    return 0;\n}","ahc011":"#include <bits/stdc++.h>\nusing namespace std;\n\nint N, T;\nvector<vector<int>> board;\npair<int, int> empty_pos;\n\n// Directions: 0=U, 1=D, 2=L, 3=R\nvector<pair<int, int>> dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};\nstring dir_chars = \"UDLR\";\n\n// Check if a position is valid\nbool valid(int i, int j) {\n    return i >= 0 && i < N && j >= 0 && j < N;\n}\n\n// Simulate a move: return new board and new empty pos\npair<vector<vector<int>>, pair<int, int>> simulate_move(const vector<vector<int>>& curr_board, pair<int, int> curr_empty, int dir) {\n    auto [ei, ej] = curr_empty;\n    auto [di, dj] = dirs[dir];\n    int ni = ei + di, nj = ej + dj;\n    if (!valid(ni, nj)) return {curr_board, curr_empty}; // Invalid move\n    auto new_board = curr_board;\n    swap(new_board[ei][ej], new_board[ni][nj]);\n    return {new_board, {ni, nj}};\n}\n\n// Compute the size of the largest tree component\nint compute_tree_size(const vector<vector<int>>& curr_board) {\n    vector<vector<bool>> visited(N, vector<bool>(N, false));\n    int max_tree = 0;\n    \n    // Directions for connections: 0=left, 1=up, 2=right, 3=down\n    vector<pair<int, int>> conn_dirs = {{0, -1}, {-1, 0}, {0, 1}, {1, 0}};\n    vector<int> masks = {1, 2, 4, 8}; // left, up, right, down\n    \n    for (int i = 0; i < N; ++i) {\n        for (int j = 0; j < N; ++j) {\n            if (curr_board[i][j] == 0 || visited[i][j]) continue;\n            \n            // BFS for component\n            queue<pair<int, int>> q;\n            q.push({i, j});\n            visited[i][j] = true;\n            int vertices = 0, edges = 0;\n            vector<pair<int, int>> component;\n            \n            while (!q.empty()) {\n                auto [ci, cj] = q.front(); q.pop();\n                vertices++;\n                component.push_back({ci, cj});\n                \n                for (int d = 0; d < 4; ++d) {\n                    auto [di, dj] = conn_dirs[d];\n                    int ni = ci + di, nj = cj + dj;\n                    if (valid(ni, nj) && curr_board[ni][nj] != 0) {\n                        // Check if connection exists\n                        bool has_out = (curr_board[ci][cj] & masks[d]) != 0;\n                        bool has_in = (curr_board[ni][nj] & masks[(d + 2) % 4]) != 0; // Opposite direction\n                        if (has_out && has_in) {\n                            edges++;\n                            if (!visited[ni][nj]) {\n                                visited[ni][nj] = true;\n                                q.push({ni, nj});\n                            }\n                        }\n                    }\n                }\n            }\n            \n            // Check if it's a tree: edges == vertices - 1\n            if (edges == vertices - 1) {\n                max_tree = max(max_tree, vertices);\n            }\n        }\n    }\n    return max_tree;\n}\n\nint main() {\n    cin >> N >> T;\n    board.resize(N, vector<int>(N));\n    for (int i = 0; i < N; ++i) {\n        string s; cin >> s;\n        for (int j = 0; j < N; ++j) {\n            if (s[j] >= '0' && s[j] <= '9') board[i][j] = s[j] - '0';\n            else board[i][j] = 10 + (s[j] - 'a');\n            if (board[i][j] == 0) empty_pos = {i, j};\n        }\n    }\n    \n    string moves = \"\";\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n    uniform_real_distribution<double> dist(0.0, 1.0);\n    \n    int current_score = compute_tree_size(board);\n    const double RANDOM_PROB = 0.1; // Probability to choose random move\n    \n    while (moves.size() < T) {\n        vector<pair<int, string>> candidates;\n        int best_score = -1;\n        string best_move = \"\";\n        \n        // Evaluate all 4 moves\n        for (int d = 0; d < 4; ++d) {\n            auto [new_board, new_empty] = simulate_move(board, empty_pos, d);\n            if (new_empty == empty_pos) continue; // Invalid move\n            int score = compute_tree_size(new_board);\n            if (score > best_score) {\n                best_score = score;\n                best_move = string(1, dir_chars[d]);\n            }\n        }\n        \n        // Decide: greedy or random\n        string chosen_move;\n        if (dist(rng) < RANDOM_PROB || best_move.empty()) {\n            // Random valid move\n            vector<int> valid_dirs;\n            for (int d = 0; d < 4; ++d) {\n                auto [new_board, new_empty] = simulate_move(board, empty_pos, d);\n                if (new_empty != empty_pos) valid_dirs.push_back(d);\n            }\n            if (!valid_dirs.empty()) {\n                uniform_int_distribution<int> rand_dir(0, valid_dirs.size() - 1);\n                chosen_move = string(1, dir_chars[valid_dirs[rand_dir(rng)]]);\n            }\n        } else {\n            chosen_move = best_move;\n        }\n        \n        if (chosen_move.empty()) break; // No valid moves, stop\n        \n        // Apply the move\n        int dir_idx = dir_chars.find(chosen_move[0]);\n        auto [new_board, new_empty] = simulate_move(board, empty_pos, dir_idx);\n        board = new_board;\n        empty_pos = new_empty;\n        moves += chosen_move;\n        \n        // Update current score\n        current_score = compute_tree_size(board);\n        \n        // If full tree, we could stop early, but for now, continue to potentially improve\n        if (current_score == N * N - 1) {\n            // Could add logic to minimize K, e.g., stop if score doesn't improve\n        }\n    }\n    \n    cout << moves << endl;\n    return 0;\n}","ahc012":"#include <bits/stdc++.h>\n\nusing namespace std;\n\nint main() {\n    int N, K;\n    cin >> N >> K;\n    vector<int> a(11);\n    for(int i = 1; i <= 10; i++) cin >> a[i];\n    vector<long long> x(N), y(N);\n    for(int i = 0; i < N; i++) cin >> x[i] >> y[i];\n    \n    // Compute angles from 0 to 2*pi\n    vector<double> theta(N);\n    for(int i = 0; i < N; i++) {\n        theta[i] = atan2(y[i], x[i]);\n        if(theta[i] < 0) theta[i] += 2 * M_PI;\n    }\n    sort(theta.begin(), theta.end());\n    \n    // Choose k = 100 angles evenly spaced\n    int k = 100;\n    vector<double> phis(k);\n    for(int i = 0; i < k; i++) {\n        phis[i] = i * 2 * M_PI / k;\n    }\n    sort(phis.begin(), phis.end());\n    \n    // Output the lines\n    cout << k << endl;\n    for(int i = 0; i < k; i++) {\n        long long px = 0, py = 0;\n        long long qx = round(1e9 * cos(phis[i]));\n        long long qy = round(1e9 * sin(phis[i]));\n        cout << px << \" \" << py << \" \" << qx << \" \" << qy << endl;\n    }\n    \n    return 0;\n}","ahc014":"#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n\nstruct Operation {\n    int w, x1, y1, x2, y2, x3, y3, x4, y4;\n};\n\nbool cmp(const Operation& a, const Operation& b) {\n    return a.w < b.w;\n}\n\nint main() {\n    int N, M;\n    cin >> N >> M;\n    vector<vector<bool>> has_dot(N, vector<bool>(N, false));\n    int c = (N - 1) / 2;\n    auto calc_w = [&](int x, int y) {\n        int dx = x - c;\n        int dy = y - c;\n        return dx * dx + dy * dy + 1;\n    };\n    for (int i = 0; i < M; i++) {\n        int x, y;\n        cin >> x >> y;\n        has_dot[x][y] = true;\n    }\n    vector<vector<vector<bool>>> horiz(N, vector<vector<bool>>(N, vector<bool>(N, false)));\n    vector<vector<vector<bool>>> vert(N, vector<vector<bool>>(N, vector<bool>(N, false)));\n    vector<vector<bool>> diag1(N - 1, vector<bool>(N - 1, false));\n    vector<vector<bool>> diag2(N - 1, vector<bool>(N, false));\n    vector<vector<bool>> diag3(N, vector<bool>(N - 1, false));\n    vector<vector<bool>> diag4(N, vector<bool>(N, false));\n    vector<Operation> ops;\n    for (int px = 0; px < N; px++) {\n        for (int py = 0; py < N; py++) {\n            if (has_dot[px][py]) continue;\n            int w = calc_w(px, py);\n            // axis-aligned\n            for (int dx = 1; dx <= N - 1 - px; dx++) {\n                for (int dy = 1; dy <= N - 1 - py; dy++) {\n                    int bx = px + dx, by = py;\n                    int cx = px + dx, cy = py + dy;\n                    int ex = px, ey = py + dy;\n                    if (has_dot[bx][by] && has_dot[cx][cy] && has_dot[ex][ey]) {\n                        bool ok = true;\n                        // check sides\n                        for (int i = px + 1; i < bx; i++) if (has_dot[py][i]) ok = false;\n                        for (int i = px + 1; i < bx; i++) if (has_dot[cy][i]) ok = false;\n                        for (int i = py + 1; i < cy; i++) if (has_dot[px][i]) ok = false;\n                        for (int i = py + 1; i < cy; i++) if (has_dot[bx][i]) ok = false;\n                        // check edges\n                        for (int i = px; i < bx; i++) if (horiz[py][i][px]) ok = false;\n                        for (int i = px; i < bx; i++) if (horiz[cy][i][px]) ok = false;\n                        for (int i = py; i < cy; i++) if (vert[px][i][py]) ok = false;\n                        for (int i = py; i < cy; i++) if (vert[bx][i][py]) ok = false;\n                        if (ok) {\n                            ops.push_back({w, px, py, bx, by, cx, cy, ex, ey});\n                        }\n                    }\n                }\n            }\n            // 45-degree\n            for (int a = 1;; a++) {\n                int p2x = px + a, p2y = py + a;\n                int p3x = px, p3y = py + 2 * a;\n                int p4x = px - a, p4y = py + a;\n                if (p2x >= N || p2y >= N || p3y >= N || p4x < 0 || p4y >= N) break;\n                if (has_dot[p2x][p2y] && has_dot[p3x][p3y] && has_dot[p4x][p4y]) {\n                    bool ok = true;\n                    // p1 to p2\n                    for (int i = 0; i < a; i++) if (diag1[px + i][py + i]) ok = false;\n                    // p2 to p3\n                    for (int i = 0; i < a; i++) if (diag3[p2x - i][p2y + i]) ok = false;\n                    // p3 to p4\n                    for (int i = 0; i < a; i++) if (diag4[p3x - i][p3y - i]) ok = false;\n                    // p4 to p1\n                    for (int i = 0; i < a; i++) if (diag2[p4x + i][p4y - i]) ok = false;\n                    if (ok) {\n                        ops.push_back({w, px, py, p2x, p2y, p3x, p3y, p4x, p4y});\n                    }\n                }\n            }\n        }\n    }\n    sort(ops.begin(), ops.end(), cmp);\n    vector<Operation> final_ops;\n    for (auto& op : ops) {\n        int x1 = op.x1, y1 = op.y1;\n        if (has_dot[x1][y1]) continue;\n        int x2 = op.x2, y2 = op.y2;\n        int x3 = op.x3, y3 = op.y3;\n        int x4 = op.x4, y4 = op.y4;\n        if (!has_dot[x2][y2] || !has_dot[x3][y3] || !has_dot[x4][y4]) continue;\n        // check edges again, in case\n        bool ok = true;\n        // assume axis or 45, check accordingly\n        if (x2 == x1 + (x3 - x1) && y2 == y1 + (y3 - y1)) { // axis\n            int dx = x2 - x1, dy = y3 - y1;\n            for (int i = x1; i < x1 + dx; i++) if (horiz[y1][i][x1]) ok = false;\n            for (int i = x1; i < x1 + dx; i++) if (horiz[y1 + dy][i][x1]) ok = false;\n            for (int i = y1; i < y1 + dy; i++) if (vert[x1][i][y1]) ok = false;\n            for (int i = y1; i < y1 + dy; i++) if (vert[x1 + dx][i][y1]) ok = false;\n        } else { // 45\n            int a = x2 - x1;\n            for (int i = 0; i < a; i++) if (diag1[x1 + i][y1 + i]) ok = false;\n            for (int i = 0; i < a; i++) if (diag3[x2 - i][y2 + i]) ok = false;\n            for (int i = 0; i < a; i++) if (diag4[x3 - i][y3 - i]) ok = false;\n            for (int i = 0; i < a; i++) if (diag2[x4 + i][y4 - i]) ok = false;\n        }\n        if (ok) {\n            final_ops.push_back(op);\n            has_dot[x1][y1] = true;\n            // set edges\n            if (x2 == x1 + (x3 - x1) && y2 == y1 + (y3 - y1)) {\n                int dx = x2 - x1, dy = y3 - y1;\n                for (int i = x1; i < x1 + dx; i++) horiz[y1][i][x1] = true;\n                for (int i = x1; i < x1 + dx; i++) horiz[y1 + dy][i][x1] = true;\n                for (int i = y1; i < y1 + dy; i++) vert[x1][i][y1] = true;\n                for (int i = y1; i < y1 + dy; i++) vert[x1 + dx][i][y1] = true;\n            } else {\n                int a = x2 - x1;\n                for (int i = 0; i < a; i++) diag1[x1 + i][y1 + i] = true;\n                for (int i = 0; i < a; i++) diag3[x2 - i][y2 + i] = true;\n                for (int i = 0; i < a; i++) diag4[x3 - i][y3 - i] = true;\n                for (int i = 0; i < a; i++) diag2[x4 + i][y4 - i] = true;\n            }\n        }\n    }\n    cout << final_ops.size() << endl;\n    for (auto& op : final_ops) {\n        cout << op.x1 << \" \" << op.y1 << \" \" << op.x2 << \" \" << op.y2 << \" \" << op.x3 << \" \" << op.y3 << \" \" << op.x4 << \" \" << op.y4 << endl;\n    }\n    return 0;\n}","ahc015":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 10;\nvector<vector<int>> grid(N, vector<int>(N, 0));\n\n// Directions: F (down), B (up), L (left), R (right)\nstring dirs = \"FBLR\";\n\n// Simulate tilt in direction dir\nvoid simulate_tilt(vector<vector<int>>& g, char dir) {\n    if (dir == 'R') { // Right\n        for (int i = 0; i < N; ++i) {\n            vector<int> new_row(N, 0);\n            int pos = N - 1;\n            for (int j = N - 1; j >= 0; --j) {\n                if (g[i][j] != 0) {\n                    new_row[pos] = g[i][j];\n                    --pos;\n                }\n            }\n            g[i] = new_row;\n        }\n    } else if (dir == 'L') { // Left\n        for (int i = 0; i < N; ++i) {\n            vector<int> new_row(N, 0);\n            int pos = 0;\n            for (int j = 0; j < N; ++j) {\n                if (g[i][j] != 0) {\n                    new_row[pos] = g[i][j];\n                    ++pos;\n                }\n            }\n            g[i] = new_row;\n        }\n    } else if (dir == 'F') { // Forward (down)\n        for (int j = 0; j < N; ++j) {\n            vector<int> new_col(N, 0);\n            int pos = N - 1;\n            for (int i = N - 1; i >= 0; --i) {\n                if (g[i][j] != 0) {\n                    new_col[pos] = g[i][j];\n                    --pos;\n                }\n            }\n            for (int i = 0; i < N; ++i) g[i][j] = new_col[i];\n        }\n    } else if (dir == 'B') { // Backward (up)\n        for (int j = 0; j < N; ++j) {\n            vector<int> new_col(N, 0);\n            int pos = 0;\n            for (int i = 0; i < N; ++i) {\n                if (g[i][j] != 0) {\n                    new_col[pos] = g[i][j];\n                    ++pos;\n                }\n            }\n            for (int i = 0; i < N; ++i) g[i][j] = new_col[i];\n        }\n    }\n}\n\n// DFS to compute component size\nint dfs(int x, int y, int fl, vector<vector<int>>& g, vector<vector<bool>>& vis) {\n    vis[x][y] = true;\n    int cnt = 1;\n    int dx[4] = {-1, 0, 1, 0};\n    int dy[4] = {0, 1, 0, -1};\n    for (int d = 0; d < 4; ++d) {\n        int nx = x + dx[d], ny = y + dy[d];\n        if (nx >= 0 && nx < N && ny >= 0 && ny < N && g[nx][ny] == fl && !vis[nx][ny]) {\n            cnt += dfs(nx, ny, fl, g, vis);\n        }\n    }\n    return cnt;\n}\n\n// Compute score for the grid\nlong long compute_score(const vector<vector<int>>& g) {\n    vector<int> component_sizes;\n    for (int fl = 1; fl <= 3; ++fl) {\n        vector<vector<bool>> visited(N, vector<bool>(N, false));\n        for (int i = 0; i < N; ++i) {\n            for (int j = 0; j < N; ++j) {\n                if (g[i][j] == fl && !visited[i][j]) {\n                    int size = dfs(i, j, fl, const_cast<vector<vector<int>>&>(g), visited);\n                    component_sizes.push_back(size);\n                }\n            }\n        }\n    }\n    long long sum_sq = 0;\n    for (int s : component_sizes) sum_sq += 1LL * s * s;\n    vector<long long> d(4, 0);\n    for (int i = 0; i < N; ++i) {\n        for (int j = 0; j < N; ++j) {\n            if (g[i][j]) d[g[i][j]]++;\n        }\n    }\n    long long sum_d_sq = 0;\n    for (int i = 1; i <= 3; ++i) sum_d_sq += d[i] * d[i];\n    if (sum_d_sq == 0) return 0; // Avoid division by zero\n    double score = 1e6 * sum_sq / (double)sum_d_sq;\n    return round(score);\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    vector<int> f(100);\n    for (int i = 0; i < 100; ++i) cin >> f[i];\n    \n    vector<pair<int, int>> empty;\n    for (int i = 0; i < N; ++i) {\n        for (int j = 0; j < N; ++j) {\n            empty.emplace_back(i, j);\n        }\n    }\n    \n    for (int t = 0; t < 100; ++t) {\n        int p;\n        cin >> p;\n        auto [x, y] = empty[p - 1];\n        grid[x][y] = f[t];\n        empty.erase(empty.begin() + p - 1);\n        \n        // Try all directions and choose the best\n        long long best_score = -1;\n        char best_dir = ' ';\n        for (char dir : dirs) {\n            auto temp_grid = grid;\n            simulate_tilt(temp_grid, dir);\n            long long score = compute_score(temp_grid);\n            if (score > best_score) {\n                best_score = score;\n                best_dir = dir;\n            }\n        }\n        \n        // Output and update grid\n        cout << best_dir << '\\n';\n        cout.flush();\n        simulate_tilt(grid, best_dir); // Apply the best tilt to the actual grid\n    }\n    \n    return 0;\n}","ahc016":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int M;\n    double eps;\n    cin >> M >> eps;\n    int N = 100;\n    cout << N << endl;\n    for (int k = 0; k < M; k++) {\n        string s = \"\";\n        for (int i = 0; i < N; i++) {\n            for (int j = i + 1; j < N; j++) {\n                if (i < k && j < k) {\n                    s += '1';\n                } else {\n                    s += '0';\n                }\n            }\n        }\n        cout << s << endl;\n    }\n    cout << flush;\n    vector<vector<int>> targets(M, vector<int>(N));\n    for (int t = 0; t < M; t++) {\n        for (int i = 0; i < N - t; i++) {\n            targets[t][i] = 0;\n        }\n        for (int i = N - t; i < N; i++) {\n            targets[t][i] = t - 1;\n        }\n    }\n    for (int q = 0; q < 100; q++) {\n        string H;\n        cin >> H;\n        vector<int> deg(N, 0);\n        int idx = 0;\n        for (int i = 0; i < N; i++) {\n            for (int j = i + 1; j < N; j++) {\n                if (H[idx] == '1') {\n                    deg[i]++;\n                    deg[j]++;\n                }\n                idx++;\n            }\n        }\n        sort(deg.begin(), deg.end());\n        int best_t = 0;\n        long long min_diff = LLONG_MAX;\n        for (int t = 0; t < M; t++) {\n            long long diff = 0;\n            for (int i = 0; i < N; i++) {\n                diff += abs(deg[i] - targets[t][i]);\n            }\n            if (diff < min_diff) {\n                min_diff = diff;\n                best_t = t;\n            }\n        }\n        cout << best_t << endl;\n        cout << flush;\n    }\n    return 0;\n}","ahc017":"#include <bits/stdc++.h>\n\nusing namespace std;\n\nint main() {\n  ios::sync_with_stdio(false);\n  cin.tie(0);\n  int N, M, D, K;\n  cin >> N >> M >> D >> K;\n  vector<tuple<int, int, int, int>> edges;\n  for (int i = 0; i < M; i++) {\n    int u, v, w;\n    cin >> u >> v >> w;\n    u--;\n    v--;\n    edges.emplace_back(w, u, v, i);\n  }\n  vector<pair<int, int>> coords(N);\n  for (int i = 0; i < N; i++) {\n    int x, y;\n    cin >> x >> y;\n    coords[i] = {x, y};\n  }\n  // Sort edges by weight ascending (lighter edges first)\n  sort(edges.begin(), edges.end());\n  // Compute target sizes for each day\n  int total = M;\n  vector<int> max_size(D, total / D);\n  for (int i = 0; i < total % D; i++) max_size[i]++;\n  // Assign edges\n  vector<int> assignment(M);\n  vector<int> current_size(D, 0);\n  for (auto [w, u, v, idx] : edges) {\n    // Find the day with the smallest current size that has space\n    int best_day = -1;\n    int min_size = INT_MAX;\n    for (int d = 0; d < D; d++) {\n      if (current_size[d] < max_size[d] && current_size[d] < min_size) {\n        min_size = current_size[d];\n        best_day = d;\n      }\n    }\n    assert(best_day != -1);\n    assignment[idx] = best_day;\n    current_size[best_day]++;\n  }\n  // Output the assignment\n  for (int i = 0; i < M; i++) {\n    if (i > 0) cout << \" \";\n    cout << assignment[i] + 1;\n  }\n  cout << \"\\n\";\n  return 0;\n}","ahc019":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MAX_D = 14;\nint D;\nvector<string> f[2], r[2];\nvector<vector<vector<int>>> grid[2]; // grid[i][x][y][z]\nvector<int> b[2]; // flattened b[i], size D*D*D\n\n// Directions for 6-connectivity (face-adjacent)\nconst int dx[6] = {1, -1, 0, 0, 0, 0};\nconst int dy[6] = {0, 0, 1, -1, 0, 0};\nconst int dz[6] = {0, 0, 0, 0, 1, -1};\n\n// Canonicalize a shape: translate to (0,0,0), try 24 rotations, pick lex smallest\nstring canonicalize(const vector<tuple<int,int,int>>& shape) {\n    if (shape.empty()) return \"\";\n    // Find min coords\n    int minx = INT_MAX, miny = INT_MAX, minz = INT_MAX;\n    for (auto [x,y,z] : shape) {\n        minx = min(minx, x);\n        miny = min(miny, y);\n        minz = min(minz, z);\n    }\n    // Translate\n    vector<tuple<int,int,int>> trans;\n    for (auto [x,y,z] : shape) {\n        trans.emplace_back(x - minx, y - miny, z - minz);\n    }\n    // Generate 24 rotations (90 deg around x,y,z)\n    vector<string> candidates;\n    for (int rx = 0; rx < 4; ++rx) {\n        for (int ry = 0; ry < 4; ++ry) {\n            for (int rz = 0; rz < 4; ++rz) {\n                if (__builtin_popcount(rx) + __builtin_popcount(ry) + __builtin_popcount(rz) > 3) continue; // Only 24 unique\n                vector<tuple<int,int,int>> rot;\n                for (auto [x,y,z] : trans) {\n                    // Apply rotations\n                    int nx = x, ny = y, nz = z;\n                    for (int i = 0; i < rx; ++i) { int t = ny; ny = -nz; nz = t; } // Rotate x\n                    for (int i = 0; i < ry; ++i) { int t = nz; nz = -nx; nx = t; } // Rotate y\n                    for (int i = 0; i < rz; ++i) { int t = nx; nx = -ny; ny = t; } // Rotate z\n                    rot.emplace_back(nx, ny, nz);\n                }\n                // Sort and stringify\n                sort(rot.begin(), rot.end());\n                string s;\n                for (auto [x,y,z] : rot) {\n                    s += to_string(x) + \",\" + to_string(y) + \",\" + to_string(z) + \";\";\n                }\n                candidates.push_back(s);\n            }\n        }\n    }\n    return *min_element(candidates.begin(), candidates.end());\n}\n\n// DFS to find connected component\nvoid dfs(int i, int x, int y, int z, vector<tuple<int,int,int>>& component, vector<vector<vector<bool>>>& visited) {\n    visited[x][y][z] = true;\n    component.emplace_back(x, y, z);\n    for (int d = 0; d < 6; ++d) {\n        int nx = x + dx[d], ny = y + dy[d], nz = z + dz[d];\n        if (nx >= 0 && nx < D && ny >= 0 && ny < D && nz >= 0 && nz < D &&\n            grid[i][nx][ny][nz] && !visited[nx][ny][nz]) {\n            dfs(i, nx, ny, nz, component, visited);\n        }\n    }\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    cin >> D;\n    f[0].resize(D); f[1].resize(D);\n    r[0].resize(D); r[1].resize(D);\n    for (int i = 0; i < 2; ++i) {\n        for (int k = 0; k < D; ++k) cin >> f[i][k];\n        for (int k = 0; k < D; ++k) cin >> r[i][k];\n    }\n    \n    // Build grids\n    grid[0].assign(D, vector<vector<int>>(D, vector<int>(D, 0)));\n    grid[1].assign(D, vector<vector<int>>(D, vector<int>(D, 0)));\n    for (int i = 0; i < 2; ++i) {\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 (f[i][z][x] == '1' && r[i][z][y] == '1') {\n                        grid[i][x][y][z] = 1;\n                    }\n                }\n            }\n        }\n    }\n    \n    // Find components for each grid\n    vector<vector<vector<tuple<int,int,int>>>> components(2);\n    for (int i = 0; i < 2; ++i) {\n        vector<vector<vector<bool>>> visited(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                    if (grid[i][x][y][z] && !visited[x][y][z]) {\n                        vector<tuple<int,int,int>> comp;\n                        dfs(i, x, y, z, comp, visited);\n                        components[i].push_back(comp);\n                    }\n                }\n            }\n        }\n    }\n    \n    // Assign block IDs with shape matching\n    map<string, int> shape_to_id;\n    int n = 0;\n    b[0].assign(D * D * D, 0);\n    b[1].assign(D * D * D, 0);\n    \n    // Process i=0 first\n    for (const auto& comp : components[0]) {\n        string shape = canonicalize(comp);\n        if (shape_to_id.find(shape) == shape_to_id.end()) {\n            shape_to_id[shape] = ++n;\n        }\n        int id = shape_to_id[shape];\n        for (auto [x,y,z] : comp) {\n            b[0][x * D * D + y * D + z] = id;\n        }\n    }\n    \n    // Process i=1, reusing IDs if possible\n    for (const auto& comp : components[1]) {\n        string shape = canonicalize(comp);\n        if (shape_to_id.find(shape) == shape_to_id.end()) {\n            shape_to_id[shape] = ++n;\n        }\n        int id = shape_to_id[shape];\n        for (auto [x,y,z] : comp) {\n            b[1][x * D * D + y * D + z] = id;\n        }\n    }\n    \n    // Output\n    cout << n << '\\n';\n    for (int val : b[0]) cout << val << ' ';\n    cout << '\\n';\n    for (int val : b[1]) cout << val << ' ';\n    cout << '\\n';\n    \n    return 0;\n}","ahc020":"#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing ld = long double;\n\nstruct Edge {\n    int u, v, w, idx;\n    bool operator<(const Edge& other) const {\n        return w < other.w;\n    }\n};\n\nint find(vector<int>& parent, int x) {\n    if (parent[x] != x) parent[x] = find(parent, parent[x]);\n    return parent[x];\n}\n\nvoid unite(vector<int>& parent, vector<int>& rank, int x, int y) {\n    int px = find(parent, x), py = find(parent, y);\n    if (px != py) {\n        if (rank[px] < rank[py]) swap(px, py);\n        parent[py] = px;\n        if (rank[px] == rank[py]) rank[px]++;\n    }\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, K;\n    cin >> N >> M >> K;\n    vector<pair<int, int>> stations(N);\n    for (int i = 0; i < N; ++i) {\n        cin >> stations[i].first >> stations[i].second;\n    }\n    vector<Edge> edges(M);\n    for (int i = 0; i < M; ++i) {\n        cin >> edges[i].u >> edges[i].v >> edges[i].w;\n        edges[i].u--; edges[i].v--; // 0-based\n        edges[i].idx = i;\n    }\n    vector<pair<int, int>> residents(K);\n    for (int i = 0; i < K; ++i) {\n        cin >> residents[i].first >> residents[i].second;\n    }\n\n    // Step 1: Assign each resident to the closest station\n    vector<int> closest(K);\n    vector<ld> min_dist(K, 1e18);\n    for (int k = 0; k < K; ++k) {\n        for (int i = 0; i < N; ++i) {\n            ld dx = residents[k].first - stations[i].first;\n            ld dy = residents[k].second - stations[i].second;\n            ld dist = hypot(dx, dy);\n            if (dist < min_dist[k]) {\n                min_dist[k] = dist;\n                closest[k] = i;\n            }\n        }\n    }\n\n    // Step 2: Get the set of stations that need to be connected (S)\n    set<int> active_stations;\n    for (int k = 0; k < K; ++k) {\n        active_stations.insert(closest[k]);\n    }\n\n    // Step 3: Compute MST on the subgraph induced by active_stations\n    vector<Edge> mst_edges;\n    vector<int> parent(N), rankk(N, 0);\n    for (int i = 0; i < N; ++i) parent[i] = i;\n    sort(edges.begin(), edges.end());\n    for (auto& e : edges) {\n        if (active_stations.count(e.u) && active_stations.count(e.v)) {\n            int pu = find(parent, e.u), pv = find(parent, e.v);\n            if (pu != pv) {\n                unite(parent, rankk, e.u, e.v);\n                mst_edges.push_back(e);\n            }\n        }\n    }\n\n    // Step 4: Set P_i\n    vector<int> P(N, 0);\n    vector<vector<ld>> assigned_dists(N);\n    for (int k = 0; k < K; ++k) {\n        int i = closest[k];\n        ld dx = residents[k].first - stations[i].first;\n        ld dy = residents[k].second - stations[i].second;\n        ld dist = hypot(dx, dy);\n        assigned_dists[i].push_back(dist);\n    }\n    for (int i = 0; i < N; ++i) {\n        if (!assigned_dists[i].empty()) {\n            ld max_d = *max_element(assigned_dists[i].begin(), assigned_dists[i].end());\n            P[i] = min(5000, (int)ceil(max_d));\n        }\n    }\n\n    // Step 5: Set B_j (1 if in MST and both ends active)\n    vector<int> B(M, 0);\n    for (auto& e : mst_edges) {\n        B[e.idx] = 1;\n    }\n\n    // Output\n    for (int i = 0; i < N; ++i) {\n        if (i > 0) cout << \" \";\n        cout << P[i];\n    }\n    cout << \"\\n\";\n    for (int i = 0; i < M; ++i) {\n        if (i > 0) cout << \" \";\n        cout << B[i];\n    }\n    cout << \"\\n\";\n\n    return 0;\n}","ahc021":"#include <bits/stdc++.h>\n\nusing namespace std;\n\nint main() {\n    vector<vector<int>> pyramid(30);\n    for (int x = 0; x < 30; x++) {\n        pyramid[x].resize(x + 1);\n        for (int y = 0; y <= x; y++) {\n            cin >> pyramid[x][y];\n        }\n    }\n\n    vector<tuple<int, int, int, int>> ops;\n\n    // Recursive lambda for sift_down\n    auto sift_down = [&](auto& self, int x, int y) -> void {\n        if (x >= 29) return;\n        int val = pyramid[x][y];\n        int lx = x + 1, ly = y;\n        int rx = x + 1, ry = y + 1;\n        int lval = pyramid[lx][ly];\n        int rval = pyramid[rx][ry];\n        if (lval >= val && rval >= val) return;\n        if (lval < val && lval <= rval) {\n            // Swap with left child\n            swap(pyramid[x][y], pyramid[lx][ly]);\n            ops.push_back({x, y, lx, ly});\n            self(self, lx, ly);\n        } else if (rval < val) {\n            // Swap with right child\n            swap(pyramid[x][y], pyramid[rx][ry]);\n            ops.push_back({x, y, rx, ry});\n            self(self, rx, ry);\n        }\n    };\n\n    // Build the heap by heapifying from bottom to top\n    for (int x = 28; x >= 0; x--) {\n        for (int y = 0; y <= x; y++) {\n            sift_down(sift_down, x, y);\n        }\n    }\n\n    // Output the result\n    cout << ops.size() << endl;\n    for (auto [a, b, c, d] : ops) {\n        cout << a << \" \" << b << \" \" << c << \" \" << d << endl;\n    }\n\n    return 0;\n}","toyota2023summer-final":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int D, N;\n    cin >> D >> N;\n    vector<vector<int>> obs(D, vector<int>(D, 0));\n    for (int k = 0; k < N; ++k) {\n        int ri, rj;\n        cin >> ri >> rj;\n        obs[ri][rj] = 1;\n    }\n    int entrance_i = 0, entrance_j = (D - 1) / 2;\n    obs[entrance_i][entrance_j] = 1; // Block entrance for containers\n\n    // Precompute distances from entrance, avoiding obstacles\n    vector<vector<int>> dist(D, vector<int>(D, -1));\n    queue<pair<int, int>> q;\n    q.push({entrance_i, entrance_j});\n    dist[entrance_i][entrance_j] = 0;\n    vector<pair<int, int>> dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};\n    while (!q.empty()) {\n        auto [i, j] = q.front();\n        q.pop();\n        for (auto [di, dj] : dirs) {\n            int ni = i + di, nj = j + dj;\n            if (ni >= 0 && ni < D && nj >= 0 && nj < D && obs[ni][nj] == 0 && dist[ni][nj] == -1) {\n                dist[ni][nj] = dist[i][j] + 1;\n                q.push({ni, nj});\n            }\n        }\n    }\n\n    int M = D * D - 1 - N; // Number of containers\n    vector<vector<int>> placed(D, vector<int>(D, -1)); // -1: empty, else: container number\n\n    // Placement phase\n    for (int d = 0; d < M; ++d) {\n        int t_d;\n        cin >> t_d;\n        // BFS to find reachable empty squares\n        vector<vector<int>> vis(D, vector<int>(D, 0));\n        queue<pair<int, int>> qq;\n        qq.push({entrance_i, entrance_j});\n        vis[entrance_i][entrance_j] = 1;\n        vector<tuple<int, int, int>> candidates; // dist, i, j\n        while (!qq.empty()) {\n            auto [i, j] = qq.front();\n            qq.pop();\n            for (auto [di, dj] : dirs) {\n                int ni = i + di, nj = j + dj;\n                if (ni >= 0 && ni < D && nj >= 0 && nj < D && obs[ni][nj] == 0 && placed[ni][nj] == -1 && !vis[ni][nj]) {\n                    vis[ni][nj] = 1;\n                    qq.push({ni, nj});\n                    candidates.emplace_back(-dist[ni][nj], ni, nj); // Negative for max-heap effect\n                }\n            }\n        }\n        // Choose the candidate with max dist, then min i, min j\n        sort(candidates.begin(), candidates.end()); // Sorts by -dist, then i, then j\n        auto [_, pi, pj] = candidates[0];\n        placed[pi][pj] = t_d;\n        cout << pi << \" \" << pj << endl;\n    }\n\n    // Removal phase\n    vector<pair<int, int>> order;\n    while (true) {\n        // BFS to find accessible containers\n        vector<vector<int>> vis(D, vector<int>(D, 0));\n        queue<pair<int, int>> qq;\n        qq.push({entrance_i, entrance_j});\n        vis[entrance_i][entrance_j] = 1;\n        vector<pair<int, int>> acc; // {number, id} where id = i*D + j\n        while (!qq.empty()) {\n            auto [i, j] = qq.front();\n            qq.pop();\n            for (auto [di, dj] : dirs) {\n                int ni = i + di, nj = j + dj;\n                if (ni >= 0 && ni < D && nj >= 0 && nj < D && obs[ni][nj] == 0 && placed[ni][nj] == -1 && !vis[ni][nj]) {\n                    vis[ni][nj] = 1;\n                    qq.push({ni, nj});\n                    if (placed[ni][nj] != -1) {\n                        acc.emplace_back(placed[ni][nj], ni * D + nj);\n                    }\n                }\n            }\n        }\n        if (acc.empty()) break;\n        // Remove the smallest number\n        sort(acc.begin(), acc.end());\n        auto [num, id] = acc[0];\n        int ii = id / D, jj = id % D;\n        order.emplace_back(ii, jj);\n        placed[ii][jj] = -1;\n    }\n\n    // Output removal order\n    for (auto [qi, qj] : order) {\n        cout << qi << \" \" << qj << endl;\n    }\n\n    return 0;\n}","ahc024":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int n, m;\n    cin >> n >> m;\n    vector<vector<int>> c(n, vector<int>(n));\n    for(int i = 0; i < n; i++) {\n        for(int j = 0; j < n; j++) {\n            cin >> c[i][j];\n        }\n    }\n    \n    vector<vector<int>> adj(m + 1, vector<int>(m + 1, 0));\n    vector<int> is_boundary(m + 1, 0);\n    vector<int> di = {0, 1, 0, -1};\n    vector<int> dj = {1, 0, -1, 0};\n    \n    // Compute adjacencies and boundary info\n    for(int i = 0; i < n; i++) {\n        for(int j = 0; j < n; j++) {\n            int col = c[i][j];\n            if(i == 0 || i == n - 1 || j == 0 || j == n - 1) {\n                is_boundary[col] = 1;\n                adj[col][0] = adj[0][col] = 1;\n            }\n            for(int d = 0; d < 4; d++) {\n                int ni = i + di[d], nj = j + dj[d];\n                if(ni >= 0 && ni < n && nj >= 0 && nj < n) {\n                    int ncol = c[ni][nj];\n                    if(ncol != col) {\n                        adj[col][ncol] = adj[ncol][col] = 1;\n                    }\n                }\n            }\n        }\n    }\n    \n    // Grid for output\n    vector<vector<int>> grid(n, vector<int>(n, 0));\n    vector<pair<int, int>> pos(m + 1, {-1, -1});\n    vector<bool> used(n * n, false);\n    \n    // Sort wards by degree (number of adjacencies) descending\n    vector<pair<int, int>> order;\n    for(int cc = 1; cc <= m; cc++) {\n        int deg = 0;\n        for(int d = 0; d <= m; d++) {\n            deg += adj[cc][d];\n        }\n        order.emplace_back(-deg, cc);\n    }\n    sort(order.begin(), order.end());\n    \n    // Place wards\n    for(auto& p : order) {\n        int cc = p.second;\n        bool found = false;\n        for(int idx = 0; idx < n * n; idx++) {\n            int i = idx / n, j = idx % n;\n            if(used[idx]) continue;\n            bool on_bound = (i == 0 || i == n - 1 || j == 0 || j == n - 1);\n            if(is_boundary[cc] != on_bound) continue;\n            bool ok = true;\n            for(int d = 1; d <= m; d++) {\n                if(pos[d].first == -1) continue;\n                int pi = pos[d].first, pj = pos[d].second;\n                int dist = abs(i - pi) + abs(j - pj);\n                if(adj[cc][d]) {\n                    if(dist != 1) ok = false;\n                } else {\n                    if(dist == 1) ok = false;\n                }\n            }\n            if(ok) {\n                pos[cc] = {i, j};\n                used[idx] = true;\n                grid[i][j] = cc;\n                found = true;\n                break;\n            }\n        }\n        if(!found) {\n            // Fallback: place at first boundary-matching position\n            for(int idx = 0; idx < n * n; idx++) {\n                int i = idx / n, j = idx % n;\n                if(used[idx]) continue;\n                bool on_bound = (i == 0 || i == n - 1 || j == 0 || j == n - 1);\n                if(is_boundary[cc] == on_bound) {\n                    pos[cc] = {i, j};\n                    used[idx] = true;\n                    grid[i][j] = cc;\n                    break;\n                }\n            }\n        }\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    \n    return 0;\n}","ahc025":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int N, D, Q;\n    cin >> N >> D >> Q;\n    \n    vector<int> items(N);\n    iota(items.begin(), items.end(), 0);  // items[i] = i\n    \n    int query_count = 0;\n    \n    // Comparator for sorting: compares weights of two items using a query\n    auto cmp = [&](int a, int b) -> bool {\n        cout << 1 << \" \" << 1 << \" \" << a << \" \" << b << endl;\n        cout.flush();\n        string res;\n        cin >> res;\n        query_count++;\n        if (res == \"<\") return true;   // a < b\n        if (res == \">\") return false;  // a > b\n        return true;  // a == b, arbitrary\n    };\n    \n    // Sort items by weight (lightest to heaviest)\n    sort(items.begin(), items.end(), cmp);\n    \n    // Assign groups using round-robin on the sorted list\n    vector<int> group(N);\n    vector<vector<int>> groups(D);\n    vector<int> pos(N);\n    for (int i = 0; i < N; i++) {\n        pos[items[i]] = i;  // pos[item] = position in sorted list\n        int g = i % D;\n        group[items[i]] = g;\n        groups[g].push_back(items[i]);\n    }\n    \n    // Refinements: Balance groups using remaining queries\n    int g1 = 0, g2 = 1;\n    while (query_count < Q) {\n        if (groups[g1].empty() || groups[g2].empty()) {\n            // Skip if a group is empty; do dummy query\n            cout << 1 << \" \" << 1 << \" \" << 0 << \" \" << 1 << endl;\n            cout.flush();\n            string dummy;\n            cin >> dummy;\n            query_count++;\n            continue;\n        }\n        \n        // Compare sums of groups g1 and g2\n        cout << groups[g1].size() << \" \" << groups[g2].size();\n        for (int x : groups[g1]) cout << \" \" << x;\n        for (int x : groups[g2]) cout << \" \" << x;\n        cout << endl;\n        cout.flush();\n        string res;\n        cin >> res;\n        query_count++;\n        \n        if (res == \">\") {  // groups[g1] > groups[g2]\n            // Move heaviest item from g1 to g2\n            auto it = max_element(groups[g1].begin(), groups[g1].end(), [&](int a, int b) {\n                return pos[a] < pos[b];\n            });\n            int item = *it;\n            groups[g1].erase(it);\n            groups[g2].push_back(item);\n            group[item] = g2;\n        } else if (res == \"<\") {  // groups[g1] < groups[g2]\n            // Move heaviest item from g2 to g1\n            auto it = max_element(groups[g2].begin(), groups[g2].end(), [&](int a, int b) {\n                return pos[a] < pos[b];\n            });\n            int item = *it;\n            groups[g2].erase(it);\n            groups[g1].push_back(item);\n            group[item] = g1;\n        }  // If ==, do nothing\n        \n        // Cycle to next pair of groups\n        g2 = (g2 + 1) % D;\n        if (g2 == g1) g2 = (g2 + 1) % D;\n    }\n    \n    // Output the group assignment\n    for (int i = 0; i < N; i++) {\n        if (i > 0) cout << \" \";\n        cout << group[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    vector<vector<int>> stacks(m);\n    vector<int> position(n + 1);\n    for (int i = 0; i < m; i++) {\n        stacks[i].resize(n / m);\n        for (int j = 0; j < n / m; j++) {\n            cin >> stacks[i][j];\n            position[stacks[i][j]] = i;\n        }\n    }\n    vector<bool> removed(n + 1, false);\n    \n    for (int v = 1; v <= n; v++) {\n        if (removed[v]) continue;\n        int s = position[v];\n        // Find index of v in stacks[s]\n        int idx = -1;\n        for (int j = 0; j < stacks[s].size(); j++) {\n            if (stacks[s][j] == v) {\n                idx = j;\n                break;\n            }\n        }\n        int h = stacks[s].size() - 1 - idx; // Boxes above v\n        while (h > 0) {\n            // Move the top h boxes: Find the box at stacks[s][size - (h+1)]\n            int move_idx = stacks[s].size() - (h + 1);\n            int box_to_move = stacks[s][move_idx];\n            // Choose destination t: Stack with smallest size, not s\n            int min_size = INT_MAX;\n            int t = -1;\n            for (int i = 0; i < m; i++) {\n                if (i != s && (int)stacks[i].size() < min_size) {\n                    min_size = stacks[i].size();\n                    t = i;\n                }\n            }\n            // Perform move: Extract from move_idx to end\n            vector<int> to_move(stacks[s].begin() + move_idx, stacks[s].end());\n            stacks[s].resize(move_idx);\n            // Append to t\n            stacks[t].insert(stacks[t].end(), to_move.begin(), to_move.end());\n            // Update positions\n            for (int bx : to_move) {\n                position[bx] = t;\n            }\n            // Output move operation: box_to_move and t+1 (1-based)\n            cout << box_to_move << \" \" << (t + 1) << endl;\n            // Update current stack to t, h remains the same\n            s = t;\n        }\n        // Now h == 0, remove v\n        cout << v << \" \" << 0 << endl;\n        removed[v] = true;\n        // Remove from stack\n        stacks[s].pop_back();\n    }\n    return 0;\n}","ahc027":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int N;\n    cin >> N;\n    vector<string> h(N-1);\n    for(int i=0; i<N-1; i++) cin >> h[i];\n    vector<string> v(N);\n    for(int i=0; i<N; i++) cin >> v[i];\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    vector<vector<bool>> visited(N, vector<bool>(N, false));\n    vector<pair<int,int>> DIJ = {{0,1},{1,0},{0,-1},{-1,0}};\n    string DIR = \"RDLU\";\n    \n    function<void(int,int)> dfs = [&](int i, int j) {\n        visited[i][j] = true;\n        vector<tuple<int, int, int, int>> candidates; // d, dir, i2, j2\n        for(int dir=0; dir<4; dir++) {\n            int di = DIJ[dir].first, dj = DIJ[dir].second;\n            int i2 = i + di, j2 = j + dj;\n            if(i2 >= 0 && i2 < N && j2 >= 0 && j2 < N && !visited[i2][j2]) {\n                bool can = false;\n                if(di == 0) { // horizontal\n                    int jj = min(j, j2);\n                    if(v[i][jj] == '0') can = true;\n                } else { // vertical\n                    int ii = min(i, i2);\n                    if(h[ii][j] == '0') can = true;\n                }\n                if(can) {\n                    candidates.emplace_back(d[i2][j2], dir, i2, j2);\n                }\n            }\n        }\n        sort(candidates.rbegin(), candidates.rend()); // sort by d descending\n        for(auto& [_, dir, i2, j2] : candidates) {\n            cout << DIR[dir];\n            dfs(i2, j2);\n            cout << DIR[(dir + 2) % 4];\n        }\n    };\n    \n    dfs(0, 0);\n    cout << endl;\n    return 0;\n}","ahc028":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int N, M;\n    cin >> N >> M;\n    int si, sj;\n    cin >> si >> sj;\n    vector<vector<char>> A(N, vector<char>(N));\n    for (int i = 0; i < N; i++) {\n        string row;\n        cin >> row;\n        for (int j = 0; j < N; j++) {\n            A[i][j] = row[j];\n        }\n    }\n    vector<string> t(M);\n    for (int k = 0; k < M; k++) {\n        cin >> t[k];\n    }\n\n    // Precompute hashes for t_k\n    const unsigned long long BASE = 31;\n    vector<unsigned long long> t_hash(M);\n    for (int k = 0; k < M; k++) {\n        unsigned long long h = 0;\n        for (char c : t[k]) {\n            h = h * BASE + (c - 'A' + 1);\n        }\n        t_hash[k] = h;\n    }\n\n    // Initial state\n    int cur_i = si, cur_j = sj;\n    string S = \"\";\n    vector<bool> covered(M, false);\n    int covered_count = 0;\n    unordered_set<unsigned long long> hashes;\n    vector<pair<int, int>> operations;\n    long long total_cost = 0;\n\n    // Main loop\n    while (operations.size() < 5000 && covered_count < M) {\n        int best_new = -1;\n        long long best_cost = LLONG_MAX;\n        pair<int, int> best_pos = {-1, -1};\n\n        for (int ii = 0; ii < N; ii++) {\n            for (int jj = 0; jj < N; jj++) {\n                long long cost = abs(ii - cur_i) + abs(jj - cur_j) + 1;\n                string temp_S = S + A[ii][jj];\n\n                // Compute new hash if possible\n                unordered_set<unsigned long long> temp_hashes = hashes;\n                if (temp_S.size() >= 5) {\n                    string sub = temp_S.substr(temp_S.size() - 5, 5);\n                    unsigned long long h = 0;\n                    for (char c : sub) {\n                        h = h * BASE + (c - 'A' + 1);\n                    }\n                    temp_hashes.insert(h);\n                }\n\n                // Count new covered\n                int new_cov = 0;\n                for (int k = 0; k < M; k++) {\n                    if (!covered[k] && temp_hashes.count(t_hash[k])) {\n                        new_cov++;\n                    }\n                }\n\n                // Update best\n                if (new_cov > best_new || (new_cov == best_new && cost < best_cost)) {\n                    best_new = new_cov;\n                    best_cost = cost;\n                    best_pos = {ii, jj};\n                }\n            }\n        }\n\n        // If no valid move, break (shouldn't happen)\n        if (best_pos.first == -1) break;\n\n        // Perform the move\n        int ii = best_pos.first, jj = best_pos.second;\n        operations.push_back({ii, jj});\n        total_cost += best_cost;\n        S += A[ii][jj];\n        cur_i = ii;\n        cur_j = jj;\n\n        // Update hashes\n        if (S.size() >= 5) {\n            string sub = S.substr(S.size() - 5, 5);\n            unsigned long long h = 0;\n            for (char c : sub) {\n                h = h * BASE + (c - 'A' + 1);\n            }\n            hashes.insert(h);\n        }\n\n        // Update covered\n        for (int k = 0; k < M; k++) {\n            if (!covered[k] && hashes.count(t_hash[k])) {\n                covered[k] = true;\n                covered_count++;\n            }\n        }\n    }\n\n    // Output\n    for (auto& op : operations) {\n        cout << op.first << \" \" << op.second << endl;\n    }\n\n    return 0;\n}","ahc030":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int N, M;\n    double eps;\n    cin >> N >> M >> eps;\n    for (int k = 0; k < M; ++k) {\n        int d;\n        cin >> d;\n        for (int i = 0; i < d; ++i) {\n            int x, y;\n            cin >> x >> y;\n        }\n    }\n\n    vector<pair<int, int>> oil;\n    const int THRESHOLD = 10;\n\n    function<void(vector<pair<int, int>>)> process = [&](vector<pair<int, int>> reg) {\n        if (reg.empty()) return;\n        int sz = reg.size();\n        if (sz <= THRESHOLD) {\n            for (auto [i, j] : reg) {\n                cout << \"q 1 \" << i << \" \" << j << endl;\n                cout.flush();\n                int v;\n                cin >> v;\n                if (v > 0) oil.push_back({i, j});\n            }\n            return;\n        }\n        // divine\n        cout << \"q \" << sz;\n        for (auto [i, j] : reg) cout << \" \" << i << \" \" << j;\n        cout << endl;\n        cout.flush();\n        int s;\n        cin >> s;\n        if (s == 0) return;\n        // subdivide\n        int min_i = INT_MAX, max_i = INT_MIN, min_j = INT_MAX, max_j = INT_MIN;\n        for (auto [i, j] : reg) {\n            min_i = min(min_i, i);\n            max_i = max(max_i, i);\n            min_j = min(min_j, j);\n            max_j = max(max_j, j);\n        }\n        int mid_i = (min_i + max_i) / 2;\n        int mid_j = (min_j + max_j) / 2;\n        vector<pair<int, int>> tl, tr, bl, br;\n        for (auto p : reg) {\n            int i = p.first, j = p.second;\n            if (i <= mid_i) {\n                if (j <= mid_j) tl.push_back(p);\n                else tr.push_back(p);\n            } else {\n                if (j <= mid_j) bl.push_back(p);\n                else br.push_back(p);\n            }\n        }\n        process(tl);\n        process(tr);\n        process(bl);\n        process(br);\n    };\n\n    vector<pair<int, int>> all;\n    for (int i = 0; i < N; ++i) {\n        for (int j = 0; j < N; ++j) {\n            all.push_back({i, j});\n        }\n    }\n    process(all);\n\n    cout << \"a \" << oil.size();\n    for (auto [i, j] : oil) cout << \" \" << i << \" \" << j;\n    cout << endl;\n    cout.flush();\n    int res;\n    cin >> res;\n    assert(res == 1);\n    return 0;\n}","ahc031":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int W, D, N;\n    cin >> W >> D >> N;\n    vector<vector<long long>> a(D, vector<long long>(N));\n    for (int d = 0; d < D; ++d) {\n        for (int k = 0; k < N; ++k) {\n            cin >> a[d][k];\n        }\n    }\n    \n    for (int d = 0; d < D; ++d) {\n        // Sort a[d] in descending order\n        sort(a[d].rbegin(), a[d].rend());\n        int current_i = 0;\n        for (int k = 0; k < N; ++k) {\n            long long area_needed = a[d][k];\n            // Compute height: ceil(area_needed / W)\n            int height = (area_needed + W - 1) / W;\n            int i0 = current_i;\n            int j0 = 0;\n            int i1 = current_i + height;\n            int j1 = W;\n            // Ensure i1 <= W (though sum heights <= 1000)\n            if (i1 > W) {\n                // This shouldn't happen, but just in case\n                i1 = W;\n            }\n            cout << i0 << \" \" << j0 << \" \" << i1 << \" \" << j1 << \"\\n\";\n            current_i += height;\n        }\n    }\n    return 0;\n}","ahc032":"#include <bits/stdc++.h>\n\nusing namespace std;\nusing ll = long long;\nconst ll MOD = 998244353LL;\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<ll>> a(N, vector<ll>(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<ll>>> s(M, vector<vector<ll>>(3, vector<ll>(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 >> s[m][i][j];\n            }\n        }\n    }\n    \n    // Initialize current_mod as a[i][j] % MOD\n    vector<vector<ll>> current_mod(N, vector<ll>(N));\n    for (int i = 0; i < N; ++i) {\n        for (int j = 0; j < N; ++j) {\n            current_mod[i][j] = a[i][j] % MOD;\n        }\n    }\n    \n    // Store operations as (m, p, q)\n    vector<tuple<int, int, int>> operations;\n    \n    // Perform up to K operations\n    for (int op = 0; op < K; ++op) {\n        ll best_delta = LLONG_MIN;\n        int best_m = -1, best_p = -1, best_q = -1;\n        \n        // Evaluate all possible operations\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                    ll delta = 0;\n                    for (int i = 0; i < 3; ++i) {\n                        for (int j = 0; j < 3; ++j) {\n                            ll old_mod = current_mod[p + i][q + j];\n                            ll s_val = s[m][i][j];\n                            ll new_mod = (old_mod + s_val) % MOD;\n                            ll cell_delta = (new_mod - old_mod + MOD) % MOD;\n                            delta += cell_delta;\n                        }\n                    }\n                    if (delta > best_delta) {\n                        best_delta = delta;\n                        best_m = m;\n                        best_p = p;\n                        best_q = q;\n                    }\n                }\n            }\n        }\n        \n        // If no positive delta, stop\n        if (best_delta <= 0) break;\n        \n        // Apply the best operation\n        operations.emplace_back(best_m, best_p, best_q);\n        for (int i = 0; i < 3; ++i) {\n            for (int j = 0; j < 3; ++j) {\n                current_mod[best_p + i][best_q + j] = (current_mod[best_p + i][best_q + j] + s[best_m][i][j]) % MOD;\n            }\n        }\n    }\n    \n    // Output the result\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\nint main() {\n    int N;\n    cin >> N;\n    vector<vector<int>> A(N, vector<int>(N));\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cin >> A[i][j];\n        }\n    }\n    \n    // Initialize receiving queues\n    vector<queue<int>> recv_queues(N);\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            recv_queues[i].push(A[i][j]);\n        }\n    }\n    \n    // Grid: -1 no container, else container id\n    vector<vector<int>> grid(N, vector<int>(N, -1));\n    \n    // Crane positions\n    vector<pair<int, int>> crane_pos(N);\n    for (int i = 0; i < N; i++) {\n        crane_pos[i] = {i, 0};\n    }\n    \n    // Crane holdings: -1 none, else id\n    vector<int> crane_holding(N, -1);\n    \n    // Crane active\n    vector<bool> crane_active(N, true);\n    \n    // Dispatched lists\n    vector<vector<int>> dispatched(N);\n    \n    // Actions\n    vector<string> actions(N);\n    \n    // Simulation\n    const int MAX_TURNS = 10000;\n    for (int turn = 0; turn < MAX_TURNS; turn++) {\n        // Step 1: Receive\n        for (int i = 0; i < N; i++) {\n            int x = i, y = 0;\n            if (grid[x][y] == -1 && crane_holding[i] == -1 && !recv_queues[i].empty()) {\n                int cont = recv_queues[i].front();\n                recv_queues[i].pop();\n                grid[x][y] = cont;\n            }\n        }\n        \n        // Step 2: Compute actions\n        vector<char> acts(N, '.');\n        for (int c = 0; c < N; c++) {\n            if (!crane_active[c]) continue;\n            \n            // Compute goal\n            pair<int, int> goal;\n            if (crane_holding[c] != -1) {\n                int cont = crane_holding[c];\n                int target_row = cont / N;\n                if (target_row == c) {\n                    goal = {c, N - 1};\n                } else {\n                    goal = {target_row, 1};  // Buffer column\n                }\n            } else {\n                goal = {c, 0};\n            }\n            \n            // BFS for next move\n            auto get_next_move = [&](int cx, int cy, pair<int, int> g) -> char {\n                vector<vector<int>> dist(N, vector<int>(N, -1));\n                queue<pair<int, int>> q;\n                q.push({cx, cy});\n                dist[cx][cy] = 0;\n                vector<vector<pair<int, int>>> prev(N, vector<pair<int, int>>(N, {-1, -1}));\n                while (!q.empty()) {\n                    auto [x, y] = q.front();\n                    q.pop();\n                    if (x == g.first && y == g.second) break;\n                    vector<pair<int, int>> dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};\n                    for (auto [dx, dy] : dirs) {\n                        int nx = x + dx, ny = y + dy;\n                        if (nx >= 0 && nx < N && ny >= 0 && ny < N) {\n                            if (dist[nx][ny] == -1) {\n                                bool can = true;\n                                if (crane_holding[c] != -1 && c >= 1) {  // Small crane\n                                    if (grid[nx][ny] != -1) can = false;\n                                }\n                                // Check other cranes (simplified: assume no collision in BFS)\n                                for (int o = 0; o < N; o++) {\n                                    if (o != c && crane_pos[o] == make_pair(nx, ny)) can = false;\n                                }\n                                if (can) {\n                                    dist[nx][ny] = dist[x][y] + 1;\n                                    prev[nx][ny] = {x, y};\n                                    q.push({nx, ny});\n                                }\n                            }\n                        }\n                    }\n                }\n                if (dist[g.first][g.second] == -1) return '.';\n                pair<int, int> current = g;\n                while (prev[current.first][current.second] != make_pair(cx, cy)) {\n                    current = prev[current.first][current.second];\n                }\n                int dx = current.first - cx;\n                int dy = current.second - cy;\n                if (dx == -1) return 'U';\n                if (dx == 1) return 'D';\n                if (dy == -1) return 'L';\n                if (dy == 1) return 'R';\n                return '.';\n            };\n            \n            char act = get_next_move(crane_pos[c].first, crane_pos[c].second, goal);\n            \n            // Prefer P/Q if possible\n            int cx = crane_pos[c].first, cy = crane_pos[c].second;\n            if (crane_holding[c] == -1 && grid[cx][cy] != -1) {\n                act = 'P';\n            } else if (crane_holding[c] != -1 && grid[cx][cy] == -1) {\n                act = 'Q';\n            }\n            \n            acts[c] = act;\n        }\n        \n        // Execute actions with validation\n        for (int c = 0; c < N; c++) {\n            char act = acts[c];\n            int cx = crane_pos[c].first, cy = crane_pos[c].second;\n            bool valid = true;\n            if (act == 'P') {\n                if (crane_holding[c] != -1 || grid[cx][cy] == -1) valid = false;\n                else {\n                    crane_holding[c] = grid[cx][cy];\n                    grid[cx][cy] = -1;\n                }\n            } else if (act == 'Q') {\n                if (crane_holding[c] == -1 || grid[cx][cy] != -1) valid = false;\n                else {\n                    grid[cx][cy] = crane_holding[c];\n                    crane_holding[c] = -1;\n                }\n            } else if (act == 'U') {\n                int nx = cx - 1, ny = cy;\n                if (nx < 0) valid = false;\n                else {\n                    if (crane_holding[c] != -1 && c >= 1 && grid[nx][ny] != -1) valid = false;\n                    for (int o = 0; o < N; o++) {\n                        if (o != c && crane_pos[o] == make_pair(nx, ny)) valid = false;\n                    }\n                    if (valid) crane_pos[c] = {nx, ny};\n                }\n            } else if (act == 'D') {\n                int nx = cx + 1, ny = cy;\n                if (nx >= N) valid = false;\n                else {\n                    if (crane_holding[c] != -1 && c >= 1 && grid[nx][ny] != -1) valid = false;\n                    for (int o = 0; o < N; o++) {\n                        if (o != c && crane_pos[o] == make_pair(nx, ny)) valid = false;\n                    }\n                    if (valid) crane_pos[c] = {nx, ny};\n                }\n            } else if (act == 'L') {\n                int nx = cx, ny = cy - 1;\n                if (ny < 0) valid = false;\n                else {\n                    if (crane_holding[c] != -1 && c >= 1 && grid[nx][ny] != -1) valid = false;\n                    for (int o = 0; o < N; o++) {\n                        if (o != c && crane_pos[o] == make_pair(nx, ny)) valid = false;\n                    }\n                    if (valid) crane_pos[c] = {nx, ny};\n                }\n            } else if (act == 'R') {\n                int nx = cx, ny = cy + 1;\n                if (ny >= N) valid = false;\n                else {\n                    if (crane_holding[c] != -1 && c >= 1 && grid[nx][ny] != -1) valid = false;\n                    for (int o = 0; o < N; o++) {\n                        if (o != c && crane_pos[o] == make_pair(nx, ny)) valid = false;\n                    }\n                    if (valid) crane_pos[c] = {nx, ny};\n                }\n            } else if (act == 'B') {\n                if (crane_holding[c] != -1) valid = false;\n                else crane_active[c] = false;\n            }\n            if (!valid) act = '.';\n            actions[c] += act;\n        }\n        \n        // Step 3: Dispatch\n        for (int i = 0; i < N; i++) {\n            int x = i, y = N - 1;\n            if (grid[x][y] != -1) {\n                int cont = grid[x][y];\n                grid[x][y] = -1;\n                dispatched[i].push_back(cont);\n            }\n        }\n        \n        // Check if done\n        bool done = true;\n        for (int i = 0; i < N; i++) {\n            if (!recv_queues[i].empty()) done = false;\n            if (grid[i][N - 1] != -1) done = false;\n            if (crane_holding[i] != -1) done = false;\n        }\n        if (done) break;\n    }\n    \n    // Output: Pad to max length\n    size_t max_len = 0;\n    for (const auto& s : actions) max_len = max(max_len, s.size());\n    for (auto& s : actions) {\n        while (s.size() < max_len) s += '.';\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>> h(N, vector<int>(N));\n    for (int i = 0; i < N; ++i) {\n        for (int j = 0; j < N; ++j) {\n            cin >> h[i][j];\n        }\n    }\n\n    // Directions: U, D, L, R\n    vector<pair<int, int>> dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};\n    string move_str = \"UDLR\";\n\n    // Priority queues: excess (max h), deficit (max -h)\n    priority_queue<tuple<int, int, int>> pq_excess; // {h, i, j}\n    priority_queue<tuple<int, int, int>> pq_deficit; // {-h, i, j}\n    for (int i = 0; i < N; ++i) {\n        for (int j = 0; j < N; ++j) {\n            if (h[i][j] > 0) pq_excess.push({h[i][j], i, j});\n            if (h[i][j] < 0) pq_deficit.push({-h[i][j], i, j});\n        }\n    }\n\n    // Function to get path from (si,sj) to (ti,tj) using BFS\n    auto get_path = [&](int si, int sj, int ti, int tj) -> vector<string> {\n        vector<vector<int>> dist(N, vector<int>(N, -1));\n        vector<vector<pair<int, int>>> prevv(N, vector<pair<int, int>>(N, {-1, -1}));\n        queue<pair<int, int>> q;\n        q.push({si, sj});\n        dist[si][sj] = 0;\n        while (!q.empty()) {\n            auto [i, j] = q.front(); q.pop();\n            if (i == ti && j == tj) break;\n            for (int d = 0; d < 4; ++d) {\n                int ni = i + dirs[d].first, nj = j + dirs[d].second;\n                if (ni >= 0 && ni < N && nj >= 0 && nj < N && dist[ni][nj] == -1) {\n                    dist[ni][nj] = dist[i][j] + 1;\n                    prevv[ni][nj] = {i, j};\n                    q.push({ni, nj});\n                }\n            }\n        }\n        // Reconstruct path\n        vector<string> path;\n        int ci = ti, cj = tj;\n        while (ci != si || cj != sj) {\n            auto [pi, pj] = prevv[ci][cj];\n            for (int d = 0; d < 4; ++d) {\n                if (pi + dirs[d].first == ci && pj + dirs[d].second == cj) {\n                    path.push_back(string(1, move_str[d]));\n                    break;\n                }\n            }\n            ci = pi; cj = pj;\n        }\n        reverse(path.begin(), path.end());\n        return path;\n    };\n\n    // Function to find nearest deficit from (si,sj) and get path\n    auto find_nearest_deficit = [&](int si, int sj) -> pair<pair<int, int>, vector<string>> {\n        vector<vector<int>> dist(N, vector<int>(N, -1));\n        vector<vector<pair<int, int>>> prevv(N, vector<pair<int, int>>(N, {-1, -1}));\n        queue<pair<int, int>> q;\n        q.push({si, sj});\n        dist[si][sj] = 0;\n        pair<int, int> target = {-1, -1};\n        while (!q.empty() && target.first == -1) {\n            auto [x, y] = q.front(); q.pop();\n            if (h[x][y] < 0) {\n                target = {x, y};\n                break;\n            }\n            for (auto& dir : dirs) {\n                int nx = x + dir.first, ny = y + dir.second;\n                if (nx >= 0 && nx < N && ny >= 0 && ny < N && dist[nx][ny] == -1) {\n                    dist[nx][ny] = dist[x][y] + 1;\n                    prevv[nx][ny] = {x, y};\n                    q.push({nx, ny});\n                }\n            }\n        }\n        if (target.first == -1) return {{ -1, -1 }, {}};\n        // Reconstruct path to target\n        vector<string> path;\n        int ci = target.first, cj = target.second;\n        while (ci != si || cj != sj) {\n            auto [pi, pj] = prevv[ci][cj];\n            for (int d = 0; d < 4; ++d) {\n                if (pi + dirs[d].first == ci && pj + dirs[d].second == cj) {\n                    path.push_back(string(1, move_str[d]));\n                    break;\n                }\n            }\n            ci = pi; cj = pj;\n        }\n        reverse(path.begin(), path.end());\n        return {target, path};\n    };\n\n    int ci = 0, cj = 0; // Current position\n    vector<string> operations;\n\n    while (!pq_excess.empty()) {\n        // Get largest excess\n        auto [val, i, j] = pq_excess.top(); pq_excess.pop();\n        if (h[i][j] <= 0) continue; // Already processed\n\n        // Move to excess cell\n        auto path_to_excess = get_path(ci, cj, i, j);\n        for (auto& s : path_to_excess) operations.push_back(s);\n        ci = i; cj = j;\n\n        // Load all soil\n        int d = h[i][j];\n        operations.push_back(\"+\" + to_string(d));\n        h[i][j] = 0;\n\n        // Find and move to nearest deficits until d is 0\n        while (d > 0) {\n            auto [target, path_to_deficit] = find_nearest_deficit(ci, cj);\n            if (target.first == -1) break; // No deficit (shouldn't happen)\n            int p = target.first, q = target.second;\n\n            // Move to deficit\n            for (auto& s : path_to_deficit) operations.push_back(s);\n            ci = p; cj = q;\n\n            // Unload as much as possible\n            int unload = min(d, -h[p][q]);\n            operations.push_back(\"-\" + to_string(unload));\n            h[p][q] += unload;\n            d -= unload;\n        }\n    }\n\n    // Output all operations\n    for (auto& op : operations) {\n        cout << op << endl;\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    int seed_count = 2 * N * (N - 1);\n    vector<vector<int>> X(seed_count, vector<int>(M));\n    \n    // Read initial seeds\n    for (int i = 0; i < seed_count; i++) {\n        for (int j = 0; j < M; j++) {\n            cin >> X[i][j];\n        }\n    }\n    \n    for (int t = 0; t < T; t++) {\n        // Compute V_k for each seed and prepare for sorting\n        vector<pair<int, int>> seeds;  // Pair: (-V_k, index) for descending sort\n        for (int i = 0; i < seed_count; i++) {\n            int v = 0;\n            for (int j = 0; j < M; j++) {\n                v += X[i][j];\n            }\n            seeds.push_back({-v, i});  // Negative V_k for descending order\n        }\n        \n        // Sort seeds by V_k descending (top 36 will be first)\n        sort(seeds.begin(), seeds.end());\n        \n        // Create planting grid: Assign top 36 seeds in row-major order\n        vector<vector<int>> A(N, vector<int>(N));\n        int idx = 0;\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                A[i][j] = seeds[idx].second;  // Seed index\n                idx++;\n            }\n        }\n        \n        // Output the planting grid\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                cout << A[i][j];\n                if (j < N - 1) cout << \" \";\n                else cout << endl;\n            }\n        }\n        cout.flush();  // Important for interactive problems\n        \n        // Read new seeds after the operation\n        for (int i = 0; i < seed_count; i++) {\n            for (int j = 0; j < M; j++) {\n                cin >> X[i][j];\n            }\n        }\n    }\n    \n    return 0;\n}","ahc038":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int N, M, V;\n    cin >> N >> M >> V;\n    vector<string> s(N), t(N);\n    for(int i = 0; i < N; i++) cin >> s[i];\n    for(int i = 0; i < N; i++) cin >> t[i];\n    \n    vector<pair<int,int>> starts, targets;\n    for(int i = 0; i < N; i++) {\n        for(int j = 0; j < N; j++) {\n            if(s[i][j] == '1') starts.emplace_back(i, j);\n            if(t[i][j] == '1') targets.emplace_back(i, j);\n        }\n    }\n    sort(starts.begin(), starts.end());\n    sort(targets.begin(), targets.end());\n    \n    // Design the tree: simple arm with one fingertip\n    cout << 2 << endl;\n    cout << 0 << \" \" << 1 << endl;\n    cout << 0 << \" \" << 0 << endl;\n    \n    // Directions: 0:right, 1:down, 2:left, 3:up\n    vector<pair<int,int>> dirs = {{0,1}, {1,0}, {0,-1}, {-1,0}};\n    \n    int x = 0, y = 0, dir = 0;\n    \n    for(int i = 0; i < M; i++) {\n        auto [a, b] = starts[i];\n        auto [c, d] = targets[i];\n        \n        // Choose best d for picking\n        int best_dist = 1e9;\n        int best_d = -1, best_rx = -1, best_ry = -1;\n        for(int dd = 0; dd < 4; dd++) {\n            auto [dx, dy] = dirs[dd];\n            int rx = a - dx, ry = b - dy;\n            if(rx >= 0 && rx < N && ry >= 0 && ry < N) {\n                int dist = abs(rx - x) + abs(ry - y);\n                if(dist < best_dist) {\n                    best_dist = dist;\n                    best_d = dd;\n                    best_rx = rx;\n                    best_ry = ry;\n                }\n            }\n        }\n        \n        // Rotate to best_d\n        int diff = (best_d - dir + 4) % 4;\n        string rot;\n        if(diff <= 2) {\n            for(int k = 0; k < diff; k++) rot += 'R';\n        } else {\n            for(int k = 0; k < 4 - diff; k++) rot += 'L';\n        }\n        for(char r : rot) {\n            string S = \".\" + string(1, r) + \".\" + \".\";\n            cout << S << endl;\n            if(r == 'R') dir = (dir + 1) % 4;\n            else dir = (dir - 1 + 4) % 4;\n        }\n        \n        // Move to best_rx, best_ry\n        string moves;\n        int dx = best_rx - x;\n        if(dx > 0) for(int k = 0; k < dx; k++) moves += 'R';\n        else if(dx < 0) for(int k = 0; k < -dx; k++) moves += 'L';\n        int dy = best_ry - y;\n        if(dy > 0) for(int k = 0; k < dy; k++) moves += 'D';\n        else if(dy < 0) for(int k = 0; k < -dy; k++) moves += 'U';\n        for(size_t k = 0; k < moves.size(); k++) {\n            string S = string(1, moves[k]) + \".\" + \".\" + \".\";\n            if(k == moves.size() - 1) S[3] = 'P';\n            cout << S << endl;\n        }\n        x = best_rx;\n        y = best_ry;\n        \n        // Choose best e for placing\n        best_dist = 1e9;\n        int best_e = -1, best_rx2 = -1, best_ry2 = -1;\n        for(int ee = 0; ee < 4; ee++) {\n            auto [ex, ey] = dirs[ee];\n            int rx2 = c - ex, ry2 = d - ey;\n            if(rx2 >= 0 && rx2 < N && ry2 >= 0 && ry2 < N) {\n                int dist = abs(rx2 - x) + abs(ry2 - y);\n                if(dist < best_dist) {\n                    best_dist = dist;\n                    best_e = ee;\n                    best_rx2 = rx2;\n                    best_ry2 = ry2;\n                }\n            }\n        }\n        \n        // Rotate to best_e\n        diff = (best_e - dir + 4) % 4;\n        rot = \"\";\n        if(diff <= 2) {\n            for(int k = 0; k < diff; k++) rot += 'R';\n        } else {\n            for(int k = 0; k < 4 - diff; k++) rot += 'L';\n        }\n        for(char r : rot) {\n            string S = \".\" + string(1, r) + \".\" + \".\";\n            cout << S << endl;\n            if(r == 'R') dir = (dir + 1) % 4;\n            else dir = (dir - 1 + 4) % 4;\n        }\n        \n        // Move to best_rx2, best_ry2\n        moves = \"\";\n        dx = best_rx2 - x;\n        if(dx > 0) for(int k = 0; k < dx; k++) moves += 'R';\n        else if(dx < 0) for(int k = 0; k < -dx; k++) moves += 'L';\n        dy = best_ry2 - y;\n        if(dy > 0) for(int k = 0; k < dy; k++) moves += 'D';\n        else if(dy < 0) for(int k = 0; k < -dy; k++) moves += 'U';\n        for(size_t k = 0; k < moves.size(); k++) {\n            string S = string(1, moves[k]) + \".\" + \".\" + \".\";\n            if(k == moves.size() - 1) S[3] = 'P';\n            cout << S << endl;\n        }\n        x = best_rx2;\n        y = best_ry2;\n    }\n    \n    return 0;\n}","ahc039":"#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n\nint main() {\n  ios::sync_with_stdio(false);\n  cin.tie(nullptr);\n\n  int N;\n  cin >> N;\n  vector<tuple<ll, ll, int>> pts;\n  for (int i = 0; i < 2 * N; ++i) {\n    ll x, y;\n    cin >> x >> y;\n    int w = (i < N ? 1 : -1);\n    pts.emplace_back(x, y, w);\n  }\n\n  sort(pts.begin(), pts.end());\n\n  map<ll, int> y_to_weight;\n  ll current_min_x = LLONG_MAX / 2;\n  ll best_score = LLONG_MIN / 2;\n  ll best_left = -1, best_right = -1, best_bottom = -1, best_top = -1;\n\n  for (auto &[x, y, w] : pts) {\n    y_to_weight[y] = w;\n    current_min_x = min(current_min_x, x);\n\n    vector<ll> ylist;\n    ll min_p = 0;\n    int min_idx = 0;\n    ll local_max = LLONG_MIN / 2;\n    int best_start = 0, best_end = -1;\n    ll cum = 0;\n    int idx = 0;\n    for (auto &p : y_to_weight) {\n      ylist.push_back(p.first);\n      cum += p.second;\n      ll sum_val = cum - min_p;\n      if (sum_val > local_max) {\n        local_max = sum_val;\n        best_start = min_idx;\n        best_end = idx;\n      }\n      if (cum < min_p) {\n        min_p = cum;\n        min_idx = idx + 1;\n      }\n      ++idx;\n    }\n\n    if (best_left < best_right && best_bottom < best_top && local_max > best_score) {\n      best_score = local_max;\n      best_left = current_min_x;\n      best_right = x;\n      best_bottom = ylist[best_start];\n      best_top = ylist[best_end];\n    }\n  }\n\n  // If no valid rectangle found, output a small one with score 0\n  if (best_score <= LLONG_MIN / 2 + 1) {\n    best_left = 0;\n    best_right = 1;\n    best_bottom = 0;\n    best_top = 1;\n  }\n\n  vector<pair<ll, ll>> poly = {\n    {best_left, best_bottom},\n    {best_left, best_top},\n    {best_right, best_top},\n    {best_right, best_bottom}\n  };\n\n  cout << 4 << '\\n';\n  for (auto &[x, y] : poly) {\n    cout << x << ' ' << y << '\\n';\n  }\n\n  return 0;\n}","ahc040":"#include <bits/stdc++.h>\n\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int N, T, sigma;\n    cin >> N >> T >> sigma;\n    vector<long long> w(N), h(N);\n    for (int i = 0; i < N; ++i) {\n        cin >> w[i] >> h[i];\n    }\n    \n    // Define modes: 0=alternate U/L with smart r/b, 1-4=fixed for baselines\n    vector<int> modes = {0, 1, 2, 3, 4};\n    \n    for (int t = 0; t < T; ++t) {\n        int mode = modes[t % modes.size()];\n        vector<tuple<int, int, char, int>> prdb;\n        \n        for (int i = 0; i < N; ++i) {\n            int p = i;\n            int r;\n            char d;\n            int b;\n            \n            if (mode == 0) {\n                // Alternate U/L, b=previous, r optimized for direction\n                d = (i % 2 == 0) ? 'U' : 'L';\n                b = (i == 0) ? -1 : i - 1;\n                if (d == 'U') {\n                    r = (h[i] > w[i]) ? 1 : 0;  // Rotate if taller to prioritize width\n                } else {\n                    r = (w[i] > h[i]) ? 1 : 0;  // Rotate if wider to prioritize height\n                }\n            } else if (mode == 1) {\n                d = 'U';\n                b = -1;\n                r = 0;\n            } else if (mode == 2) {\n                d = 'L';\n                b = -1;\n                r = 0;\n            } else if (mode == 3) {\n                d = 'U';\n                b = -1;\n                r = 1;\n            } else if (mode == 4) {\n                d = 'L';\n                b = -1;\n                r = 1;\n            }\n            \n            prdb.emplace_back(p, r, d, b);\n        }\n        \n        cout << N << '\\n';\n        for (auto [p, r, d, b] : prdb) {\n            cout << p << ' ' << r << ' ' << d << ' ' << b << '\\n';\n        }\n        cout << flush;\n        \n        long long Wp, Hp;\n        cin >> Wp >> Hp;\n        // Future: Use Wp, Hp to adapt modes (e.g., if Wp > Hp, favor 'L')\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(0);\n    int N, M, H;\n    cin >> N >> M >> H;\n    vector<int> A(N);\n    for (int i = 0; i < N; ++i) {\n        cin >> A[i];\n    }\n    vector<vector<int>> adj(N);\n    for (int i = 0; i < M; ++i) {\n        int u, v;\n        cin >> u >> v;\n        adj[u].push_back(v);\n        adj[v].push_back(u);\n    }\n    // Skip coordinates as not needed for computation\n    for (int i = 0; i < N; ++i) {\n        int x, y;\n        cin >> x >> y;\n    }\n    // Greedy assignment\n    vector<int> d(N, -1);\n    vector<int> p(N, -1);\n    vector<pair<int, int>> verts;\n    for (int i = 0; i < N; ++i) {\n        verts.push_back({-A[i], i}); // Sort by decreasing A\n    }\n    sort(verts.begin(), verts.end());\n    for (auto [_, v] : verts) {\n        if (d[v] != -1) continue;\n        int best_u = -1;\n        int max_du = -1;\n        for (int u : adj[v]) {\n            if (d[u] != -1 && d[u] < H && d[u] > max_du) {\n                max_du = d[u];\n                best_u = u;\n            }\n        }\n        if (best_u != -1) {\n            p[v] = best_u;\n            d[v] = max_du + 1;\n        } else {\n            p[v] = -1;\n            d[v] = 0;\n        }\n    }\n    // Output\n    for (int i = 0; i < N; ++i) {\n        if (i > 0) cout << \" \";\n        cout << p[i];\n    }\n    cout << \"\\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> board(N);\n    for (int i = 0; i < N; i++) {\n        cin >> board[i];\n    }\n    vector<pair<int, int>> oni;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (board[i][j] == 'x') {\n                oni.emplace_back(i, j);\n            }\n        }\n    }\n    vector<pair<char, int>> all_ops;\n    for (auto [i, j] : oni) {\n        // Check directions\n        bool can_up = true;\n        for (int k = 0; k < i; k++) {\n            if (board[k][j] == 'o') can_up = false;\n        }\n        int cost_up = can_up ? 2 * (i + 1) : 1e9;\n\n        bool can_down = true;\n        for (int k = i + 1; k < N; k++) {\n            if (board[k][j] == 'o') can_down = false;\n        }\n        int cost_down = can_down ? 2 * (N - i) : 1e9;\n\n        bool can_left = true;\n        for (int k = 0; k < j; k++) {\n            if (board[i][k] == 'o') can_left = false;\n        }\n        int cost_left = can_left ? 2 * (j + 1) : 1e9;\n\n        bool can_right = true;\n        for (int k = j + 1; k < N; k++) {\n            if (board[i][k] == 'o') can_right = false;\n        }\n        int cost_right = can_right ? 2 * (N - j) : 1e9;\n\n        // Find best direction\n        vector<tuple<int, char, int>> candidates;\n        if (can_up) candidates.emplace_back(cost_up, 'U', j);\n        if (can_down) candidates.emplace_back(cost_down, 'D', j);\n        if (can_left) candidates.emplace_back(cost_left, 'L', i);\n        if (can_right) candidates.emplace_back(cost_right, 'R', i);\n        sort(candidates.begin(), candidates.end());\n        auto [cost, dir, idx] = candidates[0];\n\n        // Generate operations\n        vector<pair<char, int>> ops;\n        if (dir == 'U') {\n            for (int t = 0; t < i + 1; t++) ops.emplace_back('U', idx);\n            for (int t = 0; t < i + 1; t++) ops.emplace_back('D', idx);\n        } else if (dir == 'D') {\n            for (int t = 0; t < N - i; t++) ops.emplace_back('D', idx);\n            for (int t = 0; t < N - i; t++) ops.emplace_back('U', idx);\n        } else if (dir == 'L') {\n            for (int t = 0; t < j + 1; t++) ops.emplace_back('L', idx);\n            for (int t = 0; t < j + 1; t++) ops.emplace_back('R', idx);\n        } else if (dir == 'R') {\n            for (int t = 0; t < N - j; t++) ops.emplace_back('R', idx);\n            for (int t = 0; t < N - j; t++) ops.emplace_back('L', idx);\n        }\n        // Append to all_ops\n        all_ops.insert(all_ops.end(), ops.begin(), ops.end());\n    }\n    // Output\n    for (auto [d, p] : all_ops) {\n        cout << d << \" \" << p << \"\\n\";\n    }\n    return 0;\n}","ahc044":"#include <bits/stdc++.h>\n#include <random>\n#include <chrono>\n\nusing namespace std;\n\nint main() {\n    const int N = 100;\n    const int L = 500000;\n    vector<long long> T(N);\n    for (auto &x : T) cin >> x;\n\n    // Random number generator\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n    uniform_real_distribution<double> urd(0.0, 1.0);\n\n    // Initial random assignment\n    vector<int> a(N), b(N);\n    for (int i = 0; i < N; i++) {\n        a[i] = rng() % N;\n        b[i] = rng() % N;\n    }\n\n    // Simulation function\n    auto simulate = [&](const vector<int> &aa, const vector<int> &bb) -> long long {\n        vector<long long> cnt(N, 0);\n        int cur = 0;\n        cnt[0] = 1;\n        for (int w = 1; w < L; w++) {\n            int t = cnt[cur];\n            int nxt = (t % 2 == 1) ? aa[cur] : bb[cur];\n            cur = nxt;\n            cnt[cur]++;\n        }\n        long long e = 0;\n        for (int i = 0; i < N; i++) e += abs(cnt[i] - T[i]);\n        return e;\n    };\n\n    // Initial error\n    long long currentE = simulate(a, b);\n\n    // Simulated Annealing parameters\n    double temp = 1000.0;\n    const int iterations = 150;  // Adjust based on time; ~150 fits within 2s\n    const double cooling_rate = 0.99;\n\n    for (int it = 0; it < iterations; it++) {\n        // Perturb: choose random i, which (0 for a, 1 for b), and new value\n        int i = rng() % N;\n        int which = rng() % 2;\n        int old_val = (which == 0) ? a[i] : b[i];\n        int new_val = rng() % N;\n\n        // Apply change\n        if (which == 0) a[i] = new_val;\n        else b[i] = new_val;\n\n        // Compute new error\n        long long newE = simulate(a, b);\n\n        // Acceptance\n        if (newE <= currentE || urd(rng) < exp((currentE - newE) / temp)) {\n            currentE = newE;\n        } else {\n            // Revert\n            if (which == 0) a[i] = old_val;\n            else b[i] = old_val;\n        }\n\n        // Cool down\n        temp *= cooling_rate;\n    }\n\n    // Output the best assignment\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\nusing ll = long long;\nusing ld = long double;\n\n// Union-Find for Kruskal\nstruct UF {\n    vector<int> par;\n    UF(int n) : par(n) { iota(par.begin(), par.end(), 0); }\n    int find(int x) { return par[x] == x ? x : par[x] = find(par[x]); }\n    bool unite(int x, int y) {\n        x = find(x); y = find(y);\n        if (x == y) return false;\n        par[x] = y;\n        return true;\n    }\n};\n\n// Compute floored Euclidean distance\nll dist(ld x1, ld y1, ld x2, ld y2) {\n    ld dx = x1 - x2, dy = y1 - y2;\n    return floorl(sqrtl(dx * dx + dy * dy));\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, Q, L, W;\n    cin >> N >> M >> Q >> L >> W;\n    vector<int> G(M);\n    for (int i = 0; i < M; ++i) cin >> G[i];\n    vector<ld> 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    // Compute approximate centers\n    vector<ld> cx(N), cy(N);\n    for (int i = 0; i < N; ++i) {\n        cx[i] = (lx[i] + rx[i]) / 2.0L;\n        cy[i] = (ly[i] + ry[i]) / 2.0L;\n    }\n\n    // Sort cities by cx, then cy\n    vector<int> cities(N);\n    iota(cities.begin(), cities.end(), 0);\n    sort(cities.begin(), cities.end(), [&](int a, int b) {\n        if (cx[a] != cx[b]) return cx[a] < cx[b];\n        return cy[a] < cy[b];\n    });\n\n    // Assign to groups\n    vector<vector<int>> groups(M);\n    int idx = 0;\n    for (int k = 0; k < M; ++k) {\n        for (int j = 0; j < G[k]; ++j) {\n            groups[k].push_back(cities[idx++]);\n        }\n    }\n\n    // Process queries and build edges\n    vector<vector<pair<int, int>>> edges(M);\n    int queries_used = 0;\n\n    auto query = [&](const vector<int>& c) -> vector<pair<int, int>> {\n        if (queries_used >= Q) return {};\n        cout << \"? \" << c.size();\n        for (int x : c) cout << \" \" << x;\n        cout << \"\\n\" << flush;\n        ++queries_used;\n        vector<pair<int, int>> res;\n        for (int i = 0; i < (int)c.size() - 1; ++i) {\n            int a, b;\n            cin >> a >> b;\n            res.emplace_back(min(a, b), max(a, b));\n        }\n        return res;\n    };\n\n    for (int k = 0; k < M; ++k) {\n        auto& grp = groups[k];\n        int sz = grp.size();\n        if (sz <= L) {\n            // Query entire group for true MST\n            auto res = query(grp);\n            edges[k] = res;\n        } else {\n            // Approximate MST using centers\n            vector<tuple<ll, int, int>> edge_list;\n            for (int i = 0; i < sz; ++i) {\n                for (int j = i + 1; j < sz; ++j) {\n                    ll d = dist(cx[grp[i]], cy[grp[i]], cx[grp[j]], cy[grp[j]]);\n                    edge_list.emplace_back(d, i, j);\n                }\n            }\n            sort(edge_list.begin(), edge_list.end());\n            UF uf(sz);\n            vector<pair<int, int>> mst_edges;\n            for (auto& [d, u, v] : edge_list) {\n                if (uf.unite(u, v)) {\n                    mst_edges.emplace_back(grp[u], grp[v]);\n                }\n            }\n            edges[k] = mst_edges;\n\n            // Optional: Refine with queries (e.g., query every L cities)\n            // For simplicity, skip if Q is low; add if time allows\n            // Example: Query subsets of size L in sliding window\n            if (queries_used < Q - 10) {  // Reserve some queries\n                for (int start = 0; start < sz; start += L - 1) {\n                    int end = min(start + L, sz);\n                    vector<int> subset(grp.begin() + start, grp.begin() + end);\n                    if ((int)subset.size() >= 2) {\n                        auto res = query(subset);\n                        // Incorporate res into edges[k] if not already present\n                        // (Simple: just add them, assuming they improve)\n                        for (auto& p : res) {\n                            edges[k].push_back(p);\n                        }\n                    }\n                }\n            }\n        }\n    }\n\n    // Output\n    cout << \"!\\n\";\n    for (int k = 0; k < M; ++k) {\n        for (int x : groups[k]) cout << x << \" \";\n        cout << \"\\n\";\n        for (auto& [a, b] : edges[k]) {\n            cout << a << \" \" << b << \"\\n\";\n        }\n    }\n\n    return 0;\n}","ahc046":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int N, M;\n    cin >> N >> M;\n    vector<pair<int, int>> pos(M);\n    for (int i = 0; i < M; i++) {\n        cin >> pos[i].first >> pos[i].second;\n    }\n    set<int> rows, cols;\n    for (auto [i, j] : pos) {\n        rows.insert(i);\n        cols.insert(j);\n    }\n    vector<pair<int, int>> blocks;\n    for (int r : rows) {\n        int maxj = -1;\n        for (auto [i, j] : pos) {\n            if (i == r) maxj = max(maxj, j);\n        }\n        if (maxj < 19) blocks.emplace_back(r, maxj + 1);\n    }\n    for (int c : cols) {\n        int maxi = -1;\n        for (auto [i, j] : pos) {\n            if (j == c) maxi = max(maxi, i);\n        }\n        if (maxi < 19) blocks.emplace_back(maxi + 1, c);\n    }\n    sort(blocks.begin(), blocks.end());\n    blocks.erase(unique(blocks.begin(), blocks.end()), blocks.end());\n    vector<vector<bool>> blocked(20, vector<bool>(20, false));\n    for (auto [x, y] : blocks) blocked[x][y] = true;\n    map<pair<int, int>, pair<int, int>> block_for;\n    vector<pair<int, int>> to_visit;\n    for (auto b : blocks) {\n        int bx = b.first, by = b.second;\n        pair<int, int> adj;\n        if (bx > 0) adj = {bx - 1, by};\n        else if (bx < 19) adj = {bx + 1, by};\n        else if (by > 0) adj = {bx, by - 1};\n        else adj = {bx, by + 1};\n        to_visit.push_back(adj);\n        block_for[adj] = b;\n    }\n    sort(to_visit.begin(), to_visit.end());\n    to_visit.erase(unique(to_visit.begin(), to_visit.end()), to_visit.end());\n    auto get_path = [&](pair<int, int> s, pair<int, int> t) {\n        vector<vector<int>> dist(20, vector<int>(20, -1));\n        vector<vector<pair<int, int>>> prevv(20, vector<pair<int, int>>(20, {-1, -1}));\n        queue<pair<int, int>> q;\n        q.push(s);\n        dist[s.first][s.second] = 0;\n        int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};\n        while (!q.empty()) {\n            auto [x, y] = q.front();\n            q.pop();\n            for (int d = 0; d < 4; d++) {\n                int nx = x + dx[d], ny = y + dy[d];\n                if (nx >= 0 && nx < 20 && ny >= 0 && ny < 20 && !blocked[nx][ny] && dist[nx][ny] == -1) {\n                    dist[nx][ny] = dist[x][y] + 1;\n                    prevv[nx][ny] = {x, y};\n                    q.push({nx, ny});\n                }\n            }\n        }\n        vector<pair<int, int>> path;\n        pair<int, int> cur = t;\n        while (cur != make_pair(-1, -1)) {\n            path.push_back(cur);\n            cur = prevv[cur.first][cur.second];\n        }\n        reverse(path.begin(), path.end());\n        if (path[0] != s) return make_pair(vector<pair<int, int>>(), vector<int>());\n        vector<int> dirs;\n        for (size_t i = 1; i < path.size(); i++) {\n            int px = path[i - 1].first, py = path[i - 1].second;\n            int cx = path[i].first, cy = path[i].second;\n            if (cx == px + 1) dirs.push_back(1);\n            else if (cx == px - 1) dirs.push_back(0);\n            else if (cy == py + 1) dirs.push_back(3);\n            else if (cy == py - 1) dirs.push_back(2);\n        }\n        return make_pair(path, dirs);\n    };\n    vector<pair<char, char>> actions;\n    pair<int, int> current = pos[0];\n    for (auto tv : to_visit) {\n        auto [path, dirs] = get_path(current, tv);\n        for (int d : dirs) {\n            actions.emplace_back('M', \"UDLR\"[d]);\n        }\n        if (block_for.count(tv)) {\n            auto b = block_for[tv];\n            int ddx = b.first - tv.first, ddy = b.second - tv.second;\n            char dir;\n            if (ddx == 1) dir = 'D';\n            else if (ddx == -1) dir = 'U';\n            else if (ddy == 1) dir = 'R';\n            else dir = 'L';\n            actions.emplace_back('A', dir);\n        }\n        current = tv;\n    }\n    for (int k = 1; k < M; k++) {\n        auto [path, dirs] = get_path(current, pos[k]);\n        vector<pair<char, char>> acts;\n        size_t i = 0;\n        while (i < dirs.size()) {\n            size_t j = i;\n            while (j < dirs.size() && dirs[j] == dirs[i]) j++;\n            if (j - i == 1) {\n                acts.emplace_back('M', \"UDLR\"[dirs[i]]);\n            } else {\n                acts.emplace_back('S', \"UDLR\"[dirs[i]]);\n            }\n            i = j;\n        }\n        for (auto p : acts) actions.push_back(p);\n        current = pos[k];\n    }\n    for (auto [a, d] : actions) {\n        cout << a << \" \" << d << endl;\n    }\n    return 0;\n}"},"2":{"ahc001":"#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n\ndouble calc_p(ll r, ll s) {\n    if (s == 0 || r == 0) return 0.0;\n    double mn = min(r, s);\n    double mx = max(r, s);\n    double ratio = mn / mx;\n    return 1.0 - (1.0 - ratio) * (1.0 - ratio);\n}\n\nbool check_overlap(int i, ll na, ll nb, ll nc, ll nd, const vector<ll>& a, const vector<ll>& b, const vector<ll>& c, const vector<ll>& d, int n) {\n    for (int j = 0; j < n; ++j) {\n        if (i == j) continue;\n        ll ax = max(na, a[j]);\n        ll bx = min(nc, c[j]);\n        ll ay = max(nb, b[j]);\n        ll by = min(nd, d[j]);\n        if (ax < bx && ay < by) return true;\n    }\n    return false;\n}\n\nll find_max_dw(int i, const vector<ll>& a, const vector<ll>& b, const vector<ll>& c, const vector<ll>& d, int n) {\n    ll left = 0, right = 10000LL - c[i];\n    while (left < right) {\n        ll mid = (left + right + 1) / 2;\n        ll new_c = c[i] + mid;\n        if (check_overlap(i, a[i], b[i], new_c, d[i], a, b, c, d, n)) {\n            right = mid - 1;\n        } else {\n            left = mid;\n        }\n    }\n    return left;\n}\n\nll find_max_dh(int i, const vector<ll>& a, const vector<ll>& b, const vector<ll>& c, const vector<ll>& d, int n) {\n    ll left = 0, right = 10000LL - d[i];\n    while (left < right) {\n        ll mid = (left + right + 1) / 2;\n        ll new_d = d[i] + mid;\n        if (check_overlap(i, a[i], b[i], c[i], new_d, a, b, c, d, n)) {\n            right = mid - 1;\n        } else {\n            left = mid;\n        }\n    }\n    return left;\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int n;\n    cin >> n;\n    vector<ll> x(n), y(n), r(n);\n    for (int i = 0; i < n; ++i) {\n        cin >> x[i] >> y[i] >> r[i];\n    }\n    \n    vector<ll> a(n), b(n), c(n), d(n), s(n);\n    for (int i = 0; i < n; ++i) {\n        a[i] = x[i];\n        c[i] = x[i] + 1;\n        b[i] = y[i];\n        d[i] = y[i] + 1;\n        s[i] = 1;\n    }\n    \n    bool changed = true;\n    while (changed) {\n        changed = false;\n        double max_gain = -1e9;\n        int best_i = -1;\n        int best_dir = -1;  // 0: width, 1: height\n        ll best_delta = 0;\n        \n        for (int i = 0; i < n; ++i) {\n            if (s[i] >= r[i]) continue;\n            \n            ll w = c[i] - a[i];\n            ll h = d[i] - b[i];\n            \n            // Find max dw with binary search\n            ll dw = find_max_dw(i, a, b, c, d, n);\n            \n            // Find max dh with binary search\n            ll dh = find_max_dh(i, a, b, c, d, n);\n            \n            // Check gain for dw\n            if (dw > 0) {\n                ll new_s = s[i] + dw * h;\n                double old_p = calc_p(r[i], s[i]);\n                double new_p = calc_p(r[i], new_s);\n                double gain = new_p - old_p;\n                if (gain > max_gain + 1e-9) {  // Small epsilon for floating-point\n                    max_gain = gain;\n                    best_i = i;\n                    best_dir = 0;\n                    best_delta = dw;\n                }\n            }\n            \n            // Check gain for dh\n            if (dh > 0) {\n                ll new_s = s[i] + dh * w;\n                double old_p = calc_p(r[i], s[i]);\n                double new_p = calc_p(r[i], new_s);\n                double gain = new_p - old_p;\n                if (gain > max_gain + 1e-9) {\n                    max_gain = gain;\n                    best_i = i;\n                    best_dir = 1;\n                    best_delta = dh;\n                }\n            }\n        }\n        \n        if (best_i != -1) {\n            changed = true;\n            if (best_dir == 0) {  // Expand width\n                c[best_i] += best_delta;\n                s[best_i] += best_delta * (d[best_i] - b[best_i]);\n            } else {  // Expand height\n                d[best_i] += best_delta;\n                s[best_i] += best_delta * (c[best_i] - a[best_i]);\n            }\n        }\n    }\n    \n    // Output\n    for (int i = 0; i < n; ++i) {\n        cout << a[i] << \" \" << b[i] << \" \" << c[i] << \" \" << d[i] << \"\\n\";\n    }\n    \n    return 0;\n}","ahc002":"#include <bits/stdc++.h>\n\nusing namespace std;\n\nint main() {\n    int si, sj;\n    cin >> si >> sj;\n    vector<vector<int>> t(50, vector<int>(50));\n    for (int i = 0; i < 50; i++) for (int j = 0; j < 50; j++) cin >> t[i][j];\n    vector<vector<int>> p(50, vector<int>(50));\n    for (int i = 0; i < 50; i++) for (int j = 0; j < 50; j++) cin >> p[i][j];\n    \n    int M = 0;\n    for (int i = 0; i < 50; i++) for (int j = 0; j < 50; j++) M = max(M, t[i][j] + 1);\n    \n    vector<vector<int>> tile_squares(M);\n    for (int i = 0; i < 50; i++) for (int j = 0; j < 50; j++) {\n        tile_squares[t[i][j]].push_back(i * 50 + j);\n    }\n    \n    vector<set<int>> adj_tile(M);\n    int di[4] = {-1, 1, 0, 0};\n    int dj[4] = {0, 0, -1, 1};\n    for (int i = 0; i < 50; i++) for (int j = 0; j < 50; j++) {\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 < 50 && nj >= 0 && nj < 50 && t[i][j] != t[ni][nj]) {\n                adj_tile[t[i][j]].insert(t[ni][nj]);\n            }\n        }\n    }\n    \n    // Greedy path on tiles, ensuring adjacent squares\n    set<int> visited;\n    vector<int> path_tiles;\n    vector<pair<int, int>> path_squares;  // Store the actual squares\n    int current_tile = t[si][sj];\n    pair<int, int> current_sq = {si, sj};\n    visited.insert(current_tile);\n    path_tiles.push_back(current_tile);\n    path_squares.push_back(current_sq);\n    \n    while (true) {\n        int best_tile = -1;\n        int best_score = -1;\n        pair<int, int> best_sq = {-1, -1};\n        for (int nei : adj_tile[current_tile]) {\n            if (visited.count(nei)) continue;\n            // Find squares in nei adjacent to current_sq\n            int max_adj_p = -1;\n            pair<int, int> chosen_sq = {-1, -1};\n            for (int sq : tile_squares[nei]) {\n                int ni = sq / 50;\n                int nj = sq % 50;\n                if (abs(ni - current_sq.first) + abs(nj - current_sq.second) == 1) {  // Adjacent\n                    if (p[ni][nj] > max_adj_p) {\n                        max_adj_p = p[ni][nj];\n                        chosen_sq = {ni, nj};\n                    }\n                }\n            }\n            if (max_adj_p > best_score) {\n                best_score = max_adj_p;\n                best_tile = nei;\n                best_sq = chosen_sq;\n            }\n        }\n        if (best_tile == -1) break;\n        visited.insert(best_tile);\n        path_tiles.push_back(best_tile);\n        path_squares.push_back(best_sq);\n        current_tile = best_tile;\n        current_sq = best_sq;\n    }\n    \n    // Build move string\n    string moves = \"\";\n    for (size_t i = 1; i < path_squares.size(); i++) {\n        auto [pi, pj] = path_squares[i - 1];\n        auto [ci, cj] = path_squares[i];\n        if (ci == pi - 1) moves += 'U';\n        else if (ci == pi + 1) moves += 'D';\n        else if (cj == pj - 1) moves += 'L';\n        else if (cj == pj + 1) moves += 'R';\n        else {\n            // Should not happen now\n            assert(false);\n        }\n    }\n    cout << moves << endl;\n    return 0;\n}","ahc003":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    const int N = 30;\n    vector<vector<double>> h(N, vector<double>(N-1, 5000.0));  // h[i][j]: cost from (i,j) to (i,j+1)\n    vector<vector<double>> v(N-1, vector<double>(N, 5000.0));  // v[i][j]: cost from (i,j) to (i+1,j)\n\n    for (int query = 0; query < 1000; ++query) {\n        int si, sj, ti, tj;\n        cin >> si >> sj >> ti >> tj;\n\n        // Dijkstra to find shortest path with current estimated costs\n        vector<vector<double>> dist(N, vector<double>(N, 1e18));\n        vector<vector<pair<int, int>>> prev(N, vector<pair<int, int>>(N, {-1, -1}));\n        priority_queue<tuple<double, int, int>, vector<tuple<double, int, int>>, greater<tuple<double, int, int>>> pq;\n\n        dist[si][sj] = 0.0;\n        pq.emplace(0.0, si, sj);\n\n        while (!pq.empty()) {\n            auto [cost, i, j] = pq.top();\n            pq.pop();\n            if (cost > dist[i][j]) continue;\n\n            // Neighbors: up, down, left, right\n            vector<tuple<int, int, double>> neighbors;\n            if (i > 0) neighbors.emplace_back(i-1, j, v[i-1][j]);  // up\n            if (i < N-1) neighbors.emplace_back(i+1, j, v[i][j]);  // down\n            if (j > 0) neighbors.emplace_back(i, j-1, h[i][j-1]);  // left\n            if (j < N-1) neighbors.emplace_back(i, j+1, h[i][j]);  // right\n\n            for (auto [ni, nj, edge_cost] : neighbors) {\n                double new_cost = cost + edge_cost;\n                if (new_cost < dist[ni][nj]) {\n                    dist[ni][nj] = new_cost;\n                    prev[ni][nj] = {i, j};\n                    pq.emplace(new_cost, ni, nj);\n                }\n            }\n        }\n\n        // If target is unreachable (shouldn't happen), output empty path\n        if (dist[ti][tj] == 1e18) {\n            cout << \"\" << endl;\n            cout.flush();\n            int F;\n            cin >> F;  // Still read feedback to continue\n            continue;\n        }\n\n        // Reconstruct path from t to s, building forward moves\n        string path = \"\";\n        int ci = ti, cj = tj;\n        while (ci != si || cj != sj) {\n            auto [pi, pj] = prev[ci][cj];\n            if (pi == -1 || pj == -1) {\n                // Invalid predecessor (shouldn't happen), break to avoid crash\n                path = \"\";\n                break;\n            }\n            // Determine forward move from (pi, pj) to (ci, cj)\n            if (pi == ci - 1) path += 'D';  // i increases: down\n            else if (pi == ci + 1) path += 'U';  // i decreases: up\n            else if (pj == cj - 1) path += 'R';  // j increases: right\n            else if (pj == cj + 1) path += 'L';  // j decreases: left\n            ci = pi;\n            cj = pj;\n        }\n        reverse(path.begin(), path.end());  // Reverse to get from s to t\n\n        // Output path and flush\n        cout << path << endl;\n        cout.flush();\n\n        // Read feedback F\n        int F;\n        cin >> F;\n\n        // Compute estimated path cost C\n        double C = 0.0;\n        ci = si, cj = sj;\n        for (char move : path) {\n            if (move == 'U') {\n                C += v[ci-1][cj];\n                ci--;\n            } else if (move == 'D') {\n                C += v[ci][cj];\n                ci++;\n            } else if (move == 'L') {\n                C += h[ci][cj-1];\n                cj--;\n            } else if (move == 'R') {\n                C += h[ci][cj];\n                cj++;\n            }\n        }\n\n        // Update estimated costs if C > 0\n        if (C > 1e-9) {\n            double factor = static_cast<double>(F) / C;\n            ci = si, cj = sj;\n            for (char move : path) {\n                if (move == 'U') {\n                    v[ci-1][cj] *= factor;\n                    ci--;\n                } else if (move == 'D') {\n                    v[ci][cj] *= factor;\n                    ci++;\n                } else if (move == 'L') {\n                    h[ci][cj-1] *= factor;\n                    cj--;\n                } else if (move == 'R') {\n                    h[ci][cj] *= factor;\n                    cj++;\n                }\n            }\n        }\n    }\n\n    return 0;\n}","ahc004":"#include <bits/stdc++.h>\n\nusing namespace std;\n\nint main() {\n    int N, M;\n    cin >> N >> M;\n    vector<string> strings(M);\n    for (int i = 0; i < M; i++) {\n        cin >> strings[i];\n    }\n\n    vector<vector<char>> grid(N, vector<char>(N, '.'));\n\n    for (const auto& s : strings) {\n        int k = s.size();\n        int best_fill = -1;\n        int best_i = -1, best_j = -1;\n        bool best_horiz = true;\n\n        // Check horizontal placements\n        for (int i = 0; i < N; i++) {  // Row\n            for (int j = 0; j < N; j++) {  // Starting column\n                bool valid = true;\n                int fill = 0;\n                for (int p = 0; p < k; p++) {\n                    int cj = (j + p) % N;\n                    char cell = grid[i][cj];\n                    if (cell != '.' && cell != s[p]) {\n                        valid = false;\n                        break;\n                    }\n                    if (cell == '.') fill++;\n                }\n                if (valid && fill > best_fill) {\n                    best_fill = fill;\n                    best_i = i;\n                    best_j = j;\n                    best_horiz = true;\n                }\n            }\n        }\n\n        // Check vertical placements\n        for (int j = 0; j < N; j++) {  // Column\n            for (int i = 0; i < N; i++) {  // Starting row\n                bool valid = true;\n                int fill = 0;\n                for (int p = 0; p < k; p++) {\n                    int ci = (i + p) % N;\n                    char cell = grid[ci][j];\n                    if (cell != '.' && cell != s[p]) {\n                        valid = false;\n                        break;\n                    }\n                    if (cell == '.') fill++;\n                }\n                if (valid && fill > best_fill) {\n                    best_fill = fill;\n                    best_i = i;\n                    best_j = j;\n                    best_horiz = false;\n                }\n            }\n        }\n\n        // Place the string if a valid position was found\n        if (best_fill >= 0) {\n            if (best_horiz) {\n                for (int p = 0; p < k; p++) {\n                    int cj = (best_j + p) % N;\n                    if (grid[best_i][cj] == '.') {\n                        grid[best_i][cj] = s[p];\n                    }\n                }\n            } else {\n                for (int p = 0; p < k; p++) {\n                    int ci = (best_i + p) % N;\n                    if (grid[ci][best_j] == '.') {\n                        grid[ci][best_j] = s[p];\n                    }\n                }\n            }\n        }\n    }\n\n    // Output the grid\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cout << grid[i][j];\n        }\n        cout << endl;\n    }\n\n    return 0;\n}","ahc005":"#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nconst ll INF = 1LL << 60;\n\nint main() {\n    int N, si, sj;\n    cin >> N >> si >> sj;\n    vector<string> c(N);\n    for(int i = 0; i < N; i++) cin >> c[i];\n    vector<vector<int>> cost(N, vector<int>(N, 0));\n    vector<pair<int, int>> roads;\n    for(int i = 0; i < N; i++) {\n        for(int j = 0; j < N; j++) {\n            if(c[i][j] != '#') {\n                cost[i][j] = c[i][j] - '0';\n                roads.emplace_back(i, j);\n            }\n        }\n    }\n    int R = roads.size();\n    \n    // Identify active rows and columns\n    vector<bool> active_row(N, false), active_col(N, false);\n    for(auto [i, j] : roads) {\n        active_row[i] = true;\n        active_col[j] = true;\n    }\n    \n    // Greedy selection for S\n    vector<bool> covered_row(N, false), covered_col(N, false);\n    vector<pair<int, int>> S;\n    set<pair<int, int>> road_set(roads.begin(), roads.end());\n    while(true) {\n        bool all_covered = true;\n        for(int i = 0; i < N; i++) {\n            if(active_row[i] && !covered_row[i]) all_covered = false;\n            if(active_col[i] && !covered_col[i]) all_covered = false;\n        }\n        if(all_covered) break;\n        \n        pair<int, int> best = {-1, -1};\n        int max_gain = -1;\n        int min_cost = INT_MAX;\n        for(auto p : roads) {\n            int i = p.first, j = p.second;\n            int gain = (active_row[i] && !covered_row[i]) + (active_col[j] && !covered_col[j]);\n            if(gain > max_gain || (gain == max_gain && cost[i][j] < min_cost)) {\n                max_gain = gain;\n                min_cost = cost[i][j];\n                best = p;\n            }\n        }\n        if(best.first == -1) break; // Should not happen\n        S.push_back(best);\n        covered_row[best.first] = true;\n        covered_col[best.second] = true;\n    }\n    // Ensure start is in S\n    bool has_start = false;\n    for(auto p : S) if(p == make_pair(si, sj)) has_start = true;\n    if(!has_start) S.push_back({si, sj});\n    \n    // Nearest neighbor to order S\n    vector<pair<int, int>> path;\n    set<pair<int, int>> visited;\n    path.push_back({si, sj});\n    visited.insert({si, sj});\n    while(path.size() < S.size()) {\n        auto curr = path.back();\n        pair<int, int> next = {-1, -1};\n        int min_dist = INT_MAX;\n        for(auto p : S) {\n            if(visited.count(p)) continue;\n            int d = abs(p.first - curr.first) + abs(p.second - curr.second);\n            if(d < min_dist) {\n                min_dist = d;\n                next = p;\n            }\n        }\n        if(next.first == -1) break;\n        path.push_back(next);\n        visited.insert(next);\n    }\n    path.push_back({si, sj});\n    \n    // Function to get path string from a to b\n    auto get_path = [&](pair<int, int> start, pair<int, int> end) -> string {\n        if(start == end) return \"\";\n        vector<vector<ll>> dist(N, vector<ll>(N, INF));\n        vector<vector<pair<int, int>>> prev(N, vector<pair<int, int>>(N, {-1, -1}));\n        priority_queue<tuple<ll, int, int>, vector<tuple<ll, int, int>>, greater<>> pq;\n        dist[start.first][start.second] = 0;\n        pq.push({0, start.first, start.second});\n        while(!pq.empty()) {\n            auto [d, i, j] = pq.top(); pq.pop();\n            if(d > dist[i][j]) continue;\n            for(auto [di, dj] : vector<pair<int, int>>{{0,1}, {0,-1}, {1,0}, {-1,0}}) {\n                int ni = i + di, nj = j + dj;\n                if(ni >= 0 && ni < N && nj >= 0 && nj < N && cost[ni][nj] > 0) {\n                    ll nd = d + cost[ni][nj];\n                    if(nd < dist[ni][nj]) {\n                        dist[ni][nj] = nd;\n                        prev[ni][nj] = {i, j};\n                        pq.push({nd, ni, nj});\n                    }\n                }\n            }\n        }\n        if(dist[end.first][end.second] == INF) return \"\"; // Unreachable (should not happen)\n        // Reconstruct path\n        vector<pair<int, int>> route;\n        pair<int, int> curr = end;\n        while(curr != make_pair(-1, -1)) {\n            route.push_back(curr);\n            curr = prev[curr.first][curr.second];\n        }\n        reverse(route.begin(), route.end());\n        // Build string\n        string s;\n        for(size_t k = 1; k < route.size(); k++) {\n            auto [i1, j1] = route[k-1];\n            auto [i2, j2] = route[k];\n            if(i2 == i1 + 1) s += 'D';\n            else if(i2 == i1 - 1) s += 'U';\n            else if(j2 == j1 + 1) s += 'R';\n            else if(j2 == j1 - 1) s += 'L';\n        }\n        return s;\n    };\n    \n    // Build full route\n    string ans;\n    for(size_t k = 1; k < path.size(); k++) {\n        ans += get_path(path[k-1], path[k]);\n    }\n    cout << ans << endl;\n    return 0;\n}","future-contest-2022-qual":"#include <bits/stdc++.h>\nusing namespace std;\n\nvoid dfs(int u, const vector<vector<int>>& adj, vector<int>& num_desc, vector<bool>& visited) {\n    visited[u] = true;\n    for (int v : adj[u]) {\n        if (!visited[v]) dfs(v, adj, num_desc, visited);\n        num_desc[u] += 1 + num_desc[v];\n    }\n}\n\nint main() {\n    ios::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 j = 0; j < K; ++j) {\n            cin >> d[i][j];\n        }\n    }\n    \n    vector<vector<int>> deps(N); // deps[i] = tasks that i depends on\n    vector<vector<int>> adj(N);  // adj[i] = tasks that depend on i (for descendants)\n    for (int i = 0; i < R; ++i) {\n        int u, v;\n        cin >> u >> v;\n        --u; --v;\n        deps[v].push_back(u);\n        adj[u].push_back(v);\n    }\n    \n    // Precompute number of descendants\n    vector<int> num_desc(N, 0);\n    vector<bool> visited(N, false);\n    for (int i = 0; i < N; ++i) {\n        if (!visited[i]) dfs(i, adj, num_desc, visited);\n    }\n    \n    vector<int> task_status(N, -1);\n    vector<int> member_status(M, -1);\n    vector<int> start_day(N, -1);\n    vector<vector<int>> est_s(M, vector<int>(K, 0));\n    \n    int current_day = 0;\n    while (true) {\n        ++current_day;\n        \n        vector<int> free_members;\n        for (int j = 0; j < M; ++j) {\n            if (member_status[j] == -1) free_members.push_back(j);\n        }\n        \n        vector<int> ready_tasks;\n        for (int i = 0; i < N; ++i) {\n            if (task_status[i] == -1) {\n                bool can_start = true;\n                for (int u : deps[i]) {\n                    if (task_status[u] != 1) {\n                        can_start = false;\n                        break;\n                    }\n                }\n                if (can_start) ready_tasks.push_back(i);\n            }\n        }\n        \n        vector<vector<long long>> est_w(ready_tasks.size(), vector<long long>(free_members.size()));\n        for (int ti = 0; ti < ready_tasks.size(); ++ti) {\n            int i = ready_tasks[ti];\n            for (int mj = 0; mj < free_members.size(); ++mj) {\n                int j = free_members[mj];\n                long long w = 0;\n                for (int k = 0; k < K; ++k) {\n                    w += max(0, d[i][k] - est_s[j][k]);\n                }\n                est_w[ti][mj] = w;\n            }\n        }\n        \n        // Sort tasks: first by num_desc descending, then by min_est_w ascending\n        vector<tuple<int, long long, int>> task_order; // (-num_desc, min_est_w, ti)\n        for (int ti = 0; ti < ready_tasks.size(); ++ti) {\n            long long min_w = LLONG_MAX;\n            for (int mj = 0; mj < free_members.size(); ++mj) {\n                min_w = min(min_w, est_w[ti][mj]);\n            }\n            task_order.emplace_back(-num_desc[ready_tasks[ti]], min_w, ti);\n        }\n        sort(task_order.begin(), task_order.end());\n        \n        vector<pair<int, int>> assignments;\n        set<int> used_members;\n        for (auto& tup : task_order) {\n            int ti = get<2>(tup);\n            int i = ready_tasks[ti];\n            long long best_w = LLONG_MAX;\n            int best_j = -1;\n            for (int mj = 0; mj < free_members.size(); ++mj) {\n                int j = free_members[mj];\n                if (used_members.count(j)) continue;\n                if (est_w[ti][mj] < best_w) {\n                    best_w = est_w[ti][mj];\n                    best_j = j;\n                }\n            }\n            if (best_j != -1) {\n                assignments.emplace_back(best_j, i);\n                used_members.insert(best_j);\n            }\n        }\n        \n        cout << assignments.size();\n        for (auto& p : assignments) {\n            cout << \" \" << (p.first + 1) << \" \" << (p.second + 1);\n            task_status[p.second] = 0;\n            member_status[p.first] = p.second;\n            start_day[p.second] = current_day;\n        }\n        cout << \"\\n\";\n        cout.flush();\n        \n        int n;\n        cin >> n;\n        if (n == -1) break;\n        \n        vector<int> completers(n);\n        for (int i = 0; i < n; ++i) {\n            cin >> completers[i];\n            --completers[i];\n        }\n        \n        for (int f : completers) {\n            int task = member_status[f];\n            if (task == -1) continue;\n            int t = current_day - start_day[task] + 1;\n            if (t == 1) {\n                for (int k = 0; k < K; ++k) {\n                    est_s[f][k] = max(est_s[f][k], d[task][k]);\n                }\n            } else {\n                // Refined update: compute current deficit, add proportionally with learning rate\n                long long current_w = 0;\n                vector<int> deficit_skills;\n                for (int k = 0; k < K; ++k) {\n                    int def = max(0, d[task][k] - est_s[f][k]);\n                    current_w += def;\n                    if (def > 0) deficit_skills.push_back(k);\n                }\n                long long additional = t - current_w; // approx additional deficit\n                if (!deficit_skills.empty() && additional > 0) {\n                    double alpha = 0.5; // learning rate\n                    double add_per = alpha * additional / deficit_skills.size();\n                    for (int k : deficit_skills) {\n                        est_s[f][k] += (int)add_per;\n                    }\n                }\n            }\n            task_status[task] = 1;\n            member_status[f] = -1;\n        }\n        \n        bool all_done = true;\n        for (int i = 0; i < N; ++i) {\n            if (task_status[i] != 1) all_done = false;\n        }\n        if (all_done) break;\n    }\n    \n    return 0;\n}","ahc006":"#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing pii = pair<int, int>;\n\nstruct Order {\n    int a, b, c, d, idx;\n    ll cost;\n};\n\nll dist(int x1, int y1, int x2, int y2) {\n    return abs(x1 - x2) + abs(y1 - y2);\n}\n\nvector<pii> simulated_annealing(const vector<pii>& points, int office_x, int office_y, mt19937& rng) {\n    int n = points.size();\n    vector<int> order(n);\n    for (int i = 0; i < n; ++i) order[i] = i;\n    \n    // Compute initial distance\n    auto calc_dist = [&](const vector<int>& ord) -> ll {\n        ll total = 0;\n        total += dist(office_x, office_y, points[ord[0]].first, points[ord[0]].second);\n        for (int i = 0; i < n - 1; ++i) {\n            total += dist(points[ord[i]].first, points[ord[i]].second,\n                          points[ord[i+1]].first, points[ord[i+1]].second);\n        }\n        total += dist(points[ord.back()].first, points[ord.back()].second, office_x, office_y);\n        return total;\n    };\n    \n    ll current_dist = calc_dist(order);\n    vector<int> best_order = order;\n    ll best_dist = current_dist;\n    \n    double T = 1000.0;\n    double alpha = 0.995;\n    int max_iter = 100000;  // Adjust based on time\n    \n    uniform_int_distribution<int> dist_idx(0, n - 1);\n    uniform_real_distribution<double> dist_prob(0.0, 1.0);\n    \n    for (int iter = 0; iter < max_iter; ++iter) {\n        // Perturb: swap two random indices\n        int i = dist_idx(rng), j = dist_idx(rng);\n        swap(order[i], order[j]);\n        \n        ll new_dist = calc_dist(order);\n        ll delta = new_dist - current_dist;\n        \n        if (delta < 0 || dist_prob(rng) < exp(-delta / T)) {\n            current_dist = new_dist;\n            if (new_dist < best_dist) {\n                best_dist = new_dist;\n                best_order = order;\n            }\n        } else {\n            swap(order[i], order[j]);  // Revert\n        }\n        \n        T *= alpha;\n    }\n    \n    // Build route\n    vector<pii> route;\n    route.emplace_back(office_x, office_y);\n    for (int idx : best_order) {\n        route.push_back(points[idx]);\n    }\n    route.emplace_back(office_x, office_y);\n    return route;\n}\n\nint main() {\n    const int OFFICE_X = 400, OFFICE_Y = 400;\n    vector<Order> orders(1000);\n    for (int i = 0; i < 1000; ++i) {\n        cin >> orders[i].a >> orders[i].b >> orders[i].c >> orders[i].d;\n        orders[i].idx = i + 1;\n        orders[i].cost = dist(OFFICE_X, OFFICE_Y, orders[i].a, orders[i].b) +\n                         dist(orders[i].a, orders[i].b, orders[i].c, orders[i].d) +\n                         dist(orders[i].c, orders[i].d, OFFICE_X, OFFICE_Y);\n    }\n    \n    // Select 50 orders with lowest cost\n    sort(orders.begin(), orders.end(), [](const Order& o1, const Order& o2) {\n        return o1.cost < o2.cost;\n    });\n    vector<Order> selected(orders.begin(), orders.begin() + 50);\n    \n    // Prepare pickups and deliveries\n    vector<pii> pickups, deliveries;\n    for (const auto& ord : selected) {\n        pickups.emplace_back(ord.a, ord.b);\n        deliveries.emplace_back(ord.c, ord.d);\n    }\n    \n    // Optimize order using SA\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n    vector<pii> pickup_route = simulated_annealing(pickups, OFFICE_X, OFFICE_Y, rng);\n    vector<pii> delivery_route = simulated_annealing(deliveries, OFFICE_X, OFFICE_Y, rng);\n    \n    // Combine routes: office -> pickups -> deliveries -> office\n    vector<pii> full_route;\n    full_route.emplace_back(OFFICE_X, OFFICE_Y);\n    for (size_t i = 1; i < pickup_route.size() - 1; ++i) {  // Skip office at start/end\n        full_route.push_back(pickup_route[i]);\n    }\n    for (size_t i = 1; i < delivery_route.size() - 1; ++i) {\n        full_route.push_back(delivery_route[i]);\n    }\n    full_route.emplace_back(OFFICE_X, OFFICE_Y);\n    \n    // Output\n    cout << 50;\n    for (const auto& ord : selected) cout << \" \" << ord.idx;\n    cout << \"\\n\";\n    cout << full_route.size();\n    for (const auto& p : full_route) cout << \" \" << p.first << \" \" << p.second;\n    cout << \"\\n\";\n    \n    return 0;\n}","ahc007":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    const int N = 400;\n    const int M = 1995;\n    vector<int> x(N), y(N);\n    for (int i = 0; i < N; i++) {\n        cin >> x[i] >> y[i];\n    }\n    vector<int> u(M), v(M);\n    for (int i = 0; i < M; i++) {\n        cin >> u[i] >> v[i];\n    }\n    // Precompute d_i for each edge\n    vector<int> d(M);\n    for (int i = 0; i < M; i++) {\n        long long dx = x[u[i]] - x[v[i]];\n        long long dy = y[u[i]] - y[v[i]];\n        d[i] = round(sqrt(dx * dx + dy * dy));\n    }\n    // Adjacency list: vector of {to, {edge_index, weight}}\n    vector<vector<pair<int, pair<int, int>>>> adj(N);\n    vector<bool> adopted(M, false);\n    vector<int> weights(M, 0);\n    // Multiset for median weight tracking\n    multiset<int> weight_set;\n    \n    auto remove_edge = [&](int a, int b, int e) {\n        for (auto it = adj[a].begin(); it != adj[a].end(); ++it) {\n            if (it->second.first == e) {\n                adj[a].erase(it);\n                break;\n            }\n        }\n    };\n    \n    for (int i = 0; i < M; i++) {\n        int l;\n        cin >> l;\n        int uu = u[i], vv = v[i];\n        // BFS to check connectivity and find max on path\n        vector<int> dist(N, -1);\n        vector<int> parent(N, -1);\n        vector<int> edge_to_parent(N, -1);\n        queue<int> q;\n        q.push(uu);\n        dist[uu] = 0;\n        bool found = false;\n        int max_weight = -1;\n        int max_edge = -1;\n        while (!q.empty() && !found) {\n            int cur = q.front();\n            q.pop();\n            for (auto &[to, p] : adj[cur]) {\n                int eidx = p.first;\n                int w = p.second;\n                if (dist[to] == -1) {\n                    dist[to] = dist[cur] + 1;\n                    parent[to] = cur;\n                    edge_to_parent[to] = eidx;\n                    q.push(to);\n                    if (to == vv) {\n                        found = true;\n                        // Backtrack to find max on path\n                        int current = vv;\n                        while (current != uu) {\n                            int e = edge_to_parent[current];\n                            int ww = weights[edge_to_parent[current]];\n                            if (ww > max_weight) {\n                                max_weight = ww;\n                                max_edge = edge_to_parent[current];\n                            }\n                            current = parent[current];\n                        }\n                        break;\n                    }\n                }\n            }\n        }\n        bool adopt = false;\n        if (!found) {\n            // Not connected, adopt\n            adopt = true;\n        } else {\n            // Connected, check for replacement (more aggressive)\n            if (l * 1.05 < max_weight) {\n                adopt = true;\n                // Remove max_edge\n                adopted[max_edge] = false;\n                weight_set.erase(weight_set.find(weights[max_edge]));\n                remove_edge(u[max_edge], v[max_edge], max_edge);\n                remove_edge(v[max_edge], u[max_edge], max_edge);\n            } else {\n                // Check for new adoption heuristic\n                int median_weight = weight_set.empty() ? INT_MAX : *next(weight_set.begin(), weight_set.size() / 2);\n                if (l <= 1.5 * d[i] && l < median_weight) {\n                    adopt = true;\n                }\n            }\n        }\n        if (adopt) {\n            adopted[i] = true;\n            adj[uu].emplace_back(vv, make_pair(i, l));\n            adj[vv].emplace_back(uu, make_pair(i, l));\n            weights[i] = l;\n            weight_set.insert(l);\n            cout << 1 << endl;\n            fflush(stdout);\n        } else {\n            cout << 0 << endl;\n            fflush(stdout);\n        }\n    }\n    return 0;\n}","ahc008":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int DX[4] = {-1, 0, 1, 0};\nconst int DY[4] = {0, 1, 0, -1};\n\ndouble compute_score(pair<int, int> pos, const vector<vector<bool>>& passable, const vector<pair<int, int>>& pets) {\n    vector<vector<bool>> vis(31, vector<bool>(31, false));\n    queue<pair<int, int>> q;\n    q.push(pos);\n    vis[pos.first][pos.second] = true;\n    int area = 1;\n    int num_pets = 0;\n    for (auto& p : pets) {\n        if (p == pos) num_pets++;\n    }\n    while (!q.empty()) {\n        auto [x, y] = q.front();\n        q.pop();\n        for (int d = 0; d < 4; d++) {\n            int nx = x + DX[d], ny = y + DY[d];\n            if (nx >= 1 && nx <= 30 && ny >= 1 && ny <= 30 && passable[nx][ny] && !vis[nx][ny]) {\n                vis[nx][ny] = true;\n                area++;\n                for (auto& p : pets) {\n                    if (p == make_pair(nx, ny)) num_pets++;\n                }\n                q.push({nx, ny});\n            }\n        }\n    }\n    return (area / 900.0) * pow(2.0, -num_pets);\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N;\n    cin >> N;\n    vector<pair<int, int>> pets(N);\n    vector<int> pt(N);\n    for (int i = 0; i < N; i++) {\n        int x, y, t;\n        cin >> x >> y >> t;\n        pets[i] = {x, y};\n        pt[i] = t;\n    }\n    int M;\n    cin >> M;\n    vector<pair<int, int>> humans(M);\n    for (int i = 0; i < M; i++) {\n        int x, y;\n        cin >> x >> y;\n        humans[i] = {x, y};\n    }\n\n    vector<vector<bool>> passable(31, vector<bool>(31, true));\n\n    for (int turn = 0; turn < 300; turn++) {\n        vector<string> actions(M, \".\");\n        for (int i = 0; i < M; i++) {\n            double max_s = -1e9;\n            string best_act = \".\";\n            pair<int, int> cur_pos = humans[i];\n\n            // Try stay\n            {\n                double s = compute_score(cur_pos, passable, pets);\n                if (s > max_s) {\n                    max_s = s;\n                    best_act = \".\";\n                }\n            }\n\n            // Try builds\n            vector<string> builds = {\"u\", \"d\", \"l\", \"r\"};\n            for (auto act : builds) {\n                int dx = 0, dy = 0;\n                if (act == \"u\") dx = -1;\n                else if (act == \"d\") dx = 1;\n                else if (act == \"l\") dy = -1;\n                else if (act == \"r\") dy = 1;\n                int nx = cur_pos.first + dx, ny = cur_pos.second + dy;\n                bool valid = true;\n                if (nx < 1 || nx > 30 || ny < 1 || ny > 30) valid = false;\n                else {\n                    // Check for entities\n                    bool has_entity = false;\n                    for (auto& p : pets) {\n                        if (p == make_pair(nx, ny)) has_entity = true;\n                    }\n                    for (int j = 0; j < M; j++) {\n                        if (humans[j] == make_pair(nx, ny)) has_entity = true;\n                    }\n                    if (has_entity) valid = false;\n                    // Check adjacent pets\n                    for (int d = 0; d < 4; d++) {\n                        int ax = nx + DX[d], ay = ny + DY[d];\n                        if (ax >= 1 && ax <= 30 && ay >= 1 && ay <= 30) {\n                            for (auto& p : pets) {\n                                if (p == make_pair(ax, ay)) {\n                                    valid = false;\n                                    break;\n                                }\n                            }\n                        }\n                        if (!valid) break;\n                    }\n                    if (valid) {\n                        passable[nx][ny] = false;\n                        double s = compute_score(cur_pos, passable, pets);\n                        if (s > max_s) {\n                            max_s = s;\n                            best_act = act;\n                        }\n                        passable[nx][ny] = true;\n                    }\n                }\n            }\n\n            // Try moves\n            vector<string> moves = {\"U\", \"D\", \"L\", \"R\"};\n            for (auto act : moves) {\n                int dx = 0, dy = 0;\n                if (act == \"U\") dx = -1;\n                else if (act == \"D\") dx = 1;\n                else if (act == \"L\") dy = -1;\n                else if (act == \"R\") dy = 1;\n                int nx = cur_pos.first + dx, ny = cur_pos.second + dy;\n                bool valid = true;\n                if (nx < 1 || nx > 30 || ny < 1 || ny > 30) valid = false;\n                else if (!passable[nx][ny]) valid = false;\n                else {\n                    pair<int, int> new_pos = {nx, ny};\n                    double s = compute_score(new_pos, passable, pets);\n                    if (s > max_s) {\n                        max_s = s;\n                        best_act = act;\n                    }\n                }\n            }\n            actions[i] = best_act;\n        }\n\n        // Apply human moves\n        for (int i = 0; i < M; i++) {\n            string act = actions[i];\n            if (act[0] >= 'A' && act[0] <= 'Z') {  // Move\n                int dx = 0, dy = 0;\n                if (act == \"U\") dx = -1;\n                else if (act == \"D\") dx = 1;\n                else if (act == \"L\") dy = -1;\n                else if (act == \"R\") dy = 1;\n                int nx = humans[i].first + dx, ny = humans[i].second + dy;\n                humans[i] = {nx, ny};\n            }\n        }\n\n        // Apply builds\n        for (int i = 0; i < M; i++) {\n            string act = actions[i];\n            if (act[0] >= 'a' && act[0] <= 'z') {  // Build\n                int dx = 0, dy = 0;\n                if (act == \"u\") dx = -1;\n                else if (act == \"d\") dx = 1;\n                else if (act == \"l\") dy = -1;\n                else if (act == \"r\") dy = 1;\n                int nx = humans[i].first + dx, ny = humans[i].second + dy;\n                passable[nx][ny] = false;\n            }\n        }\n\n        // Output actions\n        for (auto& a : actions) cout << a;\n        cout << endl;\n        cout.flush();\n\n        // Read and apply pet moves\n        for (int i = 0; i < N; i++) {\n            string s;\n            cin >> s;\n            if (s == \".\") continue;\n            pair<int, int> pos = pets[i];\n            for (char c : s) {\n                int dx = 0, dy = 0;\n                if (c == 'U') dx = -1;\n                else if (c == 'D') dx = 1;\n                else if (c == 'L') dy = -1;\n                else if (c == 'R') dy = 1;\n                int nx = pos.first + dx, ny = pos.second + dy;\n                if (nx >= 1 && nx <= 30 && ny >= 1 && ny <= 30 && passable[nx][ny]) {\n                    pos = {nx, ny};\n                }\n            }\n            pets[i] = pos;\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), v(20);\n    for (int i = 0; i < 20; i++) cin >> h[i];\n    for (int i = 0; i < 20; i++) cin >> v[i];\n    \n    // Build adjacency list for the grid\n    vector<vector<vector<pair<int, int>>>> adj(20, vector<vector<pair<int, int>>>(20));\n    for (int i = 0; i < 20; i++) {\n        for (int j = 0; j < 20; j++) {\n            // Right\n            if (j < 19 && h[i][j] == '0') adj[i][j].push_back({i, j + 1});\n            // Left\n            if (j > 0 && h[i][j - 1] == '0') adj[i][j].push_back({i, j - 1});\n            // Down\n            if (i < 19 && v[i][j] == '0') adj[i][j].push_back({i + 1, j});\n            // Up\n            if (i > 0 && v[i - 1][j] == '0') adj[i][j].push_back({i - 1, j});\n        }\n    }\n    \n    // BFS to find shortest path\n    vector<vector<int>> dist(20, vector<int>(20, -1));\n    vector<vector<pair<int, int>>> parent(20, vector<pair<int, int>>(20, {-1, -1}));\n    queue<pair<int, int>> q;\n    q.push({si, sj});\n    dist[si][sj] = 0;\n    \n    while (!q.empty()) {\n        auto [x, y] = q.front(); q.pop();\n        for (auto [nx, ny] : adj[x][y]) {\n            if (dist[nx][ny] == -1) {\n                dist[nx][ny] = dist[x][y] + 1;\n                parent[nx][ny] = {x, y};\n                q.push({nx, ny});\n            }\n        }\n    }\n    \n    // If unreachable (shouldn't happen), output empty\n    if (dist[ti][tj] == -1) {\n        cout << \"\" << endl;\n        return 0;\n    }\n    \n    // Reconstruct the path\n    vector<pair<int, int>> path;\n    pair<int, int> cur = {ti, tj};\n    while (cur != make_pair(si, sj)) {\n        path.push_back(cur);\n        cur = parent[cur.first][cur.second];\n    }\n    path.push_back({si, sj});\n    reverse(path.begin(), path.end());\n    \n    // Generate the move string\n    string full_moves = \"\";\n    for (size_t i = 1; i < path.size(); i++) {\n        auto [px, py] = path[i - 1];\n        auto [cx, cy] = path[i];\n        if (cx == px + 1) full_moves += 'D';\n        else if (cx == px - 1) full_moves += 'U';\n        else if (cy == py + 1) full_moves += 'R';\n        else if (cy == py - 1) full_moves += 'L';\n    }\n    \n    // Handle length <= 200 with repetition for robustness\n    int L = full_moves.size();\n    string output;\n    if (L <= 200) {\n        int repeat = 200 / L;\n        for (int i = 0; i < repeat; i++) output += full_moves;\n        int rem = 200 - output.size();\n        for (int i = 0; i < rem; i++) output += full_moves[i % L];\n    } else {\n        // Truncate to 200 moves\n        output = full_moves.substr(0, 200);\n    }\n    \n    cout << output << endl;\n    return 0;\n}","ahc010":"#include <bits/stdc++.h>\nusing namespace std;\n\nmt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n\nconst vector<vector<int>> to = {\n    {1, 0, -1, -1},  // 0\n    {3, -1, -1, 0},  // 1\n    {-1, -1, 3, 2},  // 2\n    {-1, 2, 1, -1},  // 3\n    {1, 0, 3, 2},    // 4\n    {3, 2, 1, 0},    // 5\n    {2, -1, 0, -1},  // 6\n    {-1, 3, -1, 1}   // 7\n};\n\nconst vector<int> di = {0, -1, 0, 1};\nconst vector<int> dj = {-1, 0, 1, 0};\n\nint get_type(int t, int r) {\n    if (t <= 3) return (t + r) % 4;\n    if (t <= 5) return 4 + (t - 4 + r) % 2;\n    return 6 + (t - 6 + r) % 2;\n}\n\nlong long compute_score(const vector<vector<int>>& tiles, const vector<int>& rot) {\n    vector<vector<int>> adj(900);\n    for (int i = 0; i < 30; i++) {\n        for (int j = 0; j < 30; j++) {\n            int idx = i * 30 + j;\n            int t = tiles[i][j];\n            int r = rot[idx];\n            int et = get_type(t, r);\n            for (int d = 0; d < 4; d++) {\n                if (to[et][d] != -1) {\n                    int d2 = to[et][d];\n                    int ni = i + di[d2];\n                    int nj = j + dj[d2];\n                    if (ni >= 0 && ni < 30 && nj >= 0 && nj < 30) {\n                        int nidx = ni * 30 + nj;\n                        if (ni > i || (ni == i && nj > j)) {\n                            adj[idx].push_back(nidx);\n                            adj[nidx].push_back(idx);\n                        }\n                    }\n                }\n            }\n        }\n    }\n    vector<int> cycle_lengths;\n    vector<bool> visited(900, false);\n    vector<int> disc(900, -1);\n    vector<int> parent(900, -1);\n    int timestamp = 0;\n    function<void(int)> dfs = [&](int u) {\n        visited[u] = true;\n        disc[u] = timestamp++;\n        for (int v : adj[u]) {\n            if (!visited[v]) {\n                parent[v] = u;\n                dfs(v);\n            } else if (v != parent[u] && disc[v] < disc[u]) {\n                int len = disc[u] - disc[v] + 1;\n                cycle_lengths.push_back(len);\n            }\n        }\n    };\n    for (int i = 0; i < 900; i++) if (!visited[i]) dfs(i);\n    sort(cycle_lengths.rbegin(), cycle_lengths.rend());\n    if (cycle_lengths.size() >= 2) {\n        return (long long)cycle_lengths[0] * cycle_lengths[1];\n    }\n    return 0;\n}\n\nint main() {\n    vector<vector<int>> tiles(30, vector<int>(30));\n    for (int i = 0; i < 30; i++) {\n        string s;\n        cin >> s;\n        for (int j = 0; j < 30; j++) tiles[i][j] = s[j] - '0';\n    }\n    vector<int> current(900);\n    for (int &x : current) x = rng() % 4;\n    long long current_score = compute_score(tiles, current);\n    double T = 1000.0;\n    const int iterations = 10000;\n    for (int it = 0; it < iterations; it++) {\n        int idx = rng() % 900;\n        int old_r = current[idx];\n        int new_r = rng() % 4;\n        if (new_r == old_r) continue;\n        current[idx] = new_r;\n        long long new_score = compute_score(tiles, current);\n        long long delta = new_score - current_score;\n        if (delta > 0 || exp(delta / T) > (double)rng() / rng.max()) {\n            current_score = new_score;\n        } else {\n            current[idx] = old_r;\n        }\n        T *= 0.999;\n    }\n    string output;\n    for (int x : current) output += '0' + x;\n    cout << output << endl;\n    return 0;\n}","ahc011":"#include <bits/stdc++.h>\nusing namespace std;\n\nint N, T;\nvector<vector<int>> board;\npair<int, int> empty_pos;\n\n// Directions: 0=U, 1=D, 2=L, 3=R\nvector<pair<int, int>> dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};\nstring dir_chars = \"UDLR\";\n\n// Check if a position is valid\nbool valid(int i, int j) {\n    return i >= 0 && i < N && j >= 0 && j < N;\n}\n\n// Simulate a move: return new board and new empty pos\npair<vector<vector<int>>, pair<int, int>> simulate_move(const vector<vector<int>>& curr_board, pair<int, int> curr_empty, int dir) {\n    auto [ei, ej] = curr_empty;\n    auto [di, dj] = dirs[dir];\n    int ni = ei + di, nj = ej + dj;\n    if (!valid(ni, nj)) return {curr_board, curr_empty}; // Invalid move\n    auto new_board = curr_board;\n    swap(new_board[ei][ej], new_board[ni][nj]);\n    return {new_board, {ni, nj}};\n}\n\n// Compute the size of the largest tree component\nint compute_tree_size(const vector<vector<int>>& curr_board) {\n    vector<vector<bool>> visited(N, vector<bool>(N, false));\n    int max_tree = 0;\n    \n    // Directions for connections: 0=left, 1=up, 2=right, 3=down\n    vector<pair<int, int>> conn_dirs = {{0, -1}, {-1, 0}, {0, 1}, {1, 0}};\n    vector<int> masks = {1, 2, 4, 8}; // left, up, right, down\n    \n    for (int i = 0; i < N; ++i) {\n        for (int j = 0; j < N; ++j) {\n            if (curr_board[i][j] == 0 || visited[i][j]) continue;\n            \n            // BFS for component\n            queue<pair<int, int>> q;\n            q.push({i, j});\n            visited[i][j] = true;\n            int vertices = 0, edges = 0;\n            vector<pair<int, int>> component;\n            \n            while (!q.empty()) {\n                auto [ci, cj] = q.front(); q.pop();\n                vertices++;\n                component.push_back({ci, cj});\n                \n                for (int d = 0; d < 4; ++d) {\n                    auto [di, dj] = conn_dirs[d];\n                    int ni = ci + di, nj = cj + dj;\n                    if (valid(ni, nj) && curr_board[ni][nj] != 0) {\n                        // Check if connection exists\n                        bool has_out = (curr_board[ci][cj] & masks[d]) != 0;\n                        bool has_in = (curr_board[ni][nj] & masks[(d + 2) % 4]) != 0; // Opposite direction\n                        if (has_out && has_in) {\n                            edges++;\n                            if (!visited[ni][nj]) {\n                                visited[ni][nj] = true;\n                                q.push({ni, nj});\n                            }\n                        }\n                    }\n                }\n            }\n            \n            // Check if it's a tree: edges == vertices - 1\n            if (edges == vertices - 1) {\n                max_tree = max(max_tree, vertices);\n            }\n        }\n    }\n    return max_tree;\n}\n\nint main() {\n    cin >> N >> T;\n    board.resize(N, vector<int>(N));\n    for (int i = 0; i < N; ++i) {\n        string s; cin >> s;\n        for (int j = 0; j < N; ++j) {\n            if (s[j] >= '0' && s[j] <= '9') board[i][j] = s[j] - '0';\n            else board[i][j] = 10 + (s[j] - 'a');\n            if (board[i][j] == 0) empty_pos = {i, j};\n        }\n    }\n    \n    string moves = \"\";\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n    uniform_real_distribution<double> dist(0.0, 1.0);\n    \n    int current_score = compute_tree_size(board);\n    const double RANDOM_PROB = 0.1; // Probability to choose random move\n    \n    while (moves.size() < T) {\n        vector<pair<int, string>> candidates;\n        int best_score = -1;\n        string best_move = \"\";\n        \n        // Evaluate all 4 moves\n        for (int d = 0; d < 4; ++d) {\n            auto [new_board, new_empty] = simulate_move(board, empty_pos, d);\n            if (new_empty == empty_pos) continue; // Invalid move\n            int score = compute_tree_size(new_board);\n            if (score > best_score) {\n                best_score = score;\n                best_move = string(1, dir_chars[d]);\n            }\n        }\n        \n        // Decide: greedy or random\n        string chosen_move;\n        if (dist(rng) < RANDOM_PROB || best_move.empty()) {\n            // Random valid move\n            vector<int> valid_dirs;\n            for (int d = 0; d < 4; ++d) {\n                auto [new_board, new_empty] = simulate_move(board, empty_pos, d);\n                if (new_empty != empty_pos) valid_dirs.push_back(d);\n            }\n            if (!valid_dirs.empty()) {\n                uniform_int_distribution<int> rand_dir(0, valid_dirs.size() - 1);\n                chosen_move = string(1, dir_chars[valid_dirs[rand_dir(rng)]]);\n            }\n        } else {\n            chosen_move = best_move;\n        }\n        \n        if (chosen_move.empty()) break; // No valid moves, stop\n        \n        // Apply the move\n        int dir_idx = dir_chars.find(chosen_move[0]);\n        auto [new_board, new_empty] = simulate_move(board, empty_pos, dir_idx);\n        board = new_board;\n        empty_pos = new_empty;\n        moves += chosen_move;\n        \n        // Update current score\n        current_score = compute_tree_size(board);\n        \n        // If full tree, we could stop early, but for now, continue to potentially improve\n        if (current_score == N * N - 1) {\n            // Could add logic to minimize K, e.g., stop if score doesn't improve\n        }\n    }\n    \n    cout << moves << endl;\n    return 0;\n}","ahc012":"#include <bits/stdc++.h>\n\nusing namespace std;\n\nint main() {\n    int N, K;\n    cin >> N >> K;\n    vector<int> a(11);\n    for(int i = 1; i <= 10; i++) cin >> a[i];\n    vector<long long> x(N), y(N);\n    for(int i = 0; i < N; i++) cin >> x[i] >> y[i];\n    \n    // Use up to K=100 lines, each through two randomly chosen strawberries\n    int k = min(K, N * (N - 1) / 2);  // But limit to 100\n    k = min(k, 100);\n    \n    // To make it deterministic, use a fixed seed\n    mt19937 rng(42);\n    uniform_int_distribution<int> dist(0, N - 1);\n    \n    set<pair<pair<long long, long long>, pair<long long, long long>>> used_lines;  // To avoid duplicates\n    \n    vector<tuple<long long, long long, long long, long long>> lines;\n    \n    while (lines.size() < k) {\n        int i = dist(rng);\n        int j = dist(rng);\n        if (i == j) continue;\n        long long px = x[i], py = y[i];\n        long long qx = x[j], qy = y[j];\n        // Normalize the line to avoid duplicates (sort points)\n        if (px > qx || (px == qx && py > qy)) {\n            swap(px, qx);\n            swap(py, qy);\n        }\n        pair<pair<long long, long long>, pair<long long, long long>> line_key = {{px, py}, {qx, qy}};\n        if (used_lines.insert(line_key).second) {\n            lines.emplace_back(px, py, qx, qy);\n        }\n    }\n    \n    // Output\n    cout << lines.size() << endl;\n    for (auto [px, py, qx, qy] : lines) {\n        cout << px << \" \" << py << \" \" << qx << \" \" << qy << endl;\n    }\n    \n    return 0;\n}","ahc014":"#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n\nstruct Operation {\n    int w, x1, y1, x2, y2, x3, y3, x4, y4;\n};\n\nbool cmp(const Operation& a, const Operation& b) {\n    return a.w < b.w;\n}\n\nint main() {\n    int N, M;\n    cin >> N >> M;\n    vector<vector<bool>> has_dot(N, vector<bool>(N, false));\n    int c = (N - 1) / 2;\n    auto calc_w = [&](int x, int y) {\n        int dx = x - c;\n        int dy = y - c;\n        return dx * dx + dy * dy + 1;\n    };\n    for (int i = 0; i < M; i++) {\n        int x, y;\n        cin >> x >> y;\n        has_dot[x][y] = true;\n    }\n    vector<vector<vector<bool>>> horiz(N, vector<vector<bool>>(N, vector<bool>(N, false)));\n    vector<vector<vector<bool>>> vert(N, vector<vector<bool>>(N, vector<bool>(N, false)));\n    vector<vector<bool>> diag1(N - 1, vector<bool>(N - 1, false));\n    vector<vector<bool>> diag2(N - 1, vector<bool>(N, false));\n    vector<vector<bool>> diag3(N, vector<bool>(N - 1, false));\n    vector<vector<bool>> diag4(N, vector<bool>(N, false));\n    vector<Operation> ops;\n    for (int px = 0; px < N; px++) {\n        for (int py = 0; py < N; py++) {\n            if (has_dot[px][py]) continue;\n            int w = calc_w(px, py);\n            // axis-aligned\n            for (int dx = 1; dx <= N - 1 - px; dx++) {\n                for (int dy = 1; dy <= N - 1 - py; dy++) {\n                    int bx = px + dx, by = py;\n                    int cx = px + dx, cy = py + dy;\n                    int ex = px, ey = py + dy;\n                    if (has_dot[bx][by] && has_dot[cx][cy] && has_dot[ex][ey]) {\n                        bool ok = true;\n                        // check sides\n                        for (int i = px + 1; i < bx; i++) if (has_dot[py][i]) ok = false;\n                        for (int i = px + 1; i < bx; i++) if (has_dot[cy][i]) ok = false;\n                        for (int i = py + 1; i < cy; i++) if (has_dot[px][i]) ok = false;\n                        for (int i = py + 1; i < cy; i++) if (has_dot[bx][i]) ok = false;\n                        // check edges\n                        for (int i = px; i < bx; i++) if (horiz[py][i][px]) ok = false;\n                        for (int i = px; i < bx; i++) if (horiz[cy][i][px]) ok = false;\n                        for (int i = py; i < cy; i++) if (vert[px][i][py]) ok = false;\n                        for (int i = py; i < cy; i++) if (vert[bx][i][py]) ok = false;\n                        if (ok) {\n                            ops.push_back({w, px, py, bx, by, cx, cy, ex, ey});\n                        }\n                    }\n                }\n            }\n            // 45-degree\n            for (int a = 1;; a++) {\n                int p2x = px + a, p2y = py + a;\n                int p3x = px, p3y = py + 2 * a;\n                int p4x = px - a, p4y = py + a;\n                if (p2x >= N || p2y >= N || p3y >= N || p4x < 0 || p4y >= N) break;\n                if (has_dot[p2x][p2y] && has_dot[p3x][p3y] && has_dot[p4x][p4y]) {\n                    bool ok = true;\n                    // p1 to p2\n                    for (int i = 0; i < a; i++) if (diag1[px + i][py + i]) ok = false;\n                    // p2 to p3\n                    for (int i = 0; i < a; i++) if (diag3[p2x - i][p2y + i]) ok = false;\n                    // p3 to p4\n                    for (int i = 0; i < a; i++) if (diag4[p3x - i][p3y - i]) ok = false;\n                    // p4 to p1\n                    for (int i = 0; i < a; i++) if (diag2[p4x + i][p4y - i]) ok = false;\n                    if (ok) {\n                        ops.push_back({w, px, py, p2x, p2y, p3x, p3y, p4x, p4y});\n                    }\n                }\n            }\n        }\n    }\n    sort(ops.begin(), ops.end(), cmp);\n    vector<Operation> final_ops;\n    for (auto& op : ops) {\n        int x1 = op.x1, y1 = op.y1;\n        if (has_dot[x1][y1]) continue;\n        int x2 = op.x2, y2 = op.y2;\n        int x3 = op.x3, y3 = op.y3;\n        int x4 = op.x4, y4 = op.y4;\n        if (!has_dot[x2][y2] || !has_dot[x3][y3] || !has_dot[x4][y4]) continue;\n        // check edges again, in case\n        bool ok = true;\n        // assume axis or 45, check accordingly\n        if (x2 == x1 + (x3 - x1) && y2 == y1 + (y3 - y1)) { // axis\n            int dx = x2 - x1, dy = y3 - y1;\n            for (int i = x1; i < x1 + dx; i++) if (horiz[y1][i][x1]) ok = false;\n            for (int i = x1; i < x1 + dx; i++) if (horiz[y1 + dy][i][x1]) ok = false;\n            for (int i = y1; i < y1 + dy; i++) if (vert[x1][i][y1]) ok = false;\n            for (int i = y1; i < y1 + dy; i++) if (vert[x1 + dx][i][y1]) ok = false;\n        } else { // 45\n            int a = x2 - x1;\n            for (int i = 0; i < a; i++) if (diag1[x1 + i][y1 + i]) ok = false;\n            for (int i = 0; i < a; i++) if (diag3[x2 - i][y2 + i]) ok = false;\n            for (int i = 0; i < a; i++) if (diag4[x3 - i][y3 - i]) ok = false;\n            for (int i = 0; i < a; i++) if (diag2[x4 + i][y4 - i]) ok = false;\n        }\n        if (ok) {\n            final_ops.push_back(op);\n            has_dot[x1][y1] = true;\n            // set edges\n            if (x2 == x1 + (x3 - x1) && y2 == y1 + (y3 - y1)) {\n                int dx = x2 - x1, dy = y3 - y1;\n                for (int i = x1; i < x1 + dx; i++) horiz[y1][i][x1] = true;\n                for (int i = x1; i < x1 + dx; i++) horiz[y1 + dy][i][x1] = true;\n                for (int i = y1; i < y1 + dy; i++) vert[x1][i][y1] = true;\n                for (int i = y1; i < y1 + dy; i++) vert[x1 + dx][i][y1] = true;\n            } else {\n                int a = x2 - x1;\n                for (int i = 0; i < a; i++) diag1[x1 + i][y1 + i] = true;\n                for (int i = 0; i < a; i++) diag3[x2 - i][y2 + i] = true;\n                for (int i = 0; i < a; i++) diag4[x3 - i][y3 - i] = true;\n                for (int i = 0; i < a; i++) diag2[x4 + i][y4 - i] = true;\n            }\n        }\n    }\n    cout << final_ops.size() << endl;\n    for (auto& op : final_ops) {\n        cout << op.x1 << \" \" << op.y1 << \" \" << op.x2 << \" \" << op.y2 << \" \" << op.x3 << \" \" << op.y3 << \" \" << op.x4 << \" \" << op.y4 << endl;\n    }\n    return 0;\n}","ahc015":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 10;\nvector<vector<int>> grid(N, vector<int>(N, 0));\n\n// Directions: F (down), B (up), L (left), R (right)\nstring dirs = \"FBLR\";\n\n// Simulate tilt in direction dir\nvoid simulate_tilt(vector<vector<int>>& g, char dir) {\n    if (dir == 'R') { // Right\n        for (int i = 0; i < N; ++i) {\n            vector<int> new_row(N, 0);\n            int pos = N - 1;\n            for (int j = N - 1; j >= 0; --j) {\n                if (g[i][j] != 0) {\n                    new_row[pos] = g[i][j];\n                    --pos;\n                }\n            }\n            g[i] = new_row;\n        }\n    } else if (dir == 'L') { // Left\n        for (int i = 0; i < N; ++i) {\n            vector<int> new_row(N, 0);\n            int pos = 0;\n            for (int j = 0; j < N; ++j) {\n                if (g[i][j] != 0) {\n                    new_row[pos] = g[i][j];\n                    ++pos;\n                }\n            }\n            g[i] = new_row;\n        }\n    } else if (dir == 'F') { // Forward (down)\n        for (int j = 0; j < N; ++j) {\n            vector<int> new_col(N, 0);\n            int pos = N - 1;\n            for (int i = N - 1; i >= 0; --i) {\n                if (g[i][j] != 0) {\n                    new_col[pos] = g[i][j];\n                    --pos;\n                }\n            }\n            for (int i = 0; i < N; ++i) g[i][j] = new_col[i];\n        }\n    } else if (dir == 'B') { // Backward (up)\n        for (int j = 0; j < N; ++j) {\n            vector<int> new_col(N, 0);\n            int pos = 0;\n            for (int i = 0; i < N; ++i) {\n                if (g[i][j] != 0) {\n                    new_col[pos] = g[i][j];\n                    ++pos;\n                }\n            }\n            for (int i = 0; i < N; ++i) g[i][j] = new_col[i];\n        }\n    }\n}\n\n// DFS to compute component size\nint dfs(int x, int y, int fl, vector<vector<int>>& g, vector<vector<bool>>& vis) {\n    vis[x][y] = true;\n    int cnt = 1;\n    int dx[4] = {-1, 0, 1, 0};\n    int dy[4] = {0, 1, 0, -1};\n    for (int d = 0; d < 4; ++d) {\n        int nx = x + dx[d], ny = y + dy[d];\n        if (nx >= 0 && nx < N && ny >= 0 && ny < N && g[nx][ny] == fl && !vis[nx][ny]) {\n            cnt += dfs(nx, ny, fl, g, vis);\n        }\n    }\n    return cnt;\n}\n\n// Compute score for the grid\nlong long compute_score(const vector<vector<int>>& g) {\n    vector<int> component_sizes;\n    for (int fl = 1; fl <= 3; ++fl) {\n        vector<vector<bool>> visited(N, vector<bool>(N, false));\n        for (int i = 0; i < N; ++i) {\n            for (int j = 0; j < N; ++j) {\n                if (g[i][j] == fl && !visited[i][j]) {\n                    int size = dfs(i, j, fl, const_cast<vector<vector<int>>&>(g), visited);\n                    component_sizes.push_back(size);\n                }\n            }\n        }\n    }\n    long long sum_sq = 0;\n    for (int s : component_sizes) sum_sq += 1LL * s * s;\n    vector<long long> d(4, 0);\n    for (int i = 0; i < N; ++i) {\n        for (int j = 0; j < N; ++j) {\n            if (g[i][j]) d[g[i][j]]++;\n        }\n    }\n    long long sum_d_sq = 0;\n    for (int i = 1; i <= 3; ++i) sum_d_sq += d[i] * d[i];\n    if (sum_d_sq == 0) return 0; // Avoid division by zero\n    double score = 1e6 * sum_sq / (double)sum_d_sq;\n    return round(score);\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    vector<int> f(100);\n    for (int i = 0; i < 100; ++i) cin >> f[i];\n    \n    vector<pair<int, int>> empty;\n    for (int i = 0; i < N; ++i) {\n        for (int j = 0; j < N; ++j) {\n            empty.emplace_back(i, j);\n        }\n    }\n    \n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); // For randomization\n    \n    for (int t = 0; t < 100; ++t) {\n        int p;\n        cin >> p;\n        auto [x, y] = empty[p - 1];\n        grid[x][y] = f[t];\n        empty.erase(empty.begin() + p - 1);\n        \n        // Try all directions and collect candidates\n        long long max_score = -1;\n        vector<char> candidates;\n        for (char dir : dirs) {\n            auto temp_grid = grid;\n            simulate_tilt(temp_grid, dir);\n            long long score = compute_score(temp_grid);\n            if (score > max_score) {\n                max_score = score;\n                candidates = {dir};\n            } else if (score >= max_score * 0.95) { // Within 5% of max\n                candidates.push_back(dir);\n            }\n        }\n        \n        // Choose randomly among candidates\n        char best_dir = candidates[rng() % candidates.size()];\n        \n        // Output\n        cout << best_dir << '\\n';\n        cout.flush();\n        \n        // Simulate tilt on actual grid (skip for last step)\n        if (t < 99) {\n            simulate_tilt(grid, best_dir);\n        }\n    }\n    \n    return 0;\n}","ahc016":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int M;\n    double eps;\n    cin >> M >> eps;\n    int N = 100;\n    cout << N << endl;\n    for (int k = 0; k < M; k++) {\n        string s = \"\";\n        for (int i = 0; i < N; i++) {\n            for (int j = i + 1; j < N; j++) {\n                if (i < k && j < k) {\n                    s += '1';\n                } else {\n                    s += '0';\n                }\n            }\n        }\n        cout << s << endl;\n    }\n    cout << flush;\n    vector<vector<int>> targets(M, vector<int>(N));\n    for (int t = 0; t < M; t++) {\n        for (int i = 0; i < N - t; i++) {\n            targets[t][i] = 0;\n        }\n        for (int i = N - t; i < N; i++) {\n            targets[t][i] = t - 1;\n        }\n    }\n    for (int q = 0; q < 100; q++) {\n        string H;\n        cin >> H;\n        vector<int> deg(N, 0);\n        int idx = 0;\n        for (int i = 0; i < N; i++) {\n            for (int j = i + 1; j < N; j++) {\n                if (H[idx] == '1') {\n                    deg[i]++;\n                    deg[j]++;\n                }\n                idx++;\n            }\n        }\n        sort(deg.begin(), deg.end());\n        int best_t = 0;\n        long long min_diff = LLONG_MAX;\n        for (int t = 0; t < M; t++) {\n            long long diff = 0;\n            for (int i = 0; i < N; i++) {\n                diff += abs(deg[i] - targets[t][i]);\n            }\n            if (diff < min_diff) {\n                min_diff = diff;\n                best_t = t;\n            }\n        }\n        cout << best_t << endl;\n        cout << flush;\n    }\n    return 0;\n}","ahc017":"#include <bits/stdc++.h>\n\nusing namespace std;\n\nint main() {\n  ios::sync_with_stdio(false);\n  cin.tie(0);\n  int N, M, D, K;\n  cin >> N >> M >> D >> K;\n  vector<tuple<int, int, int, int>> edges;\n  for (int i = 0; i < M; i++) {\n    int u, v, w;\n    cin >> u >> v >> w;\n    u--;\n    v--;\n    edges.emplace_back(0, u, v, i);  // Placeholder for key, will update with midx\n  }\n  vector<pair<int, int>> coords(N);\n  for (int i = 0; i < N; i++) {\n    int x, y;\n    cin >> x >> y;\n    coords[i] = {x, y};\n  }\n  // Update edges with midx (x-coordinate of midpoint) as the sorting key\n  for (auto& [key, u, v, idx] : edges) {\n    int midx = (coords[u].first + coords[v].first) / 2;\n    key = midx;\n  }\n  // Sort edges by midx ascending (leftmost edges first)\n  sort(edges.begin(), edges.end());\n  // Compute target sizes for each day\n  int total = M;\n  vector<int> max_size(D, total / D);\n  for (int i = 0; i < total % D; i++) max_size[i]++;\n  // Assign edges\n  vector<int> assignment(M);\n  vector<int> current_size(D, 0);\n  for (auto [midx, u, v, idx] : edges) {\n    // Find the day with the smallest current size that has space\n    int best_day = -1;\n    int min_size = INT_MAX;\n    for (int d = 0; d < D; d++) {\n      if (current_size[d] < max_size[d] && current_size[d] < min_size) {\n        min_size = current_size[d];\n        best_day = d;\n      }\n    }\n    assert(best_day != -1);\n    assignment[idx] = best_day;\n    current_size[best_day]++;\n  }\n  // Output the assignment\n  for (int i = 0; i < M; i++) {\n    if (i > 0) cout << \" \";\n    cout << assignment[i] + 1;\n  }\n  cout << \"\\n\";\n  return 0;\n}","ahc019":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MAX_D = 14;\nint D;\nvector<string> f[2], r[2];\nvector<vector<vector<int>>> grid[2]; // grid[i][x][y][z]\nvector<int> b[2]; // flattened b[i], size D*D*D\n\n// Directions for 6-connectivity (face-adjacent)\nconst int dx[6] = {1, -1, 0, 0, 0, 0};\nconst int dy[6] = {0, 0, 1, -1, 0, 0};\nconst int dz[6] = {0, 0, 0, 0, 1, -1};\n\n// Canonicalize a shape: translate to (0,0,0), try 24 rotations, pick lex smallest\nstring canonicalize(const vector<tuple<int,int,int>>& shape) {\n    if (shape.empty()) return \"\";\n    // Find min coords\n    int minx = INT_MAX, miny = INT_MAX, minz = INT_MAX;\n    for (auto [x,y,z] : shape) {\n        minx = min(minx, x);\n        miny = min(miny, y);\n        minz = min(minz, z);\n    }\n    // Translate\n    vector<tuple<int,int,int>> trans;\n    for (auto [x,y,z] : shape) {\n        trans.emplace_back(x - minx, y - miny, z - minz);\n    }\n    // Generate 24 rotations (90 deg around x,y,z)\n    vector<string> candidates;\n    for (int rx = 0; rx < 4; ++rx) {\n        for (int ry = 0; ry < 4; ++ry) {\n            for (int rz = 0; rz < 4; ++rz) {\n                if (__builtin_popcount(rx) + __builtin_popcount(ry) + __builtin_popcount(rz) > 3) continue; // Only 24 unique\n                vector<tuple<int,int,int>> rot;\n                for (auto [x,y,z] : trans) {\n                    // Apply rotations\n                    int nx = x, ny = y, nz = z;\n                    for (int i = 0; i < rx; ++i) { int t = ny; ny = -nz; nz = t; } // Rotate x\n                    for (int i = 0; i < ry; ++i) { int t = nz; nz = -nx; nx = t; } // Rotate y\n                    for (int i = 0; i < rz; ++i) { int t = nx; nx = -ny; ny = t; } // Rotate z\n                    rot.emplace_back(nx, ny, nz);\n                }\n                // Sort and stringify\n                sort(rot.begin(), rot.end());\n                string s;\n                for (auto [x,y,z] : rot) {\n                    s += to_string(x) + \",\" + to_string(y) + \",\" + to_string(z) + \";\";\n                }\n                candidates.push_back(s);\n            }\n        }\n    }\n    return *min_element(candidates.begin(), candidates.end());\n}\n\n// DFS to find connected component\nvoid dfs(int i, int x, int y, int z, vector<tuple<int,int,int>>& component, vector<vector<vector<bool>>>& visited) {\n    visited[x][y][z] = true;\n    component.emplace_back(x, y, z);\n    for (int d = 0; d < 6; ++d) {\n        int nx = x + dx[d], ny = y + dy[d], nz = z + dz[d];\n        if (nx >= 0 && nx < D && ny >= 0 && ny < D && nz >= 0 && nz < D &&\n            grid[i][nx][ny][nz] && !visited[nx][ny][nz]) {\n            dfs(i, nx, ny, nz, component, visited);\n        }\n    }\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    cin >> D;\n    f[0].resize(D); f[1].resize(D);\n    r[0].resize(D); r[1].resize(D);\n    for (int i = 0; i < 2; ++i) {\n        for (int k = 0; k < D; ++k) cin >> f[i][k];\n        for (int k = 0; k < D; ++k) cin >> r[i][k];\n    }\n    \n    // Build grids\n    grid[0].assign(D, vector<vector<int>>(D, vector<int>(D, 0)));\n    grid[1].assign(D, vector<vector<int>>(D, vector<int>(D, 0)));\n    for (int i = 0; i < 2; ++i) {\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 (f[i][z][x] == '1' && r[i][z][y] == '1') {\n                        grid[i][x][y][z] = 1;\n                    }\n                }\n            }\n        }\n    }\n    \n    // Find components for each grid\n    vector<vector<vector<tuple<int,int,int>>>> components(2);\n    for (int i = 0; i < 2; ++i) {\n        vector<vector<vector<bool>>> visited(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                    if (grid[i][x][y][z] && !visited[x][y][z]) {\n                        vector<tuple<int,int,int>> comp;\n                        dfs(i, x, y, z, comp, visited);\n                        components[i].push_back(comp);\n                    }\n                }\n            }\n        }\n    }\n    \n    // Assign block IDs with shape matching\n    map<string, int> shape_to_id;\n    int n = 0;\n    b[0].assign(D * D * D, 0);\n    b[1].assign(D * D * D, 0);\n    \n    // Process i=0 first\n    for (const auto& comp : components[0]) {\n        string shape = canonicalize(comp);\n        if (shape_to_id.find(shape) == shape_to_id.end()) {\n            shape_to_id[shape] = ++n;\n        }\n        int id = shape_to_id[shape];\n        for (auto [x,y,z] : comp) {\n            b[0][x * D * D + y * D + z] = id;\n        }\n    }\n    \n    // Process i=1, reusing IDs if possible\n    for (const auto& comp : components[1]) {\n        string shape = canonicalize(comp);\n        if (shape_to_id.find(shape) == shape_to_id.end()) {\n            shape_to_id[shape] = ++n;\n        }\n        int id = shape_to_id[shape];\n        for (auto [x,y,z] : comp) {\n            b[1][x * D * D + y * D + z] = id;\n        }\n    }\n    \n    // Output\n    cout << n << '\\n';\n    for (int val : b[0]) cout << val << ' ';\n    cout << '\\n';\n    for (int val : b[1]) cout << val << ' ';\n    cout << '\\n';\n    \n    return 0;\n}","ahc020":"#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing ld = long double;\n\nstruct Edge {\n    int u, v, w, idx;\n    bool operator<(const Edge& other) const {\n        return w < other.w;\n    }\n};\n\nint find(vector<int>& parent, int x) {\n    if (parent[x] != x) parent[x] = find(parent, parent[x]);\n    return parent[x];\n}\n\nvoid unite(vector<int>& parent, vector<int>& rank, int x, int y) {\n    int px = find(parent, x), py = find(parent, y);\n    if (px != py) {\n        if (rank[px] < rank[py]) swap(px, py);\n        parent[py] = px;\n        if (rank[px] == rank[py]) rank[px]++;\n    }\n}\n\nld compute_dist(pair<int, int> a, pair<int, int> b) {\n    ld dx = a.first - b.first;\n    ld dy = a.second - b.second;\n    return hypot(dx, dy);\n}\n\nll compute_cost(const vector<int>& P, const vector<int>& B, const vector<Edge>& edges) {\n    ll s = 0;\n    for (int p : P) s += (ll)p * p;\n    for (int i = 0; i < B.size(); ++i) {\n        if (B[i]) s += edges[i].w;\n    }\n    return s;\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    vector<pair<int, int>> stations(N);\n    for (int i = 0; i < N; ++i) {\n        cin >> stations[i].first >> stations[i].second;\n    }\n    vector<Edge> edges(M);\n    for (int i = 0; i < M; ++i) {\n        cin >> edges[i].u >> edges[i].v >> edges[i].w;\n        edges[i].u--; edges[i].v--; // 0-based\n        edges[i].idx = i;\n    }\n    vector<pair<int, int>> residents(K);\n    for (int i = 0; i < K; ++i) {\n        cin >> residents[i].first >> residents[i].second;\n    }\n\n    // Precompute distances from residents to stations\n    vector<vector<ld>> dist(K, vector<ld>(N));\n    for (int k = 0; k < K; ++k) {\n        for (int i = 0; i < N; ++i) {\n            dist[k][i] = compute_dist(residents[k], stations[i]);\n        }\n    }\n\n    // Initial assignment: each resident to closest station\n    vector<int> closest(K);\n    for (int k = 0; k < K; ++k) {\n        ld min_d = 1e18;\n        for (int i = 0; i < N; ++i) {\n            if (dist[k][i] < min_d) {\n                min_d = dist[k][i];\n                closest[k] = i;\n            }\n        }\n    }\n\n    // Active stations: include all assigned + vertex 1\n    set<int> active_stations;\n    for (int k = 0; k < K; ++k) {\n        active_stations.insert(closest[k]);\n    }\n    active_stations.insert(0); // Vertex 1 (0-based)\n\n    // Compute MST on active_stations\n    vector<int> parent(N), rankk(N, 0);\n    for (int i = 0; i < N; ++i) parent[i] = i;\n    sort(edges.begin(), edges.end());\n    vector<int> B(M, 0);\n    for (auto& e : edges) {\n        if (active_stations.count(e.u) && active_stations.count(e.v)) {\n            int pu = find(parent, e.u), pv = find(parent, e.v);\n            if (pu != pv) {\n                unite(parent, rankk, e.u, e.v);\n                B[e.idx] = 1;\n            }\n        }\n    }\n\n    // Set initial P_i\n    vector<int> P(N, 0);\n    vector<vector<ld>> assigned_dists(N);\n    for (int k = 0; k < K; ++k) {\n        int i = closest[k];\n        assigned_dists[i].push_back(dist[k][i]);\n    }\n    for (int i = 0; i < N; ++i) {\n        if (!assigned_dists[i].empty()) {\n            ld max_d = *max_element(assigned_dists[i].begin(), assigned_dists[i].end());\n            P[i] = min(5000, (int)ceil(max_d));\n        }\n    }\n\n    // Local search: try reassigning each resident to next-closest if it reduces cost\n    ll current_cost = compute_cost(P, B, edges);\n    const int max_iters = 1000; // Limit for time\n    for (int iter = 0; iter < max_iters; ++iter) {\n        bool improved = false;\n        for (int k = 0; k < K; ++k) {\n            int current_i = closest[k];\n            ld current_d = dist[k][current_i];\n            // Find next-closest\n            ld next_d = 1e18;\n            int next_i = -1;\n            for (int i = 0; i < N; ++i) {\n                if (i != current_i && dist[k][i] < next_d) {\n                    next_d = dist[k][i];\n                    next_i = i;\n                }\n            }\n            if (next_i == -1 || next_d > 5000) continue; // No better option\n\n            // Simulate reassignment\n            vector<int> new_P = P;\n            vector<int> new_B = B;\n            set<int> new_active = active_stations;\n\n            // Remove from current\n            assigned_dists[current_i].erase(remove(assigned_dists[current_i].begin(), assigned_dists[current_i].end(), current_d), assigned_dists[current_i].end());\n            if (assigned_dists[current_i].empty()) {\n                new_P[current_i] = 0;\n                new_active.erase(current_i);\n            } else {\n                ld max_d = *max_element(assigned_dists[current_i].begin(), assigned_dists[current_i].end());\n                new_P[current_i] = min(5000, (int)ceil(max_d));\n            }\n\n            // Add to next\n            assigned_dists[next_i].push_back(next_d);\n            ld max_d_next = *max_element(assigned_dists[next_i].begin(), assigned_dists[next_i].end());\n            new_P[next_i] = min(5000, (int)ceil(max_d_next));\n            new_active.insert(next_i);\n\n            // Recompute MST if active set changed\n            if (new_active != active_stations) {\n                fill(new_B.begin(), new_B.end(), 0);\n                vector<int> new_parent(N), new_rankk(N, 0);\n                for (int i = 0; i < N; ++i) new_parent[i] = i;\n                for (auto& e : edges) {\n                    if (new_active.count(e.u) && new_active.count(e.v)) {\n                        int pu = find(new_parent, e.u), pv = find(new_parent, e.v);\n                        if (pu != pv) {\n                            unite(new_parent, new_rankk, e.u, e.v);\n                            new_B[e.idx] = 1;\n                        }\n                    }\n                }\n            }\n\n            ll new_cost = compute_cost(new_P, new_B, edges);\n            if (new_cost < current_cost) {\n                // Accept\n                closest[k] = next_i;\n                P = new_P;\n                B = new_B;\n                active_stations = new_active;\n                current_cost = new_cost;\n                improved = true;\n            } else {\n                // Revert\n                assigned_dists[current_i].push_back(current_d);\n                assigned_dists[next_i].pop_back();\n            }\n        }\n        if (!improved) break;\n    }\n\n    // Output\n    for (int i = 0; i < N; ++i) {\n        if (i > 0) cout << \" \";\n        cout << P[i];\n    }\n    cout << \"\\n\";\n    for (int i = 0; i < M; ++i) {\n        if (i > 0) cout << \" \";\n        cout << B[i];\n    }\n    cout << \"\\n\";\n\n    return 0;\n}","ahc021":"#include <bits/stdc++.h>\n\nusing namespace std;\n\nint main() {\n    vector<vector<int>> pyramid(30);\n    for (int x = 0; x < 30; x++) {\n        pyramid[x].resize(x + 1);\n        for (int y = 0; y <= x; y++) {\n            cin >> pyramid[x][y];\n        }\n    }\n\n    vector<tuple<int, int, int, int>> ops;\n\n    // Recursive lambda for sift_down\n    auto sift_down = [&](auto& self, int x, int y) -> void {\n        if (x >= 29) return;\n        int val = pyramid[x][y];\n        int lx = x + 1, ly = y;\n        int rx = x + 1, ry = y + 1;\n        int lval = pyramid[lx][ly];\n        int rval = pyramid[rx][ry];\n        if (lval >= val && rval >= val) return;\n        if (lval < val && lval <= rval) {\n            // Swap with left child\n            swap(pyramid[x][y], pyramid[lx][ly]);\n            ops.push_back({x, y, lx, ly});\n            self(self, lx, ly);\n        } else if (rval < val) {\n            // Swap with right child\n            swap(pyramid[x][y], pyramid[rx][ry]);\n            ops.push_back({x, y, rx, ry});\n            self(self, rx, ry);\n        }\n    };\n\n    // Build the heap by heapifying from bottom to top\n    for (int x = 28; x >= 0; x--) {\n        for (int y = 0; y <= x; y++) {\n            sift_down(sift_down, x, y);\n        }\n    }\n\n    // Output the result\n    cout << ops.size() << endl;\n    for (auto [a, b, c, d] : ops) {\n        cout << a << \" \" << b << \" \" << c << \" \" << d << endl;\n    }\n\n    return 0;\n}","toyota2023summer-final":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int D, N;\n    cin >> D >> N;\n    vector<vector<int>> obs(D, vector<int>(D, 0));\n    for (int k = 0; k < N; ++k) {\n        int ri, rj;\n        cin >> ri >> rj;\n        obs[ri][rj] = 1;\n    }\n    int entrance_i = 0, entrance_j = (D - 1) / 2;\n    obs[entrance_i][entrance_j] = 1; // Block entrance for containers\n\n    // Precompute distances from entrance, avoiding obstacles\n    vector<vector<int>> dist(D, vector<int>(D, -1));\n    queue<pair<int, int>> q;\n    q.push({entrance_i, entrance_j});\n    dist[entrance_i][entrance_j] = 0;\n    vector<pair<int, int>> dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};\n    while (!q.empty()) {\n        auto [i, j] = q.front();\n        q.pop();\n        for (auto [di, dj] : dirs) {\n            int ni = i + di, nj = j + dj;\n            if (ni >= 0 && ni < D && nj >= 0 && nj < D && obs[ni][nj] == 0 && dist[ni][nj] == -1) {\n                dist[ni][nj] = dist[i][j] + 1;\n                q.push({ni, nj});\n            }\n        }\n    }\n\n    int M = D * D - 1 - N; // Number of containers\n    vector<vector<int>> placed(D, vector<int>(D, -1)); // -1: empty, else: container number\n\n    // Placement phase\n    for (int d = 0; d < M; ++d) {\n        int t_d;\n        cin >> t_d;\n        // BFS to find reachable empty squares\n        vector<vector<int>> vis(D, vector<int>(D, 0));\n        queue<pair<int, int>> qq;\n        qq.push({entrance_i, entrance_j});\n        vis[entrance_i][entrance_j] = 1;\n        vector<tuple<int, int, int>> candidates; // dist, i, j\n        while (!qq.empty()) {\n            auto [i, j] = qq.front();\n            qq.pop();\n            for (auto [di, dj] : dirs) {\n                int ni = i + di, nj = j + dj;\n                if (ni >= 0 && ni < D && nj >= 0 && nj < D && obs[ni][nj] == 0 && placed[ni][nj] == -1 && !vis[ni][nj]) {\n                    vis[ni][nj] = 1;\n                    qq.push({ni, nj});\n                    candidates.emplace_back(-dist[ni][nj], ni, nj); // Negative for max-heap effect\n                }\n            }\n        }\n        // Choose the candidate with max dist, then min i, min j\n        sort(candidates.begin(), candidates.end()); // Sorts by -dist, then i, then j\n        auto [_, pi, pj] = candidates[0];\n        placed[pi][pj] = t_d;\n        cout << pi << \" \" << pj << endl;\n    }\n\n    // Removal phase\n    vector<pair<int, int>> order;\n    while (true) {\n        // BFS to find accessible containers\n        vector<vector<int>> vis(D, vector<int>(D, 0));\n        queue<pair<int, int>> qq;\n        qq.push({entrance_i, entrance_j});\n        vis[entrance_i][entrance_j] = 1;\n        vector<pair<int, int>> acc; // {number, id} where id = i*D + j\n        while (!qq.empty()) {\n            auto [i, j] = qq.front();\n            qq.pop();\n            for (auto [di, dj] : dirs) {\n                int ni = i + di, nj = j + dj;\n                if (ni >= 0 && ni < D && nj >= 0 && nj < D && obs[ni][nj] == 0 && placed[ni][nj] == -1 && !vis[ni][nj]) {\n                    vis[ni][nj] = 1;\n                    qq.push({ni, nj});\n                    if (placed[ni][nj] != -1) {\n                        acc.emplace_back(placed[ni][nj], ni * D + nj);\n                    }\n                }\n            }\n        }\n        if (acc.empty()) break;\n        // Remove the smallest number\n        sort(acc.begin(), acc.end());\n        auto [num, id] = acc[0];\n        int ii = id / D, jj = id % D;\n        order.emplace_back(ii, jj);\n        placed[ii][jj] = -1;\n    }\n\n    // Output removal order\n    for (auto [qi, qj] : order) {\n        cout << qi << \" \" << qj << endl;\n    }\n\n    return 0;\n}","ahc024":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int n, m;\n    cin >> n >> m;\n    vector<vector<int>> c(n, vector<int>(n));\n    for(int i = 0; i < n; i++) {\n        for(int j = 0; j < n; j++) {\n            cin >> c[i][j];\n        }\n    }\n    \n    vector<vector<int>> adj(m + 1, vector<int>(m + 1, 0));\n    vector<int> is_boundary(m + 1, 0);\n    vector<int> di = {0, 1, 0, -1};\n    vector<int> dj = {1, 0, -1, 0};\n    \n    // Compute adjacencies and boundary info\n    for(int i = 0; i < n; i++) {\n        for(int j = 0; j < n; j++) {\n            int col = c[i][j];\n            if(i == 0 || i == n - 1 || j == 0 || j == n - 1) {\n                is_boundary[col] = 1;\n                adj[col][0] = adj[0][col] = 1;\n            }\n            for(int d = 0; d < 4; d++) {\n                int ni = i + di[d], nj = j + dj[d];\n                if(ni >= 0 && ni < n && nj >= 0 && nj < n) {\n                    int ncol = c[ni][nj];\n                    if(ncol != col) {\n                        adj[col][ncol] = adj[ncol][col] = 1;\n                    }\n                }\n            }\n        }\n    }\n    \n    // Grid for output\n    vector<vector<int>> grid(n, vector<int>(n, 0));\n    vector<pair<int, int>> pos(m + 1, {-1, -1});\n    vector<bool> used(n * n, false);\n    \n    // Sort wards by degree (number of adjacencies) descending\n    vector<pair<int, int>> order;\n    for(int cc = 1; cc <= m; cc++) {\n        int deg = 0;\n        for(int d = 0; d <= m; d++) {\n            deg += adj[cc][d];\n        }\n        order.emplace_back(-deg, cc);\n    }\n    sort(order.begin(), order.end());\n    \n    // Place wards\n    for(auto& p : order) {\n        int cc = p.second;\n        bool found = false;\n        for(int idx = 0; idx < n * n; idx++) {\n            int i = idx / n, j = idx % n;\n            if(used[idx]) continue;\n            bool on_bound = (i == 0 || i == n - 1 || j == 0 || j == n - 1);\n            if(is_boundary[cc] != on_bound) continue;\n            bool ok = true;\n            for(int d = 1; d <= m; d++) {\n                if(pos[d].first == -1) continue;\n                int pi = pos[d].first, pj = pos[d].second;\n                int dist = abs(i - pi) + abs(j - pj);\n                if(adj[cc][d]) {\n                    if(dist != 1) ok = false;\n                } else {\n                    if(dist == 1) ok = false;\n                }\n            }\n            if(ok) {\n                pos[cc] = {i, j};\n                used[idx] = true;\n                grid[i][j] = cc;\n                found = true;\n                break;\n            }\n        }\n        if(!found) {\n            // Fallback: place at first boundary-matching position\n            for(int idx = 0; idx < n * n; idx++) {\n                int i = idx / n, j = idx % n;\n                if(used[idx]) continue;\n                bool on_bound = (i == 0 || i == n - 1 || j == 0 || j == n - 1);\n                if(is_boundary[cc] == on_bound) {\n                    pos[cc] = {i, j};\n                    used[idx] = true;\n                    grid[i][j] = cc;\n                    break;\n                }\n            }\n        }\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    \n    return 0;\n}","ahc025":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int N, D, Q;\n    cin >> N >> D >> Q;\n    \n    vector<int> items(N);\n    iota(items.begin(), items.end(), 0);  // items[i] = i\n    \n    int query_count = 0;\n    \n    // Comparator for sorting: compares weights of two items using a query\n    auto cmp = [&](int a, int b) -> bool {\n        cout << 1 << \" \" << 1 << \" \" << a << \" \" << b << endl;\n        cout.flush();\n        string res;\n        cin >> res;\n        query_count++;\n        if (res == \"<\") return true;   // a < b\n        if (res == \">\") return false;  // a > b\n        return true;  // a == b, arbitrary\n    };\n    \n    // Sort items by weight (lightest to heaviest)\n    sort(items.begin(), items.end(), cmp);\n    \n    // Assign groups using round-robin on the sorted list\n    vector<int> group(N);\n    vector<vector<int>> groups(D);\n    vector<int> pos(N);\n    for (int i = 0; i < N; i++) {\n        pos[items[i]] = i;  // pos[item] = position in sorted list\n        int g = i % D;\n        group[items[i]] = g;\n        groups[g].push_back(items[i]);\n    }\n    \n    // Refinements: Balance groups using remaining queries\n    int g1 = 0, g2 = 1;\n    while (query_count < Q) {\n        if (groups[g1].empty() || groups[g2].empty()) {\n            // Skip if a group is empty; do dummy query\n            cout << 1 << \" \" << 1 << \" \" << 0 << \" \" << 1 << endl;\n            cout.flush();\n            string dummy;\n            cin >> dummy;\n            query_count++;\n            continue;\n        }\n        \n        // Compare sums of groups g1 and g2\n        cout << groups[g1].size() << \" \" << groups[g2].size();\n        for (int x : groups[g1]) cout << \" \" << x;\n        for (int x : groups[g2]) cout << \" \" << x;\n        cout << endl;\n        cout.flush();\n        string res;\n        cin >> res;\n        query_count++;\n        \n        if (res == \">\") {  // groups[g1] > groups[g2]\n            // Move heaviest item from g1 to g2\n            auto it = max_element(groups[g1].begin(), groups[g1].end(), [&](int a, int b) {\n                return pos[a] < pos[b];\n            });\n            int item = *it;\n            groups[g1].erase(it);\n            groups[g2].push_back(item);\n            group[item] = g2;\n        } else if (res == \"<\") {  // groups[g1] < groups[g2]\n            // Move heaviest item from g2 to g1\n            auto it = max_element(groups[g2].begin(), groups[g2].end(), [&](int a, int b) {\n                return pos[a] < pos[b];\n            });\n            int item = *it;\n            groups[g2].erase(it);\n            groups[g1].push_back(item);\n            group[item] = g1;\n        }  // If ==, do nothing\n        \n        // Cycle to next pair of groups\n        g2 = (g2 + 1) % D;\n        if (g2 == g1) g2 = (g2 + 1) % D;\n    }\n    \n    // Output the group assignment\n    for (int i = 0; i < N; i++) {\n        if (i > 0) cout << \" \";\n        cout << group[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    vector<vector<int>> stacks(m);\n    vector<int> position(n + 1);\n    for (int i = 0; i < m; i++) {\n        stacks[i].resize(n / m);\n        for (int j = 0; j < n / m; j++) {\n            cin >> stacks[i][j];\n            position[stacks[i][j]] = i;\n        }\n    }\n    vector<bool> removed(n + 1, false);\n    \n    for (int v = 1; v <= n; v++) {\n        if (removed[v]) continue;\n        int s = position[v];\n        // Find index of v in stacks[s]\n        int idx = -1;\n        for (int j = 0; j < stacks[s].size(); j++) {\n            if (stacks[s][j] == v) {\n                idx = j;\n                break;\n            }\n        }\n        int h = stacks[s].size() - 1 - idx; // Boxes above v\n        while (h > 0) {\n            // Move the top h boxes: Find the box at stacks[s][size - (h+1)]\n            int move_idx = stacks[s].size() - (h + 1);\n            int box_to_move = stacks[s][move_idx];\n            // Choose destination t: Stack with smallest size, not s\n            int min_size = INT_MAX;\n            int t = -1;\n            for (int i = 0; i < m; i++) {\n                if (i != s && (int)stacks[i].size() < min_size) {\n                    min_size = stacks[i].size();\n                    t = i;\n                }\n            }\n            // Perform move: Extract from move_idx to end\n            vector<int> to_move(stacks[s].begin() + move_idx, stacks[s].end());\n            stacks[s].resize(move_idx);\n            // Append to t\n            stacks[t].insert(stacks[t].end(), to_move.begin(), to_move.end());\n            // Update positions\n            for (int bx : to_move) {\n                position[bx] = t;\n            }\n            // Output move operation: box_to_move and t+1 (1-based)\n            cout << box_to_move << \" \" << (t + 1) << endl;\n            // Update current stack to t, h remains the same\n            s = t;\n        }\n        // Now h == 0, remove v\n        cout << v << \" \" << 0 << endl;\n        removed[v] = true;\n        // Remove from stack\n        stacks[s].pop_back();\n    }\n    return 0;\n}","ahc027":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int N;\n    cin >> N;\n    vector<string> h(N-1);\n    for(int i=0; i<N-1; i++) cin >> h[i];\n    vector<string> v(N);\n    for(int i=0; i<N; i++) cin >> v[i];\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    vector<vector<bool>> visited(N, vector<bool>(N, false));\n    vector<pair<int,int>> DIJ = {{0,1},{1,0},{0,-1},{-1,0}};\n    string DIR = \"RDLU\";\n    \n    function<void(int,int)> dfs = [&](int i, int j) {\n        visited[i][j] = true;\n        vector<tuple<int, int, int, int>> candidates; // d, dir, i2, j2\n        for(int dir=0; dir<4; dir++) {\n            int di = DIJ[dir].first, dj = DIJ[dir].second;\n            int i2 = i + di, j2 = j + dj;\n            if(i2 >= 0 && i2 < N && j2 >= 0 && j2 < N && !visited[i2][j2]) {\n                bool can = false;\n                if(di == 0) { // horizontal\n                    int jj = min(j, j2);\n                    if(v[i][jj] == '0') can = true;\n                } else { // vertical\n                    int ii = min(i, i2);\n                    if(h[ii][j] == '0') can = true;\n                }\n                if(can) {\n                    candidates.emplace_back(d[i2][j2], dir, i2, j2);\n                }\n            }\n        }\n        sort(candidates.rbegin(), candidates.rend()); // sort by d descending\n        for(auto& [_, dir, i2, j2] : candidates) {\n            cout << DIR[dir];\n            dfs(i2, j2);\n            cout << DIR[(dir + 2) % 4];\n        }\n    };\n    \n    dfs(0, 0);\n    cout << endl;\n    return 0;\n}","ahc028":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int N, M;\n    cin >> N >> M;\n    int si, sj;\n    cin >> si >> sj;\n    vector<vector<char>> A(N, vector<char>(N));\n    for (int i = 0; i < N; i++) {\n        string row;\n        cin >> row;\n        for (int j = 0; j < N; j++) {\n            A[i][j] = row[j];\n        }\n    }\n    vector<string> t(M);\n    for (int k = 0; k < M; k++) {\n        cin >> t[k];\n    }\n\n    // Precompute hashes for t_k\n    const unsigned long long BASE = 31;\n    vector<unsigned long long> t_hash(M);\n    for (int k = 0; k < M; k++) {\n        unsigned long long h = 0;\n        for (char c : t[k]) {\n            h = h * BASE + (c - 'A' + 1);\n        }\n        t_hash[k] = h;\n    }\n\n    // Initial state\n    int cur_i = si, cur_j = sj;\n    string S = \"\";\n    vector<bool> covered(M, false);\n    int covered_count = 0;\n    unordered_set<unsigned long long> hashes;\n    vector<pair<int, int>> operations;\n    long long total_cost = 0;\n\n    // Main loop\n    while (operations.size() < 5000 && covered_count < M) {\n        int best_new = -1;\n        long long best_cost = LLONG_MAX;\n        pair<int, int> best_pos = {-1, -1};\n\n        for (int ii = 0; ii < N; ii++) {\n            for (int jj = 0; jj < N; jj++) {\n                long long cost = abs(ii - cur_i) + abs(jj - cur_j) + 1;\n                string temp_S = S + A[ii][jj];\n\n                // Compute new hash if possible\n                unordered_set<unsigned long long> temp_hashes = hashes;\n                if (temp_S.size() >= 5) {\n                    string sub = temp_S.substr(temp_S.size() - 5, 5);\n                    unsigned long long h = 0;\n                    for (char c : sub) {\n                        h = h * BASE + (c - 'A' + 1);\n                    }\n                    temp_hashes.insert(h);\n                }\n\n                // Count new covered\n                int new_cov = 0;\n                for (int k = 0; k < M; k++) {\n                    if (!covered[k] && temp_hashes.count(t_hash[k])) {\n                        new_cov++;\n                    }\n                }\n\n                // Update best\n                if (new_cov > best_new || (new_cov == best_new && cost < best_cost)) {\n                    best_new = new_cov;\n                    best_cost = cost;\n                    best_pos = {ii, jj};\n                }\n            }\n        }\n\n        // If no valid move, break (shouldn't happen)\n        if (best_pos.first == -1) break;\n\n        // Perform the move\n        int ii = best_pos.first, jj = best_pos.second;\n        operations.push_back({ii, jj});\n        total_cost += best_cost;\n        S += A[ii][jj];\n        cur_i = ii;\n        cur_j = jj;\n\n        // Update hashes\n        if (S.size() >= 5) {\n            string sub = S.substr(S.size() - 5, 5);\n            unsigned long long h = 0;\n            for (char c : sub) {\n                h = h * BASE + (c - 'A' + 1);\n            }\n            hashes.insert(h);\n        }\n\n        // Update covered\n        for (int k = 0; k < M; k++) {\n            if (!covered[k] && hashes.count(t_hash[k])) {\n                covered[k] = true;\n                covered_count++;\n            }\n        }\n    }\n\n    // Output\n    for (auto& op : operations) {\n        cout << op.first << \" \" << op.second << endl;\n    }\n\n    return 0;\n}","ahc030":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int N, M;\n    double eps;\n    cin >> N >> M >> eps;\n    for (int k = 0; k < M; ++k) {\n        int d;\n        cin >> d;\n        for (int i = 0; i < d; ++i) {\n            int x, y;\n            cin >> x >> y;\n        }\n    }\n\n    set<pair<int, int>> oil_set;\n    vector<pair<int, int>> assumed_zero;\n    const int THRESHOLD = 10;\n\n    function<void(vector<pair<int, int>>)> process = [&](vector<pair<int, int>> reg) {\n        if (reg.empty()) return;\n        int sz = reg.size();\n        if (sz <= THRESHOLD) {\n            for (auto [i, j] : reg) {\n                cout << \"q 1 \" << i << \" \" << j << endl;\n                cout.flush();\n                int v;\n                cin >> v;\n                if (v > 0) oil_set.insert({i, j});\n            }\n            return;\n        }\n        // divine\n        cout << \"q \" << sz;\n        for (auto [i, j] : reg) cout << \" \" << i << \" \" << j;\n        cout << endl;\n        cout.flush();\n        int s;\n        cin >> s;\n        if (s == 0) {\n            for (auto p : reg) assumed_zero.push_back(p);\n            return;\n        }\n        // subdivide\n        int min_i = INT_MAX, max_i = INT_MIN, min_j = INT_MAX, max_j = INT_MIN;\n        for (auto [i, j] : reg) {\n            min_i = min(min_i, i);\n            max_i = max(max_i, i);\n            min_j = min(min_j, j);\n            max_j = max(max_j, j);\n        }\n        int mid_i = (min_i + max_i) / 2;\n        int mid_j = (min_j + max_j) / 2;\n        vector<pair<int, int>> tl, tr, bl, br;\n        for (auto p : reg) {\n            int i = p.first, j = p.second;\n            if (i <= mid_i) {\n                if (j <= mid_j) tl.push_back(p);\n                else tr.push_back(p);\n            } else {\n                if (j <= mid_j) bl.push_back(p);\n                else br.push_back(p);\n            }\n        }\n        process(tl);\n        process(tr);\n        process(bl);\n        process(br);\n    };\n\n    vector<pair<int, int>> all;\n    for (int i = 0; i < N; ++i) {\n        for (int j = 0; j < N; ++j) {\n            all.push_back({i, j});\n        }\n    }\n    process(all);\n\n    while (true) {\n        cout << \"a \" << oil_set.size();\n        for (auto [i, j] : oil_set) cout << \" \" << i << \" \" << j;\n        cout << endl;\n        cout.flush();\n        int res;\n        cin >> res;\n        if (res == 1) break;\n        // drill the assumed_zero\n        for (auto [i, j] : assumed_zero) {\n            cout << \"q 1 \" << i << \" \" << j << endl;\n            cout.flush();\n            int v;\n            cin >> v;\n            if (v > 0) oil_set.insert({i, j});\n        }\n        assumed_zero.clear();\n    }\n    return 0;\n}","ahc031":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int W, D, N;\n    cin >> W >> D >> N;\n    vector<vector<long long>> a(D, vector<long long>(N));\n    for (int d = 0; d < D; ++d) {\n        for (int k = 0; k < N; ++k) {\n            cin >> a[d][k];\n        }\n    }\n    \n    for (int d = 0; d < D; ++d) {\n        // Sort a[d] in descending order\n        sort(a[d].rbegin(), a[d].rend());\n        int current_i = 0;\n        for (int k = 0; k < N; ++k) {\n            long long area_needed = a[d][k];\n            // Compute height: ceil(area_needed / W)\n            int height = (area_needed + W - 1) / W;\n            int i0 = current_i;\n            int j0 = 0;\n            int i1 = current_i + height;\n            int j1 = W;\n            // Ensure i1 <= W (though sum heights <= 1000)\n            if (i1 > W) {\n                // This shouldn't happen, but just in case\n                i1 = W;\n            }\n            cout << i0 << \" \" << j0 << \" \" << i1 << \" \" << j1 << \"\\n\";\n            current_i += height;\n        }\n    }\n    return 0;\n}","ahc032":"#include <bits/stdc++.h>\n\nusing namespace std;\nusing ll = long long;\nconst ll MOD = 998244353LL;\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<ll>> a(N, vector<ll>(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<ll>>> s(M, vector<vector<ll>>(3, vector<ll>(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 >> s[m][i][j];\n            }\n        }\n    }\n    \n    // Initialize current_mod as a[i][j] % MOD\n    vector<vector<ll>> current_mod(N, vector<ll>(N));\n    for (int i = 0; i < N; ++i) {\n        for (int j = 0; j < N; ++j) {\n            current_mod[i][j] = a[i][j] % MOD;\n        }\n    }\n    \n    // Store operations as (m, p, q)\n    vector<tuple<int, int, int>> operations;\n    \n    // Perform up to K operations\n    for (int op = 0; op < K; ++op) {\n        ll best_delta = LLONG_MIN;\n        int best_m = -1, best_p = -1, best_q = -1;\n        \n        // Evaluate all possible operations\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                    ll delta = 0;\n                    for (int i = 0; i < 3; ++i) {\n                        for (int j = 0; j < 3; ++j) {\n                            ll old_mod = current_mod[p + i][q + j];\n                            ll s_val = s[m][i][j];\n                            ll new_mod = (old_mod + s_val) % MOD;\n                            ll cell_delta = (new_mod - old_mod + MOD) % MOD;\n                            delta += cell_delta;\n                        }\n                    }\n                    if (delta > best_delta) {\n                        best_delta = delta;\n                        best_m = m;\n                        best_p = p;\n                        best_q = q;\n                    }\n                }\n            }\n        }\n        \n        // If no positive delta, stop\n        if (best_delta <= 0) break;\n        \n        // Apply the best operation\n        operations.emplace_back(best_m, best_p, best_q);\n        for (int i = 0; i < 3; ++i) {\n            for (int j = 0; j < 3; ++j) {\n                current_mod[best_p + i][best_q + j] = (current_mod[best_p + i][best_q + j] + s[best_m][i][j]) % MOD;\n            }\n        }\n    }\n    \n    // Output the result\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\nint main() {\n    int N;\n    cin >> N;\n    vector<vector<int>> A(N, vector<int>(N));\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cin >> A[i][j];\n        }\n    }\n    \n    // Initialize receiving queues\n    vector<queue<int>> recv_queues(N);\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            recv_queues[i].push(A[i][j]);\n        }\n    }\n    \n    // Grid: -1 no container, else container id\n    vector<vector<int>> grid(N, vector<int>(N, -1));\n    \n    // Crane positions\n    vector<pair<int, int>> crane_pos(N);\n    for (int i = 0; i < N; i++) {\n        crane_pos[i] = {i, 0};\n    }\n    \n    // Crane holdings: -1 none, else id\n    vector<int> crane_holding(N, -1);\n    \n    // Crane active\n    vector<bool> crane_active(N, true);\n    \n    // Dispatched lists\n    vector<vector<int>> dispatched(N);\n    \n    // Actions\n    vector<string> actions(N);\n    \n    // Simulation\n    const int MAX_TURNS = 10000;\n    for (int turn = 0; turn < MAX_TURNS; turn++) {\n        // Step 1: Receive\n        for (int i = 0; i < N; i++) {\n            int x = i, y = 0;\n            if (grid[x][y] == -1 && crane_holding[i] == -1 && !recv_queues[i].empty()) {\n                int cont = recv_queues[i].front();\n                recv_queues[i].pop();\n                grid[x][y] = cont;\n            }\n        }\n        \n        // Step 2: Compute actions\n        vector<char> acts(N, '.');\n        for (int c = 0; c < N; c++) {\n            if (!crane_active[c]) continue;\n            \n            // Compute goal\n            pair<int, int> goal;\n            if (crane_holding[c] != -1) {\n                int cont = crane_holding[c];\n                int target_row = cont / N;\n                if (target_row == c) {\n                    goal = {c, N - 1};\n                } else {\n                    goal = {target_row, 1};  // Buffer column\n                }\n            } else {\n                goal = {c, 0};\n            }\n            \n            // BFS for next move\n            auto get_next_move = [&](int cx, int cy, pair<int, int> g) -> char {\n                vector<vector<int>> dist(N, vector<int>(N, -1));\n                queue<pair<int, int>> q;\n                q.push({cx, cy});\n                dist[cx][cy] = 0;\n                vector<vector<pair<int, int>>> prev(N, vector<pair<int, int>>(N, {-1, -1}));\n                while (!q.empty()) {\n                    auto [x, y] = q.front();\n                    q.pop();\n                    if (x == g.first && y == g.second) break;\n                    vector<pair<int, int>> dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};\n                    for (auto [dx, dy] : dirs) {\n                        int nx = x + dx, ny = y + dy;\n                        if (nx >= 0 && nx < N && ny >= 0 && ny < N) {\n                            if (dist[nx][ny] == -1) {\n                                bool can = true;\n                                if (crane_holding[c] != -1 && c >= 1) {  // Small crane\n                                    if (grid[nx][ny] != -1) can = false;\n                                }\n                                // Check other cranes (simplified: assume no collision in BFS)\n                                for (int o = 0; o < N; o++) {\n                                    if (o != c && crane_pos[o] == make_pair(nx, ny)) can = false;\n                                }\n                                if (can) {\n                                    dist[nx][ny] = dist[x][y] + 1;\n                                    prev[nx][ny] = {x, y};\n                                    q.push({nx, ny});\n                                }\n                            }\n                        }\n                    }\n                }\n                if (dist[g.first][g.second] == -1) return '.';\n                pair<int, int> current = g;\n                while (prev[current.first][current.second] != make_pair(cx, cy)) {\n                    current = prev[current.first][current.second];\n                }\n                int dx = current.first - cx;\n                int dy = current.second - cy;\n                if (dx == -1) return 'U';\n                if (dx == 1) return 'D';\n                if (dy == -1) return 'L';\n                if (dy == 1) return 'R';\n                return '.';\n            };\n            \n            char act = get_next_move(crane_pos[c].first, crane_pos[c].second, goal);\n            \n            // Prefer P/Q if possible\n            int cx = crane_pos[c].first, cy = crane_pos[c].second;\n            if (crane_holding[c] == -1 && grid[cx][cy] != -1) {\n                act = 'P';\n            } else if (crane_holding[c] != -1 && grid[cx][cy] == -1) {\n                act = 'Q';\n            }\n            \n            acts[c] = act;\n        }\n        \n        // Execute actions with validation\n        for (int c = 0; c < N; c++) {\n            char act = acts[c];\n            int cx = crane_pos[c].first, cy = crane_pos[c].second;\n            bool valid = true;\n            if (act == 'P') {\n                if (crane_holding[c] != -1 || grid[cx][cy] == -1) valid = false;\n                else {\n                    crane_holding[c] = grid[cx][cy];\n                    grid[cx][cy] = -1;\n                }\n            } else if (act == 'Q') {\n                if (crane_holding[c] == -1 || grid[cx][cy] != -1) valid = false;\n                else {\n                    grid[cx][cy] = crane_holding[c];\n                    crane_holding[c] = -1;\n                }\n            } else if (act == 'U') {\n                int nx = cx - 1, ny = cy;\n                if (nx < 0) valid = false;\n                else {\n                    if (crane_holding[c] != -1 && c >= 1 && grid[nx][ny] != -1) valid = false;\n                    for (int o = 0; o < N; o++) {\n                        if (o != c && crane_pos[o] == make_pair(nx, ny)) valid = false;\n                    }\n                    if (valid) crane_pos[c] = {nx, ny};\n                }\n            } else if (act == 'D') {\n                int nx = cx + 1, ny = cy;\n                if (nx >= N) valid = false;\n                else {\n                    if (crane_holding[c] != -1 && c >= 1 && grid[nx][ny] != -1) valid = false;\n                    for (int o = 0; o < N; o++) {\n                        if (o != c && crane_pos[o] == make_pair(nx, ny)) valid = false;\n                    }\n                    if (valid) crane_pos[c] = {nx, ny};\n                }\n            } else if (act == 'L') {\n                int nx = cx, ny = cy - 1;\n                if (ny < 0) valid = false;\n                else {\n                    if (crane_holding[c] != -1 && c >= 1 && grid[nx][ny] != -1) valid = false;\n                    for (int o = 0; o < N; o++) {\n                        if (o != c && crane_pos[o] == make_pair(nx, ny)) valid = false;\n                    }\n                    if (valid) crane_pos[c] = {nx, ny};\n                }\n            } else if (act == 'R') {\n                int nx = cx, ny = cy + 1;\n                if (ny >= N) valid = false;\n                else {\n                    if (crane_holding[c] != -1 && c >= 1 && grid[nx][ny] != -1) valid = false;\n                    for (int o = 0; o < N; o++) {\n                        if (o != c && crane_pos[o] == make_pair(nx, ny)) valid = false;\n                    }\n                    if (valid) crane_pos[c] = {nx, ny};\n                }\n            } else if (act == 'B') {\n                if (crane_holding[c] != -1) valid = false;\n                else crane_active[c] = false;\n            }\n            if (!valid) act = '.';\n            actions[c] += act;\n        }\n        \n        // Step 3: Dispatch\n        for (int i = 0; i < N; i++) {\n            int x = i, y = N - 1;\n            if (grid[x][y] != -1) {\n                int cont = grid[x][y];\n                grid[x][y] = -1;\n                dispatched[i].push_back(cont);\n            }\n        }\n        \n        // Check if done\n        bool done = true;\n        for (int i = 0; i < N; i++) {\n            if (!recv_queues[i].empty()) done = false;\n            if (grid[i][N - 1] != -1) done = false;\n            if (crane_holding[i] != -1) done = false;\n        }\n        if (done) break;\n    }\n    \n    // Output: Pad to max length\n    size_t max_len = 0;\n    for (const auto& s : actions) max_len = max(max_len, s.size());\n    for (auto& s : actions) {\n        while (s.size() < max_len) s += '.';\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>> h(N, vector<int>(N));\n    for (int i = 0; i < N; ++i) {\n        for (int j = 0; j < N; ++j) {\n            cin >> h[i][j];\n        }\n    }\n\n    // Directions: U, D, L, R\n    vector<pair<int, int>> dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};\n    string move_str = \"UDLR\";\n\n    // Priority queues: excess (max h), deficit (max -h)\n    priority_queue<tuple<int, int, int>> pq_excess; // {h, i, j}\n    priority_queue<tuple<int, int, int>> pq_deficit; // {-h, i, j}\n    for (int i = 0; i < N; ++i) {\n        for (int j = 0; j < N; ++j) {\n            if (h[i][j] > 0) pq_excess.push({h[i][j], i, j});\n            if (h[i][j] < 0) pq_deficit.push({-h[i][j], i, j});\n        }\n    }\n\n    // Function to get path from (si,sj) to (ti,tj) using BFS\n    auto get_path = [&](int si, int sj, int ti, int tj) -> vector<string> {\n        vector<vector<int>> dist(N, vector<int>(N, -1));\n        vector<vector<pair<int, int>>> prevv(N, vector<pair<int, int>>(N, {-1, -1}));\n        queue<pair<int, int>> q;\n        q.push({si, sj});\n        dist[si][sj] = 0;\n        while (!q.empty()) {\n            auto [i, j] = q.front(); q.pop();\n            if (i == ti && j == tj) break;\n            for (int d = 0; d < 4; ++d) {\n                int ni = i + dirs[d].first, nj = j + dirs[d].second;\n                if (ni >= 0 && ni < N && nj >= 0 && nj < N && dist[ni][nj] == -1) {\n                    dist[ni][nj] = dist[i][j] + 1;\n                    prevv[ni][nj] = {i, j};\n                    q.push({ni, nj});\n                }\n            }\n        }\n        // Reconstruct path\n        vector<string> path;\n        int ci = ti, cj = tj;\n        while (ci != si || cj != sj) {\n            auto [pi, pj] = prevv[ci][cj];\n            for (int d = 0; d < 4; ++d) {\n                if (pi + dirs[d].first == ci && pj + dirs[d].second == cj) {\n                    path.push_back(string(1, move_str[d]));\n                    break;\n                }\n            }\n            ci = pi; cj = pj;\n        }\n        reverse(path.begin(), path.end());\n        return path;\n    };\n\n    // Function to find nearest deficit from (si,sj) and get path\n    auto find_nearest_deficit = [&](int si, int sj) -> pair<pair<int, int>, vector<string>> {\n        vector<vector<int>> dist(N, vector<int>(N, -1));\n        vector<vector<pair<int, int>>> prevv(N, vector<pair<int, int>>(N, {-1, -1}));\n        queue<pair<int, int>> q;\n        q.push({si, sj});\n        dist[si][sj] = 0;\n        pair<int, int> target = {-1, -1};\n        while (!q.empty() && target.first == -1) {\n            auto [x, y] = q.front(); q.pop();\n            if (h[x][y] < 0) {\n                target = {x, y};\n                break;\n            }\n            for (auto& dir : dirs) {\n                int nx = x + dir.first, ny = y + dir.second;\n                if (nx >= 0 && nx < N && ny >= 0 && ny < N && dist[nx][ny] == -1) {\n                    dist[nx][ny] = dist[x][y] + 1;\n                    prevv[nx][ny] = {x, y};\n                    q.push({nx, ny});\n                }\n            }\n        }\n        if (target.first == -1) return {{ -1, -1 }, {}};\n        // Reconstruct path to target\n        vector<string> path;\n        int ci = target.first, cj = target.second;\n        while (ci != si || cj != sj) {\n            auto [pi, pj] = prevv[ci][cj];\n            for (int d = 0; d < 4; ++d) {\n                if (pi + dirs[d].first == ci && pj + dirs[d].second == cj) {\n                    path.push_back(string(1, move_str[d]));\n                    break;\n                }\n            }\n            ci = pi; cj = pj;\n        }\n        reverse(path.begin(), path.end());\n        return {target, path};\n    };\n\n    int ci = 0, cj = 0; // Current position\n    vector<string> operations;\n\n    while (!pq_excess.empty()) {\n        // Get largest excess\n        auto [val, i, j] = pq_excess.top(); pq_excess.pop();\n        if (h[i][j] <= 0) continue; // Already processed\n\n        // Move to excess cell\n        auto path_to_excess = get_path(ci, cj, i, j);\n        for (auto& s : path_to_excess) operations.push_back(s);\n        ci = i; cj = j;\n\n        // Load all soil\n        int d = h[i][j];\n        operations.push_back(\"+\" + to_string(d));\n        h[i][j] = 0;\n\n        // Find and move to nearest deficits until d is 0\n        while (d > 0) {\n            auto [target, path_to_deficit] = find_nearest_deficit(ci, cj);\n            if (target.first == -1) break; // No deficit (shouldn't happen)\n            int p = target.first, q = target.second;\n\n            // Move to deficit\n            for (auto& s : path_to_deficit) operations.push_back(s);\n            ci = p; cj = q;\n\n            // Unload as much as possible\n            int unload = min(d, -h[p][q]);\n            operations.push_back(\"-\" + to_string(unload));\n            h[p][q] += unload;\n            d -= unload;\n        }\n    }\n\n    // Output all operations\n    for (auto& op : operations) {\n        cout << op << endl;\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    int seed_count = 2 * N * (N - 1);\n    vector<vector<int>> X(seed_count, vector<int>(M));\n    \n    // Read initial seeds\n    for (int i = 0; i < seed_count; i++) {\n        for (int j = 0; j < M; j++) {\n            cin >> X[i][j];\n        }\n    }\n    \n    for (int t = 0; t < T; t++) {\n        // Compute V_k for each seed and prepare for sorting\n        vector<pair<int, int>> seeds;  // Pair: (-V_k, index) for descending sort\n        for (int i = 0; i < seed_count; i++) {\n            int v = 0;\n            for (int j = 0; j < M; j++) {\n                v += X[i][j];\n            }\n            seeds.push_back({-v, i});  // Negative V_k for descending order\n        }\n        \n        // Sort seeds by V_k descending (top 36 will be first)\n        sort(seeds.begin(), seeds.end());\n        \n        // Create planting grid: Assign top 36 seeds in row-major order\n        vector<vector<int>> A(N, vector<int>(N));\n        int idx = 0;\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                A[i][j] = seeds[idx].second;  // Seed index\n                idx++;\n            }\n        }\n        \n        // Output the planting grid\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                cout << A[i][j];\n                if (j < N - 1) cout << \" \";\n                else cout << endl;\n            }\n        }\n        cout.flush();  // Important for interactive problems\n        \n        // Read new seeds after the operation\n        for (int i = 0; i < seed_count; i++) {\n            for (int j = 0; j < M; j++) {\n                cin >> X[i][j];\n            }\n        }\n    }\n    \n    return 0;\n}","ahc038":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int N, M, V;\n    cin >> N >> M >> V;\n    vector<string> s(N), t(N);\n    for(int i = 0; i < N; i++) cin >> s[i];\n    for(int i = 0; i < N; i++) cin >> t[i];\n    \n    vector<pair<int,int>> starts, targets;\n    for(int i = 0; i < N; i++) {\n        for(int j = 0; j < N; j++) {\n            if(s[i][j] == '1') starts.emplace_back(i, j);\n            if(t[i][j] == '1') targets.emplace_back(i, j);\n        }\n    }\n    sort(starts.begin(), starts.end());\n    sort(targets.begin(), targets.end());\n    \n    // Design the tree: simple arm with one fingertip\n    cout << 2 << endl;\n    cout << 0 << \" \" << 1 << endl;\n    cout << 0 << \" \" << 0 << endl;\n    \n    // Directions: 0:right, 1:down, 2:left, 3:up\n    vector<pair<int,int>> dirs = {{0,1}, {1,0}, {0,-1}, {-1,0}};\n    \n    int x = 0, y = 0, dir = 0;\n    \n    for(int i = 0; i < M; i++) {\n        auto [a, b] = starts[i];\n        auto [c, d] = targets[i];\n        \n        // Choose best d for picking\n        int best_dist = 1e9;\n        int best_d = -1, best_rx = -1, best_ry = -1;\n        for(int dd = 0; dd < 4; dd++) {\n            auto [dx, dy] = dirs[dd];\n            int rx = a - dx, ry = b - dy;\n            if(rx >= 0 && rx < N && ry >= 0 && ry < N) {\n                int dist = abs(rx - x) + abs(ry - y);\n                if(dist < best_dist) {\n                    best_dist = dist;\n                    best_d = dd;\n                    best_rx = rx;\n                    best_ry = ry;\n                }\n            }\n        }\n        \n        // Rotate to best_d\n        int diff = (best_d - dir + 4) % 4;\n        string rot;\n        if(diff <= 2) {\n            for(int k = 0; k < diff; k++) rot += 'R';\n        } else {\n            for(int k = 0; k < 4 - diff; k++) rot += 'L';\n        }\n        for(char r : rot) {\n            string S = \".\" + string(1, r) + \".\" + \".\";\n            cout << S << endl;\n            if(r == 'R') dir = (dir + 1) % 4;\n            else dir = (dir - 1 + 4) % 4;\n        }\n        \n        // Move to best_rx, best_ry\n        string moves;\n        int dx = best_rx - x;\n        if(dx > 0) for(int k = 0; k < dx; k++) moves += 'R';\n        else if(dx < 0) for(int k = 0; k < -dx; k++) moves += 'L';\n        int dy = best_ry - y;\n        if(dy > 0) for(int k = 0; k < dy; k++) moves += 'D';\n        else if(dy < 0) for(int k = 0; k < -dy; k++) moves += 'U';\n        for(size_t k = 0; k < moves.size(); k++) {\n            string S = string(1, moves[k]) + \".\" + \".\" + \".\";\n            if(k == moves.size() - 1) S[3] = 'P';\n            cout << S << endl;\n        }\n        x = best_rx;\n        y = best_ry;\n        \n        // Choose best e for placing\n        best_dist = 1e9;\n        int best_e = -1, best_rx2 = -1, best_ry2 = -1;\n        for(int ee = 0; ee < 4; ee++) {\n            auto [ex, ey] = dirs[ee];\n            int rx2 = c - ex, ry2 = d - ey;\n            if(rx2 >= 0 && rx2 < N && ry2 >= 0 && ry2 < N) {\n                int dist = abs(rx2 - x) + abs(ry2 - y);\n                if(dist < best_dist) {\n                    best_dist = dist;\n                    best_e = ee;\n                    best_rx2 = rx2;\n                    best_ry2 = ry2;\n                }\n            }\n        }\n        \n        // Rotate to best_e\n        diff = (best_e - dir + 4) % 4;\n        rot = \"\";\n        if(diff <= 2) {\n            for(int k = 0; k < diff; k++) rot += 'R';\n        } else {\n            for(int k = 0; k < 4 - diff; k++) rot += 'L';\n        }\n        for(char r : rot) {\n            string S = \".\" + string(1, r) + \".\" + \".\";\n            cout << S << endl;\n            if(r == 'R') dir = (dir + 1) % 4;\n            else dir = (dir - 1 + 4) % 4;\n        }\n        \n        // Move to best_rx2, best_ry2\n        moves = \"\";\n        dx = best_rx2 - x;\n        if(dx > 0) for(int k = 0; k < dx; k++) moves += 'R';\n        else if(dx < 0) for(int k = 0; k < -dx; k++) moves += 'L';\n        dy = best_ry2 - y;\n        if(dy > 0) for(int k = 0; k < dy; k++) moves += 'D';\n        else if(dy < 0) for(int k = 0; k < -dy; k++) moves += 'U';\n        for(size_t k = 0; k < moves.size(); k++) {\n            string S = string(1, moves[k]) + \".\" + \".\" + \".\";\n            if(k == moves.size() - 1) S[3] = 'P';\n            cout << S << endl;\n        }\n        x = best_rx2;\n        y = best_ry2;\n    }\n    \n    return 0;\n}","ahc039":"#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n\nint main() {\n  ios::sync_with_stdio(false);\n  cin.tie(nullptr);\n\n  int N;\n  cin >> N;\n  vector<tuple<ll, ll, int>> pts;\n  for (int i = 0; i < 2 * N; ++i) {\n    ll x, y;\n    cin >> x >> y;\n    int w = (i < N ? 1 : -1);\n    pts.emplace_back(x, y, w);\n  }\n\n  sort(pts.begin(), pts.end());\n\n  map<ll, int> y_to_weight;\n  ll current_min_x = LLONG_MAX / 2;\n  ll best_score = LLONG_MIN / 2;\n  ll best_left = -1, best_right = -1, best_bottom = -1, best_top = -1;\n\n  for (auto &[x, y, w] : pts) {\n    y_to_weight[y] = w;\n    current_min_x = min(current_min_x, x);\n\n    vector<ll> ylist;\n    ll min_p = 0;\n    int min_idx = 0;\n    ll local_max = LLONG_MIN / 2;\n    int best_start = 0, best_end = -1;\n    ll cum = 0;\n    int idx = 0;\n    for (auto &p : y_to_weight) {\n      ylist.push_back(p.first);\n      cum += p.second;\n      ll sum_val = cum - min_p;\n      if (sum_val > local_max) {\n        local_max = sum_val;\n        best_start = min_idx;\n        best_end = idx;\n      }\n      if (cum < min_p) {\n        min_p = cum;\n        min_idx = idx + 1;\n      }\n      ++idx;\n    }\n\n    if (best_left < best_right && best_bottom < best_top && local_max > best_score) {\n      best_score = local_max;\n      best_left = current_min_x;\n      best_right = x;\n      best_bottom = ylist[best_start];\n      best_top = ylist[best_end];\n    }\n  }\n\n  // If no valid rectangle found, output a small one with score 0\n  if (best_score <= LLONG_MIN / 2 + 1) {\n    best_left = 0;\n    best_right = 1;\n    best_bottom = 0;\n    best_top = 1;\n  }\n\n  vector<pair<ll, ll>> poly = {\n    {best_left, best_bottom},\n    {best_left, best_top},\n    {best_right, best_top},\n    {best_right, best_bottom}\n  };\n\n  cout << 4 << '\\n';\n  for (auto &[x, y] : poly) {\n    cout << x << ' ' << y << '\\n';\n  }\n\n  return 0;\n}","ahc040":"#include <bits/stdc++.h>\n\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int N, T, sigma;\n    cin >> N >> T >> sigma;\n    vector<long long> w(N), h(N);\n    for (int i = 0; i < N; ++i) {\n        cin >> w[i] >> h[i];\n    }\n    \n    // Define modes: 0=alternate U/L with smart r/b, 1-4=fixed for baselines\n    vector<int> modes = {0, 1, 2, 3, 4};\n    \n    for (int t = 0; t < T; ++t) {\n        int mode = modes[t % modes.size()];\n        vector<tuple<int, int, char, int>> prdb;\n        \n        for (int i = 0; i < N; ++i) {\n            int p = i;\n            int r;\n            char d;\n            int b;\n            \n            if (mode == 0) {\n                // Alternate U/L, b=previous, r optimized for direction\n                d = (i % 2 == 0) ? 'U' : 'L';\n                b = (i == 0) ? -1 : i - 1;\n                if (d == 'U') {\n                    r = (h[i] > w[i]) ? 1 : 0;  // Rotate if taller to prioritize width\n                } else {\n                    r = (w[i] > h[i]) ? 1 : 0;  // Rotate if wider to prioritize height\n                }\n            } else if (mode == 1) {\n                d = 'U';\n                b = -1;\n                r = 0;\n            } else if (mode == 2) {\n                d = 'L';\n                b = -1;\n                r = 0;\n            } else if (mode == 3) {\n                d = 'U';\n                b = -1;\n                r = 1;\n            } else if (mode == 4) {\n                d = 'L';\n                b = -1;\n                r = 1;\n            }\n            \n            prdb.emplace_back(p, r, d, b);\n        }\n        \n        cout << N << '\\n';\n        for (auto [p, r, d, b] : prdb) {\n            cout << p << ' ' << r << ' ' << d << ' ' << b << '\\n';\n        }\n        cout << flush;\n        \n        long long Wp, Hp;\n        cin >> Wp >> Hp;\n        // Future: Use Wp, Hp to adapt modes (e.g., if Wp > Hp, favor 'L')\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(0);\n    int N, M, H;\n    cin >> N >> M >> H;\n    vector<int> A(N);\n    for (int i = 0; i < N; ++i) {\n        cin >> A[i];\n    }\n    vector<vector<int>> adj(N);\n    for (int i = 0; i < M; ++i) {\n        int u, v;\n        cin >> u >> v;\n        adj[u].push_back(v);\n        adj[v].push_back(u);\n    }\n    // Skip coordinates as not needed for computation\n    for (int i = 0; i < N; ++i) {\n        int x, y;\n        cin >> x >> y;\n    }\n    // Greedy assignment\n    vector<int> d(N, -1);\n    vector<int> p(N, -1);\n    vector<pair<int, int>> verts;\n    for (int i = 0; i < N; ++i) {\n        verts.push_back({-A[i], i}); // Sort by decreasing A\n    }\n    sort(verts.begin(), verts.end());\n    for (auto [_, v] : verts) {\n        if (d[v] != -1) continue;\n        int best_u = -1;\n        int max_du = -1;\n        for (int u : adj[v]) {\n            if (d[u] != -1 && d[u] < H && d[u] > max_du) {\n                max_du = d[u];\n                best_u = u;\n            }\n        }\n        if (best_u != -1) {\n            p[v] = best_u;\n            d[v] = max_du + 1;\n        } else {\n            p[v] = -1;\n            d[v] = 0;\n        }\n    }\n    // Output\n    for (int i = 0; i < N; ++i) {\n        if (i > 0) cout << \" \";\n        cout << p[i];\n    }\n    cout << \"\\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> board(N);\n    for (int i = 0; i < N; i++) {\n        cin >> board[i];\n    }\n    vector<pair<int, int>> oni;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (board[i][j] == 'x') {\n                oni.emplace_back(i, j);\n            }\n        }\n    }\n    // Maps for batching: direction -> index -> max distance\n    map<pair<char, int>, int> max_dist;\n    for (auto [i, j] : oni) {\n        // Check directions and choose best\n        bool can_up = true;\n        for (int k = 0; k < i; k++) {\n            if (board[k][j] == 'o') can_up = false;\n        }\n        int cost_up = can_up ? 2 * (i + 1) : 1e9;\n\n        bool can_down = true;\n        for (int k = i + 1; k < N; k++) {\n            if (board[k][j] == 'o') can_down = false;\n        }\n        int cost_down = can_down ? 2 * (N - i) : 1e9;\n\n        bool can_left = true;\n        for (int k = 0; k < j; k++) {\n            if (board[i][k] == 'o') can_left = false;\n        }\n        int cost_left = can_left ? 2 * (j + 1) : 1e9;\n\n        bool can_right = true;\n        for (int k = j + 1; k < N; k++) {\n            if (board[i][k] == 'o') can_right = false;\n        }\n        int cost_right = can_right ? 2 * (N - j) : 1e9;\n\n        // Find best direction\n        vector<tuple<int, char, int, int>> candidates; // cost, dir, idx, dist\n        if (can_up) candidates.emplace_back(cost_up, 'U', j, i + 1);\n        if (can_down) candidates.emplace_back(cost_down, 'D', j, N - i);\n        if (can_left) candidates.emplace_back(cost_left, 'L', i, j + 1);\n        if (can_right) candidates.emplace_back(cost_right, 'R', i, N - j);\n        sort(candidates.begin(), candidates.end());\n        auto [cost, dir, idx, dist] = candidates[0];\n\n        // Update max_dist\n        pair<char, int> key = {dir, idx};\n        max_dist[key] = max(max_dist[key], dist);\n    }\n\n    // Generate batched operations\n    vector<pair<char, int>> all_ops;\n    for (auto& [key, dist] : max_dist) {\n        char dir = key.first;\n        int idx = key.second;\n        if (dir == 'U') {\n            for (int t = 0; t < dist; t++) all_ops.emplace_back('U', idx);\n            for (int t = 0; t < dist; t++) all_ops.emplace_back('D', idx);\n        } else if (dir == 'D') {\n            for (int t = 0; t < dist; t++) all_ops.emplace_back('D', idx);\n            for (int t = 0; t < dist; t++) all_ops.emplace_back('U', idx);\n        } else if (dir == 'L') {\n            for (int t = 0; t < dist; t++) all_ops.emplace_back('L', idx);\n            for (int t = 0; t < dist; t++) all_ops.emplace_back('R', idx);\n        } else if (dir == 'R') {\n            for (int t = 0; t < dist; t++) all_ops.emplace_back('R', idx);\n            for (int t = 0; t < dist; t++) all_ops.emplace_back('L', idx);\n        }\n    }\n\n    // Output\n    for (auto [d, p] : all_ops) {\n        cout << d << \" \" << p << \"\\n\";\n    }\n    return 0;\n}","ahc044":"#include <bits/stdc++.h>\n#include <random>\n#include <chrono>\n\nusing namespace std;\n\nint main() {\n    const int N = 100;\n    const int L = 500000;\n    vector<long long> T(N);\n    for (auto &x : T) cin >> x;\n\n    // Random number generator\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n    uniform_real_distribution<double> urd(0.0, 1.0);\n\n    // Initial random assignment\n    vector<int> a(N), b(N);\n    for (int i = 0; i < N; i++) {\n        a[i] = rng() % N;\n        b[i] = rng() % N;\n    }\n\n    // Simulation function\n    auto simulate = [&](const vector<int> &aa, const vector<int> &bb) -> long long {\n        vector<long long> cnt(N, 0);\n        int cur = 0;\n        cnt[0] = 1;\n        for (int w = 1; w < L; w++) {\n            int t = cnt[cur];\n            int nxt = (t % 2 == 1) ? aa[cur] : bb[cur];\n            cur = nxt;\n            cnt[cur]++;\n        }\n        long long e = 0;\n        for (int i = 0; i < N; i++) e += abs(cnt[i] - T[i]);\n        return e;\n    };\n\n    // Initial error\n    long long currentE = simulate(a, b);\n\n    // Simulated Annealing parameters\n    double temp = 1000.0;\n    const int iterations = 150;  // Adjust based on time; ~150 fits within 2s\n    const double cooling_rate = 0.99;\n\n    for (int it = 0; it < iterations; it++) {\n        // Perturb: choose random i, which (0 for a, 1 for b), and new value\n        int i = rng() % N;\n        int which = rng() % 2;\n        int old_val = (which == 0) ? a[i] : b[i];\n        int new_val = rng() % N;\n\n        // Apply change\n        if (which == 0) a[i] = new_val;\n        else b[i] = new_val;\n\n        // Compute new error\n        long long newE = simulate(a, b);\n\n        // Acceptance\n        if (newE <= currentE || urd(rng) < exp((currentE - newE) / temp)) {\n            currentE = newE;\n        } else {\n            // Revert\n            if (which == 0) a[i] = old_val;\n            else b[i] = old_val;\n        }\n\n        // Cool down\n        temp *= cooling_rate;\n    }\n\n    // Output the best assignment\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\nusing ll = long long;\nusing ld = long double;\n\n// Union-Find for Kruskal\nstruct UF {\n    vector<int> par;\n    UF(int n) : par(n) { iota(par.begin(), par.end(), 0); }\n    int find(int x) { return par[x] == x ? x : par[x] = find(par[x]); }\n    bool unite(int x, int y) {\n        x = find(x); y = find(y);\n        if (x == y) return false;\n        par[x] = y;\n        return true;\n    }\n};\n\n// Compute floored Euclidean distance\nll dist(ld x1, ld y1, ld x2, ld y2) {\n    ld dx = x1 - x2, dy = y1 - y2;\n    return floorl(sqrtl(dx * dx + dy * dy));\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, Q, L, W;\n    cin >> N >> M >> Q >> L >> W;\n    vector<int> G(M);\n    for (int i = 0; i < M; ++i) cin >> G[i];\n    vector<ld> 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    // Compute approximate centers\n    vector<ld> cx(N), cy(N);\n    for (int i = 0; i < N; ++i) {\n        cx[i] = (lx[i] + rx[i]) / 2.0L;\n        cy[i] = (ly[i] + ry[i]) / 2.0L;\n    }\n\n    // Sort cities by cx, then cy\n    vector<int> cities(N);\n    iota(cities.begin(), cities.end(), 0);\n    sort(cities.begin(), cities.end(), [&](int a, int b) {\n        if (cx[a] != cx[b]) return cx[a] < cx[b];\n        return cy[a] < cy[b];\n    });\n\n    // Assign to groups\n    vector<vector<int>> groups(M);\n    int idx = 0;\n    for (int k = 0; k < M; ++k) {\n        for (int j = 0; j < G[k]; ++j) {\n            groups[k].push_back(cities[idx++]);\n        }\n    }\n\n    // Process queries and build edges\n    vector<vector<pair<int, int>>> edges(M);\n    int queries_used = 0;\n\n    auto query = [&](const vector<int>& c) -> vector<pair<int, int>> {\n        if (queries_used >= Q) return {};\n        cout << \"? \" << c.size();\n        for (int x : c) cout << \" \" << x;\n        cout << \"\\n\" << flush;\n        ++queries_used;\n        vector<pair<int, int>> res;\n        for (int i = 0; i < (int)c.size() - 1; ++i) {\n            int a, b;\n            cin >> a >> b;\n            res.emplace_back(min(a, b), max(a, b));\n        }\n        return res;\n    };\n\n    for (int k = 0; k < M; ++k) {\n        auto& grp = groups[k];\n        int sz = grp.size();\n        if (sz <= L) {\n            // Query entire group for true MST\n            auto res = query(grp);\n            edges[k] = res;\n        } else {\n            // Approximate MST using centers\n            vector<tuple<ll, int, int>> edge_list;\n            for (int i = 0; i < sz; ++i) {\n                for (int j = i + 1; j < sz; ++j) {\n                    ll d = dist(cx[grp[i]], cy[grp[i]], cx[grp[j]], cy[grp[j]]);\n                    edge_list.emplace_back(d, i, j);\n                }\n            }\n            sort(edge_list.begin(), edge_list.end());\n            UF uf(sz);\n            vector<pair<int, int>> mst_edges;\n            for (auto& [d, u, v] : edge_list) {\n                if (uf.unite(u, v)) {\n                    mst_edges.emplace_back(grp[u], grp[v]);\n                }\n            }\n            edges[k] = mst_edges;\n\n            // Optional: Refine with queries (e.g., query every L cities)\n            // For simplicity, skip if Q is low; add if time allows\n            // Example: Query subsets of size L in sliding window\n            if (queries_used < Q - 10) {  // Reserve some queries\n                for (int start = 0; start < sz; start += L - 1) {\n                    int end = min(start + L, sz);\n                    vector<int> subset(grp.begin() + start, grp.begin() + end);\n                    if ((int)subset.size() >= 2) {\n                        auto res = query(subset);\n                        // Incorporate res into edges[k] if not already present\n                        // (Simple: just add them, assuming they improve)\n                        for (auto& p : res) {\n                            edges[k].push_back(p);\n                        }\n                    }\n                }\n            }\n        }\n    }\n\n    // Output\n    cout << \"!\\n\";\n    for (int k = 0; k < M; ++k) {\n        for (int x : groups[k]) cout << x << \" \";\n        cout << \"\\n\";\n        for (auto& [a, b] : edges[k]) {\n            cout << a << \" \" << b << \"\\n\";\n        }\n    }\n\n    return 0;\n}","ahc046":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int N, M;\n    cin >> N >> M;\n    vector<pair<int, int>> pos(M);\n    for (int i = 0; i < M; i++) {\n        cin >> pos[i].first >> pos[i].second;\n    }\n    set<int> rows, cols;\n    for (auto [i, j] : pos) {\n        rows.insert(i);\n        cols.insert(j);\n    }\n    vector<pair<int, int>> blocks;\n    for (int r : rows) {\n        int maxj = -1;\n        for (auto [i, j] : pos) {\n            if (i == r) maxj = max(maxj, j);\n        }\n        if (maxj < 19) blocks.emplace_back(r, maxj + 1);\n    }\n    for (int c : cols) {\n        int maxi = -1;\n        for (auto [i, j] : pos) {\n            if (j == c) maxi = max(maxi, i);\n        }\n        if (maxi < 19) blocks.emplace_back(maxi + 1, c);\n    }\n    sort(blocks.begin(), blocks.end());\n    blocks.erase(unique(blocks.begin(), blocks.end()), blocks.end());\n    vector<vector<bool>> blocked(20, vector<bool>(20, false));\n    for (auto [x, y] : blocks) blocked[x][y] = true;\n    map<pair<int, int>, pair<int, int>> block_for;\n    vector<pair<int, int>> to_visit;\n    for (auto b : blocks) {\n        int bx = b.first, by = b.second;\n        pair<int, int> adj;\n        if (bx > 0) adj = {bx - 1, by};\n        else if (bx < 19) adj = {bx + 1, by};\n        else if (by > 0) adj = {bx, by - 1};\n        else adj = {bx, by + 1};\n        to_visit.push_back(adj);\n        block_for[adj] = b;\n    }\n    sort(to_visit.begin(), to_visit.end());\n    to_visit.erase(unique(to_visit.begin(), to_visit.end()), to_visit.end());\n    auto get_path = [&](pair<int, int> s, pair<int, int> t) {\n        vector<vector<int>> dist(20, vector<int>(20, -1));\n        vector<vector<pair<int, int>>> prevv(20, vector<pair<int, int>>(20, {-1, -1}));\n        queue<pair<int, int>> q;\n        q.push(s);\n        dist[s.first][s.second] = 0;\n        int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};\n        while (!q.empty()) {\n            auto [x, y] = q.front();\n            q.pop();\n            for (int d = 0; d < 4; d++) {\n                int nx = x + dx[d], ny = y + dy[d];\n                if (nx >= 0 && nx < 20 && ny >= 0 && ny < 20 && !blocked[nx][ny] && dist[nx][ny] == -1) {\n                    dist[nx][ny] = dist[x][y] + 1;\n                    prevv[nx][ny] = {x, y};\n                    q.push({nx, ny});\n                }\n            }\n        }\n        vector<pair<int, int>> path;\n        pair<int, int> cur = t;\n        while (cur != make_pair(-1, -1)) {\n            path.push_back(cur);\n            cur = prevv[cur.first][cur.second];\n        }\n        reverse(path.begin(), path.end());\n        if (path[0] != s) return make_pair(vector<pair<int, int>>(), vector<int>());\n        vector<int> dirs;\n        for (size_t i = 1; i < path.size(); i++) {\n            int px = path[i - 1].first, py = path[i - 1].second;\n            int cx = path[i].first, cy = path[i].second;\n            if (cx == px + 1) dirs.push_back(1);\n            else if (cx == px - 1) dirs.push_back(0);\n            else if (cy == py + 1) dirs.push_back(3);\n            else if (cy == py - 1) dirs.push_back(2);\n        }\n        return make_pair(path, dirs);\n    };\n    vector<pair<char, char>> actions;\n    pair<int, int> current = pos[0];\n    for (auto tv : to_visit) {\n        auto [path, dirs] = get_path(current, tv);\n        for (int d : dirs) {\n            actions.emplace_back('M', \"UDLR\"[d]);\n        }\n        if (block_for.count(tv)) {\n            auto b = block_for[tv];\n            int ddx = b.first - tv.first, ddy = b.second - tv.second;\n            char dir;\n            if (ddx == 1) dir = 'D';\n            else if (ddx == -1) dir = 'U';\n            else if (ddy == 1) dir = 'R';\n            else dir = 'L';\n            actions.emplace_back('A', dir);\n        }\n        current = tv;\n    }\n    for (int k = 1; k < M; k++) {\n        auto [path, dirs] = get_path(current, pos[k]);\n        vector<pair<char, char>> acts;\n        size_t i = 0;\n        while (i < dirs.size()) {\n            size_t j = i;\n            while (j < dirs.size() && dirs[j] == dirs[i]) j++;\n            if (j - i == 1) {\n                acts.emplace_back('M', \"UDLR\"[dirs[i]]);\n            } else {\n                acts.emplace_back('S', \"UDLR\"[dirs[i]]);\n            }\n            i = j;\n        }\n        for (auto p : acts) actions.push_back(p);\n        current = pos[k];\n    }\n    for (auto [a, d] : actions) {\n        cout << a << \" \" << d << endl;\n    }\n    return 0;\n}"},"4":{"ahc001":"#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n\ndouble calc_p(ll r, ll s) {\n    if (s == 0 || r == 0) return 0.0;\n    double mn = min(r, s);\n    double mx = max(r, s);\n    double ratio = mn / mx;\n    return 1.0 - (1.0 - ratio) * (1.0 - ratio);\n}\n\nbool check_overlap(int i, ll na, ll nb, ll nc, ll nd, const vector<ll>& a, const vector<ll>& b, const vector<ll>& c, const vector<ll>& d, int n) {\n    for (int j = 0; j < n; ++j) {\n        if (i == j) continue;\n        ll ax = max(na, a[j]);\n        ll bx = min(nc, c[j]);\n        ll ay = max(nb, b[j]);\n        ll by = min(nd, d[j]);\n        if (ax < bx && ay < by) return true;\n    }\n    return false;\n}\n\nll find_max_dw(int i, const vector<ll>& a, const vector<ll>& b, const vector<ll>& c, const vector<ll>& d, int n) {\n    ll left = 0, right = 10000LL - c[i];\n    while (left < right) {\n        ll mid = (left + right + 1) / 2;\n        ll new_c = c[i] + mid;\n        if (check_overlap(i, a[i], b[i], new_c, d[i], a, b, c, d, n)) {\n            right = mid - 1;\n        } else {\n            left = mid;\n        }\n    }\n    return left;\n}\n\nll find_max_dh(int i, const vector<ll>& a, const vector<ll>& b, const vector<ll>& c, const vector<ll>& d, int n) {\n    ll left = 0, right = 10000LL - d[i];\n    while (left < right) {\n        ll mid = (left + right + 1) / 2;\n        ll new_d = d[i] + mid;\n        if (check_overlap(i, a[i], b[i], c[i], new_d, a, b, c, d, n)) {\n            right = mid - 1;\n        } else {\n            left = mid;\n        }\n    }\n    return left;\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int n;\n    cin >> n;\n    vector<ll> x(n), y(n), r(n);\n    for (int i = 0; i < n; ++i) {\n        cin >> x[i] >> y[i] >> r[i];\n    }\n    \n    vector<ll> a(n), b(n), c(n), d(n), s(n);\n    for (int i = 0; i < n; ++i) {\n        a[i] = x[i];\n        c[i] = x[i] + 1;\n        b[i] = y[i];\n        d[i] = y[i] + 1;\n        s[i] = 1;\n    }\n    \n    bool changed = true;\n    while (changed) {\n        changed = false;\n        double max_gain = -1e9;\n        int best_i = -1;\n        int best_dir = -1;  // 0: width, 1: height\n        ll best_delta = 0;\n        \n        for (int i = 0; i < n; ++i) {\n            if (s[i] >= r[i]) continue;\n            \n            ll w = c[i] - a[i];\n            ll h = d[i] - b[i];\n            \n            // Find max dw with binary search\n            ll dw = find_max_dw(i, a, b, c, d, n);\n            \n            // Find max dh with binary search\n            ll dh = find_max_dh(i, a, b, c, d, n);\n            \n            // Check gain for dw\n            if (dw > 0) {\n                ll new_s = s[i] + dw * h;\n                double old_p = calc_p(r[i], s[i]);\n                double new_p = calc_p(r[i], new_s);\n                double gain = new_p - old_p;\n                if (gain > max_gain + 1e-9) {  // Small epsilon for floating-point\n                    max_gain = gain;\n                    best_i = i;\n                    best_dir = 0;\n                    best_delta = dw;\n                }\n            }\n            \n            // Check gain for dh\n            if (dh > 0) {\n                ll new_s = s[i] + dh * w;\n                double old_p = calc_p(r[i], s[i]);\n                double new_p = calc_p(r[i], new_s);\n                double gain = new_p - old_p;\n                if (gain > max_gain + 1e-9) {\n                    max_gain = gain;\n                    best_i = i;\n                    best_dir = 1;\n                    best_delta = dh;\n                }\n            }\n        }\n        \n        if (best_i != -1) {\n            changed = true;\n            if (best_dir == 0) {  // Expand width\n                c[best_i] += best_delta;\n                s[best_i] += best_delta * (d[best_i] - b[best_i]);\n            } else {  // Expand height\n                d[best_i] += best_delta;\n                s[best_i] += best_delta * (c[best_i] - a[best_i]);\n            }\n        }\n    }\n    \n    // Output\n    for (int i = 0; i < n; ++i) {\n        cout << a[i] << \" \" << b[i] << \" \" << c[i] << \" \" << d[i] << \"\\n\";\n    }\n    \n    return 0;\n}","ahc002":"#include <bits/stdc++.h>\n\nusing namespace std;\n\nstruct State {\n    int tile;\n    pair<int, int> sq;\n    set<int> visited;\n    vector<pair<int, int>> path_squares;\n    int score;\n    bool operator<(const State& other) const {\n        return score < other.score;  // Max-heap\n    }\n};\n\nint main() {\n    int si, sj;\n    cin >> si >> sj;\n    vector<vector<int>> t(50, vector<int>(50));\n    for (int i = 0; i < 50; i++) for (int j = 0; j < 50; j++) cin >> t[i][j];\n    vector<vector<int>> p(50, vector<int>(50));\n    for (int i = 0; i < 50; i++) for (int j = 0; j < 50; j++) cin >> p[i][j];\n    \n    int M = 0;\n    for (int i = 0; i < 50; i++) for (int j = 0; j < 50; j++) M = max(M, t[i][j] + 1);\n    \n    vector<vector<int>> tile_squares(M);\n    for (int i = 0; i < 50; i++) for (int j = 0; j < 50; j++) {\n        tile_squares[t[i][j]].push_back(i * 50 + j);\n    }\n    \n    vector<set<int>> adj_tile(M);\n    int di[4] = {-1, 1, 0, 0};\n    int dj[4] = {0, 0, -1, 1};\n    for (int i = 0; i < 50; i++) for (int j = 0; j < 50; j++) {\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 < 50 && nj >= 0 && nj < 50 && t[i][j] != t[ni][nj]) {\n                adj_tile[t[i][j]].insert(t[ni][nj]);\n            }\n        }\n    }\n    \n    // Beam search\n    const int BEAM_WIDTH = 10;\n    const int MAX_STEPS = 100;  // Limit to prevent timeout\n    priority_queue<State> pq;\n    set<int> initial_visited;\n    initial_visited.insert(t[si][sj]);\n    vector<pair<int, int>> initial_path = {{si, sj}};\n    int initial_score = p[si][sj];\n    pq.push({t[si][sj], {si, sj}, initial_visited, initial_path, initial_score});\n    \n    State best_state = pq.top();\n    int steps = 0;\n    while (!pq.empty() && steps < MAX_STEPS) {\n        vector<State> current_beam;\n        for (int i = 0; i < BEAM_WIDTH && !pq.empty(); i++) {\n            current_beam.push_back(pq.top());\n            pq.pop();\n        }\n        for (const auto& state : current_beam) {\n            if (state.score > best_state.score) best_state = state;\n            bool expanded = false;\n            for (int nei : adj_tile[state.tile]) {\n                if (state.visited.count(nei)) continue;\n                int max_adj_p = -1;\n                pair<int, int> chosen_sq = {-1, -1};\n                for (int sq : tile_squares[nei]) {\n                    int ni = sq / 50;\n                    int nj = sq % 50;\n                    if (abs(ni - state.sq.first) + abs(nj - state.sq.second) == 1) {\n                        if (p[ni][nj] > max_adj_p) {\n                            max_adj_p = p[ni][nj];\n                            chosen_sq = {ni, nj};\n                        }\n                    }\n                }\n                if (chosen_sq.first != -1) {\n                    set<int> new_visited = state.visited;\n                    new_visited.insert(nei);\n                    vector<pair<int, int>> new_path = state.path_squares;\n                    new_path.push_back(chosen_sq);\n                    int new_score = state.score + max_adj_p;\n                    pq.push({nei, chosen_sq, new_visited, new_path, new_score});\n                    expanded = true;\n                }\n            }\n            if (!expanded) {\n                // No moves, this path ends\n            }\n        }\n        steps++;\n    }\n    \n    // Build move string from best_state\n    string moves = \"\";\n    for (size_t i = 1; i < best_state.path_squares.size(); i++) {\n        auto [pi, pj] = best_state.path_squares[i - 1];\n        auto [ci, cj] = best_state.path_squares[i];\n        if (ci == pi - 1) moves += 'U';\n        else if (ci == pi + 1) moves += 'D';\n        else if (cj == pj - 1) moves += 'L';\n        else if (cj == pj + 1) moves += 'R';\n        else assert(false);\n    }\n    cout << moves << endl;\n    return 0;\n}","ahc003":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    const int N = 30;\n    vector<vector<double>> h(N, vector<double>(N-1, 5000.0));  // h[i][j]: cost from (i,j) to (i,j+1)\n    vector<vector<double>> v(N-1, vector<double>(N, 5000.0));  // v[i][j]: cost from (i,j) to (i+1,j)\n\n    for (int query = 0; query < 1000; ++query) {\n        int si, sj, ti, tj;\n        cin >> si >> sj >> ti >> tj;\n\n        // Dijkstra to find shortest path with current estimated costs\n        vector<vector<double>> dist(N, vector<double>(N, 1e18));\n        vector<vector<pair<int, int>>> prev(N, vector<pair<int, int>>(N, {-1, -1}));\n        priority_queue<tuple<double, int, int>, vector<tuple<double, int, int>>, greater<tuple<double, int, int>>> pq;\n\n        dist[si][sj] = 0.0;\n        pq.emplace(0.0, si, sj);\n\n        while (!pq.empty()) {\n            auto [cost, i, j] = pq.top();\n            pq.pop();\n            if (cost > dist[i][j]) continue;\n\n            // Neighbors: up, down, left, right\n            vector<tuple<int, int, double>> neighbors;\n            if (i > 0) neighbors.emplace_back(i-1, j, v[i-1][j]);  // up\n            if (i < N-1) neighbors.emplace_back(i+1, j, v[i][j]);  // down\n            if (j > 0) neighbors.emplace_back(i, j-1, h[i][j-1]);  // left\n            if (j < N-1) neighbors.emplace_back(i, j+1, h[i][j]);  // right\n\n            for (auto [ni, nj, edge_cost] : neighbors) {\n                double new_cost = cost + edge_cost;\n                if (new_cost < dist[ni][nj]) {\n                    dist[ni][nj] = new_cost;\n                    prev[ni][nj] = {i, j};\n                    pq.emplace(new_cost, ni, nj);\n                }\n            }\n        }\n\n        // If target is unreachable (shouldn't happen), output empty path\n        if (dist[ti][tj] == 1e18) {\n            cout << \"\" << endl;\n            cout.flush();\n            int F;\n            cin >> F;  // Still read feedback to continue\n            continue;\n        }\n\n        // Reconstruct path from t to s, building forward moves\n        string path = \"\";\n        int ci = ti, cj = tj;\n        while (ci != si || cj != sj) {\n            auto [pi, pj] = prev[ci][cj];\n            if (pi == -1 || pj == -1) {\n                // Invalid predecessor (shouldn't happen), break to avoid crash\n                path = \"\";\n                break;\n            }\n            // Determine forward move from (pi, pj) to (ci, cj)\n            if (pi == ci - 1) path += 'D';  // i increases: down\n            else if (pi == ci + 1) path += 'U';  // i decreases: up\n            else if (pj == cj - 1) path += 'R';  // j increases: right\n            else if (pj == cj + 1) path += 'L';  // j decreases: left\n            ci = pi;\n            cj = pj;\n        }\n        reverse(path.begin(), path.end());  // Reverse to get from s to t\n\n        // Output path and flush\n        cout << path << endl;\n        cout.flush();\n\n        // Read feedback F\n        int F;\n        cin >> F;\n\n        // Compute estimated path cost C\n        double C = 0.0;\n        ci = si, cj = sj;\n        for (char move : path) {\n            if (move == 'U') {\n                C += v[ci-1][cj];\n                ci--;\n            } else if (move == 'D') {\n                C += v[ci][cj];\n                ci++;\n            } else if (move == 'L') {\n                C += h[ci][cj-1];\n                cj--;\n            } else if (move == 'R') {\n                C += h[ci][cj];\n                cj++;\n            }\n        }\n\n        // Update estimated costs if C > 0\n        if (C > 1e-9) {\n            double factor = static_cast<double>(F) / C;\n            ci = si, cj = sj;\n            for (char move : path) {\n                if (move == 'U') {\n                    v[ci-1][cj] *= factor;\n                    ci--;\n                } else if (move == 'D') {\n                    v[ci][cj] *= factor;\n                    ci++;\n                } else if (move == 'L') {\n                    h[ci][cj-1] *= factor;\n                    cj--;\n                } else if (move == 'R') {\n                    h[ci][cj] *= factor;\n                    cj++;\n                }\n            }\n        }\n    }\n\n    return 0;\n}","ahc004":"#include <bits/stdc++.h>\n\nusing namespace std;\n\nint N;\n\n// Function to compute c: number of strings that are subsequences\nint compute_c(const vector<vector<char>>& grid, const vector<string>& strings) {\n    int covered = 0;\n    for (const auto& s : strings) {\n        bool found = false;\n        // Horizontal check\n        for (int i = 0; i < N && !found; i++) {\n            string row(grid[i].begin(), grid[i].end());\n            row += row;  // Double for wrap-around\n            if (row.find(s) != string::npos) found = true;\n        }\n        // Vertical check\n        for (int j = 0; j < N && !found; j++) {\n            string col;\n            for (int i = 0; i < N; i++) col += grid[i][j];\n            col += col;  // Double for wrap-around\n            if (col.find(s) != string::npos) found = true;\n        }\n        if (found) covered++;\n    }\n    return covered;\n}\n\n// Function to compute d: number of dots\nint compute_d(const vector<vector<char>>& grid) {\n    int dots = 0;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (grid[i][j] == '.') dots++;\n        }\n    }\n    return dots;\n}\n\nint main() {\n    int M;\n    cin >> N >> M;\n    vector<string> strings(M);\n    for (int i = 0; i < M; i++) {\n        cin >> strings[i];\n    }\n\n    // Sort strings by length descending\n    sort(strings.begin(), strings.end(), [](const string& a, const string& b) {\n        return a.size() > b.size();\n    });\n\n    vector<vector<char>> best_grid(N, vector<char>(N, '.'));\n    int best_c = -1;\n    int best_d = INT_MAX;\n\n    const int ATTEMPTS = 10;  // Increased number of attempts\n    const int PERTURBATIONS = 5;  // Number of post-placement perturbations\n    mt19937 rng(42);  // Fixed seed for reproducibility\n\n    for (int attempt = 0; attempt < ATTEMPTS; attempt++) {\n        // Shuffle within length groups\n        vector<string> shuffled_strings = strings;\n        auto it = shuffled_strings.begin();\n        while (it != shuffled_strings.end()) {\n            auto end = find_if(it, shuffled_strings.end(), [it](const string& s) { return s.size() != it->size(); });\n            shuffle(it, end, rng);\n            it = end;\n        }\n\n        vector<vector<char>> grid(N, vector<char>(N, '.'));\n\n        // Greedy placement\n        for (const auto& s : shuffled_strings) {\n            int k = s.size();\n            vector<tuple<int, int, int, bool>> candidates;  // (fill, i, j, horiz)\n            int max_fill = -1;\n\n            // Horizontal placements\n            for (int i = 0; i < N; i++) {\n                for (int j = 0; j < N; j++) {\n                    bool valid = true;\n                    int fill = 0;\n                    for (int p = 0; p < k; p++) {\n                        int cj = (j + p) % N;\n                        char cell = grid[i][cj];\n                        if (cell != '.' && cell != s[p]) {\n                            valid = false;\n                            break;\n                        }\n                        if (cell == '.') fill++;\n                    }\n                    if (valid) {\n                        if (fill > max_fill) {\n                            max_fill = fill;\n                            candidates.clear();\n                        }\n                        if (fill == max_fill) {\n                            candidates.emplace_back(fill, i, j, true);\n                        }\n                    }\n                }\n            }\n\n            // Vertical placements\n            for (int j = 0; j < N; j++) {\n                for (int i = 0; i < N; i++) {\n                    bool valid = true;\n                    int fill = 0;\n                    for (int p = 0; p < k; p++) {\n                        int ci = (i + p) % N;\n                        char cell = grid[ci][j];\n                        if (cell != '.' && cell != s[p]) {\n                            valid = false;\n                            break;\n                        }\n                        if (cell == '.') fill++;\n                    }\n                    if (valid) {\n                        if (fill > max_fill) {\n                            max_fill = fill;\n                            candidates.clear();\n                        }\n                        if (fill == max_fill) {\n                            candidates.emplace_back(fill, i, j, false);\n                        }\n                    }\n                }\n            }\n\n            // Place if candidates exist; randomize if multiple\n            if (!candidates.empty()) {\n                auto& selected = candidates[rng() % candidates.size()];\n                auto [fill, i, j, horiz] = selected;\n                if (horiz) {\n                    for (int p = 0; p < k; p++) {\n                        int cj = (j + p) % N;\n                        if (grid[i][cj] == '.') grid[i][cj] = s[p];\n                    }\n                } else {\n                    for (int p = 0; p < k; p++) {\n                        int ci = (i + p) % N;\n                        if (grid[ci][j] == '.') grid[ci][j] = s[p];\n                    }\n                }\n            }\n        }\n\n        // Post-placement perturbations\n        int current_c = compute_c(grid, strings);\n        for (int pert = 0; pert < PERTURBATIONS; pert++) {\n            int ri = rng() % N, rj = rng() % N;\n            char original = grid[ri][rj];\n            char new_char = (original == '.') ? \"ABCDEFGH\"[rng() % 8] : '.';\n            grid[ri][rj] = new_char;\n            int new_c = compute_c(grid, strings);\n            if (new_c >= current_c) {\n                current_c = new_c;\n            } else {\n                grid[ri][rj] = original;  // Revert\n            }\n        }\n\n        // Evaluate this attempt\n        int c = compute_c(grid, strings);\n        int d = compute_d(grid);\n        if (c > best_c || (c == best_c && d < best_d)) {\n            best_c = c;\n            best_d = d;\n            best_grid = grid;\n        }\n    }\n\n    // Output the best grid\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cout << best_grid[i][j];\n        }\n        cout << endl;\n    }\n\n    return 0;\n}","ahc005":"#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nconst ll INF = 1LL << 60;\n\nint main() {\n    int N, si, sj;\n    cin >> N >> si >> sj;\n    vector<string> c(N);\n    for(int i = 0; i < N; i++) cin >> c[i];\n    vector<vector<int>> cost(N, vector<int>(N, 0));\n    vector<pair<int, int>> roads;\n    for(int i = 0; i < N; i++) {\n        for(int j = 0; j < N; j++) {\n            if(c[i][j] != '#') {\n                cost[i][j] = c[i][j] - '0';\n                roads.emplace_back(i, j);\n            }\n        }\n    }\n    int R = roads.size();\n    \n    // Identify active rows and columns\n    vector<bool> active_row(N, false), active_col(N, false);\n    for(auto [i, j] : roads) {\n        active_row[i] = true;\n        active_col[j] = true;\n    }\n    \n    // Greedy selection for S\n    vector<bool> covered_row(N, false), covered_col(N, false);\n    vector<pair<int, int>> S;\n    set<pair<int, int>> road_set(roads.begin(), roads.end());\n    while(true) {\n        bool all_covered = true;\n        for(int i = 0; i < N; i++) {\n            if(active_row[i] && !covered_row[i]) all_covered = false;\n            if(active_col[i] && !covered_col[i]) all_covered = false;\n        }\n        if(all_covered) break;\n        \n        pair<int, int> best = {-1, -1};\n        int max_gain = -1;\n        int min_cost = INT_MAX;\n        for(auto p : roads) {\n            int i = p.first, j = p.second;\n            int gain = (active_row[i] && !covered_row[i]) + (active_col[j] && !covered_col[j]);\n            if(gain > max_gain || (gain == max_gain && cost[i][j] < min_cost)) {\n                max_gain = gain;\n                min_cost = cost[i][j];\n                best = p;\n            }\n        }\n        if(best.first == -1) break; // Should not happen\n        S.push_back(best);\n        covered_row[best.first] = true;\n        covered_col[best.second] = true;\n    }\n    // Ensure start is in S\n    bool has_start = false;\n    for(auto p : S) if(p == make_pair(si, sj)) has_start = true;\n    if(!has_start) S.push_back({si, sj});\n    \n    // Nearest neighbor to order S\n    vector<pair<int, int>> path;\n    set<pair<int, int>> visited;\n    path.push_back({si, sj});\n    visited.insert({si, sj});\n    while(path.size() < S.size()) {\n        auto curr = path.back();\n        pair<int, int> next = {-1, -1};\n        int min_dist = INT_MAX;\n        for(auto p : S) {\n            if(visited.count(p)) continue;\n            int d = abs(p.first - curr.first) + abs(p.second - curr.second);\n            if(d < min_dist) {\n                min_dist = d;\n                next = p;\n            }\n        }\n        if(next.first == -1) break;\n        path.push_back(next);\n        visited.insert(next);\n    }\n    path.push_back({si, sj});\n    \n    // Function to get path string from a to b\n    auto get_path = [&](pair<int, int> start, pair<int, int> end) -> string {\n        if(start == end) return \"\";\n        vector<vector<ll>> dist(N, vector<ll>(N, INF));\n        vector<vector<pair<int, int>>> prev(N, vector<pair<int, int>>(N, {-1, -1}));\n        priority_queue<tuple<ll, int, int>, vector<tuple<ll, int, int>>, greater<>> pq;\n        dist[start.first][start.second] = 0;\n        pq.push({0, start.first, start.second});\n        while(!pq.empty()) {\n            auto [d, i, j] = pq.top(); pq.pop();\n            if(d > dist[i][j]) continue;\n            for(auto [di, dj] : vector<pair<int, int>>{{0,1}, {0,-1}, {1,0}, {-1,0}}) {\n                int ni = i + di, nj = j + dj;\n                if(ni >= 0 && ni < N && nj >= 0 && nj < N && cost[ni][nj] > 0) {\n                    ll nd = d + cost[ni][nj];\n                    if(nd < dist[ni][nj]) {\n                        dist[ni][nj] = nd;\n                        prev[ni][nj] = {i, j};\n                        pq.push({nd, ni, nj});\n                    }\n                }\n            }\n        }\n        if(dist[end.first][end.second] == INF) return \"\"; // Unreachable (should not happen)\n        // Reconstruct path\n        vector<pair<int, int>> route;\n        pair<int, int> curr = end;\n        while(curr != make_pair(-1, -1)) {\n            route.push_back(curr);\n            curr = prev[curr.first][curr.second];\n        }\n        reverse(route.begin(), route.end());\n        // Build string\n        string s;\n        for(size_t k = 1; k < route.size(); k++) {\n            auto [i1, j1] = route[k-1];\n            auto [i2, j2] = route[k];\n            if(i2 == i1 + 1) s += 'D';\n            else if(i2 == i1 - 1) s += 'U';\n            else if(j2 == j1 + 1) s += 'R';\n            else if(j2 == j1 - 1) s += 'L';\n        }\n        return s;\n    };\n    \n    // Build full route\n    string ans;\n    for(size_t k = 1; k < path.size(); k++) {\n        ans += get_path(path[k-1], path[k]);\n    }\n    cout << ans << endl;\n    return 0;\n}","future-contest-2022-qual":"#include <bits/stdc++.h>\nusing namespace std;\n\nvoid dfs(int u, const vector<vector<int>>& adj, vector<int>& num_desc, vector<bool>& visited) {\n    visited[u] = true;\n    for (int v : adj[u]) {\n        if (!visited[v]) dfs(v, adj, num_desc, visited);\n        num_desc[u] += 1 + num_desc[v];\n    }\n}\n\nint main() {\n    ios::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 j = 0; j < K; ++j) {\n            cin >> d[i][j];\n        }\n    }\n    \n    vector<vector<int>> deps(N);\n    vector<vector<int>> adj(N);\n    for (int i = 0; i < R; ++i) {\n        int u, v;\n        cin >> u >> v;\n        --u; --v;\n        deps[v].push_back(u);\n        adj[u].push_back(v);\n    }\n    \n    vector<int> num_desc(N, 0);\n    vector<bool> visited(N, false);\n    for (int i = 0; i < N; ++i) {\n        if (!visited[i]) dfs(i, adj, num_desc, visited);\n    }\n    \n    vector<int> task_status(N, -1);\n    vector<int> member_status(M, -1);\n    vector<int> start_day(N, -1);\n    vector<vector<int>> est_s(M, vector<int>(K, 0));\n    \n    int current_day = 0;\n    while (true) {\n        ++current_day;\n        \n        vector<int> free_members;\n        for (int j = 0; j < M; ++j) {\n            if (member_status[j] == -1) free_members.push_back(j);\n        }\n        \n        vector<int> ready_tasks;\n        for (int i = 0; i < N; ++i) {\n            if (task_status[i] == -1) {\n                bool can_start = true;\n                for (int u : deps[i]) {\n                    if (task_status[u] != 1) {\n                        can_start = false;\n                        break;\n                    }\n                }\n                if (can_start) ready_tasks.push_back(i);\n            }\n        }\n        \n        vector<vector<long long>> est_w(ready_tasks.size(), vector<long long>(free_members.size()));\n        for (int ti = 0; ti < ready_tasks.size(); ++ti) {\n            int i = ready_tasks[ti];\n            for (int mj = 0; mj < free_members.size(); ++mj) {\n                int j = free_members[mj];\n                long long w = 0;\n                for (int k = 0; k < K; ++k) {\n                    w += max(0, d[i][k] - est_s[j][k]);\n                }\n                est_w[ti][mj] = w;\n            }\n        }\n        \n        vector<tuple<int, long long, int>> task_order;\n        for (int ti = 0; ti < ready_tasks.size(); ++ti) {\n            long long min_w = LLONG_MAX;\n            for (int mj = 0; mj < free_members.size(); ++mj) {\n                min_w = min(min_w, est_w[ti][mj]);\n            }\n            task_order.emplace_back(-num_desc[ready_tasks[ti]], min_w, ti);\n        }\n        sort(task_order.begin(), task_order.end());\n        \n        vector<pair<int, int>> assignments;\n        set<int> used_members;\n        for (auto& tup : task_order) {\n            int ti = get<2>(tup);\n            int i = ready_tasks[ti];\n            long long best_w = LLONG_MAX;\n            int best_j = -1;\n            for (int mj = 0; mj < free_members.size(); ++mj) {\n                int j = free_members[mj];\n                if (used_members.count(j)) continue;\n                if (est_w[ti][mj] < best_w) {\n                    best_w = est_w[ti][mj];\n                    best_j = j;\n                }\n            }\n            if (best_j != -1) {\n                assignments.emplace_back(best_j, i);\n                used_members.insert(best_j);\n            }\n        }\n        \n        cout << assignments.size();\n        for (auto& p : assignments) {\n            cout << \" \" << (p.first + 1) << \" \" << (p.second + 1);\n            task_status[p.second] = 0;\n            member_status[p.first] = p.second;\n            start_day[p.second] = current_day;\n        }\n        cout << \"\\n\";\n        cout.flush();\n        \n        int n;\n        cin >> n;\n        if (n == -1) break;\n        \n        vector<int> completers(n);\n        for (int i = 0; i < n; ++i) {\n            cin >> completers[i];\n            --completers[i];\n        }\n        \n        for (int f : completers) {\n            int task = member_status[f];\n            if (task == -1) continue;\n            int t = current_day - start_day[task] + 1;\n            if (t == 1) {\n                for (int k = 0; k < K; ++k) {\n                    est_s[f][k] = max(est_s[f][k], d[task][k]);\n                }\n            } else {\n                long long current_w = 0;\n                vector<int> deficit_skills;\n                for (int k = 0; k < K; ++k) {\n                    int def = max(0, d[task][k] - est_s[f][k]);\n                    current_w += def;\n                    if (def > 0) deficit_skills.push_back(k);\n                }\n                long long additional = t - current_w;\n                if (!deficit_skills.empty() && additional > 0) {\n                    double alpha = 0.6;\n                    double add_per = alpha * additional / deficit_skills.size();\n                    for (int k : deficit_skills) {\n                        est_s[f][k] += (int)add_per;\n                    }\n                }\n            }\n            task_status[task] = 1;\n            member_status[f] = -1;\n        }\n        \n        bool all_done = true;\n        for (int i = 0; i < N; ++i) {\n            if (task_status[i] != 1) all_done = false;\n        }\n        if (all_done) break;\n    }\n    \n    return 0;\n}","ahc006":"#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing pii = pair<int, int>;\n\nstruct Order {\n    int a, b, c, d, idx;\n    ll cost;\n};\n\nll dist(int x1, int y1, int x2, int y2) {\n    return abs(x1 - x2) + abs(y1 - y2);\n}\n\n// Check if a sequence violates precedence (pickup before delivery for each order)\n// Removed unused 'selected' parameter\nbool is_valid(const vector<int>& order) {\n    vector<int> pickup_pos(50, -1), delivery_pos(50, -1);\n    for (size_t i = 0; i < order.size(); ++i) {  // Fixed: use size_t to match vector::size_type\n        int idx = order[i];\n        if (idx < 50) {  // Pickup (0-49)\n            pickup_pos[idx] = i;\n        } else {  // Delivery (50-99)\n            delivery_pos[idx - 50] = i;\n        }\n    }\n    for (int i = 0; i < 50; ++i) {\n        if (pickup_pos[i] >= delivery_pos[i]) return false;\n    }\n    return true;\n}\n\n// Simulated Annealing with precedence constraints and 2-opt\nvector<int> simulated_annealing(const vector<pii>& points, mt19937& rng) {\n    int n = 100;  // 50 pickups + 50 deliveries\n    vector<int> order(n);\n    for (int i = 0; i < n; ++i) order[i] = i;  // Initial: pickups 0-49, deliveries 50-99\n    \n    auto calc_dist = [&](const vector<int>& ord) -> ll {\n        ll total = dist(400, 400, points[ord[0]].first, points[ord[0]].second);  // Office is (400,400)\n        for (int i = 0; i < n - 1; ++i) {\n            total += dist(points[ord[i]].first, points[ord[i]].second,\n                          points[ord[i+1]].first, points[ord[i+1]].second);\n        }\n        total += dist(points[ord.back()].first, points[ord.back()].second, 400, 400);\n        return total;\n    };\n    \n    ll best_dist = calc_dist(order);\n    vector<int> best_order = order;\n    \n    double T = 1000.0;\n    double alpha = 0.998;  // Slower cooling for better exploration\n    int max_iter = 200000;  // Increased iterations\n    \n    uniform_int_distribution<int> dist_idx(0, n - 1);\n    uniform_real_distribution<double> dist_prob(0.0, 1.0);\n    \n    for (int iter = 0; iter < max_iter; ++iter) {\n        // Perturb: swap two random indices, but only if valid\n        vector<int> new_order = order;\n        int i = dist_idx(rng), j = dist_idx(rng);\n        swap(new_order[i], new_order[j]);\n        if (!is_valid(new_order)) continue;  // Skip invalid swaps\n        \n        ll new_dist = calc_dist(new_order);\n        ll delta = new_dist - calc_dist(order);\n        \n        if (delta < 0 || dist_prob(rng) < exp(-delta / T)) {\n            order = new_order;\n            if (new_dist < best_dist) {\n                best_dist = new_dist;\n                best_order = order;\n            }\n        }\n        \n        T *= alpha;\n    }\n    \n    // Apply 2-opt for local refinement\n    auto two_opt = [&](vector<int>& ord) {\n        bool improved = true;\n        while (improved) {\n            improved = false;\n            for (int i = 0; i < n - 1; ++i) {\n                for (int j = i + 1; j < n; ++j) {\n                    vector<int> new_ord = ord;\n                    reverse(new_ord.begin() + i, new_ord.begin() + j + 1);\n                    if (is_valid(new_ord) && calc_dist(new_ord) < calc_dist(ord)) {\n                        ord = new_ord;\n                        improved = true;\n                    }\n                }\n            }\n        }\n    };\n    two_opt(best_order);\n    \n    return best_order;\n}\n\nint main() {\n    const int OFFICE_X = 400, OFFICE_Y = 400;\n    vector<Order> orders(1000);\n    for (int i = 0; i < 1000; ++i) {\n        cin >> orders[i].a >> orders[i].b >> orders[i].c >> orders[i].d;\n        orders[i].idx = i + 1;\n        orders[i].cost = dist(OFFICE_X, OFFICE_Y, orders[i].a, orders[i].b) +\n                         dist(orders[i].a, orders[i].b, orders[i].c, orders[i].d) +\n                         dist(orders[i].c, orders[i].d, OFFICE_X, OFFICE_Y);\n    }\n    \n    // Select 50 orders with lowest cost\n    sort(orders.begin(), orders.end(), [](const Order& o1, const Order& o2) {\n        return o1.cost < o2.cost;\n    });\n    vector<Order> selected(orders.begin(), orders.begin() + 50);\n    \n    // Build points vector in main (fixed scoping)\n    vector<pii> points(100);\n    for (int i = 0; i < 50; ++i) {\n        points[i] = {selected[i].a, selected[i].b};\n        points[i + 50] = {selected[i].c, selected[i].d};\n    }\n    \n    // Optimize full route with SA (multiple restarts)\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n    vector<int> best_order;\n    ll best_total_dist = LLONG_MAX;\n    for (int restart = 0; restart < 3; ++restart) {  // 3 restarts for robustness\n        auto order = simulated_annealing(points, rng);\n        // Compute distance for this order\n        ll total_dist = dist(OFFICE_X, OFFICE_Y, points[order[0]].first, points[order[0]].second);\n        for (int i = 0; i < 99; ++i) {\n            total_dist += dist(points[order[i]].first, points[order[i]].second,\n                               points[order[i+1]].first, points[order[i+1]].second);\n        }\n        total_dist += dist(points[order.back()].first, points[order.back()].second, OFFICE_X, OFFICE_Y);\n        if (total_dist < best_total_dist) {\n            best_total_dist = total_dist;\n            best_order = order;\n        }\n    }\n    \n    // Build final route\n    vector<pii> full_route;\n    full_route.emplace_back(OFFICE_X, OFFICE_Y);\n    for (int idx : best_order) {\n        full_route.push_back(points[idx]);\n    }\n    full_route.emplace_back(OFFICE_X, OFFICE_Y);\n    \n    // Output\n    cout << 50;\n    for (const auto& ord : selected) cout << \" \" << ord.idx;\n    cout << \"\\n\";\n    cout << full_route.size();\n    for (const auto& p : full_route) cout << \" \" << p.first << \" \" << p.second;\n    cout << \"\\n\";\n    \n    return 0;\n}","ahc007":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    const int N = 400;\n    const int M = 1995;\n    vector<int> x(N), y(N);\n    for (int i = 0; i < N; i++) {\n        cin >> x[i] >> y[i];\n    }\n    vector<int> u(M), v(M);\n    for (int i = 0; i < M; i++) {\n        cin >> u[i] >> v[i];\n    }\n    // Precompute d_i for each edge\n    vector<int> d(M);\n    for (int i = 0; i < M; i++) {\n        long long dx = x[u[i]] - x[v[i]];\n        long long dy = y[u[i]] - y[v[i]];\n        d[i] = round(sqrt(dx * dx + dy * dy));\n    }\n    // Adjacency list: vector of {to, {edge_index, weight}}\n    vector<vector<pair<int, pair<int, int>>>> adj(N);\n    vector<bool> adopted(M, false);\n    vector<int> weights(M, 0);\n    // Multiset for median weight tracking\n    multiset<int> weight_set;\n    // Running total cost\n    long long total_cost = 0;\n    int adopted_count = 0;\n    \n    auto remove_edge = [&](int a, int b, int e) {\n        for (auto it = adj[a].begin(); it != adj[a].end(); ++it) {\n            if (it->second.first == e) {\n                adj[a].erase(it);\n                break;\n            }\n        }\n    };\n    \n    for (int i = 0; i < M; i++) {\n        int l;\n        cin >> l;\n        int uu = u[i], vv = v[i];\n        // BFS to check connectivity and find max on path\n        vector<int> dist(N, -1);\n        vector<int> parent(N, -1);\n        vector<int> edge_to_parent(N, -1);\n        queue<int> q;\n        q.push(uu);\n        dist[uu] = 0;\n        bool found = false;\n        int max_weight = -1;\n        int max_edge = -1;\n        while (!q.empty() && !found) {\n            int cur = q.front();\n            q.pop();\n            for (auto &[to, p] : adj[cur]) {\n                int eidx = p.first;\n                int w = p.second;\n                if (dist[to] == -1) {\n                    dist[to] = dist[cur] + 1;\n                    parent[to] = cur;\n                    edge_to_parent[to] = eidx;\n                    q.push(to);\n                    if (to == vv) {\n                        found = true;\n                        // Backtrack to find max on path\n                        int current = vv;\n                        while (current != uu) {\n                            int e = edge_to_parent[current];\n                            int ww = weights[edge_to_parent[current]];\n                            if (ww > max_weight) {\n                                max_weight = ww;\n                                max_edge = edge_to_parent[current];\n                            }\n                            current = parent[current];\n                        }\n                        break;\n                    }\n                }\n            }\n        }\n        bool adopt = false;\n        if (!found) {\n            // Not connected, adopt\n            adopt = true;\n        } else {\n            // Connected, check for replacement (more aggressive)\n            if (l * 1.1 < max_weight) {\n                adopt = true;\n                // Remove max_edge\n                adopted[max_edge] = false;\n                weight_set.erase(weight_set.find(weights[max_edge]));\n                total_cost -= weights[max_edge];\n                adopted_count--;\n                remove_edge(u[max_edge], v[max_edge], max_edge);\n                remove_edge(v[max_edge], u[max_edge], max_edge);\n            } else {\n                // Check for new adoption heuristic\n                int median_weight = (weight_set.size() < 10) ? 2000 : *next(weight_set.begin(), weight_set.size() / 2);\n                double avg_cost = (adopted_count == 0) ? 1e9 : total_cost / (double)adopted_count;\n                if (l <= 1.2 * d[i] && l < median_weight * 1.05 && l < avg_cost * 0.9) {\n                    adopt = true;\n                }\n            }\n        }\n        if (adopt) {\n            adopted[i] = true;\n            adj[uu].emplace_back(vv, make_pair(i, l));\n            adj[vv].emplace_back(uu, make_pair(i, l));\n            weights[i] = l;\n            weight_set.insert(l);\n            total_cost += l;\n            adopted_count++;\n            cout << 1 << endl;\n            fflush(stdout);\n        } else {\n            cout << 0 << endl;\n            fflush(stdout);\n        }\n    }\n    return 0;\n}","ahc008":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int DX[4] = {-1, 0, 1, 0};\nconst int DY[4] = {0, 1, 0, -1};\n\ndouble compute_score(pair<int, int> pos, const vector<vector<bool>>& passable, const vector<pair<int, int>>& pets) {\n    vector<vector<bool>> vis(31, vector<bool>(31, false));\n    queue<pair<int, int>> q;\n    q.push(pos);\n    vis[pos.first][pos.second] = true;\n    int area = 1;\n    int num_pets = 0;\n    for (auto& p : pets) {\n        if (p == pos) num_pets++;\n    }\n    while (!q.empty()) {\n        auto [x, y] = q.front();\n        q.pop();\n        for (int d = 0; d < 4; d++) {\n            int nx = x + DX[d], ny = y + DY[d];\n            if (nx >= 1 && nx <= 30 && ny >= 1 && ny <= 30 && passable[nx][ny] && !vis[nx][ny]) {\n                vis[nx][ny] = true;\n                area++;\n                for (auto& p : pets) {\n                    if (p == make_pair(nx, ny)) num_pets++;\n                }\n                q.push({nx, ny});\n            }\n        }\n    }\n    return (area / 900.0) * pow(2.0, -num_pets);\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N;\n    cin >> N;\n    vector<pair<int, int>> pets(N);\n    vector<int> pt(N);\n    for (int i = 0; i < N; i++) {\n        int x, y, t;\n        cin >> x >> y >> t;\n        pets[i] = {x, y};\n        pt[i] = t;\n    }\n    int M;\n    cin >> M;\n    vector<pair<int, int>> humans(M);\n    for (int i = 0; i < M; i++) {\n        int x, y;\n        cin >> x >> y;\n        humans[i] = {x, y};\n    }\n\n    vector<vector<bool>> passable(31, vector<bool>(31, true));\n\n    for (int turn = 0; turn < 300; turn++) {\n        vector<string> actions(M, \".\");\n        for (int i = 0; i < M; i++) {\n            double max_s = -1e9;\n            string best_act = \".\";\n            pair<int, int> cur_pos = humans[i];\n\n            // Try stay\n            {\n                double s = compute_score(cur_pos, passable, pets);\n                if (s > max_s) {\n                    max_s = s;\n                    best_act = \".\";\n                }\n            }\n\n            // Try builds\n            vector<string> builds = {\"u\", \"d\", \"l\", \"r\"};\n            for (auto act : builds) {\n                int dx = 0, dy = 0;\n                if (act == \"u\") dx = -1;\n                else if (act == \"d\") dx = 1;\n                else if (act == \"l\") dy = -1;\n                else if (act == \"r\") dy = 1;\n                int nx = cur_pos.first + dx, ny = cur_pos.second + dy;\n                bool valid = true;\n                if (nx < 1 || nx > 30 || ny < 1 || ny > 30) valid = false;\n                else {\n                    // Check for entities\n                    bool has_entity = false;\n                    for (auto& p : pets) {\n                        if (p == make_pair(nx, ny)) has_entity = true;\n                    }\n                    for (int j = 0; j < M; j++) {\n                        if (humans[j] == make_pair(nx, ny)) has_entity = true;\n                    }\n                    if (has_entity) valid = false;\n                    // Check adjacent pets\n                    for (int d = 0; d < 4; d++) {\n                        int ax = nx + DX[d], ay = ny + DY[d];\n                        if (ax >= 1 && ax <= 30 && ay >= 1 && ay <= 30) {\n                            for (auto& p : pets) {\n                                if (p == make_pair(ax, ay)) {\n                                    valid = false;\n                                    break;\n                                }\n                            }\n                        }\n                        if (!valid) break;\n                    }\n                    if (valid) {\n                        passable[nx][ny] = false;\n                        double s = compute_score(cur_pos, passable, pets);\n                        if (s > max_s) {\n                            max_s = s;\n                            best_act = act;\n                        }\n                        passable[nx][ny] = true;\n                    }\n                }\n            }\n\n            // Try moves\n            vector<string> moves = {\"U\", \"D\", \"L\", \"R\"};\n            for (auto act : moves) {\n                int dx = 0, dy = 0;\n                if (act == \"U\") dx = -1;\n                else if (act == \"D\") dx = 1;\n                else if (act == \"L\") dy = -1;\n                else if (act == \"R\") dy = 1;\n                int nx = cur_pos.first + dx, ny = cur_pos.second + dy;\n                bool valid = true;\n                if (nx < 1 || nx > 30 || ny < 1 || ny > 30) valid = false;\n                else if (!passable[nx][ny]) valid = false;\n                else {\n                    pair<int, int> new_pos = {nx, ny};\n                    double s = compute_score(new_pos, passable, pets);\n                    if (s > max_s) {\n                        max_s = s;\n                        best_act = act;\n                    }\n                }\n            }\n            actions[i] = best_act;\n        }\n\n        // Apply human moves\n        for (int i = 0; i < M; i++) {\n            string act = actions[i];\n            if (act[0] >= 'A' && act[0] <= 'Z') {  // Move\n                int dx = 0, dy = 0;\n                if (act == \"U\") dx = -1;\n                else if (act == \"D\") dx = 1;\n                else if (act == \"L\") dy = -1;\n                else if (act == \"R\") dy = 1;\n                int nx = humans[i].first + dx, ny = humans[i].second + dy;\n                humans[i] = {nx, ny};\n            }\n        }\n\n        // Apply builds\n        for (int i = 0; i < M; i++) {\n            string act = actions[i];\n            if (act[0] >= 'a' && act[0] <= 'z') {  // Build\n                int dx = 0, dy = 0;\n                if (act == \"u\") dx = -1;\n                else if (act == \"d\") dx = 1;\n                else if (act == \"l\") dy = -1;\n                else if (act == \"r\") dy = 1;\n                int nx = humans[i].first + dx, ny = humans[i].second + dy;\n                passable[nx][ny] = false;\n            }\n        }\n\n        // Output actions\n        for (auto& a : actions) cout << a;\n        cout << endl;\n        cout.flush();\n\n        // Read and apply pet moves\n        for (int i = 0; i < N; i++) {\n            string s;\n            cin >> s;\n            if (s == \".\") continue;\n            pair<int, int> pos = pets[i];\n            for (char c : s) {\n                int dx = 0, dy = 0;\n                if (c == 'U') dx = -1;\n                else if (c == 'D') dx = 1;\n                else if (c == 'L') dy = -1;\n                else if (c == 'R') dy = 1;\n                int nx = pos.first + dx, ny = pos.second + dy;\n                if (nx >= 1 && nx <= 30 && ny >= 1 && ny <= 30 && passable[nx][ny]) {\n                    pos = {nx, ny};\n                }\n            }\n            pets[i] = pos;\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), v(20);\n    for (int i = 0; i < 20; i++) cin >> h[i];\n    for (int i = 0; i < 20; i++) cin >> v[i];\n    \n    // Build adjacency list for the grid\n    vector<vector<vector<pair<int, int>>>> adj(20, vector<vector<pair<int, int>>>(20));\n    for (int i = 0; i < 20; i++) {\n        for (int j = 0; j < 20; j++) {\n            // Right\n            if (j < 19 && h[i][j] == '0') adj[i][j].push_back({i, j + 1});\n            // Left\n            if (j > 0 && h[i][j - 1] == '0') adj[i][j].push_back({i, j - 1});\n            // Down\n            if (i < 19 && v[i][j] == '0') adj[i][j].push_back({i + 1, j});\n            // Up\n            if (i > 0 && v[i - 1][j] == '0') adj[i][j].push_back({i - 1, j});\n        }\n    }\n    \n    // BFS to find shortest path\n    vector<vector<int>> dist(20, vector<int>(20, -1));\n    vector<vector<pair<int, int>>> parent(20, vector<pair<int, int>>(20, {-1, -1}));\n    queue<pair<int, int>> q;\n    q.push({si, sj});\n    dist[si][sj] = 0;\n    \n    while (!q.empty()) {\n        auto [x, y] = q.front(); q.pop();\n        for (auto [nx, ny] : adj[x][y]) {\n            if (dist[nx][ny] == -1) {\n                dist[nx][ny] = dist[x][y] + 1;\n                parent[nx][ny] = {x, y};\n                q.push({nx, ny});\n            }\n        }\n    }\n    \n    // If unreachable (shouldn't happen), output empty\n    if (dist[ti][tj] == -1) {\n        cout << \"\" << endl;\n        return 0;\n    }\n    \n    // Reconstruct the path\n    vector<pair<int, int>> path;\n    pair<int, int> cur = {ti, tj};\n    while (cur != make_pair(si, sj)) {\n        path.push_back(cur);\n        cur = parent[cur.first][cur.second];\n    }\n    path.push_back({si, sj});\n    reverse(path.begin(), path.end());\n    \n    // Generate the move string\n    string full_moves = \"\";\n    for (size_t i = 1; i < path.size(); i++) {\n        auto [px, py] = path[i - 1];\n        auto [cx, cy] = path[i];\n        if (cx == px + 1) full_moves += 'D';\n        else if (cx == px - 1) full_moves += 'U';\n        else if (cy == py + 1) full_moves += 'R';\n        else if (cy == py - 1) full_moves += 'L';\n    }\n    \n    // Handle length <= 200 with repetition for robustness\n    int L = full_moves.size();\n    string output;\n    if (L <= 200) {\n        int repeat = 200 / L;\n        for (int i = 0; i < repeat; i++) output += full_moves;\n        int rem = 200 - output.size();\n        for (int i = 0; i < rem; i++) output += full_moves[i % L];\n    } else {\n        // Truncate to 200 moves\n        output = full_moves.substr(0, 200);\n    }\n    \n    cout << output << endl;\n    return 0;\n}","ahc010":"#include <bits/stdc++.h>\nusing namespace std;\n\nmt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n\nconst vector<vector<int>> to = {\n    {1, 0, -1, -1},  // 0\n    {3, -1, -1, 0},  // 1\n    {-1, -1, 3, 2},  // 2\n    {-1, 2, 1, -1},  // 3\n    {1, 0, 3, 2},    // 4\n    {3, 2, 1, 0},    // 5\n    {2, -1, 0, -1},  // 6\n    {-1, 3, -1, 1}   // 7\n};\n\nconst vector<int> di = {0, -1, 0, 1};\nconst vector<int> dj = {-1, 0, 1, 0};\n\nint get_type(int t, int r) {\n    if (t <= 3) return (t + r) % 4;\n    if (t <= 5) return 4 + (t - 4 + r) % 2;\n    return 6 + (t - 6 + r) % 2;\n}\n\nlong long compute_score(const vector<vector<int>>& tiles, const vector<int>& rot) {\n    vector<vector<int>> adj(900);\n    for (int i = 0; i < 30; i++) {\n        for (int j = 0; j < 30; j++) {\n            int idx = i * 30 + j;\n            int t = tiles[i][j];\n            int r = rot[idx];\n            int et = get_type(t, r);\n            for (int d = 0; d < 4; d++) {\n                if (to[et][d] != -1) {\n                    int d2 = to[et][d];\n                    int ni = i + di[d2];\n                    int nj = j + dj[d2];\n                    if (ni >= 0 && ni < 30 && nj >= 0 && nj < 30) {\n                        int nidx = ni * 30 + nj;\n                        if (ni > i || (ni == i && nj > j)) {\n                            adj[idx].push_back(nidx);\n                            adj[nidx].push_back(idx);\n                        }\n                    }\n                }\n            }\n        }\n    }\n    vector<int> cycle_lengths;\n    vector<bool> visited(900, false);\n    vector<int> disc(900, -1);\n    vector<int> parent(900, -1);\n    int timestamp = 0;\n    function<void(int)> dfs = [&](int u) {\n        visited[u] = true;\n        disc[u] = timestamp++;\n        for (int v : adj[u]) {\n            if (!visited[v]) {\n                parent[v] = u;\n                dfs(v);\n            } else if (v != parent[u] && disc[v] < disc[u]) {\n                int len = disc[u] - disc[v] + 1;\n                cycle_lengths.push_back(len);\n            }\n        }\n    };\n    for (int i = 0; i < 900; i++) if (!visited[i]) dfs(i);\n    sort(cycle_lengths.rbegin(), cycle_lengths.rend());\n    if (cycle_lengths.size() >= 2) {\n        return (long long)cycle_lengths[0] * cycle_lengths[1];\n    }\n    return 0;\n}\n\nint main() {\n    vector<vector<int>> tiles(30, vector<int>(30));\n    for (int i = 0; i < 30; i++) {\n        string s;\n        cin >> s;\n        for (int j = 0; j < 30; j++) tiles[i][j] = s[j] - '0';\n    }\n    // Random sampling: Generate 1000 random configurations and pick the best\n    vector<int> best_rot(900);\n    long long best_score = 0;\n    for (int sample = 0; sample < 1000; sample++) {\n        vector<int> rot(900);\n        for (int &x : rot) x = rng() % 4;\n        long long score = compute_score(tiles, rot);\n        if (score > best_score) {\n            best_score = score;\n            best_rot = rot;\n        }\n    }\n    // Limited local search: Try 1-2 random rotations per tile on the best\n    for (int attempt = 0; attempt < 900; attempt++) {\n        int idx = rng() % 900;\n        int old_r = best_rot[idx];\n        for (int r = 0; r < 4; r++) {\n            if (r == old_r) continue;\n            best_rot[idx] = r;\n            long long new_score = compute_score(tiles, best_rot);\n            if (new_score > best_score) {\n                best_score = new_score;\n            } else {\n                best_rot[idx] = old_r;\n            }\n        }\n    }\n    string output;\n    for (int x : best_rot) output += '0' + x;\n    cout << output << endl;\n    return 0;\n}","ahc011":"#include <bits/stdc++.h>\nusing namespace std;\n\nint N, T;\nvector<vector<int>> board;\npair<int, int> empty_pos;\n\n// Directions: 0=U, 1=D, 2=L, 3=R\nvector<pair<int, int>> dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};\nstring dir_chars = \"UDLR\";\n\n// Check if a position is valid\nbool valid(int i, int j) {\n    return i >= 0 && i < N && j >= 0 && j < N;\n}\n\n// Simulate a move: return new board and new empty pos\npair<vector<vector<int>>, pair<int, int>> simulate_move(const vector<vector<int>>& curr_board, pair<int, int> curr_empty, int dir) {\n    auto [ei, ej] = curr_empty;\n    auto [di, dj] = dirs[dir];\n    int ni = ei + di, nj = ej + dj;\n    if (!valid(ni, nj)) return {curr_board, curr_empty}; // Invalid move\n    auto new_board = curr_board;\n    swap(new_board[ei][ej], new_board[ni][nj]);\n    return {new_board, {ni, nj}};\n}\n\n// Compute the size of the largest tree component\nint compute_tree_size(const vector<vector<int>>& curr_board) {\n    vector<vector<bool>> visited(N, vector<bool>(N, false));\n    int max_tree = 0;\n    \n    // Directions for connections: 0=left, 1=up, 2=right, 3=down\n    vector<pair<int, int>> conn_dirs = {{0, -1}, {-1, 0}, {0, 1}, {1, 0}};\n    vector<int> masks = {1, 2, 4, 8}; // left, up, right, down\n    \n    for (int i = 0; i < N; ++i) {\n        for (int j = 0; j < N; ++j) {\n            if (curr_board[i][j] == 0 || visited[i][j]) continue;\n            \n            // BFS for component\n            queue<pair<int, int>> q;\n            q.push({i, j});\n            visited[i][j] = true;\n            int vertices = 0, edges = 0;\n            vector<pair<int, int>> component;\n            \n            while (!q.empty()) {\n                auto [ci, cj] = q.front(); q.pop();\n                vertices++;\n                component.push_back({ci, cj});\n                \n                for (int d = 0; d < 4; ++d) {\n                    auto [di, dj] = conn_dirs[d];\n                    int ni = ci + di, nj = cj + dj;\n                    if (valid(ni, nj) && curr_board[ni][nj] != 0) {\n                        // Check if connection exists\n                        bool has_out = (curr_board[ci][cj] & masks[d]) != 0;\n                        bool has_in = (curr_board[ni][nj] & masks[(d + 2) % 4]) != 0; // Opposite direction\n                        if (has_out && has_in) {\n                            edges++;\n                            if (!visited[ni][nj]) {\n                                visited[ni][nj] = true;\n                                q.push({ni, nj});\n                            }\n                        }\n                    }\n                }\n            }\n            \n            // Check if it's a tree: edges == vertices - 1\n            if (edges == vertices - 1) {\n                max_tree = max(max_tree, vertices);\n            }\n        }\n    }\n    return max_tree;\n}\n\nint main() {\n    cin >> N >> T;\n    board.resize(N, vector<int>(N));\n    for (int i = 0; i < N; ++i) {\n        string s; cin >> s;\n        for (int j = 0; j < N; ++j) {\n            if (s[j] >= '0' && s[j] <= '9') board[i][j] = s[j] - '0';\n            else board[i][j] = 10 + (s[j] - 'a');\n            if (board[i][j] == 0) empty_pos = {i, j};\n        }\n    }\n    \n    string moves = \"\";\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n    uniform_real_distribution<double> dist(0.0, 1.0);\n    \n    int current_score = compute_tree_size(board);\n    const double RANDOM_PROB = 0.1; // Probability to choose random move\n    \n    while (moves.size() < T) {\n        vector<pair<int, string>> candidates;\n        int best_score = -1;\n        string best_move = \"\";\n        \n        // Evaluate all 4 moves\n        for (int d = 0; d < 4; ++d) {\n            auto [new_board, new_empty] = simulate_move(board, empty_pos, d);\n            if (new_empty == empty_pos) continue; // Invalid move\n            int score = compute_tree_size(new_board);\n            if (score > best_score) {\n                best_score = score;\n                best_move = string(1, dir_chars[d]);\n            }\n        }\n        \n        // Decide: greedy or random\n        string chosen_move;\n        if (dist(rng) < RANDOM_PROB || best_move.empty()) {\n            // Random valid move\n            vector<int> valid_dirs;\n            for (int d = 0; d < 4; ++d) {\n                auto [new_board, new_empty] = simulate_move(board, empty_pos, d);\n                if (new_empty != empty_pos) valid_dirs.push_back(d);\n            }\n            if (!valid_dirs.empty()) {\n                uniform_int_distribution<int> rand_dir(0, valid_dirs.size() - 1);\n                chosen_move = string(1, dir_chars[valid_dirs[rand_dir(rng)]]);\n            }\n        } else {\n            chosen_move = best_move;\n        }\n        \n        if (chosen_move.empty()) break; // No valid moves, stop\n        \n        // Apply the move\n        int dir_idx = dir_chars.find(chosen_move[0]);\n        auto [new_board, new_empty] = simulate_move(board, empty_pos, dir_idx);\n        board = new_board;\n        empty_pos = new_empty;\n        moves += chosen_move;\n        \n        // Update current score\n        current_score = compute_tree_size(board);\n        \n        // If full tree, we could stop early, but for now, continue to potentially improve\n        if (current_score == N * N - 1) {\n            // Could add logic to minimize K, e.g., stop if score doesn't improve\n        }\n    }\n    \n    cout << moves << endl;\n    return 0;\n}","ahc012":"#include <bits/stdc++.h>\n\nusing namespace std;\n\nint main() {\n    int N, K;\n    cin >> N >> K;\n    vector<int> a(11);\n    for(int i = 1; i <= 10; i++) cin >> a[i];\n    vector<long long> x(N), y(N);\n    for(int i = 0; i < N; i++) cin >> x[i] >> y[i];\n    \n    // Output multiple solutions (last one used), each with 100 lines\n    for(int sol = 0; sol < 3; sol++) {\n        mt19937 rng(42 + sol);  // Different seed for each solution\n        uniform_int_distribution<int> dist(0, N - 1);\n        \n        // Collect candidates: pairs with their distances\n        vector<tuple<double, int, int>> candidates;\n        const int attempts = 10000;  // Generate 10000 random pairs\n        for(int attempt = 0; attempt < attempts; attempt++) {\n            int i = dist(rng);\n            int j = dist(rng);\n            if(i == j) continue;\n            double dx = x[i] - x[j];\n            double dy = y[i] - y[j];\n            double dist_val = sqrt(dx * dx + dy * dy);\n            candidates.emplace_back(dist_val, i, j);\n        }\n        \n        // Sort by distance ascending (prefer closer pairs)\n        sort(candidates.begin(), candidates.end());\n        \n        // Select up to 100 unique lines from the closest candidates\n        set<pair<pair<long long, long long>, pair<long long, long long>>> used_lines;\n        vector<tuple<long long, long long, long long, long long>> lines;\n        for(auto [dist_val, i, j] : candidates) {\n            if(lines.size() >= 100) break;\n            long long px = x[i], py = y[i];\n            long long qx = x[j], qy = y[j];\n            // Normalize: sort points to avoid duplicates\n            if(px > qx || (px == qx && py > qy)) {\n                swap(px, qx);\n                swap(py, qy);\n            }\n            pair<pair<long long, long long>, pair<long long, long long>> line_key = {{px, py}, {qx, qy}};\n            if(used_lines.insert(line_key).second) {\n                lines.emplace_back(px, py, qx, qy);\n            }\n        }\n        \n        // Output this solution\n        cout << lines.size() << endl;\n        for(auto [px, py, qx, qy] : lines) {\n            cout << px << \" \" << py << \" \" << qx << \" \" << qy << endl;\n        }\n    }\n    \n    return 0;\n}","ahc014":"#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n\nstruct Operation {\n    int w, x1, y1, x2, y2, x3, y3, x4, y4;\n};\n\nbool cmp(const Operation& a, const Operation& b) {\n    return a.w < b.w;\n}\n\nint main() {\n    int N, M;\n    cin >> N >> M;\n    vector<vector<bool>> has_dot(N, vector<bool>(N, false));\n    int c = (N - 1) / 2;\n    auto calc_w = [&](int x, int y) {\n        int dx = x - c;\n        int dy = y - c;\n        return dx * dx + dy * dy + 1;\n    };\n    for (int i = 0; i < M; i++) {\n        int x, y;\n        cin >> x >> y;\n        has_dot[x][y] = true;\n    }\n    vector<vector<vector<bool>>> horiz(N, vector<vector<bool>>(N, vector<bool>(N, false)));\n    vector<vector<vector<bool>>> vert(N, vector<vector<bool>>(N, vector<bool>(N, false)));\n    vector<vector<bool>> diag1(N - 1, vector<bool>(N - 1, false));\n    vector<vector<bool>> diag2(N - 1, vector<bool>(N, false));\n    vector<vector<bool>> diag3(N, vector<bool>(N - 1, false));\n    vector<vector<bool>> diag4(N, vector<bool>(N, false));\n    vector<Operation> ops;\n    for (int px = 0; px < N; px++) {\n        for (int py = 0; py < N; py++) {\n            if (has_dot[px][py]) continue;\n            int w = calc_w(px, py);\n            // axis-aligned\n            for (int dx = 1; dx <= N - 1 - px; dx++) {\n                for (int dy = 1; dy <= N - 1 - py; dy++) {\n                    int bx = px + dx, by = py;\n                    int cx = px + dx, cy = py + dy;\n                    int ex = px, ey = py + dy;\n                    if (has_dot[bx][by] && has_dot[cx][cy] && has_dot[ex][ey]) {\n                        bool ok = true;\n                        // check sides\n                        for (int i = px + 1; i < bx; i++) if (has_dot[py][i]) ok = false;\n                        for (int i = px + 1; i < bx; i++) if (has_dot[cy][i]) ok = false;\n                        for (int i = py + 1; i < cy; i++) if (has_dot[px][i]) ok = false;\n                        for (int i = py + 1; i < cy; i++) if (has_dot[bx][i]) ok = false;\n                        // check edges\n                        for (int i = px; i < bx; i++) if (horiz[py][i][px]) ok = false;\n                        for (int i = px; i < bx; i++) if (horiz[cy][i][px]) ok = false;\n                        for (int i = py; i < cy; i++) if (vert[px][i][py]) ok = false;\n                        for (int i = py; i < cy; i++) if (vert[bx][i][py]) ok = false;\n                        if (ok) {\n                            ops.push_back({w, px, py, bx, by, cx, cy, ex, ey});\n                        }\n                    }\n                }\n            }\n            // 45-degree\n            for (int a = 1;; a++) {\n                int p2x = px + a, p2y = py + a;\n                int p3x = px, p3y = py + 2 * a;\n                int p4x = px - a, p4y = py + a;\n                if (p2x >= N || p2y >= N || p3y >= N || p4x < 0 || p4y >= N) break;\n                if (has_dot[p2x][p2y] && has_dot[p3x][p3y] && has_dot[p4x][p4y]) {\n                    bool ok = true;\n                    // p1 to p2\n                    for (int i = 0; i < a; i++) if (diag1[px + i][py + i]) ok = false;\n                    // p2 to p3\n                    for (int i = 0; i < a; i++) if (diag3[p2x - i][p2y + i]) ok = false;\n                    // p3 to p4\n                    for (int i = 0; i < a; i++) if (diag4[p3x - i][p3y - i]) ok = false;\n                    // p4 to p1\n                    for (int i = 0; i < a; i++) if (diag2[p4x + i][p4y - i]) ok = false;\n                    if (ok) {\n                        ops.push_back({w, px, py, p2x, p2y, p3x, p3y, p4x, p4y});\n                    }\n                }\n            }\n        }\n    }\n    sort(ops.begin(), ops.end(), cmp);\n    vector<Operation> final_ops;\n    for (auto& op : ops) {\n        int x1 = op.x1, y1 = op.y1;\n        if (has_dot[x1][y1]) continue;\n        int x2 = op.x2, y2 = op.y2;\n        int x3 = op.x3, y3 = op.y3;\n        int x4 = op.x4, y4 = op.y4;\n        if (!has_dot[x2][y2] || !has_dot[x3][y3] || !has_dot[x4][y4]) continue;\n        // check edges again, in case\n        bool ok = true;\n        // assume axis or 45, check accordingly\n        if (x2 == x1 + (x3 - x1) && y2 == y1 + (y3 - y1)) { // axis\n            int dx = x2 - x1, dy = y3 - y1;\n            for (int i = x1; i < x1 + dx; i++) if (horiz[y1][i][x1]) ok = false;\n            for (int i = x1; i < x1 + dx; i++) if (horiz[y1 + dy][i][x1]) ok = false;\n            for (int i = y1; i < y1 + dy; i++) if (vert[x1][i][y1]) ok = false;\n            for (int i = y1; i < y1 + dy; i++) if (vert[x1 + dx][i][y1]) ok = false;\n        } else { // 45\n            int a = x2 - x1;\n            for (int i = 0; i < a; i++) if (diag1[x1 + i][y1 + i]) ok = false;\n            for (int i = 0; i < a; i++) if (diag3[x2 - i][y2 + i]) ok = false;\n            for (int i = 0; i < a; i++) if (diag4[x3 - i][y3 - i]) ok = false;\n            for (int i = 0; i < a; i++) if (diag2[x4 + i][y4 - i]) ok = false;\n        }\n        if (ok) {\n            final_ops.push_back(op);\n            has_dot[x1][y1] = true;\n            // set edges\n            if (x2 == x1 + (x3 - x1) && y2 == y1 + (y3 - y1)) {\n                int dx = x2 - x1, dy = y3 - y1;\n                for (int i = x1; i < x1 + dx; i++) horiz[y1][i][x1] = true;\n                for (int i = x1; i < x1 + dx; i++) horiz[y1 + dy][i][x1] = true;\n                for (int i = y1; i < y1 + dy; i++) vert[x1][i][y1] = true;\n                for (int i = y1; i < y1 + dy; i++) vert[x1 + dx][i][y1] = true;\n            } else {\n                int a = x2 - x1;\n                for (int i = 0; i < a; i++) diag1[x1 + i][y1 + i] = true;\n                for (int i = 0; i < a; i++) diag3[x2 - i][y2 + i] = true;\n                for (int i = 0; i < a; i++) diag4[x3 - i][y3 - i] = true;\n                for (int i = 0; i < a; i++) diag2[x4 + i][y4 - i] = true;\n            }\n        }\n    }\n    cout << final_ops.size() << endl;\n    for (auto& op : final_ops) {\n        cout << op.x1 << \" \" << op.y1 << \" \" << op.x2 << \" \" << op.y2 << \" \" << op.x3 << \" \" << op.y3 << \" \" << op.x4 << \" \" << op.y4 << endl;\n    }\n    return 0;\n}","ahc015":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 10;\nvector<vector<int>> grid(N, vector<int>(N, 0));\n\n// Directions: F (down), B (up), L (left), R (right)\nstring dirs = \"FBLR\";\n\n// Simulate tilt in direction dir\nvoid simulate_tilt(vector<vector<int>>& g, char dir) {\n    if (dir == 'R') { // Right\n        for (int i = 0; i < N; ++i) {\n            vector<int> new_row(N, 0);\n            int pos = N - 1;\n            for (int j = N - 1; j >= 0; --j) {\n                if (g[i][j] != 0) {\n                    new_row[pos] = g[i][j];\n                    --pos;\n                }\n            }\n            g[i] = new_row;\n        }\n    } else if (dir == 'L') { // Left\n        for (int i = 0; i < N; ++i) {\n            vector<int> new_row(N, 0);\n            int pos = 0;\n            for (int j = 0; j < N; ++j) {\n                if (g[i][j] != 0) {\n                    new_row[pos] = g[i][j];\n                    ++pos;\n                }\n            }\n            g[i] = new_row;\n        }\n    } else if (dir == 'F') { // Forward (down)\n        for (int j = 0; j < N; ++j) {\n            vector<int> new_col(N, 0);\n            int pos = N - 1;\n            for (int i = N - 1; i >= 0; --i) {\n                if (g[i][j] != 0) {\n                    new_col[pos] = g[i][j];\n                    --pos;\n                }\n            }\n            for (int i = 0; i < N; ++i) g[i][j] = new_col[i];\n        }\n    } else if (dir == 'B') { // Backward (up)\n        for (int j = 0; j < N; ++j) {\n            vector<int> new_col(N, 0);\n            int pos = 0;\n            for (int i = 0; i < N; ++i) {\n                if (g[i][j] != 0) {\n                    new_col[pos] = g[i][j];\n                    ++pos;\n                }\n            }\n            for (int i = 0; i < N; ++i) g[i][j] = new_col[i];\n        }\n    }\n}\n\n// DFS to compute component size\nint dfs(int x, int y, int fl, vector<vector<int>>& g, vector<vector<bool>>& vis) {\n    vis[x][y] = true;\n    int cnt = 1;\n    int dx[4] = {-1, 0, 1, 0};\n    int dy[4] = {0, 1, 0, -1};\n    for (int d = 0; d < 4; ++d) {\n        int nx = x + dx[d], ny = y + dy[d];\n        if (nx >= 0 && nx < N && ny >= 0 && ny < N && g[nx][ny] == fl && !vis[nx][ny]) {\n            cnt += dfs(nx, ny, fl, g, vis);\n        }\n    }\n    return cnt;\n}\n\n// Compute score for the grid\nlong long compute_score(const vector<vector<int>>& g) {\n    vector<int> component_sizes;\n    for (int fl = 1; fl <= 3; ++fl) {\n        vector<vector<bool>> visited(N, vector<bool>(N, false));\n        for (int i = 0; i < N; ++i) {\n            for (int j = 0; j < N; ++j) {\n                if (g[i][j] == fl && !visited[i][j]) {\n                    int size = dfs(i, j, fl, const_cast<vector<vector<int>>&>(g), visited);\n                    component_sizes.push_back(size);\n                }\n            }\n        }\n    }\n    long long sum_sq = 0;\n    for (int s : component_sizes) sum_sq += 1LL * s * s;\n    vector<long long> d(4, 0);\n    for (int i = 0; i < N; ++i) {\n        for (int j = 0; j < N; ++j) {\n            if (g[i][j]) d[g[i][j]]++;\n        }\n    }\n    long long sum_d_sq = 0;\n    for (int i = 1; i <= 3; ++i) sum_d_sq += d[i] * d[i];\n    if (sum_d_sq == 0) return 0; // Avoid division by zero\n    double score = 1e6 * sum_sq / (double)sum_d_sq;\n    return round(score);\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    vector<int> f(100);\n    for (int i = 0; i < 100; ++i) cin >> f[i];\n    \n    vector<pair<int, int>> empty;\n    for (int i = 0; i < N; ++i) {\n        for (int j = 0; j < N; ++j) {\n            empty.emplace_back(i, j);\n        }\n    }\n    \n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); // For randomization\n    \n    for (int t = 0; t < 100; ++t) {\n        int p;\n        cin >> p;\n        auto [x, y] = empty[p - 1];\n        grid[x][y] = f[t];\n        empty.erase(empty.begin() + p - 1);\n        \n        // Try all directions and collect candidates\n        long long max_score = -1;\n        vector<char> candidates;\n        for (char dir : dirs) {\n            auto temp_grid = grid;\n            simulate_tilt(temp_grid, dir);\n            long long score = compute_score(temp_grid);\n            if (score > max_score) {\n                max_score = score;\n                candidates = {dir};\n            } else if (score >= max_score * 0.95) { // Within 5% of max\n                candidates.push_back(dir);\n            }\n        }\n        \n        // Choose randomly among candidates\n        char best_dir = candidates[rng() % candidates.size()];\n        \n        // Output\n        cout << best_dir << '\\n';\n        cout.flush();\n        \n        // Simulate tilt on actual grid (skip for last step)\n        if (t < 99) {\n            simulate_tilt(grid, best_dir);\n        }\n    }\n    \n    return 0;\n}","ahc016":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int M;\n    double eps;\n    cin >> M >> eps;\n    int N = 100;\n    cout << N << endl;\n    for (int k = 0; k < M; k++) {\n        string s = \"\";\n        for (int i = 0; i < N; i++) {\n            for (int j = i + 1; j < N; j++) {\n                if (i < k && j < k) {\n                    s += '1';\n                } else {\n                    s += '0';\n                }\n            }\n        }\n        cout << s << endl;\n    }\n    cout << flush;\n    vector<vector<int>> targets(M, vector<int>(N));\n    for (int t = 0; t < M; t++) {\n        for (int i = 0; i < N - t; i++) {\n            targets[t][i] = 0;\n        }\n        for (int i = N - t; i < N; i++) {\n            targets[t][i] = t - 1;\n        }\n    }\n    for (int q = 0; q < 100; q++) {\n        string H;\n        cin >> H;\n        vector<int> deg(N, 0);\n        int idx = 0;\n        for (int i = 0; i < N; i++) {\n            for (int j = i + 1; j < N; j++) {\n                if (H[idx] == '1') {\n                    deg[i]++;\n                    deg[j]++;\n                }\n                idx++;\n            }\n        }\n        sort(deg.begin(), deg.end());\n        int best_t = 0;\n        long long min_diff = LLONG_MAX;\n        for (int t = 0; t < M; t++) {\n            long long diff = 0;\n            for (int i = 0; i < N; i++) {\n                diff += abs(deg[i] - targets[t][i]);\n            }\n            if (diff < min_diff) {\n                min_diff = diff;\n                best_t = t;\n            }\n        }\n        cout << best_t << endl;\n        cout << flush;\n    }\n    return 0;\n}","ahc017":"#include <bits/stdc++.h>\n\nusing namespace std;\n\nint main() {\n  ios::sync_with_stdio(false);\n  cin.tie(0);\n  int N, M, D, K;\n  cin >> N >> M >> D >> K;\n  vector<tuple<int, int, int, int>> edges;\n  for (int i = 0; i < M; i++) {\n    int u, v, w;\n    cin >> u >> v >> w;\n    u--;\n    v--;\n    edges.emplace_back(0, u, v, i);  // Placeholder for key, will update with midx\n  }\n  vector<pair<int, int>> coords(N);\n  for (int i = 0; i < N; i++) {\n    int x, y;\n    cin >> x >> y;\n    coords[i] = {x, y};\n  }\n  // Update edges with midx (x-coordinate of midpoint) as the sorting key\n  for (auto& [key, u, v, idx] : edges) {\n    int midx = (coords[u].first + coords[v].first) / 2;\n    key = midx;\n  }\n  // Sort edges by midx ascending (leftmost edges first)\n  sort(edges.begin(), edges.end());\n  // Compute target sizes for each day\n  int total = M;\n  vector<int> max_size(D, total / D);\n  for (int i = 0; i < total % D; i++) max_size[i]++;\n  // Assign edges\n  vector<int> assignment(M);\n  vector<int> current_size(D, 0);\n  for (auto [midx, u, v, idx] : edges) {\n    // Find the day with the smallest current size that has space\n    int best_day = -1;\n    int min_size = INT_MAX;\n    for (int d = 0; d < D; d++) {\n      if (current_size[d] < max_size[d] && current_size[d] < min_size) {\n        min_size = current_size[d];\n        best_day = d;\n      }\n    }\n    assert(best_day != -1);\n    assignment[idx] = best_day;\n    current_size[best_day]++;\n  }\n  // Output the assignment\n  for (int i = 0; i < M; i++) {\n    if (i > 0) cout << \" \";\n    cout << assignment[i] + 1;\n  }\n  cout << \"\\n\";\n  return 0;\n}","ahc019":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MAX_D = 14;\nint D;\nvector<string> f[2], r[2];\nvector<vector<vector<int>>> grid[2]; // grid[i][x][y][z]\nvector<int> b[2]; // flattened b[i], size D*D*D\n\n// Directions for 6-connectivity (face-adjacent)\nconst int dx[6] = {1, -1, 0, 0, 0, 0};\nconst int dy[6] = {0, 0, 1, -1, 0, 0};\nconst int dz[6] = {0, 0, 0, 0, 1, -1};\n\n// Canonicalize a shape: translate to (0,0,0), try 24 rotations, pick lex smallest\nstring canonicalize(const vector<tuple<int,int,int>>& shape) {\n    if (shape.empty()) return \"\";\n    // Find min coords\n    int minx = INT_MAX, miny = INT_MAX, minz = INT_MAX;\n    for (auto [x,y,z] : shape) {\n        minx = min(minx, x);\n        miny = min(miny, y);\n        minz = min(minz, z);\n    }\n    // Translate\n    vector<tuple<int,int,int>> trans;\n    for (auto [x,y,z] : shape) {\n        trans.emplace_back(x - minx, y - miny, z - minz);\n    }\n    // Generate 24 rotations (90 deg around x,y,z)\n    vector<string> candidates;\n    for (int rx = 0; rx < 4; ++rx) {\n        for (int ry = 0; ry < 4; ++ry) {\n            for (int rz = 0; rz < 4; ++rz) {\n                vector<tuple<int,int,int>> rot;\n                for (auto [x,y,z] : trans) {\n                    // Apply rotations\n                    int nx = x, ny = y, nz = z;\n                    for (int i = 0; i < rx; ++i) { int t = ny; ny = -nz; nz = t; } // Rotate x\n                    for (int i = 0; i < ry; ++i) { int t = nz; nz = -nx; nx = t; } // Rotate y\n                    for (int i = 0; i < rz; ++i) { int t = nx; nx = -ny; ny = t; } // Rotate z\n                    rot.emplace_back(nx, ny, nz);\n                }\n                // Sort and stringify\n                sort(rot.begin(), rot.end());\n                string s;\n                for (auto [x,y,z] : rot) {\n                    s += to_string(x) + \",\" + to_string(y) + \",\" + to_string(z) + \";\";\n                }\n                candidates.push_back(s);\n            }\n        }\n    }\n    return *min_element(candidates.begin(), candidates.end());\n}\n\n// DFS to find connected component\nvoid dfs(int i, int x, int y, int z, vector<tuple<int,int,int>>& component, vector<vector<vector<bool>>>& visited) {\n    visited[x][y][z] = true;\n    component.emplace_back(x, y, z);\n    for (int d = 0; d < 6; ++d) {\n        int nx = x + dx[d], ny = y + dy[d], nz = z + dz[d];\n        if (nx >= 0 && nx < D && ny >= 0 && ny < D && nz >= 0 && nz < D &&\n            grid[i][nx][ny][nz] && !visited[nx][ny][nz]) {\n            dfs(i, nx, ny, nz, component, visited);\n        }\n    }\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    cin >> D;\n    f[0].resize(D); f[1].resize(D);\n    r[0].resize(D); r[1].resize(D);\n    for (int i = 0; i < 2; ++i) {\n        for (int k = 0; k < D; ++k) cin >> f[i][k];\n        for (int k = 0; k < D; ++k) cin >> r[i][k];\n    }\n    \n    // Build grids\n    grid[0].assign(D, vector<vector<int>>(D, vector<int>(D, 0)));\n    grid[1].assign(D, vector<vector<int>>(D, vector<int>(D, 0)));\n    for (int i = 0; i < 2; ++i) {\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 (f[i][z][x] == '1' && r[i][z][y] == '1') {\n                        grid[i][x][y][z] = 1;\n                    }\n                }\n            }\n        }\n    }\n    \n    // Find components for each grid\n    vector<vector<vector<tuple<int,int,int>>>> components(2);\n    for (int i = 0; i < 2; ++i) {\n        vector<vector<vector<bool>>> visited(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                    if (grid[i][x][y][z] && !visited[x][y][z]) {\n                        vector<tuple<int,int,int>> comp;\n                        dfs(i, x, y, z, comp, visited);\n                        components[i].push_back(comp);\n                    }\n                }\n            }\n        }\n    }\n    \n    // Assign block IDs: unique per component, with cross-object sharing\n    map<string, int> shape_to_id;\n    int n = 0;\n    b[0].assign(D * D * D, 0);\n    b[1].assign(D * D * D, 0);\n    \n    // Process i=0: assign new IDs to each component\n    for (const auto& comp : components[0]) {\n        string shape = canonicalize(comp);\n        int id = ++n;\n        shape_to_id[shape] = id;  // Record for potential reuse in i=1\n        for (auto [x,y,z] : comp) {\n            b[0][x * D * D + y * D + z] = id;\n        }\n    }\n    \n    // Process i=1: assign unique IDs, reuse from i=0 if shape matches and ID not used yet in i=1\n    set<int> used_ids_in_1;\n    for (const auto& comp : components[1]) {\n        string shape = canonicalize(comp);\n        int id;\n        if (shape_to_id.find(shape) != shape_to_id.end()) {\n            int potential_id = shape_to_id[shape];\n            if (used_ids_in_1.find(potential_id) == used_ids_in_1.end()) {\n                id = potential_id;  // Reuse\n                used_ids_in_1.insert(id);\n            } else {\n                id = ++n;  // New, since ID already used in i=1\n            }\n        } else {\n            id = ++n;  // New\n        }\n        for (auto [x,y,z] : comp) {\n            b[1][x * D * D + y * D + z] = id;\n        }\n    }\n    \n    // Output\n    cout << n << '\\n';\n    for (int val : b[0]) cout << val << ' ';\n    cout << '\\n';\n    for (int val : b[1]) cout << val << ' ';\n    cout << '\\n';\n    \n    return 0;\n}","ahc020":"#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing ld = long double;\n\nstruct Edge {\n    int u, v, w, idx;\n    bool operator<(const Edge& other) const {\n        return w < other.w;\n    }\n};\n\nint find(vector<int>& parent, int x) {\n    if (parent[x] != x) parent[x] = find(parent, parent[x]);\n    return parent[x];\n}\n\nvoid unite(vector<int>& parent, vector<int>& rank, int x, int y) {\n    int px = find(parent, x), py = find(parent, y);\n    if (px != py) {\n        if (rank[px] < rank[py]) swap(px, py);\n        parent[py] = px;\n        if (rank[px] == rank[py]) rank[px]++;\n    }\n}\n\nld compute_dist(pair<int, int> a, pair<int, int> b) {\n    ld dx = a.first - b.first;\n    ld dy = a.second - b.second;\n    return hypot(dx, dy);\n}\n\nll compute_cost(const vector<int>& P, const vector<int>& B, const vector<Edge>& edges) {\n    ll s = 0;\n    for (int p : P) s += (ll)p * p;\n    for (int i = 0; i < B.size(); ++i) {\n        if (B[i]) s += edges[i].w;\n    }\n    return s;\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    vector<pair<int, int>> stations(N);\n    for (int i = 0; i < N; ++i) {\n        cin >> stations[i].first >> stations[i].second;\n    }\n    vector<Edge> edges(M);\n    for (int i = 0; i < M; ++i) {\n        cin >> edges[i].u >> edges[i].v >> edges[i].w;\n        edges[i].u--; edges[i].v--; // 0-based\n        edges[i].idx = i;\n    }\n    vector<pair<int, int>> residents(K);\n    for (int i = 0; i < K; ++i) {\n        cin >> residents[i].first >> residents[i].second;\n    }\n\n    // Precompute distances from residents to stations\n    vector<vector<ld>> dist(K, vector<ld>(N));\n    for (int k = 0; k < K; ++k) {\n        for (int i = 0; i < N; ++i) {\n            dist[k][i] = compute_dist(residents[k], stations[i]);\n        }\n    }\n\n    // Initial assignment: each resident to closest station\n    vector<int> closest(K);\n    for (int k = 0; k < K; ++k) {\n        ld min_d = 1e18;\n        for (int i = 0; i < N; ++i) {\n            if (dist[k][i] < min_d) {\n                min_d = dist[k][i];\n                closest[k] = i;\n            }\n        }\n    }\n\n    // Active stations: include all assigned + vertex 1\n    set<int> active_stations;\n    for (int k = 0; k < K; ++k) {\n        active_stations.insert(closest[k]);\n    }\n    active_stations.insert(0); // Vertex 1 (0-based)\n\n    // Compute MST on active_stations\n    vector<int> parent(N), rankk(N, 0);\n    for (int i = 0; i < N; ++i) parent[i] = i;\n    sort(edges.begin(), edges.end());\n    vector<int> B(M, 0);\n    for (auto& e : edges) {\n        if (active_stations.count(e.u) && active_stations.count(e.v)) {\n            int pu = find(parent, e.u), pv = find(parent, e.v);\n            if (pu != pv) {\n                unite(parent, rankk, e.u, e.v);\n                B[e.idx] = 1;\n            }\n        }\n    }\n\n    // Set initial P_i\n    vector<int> P(N, 0);\n    vector<vector<ld>> assigned_dists(N);\n    for (int k = 0; k < K; ++k) {\n        int i = closest[k];\n        assigned_dists[i].push_back(dist[k][i]);\n    }\n    for (int i = 0; i < N; ++i) {\n        if (!assigned_dists[i].empty()) {\n            ld max_d = *max_element(assigned_dists[i].begin(), assigned_dists[i].end());\n            P[i] = min(5000, (int)ceil(max_d));\n        }\n    }\n\n    // Local search: try reassigning each resident to next-closest if it reduces cost\n    ll current_cost = compute_cost(P, B, edges);\n    const int max_iters = 1000; // Limit for time\n    for (int iter = 0; iter < max_iters; ++iter) {\n        bool improved = false;\n        for (int k = 0; k < K; ++k) {\n            int current_i = closest[k];\n            ld current_d = dist[k][current_i];\n            // Find next-closest\n            ld next_d = 1e18;\n            int next_i = -1;\n            for (int i = 0; i < N; ++i) {\n                if (i != current_i && dist[k][i] < next_d) {\n                    next_d = dist[k][i];\n                    next_i = i;\n                }\n            }\n            if (next_i == -1 || next_d > 5000) continue; // No better option\n\n            // Simulate reassignment\n            vector<int> new_P = P;\n            vector<int> new_B = B;\n            set<int> new_active = active_stations;\n\n            // Remove from current\n            assigned_dists[current_i].erase(remove(assigned_dists[current_i].begin(), assigned_dists[current_i].end(), current_d), assigned_dists[current_i].end());\n            if (assigned_dists[current_i].empty()) {\n                new_P[current_i] = 0;\n                new_active.erase(current_i);\n            } else {\n                ld max_d = *max_element(assigned_dists[current_i].begin(), assigned_dists[current_i].end());\n                new_P[current_i] = min(5000, (int)ceil(max_d));\n            }\n\n            // Add to next\n            assigned_dists[next_i].push_back(next_d);\n            ld max_d_next = *max_element(assigned_dists[next_i].begin(), assigned_dists[next_i].end());\n            new_P[next_i] = min(5000, (int)ceil(max_d_next));\n            new_active.insert(next_i);\n\n            // Recompute MST if active set changed\n            if (new_active != active_stations) {\n                fill(new_B.begin(), new_B.end(), 0);\n                vector<int> new_parent(N), new_rankk(N, 0);\n                for (int i = 0; i < N; ++i) new_parent[i] = i;\n                for (auto& e : edges) {\n                    if (new_active.count(e.u) && new_active.count(e.v)) {\n                        int pu = find(new_parent, e.u), pv = find(new_parent, e.v);\n                        if (pu != pv) {\n                            unite(new_parent, new_rankk, e.u, e.v);\n                            new_B[e.idx] = 1;\n                        }\n                    }\n                }\n            }\n\n            ll new_cost = compute_cost(new_P, new_B, edges);\n            if (new_cost < current_cost) {\n                // Accept\n                closest[k] = next_i;\n                P = new_P;\n                B = new_B;\n                active_stations = new_active;\n                current_cost = new_cost;\n                improved = true;\n            } else {\n                // Revert\n                assigned_dists[current_i].push_back(current_d);\n                assigned_dists[next_i].pop_back();\n            }\n        }\n        if (!improved) break;\n    }\n\n    // Output\n    for (int i = 0; i < N; ++i) {\n        if (i > 0) cout << \" \";\n        cout << P[i];\n    }\n    cout << \"\\n\";\n    for (int i = 0; i < M; ++i) {\n        if (i > 0) cout << \" \";\n        cout << B[i];\n    }\n    cout << \"\\n\";\n\n    return 0;\n}","ahc021":"#include <bits/stdc++.h>\n\nusing namespace std;\n\nint main() {\n    vector<vector<int>> pyramid(30);\n    for (int x = 0; x < 30; x++) {\n        pyramid[x].resize(x + 1);\n        for (int y = 0; y <= x; y++) {\n            cin >> pyramid[x][y];\n        }\n    }\n\n    vector<tuple<int, int, int, int>> ops;\n\n    // Recursive lambda for sift_down\n    auto sift_down = [&](auto& self, int x, int y) -> void {\n        if (x >= 29) return;\n        int val = pyramid[x][y];\n        int lx = x + 1, ly = y;\n        int rx = x + 1, ry = y + 1;\n        int lval = pyramid[lx][ly];\n        int rval = pyramid[rx][ry];\n        if (lval >= val && rval >= val) return;\n        if (lval < val && lval <= rval) {\n            // Swap with left child\n            swap(pyramid[x][y], pyramid[lx][ly]);\n            ops.push_back({x, y, lx, ly});\n            self(self, lx, ly);\n        } else if (rval < val) {\n            // Swap with right child\n            swap(pyramid[x][y], pyramid[rx][ry]);\n            ops.push_back({x, y, rx, ry});\n            self(self, rx, ry);\n        }\n    };\n\n    // Build the heap by heapifying from bottom to top\n    for (int x = 28; x >= 0; x--) {\n        for (int y = 0; y <= x; y++) {\n            sift_down(sift_down, x, y);\n        }\n    }\n\n    // Output the result\n    cout << ops.size() << endl;\n    for (auto [a, b, c, d] : ops) {\n        cout << a << \" \" << b << \" \" << c << \" \" << d << endl;\n    }\n\n    return 0;\n}","toyota2023summer-final":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int D, N;\n    cin >> D >> N;\n    vector<vector<int>> obs(D, vector<int>(D, 0));\n    for (int k = 0; k < N; ++k) {\n        int ri, rj;\n        cin >> ri >> rj;\n        obs[ri][rj] = 1;\n    }\n    int entrance_i = 0, entrance_j = (D - 1) / 2;\n    obs[entrance_i][entrance_j] = 1; // Block entrance for containers\n\n    // Precompute distances from entrance, avoiding obstacles\n    vector<vector<int>> dist(D, vector<int>(D, -1));\n    queue<pair<int, int>> q;\n    q.push({entrance_i, entrance_j});\n    dist[entrance_i][entrance_j] = 0;\n    vector<pair<int, int>> dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};\n    while (!q.empty()) {\n        auto [i, j] = q.front();\n        q.pop();\n        for (auto [di, dj] : dirs) {\n            int ni = i + di, nj = j + dj;\n            if (ni >= 0 && ni < D && nj >= 0 && nj < D && obs[ni][nj] == 0 && dist[ni][nj] == -1) {\n                dist[ni][nj] = dist[i][j] + 1;\n                q.push({ni, nj});\n            }\n        }\n    }\n\n    int M = D * D - 1 - N; // Number of containers\n    vector<vector<int>> placed(D, vector<int>(D, -1)); // -1: empty, else: container number\n\n    // Placement phase\n    for (int d = 0; d < M; ++d) {\n        int t_d;\n        cin >> t_d;\n        // BFS to find reachable empty squares\n        vector<vector<int>> vis(D, vector<int>(D, 0));\n        queue<pair<int, int>> qq;\n        qq.push({entrance_i, entrance_j});\n        vis[entrance_i][entrance_j] = 1;\n        vector<tuple<int, int, int>> candidates; // dist, i, j\n        while (!qq.empty()) {\n            auto [i, j] = qq.front();\n            qq.pop();\n            for (auto [di, dj] : dirs) {\n                int ni = i + di, nj = j + dj;\n                if (ni >= 0 && ni < D && nj >= 0 && nj < D && obs[ni][nj] == 0 && placed[ni][nj] == -1 && !vis[ni][nj]) {\n                    vis[ni][nj] = 1;\n                    qq.push({ni, nj});\n                    candidates.emplace_back(-dist[ni][nj], ni, nj); // Negative for max-heap effect\n                }\n            }\n        }\n        // Choose the candidate with max dist, then min i, min j\n        sort(candidates.begin(), candidates.end()); // Sorts by -dist, then i, then j\n        auto [_, pi, pj] = candidates[0];\n        placed[pi][pj] = t_d;\n        cout << pi << \" \" << pj << endl;\n    }\n\n    // Removal phase\n    vector<pair<int, int>> order;\n    while (true) {\n        // BFS to find accessible containers\n        vector<vector<int>> vis(D, vector<int>(D, 0));\n        queue<pair<int, int>> qq;\n        qq.push({entrance_i, entrance_j});\n        vis[entrance_i][entrance_j] = 1;\n        vector<pair<int, int>> acc; // {number, id} where id = i*D + j\n        while (!qq.empty()) {\n            auto [i, j] = qq.front();\n            qq.pop();\n            for (auto [di, dj] : dirs) {\n                int ni = i + di, nj = j + dj;\n                if (ni >= 0 && ni < D && nj >= 0 && nj < D && obs[ni][nj] == 0 && placed[ni][nj] == -1 && !vis[ni][nj]) {\n                    vis[ni][nj] = 1;\n                    qq.push({ni, nj});\n                    if (placed[ni][nj] != -1) {\n                        acc.emplace_back(placed[ni][nj], ni * D + nj);\n                    }\n                }\n            }\n        }\n        if (acc.empty()) break;\n        // Remove the smallest number\n        sort(acc.begin(), acc.end());\n        auto [num, id] = acc[0];\n        int ii = id / D, jj = id % D;\n        order.emplace_back(ii, jj);\n        placed[ii][jj] = -1;\n    }\n\n    // Output removal order\n    for (auto [qi, qj] : order) {\n        cout << qi << \" \" << qj << endl;\n    }\n\n    return 0;\n}","ahc024":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int n, m;\n    cin >> n >> m;\n    vector<vector<int>> c(n, vector<int>(n));\n    for(int i = 0; i < n; i++) {\n        for(int j = 0; j < n; j++) {\n            cin >> c[i][j];\n        }\n    }\n    \n    vector<vector<int>> adj(m + 1, vector<int>(m + 1, 0));\n    vector<int> is_boundary(m + 1, 0);\n    vector<int> di = {0, 1, 0, -1};\n    vector<int> dj = {1, 0, -1, 0};\n    \n    // Compute adjacencies and boundary info\n    for(int i = 0; i < n; i++) {\n        for(int j = 0; j < n; j++) {\n            int col = c[i][j];\n            if(i == 0 || i == n - 1 || j == 0 || j == n - 1) {\n                is_boundary[col] = 1;\n                adj[col][0] = adj[0][col] = 1;\n            }\n            for(int d = 0; d < 4; d++) {\n                int ni = i + di[d], nj = j + dj[d];\n                if(ni >= 0 && ni < n && nj >= 0 && nj < n) {\n                    int ncol = c[ni][nj];\n                    if(ncol != col) {\n                        adj[col][ncol] = adj[ncol][col] = 1;\n                    }\n                }\n            }\n        }\n    }\n    \n    // Grid for output\n    vector<vector<int>> grid(n, vector<int>(n, 0));\n    vector<pair<int, int>> pos(m + 1, {-1, -1});\n    vector<bool> used(n * n, false);\n    \n    // Sort wards by degree (number of adjacencies) descending\n    vector<pair<int, int>> order;\n    for(int cc = 1; cc <= m; cc++) {\n        int deg = 0;\n        for(int d = 0; d <= m; d++) {\n            deg += adj[cc][d];\n        }\n        order.emplace_back(-deg, cc);\n    }\n    sort(order.begin(), order.end());\n    \n    // Place wards\n    for(auto& p : order) {\n        int cc = p.second;\n        bool found = false;\n        for(int idx = 0; idx < n * n; idx++) {\n            int i = idx / n, j = idx % n;\n            if(used[idx]) continue;\n            bool on_bound = (i == 0 || i == n - 1 || j == 0 || j == n - 1);\n            if(is_boundary[cc] != on_bound) continue;\n            bool ok = true;\n            for(int d = 1; d <= m; d++) {\n                if(pos[d].first == -1) continue;\n                int pi = pos[d].first, pj = pos[d].second;\n                int dist = abs(i - pi) + abs(j - pj);\n                if(adj[cc][d]) {\n                    if(dist != 1) ok = false;\n                } else {\n                    if(dist == 1) ok = false;\n                }\n            }\n            if(ok) {\n                pos[cc] = {i, j};\n                used[idx] = true;\n                grid[i][j] = cc;\n                found = true;\n                break;\n            }\n        }\n        if(!found) {\n            // Fallback: place at first boundary-matching position\n            for(int idx = 0; idx < n * n; idx++) {\n                int i = idx / n, j = idx % n;\n                if(used[idx]) continue;\n                bool on_bound = (i == 0 || i == n - 1 || j == 0 || j == n - 1);\n                if(is_boundary[cc] == on_bound) {\n                    pos[cc] = {i, j};\n                    used[idx] = true;\n                    grid[i][j] = cc;\n                    break;\n                }\n            }\n        }\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    \n    return 0;\n}","ahc025":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int N, D, Q;\n    cin >> N >> D >> Q;\n    \n    vector<int> items(N);\n    iota(items.begin(), items.end(), 0);  // items[i] = i\n    \n    int query_count = 0;\n    \n    // Comparator for sorting: compares weights of two items using a query\n    auto cmp = [&](int a, int b) -> bool {\n        cout << 1 << \" \" << 1 << \" \" << a << \" \" << b << endl;\n        cout.flush();\n        string res;\n        cin >> res;\n        query_count++;\n        if (res == \"<\") return true;   // a < b\n        if (res == \">\") return false;  // a > b\n        return true;  // a == b, arbitrary\n    };\n    \n    // Sort items by weight (lightest to heaviest)\n    sort(items.begin(), items.end(), cmp);\n    \n    // Assign groups using round-robin on the sorted list\n    vector<int> group(N);\n    vector<vector<int>> groups(D);\n    vector<int> pos(N);\n    for (int i = 0; i < N; i++) {\n        pos[items[i]] = i;  // pos[item] = position in sorted list\n        int g = i % D;\n        group[items[i]] = g;\n        groups[g].push_back(items[i]);\n    }\n    \n    // Refinements: Balance groups using remaining queries\n    int g1 = 0, g2 = 1;\n    while (query_count < Q) {\n        if (groups[g1].empty() || groups[g2].empty()) {\n            // Skip if a group is empty; do dummy query\n            cout << 1 << \" \" << 1 << \" \" << 0 << \" \" << 1 << endl;\n            cout.flush();\n            string dummy;\n            cin >> dummy;\n            query_count++;\n            continue;\n        }\n        \n        // Compare sums of groups g1 and g2\n        cout << groups[g1].size() << \" \" << groups[g2].size();\n        for (int x : groups[g1]) cout << \" \" << x;\n        for (int x : groups[g2]) cout << \" \" << x;\n        cout << endl;\n        cout.flush();\n        string res;\n        cin >> res;\n        query_count++;\n        \n        if (res == \">\") {  // groups[g1] > groups[g2]\n            // Move heaviest item from g1 to g2\n            auto it = max_element(groups[g1].begin(), groups[g1].end(), [&](int a, int b) {\n                return pos[a] < pos[b];\n            });\n            int item = *it;\n            groups[g1].erase(it);\n            groups[g2].push_back(item);\n            group[item] = g2;\n        } else if (res == \"<\") {  // groups[g1] < groups[g2]\n            // Move heaviest item from g2 to g1\n            auto it = max_element(groups[g2].begin(), groups[g2].end(), [&](int a, int b) {\n                return pos[a] < pos[b];\n            });\n            int item = *it;\n            groups[g2].erase(it);\n            groups[g1].push_back(item);\n            group[item] = g1;\n        }  // If ==, do nothing\n        \n        // Cycle to next pair of groups\n        g2 = (g2 + 1) % D;\n        if (g2 == g1) g2 = (g2 + 1) % D;\n    }\n    \n    // Output the group assignment\n    for (int i = 0; i < N; i++) {\n        if (i > 0) cout << \" \";\n        cout << group[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    vector<vector<int>> stacks(m);\n    vector<int> position(n + 1);\n    for (int i = 0; i < m; i++) {\n        stacks[i].resize(n / m);\n        for (int j = 0; j < n / m; j++) {\n            cin >> stacks[i][j];\n            position[stacks[i][j]] = i;\n        }\n    }\n    vector<bool> removed(n + 1, false);\n    \n    for (int v = 1; v <= n; v++) {\n        if (removed[v]) continue;\n        int s = position[v];\n        // Find index of v in stacks[s]\n        int idx = -1;\n        for (int j = 0; j < stacks[s].size(); j++) {\n            if (stacks[s][j] == v) {\n                idx = j;\n                break;\n            }\n        }\n        int h = stacks[s].size() - 1 - idx; // Boxes above v\n        while (h > 0) {\n            // Move the top h boxes: Find the box at stacks[s][size - (h+1)]\n            int move_idx = stacks[s].size() - (h + 1);\n            int box_to_move = stacks[s][move_idx];\n            // Choose destination t: Stack with smallest size, not s\n            int min_size = INT_MAX;\n            int t = -1;\n            for (int i = 0; i < m; i++) {\n                if (i != s && (int)stacks[i].size() < min_size) {\n                    min_size = stacks[i].size();\n                    t = i;\n                }\n            }\n            // Perform move: Extract from move_idx to end\n            vector<int> to_move(stacks[s].begin() + move_idx, stacks[s].end());\n            stacks[s].resize(move_idx);\n            // Append to t\n            stacks[t].insert(stacks[t].end(), to_move.begin(), to_move.end());\n            // Update positions\n            for (int bx : to_move) {\n                position[bx] = t;\n            }\n            // Output move operation: box_to_move and t+1 (1-based)\n            cout << box_to_move << \" \" << (t + 1) << endl;\n            // Update current stack to t, h remains the same\n            s = t;\n        }\n        // Now h == 0, remove v\n        cout << v << \" \" << 0 << endl;\n        removed[v] = true;\n        // Remove from stack\n        stacks[s].pop_back();\n    }\n    return 0;\n}","ahc027":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int N;\n    cin >> N;\n    vector<string> h(N-1);\n    for(int i=0; i<N-1; i++) cin >> h[i];\n    vector<string> v(N);\n    for(int i=0; i<N; i++) cin >> v[i];\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    vector<vector<bool>> visited(N, vector<bool>(N, false));\n    vector<pair<int,int>> DIJ = {{0,1},{1,0},{0,-1},{-1,0}};\n    string DIR = \"RDLU\";\n    \n    function<void(int,int)> dfs = [&](int i, int j) {\n        visited[i][j] = true;\n        vector<tuple<int, int, int, int>> candidates; // d, dir, i2, j2\n        for(int dir=0; dir<4; dir++) {\n            int di = DIJ[dir].first, dj = DIJ[dir].second;\n            int i2 = i + di, j2 = j + dj;\n            if(i2 >= 0 && i2 < N && j2 >= 0 && j2 < N && !visited[i2][j2]) {\n                bool can = false;\n                if(di == 0) { // horizontal\n                    int jj = min(j, j2);\n                    if(v[i][jj] == '0') can = true;\n                } else { // vertical\n                    int ii = min(i, i2);\n                    if(h[ii][j] == '0') can = true;\n                }\n                if(can) {\n                    candidates.emplace_back(d[i2][j2], dir, i2, j2);\n                }\n            }\n        }\n        sort(candidates.rbegin(), candidates.rend()); // sort by d descending\n        for(auto& [_, dir, i2, j2] : candidates) {\n            cout << DIR[dir];\n            dfs(i2, j2);\n            cout << DIR[(dir + 2) % 4];\n        }\n    };\n    \n    dfs(0, 0);\n    cout << endl;\n    return 0;\n}","ahc028":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int N, M;\n    cin >> N >> M;\n    int si, sj;\n    cin >> si >> sj;\n    vector<vector<char>> A(N, vector<char>(N));\n    for (int i = 0; i < N; i++) {\n        string row;\n        cin >> row;\n        for (int j = 0; j < N; j++) {\n            A[i][j] = row[j];\n        }\n    }\n    vector<string> t(M);\n    for (int k = 0; k < M; k++) {\n        cin >> t[k];\n    }\n\n    // Precompute hashes for t_k\n    const unsigned long long BASE = 31;\n    vector<unsigned long long> t_hash(M);\n    for (int k = 0; k < M; k++) {\n        unsigned long long h = 0;\n        for (char c : t[k]) {\n            h = h * BASE + (c - 'A' + 1);\n        }\n        t_hash[k] = h;\n    }\n\n    // Initial state\n    int cur_i = si, cur_j = sj;\n    string S = \"\";\n    vector<bool> covered(M, false);\n    int covered_count = 0;\n    unordered_set<unsigned long long> hashes;\n    vector<pair<int, int>> operations;\n    long long total_cost = 0;\n\n    // Main loop\n    while (operations.size() < 5000 && covered_count < M) {\n        int best_new = -1;\n        long long best_cost = LLONG_MAX;\n        pair<int, int> best_pos = {-1, -1};\n\n        for (int ii = 0; ii < N; ii++) {\n            for (int jj = 0; jj < N; jj++) {\n                long long cost = abs(ii - cur_i) + abs(jj - cur_j) + 1;\n                string temp_S = S + A[ii][jj];\n\n                // Compute new hash if possible\n                unordered_set<unsigned long long> temp_hashes = hashes;\n                if (temp_S.size() >= 5) {\n                    string sub = temp_S.substr(temp_S.size() - 5, 5);\n                    unsigned long long h = 0;\n                    for (char c : sub) {\n                        h = h * BASE + (c - 'A' + 1);\n                    }\n                    temp_hashes.insert(h);\n                }\n\n                // Count new covered\n                int new_cov = 0;\n                for (int k = 0; k < M; k++) {\n                    if (!covered[k] && temp_hashes.count(t_hash[k])) {\n                        new_cov++;\n                    }\n                }\n\n                // Update best\n                if (new_cov > best_new || (new_cov == best_new && cost < best_cost)) {\n                    best_new = new_cov;\n                    best_cost = cost;\n                    best_pos = {ii, jj};\n                }\n            }\n        }\n\n        // If no valid move, break (shouldn't happen)\n        if (best_pos.first == -1) break;\n\n        // Perform the move\n        int ii = best_pos.first, jj = best_pos.second;\n        operations.push_back({ii, jj});\n        total_cost += best_cost;\n        S += A[ii][jj];\n        cur_i = ii;\n        cur_j = jj;\n\n        // Update hashes\n        if (S.size() >= 5) {\n            string sub = S.substr(S.size() - 5, 5);\n            unsigned long long h = 0;\n            for (char c : sub) {\n                h = h * BASE + (c - 'A' + 1);\n            }\n            hashes.insert(h);\n        }\n\n        // Update covered\n        for (int k = 0; k < M; k++) {\n            if (!covered[k] && hashes.count(t_hash[k])) {\n                covered[k] = true;\n                covered_count++;\n            }\n        }\n    }\n\n    // Output\n    for (auto& op : operations) {\n        cout << op.first << \" \" << op.second << endl;\n    }\n\n    return 0;\n}","ahc030":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int N, M;\n    double eps;\n    cin >> N >> M >> eps;\n    for (int k = 0; k < M; ++k) {\n        int d;\n        cin >> d;\n        for (int i = 0; i < d; ++i) {\n            int x, y;\n            cin >> x >> y;\n        }\n    }\n\n    set<pair<int, int>> oil_set;\n    vector<pair<int, int>> assumed_zero;\n    const int THRESHOLD = 10;\n\n    function<void(vector<pair<int, int>>)> process = [&](vector<pair<int, int>> reg) {\n        if (reg.empty()) return;\n        int sz = reg.size();\n        if (sz <= THRESHOLD) {\n            for (auto [i, j] : reg) {\n                cout << \"q 1 \" << i << \" \" << j << endl;\n                cout.flush();\n                int v;\n                cin >> v;\n                if (v > 0) oil_set.insert({i, j});\n            }\n            return;\n        }\n        // divine\n        cout << \"q \" << sz;\n        for (auto [i, j] : reg) cout << \" \" << i << \" \" << j;\n        cout << endl;\n        cout.flush();\n        int s;\n        cin >> s;\n        if (s == 0) {\n            for (auto p : reg) assumed_zero.push_back(p);\n            return;\n        }\n        // subdivide\n        int min_i = INT_MAX, max_i = INT_MIN, min_j = INT_MAX, max_j = INT_MIN;\n        for (auto [i, j] : reg) {\n            min_i = min(min_i, i);\n            max_i = max(max_i, i);\n            min_j = min(min_j, j);\n            max_j = max(max_j, j);\n        }\n        int mid_i = (min_i + max_i) / 2;\n        int mid_j = (min_j + max_j) / 2;\n        vector<pair<int, int>> tl, tr, bl, br;\n        for (auto p : reg) {\n            int i = p.first, j = p.second;\n            if (i <= mid_i) {\n                if (j <= mid_j) tl.push_back(p);\n                else tr.push_back(p);\n            } else {\n                if (j <= mid_j) bl.push_back(p);\n                else br.push_back(p);\n            }\n        }\n        process(tl);\n        process(tr);\n        process(bl);\n        process(br);\n    };\n\n    vector<pair<int, int>> all;\n    for (int i = 0; i < N; ++i) {\n        for (int j = 0; j < N; ++j) {\n            all.push_back({i, j});\n        }\n    }\n    process(all);\n\n    while (true) {\n        cout << \"a \" << oil_set.size();\n        for (auto [i, j] : oil_set) cout << \" \" << i << \" \" << j;\n        cout << endl;\n        cout.flush();\n        int res;\n        cin >> res;\n        if (res == 1) break;\n        // drill the assumed_zero\n        for (auto [i, j] : assumed_zero) {\n            cout << \"q 1 \" << i << \" \" << j << endl;\n            cout.flush();\n            int v;\n            cin >> v;\n            if (v > 0) oil_set.insert({i, j});\n        }\n        assumed_zero.clear();\n    }\n    return 0;\n}","ahc031":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int W, D, N;\n    cin >> W >> D >> N;\n    vector<vector<long long>> a(D, vector<long long>(N));\n    for (int d = 0; d < D; ++d) {\n        for (int k = 0; k < N; ++k) {\n            cin >> a[d][k];\n        }\n    }\n    \n    for (int d = 0; d < D; ++d) {\n        // Sort a[d] in descending order\n        sort(a[d].rbegin(), a[d].rend());\n        for (int k = 0; k < N; ++k) {\n            int i0 = k;\n            int j0 = 0;\n            int i1 = k + 1;\n            int j1 = W;\n            cout << i0 << \" \" << j0 << \" \" << i1 << \" \" << j1 << \"\\n\";\n        }\n    }\n    return 0;\n}","ahc032":"#include <bits/stdc++.h>\n\nusing namespace std;\nusing ll = long long;\nconst ll MOD = 998244353LL;\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<ll>> a(N, vector<ll>(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<ll>>> s(M, vector<vector<ll>>(3, vector<ll>(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 >> s[m][i][j];\n            }\n        }\n    }\n    \n    // Use mt19937 for randomness\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n    \n    // Store the best operations and score\n    vector<tuple<int, int, int>> best_operations;\n    ll best_score = LLONG_MIN;\n    \n    // Number of runs\n    const int NUM_RUNS = 20;\n    \n    for (int run = 0; run < NUM_RUNS; ++run) {\n        // Initialize current_mod for this run\n        vector<vector<ll>> current_mod(N, vector<ll>(N));\n        for (int i = 0; i < N; ++i) {\n            for (int j = 0; j < N; ++j) {\n                current_mod[i][j] = a[i][j] % MOD;\n            }\n        }\n        \n        // Operations for this run\n        vector<tuple<int, int, int>> operations;\n        \n        // Perform up to K operations\n        for (int op = 0; op < K; ++op) {\n            ll best_delta = LLONG_MIN;\n            int best_m = -1, best_p = -1, best_q = -1;\n            \n            // Create and shuffle list of all possible operations\n            vector<tuple<int, int, int>> possible_ops;\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                        possible_ops.emplace_back(m, p, q);\n                    }\n                }\n            }\n            shuffle(possible_ops.begin(), possible_ops.end(), rng);\n            \n            // Evaluate in shuffled order\n            for (auto [m, p, q] : possible_ops) {\n                ll delta = 0;\n                for (int i = 0; i < 3; ++i) {\n                    for (int j = 0; j < 3; ++j) {\n                        ll old_mod = current_mod[p + i][q + j];\n                        ll s_val = s[m][i][j];\n                        ll new_mod = (old_mod + s_val) % MOD;\n                        ll cell_delta = (new_mod - old_mod + MOD) % MOD;\n                        delta += cell_delta;\n                    }\n                }\n                if (delta > best_delta) {\n                    best_delta = delta;\n                    best_m = m;\n                    best_p = p;\n                    best_q = q;\n                }\n            }\n            \n            // If no positive delta, stop\n            if (best_delta <= 0) break;\n            \n            // Apply the best operation\n            operations.emplace_back(best_m, best_p, best_q);\n            for (int i = 0; i < 3; ++i) {\n                for (int j = 0; j < 3; ++j) {\n                    current_mod[best_p + i][best_q + j] = (current_mod[best_p + i][best_q + j] + s[best_m][i][j]) % MOD;\n                }\n            }\n        }\n        \n        // Simple local search: Try removing one operation and check score\n        ll current_score = 0;\n        for (int i = 0; i < N; ++i) {\n            for (int j = 0; j < N; ++j) {\n                current_score += current_mod[i][j];\n            }\n        }\n        vector<tuple<int, int, int>> improved_operations = operations;\n        for (size_t idx = 0; idx < operations.size(); ++idx) {\n            // Temporarily remove operation at idx\n            auto removed_op = operations[idx];\n            vector<tuple<int, int, int>> temp_ops = operations;\n            temp_ops.erase(temp_ops.begin() + idx);\n            \n            // Recompute current_mod from scratch (simple way)\n            vector<vector<ll>> temp_mod(N, vector<ll>(N));\n            for (int i = 0; i < N; ++i) {\n                for (int j = 0; j < N; ++j) {\n                    temp_mod[i][j] = a[i][j] % MOD;\n                }\n            }\n            for (auto [m, p, q] : temp_ops) {\n                for (int i = 0; i < 3; ++i) {\n                    for (int j = 0; j < 3; ++j) {\n                        temp_mod[p + i][q + j] = (temp_mod[p + i][q + j] + s[m][i][j]) % MOD;\n                    }\n                }\n            }\n            ll temp_score = 0;\n            for (int i = 0; i < N; ++i) {\n                for (int j = 0; j < N; ++j) {\n                    temp_score += temp_mod[i][j];\n                }\n            }\n            if (temp_score > current_score) {\n                current_score = temp_score;\n                improved_operations = temp_ops;\n            }\n        }\n        \n        // Update best if better\n        if (current_score > best_score) {\n            best_score = current_score;\n            best_operations = improved_operations;\n        }\n    }\n    \n    // Output the best result\n    cout << best_operations.size() << '\\n';\n    for (auto [m, p, q] : best_operations) {\n        cout << m << ' ' << p << ' ' << q << '\\n';\n    }\n    \n    return 0;\n}","ahc033":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int N;\n    cin >> N;\n    vector<vector<int>> A(N, vector<int>(N));\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cin >> A[i][j];\n        }\n    }\n    \n    // Initialize receiving queues\n    vector<queue<int>> recv_queues(N);\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            recv_queues[i].push(A[i][j]);\n        }\n    }\n    \n    // Grid: -1 no container, else container id\n    vector<vector<int>> grid(N, vector<int>(N, -1));\n    \n    // Crane positions\n    vector<pair<int, int>> crane_pos(N);\n    for (int i = 0; i < N; i++) {\n        crane_pos[i] = {i, 0};\n    }\n    \n    // Crane holdings: -1 none, else id\n    vector<int> crane_holding(N, -1);\n    \n    // Crane active\n    vector<bool> crane_active(N, true);\n    \n    // Dispatched lists\n    vector<vector<int>> dispatched(N);\n    \n    // Actions\n    vector<string> actions(N);\n    \n    // Simulation\n    const int MAX_TURNS = 10000;\n    for (int turn = 0; turn < MAX_TURNS; turn++) {\n        // Step 1: Receive\n        for (int i = 0; i < N; i++) {\n            int x = i, y = 0;\n            if (grid[x][y] == -1 && crane_holding[i] == -1 && !recv_queues[i].empty()) {\n                int cont = recv_queues[i].front();\n                recv_queues[i].pop();\n                grid[x][y] = cont;\n            }\n        }\n        \n        // Step 2: Compute actions\n        vector<char> acts(N, '.');\n        for (int c = 0; c < N; c++) {\n            if (!crane_active[c]) continue;\n            \n            // Compute goal\n            pair<int, int> goal;\n            if (crane_holding[c] != -1) {\n                int cont = crane_holding[c];\n                int target_row = cont / N;\n                if (target_row == c) {\n                    goal = {c, N - 1};\n                } else {\n                    goal = {target_row, 1};  // Buffer column\n                }\n            } else {\n                goal = {c, 0};\n            }\n            \n            // BFS for next move\n            auto get_next_move = [&](int cx, int cy, pair<int, int> g) -> char {\n                vector<vector<int>> dist(N, vector<int>(N, -1));\n                queue<pair<int, int>> q;\n                q.push({cx, cy});\n                dist[cx][cy] = 0;\n                vector<vector<pair<int, int>>> prev(N, vector<pair<int, int>>(N, {-1, -1}));\n                while (!q.empty()) {\n                    auto [x, y] = q.front();\n                    q.pop();\n                    if (x == g.first && y == g.second) break;\n                    vector<pair<int, int>> dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};\n                    for (auto [dx, dy] : dirs) {\n                        int nx = x + dx, ny = y + dy;\n                        if (nx >= 0 && nx < N && ny >= 0 && ny < N) {\n                            if (dist[nx][ny] == -1) {\n                                bool can = true;\n                                if (crane_holding[c] != -1 && c >= 1) {  // Small crane\n                                    if (grid[nx][ny] != -1) can = false;\n                                }\n                                // Check other cranes (simplified: assume no collision in BFS)\n                                for (int o = 0; o < N; o++) {\n                                    if (o != c && crane_pos[o] == make_pair(nx, ny)) can = false;\n                                }\n                                if (can) {\n                                    dist[nx][ny] = dist[x][y] + 1;\n                                    prev[nx][ny] = {x, y};\n                                    q.push({nx, ny});\n                                }\n                            }\n                        }\n                    }\n                }\n                if (dist[g.first][g.second] == -1) return '.';\n                pair<int, int> current = g;\n                while (prev[current.first][current.second] != make_pair(cx, cy)) {\n                    current = prev[current.first][current.second];\n                }\n                int dx = current.first - cx;\n                int dy = current.second - cy;\n                if (dx == -1) return 'U';\n                if (dx == 1) return 'D';\n                if (dy == -1) return 'L';\n                if (dy == 1) return 'R';\n                return '.';\n            };\n            \n            char act = get_next_move(crane_pos[c].first, crane_pos[c].second, goal);\n            \n            // Prefer P/Q if possible\n            int cx = crane_pos[c].first, cy = crane_pos[c].second;\n            if (crane_holding[c] == -1 && grid[cx][cy] != -1) {\n                act = 'P';\n            } else if (crane_holding[c] != -1 && grid[cx][cy] == -1) {\n                act = 'Q';\n            }\n            \n            acts[c] = act;\n        }\n        \n        // Execute actions with validation\n        for (int c = 0; c < N; c++) {\n            char act = acts[c];\n            int cx = crane_pos[c].first, cy = crane_pos[c].second;\n            bool valid = true;\n            if (act == 'P') {\n                if (crane_holding[c] != -1 || grid[cx][cy] == -1) valid = false;\n                else {\n                    crane_holding[c] = grid[cx][cy];\n                    grid[cx][cy] = -1;\n                }\n            } else if (act == 'Q') {\n                if (crane_holding[c] == -1 || grid[cx][cy] != -1) valid = false;\n                else {\n                    grid[cx][cy] = crane_holding[c];\n                    crane_holding[c] = -1;\n                }\n            } else if (act == 'U') {\n                int nx = cx - 1, ny = cy;\n                if (nx < 0) valid = false;\n                else {\n                    if (crane_holding[c] != -1 && c >= 1 && grid[nx][ny] != -1) valid = false;\n                    for (int o = 0; o < N; o++) {\n                        if (o != c && crane_pos[o] == make_pair(nx, ny)) valid = false;\n                    }\n                    if (valid) crane_pos[c] = {nx, ny};\n                }\n            } else if (act == 'D') {\n                int nx = cx + 1, ny = cy;\n                if (nx >= N) valid = false;\n                else {\n                    if (crane_holding[c] != -1 && c >= 1 && grid[nx][ny] != -1) valid = false;\n                    for (int o = 0; o < N; o++) {\n                        if (o != c && crane_pos[o] == make_pair(nx, ny)) valid = false;\n                    }\n                    if (valid) crane_pos[c] = {nx, ny};\n                }\n            } else if (act == 'L') {\n                int nx = cx, ny = cy - 1;\n                if (ny < 0) valid = false;\n                else {\n                    if (crane_holding[c] != -1 && c >= 1 && grid[nx][ny] != -1) valid = false;\n                    for (int o = 0; o < N; o++) {\n                        if (o != c && crane_pos[o] == make_pair(nx, ny)) valid = false;\n                    }\n                    if (valid) crane_pos[c] = {nx, ny};\n                }\n            } else if (act == 'R') {\n                int nx = cx, ny = cy + 1;\n                if (ny >= N) valid = false;\n                else {\n                    if (crane_holding[c] != -1 && c >= 1 && grid[nx][ny] != -1) valid = false;\n                    for (int o = 0; o < N; o++) {\n                        if (o != c && crane_pos[o] == make_pair(nx, ny)) valid = false;\n                    }\n                    if (valid) crane_pos[c] = {nx, ny};\n                }\n            } else if (act == 'B') {\n                if (crane_holding[c] != -1) valid = false;\n                else crane_active[c] = false;\n            }\n            if (!valid) act = '.';\n            actions[c] += act;\n        }\n        \n        // Step 3: Dispatch\n        for (int i = 0; i < N; i++) {\n            int x = i, y = N - 1;\n            if (grid[x][y] != -1) {\n                int cont = grid[x][y];\n                grid[x][y] = -1;\n                dispatched[i].push_back(cont);\n            }\n        }\n        \n        // Check if done\n        bool done = true;\n        for (int i = 0; i < N; i++) {\n            if (!recv_queues[i].empty()) done = false;\n            if (grid[i][N - 1] != -1) done = false;\n            if (crane_holding[i] != -1) done = false;\n        }\n        if (done) break;\n    }\n    \n    // Output: Pad to max length\n    size_t max_len = 0;\n    for (const auto& s : actions) max_len = max(max_len, s.size());\n    for (auto& s : actions) {\n        while (s.size() < max_len) s += '.';\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>> h(N, vector<int>(N));\n    for (int i = 0; i < N; ++i) {\n        for (int j = 0; j < N; ++j) {\n            cin >> h[i][j];\n        }\n    }\n\n    // Directions: U, D, L, R\n    vector<pair<int, int>> dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};\n    string move_str = \"UDLR\";\n\n    // Priority queues: excess (max h), deficit (max -h)\n    priority_queue<tuple<int, int, int>> pq_excess; // {h, i, j}\n    priority_queue<tuple<int, int, int>> pq_deficit; // {-h, i, j}\n    for (int i = 0; i < N; ++i) {\n        for (int j = 0; j < N; ++j) {\n            if (h[i][j] > 0) pq_excess.push({h[i][j], i, j});\n            if (h[i][j] < 0) pq_deficit.push({-h[i][j], i, j});\n        }\n    }\n\n    // Function to get path from (si,sj) to (ti,tj) using BFS\n    auto get_path = [&](int si, int sj, int ti, int tj) -> vector<string> {\n        vector<vector<int>> dist(N, vector<int>(N, -1));\n        vector<vector<pair<int, int>>> prevv(N, vector<pair<int, int>>(N, {-1, -1}));\n        queue<pair<int, int>> q;\n        q.push({si, sj});\n        dist[si][sj] = 0;\n        while (!q.empty()) {\n            auto [i, j] = q.front(); q.pop();\n            if (i == ti && j == tj) break;\n            for (int d = 0; d < 4; ++d) {\n                int ni = i + dirs[d].first, nj = j + dirs[d].second;\n                if (ni >= 0 && ni < N && nj >= 0 && nj < N && dist[ni][nj] == -1) {\n                    dist[ni][nj] = dist[i][j] + 1;\n                    prevv[ni][nj] = {i, j};\n                    q.push({ni, nj});\n                }\n            }\n        }\n        // Reconstruct path\n        vector<string> path;\n        int ci = ti, cj = tj;\n        while (ci != si || cj != sj) {\n            auto [pi, pj] = prevv[ci][cj];\n            for (int d = 0; d < 4; ++d) {\n                if (pi + dirs[d].first == ci && pj + dirs[d].second == cj) {\n                    path.push_back(string(1, move_str[d]));\n                    break;\n                }\n            }\n            ci = pi; cj = pj;\n        }\n        reverse(path.begin(), path.end());\n        return path;\n    };\n\n    // Function to find nearest deficit from (si,sj) and get path\n    auto find_nearest_deficit = [&](int si, int sj) -> pair<pair<int, int>, vector<string>> {\n        vector<vector<int>> dist(N, vector<int>(N, -1));\n        vector<vector<pair<int, int>>> prevv(N, vector<pair<int, int>>(N, {-1, -1}));\n        queue<pair<int, int>> q;\n        q.push({si, sj});\n        dist[si][sj] = 0;\n        pair<int, int> target = {-1, -1};\n        while (!q.empty() && target.first == -1) {\n            auto [x, y] = q.front(); q.pop();\n            if (h[x][y] < 0) {\n                target = {x, y};\n                break;\n            }\n            for (auto& dir : dirs) {\n                int nx = x + dir.first, ny = y + dir.second;\n                if (nx >= 0 && nx < N && ny >= 0 && ny < N && dist[nx][ny] == -1) {\n                    dist[nx][ny] = dist[x][y] + 1;\n                    prevv[nx][ny] = {x, y};\n                    q.push({nx, ny});\n                }\n            }\n        }\n        if (target.first == -1) return {{ -1, -1 }, {}};\n        // Reconstruct path to target\n        vector<string> path;\n        int ci = target.first, cj = target.second;\n        while (ci != si || cj != sj) {\n            auto [pi, pj] = prevv[ci][cj];\n            for (int d = 0; d < 4; ++d) {\n                if (pi + dirs[d].first == ci && pj + dirs[d].second == cj) {\n                    path.push_back(string(1, move_str[d]));\n                    break;\n                }\n            }\n            ci = pi; cj = pj;\n        }\n        reverse(path.begin(), path.end());\n        return {target, path};\n    };\n\n    int ci = 0, cj = 0; // Current position\n    vector<string> operations;\n\n    while (!pq_excess.empty()) {\n        // Get largest excess\n        auto [val, i, j] = pq_excess.top(); pq_excess.pop();\n        if (h[i][j] <= 0) continue; // Already processed\n\n        // Move to excess cell\n        auto path_to_excess = get_path(ci, cj, i, j);\n        for (auto& s : path_to_excess) operations.push_back(s);\n        ci = i; cj = j;\n\n        // Load all soil\n        int d = h[i][j];\n        operations.push_back(\"+\" + to_string(d));\n        h[i][j] = 0;\n\n        // Find and move to nearest deficits until d is 0\n        while (d > 0) {\n            auto [target, path_to_deficit] = find_nearest_deficit(ci, cj);\n            if (target.first == -1) break; // No deficit (shouldn't happen)\n            int p = target.first, q = target.second;\n\n            // Move to deficit\n            for (auto& s : path_to_deficit) operations.push_back(s);\n            ci = p; cj = q;\n\n            // Unload as much as possible\n            int unload = min(d, -h[p][q]);\n            operations.push_back(\"-\" + to_string(unload));\n            h[p][q] += unload;\n            d -= unload;\n        }\n    }\n\n    // Output all operations\n    for (auto& op : operations) {\n        cout << op << endl;\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    int seed_count = 2 * N * (N - 1);\n    vector<vector<int>> X(seed_count, vector<int>(M));\n    \n    // Read initial seeds\n    for (int i = 0; i < seed_count; i++) {\n        for (int j = 0; j < M; j++) {\n            cin >> X[i][j];\n        }\n    }\n    \n    for (int t = 0; t < T; t++) {\n        // Compute V_k for each seed and prepare for sorting\n        vector<pair<int, int>> seeds;  // Pair: (-V_k, index) for descending sort\n        for (int i = 0; i < seed_count; i++) {\n            int v = 0;\n            for (int j = 0; j < M; j++) {\n                v += X[i][j];\n            }\n            seeds.push_back({-v, i});  // Negative V_k for descending order\n        }\n        \n        // Sort seeds by V_k descending (top 36 will be first)\n        sort(seeds.begin(), seeds.end());\n        \n        // Create planting grid: Assign top 36 seeds in row-major order\n        vector<vector<int>> A(N, vector<int>(N));\n        int idx = 0;\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                A[i][j] = seeds[idx].second;  // Seed index\n                idx++;\n            }\n        }\n        \n        // Output the planting grid\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                cout << A[i][j];\n                if (j < N - 1) cout << \" \";\n                else cout << endl;\n            }\n        }\n        cout.flush();  // Important for interactive problems\n        \n        // Read new seeds after the operation\n        for (int i = 0; i < seed_count; i++) {\n            for (int j = 0; j < M; j++) {\n                cin >> X[i][j];\n            }\n        }\n    }\n    \n    return 0;\n}","ahc038":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int N, M, V;\n    cin >> N >> M >> V;\n    vector<string> s(N), t(N);\n    for(int i = 0; i < N; i++) cin >> s[i];\n    for(int i = 0; i < N; i++) cin >> t[i];\n    \n    vector<pair<int,int>> starts, targets;\n    for(int i = 0; i < N; i++) {\n        for(int j = 0; j < N; j++) {\n            if(s[i][j] == '1') starts.emplace_back(i, j);\n            if(t[i][j] == '1') targets.emplace_back(i, j);\n        }\n    }\n    sort(starts.begin(), starts.end());\n    sort(targets.begin(), targets.end());\n    \n    // Design the tree: simple arm with one fingertip\n    cout << 2 << endl;\n    cout << 0 << \" \" << 1 << endl;\n    cout << 0 << \" \" << 0 << endl;\n    \n    // Directions: 0:right, 1:down, 2:left, 3:up\n    vector<pair<int,int>> dirs = {{0,1}, {1,0}, {0,-1}, {-1,0}};\n    \n    int x = 0, y = 0, dir = 0;\n    \n    for(int i = 0; i < M; i++) {\n        auto [a, b] = starts[i];\n        auto [c, d] = targets[i];\n        \n        // Choose best d for picking\n        int best_dist = 1e9;\n        int best_d = -1, best_rx = -1, best_ry = -1;\n        for(int dd = 0; dd < 4; dd++) {\n            auto [dx, dy] = dirs[dd];\n            int rx = a - dx, ry = b - dy;\n            if(rx >= 0 && rx < N && ry >= 0 && ry < N) {\n                int dist = abs(rx - x) + abs(ry - y);\n                if(dist < best_dist) {\n                    best_dist = dist;\n                    best_d = dd;\n                    best_rx = rx;\n                    best_ry = ry;\n                }\n            }\n        }\n        \n        // Rotate to best_d\n        int diff = (best_d - dir + 4) % 4;\n        string rot;\n        if(diff <= 2) {\n            for(int k = 0; k < diff; k++) rot += 'R';\n        } else {\n            for(int k = 0; k < 4 - diff; k++) rot += 'L';\n        }\n        for(char r : rot) {\n            string S = \".\" + string(1, r) + \".\" + \".\";\n            cout << S << endl;\n            if(r == 'R') dir = (dir + 1) % 4;\n            else dir = (dir - 1 + 4) % 4;\n        }\n        \n        // Move to best_rx, best_ry\n        string moves;\n        int dx = best_rx - x;\n        if(dx > 0) for(int k = 0; k < dx; k++) moves += 'R';\n        else if(dx < 0) for(int k = 0; k < -dx; k++) moves += 'L';\n        int dy = best_ry - y;\n        if(dy > 0) for(int k = 0; k < dy; k++) moves += 'D';\n        else if(dy < 0) for(int k = 0; k < -dy; k++) moves += 'U';\n        for(size_t k = 0; k < moves.size(); k++) {\n            string S = string(1, moves[k]) + \".\" + \".\" + \".\";\n            if(k == moves.size() - 1) S[3] = 'P';\n            cout << S << endl;\n        }\n        x = best_rx;\n        y = best_ry;\n        \n        // Choose best e for placing\n        best_dist = 1e9;\n        int best_e = -1, best_rx2 = -1, best_ry2 = -1;\n        for(int ee = 0; ee < 4; ee++) {\n            auto [ex, ey] = dirs[ee];\n            int rx2 = c - ex, ry2 = d - ey;\n            if(rx2 >= 0 && rx2 < N && ry2 >= 0 && ry2 < N) {\n                int dist = abs(rx2 - x) + abs(ry2 - y);\n                if(dist < best_dist) {\n                    best_dist = dist;\n                    best_e = ee;\n                    best_rx2 = rx2;\n                    best_ry2 = ry2;\n                }\n            }\n        }\n        \n        // Rotate to best_e\n        diff = (best_e - dir + 4) % 4;\n        rot = \"\";\n        if(diff <= 2) {\n            for(int k = 0; k < diff; k++) rot += 'R';\n        } else {\n            for(int k = 0; k < 4 - diff; k++) rot += 'L';\n        }\n        for(char r : rot) {\n            string S = \".\" + string(1, r) + \".\" + \".\";\n            cout << S << endl;\n            if(r == 'R') dir = (dir + 1) % 4;\n            else dir = (dir - 1 + 4) % 4;\n        }\n        \n        // Move to best_rx2, best_ry2\n        moves = \"\";\n        dx = best_rx2 - x;\n        if(dx > 0) for(int k = 0; k < dx; k++) moves += 'R';\n        else if(dx < 0) for(int k = 0; k < -dx; k++) moves += 'L';\n        dy = best_ry2 - y;\n        if(dy > 0) for(int k = 0; k < dy; k++) moves += 'D';\n        else if(dy < 0) for(int k = 0; k < -dy; k++) moves += 'U';\n        for(size_t k = 0; k < moves.size(); k++) {\n            string S = string(1, moves[k]) + \".\" + \".\" + \".\";\n            if(k == moves.size() - 1) S[3] = 'P';\n            cout << S << endl;\n        }\n        x = best_rx2;\n        y = best_ry2;\n    }\n    \n    return 0;\n}","ahc039":"#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n\nstruct Fenwick {\n  vector<ll> tree;\n  int n;\n  Fenwick(int _n) : n(_n), tree(_n + 1, 0) {}\n  void update(int idx, ll val) {\n    while (idx <= n) {\n      tree[idx] += val;\n      idx += idx & -idx;\n    }\n  }\n  ll query(int idx) {\n    ll sum = 0;\n    while (idx > 0) {\n      sum += tree[idx];\n      idx -= idx & -idx;\n    }\n    return sum;\n  }\n};\n\nint main() {\n  ios::sync_with_stdio(false);\n  cin.tie(nullptr);\n\n  int N;\n  cin >> N;\n  vector<tuple<ll, ll, int>> pts;\n  for (int i = 0; i < 2 * N; ++i) {\n    ll x, y;\n    cin >> x >> y;\n    int w = (i < N ? 1 : -1);\n    pts.emplace_back(x, y, w);\n  }\n\n  sort(pts.begin(), pts.end());\n\n  // Assign ranks to y\n  set<ll> yset;\n  for (auto &[x, y, w] : pts) yset.insert(y);\n  vector<ll> ylist(yset.begin(), yset.end());\n  map<ll, int> yrank;\n  for (int i = 0; i < ylist.size(); ++i) {\n    yrank[ylist[i]] = i + 1; // 1-based\n  }\n\n  int max_rank = ylist.size();\n  Fenwick ft(max_rank);\n\n  ll best_score = LLONG_MIN / 2;\n  ll best_left = -1, best_right = -1, best_bottom = -1, best_top = -1;\n\n  ll current_min_x = LLONG_MAX / 2;\n  set<int> active_ranks;\n\n  for (auto &[x, y, w] : pts) {\n    current_min_x = min(current_min_x, x);\n    int r = yrank[y];\n    ft.update(r, w);\n    active_ranks.insert(r);\n\n    // Compute max subarray in active_ranks\n    vector<int> order(active_ranks.begin(), active_ranks.end());\n    ll cum = 0;\n    ll min_p = 0;\n    int min_idx = 0;\n    ll local_max = LLONG_MIN / 2;\n    int best_start = 0, best_end = -1;\n    for (int j = 0; j < order.size(); ++j) {\n      cum = ft.query(order[j]);\n      ll sum_val = cum - min_p;\n      if (sum_val > local_max) {\n        local_max = sum_val;\n        best_start = min_idx;\n        best_end = j;\n      }\n      if (cum < min_p) {\n        min_p = cum;\n        min_idx = j + 1;\n      }\n    }\n\n    if (current_min_x < x && ylist[order[best_start] - 1] < ylist[order[best_end] - 1] && local_max > best_score) {\n      best_score = local_max;\n      best_left = current_min_x;\n      best_right = x;\n      best_bottom = ylist[order[best_start] - 1];\n      best_top = ylist[order[best_end] - 1];\n    }\n  }\n\n  // If no valid, small one\n  if (best_score <= LLONG_MIN / 2 + 1) {\n    best_left = 0;\n    best_right = 1;\n    best_bottom = 0;\n    best_top = 1;\n  }\n\n  vector<pair<ll, ll>> poly = {\n    {best_left, best_bottom},\n    {best_left, best_top},\n    {best_right, best_top},\n    {best_right, best_bottom}\n  };\n\n  cout << 4 << '\\n';\n  for (auto &[x, y] : poly) {\n    cout << x << ' ' << y << '\\n';\n  }\n\n  return 0;\n}","ahc040":"#include <bits/stdc++.h>\n\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int N, T, sigma;\n    cin >> N >> T >> sigma;\n    vector<long long> w(N), h(N);\n    for (int i = 0; i < N; ++i) {\n        cin >> w[i] >> h[i];\n    }\n    \n    // Define modes: 0=alternate U/L with smart r/b, 1-4=fixed for baselines\n    vector<int> modes = {0, 1, 2, 3, 4};\n    \n    for (int t = 0; t < T; ++t) {\n        int mode = modes[t % modes.size()];\n        vector<tuple<int, int, char, int>> prdb;\n        \n        for (int i = 0; i < N; ++i) {\n            int p = i;\n            int r;\n            char d;\n            int b;\n            \n            if (mode == 0) {\n                // Alternate U/L, b=previous, r optimized for direction\n                d = (i % 2 == 0) ? 'U' : 'L';\n                b = (i == 0) ? -1 : i - 1;\n                if (d == 'U') {\n                    r = (h[i] > w[i]) ? 1 : 0;  // Rotate if taller to prioritize width\n                } else {\n                    r = (w[i] > h[i]) ? 1 : 0;  // Rotate if wider to prioritize height\n                }\n            } else if (mode == 1) {\n                d = 'U';\n                b = -1;\n                r = 0;\n            } else if (mode == 2) {\n                d = 'L';\n                b = -1;\n                r = 0;\n            } else if (mode == 3) {\n                d = 'U';\n                b = -1;\n                r = 1;\n            } else if (mode == 4) {\n                d = 'L';\n                b = -1;\n                r = 1;\n            }\n            \n            prdb.emplace_back(p, r, d, b);\n        }\n        \n        cout << N << '\\n';\n        for (auto [p, r, d, b] : prdb) {\n            cout << p << ' ' << r << ' ' << d << ' ' << b << '\\n';\n        }\n        cout << flush;\n        \n        long long Wp, Hp;\n        cin >> Wp >> Hp;\n        // Future: Use Wp, Hp to adapt modes (e.g., if Wp > Hp, favor 'L')\n    }\n    \n    return 0;\n}","ahc041":"#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n\npair<ll, int> compute_score_and_max_height(int N, const vector<int>& A, const vector<int>& p, const vector<vector<int>>& adj, int H) {\n    vector<int> depth(N, -1);\n    ll score = 0;\n    int max_h = 0;\n    bool has_cycle = false;\n    for (int i = 0; i < N; ++i) {\n        if (depth[i] != -1) continue;\n        queue<int> q;\n        q.push(i);\n        depth[i] = 0;\n        vector<bool> visited(N, false);\n        visited[i] = true;\n        while (!q.empty()) {\n            int u = q.front(); q.pop();\n            score += (ll)(depth[u] + 1) * A[u];\n            max_h = max(max_h, depth[u]);\n            for (int v : adj[u]) {\n                if (v == p[u]) continue;\n                if (visited[v]) {\n                    has_cycle = true;\n                    break;\n                }\n                if (depth[v] == -1) {\n                    depth[v] = depth[u] + 1;\n                    visited[v] = true;\n                    q.push(v);\n                }\n            }\n            if (has_cycle) break;\n        }\n        if (has_cycle) break;\n    }\n    if (has_cycle || max_h > H) return {-1, -1};\n    return {score, max_h};\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(0);\n    int N, M, H;\n    cin >> N >> M >> H;\n    vector<int> A(N);\n    for (int i = 0; i < N; ++i) {\n        cin >> A[i];\n    }\n    vector<vector<int>> adj(N);\n    for (int i = 0; i < M; ++i) {\n        int u, v;\n        cin >> u >> v;\n        adj[u].push_back(v);\n        adj[v].push_back(u);\n    }\n    vector<pair<int, int>> points(N);\n    for (int i = 0; i < N; ++i) {\n        cin >> points[i].first >> points[i].second;\n    }\n    vector<vector<double>> dist(N, vector<double>(N));\n    for (int i = 0; i < N; ++i) {\n        for (int j = 0; j < N; ++j) {\n            double dx = points[i].first - points[j].first;\n            double dy = points[i].second - points[j].second;\n            dist[i][j] = sqrt(dx * dx + dy * dy);\n        }\n    }\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n    vector<pair<int, int>> verts;\n    for (int i = 0; i < N; ++i) {\n        verts.push_back({-A[i], i});\n    }\n    shuffle(verts.begin(), verts.end(), rng); // Randomize initial order\n    vector<int> d(N, -1);\n    vector<int> p(N, -1);\n    for (auto [_, v] : verts) {\n        if (d[v] != -1) continue;\n        int best_u = -1;\n        double min_dist = 1e9;\n        int max_du = -1;\n        for (int u : adj[v]) {\n            if (d[u] != -1 && d[u] < H) {\n                if (d[u] > max_du || (d[u] == max_du && dist[v][u] < min_dist)) {\n                    max_du = d[u];\n                    min_dist = dist[v][u];\n                    best_u = u;\n                }\n            }\n        }\n        if (best_u != -1) {\n            p[v] = best_u;\n            d[v] = max_du + 1;\n        } else {\n            p[v] = -1;\n            d[v] = 0;\n        }\n    }\n    auto [current_score, _] = compute_score_and_max_height(N, A, p, adj, H);\n    const int MAX_ITER = 10000;\n    double T = 1000000.0;\n    double alpha = 0.9999;\n    for (int iter = 0; iter < MAX_ITER; ++iter) {\n        int v = rng() % N;\n        vector<int> candidates = adj[v];\n        candidates.push_back(-1);\n        int new_parent = candidates[rng() % candidates.size()];\n        if (new_parent == p[v]) continue;\n        int old_p = p[v];\n        p[v] = new_parent;\n        auto [new_score, new_max_h] = compute_score_and_max_height(N, A, p, adj, H);\n        if (new_score != -1) {\n            double delta = new_score - current_score;\n            if (delta > 0 || exp(delta / T) > uniform_real_distribution<double>(0, 1)(rng)) {\n                current_score = new_score;\n            } else {\n                p[v] = old_p;\n            }\n        } else {\n            p[v] = old_p;\n        }\n        T *= alpha;\n    }\n    for (int i = 0; i < N; ++i) {\n        if (i > 0) cout << \" \";\n        cout << p[i];\n    }\n    cout << \"\\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> board(N);\n    for (int i = 0; i < N; i++) {\n        cin >> board[i];\n    }\n    vector<pair<int, int>> oni;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (board[i][j] == 'x') {\n                oni.emplace_back(i, j);\n            }\n        }\n    }\n    // Maps for batching: direction -> index -> max distance\n    map<pair<char, int>, int> max_dist;\n    for (auto [i, j] : oni) {\n        // Check directions and choose best\n        bool can_up = true;\n        for (int k = 0; k < i; k++) {\n            if (board[k][j] == 'o') can_up = false;\n        }\n        int cost_up = can_up ? 2 * (i + 1) : 1e9;\n\n        bool can_down = true;\n        for (int k = i + 1; k < N; k++) {\n            if (board[k][j] == 'o') can_down = false;\n        }\n        int cost_down = can_down ? 2 * (N - i) : 1e9;\n\n        bool can_left = true;\n        for (int k = 0; k < j; k++) {\n            if (board[i][k] == 'o') can_left = false;\n        }\n        int cost_left = can_left ? 2 * (j + 1) : 1e9;\n\n        bool can_right = true;\n        for (int k = j + 1; k < N; k++) {\n            if (board[i][k] == 'o') can_right = false;\n        }\n        int cost_right = can_right ? 2 * (N - j) : 1e9;\n\n        // Find best direction\n        vector<tuple<int, char, int, int>> candidates; // cost, dir, idx, dist\n        if (can_up) candidates.emplace_back(cost_up, 'U', j, i + 1);\n        if (can_down) candidates.emplace_back(cost_down, 'D', j, N - i);\n        if (can_left) candidates.emplace_back(cost_left, 'L', i, j + 1);\n        if (can_right) candidates.emplace_back(cost_right, 'R', i, N - j);\n        sort(candidates.begin(), candidates.end());\n        auto [cost, dir, idx, dist] = candidates[0];\n\n        // Update max_dist\n        pair<char, int> key = {dir, idx};\n        max_dist[key] = max(max_dist[key], dist);\n    }\n\n    // Generate batched operations\n    vector<pair<char, int>> all_ops;\n    for (auto& [key, dist] : max_dist) {\n        char dir = key.first;\n        int idx = key.second;\n        if (dir == 'U') {\n            for (int t = 0; t < dist; t++) all_ops.emplace_back('U', idx);\n            for (int t = 0; t < dist; t++) all_ops.emplace_back('D', idx);\n        } else if (dir == 'D') {\n            for (int t = 0; t < dist; t++) all_ops.emplace_back('D', idx);\n            for (int t = 0; t < dist; t++) all_ops.emplace_back('U', idx);\n        } else if (dir == 'L') {\n            for (int t = 0; t < dist; t++) all_ops.emplace_back('L', idx);\n            for (int t = 0; t < dist; t++) all_ops.emplace_back('R', idx);\n        } else if (dir == 'R') {\n            for (int t = 0; t < dist; t++) all_ops.emplace_back('R', idx);\n            for (int t = 0; t < dist; t++) all_ops.emplace_back('L', idx);\n        }\n    }\n\n    // Output\n    for (auto [d, p] : all_ops) {\n        cout << d << \" \" << p << \"\\n\";\n    }\n    return 0;\n}","ahc044":"#include <bits/stdc++.h>\n#include <random>\n#include <chrono>\n\nusing namespace std;\n\nint main() {\n    const int N = 100;\n    const int L = 500000;\n    vector<long long> T(N);\n    for (auto &x : T) cin >> x;\n\n    // Random number generator\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n    uniform_real_distribution<double> urd(0.0, 1.0);\n\n    // Initial random assignment\n    vector<int> a(N), b(N);\n    for (int i = 0; i < N; i++) {\n        a[i] = rng() % N;\n        b[i] = rng() % N;\n    }\n\n    // Simulation function\n    auto simulate = [&](const vector<int> &aa, const vector<int> &bb) -> long long {\n        vector<long long> cnt(N, 0);\n        int cur = 0;\n        cnt[0] = 1;\n        for (int w = 1; w < L; w++) {\n            int t = cnt[cur];\n            int nxt = (t % 2 == 1) ? aa[cur] : bb[cur];\n            cur = nxt;\n            cnt[cur]++;\n        }\n        long long e = 0;\n        for (int i = 0; i < N; i++) e += abs(cnt[i] - T[i]);\n        return e;\n    };\n\n    // Initial error\n    long long currentE = simulate(a, b);\n\n    // Simulated Annealing parameters\n    double temp = 1000.0;\n    const int iterations = 150;  // Adjust based on time; ~150 fits within 2s\n    const double cooling_rate = 0.99;\n\n    for (int it = 0; it < iterations; it++) {\n        // Perturb: choose random i, which (0 for a, 1 for b), and new value\n        int i = rng() % N;\n        int which = rng() % 2;\n        int old_val = (which == 0) ? a[i] : b[i];\n        int new_val = rng() % N;\n\n        // Apply change\n        if (which == 0) a[i] = new_val;\n        else b[i] = new_val;\n\n        // Compute new error\n        long long newE = simulate(a, b);\n\n        // Acceptance\n        if (newE <= currentE || urd(rng) < exp((currentE - newE) / temp)) {\n            currentE = newE;\n        } else {\n            // Revert\n            if (which == 0) a[i] = old_val;\n            else b[i] = old_val;\n        }\n\n        // Cool down\n        temp *= cooling_rate;\n    }\n\n    // Output the best assignment\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\nusing ll = long long;\nusing ld = long double;\n\n// Union-Find for Kruskal\nstruct UF {\n    vector<int> par;\n    UF(int n) : par(n) { iota(par.begin(), par.end(), 0); }\n    int find(int x) { return par[x] == x ? x : par[x] = find(par[x]); }\n    bool unite(int x, int y) {\n        x = find(x); y = find(y);\n        if (x == y) return false;\n        par[x] = y;\n        return true;\n    }\n};\n\n// Compute floored Euclidean distance\nll dist(ld x1, ld y1, ld x2, ld y2) {\n    ld dx = x1 - x2, dy = y1 - y2;\n    return floorl(sqrtl(dx * dx + dy * dy));\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, Q, L, W;\n    cin >> N >> M >> Q >> L >> W;\n    vector<int> G(M);\n    for (int i = 0; i < M; ++i) cin >> G[i];\n    vector<ld> 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    // Compute approximate centers\n    vector<ld> cx(N), cy(N);\n    for (int i = 0; i < N; ++i) {\n        cx[i] = (lx[i] + rx[i]) / 2.0L;\n        cy[i] = (ly[i] + ry[i]) / 2.0L;\n    }\n\n    // Sort cities by cx, then cy\n    vector<int> cities(N);\n    iota(cities.begin(), cities.end(), 0);\n    sort(cities.begin(), cities.end(), [&](int a, int b) {\n        if (cx[a] != cx[b]) return cx[a] < cx[b];\n        return cy[a] < cy[b];\n    });\n\n    // Assign to groups\n    vector<vector<int>> groups(M);\n    int idx = 0;\n    for (int k = 0; k < M; ++k) {\n        for (int j = 0; j < G[k]; ++j) {\n            groups[k].push_back(cities[idx++]);\n        }\n    }\n\n    // Process queries and build edges\n    vector<vector<pair<int, int>>> edges(M);\n    int queries_used = 0;\n\n    auto query = [&](const vector<int>& c) -> vector<pair<int, int>> {\n        if (queries_used >= Q) return {};\n        cout << \"? \" << c.size();\n        for (int x : c) cout << \" \" << x;\n        cout << \"\\n\" << flush;\n        ++queries_used;\n        vector<pair<int, int>> res;\n        for (int i = 0; i < (int)c.size() - 1; ++i) {\n            int a, b;\n            cin >> a >> b;\n            res.emplace_back(min(a, b), max(a, b));\n        }\n        return res;\n    };\n\n    for (int k = 0; k < M; ++k) {\n        auto& grp = groups[k];\n        int sz = grp.size();\n        if (sz >= 2 && sz <= L) {\n            // Query entire group for true MST (only if valid size)\n            auto res = query(grp);\n            edges[k] = res;\n        } else if (sz > L) {\n            // Approximate MST using centers (exactly sz-1 edges)\n            vector<tuple<ll, int, int>> edge_list;\n            for (int i = 0; i < sz; ++i) {\n                for (int j = i + 1; j < sz; ++j) {\n                    ll d = dist(cx[grp[i]], cy[grp[i]], cx[grp[j]], cy[grp[j]]);\n                    edge_list.emplace_back(d, i, j);\n                }\n            }\n            sort(edge_list.begin(), edge_list.end());\n            UF uf(sz);\n            vector<pair<int, int>> mst_edges;\n            for (auto& [d, u, v] : edge_list) {\n                if (uf.unite(u, v)) {\n                    mst_edges.emplace_back(grp[u], grp[v]);\n                }\n            }\n            edges[k] = mst_edges;\n        }\n        // For sz == 1, edges[k] remains empty (default vector)\n    }\n\n    // Output\n    cout << \"!\\n\";\n    for (int k = 0; k < M; ++k) {\n        for (int x : groups[k]) cout << x << \" \";\n        cout << \"\\n\";\n        for (auto& [a, b] : edges[k]) {\n            cout << a << \" \" << b << \"\\n\";\n        }\n    }\n\n    return 0;\n}","ahc046":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int N, M;\n    cin >> N >> M;\n    vector<pair<int, int>> pos(M);\n    for (int i = 0; i < M; i++) {\n        cin >> pos[i].first >> pos[i].second;\n    }\n    set<int> rows, cols;\n    for (auto [i, j] : pos) {\n        rows.insert(i);\n        cols.insert(j);\n    }\n    vector<pair<int, int>> blocks;\n    for (int r : rows) {\n        int maxj = -1;\n        for (auto [i, j] : pos) {\n            if (i == r) maxj = max(maxj, j);\n        }\n        if (maxj < 19) blocks.emplace_back(r, maxj + 1);\n    }\n    for (int c : cols) {\n        int maxi = -1;\n        for (auto [i, j] : pos) {\n            if (j == c) maxi = max(maxi, i);\n        }\n        if (maxi < 19) blocks.emplace_back(maxi + 1, c);\n    }\n    sort(blocks.begin(), blocks.end());\n    blocks.erase(unique(blocks.begin(), blocks.end()), blocks.end());\n    vector<vector<bool>> blocked(20, vector<bool>(20, false));\n    for (auto [x, y] : blocks) blocked[x][y] = true;\n    map<pair<int, int>, pair<int, int>> block_for;\n    vector<pair<int, int>> to_visit;\n    for (auto b : blocks) {\n        int bx = b.first, by = b.second;\n        pair<int, int> adj;\n        if (bx > 0) adj = {bx - 1, by};\n        else if (bx < 19) adj = {bx + 1, by};\n        else if (by > 0) adj = {bx, by - 1};\n        else adj = {bx, by + 1};\n        to_visit.push_back(adj);\n        block_for[adj] = b;\n    }\n    sort(to_visit.begin(), to_visit.end());\n    to_visit.erase(unique(to_visit.begin(), to_visit.end()), to_visit.end());\n    auto get_path = [&](pair<int, int> s, pair<int, int> t) {\n        vector<vector<int>> dist(20, vector<int>(20, -1));\n        vector<vector<pair<int, int>>> prevv(20, vector<pair<int, int>>(20, {-1, -1}));\n        queue<pair<int, int>> q;\n        q.push(s);\n        dist[s.first][s.second] = 0;\n        int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};\n        while (!q.empty()) {\n            auto [x, y] = q.front();\n            q.pop();\n            for (int d = 0; d < 4; d++) {\n                int nx = x + dx[d], ny = y + dy[d];\n                if (nx >= 0 && nx < 20 && ny >= 0 && ny < 20 && !blocked[nx][ny] && dist[nx][ny] == -1) {\n                    dist[nx][ny] = dist[x][y] + 1;\n                    prevv[nx][ny] = {x, y};\n                    q.push({nx, ny});\n                }\n            }\n        }\n        vector<pair<int, int>> path;\n        pair<int, int> cur = t;\n        while (cur != make_pair(-1, -1)) {\n            path.push_back(cur);\n            cur = prevv[cur.first][cur.second];\n        }\n        reverse(path.begin(), path.end());\n        if (path[0] != s) return make_pair(vector<pair<int, int>>(), vector<int>());\n        vector<int> dirs;\n        for (size_t i = 1; i < path.size(); i++) {\n            int px = path[i - 1].first, py = path[i - 1].second;\n            int cx = path[i].first, cy = path[i].second;\n            if (cx == px + 1) dirs.push_back(1);\n            else if (cx == px - 1) dirs.push_back(0);\n            else if (cy == py + 1) dirs.push_back(3);\n            else if (cy == py - 1) dirs.push_back(2);\n        }\n        return make_pair(path, dirs);\n    };\n    vector<pair<char, char>> actions;\n    pair<int, int> current = pos[0];\n    for (auto tv : to_visit) {\n        auto [path, dirs] = get_path(current, tv);\n        for (int d : dirs) {\n            actions.emplace_back('M', \"UDLR\"[d]);\n        }\n        if (block_for.count(tv)) {\n            auto b = block_for[tv];\n            int ddx = b.first - tv.first, ddy = b.second - tv.second;\n            char dir;\n            if (ddx == 1) dir = 'D';\n            else if (ddx == -1) dir = 'U';\n            else if (ddy == 1) dir = 'R';\n            else dir = 'L';\n            actions.emplace_back('A', dir);\n        }\n        current = tv;\n    }\n    for (int k = 1; k < M; k++) {\n        auto [path, dirs] = get_path(current, pos[k]);\n        vector<pair<char, char>> acts;\n        size_t i = 0;\n        while (i < dirs.size()) {\n            size_t j = i;\n            while (j < dirs.size() && dirs[j] == dirs[i]) j++;\n            if (j - i == 1) {\n                acts.emplace_back('M', \"UDLR\"[dirs[i]]);\n            } else {\n                acts.emplace_back('S', \"UDLR\"[dirs[i]]);\n            }\n            i = j;\n        }\n        for (auto p : acts) actions.push_back(p);\n        current = pos[k];\n    }\n    for (auto [a, d] : actions) {\n        cout << a << \" \" << d << endl;\n    }\n    return 0;\n}"},"8":{"ahc001":"#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n\ndouble calc_p(ll r, ll s) {\n    if (s == 0 || r == 0) return 0.0;\n    double mn = min(r, s);\n    double mx = max(r, s);\n    double ratio = mn / mx;\n    return 1.0 - (1.0 - ratio) * (1.0 - ratio);\n}\n\nbool check_overlap(int i, ll na, ll nb, ll nc, ll nd, const vector<ll>& a, const vector<ll>& b, const vector<ll>& c, const vector<ll>& d, int n) {\n    for (int j = 0; j < n; ++j) {\n        if (i == j) continue;\n        ll ax = max(na, a[j]);\n        ll bx = min(nc, c[j]);\n        ll ay = max(nb, b[j]);\n        ll by = min(nd, d[j]);\n        if (ax < bx && ay < by) return true;\n    }\n    return false;\n}\n\nll find_max_dw(int i, const vector<ll>& a, const vector<ll>& b, const vector<ll>& c, const vector<ll>& d, int n) {\n    ll left = 0, right = 10000LL - c[i];\n    while (left < right) {\n        ll mid = (left + right + 1) / 2;\n        ll new_c = c[i] + mid;\n        if (check_overlap(i, a[i], b[i], new_c, d[i], a, b, c, d, n)) {\n            right = mid - 1;\n        } else {\n            left = mid;\n        }\n    }\n    return left;\n}\n\nll find_max_dh(int i, const vector<ll>& a, const vector<ll>& b, const vector<ll>& c, const vector<ll>& d, int n) {\n    ll left = 0, right = 10000LL - d[i];\n    while (left < right) {\n        ll mid = (left + right + 1) / 2;\n        ll new_d = d[i] + mid;\n        if (check_overlap(i, a[i], b[i], c[i], new_d, a, b, c, d, n)) {\n            right = mid - 1;\n        } else {\n            left = mid;\n        }\n    }\n    return left;\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int n;\n    cin >> n;\n    vector<ll> x(n), y(n), r(n);\n    for (int i = 0; i < n; ++i) {\n        cin >> x[i] >> y[i] >> r[i];\n    }\n    \n    vector<ll> a(n), b(n), c(n), d(n), s(n);\n    for (int i = 0; i < n; ++i) {\n        a[i] = x[i];\n        c[i] = x[i] + 1;\n        b[i] = y[i];\n        d[i] = y[i] + 1;\n        s[i] = 1;\n    }\n    \n    bool changed = true;\n    while (changed) {\n        changed = false;\n        double max_gain = -1e9;\n        int best_i = -1;\n        int best_dir = -1;  // 0: width, 1: height\n        ll best_delta = 0;\n        \n        for (int i = 0; i < n; ++i) {\n            if (s[i] >= r[i]) continue;\n            \n            ll w = c[i] - a[i];\n            ll h = d[i] - b[i];\n            \n            // Find max dw with binary search\n            ll dw = find_max_dw(i, a, b, c, d, n);\n            \n            // Find max dh with binary search\n            ll dh = find_max_dh(i, a, b, c, d, n);\n            \n            // Check gain for dw\n            if (dw > 0) {\n                ll new_s = s[i] + dw * h;\n                double old_p = calc_p(r[i], s[i]);\n                double new_p = calc_p(r[i], new_s);\n                double gain = new_p - old_p;\n                if (gain > max_gain + 1e-9) {  // Small epsilon for floating-point\n                    max_gain = gain;\n                    best_i = i;\n                    best_dir = 0;\n                    best_delta = dw;\n                }\n            }\n            \n            // Check gain for dh\n            if (dh > 0) {\n                ll new_s = s[i] + dh * w;\n                double old_p = calc_p(r[i], s[i]);\n                double new_p = calc_p(r[i], new_s);\n                double gain = new_p - old_p;\n                if (gain > max_gain + 1e-9) {\n                    max_gain = gain;\n                    best_i = i;\n                    best_dir = 1;\n                    best_delta = dh;\n                }\n            }\n        }\n        \n        if (best_i != -1) {\n            changed = true;\n            if (best_dir == 0) {  // Expand width\n                c[best_i] += best_delta;\n                s[best_i] += best_delta * (d[best_i] - b[best_i]);\n            } else {  // Expand height\n                d[best_i] += best_delta;\n                s[best_i] += best_delta * (c[best_i] - a[best_i]);\n            }\n        }\n    }\n    \n    // Output\n    for (int i = 0; i < n; ++i) {\n        cout << a[i] << \" \" << b[i] << \" \" << c[i] << \" \" << d[i] << \"\\n\";\n    }\n    \n    return 0;\n}","ahc002":"#include <bits/stdc++.h>\n\nusing namespace std;\n\nstruct State {\n    int tile;\n    pair<int, int> sq;\n    set<int> visited;\n    vector<pair<int, int>> path_squares;\n    int score;\n    bool operator<(const State& other) const {\n        return score < other.score;  // Max-heap\n    }\n};\n\nint main() {\n    int si, sj;\n    cin >> si >> sj;\n    vector<vector<int>> t(50, vector<int>(50));\n    for (int i = 0; i < 50; i++) for (int j = 0; j < 50; j++) cin >> t[i][j];\n    vector<vector<int>> p(50, vector<int>(50));\n    for (int i = 0; i < 50; i++) for (int j = 0; j < 50; j++) cin >> p[i][j];\n    \n    int M = 0;\n    for (int i = 0; i < 50; i++) for (int j = 0; j < 50; j++) M = max(M, t[i][j] + 1);\n    \n    vector<vector<int>> tile_squares(M);\n    for (int i = 0; i < 50; i++) for (int j = 0; j < 50; j++) {\n        tile_squares[t[i][j]].push_back(i * 50 + j);\n    }\n    \n    vector<set<int>> adj_tile(M);\n    int di[4] = {-1, 1, 0, 0};\n    int dj[4] = {0, 0, -1, 1};\n    for (int i = 0; i < 50; i++) for (int j = 0; j < 50; j++) {\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 < 50 && nj >= 0 && nj < 50 && t[i][j] != t[ni][nj]) {\n                adj_tile[t[i][j]].insert(t[ni][nj]);\n            }\n        }\n    }\n    \n    // Beam search with width 50, max steps 150\n    const int BEAM_WIDTH = 50;\n    const int MAX_STEPS = 150;\n    priority_queue<State> pq;\n    set<int> initial_visited;\n    initial_visited.insert(t[si][sj]);\n    vector<pair<int, int>> initial_path = {{si, sj}};\n    int initial_score = p[si][sj];\n    pq.push({t[si][sj], {si, sj}, initial_visited, initial_path, initial_score});\n    \n    State best_state = pq.top();\n    int steps = 0;\n    while (!pq.empty() && steps < MAX_STEPS) {\n        vector<State> current_beam;\n        for (int i = 0; i < BEAM_WIDTH && !pq.empty(); i++) {\n            current_beam.push_back(pq.top());\n            pq.pop();\n        }\n        for (const auto& state : current_beam) {\n            if (state.score > best_state.score) best_state = state;\n            for (int nei : adj_tile[state.tile]) {\n                if (state.visited.count(nei)) continue;\n                int max_adj_p = -1;\n                pair<int, int> chosen_sq = {-1, -1};\n                for (int sq : tile_squares[nei]) {\n                    int ni = sq / 50;\n                    int nj = sq % 50;\n                    if (abs(ni - state.sq.first) + abs(nj - state.sq.second) == 1) {\n                        if (p[ni][nj] > max_adj_p) {\n                            max_adj_p = p[ni][nj];\n                            chosen_sq = {ni, nj};\n                        }\n                    }\n                }\n                if (chosen_sq.first != -1) {\n                    set<int> new_visited = state.visited;\n                    new_visited.insert(nei);\n                    vector<pair<int, int>> new_path = state.path_squares;\n                    new_path.push_back(chosen_sq);\n                    int new_score = state.score + max_adj_p;\n                    pq.push({nei, chosen_sq, new_visited, new_path, new_score});\n                }\n            }\n        }\n        steps++;\n    }\n    \n    // Build move string from best_state\n    string moves = \"\";\n    for (size_t i = 1; i < best_state.path_squares.size(); i++) {\n        auto [pi, pj] = best_state.path_squares[i - 1];\n        auto [ci, cj] = best_state.path_squares[i];\n        if (ci == pi - 1) moves += 'U';\n        else if (ci == pi + 1) moves += 'D';\n        else if (cj == pj - 1) moves += 'L';\n        else if (cj == pj + 1) moves += 'R';\n        else assert(false);\n    }\n    cout << moves << endl;\n    return 0;\n}","ahc003":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    const int N = 30;\n    vector<vector<double>> h(N, vector<double>(N-1, 5000.0));  // h[i][j]: cost from (i,j) to (i,j+1)\n    vector<vector<double>> v(N-1, vector<double>(N, 5000.0));  // v[i][j]: cost from (i,j) to (i+1,j)\n\n    for (int query = 0; query < 1000; ++query) {\n        int si, sj, ti, tj;\n        cin >> si >> sj >> ti >> tj;\n\n        // Dijkstra to find shortest path with current estimated costs\n        vector<vector<double>> dist(N, vector<double>(N, 1e18));\n        vector<vector<pair<int, int>>> prev(N, vector<pair<int, int>>(N, {-1, -1}));\n        priority_queue<tuple<double, int, int>, vector<tuple<double, int, int>>, greater<tuple<double, int, int>>> pq;\n\n        dist[si][sj] = 0.0;\n        pq.emplace(0.0, si, sj);\n\n        while (!pq.empty()) {\n            auto [cost, i, j] = pq.top();\n            pq.pop();\n            if (cost > dist[i][j]) continue;\n\n            // Neighbors: up, down, left, right\n            vector<tuple<int, int, double>> neighbors;\n            if (i > 0) neighbors.emplace_back(i-1, j, v[i-1][j]);  // up\n            if (i < N-1) neighbors.emplace_back(i+1, j, v[i][j]);  // down\n            if (j > 0) neighbors.emplace_back(i, j-1, h[i][j-1]);  // left\n            if (j < N-1) neighbors.emplace_back(i, j+1, h[i][j]);  // right\n\n            for (auto [ni, nj, edge_cost] : neighbors) {\n                double new_cost = cost + edge_cost;\n                if (new_cost < dist[ni][nj]) {\n                    dist[ni][nj] = new_cost;\n                    prev[ni][nj] = {i, j};\n                    pq.emplace(new_cost, ni, nj);\n                }\n            }\n        }\n\n        // If target is unreachable (shouldn't happen), output empty path\n        if (dist[ti][tj] == 1e18) {\n            cout << \"\" << endl;\n            cout.flush();\n            int F;\n            cin >> F;  // Still read feedback to continue\n            continue;\n        }\n\n        // Reconstruct path from t to s, building forward moves\n        string path = \"\";\n        int ci = ti, cj = tj;\n        while (ci != si || cj != sj) {\n            auto [pi, pj] = prev[ci][cj];\n            if (pi == -1 || pj == -1) {\n                // Invalid predecessor (shouldn't happen), break to avoid crash\n                path = \"\";\n                break;\n            }\n            // Determine forward move from (pi, pj) to (ci, cj)\n            if (pi == ci - 1) path += 'D';  // i increases: down\n            else if (pi == ci + 1) path += 'U';  // i decreases: up\n            else if (pj == cj - 1) path += 'R';  // j increases: right\n            else if (pj == cj + 1) path += 'L';  // j decreases: left\n            ci = pi;\n            cj = pj;\n        }\n        reverse(path.begin(), path.end());  // Reverse to get from s to t\n\n        // Output path and flush\n        cout << path << endl;\n        cout.flush();\n\n        // Read feedback F\n        int F;\n        cin >> F;\n\n        // Compute estimated path cost C\n        double C = 0.0;\n        ci = si, cj = sj;\n        for (char move : path) {\n            if (move == 'U') {\n                C += v[ci-1][cj];\n                ci--;\n            } else if (move == 'D') {\n                C += v[ci][cj];\n                ci++;\n            } else if (move == 'L') {\n                C += h[ci][cj-1];\n                cj--;\n            } else if (move == 'R') {\n                C += h[ci][cj];\n                cj++;\n            }\n        }\n\n        // Update estimated costs if C > 0\n        if (C > 1e-9) {\n            double factor = static_cast<double>(F) / C;\n            ci = si, cj = sj;\n            for (char move : path) {\n                if (move == 'U') {\n                    v[ci-1][cj] *= factor;\n                    ci--;\n                } else if (move == 'D') {\n                    v[ci][cj] *= factor;\n                    ci++;\n                } else if (move == 'L') {\n                    h[ci][cj-1] *= factor;\n                    cj--;\n                } else if (move == 'R') {\n                    h[ci][cj] *= factor;\n                    cj++;\n                }\n            }\n        }\n    }\n\n    return 0;\n}","ahc004":"#include <bits/stdc++.h>\n\nusing namespace std;\n\nint N;\n\n// Function to compute c: number of strings that are subsequences\nint compute_c(const vector<vector<char>>& grid, const vector<string>& strings) {\n    int covered = 0;\n    for (const auto& s : strings) {\n        bool found = false;\n        // Horizontal check\n        for (int i = 0; i < N && !found; i++) {\n            string row(grid[i].begin(), grid[i].end());\n            row += row;  // Double for wrap-around\n            if (row.find(s) != string::npos) found = true;\n        }\n        // Vertical check\n        for (int j = 0; j < N && !found; j++) {\n            string col;\n            for (int i = 0; i < N; i++) col += grid[i][j];\n            col += col;  // Double for wrap-around\n            if (col.find(s) != string::npos) found = true;\n        }\n        if (found) covered++;\n    }\n    return covered;\n}\n\n// Function to compute d: number of dots\nint compute_d(const vector<vector<char>>& grid) {\n    int dots = 0;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (grid[i][j] == '.') dots++;\n        }\n    }\n    return dots;\n}\n\nint main() {\n    int M;\n    cin >> N >> M;\n    vector<string> strings(M);\n    for (int i = 0; i < M; i++) {\n        cin >> strings[i];\n    }\n\n    // Sort strings by length descending\n    sort(strings.begin(), strings.end(), [](const string& a, const string& b) {\n        return a.size() > b.size();\n    });\n\n    vector<vector<char>> best_grid(N, vector<char>(N, '.'));\n    int best_c = -1;\n    int best_d = INT_MAX;\n\n    const int ATTEMPTS = 20;  // Increased number of attempts\n    const int PERTURBATIONS = 10;  // Increased number of perturbations\n    const int SA_ITERATIONS = 20;  // Number of SA iterations\n    mt19937 rng(42);  // Fixed seed for reproducibility\n\n    for (int attempt = 0; attempt < ATTEMPTS; attempt++) {\n        // Shuffle within length groups\n        vector<string> shuffled_strings = strings;\n        auto it = shuffled_strings.begin();\n        while (it != shuffled_strings.end()) {\n            auto end = find_if(it, shuffled_strings.end(), [it](const string& s) { return s.size() != it->size(); });\n            shuffle(it, end, rng);\n            it = end;\n        }\n\n        vector<vector<char>> grid(N, vector<char>(N, '.'));\n\n        // Greedy placement\n        for (const auto& s : shuffled_strings) {\n            int k = s.size();\n            vector<tuple<int, int, int, bool>> candidates;  // (fill, i, j, horiz)\n            int max_fill = -1;\n\n            // Horizontal placements\n            for (int i = 0; i < N; i++) {\n                for (int j = 0; j < N; j++) {\n                    bool valid = true;\n                    int fill = 0;\n                    for (int p = 0; p < k; p++) {\n                        int cj = (j + p) % N;\n                        char cell = grid[i][cj];\n                        if (cell != '.' && cell != s[p]) {\n                            valid = false;\n                            break;\n                        }\n                        if (cell == '.') fill++;\n                    }\n                    if (valid) {\n                        if (fill > max_fill) {\n                            max_fill = fill;\n                            candidates.clear();\n                        }\n                        if (fill == max_fill) {\n                            candidates.emplace_back(fill, i, j, true);\n                        }\n                    }\n                }\n            }\n\n            // Vertical placements\n            for (int j = 0; j < N; j++) {\n                for (int i = 0; i < N; i++) {\n                    bool valid = true;\n                    int fill = 0;\n                    for (int p = 0; p < k; p++) {\n                        int ci = (i + p) % N;\n                        char cell = grid[ci][j];\n                        if (cell != '.' && cell != s[p]) {\n                            valid = false;\n                            break;\n                        }\n                        if (cell == '.') fill++;\n                    }\n                    if (valid) {\n                        if (fill > max_fill) {\n                            max_fill = fill;\n                            candidates.clear();\n                        }\n                        if (fill == max_fill) {\n                            candidates.emplace_back(fill, i, j, false);\n                        }\n                    }\n                }\n            }\n\n            // Place if candidates exist; randomize if multiple\n            if (!candidates.empty()) {\n                auto& selected = candidates[rng() % candidates.size()];\n                auto [fill, i, j, horiz] = selected;\n                if (horiz) {\n                    for (int p = 0; p < k; p++) {\n                        int cj = (j + p) % N;\n                        if (grid[i][cj] == '.') grid[i][cj] = s[p];\n                    }\n                } else {\n                    for (int p = 0; p < k; p++) {\n                        int ci = (i + p) % N;\n                        if (grid[ci][j] == '.') grid[ci][j] = s[p];\n                    }\n                }\n            }\n        }\n\n        // Post-placement perturbations\n        int current_c = compute_c(grid, strings);\n        for (int pert = 0; pert < PERTURBATIONS; pert++) {\n            int ri = rng() % N, rj = rng() % N;\n            char original = grid[ri][rj];\n            char new_char = (original == '.') ? \"ABCDEFGH\"[rng() % 8] : '.';\n            grid[ri][rj] = new_char;\n            int new_c = compute_c(grid, strings);\n            if (new_c >= current_c) {\n                current_c = new_c;\n            } else {\n                grid[ri][rj] = original;  // Revert\n            }\n        }\n\n        // Simulated Annealing\n        double temperature = 1.0;\n        const double cooling_rate = 0.95;\n        for (int sa = 0; sa < SA_ITERATIONS; sa++) {\n            int ri = rng() % N, rj = rng() % N;\n            char original = grid[ri][rj];\n            char new_char = (original == '.') ? \"ABCDEFGH\"[rng() % 8] : '.';\n            grid[ri][rj] = new_char;\n            int new_c = compute_c(grid, strings);\n            if (new_c > current_c || (rng() % 100) < (int)(100 * exp((new_c - current_c) / temperature))) {\n                current_c = new_c;\n            } else {\n                grid[ri][rj] = original;  // Revert\n            }\n            temperature *= cooling_rate;\n        }\n\n        // Evaluate this attempt\n        int c = compute_c(grid, strings);\n        int d = compute_d(grid);\n        if (c > best_c || (c == best_c && d < best_d)) {\n            best_c = c;\n            best_d = d;\n            best_grid = grid;\n        }\n    }\n\n    // Output the best grid\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cout << best_grid[i][j];\n        }\n        cout << endl;\n    }\n\n    return 0;\n}","ahc005":"#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nconst ll INF = 1LL << 60;\n\nint main() {\n    int N, si, sj;\n    cin >> N >> si >> sj;\n    vector<string> c(N);\n    for(int i = 0; i < N; i++) cin >> c[i];\n    vector<vector<int>> cost(N, vector<int>(N, 0));\n    vector<pair<int, int>> roads;\n    for(int i = 0; i < N; i++) {\n        for(int j = 0; j < N; j++) {\n            if(c[i][j] != '#') {\n                cost[i][j] = c[i][j] - '0';\n                roads.emplace_back(i, j);\n            }\n        }\n    }\n    int R = roads.size();\n    \n    // Identify active rows and columns\n    vector<bool> active_row(N, false), active_col(N, false);\n    for(auto [i, j] : roads) {\n        active_row[i] = true;\n        active_col[j] = true;\n    }\n    \n    // Greedy selection for S\n    vector<bool> covered_row(N, false), covered_col(N, false);\n    vector<pair<int, int>> S;\n    set<pair<int, int>> road_set(roads.begin(), roads.end());\n    while(true) {\n        bool all_covered = true;\n        for(int i = 0; i < N; i++) {\n            if(active_row[i] && !covered_row[i]) all_covered = false;\n            if(active_col[i] && !covered_col[i]) all_covered = false;\n        }\n        if(all_covered) break;\n        \n        pair<int, int> best = {-1, -1};\n        int max_gain = -1;\n        int min_cost = INT_MAX;\n        for(auto p : roads) {\n            int i = p.first, j = p.second;\n            int gain = (active_row[i] && !covered_row[i]) + (active_col[j] && !covered_col[j]);\n            if(gain > max_gain || (gain == max_gain && cost[i][j] < min_cost)) {\n                max_gain = gain;\n                min_cost = cost[i][j];\n                best = p;\n            }\n        }\n        if(best.first == -1) break;\n        S.push_back(best);\n        covered_row[best.first] = true;\n        covered_col[best.second] = true;\n    }\n    // Ensure start is in S\n    bool has_start = false;\n    int start_idx = -1;\n    for(int i = 0; i < S.size(); i++) {\n        if(S[i] == make_pair(si, sj)) {\n            has_start = true;\n            start_idx = i;\n            break;\n        }\n    }\n    if(!has_start) {\n        S.push_back({si, sj});\n        start_idx = S.size() - 1;\n    }\n    \n    // Order S: use the order selected, rotated to start with start\n    vector<pair<int, int>> path;\n    for(int i = start_idx; i < S.size(); i++) path.push_back(S[i]);\n    for(int i = 0; i < start_idx; i++) path.push_back(S[i]);\n    \n    // Simple 2-opt local search on path (excluding start/end)\n    auto compute_cost = [&](const vector<pair<int, int>>& p) -> ll {\n        ll total = 0;\n        for(size_t k = 1; k < p.size(); k++) {\n            // Simplified: use grid distance as proxy for cost\n            auto [x1, y1] = p[k-1];\n            auto [x2, y2] = p[k];\n            total += abs(x1 - x2) + abs(y1 - y2);\n        }\n        return total;\n    };\n    bool improved = true;\n    int iterations = 0;\n    while(improved && iterations < 10) {  // Limit iterations\n        improved = false;\n        ll current_cost = compute_cost(path);\n        for(size_t i = 1; i < path.size() - 1; i++) {\n            for(size_t j = i + 1; j < path.size() - 1; j++) {\n                // Try swapping i and j\n                swap(path[i], path[j]);\n                ll new_cost = compute_cost(path);\n                if(new_cost < current_cost) {\n                    current_cost = new_cost;\n                    improved = true;\n                } else {\n                    swap(path[i], path[j]);  // Revert\n                }\n            }\n        }\n        iterations++;\n    }\n    path.push_back({si, sj});\n    \n    // Function to get path string\n    auto get_path = [&](pair<int, int> start, pair<int, int> end) -> string {\n        if(start == end) return \"\";\n        vector<vector<ll>> dist(N, vector<ll>(N, INF));\n        vector<vector<pair<int, int>>> prevv(N, vector<pair<int, int>>(N, {-1, -1}));\n        priority_queue<tuple<ll, int, int>, vector<tuple<ll, int, int>>, greater<>> pq;\n        dist[start.first][start.second] = 0;\n        pq.push({0, start.first, start.second});\n        while(!pq.empty()) {\n            auto [d, i, j] = pq.top(); pq.pop();\n            if(d > dist[i][j]) continue;\n            for(auto [di, dj] : vector<pair<int, int>>{{0,1}, {0,-1}, {1,0}, {-1,0}}) {\n                int ni = i + di, nj = j + dj;\n                if(ni >= 0 && ni < N && nj >= 0 && nj < N && cost[ni][nj] > 0) {\n                    ll nd = d + cost[ni][nj];\n                    if(nd < dist[ni][nj]) {\n                        dist[ni][nj] = nd;\n                        prevv[ni][nj] = {i, j};\n                        pq.push({nd, ni, nj});\n                    }\n                }\n            }\n        }\n        if(dist[end.first][end.second] == INF) return \"\";\n        vector<pair<int, int>> route;\n        pair<int, int> curr = end;\n        while(curr != make_pair(-1, -1)) {\n            route.push_back(curr);\n            curr = prevv[curr.first][curr.second];\n        }\n        reverse(route.begin(), route.end());\n        string s;\n        for(size_t k = 1; k < route.size(); k++) {\n            auto [i1, j1] = route[k-1];\n            auto [i2, j2] = route[k];\n            if(i2 == i1 + 1) s += 'D';\n            else if(i2 == i1 - 1) s += 'U';\n            else if(j2 == j1 + 1) s += 'R';\n            else if(j2 == j1 - 1) s += 'L';\n        }\n        return s;\n    };\n    \n    // Build full route\n    string ans;\n    for(size_t k = 1; k < path.size(); k++) {\n        ans += get_path(path[k-1], path[k]);\n    }\n    cout << ans << endl;\n    return 0;\n}","future-contest-2022-qual":"#include <bits/stdc++.h>\nusing namespace std;\n\nvoid dfs(int u, const vector<vector<int>>& adj, vector<int>& num_desc, vector<bool>& visited) {\n    visited[u] = true;\n    for (int v : adj[u]) {\n        if (!visited[v]) dfs(v, adj, num_desc, visited);\n        num_desc[u] += 1 + num_desc[v];\n    }\n}\n\nint main() {\n    ios::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 j = 0; j < K; ++j) {\n            cin >> d[i][j];\n        }\n    }\n    \n    vector<vector<int>> deps(N);\n    vector<vector<int>> adj(N);\n    for (int i = 0; i < R; ++i) {\n        int u, v;\n        cin >> u >> v;\n        --u; --v;\n        deps[v].push_back(u);\n        adj[u].push_back(v);\n    }\n    \n    vector<int> num_desc(N, 0);\n    vector<bool> visited(N, false);\n    for (int i = 0; i < N; ++i) {\n        if (!visited[i]) dfs(i, adj, num_desc, visited);\n    }\n    \n    vector<int> task_status(N, -1);\n    vector<int> member_status(M, -1);\n    vector<int> start_day(N, -1);\n    vector<vector<int>> est_s(M, vector<int>(K, 0));\n    \n    int current_day = 0;\n    while (true) {\n        ++current_day;\n        \n        vector<int> free_members;\n        for (int j = 0; j < M; ++j) {\n            if (member_status[j] == -1) free_members.push_back(j);\n        }\n        \n        vector<int> ready_tasks;\n        for (int i = 0; i < N; ++i) {\n            if (task_status[i] == -1) {\n                bool can_start = true;\n                for (int u : deps[i]) {\n                    if (task_status[u] != 1) {\n                        can_start = false;\n                        break;\n                    }\n                }\n                if (can_start) ready_tasks.push_back(i);\n            }\n        }\n        \n        vector<vector<long long>> est_w(ready_tasks.size(), vector<long long>(free_members.size()));\n        for (int ti = 0; ti < ready_tasks.size(); ++ti) {\n            int i = ready_tasks[ti];\n            for (int mj = 0; mj < free_members.size(); ++mj) {\n                int j = free_members[mj];\n                long long w = 0;\n                for (int k = 0; k < K; ++k) {\n                    w += max(0, d[i][k] - est_s[j][k]);\n                }\n                est_w[ti][mj] = w;\n            }\n        }\n        \n        vector<tuple<int, long long, int>> task_order;\n        for (int ti = 0; ti < ready_tasks.size(); ++ti) {\n            long long min_w = LLONG_MAX;\n            for (int mj = 0; mj < free_members.size(); ++mj) {\n                min_w = min(min_w, est_w[ti][mj]);\n            }\n            task_order.emplace_back(-num_desc[ready_tasks[ti]], min_w, ti);\n        }\n        sort(task_order.begin(), task_order.end());\n        \n        vector<pair<int, int>> assignments;\n        set<int> used_members;\n        for (auto& tup : task_order) {\n            int ti = get<2>(tup);\n            int i = ready_tasks[ti];\n            long long best_w = LLONG_MAX;\n            int best_j = -1;\n            for (int mj = 0; mj < free_members.size(); ++mj) {\n                int j = free_members[mj];\n                if (used_members.count(j)) continue;\n                if (est_w[ti][mj] < best_w) {\n                    best_w = est_w[ti][mj];\n                    best_j = j;\n                }\n            }\n            if (best_j != -1) {\n                assignments.emplace_back(best_j, i);\n                used_members.insert(best_j);\n            }\n        }\n        \n        cout << assignments.size();\n        for (auto& p : assignments) {\n            cout << \" \" << (p.first + 1) << \" \" << (p.second + 1);\n            task_status[p.second] = 0;\n            member_status[p.first] = p.second;\n            start_day[p.second] = current_day;\n        }\n        cout << \"\\n\";\n        cout.flush();\n        \n        int n;\n        cin >> n;\n        if (n == -1) break;\n        \n        vector<int> completers(n);\n        for (int i = 0; i < n; ++i) {\n            cin >> completers[i];\n            --completers[i];\n        }\n        \n        for (int f : completers) {\n            int task = member_status[f];\n            if (task == -1) continue;\n            int t = current_day - start_day[task] + 1;\n            if (t == 1) {\n                for (int k = 0; k < K; ++k) {\n                    est_s[f][k] = max(est_s[f][k], d[task][k]);\n                }\n            } else {\n                long long current_w = 0;\n                vector<int> deficit_skills;\n                for (int k = 0; k < K; ++k) {\n                    int def = max(0, d[task][k] - est_s[f][k]);\n                    current_w += def;\n                    if (def > 0) deficit_skills.push_back(k);\n                }\n                long long additional = t - current_w;\n                if (!deficit_skills.empty() && additional > 0) {\n                    double alpha = 0.6;\n                    double add_per = alpha * additional / deficit_skills.size();\n                    for (int k : deficit_skills) {\n                        est_s[f][k] += (int)add_per;\n                    }\n                }\n            }\n            task_status[task] = 1;\n            member_status[f] = -1;\n        }\n        \n        bool all_done = true;\n        for (int i = 0; i < N; ++i) {\n            if (task_status[i] != 1) all_done = false;\n        }\n        if (all_done) break;\n    }\n    \n    return 0;\n}","ahc006":"#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing pii = pair<int, int>;\n\nstruct Order {\n    int a, b, c, d, idx;\n    ll cost;\n};\n\nll dist(int x1, int y1, int x2, int y2) {\n    return abs(x1 - x2) + abs(y1 - y2);\n}\n\n// Check if a sequence violates precedence\nbool is_valid(const vector<int>& order) {\n    vector<int> pickup_pos(50, -1), delivery_pos(50, -1);\n    for (size_t i = 0; i < order.size(); ++i) {\n        int idx = order[i];\n        if (idx < 50) pickup_pos[idx] = i;\n        else delivery_pos[idx - 50] = i;\n    }\n    for (int i = 0; i < 50; ++i) {\n        if (pickup_pos[i] >= delivery_pos[i]) return false;\n    }\n    return true;\n}\n\n// Simulated Annealing with precedence and 2-opt\nvector<int> simulated_annealing(const vector<pii>& points, mt19937& rng) {\n    int n = 100;\n    vector<int> order(n);\n    for (int i = 0; i < n; ++i) order[i] = i;\n    \n    auto calc_dist = [&](const vector<int>& ord) -> ll {\n        ll total = dist(400, 400, points[ord[0]].first, points[ord[0]].second);\n        for (int i = 0; i < n - 1; ++i) {\n            total += dist(points[ord[i]].first, points[ord[i]].second,\n                          points[ord[i+1]].first, points[ord[i+1]].second);\n        }\n        total += dist(points[ord.back()].first, points[ord.back()].second, 400, 400);\n        return total;\n    };\n    \n    ll best_dist = calc_dist(order);\n    vector<int> best_order = order;\n    \n    double T = 1000.0;\n    double alpha = 0.998;\n    int max_iter = 500000;\n    \n    uniform_int_distribution<int> dist_idx(0, n - 1);\n    uniform_real_distribution<double> dist_prob(0.0, 1.0);\n    \n    for (int iter = 0; iter < max_iter; ++iter) {\n        vector<int> new_order = order;\n        int i = dist_idx(rng), j = dist_idx(rng);\n        swap(new_order[i], new_order[j]);\n        if (!is_valid(new_order)) continue;\n        \n        ll new_dist = calc_dist(new_order);\n        ll delta = new_dist - calc_dist(order);\n        \n        if (delta < 0 || dist_prob(rng) < exp(-delta / T)) {\n            order = new_order;\n            if (new_dist < best_dist) {\n                best_dist = new_dist;\n                best_order = order;\n            }\n        }\n        \n        T *= alpha;\n    }\n    \n    // 2-opt\n    auto two_opt = [&](vector<int>& ord) {\n        bool improved = true;\n        while (improved) {\n            improved = false;\n            for (int i = 0; i < n - 1; ++i) {\n                for (int j = i + 1; j < n; ++j) {\n                    vector<int> new_ord = ord;\n                    reverse(new_ord.begin() + i, new_ord.begin() + j + 1);\n                    if (is_valid(new_ord) && calc_dist(new_ord) < calc_dist(ord)) {\n                        ord = new_ord;\n                        improved = true;\n                    }\n                }\n            }\n        }\n    };\n    two_opt(best_order);\n    \n    return best_order;\n}\n\nint main() {\n    const int OFFICE_X = 400, OFFICE_Y = 400;\n    vector<Order> orders(1000);\n    for (int i = 0; i < 1000; ++i) {\n        cin >> orders[i].a >> orders[i].b >> orders[i].c >> orders[i].d;\n        orders[i].idx = i + 1;\n        orders[i].cost = dist(OFFICE_X, OFFICE_Y, orders[i].a, orders[i].b) +\n                         dist(orders[i].a, orders[i].b, orders[i].c, orders[i].d) +\n                         dist(orders[i].c, orders[i].d, OFFICE_X, OFFICE_Y);\n    }\n    \n    sort(orders.begin(), orders.end(), [](const Order& o1, const Order& o2) {\n        return o1.cost < o2.cost;\n    });\n    vector<Order> selected(orders.begin(), orders.begin() + 50);\n    \n    vector<pii> points(100);\n    for (int i = 0; i < 50; ++i) {\n        points[i] = {selected[i].a, selected[i].b};\n        points[i + 50] = {selected[i].c, selected[i].d};\n    }\n    \n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n    vector<int> best_order;\n    ll best_total_dist = LLONG_MAX;\n    for (int restart = 0; restart < 5; ++restart) {\n        auto order = simulated_annealing(points, rng);\n        ll total_dist = dist(OFFICE_X, OFFICE_Y, points[order[0]].first, points[order[0]].second);\n        for (int i = 0; i < 99; ++i) {\n            total_dist += dist(points[order[i]].first, points[order[i]].second,\n                               points[order[i+1]].first, points[order[i+1]].second);\n        }\n        total_dist += dist(points[order.back()].first, points[order.back()].second, OFFICE_X, OFFICE_Y);\n        if (total_dist < best_total_dist) {\n            best_total_dist = total_dist;\n            best_order = order;\n        }\n    }\n    \n    vector<pii> full_route;\n    full_route.emplace_back(OFFICE_X, OFFICE_Y);\n    for (int idx : best_order) full_route.push_back(points[idx]);\n    full_route.emplace_back(OFFICE_X, OFFICE_Y);\n    \n    cout << 50;\n    for (const auto& ord : selected) cout << \" \" << ord.idx;\n    cout << \"\\n\" << full_route.size();\n    for (const auto& p : full_route) cout << \" \" << p.first << \" \" << p.second;\n    cout << \"\\n\";\n    \n    return 0;\n}","ahc007":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    const int N = 400;\n    const int M = 1995;\n    vector<int> x(N), y(N);\n    for (int i = 0; i < N; i++) {\n        cin >> x[i] >> y[i];\n    }\n    vector<int> u(M), v(M);\n    for (int i = 0; i < M; i++) {\n        cin >> u[i] >> v[i];\n    }\n    // Precompute d_i for each edge\n    vector<int> d(M);\n    for (int i = 0; i < M; i++) {\n        long long dx = x[u[i]] - x[v[i]];\n        long long dy = y[u[i]] - y[v[i]];\n        d[i] = round(sqrt(dx * dx + dy * dy));\n    }\n    // Adjacency list: vector of {to, {edge_index, weight}}\n    vector<vector<pair<int, pair<int, int>>>> adj(N);\n    vector<bool> adopted(M, false);\n    vector<int> weights(M, 0);\n    // Multiset for median weight tracking\n    multiset<int> weight_set;\n    // Running total cost\n    long long total_cost = 0;\n    int adopted_count = 0;\n    \n    auto remove_edge = [&](int a, int b, int e) {\n        for (auto it = adj[a].begin(); it != adj[a].end(); ++it) {\n            if (it->second.first == e) {\n                adj[a].erase(it);\n                break;\n            }\n        }\n    };\n    \n    for (int i = 0; i < M; i++) {\n        int l;\n        cin >> l;\n        int uu = u[i], vv = v[i];\n        // BFS to check connectivity and find max on path\n        vector<int> dist(N, -1);\n        vector<int> parent(N, -1);\n        vector<int> edge_to_parent(N, -1);\n        queue<int> q;\n        q.push(uu);\n        dist[uu] = 0;\n        bool found = false;\n        int max_weight = -1;\n        int max_edge = -1;\n        while (!q.empty() && !found) {\n            int cur = q.front();\n            q.pop();\n            for (auto &[to, p] : adj[cur]) {\n                int eidx = p.first;\n                int w = p.second;\n                if (dist[to] == -1) {\n                    dist[to] = dist[cur] + 1;\n                    parent[to] = cur;\n                    edge_to_parent[to] = eidx;\n                    q.push(to);\n                    if (to == vv) {\n                        found = true;\n                        // Backtrack to find max on path\n                        int current = vv;\n                        while (current != uu) {\n                            int e = edge_to_parent[current];\n                            int ww = weights[edge_to_parent[current]];\n                            if (ww > max_weight) {\n                                max_weight = ww;\n                                max_edge = edge_to_parent[current];\n                            }\n                            current = parent[current];\n                        }\n                        break;\n                    }\n                }\n            }\n        }\n        bool adopt = false;\n        if (!found) {\n            // Not connected, adopt\n            adopt = true;\n        } else {\n            // Connected, check for replacement (more aggressive)\n            if (l * 1.1 < max_weight) {\n                adopt = true;\n                // Remove max_edge\n                adopted[max_edge] = false;\n                weight_set.erase(weight_set.find(weights[max_edge]));\n                total_cost -= weights[max_edge];\n                adopted_count--;\n                remove_edge(u[max_edge], v[max_edge], max_edge);\n                remove_edge(v[max_edge], u[max_edge], max_edge);\n            } else {\n                // Check for new adoption heuristic\n                int median_weight = (weight_set.size() < 10) ? 2000 : *next(weight_set.begin(), weight_set.size() / 2);\n                double avg_cost = (adopted_count == 0) ? 1e9 : total_cost / (double)adopted_count;\n                if (l <= 1.2 * d[i] && l < median_weight * 1.05 && l < avg_cost * 0.9) {\n                    adopt = true;\n                }\n            }\n        }\n        if (adopt) {\n            adopted[i] = true;\n            adj[uu].emplace_back(vv, make_pair(i, l));\n            adj[vv].emplace_back(uu, make_pair(i, l));\n            weights[i] = l;\n            weight_set.insert(l);\n            total_cost += l;\n            adopted_count++;\n            cout << 1 << endl;\n            fflush(stdout);\n        } else {\n            cout << 0 << endl;\n            fflush(stdout);\n        }\n    }\n    return 0;\n}","ahc008":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int DX[4] = {-1, 0, 1, 0};\nconst int DY[4] = {0, 1, 0, -1};\n\ndouble compute_score(pair<int, int> pos, const vector<vector<bool>>& passable, const vector<pair<int, int>>& pets) {\n    vector<vector<bool>> vis(31, vector<bool>(31, false));\n    queue<pair<int, int>> q;\n    q.push(pos);\n    vis[pos.first][pos.second] = true;\n    int area = 1;\n    int num_pets = 0;\n    for (auto& p : pets) {\n        if (p == pos) num_pets++;\n    }\n    while (!q.empty()) {\n        auto [x, y] = q.front();\n        q.pop();\n        for (int d = 0; d < 4; d++) {\n            int nx = x + DX[d], ny = y + DY[d];\n            if (nx >= 1 && nx <= 30 && ny >= 1 && ny <= 30 && passable[nx][ny] && !vis[nx][ny]) {\n                vis[nx][ny] = true;\n                area++;\n                for (auto& p : pets) {\n                    if (p == make_pair(nx, ny)) num_pets++;\n                }\n                q.push({nx, ny});\n            }\n        }\n    }\n    return (area / 900.0) * pow(2.0, -num_pets);\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N;\n    cin >> N;\n    vector<pair<int, int>> pets(N);\n    vector<int> pt(N);\n    for (int i = 0; i < N; i++) {\n        int x, y, t;\n        cin >> x >> y >> t;\n        pets[i] = {x, y};\n        pt[i] = t;\n    }\n    int M;\n    cin >> M;\n    vector<pair<int, int>> humans(M);\n    for (int i = 0; i < M; i++) {\n        int x, y;\n        cin >> x >> y;\n        humans[i] = {x, y};\n    }\n\n    vector<vector<bool>> passable(31, vector<bool>(31, true));\n\n    for (int turn = 0; turn < 300; turn++) {\n        vector<string> actions(M, \".\");\n        for (int i = 0; i < M; i++) {\n            double max_s = -1e9;\n            string best_act = \".\";\n            pair<int, int> cur_pos = humans[i];\n\n            // Try stay\n            {\n                double s = compute_score(cur_pos, passable, pets);\n                if (s > max_s) {\n                    max_s = s;\n                    best_act = \".\";\n                }\n            }\n\n            // Try builds\n            vector<string> builds = {\"u\", \"d\", \"l\", \"r\"};\n            for (auto act : builds) {\n                int dx = 0, dy = 0;\n                if (act == \"u\") dx = -1;\n                else if (act == \"d\") dx = 1;\n                else if (act == \"l\") dy = -1;\n                else if (act == \"r\") dy = 1;\n                int nx = cur_pos.first + dx, ny = cur_pos.second + dy;\n                bool valid = true;\n                if (nx < 1 || nx > 30 || ny < 1 || ny > 30) valid = false;\n                else {\n                    // Check for entities\n                    bool has_entity = false;\n                    for (auto& p : pets) {\n                        if (p == make_pair(nx, ny)) has_entity = true;\n                    }\n                    for (int j = 0; j < M; j++) {\n                        if (humans[j] == make_pair(nx, ny)) has_entity = true;\n                    }\n                    if (has_entity) valid = false;\n                    // Check adjacent pets\n                    for (int d = 0; d < 4; d++) {\n                        int ax = nx + DX[d], ay = ny + DY[d];\n                        if (ax >= 1 && ax <= 30 && ay >= 1 && ay <= 30) {\n                            for (auto& p : pets) {\n                                if (p == make_pair(ax, ay)) {\n                                    valid = false;\n                                    break;\n                                }\n                            }\n                        }\n                        if (!valid) break;\n                    }\n                    if (valid) {\n                        passable[nx][ny] = false;\n                        double s = compute_score(cur_pos, passable, pets);\n                        if (s > max_s) {\n                            max_s = s;\n                            best_act = act;\n                        }\n                        passable[nx][ny] = true;\n                    }\n                }\n            }\n\n            // Try moves\n            vector<string> moves = {\"U\", \"D\", \"L\", \"R\"};\n            for (auto act : moves) {\n                int dx = 0, dy = 0;\n                if (act == \"U\") dx = -1;\n                else if (act == \"D\") dx = 1;\n                else if (act == \"L\") dy = -1;\n                else if (act == \"R\") dy = 1;\n                int nx = cur_pos.first + dx, ny = cur_pos.second + dy;\n                bool valid = true;\n                if (nx < 1 || nx > 30 || ny < 1 || ny > 30) valid = false;\n                else if (!passable[nx][ny]) valid = false;\n                else {\n                    pair<int, int> new_pos = {nx, ny};\n                    double s = compute_score(new_pos, passable, pets);\n                    if (s > max_s) {\n                        max_s = s;\n                        best_act = act;\n                    }\n                }\n            }\n            actions[i] = best_act;\n        }\n\n        // Apply human moves\n        for (int i = 0; i < M; i++) {\n            string act = actions[i];\n            if (act[0] >= 'A' && act[0] <= 'Z') {  // Move\n                int dx = 0, dy = 0;\n                if (act == \"U\") dx = -1;\n                else if (act == \"D\") dx = 1;\n                else if (act == \"L\") dy = -1;\n                else if (act == \"R\") dy = 1;\n                int nx = humans[i].first + dx, ny = humans[i].second + dy;\n                humans[i] = {nx, ny};\n            }\n        }\n\n        // Apply builds\n        for (int i = 0; i < M; i++) {\n            string act = actions[i];\n            if (act[0] >= 'a' && act[0] <= 'z') {  // Build\n                int dx = 0, dy = 0;\n                if (act == \"u\") dx = -1;\n                else if (act == \"d\") dx = 1;\n                else if (act == \"l\") dy = -1;\n                else if (act == \"r\") dy = 1;\n                int nx = humans[i].first + dx, ny = humans[i].second + dy;\n                passable[nx][ny] = false;\n            }\n        }\n\n        // Output actions\n        for (auto& a : actions) cout << a;\n        cout << endl;\n        cout.flush();\n\n        // Read and apply pet moves\n        for (int i = 0; i < N; i++) {\n            string s;\n            cin >> s;\n            if (s == \".\") continue;\n            pair<int, int> pos = pets[i];\n            for (char c : s) {\n                int dx = 0, dy = 0;\n                if (c == 'U') dx = -1;\n                else if (c == 'D') dx = 1;\n                else if (c == 'L') dy = -1;\n                else if (c == 'R') dy = 1;\n                int nx = pos.first + dx, ny = pos.second + dy;\n                if (nx >= 1 && nx <= 30 && ny >= 1 && ny <= 30 && passable[nx][ny]) {\n                    pos = {nx, ny};\n                }\n            }\n            pets[i] = pos;\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), v(20);\n    for (int i = 0; i < 20; i++) cin >> h[i];\n    for (int i = 0; i < 20; i++) cin >> v[i];\n    \n    // Build adjacency list for the grid\n    vector<vector<vector<pair<int, int>>>> adj(20, vector<vector<pair<int, int>>>(20));\n    for (int i = 0; i < 20; i++) {\n        for (int j = 0; j < 20; j++) {\n            // Right\n            if (j < 19 && h[i][j] == '0') adj[i][j].push_back({i, j + 1});\n            // Left\n            if (j > 0 && h[i][j - 1] == '0') adj[i][j].push_back({i, j - 1});\n            // Down\n            if (i < 19 && v[i][j] == '0') adj[i][j].push_back({i + 1, j});\n            // Up\n            if (i > 0 && v[i - 1][j] == '0') adj[i][j].push_back({i - 1, j});\n        }\n    }\n    \n    // BFS to find shortest path\n    vector<vector<int>> dist(20, vector<int>(20, -1));\n    vector<vector<pair<int, int>>> parent(20, vector<pair<int, int>>(20, {-1, -1}));\n    queue<pair<int, int>> q;\n    q.push({si, sj});\n    dist[si][sj] = 0;\n    \n    while (!q.empty()) {\n        auto [x, y] = q.front(); q.pop();\n        for (auto [nx, ny] : adj[x][y]) {\n            if (dist[nx][ny] == -1) {\n                dist[nx][ny] = dist[x][y] + 1;\n                parent[nx][ny] = {x, y};\n                q.push({nx, ny});\n            }\n        }\n    }\n    \n    // If unreachable (shouldn't happen), output empty\n    if (dist[ti][tj] == -1) {\n        cout << \"\" << endl;\n        return 0;\n    }\n    \n    // Reconstruct the path\n    vector<pair<int, int>> path;\n    pair<int, int> cur = {ti, tj};\n    while (cur != make_pair(si, sj)) {\n        path.push_back(cur);\n        cur = parent[cur.first][cur.second];\n    }\n    path.push_back({si, sj});\n    reverse(path.begin(), path.end());\n    \n    // Generate the move string\n    string full_moves = \"\";\n    for (size_t i = 1; i < path.size(); i++) {\n        auto [px, py] = path[i - 1];\n        auto [cx, cy] = path[i];\n        if (cx == px + 1) full_moves += 'D';\n        else if (cx == px - 1) full_moves += 'U';\n        else if (cy == py + 1) full_moves += 'R';\n        else if (cy == py - 1) full_moves += 'L';\n    }\n    \n    // Determine safe loop moves from office\n    string loop = \"\";\n    bool can_up = (ti > 0 && v[ti - 1][tj] == '0');\n    bool can_down = (ti < 19 && v[ti][tj] == '0');\n    bool can_left = (tj > 0 && h[ti][tj - 1] == '0');\n    bool can_right = (tj < 19 && h[ti][tj] == '0');\n    if (can_up && can_down) loop = \"UD\";\n    else if (can_left && can_right) loop = \"LR\";\n    else if (can_up) loop = \"U\";\n    else if (can_down) loop = \"D\";\n    else if (can_left) loop = \"L\";\n    else if (can_right) loop = \"R\";\n    else loop = \"\";  // No safe moves, skip loop\n    \n    // Handle length <= 200 with repetition and adaptive loops\n    int L = full_moves.size();\n    string output;\n    if (L <= 200) {\n        int max_repeat = (L <= 100) ? 4 : 2;\n        int repeat = min(max_repeat, 200 / L);\n        for (int i = 0; i < repeat; i++) output += full_moves;\n        int rem = 200 - output.size();\n        // Add adaptive loop to fill remaining\n        for (int i = 0; i < rem; i++) {\n            if (!loop.empty()) output += loop[i % loop.size()];\n            else output += full_moves[i % L];  // Fallback to path prefix\n        }\n    } else {\n        // Truncate and add loop\n        output = full_moves.substr(0, 200);\n    }\n    \n    cout << output << endl;\n    return 0;\n}","ahc010":"#include <bits/stdc++.h>\nusing namespace std;\n\nmt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n\nconst vector<vector<int>> to = {\n    {1, 0, -1, -1},  // 0\n    {3, -1, -1, 0},  // 1\n    {-1, -1, 3, 2},  // 2\n    {-1, 2, 1, -1},  // 3\n    {1, 0, 3, 2},    // 4\n    {3, 2, 1, 0},    // 5\n    {2, -1, 0, -1},  // 6\n    {-1, 3, -1, 1}   // 7\n};\n\nconst vector<int> di = {0, -1, 0, 1};\nconst vector<int> dj = {-1, 0, 1, 0};\n\nint get_type(int t, int r) {\n    if (t <= 3) return (t + r) % 4;\n    if (t <= 5) return 4 + (t - 4 + r) % 2;\n    return 6 + (t - 6 + r) % 2;\n}\n\nlong long compute_score(const vector<vector<int>>& tiles, const vector<int>& rot) {\n    vector<vector<int>> adj(900);\n    for (int i = 0; i < 30; i++) {\n        for (int j = 0; j < 30; j++) {\n            int idx = i * 30 + j;\n            int t = tiles[i][j];\n            int r = rot[idx];\n            int et = get_type(t, r);\n            for (int d = 0; d < 4; d++) {\n                if (to[et][d] != -1) {\n                    int d2 = to[et][d];\n                    int ni = i + di[d2];\n                    int nj = j + dj[d2];\n                    if (ni >= 0 && ni < 30 && nj >= 0 && nj < 30) {\n                        int nidx = ni * 30 + nj;\n                        if (ni > i || (ni == i && nj > j)) {\n                            adj[idx].push_back(nidx);\n                            adj[nidx].push_back(idx);\n                        }\n                    }\n                }\n            }\n        }\n    }\n    vector<int> cycle_lengths;\n    vector<bool> visited(900, false);\n    vector<int> disc(900, -1);\n    vector<int> parent(900, -1);\n    int timestamp = 0;\n    function<void(int)> dfs = [&](int u) {\n        visited[u] = true;\n        disc[u] = timestamp++;\n        for (int v : adj[u]) {\n            if (!visited[v]) {\n                parent[v] = u;\n                dfs(v);\n            } else if (v != parent[u] && disc[v] < disc[u]) {\n                int len = disc[u] - disc[v] + 1;\n                cycle_lengths.push_back(len);\n            }\n        }\n    };\n    for (int i = 0; i < 900; i++) if (!visited[i]) dfs(i);\n    sort(cycle_lengths.rbegin(), cycle_lengths.rend());\n    if (cycle_lengths.size() >= 2) {\n        return (long long)cycle_lengths[0] * cycle_lengths[1];\n    }\n    return 0;\n}\n\nint main() {\n    vector<vector<int>> tiles(30, vector<int>(30));\n    for (int i = 0; i < 30; i++) {\n        string s;\n        cin >> s;\n        for (int j = 0; j < 30; j++) tiles[i][j] = s[j] - '0';\n    }\n    // Random sampling: Generate 1000 random configurations and pick the best\n    vector<int> best_rot(900);\n    long long best_score = 0;\n    for (int sample = 0; sample < 1000; sample++) {\n        vector<int> rot(900);\n        for (int &x : rot) x = rng() % 4;\n        long long score = compute_score(tiles, rot);\n        if (score > best_score) {\n            best_score = score;\n            best_rot = rot;\n        }\n    }\n    // Limited local search: Try 1-2 random rotations per tile on the best\n    for (int attempt = 0; attempt < 900; attempt++) {\n        int idx = rng() % 900;\n        int old_r = best_rot[idx];\n        for (int r = 0; r < 4; r++) {\n            if (r == old_r) continue;\n            best_rot[idx] = r;\n            long long new_score = compute_score(tiles, best_rot);\n            if (new_score > best_score) {\n                best_score = new_score;\n            } else {\n                best_rot[idx] = old_r;\n            }\n        }\n    }\n    string output;\n    for (int x : best_rot) output += '0' + x;\n    cout << output << endl;\n    return 0;\n}","ahc011":"#include <bits/stdc++.h>\nusing namespace std;\n\nint N, T;\nvector<vector<int>> board;\npair<int, int> empty_pos;\n\n// Directions: 0=U, 1=D, 2=L, 3=R\nvector<pair<int, int>> dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};\nstring dir_chars = \"UDLR\";\n\n// Check if a position is valid\nbool valid(int i, int j) {\n    return i >= 0 && i < N && j >= 0 && j < N;\n}\n\n// Simulate a move: return new board and new empty pos\npair<vector<vector<int>>, pair<int, int>> simulate_move(const vector<vector<int>>& curr_board, pair<int, int> curr_empty, int dir) {\n    auto [ei, ej] = curr_empty;\n    auto [di, dj] = dirs[dir];\n    int ni = ei + di, nj = ej + dj;\n    if (!valid(ni, nj)) return {curr_board, curr_empty}; // Invalid move\n    auto new_board = curr_board;\n    swap(new_board[ei][ej], new_board[ni][nj]);\n    return {new_board, {ni, nj}};\n}\n\n// Compute tree size, connections, and component count\ntuple<int, int, int> compute_metrics(const vector<vector<int>>& curr_board) {\n    vector<vector<bool>> visited(N, vector<bool>(N, false));\n    int max_tree = 0, total_connections = 0, components = 0;\n    \n    // Directions for connections: 0=left, 1=up, 2=right, 3=down\n    vector<pair<int, int>> conn_dirs = {{0, -1}, {-1, 0}, {0, 1}, {1, 0}};\n    vector<int> masks = {1, 2, 4, 8}; // left, up, right, down\n    \n    for (int i = 0; i < N; ++i) {\n        for (int j = 0; j < N; ++j) {\n            if (curr_board[i][j] == 0 || visited[i][j]) continue;\n            \n            // BFS for component\n            queue<pair<int, int>> q;\n            q.push({i, j});\n            visited[i][j] = true;\n            int vertices = 0, edges = 0;\n            components++;\n            \n            while (!q.empty()) {\n                auto [ci, cj] = q.front(); q.pop();\n                vertices++;\n                \n                for (int d = 0; d < 4; ++d) {\n                    auto [di, dj] = conn_dirs[d];\n                    int ni = ci + di, nj = cj + dj;\n                    if (valid(ni, nj) && curr_board[ni][nj] != 0) {\n                        // Check if connection exists\n                        bool has_out = (curr_board[ci][cj] & masks[d]) != 0;\n                        bool has_in = (curr_board[ni][nj] & masks[(d + 2) % 4]) != 0;\n                        if (has_out && has_in) {\n                            edges++;\n                            if (!visited[ni][nj]) {\n                                visited[ni][nj] = true;\n                                q.push({ni, nj});\n                            }\n                        }\n                    }\n                }\n            }\n            \n            // Check if it's a tree\n            if (edges == vertices - 1) {\n                max_tree = max(max_tree, vertices);\n            }\n        }\n    }\n    \n    // Compute total possible connections\n    for (int i = 0; i < N; ++i) {\n        for (int j = 0; j < N; ++j) {\n            if (curr_board[i][j] == 0) continue;\n            for (int d = 0; d < 4; ++d) {\n                auto [di, dj] = conn_dirs[d];\n                int ni = i + di, nj = j + dj;\n                if (valid(ni, nj) && curr_board[ni][nj] != 0) {\n                    bool has_out = (curr_board[i][j] & masks[d]) != 0;\n                    bool has_in = (curr_board[ni][nj] & masks[(d + 2) % 4]) != 0;\n                    if (has_out && has_in) total_connections++;\n                }\n            }\n        }\n    }\n    total_connections /= 2;\n    \n    return {max_tree, total_connections, components};\n}\n\n// Heuristic for A* (reverted)\nint heuristic(int tree_size, int connections, int components) {\n    return (N * N - 1 - tree_size) + (components - 1) + (connections / 10);\n}\n\nint main() {\n    cin >> N >> T;\n    board.resize(N, vector<int>(N));\n    for (int i = 0; i < N; ++i) {\n        string s; cin >> s;\n        for (int j = 0; j < N; ++j) {\n            if (s[j] >= '0' && s[j] <= '9') board[i][j] = s[j] - '0';\n            else board[i][j] = 10 + (s[j] - 'a');\n            if (board[i][j] == 0) empty_pos = {i, j};\n        }\n    }\n    \n    string moves = \"\";\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n    uniform_real_distribution<double> dist(0.0, 1.0);\n    \n    auto [current_tree, current_conn, current_comp] = compute_metrics(board);\n    double temperature = 50.0 * N;\n    const double COOLING_RATE = 0.99;\n    const int MAX_ACCEPT_WORSE = 3;  // Reverted\n    int no_improve_count = 0;\n    const int STUCK_THRESHOLD = 5 + N;  // Reverted\n    bool full_tree_achieved = false;\n    const int ANNEALING_LIMIT = 1000;  // Limit annealing to early moves\n    \n    while ((int)moves.size() < T) {\n        vector<tuple<int, int, int, string, vector<vector<int>>, pair<int, int>>> candidates;\n        \n        // Evaluate valid moves\n        for (int d = 0; d < 4; ++d) {\n            auto [new_board, new_empty] = simulate_move(board, empty_pos, d);\n            if (new_empty == empty_pos) continue;\n            auto [new_tree, new_conn, new_comp] = compute_metrics(new_board);\n            candidates.emplace_back(new_tree, new_conn, new_comp, string(1, dir_chars[d]), new_board, new_empty);\n        }\n        \n        if (candidates.empty()) break;\n        \n        // Sort by tree size, then connections, then components (fewer better), then heuristic\n        sort(candidates.begin(), candidates.end(), [](const auto& a, const auto& b) {\n            int at, ac, acomp;\n            std::tie(at, ac, acomp, std::ignore, std::ignore, std::ignore) = a;\n            int bt, bc, bcomp;\n            std::tie(bt, bc, bcomp, std::ignore, std::ignore, std::ignore) = b;\n            if (at != bt) return at > bt;\n            if (ac != bc) return ac > bc;\n            if (acomp != bcomp) return acomp < bcomp;\n            return heuristic(at, ac, acomp) < heuristic(bt, bc, bcomp);\n        });\n        \n        int best_idx = 0;\n        int bt, bc, bcomp;\n        string dummy_str;\n        vector<vector<int>> dummy_board;\n        pair<int, int> dummy_pair;\n        tie(bt, bc, bcomp, dummy_str, dummy_board, dummy_pair) = candidates[best_idx];\n        \n        bool use_annealing = (no_improve_count >= STUCK_THRESHOLD && !full_tree_achieved && (int)moves.size() < ANNEALING_LIMIT);\n        \n        for (int i = 1; i < min(MAX_ACCEPT_WORSE, (int)candidates.size()); ++i) {\n            int nt, nc, ncomp;\n            tie(nt, nc, ncomp, ignore, ignore, ignore) = candidates[i];\n            bool better = (nt > bt) || (nt == bt && nc > bc) || (nt == bt && nc == bc && ncomp < bcomp);\n            if (better) {\n                best_idx = i;\n                bt = nt; bc = nc; bcomp = ncomp;\n            } else if (use_annealing && dist(rng) < exp((nt - bt) / temperature)) {\n                best_idx = i;\n                bt = nt; bc = nc; bcomp = ncomp;\n            }\n        }\n        \n        // Apply move\n        string chosen_move;\n        vector<vector<int>> new_board;\n        pair<int, int> new_empty;\n        tie(ignore, ignore, ignore, chosen_move, new_board, new_empty) = candidates[best_idx];\n        board = new_board;\n        empty_pos = new_empty;\n        moves += chosen_move;\n        \n        // Update\n        auto [new_tree, new_conn, new_comp] = compute_metrics(board);\n        if (new_tree > current_tree) {\n            no_improve_count = 0;\n        } else {\n            no_improve_count++;\n        }\n        current_tree = new_tree; current_conn = new_conn; current_comp = new_comp;\n        \n        if (current_tree == N * N - 1) {\n            full_tree_achieved = true;\n            // For full tree, only accept better or equal\n            if (new_tree < current_tree) continue; // Skip worse\n        }\n        temperature *= COOLING_RATE;\n    }\n    \n    cout << moves << endl;\n    return 0;\n}","ahc012":"#include <bits/stdc++.h>\n\nusing namespace std;\n\nint main() {\n    int N, K;\n    cin >> N >> K;\n    vector<int> a(11);\n    for(int i = 1; i <= 10; i++) cin >> a[i];\n    vector<long long> x(N), y(N);\n    for(int i = 0; i < N; i++) cin >> x[i] >> y[i];\n    \n    // Use one solution with 100 lines through random points on the circle\n    mt19937 rng(42);\n    uniform_real_distribution<double> theta_dist(0, 2 * M_PI);\n    const long long R = 10000;  // Radius of the cake\n    \n    set<pair<pair<long long, long long>, pair<long long, long long>>> used_lines;\n    vector<tuple<long long, long long, long long, long long>> lines;\n    \n    while(lines.size() < 100) {\n        double theta1 = theta_dist(rng);\n        double theta2 = theta_dist(rng);\n        long long px = round(R * cos(theta1));\n        long long py = round(R * sin(theta1));\n        long long qx = round(R * cos(theta2));\n        long long qy = round(R * sin(theta2));\n        // Normalize\n        if(px > qx || (px == qx && py > qy)) {\n            swap(px, qx);\n            swap(py, qy);\n        }\n        pair<pair<long long, long long>, pair<long long, long long>> line_key = {{px, py}, {qx, qy}};\n        if(used_lines.insert(line_key).second) {\n            lines.emplace_back(px, py, qx, qy);\n        }\n    }\n    \n    cout << lines.size() << endl;\n    for(auto [px, py, qx, qy] : lines) {\n        cout << px << \" \" << py << \" \" << qx << \" \" << qy << endl;\n    }\n    \n    return 0;\n}","ahc014":"#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n\nstruct Operation {\n    int w, x1, y1, x2, y2, x3, y3, x4, y4;\n};\n\nbool cmp(const Operation& a, const Operation& b) {\n    return a.w < b.w;\n}\n\nint main() {\n    int N, M;\n    cin >> N >> M;\n    vector<vector<bool>> has_dot(N, vector<bool>(N, false));\n    int c = (N - 1) / 2;\n    auto calc_w = [&](int x, int y) {\n        int dx = x - c;\n        int dy = y - c;\n        return dx * dx + dy * dy + 1;\n    };\n    for (int i = 0; i < M; i++) {\n        int x, y;\n        cin >> x >> y;\n        has_dot[x][y] = true;\n    }\n    vector<vector<vector<bool>>> horiz(N, vector<vector<bool>>(N, vector<bool>(N, false)));\n    vector<vector<vector<bool>>> vert(N, vector<vector<bool>>(N, vector<bool>(N, false)));\n    vector<vector<bool>> diag1(N - 1, vector<bool>(N - 1, false));\n    vector<vector<bool>> diag2(N - 1, vector<bool>(N, false));\n    vector<vector<bool>> diag3(N, vector<bool>(N - 1, false));\n    vector<vector<bool>> diag4(N, vector<bool>(N, false));\n    vector<Operation> ops;\n    for (int px = 0; px < N; px++) {\n        for (int py = 0; py < N; py++) {\n            if (has_dot[px][py]) continue;\n            int w = calc_w(px, py);\n            // axis-aligned\n            for (int dx = 1; dx <= N - 1 - px; dx++) {\n                for (int dy = 1; dy <= N - 1 - py; dy++) {\n                    int bx = px + dx, by = py;\n                    int cx = px + dx, cy = py + dy;\n                    int ex = px, ey = py + dy;\n                    if (has_dot[bx][by] && has_dot[cx][cy] && has_dot[ex][ey]) {\n                        bool ok = true;\n                        // check sides\n                        for (int i = px + 1; i < bx; i++) if (has_dot[py][i]) ok = false;\n                        for (int i = px + 1; i < bx; i++) if (has_dot[cy][i]) ok = false;\n                        for (int i = py + 1; i < cy; i++) if (has_dot[px][i]) ok = false;\n                        for (int i = py + 1; i < cy; i++) if (has_dot[bx][i]) ok = false;\n                        // check edges\n                        for (int i = px; i < bx; i++) if (horiz[py][i][px]) ok = false;\n                        for (int i = px; i < bx; i++) if (horiz[cy][i][px]) ok = false;\n                        for (int i = py; i < cy; i++) if (vert[px][i][py]) ok = false;\n                        for (int i = py; i < cy; i++) if (vert[bx][i][py]) ok = false;\n                        if (ok) {\n                            ops.push_back({w, px, py, bx, by, cx, cy, ex, ey});\n                        }\n                    }\n                }\n            }\n            // 45-degree\n            for (int a = 1;; a++) {\n                int p2x = px + a, p2y = py + a;\n                int p3x = px, p3y = py + 2 * a;\n                int p4x = px - a, p4y = py + a;\n                if (p2x >= N || p2y >= N || p3y >= N || p4x < 0 || p4y >= N) break;\n                if (has_dot[p2x][p2y] && has_dot[p3x][p3y] && has_dot[p4x][p4y]) {\n                    bool ok = true;\n                    // p1 to p2\n                    for (int i = 0; i < a; i++) if (diag1[px + i][py + i]) ok = false;\n                    // p2 to p3\n                    for (int i = 0; i < a; i++) if (diag3[p2x - i][p2y + i]) ok = false;\n                    // p3 to p4\n                    for (int i = 0; i < a; i++) if (diag4[p3x - i][p3y - i]) ok = false;\n                    // p4 to p1\n                    for (int i = 0; i < a; i++) if (diag2[p4x + i][p4y - i]) ok = false;\n                    if (ok) {\n                        ops.push_back({w, px, py, p2x, p2y, p3x, p3y, p4x, p4y});\n                    }\n                }\n            }\n        }\n    }\n    sort(ops.begin(), ops.end(), cmp);\n    vector<Operation> final_ops;\n    for (auto& op : ops) {\n        int x1 = op.x1, y1 = op.y1;\n        if (has_dot[x1][y1]) continue;\n        int x2 = op.x2, y2 = op.y2;\n        int x3 = op.x3, y3 = op.y3;\n        int x4 = op.x4, y4 = op.y4;\n        if (!has_dot[x2][y2] || !has_dot[x3][y3] || !has_dot[x4][y4]) continue;\n        // check edges again, in case\n        bool ok = true;\n        // assume axis or 45, check accordingly\n        if (x2 == x1 + (x3 - x1) && y2 == y1 + (y3 - y1)) { // axis\n            int dx = x2 - x1, dy = y3 - y1;\n            for (int i = x1; i < x1 + dx; i++) if (horiz[y1][i][x1]) ok = false;\n            for (int i = x1; i < x1 + dx; i++) if (horiz[y1 + dy][i][x1]) ok = false;\n            for (int i = y1; i < y1 + dy; i++) if (vert[x1][i][y1]) ok = false;\n            for (int i = y1; i < y1 + dy; i++) if (vert[x1 + dx][i][y1]) ok = false;\n        } else { // 45\n            int a = x2 - x1;\n            for (int i = 0; i < a; i++) if (diag1[x1 + i][y1 + i]) ok = false;\n            for (int i = 0; i < a; i++) if (diag3[x2 - i][y2 + i]) ok = false;\n            for (int i = 0; i < a; i++) if (diag4[x3 - i][y3 - i]) ok = false;\n            for (int i = 0; i < a; i++) if (diag2[x4 + i][y4 - i]) ok = false;\n        }\n        if (ok) {\n            final_ops.push_back(op);\n            has_dot[x1][y1] = true;\n            // set edges\n            if (x2 == x1 + (x3 - x1) && y2 == y1 + (y3 - y1)) {\n                int dx = x2 - x1, dy = y3 - y1;\n                for (int i = x1; i < x1 + dx; i++) horiz[y1][i][x1] = true;\n                for (int i = x1; i < x1 + dx; i++) horiz[y1 + dy][i][x1] = true;\n                for (int i = y1; i < y1 + dy; i++) vert[x1][i][y1] = true;\n                for (int i = y1; i < y1 + dy; i++) vert[x1 + dx][i][y1] = true;\n            } else {\n                int a = x2 - x1;\n                for (int i = 0; i < a; i++) diag1[x1 + i][y1 + i] = true;\n                for (int i = 0; i < a; i++) diag3[x2 - i][y2 + i] = true;\n                for (int i = 0; i < a; i++) diag4[x3 - i][y3 - i] = true;\n                for (int i = 0; i < a; i++) diag2[x4 + i][y4 - i] = true;\n            }\n        }\n    }\n    cout << final_ops.size() << endl;\n    for (auto& op : final_ops) {\n        cout << op.x1 << \" \" << op.y1 << \" \" << op.x2 << \" \" << op.y2 << \" \" << op.x3 << \" \" << op.y3 << \" \" << op.x4 << \" \" << op.y4 << endl;\n    }\n    return 0;\n}","ahc015":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 10;\nvector<vector<int>> grid(N, vector<int>(N, 0));\n\n// Directions: F (down), B (up), L (left), R (right)\nstring dirs = \"FBLR\";\n\n// Simulate tilt in direction dir\nvoid simulate_tilt(vector<vector<int>>& g, char dir) {\n    if (dir == 'R') { // Right\n        for (int i = 0; i < N; ++i) {\n            vector<int> new_row(N, 0);\n            int pos = N - 1;\n            for (int j = N - 1; j >= 0; --j) {\n                if (g[i][j] != 0) {\n                    new_row[pos] = g[i][j];\n                    --pos;\n                }\n            }\n            g[i] = new_row;\n        }\n    } else if (dir == 'L') { // Left\n        for (int i = 0; i < N; ++i) {\n            vector<int> new_row(N, 0);\n            int pos = 0;\n            for (int j = 0; j < N; ++j) {\n                if (g[i][j] != 0) {\n                    new_row[pos] = g[i][j];\n                    ++pos;\n                }\n            }\n            g[i] = new_row;\n        }\n    } else if (dir == 'F') { // Forward (down)\n        for (int j = 0; j < N; ++j) {\n            vector<int> new_col(N, 0);\n            int pos = N - 1;\n            for (int i = N - 1; i >= 0; --i) {\n                if (g[i][j] != 0) {\n                    new_col[pos] = g[i][j];\n                    --pos;\n                }\n            }\n            for (int i = 0; i < N; ++i) g[i][j] = new_col[i];\n        }\n    } else if (dir == 'B') { // Backward (up)\n        for (int j = 0; j < N; ++j) {\n            vector<int> new_col(N, 0);\n            int pos = 0;\n            for (int i = 0; i < N; ++i) {\n                if (g[i][j] != 0) {\n                    new_col[pos] = g[i][j];\n                    ++pos;\n                }\n            }\n            for (int i = 0; i < N; ++i) g[i][j] = new_col[i];\n        }\n    }\n}\n\n// DFS to compute component size\nint dfs(int x, int y, int fl, vector<vector<int>>& g, vector<vector<bool>>& vis) {\n    vis[x][y] = true;\n    int cnt = 1;\n    int dx[4] = {-1, 0, 1, 0};\n    int dy[4] = {0, 1, 0, -1};\n    for (int d = 0; d < 4; ++d) {\n        int nx = x + dx[d], ny = y + dy[d];\n        if (nx >= 0 && nx < N && ny >= 0 && ny < N && g[nx][ny] == fl && !vis[nx][ny]) {\n            cnt += dfs(nx, ny, fl, g, vis);\n        }\n    }\n    return cnt;\n}\n\n// Compute score for the grid\nlong long compute_score(const vector<vector<int>>& g) {\n    vector<int> component_sizes;\n    for (int fl = 1; fl <= 3; ++fl) {\n        vector<vector<bool>> visited(N, vector<bool>(N, false));\n        for (int i = 0; i < N; ++i) {\n            for (int j = 0; j < N; ++j) {\n                if (g[i][j] == fl && !visited[i][j]) {\n                    int size = dfs(i, j, fl, const_cast<vector<vector<int>>&>(g), visited);\n                    component_sizes.push_back(size);\n                }\n            }\n        }\n    }\n    long long sum_sq = 0;\n    for (int s : component_sizes) sum_sq += 1LL * s * s;\n    vector<long long> d(4, 0);\n    for (int i = 0; i < N; ++i) {\n        for (int j = 0; j < N; ++j) {\n            if (g[i][j]) d[g[i][j]]++;\n        }\n    }\n    long long sum_d_sq = 0;\n    for (int i = 1; i <= 3; ++i) sum_d_sq += d[i] * d[i];\n    if (sum_d_sq == 0) return 0; // Avoid division by zero\n    double score = 1e6 * sum_sq / (double)sum_d_sq;\n    return round(score);\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    vector<int> f(100);\n    for (int i = 0; i < 100; ++i) cin >> f[i];\n    \n    vector<pair<int, int>> empty;\n    for (int i = 0; i < N; ++i) {\n        for (int j = 0; j < N; ++j) {\n            empty.emplace_back(i, j);\n        }\n    }\n    \n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); // For randomization\n    \n    for (int t = 0; t < 100; ++t) {\n        int p;\n        cin >> p;\n        auto [x, y] = empty[p - 1];\n        grid[x][y] = f[t];\n        empty.erase(empty.begin() + p - 1);\n        \n        // Try all directions and collect candidates\n        long long max_score = -1;\n        vector<char> candidates;\n        for (char dir : dirs) {\n            auto temp_grid = grid;\n            simulate_tilt(temp_grid, dir);\n            long long score = compute_score(temp_grid);\n            if (score > max_score) {\n                max_score = score;\n                candidates = {dir};\n            } else if (score >= max_score * 0.95) { // Within 5% of max\n                candidates.push_back(dir);\n            }\n        }\n        \n        // Choose randomly among candidates\n        char best_dir = candidates[rng() % candidates.size()];\n        \n        // Output\n        cout << best_dir << '\\n';\n        cout.flush();\n        \n        // Simulate tilt on actual grid (skip for last step)\n        if (t < 99) {\n            simulate_tilt(grid, best_dir);\n        }\n    }\n    \n    return 0;\n}","ahc016":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int M;\n    double eps;\n    cin >> M >> eps;\n    int N;\n    if (eps <= 0.1) N = 100;\n    else if (eps <= 0.2) N = 80;\n    else if (eps <= 0.3) N = 60;\n    else N = 40;\n    cout << N << endl;\n    for (int k = 0; k < M; k++) {\n        string s = \"\";\n        for (int i = 0; i < N; i++) {\n            for (int j = i + 1; j < N; j++) {\n                if (i < k && j < k) {\n                    s += '1';\n                } else {\n                    s += '0';\n                }\n            }\n        }\n        cout << s << endl;\n    }\n    cout << flush;\n    vector<vector<int>> targets(M, vector<int>(N));\n    for (int t = 0; t < M; t++) {\n        int kt = min(t, N - 1);\n        for (int i = 0; i < N - kt; i++) {\n            targets[t][i] = 0;\n        }\n        for (int i = N - kt; i < N; i++) {\n            targets[t][i] = kt - 1;\n        }\n    }\n    for (int q = 0; q < 100; q++) {\n        string H;\n        cin >> H;\n        vector<int> deg(N, 0);\n        int idx = 0;\n        for (int i = 0; i < N; i++) {\n            for (int j = i + 1; j < N; j++) {\n                if (H[idx] == '1') {\n                    deg[i]++;\n                    deg[j]++;\n                }\n                idx++;\n            }\n        }\n        sort(deg.begin(), deg.end());\n        int best_t = 0;\n        long long min_diff = LLONG_MAX;\n        for (int t = 0; t < M; t++) {\n            long long diff = 0;\n            for (int i = 0; i < N; i++) {\n                diff += abs(deg[i] - targets[t][i]);\n            }\n            if (diff < min_diff) {\n                min_diff = diff;\n                best_t = t;\n            }\n        }\n        cout << best_t << endl;\n        cout << flush;\n    }\n    return 0;\n}","ahc017":"#include <bits/stdc++.h>\n\nusing namespace std;\n\nint main() {\n  ios::sync_with_stdio(false);\n  cin.tie(0);\n  int N, M, D, K;\n  cin >> N >> M >> D >> K;\n  vector<tuple<int, int, int, int>> edges;\n  for (int i = 0; i < M; i++) {\n    int u, v, w;\n    cin >> u >> v >> w;\n    u--;\n    v--;\n    edges.emplace_back(0, u, v, i);  // Placeholder for key, will update with midx\n  }\n  vector<pair<int, int>> coords(N);\n  for (int i = 0; i < N; i++) {\n    int x, y;\n    cin >> x >> y;\n    coords[i] = {x, y};\n  }\n  // Update edges with midx (x-coordinate of midpoint) as the sorting key\n  for (auto& [key, u, v, idx] : edges) {\n    int midx = (coords[u].first + coords[v].first) / 2;\n    key = midx;\n  }\n  // Sort edges by midx ascending (leftmost edges first)\n  sort(edges.begin(), edges.end());\n  // Compute target sizes for each day\n  int total = M;\n  vector<int> max_size(D, total / D);\n  for (int i = 0; i < total % D; i++) max_size[i]++;\n  // Assign edges\n  vector<int> assignment(M);\n  vector<int> current_size(D, 0);\n  for (auto [midx, u, v, idx] : edges) {\n    // Find the day with the smallest current size that has space\n    int best_day = -1;\n    int min_size = INT_MAX;\n    for (int d = 0; d < D; d++) {\n      if (current_size[d] < max_size[d] && current_size[d] < min_size) {\n        min_size = current_size[d];\n        best_day = d;\n      }\n    }\n    assert(best_day != -1);\n    assignment[idx] = best_day;\n    current_size[best_day]++;\n  }\n  // Output the assignment\n  for (int i = 0; i < M; i++) {\n    if (i > 0) cout << \" \";\n    cout << assignment[i] + 1;\n  }\n  cout << \"\\n\";\n  return 0;\n}","ahc019":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MAX_D = 14;\nint D;\nvector<string> f[2], r[2];\nvector<vector<vector<int>>> grid[2]; // grid[i][x][y][z]\nvector<int> b[2]; // flattened b[i], size D*D*D\n\n// Directions for 6-connectivity (face-adjacent)\nconst int dx[6] = {1, -1, 0, 0, 0, 0};\nconst int dy[6] = {0, 0, 1, -1, 0, 0};\nconst int dz[6] = {0, 0, 0, 0, 1, -1};\n\n// Canonicalize a shape: translate to (0,0,0), try 24 rotations, pick lex smallest\nstring canonicalize(const vector<tuple<int,int,int>>& shape) {\n    if (shape.empty()) return \"\";\n    // Find min coords\n    int minx = INT_MAX, miny = INT_MAX, minz = INT_MAX;\n    for (auto [x,y,z] : shape) {\n        minx = min(minx, x);\n        miny = min(miny, y);\n        minz = min(minz, z);\n    }\n    // Translate\n    vector<tuple<int,int,int>> trans;\n    for (auto [x,y,z] : shape) {\n        trans.emplace_back(x - minx, y - miny, z - minz);\n    }\n    // Generate 24 rotations (90 deg around x,y,z)\n    vector<string> candidates;\n    for (int rx = 0; rx < 4; ++rx) {\n        for (int ry = 0; ry < 4; ++ry) {\n            for (int rz = 0; rz < 4; ++rz) {\n                vector<tuple<int,int,int>> rot;\n                for (auto [x,y,z] : trans) {\n                    // Apply rotations\n                    int nx = x, ny = y, nz = z;\n                    for (int i = 0; i < rx; ++i) { int t = ny; ny = -nz; nz = t; } // Rotate x\n                    for (int i = 0; i < ry; ++i) { int t = nz; nz = -nx; nx = t; } // Rotate y\n                    for (int i = 0; i < rz; ++i) { int t = nx; nx = -ny; ny = t; } // Rotate z\n                    rot.emplace_back(nx, ny, nz);\n                }\n                // Sort and stringify\n                sort(rot.begin(), rot.end());\n                string s;\n                for (auto [x,y,z] : rot) {\n                    s += to_string(x) + \",\" + to_string(y) + \",\" + to_string(z) + \";\";\n                }\n                candidates.push_back(s);\n            }\n        }\n    }\n    return *min_element(candidates.begin(), candidates.end());\n}\n\n// DFS to find connected component\nvoid dfs(int i, int x, int y, int z, vector<tuple<int,int,int>>& component, vector<vector<vector<bool>>>& visited) {\n    visited[x][y][z] = true;\n    component.emplace_back(x, y, z);\n    for (int d = 0; d < 6; ++d) {\n        int nx = x + dx[d], ny = y + dy[d], nz = z + dz[d];\n        if (nx >= 0 && nx < D && ny >= 0 && ny < D && nz >= 0 && nz < D &&\n            grid[i][nx][ny][nz] && !visited[nx][ny][nz]) {\n            dfs(i, nx, ny, nz, component, visited);\n        }\n    }\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    cin >> D;\n    f[0].resize(D); f[1].resize(D);\n    r[0].resize(D); r[1].resize(D);\n    for (int i = 0; i < 2; ++i) {\n        for (int k = 0; k < D; ++k) cin >> f[i][k];\n        for (int k = 0; k < D; ++k) cin >> r[i][k];\n    }\n    \n    // Build grids\n    grid[0].assign(D, vector<vector<int>>(D, vector<int>(D, 0)));\n    grid[1].assign(D, vector<vector<int>>(D, vector<int>(D, 0)));\n    for (int i = 0; i < 2; ++i) {\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 (f[i][z][x] == '1' && r[i][z][y] == '1') {\n                        grid[i][x][y][z] = 1;\n                    }\n                }\n            }\n        }\n    }\n    \n    // Find components for each grid\n    vector<vector<vector<tuple<int,int,int>>>> components(2);\n    for (int i = 0; i < 2; ++i) {\n        vector<vector<vector<bool>>> visited(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                    if (grid[i][x][y][z] && !visited[x][y][z]) {\n                        vector<tuple<int,int,int>> comp;\n                        dfs(i, x, y, z, comp, visited);\n                        components[i].push_back(comp);\n                    }\n                }\n            }\n        }\n    }\n    \n    // Assign block IDs: unique per component, with cross-object sharing\n    map<string, int> shape_to_id;\n    int n = 0;\n    b[0].assign(D * D * D, 0);\n    b[1].assign(D * D * D, 0);\n    \n    // Process i=0: assign new IDs to each component\n    for (const auto& comp : components[0]) {\n        string shape = canonicalize(comp);\n        int id = ++n;\n        shape_to_id[shape] = id;  // Record for potential reuse in i=1\n        for (auto [x,y,z] : comp) {\n            b[0][x * D * D + y * D + z] = id;\n        }\n    }\n    \n    // Process i=1: assign unique IDs, reuse from i=0 if shape matches and ID not used yet in i=1\n    set<int> used_ids_in_1;\n    for (const auto& comp : components[1]) {\n        string shape = canonicalize(comp);\n        int id;\n        if (shape_to_id.find(shape) != shape_to_id.end()) {\n            int potential_id = shape_to_id[shape];\n            if (used_ids_in_1.find(potential_id) == used_ids_in_1.end()) {\n                id = potential_id;  // Reuse\n                used_ids_in_1.insert(id);\n            } else {\n                id = ++n;  // New, since ID already used in i=1\n            }\n        } else {\n            id = ++n;  // New\n        }\n        for (auto [x,y,z] : comp) {\n            b[1][x * D * D + y * D + z] = id;\n        }\n    }\n    \n    // Output\n    cout << n << '\\n';\n    for (int val : b[0]) cout << val << ' ';\n    cout << '\\n';\n    for (int val : b[1]) cout << val << ' ';\n    cout << '\\n';\n    \n    return 0;\n}","ahc020":"#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing ld = long double;\n\nstruct Edge {\n    int u, v, w, idx;\n    bool operator<(const Edge& other) const {\n        return w < other.w;\n    }\n};\n\nint find(vector<int>& parent, int x) {\n    if (parent[x] != x) parent[x] = find(parent, parent[x]);\n    return parent[x];\n}\n\nvoid unite(vector<int>& parent, vector<int>& rank, int x, int y) {\n    int px = find(parent, x), py = find(parent, y);\n    if (px != py) {\n        if (rank[px] < rank[py]) swap(px, py);\n        parent[py] = px;\n        if (rank[px] == rank[py]) rank[px]++;\n    }\n}\n\nld compute_dist(pair<int, int> a, pair<int, int> b) {\n    ld dx = a.first - b.first;\n    ld dy = a.second - b.second;\n    return hypot(dx, dy);\n}\n\nll compute_cost(const vector<int>& P, const vector<int>& B, const vector<Edge>& edges) {\n    ll s = 0;\n    for (int p : P) s += (ll)p * p;\n    for (size_t i = 0; i < B.size(); ++i) {\n        if (B[i]) s += edges[i].w;\n    }\n    return s;\n}\n\nbool is_covered(int k, const vector<pair<int, int>>& residents, const vector<pair<int, int>>& stations, const vector<int>& P, const vector<int>& reachable) {\n    for (int i : reachable) {\n        if (P[i] > 0 && compute_dist(residents[k], stations[i]) <= P[i]) return true;\n    }\n    return false;\n}\n\nvector<int> get_reachable(int start, int N, const vector<Edge>& edges, const vector<int>& B) {\n    vector<vector<int>> adj(N);\n    for (auto& e : edges) {\n        if (B[e.idx]) {\n            adj[e.u].push_back(e.v);\n            adj[e.v].push_back(e.u);\n        }\n    }\n    vector<int> reachable;\n    vector<bool> vis(N, false);\n    queue<int> q;\n    q.push(start);\n    vis[start] = true;\n    while (!q.empty()) {\n        int u = q.front(); q.pop();\n        reachable.push_back(u);\n        for (int v : adj[u]) {\n            if (!vis[v]) {\n                vis[v] = true;\n                q.push(v);\n            }\n        }\n    }\n    return reachable;\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    vector<pair<int, int>> stations(N);\n    for (int i = 0; i < N; ++i) {\n        cin >> stations[i].first >> stations[i].second;\n    }\n    vector<Edge> edges(M);\n    for (int i = 0; i < M; ++i) {\n        cin >> edges[i].u >> edges[i].v >> edges[i].w;\n        edges[i].u--; edges[i].v--; // 0-based\n        edges[i].idx = i;\n    }\n    vector<pair<int, int>> residents(K);\n    for (int i = 0; i < K; ++i) {\n        cin >> residents[i].first >> residents[i].second;\n    }\n\n    // Precompute distances\n    vector<vector<ld>> dist(K, vector<ld>(N));\n    for (int k = 0; k < K; ++k) {\n        for (int i = 0; i < N; ++i) {\n            dist[k][i] = compute_dist(residents[k], stations[i]);\n        }\n    }\n\n    // Precompute top-3 candidates per resident\n    vector<vector<pair<ld, int>>> candidates(K);\n    for (int k = 0; k < K; ++k) {\n        vector<pair<ld, int>> cands;\n        for (int i = 0; i < N; ++i) {\n            cands.emplace_back(dist[k][i], i);\n        }\n        sort(cands.begin(), cands.end());\n        for (int j = 0; j < min(3, N); ++j) {\n            candidates[k].push_back(cands[j]);\n        }\n    }\n\n    // Initial assignment\n    vector<int> closest(K);\n    for (int k = 0; k < K; ++k) {\n        closest[k] = candidates[k][0].second;\n    }\n\n    // Active stations\n    set<int> active_stations;\n    for (int k = 0; k < K; ++k) active_stations.insert(closest[k]);\n    active_stations.insert(0); // Vertex 1\n\n    // Compute MST\n    vector<int> parent(N), rankk(N, 0);\n    for (int i = 0; i < N; ++i) parent[i] = i;\n    sort(edges.begin(), edges.end());\n    vector<int> B(M, 0);\n    for (auto& e : edges) {\n        if (active_stations.count(e.u) && active_stations.count(e.v)) {\n            int pu = find(parent, e.u), pv = find(parent, e.v);\n            if (pu != pv) {\n                unite(parent, rankk, e.u, e.v);\n                B[e.idx] = 1;\n            }\n        }\n    }\n\n    // Set initial P_i\n    vector<int> P(N, 0);\n    vector<vector<ld>> assigned_dists(N);\n    for (int k = 0; k < K; ++k) {\n        int i = closest[k];\n        assigned_dists[i].push_back(dist[k][i]);\n    }\n    for (int i = 0; i < N; ++i) {\n        if (!assigned_dists[i].empty()) {\n            ld max_d = *max_element(assigned_dists[i].begin(), assigned_dists[i].end());\n            P[i] = min(5000, (int)ceil(max_d));\n        }\n    }\n\n    // Simplified local search (no annealing, no MST in loop)\n    ll current_cost = compute_cost(P, B, edges);\n    const int max_iters = 100;  // Reduced\n    for (int iter = 0; iter < max_iters; ++iter) {\n        bool improved = false;\n        for (int k = 0; k < K; ++k) {\n            int current_i = closest[k];\n            ld current_d = dist[k][current_i];\n            for (auto& cand : candidates[k]) {\n                ld next_d = cand.first;\n                int next_i = cand.second;\n                if (next_d > 5000 || next_i == current_i) continue;\n\n                // Simulate P_i change only\n                vector<int> new_P = P;\n                vector<vector<ld>> new_assigned = assigned_dists;\n\n                // Remove\n                new_assigned[current_i].erase(remove(new_assigned[current_i].begin(), new_assigned[current_i].end(), current_d), new_assigned[current_i].end());\n                if (new_assigned[current_i].empty()) {\n                    new_P[current_i] = 0;\n                } else {\n                    ld max_d = *max_element(new_assigned[current_i].begin(), new_assigned[current_i].end());\n                    new_P[current_i] = min(5000, (int)ceil(max_d));\n                }\n\n                // Add\n                new_assigned[next_i].push_back(next_d);\n                ld max_d_next = *max_element(new_assigned[next_i].begin(), new_assigned[next_i].end());\n                new_P[next_i] = min(5000, (int)ceil(max_d_next));\n\n                ll new_cost = 0;\n                for (int p : new_P) new_cost += (ll)p * p;\n                // Assume B cost unchanged for simplicity\n\n                if (new_cost < current_cost) {\n                    closest[k] = next_i;\n                    P = new_P;\n                    assigned_dists = new_assigned;\n                    current_cost = new_cost;\n                    improved = true;\n                } else {\n                    // Revert\n                    new_assigned[current_i].push_back(current_d);\n                    new_assigned[next_i].pop_back();\n                }\n            }\n        }\n        if (!improved) break;\n    }\n\n    // Recompute MST once at the end\n    active_stations.clear();\n    for (int k = 0; k < K; ++k) active_stations.insert(closest[k]);\n    active_stations.insert(0);\n    fill(B.begin(), B.end(), 0);\n    vector<int> new_parent(N), new_rankk(N, 0);\n    for (int i = 0; i < N; ++i) new_parent[i] = i;\n    for (auto& e : edges) {\n        if (active_stations.count(e.u) && active_stations.count(e.v)) {\n            int pu = find(new_parent, e.u), pv = find(new_parent, e.v);\n            if (pu != pv) {\n                unite(new_parent, new_rankk, e.u, e.v);\n                B[e.idx] = 1;\n            }\n        }\n    }\n\n    // Coverage check and fix\n    vector<int> reachable = get_reachable(0, N, edges, B);\n    for (int k = 0; k < K; ++k) {\n        if (!is_covered(k, residents, stations, P, reachable)) {\n            ld min_d = 1e18;\n            int best_i = -1;\n            for (int i : reachable) {\n                if (P[i] > 0 && dist[k][i] < min_d) {\n                    min_d = dist[k][i];\n                    best_i = i;\n                }\n            }\n            if (best_i != -1) {\n                int old_i = closest[k];\n                closest[k] = best_i;\n                assigned_dists[old_i].erase(remove(assigned_dists[old_i].begin(), assigned_dists[old_i].end(), dist[k][old_i]), assigned_dists[old_i].end());\n                assigned_dists[best_i].push_back(dist[k][best_i]);\n                ld max_d = *max_element(assigned_dists[best_i].begin(), assigned_dists[best_i].end());\n                P[best_i] = min(5000, (int)ceil(max_d));\n                if (assigned_dists[old_i].empty()) P[old_i] = 0;\n            }\n        }\n    }\n\n    // Output\n    for (int i = 0; i < N; ++i) {\n        if (i > 0) cout << \" \";\n        cout << P[i];\n    }\n    cout << \"\\n\";\n    for (int i = 0; i < M; ++i) {\n        if (i > 0) cout << \" \";\n        cout << B[i];\n    }\n    cout << \"\\n\";\n\n    return 0;\n}","ahc021":"#include <bits/stdc++.h>\n\nusing namespace std;\n\nint main() {\n    vector<vector<int>> pyramid(30);\n    for (int x = 0; x < 30; x++) {\n        pyramid[x].resize(x + 1);\n        for (int y = 0; y <= x; y++) {\n            cin >> pyramid[x][y];\n        }\n    }\n\n    vector<tuple<int, int, int, int>> ops;\n\n    // Recursive lambda for sift_down\n    auto sift_down = [&](auto& self, int x, int y) -> void {\n        if (x >= 29) return;\n        int val = pyramid[x][y];\n        int lx = x + 1, ly = y;\n        int rx = x + 1, ry = y + 1;\n        int lval = pyramid[lx][ly];\n        int rval = pyramid[rx][ry];\n        if (lval >= val && rval >= val) return;\n        if (lval < val && lval <= rval) {\n            // Swap with left child\n            swap(pyramid[x][y], pyramid[lx][ly]);\n            ops.push_back({x, y, lx, ly});\n            self(self, lx, ly);\n        } else if (rval < val) {\n            // Swap with right child\n            swap(pyramid[x][y], pyramid[rx][ry]);\n            ops.push_back({x, y, rx, ry});\n            self(self, rx, ry);\n        }\n    };\n\n    // Build the heap by heapifying from bottom to top\n    for (int x = 28; x >= 0; x--) {\n        for (int y = 0; y <= x; y++) {\n            sift_down(sift_down, x, y);\n        }\n    }\n\n    // Output the result\n    cout << ops.size() << endl;\n    for (auto [a, b, c, d] : ops) {\n        cout << a << \" \" << b << \" \" << c << \" \" << d << endl;\n    }\n\n    return 0;\n}","toyota2023summer-final":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int D, N;\n    cin >> D >> N;\n    vector<vector<int>> obs(D, vector<int>(D, 0));\n    for (int k = 0; k < N; ++k) {\n        int ri, rj;\n        cin >> ri >> rj;\n        obs[ri][rj] = 1;\n    }\n    int entrance_i = 0, entrance_j = (D - 1) / 2;\n    obs[entrance_i][entrance_j] = 1; // Block entrance for containers\n\n    // Precompute distances from entrance, avoiding obstacles\n    vector<vector<int>> dist(D, vector<int>(D, -1));\n    queue<pair<int, int>> q;\n    q.push({entrance_i, entrance_j});\n    dist[entrance_i][entrance_j] = 0;\n    vector<pair<int, int>> dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};\n    while (!q.empty()) {\n        auto [i, j] = q.front();\n        q.pop();\n        for (auto [di, dj] : dirs) {\n            int ni = i + di, nj = j + dj;\n            if (ni >= 0 && ni < D && nj >= 0 && nj < D && obs[ni][nj] == 0 && dist[ni][nj] == -1) {\n                dist[ni][nj] = dist[i][j] + 1;\n                q.push({ni, nj});\n            }\n        }\n    }\n\n    int M = D * D - 1 - N; // Number of containers\n    vector<vector<int>> placed(D, vector<int>(D, -1)); // -1: empty, else: container number\n\n    // Placement phase\n    for (int d = 0; d < M; ++d) {\n        int t_d;\n        cin >> t_d;\n        // BFS to find reachable empty squares\n        vector<vector<int>> vis(D, vector<int>(D, 0));\n        queue<pair<int, int>> qq;\n        qq.push({entrance_i, entrance_j});\n        vis[entrance_i][entrance_j] = 1;\n        vector<tuple<int, int, int>> candidates; // dist, i, j\n        while (!qq.empty()) {\n            auto [i, j] = qq.front();\n            qq.pop();\n            for (auto [di, dj] : dirs) {\n                int ni = i + di, nj = j + dj;\n                if (ni >= 0 && ni < D && nj >= 0 && nj < D && obs[ni][nj] == 0 && placed[ni][nj] == -1 && !vis[ni][nj]) {\n                    vis[ni][nj] = 1;\n                    qq.push({ni, nj});\n                    candidates.emplace_back(-dist[ni][nj], ni, nj); // Negative for max-heap effect\n                }\n            }\n        }\n        // Choose the candidate with max dist, then min i, min j\n        sort(candidates.begin(), candidates.end()); // Sorts by -dist, then i, then j\n        auto [_, pi, pj] = candidates[0];\n        placed[pi][pj] = t_d;\n        cout << pi << \" \" << pj << endl;\n    }\n\n    // Removal phase\n    vector<pair<int, int>> order;\n    while (true) {\n        // BFS to find accessible containers\n        vector<vector<int>> vis(D, vector<int>(D, 0));\n        queue<pair<int, int>> qq;\n        qq.push({entrance_i, entrance_j});\n        vis[entrance_i][entrance_j] = 1;\n        vector<pair<int, int>> acc; // {number, id} where id = i*D + j\n        while (!qq.empty()) {\n            auto [i, j] = qq.front();\n            qq.pop();\n            for (auto [di, dj] : dirs) {\n                int ni = i + di, nj = j + dj;\n                if (ni >= 0 && ni < D && nj >= 0 && nj < D && obs[ni][nj] == 0 && placed[ni][nj] == -1 && !vis[ni][nj]) {\n                    vis[ni][nj] = 1;\n                    qq.push({ni, nj});\n                    if (placed[ni][nj] != -1) {\n                        acc.emplace_back(placed[ni][nj], ni * D + nj);\n                    }\n                }\n            }\n        }\n        if (acc.empty()) break;\n        // Remove the smallest number\n        sort(acc.begin(), acc.end());\n        auto [num, id] = acc[0];\n        int ii = id / D, jj = id % D;\n        order.emplace_back(ii, jj);\n        placed[ii][jj] = -1;\n    }\n\n    // Output removal order\n    for (auto [qi, qj] : order) {\n        cout << qi << \" \" << qj << endl;\n    }\n\n    return 0;\n}","ahc024":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int n, m;\n    cin >> n >> m;\n    vector<vector<int>> c(n, vector<int>(n));\n    for(int i = 0; i < n; i++) {\n        for(int j = 0; j < n; j++) {\n            cin >> c[i][j];\n        }\n    }\n    \n    vector<vector<int>> adj(m + 1, vector<int>(m + 1, 0));\n    vector<int> is_boundary(m + 1, 0);\n    vector<int> di = {0, 1, 0, -1};\n    vector<int> dj = {1, 0, -1, 0};\n    \n    // Compute adjacencies and boundary info\n    for(int i = 0; i < n; i++) {\n        for(int j = 0; j < n; j++) {\n            int col = c[i][j];\n            if(i == 0 || i == n - 1 || j == 0 || j == n - 1) {\n                is_boundary[col] = 1;\n                adj[col][0] = adj[0][col] = 1;\n            }\n            for(int d = 0; d < 4; d++) {\n                int ni = i + di[d], nj = j + dj[d];\n                if(ni >= 0 && ni < n && nj >= 0 && nj < n) {\n                    int ncol = c[ni][nj];\n                    if(ncol != col) {\n                        adj[col][ncol] = adj[ncol][col] = 1;\n                    }\n                }\n            }\n        }\n    }\n    \n    // Function to validate the grid\n    auto validate = [&](const vector<vector<int>>& grid) -> bool {\n        // Check connectivity for each ward\n        for(int cc = 1; cc <= m; cc++) {\n            vector<vector<bool>> visited(n, vector<bool>(n, false));\n            bool found = false;\n            function<void(int, int)> dfs = [&](int i, int j) {\n                if(i < 0 || i >= n || j < 0 || j >= n || visited[i][j] || grid[i][j] != cc) return;\n                visited[i][j] = true;\n                found = true;\n                for(int d = 0; d < 4; d++) dfs(i + di[d], j + dj[d]);\n            };\n            for(int i = 0; i < n && !found; i++) {\n                for(int j = 0; j < n && !found; j++) {\n                    if(grid[i][j] == cc) dfs(i, j);\n                }\n            }\n            if(!found) return false;\n        }\n        // Check adjacencies\n        vector<vector<int>> new_adj(m + 1, vector<int>(m + 1, 0));\n        for(int i = 0; i < n; i++) {\n            for(int j = 0; j < n; j++) {\n                int col = grid[i][j];\n                for(int d = 0; d < 4; d++) {\n                    int ni = i + di[d], nj = j + dj[d];\n                    if(ni >= 0 && ni < n && nj >= 0 && nj < n) {\n                        int ncol = grid[ni][nj];\n                        if(ncol != col) {\n                            new_adj[col][ncol] = new_adj[ncol][col] = 1;\n                        }\n                    } else if(col != 0) {\n                        // Adjacent to outside 0\n                        new_adj[col][0] = new_adj[0][col] = 1;\n                    }\n                }\n            }\n        }\n        for(int a = 0; a <= m; a++) {\n            for(int b = 0; b <= m; b++) {\n                if(new_adj[a][b] != adj[a][b]) return false;\n            }\n        }\n        return true;\n    };\n    \n    // Prepare positions sorted by distance to center\n    int center_i = n / 2, center_j = n / 2;\n    vector<pair<int, int>> positions;\n    for(int i = 0; i < n; i++) {\n        for(int j = 0; j < n; j++) {\n            int dist = abs(i - center_i) + abs(j - center_j);\n            positions.emplace_back(dist, i * n + j);\n        }\n    }\n    sort(positions.begin(), positions.end());\n    \n    // Separate boundary and internal positions\n    vector<pair<int, int>> boundary_pos, internal_pos;\n    for(auto& p : positions) {\n        int idx = p.second;\n        int i = idx / n, j = idx % n;\n        bool on_bound = (i == 0 || i == n - 1 || j == 0 || j == n - 1);\n        if(on_bound) boundary_pos.push_back(p);\n        else internal_pos.push_back(p);\n    }\n    \n    // Best grid tracking, start with original\n    vector<vector<int>> best_grid = c;\n    int best_score = 0;\n    for(auto& row : c) for(int val : row) if(val == 0) best_score++;\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n    \n    // Multiple attempts\n    const int max_attempts = 20;\n    for(int attempt = 0; attempt < max_attempts; attempt++) {\n        // Sort wards by is_boundary descending, then shuffle\n        vector<pair<int, int>> order;\n        for(int cc = 1; cc <= m; cc++) order.emplace_back(is_boundary[cc], cc);\n        sort(order.rbegin(), order.rend());\n        vector<int> wards;\n        for(auto& p : order) wards.push_back(p.second);\n        shuffle(wards.begin(), wards.end(), rng);\n        \n        // Grid for this attempt\n        vector<vector<int>> grid(n, vector<int>(n, 0));\n        vector<set<pair<int, int>>> ward_cells(m + 1);\n        vector<bool> used(n * n, false);\n        bool valid_attempt = true;\n        \n        // Place wards\n        for(int cc : wards) {\n            bool placed = false;\n            vector<pair<int, int>> cand = is_boundary[cc] ? positions : internal_pos;\n            // Try single cell\n            for(auto& p : cand) {\n                int idx = p.second;\n                int i = idx / n, j = idx % n;\n                if(used[idx]) continue;\n                bool on_bound = (i == 0 || i == n - 1 || j == 0 || j == n - 1);\n                if(is_boundary[cc]) {\n                    if(!on_bound) continue;  // Must be on boundary for single cell\n                } else {\n                    if(on_bound) continue;  // Must not be on boundary\n                }\n                bool ok = true;\n                for(int d = 1; d <= m; d++) {\n                    if(ward_cells[d].empty()) continue;\n                    for(auto& cell : ward_cells[d]) {\n                        int pi = cell.first, pj = cell.second;\n                        int dist = abs(i - pi) + abs(j - pj);\n                        if(adj[cc][d]) {\n                            if(dist > 1) ok = false;\n                        } else {\n                            if(dist == 1) ok = false;\n                        }\n                    }\n                }\n                if(ok) {\n                    ward_cells[cc].insert({i, j});\n                    used[idx] = true;\n                    grid[i][j] = cc;\n                    placed = true;\n                    break;\n                }\n            }\n            if(!placed) {\n                // Try 2-cell expansion\n                for(auto& p : cand) {\n                    int idx = p.second;\n                    int i = idx / n, j = idx % n;\n                    if(used[idx]) continue;\n                    bool on_bound1 = (i == 0 || i == n - 1 || j == 0 || j == n - 1);\n                    if(is_boundary[cc]) {\n                        if(!on_bound1) continue;  // First cell must be on boundary for boundary wards\n                    } else {\n                        if(on_bound1) continue;  // First cell must not be on boundary for internal\n                    }\n                    for(int d = 0; d < 4; d++) {\n                        int ni = i + di[d], nj = j + dj[d];\n                        if(ni >= 0 && ni < n && nj >= 0 && nj < n && !used[ni * n + nj] && grid[ni][nj] == 0) {\n                            bool on_bound2 = (ni == 0 || ni == n - 1 || nj == 0 || nj == n - 1);\n                            set<pair<int, int>> temp_cells = {{i, j}, {ni, nj}};\n                            bool shape_ok = true;\n                            bool has_bound = on_bound1 || on_bound2;\n                            if(is_boundary[cc]) {\n                                if(!has_bound) shape_ok = false;  // At least one on boundary\n                            } else {\n                                if(has_bound) shape_ok = false;  // None on boundary\n                            }\n                            if(!shape_ok) continue;\n                            bool ok = true;\n                            for(int dd = 1; dd <= m; dd++) {\n                                if(ward_cells[dd].empty()) continue;\n                                for(auto& cell : ward_cells[dd]) {\n                                    for(auto& tcell : temp_cells) {\n                                        int pi = cell.first, pj = cell.second;\n                                        int dist = abs(tcell.first - pi) + abs(tcell.second - pj);\n                                        if(adj[cc][dd]) {\n                                            if(dist > 1) ok = false;\n                                        } else {\n                                            if(dist == 1) ok = false;\n                                        }\n                                    }\n                                }\n                            }\n                            if(ok) {\n                                ward_cells[cc] = temp_cells;\n                                used[idx] = true;\n                                used[ni * n + nj] = true;\n                                grid[i][j] = cc;\n                                grid[ni][nj] = cc;\n                                placed = true;\n                                break;\n                            }\n                        }\n                    }\n                    if(placed) break;\n                }\n            }\n            if(!placed) {\n                valid_attempt = false;\n                break;\n            }\n        }\n        \n        if(valid_attempt && validate(grid)) {\n            int score = 0;\n            for(auto& row : grid) for(int val : row) if(val == 0) score++;\n            if(score > best_score) {\n                best_score = score;\n                best_grid = grid;\n            }\n        }\n    }\n    \n    // Output the best valid grid\n    for(int i = 0; i < n; i++) {\n        for(int j = 0; j < n; j++) {\n            if(j > 0) cout << \" \";\n            cout << best_grid[i][j];\n        }\n        cout << \"\\n\";\n    }\n    \n    return 0;\n}","ahc025":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int N, D, Q;\n    cin >> N >> D >> Q;\n    \n    vector<int> items(N);\n    iota(items.begin(), items.end(), 0);  // items[i] = i\n    \n    int query_count = 0;\n    \n    // Comparator for sorting: compares weights of two items using a query\n    auto cmp = [&](int a, int b) -> bool {\n        cout << 1 << \" \" << 1 << \" \" << a << \" \" << b << endl;\n        cout.flush();\n        string res;\n        cin >> res;\n        query_count++;\n        if (res == \"<\") return true;   // a < b\n        if (res == \">\") return false;  // a > b\n        return true;  // a == b, arbitrary\n    };\n    \n    // Sort items by weight (lightest to heaviest)\n    sort(items.begin(), items.end(), cmp);\n    \n    // Assign groups using round-robin on the sorted list\n    vector<int> group(N);\n    vector<vector<int>> groups(D);\n    vector<int> pos(N);\n    for (int i = 0; i < N; i++) {\n        pos[items[i]] = i;  // pos[item] = position in sorted list\n        int g = i % D;\n        group[items[i]] = g;\n        groups[g].push_back(items[i]);\n    }\n    \n    // Refinements: Balance groups using remaining queries\n    int g1 = 0, g2 = 1;\n    while (query_count < Q) {\n        if (groups[g1].empty() || groups[g2].empty()) {\n            // Skip if a group is empty; do dummy query\n            cout << 1 << \" \" << 1 << \" \" << 0 << \" \" << 1 << endl;\n            cout.flush();\n            string dummy;\n            cin >> dummy;\n            query_count++;\n            continue;\n        }\n        \n        // Compare sums of groups g1 and g2\n        cout << groups[g1].size() << \" \" << groups[g2].size();\n        for (int x : groups[g1]) cout << \" \" << x;\n        for (int x : groups[g2]) cout << \" \" << x;\n        cout << endl;\n        cout.flush();\n        string res;\n        cin >> res;\n        query_count++;\n        \n        if (res == \">\") {  // groups[g1] > groups[g2]\n            // Move heaviest item from g1 to g2\n            auto it = max_element(groups[g1].begin(), groups[g1].end(), [&](int a, int b) {\n                return pos[a] < pos[b];\n            });\n            int item = *it;\n            groups[g1].erase(it);\n            groups[g2].push_back(item);\n            group[item] = g2;\n        } else if (res == \"<\") {  // groups[g1] < groups[g2]\n            // Move heaviest item from g2 to g1\n            auto it = max_element(groups[g2].begin(), groups[g2].end(), [&](int a, int b) {\n                return pos[a] < pos[b];\n            });\n            int item = *it;\n            groups[g2].erase(it);\n            groups[g1].push_back(item);\n            group[item] = g1;\n        }  // If ==, do nothing\n        \n        // Cycle to next pair of groups\n        g2 = (g2 + 1) % D;\n        if (g2 == g1) g2 = (g2 + 1) % D;\n    }\n    \n    // Output the group assignment\n    for (int i = 0; i < N; i++) {\n        if (i > 0) cout << \" \";\n        cout << group[i];\n    }\n    cout << endl;\n    \n    return 0;\n}","ahc026":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    int n, m;\n    cin >> n >> m;\n    vector<deque<int>> stacks(m);\n    vector<int> position(n + 1);\n    for (int i = 0; i < m; i++) {\n        for (int j = 0; j < n / m; j++) {\n            int box;\n            cin >> box;\n            stacks[i].push_back(box);\n            position[box] = i;\n        }\n    }\n    vector<bool> removed(n + 1, false);\n    \n    int next_temp = 0;  // Counter for cyclic stack selection\n    for (int v = 1; v <= n; v++) {\n        if (removed[v]) continue;\n        int s = position[v];\n        // Find index of v in stacks[s]\n        int idx = -1;\n        for (int j = 0; j < stacks[s].size(); j++) {\n            if (stacks[s][j] == v) {\n                idx = j;\n                break;\n            }\n        }\n        int h = stacks[s].size() - 1 - idx;  // Boxes above v\n        while (h > 0) {\n            // Move the top h boxes: Find the box at stacks[s][size - h]\n            int move_idx = stacks[s].size() - h;\n            int box_to_move = stacks[s][move_idx];\n            // Choose destination t: Cycle through stacks, skip s\n            int t = next_temp;\n            next_temp = (next_temp + 1) % m;\n            if (t == s) {\n                t = next_temp;\n                next_temp = (next_temp + 1) % m;\n            }\n            // Perform move: Extract from move_idx to end\n            deque<int> to_move;\n            for (size_t k = move_idx; k < stacks[s].size(); ++k) {\n                to_move.push_back(stacks[s][k]);\n            }\n            stacks[s].erase(stacks[s].begin() + move_idx, stacks[s].end());\n            // Append to t\n            for (int bx : to_move) {\n                stacks[t].push_back(bx);\n                position[bx] = t;\n            }\n            // Record move operation\n            printf(\"%d %d\\n\", box_to_move, t + 1);\n            // Since we moved the top h, v is now at top of s, so h = 0\n            h = 0;\n        }\n        // Now h == 0, remove v\n        printf(\"%d %d\\n\", v, 0);\n        removed[v] = true;\n        // Remove from stack\n        stacks[s].pop_back();\n    }\n    return 0;\n}","ahc027":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int N;\n    cin >> N;\n    vector<string> h(N-1);\n    for(int i=0; i<N-1; i++) cin >> h[i];\n    vector<string> v(N);\n    for(int i=0; i<N; i++) cin >> v[i];\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    vector<vector<bool>> visited(N, vector<bool>(N, false));\n    vector<pair<int,int>> DIJ = {{0,1},{1,0},{0,-1},{-1,0}};\n    string DIR = \"RDLU\";\n    \n    function<void(int,int)> dfs = [&](int i, int j) {\n        visited[i][j] = true;\n        vector<tuple<int, int, int, int>> candidates; // d, dir, i2, j2\n        for(int dir=0; dir<4; dir++) {\n            int di = DIJ[dir].first, dj = DIJ[dir].second;\n            int i2 = i + di, j2 = j + dj;\n            if(i2 >= 0 && i2 < N && j2 >= 0 && j2 < N && !visited[i2][j2]) {\n                bool can = false;\n                if(di == 0) { // horizontal\n                    int jj = min(j, j2);\n                    if(v[i][jj] == '0') can = true;\n                } else { // vertical\n                    int ii = min(i, i2);\n                    if(h[ii][j] == '0') can = true;\n                }\n                if(can) {\n                    candidates.emplace_back(d[i2][j2], dir, i2, j2);\n                }\n            }\n        }\n        sort(candidates.rbegin(), candidates.rend()); // sort by d descending\n        for(auto& [_, dir, i2, j2] : candidates) {\n            cout << DIR[dir];\n            dfs(i2, j2);\n            cout << DIR[(dir + 2) % 4];\n        }\n    };\n    \n    dfs(0, 0);\n    cout << endl;\n    return 0;\n}","ahc028":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int N, M;\n    cin >> N >> M;\n    int si, sj;\n    cin >> si >> sj;\n    vector<vector<char>> A(N, vector<char>(N));\n    for (int i = 0; i < N; i++) {\n        string row;\n        cin >> row;\n        for (int j = 0; j < N; j++) {\n            A[i][j] = row[j];\n        }\n    }\n    vector<string> t(M);\n    for (int k = 0; k < M; k++) {\n        cin >> t[k];\n    }\n\n    // Precompute hashes for t_k\n    const unsigned long long BASE = 31;\n    vector<unsigned long long> t_hash(M);\n    for (int k = 0; k < M; k++) {\n        unsigned long long h = 0;\n        for (char c : t[k]) {\n            h = h * BASE + (c - 'A' + 1);\n        }\n        t_hash[k] = h;\n    }\n\n    // Random number generator\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n\n    // Initial state\n    int cur_i = si, cur_j = sj;\n    string S = \"\";\n    vector<bool> covered(M, false);\n    int covered_count = 0;\n    unordered_set<unsigned long long> hashes;\n    vector<pair<int, int>> operations;\n    long long total_cost = 0;\n\n    // Main loop\n    while (operations.size() < 5000 && covered_count < M) {\n        int best_new = -1;\n        long long best_cost = LLONG_MAX;\n        pair<int, int> best_pos = {-1, -1};\n\n        for (int ii = 0; ii < N; ii++) {\n            for (int jj = 0; jj < N; jj++) {\n                long long cost = abs(ii - cur_i) + abs(jj - cur_j) + 1;\n                // Penalty for staying to encourage exploration\n                if (ii == cur_i && jj == cur_j) {\n                    cost += 1000;\n                }\n                string temp_S = S + A[ii][jj];\n\n                // Compute new hash if possible\n                bool has_new = (temp_S.size() >= 5);\n                unsigned long long new_h = 0;\n                if (has_new) {\n                    string sub = temp_S.substr(temp_S.size() - 5, 5);\n                    for (char c : sub) {\n                        new_h = new_h * BASE + (c - 'A' + 1);\n                    }\n                }\n\n                // Count new covered\n                int new_cov = 0;\n                for (int k = 0; k < M; k++) {\n                    if (!covered[k]) {\n                        bool is_covered = hashes.count(t_hash[k]);\n                        if (!is_covered && has_new && new_h == t_hash[k]) {\n                            is_covered = true;\n                        }\n                        if (is_covered) {\n                            new_cov++;\n                        }\n                    }\n                }\n\n                // Update best\n                bool better = false;\n                if (new_cov > best_new) {\n                    better = true;\n                } else if (new_cov == best_new) {\n                    if (cost < best_cost) {\n                        better = true;\n                    } else if (cost == best_cost) {\n                        // Random tie-breaker\n                        if (rng() % 2 == 0) {\n                            better = true;\n                        }\n                    }\n                }\n                if (better) {\n                    best_new = new_cov;\n                    best_cost = cost;\n                    best_pos = {ii, jj};\n                }\n            }\n        }\n\n        // If no valid move, break (shouldn't happen)\n        if (best_pos.first == -1) break;\n\n        // Perform the move\n        int ii = best_pos.first, jj = best_pos.second;\n        operations.push_back({ii, jj});\n        total_cost += (abs(ii - cur_i) + abs(jj - cur_j) + 1);  // Actual cost without penalty\n        S += A[ii][jj];\n        cur_i = ii;\n        cur_j = jj;\n\n        // Update hashes\n        if (S.size() >= 5) {\n            string sub = S.substr(S.size() - 5, 5);\n            unsigned long long h = 0;\n            for (char c : sub) {\n                h = h * BASE + (c - 'A' + 1);\n            }\n            hashes.insert(h);\n        }\n\n        // Update covered\n        for (int k = 0; k < M; k++) {\n            if (!covered[k] && hashes.count(t_hash[k])) {\n                covered[k] = true;\n                covered_count++;\n            }\n        }\n    }\n\n    // Output\n    for (auto& op : operations) {\n        cout << op.first << \" \" << op.second << endl;\n    }\n\n    return 0;\n}","ahc030":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int N, M;\n    double eps;\n    cin >> N >> M >> eps;\n    for (int k = 0; k < M; ++k) {\n        int d;\n        cin >> d;\n        for (int i = 0; i < d; ++i) {\n            int x, y;\n            cin >> x >> y;\n        }\n    }\n\n    set<pair<int, int>> oil_set;\n    vector<pair<int, int>> assumed_zero;\n    const int BLOCK_SIZE = 3;\n\n    for (int i = 0; i < N; i += BLOCK_SIZE) {\n        for (int j = 0; j < N; j += BLOCK_SIZE) {\n            vector<pair<int, int>> block;\n            for (int x = i; x < min(i + BLOCK_SIZE, N); ++x) {\n                for (int y = j; y < min(j + BLOCK_SIZE, N); ++y) {\n                    block.push_back({x, y});\n                }\n            }\n            int sz = block.size();\n            cout << \"q \" << sz;\n            for (auto [a, b] : block) cout << \" \" << a << \" \" << b;\n            cout << endl;\n            cout.flush();\n            int s;\n            cin >> s;\n            if (s == 0) {\n                for (auto p : block) assumed_zero.push_back(p);\n            } else {\n                for (auto [a, b] : block) {\n                    cout << \"q 1 \" << a << \" \" << b << endl;\n                    cout.flush();\n                    int v;\n                    cin >> v;\n                    if (v > 0) oil_set.insert({a, b});\n                }\n            }\n        }\n    }\n\n    while (true) {\n        cout << \"a \" << oil_set.size();\n        for (auto [i, j] : oil_set) cout << \" \" << i << \" \" << j;\n        cout << endl;\n        cout.flush();\n        int res;\n        cin >> res;\n        if (res == 1) break;\n        // process the assumed_zero with recursive\n        vector<pair<int, int>> new_all = assumed_zero;\n        assumed_zero.clear();\n        // define process inside or use a function\n        function<void(vector<pair<int, int>>)> process = [&](vector<pair<int, int>> reg) {\n            if (reg.empty()) return;\n            int sz = reg.size();\n            if (sz <= 10) {\n                for (auto [ii, jj] : reg) {\n                    cout << \"q 1 \" << ii << \" \" << jj << endl;\n                    cout.flush();\n                    int v;\n                    cin >> v;\n                    if (v > 0) oil_set.insert({ii, jj});\n                }\n                return;\n            }\n            // divine\n            cout << \"q \" << sz;\n            for (auto [ii, jj] : reg) cout << \" \" << ii << \" \" << jj;\n            cout << endl;\n            cout.flush();\n            int ss;\n            cin >> ss;\n            if (ss == 0) {\n                for (auto p : reg) assumed_zero.push_back(p);\n                return;\n            }\n            // subdivide\n            int min_ii = INT_MAX, max_ii = INT_MIN, min_jj = INT_MAX, max_jj = INT_MIN;\n            for (auto [ii, jj] : reg) {\n                min_ii = min(min_ii, ii);\n                max_ii = max(max_ii, ii);\n                min_jj = min(min_jj, jj);\n                max_jj = max(max_jj, jj);\n            }\n            int mid_ii = (min_ii + max_ii) / 2;\n            int mid_jj = (min_jj + max_jj) / 2;\n            vector<pair<int, int>> tl, tr, bl, br;\n            for (auto p : reg) {\n                int ii = p.first, jj = p.second;\n                if (ii <= mid_ii) {\n                    if (jj <= mid_jj) tl.push_back(p);\n                    else tr.push_back(p);\n                } else {\n                    if (jj <= mid_jj) bl.push_back(p);\n                    else br.push_back(p);\n                }\n            }\n            process(tl);\n            process(tr);\n            process(bl);\n            process(br);\n        };\n        process(new_all);\n    }\n    return 0;\n}","ahc031":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int W, D, N;\n    cin >> W >> D >> N;\n    vector<vector<long long>> a(D, vector<long long>(N));\n    for (int d = 0; d < D; ++d) {\n        for (int k = 0; k < N; ++k) {\n            cin >> a[d][k];\n        }\n    }\n    \n    for (int d = 0; d < D; ++d) {\n        // Sort a[d] in descending order\n        sort(a[d].rbegin(), a[d].rend());\n        // Compute desired heights\n        vector<int> heights(N);\n        long long total_height = 0;\n        for (int k = 0; k < N; ++k) {\n            heights[k] = (a[d][k] + W - 1LL) / W;\n            total_height += heights[k];\n        }\n        // If total_height > W, reduce excess from largest heights\n        if (total_height > W) {\n            long long excess = total_height - W;\n            for (int k = 0; k < N && excess > 0; ++k) {\n                int reduce = min((long long)heights[k] - 1, excess);\n                heights[k] -= reduce;\n                excess -= reduce;\n            }\n            total_height = W;  // Ensure exact fit\n        }\n        // Place rectangles\n        int current_i = 0;\n        for (int k = 0; k < N; ++k) {\n            int i0 = current_i;\n            int j0 = 0;\n            int i1 = current_i + heights[k];\n            int j1 = W;\n            cout << i0 << \" \" << j0 << \" \" << i1 << \" \" << j1 << \"\\n\";\n            current_i += heights[k];\n        }\n    }\n    return 0;\n}","ahc032":"#include <bits/stdc++.h>\n\nusing namespace std;\nusing ll = long long;\nconst ll MOD = 998244353LL;\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<ll>> a(N, vector<ll>(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<ll>>> s(M, vector<vector<ll>>(3, vector<ll>(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 >> s[m][i][j];\n            }\n        }\n    }\n    \n    // Use mt19937 for randomness\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n    \n    // Store the best operations and score\n    vector<tuple<int, int, int>> best_operations;\n    ll best_score = LLONG_MIN;\n    \n    // Number of runs\n    const int NUM_RUNS = 25;\n    \n    for (int run = 0; run < NUM_RUNS; ++run) {\n        // Initialize current_mod for this run\n        vector<vector<ll>> current_mod(N, vector<ll>(N));\n        for (int i = 0; i < N; ++i) {\n            for (int j = 0; j < N; ++j) {\n                current_mod[i][j] = a[i][j] % MOD;\n            }\n        }\n        \n        // Operations for this run\n        vector<tuple<int, int, int>> operations;\n        \n        // Perform up to K operations\n        for (int op = 0; op < K; ++op) {\n            ll best_delta = LLONG_MIN;\n            int best_m = -1, best_p = -1, best_q = -1;\n            \n            // Create and shuffle list of all possible operations\n            vector<tuple<int, int, int>> possible_ops;\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                        possible_ops.emplace_back(m, p, q);\n                    }\n                }\n            }\n            shuffle(possible_ops.begin(), possible_ops.end(), rng);\n            \n            // Evaluate in shuffled order\n            for (auto [m, p, q] : possible_ops) {\n                ll delta = 0;\n                for (int i = 0; i < 3; ++i) {\n                    for (int j = 0; j < 3; ++j) {\n                        ll old_mod = current_mod[p + i][q + j];\n                        ll s_val = s[m][i][j];\n                        ll new_mod = (old_mod + s_val) % MOD;\n                        ll cell_delta = (new_mod - old_mod + MOD) % MOD;\n                        delta += cell_delta;\n                    }\n                }\n                if (delta > best_delta) {\n                    best_delta = delta;\n                    best_m = m;\n                    best_p = p;\n                    best_q = q;\n                }\n            }\n            \n            // If no positive delta, stop\n            if (best_delta <= 0) break;\n            \n            // Apply the best operation\n            operations.emplace_back(best_m, best_p, best_q);\n            for (int i = 0; i < 3; ++i) {\n                for (int j = 0; j < 3; ++j) {\n                    current_mod[best_p + i][best_q + j] = (current_mod[best_p + i][best_q + j] + s[best_m][i][j]) % MOD;\n                }\n            }\n        }\n        \n        // Optimized local search: Try removals, swaps, and adding one operation\n        ll current_score = 0;\n        for (int i = 0; i < N; ++i) {\n            for (int j = 0; j < N; ++j) {\n                current_score += current_mod[i][j];\n            }\n        }\n        vector<tuple<int, int, int>> improved_operations = operations;\n        \n        // Try removals\n        for (size_t idx = 0; idx < operations.size(); ++idx) {\n            vector<tuple<int, int, int>> temp_ops = operations;\n            temp_ops.erase(temp_ops.begin() + idx);\n            \n            vector<vector<ll>> temp_mod(N, vector<ll>(N));\n            for (int i = 0; i < N; ++i) {\n                for (int j = 0; j < N; ++j) {\n                    temp_mod[i][j] = a[i][j] % MOD;\n                }\n            }\n            for (auto [m, p, q] : temp_ops) {\n                for (int i = 0; i < 3; ++i) {\n                    for (int j = 0; j < 3; ++j) {\n                        temp_mod[p + i][q + j] = (temp_mod[p + i][q + j] + s[m][i][j]) % MOD;\n                    }\n                }\n            }\n            ll temp_score = 0;\n            for (int i = 0; i < N; ++i) {\n                for (int j = 0; j < N; ++j) {\n                    temp_score += temp_mod[i][j];\n                }\n            }\n            if (temp_score > current_score) {\n                current_score = temp_score;\n                improved_operations = temp_ops;\n            }\n        }\n        \n        // Try limited random swaps (20 per operation)\n        const int SWAP_SAMPLES = 20;\n        for (size_t idx = 0; idx < operations.size(); ++idx) {\n            auto [orig_m, orig_p, orig_q] = operations[idx];\n            for (int sample = 0; sample < SWAP_SAMPLES; ++sample) {\n                int new_m = rng() % M;\n                int new_p = rng() % (N - 2);\n                int new_q = rng() % (N - 2);\n                if (new_m == orig_m && new_p == orig_p && new_q == orig_q) continue;\n                \n                vector<tuple<int, int, int>> temp_ops = operations;\n                temp_ops[idx] = {new_m, new_p, new_q};\n                \n                vector<vector<ll>> temp_mod(N, vector<ll>(N));\n                for (int i = 0; i < N; ++i) {\n                    for (int j = 0; j < N; ++j) {\n                        temp_mod[i][j] = a[i][j] % MOD;\n                    }\n                }\n                for (auto [m, p, q] : temp_ops) {\n                    for (int i = 0; i < 3; ++i) {\n                        for (int j = 0; j < 3; ++j) {\n                            temp_mod[p + i][q + j] = (temp_mod[p + i][q + j] + s[m][i][j]) % MOD;\n                        }\n                    }\n                }\n                ll temp_score = 0;\n                for (int i = 0; i < N; ++i) {\n                    for (int j = 0; j < N; ++j) {\n                        temp_score += temp_mod[i][j];\n                    }\n                }\n                if (temp_score > current_score) {\n                    current_score = temp_score;\n                    improved_operations = temp_ops;\n                }\n            }\n        }\n        \n        // Try adding one operation\n        if (improved_operations.size() < K) {\n            ll best_add_delta = LLONG_MIN;\n            int best_add_m = -1, best_add_p = -1, best_add_q = -1;\n            \n            vector<vector<ll>> temp_mod = current_mod;  // Use current_mod as base\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                        ll delta = 0;\n                        for (int i = 0; i < 3; ++i) {\n                            for (int j = 0; j < 3; ++j) {\n                                ll old_mod = temp_mod[p + i][q + j];\n                                ll s_val = s[m][i][j];\n                                ll new_mod = (old_mod + s_val) % MOD;\n                                ll cell_delta = (new_mod - old_mod + MOD) % MOD;\n                                delta += cell_delta;\n                            }\n                        }\n                        if (delta > best_add_delta) {\n                            best_add_delta = delta;\n                            best_add_m = m;\n                            best_add_p = p;\n                            best_add_q = q;\n                        }\n                    }\n                }\n            }\n            if (best_add_delta > 0) {\n                improved_operations.emplace_back(best_add_m, best_add_p, best_add_q);\n                current_score += best_add_delta;\n            }\n        }\n        \n        // Update best if better\n        if (current_score > best_score) {\n            best_score = current_score;\n            best_operations = improved_operations;\n        }\n    }\n    \n    // Output the best result\n    cout << best_operations.size() << '\\n';\n    for (auto [m, p, q] : best_operations) {\n        cout << m << ' ' << p << ' ' << q << '\\n';\n    }\n    \n    return 0;\n}","ahc033":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int N;\n    cin >> N;\n    vector<vector<int>> A(N, vector<int>(N));\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cin >> A[i][j];\n        }\n    }\n    \n    // Initialize receiving queues\n    vector<queue<int>> recv_queues(N);\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            recv_queues[i].push(A[i][j]);\n        }\n    }\n    \n    // Grid: -1 no container, else container id\n    vector<vector<int>> grid(N, vector<int>(N, -1));\n    \n    // Crane positions\n    vector<pair<int, int>> crane_pos(N);\n    for (int i = 0; i < N; i++) {\n        crane_pos[i] = {i, 0};\n    }\n    \n    // Crane holdings: -1 none, else id\n    vector<int> crane_holding(N, -1);\n    \n    // Crane active\n    vector<bool> crane_active(N, true);\n    \n    // Dispatched lists\n    vector<vector<int>> dispatched(N);\n    \n    // Actions\n    vector<string> actions(N);\n    \n    // Simulation\n    const int MAX_TURNS = 10000;\n    for (int turn = 0; turn < MAX_TURNS; turn++) {\n        // Step 1: Receive\n        for (int i = 0; i < N; i++) {\n            int x = i, y = 0;\n            if (grid[x][y] == -1 && crane_holding[i] == -1 && !recv_queues[i].empty()) {\n                int cont = recv_queues[i].front();\n                recv_queues[i].pop();\n                grid[x][y] = cont;\n            }\n        }\n        \n        // Step 2: Compute actions\n        vector<char> acts(N, '.');\n        for (int c = 0; c < N; c++) {\n            if (!crane_active[c]) continue;\n            \n            // Compute goal\n            pair<int, int> goal;\n            if (crane_holding[c] != -1) {\n                int cont = crane_holding[c];\n                int target_row = cont / N;\n                if (target_row == c) {\n                    goal = {c, N - 1};\n                } else {\n                    goal = {target_row, 1};  // Buffer column\n                }\n            } else {\n                goal = {c, 0};\n            }\n            \n            // BFS for next move\n            auto get_next_move = [&](int cx, int cy, pair<int, int> g) -> char {\n                vector<vector<int>> dist(N, vector<int>(N, -1));\n                queue<pair<int, int>> q;\n                q.push({cx, cy});\n                dist[cx][cy] = 0;\n                vector<vector<pair<int, int>>> prev(N, vector<pair<int, int>>(N, {-1, -1}));\n                while (!q.empty()) {\n                    auto [x, y] = q.front();\n                    q.pop();\n                    if (x == g.first && y == g.second) break;\n                    vector<pair<int, int>> dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};\n                    for (auto [dx, dy] : dirs) {\n                        int nx = x + dx, ny = y + dy;\n                        if (nx >= 0 && nx < N && ny >= 0 && ny < N) {\n                            if (dist[nx][ny] == -1) {\n                                bool can = true;\n                                if (crane_holding[c] != -1 && c >= 1) {  // Small crane\n                                    if (grid[nx][ny] != -1) can = false;\n                                }\n                                // Check other cranes (simplified: assume no collision in BFS)\n                                for (int o = 0; o < N; o++) {\n                                    if (o != c && crane_pos[o] == make_pair(nx, ny)) can = false;\n                                }\n                                if (can) {\n                                    dist[nx][ny] = dist[x][y] + 1;\n                                    prev[nx][ny] = {x, y};\n                                    q.push({nx, ny});\n                                }\n                            }\n                        }\n                    }\n                }\n                if (dist[g.first][g.second] == -1) return '.';\n                pair<int, int> current = g;\n                while (prev[current.first][current.second] != make_pair(cx, cy)) {\n                    current = prev[current.first][current.second];\n                }\n                int dx = current.first - cx;\n                int dy = current.second - cy;\n                if (dx == -1) return 'U';\n                if (dx == 1) return 'D';\n                if (dy == -1) return 'L';\n                if (dy == 1) return 'R';\n                return '.';\n            };\n            \n            char act = get_next_move(crane_pos[c].first, crane_pos[c].second, goal);\n            \n            // Prefer P/Q if possible\n            int cx = crane_pos[c].first, cy = crane_pos[c].second;\n            if (crane_holding[c] == -1 && grid[cx][cy] != -1) {\n                act = 'P';\n            } else if (crane_holding[c] != -1 && grid[cx][cy] == -1) {\n                act = 'Q';\n            }\n            \n            acts[c] = act;\n        }\n        \n        // Execute actions with validation\n        for (int c = 0; c < N; c++) {\n            char act = acts[c];\n            int cx = crane_pos[c].first, cy = crane_pos[c].second;\n            bool valid = true;\n            if (act == 'P') {\n                if (crane_holding[c] != -1 || grid[cx][cy] == -1) valid = false;\n                else {\n                    crane_holding[c] = grid[cx][cy];\n                    grid[cx][cy] = -1;\n                }\n            } else if (act == 'Q') {\n                if (crane_holding[c] == -1 || grid[cx][cy] != -1) valid = false;\n                else {\n                    grid[cx][cy] = crane_holding[c];\n                    crane_holding[c] = -1;\n                }\n            } else if (act == 'U') {\n                int nx = cx - 1, ny = cy;\n                if (nx < 0) valid = false;\n                else {\n                    if (crane_holding[c] != -1 && c >= 1 && grid[nx][ny] != -1) valid = false;\n                    for (int o = 0; o < N; o++) {\n                        if (o != c && crane_pos[o] == make_pair(nx, ny)) valid = false;\n                    }\n                    if (valid) crane_pos[c] = {nx, ny};\n                }\n            } else if (act == 'D') {\n                int nx = cx + 1, ny = cy;\n                if (nx >= N) valid = false;\n                else {\n                    if (crane_holding[c] != -1 && c >= 1 && grid[nx][ny] != -1) valid = false;\n                    for (int o = 0; o < N; o++) {\n                        if (o != c && crane_pos[o] == make_pair(nx, ny)) valid = false;\n                    }\n                    if (valid) crane_pos[c] = {nx, ny};\n                }\n            } else if (act == 'L') {\n                int nx = cx, ny = cy - 1;\n                if (ny < 0) valid = false;\n                else {\n                    if (crane_holding[c] != -1 && c >= 1 && grid[nx][ny] != -1) valid = false;\n                    for (int o = 0; o < N; o++) {\n                        if (o != c && crane_pos[o] == make_pair(nx, ny)) valid = false;\n                    }\n                    if (valid) crane_pos[c] = {nx, ny};\n                }\n            } else if (act == 'R') {\n                int nx = cx, ny = cy + 1;\n                if (ny >= N) valid = false;\n                else {\n                    if (crane_holding[c] != -1 && c >= 1 && grid[nx][ny] != -1) valid = false;\n                    for (int o = 0; o < N; o++) {\n                        if (o != c && crane_pos[o] == make_pair(nx, ny)) valid = false;\n                    }\n                    if (valid) crane_pos[c] = {nx, ny};\n                }\n            } else if (act == 'B') {\n                if (crane_holding[c] != -1) valid = false;\n                else crane_active[c] = false;\n            }\n            if (!valid) act = '.';\n            actions[c] += act;\n        }\n        \n        // Step 3: Dispatch\n        for (int i = 0; i < N; i++) {\n            int x = i, y = N - 1;\n            if (grid[x][y] != -1) {\n                int cont = grid[x][y];\n                grid[x][y] = -1;\n                dispatched[i].push_back(cont);\n            }\n        }\n        \n        // Check if done\n        bool done = true;\n        for (int i = 0; i < N; i++) {\n            if (!recv_queues[i].empty()) done = false;\n            if (grid[i][N - 1] != -1) done = false;\n            if (crane_holding[i] != -1) done = false;\n        }\n        if (done) break;\n    }\n    \n    // Output: Pad to max length\n    size_t max_len = 0;\n    for (const auto& s : actions) max_len = max(max_len, s.size());\n    for (auto& s : actions) {\n        while (s.size() < max_len) s += '.';\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>> h(N, vector<int>(N));\n    for (int i = 0; i < N; ++i) {\n        for (int j = 0; j < N; ++j) {\n            cin >> h[i][j];\n        }\n    }\n\n    // Directions: U, D, L, R\n    vector<pair<int, int>> dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};\n    string move_str = \"UDLR\";\n\n    // Priority queues: excess (max h), deficit (max -h)\n    priority_queue<tuple<int, int, int>> pq_excess; // {h, i, j}\n    priority_queue<tuple<int, int, int>> pq_deficit; // {-h, i, j}\n    for (int i = 0; i < N; ++i) {\n        for (int j = 0; j < N; ++j) {\n            if (h[i][j] > 0) pq_excess.push({h[i][j], i, j});\n            if (h[i][j] < 0) pq_deficit.push({-h[i][j], i, j});\n        }\n    }\n\n    // Function to get path from (si,sj) to (ti,tj) using BFS\n    auto get_path = [&](int si, int sj, int ti, int tj) -> vector<string> {\n        vector<vector<int>> dist(N, vector<int>(N, -1));\n        vector<vector<pair<int, int>>> prevv(N, vector<pair<int, int>>(N, {-1, -1}));\n        queue<pair<int, int>> q;\n        q.push({si, sj});\n        dist[si][sj] = 0;\n        while (!q.empty()) {\n            auto [i, j] = q.front(); q.pop();\n            if (i == ti && j == tj) break;\n            for (int d = 0; d < 4; ++d) {\n                int ni = i + dirs[d].first, nj = j + dirs[d].second;\n                if (ni >= 0 && ni < N && nj >= 0 && nj < N && dist[ni][nj] == -1) {\n                    dist[ni][nj] = dist[i][j] + 1;\n                    prevv[ni][nj] = {i, j};\n                    q.push({ni, nj});\n                }\n            }\n        }\n        // Reconstruct path\n        vector<string> path;\n        int ci = ti, cj = tj;\n        while (ci != si || cj != sj) {\n            auto [pi, pj] = prevv[ci][cj];\n            for (int d = 0; d < 4; ++d) {\n                if (pi + dirs[d].first == ci && pj + dirs[d].second == cj) {\n                    path.push_back(string(1, move_str[d]));\n                    break;\n                }\n            }\n            ci = pi; cj = pj;\n        }\n        reverse(path.begin(), path.end());\n        return path;\n    };\n\n    // Function to find nearest deficit from (si,sj) and get path\n    auto find_nearest_deficit = [&](int si, int sj) -> pair<pair<int, int>, vector<string>> {\n        vector<vector<int>> dist(N, vector<int>(N, -1));\n        vector<vector<pair<int, int>>> prevv(N, vector<pair<int, int>>(N, {-1, -1}));\n        queue<pair<int, int>> q;\n        q.push({si, sj});\n        dist[si][sj] = 0;\n        pair<int, int> target = {-1, -1};\n        while (!q.empty() && target.first == -1) {\n            auto [x, y] = q.front(); q.pop();\n            if (h[x][y] < 0) {\n                target = {x, y};\n                break;\n            }\n            for (auto& dir : dirs) {\n                int nx = x + dir.first, ny = y + dir.second;\n                if (nx >= 0 && nx < N && ny >= 0 && ny < N && dist[nx][ny] == -1) {\n                    dist[nx][ny] = dist[x][y] + 1;\n                    prevv[nx][ny] = {x, y};\n                    q.push({nx, ny});\n                }\n            }\n        }\n        if (target.first == -1) return {{ -1, -1 }, {}};\n        // Reconstruct path to target\n        vector<string> path;\n        int ci = target.first, cj = target.second;\n        while (ci != si || cj != sj) {\n            auto [pi, pj] = prevv[ci][cj];\n            for (int d = 0; d < 4; ++d) {\n                if (pi + dirs[d].first == ci && pj + dirs[d].second == cj) {\n                    path.push_back(string(1, move_str[d]));\n                    break;\n                }\n            }\n            ci = pi; cj = pj;\n        }\n        reverse(path.begin(), path.end());\n        return {target, path};\n    };\n\n    int ci = 0, cj = 0; // Current position\n    vector<string> operations;\n\n    while (!pq_excess.empty()) {\n        // Get largest excess\n        auto [val, i, j] = pq_excess.top(); pq_excess.pop();\n        if (h[i][j] <= 0) continue; // Already processed\n\n        // Move to excess cell\n        auto path_to_excess = get_path(ci, cj, i, j);\n        for (auto& s : path_to_excess) operations.push_back(s);\n        ci = i; cj = j;\n\n        // Load all soil\n        int d = h[i][j];\n        operations.push_back(\"+\" + to_string(d));\n        h[i][j] = 0;\n\n        // Find and move to nearest deficits until d is 0\n        while (d > 0) {\n            auto [target, path_to_deficit] = find_nearest_deficit(ci, cj);\n            if (target.first == -1) break; // No deficit (shouldn't happen)\n            int p = target.first, q = target.second;\n\n            // Move to deficit\n            for (auto& s : path_to_deficit) operations.push_back(s);\n            ci = p; cj = q;\n\n            // Unload as much as possible\n            int unload = min(d, -h[p][q]);\n            operations.push_back(\"-\" + to_string(unload));\n            h[p][q] += unload;\n            d -= unload;\n        }\n    }\n\n    // Output all operations\n    for (auto& op : operations) {\n        cout << op << endl;\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    int seed_count = 2 * N * (N - 1);\n    vector<vector<int>> X(seed_count, vector<int>(M));\n    \n    // Read initial seeds\n    for (int i = 0; i < seed_count; i++) {\n        for (int j = 0; j < M; j++) {\n            cin >> X[i][j];\n        }\n    }\n    \n    for (int t = 0; t < T; t++) {\n        // Compute V_k for each seed and prepare for sorting\n        vector<pair<int, int>> seeds;  // Pair: (-V_k, index) for descending sort\n        for (int i = 0; i < seed_count; i++) {\n            int v = 0;\n            for (int j = 0; j < M; j++) {\n                v += X[i][j];\n            }\n            seeds.push_back({-v, i});  // Negative V_k for descending order\n        }\n        \n        // Sort seeds by V_k descending (top 36 will be first)\n        sort(seeds.begin(), seeds.end());\n        \n        // Create planting grid: Assign top 36 seeds in row-major order\n        vector<vector<int>> A(N, vector<int>(N));\n        int idx = 0;\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                A[i][j] = seeds[idx].second;  // Seed index\n                idx++;\n            }\n        }\n        \n        // Output the planting grid\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                cout << A[i][j];\n                if (j < N - 1) cout << \" \";\n                else cout << endl;\n            }\n        }\n        cout.flush();  // Important for interactive problems\n        \n        // Read new seeds after the operation\n        for (int i = 0; i < seed_count; i++) {\n            for (int j = 0; j < M; j++) {\n                cin >> X[i][j];\n            }\n        }\n    }\n    \n    return 0;\n}","ahc038":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int N, M, V;\n    cin >> N >> M >> V;\n    vector<string> s(N), t(N);\n    for(int i = 0; i < N; i++) cin >> s[i];\n    for(int i = 0; i < N; i++) cin >> t[i];\n    \n    vector<pair<int,int>> starts, targets;\n    for(int i = 0; i < N; i++) {\n        for(int j = 0; j < N; j++) {\n            if(s[i][j] == '1') starts.emplace_back(i, j);\n            if(t[i][j] == '1') targets.emplace_back(i, j);\n        }\n    }\n    sort(starts.begin(), starts.end());\n    sort(targets.begin(), targets.end());\n    \n    // Design the tree: simple arm with one fingertip\n    cout << 2 << endl;\n    cout << 0 << \" \" << 1 << endl;\n    cout << 0 << \" \" << 0 << endl;\n    \n    // Directions: 0:right, 1:down, 2:left, 3:up\n    vector<pair<int,int>> dirs = {{0,1}, {1,0}, {0,-1}, {-1,0}};\n    \n    int x = 0, y = 0, dir = 0;\n    \n    for(int i = 0; i < M; i++) {\n        auto [a, b] = starts[i];\n        auto [c, d] = targets[i];\n        \n        // Choose best d for picking\n        int best_dist = 1e9;\n        int best_d = -1, best_rx = -1, best_ry = -1;\n        for(int dd = 0; dd < 4; dd++) {\n            auto [dx, dy] = dirs[dd];\n            int rx = a - dx, ry = b - dy;\n            if(rx >= 0 && rx < N && ry >= 0 && ry < N) {\n                int dist = abs(rx - x) + abs(ry - y);\n                if(dist < best_dist) {\n                    best_dist = dist;\n                    best_d = dd;\n                    best_rx = rx;\n                    best_ry = ry;\n                }\n            }\n        }\n        \n        // Rotate to best_d\n        int diff = (best_d - dir + 4) % 4;\n        string rot;\n        if(diff <= 2) {\n            for(int k = 0; k < diff; k++) rot += 'R';\n        } else {\n            for(int k = 0; k < 4 - diff; k++) rot += 'L';\n        }\n        for(char r : rot) {\n            string S = \".\" + string(1, r) + \".\" + \".\";\n            cout << S << endl;\n            if(r == 'R') dir = (dir + 1) % 4;\n            else dir = (dir - 1 + 4) % 4;\n        }\n        \n        // Move to best_rx, best_ry\n        string moves;\n        int dx = best_rx - x;\n        if(dx > 0) for(int k = 0; k < dx; k++) moves += 'R';\n        else if(dx < 0) for(int k = 0; k < -dx; k++) moves += 'L';\n        int dy = best_ry - y;\n        if(dy > 0) for(int k = 0; k < dy; k++) moves += 'D';\n        else if(dy < 0) for(int k = 0; k < -dy; k++) moves += 'U';\n        for(size_t k = 0; k < moves.size(); k++) {\n            string S = string(1, moves[k]) + \".\" + \".\" + \".\";\n            if(k == moves.size() - 1) S[3] = 'P';\n            cout << S << endl;\n        }\n        x = best_rx;\n        y = best_ry;\n        \n        // Choose best e for placing\n        best_dist = 1e9;\n        int best_e = -1, best_rx2 = -1, best_ry2 = -1;\n        for(int ee = 0; ee < 4; ee++) {\n            auto [ex, ey] = dirs[ee];\n            int rx2 = c - ex, ry2 = d - ey;\n            if(rx2 >= 0 && rx2 < N && ry2 >= 0 && ry2 < N) {\n                int dist = abs(rx2 - x) + abs(ry2 - y);\n                if(dist < best_dist) {\n                    best_dist = dist;\n                    best_e = ee;\n                    best_rx2 = rx2;\n                    best_ry2 = ry2;\n                }\n            }\n        }\n        \n        // Rotate to best_e\n        diff = (best_e - dir + 4) % 4;\n        rot = \"\";\n        if(diff <= 2) {\n            for(int k = 0; k < diff; k++) rot += 'R';\n        } else {\n            for(int k = 0; k < 4 - diff; k++) rot += 'L';\n        }\n        for(char r : rot) {\n            string S = \".\" + string(1, r) + \".\" + \".\";\n            cout << S << endl;\n            if(r == 'R') dir = (dir + 1) % 4;\n            else dir = (dir - 1 + 4) % 4;\n        }\n        \n        // Move to best_rx2, best_ry2\n        moves = \"\";\n        dx = best_rx2 - x;\n        if(dx > 0) for(int k = 0; k < dx; k++) moves += 'R';\n        else if(dx < 0) for(int k = 0; k < -dx; k++) moves += 'L';\n        dy = best_ry2 - y;\n        if(dy > 0) for(int k = 0; k < dy; k++) moves += 'D';\n        else if(dy < 0) for(int k = 0; k < -dy; k++) moves += 'U';\n        for(size_t k = 0; k < moves.size(); k++) {\n            string S = string(1, moves[k]) + \".\" + \".\" + \".\";\n            if(k == moves.size() - 1) S[3] = 'P';\n            cout << S << endl;\n        }\n        x = best_rx2;\n        y = best_ry2;\n    }\n    \n    return 0;\n}","ahc039":"#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n\nstruct Fenwick {\n  vector<ll> tree;\n  int n;\n  Fenwick(int _n) : n(_n), tree(_n + 1, 0) {}\n  void update(int idx, ll val) {\n    while (idx <= n) {\n      tree[idx] += val;\n      idx += idx & -idx;\n    }\n  }\n  ll query(int idx) {\n    ll sum = 0;\n    while (idx > 0) {\n      sum += tree[idx];\n      idx -= idx & -idx;\n    }\n    return sum;\n  }\n};\n\nint main() {\n  ios::sync_with_stdio(false);\n  cin.tie(nullptr);\n\n  int N;\n  cin >> N;\n  vector<tuple<ll, ll, int>> pts;\n  for (int i = 0; i < 2 * N; ++i) {\n    ll x, y;\n    cin >> x >> y;\n    int w = (i < N ? 1 : -1);\n    pts.emplace_back(x, y, w);\n  }\n\n  sort(pts.begin(), pts.end());\n\n  // Assign ranks to y\n  set<ll> yset;\n  for (auto &[x, y, w] : pts) yset.insert(y);\n  vector<ll> ylist(yset.begin(), yset.end());\n  map<ll, int> yrank;\n  for (int i = 0; i < ylist.size(); ++i) {\n    yrank[ylist[i]] = i + 1; // 1-based\n  }\n\n  int max_rank = ylist.size();\n  Fenwick ft(max_rank);\n\n  ll best_score = LLONG_MIN / 2;\n  ll best_left = -1, best_right = -1, best_bottom = -1, best_top = -1;\n\n  ll current_min_x = LLONG_MAX / 2;\n  set<int> active_ranks;\n\n  for (auto &[x, y, w] : pts) {\n    current_min_x = min(current_min_x, x);\n    int r = yrank[y];\n    ft.update(r, w);\n    active_ranks.insert(r);\n\n    // Compute max subarray in active_ranks\n    vector<int> order(active_ranks.begin(), active_ranks.end());\n    ll cum = 0;\n    ll min_p = 0;\n    int min_idx = 0;\n    ll local_max = LLONG_MIN / 2;\n    int best_start = 0, best_end = -1;\n    for (int j = 0; j < order.size(); ++j) {\n      cum = ft.query(order[j]);\n      ll sum_val = cum - min_p;\n      if (sum_val > local_max) {\n        local_max = sum_val;\n        best_start = min_idx;\n        best_end = j;\n      }\n      if (cum < min_p) {\n        min_p = cum;\n        min_idx = j + 1;\n      }\n    }\n\n    if (current_min_x < x && ylist[order[best_start] - 1] < ylist[order[best_end] - 1] && local_max > best_score) {\n      best_score = local_max;\n      best_left = current_min_x;\n      best_right = x;\n      best_bottom = ylist[order[best_start] - 1];\n      best_top = ylist[order[best_end] - 1];\n    }\n  }\n\n  // If no valid, small one\n  if (best_score <= LLONG_MIN / 2 + 1) {\n    best_left = 0;\n    best_right = 1;\n    best_bottom = 0;\n    best_top = 1;\n  }\n\n  vector<pair<ll, ll>> poly = {\n    {best_left, best_bottom},\n    {best_left, best_top},\n    {best_right, best_top},\n    {best_right, best_bottom}\n  };\n\n  cout << 4 << '\\n';\n  for (auto &[x, y] : poly) {\n    cout << x << ' ' << y << '\\n';\n  }\n\n  return 0;\n}","ahc040":"#include <bits/stdc++.h>\n\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int N, T, sigma;\n    cin >> N >> T >> sigma;\n    vector<long long> w(N), h(N);\n    for (int i = 0; i < N; ++i) {\n        cin >> w[i] >> h[i];\n    }\n    \n    // Define modes: 0=alternate U/L with smart r/b, 1-4=fixed for baselines\n    vector<int> modes = {0, 1, 2, 3, 4};\n    \n    for (int t = 0; t < T; ++t) {\n        int mode = modes[t % modes.size()];\n        vector<tuple<int, int, char, int>> prdb;\n        \n        for (int i = 0; i < N; ++i) {\n            int p = i;\n            int r;\n            char d;\n            int b;\n            \n            if (mode == 0) {\n                // Alternate U/L, b=previous, r optimized for direction\n                d = (i % 2 == 0) ? 'U' : 'L';\n                b = (i == 0) ? -1 : i - 1;\n                if (d == 'U') {\n                    r = (h[i] > w[i]) ? 1 : 0;  // Rotate if taller to prioritize width\n                } else {\n                    r = (w[i] > h[i]) ? 1 : 0;  // Rotate if wider to prioritize height\n                }\n            } else if (mode == 1) {\n                d = 'U';\n                b = -1;\n                r = 0;\n            } else if (mode == 2) {\n                d = 'L';\n                b = -1;\n                r = 0;\n            } else if (mode == 3) {\n                d = 'U';\n                b = -1;\n                r = 1;\n            } else if (mode == 4) {\n                d = 'L';\n                b = -1;\n                r = 1;\n            }\n            \n            prdb.emplace_back(p, r, d, b);\n        }\n        \n        cout << N << '\\n';\n        for (auto [p, r, d, b] : prdb) {\n            cout << p << ' ' << r << ' ' << d << ' ' << b << '\\n';\n        }\n        cout << flush;\n        \n        long long Wp, Hp;\n        cin >> Wp >> Hp;\n        // Future: Use Wp, Hp to adapt modes (e.g., if Wp > Hp, favor 'L')\n    }\n    \n    return 0;\n}","ahc041":"#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n\npair<ll, int> compute_score_and_max_height(int N, const vector<int>& A, const vector<int>& p, const vector<vector<int>>& adj, int H) {\n    vector<int> depth(N, -1);\n    ll score = 0;\n    int max_h = 0;\n    bool has_cycle = false;\n    for (int i = 0; i < N; ++i) {\n        if (depth[i] != -1) continue;\n        queue<int> q;\n        q.push(i);\n        depth[i] = 0;\n        vector<bool> visited(N, false);\n        visited[i] = true;\n        while (!q.empty()) {\n            int u = q.front(); q.pop();\n            score += (ll)(depth[u] + 1) * A[u];\n            max_h = max(max_h, depth[u]);\n            for (int v : adj[u]) {\n                if (v == p[u]) continue;\n                if (visited[v]) {\n                    has_cycle = true;\n                    break;\n                }\n                if (depth[v] == -1) {\n                    depth[v] = depth[u] + 1;\n                    visited[v] = true;\n                    q.push(v);\n                }\n            }\n            if (has_cycle) break;\n        }\n        if (has_cycle) break;\n    }\n    if (has_cycle || max_h > H) return {-1, -1};\n    return {score, max_h};\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(0);\n    int N, M, H;\n    cin >> N >> M >> H;\n    vector<int> A(N);\n    for (int i = 0; i < N; ++i) {\n        cin >> A[i];\n    }\n    vector<vector<int>> adj(N);\n    for (int i = 0; i < M; ++i) {\n        int u, v;\n        cin >> u >> v;\n        adj[u].push_back(v);\n        adj[v].push_back(u);\n    }\n    vector<pair<int, int>> points(N);\n    for (int i = 0; i < N; ++i) {\n        cin >> points[i].first >> points[i].second;\n    }\n    vector<vector<double>> dist(N, vector<double>(N));\n    for (int i = 0; i < N; ++i) {\n        for (int j = 0; j < N; ++j) {\n            double dx = points[i].first - points[j].first;\n            double dy = points[i].second - points[j].second;\n            dist[i][j] = sqrt(dx * dx + dy * dy);\n        }\n    }\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n    vector<pair<int, int>> verts;\n    for (int i = 0; i < N; ++i) {\n        verts.push_back({-A[i], i});\n    }\n    shuffle(verts.begin(), verts.end(), rng);\n    vector<int> d(N, -1);\n    vector<int> p(N, -1);\n    for (auto [_, v] : verts) {\n        if (d[v] != -1) continue;\n        int best_u = -1;\n        double min_dist = 1e9;\n        int max_du = -1;\n        for (int u : adj[v]) {\n            if (d[u] != -1 && d[u] < H) {\n                if (d[u] > max_du || (d[u] == max_du && dist[v][u] < min_dist)) {\n                    max_du = d[u];\n                    min_dist = dist[v][u];\n                    best_u = u;\n                }\n            }\n        }\n        if (best_u != -1) {\n            p[v] = best_u;\n            d[v] = max_du + 1;\n        } else {\n            p[v] = -1;\n            d[v] = 0;\n        }\n    }\n    auto [current_score, _] = compute_score_and_max_height(N, A, p, adj, H);\n    const int MAX_ITER = 10000;\n    double T = 1000000.0;\n    double alpha = 0.9999;\n    for (int iter = 0; iter < MAX_ITER; ++iter) {\n        int v = rng() % N;\n        vector<int> candidates = adj[v];\n        candidates.push_back(-1);\n        int new_parent = candidates[rng() % candidates.size()];\n        if (new_parent == p[v]) continue;\n        int old_p = p[v];\n        p[v] = new_parent;\n        auto [new_score, new_max_h] = compute_score_and_max_height(N, A, p, adj, H);\n        if (new_score != -1) {\n            double delta = new_score - current_score;\n            if (delta > 0 || exp(delta / T) > uniform_real_distribution<double>(0, 1)(rng)) {\n                current_score = new_score;\n            } else {\n                p[v] = old_p;\n            }\n        } else {\n            p[v] = old_p;\n        }\n        T *= alpha;\n    }\n    for (int i = 0; i < N; ++i) {\n        if (i > 0) cout << \" \";\n        cout << p[i];\n    }\n    cout << \"\\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> board(N);\n    for (int i = 0; i < N; i++) {\n        cin >> board[i];\n    }\n    vector<pair<int, int>> oni;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (board[i][j] == 'x') {\n                oni.emplace_back(i, j);\n            }\n        }\n    }\n    // Maps for batching: direction -> index -> max distance\n    map<pair<char, int>, int> max_dist;\n    for (auto [i, j] : oni) {\n        // Check directions and choose best\n        bool can_up = true;\n        for (int k = 0; k < i; k++) {\n            if (board[k][j] == 'o') can_up = false;\n        }\n        int cost_up = can_up ? 2 * (i + 1) : 1e9;\n\n        bool can_down = true;\n        for (int k = i + 1; k < N; k++) {\n            if (board[k][j] == 'o') can_down = false;\n        }\n        int cost_down = can_down ? 2 * (N - i) : 1e9;\n\n        bool can_left = true;\n        for (int k = 0; k < j; k++) {\n            if (board[i][k] == 'o') can_left = false;\n        }\n        int cost_left = can_left ? 2 * (j + 1) : 1e9;\n\n        bool can_right = true;\n        for (int k = j + 1; k < N; k++) {\n            if (board[i][k] == 'o') can_right = false;\n        }\n        int cost_right = can_right ? 2 * (N - j) : 1e9;\n\n        // Find best direction\n        vector<tuple<int, char, int, int>> candidates; // cost, dir, idx, dist\n        if (can_up) candidates.emplace_back(cost_up, 'U', j, i + 1);\n        if (can_down) candidates.emplace_back(cost_down, 'D', j, N - i);\n        if (can_left) candidates.emplace_back(cost_left, 'L', i, j + 1);\n        if (can_right) candidates.emplace_back(cost_right, 'R', i, N - j);\n        sort(candidates.begin(), candidates.end());\n        auto [cost, dir, idx, dist] = candidates[0];\n\n        // Update max_dist\n        pair<char, int> key = {dir, idx};\n        max_dist[key] = max(max_dist[key], dist);\n    }\n\n    // Generate batched operations\n    vector<pair<char, int>> all_ops;\n    for (auto& [key, dist] : max_dist) {\n        char dir = key.first;\n        int idx = key.second;\n        if (dir == 'U') {\n            for (int t = 0; t < dist; t++) all_ops.emplace_back('U', idx);\n            for (int t = 0; t < dist; t++) all_ops.emplace_back('D', idx);\n        } else if (dir == 'D') {\n            for (int t = 0; t < dist; t++) all_ops.emplace_back('D', idx);\n            for (int t = 0; t < dist; t++) all_ops.emplace_back('U', idx);\n        } else if (dir == 'L') {\n            for (int t = 0; t < dist; t++) all_ops.emplace_back('L', idx);\n            for (int t = 0; t < dist; t++) all_ops.emplace_back('R', idx);\n        } else if (dir == 'R') {\n            for (int t = 0; t < dist; t++) all_ops.emplace_back('R', idx);\n            for (int t = 0; t < dist; t++) all_ops.emplace_back('L', idx);\n        }\n    }\n\n    // Output\n    for (auto [d, p] : all_ops) {\n        cout << d << \" \" << p << \"\\n\";\n    }\n    return 0;\n}","ahc044":"#include <bits/stdc++.h>\n#include <random>\n#include <chrono>\n\nusing namespace std;\n\nint main() {\n    const int N = 100;\n    const int L = 500000;\n    vector<long long> T(N);\n    for (auto &x : T) cin >> x;\n\n    // Random number generator\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n    uniform_real_distribution<double> urd(0.0, 1.0);\n\n    // Initial random assignment\n    vector<int> a(N), b(N);\n    for (int i = 0; i < N; i++) {\n        a[i] = rng() % N;\n        b[i] = rng() % N;\n    }\n\n    // Simulation function\n    auto simulate = [&](const vector<int> &aa, const vector<int> &bb) -> long long {\n        vector<long long> cnt(N, 0);\n        int cur = 0;\n        cnt[0] = 1;\n        for (int w = 1; w < L; w++) {\n            int t = cnt[cur];\n            int nxt = (t % 2 == 1) ? aa[cur] : bb[cur];\n            cur = nxt;\n            cnt[cur]++;\n        }\n        long long e = 0;\n        for (int i = 0; i < N; i++) e += abs(cnt[i] - T[i]);\n        return e;\n    };\n\n    // Initial error\n    long long currentE = simulate(a, b);\n\n    // Simulated Annealing parameters\n    double temp = 1000.0;\n    const int iterations = 150;  // Adjust based on time; ~150 fits within 2s\n    const double cooling_rate = 0.99;\n\n    for (int it = 0; it < iterations; it++) {\n        // Perturb: choose random i, which (0 for a, 1 for b), and new value\n        int i = rng() % N;\n        int which = rng() % 2;\n        int old_val = (which == 0) ? a[i] : b[i];\n        int new_val = rng() % N;\n\n        // Apply change\n        if (which == 0) a[i] = new_val;\n        else b[i] = new_val;\n\n        // Compute new error\n        long long newE = simulate(a, b);\n\n        // Acceptance\n        if (newE <= currentE || urd(rng) < exp((currentE - newE) / temp)) {\n            currentE = newE;\n        } else {\n            // Revert\n            if (which == 0) a[i] = old_val;\n            else b[i] = old_val;\n        }\n\n        // Cool down\n        temp *= cooling_rate;\n    }\n\n    // Output the best assignment\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\nusing ll = long long;\nusing ld = long double;\n\n// Union-Find for Kruskal\nstruct UF {\n    vector<int> par;\n    UF(int n) : par(n) { iota(par.begin(), par.end(), 0); }\n    int find(int x) { return par[x] == x ? x : par[x] = find(par[x]); }\n    bool unite(int x, int y) {\n        x = find(x); y = find(y);\n        if (x == y) return false;\n        par[x] = y;\n        return true;\n    }\n};\n\n// Compute floored Euclidean distance\nll dist(ld x1, ld y1, ld x2, ld y2) {\n    ld dx = x1 - x2, dy = y1 - y2;\n    return floorl(sqrtl(dx * dx + dy * dy));\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, Q, L, W;\n    cin >> N >> M >> Q >> L >> W;\n    vector<int> G(M);\n    for (int i = 0; i < M; ++i) cin >> G[i];\n    vector<ld> 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    // Compute approximate centers\n    vector<ld> cx(N), cy(N);\n    for (int i = 0; i < N; ++i) {\n        cx[i] = (lx[i] + rx[i]) / 2.0L;\n        cy[i] = (ly[i] + ry[i]) / 2.0L;\n    }\n\n    // Sort cities by cx, then cy\n    vector<int> cities(N);\n    iota(cities.begin(), cities.end(), 0);\n    sort(cities.begin(), cities.end(), [&](int a, int b) {\n        if (cx[a] != cx[b]) return cx[a] < cx[b];\n        return cy[a] < cy[b];\n    });\n\n    // Assign to groups\n    vector<vector<int>> groups(M);\n    int idx = 0;\n    for (int k = 0; k < M; ++k) {\n        for (int j = 0; j < G[k]; ++j) {\n            groups[k].push_back(cities[idx++]);\n        }\n    }\n\n    // Process queries and build edges\n    vector<vector<pair<int, int>>> edges(M);\n    int queries_used = 0;\n\n    auto query = [&](const vector<int>& c) -> vector<pair<int, int>> {\n        if (queries_used >= Q) return {};\n        cout << \"? \" << c.size();\n        for (int x : c) cout << \" \" << x;\n        cout << \"\\n\" << flush;\n        ++queries_used;\n        vector<pair<int, int>> res;\n        for (int i = 0; i < (int)c.size() - 1; ++i) {\n            int a, b;\n            cin >> a >> b;\n            res.emplace_back(min(a, b), max(a, b));\n        }\n        return res;\n    };\n\n    for (int k = 0; k < M; ++k) {\n        auto& grp = groups[k];\n        int sz = grp.size();\n        if (sz >= 2 && sz <= L) {\n            // Query entire group for true MST (only if valid size)\n            auto res = query(grp);\n            edges[k] = res;\n        } else if (sz > L) {\n            // Approximate MST using centers (exactly sz-1 edges)\n            vector<tuple<ll, int, int>> edge_list;\n            for (int i = 0; i < sz; ++i) {\n                for (int j = i + 1; j < sz; ++j) {\n                    ll d = dist(cx[grp[i]], cy[grp[i]], cx[grp[j]], cy[grp[j]]);\n                    edge_list.emplace_back(d, i, j);\n                }\n            }\n            sort(edge_list.begin(), edge_list.end());\n            UF uf(sz);\n            vector<pair<int, int>> mst_edges;\n            for (auto& [d, u, v] : edge_list) {\n                if (uf.unite(u, v)) {\n                    mst_edges.emplace_back(grp[u], grp[v]);\n                }\n            }\n            edges[k] = mst_edges;\n        }\n        // For sz == 1, edges[k] remains empty (default vector)\n    }\n\n    // Output\n    cout << \"!\\n\";\n    for (int k = 0; k < M; ++k) {\n        for (int x : groups[k]) cout << x << \" \";\n        cout << \"\\n\";\n        for (auto& [a, b] : edges[k]) {\n            cout << a << \" \" << b << \"\\n\";\n        }\n    }\n\n    return 0;\n}","ahc046":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int N, M;\n    cin >> N >> M;\n    vector<pair<int, int>> pos(M);\n    for (int i = 0; i < M; i++) {\n        cin >> pos[i].first >> pos[i].second;\n    }\n    vector<vector<bool>> blocked(20, vector<bool>(20, false));\n    auto get_path = [&](pair<int, int> s, pair<int, int> t) {\n        vector<vector<int>> dist(20, vector<int>(20, -1));\n        vector<vector<pair<int, int>>> prevv(20, vector<pair<int, int>>(20, {-1, -1}));\n        queue<pair<int, int>> q;\n        q.push(s);\n        dist[s.first][s.second] = 0;\n        int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};\n        while (!q.empty()) {\n            auto [x, y] = q.front();\n            q.pop();\n            for (int d = 0; d < 4; d++) {\n                int nx = x + dx[d], ny = y + dy[d];\n                if (nx >= 0 && nx < 20 && ny >= 0 && ny < 20 && !blocked[nx][ny] && dist[nx][ny] == -1) {\n                    dist[nx][ny] = dist[x][y] + 1;\n                    prevv[nx][ny] = {x, y};\n                    q.push({nx, ny});\n                }\n            }\n        }\n        vector<pair<int, int>> path;\n        pair<int, int> cur = t;\n        while (cur != make_pair(-1, -1)) {\n            path.push_back(cur);\n            cur = prevv[cur.first][cur.second];\n        }\n        reverse(path.begin(), path.end());\n        if (path.empty() || path[0] != s) return vector<int>();\n        vector<int> dirs;\n        for (size_t i = 1; i < path.size(); i++) {\n            int px = path[i - 1].first, py = path[i - 1].second;\n            int cx = path[i].first, cy = path[i].second;\n            if (cx == px + 1) dirs.push_back(1);\n            else if (cx == px - 1) dirs.push_back(0);\n            else if (cy == py + 1) dirs.push_back(3);\n            else if (cy == py - 1) dirs.push_back(2);\n        }\n        return dirs;\n    };\n    vector<pair<char, char>> actions;\n    pair<int, int> current = pos[0];\n    for (int k = 1; k < M; k++) {\n        vector<int> dirs = get_path(current, pos[k]);\n        for (int d : dirs) {\n            actions.emplace_back('M', \"UDLR\"[d]);\n        }\n        current = pos[k];\n    }\n    for (auto [a, d] : actions) {\n        cout << a << \" \" << d << endl;\n    }\n    return 0;\n}"},"16":{"ahc001":"#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n\ndouble calc_p(ll r, ll s) {\n    if (s == 0 || r == 0) return 0.0;\n    double mn = min(r, s);\n    double mx = max(r, s);\n    double ratio = mn / mx;\n    return 1.0 - (1.0 - ratio) * (1.0 - ratio);\n}\n\nbool check_overlap(int i, ll na, ll nb, ll nc, ll nd, const vector<ll>& a, const vector<ll>& b, const vector<ll>& c, const vector<ll>& d, int n) {\n    for (int j = 0; j < n; ++j) {\n        if (i == j) continue;\n        ll ax = max(na, a[j]);\n        ll bx = min(nc, c[j]);\n        ll ay = max(nb, b[j]);\n        ll by = min(nd, d[j]);\n        if (ax < bx && ay < by) return true;\n    }\n    return false;\n}\n\nll find_max_dw(int i, const vector<ll>& a, const vector<ll>& b, const vector<ll>& c, const vector<ll>& d, int n) {\n    ll left = 0, right = 10000LL - c[i];\n    while (left < right) {\n        ll mid = (left + right + 1) / 2;\n        ll new_c = c[i] + mid;\n        if (check_overlap(i, a[i], b[i], new_c, d[i], a, b, c, d, n)) {\n            right = mid - 1;\n        } else {\n            left = mid;\n        }\n    }\n    return left;\n}\n\nll find_max_dh(int i, const vector<ll>& a, const vector<ll>& b, const vector<ll>& c, const vector<ll>& d, int n) {\n    ll left = 0, right = 10000LL - d[i];\n    while (left < right) {\n        ll mid = (left + right + 1) / 2;\n        ll new_d = d[i] + mid;\n        if (check_overlap(i, a[i], b[i], c[i], new_d, a, b, c, d, n)) {\n            right = mid - 1;\n        } else {\n            left = mid;\n        }\n    }\n    return left;\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int n;\n    cin >> n;\n    vector<ll> x(n), y(n), r(n);\n    for (int i = 0; i < n; ++i) {\n        cin >> x[i] >> y[i] >> r[i];\n    }\n    \n    vector<ll> a(n), b(n), c(n), d(n), s(n);\n    for (int i = 0; i < n; ++i) {\n        a[i] = x[i];\n        c[i] = x[i] + 1;\n        b[i] = y[i];\n        d[i] = y[i] + 1;\n        s[i] = 1;\n    }\n    \n    bool changed = true;\n    while (changed) {\n        changed = false;\n        double max_gain = -1e9;\n        int best_i = -1;\n        int best_dir = -1;  // 0: width, 1: height\n        ll best_delta = 0;\n        \n        for (int i = 0; i < n; ++i) {\n            if (s[i] >= r[i]) continue;\n            \n            ll w = c[i] - a[i];\n            ll h = d[i] - b[i];\n            \n            // Find max dw with binary search\n            ll dw = find_max_dw(i, a, b, c, d, n);\n            \n            // Find max dh with binary search\n            ll dh = find_max_dh(i, a, b, c, d, n);\n            \n            // Check gain for dw\n            if (dw > 0) {\n                ll new_s = s[i] + dw * h;\n                double old_p = calc_p(r[i], s[i]);\n                double new_p = calc_p(r[i], new_s);\n                double gain = new_p - old_p;\n                if (gain > max_gain + 1e-9) {  // Small epsilon for floating-point\n                    max_gain = gain;\n                    best_i = i;\n                    best_dir = 0;\n                    best_delta = dw;\n                }\n            }\n            \n            // Check gain for dh\n            if (dh > 0) {\n                ll new_s = s[i] + dh * w;\n                double old_p = calc_p(r[i], s[i]);\n                double new_p = calc_p(r[i], new_s);\n                double gain = new_p - old_p;\n                if (gain > max_gain + 1e-9) {\n                    max_gain = gain;\n                    best_i = i;\n                    best_dir = 1;\n                    best_delta = dh;\n                }\n            }\n        }\n        \n        if (best_i != -1) {\n            changed = true;\n            if (best_dir == 0) {  // Expand width\n                c[best_i] += best_delta;\n                s[best_i] += best_delta * (d[best_i] - b[best_i]);\n            } else {  // Expand height\n                d[best_i] += best_delta;\n                s[best_i] += best_delta * (c[best_i] - a[best_i]);\n            }\n        }\n    }\n    \n    // Output\n    for (int i = 0; i < n; ++i) {\n        cout << a[i] << \" \" << b[i] << \" \" << c[i] << \" \" << d[i] << \"\\n\";\n    }\n    \n    return 0;\n}","ahc002":"#include <bits/stdc++.h>\n\nusing namespace std;\n\nstruct State {\n    int tile;\n    pair<int, int> sq;\n    set<int> visited;\n    vector<pair<int, int>> path_squares;\n    int score;\n    bool operator<(const State& other) const {\n        return score < other.score;  // Max-heap\n    }\n};\n\nint main() {\n    int si, sj;\n    cin >> si >> sj;\n    vector<vector<int>> t(50, vector<int>(50));\n    for (int i = 0; i < 50; i++) for (int j = 0; j < 50; j++) cin >> t[i][j];\n    vector<vector<int>> p(50, vector<int>(50));\n    for (int i = 0; i < 50; i++) for (int j = 0; j < 50; j++) cin >> p[i][j];\n    \n    int M = 0;\n    for (int i = 0; i < 50; i++) for (int j = 0; j < 50; j++) M = max(M, t[i][j] + 1);\n    \n    vector<vector<int>> tile_squares(M);\n    for (int i = 0; i < 50; i++) for (int j = 0; j < 50; j++) {\n        tile_squares[t[i][j]].push_back(i * 50 + j);\n    }\n    \n    vector<set<int>> adj_tile(M);\n    int di[4] = {-1, 1, 0, 0};\n    int dj[4] = {0, 0, -1, 1};\n    for (int i = 0; i < 50; i++) for (int j = 0; j < 50; j++) {\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 < 50 && nj >= 0 && nj < 50 && t[i][j] != t[ni][nj]) {\n                adj_tile[t[i][j]].insert(t[ni][nj]);\n            }\n        }\n    }\n    \n    // Beam search with width 50, max steps 150\n    const int BEAM_WIDTH = 50;\n    const int MAX_STEPS = 150;\n    priority_queue<State> pq;\n    set<int> initial_visited;\n    initial_visited.insert(t[si][sj]);\n    vector<pair<int, int>> initial_path = {{si, sj}};\n    int initial_score = p[si][sj];\n    pq.push({t[si][sj], {si, sj}, initial_visited, initial_path, initial_score});\n    \n    State best_state = pq.top();\n    int steps = 0;\n    while (!pq.empty() && steps < MAX_STEPS) {\n        vector<State> current_beam;\n        for (int i = 0; i < BEAM_WIDTH && !pq.empty(); i++) {\n            current_beam.push_back(pq.top());\n            pq.pop();\n        }\n        for (const auto& state : current_beam) {\n            if (state.score > best_state.score) best_state = state;\n            for (int nei : adj_tile[state.tile]) {\n                if (state.visited.count(nei)) continue;\n                int max_adj_p = -1;\n                pair<int, int> chosen_sq = {-1, -1};\n                for (int sq : tile_squares[nei]) {\n                    int ni = sq / 50;\n                    int nj = sq % 50;\n                    if (abs(ni - state.sq.first) + abs(nj - state.sq.second) == 1) {\n                        if (p[ni][nj] > max_adj_p) {\n                            max_adj_p = p[ni][nj];\n                            chosen_sq = {ni, nj};\n                        }\n                    }\n                }\n                if (chosen_sq.first != -1) {\n                    set<int> new_visited = state.visited;\n                    new_visited.insert(nei);\n                    vector<pair<int, int>> new_path = state.path_squares;\n                    new_path.push_back(chosen_sq);\n                    int new_score = state.score + max_adj_p;\n                    pq.push({nei, chosen_sq, new_visited, new_path, new_score});\n                }\n            }\n        }\n        steps++;\n    }\n    \n    // Build move string from best_state\n    string moves = \"\";\n    for (size_t i = 1; i < best_state.path_squares.size(); i++) {\n        auto [pi, pj] = best_state.path_squares[i - 1];\n        auto [ci, cj] = best_state.path_squares[i];\n        if (ci == pi - 1) moves += 'U';\n        else if (ci == pi + 1) moves += 'D';\n        else if (cj == pj - 1) moves += 'L';\n        else if (cj == pj + 1) moves += 'R';\n        else assert(false);\n    }\n    cout << moves << endl;\n    return 0;\n}","ahc003":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    const int N = 30;\n    vector<vector<double>> h(N, vector<double>(N-1, 5000.0));  // h[i][j]: cost from (i,j) to (i,j+1)\n    vector<vector<double>> v(N-1, vector<double>(N, 5000.0));  // v[i][j]: cost from (i,j) to (i+1,j)\n\n    for (int query = 0; query < 1000; ++query) {\n        int si, sj, ti, tj;\n        cin >> si >> sj >> ti >> tj;\n\n        // Dijkstra to find shortest path with current estimated costs\n        vector<vector<double>> dist(N, vector<double>(N, 1e18));\n        vector<vector<pair<int, int>>> prev(N, vector<pair<int, int>>(N, {-1, -1}));\n        priority_queue<tuple<double, int, int>, vector<tuple<double, int, int>>, greater<tuple<double, int, int>>> pq;\n\n        dist[si][sj] = 0.0;\n        pq.emplace(0.0, si, sj);\n\n        while (!pq.empty()) {\n            auto [cost, i, j] = pq.top();\n            pq.pop();\n            if (cost > dist[i][j]) continue;\n\n            // Neighbors: up, down, left, right\n            vector<tuple<int, int, double>> neighbors;\n            if (i > 0) neighbors.emplace_back(i-1, j, v[i-1][j]);  // up\n            if (i < N-1) neighbors.emplace_back(i+1, j, v[i][j]);  // down\n            if (j > 0) neighbors.emplace_back(i, j-1, h[i][j-1]);  // left\n            if (j < N-1) neighbors.emplace_back(i, j+1, h[i][j]);  // right\n\n            for (auto [ni, nj, edge_cost] : neighbors) {\n                double new_cost = cost + edge_cost;\n                if (new_cost < dist[ni][nj]) {\n                    dist[ni][nj] = new_cost;\n                    prev[ni][nj] = {i, j};\n                    pq.emplace(new_cost, ni, nj);\n                }\n            }\n        }\n\n        // If target is unreachable (shouldn't happen), output empty path\n        if (dist[ti][tj] == 1e18) {\n            cout << \"\" << endl;\n            cout.flush();\n            int F;\n            cin >> F;  // Still read feedback to continue\n            continue;\n        }\n\n        // Reconstruct path from t to s, building forward moves\n        string path = \"\";\n        int ci = ti, cj = tj;\n        while (ci != si || cj != sj) {\n            auto [pi, pj] = prev[ci][cj];\n            if (pi == -1 || pj == -1) {\n                // Invalid predecessor (shouldn't happen), break to avoid crash\n                path = \"\";\n                break;\n            }\n            // Determine forward move from (pi, pj) to (ci, cj)\n            if (pi == ci - 1) path += 'D';  // i increases: down\n            else if (pi == ci + 1) path += 'U';  // i decreases: up\n            else if (pj == cj - 1) path += 'R';  // j increases: right\n            else if (pj == cj + 1) path += 'L';  // j decreases: left\n            ci = pi;\n            cj = pj;\n        }\n        reverse(path.begin(), path.end());  // Reverse to get from s to t\n\n        // Output path and flush\n        cout << path << endl;\n        cout.flush();\n\n        // Read feedback F\n        int F;\n        cin >> F;\n\n        // Compute estimated path cost C\n        double C = 0.0;\n        ci = si, cj = sj;\n        for (char move : path) {\n            if (move == 'U') {\n                C += v[ci-1][cj];\n                ci--;\n            } else if (move == 'D') {\n                C += v[ci][cj];\n                ci++;\n            } else if (move == 'L') {\n                C += h[ci][cj-1];\n                cj--;\n            } else if (move == 'R') {\n                C += h[ci][cj];\n                cj++;\n            }\n        }\n\n        // Update estimated costs if C > 0\n        if (C > 1e-9) {\n            double factor = static_cast<double>(F) / C;\n            ci = si, cj = sj;\n            for (char move : path) {\n                if (move == 'U') {\n                    v[ci-1][cj] *= factor;\n                    ci--;\n                } else if (move == 'D') {\n                    v[ci][cj] *= factor;\n                    ci++;\n                } else if (move == 'L') {\n                    h[ci][cj-1] *= factor;\n                    cj--;\n                } else if (move == 'R') {\n                    h[ci][cj] *= factor;\n                    cj++;\n                }\n            }\n        }\n    }\n\n    return 0;\n}","ahc004":"#include <bits/stdc++.h>\n\nusing namespace std;\n\nint N;\n\n// Function to compute c: number of strings that are subsequences\nint compute_c(const vector<vector<char>>& grid, const vector<string>& strings) {\n    int covered = 0;\n    for (const auto& s : strings) {\n        bool found = false;\n        // Horizontal check\n        for (int i = 0; i < N && !found; i++) {\n            string row(grid[i].begin(), grid[i].end());\n            row += row;  // Double for wrap-around\n            if (row.find(s) != string::npos) found = true;\n        }\n        // Vertical check\n        for (int j = 0; j < N && !found; j++) {\n            string col;\n            for (int i = 0; i < N; i++) col += grid[i][j];\n            col += col;  // Double for wrap-around\n            if (col.find(s) != string::npos) found = true;\n        }\n        if (found) covered++;\n    }\n    return covered;\n}\n\n// Function to compute d: number of dots\nint compute_d(const vector<vector<char>>& grid) {\n    int dots = 0;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (grid[i][j] == '.') dots++;\n        }\n    }\n    return dots;\n}\n\nint main() {\n    int M;\n    cin >> N >> M;\n    vector<string> strings(M);\n    for (int i = 0; i < M; i++) {\n        cin >> strings[i];\n    }\n\n    // Sort strings by length descending\n    sort(strings.begin(), strings.end(), [](const string& a, const string& b) {\n        return a.size() > b.size();\n    });\n\n    vector<vector<char>> best_grid(N, vector<char>(N, '.'));\n    int best_c = -1;\n    int best_d = INT_MAX;\n\n    const int ATTEMPTS = 20;  // Increased number of attempts\n    const int PERTURBATIONS = 10;  // Increased number of perturbations\n    const int SA_ITERATIONS = 20;  // Number of SA iterations\n    mt19937 rng(42);  // Fixed seed for reproducibility\n\n    for (int attempt = 0; attempt < ATTEMPTS; attempt++) {\n        // Shuffle within length groups\n        vector<string> shuffled_strings = strings;\n        auto it = shuffled_strings.begin();\n        while (it != shuffled_strings.end()) {\n            auto end = find_if(it, shuffled_strings.end(), [it](const string& s) { return s.size() != it->size(); });\n            shuffle(it, end, rng);\n            it = end;\n        }\n\n        vector<vector<char>> grid(N, vector<char>(N, '.'));\n\n        // Greedy placement\n        for (const auto& s : shuffled_strings) {\n            int k = s.size();\n            vector<tuple<int, int, int, bool>> candidates;  // (fill, i, j, horiz)\n            int max_fill = -1;\n\n            // Horizontal placements\n            for (int i = 0; i < N; i++) {\n                for (int j = 0; j < N; j++) {\n                    bool valid = true;\n                    int fill = 0;\n                    for (int p = 0; p < k; p++) {\n                        int cj = (j + p) % N;\n                        char cell = grid[i][cj];\n                        if (cell != '.' && cell != s[p]) {\n                            valid = false;\n                            break;\n                        }\n                        if (cell == '.') fill++;\n                    }\n                    if (valid) {\n                        if (fill > max_fill) {\n                            max_fill = fill;\n                            candidates.clear();\n                        }\n                        if (fill == max_fill) {\n                            candidates.emplace_back(fill, i, j, true);\n                        }\n                    }\n                }\n            }\n\n            // Vertical placements\n            for (int j = 0; j < N; j++) {\n                for (int i = 0; i < N; i++) {\n                    bool valid = true;\n                    int fill = 0;\n                    for (int p = 0; p < k; p++) {\n                        int ci = (i + p) % N;\n                        char cell = grid[ci][j];\n                        if (cell != '.' && cell != s[p]) {\n                            valid = false;\n                            break;\n                        }\n                        if (cell == '.') fill++;\n                    }\n                    if (valid) {\n                        if (fill > max_fill) {\n                            max_fill = fill;\n                            candidates.clear();\n                        }\n                        if (fill == max_fill) {\n                            candidates.emplace_back(fill, i, j, false);\n                        }\n                    }\n                }\n            }\n\n            // Place if candidates exist; randomize if multiple\n            if (!candidates.empty()) {\n                auto& selected = candidates[rng() % candidates.size()];\n                auto [fill, i, j, horiz] = selected;\n                if (horiz) {\n                    for (int p = 0; p < k; p++) {\n                        int cj = (j + p) % N;\n                        if (grid[i][cj] == '.') grid[i][cj] = s[p];\n                    }\n                } else {\n                    for (int p = 0; p < k; p++) {\n                        int ci = (i + p) % N;\n                        if (grid[ci][j] == '.') grid[ci][j] = s[p];\n                    }\n                }\n            }\n        }\n\n        // Post-placement perturbations\n        int current_c = compute_c(grid, strings);\n        for (int pert = 0; pert < PERTURBATIONS; pert++) {\n            int ri = rng() % N, rj = rng() % N;\n            char original = grid[ri][rj];\n            char new_char = (original == '.') ? \"ABCDEFGH\"[rng() % 8] : '.';\n            grid[ri][rj] = new_char;\n            int new_c = compute_c(grid, strings);\n            if (new_c >= current_c) {\n                current_c = new_c;\n            } else {\n                grid[ri][rj] = original;  // Revert\n            }\n        }\n\n        // Simulated Annealing\n        double temperature = 1.0;\n        const double cooling_rate = 0.95;\n        for (int sa = 0; sa < SA_ITERATIONS; sa++) {\n            int ri = rng() % N, rj = rng() % N;\n            char original = grid[ri][rj];\n            char new_char = (original == '.') ? \"ABCDEFGH\"[rng() % 8] : '.';\n            grid[ri][rj] = new_char;\n            int new_c = compute_c(grid, strings);\n            if (new_c > current_c || (rng() % 100) < (int)(100 * exp((new_c - current_c) / temperature))) {\n                current_c = new_c;\n            } else {\n                grid[ri][rj] = original;  // Revert\n            }\n            temperature *= cooling_rate;\n        }\n\n        // Evaluate this attempt\n        int c = compute_c(grid, strings);\n        int d = compute_d(grid);\n        if (c > best_c || (c == best_c && d < best_d)) {\n            best_c = c;\n            best_d = d;\n            best_grid = grid;\n        }\n    }\n\n    // Output the best grid\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cout << best_grid[i][j];\n        }\n        cout << endl;\n    }\n\n    return 0;\n}","ahc005":"#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nconst ll INF = 1LL << 60;\n\nint main() {\n    int N, si, sj;\n    cin >> N >> si >> sj;\n    vector<string> c(N);\n    for(int i = 0; i < N; i++) cin >> c[i];\n    vector<vector<int>> cost(N, vector<int>(N, 0));\n    vector<pair<int, int>> roads;\n    for(int i = 0; i < N; i++) {\n        for(int j = 0; j < N; j++) {\n            if(c[i][j] != '#') {\n                cost[i][j] = c[i][j] - '0';\n                roads.emplace_back(i, j);\n            }\n        }\n    }\n    int R = roads.size();\n    \n    // Identify active rows and columns\n    vector<bool> active_row(N, false), active_col(N, false);\n    for(auto [i, j] : roads) {\n        active_row[i] = true;\n        active_col[j] = true;\n    }\n    \n    // Greedy selection for S\n    vector<bool> covered_row(N, false), covered_col(N, false);\n    vector<pair<int, int>> S;\n    set<pair<int, int>> road_set(roads.begin(), roads.end());\n    while(true) {\n        bool all_covered = true;\n        for(int i = 0; i < N; i++) {\n            if(active_row[i] && !covered_row[i]) all_covered = false;\n            if(active_col[i] && !covered_col[i]) all_covered = false;\n        }\n        if(all_covered) break;\n        \n        pair<int, int> best = {-1, -1};\n        int max_gain = -1;\n        int min_cost = INT_MAX;\n        for(auto p : roads) {\n            int i = p.first, j = p.second;\n            int gain = (active_row[i] && !covered_row[i]) + (active_col[j] && !covered_col[j]);\n            if(gain > max_gain || (gain == max_gain && cost[i][j] < min_cost)) {\n                max_gain = gain;\n                min_cost = cost[i][j];\n                best = p;\n            }\n        }\n        if(best.first == -1) break;\n        S.push_back(best);\n        covered_row[best.first] = true;\n        covered_col[best.second] = true;\n    }\n    // Ensure start is in S\n    bool has_start = false;\n    int start_idx = -1;\n    for(int i = 0; i < S.size(); i++) {\n        if(S[i] == make_pair(si, sj)) {\n            has_start = true;\n            start_idx = i;\n            break;\n        }\n    }\n    if(!has_start) {\n        S.push_back({si, sj});\n        start_idx = S.size() - 1;\n    }\n    \n    // Order S: use the order selected, rotated to start with start\n    vector<pair<int, int>> path;\n    for(int i = start_idx; i < S.size(); i++) path.push_back(S[i]);\n    for(int i = 0; i < start_idx; i++) path.push_back(S[i]);\n    \n    // Simple 2-opt local search on path (excluding start/end)\n    auto compute_cost = [&](const vector<pair<int, int>>& p) -> ll {\n        ll total = 0;\n        for(size_t k = 1; k < p.size(); k++) {\n            // Simplified: use grid distance as proxy for cost\n            auto [x1, y1] = p[k-1];\n            auto [x2, y2] = p[k];\n            total += abs(x1 - x2) + abs(y1 - y2);\n        }\n        return total;\n    };\n    bool improved = true;\n    int iterations = 0;\n    while(improved && iterations < 10) {  // Limit iterations\n        improved = false;\n        ll current_cost = compute_cost(path);\n        for(size_t i = 1; i < path.size() - 1; i++) {\n            for(size_t j = i + 1; j < path.size() - 1; j++) {\n                // Try swapping i and j\n                swap(path[i], path[j]);\n                ll new_cost = compute_cost(path);\n                if(new_cost < current_cost) {\n                    current_cost = new_cost;\n                    improved = true;\n                } else {\n                    swap(path[i], path[j]);  // Revert\n                }\n            }\n        }\n        iterations++;\n    }\n    path.push_back({si, sj});\n    \n    // Function to get path string\n    auto get_path = [&](pair<int, int> start, pair<int, int> end) -> string {\n        if(start == end) return \"\";\n        vector<vector<ll>> dist(N, vector<ll>(N, INF));\n        vector<vector<pair<int, int>>> prevv(N, vector<pair<int, int>>(N, {-1, -1}));\n        priority_queue<tuple<ll, int, int>, vector<tuple<ll, int, int>>, greater<>> pq;\n        dist[start.first][start.second] = 0;\n        pq.push({0, start.first, start.second});\n        while(!pq.empty()) {\n            auto [d, i, j] = pq.top(); pq.pop();\n            if(d > dist[i][j]) continue;\n            for(auto [di, dj] : vector<pair<int, int>>{{0,1}, {0,-1}, {1,0}, {-1,0}}) {\n                int ni = i + di, nj = j + dj;\n                if(ni >= 0 && ni < N && nj >= 0 && nj < N && cost[ni][nj] > 0) {\n                    ll nd = d + cost[ni][nj];\n                    if(nd < dist[ni][nj]) {\n                        dist[ni][nj] = nd;\n                        prevv[ni][nj] = {i, j};\n                        pq.push({nd, ni, nj});\n                    }\n                }\n            }\n        }\n        if(dist[end.first][end.second] == INF) return \"\";\n        vector<pair<int, int>> route;\n        pair<int, int> curr = end;\n        while(curr != make_pair(-1, -1)) {\n            route.push_back(curr);\n            curr = prevv[curr.first][curr.second];\n        }\n        reverse(route.begin(), route.end());\n        string s;\n        for(size_t k = 1; k < route.size(); k++) {\n            auto [i1, j1] = route[k-1];\n            auto [i2, j2] = route[k];\n            if(i2 == i1 + 1) s += 'D';\n            else if(i2 == i1 - 1) s += 'U';\n            else if(j2 == j1 + 1) s += 'R';\n            else if(j2 == j1 - 1) s += 'L';\n        }\n        return s;\n    };\n    \n    // Build full route\n    string ans;\n    for(size_t k = 1; k < path.size(); k++) {\n        ans += get_path(path[k-1], path[k]);\n    }\n    cout << ans << endl;\n    return 0;\n}","future-contest-2022-qual":"#include <bits/stdc++.h>\nusing namespace std;\n\nvoid dfs(int u, const vector<vector<int>>& adj, vector<int>& num_desc, vector<bool>& visited) {\n    visited[u] = true;\n    for (int v : adj[u]) {\n        if (!visited[v]) dfs(v, adj, num_desc, visited);\n        num_desc[u] += 1 + num_desc[v];\n    }\n}\n\nint main() {\n    ios::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 j = 0; j < K; ++j) {\n            cin >> d[i][j];\n        }\n    }\n    \n    vector<vector<int>> deps(N);\n    vector<vector<int>> adj(N);\n    for (int i = 0; i < R; ++i) {\n        int u, v;\n        cin >> u >> v;\n        --u; --v;\n        deps[v].push_back(u);\n        adj[u].push_back(v);\n    }\n    \n    vector<int> num_desc(N, 0);\n    vector<bool> visited(N, false);\n    for (int i = 0; i < N; ++i) {\n        if (!visited[i]) dfs(i, adj, num_desc, visited);\n    }\n    \n    vector<int> task_status(N, -1);\n    vector<int> member_status(M, -1);\n    vector<int> start_day(N, -1);\n    vector<vector<int>> est_s(M, vector<int>(K, 0));\n    \n    int current_day = 0;\n    while (true) {\n        ++current_day;\n        \n        vector<int> free_members;\n        for (int j = 0; j < M; ++j) {\n            if (member_status[j] == -1) free_members.push_back(j);\n        }\n        \n        vector<int> ready_tasks;\n        for (int i = 0; i < N; ++i) {\n            if (task_status[i] == -1) {\n                bool can_start = true;\n                for (int u : deps[i]) {\n                    if (task_status[u] != 1) {\n                        can_start = false;\n                        break;\n                    }\n                }\n                if (can_start) ready_tasks.push_back(i);\n            }\n        }\n        \n        vector<vector<long long>> est_w(ready_tasks.size(), vector<long long>(free_members.size()));\n        for (int ti = 0; ti < ready_tasks.size(); ++ti) {\n            int i = ready_tasks[ti];\n            for (int mj = 0; mj < free_members.size(); ++mj) {\n                int j = free_members[mj];\n                long long w = 0;\n                for (int k = 0; k < K; ++k) {\n                    w += max(0, d[i][k] - est_s[j][k]);\n                }\n                est_w[ti][mj] = w;\n            }\n        }\n        \n        vector<tuple<int, long long, int>> task_order;\n        for (int ti = 0; ti < ready_tasks.size(); ++ti) {\n            long long min_w = LLONG_MAX;\n            for (int mj = 0; mj < free_members.size(); ++mj) {\n                min_w = min(min_w, est_w[ti][mj]);\n            }\n            task_order.emplace_back(-num_desc[ready_tasks[ti]], min_w, ti);\n        }\n        sort(task_order.begin(), task_order.end());\n        \n        vector<pair<int, int>> assignments;\n        set<int> used_members;\n        for (auto& tup : task_order) {\n            int ti = get<2>(tup);\n            int i = ready_tasks[ti];\n            long long best_w = LLONG_MAX;\n            int best_j = -1;\n            for (int mj = 0; mj < free_members.size(); ++mj) {\n                int j = free_members[mj];\n                if (used_members.count(j)) continue;\n                if (est_w[ti][mj] < best_w) {\n                    best_w = est_w[ti][mj];\n                    best_j = j;\n                }\n            }\n            if (best_j != -1) {\n                assignments.emplace_back(best_j, i);\n                used_members.insert(best_j);\n            }\n        }\n        \n        cout << assignments.size();\n        for (auto& p : assignments) {\n            cout << \" \" << (p.first + 1) << \" \" << (p.second + 1);\n            task_status[p.second] = 0;\n            member_status[p.first] = p.second;\n            start_day[p.second] = current_day;\n        }\n        cout << \"\\n\";\n        cout.flush();\n        \n        int n;\n        cin >> n;\n        if (n == -1) break;\n        \n        vector<int> completers(n);\n        for (int i = 0; i < n; ++i) {\n            cin >> completers[i];\n            --completers[i];\n        }\n        \n        for (int f : completers) {\n            int task = member_status[f];\n            if (task == -1) continue;\n            int t = current_day - start_day[task] + 1;\n            if (t == 1) {\n                for (int k = 0; k < K; ++k) {\n                    est_s[f][k] = max(est_s[f][k], d[task][k]);\n                }\n            } else {\n                long long current_w = 0;\n                vector<int> deficit_skills;\n                for (int k = 0; k < K; ++k) {\n                    int def = max(0, d[task][k] - est_s[f][k]);\n                    current_w += def;\n                    if (def > 0) deficit_skills.push_back(k);\n                }\n                long long additional = t - current_w;\n                if (!deficit_skills.empty() && additional > 0) {\n                    double alpha = 0.6;\n                    double add_per = alpha * additional / deficit_skills.size();\n                    for (int k : deficit_skills) {\n                        est_s[f][k] += (int)add_per;\n                    }\n                }\n            }\n            task_status[task] = 1;\n            member_status[f] = -1;\n        }\n        \n        bool all_done = true;\n        for (int i = 0; i < N; ++i) {\n            if (task_status[i] != 1) all_done = false;\n        }\n        if (all_done) break;\n    }\n    \n    return 0;\n}","ahc006":"#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing pii = pair<int, int>;\n\nstruct Order {\n    int a, b, c, d, idx;\n    ll cost;\n};\n\nll dist(int x1, int y1, int x2, int y2) {\n    return abs(x1 - x2) + abs(y1 - y2);\n}\n\n// Check if a sequence violates precedence\nbool is_valid(const vector<int>& order) {\n    vector<int> pickup_pos(50, -1), delivery_pos(50, -1);\n    for (size_t i = 0; i < order.size(); ++i) {\n        int idx = order[i];\n        if (idx < 50) pickup_pos[idx] = i;\n        else delivery_pos[idx - 50] = i;\n    }\n    for (int i = 0; i < 50; ++i) {\n        if (pickup_pos[i] >= delivery_pos[i]) return false;\n    }\n    return true;\n}\n\n// Simulated Annealing with precedence and 2-opt\nvector<int> simulated_annealing(const vector<pii>& points, mt19937& rng) {\n    int n = 100;\n    vector<int> order(n);\n    for (int i = 0; i < n; ++i) order[i] = i;\n    \n    auto calc_dist = [&](const vector<int>& ord) -> ll {\n        ll total = dist(400, 400, points[ord[0]].first, points[ord[0]].second);\n        for (int i = 0; i < n - 1; ++i) {\n            total += dist(points[ord[i]].first, points[ord[i]].second,\n                          points[ord[i+1]].first, points[ord[i+1]].second);\n        }\n        total += dist(points[ord.back()].first, points[ord.back()].second, 400, 400);\n        return total;\n    };\n    \n    ll best_dist = calc_dist(order);\n    vector<int> best_order = order;\n    \n    double T = 1000.0;\n    double alpha = 0.998;\n    int max_iter = 500000;\n    \n    uniform_int_distribution<int> dist_idx(0, n - 1);\n    uniform_real_distribution<double> dist_prob(0.0, 1.0);\n    \n    for (int iter = 0; iter < max_iter; ++iter) {\n        vector<int> new_order = order;\n        int i = dist_idx(rng), j = dist_idx(rng);\n        swap(new_order[i], new_order[j]);\n        if (!is_valid(new_order)) continue;\n        \n        ll new_dist = calc_dist(new_order);\n        ll delta = new_dist - calc_dist(order);\n        \n        if (delta < 0 || dist_prob(rng) < exp(-delta / T)) {\n            order = new_order;\n            if (new_dist < best_dist) {\n                best_dist = new_dist;\n                best_order = order;\n            }\n        }\n        \n        T *= alpha;\n    }\n    \n    // 2-opt\n    auto two_opt = [&](vector<int>& ord) {\n        bool improved = true;\n        while (improved) {\n            improved = false;\n            for (int i = 0; i < n - 1; ++i) {\n                for (int j = i + 1; j < n; ++j) {\n                    vector<int> new_ord = ord;\n                    reverse(new_ord.begin() + i, new_ord.begin() + j + 1);\n                    if (is_valid(new_ord) && calc_dist(new_ord) < calc_dist(ord)) {\n                        ord = new_ord;\n                        improved = true;\n                    }\n                }\n            }\n        }\n    };\n    two_opt(best_order);\n    \n    return best_order;\n}\n\nint main() {\n    const int OFFICE_X = 400, OFFICE_Y = 400;\n    vector<Order> orders(1000);\n    for (int i = 0; i < 1000; ++i) {\n        cin >> orders[i].a >> orders[i].b >> orders[i].c >> orders[i].d;\n        orders[i].idx = i + 1;\n        orders[i].cost = dist(OFFICE_X, OFFICE_Y, orders[i].a, orders[i].b) +\n                         dist(orders[i].a, orders[i].b, orders[i].c, orders[i].d) +\n                         dist(orders[i].c, orders[i].d, OFFICE_X, OFFICE_Y);\n    }\n    \n    sort(orders.begin(), orders.end(), [](const Order& o1, const Order& o2) {\n        return o1.cost < o2.cost;\n    });\n    vector<Order> selected(orders.begin(), orders.begin() + 50);\n    \n    vector<pii> points(100);\n    for (int i = 0; i < 50; ++i) {\n        points[i] = {selected[i].a, selected[i].b};\n        points[i + 50] = {selected[i].c, selected[i].d};\n    }\n    \n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n    vector<int> best_order;\n    ll best_total_dist = LLONG_MAX;\n    for (int restart = 0; restart < 5; ++restart) {\n        auto order = simulated_annealing(points, rng);\n        ll total_dist = dist(OFFICE_X, OFFICE_Y, points[order[0]].first, points[order[0]].second);\n        for (int i = 0; i < 99; ++i) {\n            total_dist += dist(points[order[i]].first, points[order[i]].second,\n                               points[order[i+1]].first, points[order[i+1]].second);\n        }\n        total_dist += dist(points[order.back()].first, points[order.back()].second, OFFICE_X, OFFICE_Y);\n        if (total_dist < best_total_dist) {\n            best_total_dist = total_dist;\n            best_order = order;\n        }\n    }\n    \n    vector<pii> full_route;\n    full_route.emplace_back(OFFICE_X, OFFICE_Y);\n    for (int idx : best_order) full_route.push_back(points[idx]);\n    full_route.emplace_back(OFFICE_X, OFFICE_Y);\n    \n    cout << 50;\n    for (const auto& ord : selected) cout << \" \" << ord.idx;\n    cout << \"\\n\" << full_route.size();\n    for (const auto& p : full_route) cout << \" \" << p.first << \" \" << p.second;\n    cout << \"\\n\";\n    \n    return 0;\n}","ahc007":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    const int N = 400;\n    const int M = 1995;\n    vector<int> x(N), y(N);\n    for (int i = 0; i < N; i++) {\n        cin >> x[i] >> y[i];\n    }\n    vector<int> u(M), v(M);\n    for (int i = 0; i < M; i++) {\n        cin >> u[i] >> v[i];\n    }\n    // Union-Find for components\n    vector<int> parent(N);\n    for (int i = 0; i < N; i++) parent[i] = i;\n    function<int(int)> find = [&](int x) { return parent[x] == x ? x : parent[x] = find(parent[x]); };\n    auto unite = [&](int x, int y) {\n        int px = find(x), py = find(y);\n        if (px != py) {\n            parent[px] = py;\n            return true;\n        }\n        return false;\n    };\n    \n    for (int i = 0; i < M; i++) {\n        int l;\n        cin >> l;\n        int uu = u[i], vv = v[i];\n        // Adopt if connects different components\n        if (unite(uu, vv)) {\n            cout << 1 << endl;\n            fflush(stdout);\n        } else {\n            cout << 0 << endl;\n            fflush(stdout);\n        }\n    }\n    return 0;\n}","ahc008":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int DX[4] = {-1, 0, 1, 0};\nconst int DY[4] = {0, 1, 0, -1};\n\ndouble compute_score(pair<int, int> pos, const vector<vector<bool>>& passable, const vector<pair<int, int>>& pets) {\n    vector<vector<bool>> vis(31, vector<bool>(31, false));\n    queue<pair<int, int>> q;\n    q.push(pos);\n    vis[pos.first][pos.second] = true;\n    int area = 1;\n    int num_pets = 0;\n    for (auto& p : pets) {\n        if (p == pos) num_pets++;\n    }\n    while (!q.empty()) {\n        auto [x, y] = q.front();\n        q.pop();\n        for (int d = 0; d < 4; d++) {\n            int nx = x + DX[d], ny = y + DY[d];\n            if (nx >= 1 && nx <= 30 && ny >= 1 && ny <= 30 && passable[nx][ny] && !vis[nx][ny]) {\n                vis[nx][ny] = true;\n                area++;\n                for (auto& p : pets) {\n                    if (p == make_pair(nx, ny)) num_pets++;\n                }\n                q.push({nx, ny});\n            }\n        }\n    }\n    return (area / 900.0) * pow(2.0, -num_pets);\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N;\n    cin >> N;\n    vector<pair<int, int>> pets(N);\n    vector<int> pt(N);\n    for (int i = 0; i < N; i++) {\n        int x, y, t;\n        cin >> x >> y >> t;\n        pets[i] = {x, y};\n        pt[i] = t;\n    }\n    int M;\n    cin >> M;\n    vector<pair<int, int>> humans(M);\n    for (int i = 0; i < M; i++) {\n        int x, y;\n        cin >> x >> y;\n        humans[i] = {x, y};\n    }\n\n    vector<vector<bool>> passable(31, vector<bool>(31, true));\n\n    for (int turn = 0; turn < 300; turn++) {\n        vector<string> actions(M, \".\");\n        for (int i = 0; i < M; i++) {\n            double max_s = -1e9;\n            string best_act = \".\";\n            pair<int, int> cur_pos = humans[i];\n\n            // Try stay\n            {\n                double s = compute_score(cur_pos, passable, pets);\n                if (s > max_s) {\n                    max_s = s;\n                    best_act = \".\";\n                }\n            }\n\n            // Try builds\n            vector<string> builds = {\"u\", \"d\", \"l\", \"r\"};\n            for (auto act : builds) {\n                int dx = 0, dy = 0;\n                if (act == \"u\") dx = -1;\n                else if (act == \"d\") dx = 1;\n                else if (act == \"l\") dy = -1;\n                else if (act == \"r\") dy = 1;\n                int nx = cur_pos.first + dx, ny = cur_pos.second + dy;\n                bool valid = true;\n                if (nx < 1 || nx > 30 || ny < 1 || ny > 30) valid = false;\n                else {\n                    // Check for entities\n                    bool has_entity = false;\n                    for (auto& p : pets) {\n                        if (p == make_pair(nx, ny)) has_entity = true;\n                    }\n                    for (int j = 0; j < M; j++) {\n                        if (humans[j] == make_pair(nx, ny)) has_entity = true;\n                    }\n                    if (has_entity) valid = false;\n                    // Check adjacent pets\n                    for (int d = 0; d < 4; d++) {\n                        int ax = nx + DX[d], ay = ny + DY[d];\n                        if (ax >= 1 && ax <= 30 && ay >= 1 && ay <= 30) {\n                            for (auto& p : pets) {\n                                if (p == make_pair(ax, ay)) {\n                                    valid = false;\n                                    break;\n                                }\n                            }\n                        }\n                        if (!valid) break;\n                    }\n                    if (valid) {\n                        passable[nx][ny] = false;\n                        double s = compute_score(cur_pos, passable, pets);\n                        if (s > max_s) {\n                            max_s = s;\n                            best_act = act;\n                        }\n                        passable[nx][ny] = true;\n                    }\n                }\n            }\n\n            // Try moves\n            vector<string> moves = {\"U\", \"D\", \"L\", \"R\"};\n            for (auto act : moves) {\n                int dx = 0, dy = 0;\n                if (act == \"U\") dx = -1;\n                else if (act == \"D\") dx = 1;\n                else if (act == \"L\") dy = -1;\n                else if (act == \"R\") dy = 1;\n                int nx = cur_pos.first + dx, ny = cur_pos.second + dy;\n                bool valid = true;\n                if (nx < 1 || nx > 30 || ny < 1 || ny > 30) valid = false;\n                else if (!passable[nx][ny]) valid = false;\n                else {\n                    pair<int, int> new_pos = {nx, ny};\n                    double s = compute_score(new_pos, passable, pets);\n                    if (s > max_s) {\n                        max_s = s;\n                        best_act = act;\n                    }\n                }\n            }\n            actions[i] = best_act;\n        }\n\n        // Apply human moves\n        for (int i = 0; i < M; i++) {\n            string act = actions[i];\n            if (act[0] >= 'A' && act[0] <= 'Z') {  // Move\n                int dx = 0, dy = 0;\n                if (act == \"U\") dx = -1;\n                else if (act == \"D\") dx = 1;\n                else if (act == \"L\") dy = -1;\n                else if (act == \"R\") dy = 1;\n                int nx = humans[i].first + dx, ny = humans[i].second + dy;\n                humans[i] = {nx, ny};\n            }\n        }\n\n        // Apply builds\n        for (int i = 0; i < M; i++) {\n            string act = actions[i];\n            if (act[0] >= 'a' && act[0] <= 'z') {  // Build\n                int dx = 0, dy = 0;\n                if (act == \"u\") dx = -1;\n                else if (act == \"d\") dx = 1;\n                else if (act == \"l\") dy = -1;\n                else if (act == \"r\") dy = 1;\n                int nx = humans[i].first + dx, ny = humans[i].second + dy;\n                passable[nx][ny] = false;\n            }\n        }\n\n        // Output actions\n        for (auto& a : actions) cout << a;\n        cout << endl;\n        cout.flush();\n\n        // Read and apply pet moves\n        for (int i = 0; i < N; i++) {\n            string s;\n            cin >> s;\n            if (s == \".\") continue;\n            pair<int, int> pos = pets[i];\n            for (char c : s) {\n                int dx = 0, dy = 0;\n                if (c == 'U') dx = -1;\n                else if (c == 'D') dx = 1;\n                else if (c == 'L') dy = -1;\n                else if (c == 'R') dy = 1;\n                int nx = pos.first + dx, ny = pos.second + dy;\n                if (nx >= 1 && nx <= 30 && ny >= 1 && ny <= 30 && passable[nx][ny]) {\n                    pos = {nx, ny};\n                }\n            }\n            pets[i] = pos;\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), v(20);\n    for (int i = 0; i < 20; i++) cin >> h[i];\n    for (int i = 0; i < 20; i++) cin >> v[i];\n    \n    // Build adjacency list for the grid\n    vector<vector<vector<pair<int, int>>>> adj(20, vector<vector<pair<int, int>>>(20));\n    for (int i = 0; i < 20; i++) {\n        for (int j = 0; j < 20; j++) {\n            // Right\n            if (j < 19 && h[i][j] == '0') adj[i][j].push_back({i, j + 1});\n            // Left\n            if (j > 0 && h[i][j - 1] == '0') adj[i][j].push_back({i, j - 1});\n            // Down\n            if (i < 19 && v[i][j] == '0') adj[i][j].push_back({i + 1, j});\n            // Up\n            if (i > 0 && v[i - 1][j] == '0') adj[i][j].push_back({i - 1, j});\n        }\n    }\n    \n    // BFS to find shortest path\n    vector<vector<int>> dist(20, vector<int>(20, -1));\n    vector<vector<pair<int, int>>> parent(20, vector<pair<int, int>>(20, {-1, -1}));\n    queue<pair<int, int>> q;\n    q.push({si, sj});\n    dist[si][sj] = 0;\n    \n    while (!q.empty()) {\n        auto [x, y] = q.front(); q.pop();\n        for (auto [nx, ny] : adj[x][y]) {\n            if (dist[nx][ny] == -1) {\n                dist[nx][ny] = dist[x][y] + 1;\n                parent[nx][ny] = {x, y};\n                q.push({nx, ny});\n            }\n        }\n    }\n    \n    // If unreachable (shouldn't happen), output empty\n    if (dist[ti][tj] == -1) {\n        cout << \"\" << endl;\n        return 0;\n    }\n    \n    // Reconstruct the path\n    vector<pair<int, int>> path;\n    pair<int, int> cur = {ti, tj};\n    while (cur != make_pair(si, sj)) {\n        path.push_back(cur);\n        cur = parent[cur.first][cur.second];\n    }\n    path.push_back({si, sj});\n    reverse(path.begin(), path.end());\n    \n    // Generate the move string\n    string full_moves = \"\";\n    for (size_t i = 1; i < path.size(); i++) {\n        auto [px, py] = path[i - 1];\n        auto [cx, cy] = path[i];\n        if (cx == px + 1) full_moves += 'D';\n        else if (cx == px - 1) full_moves += 'U';\n        else if (cy == py + 1) full_moves += 'R';\n        else if (cy == py - 1) full_moves += 'L';\n    }\n    \n    // Determine safe loop moves from office\n    string loop = \"\";\n    bool can_up = (ti > 0 && v[ti - 1][tj] == '0');\n    bool can_down = (ti < 19 && v[ti][tj] == '0');\n    bool can_left = (tj > 0 && h[ti][tj - 1] == '0');\n    bool can_right = (tj < 19 && h[ti][tj] == '0');\n    if (can_up && can_down) loop = \"UD\";\n    else if (can_left && can_right) loop = \"LR\";\n    else if (can_up) loop = \"U\";\n    else if (can_down) loop = \"D\";\n    else if (can_left) loop = \"L\";\n    else if (can_right) loop = \"R\";\n    else loop = \"\";  // No safe moves, skip loop\n    \n    // Handle length <= 200 with repetition and adaptive loops\n    int L = full_moves.size();\n    string output;\n    if (L <= 200) {\n        int max_repeat = (L <= 100) ? 4 : 2;\n        int repeat = min(max_repeat, 200 / L);\n        for (int i = 0; i < repeat; i++) output += full_moves;\n        int rem = 200 - output.size();\n        // Add adaptive loop to fill remaining\n        for (int i = 0; i < rem; i++) {\n            if (!loop.empty()) output += loop[i % loop.size()];\n            else output += full_moves[i % L];  // Fallback to path prefix\n        }\n    } else {\n        // Truncate and add loop\n        output = full_moves.substr(0, 200);\n    }\n    \n    cout << output << endl;\n    return 0;\n}","ahc010":"#include <bits/stdc++.h>\nusing namespace std;\n\nmt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n\nconst vector<vector<int>> to = {\n    {1, 0, -1, -1},  // 0\n    {3, -1, -1, 0},  // 1\n    {-1, -1, 3, 2},  // 2\n    {-1, 2, 1, -1},  // 3\n    {1, 0, 3, 2},    // 4\n    {3, 2, 1, 0},    // 5\n    {2, -1, 0, -1},  // 6\n    {-1, 3, -1, 1}   // 7\n};\n\nconst vector<int> di = {0, -1, 0, 1};\nconst vector<int> dj = {-1, 0, 1, 0};\n\nint get_type(int t, int r) {\n    if (t <= 3) return (t + r) % 4;\n    if (t <= 5) return 4 + (t - 4 + r) % 2;\n    return 6 + (t - 6 + r) % 2;\n}\n\nlong long compute_score(const vector<vector<int>>& tiles, const vector<int>& rot) {\n    vector<vector<int>> adj(900);\n    for (int i = 0; i < 30; i++) {\n        for (int j = 0; j < 30; j++) {\n            int idx = i * 30 + j;\n            int t = tiles[i][j];\n            int r = rot[idx];\n            int et = get_type(t, r);\n            for (int d = 0; d < 4; d++) {\n                if (to[et][d] != -1) {\n                    int d2 = to[et][d];\n                    int ni = i + di[d2];\n                    int nj = j + dj[d2];\n                    if (ni >= 0 && ni < 30 && nj >= 0 && nj < 30) {\n                        int nidx = ni * 30 + nj;\n                        if (ni > i || (ni == i && nj > j)) {\n                            adj[idx].push_back(nidx);\n                            adj[nidx].push_back(idx);\n                        }\n                    }\n                }\n            }\n        }\n    }\n    vector<int> cycle_lengths;\n    vector<bool> visited(900, false);\n    vector<int> disc(900, -1);\n    vector<int> parent(900, -1);\n    int timestamp = 0;\n    function<void(int)> dfs = [&](int u) {\n        visited[u] = true;\n        disc[u] = timestamp++;\n        for (int v : adj[u]) {\n            if (!visited[v]) {\n                parent[v] = u;\n                dfs(v);\n            } else if (v != parent[u] && disc[v] < disc[u]) {\n                int len = disc[u] - disc[v] + 1;\n                cycle_lengths.push_back(len);\n            }\n        }\n    };\n    for (int i = 0; i < 900; i++) if (!visited[i]) dfs(i);\n    sort(cycle_lengths.rbegin(), cycle_lengths.rend());\n    if (cycle_lengths.size() >= 2) {\n        return (long long)cycle_lengths[0] * cycle_lengths[1];\n    }\n    return 0;\n}\n\nint main() {\n    vector<vector<int>> tiles(30, vector<int>(30));\n    for (int i = 0; i < 30; i++) {\n        string s;\n        cin >> s;\n        for (int j = 0; j < 30; j++) tiles[i][j] = s[j] - '0';\n    }\n    // Pure random sampling: 1000 samples\n    vector<int> best_rot(900);\n    long long best_score = 0;\n    for (int sample = 0; sample < 1000; sample++) {\n        vector<int> rot(900);\n        for (int &x : rot) x = rng() % 4;\n        long long score = compute_score(tiles, rot);\n        if (score > best_score) {\n            best_score = score;\n            best_rot = rot;\n        }\n    }\n    // SA on the best: 2000 iterations\n    double T = 1000.0;\n    const int sa_iterations = 2000;\n    for (int it = 0; it < sa_iterations; it++) {\n        int idx = rng() % 900;\n        int old_r = best_rot[idx];\n        int new_r = rng() % 4;\n        if (new_r == old_r) continue;\n        best_rot[idx] = new_r;\n        long long new_score = compute_score(tiles, best_rot);\n        long long delta = new_score - best_score;\n        if (delta > 0 || exp(delta / T) > (double)rng() / rng.max()) {\n            best_score = new_score;\n        } else {\n            best_rot[idx] = old_r;\n        }\n        T *= 0.999;\n    }\n    string output;\n    for (int x : best_rot) output += '0' + x;\n    cout << output << endl;\n    return 0;\n}","ahc011":"#include <bits/stdc++.h>\nusing namespace std;\n\nint N, T;\nvector<vector<int>> board;\npair<int, int> empty_pos;\n\n// Directions: 0=U, 1=D, 2=L, 3=R\nvector<pair<int, int>> dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};\nstring dir_chars = \"UDLR\";\n\n// Check if a position is valid\nbool valid(int i, int j) {\n    return i >= 0 && i < N && j >= 0 && j < N;\n}\n\n// Simulate a move: return new board and new empty pos\npair<vector<vector<int>>, pair<int, int>> simulate_move(const vector<vector<int>>& curr_board, pair<int, int> curr_empty, int dir) {\n    auto [ei, ej] = curr_empty;\n    auto [di, dj] = dirs[dir];\n    int ni = ei + di, nj = ej + dj;\n    if (!valid(ni, nj)) return {curr_board, curr_empty}; // Invalid move\n    auto new_board = curr_board;\n    swap(new_board[ei][ej], new_board[ni][nj]);\n    return {new_board, {ni, nj}};\n}\n\n// Compute tree size, connections, and component count\ntuple<int, int, int> compute_metrics(const vector<vector<int>>& curr_board) {\n    vector<vector<bool>> visited(N, vector<bool>(N, false));\n    int max_tree = 0, total_connections = 0, components = 0;\n    \n    // Directions for connections: 0=left, 1=up, 2=right, 3=down\n    vector<pair<int, int>> conn_dirs = {{0, -1}, {-1, 0}, {0, 1}, {1, 0}};\n    vector<int> masks = {1, 2, 4, 8}; // left, up, right, down\n    \n    for (int i = 0; i < N; ++i) {\n        for (int j = 0; j < N; ++j) {\n            if (curr_board[i][j] == 0 || visited[i][j]) continue;\n            \n            // BFS for component\n            queue<pair<int, int>> q;\n            q.push({i, j});\n            visited[i][j] = true;\n            int vertices = 0, edges = 0;\n            components++;\n            \n            while (!q.empty()) {\n                auto [ci, cj] = q.front(); q.pop();\n                vertices++;\n                \n                for (int d = 0; d < 4; ++d) {\n                    auto [di, dj] = conn_dirs[d];\n                    int ni = ci + di, nj = cj + dj;\n                    if (valid(ni, nj) && curr_board[ni][nj] != 0) {\n                        // Check if connection exists\n                        bool has_out = (curr_board[ci][cj] & masks[d]) != 0;\n                        bool has_in = (curr_board[ni][nj] & masks[(d + 2) % 4]) != 0;\n                        if (has_out && has_in) {\n                            edges++;\n                            if (!visited[ni][nj]) {\n                                visited[ni][nj] = true;\n                                q.push({ni, nj});\n                            }\n                        }\n                    }\n                }\n            }\n            \n            // Check if it's a tree\n            if (edges == vertices - 1) {\n                max_tree = max(max_tree, vertices);\n            }\n        }\n    }\n    \n    // Compute total possible connections\n    for (int i = 0; i < N; ++i) {\n        for (int j = 0; j < N; ++j) {\n            if (curr_board[i][j] == 0) continue;\n            for (int d = 0; d < 4; ++d) {\n                auto [di, dj] = conn_dirs[d];\n                int ni = i + di, nj = j + dj;\n                if (valid(ni, nj) && curr_board[ni][nj] != 0) {\n                    bool has_out = (curr_board[i][j] & masks[d]) != 0;\n                    bool has_in = (curr_board[ni][nj] & masks[(d + 2) % 4]) != 0;\n                    if (has_out && has_in) total_connections++;\n                }\n            }\n        }\n    }\n    total_connections /= 2;\n    \n    return {max_tree, total_connections, components};\n}\n\n// Heuristic for A* (reverted)\nint heuristic(int tree_size, int connections, int components) {\n    return (N * N - 1 - tree_size) + (components - 1) + (connections / 10);\n}\n\nint main() {\n    cin >> N >> T;\n    board.resize(N, vector<int>(N));\n    for (int i = 0; i < N; ++i) {\n        string s; cin >> s;\n        for (int j = 0; j < N; ++j) {\n            if (s[j] >= '0' && s[j] <= '9') board[i][j] = s[j] - '0';\n            else board[i][j] = 10 + (s[j] - 'a');\n            if (board[i][j] == 0) empty_pos = {i, j};\n        }\n    }\n    \n    string moves = \"\";\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n    uniform_real_distribution<double> dist(0.0, 1.0);\n    \n    auto [current_tree, current_conn, current_comp] = compute_metrics(board);\n    double temperature = 50.0 * N;\n    const double COOLING_RATE = 0.99;\n    const int MAX_ACCEPT_WORSE = 3;  // Reverted\n    int no_improve_count = 0;\n    const int STUCK_THRESHOLD = 5 + N;  // Reverted\n    bool full_tree_achieved = false;\n    const int ANNEALING_LIMIT = 1000;  // Limit annealing to early moves\n    \n    while ((int)moves.size() < T) {\n        vector<tuple<int, int, int, string, vector<vector<int>>, pair<int, int>>> candidates;\n        \n        // Evaluate valid moves\n        for (int d = 0; d < 4; ++d) {\n            auto [new_board, new_empty] = simulate_move(board, empty_pos, d);\n            if (new_empty == empty_pos) continue;\n            auto [new_tree, new_conn, new_comp] = compute_metrics(new_board);\n            candidates.emplace_back(new_tree, new_conn, new_comp, string(1, dir_chars[d]), new_board, new_empty);\n        }\n        \n        if (candidates.empty()) break;\n        \n        // Sort by tree size, then connections, then components (fewer better), then heuristic\n        sort(candidates.begin(), candidates.end(), [](const auto& a, const auto& b) {\n            int at, ac, acomp;\n            std::tie(at, ac, acomp, std::ignore, std::ignore, std::ignore) = a;\n            int bt, bc, bcomp;\n            std::tie(bt, bc, bcomp, std::ignore, std::ignore, std::ignore) = b;\n            if (at != bt) return at > bt;\n            if (ac != bc) return ac > bc;\n            if (acomp != bcomp) return acomp < bcomp;\n            return heuristic(at, ac, acomp) < heuristic(bt, bc, bcomp);\n        });\n        \n        int best_idx = 0;\n        int bt, bc, bcomp;\n        string dummy_str;\n        vector<vector<int>> dummy_board;\n        pair<int, int> dummy_pair;\n        tie(bt, bc, bcomp, dummy_str, dummy_board, dummy_pair) = candidates[best_idx];\n        \n        bool use_annealing = (no_improve_count >= STUCK_THRESHOLD && !full_tree_achieved && (int)moves.size() < ANNEALING_LIMIT);\n        \n        for (int i = 1; i < min(MAX_ACCEPT_WORSE, (int)candidates.size()); ++i) {\n            int nt, nc, ncomp;\n            tie(nt, nc, ncomp, ignore, ignore, ignore) = candidates[i];\n            bool better = (nt > bt) || (nt == bt && nc > bc) || (nt == bt && nc == bc && ncomp < bcomp);\n            if (better) {\n                best_idx = i;\n                bt = nt; bc = nc; bcomp = ncomp;\n            } else if (use_annealing && dist(rng) < exp((nt - bt) / temperature)) {\n                best_idx = i;\n                bt = nt; bc = nc; bcomp = ncomp;\n            }\n        }\n        \n        // Apply move\n        string chosen_move;\n        vector<vector<int>> new_board;\n        pair<int, int> new_empty;\n        tie(ignore, ignore, ignore, chosen_move, new_board, new_empty) = candidates[best_idx];\n        board = new_board;\n        empty_pos = new_empty;\n        moves += chosen_move;\n        \n        // Update\n        auto [new_tree, new_conn, new_comp] = compute_metrics(board);\n        if (new_tree > current_tree) {\n            no_improve_count = 0;\n        } else {\n            no_improve_count++;\n        }\n        current_tree = new_tree; current_conn = new_conn; current_comp = new_comp;\n        \n        if (current_tree == N * N - 1) {\n            full_tree_achieved = true;\n            // For full tree, only accept better or equal\n            if (new_tree < current_tree) continue; // Skip worse\n        }\n        temperature *= COOLING_RATE;\n    }\n    \n    cout << moves << endl;\n    return 0;\n}","ahc012":"#include <bits/stdc++.h>\n\nusing namespace std;\n\nint main() {\n    int N, K;\n    cin >> N >> K;\n    vector<int> a(11);\n    for(int i = 1; i <= 10; i++) cin >> a[i];\n    vector<long long> x(N), y(N);\n    for(int i = 0; i < N; i++) cin >> x[i] >> y[i];\n    \n    // Use one solution with 100 lines through random points on the circle\n    mt19937 rng(42);\n    uniform_real_distribution<double> theta_dist(0, 2 * M_PI);\n    const long long R = 10000;  // Radius of the cake\n    \n    set<pair<pair<long long, long long>, pair<long long, long long>>> used_lines;\n    vector<tuple<long long, long long, long long, long long>> lines;\n    \n    while(lines.size() < 100) {\n        double theta1 = theta_dist(rng);\n        double theta2 = theta_dist(rng);\n        long long px = round(R * cos(theta1));\n        long long py = round(R * sin(theta1));\n        long long qx = round(R * cos(theta2));\n        long long qy = round(R * sin(theta2));\n        // Normalize\n        if(px > qx || (px == qx && py > qy)) {\n            swap(px, qx);\n            swap(py, qy);\n        }\n        pair<pair<long long, long long>, pair<long long, long long>> line_key = {{px, py}, {qx, qy}};\n        if(used_lines.insert(line_key).second) {\n            lines.emplace_back(px, py, qx, qy);\n        }\n    }\n    \n    cout << lines.size() << endl;\n    for(auto [px, py, qx, qy] : lines) {\n        cout << px << \" \" << py << \" \" << qx << \" \" << qy << endl;\n    }\n    \n    return 0;\n}","ahc014":"#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n\nstruct Operation {\n    int w, x1, y1, x2, y2, x3, y3, x4, y4;\n};\n\nbool cmp(const Operation& a, const Operation& b) {\n    return a.w < b.w;\n}\n\nint main() {\n    int N, M;\n    cin >> N >> M;\n    vector<vector<bool>> has_dot(N, vector<bool>(N, false));\n    int c = (N - 1) / 2;\n    auto calc_w = [&](int x, int y) {\n        int dx = x - c;\n        int dy = y - c;\n        return dx * dx + dy * dy + 1;\n    };\n    for (int i = 0; i < M; i++) {\n        int x, y;\n        cin >> x >> y;\n        has_dot[x][y] = true;\n    }\n    vector<vector<vector<bool>>> horiz(N, vector<vector<bool>>(N, vector<bool>(N, false)));\n    vector<vector<vector<bool>>> vert(N, vector<vector<bool>>(N, vector<bool>(N, false)));\n    vector<vector<bool>> diag1(N - 1, vector<bool>(N - 1, false));\n    vector<vector<bool>> diag2(N - 1, vector<bool>(N, false));\n    vector<vector<bool>> diag3(N, vector<bool>(N - 1, false));\n    vector<vector<bool>> diag4(N, vector<bool>(N, false));\n    vector<Operation> ops;\n    for (int px = 0; px < N; px++) {\n        for (int py = 0; py < N; py++) {\n            if (has_dot[px][py]) continue;\n            int w = calc_w(px, py);\n            // axis-aligned\n            for (int dx = 1; dx <= N - 1 - px; dx++) {\n                for (int dy = 1; dy <= N - 1 - py; dy++) {\n                    int bx = px + dx, by = py;\n                    int cx = px + dx, cy = py + dy;\n                    int ex = px, ey = py + dy;\n                    if (has_dot[bx][by] && has_dot[cx][cy] && has_dot[ex][ey]) {\n                        bool ok = true;\n                        // check sides\n                        for (int i = px + 1; i < bx; i++) if (has_dot[py][i]) ok = false;\n                        for (int i = px + 1; i < bx; i++) if (has_dot[cy][i]) ok = false;\n                        for (int i = py + 1; i < cy; i++) if (has_dot[px][i]) ok = false;\n                        for (int i = py + 1; i < cy; i++) if (has_dot[bx][i]) ok = false;\n                        // check edges\n                        for (int i = px; i < bx; i++) if (horiz[py][i][px]) ok = false;\n                        for (int i = px; i < bx; i++) if (horiz[cy][i][px]) ok = false;\n                        for (int i = py; i < cy; i++) if (vert[px][i][py]) ok = false;\n                        for (int i = py; i < cy; i++) if (vert[bx][i][py]) ok = false;\n                        if (ok) {\n                            ops.push_back({w, px, py, bx, by, cx, cy, ex, ey});\n                        }\n                    }\n                }\n            }\n            // 45-degree\n            for (int a = 1;; a++) {\n                int p2x = px + a, p2y = py + a;\n                int p3x = px, p3y = py + 2 * a;\n                int p4x = px - a, p4y = py + a;\n                if (p2x >= N || p2y >= N || p3y >= N || p4x < 0 || p4y >= N) break;\n                if (has_dot[p2x][p2y] && has_dot[p3x][p3y] && has_dot[p4x][p4y]) {\n                    bool ok = true;\n                    // p1 to p2\n                    for (int i = 0; i < a; i++) if (diag1[px + i][py + i]) ok = false;\n                    // p2 to p3\n                    for (int i = 0; i < a; i++) if (diag3[p2x - i][p2y + i]) ok = false;\n                    // p3 to p4\n                    for (int i = 0; i < a; i++) if (diag4[p3x - i][p3y - i]) ok = false;\n                    // p4 to p1\n                    for (int i = 0; i < a; i++) if (diag2[p4x + i][p4y - i]) ok = false;\n                    if (ok) {\n                        ops.push_back({w, px, py, p2x, p2y, p3x, p3y, p4x, p4y});\n                    }\n                }\n            }\n        }\n    }\n    sort(ops.begin(), ops.end(), cmp);\n    vector<Operation> final_ops;\n    for (auto& op : ops) {\n        int x1 = op.x1, y1 = op.y1;\n        if (has_dot[x1][y1]) continue;\n        int x2 = op.x2, y2 = op.y2;\n        int x3 = op.x3, y3 = op.y3;\n        int x4 = op.x4, y4 = op.y4;\n        if (!has_dot[x2][y2] || !has_dot[x3][y3] || !has_dot[x4][y4]) continue;\n        // check edges again, in case\n        bool ok = true;\n        // assume axis or 45, check accordingly\n        if (x2 == x1 + (x3 - x1) && y2 == y1 + (y3 - y1)) { // axis\n            int dx = x2 - x1, dy = y3 - y1;\n            for (int i = x1; i < x1 + dx; i++) if (horiz[y1][i][x1]) ok = false;\n            for (int i = x1; i < x1 + dx; i++) if (horiz[y1 + dy][i][x1]) ok = false;\n            for (int i = y1; i < y1 + dy; i++) if (vert[x1][i][y1]) ok = false;\n            for (int i = y1; i < y1 + dy; i++) if (vert[x1 + dx][i][y1]) ok = false;\n        } else { // 45\n            int a = x2 - x1;\n            for (int i = 0; i < a; i++) if (diag1[x1 + i][y1 + i]) ok = false;\n            for (int i = 0; i < a; i++) if (diag3[x2 - i][y2 + i]) ok = false;\n            for (int i = 0; i < a; i++) if (diag4[x3 - i][y3 - i]) ok = false;\n            for (int i = 0; i < a; i++) if (diag2[x4 + i][y4 - i]) ok = false;\n        }\n        if (ok) {\n            final_ops.push_back(op);\n            has_dot[x1][y1] = true;\n            // set edges\n            if (x2 == x1 + (x3 - x1) && y2 == y1 + (y3 - y1)) {\n                int dx = x2 - x1, dy = y3 - y1;\n                for (int i = x1; i < x1 + dx; i++) horiz[y1][i][x1] = true;\n                for (int i = x1; i < x1 + dx; i++) horiz[y1 + dy][i][x1] = true;\n                for (int i = y1; i < y1 + dy; i++) vert[x1][i][y1] = true;\n                for (int i = y1; i < y1 + dy; i++) vert[x1 + dx][i][y1] = true;\n            } else {\n                int a = x2 - x1;\n                for (int i = 0; i < a; i++) diag1[x1 + i][y1 + i] = true;\n                for (int i = 0; i < a; i++) diag3[x2 - i][y2 + i] = true;\n                for (int i = 0; i < a; i++) diag4[x3 - i][y3 - i] = true;\n                for (int i = 0; i < a; i++) diag2[x4 + i][y4 - i] = true;\n            }\n        }\n    }\n    cout << final_ops.size() << endl;\n    for (auto& op : final_ops) {\n        cout << op.x1 << \" \" << op.y1 << \" \" << op.x2 << \" \" << op.y2 << \" \" << op.x3 << \" \" << op.y3 << \" \" << op.x4 << \" \" << op.y4 << endl;\n    }\n    return 0;\n}","ahc015":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 10;\nvector<vector<int>> grid(N, vector<int>(N, 0));\n\n// Directions: F (down), B (up), L (left), R (right)\nstring dirs = \"FBLR\";\n\n// Simulate tilt in direction dir\nvoid simulate_tilt(vector<vector<int>>& g, char dir) {\n    if (dir == 'R') { // Right\n        for (int i = 0; i < N; ++i) {\n            vector<int> new_row(N, 0);\n            int pos = N - 1;\n            for (int j = N - 1; j >= 0; --j) {\n                if (g[i][j] != 0) {\n                    new_row[pos] = g[i][j];\n                    --pos;\n                }\n            }\n            g[i] = new_row;\n        }\n    } else if (dir == 'L') { // Left\n        for (int i = 0; i < N; ++i) {\n            vector<int> new_row(N, 0);\n            int pos = 0;\n            for (int j = 0; j < N; ++j) {\n                if (g[i][j] != 0) {\n                    new_row[pos] = g[i][j];\n                    ++pos;\n                }\n            }\n            g[i] = new_row;\n        }\n    } else if (dir == 'F') { // Forward (down)\n        for (int j = 0; j < N; ++j) {\n            vector<int> new_col(N, 0);\n            int pos = N - 1;\n            for (int i = N - 1; i >= 0; --i) {\n                if (g[i][j] != 0) {\n                    new_col[pos] = g[i][j];\n                    --pos;\n                }\n            }\n            for (int i = 0; i < N; ++i) g[i][j] = new_col[i];\n        }\n    } else if (dir == 'B') { // Backward (up)\n        for (int j = 0; j < N; ++j) {\n            vector<int> new_col(N, 0);\n            int pos = 0;\n            for (int i = 0; i < N; ++i) {\n                if (g[i][j] != 0) {\n                    new_col[pos] = g[i][j];\n                    ++pos;\n                }\n            }\n            for (int i = 0; i < N; ++i) g[i][j] = new_col[i];\n        }\n    }\n}\n\n// DFS to compute component size\nint dfs(int x, int y, int fl, vector<vector<int>>& g, vector<vector<bool>>& vis) {\n    vis[x][y] = true;\n    int cnt = 1;\n    int dx[4] = {-1, 0, 1, 0};\n    int dy[4] = {0, 1, 0, -1};\n    for (int d = 0; d < 4; ++d) {\n        int nx = x + dx[d], ny = y + dy[d];\n        if (nx >= 0 && nx < N && ny >= 0 && ny < N && g[nx][ny] == fl && !vis[nx][ny]) {\n            cnt += dfs(nx, ny, fl, g, vis);\n        }\n    }\n    return cnt;\n}\n\n// Compute score for the grid\nlong long compute_score(const vector<vector<int>>& g) {\n    vector<int> component_sizes;\n    for (int fl = 1; fl <= 3; ++fl) {\n        vector<vector<bool>> visited(N, vector<bool>(N, false));\n        for (int i = 0; i < N; ++i) {\n            for (int j = 0; j < N; ++j) {\n                if (g[i][j] == fl && !visited[i][j]) {\n                    int size = dfs(i, j, fl, const_cast<vector<vector<int>>&>(g), visited);\n                    component_sizes.push_back(size);\n                }\n            }\n        }\n    }\n    long long sum_sq = 0;\n    for (int s : component_sizes) sum_sq += 1LL * s * s;\n    vector<long long> d(4, 0);\n    for (int i = 0; i < N; ++i) {\n        for (int j = 0; j < N; ++j) {\n            if (g[i][j]) d[g[i][j]]++;\n        }\n    }\n    long long sum_d_sq = 0;\n    for (int i = 1; i <= 3; ++i) sum_d_sq += d[i] * d[i];\n    if (sum_d_sq == 0) return 0; // Avoid division by zero\n    double score = 1e6 * sum_sq / (double)sum_d_sq;\n    return round(score);\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    vector<int> f(100);\n    for (int i = 0; i < 100; ++i) cin >> f[i];\n    \n    vector<pair<int, int>> empty;\n    for (int i = 0; i < N; ++i) {\n        for (int j = 0; j < N; ++j) {\n            empty.emplace_back(i, j);\n        }\n    }\n    \n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); // For randomization\n    \n    for (int t = 0; t < 100; ++t) {\n        int p;\n        cin >> p;\n        auto [x, y] = empty[p - 1];\n        grid[x][y] = f[t];\n        empty.erase(empty.begin() + p - 1);\n        \n        // Try all directions and collect candidates\n        long long max_score = -1;\n        vector<char> candidates;\n        for (char dir : dirs) {\n            auto temp_grid = grid;\n            simulate_tilt(temp_grid, dir);\n            long long score = compute_score(temp_grid);\n            if (score > max_score) {\n                max_score = score;\n                candidates = {dir};\n            } else if (score >= max_score * 0.95) { // Within 5% of max\n                candidates.push_back(dir);\n            }\n        }\n        \n        // Choose randomly among candidates\n        char best_dir = candidates[rng() % candidates.size()];\n        \n        // Output\n        cout << best_dir << '\\n';\n        cout.flush();\n        \n        // Simulate tilt on actual grid (skip for last step)\n        if (t < 99) {\n            simulate_tilt(grid, best_dir);\n        }\n    }\n    \n    return 0;\n}","ahc016":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int M;\n    double eps;\n    cin >> M >> eps;\n    int N;\n    if (eps <= 0.1) N = 100;\n    else if (eps <= 0.2) N = 80;\n    else if (eps <= 0.3) N = 60;\n    else N = 40;\n    cout << N << endl;\n    for (int k = 0; k < M; k++) {\n        string s = \"\";\n        for (int i = 0; i < N; i++) {\n            for (int j = i + 1; j < N; j++) {\n                if (i < k && j < k) {\n                    s += '1';\n                } else {\n                    s += '0';\n                }\n            }\n        }\n        cout << s << endl;\n    }\n    cout << flush;\n    vector<vector<int>> targets(M, vector<int>(N));\n    for (int t = 0; t < M; t++) {\n        int kt = min(t, N - 1);\n        for (int i = 0; i < N - kt; i++) {\n            targets[t][i] = 0;\n        }\n        for (int i = N - kt; i < N; i++) {\n            targets[t][i] = kt - 1;\n        }\n    }\n    for (int q = 0; q < 100; q++) {\n        string H;\n        cin >> H;\n        vector<int> deg(N, 0);\n        int idx = 0;\n        for (int i = 0; i < N; i++) {\n            for (int j = i + 1; j < N; j++) {\n                if (H[idx] == '1') {\n                    deg[i]++;\n                    deg[j]++;\n                }\n                idx++;\n            }\n        }\n        sort(deg.begin(), deg.end());\n        int best_t = 0;\n        long long min_diff = LLONG_MAX;\n        for (int t = 0; t < M; t++) {\n            long long diff = 0;\n            for (int i = 0; i < N; i++) {\n                diff += abs(deg[i] - targets[t][i]);\n            }\n            if (diff < min_diff) {\n                min_diff = diff;\n                best_t = t;\n            }\n        }\n        cout << best_t << endl;\n        cout << flush;\n    }\n    return 0;\n}","ahc017":"#include <bits/stdc++.h>\n\nusing namespace std;\n\nint main() {\n  ios::sync_with_stdio(false);\n  cin.tie(0);\n  int N, M, D, K;\n  cin >> N >> M >> D >> K;\n  vector<tuple<int, int, int, int>> edges;\n  for (int i = 0; i < M; i++) {\n    int u, v, w;\n    cin >> u >> v >> w;\n    u--;\n    v--;\n    edges.emplace_back(0, u, v, i);  // Placeholder for key, will update with midx\n  }\n  vector<pair<int, int>> coords(N);\n  for (int i = 0; i < N; i++) {\n    int x, y;\n    cin >> x >> y;\n    coords[i] = {x, y};\n  }\n  // Update edges with midx (x-coordinate of midpoint) as the sorting key\n  for (auto& [key, u, v, idx] : edges) {\n    int midx = (coords[u].first + coords[v].first) / 2;\n    key = midx;\n  }\n  // Sort edges by midx ascending (leftmost edges first)\n  sort(edges.begin(), edges.end());\n  // Compute target sizes for each day\n  int total = M;\n  vector<int> max_size(D, total / D);\n  for (int i = 0; i < total % D; i++) max_size[i]++;\n  // Assign edges\n  vector<int> assignment(M);\n  vector<int> current_size(D, 0);\n  for (auto [midx, u, v, idx] : edges) {\n    // Find the day with the smallest current size that has space\n    int best_day = -1;\n    int min_size = INT_MAX;\n    for (int d = 0; d < D; d++) {\n      if (current_size[d] < max_size[d] && current_size[d] < min_size) {\n        min_size = current_size[d];\n        best_day = d;\n      }\n    }\n    assert(best_day != -1);\n    assignment[idx] = best_day;\n    current_size[best_day]++;\n  }\n  // Output the assignment\n  for (int i = 0; i < M; i++) {\n    if (i > 0) cout << \" \";\n    cout << assignment[i] + 1;\n  }\n  cout << \"\\n\";\n  return 0;\n}","ahc019":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MAX_D = 14;\nint D;\nvector<string> f[2], r[2];\nvector<vector<vector<int>>> grid[2]; // grid[i][x][y][z]\nvector<int> b[2]; // flattened b[i], size D*D*D\n\n// Directions for 6-connectivity (face-adjacent)\nconst int dx[6] = {1, -1, 0, 0, 0, 0};\nconst int dy[6] = {0, 0, 1, -1, 0, 0};\nconst int dz[6] = {0, 0, 0, 0, 1, -1};\n\n// Canonicalize a shape: translate to (0,0,0), try 24 rotations, pick lex smallest\nstring canonicalize(const vector<tuple<int,int,int>>& shape) {\n    if (shape.empty()) return \"\";\n    // Find min coords\n    int minx = INT_MAX, miny = INT_MAX, minz = INT_MAX;\n    for (auto [x,y,z] : shape) {\n        minx = min(minx, x);\n        miny = min(miny, y);\n        minz = min(minz, z);\n    }\n    // Translate\n    vector<tuple<int,int,int>> trans;\n    for (auto [x,y,z] : shape) {\n        trans.emplace_back(x - minx, y - miny, z - minz);\n    }\n    // Generate 24 rotations (90 deg around x,y,z)\n    vector<string> candidates;\n    for (int rx = 0; rx < 4; ++rx) {\n        for (int ry = 0; ry < 4; ++ry) {\n            for (int rz = 0; rz < 4; ++rz) {\n                vector<tuple<int,int,int>> rot;\n                for (auto [x,y,z] : trans) {\n                    // Apply rotations\n                    int nx = x, ny = y, nz = z;\n                    for (int i = 0; i < rx; ++i) { int t = ny; ny = -nz; nz = t; } // Rotate x\n                    for (int i = 0; i < ry; ++i) { int t = nz; nz = -nx; nx = t; } // Rotate y\n                    for (int i = 0; i < rz; ++i) { int t = nx; nx = -ny; ny = t; } // Rotate z\n                    rot.emplace_back(nx, ny, nz);\n                }\n                // Sort and stringify\n                sort(rot.begin(), rot.end());\n                string s;\n                for (auto [x,y,z] : rot) {\n                    s += to_string(x) + \",\" + to_string(y) + \",\" + to_string(z) + \";\";\n                }\n                candidates.push_back(s);\n            }\n        }\n    }\n    return *min_element(candidates.begin(), candidates.end());\n}\n\n// DFS to find connected component\nvoid dfs(int i, int x, int y, int z, vector<tuple<int,int,int>>& component, vector<vector<vector<bool>>>& visited) {\n    visited[x][y][z] = true;\n    component.emplace_back(x, y, z);\n    for (int d = 0; d < 6; ++d) {\n        int nx = x + dx[d], ny = y + dy[d], nz = z + dz[d];\n        if (nx >= 0 && nx < D && ny >= 0 && ny < D && nz >= 0 && nz < D &&\n            grid[i][nx][ny][nz] && !visited[nx][ny][nz]) {\n            dfs(i, nx, ny, nz, component, visited);\n        }\n    }\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    cin >> D;\n    f[0].resize(D); f[1].resize(D);\n    r[0].resize(D); r[1].resize(D);\n    for (int i = 0; i < 2; ++i) {\n        for (int k = 0; k < D; ++k) cin >> f[i][k];\n        for (int k = 0; k < D; ++k) cin >> r[i][k];\n    }\n    \n    // Build grids\n    grid[0].assign(D, vector<vector<int>>(D, vector<int>(D, 0)));\n    grid[1].assign(D, vector<vector<int>>(D, vector<int>(D, 0)));\n    for (int i = 0; i < 2; ++i) {\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 (f[i][z][x] == '1' && r[i][z][y] == '1') {\n                        grid[i][x][y][z] = 1;\n                    }\n                }\n            }\n        }\n    }\n    \n    // Find components for each grid\n    vector<vector<vector<tuple<int,int,int>>>> components(2);\n    for (int i = 0; i < 2; ++i) {\n        vector<vector<vector<bool>>> visited(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                    if (grid[i][x][y][z] && !visited[x][y][z]) {\n                        vector<tuple<int,int,int>> comp;\n                        dfs(i, x, y, z, comp, visited);\n                        components[i].push_back(comp);\n                    }\n                }\n            }\n        }\n    }\n    \n    // Assign block IDs: unique per component, with cross-object sharing\n    map<string, int> shape_to_id;\n    int n = 0;\n    b[0].assign(D * D * D, 0);\n    b[1].assign(D * D * D, 0);\n    \n    // Process i=0: assign new IDs to each component\n    for (const auto& comp : components[0]) {\n        string shape = canonicalize(comp);\n        int id = ++n;\n        shape_to_id[shape] = id;  // Record for potential reuse in i=1\n        for (auto [x,y,z] : comp) {\n            b[0][x * D * D + y * D + z] = id;\n        }\n    }\n    \n    // Process i=1: assign unique IDs, reuse from i=0 if shape matches and ID not used yet in i=1\n    set<int> used_ids_in_1;\n    for (const auto& comp : components[1]) {\n        string shape = canonicalize(comp);\n        int id;\n        if (shape_to_id.find(shape) != shape_to_id.end()) {\n            int potential_id = shape_to_id[shape];\n            if (used_ids_in_1.find(potential_id) == used_ids_in_1.end()) {\n                id = potential_id;  // Reuse\n                used_ids_in_1.insert(id);\n            } else {\n                id = ++n;  // New, since ID already used in i=1\n            }\n        } else {\n            id = ++n;  // New\n        }\n        for (auto [x,y,z] : comp) {\n            b[1][x * D * D + y * D + z] = id;\n        }\n    }\n    \n    // Output\n    cout << n << '\\n';\n    for (int val : b[0]) cout << val << ' ';\n    cout << '\\n';\n    for (int val : b[1]) cout << val << ' ';\n    cout << '\\n';\n    \n    return 0;\n}","ahc020":"#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing ld = long double;\n\nstruct Edge {\n    int u, v, w, idx;\n    bool operator<(const Edge& other) const {\n        return w < other.w;\n    }\n};\n\nint find(vector<int>& parent, int x) {\n    if (parent[x] != x) parent[x] = find(parent, parent[x]);\n    return parent[x];\n}\n\nvoid unite(vector<int>& parent, vector<int>& rank, int x, int y) {\n    int px = find(parent, x), py = find(parent, y);\n    if (px != py) {\n        if (rank[px] < rank[py]) swap(px, py);\n        parent[py] = px;\n        if (rank[px] == rank[py]) rank[px]++;\n    }\n}\n\nld compute_dist(pair<int, int> a, pair<int, int> b) {\n    ld dx = a.first - b.first;\n    ld dy = a.second - b.second;\n    return hypot(dx, dy);\n}\n\nll compute_cost(const vector<int>& P, const vector<int>& B, const vector<Edge>& edges) {\n    ll s = 0;\n    for (int p : P) s += (ll)p * p;\n    for (size_t i = 0; i < B.size(); ++i) {\n        if (B[i]) s += edges[i].w;\n    }\n    return s;\n}\n\nbool is_covered(int k, const vector<pair<int, int>>& residents, const vector<pair<int, int>>& stations, const vector<int>& P, const vector<int>& reachable) {\n    for (int i : reachable) {\n        if (P[i] > 0 && compute_dist(residents[k], stations[i]) <= P[i]) return true;\n    }\n    return false;\n}\n\nvector<int> get_reachable(int start, int N, const vector<Edge>& edges, const vector<int>& B) {\n    vector<vector<int>> adj(N);\n    for (auto& e : edges) {\n        if (B[e.idx]) {\n            adj[e.u].push_back(e.v);\n            adj[e.v].push_back(e.u);\n        }\n    }\n    vector<int> reachable;\n    vector<bool> vis(N, false);\n    queue<int> q;\n    q.push(start);\n    vis[start] = true;\n    while (!q.empty()) {\n        int u = q.front(); q.pop();\n        reachable.push_back(u);\n        for (int v : adj[u]) {\n            if (!vis[v]) {\n                vis[v] = true;\n                q.push(v);\n            }\n        }\n    }\n    return reachable;\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    vector<pair<int, int>> stations(N);\n    for (int i = 0; i < N; ++i) {\n        cin >> stations[i].first >> stations[i].second;\n    }\n    vector<Edge> edges(M);\n    for (int i = 0; i < M; ++i) {\n        cin >> edges[i].u >> edges[i].v >> edges[i].w;\n        edges[i].u--; edges[i].v--; // 0-based\n        edges[i].idx = i;\n    }\n    vector<pair<int, int>> residents(K);\n    for (int i = 0; i < K; ++i) {\n        cin >> residents[i].first >> residents[i].second;\n    }\n\n    // Precompute distances\n    vector<vector<ld>> dist(K, vector<ld>(N));\n    for (int k = 0; k < K; ++k) {\n        for (int i = 0; i < N; ++i) {\n            dist[k][i] = compute_dist(residents[k], stations[i]);\n        }\n    }\n\n    // Precompute top-3 candidates per resident\n    vector<vector<pair<ld, int>>> candidates(K);\n    for (int k = 0; k < K; ++k) {\n        vector<pair<ld, int>> cands;\n        for (int i = 0; i < N; ++i) {\n            cands.emplace_back(dist[k][i], i);\n        }\n        sort(cands.begin(), cands.end());\n        for (int j = 0; j < min(3, N); ++j) {\n            candidates[k].push_back(cands[j]);\n        }\n    }\n\n    // Initial assignment\n    vector<int> closest(K);\n    for (int k = 0; k < K; ++k) {\n        closest[k] = candidates[k][0].second;\n    }\n\n    // Active stations\n    set<int> active_stations;\n    for (int k = 0; k < K; ++k) active_stations.insert(closest[k]);\n    active_stations.insert(0); // Vertex 1\n\n    // Compute MST\n    vector<int> parent(N), rankk(N, 0);\n    for (int i = 0; i < N; ++i) parent[i] = i;\n    sort(edges.begin(), edges.end());\n    vector<int> B(M, 0);\n    for (auto& e : edges) {\n        if (active_stations.count(e.u) && active_stations.count(e.v)) {\n            int pu = find(parent, e.u), pv = find(parent, e.v);\n            if (pu != pv) {\n                unite(parent, rankk, e.u, e.v);\n                B[e.idx] = 1;\n            }\n        }\n    }\n\n    // Set initial P_i\n    vector<int> P(N, 0);\n    vector<vector<ld>> assigned_dists(N);\n    for (int k = 0; k < K; ++k) {\n        int i = closest[k];\n        assigned_dists[i].push_back(dist[k][i]);\n    }\n    for (int i = 0; i < N; ++i) {\n        if (!assigned_dists[i].empty()) {\n            ld max_d = *max_element(assigned_dists[i].begin(), assigned_dists[i].end());\n            P[i] = min(5000, (int)ceil(max_d));\n        }\n    }\n\n    // Simplified local search (no annealing, no MST in loop)\n    ll current_cost = compute_cost(P, B, edges);\n    const int max_iters = 100;  // Reduced\n    for (int iter = 0; iter < max_iters; ++iter) {\n        bool improved = false;\n        for (int k = 0; k < K; ++k) {\n            int current_i = closest[k];\n            ld current_d = dist[k][current_i];\n            for (auto& cand : candidates[k]) {\n                ld next_d = cand.first;\n                int next_i = cand.second;\n                if (next_d > 5000 || next_i == current_i) continue;\n\n                // Simulate P_i change only\n                vector<int> new_P = P;\n                vector<vector<ld>> new_assigned = assigned_dists;\n\n                // Remove\n                new_assigned[current_i].erase(remove(new_assigned[current_i].begin(), new_assigned[current_i].end(), current_d), new_assigned[current_i].end());\n                if (new_assigned[current_i].empty()) {\n                    new_P[current_i] = 0;\n                } else {\n                    ld max_d = *max_element(new_assigned[current_i].begin(), new_assigned[current_i].end());\n                    new_P[current_i] = min(5000, (int)ceil(max_d));\n                }\n\n                // Add\n                new_assigned[next_i].push_back(next_d);\n                ld max_d_next = *max_element(new_assigned[next_i].begin(), new_assigned[next_i].end());\n                new_P[next_i] = min(5000, (int)ceil(max_d_next));\n\n                ll new_cost = 0;\n                for (int p : new_P) new_cost += (ll)p * p;\n                // Assume B cost unchanged for simplicity\n\n                if (new_cost < current_cost) {\n                    closest[k] = next_i;\n                    P = new_P;\n                    assigned_dists = new_assigned;\n                    current_cost = new_cost;\n                    improved = true;\n                } else {\n                    // Revert\n                    new_assigned[current_i].push_back(current_d);\n                    new_assigned[next_i].pop_back();\n                }\n            }\n        }\n        if (!improved) break;\n    }\n\n    // Recompute MST once at the end\n    active_stations.clear();\n    for (int k = 0; k < K; ++k) active_stations.insert(closest[k]);\n    active_stations.insert(0);\n    fill(B.begin(), B.end(), 0);\n    vector<int> new_parent(N), new_rankk(N, 0);\n    for (int i = 0; i < N; ++i) new_parent[i] = i;\n    for (auto& e : edges) {\n        if (active_stations.count(e.u) && active_stations.count(e.v)) {\n            int pu = find(new_parent, e.u), pv = find(new_parent, e.v);\n            if (pu != pv) {\n                unite(new_parent, new_rankk, e.u, e.v);\n                B[e.idx] = 1;\n            }\n        }\n    }\n\n    // Coverage check and fix\n    vector<int> reachable = get_reachable(0, N, edges, B);\n    for (int k = 0; k < K; ++k) {\n        if (!is_covered(k, residents, stations, P, reachable)) {\n            ld min_d = 1e18;\n            int best_i = -1;\n            for (int i : reachable) {\n                if (P[i] > 0 && dist[k][i] < min_d) {\n                    min_d = dist[k][i];\n                    best_i = i;\n                }\n            }\n            if (best_i != -1) {\n                int old_i = closest[k];\n                closest[k] = best_i;\n                assigned_dists[old_i].erase(remove(assigned_dists[old_i].begin(), assigned_dists[old_i].end(), dist[k][old_i]), assigned_dists[old_i].end());\n                assigned_dists[best_i].push_back(dist[k][best_i]);\n                ld max_d = *max_element(assigned_dists[best_i].begin(), assigned_dists[best_i].end());\n                P[best_i] = min(5000, (int)ceil(max_d));\n                if (assigned_dists[old_i].empty()) P[old_i] = 0;\n            }\n        }\n    }\n\n    // Output\n    for (int i = 0; i < N; ++i) {\n        if (i > 0) cout << \" \";\n        cout << P[i];\n    }\n    cout << \"\\n\";\n    for (int i = 0; i < M; ++i) {\n        if (i > 0) cout << \" \";\n        cout << B[i];\n    }\n    cout << \"\\n\";\n\n    return 0;\n}","ahc021":"#include <bits/stdc++.h>\n\nusing namespace std;\n\nint main() {\n    vector<vector<int>> pyramid(30);\n    for (int x = 0; x < 30; x++) {\n        pyramid[x].resize(x + 1);\n        for (int y = 0; y <= x; y++) {\n            cin >> pyramid[x][y];\n        }\n    }\n\n    vector<tuple<int, int, int, int>> ops;\n\n    // Recursive lambda for sift_down\n    auto sift_down = [&](auto& self, int x, int y) -> void {\n        if (x >= 29) return;\n        int val = pyramid[x][y];\n        int lx = x + 1, ly = y;\n        int rx = x + 1, ry = y + 1;\n        int lval = pyramid[lx][ly];\n        int rval = pyramid[rx][ry];\n        if (lval >= val && rval >= val) return;\n        if (lval < val && lval <= rval) {\n            // Swap with left child\n            swap(pyramid[x][y], pyramid[lx][ly]);\n            ops.push_back({x, y, lx, ly});\n            self(self, lx, ly);\n        } else if (rval < val) {\n            // Swap with right child\n            swap(pyramid[x][y], pyramid[rx][ry]);\n            ops.push_back({x, y, rx, ry});\n            self(self, rx, ry);\n        }\n    };\n\n    // Build the heap by heapifying from bottom to top\n    for (int x = 28; x >= 0; x--) {\n        for (int y = 0; y <= x; y++) {\n            sift_down(sift_down, x, y);\n        }\n    }\n\n    // Output the result\n    cout << ops.size() << endl;\n    for (auto [a, b, c, d] : ops) {\n        cout << a << \" \" << b << \" \" << c << \" \" << d << endl;\n    }\n\n    return 0;\n}","toyota2023summer-final":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int D, N;\n    cin >> D >> N;\n    vector<vector<int>> obs(D, vector<int>(D, 0));\n    for (int k = 0; k < N; ++k) {\n        int ri, rj;\n        cin >> ri >> rj;\n        obs[ri][rj] = 1;\n    }\n    int entrance_i = 0, entrance_j = (D - 1) / 2;\n    obs[entrance_i][entrance_j] = 1; // Block entrance for containers\n\n    // Precompute distances from entrance, avoiding obstacles\n    vector<vector<int>> dist(D, vector<int>(D, -1));\n    queue<pair<int, int>> q;\n    q.push({entrance_i, entrance_j});\n    dist[entrance_i][entrance_j] = 0;\n    vector<pair<int, int>> dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};\n    while (!q.empty()) {\n        auto [i, j] = q.front();\n        q.pop();\n        for (auto [di, dj] : dirs) {\n            int ni = i + di, nj = j + dj;\n            if (ni >= 0 && ni < D && nj >= 0 && nj < D && obs[ni][nj] == 0 && dist[ni][nj] == -1) {\n                dist[ni][nj] = dist[i][j] + 1;\n                q.push({ni, nj});\n            }\n        }\n    }\n\n    int M = D * D - 1 - N; // Number of containers\n    vector<vector<int>> placed(D, vector<int>(D, -1)); // -1: empty, else: container number\n\n    // Placement phase\n    for (int d = 0; d < M; ++d) {\n        int t_d;\n        cin >> t_d;\n        // BFS to find reachable empty squares\n        vector<vector<int>> vis(D, vector<int>(D, 0));\n        queue<pair<int, int>> qq;\n        qq.push({entrance_i, entrance_j});\n        vis[entrance_i][entrance_j] = 1;\n        vector<tuple<int, int, int>> candidates; // dist, i, j\n        while (!qq.empty()) {\n            auto [i, j] = qq.front();\n            qq.pop();\n            for (auto [di, dj] : dirs) {\n                int ni = i + di, nj = j + dj;\n                if (ni >= 0 && ni < D && nj >= 0 && nj < D && obs[ni][nj] == 0 && placed[ni][nj] == -1 && !vis[ni][nj]) {\n                    vis[ni][nj] = 1;\n                    qq.push({ni, nj});\n                    candidates.emplace_back(-dist[ni][nj], ni, nj); // Negative for max-heap effect\n                }\n            }\n        }\n        // Choose the candidate with max dist, then min i, min j\n        sort(candidates.begin(), candidates.end()); // Sorts by -dist, then i, then j\n        auto [_, pi, pj] = candidates[0];\n        placed[pi][pj] = t_d;\n        cout << pi << \" \" << pj << endl;\n    }\n\n    // Removal phase\n    vector<pair<int, int>> order;\n    while (true) {\n        // BFS to find accessible containers\n        vector<vector<int>> vis(D, vector<int>(D, 0));\n        queue<pair<int, int>> qq;\n        qq.push({entrance_i, entrance_j});\n        vis[entrance_i][entrance_j] = 1;\n        vector<pair<int, int>> acc; // {number, id} where id = i*D + j\n        while (!qq.empty()) {\n            auto [i, j] = qq.front();\n            qq.pop();\n            for (auto [di, dj] : dirs) {\n                int ni = i + di, nj = j + dj;\n                if (ni >= 0 && ni < D && nj >= 0 && nj < D && obs[ni][nj] == 0 && placed[ni][nj] == -1 && !vis[ni][nj]) {\n                    vis[ni][nj] = 1;\n                    qq.push({ni, nj});\n                    if (placed[ni][nj] != -1) {\n                        acc.emplace_back(placed[ni][nj], ni * D + nj);\n                    }\n                }\n            }\n        }\n        if (acc.empty()) break;\n        // Remove the smallest number\n        sort(acc.begin(), acc.end());\n        auto [num, id] = acc[0];\n        int ii = id / D, jj = id % D;\n        order.emplace_back(ii, jj);\n        placed[ii][jj] = -1;\n    }\n\n    // Output removal order\n    for (auto [qi, qj] : order) {\n        cout << qi << \" \" << qj << endl;\n    }\n\n    return 0;\n}","ahc024":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int n, m;\n    cin >> n >> m;\n    vector<vector<int>> c(n, vector<int>(n));\n    for(int i = 0; i < n; i++) {\n        for(int j = 0; j < n; j++) {\n            cin >> c[i][j];\n        }\n    }\n    \n    vector<vector<int>> adj(m + 1, vector<int>(m + 1, 0));\n    vector<int> is_boundary(m + 1, 0);\n    vector<int> di = {0, 1, 0, -1};\n    vector<int> dj = {1, 0, -1, 0};\n    \n    // Compute adjacencies and boundary info\n    for(int i = 0; i < n; i++) {\n        for(int j = 0; j < n; j++) {\n            int col = c[i][j];\n            if(i == 0 || i == n - 1 || j == 0 || j == n - 1) {\n                is_boundary[col] = 1;\n                adj[col][0] = adj[0][col] = 1;\n            }\n            for(int d = 0; d < 4; d++) {\n                int ni = i + di[d], nj = j + dj[d];\n                if(ni >= 0 && ni < n && nj >= 0 && nj < n) {\n                    int ncol = c[ni][nj];\n                    if(ncol != col) {\n                        adj[col][ncol] = adj[ncol][col] = 1;\n                    }\n                }\n            }\n        }\n    }\n    \n    // Function to validate the grid\n    auto validate = [&](const vector<vector<int>>& grid) -> bool {\n        // Check connectivity for each ward\n        for(int cc = 1; cc <= m; cc++) {\n            vector<vector<bool>> visited(n, vector<bool>(n, false));\n            bool found = false;\n            function<void(int, int)> dfs = [&](int i, int j) {\n                if(i < 0 || i >= n || j < 0 || j >= n || visited[i][j] || grid[i][j] != cc) return;\n                visited[i][j] = true;\n                found = true;\n                for(int d = 0; d < 4; d++) dfs(i + di[d], j + dj[d]);\n            };\n            for(int i = 0; i < n && !found; i++) {\n                for(int j = 0; j < n && !found; j++) {\n                    if(grid[i][j] == cc) dfs(i, j);\n                }\n            }\n            if(!found) return false;\n        }\n        // Check adjacencies\n        vector<vector<int>> new_adj(m + 1, vector<int>(m + 1, 0));\n        for(int i = 0; i < n; i++) {\n            for(int j = 0; j < n; j++) {\n                int col = grid[i][j];\n                for(int d = 0; d < 4; d++) {\n                    int ni = i + di[d], nj = j + dj[d];\n                    if(ni >= 0 && ni < n && nj >= 0 && nj < n) {\n                        int ncol = grid[ni][nj];\n                        if(ncol != col) {\n                            new_adj[col][ncol] = new_adj[ncol][col] = 1;\n                        }\n                    } else if(col != 0) {\n                        // Adjacent to outside 0\n                        new_adj[col][0] = new_adj[0][col] = 1;\n                    }\n                }\n            }\n        }\n        for(int a = 0; a <= m; a++) {\n            for(int b = 0; b <= m; b++) {\n                if(new_adj[a][b] != adj[a][b]) return false;\n            }\n        }\n        return true;\n    };\n    \n    // Prepare positions sorted by distance to center\n    int center_i = n / 2, center_j = n / 2;\n    vector<pair<int, int>> positions;\n    for(int i = 0; i < n; i++) {\n        for(int j = 0; j < n; j++) {\n            int dist = abs(i - center_i) + abs(j - center_j);\n            positions.emplace_back(dist, i * n + j);\n        }\n    }\n    sort(positions.begin(), positions.end());\n    \n    // Separate boundary and internal positions\n    vector<pair<int, int>> boundary_pos, internal_pos;\n    for(auto& p : positions) {\n        int idx = p.second;\n        int i = idx / n, j = idx % n;\n        bool on_bound = (i == 0 || i == n - 1 || j == 0 || j == n - 1);\n        if(on_bound) boundary_pos.push_back(p);\n        else internal_pos.push_back(p);\n    }\n    \n    // Best grid tracking, start with original\n    vector<vector<int>> best_grid = c;\n    int best_score = 0;\n    for(auto& row : c) for(int val : row) if(val == 0) best_score++;\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n    \n    // Multiple attempts\n    const int max_attempts = 20;\n    for(int attempt = 0; attempt < max_attempts; attempt++) {\n        // Sort wards by is_boundary descending, then shuffle\n        vector<pair<int, int>> order;\n        for(int cc = 1; cc <= m; cc++) order.emplace_back(is_boundary[cc], cc);\n        sort(order.rbegin(), order.rend());\n        vector<int> wards;\n        for(auto& p : order) wards.push_back(p.second);\n        shuffle(wards.begin(), wards.end(), rng);\n        \n        // Grid for this attempt\n        vector<vector<int>> grid(n, vector<int>(n, 0));\n        vector<set<pair<int, int>>> ward_cells(m + 1);\n        vector<bool> used(n * n, false);\n        bool valid_attempt = true;\n        \n        // Place wards\n        for(int cc : wards) {\n            bool placed = false;\n            vector<pair<int, int>> cand = is_boundary[cc] ? positions : internal_pos;\n            // Try single cell\n            for(auto& p : cand) {\n                int idx = p.second;\n                int i = idx / n, j = idx % n;\n                if(used[idx]) continue;\n                bool on_bound = (i == 0 || i == n - 1 || j == 0 || j == n - 1);\n                if(is_boundary[cc]) {\n                    if(!on_bound) continue;  // Must be on boundary for single cell\n                } else {\n                    if(on_bound) continue;  // Must not be on boundary\n                }\n                bool ok = true;\n                for(int d = 1; d <= m; d++) {\n                    if(ward_cells[d].empty()) continue;\n                    for(auto& cell : ward_cells[d]) {\n                        int pi = cell.first, pj = cell.second;\n                        int dist = abs(i - pi) + abs(j - pj);\n                        if(adj[cc][d]) {\n                            if(dist > 1) ok = false;\n                        } else {\n                            if(dist == 1) ok = false;\n                        }\n                    }\n                }\n                if(ok) {\n                    ward_cells[cc].insert({i, j});\n                    used[idx] = true;\n                    grid[i][j] = cc;\n                    placed = true;\n                    break;\n                }\n            }\n            if(!placed) {\n                // Try 2-cell expansion\n                for(auto& p : cand) {\n                    int idx = p.second;\n                    int i = idx / n, j = idx % n;\n                    if(used[idx]) continue;\n                    bool on_bound1 = (i == 0 || i == n - 1 || j == 0 || j == n - 1);\n                    if(is_boundary[cc]) {\n                        if(!on_bound1) continue;  // First cell must be on boundary for boundary wards\n                    } else {\n                        if(on_bound1) continue;  // First cell must not be on boundary for internal\n                    }\n                    for(int d = 0; d < 4; d++) {\n                        int ni = i + di[d], nj = j + dj[d];\n                        if(ni >= 0 && ni < n && nj >= 0 && nj < n && !used[ni * n + nj] && grid[ni][nj] == 0) {\n                            bool on_bound2 = (ni == 0 || ni == n - 1 || nj == 0 || nj == n - 1);\n                            set<pair<int, int>> temp_cells = {{i, j}, {ni, nj}};\n                            bool shape_ok = true;\n                            bool has_bound = on_bound1 || on_bound2;\n                            if(is_boundary[cc]) {\n                                if(!has_bound) shape_ok = false;  // At least one on boundary\n                            } else {\n                                if(has_bound) shape_ok = false;  // None on boundary\n                            }\n                            if(!shape_ok) continue;\n                            bool ok = true;\n                            for(int dd = 1; dd <= m; dd++) {\n                                if(ward_cells[dd].empty()) continue;\n                                for(auto& cell : ward_cells[dd]) {\n                                    for(auto& tcell : temp_cells) {\n                                        int pi = cell.first, pj = cell.second;\n                                        int dist = abs(tcell.first - pi) + abs(tcell.second - pj);\n                                        if(adj[cc][dd]) {\n                                            if(dist > 1) ok = false;\n                                        } else {\n                                            if(dist == 1) ok = false;\n                                        }\n                                    }\n                                }\n                            }\n                            if(ok) {\n                                ward_cells[cc] = temp_cells;\n                                used[idx] = true;\n                                used[ni * n + nj] = true;\n                                grid[i][j] = cc;\n                                grid[ni][nj] = cc;\n                                placed = true;\n                                break;\n                            }\n                        }\n                    }\n                    if(placed) break;\n                }\n            }\n            if(!placed) {\n                valid_attempt = false;\n                break;\n            }\n        }\n        \n        if(valid_attempt && validate(grid)) {\n            int score = 0;\n            for(auto& row : grid) for(int val : row) if(val == 0) score++;\n            if(score > best_score) {\n                best_score = score;\n                best_grid = grid;\n            }\n        }\n    }\n    \n    // Output the best valid grid\n    for(int i = 0; i < n; i++) {\n        for(int j = 0; j < n; j++) {\n            if(j > 0) cout << \" \";\n            cout << best_grid[i][j];\n        }\n        cout << \"\\n\";\n    }\n    \n    return 0;\n}","ahc025":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int N, D, Q;\n    cin >> N >> D >> Q;\n    \n    vector<int> items(N);\n    iota(items.begin(), items.end(), 0);  // items[i] = i\n    \n    int query_count = 0;\n    \n    // Comparator for sorting: compares weights of two items using a query\n    auto cmp = [&](int a, int b) -> bool {\n        cout << 1 << \" \" << 1 << \" \" << a << \" \" << b << endl;\n        cout.flush();\n        string res;\n        cin >> res;\n        query_count++;\n        if (res == \"<\") return true;   // a < b\n        if (res == \">\") return false;  // a > b\n        return true;  // a == b, arbitrary\n    };\n    \n    // Sort items by weight (lightest to heaviest)\n    sort(items.begin(), items.end(), cmp);\n    \n    // Assign groups using round-robin on the sorted list\n    vector<int> group(N);\n    vector<vector<int>> groups(D);\n    vector<int> pos(N);\n    for (int i = 0; i < N; i++) {\n        pos[items[i]] = i;  // pos[item] = position in sorted list\n        int g = i % D;\n        group[items[i]] = g;\n        groups[g].push_back(items[i]);\n    }\n    \n    // Refinements: Balance groups using remaining queries\n    int g1 = 0, g2 = 1;\n    while (query_count < Q) {\n        if (groups[g1].empty() || groups[g2].empty()) {\n            // Skip if a group is empty; do dummy query\n            cout << 1 << \" \" << 1 << \" \" << 0 << \" \" << 1 << endl;\n            cout.flush();\n            string dummy;\n            cin >> dummy;\n            query_count++;\n            continue;\n        }\n        \n        // Compare sums of groups g1 and g2\n        cout << groups[g1].size() << \" \" << groups[g2].size();\n        for (int x : groups[g1]) cout << \" \" << x;\n        for (int x : groups[g2]) cout << \" \" << x;\n        cout << endl;\n        cout.flush();\n        string res;\n        cin >> res;\n        query_count++;\n        \n        if (res == \">\") {  // groups[g1] > groups[g2]\n            // Move heaviest item from g1 to g2\n            auto it = max_element(groups[g1].begin(), groups[g1].end(), [&](int a, int b) {\n                return pos[a] < pos[b];\n            });\n            int item = *it;\n            groups[g1].erase(it);\n            groups[g2].push_back(item);\n            group[item] = g2;\n        } else if (res == \"<\") {  // groups[g1] < groups[g2]\n            // Move heaviest item from g2 to g1\n            auto it = max_element(groups[g2].begin(), groups[g2].end(), [&](int a, int b) {\n                return pos[a] < pos[b];\n            });\n            int item = *it;\n            groups[g2].erase(it);\n            groups[g1].push_back(item);\n            group[item] = g1;\n        }  // If ==, do nothing\n        \n        // Cycle to next pair of groups\n        g2 = (g2 + 1) % D;\n        if (g2 == g1) g2 = (g2 + 1) % D;\n    }\n    \n    // Output the group assignment\n    for (int i = 0; i < N; i++) {\n        if (i > 0) cout << \" \";\n        cout << group[i];\n    }\n    cout << endl;\n    \n    return 0;\n}","ahc026":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    int n, m;\n    cin >> n >> m;\n    vector<deque<int>> stacks(m);\n    vector<int> position(n + 1);\n    for (int i = 0; i < m; i++) {\n        for (int j = 0; j < n / m; j++) {\n            int box;\n            cin >> box;\n            stacks[i].push_back(box);\n            position[box] = i;\n        }\n    }\n    vector<bool> removed(n + 1, false);\n    \n    for (int v = 1; v <= n; v++) {\n        if (removed[v]) continue;\n        int s = position[v];\n        // Find index of v in stacks[s]\n        int idx = -1;\n        for (int j = 0; j < stacks[s].size(); j++) {\n            if (stacks[s][j] == v) {\n                idx = j;\n                break;\n            }\n        }\n        int h = stacks[s].size() - 1 - idx;  // Boxes above v\n        while (h > 0) {\n            // Move the top h boxes: Find the box at stacks[s][size - h]\n            int move_idx = stacks[s].size() - h;\n            int box_to_move = stacks[s][move_idx];\n            // Choose destination t: Stack with smallest size, not s\n            int min_size = INT_MAX;\n            int t = -1;\n            for (int i = 0; i < m; i++) {\n                if (i != s && (int)stacks[i].size() < min_size) {\n                    min_size = stacks[i].size();\n                    t = i;\n                }\n            }\n            // Perform move: Extract from move_idx to end\n            deque<int> to_move;\n            for (size_t k = move_idx; k < stacks[s].size(); ++k) {\n                to_move.push_back(stacks[s][k]);\n            }\n            stacks[s].erase(stacks[s].begin() + move_idx, stacks[s].end());\n            // Append to t\n            for (int bx : to_move) {\n                stacks[t].push_back(bx);\n                position[bx] = t;\n            }\n            // Record move operation\n            printf(\"%d %d\\n\", box_to_move, t + 1);\n            // Since we moved the top h, v is now at top of s, so h = 0\n            h = 0;\n        }\n        // Now h == 0, remove v\n        printf(\"%d %d\\n\", v, 0);\n        removed[v] = true;\n        // Remove from stack\n        stacks[s].pop_back();\n    }\n    return 0;\n}","ahc027":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int N;\n    cin >> N;\n    vector<string> h(N-1);\n    for(int i=0; i<N-1; i++) cin >> h[i];\n    vector<string> v(N);\n    for(int i=0; i<N; i++) cin >> v[i];\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    vector<vector<bool>> visited(N, vector<bool>(N, false));\n    vector<pair<int,int>> DIJ = {{0,1},{1,0},{0,-1},{-1,0}};\n    string DIR = \"RDLU\";\n    \n    function<void(int,int)> dfs = [&](int i, int j) {\n        visited[i][j] = true;\n        vector<tuple<int, int, int, int>> candidates; // d, dir, i2, j2\n        for(int dir=0; dir<4; dir++) {\n            int di = DIJ[dir].first, dj = DIJ[dir].second;\n            int i2 = i + di, j2 = j + dj;\n            if(i2 >= 0 && i2 < N && j2 >= 0 && j2 < N && !visited[i2][j2]) {\n                bool can = false;\n                if(di == 0) { // horizontal\n                    int jj = min(j, j2);\n                    if(v[i][jj] == '0') can = true;\n                } else { // vertical\n                    int ii = min(i, i2);\n                    if(h[ii][j] == '0') can = true;\n                }\n                if(can) {\n                    candidates.emplace_back(d[i2][j2], dir, i2, j2);\n                }\n            }\n        }\n        sort(candidates.rbegin(), candidates.rend()); // sort by d descending\n        for(auto& [_, dir, i2, j2] : candidates) {\n            cout << DIR[dir];\n            dfs(i2, j2);\n            cout << DIR[(dir + 2) % 4];\n        }\n    };\n    \n    dfs(0, 0);\n    cout << endl;\n    return 0;\n}","ahc028":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int N, M;\n    cin >> N >> M;\n    int si, sj;\n    cin >> si >> sj;\n    vector<vector<char>> A(N, vector<char>(N));\n    for (int i = 0; i < N; i++) {\n        string row;\n        cin >> row;\n        for (int j = 0; j < N; j++) {\n            A[i][j] = row[j];\n        }\n    }\n    vector<string> t(M);\n    for (int k = 0; k < M; k++) {\n        cin >> t[k];\n    }\n\n    // Precompute hashes for t_k\n    const unsigned long long BASE = 31;\n    vector<unsigned long long> t_hash(M);\n    for (int k = 0; k < M; k++) {\n        unsigned long long h = 0;\n        for (char c : t[k]) {\n            h = h * BASE + (c - 'A' + 1);\n        }\n        t_hash[k] = h;\n    }\n\n    // Random number generator\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n\n    // Initial state\n    int cur_i = si, cur_j = sj;\n    string S = \"\";\n    vector<bool> covered(M, false);\n    int covered_count = 0;\n    unordered_set<unsigned long long> hashes;\n    vector<pair<int, int>> operations;\n    long long total_cost = 0;\n\n    // Main loop\n    while (operations.size() < 5000 && covered_count < M) {\n        int best_new = -1;\n        long long best_cost = LLONG_MAX;\n        pair<int, int> best_pos = {-1, -1};\n\n        for (int ii = 0; ii < N; ii++) {\n            for (int jj = 0; jj < N; jj++) {\n                long long cost = abs(ii - cur_i) + abs(jj - cur_j) + 1 + (rng() % 10);\n                // Penalty for staying to encourage exploration\n                if (ii == cur_i && jj == cur_j) {\n                    cost += 1000;\n                }\n                string temp_S = S + A[ii][jj];\n\n                // Compute new hash if possible\n                bool has_new = (temp_S.size() >= 5);\n                unsigned long long new_h = 0;\n                if (has_new) {\n                    string sub = temp_S.substr(temp_S.size() - 5, 5);\n                    for (char c : sub) {\n                        new_h = new_h * BASE + (c - 'A' + 1);\n                    }\n                }\n\n                // Count new covered\n                int new_cov = 0;\n                for (int k = 0; k < M; k++) {\n                    if (!covered[k]) {\n                        bool is_covered = hashes.count(t_hash[k]);\n                        if (!is_covered && has_new && new_h == t_hash[k]) {\n                            is_covered = true;\n                        }\n                        if (is_covered) {\n                            new_cov++;\n                        }\n                    }\n                }\n\n                // Update best\n                bool better = false;\n                if (new_cov > best_new) {\n                    better = true;\n                } else if (new_cov == best_new) {\n                    if (cost < best_cost) {\n                        better = true;\n                    } else if (cost == best_cost) {\n                        // Random tie-breaker with 50% chance\n                        if (rng() % 2 == 0) {\n                            better = true;\n                        }\n                    }\n                }\n                if (better) {\n                    best_new = new_cov;\n                    best_cost = cost;\n                    best_pos = {ii, jj};\n                }\n            }\n        }\n\n        // If no valid move, break (shouldn't happen)\n        if (best_pos.first == -1) break;\n\n        // Perform the move\n        int ii = best_pos.first, jj = best_pos.second;\n        operations.push_back({ii, jj});\n        total_cost += (abs(ii - cur_i) + abs(jj - cur_j) + 1);  // Actual cost without penalty\n        S += A[ii][jj];\n        cur_i = ii;\n        cur_j = jj;\n\n        // Update hashes\n        if (S.size() >= 5) {\n            string sub = S.substr(S.size() - 5, 5);\n            unsigned long long h = 0;\n            for (char c : sub) {\n                h = h * BASE + (c - 'A' + 1);\n            }\n            hashes.insert(h);\n        }\n\n        // Update covered\n        for (int k = 0; k < M; k++) {\n            if (!covered[k] && hashes.count(t_hash[k])) {\n                covered[k] = true;\n                covered_count++;\n            }\n        }\n    }\n\n    // Output\n    for (auto& op : operations) {\n        cout << op.first << \" \" << op.second << endl;\n    }\n\n    return 0;\n}","ahc030":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int N, M;\n    double eps;\n    cin >> N >> M >> eps;\n    for (int k = 0; k < M; ++k) {\n        int d;\n        cin >> d;\n        for (int i = 0; i < d; ++i) {\n            int x, y;\n            cin >> x >> y;\n        }\n    }\n\n    set<pair<int, int>> oil_set;\n    vector<pair<int, int>> assumed_zero;\n    const int BLOCK_SIZE = 3;\n\n    for (int i = 0; i < N; i += BLOCK_SIZE) {\n        for (int j = 0; j < N; j += BLOCK_SIZE) {\n            vector<pair<int, int>> block;\n            for (int x = i; x < min(i + BLOCK_SIZE, N); ++x) {\n                for (int y = j; y < min(j + BLOCK_SIZE, N); ++y) {\n                    block.push_back({x, y});\n                }\n            }\n            int sz = block.size();\n            cout << \"q \" << sz;\n            for (auto [a, b] : block) cout << \" \" << a << \" \" << b;\n            cout << endl;\n            cout.flush();\n            int s;\n            cin >> s;\n            if (s == 0) {\n                for (auto p : block) assumed_zero.push_back(p);\n            } else {\n                for (auto [a, b] : block) {\n                    cout << \"q 1 \" << a << \" \" << b << endl;\n                    cout.flush();\n                    int v;\n                    cin >> v;\n                    if (v > 0) oil_set.insert({a, b});\n                }\n            }\n        }\n    }\n\n    while (true) {\n        cout << \"a \" << oil_set.size();\n        for (auto [i, j] : oil_set) cout << \" \" << i << \" \" << j;\n        cout << endl;\n        cout.flush();\n        int res;\n        cin >> res;\n        if (res == 1) break;\n        // process the assumed_zero with recursive\n        vector<pair<int, int>> new_all = assumed_zero;\n        assumed_zero.clear();\n        // define process inside or use a function\n        function<void(vector<pair<int, int>>)> process = [&](vector<pair<int, int>> reg) {\n            if (reg.empty()) return;\n            int sz = reg.size();\n            if (sz <= 10) {\n                for (auto [ii, jj] : reg) {\n                    cout << \"q 1 \" << ii << \" \" << jj << endl;\n                    cout.flush();\n                    int v;\n                    cin >> v;\n                    if (v > 0) oil_set.insert({ii, jj});\n                }\n                return;\n            }\n            // divine\n            cout << \"q \" << sz;\n            for (auto [ii, jj] : reg) cout << \" \" << ii << \" \" << jj;\n            cout << endl;\n            cout.flush();\n            int ss;\n            cin >> ss;\n            if (ss == 0) {\n                for (auto p : reg) assumed_zero.push_back(p);\n                return;\n            }\n            // subdivide\n            int min_ii = INT_MAX, max_ii = INT_MIN, min_jj = INT_MAX, max_jj = INT_MIN;\n            for (auto [ii, jj] : reg) {\n                min_ii = min(min_ii, ii);\n                max_ii = max(max_ii, ii);\n                min_jj = min(min_jj, jj);\n                max_jj = max(max_jj, jj);\n            }\n            int mid_ii = (min_ii + max_ii) / 2;\n            int mid_jj = (min_jj + max_jj) / 2;\n            vector<pair<int, int>> tl, tr, bl, br;\n            for (auto p : reg) {\n                int ii = p.first, jj = p.second;\n                if (ii <= mid_ii) {\n                    if (jj <= mid_jj) tl.push_back(p);\n                    else tr.push_back(p);\n                } else {\n                    if (jj <= mid_jj) bl.push_back(p);\n                    else br.push_back(p);\n                }\n            }\n            process(tl);\n            process(tr);\n            process(bl);\n            process(br);\n        };\n        process(new_all);\n    }\n    return 0;\n}","ahc031":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int W, D, N;\n    cin >> W >> D >> N;\n    vector<vector<long long>> a(D, vector<long long>(N));\n    for (int d = 0; d < D; ++d) {\n        for (int k = 0; k < N; ++k) {\n            cin >> a[d][k];\n        }\n    }\n    \n    for (int d = 0; d < D; ++d) {\n        // Sort a[d] in descending order\n        sort(a[d].rbegin(), a[d].rend());\n        // Compute desired heights\n        vector<int> heights(N);\n        long long total_height = 0;\n        for (int k = 0; k < N; ++k) {\n            heights[k] = (a[d][k] + W - 1LL) / W;\n            total_height += heights[k];\n        }\n        // If total_height > W, reduce excess from largest heights\n        if (total_height > W) {\n            long long excess = total_height - W;\n            for (int k = 0; k < N && excess > 0; ++k) {\n                int reduce = min((long long)heights[k] - 1, excess);\n                heights[k] -= reduce;\n                excess -= reduce;\n            }\n            total_height = W;  // Ensure exact fit\n        }\n        // Place rectangles\n        int current_i = 0;\n        for (int k = 0; k < N; ++k) {\n            int i0 = current_i;\n            int j0 = 0;\n            int i1 = current_i + heights[k];\n            int j1 = W;\n            cout << i0 << \" \" << j0 << \" \" << i1 << \" \" << j1 << \"\\n\";\n            current_i += heights[k];\n        }\n    }\n    return 0;\n}","ahc032":"#include <bits/stdc++.h>\n\nusing namespace std;\nusing ll = long long;\nconst ll MOD = 998244353LL;\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<ll>> a(N, vector<ll>(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<ll>>> s(M, vector<vector<ll>>(3, vector<ll>(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 >> s[m][i][j];\n            }\n        }\n    }\n    \n    // Use mt19937 for randomness\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n    \n    // Store the best operations and score\n    vector<tuple<int, int, int>> best_operations;\n    ll best_score = LLONG_MIN;\n    \n    // Number of runs\n    const int NUM_RUNS = 25;\n    \n    for (int run = 0; run < NUM_RUNS; ++run) {\n        // Initialize current_mod for this run\n        vector<vector<ll>> current_mod(N, vector<ll>(N));\n        for (int i = 0; i < N; ++i) {\n            for (int j = 0; j < N; ++j) {\n                current_mod[i][j] = a[i][j] % MOD;\n            }\n        }\n        \n        // Operations for this run\n        vector<tuple<int, int, int>> operations;\n        \n        // Perform up to K operations\n        for (int op = 0; op < K; ++op) {\n            ll best_delta = LLONG_MIN;\n            int best_m = -1, best_p = -1, best_q = -1;\n            \n            // Create and shuffle list of all possible operations\n            vector<tuple<int, int, int>> possible_ops;\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                        possible_ops.emplace_back(m, p, q);\n                    }\n                }\n            }\n            shuffle(possible_ops.begin(), possible_ops.end(), rng);\n            \n            // Evaluate in shuffled order\n            for (auto [m, p, q] : possible_ops) {\n                ll delta = 0;\n                for (int i = 0; i < 3; ++i) {\n                    for (int j = 0; j < 3; ++j) {\n                        ll old_mod = current_mod[p + i][q + j];\n                        ll s_val = s[m][i][j];\n                        ll new_mod = (old_mod + s_val) % MOD;\n                        ll cell_delta = (new_mod - old_mod + MOD) % MOD;\n                        delta += cell_delta;\n                    }\n                }\n                if (delta > best_delta) {\n                    best_delta = delta;\n                    best_m = m;\n                    best_p = p;\n                    best_q = q;\n                }\n            }\n            \n            // If no positive delta, stop\n            if (best_delta <= 0) break;\n            \n            // Apply the best operation\n            operations.emplace_back(best_m, best_p, best_q);\n            for (int i = 0; i < 3; ++i) {\n                for (int j = 0; j < 3; ++j) {\n                    current_mod[best_p + i][best_q + j] = (current_mod[best_p + i][best_q + j] + s[best_m][i][j]) % MOD;\n                }\n            }\n        }\n        \n        // Optimized local search: Try removals, swaps, and adding one operation\n        ll current_score = 0;\n        for (int i = 0; i < N; ++i) {\n            for (int j = 0; j < N; ++j) {\n                current_score += current_mod[i][j];\n            }\n        }\n        vector<tuple<int, int, int>> improved_operations = operations;\n        \n        // Try removals\n        for (size_t idx = 0; idx < operations.size(); ++idx) {\n            vector<tuple<int, int, int>> temp_ops = operations;\n            temp_ops.erase(temp_ops.begin() + idx);\n            \n            vector<vector<ll>> temp_mod(N, vector<ll>(N));\n            for (int i = 0; i < N; ++i) {\n                for (int j = 0; j < N; ++j) {\n                    temp_mod[i][j] = a[i][j] % MOD;\n                }\n            }\n            for (auto [m, p, q] : temp_ops) {\n                for (int i = 0; i < 3; ++i) {\n                    for (int j = 0; j < 3; ++j) {\n                        temp_mod[p + i][q + j] = (temp_mod[p + i][q + j] + s[m][i][j]) % MOD;\n                    }\n                }\n            }\n            ll temp_score = 0;\n            for (int i = 0; i < N; ++i) {\n                for (int j = 0; j < N; ++j) {\n                    temp_score += temp_mod[i][j];\n                }\n            }\n            if (temp_score > current_score) {\n                current_score = temp_score;\n                improved_operations = temp_ops;\n            }\n        }\n        \n        // Try limited random swaps (20 per operation)\n        const int SWAP_SAMPLES = 20;\n        for (size_t idx = 0; idx < operations.size(); ++idx) {\n            auto [orig_m, orig_p, orig_q] = operations[idx];\n            for (int sample = 0; sample < SWAP_SAMPLES; ++sample) {\n                int new_m = rng() % M;\n                int new_p = rng() % (N - 2);\n                int new_q = rng() % (N - 2);\n                if (new_m == orig_m && new_p == orig_p && new_q == orig_q) continue;\n                \n                vector<tuple<int, int, int>> temp_ops = operations;\n                temp_ops[idx] = {new_m, new_p, new_q};\n                \n                vector<vector<ll>> temp_mod(N, vector<ll>(N));\n                for (int i = 0; i < N; ++i) {\n                    for (int j = 0; j < N; ++j) {\n                        temp_mod[i][j] = a[i][j] % MOD;\n                    }\n                }\n                for (auto [m, p, q] : temp_ops) {\n                    for (int i = 0; i < 3; ++i) {\n                        for (int j = 0; j < 3; ++j) {\n                            temp_mod[p + i][q + j] = (temp_mod[p + i][q + j] + s[m][i][j]) % MOD;\n                        }\n                    }\n                }\n                ll temp_score = 0;\n                for (int i = 0; i < N; ++i) {\n                    for (int j = 0; j < N; ++j) {\n                        temp_score += temp_mod[i][j];\n                    }\n                }\n                if (temp_score > current_score) {\n                    current_score = temp_score;\n                    improved_operations = temp_ops;\n                }\n            }\n        }\n        \n        // Try adding one operation\n        if (improved_operations.size() < K) {\n            ll best_add_delta = LLONG_MIN;\n            int best_add_m = -1, best_add_p = -1, best_add_q = -1;\n            \n            vector<vector<ll>> temp_mod = current_mod;  // Use current_mod as base\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                        ll delta = 0;\n                        for (int i = 0; i < 3; ++i) {\n                            for (int j = 0; j < 3; ++j) {\n                                ll old_mod = temp_mod[p + i][q + j];\n                                ll s_val = s[m][i][j];\n                                ll new_mod = (old_mod + s_val) % MOD;\n                                ll cell_delta = (new_mod - old_mod + MOD) % MOD;\n                                delta += cell_delta;\n                            }\n                        }\n                        if (delta > best_add_delta) {\n                            best_add_delta = delta;\n                            best_add_m = m;\n                            best_add_p = p;\n                            best_add_q = q;\n                        }\n                    }\n                }\n            }\n            if (best_add_delta > 0) {\n                improved_operations.emplace_back(best_add_m, best_add_p, best_add_q);\n                current_score += best_add_delta;\n            }\n        }\n        \n        // Update best if better\n        if (current_score > best_score) {\n            best_score = current_score;\n            best_operations = improved_operations;\n        }\n    }\n    \n    // Output the best result\n    cout << best_operations.size() << '\\n';\n    for (auto [m, p, q] : best_operations) {\n        cout << m << ' ' << p << ' ' << q << '\\n';\n    }\n    \n    return 0;\n}","ahc033":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int N;\n    cin >> N;\n    vector<vector<int>> A(N, vector<int>(N));\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cin >> A[i][j];\n        }\n    }\n    \n    // Initialize receiving queues\n    vector<queue<int>> recv_queues(N);\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            recv_queues[i].push(A[i][j]);\n        }\n    }\n    \n    // Grid: -1 no container, else container id\n    vector<vector<int>> grid(N, vector<int>(N, -1));\n    \n    // Crane positions\n    vector<pair<int, int>> crane_pos(N);\n    for (int i = 0; i < N; i++) {\n        crane_pos[i] = {i, 0};\n    }\n    \n    // Crane holdings: -1 none, else id\n    vector<int> crane_holding(N, -1);\n    \n    // Crane active\n    vector<bool> crane_active(N, true);\n    \n    // Dispatched lists\n    vector<vector<int>> dispatched(N);\n    \n    // Actions\n    vector<string> actions(N);\n    \n    // Simulation\n    const int MAX_TURNS = 10000;\n    for (int turn = 0; turn < MAX_TURNS; turn++) {\n        // Step 1: Receive\n        for (int i = 0; i < N; i++) {\n            int x = i, y = 0;\n            if (grid[x][y] == -1 && crane_holding[i] == -1 && !recv_queues[i].empty()) {\n                int cont = recv_queues[i].front();\n                recv_queues[i].pop();\n                grid[x][y] = cont;\n            }\n        }\n        \n        // Step 2: Compute actions\n        vector<char> acts(N, '.');\n        for (int c = 0; c < N; c++) {\n            if (!crane_active[c]) continue;\n            \n            // Compute goal\n            pair<int, int> goal;\n            if (crane_holding[c] != -1) {\n                int cont = crane_holding[c];\n                int target_row = cont / N;\n                if (target_row == c) {\n                    goal = {c, N - 1};\n                } else {\n                    goal = {target_row, 1};  // Buffer column\n                }\n            } else {\n                goal = {c, 0};\n            }\n            \n            // BFS for next move\n            auto get_next_move = [&](int cx, int cy, pair<int, int> g) -> char {\n                vector<vector<int>> dist(N, vector<int>(N, -1));\n                queue<pair<int, int>> q;\n                q.push({cx, cy});\n                dist[cx][cy] = 0;\n                vector<vector<pair<int, int>>> prev(N, vector<pair<int, int>>(N, {-1, -1}));\n                while (!q.empty()) {\n                    auto [x, y] = q.front();\n                    q.pop();\n                    if (x == g.first && y == g.second) break;\n                    vector<pair<int, int>> dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};\n                    for (auto [dx, dy] : dirs) {\n                        int nx = x + dx, ny = y + dy;\n                        if (nx >= 0 && nx < N && ny >= 0 && ny < N) {\n                            if (dist[nx][ny] == -1) {\n                                bool can = true;\n                                if (crane_holding[c] != -1 && c >= 1) {  // Small crane\n                                    if (grid[nx][ny] != -1) can = false;\n                                }\n                                // Check other cranes (simplified: assume no collision in BFS)\n                                for (int o = 0; o < N; o++) {\n                                    if (o != c && crane_pos[o] == make_pair(nx, ny)) can = false;\n                                }\n                                if (can) {\n                                    dist[nx][ny] = dist[x][y] + 1;\n                                    prev[nx][ny] = {x, y};\n                                    q.push({nx, ny});\n                                }\n                            }\n                        }\n                    }\n                }\n                if (dist[g.first][g.second] == -1) return '.';\n                pair<int, int> current = g;\n                while (prev[current.first][current.second] != make_pair(cx, cy)) {\n                    current = prev[current.first][current.second];\n                }\n                int dx = current.first - cx;\n                int dy = current.second - cy;\n                if (dx == -1) return 'U';\n                if (dx == 1) return 'D';\n                if (dy == -1) return 'L';\n                if (dy == 1) return 'R';\n                return '.';\n            };\n            \n            char act = get_next_move(crane_pos[c].first, crane_pos[c].second, goal);\n            \n            // Prefer P/Q if possible\n            int cx = crane_pos[c].first, cy = crane_pos[c].second;\n            if (crane_holding[c] == -1 && grid[cx][cy] != -1) {\n                act = 'P';\n            } else if (crane_holding[c] != -1 && grid[cx][cy] == -1) {\n                act = 'Q';\n            }\n            \n            acts[c] = act;\n        }\n        \n        // Execute actions with validation\n        for (int c = 0; c < N; c++) {\n            char act = acts[c];\n            int cx = crane_pos[c].first, cy = crane_pos[c].second;\n            bool valid = true;\n            if (act == 'P') {\n                if (crane_holding[c] != -1 || grid[cx][cy] == -1) valid = false;\n                else {\n                    crane_holding[c] = grid[cx][cy];\n                    grid[cx][cy] = -1;\n                }\n            } else if (act == 'Q') {\n                if (crane_holding[c] == -1 || grid[cx][cy] != -1) valid = false;\n                else {\n                    grid[cx][cy] = crane_holding[c];\n                    crane_holding[c] = -1;\n                }\n            } else if (act == 'U') {\n                int nx = cx - 1, ny = cy;\n                if (nx < 0) valid = false;\n                else {\n                    if (crane_holding[c] != -1 && c >= 1 && grid[nx][ny] != -1) valid = false;\n                    for (int o = 0; o < N; o++) {\n                        if (o != c && crane_pos[o] == make_pair(nx, ny)) valid = false;\n                    }\n                    if (valid) crane_pos[c] = {nx, ny};\n                }\n            } else if (act == 'D') {\n                int nx = cx + 1, ny = cy;\n                if (nx >= N) valid = false;\n                else {\n                    if (crane_holding[c] != -1 && c >= 1 && grid[nx][ny] != -1) valid = false;\n                    for (int o = 0; o < N; o++) {\n                        if (o != c && crane_pos[o] == make_pair(nx, ny)) valid = false;\n                    }\n                    if (valid) crane_pos[c] = {nx, ny};\n                }\n            } else if (act == 'L') {\n                int nx = cx, ny = cy - 1;\n                if (ny < 0) valid = false;\n                else {\n                    if (crane_holding[c] != -1 && c >= 1 && grid[nx][ny] != -1) valid = false;\n                    for (int o = 0; o < N; o++) {\n                        if (o != c && crane_pos[o] == make_pair(nx, ny)) valid = false;\n                    }\n                    if (valid) crane_pos[c] = {nx, ny};\n                }\n            } else if (act == 'R') {\n                int nx = cx, ny = cy + 1;\n                if (ny >= N) valid = false;\n                else {\n                    if (crane_holding[c] != -1 && c >= 1 && grid[nx][ny] != -1) valid = false;\n                    for (int o = 0; o < N; o++) {\n                        if (o != c && crane_pos[o] == make_pair(nx, ny)) valid = false;\n                    }\n                    if (valid) crane_pos[c] = {nx, ny};\n                }\n            } else if (act == 'B') {\n                if (crane_holding[c] != -1) valid = false;\n                else crane_active[c] = false;\n            }\n            if (!valid) act = '.';\n            actions[c] += act;\n        }\n        \n        // Step 3: Dispatch\n        for (int i = 0; i < N; i++) {\n            int x = i, y = N - 1;\n            if (grid[x][y] != -1) {\n                int cont = grid[x][y];\n                grid[x][y] = -1;\n                dispatched[i].push_back(cont);\n            }\n        }\n        \n        // Check if done\n        bool done = true;\n        for (int i = 0; i < N; i++) {\n            if (!recv_queues[i].empty()) done = false;\n            if (grid[i][N - 1] != -1) done = false;\n            if (crane_holding[i] != -1) done = false;\n        }\n        if (done) break;\n    }\n    \n    // Output: Pad to max length\n    size_t max_len = 0;\n    for (const auto& s : actions) max_len = max(max_len, s.size());\n    for (auto& s : actions) {\n        while (s.size() < max_len) s += '.';\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>> h(N, vector<int>(N));\n    for (int i = 0; i < N; ++i) {\n        for (int j = 0; j < N; ++j) {\n            cin >> h[i][j];\n        }\n    }\n\n    // Directions: U, D, L, R\n    vector<pair<int, int>> dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};\n    string move_str = \"UDLR\";\n\n    // Priority queues: excess (max h), deficit (max -h)\n    priority_queue<tuple<int, int, int>> pq_excess; // {h, i, j}\n    priority_queue<tuple<int, int, int>> pq_deficit; // {-h, i, j}\n    for (int i = 0; i < N; ++i) {\n        for (int j = 0; j < N; ++j) {\n            if (h[i][j] > 0) pq_excess.push({h[i][j], i, j});\n            if (h[i][j] < 0) pq_deficit.push({-h[i][j], i, j});\n        }\n    }\n\n    // Function to get path from (si,sj) to (ti,tj) using BFS\n    auto get_path = [&](int si, int sj, int ti, int tj) -> vector<string> {\n        vector<vector<int>> dist(N, vector<int>(N, -1));\n        vector<vector<pair<int, int>>> prevv(N, vector<pair<int, int>>(N, {-1, -1}));\n        queue<pair<int, int>> q;\n        q.push({si, sj});\n        dist[si][sj] = 0;\n        while (!q.empty()) {\n            auto [i, j] = q.front(); q.pop();\n            if (i == ti && j == tj) break;\n            for (int d = 0; d < 4; ++d) {\n                int ni = i + dirs[d].first, nj = j + dirs[d].second;\n                if (ni >= 0 && ni < N && nj >= 0 && nj < N && dist[ni][nj] == -1) {\n                    dist[ni][nj] = dist[i][j] + 1;\n                    prevv[ni][nj] = {i, j};\n                    q.push({ni, nj});\n                }\n            }\n        }\n        // Reconstruct path\n        vector<string> path;\n        int ci = ti, cj = tj;\n        while (ci != si || cj != sj) {\n            auto [pi, pj] = prevv[ci][cj];\n            for (int d = 0; d < 4; ++d) {\n                if (pi + dirs[d].first == ci && pj + dirs[d].second == cj) {\n                    path.push_back(string(1, move_str[d]));\n                    break;\n                }\n            }\n            ci = pi; cj = pj;\n        }\n        reverse(path.begin(), path.end());\n        return path;\n    };\n\n    // Function to find nearest deficit from (si,sj) and get path\n    auto find_nearest_deficit = [&](int si, int sj) -> pair<pair<int, int>, vector<string>> {\n        vector<vector<int>> dist(N, vector<int>(N, -1));\n        vector<vector<pair<int, int>>> prevv(N, vector<pair<int, int>>(N, {-1, -1}));\n        queue<pair<int, int>> q;\n        q.push({si, sj});\n        dist[si][sj] = 0;\n        pair<int, int> target = {-1, -1};\n        while (!q.empty() && target.first == -1) {\n            auto [x, y] = q.front(); q.pop();\n            if (h[x][y] < 0) {\n                target = {x, y};\n                break;\n            }\n            for (auto& dir : dirs) {\n                int nx = x + dir.first, ny = y + dir.second;\n                if (nx >= 0 && nx < N && ny >= 0 && ny < N && dist[nx][ny] == -1) {\n                    dist[nx][ny] = dist[x][y] + 1;\n                    prevv[nx][ny] = {x, y};\n                    q.push({nx, ny});\n                }\n            }\n        }\n        if (target.first == -1) return {{ -1, -1 }, {}};\n        // Reconstruct path to target\n        vector<string> path;\n        int ci = target.first, cj = target.second;\n        while (ci != si || cj != sj) {\n            auto [pi, pj] = prevv[ci][cj];\n            for (int d = 0; d < 4; ++d) {\n                if (pi + dirs[d].first == ci && pj + dirs[d].second == cj) {\n                    path.push_back(string(1, move_str[d]));\n                    break;\n                }\n            }\n            ci = pi; cj = pj;\n        }\n        reverse(path.begin(), path.end());\n        return {target, path};\n    };\n\n    int ci = 0, cj = 0; // Current position\n    vector<string> operations;\n\n    while (!pq_excess.empty()) {\n        // Get largest excess\n        auto [val, i, j] = pq_excess.top(); pq_excess.pop();\n        if (h[i][j] <= 0) continue; // Already processed\n\n        // Move to excess cell\n        auto path_to_excess = get_path(ci, cj, i, j);\n        for (auto& s : path_to_excess) operations.push_back(s);\n        ci = i; cj = j;\n\n        // Load all soil\n        int d = h[i][j];\n        operations.push_back(\"+\" + to_string(d));\n        h[i][j] = 0;\n\n        // Find and move to nearest deficits until d is 0\n        while (d > 0) {\n            auto [target, path_to_deficit] = find_nearest_deficit(ci, cj);\n            if (target.first == -1) break; // No deficit (shouldn't happen)\n            int p = target.first, q = target.second;\n\n            // Move to deficit\n            for (auto& s : path_to_deficit) operations.push_back(s);\n            ci = p; cj = q;\n\n            // Unload as much as possible\n            int unload = min(d, -h[p][q]);\n            operations.push_back(\"-\" + to_string(unload));\n            h[p][q] += unload;\n            d -= unload;\n        }\n    }\n\n    // Output all operations\n    for (auto& op : operations) {\n        cout << op << endl;\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    int seed_count = 2 * N * (N - 1);\n    vector<vector<int>> X(seed_count, vector<int>(M));\n    \n    // Read initial seeds\n    for (int i = 0; i < seed_count; i++) {\n        for (int j = 0; j < M; j++) {\n            cin >> X[i][j];\n        }\n    }\n    \n    for (int t = 0; t < T; t++) {\n        // Compute V_k for each seed and prepare for sorting\n        vector<pair<int, int>> seeds;  // Pair: (-V_k, index) for descending sort\n        for (int i = 0; i < seed_count; i++) {\n            int v = 0;\n            for (int j = 0; j < M; j++) {\n                v += X[i][j];\n            }\n            seeds.push_back({-v, i});  // Negative V_k for descending order\n        }\n        \n        // Sort seeds by V_k descending (top 36 will be first)\n        sort(seeds.begin(), seeds.end());\n        \n        // Create planting grid: Assign top 36 seeds in row-major order\n        vector<vector<int>> A(N, vector<int>(N));\n        int idx = 0;\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                A[i][j] = seeds[idx].second;  // Seed index\n                idx++;\n            }\n        }\n        \n        // Output the planting grid\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                cout << A[i][j];\n                if (j < N - 1) cout << \" \";\n                else cout << endl;\n            }\n        }\n        cout.flush();  // Important for interactive problems\n        \n        // Read new seeds after the operation\n        for (int i = 0; i < seed_count; i++) {\n            for (int j = 0; j < M; j++) {\n                cin >> X[i][j];\n            }\n        }\n    }\n    \n    return 0;\n}","ahc038":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int N, M, V;\n    cin >> N >> M >> V;\n    vector<vector<int>> s(N, vector<int>(N));\n    for(int i = 0; i < N; i++) {\n        string str;\n        cin >> str;\n        for(int j = 0; j < N; j++) {\n            s[i][j] = str[j] - '0';\n        }\n    }\n    vector<vector<int>> t(N, vector<int>(N));\n    for(int i = 0; i < N; i++) {\n        string str;\n        cin >> str;\n        for(int j = 0; j < N; j++) {\n            t[i][j] = str[j] - '0';\n        }\n    }\n    \n    // design the tree\n    cout << 2 << endl;\n    cout << 0 << \" \" << 1 << endl;\n    cout << 0 << \" \" << 0 << endl;\n    \n    vector<int> DX = {0, 1, 0, -1};\n    vector<int> DY = {1, 0, -1, 0};\n    vector<char> DIR = {'R', 'D', 'L', 'U'};\n    \n    int rx = 0, ry = 0;\n    int dir1 = 0; // direction of edge (0, 1)\n    bool holding = false; // whether holding a takoyaki\n    \n    mt19937 rng(42);\n    uniform_int_distribution<int> dist_dir(0, 3);\n    uniform_int_distribution<int> dist_rot(0, 2);\n    \n    for(int turn = 0; turn < 10000; turn++) {\n        string S = \"\";\n        // random move\n        int dir = dist_dir(rng);\n        int dx = DX[dir], dy = DY[dir];\n        if(0 <= rx + dx && rx + dx < N && 0 <= ry + dy && ry + dy < N) {\n            rx += dx;\n            ry += dy;\n            S += DIR[dir];\n        } else {\n            S += '.';\n        }\n        // random rotate\n        int rot = dist_rot(rng);\n        if(rot == 0) {\n            S += '.';\n        } else if(rot == 1) {\n            S += 'L';\n            dir1 = (dir1 + 3) % 4;\n        } else {\n            S += 'R';\n            dir1 = (dir1 + 1) % 4;\n        }\n        // grab or release takoyaki\n        int x = rx + DX[dir1], y = ry + DY[dir1];\n        bool change = false;\n        if(0 <= x && x < N && 0 <= y && y < N) {\n            if(s[x][y] == 1 && t[x][y] == 0 && !holding) {\n                change = true;\n                s[x][y] = 0;\n                holding = true;\n            } else if(s[x][y] == 0 && t[x][y] == 1 && holding) {\n                change = true;\n                s[x][y] = 1;\n                holding = false;\n            }\n        }\n        S += '.'; // vertex 0 (root) is not a leaf\n        if(change) {\n            S += 'P';\n        } else {\n            S += '.';\n        }\n        // output the command\n        cout << S << endl;\n    }\n    \n    return 0;\n}","ahc039":"#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n\nstruct Fenwick {\n  vector<ll> tree;\n  int n;\n  Fenwick(int _n) : n(_n), tree(_n + 1, 0) {}\n  void update(int idx, ll val) {\n    while (idx <= n) {\n      tree[idx] += val;\n      idx += idx & -idx;\n    }\n  }\n  ll query(int idx) {\n    ll sum = 0;\n    while (idx > 0) {\n      sum += tree[idx];\n      idx -= idx & -idx;\n    }\n    return sum;\n  }\n};\n\nint main() {\n  ios::sync_with_stdio(false);\n  cin.tie(nullptr);\n\n  int N;\n  cin >> N;\n  vector<tuple<ll, ll, int>> pts;\n  for (int i = 0; i < 2 * N; ++i) {\n    ll x, y;\n    cin >> x >> y;\n    int w = (i < N ? 1 : -1);\n    pts.emplace_back(x, y, w);\n  }\n\n  sort(pts.begin(), pts.end());\n\n  // Assign ranks to y\n  set<ll> yset;\n  for (auto &[x, y, w] : pts) yset.insert(y);\n  vector<ll> ylist(yset.begin(), yset.end());\n  map<ll, int> yrank;\n  for (int i = 0; i < ylist.size(); ++i) {\n    yrank[ylist[i]] = i + 1; // 1-based\n  }\n\n  int max_rank = ylist.size();\n  Fenwick ft(max_rank);\n\n  ll best_score = LLONG_MIN / 2;\n  ll best_left = -1, best_right = -1, best_bottom = -1, best_top = -1;\n\n  ll current_min_x = LLONG_MAX / 2;\n  set<int> active_ranks;\n\n  for (auto &[x, y, w] : pts) {\n    current_min_x = min(current_min_x, x);\n    int r = yrank[y];\n    ft.update(r, w);\n    active_ranks.insert(r);\n\n    // Compute max subarray in active_ranks\n    vector<int> order(active_ranks.begin(), active_ranks.end());\n    ll cum = 0;\n    ll min_p = 0;\n    int min_idx = 0;\n    ll local_max = LLONG_MIN / 2;\n    int best_start = 0, best_end = -1;\n    for (int j = 0; j < order.size(); ++j) {\n      cum = ft.query(order[j]);\n      ll sum_val = cum - min_p;\n      if (sum_val > local_max) {\n        local_max = sum_val;\n        best_start = min_idx;\n        best_end = j;\n      }\n      if (cum < min_p) {\n        min_p = cum;\n        min_idx = j + 1;\n      }\n    }\n\n    if (current_min_x < x && ylist[order[best_start] - 1] < ylist[order[best_end] - 1] && local_max > best_score) {\n      best_score = local_max;\n      best_left = current_min_x;\n      best_right = x;\n      best_bottom = ylist[order[best_start] - 1];\n      best_top = ylist[order[best_end] - 1];\n    }\n  }\n\n  // If no valid, small one\n  if (best_score <= LLONG_MIN / 2 + 1) {\n    best_left = 0;\n    best_right = 1;\n    best_bottom = 0;\n    best_top = 1;\n  }\n\n  vector<pair<ll, ll>> poly = {\n    {best_left, best_bottom},\n    {best_left, best_top},\n    {best_right, best_top},\n    {best_right, best_bottom}\n  };\n\n  cout << 4 << '\\n';\n  for (auto &[x, y] : poly) {\n    cout << x << ' ' << y << '\\n';\n  }\n\n  return 0;\n}","ahc040":"#include <bits/stdc++.h>\n\nusing namespace std;\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int N, T, sigma;\n    cin >> N >> T >> sigma;\n    vector<long long> w(N), h(N);\n    for (int i = 0; i < N; ++i) {\n        cin >> w[i] >> h[i];\n    }\n    \n    // Define modes: 0=alternate U/L with smart r/b, 1-4=fixed for baselines\n    vector<int> modes = {0, 1, 2, 3, 4};\n    \n    for (int t = 0; t < T; ++t) {\n        int mode = modes[t % modes.size()];\n        vector<tuple<int, int, char, int>> prdb;\n        \n        for (int i = 0; i < N; ++i) {\n            int p = i;\n            int r;\n            char d;\n            int b;\n            \n            if (mode == 0) {\n                // Alternate U/L, b=previous, r optimized for direction\n                d = (i % 2 == 0) ? 'U' : 'L';\n                b = (i == 0) ? -1 : i - 1;\n                if (d == 'U') {\n                    r = (h[i] > w[i]) ? 1 : 0;  // Rotate if taller to prioritize width\n                } else {\n                    r = (w[i] > h[i]) ? 1 : 0;  // Rotate if wider to prioritize height\n                }\n            } else if (mode == 1) {\n                d = 'U';\n                b = -1;\n                r = 0;\n            } else if (mode == 2) {\n                d = 'L';\n                b = -1;\n                r = 0;\n            } else if (mode == 3) {\n                d = 'U';\n                b = -1;\n                r = 1;\n            } else if (mode == 4) {\n                d = 'L';\n                b = -1;\n                r = 1;\n            }\n            \n            prdb.emplace_back(p, r, d, b);\n        }\n        \n        cout << N << '\\n';\n        for (auto [p, r, d, b] : prdb) {\n            cout << p << ' ' << r << ' ' << d << ' ' << b << '\\n';\n        }\n        cout << flush;\n        \n        long long Wp, Hp;\n        cin >> Wp >> Hp;\n        // Future: Use Wp, Hp to adapt modes (e.g., if Wp > Hp, favor 'L')\n    }\n    \n    return 0;\n}","ahc041":"#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n\npair<ll, int> compute_score_and_max_height(int N, const vector<int>& A, const vector<int>& p, const vector<vector<int>>& adj, int H) {\n    vector<int> depth(N, -1);\n    ll score = 0;\n    int max_h = 0;\n    bool has_cycle = false;\n    for (int i = 0; i < N; ++i) {\n        if (depth[i] != -1) continue;\n        queue<int> q;\n        q.push(i);\n        depth[i] = 0;\n        vector<bool> visited(N, false);\n        visited[i] = true;\n        while (!q.empty()) {\n            int u = q.front(); q.pop();\n            score += (ll)(depth[u] + 1) * A[u];\n            max_h = max(max_h, depth[u]);\n            for (int v : adj[u]) {\n                if (v == p[u]) continue;\n                if (visited[v]) {\n                    has_cycle = true;\n                    break;\n                }\n                if (depth[v] == -1) {\n                    depth[v] = depth[u] + 1;\n                    visited[v] = true;\n                    q.push(v);\n                }\n            }\n            if (has_cycle) break;\n        }\n        if (has_cycle) break;\n    }\n    if (has_cycle || max_h > H) return {-1, -1};\n    return {score, max_h};\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(0);\n    int N, M, H;\n    cin >> N >> M >> H;\n    vector<int> A(N);\n    for (int i = 0; i < N; ++i) {\n        cin >> A[i];\n    }\n    vector<vector<int>> adj(N);\n    for (int i = 0; i < M; ++i) {\n        int u, v;\n        cin >> u >> v;\n        adj[u].push_back(v);\n        adj[v].push_back(u);\n    }\n    vector<pair<int, int>> points(N);\n    for (int i = 0; i < N; ++i) {\n        cin >> points[i].first >> points[i].second;\n    }\n    vector<vector<double>> dist(N, vector<double>(N));\n    for (int i = 0; i < N; ++i) {\n        for (int j = 0; j < N; ++j) {\n            double dx = points[i].first - points[j].first;\n            double dy = points[i].second - points[j].second;\n            dist[i][j] = sqrt(dx * dx + dy * dy);\n        }\n    }\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n    vector<pair<int, int>> verts;\n    for (int i = 0; i < N; ++i) {\n        verts.push_back({-A[i], i});\n    }\n    shuffle(verts.begin(), verts.end(), rng);\n    vector<int> d(N, -1);\n    vector<int> p(N, -1);\n    for (auto [_, v] : verts) {\n        if (d[v] != -1) continue;\n        int best_u = -1;\n        double min_dist = 1e9;\n        int max_du = -1;\n        for (int u : adj[v]) {\n            if (d[u] != -1 && d[u] < H) {\n                if (d[u] > max_du || (d[u] == max_du && dist[v][u] < min_dist)) {\n                    max_du = d[u];\n                    min_dist = dist[v][u];\n                    best_u = u;\n                }\n            }\n        }\n        if (best_u != -1) {\n            p[v] = best_u;\n            d[v] = max_du + 1;\n        } else {\n            p[v] = -1;\n            d[v] = 0;\n        }\n    }\n    auto [current_score, _] = compute_score_and_max_height(N, A, p, adj, H);\n    const int MAX_ITER = 10000;\n    double T = 1000000.0;\n    double alpha = 0.9999;\n    for (int iter = 0; iter < MAX_ITER; ++iter) {\n        int v = rng() % N;\n        vector<int> candidates = adj[v];\n        candidates.push_back(-1);\n        int new_parent = candidates[rng() % candidates.size()];\n        if (new_parent == p[v]) continue;\n        int old_p = p[v];\n        p[v] = new_parent;\n        auto [new_score, new_max_h] = compute_score_and_max_height(N, A, p, adj, H);\n        if (new_score != -1) {\n            double delta = new_score - current_score;\n            if (delta > 0 || exp(delta / T) > uniform_real_distribution<double>(0, 1)(rng)) {\n                current_score = new_score;\n            } else {\n                p[v] = old_p;\n            }\n        } else {\n            p[v] = old_p;\n        }\n        T *= alpha;\n    }\n    for (int i = 0; i < N; ++i) {\n        if (i > 0) cout << \" \";\n        cout << p[i];\n    }\n    cout << \"\\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> board(N);\n    for (int i = 0; i < N; i++) {\n        cin >> board[i];\n    }\n    vector<pair<int, int>> oni;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (board[i][j] == 'x') {\n                oni.emplace_back(i, j);\n            }\n        }\n    }\n    // Maps for batching: direction -> index -> max distance\n    map<pair<char, int>, int> max_dist;\n    for (auto [i, j] : oni) {\n        // Check directions and choose best\n        bool can_up = true;\n        for (int k = 0; k < i; k++) {\n            if (board[k][j] == 'o') can_up = false;\n        }\n        int cost_up = can_up ? 2 * (i + 1) : 1e9;\n\n        bool can_down = true;\n        for (int k = i + 1; k < N; k++) {\n            if (board[k][j] == 'o') can_down = false;\n        }\n        int cost_down = can_down ? 2 * (N - i) : 1e9;\n\n        bool can_left = true;\n        for (int k = 0; k < j; k++) {\n            if (board[i][k] == 'o') can_left = false;\n        }\n        int cost_left = can_left ? 2 * (j + 1) : 1e9;\n\n        bool can_right = true;\n        for (int k = j + 1; k < N; k++) {\n            if (board[i][k] == 'o') can_right = false;\n        }\n        int cost_right = can_right ? 2 * (N - j) : 1e9;\n\n        // Find best direction\n        vector<tuple<int, char, int, int>> candidates; // cost, dir, idx, dist\n        if (can_up) candidates.emplace_back(cost_up, 'U', j, i + 1);\n        if (can_down) candidates.emplace_back(cost_down, 'D', j, N - i);\n        if (can_left) candidates.emplace_back(cost_left, 'L', i, j + 1);\n        if (can_right) candidates.emplace_back(cost_right, 'R', i, N - j);\n        sort(candidates.begin(), candidates.end());\n        auto [cost, dir, idx, dist] = candidates[0];\n\n        // Update max_dist\n        pair<char, int> key = {dir, idx};\n        max_dist[key] = max(max_dist[key], dist);\n    }\n\n    // Generate batched operations\n    vector<pair<char, int>> all_ops;\n    for (auto& [key, dist] : max_dist) {\n        char dir = key.first;\n        int idx = key.second;\n        if (dir == 'U') {\n            for (int t = 0; t < dist; t++) all_ops.emplace_back('U', idx);\n            for (int t = 0; t < dist; t++) all_ops.emplace_back('D', idx);\n        } else if (dir == 'D') {\n            for (int t = 0; t < dist; t++) all_ops.emplace_back('D', idx);\n            for (int t = 0; t < dist; t++) all_ops.emplace_back('U', idx);\n        } else if (dir == 'L') {\n            for (int t = 0; t < dist; t++) all_ops.emplace_back('L', idx);\n            for (int t = 0; t < dist; t++) all_ops.emplace_back('R', idx);\n        } else if (dir == 'R') {\n            for (int t = 0; t < dist; t++) all_ops.emplace_back('R', idx);\n            for (int t = 0; t < dist; t++) all_ops.emplace_back('L', idx);\n        }\n    }\n\n    // Output\n    for (auto [d, p] : all_ops) {\n        cout << d << \" \" << p << \"\\n\";\n    }\n    return 0;\n}","ahc044":"#include <bits/stdc++.h>\n#include <random>\n#include <chrono>\n\nusing namespace std;\n\nint main() {\n    const int N = 100;\n    const int L = 500000;\n    vector<long long> T(N);\n    for (auto &x : T) cin >> x;\n\n    // Random number generator\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n    uniform_real_distribution<double> urd(0.0, 1.0);\n\n    // Initial random assignment\n    vector<int> a(N), b(N);\n    for (int i = 0; i < N; i++) {\n        a[i] = rng() % N;\n        b[i] = rng() % N;\n    }\n\n    // Simulation function\n    auto simulate = [&](const vector<int> &aa, const vector<int> &bb) -> long long {\n        vector<long long> cnt(N, 0);\n        int cur = 0;\n        cnt[0] = 1;\n        for (int w = 1; w < L; w++) {\n            int t = cnt[cur];\n            int nxt = (t % 2 == 1) ? aa[cur] : bb[cur];\n            cur = nxt;\n            cnt[cur]++;\n        }\n        long long e = 0;\n        for (int i = 0; i < N; i++) e += abs(cnt[i] - T[i]);\n        return e;\n    };\n\n    // Initial error\n    long long currentE = simulate(a, b);\n\n    // Simulated Annealing parameters\n    double temp = 1000.0;\n    const int iterations = 150;  // Adjust based on time; ~150 fits within 2s\n    const double cooling_rate = 0.99;\n\n    for (int it = 0; it < iterations; it++) {\n        // Perturb: choose random i, which (0 for a, 1 for b), and new value\n        int i = rng() % N;\n        int which = rng() % 2;\n        int old_val = (which == 0) ? a[i] : b[i];\n        int new_val = rng() % N;\n\n        // Apply change\n        if (which == 0) a[i] = new_val;\n        else b[i] = new_val;\n\n        // Compute new error\n        long long newE = simulate(a, b);\n\n        // Acceptance\n        if (newE <= currentE || urd(rng) < exp((currentE - newE) / temp)) {\n            currentE = newE;\n        } else {\n            // Revert\n            if (which == 0) a[i] = old_val;\n            else b[i] = old_val;\n        }\n\n        // Cool down\n        temp *= cooling_rate;\n    }\n\n    // Output the best assignment\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\nusing ll = long long;\nusing ld = long double;\n\n// Union-Find for Kruskal\nstruct UF {\n    vector<int> par;\n    UF(int n) : par(n) { iota(par.begin(), par.end(), 0); }\n    int find(int x) { return par[x] == x ? x : par[x] = find(par[x]); }\n    bool unite(int x, int y) {\n        x = find(x); y = find(y);\n        if (x == y) return false;\n        par[x] = y;\n        return true;\n    }\n};\n\n// Compute floored Euclidean distance\nll dist(ld x1, ld y1, ld x2, ld y2) {\n    ld dx = x1 - x2, dy = y1 - y2;\n    return floorl(sqrtl(dx * dx + dy * dy));\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int N, M, Q, L, W;\n    cin >> N >> M >> Q >> L >> W;\n    vector<int> G(M);\n    for (int i = 0; i < M; ++i) cin >> G[i];\n    vector<ld> 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    // Compute approximate centers\n    vector<ld> cx(N), cy(N);\n    for (int i = 0; i < N; ++i) {\n        cx[i] = (lx[i] + rx[i]) / 2.0L;\n        cy[i] = (ly[i] + ry[i]) / 2.0L;\n    }\n\n    // Sort cities by cx, then cy\n    vector<int> cities(N);\n    iota(cities.begin(), cities.end(), 0);\n    sort(cities.begin(), cities.end(), [&](int a, int b) {\n        if (cx[a] != cx[b]) return cx[a] < cx[b];\n        return cy[a] < cy[b];\n    });\n\n    // Assign to groups\n    vector<vector<int>> groups(M);\n    int idx = 0;\n    for (int k = 0; k < M; ++k) {\n        for (int j = 0; j < G[k]; ++j) {\n            groups[k].push_back(cities[idx++]);\n        }\n    }\n\n    // Process queries and build edges\n    vector<vector<pair<int, int>>> edges(M);\n    int queries_used = 0;\n\n    auto query = [&](const vector<int>& c) -> vector<pair<int, int>> {\n        if (queries_used >= Q) return {};\n        cout << \"? \" << c.size();\n        for (int x : c) cout << \" \" << x;\n        cout << \"\\n\" << flush;\n        ++queries_used;\n        vector<pair<int, int>> res;\n        for (int i = 0; i < (int)c.size() - 1; ++i) {\n            int a, b;\n            cin >> a >> b;\n            res.emplace_back(min(a, b), max(a, b));\n        }\n        return res;\n    };\n\n    for (int k = 0; k < M; ++k) {\n        auto& grp = groups[k];\n        int sz = grp.size();\n        if (sz >= 2 && sz <= L) {\n            // Query entire group for true MST (only if valid size)\n            auto res = query(grp);\n            edges[k] = res;\n        } else if (sz > L) {\n            // Approximate MST using centers (exactly sz-1 edges)\n            vector<tuple<ll, int, int>> edge_list;\n            for (int i = 0; i < sz; ++i) {\n                for (int j = i + 1; j < sz; ++j) {\n                    ll d = dist(cx[grp[i]], cy[grp[i]], cx[grp[j]], cy[grp[j]]);\n                    edge_list.emplace_back(d, i, j);\n                }\n            }\n            sort(edge_list.begin(), edge_list.end());\n            UF uf(sz);\n            vector<pair<int, int>> mst_edges;\n            for (auto& [d, u, v] : edge_list) {\n                if (uf.unite(u, v)) {\n                    mst_edges.emplace_back(grp[u], grp[v]);\n                }\n            }\n            edges[k] = mst_edges;\n        }\n        // For sz == 1, edges[k] remains empty (default vector)\n    }\n\n    // Output\n    cout << \"!\\n\";\n    for (int k = 0; k < M; ++k) {\n        for (int x : groups[k]) cout << x << \" \";\n        cout << \"\\n\";\n        for (auto& [a, b] : edges[k]) {\n            cout << a << \" \" << b << \"\\n\";\n        }\n    }\n\n    return 0;\n}","ahc046":"#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n    int N, M;\n    cin >> N >> M;\n    vector<pair<int, int>> pos(M);\n    for (int i = 0; i < M; i++) {\n        cin >> pos[i].first >> pos[i].second;\n    }\n    vector<vector<bool>> blocked(20, vector<bool>(20, false));\n    auto get_path = [&](pair<int, int> s, pair<int, int> t) {\n        vector<vector<int>> dist(20, vector<int>(20, -1));\n        vector<vector<pair<int, int>>> prevv(20, vector<pair<int, int>>(20, {-1, -1}));\n        queue<pair<int, int>> q;\n        q.push(s);\n        dist[s.first][s.second] = 0;\n        int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};\n        while (!q.empty()) {\n            auto [x, y] = q.front();\n            q.pop();\n            for (int d = 0; d < 4; d++) {\n                int nx = x + dx[d], ny = y + dy[d];\n                if (nx >= 0 && nx < 20 && ny >= 0 && ny < 20 && !blocked[nx][ny] && dist[nx][ny] == -1) {\n                    dist[nx][ny] = dist[x][y] + 1;\n                    prevv[nx][ny] = {x, y};\n                    q.push({nx, ny});\n                }\n            }\n        }\n        vector<pair<int, int>> path;\n        pair<int, int> cur = t;\n        while (cur != make_pair(-1, -1)) {\n            path.push_back(cur);\n            cur = prevv[cur.first][cur.second];\n        }\n        reverse(path.begin(), path.end());\n        if (path.empty() || path[0] != s) return vector<int>();\n        vector<int> dirs;\n        for (size_t i = 1; i < path.size(); i++) {\n            int px = path[i - 1].first, py = path[i - 1].second;\n            int cx = path[i].first, cy = path[i].second;\n            if (cx == px + 1) dirs.push_back(1);\n            else if (cx == px - 1) dirs.push_back(0);\n            else if (cy == py + 1) dirs.push_back(3);\n            else if (cy == py - 1) dirs.push_back(2);\n        }\n        return dirs;\n    };\n    vector<pair<char, char>> actions;\n    pair<int, int> current = pos[0];\n    for (int k = 1; k < M; k++) {\n        vector<int> dirs = get_path(current, pos[k]);\n        for (int d : dirs) {\n            actions.emplace_back('M', \"UDLR\"[d]);\n        }\n        current = pos[k];\n    }\n    for (auto [a, d] : actions) {\n        cout << a << \" \" << d << endl;\n    }\n    return 0;\n}"}}}