{"model_name":"qwen3-coder","codes":{"1":{"ahc001":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Rectangle {\n    int a, b, c, d; // (a,b) to (c,d)\n    Rectangle() : a(0), b(0), c(0), d(0) {}\n    Rectangle(int _a, int _b, int _c, int _d) : a(_a), b(_b), c(_c), d(_d) {}\n    bool isValid() const { return a < c && b < d; }\n    long long area() const { return 1LL * (c - a) * (d - b); }\n};\n\nstruct Company {\n    int x, y, r;\n    int id;\n    Company() : x(0), y(0), r(0), id(0) {}\n    Company(int _x, int _y, int _r, int _id) : x(_x), y(_y), r(_r), id(_id) {}\n};\n\nint n;\nvector<Company> companies;\nvector<Rectangle> rectangles;\n\n// Check if two rectangles overlap\nbool overlap(const Rectangle& r1, const Rectangle& r2) {\n    return !(r1.c <= r2.a || r2.c <= r1.a || r1.d <= r2.b || r2.d <= r1.b);\n}\n\n// Check if rectangle overlaps with any existing rectangle\nbool hasOverlap(const Rectangle& rect, int skip_id = -1) {\n    for (int i = 0; i < n; i++) {\n        if (i == skip_id) continue;\n        if (rectangles[i].isValid() && overlap(rect, rectangles[i])) {\n            return true;\n        }\n    }\n    return false;\n}\n\n// Try to place a rectangle for company at index idx\nbool placeRectangle(int idx) {\n    const Company& company = companies[idx];\n    int x = company.x;\n    int y = company.y;\n    int r = company.r;\n    \n    // Try different rectangle dimensions\n    vector<pair<int, int>> dimensions;\n    for (int w = 1; w <= min(r, 10000); w++) {\n        if (r % w == 0) {\n            int h = r / w;\n            if (h <= 10000) {\n                dimensions.push_back({w, h});\n            }\n        }\n    }\n    \n    // Sort by perimeter (prefer more square-like rectangles)\n    sort(dimensions.begin(), dimensions.end(), [](const pair<int,int>& a, const pair<int,int>& b) {\n        return abs(a.first - a.second) < abs(b.first - b.second);\n    });\n    \n    // Try to place rectangle containing (x, y) \n    for (auto& dim : dimensions) {\n        int w = dim.first;\n        int h = dim.second;\n        \n        // Try different positions where (x, y) is inside the rectangle\n        for (int dx = 0; dx < w; dx++) {\n            for (int dy = 0; dy < h; dy++) {\n                int a = x - dx;\n                int b = y - dy;\n                int c = a + w;\n                int d = b + h;\n                \n                // Check bounds\n                if (a >= 0 && b >= 0 && c <= 10000 && d <= 10000) {\n                    Rectangle rect(a, b, c, d);\n                    // Check if (x, y) is inside (including boundary)\n                    if (a <= x && x < c && b <= y && y < d) {\n                        if (!hasOverlap(rect, idx)) {\n                            rectangles[idx] = rect;\n                            return true;\n                        }\n                    }\n                }\n            }\n        }\n    }\n    \n    // If we can't place exact area, try approximate placement\n    // Try to place any rectangle containing the point\n    for (int size = max(1, (int)sqrt(r)); size >= 1; size--) {\n        int w = min(size, 10000);\n        int h = min(max(1, r / w), 10000);\n        \n        // Center around the point\n        int a = max(0, min(x - w/2, 10000 - w));\n        int b = max(0, min(y - h/2, 10000 - h));\n        int c = a + w;\n        int d = b + h;\n        \n        if (c <= 10000 && d <= 10000) {\n            Rectangle rect(a, b, c, d);\n            // Check if point is inside\n            if (a <= x && x < c && b <= y && y < d) {\n                if (!hasOverlap(rect, idx)) {\n                    rectangles[idx] = rect;\n                    return true;\n                }\n            }\n        }\n    }\n    \n    // Last resort: place smallest possible rectangle\n    Rectangle rect(max(0, x), max(0, y), min(10000, x + 1), min(10000, y + 1));\n    if (!hasOverlap(rect, idx)) {\n        rectangles[idx] = rect;\n        return true;\n    }\n    \n    return false;\n}\n\n// Improved placement trying multiple strategies\nbool placeRectangleImproved(int idx) {\n    const Company& company = companies[idx];\n    int x = company.x;\n    int y = company.y;\n    int r = company.r;\n    \n    // Strategy 1: Try exact area placement\n    for (int w = 1; w <= min(r, 10000); w++) {\n        if (r % w == 0) {\n            int h = r / w;\n            if (h <= 10000) {\n                // Try to center around the point\n                for (int offset_x = min(w, 20); offset_x >= 0; offset_x--) {\n                    for (int offset_y = min(h, 20); offset_y >= 0; offset_y--) {\n                        int a = max(0, min(x - offset_x, 10000 - w));\n                        int b = max(0, min(y - offset_y, 10000 - h));\n                        int c = a + w;\n                        int d = b + h;\n                        \n                        if (c <= 10000 && d <= 10000) {\n                            Rectangle rect(a, b, c, d);\n                            if (a <= x && x < c && b <= y && y < d) {\n                                if (!hasOverlap(rect, idx)) {\n                                    rectangles[idx] = rect;\n                                    return true;\n                                }\n                            }\n                        }\n                    }\n                }\n            }\n        }\n    }\n    \n    // Strategy 2: Place any valid rectangle containing the point\n    int best_area = 0;\n    Rectangle best_rect;\n    \n    for (int w = 1; w <= 100 && w <= 10000; w++) {\n        for (int h = 1; h <= 100 && h <= 10000; h++) {\n            // Try different positions\n            for (int dx = 0; dx < min(w, 20); dx++) {\n                for (int dy = 0; dy < min(h, 20); dy++) {\n                    int a = max(0, min(x - dx, 10000 - w));\n                    int b = max(0, min(y - dy, 10000 - h));\n                    int c = a + w;\n                    int d = b + h;\n                    \n                    if (c <= 10000 && d <= 10000) {\n                        Rectangle rect(a, b, c, d);\n                        if (a <= x && x < c && b <= y && y < d) {\n                            if (!hasOverlap(rect, idx)) {\n                                int area = rect.area();\n                                if (area > best_area) {\n                                    best_area = area;\n                                    best_rect = rect;\n                                }\n                            }\n                        }\n                    }\n                }\n            }\n        }\n    }\n    \n    if (best_area > 0) {\n        rectangles[idx] = best_rect;\n        return true;\n    }\n    \n    // Strategy 3: Last resort - minimal rectangle\n    for (int a = max(0, x - 5); a <= min(x + 5, 9999); a++) {\n        for (int b = max(0, y - 5); b <= min(y + 5, 9999); b++) {\n            Rectangle rect(a, b, a + 1, b + 1);\n            if (a <= x && x < a + 1 && b <= y && y < b + 1) {\n                if (!hasOverlap(rect, idx)) {\n                    rectangles[idx] = rect;\n                    return true;\n                }\n            }\n        }\n    }\n    \n    return false;\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    cin >> n;\n    companies.resize(n);\n    rectangles.resize(n);\n    \n    for (int i = 0; i < n; i++) {\n        int x, y, r;\n        cin >> x >> y >> r;\n        companies[i] = Company(x, y, r, i);\n    }\n    \n    // Sort by area descending (place larger rectangles first)\n    sort(companies.begin(), companies.end(), [](const Company& a, const Company& b) {\n        return a.r > b.r;\n    });\n    \n    // Place rectangles\n    for (int i = 0; i < n; i++) {\n        placeRectangleImproved(companies[i].id);\n    }\n    \n    // Output results\n    vector<pair<int, Rectangle>> result(n);\n    for (int i = 0; i < n; i++) {\n        result[i] = {companies[i].id, rectangles[companies[i].id]};\n    }\n    \n    sort(result.begin(), result.end());\n    \n    for (int i = 0; i < n; i++) {\n        cout << result[i].second.a << \" \" << result[i].second.b << \" \" \n             << result[i].second.c << \" \" << result[i].second.d << \"\\n\";\n    }\n    \n    return 0;\n}","ahc002":"#include <bits/stdc++.h>\n#include <atcoder/all>\nusing namespace std;\nusing namespace atcoder;\n\nconst int SIZE = 50;\nint si, sj;\nint tile[SIZE][SIZE];\nint value[SIZE][SIZE];\nbool visited_tile[2500]; // max 2500 tiles\nvector<pair<int, int>> tile_positions[2500];\nint tile_values[2500];\n\n// Directions: U, D, L, R\nconst int dx[4] = {-1, 1, 0, 0};\nconst int dy[4] = {0, 0, -1, 1};\nconst char dir_chars[4] = {'U', 'D', 'L', 'R'};\n\n// Check if position is valid\nbool is_valid(int x, int y) {\n    return x >= 0 && x < SIZE && y >= 0 && y < SIZE;\n}\n\n// Get tile ID at position\nint get_tile_id(int x, int y) {\n    return tile[x][y];\n}\n\n// Calculate total value of a tile\nint calculate_tile_value(int tile_id) {\n    int total = 0;\n    for (auto& pos : tile_positions[tile_id]) {\n        total += value[pos.first][pos.second];\n    }\n    return total;\n}\n\n// Initialize tile information\nvoid init_tiles() {\n    set<int> tile_ids;\n    for (int i = 0; i < SIZE; i++) {\n        for (int j = 0; j < SIZE; j++) {\n            tile_ids.insert(tile[i][j]);\n        }\n    }\n    \n    for (int i = 0; i < SIZE; i++) {\n        for (int j = 0; j < SIZE; j++) {\n            tile_positions[tile[i][j]].push_back({i, j});\n        }\n    }\n    \n    for (int i = 0; i < 2500; i++) {\n        if (!tile_positions[i].empty()) {\n            tile_values[i] = calculate_tile_value(i);\n        }\n    }\n}\n\n// Check if we can move to a position\nbool can_move(int x, int y) {\n    if (!is_valid(x, y)) return false;\n    int tile_id = get_tile_id(x, y);\n    return !visited_tile[tile_id];\n}\n\n// Greedy path finding with beam search\nstring find_path() {\n    string best_path = \"\";\n    int best_score = -1;\n    \n    // Try multiple strategies\n    for (int strategy = 0; strategy < 3; strategy++) {\n        // Reset visited tiles\n        memset(visited_tile, false, sizeof(visited_tile));\n        \n        string current_path = \"\";\n        int x = si, y = sj;\n        int current_score = value[x][y];\n        int tile_id = get_tile_id(x, y);\n        visited_tile[tile_id] = true;\n        \n        // Maximum steps to prevent infinite loops\n        for (int step = 0; step < 200; step++) {\n            vector<pair<int, pair<int, int>>> candidates; // {score, {nx, ny}}\n            \n            // Evaluate all possible moves\n            for (int d = 0; d < 4; d++) {\n                int nx = x + dx[d];\n                int ny = y + dy[d];\n                \n                if (can_move(nx, ny)) {\n                    int next_tile_id = get_tile_id(nx, ny);\n                    int potential_score = tile_values[next_tile_id];\n                    \n                    // Add some randomness for different strategies\n                    if (strategy == 1) {\n                        potential_score += rand() % 100;\n                    } else if (strategy == 2) {\n                        potential_score -= rand() % 50;\n                    }\n                    \n                    candidates.push_back({potential_score, {nx, ny}});\n                }\n            }\n            \n            if (candidates.empty()) break;\n            \n            // Sort by score (descending)\n            sort(candidates.rbegin(), candidates.rend());\n            \n            // Choose best candidate (with some randomness for strategy 0)\n            int chosen_idx = 0;\n            if (strategy == 0 && candidates.size() > 1) {\n                // 70% chance to pick best, 30% chance to pick second best\n                if (rand() % 100 < 30 && candidates.size() > 1) {\n                    chosen_idx = 1;\n                }\n            }\n            \n            auto chosen = candidates[chosen_idx];\n            int nx = chosen.second.first;\n            int ny = chosen.second.second;\n            \n            // Move to the new position\n            for (int d = 0; d < 4; d++) {\n                if (x + dx[d] == nx && y + dy[d] == ny) {\n                    current_path += dir_chars[d];\n                    break;\n                }\n            }\n            \n            x = nx;\n            y = ny;\n            int new_tile_id = get_tile_id(x, y);\n            current_score += tile_values[new_tile_id];\n            visited_tile[new_tile_id] = true;\n        }\n        \n        if (current_score > best_score) {\n            best_score = current_score;\n            best_path = current_path;\n        }\n    }\n    \n    return best_path;\n}\n\n// Enhanced path finding with look-ahead\nstring find_path_enhanced() {\n    // Reset visited tiles\n    memset(visited_tile, false, sizeof(visited_tile));\n    \n    string path = \"\";\n    int x = si, y = sj;\n    int tile_id = get_tile_id(x, y);\n    visited_tile[tile_id] = true;\n    \n    // Use a more sophisticated approach with look-ahead\n    for (int step = 0; step < 250; step++) {\n        vector<tuple<int, int, int, int>> candidates; // {score, future_score, nx, ny}\n        \n        // Evaluate moves\n        for (int d = 0; d < 4; d++) {\n            int nx = x + dx[d];\n            int ny = y + dy[d];\n            \n            if (can_move(nx, ny)) {\n                int next_tile_id = get_tile_id(nx, ny);\n                int immediate_score = tile_values[next_tile_id];\n                \n                // Look ahead one step\n                int future_score = 0;\n                for (int d2 = 0; d2 < 4; d2++) {\n                    int nnx = nx + dx[d2];\n                    int nny = ny + dy[d2];\n                    \n                    if (is_valid(nnx, nny) && !visited_tile[get_tile_id(nnx, nny)]) {\n                        future_score = max(future_score, tile_values[get_tile_id(nnx, nny)]);\n                    }\n                }\n                \n                candidates.push_back({immediate_score, future_score, nx, ny});\n            }\n        }\n        \n        if (candidates.empty()) break;\n        \n        // Sort by immediate score, then by future score\n        sort(candidates.rbegin(), candidates.rend());\n        \n        // Choose the best move\n        auto best = candidates[0];\n        int nx = get<2>(best);\n        int ny = get<3>(best);\n        \n        // Find direction\n        for (int d = 0; d < 4; d++) {\n            if (x + dx[d] == nx && y + dy[d] == ny) {\n                path += dir_chars[d];\n                break;\n            }\n        }\n        \n        x = nx;\n        y = ny;\n        visited_tile[get_tile_id(x, y)] = true;\n    }\n    \n    return path;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    // Read input\n    cin >> si >> sj;\n    \n    for (int i = 0; i < SIZE; i++) {\n        for (int j = 0; j < SIZE; j++) {\n            cin >> tile[i][j];\n        }\n    }\n    \n    for (int i = 0; i < SIZE; i++) {\n        for (int j = 0; j < SIZE; j++) {\n            cin >> value[i][j];\n        }\n    }\n    \n    // Initialize tile information\n    init_tiles();\n    \n    // Find and output path\n    string result = find_path_enhanced();\n    cout << result << endl;\n    \n    return 0;\n}","ahc003":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int GRID_SIZE = 30;\nconst int MAX_QUERIES = 1000;\n\n// Direction vectors: U, D, L, R\nconst int di[4] = {-1, 1, 0, 0};\nconst int dj[4] = {0, 0, -1, 1};\nconst char dir_chars[4] = {'U', 'D', 'L', 'R'};\n\n// Estimated edge weights\ndouble h[GRID_SIZE][GRID_SIZE-1]; // horizontal edges (i, j) -> (i, j+1)\ndouble v[GRID_SIZE-1][GRID_SIZE]; // vertical edges (i, j) -> (i+1, j)\n\n// Count of observations for each edge (for averaging)\nint h_count[GRID_SIZE][GRID_SIZE-1];\nint v_count[GRID_SIZE-1][GRID_SIZE];\n\nvoid init_weights() {\n    // Initialize with reasonable estimates\n    for (int i = 0; i < GRID_SIZE; i++) {\n        for (int j = 0; j < GRID_SIZE-1; j++) {\n            h[i][j] = 5000.0;\n            h_count[i][j] = 1;\n        }\n    }\n    for (int i = 0; i < GRID_SIZE-1; i++) {\n        for (int j = 0; j < GRID_SIZE; j++) {\n            v[i][j] = 5000.0;\n            v_count[i][j] = 1;\n        }\n    }\n}\n\nstruct State {\n    int i, j;\n    double cost;\n    bool operator>(const State& other) const {\n        return cost > other.cost;\n    }\n};\n\nstring find_path(int si, int sj, int ti, int tj) {\n    // Dijkstra's algorithm\n    vector<vector<double>> dist(GRID_SIZE, vector<double>(GRID_SIZE, 1e18));\n    vector<vector<pair<int, int>>> prev(GRID_SIZE, vector<pair<int, int>>(GRID_SIZE, make_pair(-1, -1)));\n    vector<vector<char>> prev_dir(GRID_SIZE, vector<char>(GRID_SIZE, ' '));\n    \n    priority_queue<State, vector<State>, greater<State>> pq;\n    dist[si][sj] = 0;\n    pq.push({si, sj, 0});\n    \n    while (!pq.empty()) {\n        auto [i, j, cost] = pq.top(); pq.pop();\n        if (cost > dist[i][j]) continue;\n        \n        // Try all 4 directions\n        for (int d = 0; d < 4; d++) {\n            int ni = i + di[d];\n            int nj = j + dj[d];\n            \n            if (ni >= 0 && ni < GRID_SIZE && nj >= 0 && nj < GRID_SIZE) {\n                double edge_cost = 0;\n                if (d == 0) { // Up: (i,j) -> (i-1,j)\n                    if (ni < i) edge_cost = v[ni][nj];\n                } else if (d == 1) { // Down: (i,j) -> (i+1,j)\n                    if (i < ni) edge_cost = v[i][j];\n                } else if (d == 2) { // Left: (i,j) -> (i,j-1)\n                    if (nj < j) edge_cost = h[ni][nj];\n                } else if (d == 3) { // Right: (i,j) -> (i,j+1)\n                    if (j < nj) edge_cost = h[i][j];\n                }\n                \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                    prev_dir[ni][nj] = dir_chars[d];\n                    pq.push({ni, nj, new_cost});\n                }\n            }\n        }\n    }\n    \n    // Reconstruct path\n    string path;\n    int ci = ti, cj = tj;\n    while (ci != si || cj != sj) {\n        path += prev_dir[ci][cj];\n        auto p = prev[ci][cj];\n        ci = p.first;\n        cj = p.second;\n    }\n    reverse(path.begin(), path.end());\n    \n    return path;\n}\n\n// Store the last path for updating weights\nvector<pair<int, int>> last_path_coords;\nstring last_path;\n\nvoid store_path_info(int si, int sj, const string& path) {\n    last_path_coords.clear();\n    last_path_coords.push_back({si, sj});\n    \n    int ci = si, cj = sj;\n    for (char c : path) {\n        if (c == 'U') ci--;\n        else if (c == 'D') ci++;\n        else if (c == 'L') cj--;\n        else if (c == 'R') cj++;\n        last_path_coords.push_back({ci, cj});\n    }\n    last_path = path;\n}\n\nvoid update_weights(int actual_cost) {\n    // Calculate estimated cost of the last path\n    double estimated_cost = 0;\n    for (int k = 0; k < (int)last_path.length(); k++) {\n        int i1 = last_path_coords[k].first;\n        int j1 = last_path_coords[k].second;\n        int i2 = last_path_coords[k+1].first;\n        int j2 = last_path_coords[k+1].second;\n        \n        if (i1 == i2) {\n            // Horizontal move\n            int j = min(j1, j2);\n            estimated_cost += h[i1][j];\n        } else {\n            // Vertical move\n            int i = min(i1, i2);\n            estimated_cost += v[i][j1];\n        }\n    }\n    \n    if (estimated_cost <= 0) return;\n    \n    // Update factor\n    double factor = (double)actual_cost / estimated_cost;\n    \n    // Update weights with some smoothing\n    const double learning_rate = 0.1;\n    \n    for (int k = 0; k < (int)last_path.length(); k++) {\n        int i1 = last_path_coords[k].first;\n        int j1 = last_path_coords[k].second;\n        int i2 = last_path_coords[k+1].first;\n        int j2 = last_path_coords[k+1].second;\n        \n        if (i1 == i2) {\n            // Horizontal move\n            int j = min(j1, j2);\n            h[i1][j] = h[i1][j] * (1 - learning_rate) + (h[i1][j] * factor) * learning_rate;\n            // Keep weights in reasonable range\n            h[i1][j] = max(100.0, min(10000.0, h[i1][j]));\n        } else {\n            // Vertical move\n            int i = min(i1, i2);\n            v[i][j1] = v[i][j1] * (1 - learning_rate) + (v[i][j1] * factor) * learning_rate;\n            // Keep weights in reasonable range\n            v[i][j1] = max(100.0, min(10000.0, v[i][j1]));\n        }\n    }\n}\n\nint main() {\n    init_weights();\n    \n    for (int query = 0; query < MAX_QUERIES; query++) {\n        int si, sj, ti, tj;\n        cin >> si >> sj >> ti >> tj;\n        \n        string path = find_path(si, sj, ti, tj);\n        store_path_info(si, sj, path);\n        \n        cout << path << endl;\n        cout.flush();\n        \n        int result;\n        cin >> result;\n        \n        update_weights(result);\n    }\n    \n    return 0;\n}","ahc004":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 20;\nconst string CHARS = \"ABCDEFGH\";\n\nstruct StringInfo {\n    string s;\n    int id;\n    int length;\n};\n\n// Matrix representation\nvector<vector<char>> matrix(N, vector<char>(N, '.'));\nvector<string> strings;\n\n// Count how many strings are satisfied\nint countSatisfied(const vector<vector<char>>& mat, const vector<string>& strs) {\n    int count = 0;\n    for (const string& s : strs) {\n        bool found = false;\n        // Check horizontal\n        for (int i = 0; i < N && !found; i++) {\n            for (int j = 0; j < N && !found; j++) {\n                bool match = true;\n                for (int k = 0; k < (int)s.length(); k++) {\n                    int col = (j + k) % N;\n                    if (mat[i][col] != '.' && mat[i][col] != s[k]) {\n                        match = false;\n                        break;\n                    }\n                }\n                if (match) {\n                    found = true;\n                }\n            }\n        }\n        // Check vertical\n        for (int j = 0; j < N && !found; j++) {\n            for (int i = 0; i < N && !found; i++) {\n                bool match = true;\n                for (int k = 0; k < (int)s.length(); k++) {\n                    int row = (i + k) % N;\n                    if (mat[row][j] != '.' && mat[row][j] != s[k]) {\n                        match = false;\n                        break;\n                    }\n                }\n                if (match) {\n                    found = true;\n                }\n            }\n        }\n        if (found) count++;\n    }\n    return count;\n}\n\n// Check if a string can be placed at a specific position and direction\nbool canPlace(const vector<vector<char>>& mat, const string& s, int row, int col, bool vertical) {\n    if (vertical) {\n        for (int i = 0; i < (int)s.length(); i++) {\n            int r = (row + i) % N;\n            if (mat[r][col] != '.' && mat[r][col] != s[i]) {\n                return false;\n            }\n        }\n    } else {\n        for (int j = 0; j < (int)s.length(); j++) {\n            int c = (col + j) % N;\n            if (mat[row][c] != '.' && mat[row][c] != s[j]) {\n                return false;\n            }\n        }\n    }\n    return true;\n}\n\n// Place a string (modifies matrix)\nvoid placeString(vector<vector<char>>& mat, const string& s, int row, int col, bool vertical) {\n    if (vertical) {\n        for (int i = 0; i < (int)s.length(); i++) {\n            int r = (row + i) % N;\n            mat[r][col] = s[i];\n        }\n    } else {\n        for (int j = 0; j < (int)s.length(); j++) {\n            int c = (col + j) % N;\n            mat[row][c] = s[j];\n        }\n    }\n}\n\n// Try to place strings greedily\nvoid greedyPlacement() {\n    // Sort strings by length (longer first) and then by some heuristic\n    vector<pair<int, int>> stringScores;\n    for (int i = 0; i < (int)strings.size(); i++) {\n        stringScores.push_back({-strings[i].length(), i}); // negative for descending order\n    }\n    sort(stringScores.begin(), stringScores.end());\n    \n    vector<int> stringOrder;\n    for (auto& p : stringScores) {\n        stringOrder.push_back(p.second);\n    }\n    \n    // Try to place each string\n    for (int idx : stringOrder) {\n        const string& s = strings[idx];\n        bool placed = false;\n        \n        // Try all positions and directions\n        for (int dir = 0; dir < 2 && !placed; dir++) {\n            bool vertical = (dir == 1);\n            for (int i = 0; i < N && !placed; i++) {\n                for (int j = 0; j < N && !placed; j++) {\n                    if (canPlace(matrix, s, i, j, vertical)) {\n                        placeString(matrix, s, i, j, vertical);\n                        placed = true;\n                    }\n                }\n            }\n        }\n    }\n}\n\n// Local search improvement\nvoid localSearch() {\n    // Simple local search: try random changes and keep improvements\n    random_device rd;\n    mt19937 gen(rd());\n    uniform_int_distribution<> dis(0, N-1);\n    uniform_int_distribution<> char_dis(0, 7);\n    \n    int current_score = countSatisfied(matrix, strings);\n    \n    for (int iter = 0; iter < 5000; iter++) {\n        // Pick a random cell to modify\n        int i = dis(gen);\n        int j = dis(gen);\n        char original = matrix[i][j];\n        \n        // Try changing it to a random character or '.'\n        if (uniform_int_distribution<>(0, 4)(gen) == 0) {\n            matrix[i][j] = '.';\n        } else {\n            matrix[i][j] = CHARS[char_dis(gen)];\n        }\n        \n        int new_score = countSatisfied(matrix, strings);\n        if (new_score < current_score) {\n            // Revert change\n            matrix[i][j] = original;\n        } else {\n            current_score = new_score;\n        }\n    }\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    int M;\n    cin >> N >> M;\n    \n    strings.resize(M);\n    for (int i = 0; i < M; i++) {\n        cin >> strings[i];\n    }\n    \n    // Initialize matrix\n    matrix.assign(N, vector<char>(N, '.'));\n    \n    // Greedy placement\n    greedyPlacement();\n    \n    // Local search improvement\n    localSearch();\n    \n    // Output result\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cout << matrix[i][j];\n        }\n        cout << \"\\n\";\n    }\n    \n    return 0;\n}","ahc005":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MAX_N = 70;\nint N, si, sj;\nchar grid[MAX_N][MAX_N];\nbool is_road[MAX_N][MAX_N];\nint cost[MAX_N][MAX_N];\nvector<pair<int,int>> roads;\nvector<pair<int,int>> visible_from[MAX_N][MAX_N];\n\n// Directions: U, D, L, R\nconst int dx[4] = {-1, 1, 0, 0};\nconst int dy[4] = {0, 0, -1, 1};\nconst char dir_char[4] = {'U', 'D', 'L', 'R'};\n\nvoid precompute_visibility() {\n    // For each road cell, precompute what cells are visible from it\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (!is_road[i][j]) continue;\n            \n            // Horizontal visibility (same row)\n            for (int col = 0; col < N; col++) {\n                if (!is_road[i][col]) continue;\n                \n                // Check if path is clear\n                bool clear = true;\n                int start_col = min(j, col);\n                int end_col = max(j, col);\n                for (int c = start_col; c <= end_col; c++) {\n                    if (!is_road[i][c]) {\n                        clear = false;\n                        break;\n                    }\n                }\n                if (clear) {\n                    visible_from[i][j].push_back({i, col});\n                }\n            }\n            \n            // Vertical visibility (same column)\n            for (int row = 0; row < N; row++) {\n                if (!is_road[row][j]) continue;\n                \n                // Check if path is clear\n                bool clear = true;\n                int start_row = min(i, row);\n                int end_row = max(i, row);\n                for (int r = start_row; r <= end_row; r++) {\n                    if (!is_road[r][j]) {\n                        clear = false;\n                        break;\n                    }\n                }\n                if (clear) {\n                    visible_from[i][j].push_back({row, j});\n                }\n            }\n        }\n    }\n}\n\nbool is_valid(int x, int y) {\n    return x >= 0 && x < N && y >= 0 && y < N && is_road[x][y];\n}\n\n// BFS to find shortest path between two points\nvector<pair<int,int>> bfs_path(int sx, int sy, int tx, int ty) {\n    if (sx == tx && sy == ty) return {{sx, sy}};\n    \n    queue<pair<int,int>> q;\n    bool visited[MAX_N][MAX_N] = {false};\n    pair<int,int> parent[MAX_N][MAX_N];\n    \n    q.push({sx, sy});\n    visited[sx][sy] = true;\n    \n    while (!q.empty()) {\n        auto [x, y] = q.front(); q.pop();\n        \n        for (int d = 0; d < 4; d++) {\n            int nx = x + dx[d];\n            int ny = y + dy[d];\n            \n            if (is_valid(nx, ny) && !visited[nx][ny]) {\n                visited[nx][ny] = true;\n                parent[nx][ny] = {x, y};\n                q.push({nx, ny});\n                \n                if (nx == tx && ny == ty) {\n                    // Reconstruct path\n                    vector<pair<int,int>> path;\n                    pair<int,int> current = {tx, ty};\n                    while (current.first != sx || current.second != sy) {\n                        path.push_back(current);\n                        current = parent[current.first][current.second];\n                    }\n                    path.push_back({sx, sy});\n                    reverse(path.begin(), path.end());\n                    return path;\n                }\n            }\n        }\n    }\n    \n    return {{sx, sy}}; // Fallback\n}\n\n// Convert path to direction string\nstring path_to_directions(const vector<pair<int,int>>& path) {\n    string result;\n    for (int i = 1; i < path.size(); i++) {\n        auto [prev_x, prev_y] = path[i-1];\n        auto [curr_x, curr_y] = path[i];\n        \n        if (curr_x == prev_x - 1 && curr_y == prev_y) result += 'U';\n        else if (curr_x == prev_x + 1 && curr_y == prev_y) result += 'D';\n        else if (curr_x == prev_x && curr_y == prev_y - 1) result += 'L';\n        else if (curr_x == prev_x && curr_y == prev_y + 1) result += 'R';\n    }\n    return result;\n}\n\n// Simple greedy approach: sweep row by row\nstring solve_greedy() {\n    string result;\n    int current_x = si, current_y = sj;\n    \n    // Collect all road positions\n    vector<pair<int,int>> road_positions;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (is_road[i][j]) {\n                road_positions.push_back({i, j});\n            }\n        }\n    }\n    \n    // Simple approach: visit roads in order, trying to cover new areas\n    set<pair<int,int>> visited_positions;\n    set<pair<int,int>> covered_roads;\n    \n    // Start from initial position\n    visited_positions.insert({si, sj});\n    \n    // Add initially visible roads\n    for (auto& pos : visible_from[si][sj]) {\n        covered_roads.insert(pos);\n    }\n    \n    // Greedy selection: pick next road that covers most uncovered roads\n    vector<pair<int,int>> path_positions = {{si, sj}};\n    \n    const int MAX_STEPS = 2000; // Limit to prevent timeout\n    int steps = 0;\n    \n    while (covered_roads.size() < roads.size() && steps < MAX_STEPS) {\n        pair<int,int> best_next = {-1, -1};\n        int best_coverage = -1;\n        \n        // Find the road position that gives maximum new coverage\n        for (auto& road_pos : road_positions) {\n            if (visited_positions.count(road_pos)) continue;\n            \n            // Count how many new roads would be covered\n            int new_coverage = 0;\n            for (auto& visible : visible_from[road_pos.first][road_pos.second]) {\n                if (!covered_roads.count(visible)) {\n                    new_coverage++;\n                }\n            }\n            \n            if (new_coverage > best_coverage) {\n                best_coverage = new_coverage;\n                best_next = road_pos;\n            }\n        }\n        \n        if (best_next.first == -1) {\n            // No good move found, pick any uncovered road\n            for (auto& road_pos : road_positions) {\n                if (!visited_positions.count(road_pos)) {\n                    best_next = road_pos;\n                    break;\n                }\n            }\n            if (best_next.first == -1) break;\n        }\n        \n        // Move to best_next position\n        auto path_segment = bfs_path(current_x, current_y, best_next.first, best_next.second);\n        for (int i = 1; i < path_segment.size(); i++) {\n            path_positions.push_back(path_segment[i]);\n        }\n        \n        current_x = best_next.first;\n        current_y = best_next.second;\n        visited_positions.insert(best_next);\n        \n        // Update covered roads\n        for (auto& visible : visible_from[current_x][current_y]) {\n            covered_roads.insert(visible);\n        }\n        \n        steps++;\n    }\n    \n    // Return to start\n    auto return_path = bfs_path(current_x, current_y, si, sj);\n    for (int i = 1; i < return_path.size(); i++) {\n        path_positions.push_back(return_path[i]);\n    }\n    \n    return path_to_directions(path_positions);\n}\n\n// Row/column sweeping approach\nstring solve_sweep() {\n    vector<pair<int,int>> path_positions = {{si, sj}};\n    \n    // Collect rows and columns with roads\n    set<int> rows_with_roads, cols_with_roads;\n    for (auto& road : roads) {\n        rows_with_roads.insert(road.first);\n        cols_with_roads.insert(road.second);\n    }\n    \n    int current_x = si, current_y = sj;\n    \n    // Sweep strategy: go to each row and sweep left-right or right-left\n    vector<int> rows(rows_with_roads.begin(), rows_with_roads.end());\n    vector<int> cols(cols_with_roads.begin(), cols_with_roads.end());\n    \n    set<pair<int,int>> visited_positions;\n    visited_positions.insert({si, sj});\n    \n    // Simple row-by-row visit\n    for (int row : rows) {\n        vector<int> row_cols;\n        for (auto& road : roads) {\n            if (road.first == row) {\n                row_cols.push_back(road.second);\n            }\n        }\n        sort(row_cols.begin(), row_cols.end());\n        \n        if (row_cols.empty()) continue;\n        \n        // Go to this row (nearest column)\n        int target_col = row_cols[0];\n        if (current_x != row || current_y != target_col) {\n            // Simple move: go to target position\n            while (current_x < row) {\n                if (is_valid(current_x + 1, current_y)) {\n                    current_x++;\n                    path_positions.push_back({current_x, current_y});\n                } else break;\n            }\n            while (current_x > row) {\n                if (is_valid(current_x - 1, current_y)) {\n                    current_x--;\n                    path_positions.push_back({current_x, current_y});\n                } else break;\n            }\n            while (current_y < target_col) {\n                if (is_valid(current_x, current_y + 1)) {\n                    current_y++;\n                    path_positions.push_back({current_x, current_y});\n                } else break;\n            }\n            while (current_y > target_col) {\n                if (is_valid(current_x, current_y - 1)) {\n                    current_y--;\n                    path_positions.push_back({current_x, current_y});\n                } else break;\n            }\n        }\n        \n        // Sweep the row\n        for (int col : row_cols) {\n            if (current_y != col) {\n                int step = (col > current_y) ? 1 : -1;\n                while (current_y != col) {\n                    int next_y = current_y + step;\n                    if (is_valid(current_x, next_y)) {\n                        current_y = next_y;\n                        path_positions.push_back({current_x, current_y});\n                    } else break;\n                }\n            }\n        }\n    }\n    \n    // Return to start\n    auto return_path = bfs_path(current_x, current_y, si, sj);\n    for (int i = 1; i < return_path.size(); i++) {\n        path_positions.push_back(return_path[i]);\n    }\n    \n    return path_to_directions(path_positions);\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    cin >> N >> si >> sj;\n    \n    for (int i = 0; i < N; i++) {\n        string line;\n        cin >> line;\n        for (int j = 0; j < N; j++) {\n            grid[i][j] = line[j];\n            if (line[j] >= '5' && line[j] <= '9') {\n                is_road[i][j] = true;\n                cost[i][j] = line[j] - '0';\n                roads.push_back({i, j});\n            } else {\n                is_road[i][j] = false;\n            }\n        }\n    }\n    \n    precompute_visibility();\n    \n    // Try different approaches and pick the best\n    string result1 = solve_greedy();\n    string result2 = solve_sweep();\n    \n    // Output the better result (simpler for now)\n    cout << result2 << endl;\n    \n    return 0;\n}","future-contest-2022-qual":"#include <bits/stdc++.h>\n#include <atcoder/all>\nusing namespace std;\nusing namespace atcoder;\n\n// Constants\nconst int MAX_N = 1000;\nconst int MAX_M = 20;\nconst int MAX_DAYS = 2000;\n\n// Global variables\nint N, M, K, R;\nvector<vector<int>> task_difficulty;  // d[i][k]\nvector<vector<int>> task_dependencies; // dependencies for each task\nvector<vector<int>> reverse_dependencies; // which tasks depend on this task\nvector<vector<int>> completion_times; // actual completion times (hidden)\nvector<vector<int>> skill_estimates;  // s[j][k] - our estimates\nvector<int> task_status;  // -1: not started, 0: in progress, 1: completed\nvector<int> member_current_task; // which task each member is working on\nvector<int> task_start_day; // when each task started\nvector<int> in_degree; // in-degree for topological sorting\nvector<bool> task_available; // whether task can be started (dependencies done)\npriority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> task_queue; // priority queue for tasks\n\n// Initialize data structures\nvoid initialize() {\n    task_difficulty.assign(N, vector<int>(K));\n    task_dependencies.assign(N, vector<int>());\n    reverse_dependencies.assign(N, vector<int>());\n    completion_times.assign(N, vector<int>(M));\n    skill_estimates.assign(M, vector<int>(K, 10)); // initial guess\n    task_status.assign(N, -1);\n    member_current_task.assign(M, -1);\n    task_start_day.assign(N, -1);\n    in_degree.assign(N, 0);\n    task_available.assign(N, false);\n}\n\n// Calculate estimated time for member j to complete task i\nint estimate_time(int task_id, int member_id) {\n    int w = 0;\n    for (int k = 0; k < K; k++) {\n        w += max(0, task_difficulty[task_id][k] - skill_estimates[member_id][k]);\n    }\n    if (w == 0) return 1;\n    return max(1, w + 1); // conservative estimate (middle of random range)\n}\n\n// Update skill estimates based on actual performance\nvoid update_skills(int task_id, int member_id, int actual_time, int days_taken) {\n    // Only update if we have meaningful data\n    if (days_taken <= 0) return;\n    \n    int w = 0;\n    for (int k = 0; k < K; k++) {\n        w += max(0, task_difficulty[task_id][k] - skill_estimates[member_id][k]);\n    }\n    \n    // Estimate actual w based on observed time\n    int estimated_w = (actual_time == 1) ? 0 : (actual_time - 1); // reverse engineer w\n    \n    // Adjust skills based on difference\n    if (w > estimated_w) {\n        // Member is better than we thought - increase skill estimates\n        for (int k = 0; k < K; k++) {\n            if (task_difficulty[task_id][k] > skill_estimates[member_id][k]) {\n                skill_estimates[member_id][k] = min(skill_estimates[member_id][k] + 1, \n                                                   task_difficulty[task_id][k] + 5);\n            }\n        }\n    } else if (w < estimated_w) {\n        // Member is worse than we thought - but be conservative\n        // Since we don't know exact breakdown, make small adjustments\n    }\n}\n\n// Check which tasks are available to start (all dependencies completed)\nvoid update_available_tasks() {\n    for (int i = 0; i < N; i++) {\n        if (task_status[i] == -1 && !task_available[i]) {\n            bool can_start = true;\n            for (int dep : task_dependencies[i]) {\n                if (task_status[dep] != 1) {\n                    can_start = false;\n                    break;\n                }\n            }\n            if (can_start) {\n                task_available[i] = true;\n                // Priority: tasks that take longer (to get them started early)\n                int max_time = 0;\n                for (int j = 0; j < M; j++) {\n                    max_time = max(max_time, estimate_time(i, j));\n                }\n                task_queue.push({-max_time, i}); // negative for max-heap behavior\n            }\n        }\n    }\n}\n\nint main() {\n    // Read initial information\n    cin >> N >> M >> K >> R;\n    initialize();\n    \n    // Read task difficulties\n    for (int i = 0; i < N; i++) {\n        for (int k = 0; k < K; k++) {\n            cin >> task_difficulty[i][k];\n        }\n    }\n    \n    // Read dependencies\n    for (int i = 0; i < R; i++) {\n        int u, v;\n        cin >> u >> v;\n        u--; v--; // Convert to 0-indexed\n        task_dependencies[v].push_back(u);\n        reverse_dependencies[u].push_back(v);\n        in_degree[v]++;\n    }\n    \n    // Initialize available tasks (those with no dependencies)\n    update_available_tasks();\n    \n    int day = 0;\n    while (day < MAX_DAYS) {\n        day++;\n        \n        // Prepare assignments for this day\n        vector<pair<int, int>> assignments; // {member, task}\n        vector<bool> member_busy(M, false);\n        \n        // Assign tasks to free members\n        for (int member = 0; member < M; member++) {\n            if (member_current_task[member] == -1) { // Member is free\n                // Find best task to assign\n                vector<pair<int, int>> candidates; // {estimated_time, task_id}\n                \n                // Look through available tasks\n                priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> temp_queue = task_queue;\n                int checked = 0;\n                while (!task_queue.empty() && checked < 20) { // Limit search\n                    auto [neg_priority, task_id] = task_queue.top();\n                    task_queue.pop();\n                    if (task_status[task_id] == -1 && task_available[task_id]) {\n                        int est_time = estimate_time(task_id, member);\n                        candidates.push_back({est_time, task_id});\n                    }\n                    checked++;\n                }\n                \n                if (!candidates.empty()) {\n                    // Sort by estimated time (prefer shorter tasks)\n                    sort(candidates.begin(), candidates.end());\n                    int best_task = candidates[0].second;\n                    \n                    assignments.push_back({member, best_task});\n                    member_current_task[member] = best_task;\n                    task_status[best_task] = 0;\n                    task_start_day[best_task] = day;\n                }\n            }\n        }\n        \n        // Output assignments\n        cout << assignments.size();\n        for (auto& assignment : assignments) {\n            cout << \" \" << (assignment.first + 1) << \" \" << (assignment.second + 1);\n        }\n        cout << endl;\n        cout.flush();\n        \n        // Read completion information\n        int completion_count;\n        cin >> completion_count;\n        \n        if (completion_count == -1) {\n            // All tasks completed or time limit reached\n            break;\n        }\n        \n        if (completion_count == 0) {\n            // No completions, continue to next day\n            continue;\n        }\n        \n        // Process completed tasks\n        for (int i = 0; i < completion_count; i++) {\n            int member_id;\n            cin >> member_id;\n            member_id--; // Convert to 0-indexed\n            \n            int task_id = member_current_task[member_id];\n            if (task_id != -1) {\n                task_status[task_id] = 1;\n                member_current_task[member_id] = -1;\n                \n                // Update skills based on performance\n                int days_taken = day - task_start_day[task_id];\n                // We don't know actual time, but we can make educated guesses\n                update_skills(task_id, member_id, completion_times[task_id][member_id], days_taken);\n                \n                // Make dependent tasks available\n                for (int dependent : reverse_dependencies[task_id]) {\n                    in_degree[dependent]--;\n                    if (in_degree[dependent] == 0) {\n                        task_available[dependent] = true;\n                        int max_time = 0;\n                        for (int j = 0; j < M; j++) {\n                            max_time = max(max_time, estimate_time(dependent, j));\n                        }\n                        task_queue.push({-max_time, dependent});\n                    }\n                }\n            }\n        }\n        \n        // Update available tasks for next assignments\n        update_available_tasks();\n    }\n    \n    return 0;\n}","ahc006":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int DEPOT_X = 400;\nconst int DEPOT_Y = 400;\nconst int NUM_ORDERS = 1000;\nconst int SELECT_COUNT = 50;\n\nstruct Order {\n    int id, a, b, c, d;\n    int pickup_id, delivery_id;\n    \n    Order(int _id, int _a, int _b, int _c, int _d) \n        : id(_id), a(_a), b(_b), c(_c), d(_d) {\n        pickup_id = 2 * id;\n        delivery_id = 2 * id + 1;\n    }\n    \n    int distance() const {\n        return abs(a - c) + abs(b - d);\n    }\n};\n\nstruct Point {\n    int x, y;\n    Point() : x(0), y(0) {}\n    Point(int _x, int _y) : x(_x), y(_y) {}\n    \n    int distance(const Point& other) const {\n        return abs(x - other.x) + abs(y - other.y);\n    }\n};\n\n// Calculate total distance of a route\nlong long calculateRouteDistance(const vector<Point>& route) {\n    long long total = 0;\n    for (size_t i = 0; i + 1 < route.size(); i++) {\n        total += route[i].distance(route[i + 1]);\n    }\n    return total;\n}\n\n// Greedy selection based on efficiency and clustering\nvector<int> selectOrders(const vector<Order>& orders) {\n    vector<pair<double, int>> scores;\n    \n    for (int i = 0; i < NUM_ORDERS; i++) {\n        const Order& order = orders[i];\n        \n        // Direct distance from depot to pickup and delivery\n        Point depot(DEPOT_X, DEPOT_Y);\n        Point pickup(order.a, order.b);\n        Point delivery(order.c, order.d);\n        \n        int direct_cost = depot.distance(pickup) + pickup.distance(delivery) + delivery.distance(depot);\n        int order_distance = order.distance();\n        \n        // Efficiency score - lower is better\n        double efficiency = (double)order_distance / direct_cost;\n        \n        scores.push_back({-efficiency, i}); // Negative for sorting (higher efficiency first)\n    }\n    \n    sort(scores.begin(), scores.end());\n    \n    vector<int> selected;\n    for (int i = 0; i < SELECT_COUNT; i++) {\n        selected.push_back(scores[i].second);\n    }\n    \n    return selected;\n}\n\n// Simple greedy TSP solver for pickup-delivery\nvector<Point> solveTSP(const vector<Order>& orders, const vector<int>& selected_ids) {\n    vector<pair<int, int>> tasks; // {order_id, is_pickup: 0=pickup, 1=delivery}\n    \n    for (int id : selected_ids) {\n        tasks.push_back({id, 0}); // pickup\n        tasks.push_back({id, 1}); // delivery\n    }\n    \n    vector<Point> route = {Point(DEPOT_X, DEPOT_Y)};\n    set<int> picked_up;\n    set<int> delivered;\n    \n    while (picked_up.size() < selected_ids.size() || delivered.size() < selected_ids.size()) {\n        Point current = route.back();\n        int best_idx = -1;\n        int best_distance = 1e9;\n        Point best_point(0, 0);\n        \n        // Try all possible next moves\n        for (size_t i = 0; i < tasks.size(); i++) {\n            int order_id = tasks[i].first;\n            int is_delivery = tasks[i].second;\n            \n            if (is_delivery && picked_up.find(order_id) == picked_up.end()) continue;\n            if (!is_delivery && picked_up.find(order_id) != picked_up.end()) continue;\n            \n            Point next_point;\n            if (is_delivery) {\n                next_point = Point(orders[order_id].c, orders[order_id].d);\n            } else {\n                next_point = Point(orders[order_id].a, orders[order_id].b);\n            }\n            \n            int dist = current.distance(next_point);\n            if (dist < best_distance) {\n                best_distance = dist;\n                best_idx = i;\n                best_point = next_point;\n            }\n        }\n        \n        if (best_idx != -1) {\n            route.push_back(best_point);\n            int order_id = tasks[best_idx].first;\n            if (tasks[best_idx].second == 0) {\n                picked_up.insert(order_id);\n            } else {\n                delivered.insert(order_id);\n            }\n        } else {\n            break;\n        }\n    }\n    \n    route.push_back(Point(DEPOT_X, DEPOT_Y));\n    return route;\n}\n\n// 2-opt local search improvement\nvoid twoOpt(vector<Point>& route) {\n    bool improved = true;\n    while (improved) {\n        improved = false;\n        for (size_t i = 1; i < route.size() - 2; i++) {\n            for (size_t j = i + 1; j < route.size() - 1; j++) {\n                long long current_dist = route[i-1].distance(route[i]) + route[j].distance(route[j+1]);\n                long long new_dist = route[i-1].distance(route[j]) + route[i].distance(route[j+1]);\n                \n                if (new_dist < current_dist) {\n                    reverse(route.begin() + i, route.begin() + j + 1);\n                    improved = true;\n                }\n            }\n        }\n    }\n}\n\nint main() {\n    vector<Order> orders;\n    \n    // Read input\n    for (int i = 0; i < NUM_ORDERS; i++) {\n        int a, b, c, d;\n        cin >> a >> b >> c >> d;\n        orders.emplace_back(i, a, b, c, d);\n    }\n    \n    // Select orders\n    vector<int> selected_ids = selectOrders(orders);\n    \n    // Solve TSP\n    vector<Point> route = solveTSP(orders, selected_ids);\n    \n    // Improve with 2-opt\n    twoOpt(route);\n    \n    // Output\n    cout << SELECT_COUNT;\n    for (int id : selected_ids) {\n        cout << \" \" << (id + 1); // 1-indexed\n    }\n    cout << endl;\n    \n    cout << route.size();\n    for (const Point& p : route) {\n        cout << \" \" << p.x << \" \" << p.y;\n    }\n    cout << endl;\n    \n    return 0;\n}","ahc007":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 400;\nconst int M = 1995;\n\nclass UnionFind {\nprivate:\n    vector<int> parent, rank;\npublic:\n    UnionFind(int n) : parent(n), rank(n, 0) {\n        for (int i = 0; i < n; i++) {\n            parent[i] = i;\n        }\n    }\n    \n    int find(int x) {\n        if (parent[x] != x) {\n            parent[x] = find(parent[x]);\n        }\n        return parent[x];\n    }\n    \n    bool unite(int x, int y) {\n        x = find(x);\n        y = find(y);\n        if (x == y) return false;\n        if (rank[x] < rank[y]) swap(x, y);\n        parent[y] = x;\n        if (rank[x] == rank[y]) rank[x]++;\n        return true;\n    }\n    \n    bool same(int x, int y) {\n        return find(x) == find(y);\n    }\n};\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    // Read coordinates\n    vector<pair<int, int>> coords(N);\n    for (int i = 0; i < N; i++) {\n        cin >> coords[i].first >> coords[i].second;\n    }\n    \n    // Read edges\n    vector<pair<int, int>> edges(M);\n    for (int i = 0; i < M; i++) {\n        cin >> edges[i].first >> edges[i].second;\n    }\n    \n    // Precompute minimum distances\n    vector<int> min_distances(M);\n    for (int i = 0; i < M; i++) {\n        int u = edges[i].first;\n        int v = edges[i].second;\n        long long dx = coords[u].first - coords[v].first;\n        long long dy = coords[u].second - coords[v].second;\n        long long dist_sq = dx * dx + dy * dy;\n        double dist = sqrt(dist_sq);\n        min_distances[i] = (int)round(dist);\n    }\n    \n    // Initialize Union-Find\n    UnionFind uf(N);\n    int components = N;\n    \n    // Process edges as they arrive\n    for (int i = 0; i < M; i++) {\n        int length;\n        cin >> length;\n        \n        int u = edges[i].first;\n        int v = edges[i].second;\n        int min_dist = min_distances[i];\n        \n        // Check if this edge connects two different components\n        if (!uf.same(u, v)) {\n            // This edge reduces the number of components - accept it\n            uf.unite(u, v);\n            components--;\n            cout << \"1\" << endl;\n            cout.flush();\n        } else {\n            // This edge creates a cycle\n            // Accept if it's significantly shorter than what we might expect later\n            double ratio = (double)length / min_dist;\n            \n            // Since edges come from 5 MSTs, many edges should be useful\n            // Be conservative but accept reasonable edges\n            if (ratio <= 1.3) {\n                cout << \"1\" << endl;\n                cout.flush();\n            } else if (ratio <= 1.8 && (rand() % 100) < 50) {\n                cout << \"1\" << endl;\n                cout.flush();\n            } else if (ratio <= 2.2 && (rand() % 100) < 20) {\n                cout << \"1\" << endl;\n                cout.flush();\n            } else {\n                cout << \"0\" << endl;\n                cout.flush();\n            }\n        }\n    }\n    \n    return 0;\n}","ahc008":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int GRID_SIZE = 30;\nconst int MAX_TURNS = 300;\nconst int dx[4] = {-1, 1, 0, 0}; // U, D, L, R\nconst int dy[4] = {0, 0, -1, 1};\nconst char dir_chars[4] = {'u', 'd', 'l', 'r'};\nconst char move_chars[4] = {'U', 'D', 'L', 'R'};\n\nstruct Position {\n    int x, y;\n    Position() : x(0), y(0) {}\n    Position(int _x, int _y) : x(_x), y(_y) {}\n    bool operator==(const Position& other) const {\n        return x == other.x && y == other.y;\n    }\n};\n\nstruct Pet {\n    Position pos;\n    int type;\n    Position target; // For dogs and cats\n    bool has_target;\n    \n    Pet() : pos(0, 0), type(0), has_target(false) {}\n    Pet(int x, int y, int t) : pos(x, y), type(t), has_target(false) {}\n};\n\nstruct Human {\n    Position pos;\n    Position initial_pos;\n    \n    Human() : pos(0, 0), initial_pos(0, 0) {}\n    Human(int x, int y) : pos(x, y), initial_pos(x, y) {}\n};\n\nclass Grid {\npublic:\n    bool blocked[GRID_SIZE][GRID_SIZE];\n    bool pet_present[GRID_SIZE][GRID_SIZE];\n    bool human_present[GRID_SIZE][GRID_SIZE];\n    \n    Grid() {\n        memset(blocked, false, sizeof(blocked));\n        memset(pet_present, false, sizeof(pet_present));\n        memset(human_present, false, sizeof(human_present));\n    }\n    \n    void reset_presence() {\n        memset(pet_present, false, sizeof(pet_present));\n        memset(human_present, false, sizeof(human_present));\n    }\n    \n    bool is_valid(int x, int y) {\n        return x >= 0 && x < GRID_SIZE && y >= 0 && y < GRID_SIZE;\n    }\n    \n    bool is_passable(int x, int y) {\n        return is_valid(x, y) && !blocked[x][y];\n    }\n};\n\nclass Solver {\nprivate:\n    int N, M;\n    vector<Pet> pets;\n    vector<Human> humans;\n    Grid grid;\n    mt19937 rng;\n    \npublic:\n    Solver() : rng(chrono::steady_clock::now().time_since_epoch().count()) {}\n    \n    void read_input() {\n        cin >> N;\n        pets.resize(N);\n        for (int i = 0; i < N; i++) {\n            int x, y, t;\n            cin >> x >> y >> t;\n            pets[i] = Pet(x-1, y-1, t); // Convert to 0-indexed\n        }\n        \n        cin >> M;\n        humans.resize(M);\n        for (int i = 0; i < M; i++) {\n            int x, y;\n            cin >> x >> y;\n            humans[i] = Human(x-1, y-1); // Convert to 0-indexed\n        }\n    }\n    \n    bool is_adjacent_to_pet(int x, int y) {\n        for (int d = 0; d < 4; d++) {\n            int nx = x + dx[d];\n            int ny = y + dy[d];\n            if (grid.is_valid(nx, ny)) {\n                for (const auto& pet : pets) {\n                    if (pet.pos.x == nx && pet.pos.y == ny) {\n                        return true;\n                    }\n                }\n            }\n        }\n        return false;\n    }\n    \n    string get_action(int human_id) {\n        Human& human = humans[human_id];\n        string actions = \".udlrUDLR\";\n        \n        // Simple strategy: try to block squares adjacent to pets\n        for (int d = 0; d < 4; d++) {\n            int nx = human.pos.x + dx[d];\n            int ny = human.pos.y + dy[d];\n            \n            if (grid.is_passable(nx, ny)) {\n                // Check if this square is adjacent to any pet\n                bool adjacent_to_pet = false;\n                for (const auto& pet : pets) {\n                    if (abs(pet.pos.x - nx) + abs(pet.pos.y - ny) <= 1) {\n                        adjacent_to_pet = true;\n                        break;\n                    }\n                }\n                \n                if (adjacent_to_pet && !is_adjacent_to_pet(nx, ny)) {\n                    return string(1, dir_chars[d]); // Block this square\n                }\n            }\n        }\n        \n        // If no good blocking move, try to move towards a pet to get closer\n        if (!pets.empty()) {\n            int closest_pet = 0;\n            int min_dist = abs(human.pos.x - pets[0].pos.x) + abs(human.pos.y - pets[0].pos.y);\n            \n            for (int i = 1; i < N; i++) {\n                int dist = abs(human.pos.x - pets[i].pos.x) + abs(human.pos.y - pets[i].pos.y);\n                if (dist < min_dist) {\n                    min_dist = dist;\n                    closest_pet = i;\n                }\n            }\n            \n            Position target = pets[closest_pet].pos;\n            \n            // Try to move closer\n            for (int d = 0; d < 4; d++) {\n                int nx = human.pos.x + dx[d];\n                int ny = human.pos.y + dy[d];\n                \n                if (grid.is_passable(nx, ny)) {\n                    int new_dist = abs(nx - target.x) + abs(ny - target.y);\n                    if (new_dist < min_dist) {\n                        return string(1, move_chars[d]);\n                    }\n                }\n            }\n        }\n        \n        // Default: do nothing\n        return \".\";\n    }\n    \n    void update_positions(const vector<string>& pet_moves) {\n        // Update pet positions based on their moves\n        for (int i = 0; i < N; i++) {\n            const string& moves = pet_moves[i];\n            for (char move : moves) {\n                if (move == 'U' && pets[i].pos.x > 0) pets[i].pos.x--;\n                else if (move == 'D' && pets[i].pos.x < GRID_SIZE - 1) pets[i].pos.x++;\n                else if (move == 'L' && pets[i].pos.y > 0) pets[i].pos.y--;\n                else if (move == 'R' && pets[i].pos.y < GRID_SIZE - 1) pets[i].pos.y++;\n            }\n        }\n        \n        // Update human positions based on our actions (simplified)\n        // In a real implementation, we would track the actual moves\n    }\n    \n    void solve() {\n        read_input();\n        \n        // Initialize grid presence\n        grid.reset_presence();\n        for (const auto& pet : pets) {\n            if (grid.is_valid(pet.pos.x, pet.pos.y)) {\n                grid.pet_present[pet.pos.x][pet.pos.y] = true;\n            }\n        }\n        for (const auto& human : humans) {\n            if (grid.is_valid(human.pos.x, human.pos.y)) {\n                grid.human_present[human.pos.x][human.pos.y] = true;\n            }\n        }\n        \n        for (int turn = 0; turn < MAX_TURNS; turn++) {\n            string actions = \"\";\n            for (int i = 0; i < M; i++) {\n                actions += get_action(i);\n            }\n            cout << actions << endl;\n            cout.flush();\n            \n            // Read pet moves\n            vector<string> pet_moves(N);\n            for (int i = 0; i < N; i++) {\n                cin >> pet_moves[i];\n            }\n            \n            // Update positions\n            update_positions(pet_moves);\n            \n            // Update grid state (simplified)\n            grid.reset_presence();\n            for (const auto& pet : pets) {\n                if (grid.is_valid(pet.pos.x, pet.pos.y)) {\n                    grid.pet_present[pet.pos.x][pet.pos.y] = true;\n                }\n            }\n            for (const auto& human : humans) {\n                if (grid.is_valid(human.pos.x, human.pos.y)) {\n                    grid.human_present[human.pos.x][human.pos.y] = true;\n                }\n            }\n        }\n    }\n};\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    Solver solver;\n    solver.solve();\n    \n    return 0;\n}","ahc009":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int SIZE = 20;\nconst int MAX_LENGTH = 200;\n\nstruct Point {\n    int r, c;\n    Point() : r(0), c(0) {}\n    Point(int _r, int _c) : r(_r), c(_c) {}\n    bool operator==(const Point& other) const {\n        return r == other.r && c == other.c;\n    }\n};\n\nstruct PathInfo {\n    string path;\n    double expected_score;\n    \n    bool operator<(const PathInfo& other) const {\n        return expected_score > other.expected_score; // Max heap\n    }\n};\n\n// Directions: U, D, L, R\nconst int dr[] = {-1, 1, 0, 0};\nconst int dc[] = {0, 0, -1, 1};\nconst char dir_chars[] = {'U', 'D', 'L', 'R'};\n\nint si, sj, ti, tj;\ndouble p;\nbool h_walls[SIZE][SIZE-1]; // horizontal walls\nbool v_walls[SIZE-1][SIZE]; // vertical walls\n\nbool can_move(int r, int c, int dir) {\n    if (dir == 0) { // Up\n        if (r == 0) return false;\n        return !v_walls[r-1][c];\n    } else if (dir == 1) { // Down\n        if (r == SIZE-1) return false;\n        return !v_walls[r][c];\n    } else if (dir == 2) { // Left\n        if (c == 0) return false;\n        return !h_walls[r][c-1];\n    } else { // Right\n        if (c == SIZE-1) return false;\n        return !h_walls[r][c];\n    }\n}\n\n// Simulate the stochastic process to estimate expected score\ndouble simulate_path(const string& path, int simulations = 10000) {\n    double total_score = 0.0;\n    Point target(ti, tj);\n    \n    for (int sim = 0; sim < simulations; sim++) {\n        Point pos(si, sj);\n        int time = 0;\n        bool reached = false;\n        \n        for (char move : path) {\n            time++;\n            \n            // With probability p, forget the move (stay in place)\n            if (((double)rand() / RAND_MAX) < p) {\n                // Stay in place\n            } else {\n                // Execute the move if possible\n                int dir;\n                if (move == 'U') dir = 0;\n                else if (move == 'D') dir = 1;\n                else if (move == 'L') dir = 2;\n                else dir = 3; // 'R'\n                \n                if (can_move(pos.r, pos.c, dir)) {\n                    pos.r += dr[dir];\n                    pos.c += dc[dir];\n                }\n            }\n            \n            // Check if reached target\n            if (pos == target) {\n                total_score += (401 - time);\n                reached = true;\n                break;\n            }\n        }\n        \n        if (!reached) {\n            // Failed to reach target\n            total_score += 0;\n        }\n    }\n    \n    return total_score / simulations;\n}\n\n// Generate a simple path using BFS\nstring find_simple_path() {\n    queue<pair<Point, string>> q;\n    bool visited[SIZE][SIZE] = {false};\n    \n    Point start(si, sj);\n    Point target(ti, tj);\n    \n    q.push({start, \"\"});\n    visited[start.r][start.c] = true;\n    \n    while (!q.empty()) {\n        auto [pos, path] = q.front();\n        q.pop();\n        \n        if (pos == target) {\n            return path;\n        }\n        \n        if (path.length() > MAX_LENGTH) continue;\n        \n        // Try all 4 directions\n        for (int dir = 0; dir < 4; dir++) {\n            int nr = pos.r + dr[dir];\n            int nc = pos.c + dc[dir];\n            \n            if (nr >= 0 && nr < SIZE && nc >= 0 && nc < SIZE && !visited[nr][nc]) {\n                if (can_move(pos.r, pos.c, dir)) {\n                    visited[nr][nc] = true;\n                    string new_path = path + dir_chars[dir];\n                    if (new_path.length() <= MAX_LENGTH) {\n                        q.push({Point(nr, nc), new_path});\n                    }\n                }\n            }\n        }\n    }\n    \n    return \"\"; // Should not happen according to problem statement\n}\n\n// Generate a more robust path by repeating movements\nstring generate_robust_path() {\n    string base_path = find_simple_path();\n    \n    // If base path is very short, just return it\n    if (base_path.length() <= 50) {\n        return base_path;\n    }\n    \n    // Create a more robust version by adding some redundancy\n    string robust_path = \"\";\n    int insert_count = 0;\n    \n    for (char c : base_path) {\n        robust_path += c;\n        // Occasionally duplicate moves for robustness\n        if (insert_count < 30 && (rand() % 3) == 0) {\n            robust_path += c;\n            insert_count++;\n        }\n        if (robust_path.length() >= MAX_LENGTH) break;\n    }\n    \n    // If still have room, add some random moves that should cancel out\n    while (robust_path.length() < MAX_LENGTH - 4) {\n        // Add a LR or UD pair that should mostly cancel out\n        if (rand() % 2) {\n            robust_path += \"LR\";\n        } else {\n            robust_path += \"UD\";\n        }\n        if (robust_path.length() >= MAX_LENGTH - 2) break;\n    }\n    \n    // Ensure we don't exceed limit\n    if (robust_path.length() > MAX_LENGTH) {\n        robust_path = robust_path.substr(0, MAX_LENGTH);\n    }\n    \n    return robust_path;\n}\n\n// Generate multiple paths and pick the best one\nstring find_best_path() {\n    vector<PathInfo> candidates;\n    \n    // Generate several candidate paths\n    for (int i = 0; i < 5; i++) {\n        string path;\n        if (i == 0) {\n            path = find_simple_path();\n        } else {\n            path = generate_robust_path();\n        }\n        \n        if (path.length() <= MAX_LENGTH) {\n            double score = simulate_path(path, 2000); // Reduced simulations for speed\n            candidates.push_back({path, score});\n        }\n    }\n    \n    // Sort by expected score and return the best\n    sort(candidates.begin(), candidates.end());\n    \n    if (!candidates.empty()) {\n        return candidates[0].path;\n    }\n    \n    // Fallback to simple path\n    return find_simple_path();\n}\n\nint main() {\n    srand(time(nullptr));\n    \n    // Read input\n    cin >> si >> sj >> ti >> tj >> p;\n    \n    // Read horizontal walls\n    for (int i = 0; i < SIZE; i++) {\n        string line;\n        cin >> line;\n        for (int j = 0; j < SIZE-1; j++) {\n            h_walls[i][j] = (line[j] == '1');\n        }\n    }\n    \n    // Read vertical walls\n    for (int i = 0; i < SIZE-1; i++) {\n        string line;\n        cin >> line;\n        for (int j = 0; j < SIZE; j++) {\n            v_walls[i][j] = (line[j] == '1');\n        }\n    }\n    \n    // Find and output the best path\n    string result = find_best_path();\n    \n    // Ensure we don't exceed the limit\n    if (result.length() > MAX_LENGTH) {\n        result = result.substr(0, MAX_LENGTH);\n    }\n    \n    cout << result << endl;\n    \n    return 0;\n}","ahc010":"#include <bits/stdc++.h>\nusing namespace std;\n\n// Direction: 0=left, 1=up, 2=right, 3=down\nconst int di[4] = {0, -1, 0, 1};\nconst int dj[4] = {-1, 0, 1, 0};\n\n// to[tile_type][direction_entering] = direction_exiting (-1 if invalid)\nconst int to[8][4] = {\n    {1, 0, -1, -1}, // tile 0\n    {3, -1, -1, 0}, // tile 1\n    {-1, -1, 3, 2}, // tile 2\n    {-1, 2, 1, -1}, // tile 3\n    {1, 0, 3, 2},   // tile 4\n    {3, 2, 1, 0},   // tile 5\n    {2, -1, 0, -1}, // tile 6\n    {-1, 3, -1, 1}  // tile 7\n};\n\nconst int N = 30;\nint original_grid[N][N];\nint rotations[N][N];\n\n// Random number generator\nmt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n\n// Get effective tile type after rotation\nint getEffectiveTile(int baseTile, int rotation) {\n    // For tiles 0-3 (single curve): 0->1->2->3->0\n    // For tiles 4-5 (double curve): 4->5->4->5->4 (alternating)\n    // For tiles 6-7 (straight): 6->7->6->7->6 (alternating)\n    \n    if (baseTile >= 0 && baseTile <= 3) {\n        return (baseTile + rotation) % 4;\n    } else if (baseTile == 4 || baseTile == 5) {\n        if (rotation % 2 == 0) return baseTile;\n        else return (baseTile == 4) ? 5 : 4;\n    } else { // baseTile == 6 or 7\n        if (rotation % 2 == 0) return baseTile;\n        else return (baseTile == 6) ? 7 : 6;\n    }\n}\n\n// Trace a loop starting from position (i,j) entering from direction d\nint traceLoop(vector<vector<int>>& effectiveGrid, int start_i, int start_j, int start_d) {\n    int i = start_i, j = start_j, d = start_d;\n    int length = 0;\n    \n    // Check if we can enter this tile from this direction\n    int d2 = to[effectiveGrid[i][j]][d];\n    if (d2 == -1) return 0;\n    \n    do {\n        d2 = to[effectiveGrid[i][j]][d];\n        if (d2 == -1) return 0;\n        \n        // Move to next tile\n        i += di[d2];\n        j += dj[d2];\n        \n        // Check bounds\n        if (i < 0 || i >= N || j < 0 || j >= N) return 0;\n        \n        // Update direction (opposite of exit direction)\n        d = (d2 + 2) % 4;\n        length++;\n        \n        // Check if we've returned to start\n        if (i == start_i && j == start_j && d == start_d) break;\n        \n        // Safety check to avoid infinite loops\n        if (length > 1000) return 0;\n        \n    } while (true);\n    \n    return length;\n}\n\n// Find all loops and return their lengths\nvector<int> findLoops(vector<vector<int>>& effectiveGrid) {\n    vector<int> loops;\n    bool visited[N][N][4] = {false};\n    \n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            for (int d = 0; d < 4; d++) {\n                if (!visited[i][j][d]) {\n                    int length = traceLoop(effectiveGrid, i, j, d);\n                    if (length > 0) {\n                        // Mark all positions in this loop as visited\n                        int ci = i, cj = j, cd = d;\n                        do {\n                            visited[ci][cj][cd] = true;\n                            int d2 = to[effectiveGrid[ci][cj]][cd];\n                            if (d2 == -1) break;\n                            ci += di[d2];\n                            cj += dj[d2];\n                            if (ci < 0 || ci >= N || cj < 0 || cj >= N) break;\n                            cd = (d2 + 2) % 4;\n                        } while (!(ci == i && cj == j && cd == d) && ci >= 0 && ci < N && cj >= 0 && cj < N);\n                        \n                        loops.push_back(length);\n                    }\n                }\n            }\n        }\n    }\n    \n    sort(loops.begin(), loops.end(), greater<int>());\n    return loops;\n}\n\n// Calculate score for current rotation configuration\nlong long calculateScore() {\n    // Create effective grid with rotations applied\n    vector<vector<int>> effectiveGrid(N, vector<int>(N));\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            effectiveGrid[i][j] = getEffectiveTile(original_grid[i][j], rotations[i][j]);\n        }\n    }\n    \n    vector<int> loops = findLoops(effectiveGrid);\n    \n    if (loops.size() < 2) return 0;\n    \n    return (long long)loops[0] * loops[1];\n}\n\nint main() {\n    // Read input\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cin >> original_grid[i][j];\n            rotations[i][j] = 0;\n        }\n    }\n    \n    // Simple greedy approach - try random rotations\n    long long bestScore = calculateScore();\n    \n    // Try some random improvements\n    for (int iter = 0; iter < 5000; iter++) {\n        // Make a random change\n        int i = rng() % N;\n        int j = rng() % N;\n        int oldRot = rotations[i][j];\n        rotations[i][j] = rng() % 4;\n        \n        long long score = calculateScore();\n        if (score > bestScore) {\n            bestScore = score;\n        } else {\n            rotations[i][j] = oldRot; // Revert\n        }\n    }\n    \n    // Output result\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cout << rotations[i][j];\n        }\n    }\n    cout << endl;\n    \n    return 0;\n}","ahc011":"#include <bits/stdc++.h>\nusing namespace std;\n\n// Direction constants\nconst int UP = 0, DOWN = 1, LEFT = 2, RIGHT = 3;\nconst int dr[4] = {-1, 1, 0, 0};\nconst int dc[4] = {0, 0, -1, 1};\nconst char move_chars[4] = {'U', 'D', 'L', 'R'};\n\nstruct State {\n    vector<vector<int>> board;\n    int empty_r, empty_c;\n    string moves;\n    \n    State(vector<vector<int>> b, int er, int ec, string m = \"\") \n        : board(b), empty_r(er), empty_c(ec), moves(m) {}\n};\n\n// Check if two adjacent tiles can connect\nbool canConnect(int tile1, int tile2, int direction) {\n    // direction: 0=up, 1=down, 2=left, 3=right\n    // tile1 is at (r,c), tile2 is at (r+dr[direction], c+dc[direction])\n    if (direction == 0) { // up\n        return (tile1 & 2) && (tile2 & 8);\n    } else if (direction == 1) { // down\n        return (tile1 & 8) && (tile2 & 2);\n    } else if (direction == 2) { // left\n        return (tile1 & 1) && (tile2 & 4);\n    } else { // right\n        return (tile1 & 4) && (tile2 & 1);\n    }\n}\n\n// Find connected components in the current board\nvector<vector<bool>> findComponents(const vector<vector<int>>& board, int empty_r, int empty_c) {\n    int N = board.size();\n    vector<vector<bool>> visited(N, vector<bool>(N, false));\n    vector<vector<bool>> is_tile(N, vector<bool>(N, true));\n    is_tile[empty_r][empty_c] = false;\n    \n    // BFS to find connected components\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (!visited[i][j] && is_tile[i][j]) {\n                queue<pair<int,int>> q;\n                q.push({i, j});\n                visited[i][j] = true;\n                \n                while (!q.empty()) {\n                    auto [r, c] = q.front(); q.pop();\n                    \n                    // Check 4 directions\n                    for (int d = 0; d < 4; d++) {\n                        int nr = r + dr[d];\n                        int nc = c + dc[d];\n                        \n                        if (nr >= 0 && nr < N && nc >= 0 && nc < N && \n                            is_tile[nr][nc] && !visited[nr][nc]) {\n                            if (canConnect(board[r][c], board[nr][nc], d)) {\n                                visited[nr][nc] = true;\n                                q.push({nr, nc});\n                            }\n                        }\n                    }\n                }\n            }\n        }\n    }\n    \n    return visited;\n}\n\n// Count size of largest connected component\nint largestComponentSize(const vector<vector<int>>& board, int empty_r, int empty_c) {\n    auto components = findComponents(board, empty_r, empty_c);\n    int max_size = 0;\n    int current_size = 0;\n    \n    for (const auto& row : components) {\n        for (bool cell : row) {\n            if (cell) current_size++;\n        }\n    }\n    \n    return current_size;\n}\n\n// Heuristic function - how good is this state?\nint evaluate(const vector<vector<int>>& board, int empty_r, int empty_c) {\n    return largestComponentSize(board, empty_r, empty_c);\n}\n\n// Try to solve by building a path to maximize tree size\nstring solve(vector<vector<int>> initial_board, int N, int T) {\n    // Find initial empty position\n    int empty_r = 0, empty_c = 0;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (initial_board[i][j] == 0) {\n                empty_r = i;\n                empty_c = j;\n                break;\n            }\n        }\n    }\n    \n    // Simple greedy approach - try to improve connectivity\n    string best_moves = \"\";\n    vector<vector<int>> current_board = initial_board;\n    int current_empty_r = empty_r;\n    int current_empty_c = empty_c;\n    \n    // Try random moves to improve connectivity\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n    uniform_int_distribution<int> dir_dist(0, 3);\n    \n    int best_score = evaluate(current_board, current_empty_r, current_empty_c);\n    \n    for (int step = 0; step < min(T, 5000); step++) {\n        // Try a few random moves\n        vector<pair<int, string>> candidates;\n        \n        for (int attempt = 0; attempt < 20; attempt++) {\n            int dir = dir_dist(rng);\n            int nr = current_empty_r + dr[dir];\n            int nc = current_empty_c + dc[dir];\n            \n            if (nr >= 0 && nr < N && nc >= 0 && nc < N) {\n                // Make the move\n                vector<vector<int>> new_board = current_board;\n                swap(new_board[current_empty_r][current_empty_c], new_board[nr][nc]);\n                \n                string new_moves = best_moves + move_chars[dir];\n                int score = evaluate(new_board, nr, nc);\n                \n                candidates.push_back({score, new_moves});\n            }\n        }\n        \n        if (!candidates.empty()) {\n            sort(candidates.rbegin(), candidates.rend());\n            if (candidates[0].first > best_score) {\n                best_score = candidates[0].first;\n                best_moves = candidates[0].second;\n                // Update current state\n                int last_move = best_moves.back() - 'A';\n                int dir = -1;\n                if (last_move == 'U' - 'A') dir = 0;\n                else if (last_move == 'D' - 'A') dir = 1;\n                else if (last_move == 'L' - 'A') dir = 2;\n                else if (last_move == 'R' - 'A') dir = 3;\n                \n                int nr = current_empty_r + dr[dir];\n                int nc = current_empty_c + dc[dir];\n                swap(current_board[current_empty_r][current_empty_c], current_board[nr][nc]);\n                current_empty_r = nr;\n                current_empty_c = nc;\n            }\n        }\n    }\n    \n    // If we still have moves, try to improve with local search\n    if (best_moves.length() < T) {\n        // Try to optimize the current position\n        for (int extra = best_moves.length(); extra < T && extra < best_moves.length() + 1000; extra++) {\n            int dir = dir_dist(rng);\n            int nr = current_empty_r + dr[dir];\n            int nc = current_empty_c + dc[dir];\n            \n            if (nr >= 0 && nr < N && nc >= 0 && nc < N) {\n                best_moves += move_chars[dir];\n                swap(current_board[current_empty_r][current_empty_c], current_board[nr][nc]);\n                current_empty_r = nr;\n                current_empty_c = nc;\n            }\n        }\n    }\n    \n    return best_moves.substr(0, T);\n}\n\nint main() {\n    int N, T;\n    cin >> N >> T;\n    \n    vector<vector<int>> board(N, vector<int>(N));\n    for (int i = 0; i < N; i++) {\n        string line;\n        cin >> line;\n        for (int j = 0; j < N; j++) {\n            char c = line[j];\n            if (c >= '0' && c <= '9') {\n                board[i][j] = c - '0';\n            } else {\n                board[i][j] = c - 'a' + 10;\n            }\n        }\n    }\n    \n    string result = solve(board, N, T);\n    cout << result << endl;\n    \n    return 0;\n}","ahc012":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst double PI = acos(-1);\nconst long long BOUND = 1000000000LL;\n\nstruct Strawberry {\n    long long x, y;\n    int id;\n    double angle;\n    \n    bool operator<(const Strawberry& other) const {\n        return angle < other.angle;\n    }\n};\n\ndouble calculate_angle(long long x, long long y) {\n    double angle = atan2((double)y, (double)x);\n    if (angle < 0) angle += 2 * PI;\n    return angle;\n}\n\npair<pair<long long, long long>, pair<long long, long long>> create_radial_line(double angle) {\n    // Create a line that goes through the origin in the given direction\n    // But we need two points, so we'll go far out\n    long long dist = BOUND / 2;\n    \n    double x1 = cos(angle) * dist;\n    double y1 = sin(angle) * dist;\n    double x2 = cos(angle + PI) * dist;\n    double y2 = sin(angle + PI) * dist;\n    \n    long long px1 = (long long)round(x1);\n    long long py1 = (long long)round(y1);\n    long long px2 = (long long)round(x2);\n    long long py2 = (long long)round(y2);\n    \n    // Ensure bounds\n    px1 = max(-BOUND, min(BOUND, px1));\n    py1 = max(-BOUND, min(BOUND, py1));\n    px2 = max(-BOUND, min(BOUND, px2));\n    py2 = max(-BOUND, min(BOUND, py2));\n    \n    return {{px1, py1}, {px2, py2}};\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    int N, K;\n    cin >> N >> K;\n    \n    vector<int> a(11); // a[1] to a[10]\n    int total_attendees = 0;\n    for (int i = 1; i <= 10; i++) {\n        cin >> a[i];\n        total_attendees += a[i];\n    }\n    \n    vector<Strawberry> strawberries(N);\n    for (int i = 0; i < N; i++) {\n        cin >> strawberries[i].x >> strawberries[i].y;\n        strawberries[i].id = i;\n        strawberries[i].angle = calculate_angle(strawberries[i].x, strawberries[i].y);\n    }\n    \n    // Sort strawberries by angle\n    sort(strawberries.begin(), strawberries.end());\n    \n    vector<pair<pair<long long, long long>, pair<long long, long long>>> lines;\n    \n    // Strategy: Create sectors to try to isolate strawberries\n    // We'll make cuts between groups of strawberries\n    \n    if (N > 0 && K > 0) {\n        // Make radial cuts to separate strawberries\n        int cuts_to_make = min(K, max(1, N/2)); // Don't make too many cuts\n        \n        if (cuts_to_make > 0) {\n            // Evenly space the cuts\n            for (int i = 0; i < cuts_to_make; i++) {\n                double angle = 2.0 * PI * (i + 0.5) / cuts_to_make;\n                lines.push_back(create_radial_line(angle));\n            }\n        }\n    }\n    \n    // Output result\n    cout << lines.size() << \"\\n\";\n    for (const auto& line : lines) {\n        cout << line.first.first << \" \" << line.first.second << \" \" \n             << line.second.first << \" \" << line.second.second << \"\\n\";\n    }\n    \n    return 0;\n}","ahc014":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MAX_N = 65;\n\nint N, M;\nbool grid[MAX_N][MAX_N];\nvector<pair<int, int>> initial_dots;\nvector<tuple<int, int, int, int, int, int, int, int>> operations;\n\n// Center of the grid\nint center;\n\n// Calculate weight of a position\ninline int weight(int x, int y) {\n    int dx = x - center;\n    int dy = y - center;\n    return dx * dx + dy * dy + 1;\n}\n\n// Check if position is valid (within bounds)\ninline bool valid_pos(int x, int y) {\n    return x >= 0 && x < N && y >= 0 && y < N;\n}\n\n// Check if a rectangle is axis-aligned\nbool is_axis_aligned(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) {\n    return (x1 == x2 && x3 == x4 && y1 == y4 && y2 == y3) ||\n           (x1 == x4 && x2 == x3 && y1 == y2 && y3 == y4);\n}\n\n// Check if a rectangle is 45-degree rotated\nbool is_45_rotated(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) {\n    int dx12 = abs(x1 - x2);\n    int dy12 = abs(y1 - y2);\n    int dx23 = abs(x2 - x3);\n    int dy23 = abs(y2 - y3);\n    int dx34 = abs(x3 - x4);\n    int dy34 = abs(y3 - y4);\n    int dx41 = abs(x4 - x1);\n    int dy41 = abs(y4 - y1);\n    \n    return (dx12 == dy12 && dx23 == dy23 && dx34 == dy34 && dx41 == dy41) &&\n           (dx12 == dx23 && dx23 == dx34 && dx34 == dx41);\n}\n\n// Check if four points form a valid rectangle (axis-aligned or 45\u00b0 rotated)\nbool is_valid_rectangle(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) {\n    // Check if it's a valid rectangle\n    if (!valid_pos(x1, y1) || !valid_pos(x2, y2) || !valid_pos(x3, y3) || !valid_pos(x4, y4))\n        return false;\n        \n    // Check if it's axis-aligned or 45-degree rotated\n    return is_axis_aligned(x1, y1, x2, y2, x3, y3, x4, y4) || \n           is_45_rotated(x1, y1, x2, y2, x3, y3, x4, y4);\n}\n\n// Check if there are no dots on the perimeter except the three given points\nbool no_dots_on_perimeter(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) {\n    // This is a simplified check - we'd need to implement proper perimeter checking\n    // For now, let's just check the four corners\n    int corners[4][2] = {{x1, y1}, {x2, y2}, {x3, y3}, {x4, y4}};\n    \n    for (int i = 0; i < 4; i++) {\n        int x = corners[i][0];\n        int y = corners[i][1];\n        // Skip the three points that should have dots\n        if ((x == x2 && y == y2) || (x == x3 && y == y3) || (x == x4 && y == y4))\n            continue;\n        // If it's the new point, it should not have a dot\n        if (x == x1 && y == y1) {\n            if (grid[x][y]) return false;\n        }\n        // For other perimeter points, they should not have dots\n        // This is simplified - we need a better perimeter check\n    }\n    return true;\n}\n\n// Find valid rectangles for a given position\nvector<tuple<int, int, int, int, int, int>> find_valid_rectangles(int new_x, int new_y) {\n    vector<tuple<int, int, int, int, int, int>> result;\n    \n    // Look for axis-aligned rectangles\n    for (int dx = 1; dx < N; dx++) {\n        for (int dy = 1; dy < N; dy++) {\n            // Try different orientations\n            vector<pair<int, int>> corners = {\n                {new_x, new_y},\n                {new_x + dx, new_y},\n                {new_x + dx, new_y + dy},\n                {new_x, new_y + dy}\n            };\n            \n            // Check all 4 rotations\n            for (int rot = 0; rot < 4; rot++) {\n                int x1 = corners[0].first, y1 = corners[0].second;\n                int x2 = corners[1].first, y2 = corners[1].second;\n                int x3 = corners[2].first, y3 = corners[2].second;\n                int x4 = corners[3].first, y4 = corners[3].second;\n                \n                if (valid_pos(x1, y1) && valid_pos(x2, y2) && valid_pos(x3, y3) && valid_pos(x4, y4)) {\n                    // Check if the three points (excluding new position) have dots\n                    if (grid[x2][y2] && grid[x3][y3] && grid[x4][y4]) {\n                        // Simple check for now - need better perimeter checking\n                        result.push_back(make_tuple(x1, y1, x2, y2, x3, y3, x4, y4));\n                    }\n                }\n                \n                // Rotate corners\n                auto temp = corners[0];\n                for (int i = 0; i < 3; i++) {\n                    corners[i] = corners[i+1];\n                }\n                corners[3] = temp;\n            }\n        }\n    }\n    \n    // Look for 45-degree rotated rectangles\n    for (int d = 1; d < N; d++) {\n        // Diamond shape with side length d\n        vector<pair<int, int>> corners = {\n            {new_x, new_y + d},\n            {new_x + d, new_y},\n            {new_x, new_y - d},\n            {new_x - d, new_y}\n        };\n        \n        if (valid_pos(corners[0].first, corners[0].second) &&\n            valid_pos(corners[1].first, corners[1].second) &&\n            valid_pos(corners[2].first, corners[2].second) &&\n            valid_pos(corners[3].first, corners[3].second)) {\n            \n            if (grid[corners[1].first][corners[1].second] &&\n                grid[corners[2].first][corners[2].second] &&\n                grid[corners[3].first][corners[3].second]) {\n                result.push_back(make_tuple(\n                    corners[0].first, corners[0].second,\n                    corners[1].first, corners[1].second,\n                    corners[2].first, corners[2].second,\n                    corners[3].first, corners[3].second\n                ));\n            }\n        }\n    }\n    \n    return result;\n}\n\n// Greedy approach: try to place dots with highest weight first\nvoid solve() {\n    // Priority queue to process positions by weight (descending)\n    priority_queue<pair<int, pair<int, int>>> pq;\n    \n    // Add all valid positions to priority queue\n    for (int x = 0; x < N; x++) {\n        for (int y = 0; y < N; y++) {\n            if (!grid[x][y]) { // Position doesn't have a dot yet\n                pq.push({weight(x, y), {x, y}});\n            }\n        }\n    }\n    \n    // Process positions\n    int attempts = 0;\n    const int MAX_ATTEMPTS = 10000; // Limit attempts for performance\n    \n    while (!pq.empty() && attempts < MAX_ATTEMPTS) {\n        auto current = pq.top();\n        pq.pop();\n        \n        int x = current.second.first;\n        int y = current.second.second;\n        \n        // If this position already has a dot, skip\n        if (grid[x][y]) continue;\n        \n        // Find valid rectangles for this position\n        auto rectangles = find_valid_rectangles(x, y);\n        \n        if (!rectangles.empty()) {\n            // Use the first valid rectangle (could be improved with better selection)\n            auto rect = rectangles[0];\n            int x1, y1, x2, y2, x3, y3, x4, y4;\n            tie(x1, y1, x2, y2, x3, y3, x4, y4) = rect;\n            \n            // Place the dot\n            grid[x1][y1] = true;\n            operations.push_back(make_tuple(x1, y1, x2, y2, x3, y3, x4, y4));\n            \n            attempts = 0; // Reset attempts since we made progress\n        }\n        \n        attempts++;\n    }\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    cin >> N >> M;\n    center = (N - 1) / 2;\n    \n    // Initialize grid\n    memset(grid, false, sizeof(grid));\n    \n    // Read initial dots\n    for (int i = 0; i < M; i++) {\n        int x, y;\n        cin >> x >> y;\n        grid[x][y] = true;\n        initial_dots.push_back({x, y});\n    }\n    \n    // Solve\n    solve();\n    \n    // Output results\n    cout << operations.size() << \"\\n\";\n    for (const auto& op : operations) {\n        cout << get<0>(op) << \" \" << get<1>(op) << \" \" \n             << get<2>(op) << \" \" << get<3>(op) << \" \"\n             << get<4>(op) << \" \" << get<5>(op) << \" \"\n             << get<6>(op) << \" \" << get<7>(op) << \"\\n\";\n    }\n    \n    return 0;\n}","ahc015":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int GRID_SIZE = 10;\nconst int TOTAL_CANDIES = 100;\n\n// Directions: F(forward/up), B(backward/down), L(left), R(right)\nconst char DIR_CHARS[] = {'F', 'B', 'L', 'R'};\nconst int DX[] = {-1, 1, 0, 0}; // F, B, L, R\nconst int DY[] = {0, 0, -1, 1};\n\nstruct Position {\n    int x, y;\n    Position() : x(0), y(0) {}\n    Position(int _x, int _y) : x(_x), y(_y) {}\n};\n\nclass CandyBox {\npublic:\n    int grid[GRID_SIZE][GRID_SIZE]; // 0 = empty, 1,2,3 = flavors\n    vector<int> flavor_sequence;\n    int candy_count[4]; // index 0 unused, 1-3 for flavors\n    \n    CandyBox() {\n        memset(grid, 0, sizeof(grid));\n        memset(candy_count, 0, sizeof(candy_count));\n    }\n    \n    void read_flavors() {\n        flavor_sequence.resize(TOTAL_CANDIES);\n        for (int i = 0; i < TOTAL_CANDIES; i++) {\n            cin >> flavor_sequence[i];\n        }\n    }\n    \n    // Convert empty cell index to position\n    Position index_to_position(int index) {\n        int count = 0;\n        for (int i = 0; i < GRID_SIZE; i++) {\n            for (int j = 0; j < GRID_SIZE; j++) {\n                if (grid[i][j] == 0) {\n                    count++;\n                    if (count == index) {\n                        return Position(i, j);\n                    }\n                }\n            }\n        }\n        return Position(-1, -1); // Error\n    }\n    \n    // Place candy at specified position\n    void place_candy(int x, int y, int flavor) {\n        grid[x][y] = flavor;\n        candy_count[flavor]++;\n    }\n    \n    // Simulate sliding in given direction\n    void slide_candies(int dir) {\n        if (dir == 0) { // Forward (up) - move rows up\n            for (int col = 0; col < GRID_SIZE; col++) {\n                int write_pos = 0;\n                for (int row = 0; row < GRID_SIZE; row++) {\n                    if (grid[row][col] != 0) {\n                        if (write_pos != row) {\n                            grid[write_pos][col] = grid[row][col];\n                            grid[row][col] = 0;\n                        }\n                        write_pos++;\n                    }\n                }\n            }\n        } else if (dir == 1) { // Backward (down) - move rows down\n            for (int col = 0; col < GRID_SIZE; col++) {\n                int write_pos = GRID_SIZE - 1;\n                for (int row = GRID_SIZE - 1; row >= 0; row--) {\n                    if (grid[row][col] != 0) {\n                        if (write_pos != row) {\n                            grid[write_pos][col] = grid[row][col];\n                            grid[row][col] = 0;\n                        }\n                        write_pos--;\n                    }\n                }\n            }\n        } else if (dir == 2) { // Left - move columns left\n            for (int row = 0; row < GRID_SIZE; row++) {\n                int write_pos = 0;\n                for (int col = 0; col < GRID_SIZE; col++) {\n                    if (grid[row][col] != 0) {\n                        if (write_pos != col) {\n                            grid[row][write_pos] = grid[row][col];\n                            grid[row][col] = 0;\n                        }\n                        write_pos++;\n                    }\n                }\n            }\n        } else if (dir == 3) { // Right - move columns right\n            for (int row = 0; row < GRID_SIZE; row++) {\n                int write_pos = GRID_SIZE - 1;\n                for (int col = GRID_SIZE - 1; col >= 0; col--) {\n                    if (grid[row][col] != 0) {\n                        if (write_pos != col) {\n                            grid[row][write_pos] = grid[row][col];\n                            grid[row][col] = 0;\n                        }\n                        write_pos--;\n                    }\n                }\n            }\n        }\n    }\n    \n    // Create a copy of current state\n    CandyBox copy() const {\n        CandyBox other = *this;\n        return other;\n    }\n    \n    // Calculate connected components score\n    int calculate_connectedness() {\n        bool visited[GRID_SIZE][GRID_SIZE];\n        memset(visited, false, sizeof(visited));\n        \n        int total_score = 0;\n        int dx[] = {0, 0, -1, 1};\n        int dy[] = {-1, 1, 0, 0};\n        \n        for (int i = 0; i < GRID_SIZE; i++) {\n            for (int j = 0; j < GRID_SIZE; j++) {\n                if (grid[i][j] != 0 && !visited[i][j]) {\n                    // BFS to find connected component\n                    queue<Position> q;\n                    vector<Position> component;\n                    q.push(Position(i, j));\n                    visited[i][j] = true;\n                    component.push_back(Position(i, j));\n                    \n                    int flavor = grid[i][j];\n                    \n                    while (!q.empty()) {\n                        Position current = q.front();\n                        q.pop();\n                        \n                        for (int d = 0; d < 4; d++) {\n                            int nx = current.x + dx[d];\n                            int ny = current.y + dy[d];\n                            \n                            if (nx >= 0 && nx < GRID_SIZE && ny >= 0 && ny < GRID_SIZE) {\n                                if (!visited[nx][ny] && grid[nx][ny] == flavor) {\n                                    visited[nx][ny] = true;\n                                    q.push(Position(nx, ny));\n                                    component.push_back(Position(nx, ny));\n                                }\n                            }\n                        }\n                    }\n                    \n                    // Add square of component size to score\n                    total_score += component.size() * component.size();\n                }\n            }\n        }\n        \n        return total_score;\n    }\n    \n    // Evaluate a move by simulating it\n    int evaluate_move(int dir) {\n        // Create copy and simulate\n        CandyBox temp = this->copy();\n        temp.slide_candies(dir);\n        return temp.calculate_connectedness();\n    }\n    \n    // Debug print\n    void print_grid() {\n        for (int i = 0; i < GRID_SIZE; i++) {\n            for (int j = 0; j < GRID_SIZE; j++) {\n                cerr << grid[i][j] << \" \";\n            }\n            cerr << endl;\n        }\n        cerr << endl;\n    }\n};\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    CandyBox box;\n    box.read_flavors();\n    \n    for (int turn = 0; turn < TOTAL_CANDIES; turn++) {\n        int p;\n        cin >> p;\n        \n        // Place the candy\n        Position pos = box.index_to_position(p);\n        box.place_candy(pos.x, pos.y, box.flavor_sequence[turn]);\n        \n        // Don't need to output direction for last candy\n        if (turn == TOTAL_CANDIES - 1) {\n            cout << \"F\" << endl;\n            cout.flush();\n            break;\n        }\n        \n        // Evaluate all four directions and choose best\n        int best_score = -1;\n        int best_direction = 0;\n        \n        for (int dir = 0; dir < 4; dir++) {\n            int score = box.evaluate_move(dir);\n            if (score > best_score) {\n                best_score = score;\n                best_direction = dir;\n            }\n        }\n        \n        // Output the chosen direction\n        cout << DIR_CHARS[best_direction] << endl;\n        cout.flush();\n        \n        // Apply the chosen move\n        box.slide_candies(best_direction);\n    }\n    \n    return 0;\n}","ahc016":"#include <bits/stdc++.h>\n#include <atcoder/all>\nusing namespace std;\nusing namespace atcoder;\n\nmt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n\nconst int MAX_N = 100;\nconst int MAX_M = 100;\nconst int QUERY_COUNT = 100;\n\nint M;\ndouble epsilon;\nint N;\nvector<string> graphs;\n\n// Convert adjacency matrix to edge string\nstring matrixToEdgeString(const vector<vector<int>>& matrix) {\n    string result;\n    for (int i = 0; i < N; i++) {\n        for (int j = i + 1; j < N; j++) {\n            result += (matrix[i][j] ? '1' : '0');\n        }\n    }\n    return result;\n}\n\n// Convert edge string to adjacency matrix\nvector<vector<int>> edgeStringToMatrix(const string& edge_str) {\n    vector<vector<int>> matrix(N, vector<int>(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 (edge_str[idx] == '1') {\n                matrix[i][j] = 1;\n                matrix[j][i] = 1;\n            }\n            idx++;\n        }\n    }\n    return matrix;\n}\n\n// Generate diverse graphs\nvoid generateGraphs() {\n    // Choose N based on M and epsilon for optimal trade-off\n    if (M <= 20) N = 16;\n    else if (M <= 40) N = 20;\n    else if (M <= 60) N = 24;\n    else N = 28;\n    \n    graphs.clear();\n    \n    // Generate graphs with different structures\n    for (int k = 0; k < M; k++) {\n        vector<vector<int>> matrix(N, vector<int>(N, 0));\n        \n        // Create graphs with different edge densities and structures\n        // This creates graphs with controlled number of edges\n        int target_edges = max(1, min(N * (N - 1) / 2, \n                                     (k * N * (N - 1)) / (2 * max(1, M - 1))));\n        \n        // Add a base structure - complete graphs with different densities\n        int edges_added = 0;\n        for (int i = 0; i < N && edges_added < target_edges; i++) {\n            for (int j = i + 1; j < N && edges_added < target_edges; j++) {\n                // Add some randomness to structure\n                if ((i + j + k) % (max(1, N/4)) != 0 || edges_added < target_edges * 0.7) {\n                    matrix[i][j] = 1;\n                    matrix[j][i] = 1;\n                    edges_added++;\n                }\n            }\n        }\n        \n        // Ensure we have enough edges\n        while (edges_added < target_edges) {\n            int i = uniform_int_distribution<int>(0, N-2)(rng);\n            int j = uniform_int_distribution<int>(i+1, N-1)(rng);\n            if (matrix[i][j] == 0) {\n                matrix[i][j] = 1;\n                matrix[j][i] = 1;\n                edges_added++;\n            }\n        }\n        \n        graphs.push_back(matrixToEdgeString(matrix));\n    }\n}\n\n// Calculate similarity between two graphs considering vertex permutations\ndouble calculateSimilarity(const string& original, const string& received) {\n    auto orig_matrix = edgeStringToMatrix(original);\n    auto recv_matrix = edgeStringToMatrix(received);\n    \n    // Try several random permutations to find best match\n    double best_score = -1e9;\n    \n    // Identity permutation\n    double identity_score = 0;\n    int total_edges = N * (N - 1) / 2;\n    for (int i = 0; i < total_edges; i++) {\n        if (original[i] == received[i]) {\n            identity_score += 1 - epsilon;\n        } else {\n            identity_score += epsilon;\n        }\n    }\n    best_score = identity_score;\n    \n    // Try some random permutations for better matching\n    vector<int> perm(N);\n    for (int i = 0; i < N; i++) perm[i] = i;\n    \n    for (int trials = 0; trials < min(50, N * N); trials++) {\n        shuffle(perm.begin(), perm.end(), rng);\n        \n        double score = 0;\n        int idx = 0;\n        for (int i = 0; i < N; i++) {\n            for (int j = i + 1; j < N; j++) {\n                int pi = perm[i], pj = perm[j];\n                if (pi > pj) swap(pi, pj);\n                \n                int orig_idx = pi * (2 * N - pi - 3) / 2 + pj - pi - 1;\n                char orig_edge = original[orig_idx];\n                char recv_edge = received[idx];\n                \n                if (orig_edge == recv_edge) {\n                    score += 1 - epsilon;\n                } else {\n                    score += epsilon;\n                }\n                idx++;\n            }\n        }\n        \n        best_score = max(best_score, score);\n    }\n    \n    return best_score;\n}\n\n// Decode which original graph generated the received graph\nint decodeGraph(const string& received) {\n    vector<pair<double, int>> scores;\n    \n    for (int i = 0; i < M; i++) {\n        double similarity = calculateSimilarity(graphs[i], received);\n        scores.emplace_back(similarity, i);\n    }\n    \n    sort(scores.rbegin(), scores.rend());\n    return scores[0].second;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    cin >> M >> epsilon;\n    \n    // Generate graphs\n    generateGraphs();\n    \n    // Output N\n    cout << N << endl;\n    \n    // Output all graphs\n    for (const string& graph : graphs) {\n        cout << graph << endl;\n    }\n    cout.flush();\n    \n    // Process queries\n    for (int q = 0; q < QUERY_COUNT; q++) {\n        string received;\n        cin >> received;\n        \n        int prediction = decodeGraph(received);\n        cout << prediction << endl;\n        cout.flush();\n    }\n    \n    return 0;\n}","ahc017":"#include <bits/stdc++.h>\n#include <atcoder/all>\nusing namespace std;\nusing namespace atcoder;\n\nusing ll = long long;\nusing P = pair<int, int>;\nusing PL = pair<ll, ll>;\nusing PLL = pair<ll, ll>;\n\nconst ll INF = 1e15;\nconst int MAXN = 1005;\nconst int MAXM = 3005;\n\nint N, M, D, K;\nvector<tuple<int, int, int>> edges;\nvector<P> coords;\nvector<vector<P>> adj;\nvector<tuple<int, int, ll>> dists;\n\n// Calculate shortest distances using Floyd-Warshall for small graphs or Dijkstra for larger ones\nvector<vector<ll>> calculate_all_pairs_distances(const vector<vector<P>>& graph) {\n    vector<vector<ll>> dist(N, vector<ll>(N, INF));\n    \n    // For smaller graphs, use Floyd-Warshall\n    if (N <= 300) {\n        // Initialize distances\n        for (int i = 0; i < N; i++) {\n            dist[i][i] = 0;\n            for (auto& edge : graph[i]) {\n                dist[i][edge.first] = min(dist[i][edge.first], (ll)edge.second);\n            }\n        }\n        \n        // Floyd-Warshall\n        for (int k = 0; k < N; k++) {\n            for (int i = 0; i < N; i++) {\n                for (int j = 0; j < N; j++) {\n                    if (dist[i][k] < INF && dist[k][j] < INF) {\n                        dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);\n                    }\n                }\n            }\n        }\n    } else {\n        // For larger graphs, use Dijkstra from each node\n        for (int s = 0; s < N; s++) {\n            priority_queue<PL, vector<PL>, greater<PL>> pq;\n            vector<ll> d(N, INF);\n            d[s] = 0;\n            pq.push({0, s});\n            \n            while (!pq.empty()) {\n                auto [cur_dist, u] = pq.top(); pq.pop();\n                if (cur_dist > d[u]) continue;\n                \n                for (auto& edge : graph[u]) {\n                    int v = edge.first;\n                    ll w = edge.second;\n                    if (d[u] + w < d[v]) {\n                        d[v] = d[u] + w;\n                        pq.push({d[v], v});\n                    }\n                }\n            }\n            \n            for (int i = 0; i < N; i++) {\n                dist[s][i] = d[i];\n            }\n        }\n    }\n    \n    return dist;\n}\n\n// Calculate frustration for a given day (edges to remove)\nll calculate_frustration(const vector<vector<ll>>& original_dist, \n                        const vector<bool>& removed_edges,\n                        const vector<vector<P>>& graph) {\n    // Create graph without removed edges\n    vector<vector<P>> temp_graph(N);\n    for (int i = 0; i < M; i++) {\n        if (!removed_edges[i]) {\n            auto [u, v, w] = edges[i];\n            temp_graph[u-1].push_back({v-1, w});\n            temp_graph[v-1].push_back({u-1, w});\n        }\n    }\n    \n    auto new_dist = calculate_all_pairs_distances(temp_graph);\n    \n    ll total_increase = 0;\n    int count = 0;\n    \n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (i != j) {\n                ll increase = new_dist[i][j] - original_dist[i][j];\n                if (new_dist[i][j] >= 1000000000) increase = 1000000000; // Cap at 1e9\n                total_increase += max(0LL, increase);\n                count++;\n            }\n        }\n    }\n    \n    return total_increase;\n}\n\n// Heuristic approach to assign edges to days\nvector<int> solve() {\n    vector<int> result(M, 1);\n    \n    // Calculate original distances\n    vector<vector<P>> original_graph(N);\n    for (auto& edge : edges) {\n        auto [u, v, w] = edge;\n        original_graph[u-1].push_back({v-1, w});\n        original_graph[v-1].push_back({u-1, w});\n    }\n    \n    auto original_dist = calculate_all_pairs_distances(original_graph);\n    \n    // Simple greedy assignment - distribute edges evenly\n    vector<vector<int>> days(D + 1);\n    for (int i = 0; i < M; i++) {\n        int day = (i % D) + 1;\n        result[i] = day;\n        days[day].push_back(i);\n    }\n    \n    // Try to improve by swapping edges between days\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n    uniform_int_distribution<int> dist_day(1, D);\n    \n    for (int iter = 0; iter < 10000 && chrono::duration_cast<chrono::milliseconds>(chrono::steady_clock::now().time_since_epoch()).count() < 5500; iter++) {\n        int day1 = dist_day(rng);\n        int day2 = dist_day(rng);\n        if (day1 == day2) continue;\n        \n        if (!days[day1].empty() && days[day2].size() < K) {\n            uniform_int_distribution<int> dist_edge(0, (int)days[day1].size() - 1);\n            int idx = dist_edge(rng);\n            int edge_idx = days[day1][idx];\n            \n            // Check if moving this edge improves the solution\n            if (days[day1].size() > 1) {  // Don't empty a day\n                result[edge_idx] = day2;\n                days[day2].push_back(edge_idx);\n                days[day1].erase(days[day1].begin() + idx);\n            }\n        }\n    }\n    \n    return result;\n}\n\n// Alternative simpler approach focusing on edge betweenness\nvector<int> solve_simple() {\n    vector<int> result(M);\n    \n    // Assign edges to days in round-robin fashion\n    for (int i = 0; i < M; i++) {\n        result[i] = (i % D) + 1;\n    }\n    \n    return result;\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    cin >> N >> M >> D >> K;\n    edges.resize(M);\n    coords.resize(N);\n    \n    for (int i = 0; i < M; i++) {\n        int u, v, w;\n        cin >> u >> v >> w;\n        edges[i] = {u, v, w};\n    }\n    \n    for (int i = 0; i < N; i++) {\n        cin >> coords[i].first >> coords[i].second;\n    }\n    \n    // Build adjacency list\n    adj.assign(N, vector<P>());\n    for (auto& edge : edges) {\n        auto [u, v, w] = edge;\n        adj[u-1].push_back({v-1, w});\n        adj[v-1].push_back({u-1, w});\n    }\n    \n    auto result = solve_simple();\n    \n    for (int i = 0; i < M; i++) {\n        cout << result[i];\n        if (i < M - 1) cout << \" \";\n    }\n    cout << \"\\n\";\n    \n    return 0;\n}","ahc019":"#include <bits/stdc++.h>\n#include <atcoder/all>\nusing namespace std;\nusing namespace atcoder;\n\n// Direction vectors for connectivity checks\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// Generate small polycube blocks\nvector<vector<tuple<int, int, int>>> generateBlocks(int maxVolume) {\n    vector<vector<tuple<int, int, int>>> blocks;\n    \n    // Single cube\n    blocks.push_back({{0, 0, 0}});\n    \n    // 2 cubes\n    blocks.push_back({{0, 0, 0}, {1, 0, 0}});\n    blocks.push_back({{0, 0, 0}, {0, 1, 0}});\n    blocks.push_back({{0, 0, 0}, {0, 0, 1}});\n    \n    // 3 cubes - lines\n    blocks.push_back({{0, 0, 0}, {1, 0, 0}, {2, 0, 0}});\n    blocks.push_back({{0, 0, 0}, {0, 1, 0}, {0, 2, 0}});\n    blocks.push_back({{0, 0, 0}, {0, 0, 1}, {0, 0, 2}});\n    \n    // 3 cubes - L-shape\n    blocks.push_back({{0, 0, 0}, {1, 0, 0}, {0, 1, 0}});\n    blocks.push_back({{0, 0, 0}, {1, 0, 0}, {0, 0, 1}});\n    blocks.push_back({{0, 0, 0}, {0, 1, 0}, {0, 0, 1}});\n    \n    // 4 cubes - 2x2 square\n    blocks.push_back({{0, 0, 0}, {1, 0, 0}, {0, 1, 0}, {1, 1, 0}});\n    \n    // 4 cubes - line\n    blocks.push_back({{0, 0, 0}, {1, 0, 0}, {2, 0, 0}, {3, 0, 0}});\n    \n    // 4 cubes - T-shape\n    blocks.push_back({{0, 0, 0}, {1, 0, 0}, {2, 0, 0}, {1, 1, 0}});\n    \n    // 4 cubes - 3D shapes\n    blocks.push_back({{0, 0, 0}, {1, 0, 0}, {0, 1, 0}, {0, 0, 1}});\n    \n    return blocks;\n}\n\n// Check if a block is connected\nbool isConnected(const vector<tuple<int, int, int>>& block) {\n    if (block.size() <= 1) return true;\n    \n    set<tuple<int, int, int>> visited;\n    queue<tuple<int, int, int>> q;\n    q.push(block[0]);\n    visited.insert(block[0]);\n    \n    while (!q.empty()) {\n        auto [x, y, z] = q.front(); q.pop();\n        \n        for (int i = 0; i < 6; i++) {\n            int nx = x + dx[i], ny = y + dy[i], nz = z + dz[i];\n            tuple<int, int, int> neighbor(nx, ny, nz);\n            \n            if (find(block.begin(), block.end(), neighbor) != block.end() && \n                visited.find(neighbor) == visited.end()) {\n                visited.insert(neighbor);\n                q.push(neighbor);\n            }\n        }\n    }\n    \n    return visited.size() == block.size();\n}\n\n// Rotate a block 90 degrees around x-axis\nvector<tuple<int, int, int>> rotateX(const vector<tuple<int, int, int>>& block) {\n    vector<tuple<int, int, int>> rotated;\n    for (auto [x, y, z] : block) {\n        rotated.push_back({x, -z, y});\n    }\n    return rotated;\n}\n\n// Rotate a block 90 degrees around y-axis\nvector<tuple<int, int, int>> rotateY(const vector<tuple<int, int, int>>& block) {\n    vector<tuple<int, int, int>> rotated;\n    for (auto [x, y, z] : block) {\n        rotated.push_back({z, y, -x});\n    }\n    return rotated;\n}\n\n// Rotate a block 90 degrees around z-axis\nvector<tuple<int, int, int>> rotateZ(const vector<tuple<int, int, int>>& block) {\n    vector<tuple<int, int, int>> rotated;\n    for (auto [x, y, z] : block) {\n        rotated.push_back({-y, x, z});\n    }\n    return rotated;\n}\n\n// Normalize block position (translate to origin)\nvector<tuple<int, int, int>> normalize(const vector<tuple<int, int, int>>& block) {\n    if (block.empty()) return block;\n    \n    int min_x = get<0>(*min_element(block.begin(), block.end(), \n                                   [](const auto& a, const auto& b) { return get<0>(a) < get<0>(b); }));\n    int min_y = get<1>(*min_element(block.begin(), block.end(), \n                                   [](const auto& a, const auto& b) { return get<1>(a) < get<1>(b); }));\n    int min_z = get<2>(*min_element(block.begin(), block.end(), \n                                   [](const auto& a, const auto& b) { return get<2>(a) < get<2>(b); }));\n    \n    vector<tuple<int, int, int>> normalized;\n    for (auto [x, y, z] : block) {\n        normalized.push_back({x - min_x, y - min_y, z - min_z});\n    }\n    return normalized;\n}\n\n// Generate all rotations of a block\nset<vector<tuple<int, int, int>>> getAllRotations(const vector<tuple<int, int, int>>& block) {\n    set<vector<tuple<int, int, int>>> rotations;\n    \n    auto current = block;\n    for (int rx = 0; rx < 4; rx++) {\n        auto temp = current;\n        for (int ry = 0; ry < 4; ry++) {\n            auto temp2 = temp;\n            for (int rz = 0; rz < 4; rz++) {\n                rotations.insert(normalize(temp2));\n                temp2 = rotateZ(temp2);\n            }\n            temp = rotateY(temp);\n        }\n        current = rotateX(current);\n    }\n    \n    return rotations;\n}\n\n// Check if block fits in valid positions for silhouette\nbool canPlaceBlock(const vector<tuple<int, int, int>>& block, \n                   const vector<vector<int>>& front, \n                   const vector<vector<int>>& right,\n                   int x_offset, int y_offset, int z_offset) {\n    for (auto [x, y, z] : block) {\n        int nx = x + x_offset, ny = y + y_offset, nz = z + z_offset;\n        if (nx < 0 || nx >= front[0].size() || \n            ny < 0 || ny >= right[0].size() || \n            nz < 0 || nz >= front.size()) {\n            return false;\n        }\n        if (front[nz][nx] == 0 || right[nz][ny] == 0) {\n            return false;\n        }\n    }\n    return true;\n}\n\n// Try to place blocks to satisfy silhouettes\npair<vector<vector<vector<int>>>, vector<int>> solveSilhouette(\n    const vector<vector<int>>& front, \n    const vector<vector<int>>& right,\n    const vector<vector<tuple<int, int, int>>>& blocks) {\n    \n    int D = front.size();\n    vector<vector<vector<int>>> result(D, vector<vector<int>>(D, vector<int>(D, 0)));\n    vector<int> used_blocks;\n    \n    // Simple greedy approach - place blocks where both silhouettes are 1\n    vector<vector<vector<bool>>> occupied(D, vector<vector<bool>>(D, vector<bool>(D, false)));\n    \n    // Try to fill positions where both silhouettes are active\n    for (int z = 0; z < D; z++) {\n        for (int x = 0; x < D; x++) {\n            if (front[z][x] == 1) {\n                for (int y = 0; y < D; y++) {\n                    if (right[z][y] == 1 && !occupied[z][x][y]) {\n                        // Try to place a small block here\n                        result[z][x][y] = used_blocks.size() + 1;\n                        used_blocks.push_back(0); // Single cube block index\n                        occupied[z][x][y] = true;\n                    }\n                }\n            }\n        }\n    }\n    \n    return {result, used_blocks};\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int D;\n    cin >> D;\n    \n    vector<vector<int>> f1(D, vector<int>(D));\n    vector<vector<int>> r1(D, vector<int>(D));\n    vector<vector<int>> f2(D, vector<int>(D));\n    vector<vector<int>> r2(D, vector<int>(D));\n    \n    // Read first silhouette pair\n    for (int i = 0; i < D; i++) {\n        string line;\n        cin >> line;\n        for (int j = 0; j < D; j++) {\n            f1[i][j] = line[j] - '0';\n        }\n    }\n    for (int i = 0; i < D; i++) {\n        string line;\n        cin >> line;\n        for (int j = 0; j < D; j++) {\n            r1[i][j] = line[j] - '0';\n        }\n    }\n    \n    // Read second silhouette pair\n    for (int i = 0; i < D; i++) {\n        string line;\n        cin >> line;\n        for (int j = 0; j < D; j++) {\n            f2[i][j] = line[j] - '0';\n        }\n    }\n    for (int i = 0; i < D; i++) {\n        string line;\n        cin >> line;\n        for (int j = 0; j < D; j++) {\n            r2[i][j] = line[j] - '0';\n        }\n    }\n    \n    // Generate basic blocks\n    auto blocks = generateBlocks(4);\n    \n    // Solve for both silhouette pairs\n    auto [solution1, used1] = solveSilhouette(f1, r1, blocks);\n    auto [solution2, used2] = solveSilhouette(f2, r2, blocks);\n    \n    // Count total unique blocks needed\n    int max_block = 0;\n    for (int z = 0; z < D; z++) {\n        for (int x = 0; x < D; x++) {\n            for (int y = 0; y < D; y++) {\n                max_block = max({max_block, solution1[z][x][y], solution2[z][x][y]});\n            }\n        }\n    }\n    \n    // Output format\n    cout << max_block << \"\\n\";\n    \n    // Output first solution\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 (z > 0) cout << \" \";\n                cout << solution1[z][x][y];\n            }\n        }\n    }\n    cout << \"\\n\";\n    \n    // Output second solution\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 (z > 0) cout << \" \";\n                cout << solution2[z][x][y];\n            }\n        }\n    }\n    cout << \"\\n\";\n    \n    return 0;\n}","ahc020":"#include <bits/stdc++.h>\n#include <atcoder/all>\nusing namespace std;\nusing namespace atcoder;\n\nusing ll = long long;\nusing ld = long double;\n\nconst int MAXN = 105;\nconst int MAXM = 305;\nconst int MAXK = 5005;\n\nint N, M, K;\npair<int, int> vertices[MAXN];\ntuple<int, int, int> edges[MAXM];\npair<int, int> residents[MAXK];\nint P[MAXN];\nint B[MAXM];\nvector<vector<int>> adj[MAXN];\nvector<pair<int, int>> edge_list;\n\n// Distance calculation\nll dist_sq(int x1, int y1, int x2, int y2) {\n    return 1LL * (x1 - x2) * (x1 - x2) + 1LL * (y1 - y2) * (y1 - y2);\n}\n\nld dist(int x1, int y1, int x2, int y2) {\n    return sqrt(dist_sq(x1, y1, x2, y2));\n}\n\n// Find closest vertex to a resident\nint find_closest_vertex(int rx, int ry) {\n    int best = 0;\n    ll best_dist = dist_sq(rx, ry, vertices[0].first, vertices[0].second);\n    for (int i = 1; i < N; i++) {\n        ll d = dist_sq(rx, ry, vertices[i].first, vertices[i].second);\n        if (d < best_dist) {\n            best_dist = d;\n            best = i;\n        }\n    }\n    return best;\n}\n\n// Calculate required radius to cover residents from a vertex\nint calculate_radius(int vertex_idx, const vector<int>& assigned_residents) {\n    if (assigned_residents.empty()) return 0;\n    \n    int vx = vertices[vertex_idx].first;\n    int vy = vertices[vertex_idx].second;\n    int max_dist = 0;\n    \n    for (int res_idx : assigned_residents) {\n        int rx = residents[res_idx].first;\n        int ry = residents[res_idx].second;\n        int d = (int)round(dist(vx, vy, rx, ry));\n        max_dist = max(max_dist, d);\n    }\n    \n    return min(max_dist, 5000);\n}\n\n// Prim's algorithm for minimum spanning tree\nvector<bool> find_minimal_network(const vector<bool>& must_include) {\n    vector<bool> in_tree(N, false);\n    vector<ll> min_cost(N, 1e18);\n    vector<int> parent(N, -1);\n    \n    // Start from vertex 0 (AtCoder office)\n    in_tree[0] = true;\n    min_cost[0] = 0;\n    \n    // Initialize costs for adjacent vertices\n    vector<pair<int, int>> adj_edges[N];\n    for (int i = 0; i < M; i++) {\n        int u = get<0>(edges[i]) - 1;\n        int v = get<1>(edges[i]) - 1;\n        int w = get<2>(edges[i]);\n        adj_edges[u].push_back({v, w});\n        adj_edges[v].push_back({u, w});\n    }\n    \n    priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<>> pq;\n    \n    for (auto& e : adj_edges[0]) {\n        int to = e.first;\n        int weight = e.second;\n        if (min_cost[to] > weight) {\n            min_cost[to] = weight;\n            parent[to] = 0;\n            pq.push({min_cost[to], to});\n        }\n    }\n    \n    while (!pq.empty()) {\n        auto [cost, u] = pq.top(); pq.pop();\n        if (in_tree[u]) continue;\n        in_tree[u] = true;\n        \n        for (auto& e : adj_edges[u]) {\n            int to = e.first;\n            int weight = e.second;\n            if (!in_tree[to] && min_cost[to] > weight) {\n                min_cost[to] = weight;\n                parent[to] = u;\n                pq.push({min_cost[to], to});\n            }\n        }\n    }\n    \n    // Ensure all must_include vertices are connected\n    vector<bool> result(N, false);\n    for (int i = 0; i < N; i++) {\n        if (must_include[i]) {\n            int current = i;\n            while (current != -1 && !result[current]) {\n                result[current] = true;\n                current = parent[current];\n            }\n        }\n    }\n    \n    // Always include vertex 0\n    result[0] = true;\n    \n    // Connect components if needed\n    queue<int> q;\n    for (int i = 0; i < N; i++) {\n        if (result[i]) q.push(i);\n    }\n    \n    vector<bool> visited(N, false);\n    while (!q.empty()) {\n        int u = q.front(); q.pop();\n        if (visited[u]) continue;\n        visited[u] = true;\n        \n        for (auto& e : adj_edges[u]) {\n            int v = e.first;\n            if (!visited[v]) {\n                q.push(v);\n                result[v] = true;\n            }\n        }\n    }\n    \n    return result;\n}\n\nvoid solve() {\n    // Assign residents to closest vertices\n    vector<vector<int>> vertex_residents(N);\n    for (int i = 0; i < K; i++) {\n        int closest = find_closest_vertex(residents[i].first, residents[i].second);\n        vertex_residents[closest].push_back(i);\n    }\n    \n    // Determine which vertices we might want to include\n    vector<bool> should_include(N, false);\n    vector<int> required_radius(N, 0);\n    \n    for (int i = 0; i < N; i++) {\n        if (!vertex_residents[i].empty()) {\n            required_radius[i] = calculate_radius(i, vertex_residents[i]);\n            if (required_radius[i] > 0) {\n                should_include[i] = true;\n            }\n        }\n    }\n    \n    // Find minimal network to connect necessary vertices\n    vector<bool> network = find_minimal_network(should_include);\n    \n    // Calculate final output strengths\n    fill(P, P + N, 0);\n    for (int i = 0; i < N; i++) {\n        if (network[i]) {\n            P[i] = required_radius[i];\n        }\n    }\n    \n    // Set edge states\n    fill(B, B + M, 0);\n    vector<vector<pair<int, int>>> graph_adj(N);\n    for (int i = 0; i < M; i++) {\n        int u = get<0>(edges[i]) - 1;\n        int v = get<1>(edges[i]) - 1;\n        if (network[u] && network[v]) {\n            graph_adj[u].push_back({v, i});\n            graph_adj[v].push_back({u, i});\n        }\n    }\n    \n    // Build spanning tree on active vertices\n    vector<bool> visited(N, false);\n    queue<int> q;\n    q.push(0);\n    visited[0] = true;\n    \n    while (!q.empty()) {\n        int u = q.front(); q.pop();\n        for (auto& [v, edge_idx] : graph_adj[u]) {\n            if (!visited[v]) {\n                visited[v] = true;\n                B[edge_idx] = 1;\n                q.push(v);\n            }\n        }\n    }\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    cin >> N >> M >> K;\n    \n    for (int i = 0; i < N; i++) {\n        cin >> vertices[i].first >> vertices[i].second;\n    }\n    \n    for (int i = 0; i < M; i++) {\n        int u, v, w;\n        cin >> u >> v >> w;\n        edges[i] = {u, v, w};\n    }\n    \n    for (int i = 0; i < K; i++) {\n        cin >> residents[i].first >> residents[i].second;\n    }\n    \n    solve();\n    \n    for (int i = 0; i < N; i++) {\n        cout << P[i];\n        if (i + 1 < N) cout << \" \";\n    }\n    cout << \"\\n\";\n    \n    for (int i = 0; i < M; i++) {\n        cout << B[i];\n        if (i + 1 < M) cout << \" \";\n    }\n    cout << \"\\n\";\n    \n    return 0;\n}","ahc021":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 30;\nconst int MAX_OPS = 10000;\n\nint pyramid[N][N];\nvector<tuple<int, int, int, int>> operations;\n\n// Check if coordinates are valid\nbool valid(int x, int y) {\n    return x >= 0 && x < N && y >= 0 && y <= x;\n}\n\n// Get adjacent positions in 6 directions\nvector<pair<int, int>> getAdjacent(int x, int y) {\n    vector<pair<int, int>> adj;\n    vector<pair<int, int>> directions = {\n        {-1, -1}, {-1, 0}, {0, -1}, {0, 1}, {1, 0}, {1, 1}\n    };\n    \n    for (auto& dir : directions) {\n        int nx = x + dir.first;\n        int ny = y + dir.second;\n        if (valid(nx, ny)) {\n            adj.push_back({nx, ny});\n        }\n    }\n    return adj;\n}\n\n// Count violations in current state\nint countViolations() {\n    int violations = 0;\n    for (int x = 0; x < N - 1; x++) {\n        for (int y = 0; y <= x; y++) {\n            if (pyramid[x][y] > pyramid[x+1][y]) violations++;\n            if (pyramid[x][y] > pyramid[x+1][y+1]) violations++;\n        }\n    }\n    return violations;\n}\n\n// Find shortest path between two positions using BFS\nvector<pair<int, int>> findPath(int startX, int startY, int endX, int endY) {\n    if (startX == endX && startY == endY) return {};\n    \n    queue<pair<int, int>> q;\n    map<pair<int, int>, pair<int, int>> parent;\n    set<pair<int, int>> visited;\n    \n    q.push({startX, startY});\n    visited.insert({startX, startY});\n    \n    vector<pair<int, int>> directions = {{-1, -1}, {-1, 0}, {0, -1}, {0, 1}, {1, 0}, {1, 1}};\n    \n    while (!q.empty()) {\n        auto [x, y] = q.front();\n        q.pop();\n        \n        if (x == endX && y == endY) {\n            // Reconstruct path\n            vector<pair<int, int>> path;\n            auto current = make_pair(endX, endY);\n            while (current != make_pair(startX, startY)) {\n                path.push_back(current);\n                current = parent[current];\n            }\n            reverse(path.begin(), path.end());\n            return path;\n        }\n        \n        for (auto& dir : directions) {\n            int nx = x + dir.first;\n            int ny = y + dir.second;\n            if (valid(nx, ny) && visited.find({nx, ny}) == visited.end()) {\n                visited.insert({nx, ny});\n                parent[{nx, ny}] = {x, y};\n                q.push({nx, ny});\n            }\n        }\n    }\n    \n    return {}; // No path found\n}\n\n// Perform a swap and record operation\nvoid performSwap(int x1, int y1, int x2, int y2) {\n    if (operations.size() >= MAX_OPS) return;\n    \n    swap(pyramid[x1][y1], pyramid[x2][y2]);\n    operations.push_back({x1, y1, x2, y2});\n}\n\n// Move a value to a specific position using path finding\nvoid moveToPosition(int startX, int startY, int endX, int endY) {\n    if (startX == endX && startY == endY) return;\n    if (operations.size() >= MAX_OPS - 10) return;\n    \n    auto path = findPath(startX, startY, endX, endY);\n    \n    int currentX = startX, currentY = startY;\n    for (auto& pos : path) {\n        if (operations.size() >= MAX_OPS) break;\n        performSwap(currentX, currentY, pos.first, pos.second);\n        currentX = pos.first;\n        currentY = pos.second;\n    }\n}\n\n// Fix violations using a greedy approach\nvoid fixViolations() {\n    // Try multiple passes\n    for (int pass = 0; pass < 3 && operations.size() < MAX_OPS - 50; pass++) {\n        // Bottom-up approach - start from second to last row and go up\n        for (int x = N - 2; x >= 0; x--) {\n            for (int y = 0; y <= x && operations.size() < MAX_OPS - 10; y++) {\n                // Check if current position violates heap property\n                if (x < N - 1) {\n                    // Check left child\n                    if (pyramid[x][y] > pyramid[x+1][y]) {\n                        // Try to fix by swapping with smaller child or moving current up\n                        if (operations.size() < MAX_OPS - 5) {\n                            performSwap(x, y, x+1, y);\n                        }\n                    }\n                    // Check right child\n                    if (pyramid[x][y] > pyramid[x+1][y+1]) {\n                        if (operations.size() < MAX_OPS - 5) {\n                            performSwap(x, y, x+1, y+1);\n                        }\n                    }\n                }\n            }\n        }\n        \n        // Additional pass - try to improve locally\n        if (pass > 0) {\n            for (int x = 0; x < N - 1 && operations.size() < MAX_OPS - 10; x++) {\n                for (int y = 0; y <= x && operations.size() < MAX_OPS - 10; y++) {\n                    // If current is larger than children, try to fix\n                    if (pyramid[x][y] > pyramid[x+1][y] || pyramid[x][y] > pyramid[x+1][y+1]) {\n                        // Find the minimum child\n                        int minChildVal = min(pyramid[x+1][y], pyramid[x+1][y+1]);\n                        if (pyramid[x][y] > minChildVal) {\n                            // Try to swap with the smaller child\n                            if (pyramid[x+1][y] <= pyramid[x+1][y+1]) {\n                                performSwap(x, y, x+1, y);\n                            } else {\n                                performSwap(x, y, x+1, y+1);\n                            }\n                        }\n                    }\n                }\n            }\n        }\n    }\n}\n\nint main() {\n    // Read input\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j <= i; j++) {\n            cin >> pyramid[i][j];\n        }\n    }\n    \n    // Initial violation count\n    int initialViolations = countViolations();\n    \n    // Apply fixing algorithm\n    fixViolations();\n    \n    // Output results\n    cout << operations.size() << \"\\n\";\n    for (auto& op : operations) {\n        cout << get<0>(op) << \" \" << get<1>(op) << \" \" << get<2>(op) << \" \" << get<3>(op) << \"\\n\";\n    }\n    \n    return 0;\n}","toyota2023summer-final":"#include <bits/stdc++.h>\n#include <atcoder/all>\nusing namespace std;\nusing namespace atcoder;\n\nconst int D = 9;\nconst int INF = 100000000;\n\nint entrance_i = 0;\nint entrance_j = 4;\n\nvector<vector<bool>> is_obstacle(D, vector<bool>(D, false));\nvector<vector<int>> container_id(D, vector<int>(D, -1));\nvector<pair<int, int>> container_positions(D * D); // id -> position\nvector<int> container_numbers(D * D); // position index -> container number\n\n// Directions: up, down, left, right\nconst int di[4] = {-1, 1, 0, 0};\nconst int dj[4] = {0, 0, -1, 1};\n\n// Check if position is valid\nbool is_valid(int i, int j) {\n    return i >= 0 && i < D && j >= 0 && j < D;\n}\n\n// Check if a position is the entrance or adjacent to entrance\nbool is_entrance_area(int i, int j) {\n    if (i == 0 && j == 4) return true;\n    if (i == 0 && j == 3) return true;\n    if (i == 0 && j == 5) return true;\n    if (i == 1 && j == 4) return true;\n    return false;\n}\n\n// BFS to find all reachable positions from entrance\nvector<vector<int>> get_distances_from_entrance() {\n    vector<vector<int>> dist(D, vector<int>(D, INF));\n    queue<pair<int, int>> q;\n    \n    // Start from entrance\n    dist[0][4] = 0;\n    q.push({0, 4});\n    \n    while (!q.empty()) {\n        auto [ci, cj] = q.front(); q.pop();\n        \n        for (int d = 0; d < 4; d++) {\n            int ni = ci + di[d];\n            int nj = cj + dj[d];\n            \n            if (is_valid(ni, nj) && !is_obstacle[ni][nj] && container_id[ni][nj] == -1) {\n                if (dist[ni][nj] > dist[ci][cj] + 1) {\n                    dist[ni][nj] = dist[ci][cj] + 1;\n                    q.push({ni, nj});\n                }\n            }\n        }\n    }\n    \n    return dist;\n}\n\n// Find all reachable empty positions\nvector<pair<int, int>> get_reachable_empty_positions() {\n    vector<vector<bool>> visited(D, vector<bool>(D, false));\n    vector<pair<int, int>> result;\n    queue<pair<int, int>> q;\n    \n    // Start from entrance\n    visited[0][4] = true;\n    q.push({0, 4});\n    if (container_id[0][4] == -1 && !is_obstacle[0][4]) {\n        result.push_back({0, 4});\n    }\n    \n    while (!q.empty()) {\n        auto [ci, cj] = q.front(); q.pop();\n        \n        for (int d = 0; d < 4; d++) {\n            int ni = ci + di[d];\n            int nj = cj + dj[d];\n            \n            if (is_valid(ni, nj) && !visited[ni][nj] && !is_obstacle[ni][nj]) {\n                visited[ni][nj] = true;\n                q.push({ni, nj});\n                if (container_id[ni][nj] == -1) {\n                    result.push_back({ni, nj});\n                }\n            }\n        }\n    }\n    \n    return result;\n}\n\n// Calculate Manhattan distance from entrance\nint manhattan_distance(int i, int j) {\n    return abs(i - 0) + abs(j - 4);\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int N;\n    cin >> D >> N;\n    \n    for (int i = 0; i < N; i++) {\n        int ri, rj;\n        cin >> ri >> rj;\n        is_obstacle[ri][rj] = true;\n    }\n    \n    int total_containers = D * D - 1 - N;\n    vector<int> transport_order(total_containers);\n    \n    // Process each container as it arrives\n    for (int d = 0; d < total_containers; d++) {\n        int t_d;\n        cin >> t_d;\n        transport_order[d] = t_d;\n        \n        // Get all reachable empty positions\n        auto reachable_positions = get_reachable_empty_positions();\n        \n        // Remove entrance area from consideration (except if it's the only option)\n        vector<pair<int, int>> valid_positions;\n        for (auto& pos : reachable_positions) {\n            if (!is_entrance_area(pos.first, pos.second)) {\n                valid_positions.push_back(pos);\n            }\n        }\n        \n        // If no valid positions except entrance area, use entrance area\n        if (valid_positions.empty()) {\n            for (auto& pos : reachable_positions) {\n                if (pos.first != 0 || pos.second != 4) { // exclude exact entrance\n                    valid_positions.push_back(pos);\n                }\n            }\n        }\n        \n        // If still empty, just use entrance\n        if (valid_positions.empty()) {\n            valid_positions.push_back({0, 4});\n        }\n        \n        // Choose position based on container number\n        // Lower numbers should be placed closer to entrance for easier retrieval\n        pair<int, int> best_position = valid_positions[0];\n        int best_score = INF;\n        \n        for (auto& pos : valid_positions) {\n            int dist = manhattan_distance(pos.first, pos.second);\n            // Prefer positions closer to entrance for lower numbered containers\n            // But also add some randomness to avoid getting stuck\n            int score = dist * 1000 + (t_d * 10) + (rand() % 100);\n            \n            if (score < best_score) {\n                best_score = score;\n                best_position = pos;\n            }\n        }\n        \n        // Place the container\n        int pi = best_position.first;\n        int pj = best_position.second;\n        container_id[pi][pj] = d;\n        container_positions[d] = {pi, pj};\n        container_numbers[d] = t_d;\n        \n        // Output the position\n        cout << pi << \" \" << pj << endl;\n        cout.flush();\n    }\n    \n    // Now determine transport order\n    // We want to transport in order of assigned numbers (0, 1, 2, ...)\n    // But we can only transport reachable containers\n    \n    vector<bool> transported(total_containers, false);\n    vector<pair<int, int>> transport_sequence;\n    \n    for (int target_number = 0; target_number < total_containers; target_number++) {\n        // Find container with target_number\n        int container_idx = -1;\n        for (int i = 0; i < total_containers; i++) {\n            if (container_numbers[i] == target_number) {\n                container_idx = i;\n                break;\n            }\n        }\n        \n        if (container_idx == -1) continue;\n        \n        // Mark this container for transport when it becomes reachable\n        pair<int, int> target_pos = container_positions[container_idx];\n        \n        // For simplicity, we'll transport containers in the order they become reachable\n        // In a more sophisticated solution, we'd use a priority queue\n        transport_sequence.push_back(target_pos);\n    }\n    \n    // Actually, let's implement a better approach:\n    // Try to transport containers in order of their numbers, but only when reachable\n    vector<bool> container_transported(total_containers, false);\n    transport_sequence.clear();\n    \n    while (transport_sequence.size() < total_containers) {\n        // Get currently reachable containers\n        auto distances = get_distances_from_entrance();\n        vector<pair<int, int>> reachable_containers;\n        vector<int> reachable_indices;\n        \n        for (int i = 0; i < total_containers; i++) {\n            if (container_transported[i]) continue;\n            \n            auto pos = container_positions[i];\n            if (distances[pos.first][pos.second] < INF) {\n                reachable_containers.push_back(pos);\n                reachable_indices.push_back(i);\n            }\n        }\n        \n        if (reachable_containers.empty()) {\n            // This shouldn't happen if the problem is solvable\n            break;\n        }\n        \n        // Among reachable containers, pick the one with lowest assigned number\n        int best_idx = reachable_indices[0];\n        int best_number = container_numbers[best_idx];\n        \n        for (int idx : reachable_indices) {\n            if (container_numbers[idx] < best_number) {\n                best_number = container_numbers[idx];\n                best_idx = idx;\n            }\n        }\n        \n        // Transport this container\n        auto pos = container_positions[best_idx];\n        transport_sequence.push_back(pos);\n        container_transported[best_idx] = true;\n        \n        // Clear the position\n        container_id[pos.first][pos.second] = -1;\n    }\n    \n    // Output transport sequence\n    for (auto& pos : transport_sequence) {\n        cout << pos.first << \" \" << pos.second << \"\\n\";\n    }\n    \n    return 0;\n}","ahc024":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 50;\nconst int M = 100;\n\nint grid[N][N];\nint result[N][N];\nvector<pair<int,int>> positions[M+1]; // positions for each color\nvector<int> adj_list[M+1]; // adjacency list\nbool adj_matrix[M+1][M+1]; // adjacency matrix\n\n// Directions: up, right, down, left\nconst int dx[4] = {-1, 0, 1, 0};\nconst int dy[4] = {0, 1, 0, -1};\n\nvoid extract_adjacencies() {\n    // Initialize adjacency matrix\n    memset(adj_matrix, 0, sizeof(adj_matrix));\n    \n    // Check adjacencies in the original grid\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            int color = grid[i][j];\n            for (int d = 0; d < 4; d++) {\n                int ni = i + dx[d];\n                int nj = j + dy[d];\n                int neighbor_color = 0; // outside is color 0\n                if (ni >= 0 && ni < N && nj >= 0 && nj < N) {\n                    neighbor_color = grid[ni][nj];\n                }\n                if (color != neighbor_color) {\n                    adj_matrix[color][neighbor_color] = true;\n                    adj_matrix[neighbor_color][color] = true;\n                }\n            }\n        }\n    }\n    \n    // Build adjacency lists\n    for (int i = 0; i <= M; i++) {\n        adj_list[i].clear();\n        for (int j = 0; j <= M; j++) {\n            if (adj_matrix[i][j] && i != j) {\n                adj_list[i].push_back(j);\n            }\n        }\n    }\n}\n\n// Check if we can place a color at position (x,y)\nbool can_place(int x, int y, int color) {\n    if (x < 0 || x >= N || y < 0 || y >= N) return false;\n    return result[x][y] == 0;\n}\n\n// Check if placement maintains adjacency requirements\nbool check_adjacency(int x, int y, int color) {\n    // Check if this placement creates required adjacencies\n    for (int d = 0; d < 4; d++) {\n        int nx = x + dx[d];\n        int ny = y + dy[d];\n        int neighbor = 0;\n        if (nx >= 0 && nx < N && ny >= 0 && ny < N) {\n            neighbor = result[nx][ny];\n        }\n        \n        // If neighbor is a color that should be adjacent, that's good\n        // If neighbor is a color that shouldn't be adjacent, that's bad (but we can't know all adjacencies yet)\n    }\n    return true;\n}\n\nvoid place_color_compactly(int color) {\n    if (color == 0) return; // Don't place color 0, it's empty space\n    \n    // If this color is already placed, skip\n    if (!positions[color].empty()) return;\n    \n    // Find a good starting position - near already placed colors that it should be adjacent to\n    vector<pair<int,int>> candidates;\n    \n    // If no colors placed yet, start in the middle\n    if (positions[1].empty()) {\n        int cx = N/2, cy = N/2;\n        result[cx][cy] = color;\n        positions[color].push_back({cx, cy});\n        return;\n    }\n    \n    // Try to place near adjacencies\n    bool placed = false;\n    for (int adj_color : adj_list[color]) {\n        if (!positions[adj_color].empty()) {\n            // Try to place near this adjacent color\n            for (auto& pos : positions[adj_color]) {\n                int x = pos.first, y = pos.second;\n                for (int d = 0; d < 4; d++) {\n                    int nx = x + dx[d];\n                    int ny = y + dy[d];\n                    if (nx >= 0 && nx < N && ny >= 0 && ny < N && result[nx][ny] == 0) {\n                        result[nx][ny] = color;\n                        positions[color].push_back({nx, ny});\n                        placed = true;\n                        break;\n                    }\n                }\n                if (placed) break;\n            }\n            if (placed) break;\n        }\n    }\n    \n    // If couldn't place near adjacencies, place anywhere available\n    if (!placed) {\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                if (result[i][j] == 0) {\n                    result[i][j] = color;\n                    positions[color].push_back({i, j});\n                    placed = true;\n                    break;\n                }\n            }\n            if (placed) break;\n        }\n    }\n    \n    // Expand the color to make it connected\n    if (!positions[color].empty()) {\n        // Simple expansion to make it connected - just place one tile\n        // In a more sophisticated solution, we'd expand to match the required size\n        // But since we're optimizing for fewer empty tiles, smaller is better\n    }\n}\n\n// Greedy approach: place colors in order of how many adjacencies they have\nvoid solve() {\n    // Initialize result grid\n    memset(result, 0, sizeof(result));\n    for (int i = 0; i <= M; i++) {\n        positions[i].clear();\n    }\n    \n    // Create ordering based on adjacency count (place high-degree nodes first)\n    vector<pair<int, int>> order; // {adjacency_count, color}\n    for (int i = 1; i <= M; i++) {\n        order.push_back({(int)adj_list[i].size(), i});\n    }\n    sort(order.rbegin(), order.rend()); // descending order\n    \n    // Place colors\n    for (auto& p : order) {\n        place_color_compactly(p.second);\n    }\n    \n    // Ensure connectivity for each color\n    for (int color = 1; color <= M; color++) {\n        if (positions[color].empty()) {\n            // Place at least one tile\n            for (int i = 0; i < N; i++) {\n                for (int j = 0; j < N; j++) {\n                    if (result[i][j] == 0) {\n                        result[i][j] = color;\n                        positions[color].push_back({i, j});\n                        goto placed;\n                    }\n                }\n            }\n            placed:;\n        }\n    }\n}\n\n// Better approach: create a compact layout\nvoid solve_better() {\n    // Initialize result grid\n    memset(result, 0, sizeof(result));\n    for (int i = 0; i <= M; i++) {\n        positions[i].clear();\n    }\n    \n    // Start by placing color 1 in the center\n    int center_x = N/2, center_y = N/2;\n    result[center_x][center_y] = 1;\n    positions[1].push_back({center_x, center_y});\n    \n    // Place other colors in a spiral pattern to minimize empty space\n    vector<pair<int,int>> spiral_dirs = {{0,1}, {1,0}, {0,-1}, {-1,0}}; // right, down, left, up\n    int dir = 0;\n    int x = center_x, y = center_y;\n    int steps = 1;\n    \n    vector<int> colors_to_place;\n    for (int i = 2; i <= M; i++) {\n        colors_to_place.push_back(i);\n    }\n    \n    int color_idx = 0;\n    while (color_idx < (int)colors_to_place.size()) {\n        for (int step = 0; step < 2; step++) {\n            for (int i = 0; i < steps && color_idx < (int)colors_to_place.size(); i++) {\n                x += spiral_dirs[dir].first;\n                y += spiral_dirs[dir].second;\n                \n                if (x >= 0 && x < N && y >= 0 && y < N && result[x][y] == 0) {\n                    int color = colors_to_place[color_idx];\n                    result[x][y] = color;\n                    positions[color].push_back({x, y});\n                    color_idx++;\n                }\n                \n                if (color_idx >= (int)colors_to_place.size()) break;\n            }\n            dir = (dir + 1) % 4;\n            if (color_idx >= (int)colors_to_place.size()) break;\n        }\n        steps++;\n    }\n    \n    // This approach doesn't preserve adjacencies, so let's try a different approach\n}\n\nvoid solve_best() {\n    // Initialize result grid\n    memset(result, 0, sizeof(result));\n    for (int i = 0; i <= M; i++) {\n        positions[i].clear();\n    }\n    \n    // Simple but effective: place colors in a dense grid pattern\n    int idx = 0;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (idx < M) {\n                int color = idx + 1;\n                result[i][j] = color;\n                positions[color].push_back({i, j});\n                idx++;\n            } else {\n                // Fill remaining with a pattern to maintain some adjacencies\n                result[i][j] = 0;\n            }\n        }\n    }\n    \n    // This still doesn't preserve the original adjacencies\n    // Let's try a more sophisticated approach\n}\n\n// Let's go with a simple but effective approach\nvoid solve_simple() {\n    // Initialize result grid\n    memset(result, 0, sizeof(result));\n    for (int i = 0; i <= M; i++) {\n        positions[i].clear();\n    }\n    \n    // Place colors in a way that tries to preserve adjacencies\n    // Place first color in center\n    result[N/2][N/2] = 1;\n    positions[1].push_back({N/2, N/2});\n    \n    // For remaining colors, place them near colors they should be adjacent to\n    vector<bool> placed(M+1, false);\n    placed[0] = true; // color 0 is special\n    placed[1] = true;\n    \n    queue<int> to_process;\n    to_process.push(1);\n    \n    while (!to_process.empty()) {\n        int current_color = to_process.front();\n        to_process.pop();\n        \n        // Try to place adjacent colors\n        for (int adj_color : adj_list[current_color]) {\n            if (!placed[adj_color] && adj_color != 0) {\n                // Try to place adj_color near current_color\n                bool placed_adj = false;\n                for (auto& pos : positions[current_color]) {\n                    int x = pos.first, y = pos.second;\n                    // Try 8 directions (including diagonals for more flexibility)\n                    for (int dx_val = -1; dx_val <= 1; dx_val++) {\n                        for (int dy_val = -1; dy_val <= 1; dy_val++) {\n                            if (dx_val == 0 && dy_val == 0) continue;\n                            int nx = x + dx_val;\n                            int ny = y + dy_val;\n                            if (nx >= 0 && nx < N && ny >= 0 && ny < N && result[nx][ny] == 0) {\n                                result[nx][ny] = adj_color;\n                                positions[adj_color].push_back({nx, ny});\n                                placed[adj_color] = true;\n                                to_process.push(adj_color);\n                                placed_adj = true;\n                                break;\n                            }\n                        }\n                        if (placed_adj) break;\n                    }\n                    if (placed_adj) break;\n                }\n                \n                // If we couldn't place it near an adjacent color, place it anywhere\n                if (!placed_adj) {\n                    for (int i = 0; i < N; i++) {\n                        for (int j = 0; j < N; j++) {\n                            if (result[i][j] == 0) {\n                                result[i][j] = adj_color;\n                                positions[adj_color].push_back({i, j});\n                                placed[adj_color] = true;\n                                to_process.push(adj_color);\n                                placed_adj = true;\n                                break;\n                            }\n                        }\n                        if (placed_adj) break;\n                    }\n                }\n            }\n        }\n    }\n    \n    // Place any remaining unplaced colors\n    for (int color = 1; color <= M; color++) {\n        if (!placed[color]) {\n            for (int i = 0; i < N; i++) {\n                for (int j = 0; j < N; j++) {\n                    if (result[i][j] == 0) {\n                        result[i][j] = color;\n                        positions[color].push_back({i, j});\n                        placed[color] = true;\n                        break;\n                    }\n                }\n                if (placed[color]) break;\n            }\n        }\n    }\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    int n, m;\n    cin >> n >> m;\n    \n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            cin >> grid[i][j];\n        }\n    }\n    \n    extract_adjacencies();\n    solve_simple();\n    \n    // Output result\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            if (j > 0) cout << \" \";\n            cout << result[i][j];\n        }\n        cout << \"\\n\";\n    }\n    \n    return 0;\n}","ahc025":"#include <bits/stdc++.h>\n#include <boost/random.hpp>\n#include <boost/random/random_device.hpp>\n\nusing namespace std;\n\n// Global variables\nint N, D, Q;\nvector<vector<pair<int, int>>> comparisons;\nvector<double> weight_estimates;\nvector<vector<int>> groups;\n\n// Random number generator\nboost::random::mt19937 gen;\n\n// Function to initialize and run the algorithm\nvoid solve() {\n    // Read input\n    cin >> N >> D >> Q;\n    \n    // Initialize data structures\n    comparisons.resize(N);\n    weight_estimates.assign(N, 1.0);\n    groups.resize(D);\n    \n    // Store all queries and responses for processing\n    vector<tuple<vector<int>, vector<int>, string>> query_history;\n    \n    // Perform queries\n    for (int q = 0; q < Q; q++) {\n        vector<int> left, right;\n        \n        // Strategy: Initially compare individual items, later refine estimates\n        if (q < min(Q/2, N*(N-1)/2)) {\n            // Pairwise comparisons for early queries\n            int i = q % N;\n            int j = (q / N + i + 1) % N;\n            left = {i};\n            right = {j};\n        } else {\n            // Later queries: compare estimated heavy vs light items\n            vector<pair<double, int>> item_weights;\n            for (int i = 0; i < N; i++) {\n                item_weights.push_back({weight_estimates[i], i});\n            }\n            sort(item_weights.begin(), item_weights.end());\n            \n            // Compare lightest vs heaviest items\n            left = {item_weights[0].second};\n            right = {item_weights.back().second};\n        }\n        \n        // Output query\n        cout << left.size() << \" \" << right.size();\n        for (int item : left) cout << \" \" << item;\n        for (int item : right) cout << \" \" << item;\n        cout << endl;\n        cout.flush();\n        \n        // Read response\n        string response;\n        cin >> response;\n        \n        // Store for later processing\n        query_history.push_back({left, right, response});\n        \n        // Update weight estimates based on immediate feedback\n        if (left.size() == 1 && right.size() == 1) {\n            int l_item = left[0];\n            int r_item = right[0];\n            \n            if (response == \"=\") {\n                // Items have similar weight\n            } else if (response == \">\") {\n                // Left item is heavier\n                weight_estimates[l_item] *= 1.1;\n            } else { // response == \"<\"\n                // Right item is heavier\n                weight_estimates[r_item] *= 1.1;\n            }\n        }\n    }\n    \n    // Process all query history to refine weight estimates\n    for (const auto& [left, right, response] : query_history) {\n        double left_weight = 0, right_weight = 0;\n        for (int item : left) left_weight += weight_estimates[item];\n        for (int item : right) right_weight += weight_estimates[item];\n        \n        if (response == \"=\") {\n            // Adjust weights to make them more equal\n            double avg_weight = (left_weight + right_weight) / 2.0;\n            if (left.size() > 0) {\n                double ratio = avg_weight / left_weight;\n                for (int item : left) weight_estimates[item] *= ratio;\n            }\n            if (right.size() > 0) {\n                double ratio = avg_weight / right_weight;\n                for (int item : right) weight_estimates[item] *= ratio;\n            }\n        } else if (response == \">\") {\n            // Left is heavier, adjust accordingly\n            if (right_weight > 0) {\n                double ratio = left_weight / right_weight * 1.1;\n                for (int item : right) weight_estimates[item] *= ratio / right.size();\n            }\n        } else { // response == \"<\"\n            // Right is heavier, adjust accordingly\n            if (left_weight > 0) {\n                double ratio = right_weight / left_weight * 1.1;\n                for (int item : left) weight_estimates[item] *= ratio / left.size();\n            }\n        }\n    }\n    \n    // Normalize weight estimates\n    double total_weight = 0;\n    for (double w : weight_estimates) total_weight += w;\n    if (total_weight > 0) {\n        for (double& w : weight_estimates) w = w * N / total_weight;\n    }\n    \n    // Create groups using greedy approach\n    vector<pair<double, int>> sorted_items;\n    for (int i = 0; i < N; i++) {\n        sorted_items.push_back({weight_estimates[i], i});\n    }\n    \n    // Sort by weight (descending)\n    sort(sorted_items.begin(), sorted_items.end(), greater<pair<double, int>>());\n    \n    // Distribute items to minimize variance\n    vector<double> group_weights(D, 0);\n    vector<int> result(N);\n    \n    for (const auto& [weight, item] : sorted_items) {\n        // Find group with minimum current weight\n        int min_group = 0;\n        for (int g = 1; g < D; g++) {\n            if (group_weights[g] < group_weights[min_group]) {\n                min_group = g;\n            }\n        }\n        result[item] = min_group;\n        group_weights[min_group] += weight;\n    }\n    \n    // Output result\n    for (int i = 0; i < N; i++) {\n        cout << result[i];\n        if (i < N - 1) cout << \" \";\n    }\n    cout << endl;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    solve();\n    \n    return 0;\n}","ahc026":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 200;\nconst int M = 10;\nconst int STACK_SIZE = 20;\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    int n, m;\n    cin >> n >> m;\n    \n    vector<deque<int>> stacks(M);\n    vector<int> box_stack(N + 1);      // which stack each box is in\n    vector<int> box_position(N + 1);   // position in stack (0 = bottom)\n    \n    // Read initial configuration\n    for (int i = 0; i < m; i++) {\n        for (int j = 0; j < STACK_SIZE; j++) {\n            int box;\n            cin >> box;\n            stacks[i].push_back(box);\n            box_stack[box] = i;\n            box_position[box] = j;\n        }\n    }\n    \n    vector<pair<int, int>> operations;\n    \n    // Process boxes in order from 1 to 200\n    for (int target = 1; target <= n; target++) {\n        int stack_idx = box_stack[target];\n        int pos_in_stack = box_position[target];\n        int stack_size = stacks[stack_idx].size();\n        \n        // If box is not at the top, we need to move boxes above it\n        if (pos_in_stack != stack_size - 1) {\n            // Determine which boxes need to be moved\n            int boxes_above = stack_size - 1 - pos_in_stack;\n            \n            // Find a destination stack (prefer one that's not full)\n            int dest_stack = -1;\n            for (int s = 0; s < m; s++) {\n                if (s != stack_idx && (int)stacks[s].size() < STACK_SIZE) {\n                    dest_stack = s;\n                    break;\n                }\n            }\n            \n            // If all stacks are full, pick any other stack\n            if (dest_stack == -1) {\n                dest_stack = (stack_idx + 1) % m;\n            }\n            \n            // Move operation: move box 'target' and all above it to dest_stack\n            operations.push_back({target, dest_stack + 1}); // 1-indexed output\n            \n            // Actually perform the move\n            vector<int> moved_boxes;\n            for (int i = pos_in_stack; i < stack_size; i++) {\n                moved_boxes.push_back(stacks[stack_idx][i]);\n            }\n            \n            // Remove from source stack\n            stacks[stack_idx].erase(\n                stacks[stack_idx].begin() + pos_in_stack, \n                stacks[stack_idx].end()\n            );\n            \n            // Add to destination stack\n            for (int box : moved_boxes) {\n                stacks[dest_stack].push_back(box);\n            }\n            \n            // Update positions for all moved boxes\n            for (int i = 0; i < (int)stacks[dest_stack].size(); i++) {\n                box_stack[stacks[dest_stack][i]] = dest_stack;\n                box_position[stacks[dest_stack][i]] = i;\n            }\n        }\n        \n        // Now the target box should be at the top of its stack\n        // Remove it\n        int final_stack = box_stack[target];\n        stacks[final_stack].pop_back();\n        box_stack[target] = -1;\n        operations.push_back({target, 0});\n        \n        // Update positions of remaining boxes in the stack\n        for (int i = 0; i < (int)stacks[final_stack].size(); i++) {\n            box_position[stacks[final_stack][i]] = i;\n        }\n    }\n    \n    // Output operations\n    for (const auto& op : operations) {\n        cout << op.first << \" \" << op.second << \"\\n\";\n    }\n    \n    return 0;\n}","ahc027":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MAXN = 45;\nint N;\nchar h[MAXN][MAXN], v[MAXN][MAXN];\nint d[MAXN][MAXN];\nbool visited[MAXN][MAXN];\nvector<pair<int, int>> tree_edges;\nvector<char> directions = {'R', 'D', 'L', 'U'};\nvector<pair<int, int>> moves = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};\n\n// Build spanning tree using BFS\nvoid build_spanning_tree() {\n    memset(visited, 0, sizeof(visited));\n    queue<pair<int, int>> q;\n    q.push({0, 0});\n    visited[0][0] = true;\n    \n    while (!q.empty()) {\n        auto [x, y] = q.front(); q.pop();\n        \n        for (int dir = 0; dir < 4; dir++) {\n            int nx = x + moves[dir].first;\n            int ny = y + moves[dir].second;\n            \n            if (nx >= 0 && nx < N && ny >= 0 && ny < N && !visited[nx][ny]) {\n                // Check if there's no wall\n                bool can_move = false;\n                if (dir == 0) { // Right\n                    if (ny > 0) can_move = (v[x][min(y, ny-1)] == '0');\n                    else can_move = true;\n                } else if (dir == 1) { // Down\n                    if (nx > 0) can_move = (h[min(x, nx-1)][y] == '0');\n                    else can_move = true;\n                } else if (dir == 2) { // Left\n                    if (y > 0) can_move = (v[x][min(y-1, ny)] == '0');\n                    else can_move = true;\n                } else { // Up\n                    if (x > 0) can_move = (h[min(x-1, nx)][y] == '0');\n                    else can_move = true;\n                }\n                \n                if (can_move || (nx >= 0 && nx < N && ny >= 0 && ny < N)) {\n                    if ((dir == 0 && (y+1 < N) && v[x][y] == '0') ||\n                        (dir == 1 && (x+1 < N) && h[x][y] == '0') ||\n                        (dir == 2 && (y > 0) && v[x][y-1] == '0') ||\n                        (dir == 3 && (x > 0) && h[x-1][y] == '0')) {\n                        visited[nx][ny] = true;\n                        tree_edges.push_back({x*N + y, nx*N + ny});\n                        q.push({nx, ny});\n                    }\n                }\n            }\n        }\n    }\n}\n\n// DFS to create Eulerian tour\nstring result = \"\";\nvoid dfs_tour(int x, int y, int parent_x, int parent_y) {\n    visited[x][y] = true;\n    \n    // Try all directions\n    for (int dir = 0; dir < 4; dir++) {\n        int nx = x + moves[dir].first;\n        int ny = y + moves[dir].second;\n        \n        // Check bounds\n        if (nx < 0 || nx >= N || ny < 0 || ny >= N) continue;\n        \n        // Check walls\n        bool can_move = false;\n        if (dir == 0) { // Right\n            can_move = (v[x][y] == '0');\n        } else if (dir == 1) { // Down\n            can_move = (h[x][y] == '0');\n        } else if (dir == 2) { // Left\n            if (y > 0) can_move = (v[x][y-1] == '0');\n        } else { // Up\n            if (x > 0) can_move = (h[x-1][y] == '0');\n        }\n        \n        if (!can_move) continue;\n        \n        // Skip parent to avoid going back immediately\n        if (nx == parent_x && ny == parent_y) continue;\n        \n        if (!visited[nx][ny]) {\n            result += directions[dir];\n            dfs_tour(nx, ny, x, y);\n            // Return move\n            if (dir == 0) result += 'L';\n            else if (dir == 1) result += 'U';\n            else if (dir == 2) result += 'R';\n            else result += 'D';\n        }\n    }\n}\n\n// Better approach: use BFS to build proper spanning tree\nvoid build_proper_spanning_tree() {\n    memset(visited, 0, sizeof(visited));\n    queue<tuple<int, int, int, int>> q; // x, y, parent_x, parent_y\n    q.push({0, 0, -1, -1});\n    visited[0][0] = true;\n    vector<tuple<int, int, int, int>> edges;\n    \n    while (!q.empty()) {\n        auto [x, y, px, py] = q.front(); q.pop();\n        \n        for (int dir = 0; dir < 4; dir++) {\n            int nx = x + moves[dir].first;\n            int ny = y + moves[dir].second;\n            \n            if (nx >= 0 && nx < N && ny >= 0 && ny < N && !visited[nx][ny]) {\n                // Check if move is possible\n                bool can_move = false;\n                if (dir == 0 && y+1 < N) can_move = (v[x][y] == '0');\n                else if (dir == 1 && x+1 < N) can_move = (h[x][y] == '0');\n                else if (dir == 2 && y > 0) can_move = (v[x][y-1] == '0');\n                else if (dir == 3 && x > 0) can_move = (h[x-1][y] == '0');\n                \n                if (can_move) {\n                    visited[nx][ny] = true;\n                    edges.push_back({x, y, nx, ny});\n                    q.push({nx, ny, x, y});\n                }\n            }\n        }\n    }\n    \n    // Now create tour from edges\n    memset(visited, 0, sizeof(visited));\n    result = \"\";\n    dfs_tour(0, 0, -1, -1);\n}\n\n// Even simpler approach: just do DFS on the grid\nstring simple_dfs() {\n    memset(visited, 0, sizeof(visited));\n    string path = \"\";\n    \n    function<void(int, int)> dfs = [&](int x, int y) {\n        visited[x][y] = true;\n        \n        // Try directions in order that prioritizes higher d values\n        vector<pair<int, int>> next_moves;\n        for (int dir = 0; dir < 4; dir++) {\n            int nx = x + moves[dir].first;\n            int ny = y + moves[dir].second;\n            \n            if (nx >= 0 && nx < N && ny >= 0 && ny < N && !visited[nx][ny]) {\n                // Check walls\n                bool can_move = false;\n                if (dir == 0 && y+1 < N) can_move = (v[x][y] == '0');\n                else if (dir == 1 && x+1 < N) can_move = (h[x][y] == '0');\n                else if (dir == 2 && y > 0) can_move = (v[x][y-1] == '0');\n                else if (dir == 3 && x > 0) can_move = (h[x-1][y] == '0');\n                \n                if (can_move) {\n                    next_moves.push_back({nx, ny});\n                }\n            }\n        }\n        \n        // Sort by dirtiness (descending)\n        sort(next_moves.begin(), next_moves.end(), [&](const pair<int,int>& a, const pair<int,int>& b) {\n            return d[a.first][a.second] > d[b.first][b.second];\n        });\n        \n        for (auto& move : next_moves) {\n            int nx = move.first, ny = move.second;\n            // Find direction\n            int dir = -1;\n            for (int i = 0; i < 4; i++) {\n                if (x + moves[i].first == nx && y + moves[i].second == ny) {\n                    dir = i;\n                    break;\n                }\n            }\n            \n            if (dir != -1) {\n                path += directions[dir];\n                dfs(nx, ny);\n                // Return move\n                if (dir == 0) path += 'L';\n                else if (dir == 1) path += 'U';\n                else if (dir == 2) path += 'R';\n                else path += 'D';\n            }\n        }\n    };\n    \n    dfs(0, 0);\n    return path;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    cin >> N;\n    \n    // Read horizontal walls\n    for (int i = 0; i < N-1; i++) {\n        string line;\n        cin >> line;\n        for (int j = 0; j < N; j++) {\n            h[i][j] = line[j];\n        }\n    }\n    \n    // Read vertical walls\n    for (int i = 0; i < N; i++) {\n        string line;\n        cin >> line;\n        for (int j = 0; j < N-1; j++) {\n            v[i][j] = line[j];\n        }\n    }\n    \n    // Read dirtiness values\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cin >> d[i][j];\n        }\n    }\n    \n    // Generate path using simple DFS approach\n    string result = simple_dfs();\n    \n    // Make sure we don't exceed limit\n    if (result.length() > 100000) {\n        result = result.substr(0, 100000);\n    }\n    \n    cout << result << endl;\n    \n    return 0;\n}","ahc028":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 15;\nconst int M = 200;\nconst int MAX_OPS = 5000;\n\nint grid_size;\nint target_count;\nint start_row, start_col;\nchar grid[N][N];\nvector<string> targets;\nvector<pair<int,int>> char_positions[26]; // positions for each character A-Z\n\n// Precompute positions for each character\nvoid precompute_positions() {\n    for (int i = 0; i < 26; i++) {\n        char_positions[i].clear();\n    }\n    \n    for (int i = 0; i < grid_size; i++) {\n        for (int j = 0; j < grid_size; j++) {\n            int char_idx = grid[i][j] - 'A';\n            char_positions[char_idx].push_back({i, j});\n        }\n    }\n}\n\n// Find the closest position of a character to current position\npair<int, int> find_closest_position(char c, int cur_row, int cur_col) {\n    int char_idx = c - 'A';\n    pair<int, int> best_pos = char_positions[char_idx][0];\n    int best_dist = abs(cur_row - best_pos.first) + abs(cur_col - best_pos.second);\n    \n    for (auto& pos : char_positions[char_idx]) {\n        int dist = abs(cur_row - pos.first) + abs(cur_col - pos.second);\n        if (dist < best_dist) {\n            best_dist = dist;\n            best_pos = pos;\n        }\n    }\n    \n    return best_pos;\n}\n\n// Build superstring using greedy overlap approach\nstring build_superstring() {\n    vector<string> remaining = targets;\n    string result = remaining.back();\n    remaining.pop_back();\n    \n    while (!remaining.empty() && result.length() < 4000) {\n        int best_overlap = -1;\n        int best_idx = -1;\n        bool append = true;\n        \n        // Try to append or prepend each remaining string\n        for (int i = 0; i < (int)remaining.size(); i++) {\n            const string& s = remaining[i];\n            \n            // Check overlap when appending\n            for (int overlap = min((int)s.length() - 1, (int)result.length()); overlap >= 0; overlap--) {\n                if (result.substr(result.length() - overlap) == s.substr(0, overlap)) {\n                    if (overlap > best_overlap) {\n                        best_overlap = overlap;\n                        best_idx = i;\n                        append = true;\n                    }\n                    break;\n                }\n            }\n            \n            // Check overlap when prepending\n            for (int overlap = min((int)s.length() - 1, (int)result.length()); overlap >= 0; overlap--) {\n                if (s.substr(s.length() - overlap) == result.substr(0, overlap)) {\n                    if (overlap > best_overlap) {\n                        best_overlap = overlap;\n                        best_idx = i;\n                        append = false;\n                    }\n                    break;\n                }\n            }\n        }\n        \n        if (best_idx == -1) {\n            // No overlap found, just append the first string\n            result += remaining[0];\n            remaining.erase(remaining.begin());\n        } else {\n            if (append) {\n                result += remaining[best_idx].substr(best_overlap);\n            } else {\n                result = remaining[best_idx] + result.substr(best_overlap);\n            }\n            remaining.erase(remaining.begin() + best_idx);\n        }\n    }\n    \n    return result;\n}\n\n// Generate path for the superstring\nvector<pair<int, int>> generate_path(const string& superstring) {\n    vector<pair<int, int>> path;\n    int cur_row = start_row;\n    int cur_col = start_col;\n    \n    for (char c : superstring) {\n        pair<int, int> next_pos = find_closest_position(c, cur_row, cur_col);\n        path.push_back(next_pos);\n        cur_row = next_pos.first;\n        cur_col = next_pos.second;\n    }\n    \n    return path;\n}\n\n// Optimize path by reducing unnecessary movements\nvoid optimize_path(vector<pair<int, int>>& path) {\n    // Simple optimization: remove consecutive duplicate positions\n    vector<pair<int, int>> optimized;\n    for (const auto& pos : path) {\n        if (optimized.empty() || optimized.back() != pos) {\n            optimized.push_back(pos);\n        }\n    }\n    path = optimized;\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    // Read input\n    cin >> grid_size >> target_count;\n    cin >> start_row >> start_col;\n    \n    for (int i = 0; i < grid_size; i++) {\n        for (int j = 0; j < grid_size; j++) {\n            cin >> grid[i][j];\n        }\n    }\n    \n    targets.resize(target_count);\n    for (int i = 0; i < target_count; i++) {\n        cin >> targets[i];\n    }\n    \n    // Precompute character positions\n    precompute_positions();\n    \n    // Build superstring containing all targets\n    string superstring = build_superstring();\n    \n    // Generate and optimize path\n    vector<pair<int, int>> path = generate_path(superstring);\n    optimize_path(path);\n    \n    // Output path\n    for (const auto& pos : path) {\n        cout << pos.first << \" \" << pos.second << \"\\n\";\n    }\n    \n    return 0;\n}","ahc030":"#include <bits/stdc++.h>\n#include <boost/random.hpp>\n#include <boost/random/normal_distribution.hpp>\n\nusing namespace std;\n\n// Random number generator\nboost::random::mt19937 gen;\n\nstruct Point {\n    int i, j;\n    Point() : i(0), j(0) {}\n    Point(int _i, int _j) : i(_i), j(_j) {}\n    bool operator<(const Point& other) const {\n        if (i != other.i) return i < other.i;\n        return j < other.j;\n    }\n    bool operator==(const Point& other) const {\n        return i == other.i && j == other.j;\n    }\n};\n\nstruct Polyomino {\n    vector<Point> points;\n    int width, height;\n    \n    Polyomino() : width(0), height(0) {}\n    \n    void normalize() {\n        if (points.empty()) return;\n        int min_i = points[0].i, min_j = points[0].j;\n        int max_i = points[0].i, max_j = points[0].j;\n        \n        for (const auto& p : points) {\n            min_i = min(min_i, p.i);\n            min_j = min(min_j, p.j);\n            max_i = max(max_i, p.i);\n            max_j = max(max_j, p.j);\n        }\n        \n        for (auto& p : points) {\n            p.i -= min_i;\n            p.j -= min_j;\n        }\n        \n        width = max_j - min_j + 1;\n        height = max_i - min_i + 1;\n    }\n};\n\nint N, M;\ndouble epsilon;\nvector<Polyomino> fields;\nvector<vector<double>> prob_map;\nvector<vector<bool>> drilled;\nvector<vector<int>> known_values;\nint operations_used = 0;\nconst int MAX_OPERATIONS = 2 * 20 * 20; // 2 * N^2, using max N=20\n\n// Function to query the system\nint query_single(int i, int j) {\n    cout << \"q 1 \" << i << \" \" << j << endl;\n    cout.flush();\n    operations_used++;\n    int response;\n    cin >> response;\n    return response;\n}\n\nint query_group(const vector<Point>& points) {\n    cout << \"q \" << points.size();\n    for (const auto& p : points) {\n        cout << \" \" << p.i << \" \" << p.j;\n    }\n    cout << endl;\n    cout.flush();\n    operations_used++;\n    int response;\n    cin >> response;\n    return response;\n}\n\nbool submit_answer(const set<Point>& oil_squares) {\n    cout << \"a \" << oil_squares.size();\n    for (const auto& p : oil_squares) {\n        cout << \" \" << p.i << \" \" << p.j;\n    }\n    cout << endl;\n    cout.flush();\n    operations_used++;\n    int response;\n    cin >> response;\n    return response == 1;\n}\n\n// Check if a polyomino can be placed at position (di, dj)\nbool can_place(const Polyomino& poly, int di, int dj) {\n    for (const auto& p : poly.points) {\n        int ni = p.i + di;\n        int nj = p.j + dj;\n        if (ni < 0 || ni >= N || nj < 0 || nj >= N) {\n            return false;\n        }\n    }\n    return true;\n}\n\n// Get all possible positions for a polyomino\nvector<pair<int, int>> get_possible_positions(const Polyomino& poly) {\n    vector<pair<int, int>> positions;\n    for (int i = 0; i <= N - poly.height; i++) {\n        for (int j = 0; j <= N - poly.width; j++) {\n            if (can_place(poly, i, j)) {\n                positions.push_back({i, j});\n            }\n        }\n    }\n    return positions;\n}\n\n// Update probability map based on a group query result\nvoid update_probabilities(const vector<Point>& group, int result) {\n    double group_size = group.size();\n    double expected = result;\n    \n    // Simple update - distribute the result evenly among group members\n    for (const auto& p : group) {\n        prob_map[p.i][p.j] += expected / group_size;\n    }\n}\n\n// Get unexplored points for group querying\nvector<Point> get_unexplored_group(int size) {\n    vector<Point> candidates;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (!drilled[i][j]) {\n                candidates.push_back(Point(i, j));\n            }\n        }\n    }\n    \n    // Shuffle and take first 'size' elements\n    shuffle(candidates.begin(), candidates.end(), gen);\n    \n    vector<Point> result;\n    for (int i = 0; i < min(size, (int)candidates.size()); i++) {\n        result.push_back(candidates[i]);\n    }\n    \n    return result;\n}\n\n// Get high probability points that haven't been drilled\nset<Point> get_high_prob_points(double threshold) {\n    set<Point> result;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (!drilled[i][j] && prob_map[i][j] > threshold) {\n                result.insert(Point(i, j));\n            }\n        }\n    }\n    return result;\n}\n\n// Get all points we know have oil\nset<Point> get_known_oil_points() {\n    set<Point> result;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (drilled[i][j] && known_values[i][j] > 0) {\n                result.insert(Point(i, j));\n            }\n        }\n    }\n    return result;\n}\n\nint main() {\n    gen.seed(time(0));\n    \n    // Read input\n    cin >> N >> M >> epsilon;\n    fields.resize(M);\n    \n    for (int i = 0; i < M; i++) {\n        int d;\n        cin >> d;\n        fields[i].points.resize(d);\n        for (int j = 0; j < d; j++) {\n            cin >> fields[i].points[j].i >> fields[i].points[j].j;\n        }\n        fields[i].normalize();\n    }\n    \n    // Initialize data structures\n    prob_map.assign(N, vector<double>(N, 0.0));\n    drilled.assign(N, vector<bool>(N, false));\n    known_values.assign(N, vector<int>(N, 0));\n    \n    // Initial exploration phase - use group queries\n    int group_queries = min(50, MAX_OPERATIONS / 4);\n    for (int q = 0; q < group_queries && operations_used < MAX_OPERATIONS - N*N/2; q++) {\n        int group_size = min(20, max(5, N*N/20)); // Adaptive group size\n        auto group = get_unexplored_group(group_size);\n        \n        if (group.size() >= 2) {\n            int result = query_group(group);\n            update_probabilities(group, result);\n        }\n    }\n    \n    // Drilling phase - drill high probability squares\n    vector<pair<double, Point>> prob_list;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (!drilled[i][j]) {\n                prob_list.push_back({prob_map[i][j], Point(i, j)});\n            }\n        }\n    }\n    \n    sort(prob_list.rbegin(), prob_list.rend()); // Sort by probability descending\n    \n    int drill_attempts = min(100, MAX_OPERATIONS - operations_used - N*N/4);\n    for (int i = 0; i < min(drill_attempts, (int)prob_list.size()); i++) {\n        Point p = prob_list[i].second;\n        int result = query_single(p.i, p.j);\n        drilled[p.i][p.j] = true;\n        known_values[p.i][p.j] = result;\n        \n        // Update probabilities around this point\n        for (int di = -2; di <= 2; di++) {\n            for (int dj = -2; dj <= 2; dj++) {\n                int ni = p.i + di;\n                int nj = p.j + dj;\n                if (ni >= 0 && ni < N && nj >= 0 && nj < N) {\n                    if (result > 0) {\n                        prob_map[ni][nj] += 1.0 / (1 + abs(di) + abs(dj)); // Proximity bonus\n                    } else {\n                        prob_map[ni][nj] *= 0.8; // Reduce probability if no oil found nearby\n                    }\n                }\n            }\n        }\n    }\n    \n    // Final phase - collect all points that likely have oil\n    set<Point> answer = get_known_oil_points();\n    \n    // Add high probability points\n    auto high_prob = get_high_prob_points(0.5);\n    answer.insert(high_prob.begin(), high_prob.end());\n    \n    // Make sure we're not exceeding operation limits\n    if (operations_used < MAX_OPERATIONS) {\n        bool success = submit_answer(answer);\n        \n        // If failed and still have operations, try again with different threshold\n        if (!success && operations_used < MAX_OPERATIONS - 1) {\n            answer = get_known_oil_points();\n            auto medium_prob = get_high_prob_points(0.3);\n            answer.insert(medium_prob.begin(), medium_prob.end());\n            submit_answer(answer);\n        }\n    }\n    \n    return 0;\n}","ahc031":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int W = 1000;\n\nstruct Rectangle {\n    int i0, j0, i1, j1;\n    Rectangle() : i0(0), j0(0), i1(0), j1(0) {}\n    Rectangle(int _i0, int _j0, int _i1, int _j1) : i0(_i0), j0(_j0), i1(_i1), j1(_j1) {}\n    int area() const { return (i1 - i0) * (j1 - j0); }\n    bool valid() const { return i0 >= 0 && j0 >= 0 && i1 <= W && j1 <= W && i0 < i1 && j0 < j1; }\n};\n\n// Calculate partition cost between two days\nint calculate_partition_cost(const vector<Rectangle>& prev, const vector<Rectangle>& curr) {\n    // Simplified: just count rectangle changes\n    // In practice, we'd need to track actual partition lines\n    int cost = 0;\n    for (size_t i = 0; i < min(prev.size(), curr.size()); ++i) {\n        if (prev[i].i0 != curr[i].i0 || prev[i].j0 != curr[i].j0 || \n            prev[i].i1 != curr[i].i1 || prev[i].j1 != curr[i].j1) {\n            cost += 10; // penalty for moving rectangles\n        }\n    }\n    return cost;\n}\n\n// Calculate area shortage cost\nint calculate_area_cost(const vector<int>& required, const vector<Rectangle>& rectangles) {\n    int cost = 0;\n    for (size_t i = 0; i < required.size(); ++i) {\n        int shortage = max(0, required[i] - rectangles[i].area());\n        cost += 100 * shortage;\n    }\n    return cost;\n}\n\n// Simple strip packing algorithm\nvector<Rectangle> pack_rectangles_simple(const vector<int>& required) {\n    vector<Rectangle> rectangles(required.size());\n    int current_row = 0;\n    \n    for (size_t i = 0; i < required.size(); ++i) {\n        int area_needed = required[i];\n        \n        // Try to make rectangle as square as possible\n        int width = min(W, (int)sqrt(area_needed) + 1);\n        while (width > 0 && area_needed % width != 0) width--;\n        if (width == 0) width = 1;\n        \n        int height = (area_needed + width - 1) / width;\n        \n        // Adjust to fit in grid\n        if (width > W) {\n            width = W;\n            height = (area_needed + W - 1) / W;\n        }\n        if (height > W) {\n            height = W;\n            width = (area_needed + W - 1) / W;\n        }\n        \n        // Position the rectangle\n        if (current_row + height <= W) {\n            rectangles[i] = Rectangle(current_row, 0, current_row + height, width);\n            current_row += height;\n        } else {\n            // If we run out of space, just place it somewhere\n            rectangles[i] = Rectangle(0, 0, min(height, W), min(width, W));\n        }\n    }\n    \n    return rectangles;\n}\n\n// Improved packing with better area satisfaction\nvector<Rectangle> pack_rectangles_improved(const vector<int>& required) {\n    vector<Rectangle> rectangles(required.size());\n    \n    // Sort rectangles by required area (descending) for better packing\n    vector<pair<int, int>> area_index;\n    for (size_t i = 0; i < required.size(); ++i) {\n        area_index.push_back({required[i], i});\n    }\n    sort(area_index.rbegin(), area_index.rend());\n    \n    // Use a simple grid-based approach\n    int grid_size = (int)sqrt(required.size()) + 1;\n    int cell_width = W / grid_size;\n    int cell_height = W / grid_size;\n    \n    vector<vector<bool>> occupied(grid_size, vector<bool>(grid_size, false));\n    \n    for (const auto& [area, idx] : area_index) {\n        int area_needed = area;\n        \n        // Try to make rectangle fit requirements\n        int width = min(W, (int)sqrt(area_needed) + 50);  // Add buffer\n        int height = (area_needed + width - 1) / width;\n        \n        if (width > W) {\n            width = W;\n            height = (area_needed + W - 1) / W + 1;\n        }\n        if (height > W) {\n            height = W;\n            width = (area_needed + W - 1) / W + 1;\n        }\n        \n        // Ensure minimum area\n        if (width * height < area_needed) {\n            int diff = area_needed - width * height;\n            width += (diff + height - 1) / height;\n            if (width > W) width = W;\n            height += (diff + width - 1) / width;\n            if (height > W) height = W;\n        }\n        \n        // Place rectangle\n        bool placed = false;\n        for (int x = 0; x <= W - width && !placed; ++x) {\n            for (int y = 0; y <= W - height && !placed; ++y) {\n                rectangles[idx] = Rectangle(x, y, x + height, y + width);\n                if (rectangles[idx].valid()) {\n                    placed = true;\n                    break;\n                }\n            }\n        }\n        \n        // Fallback if not placed properly\n        if (!placed || rectangles[idx].area() < area_needed) {\n            int side = max(1, (int)sqrt(area_needed));\n            rectangles[idx] = Rectangle(0, 0, min(side, W), min((area_needed + side - 1) / side, W));\n        }\n    }\n    \n    return rectangles;\n}\n\n// Optimize rectangles to reduce area shortage\nvector<Rectangle> optimize_rectangles(vector<Rectangle> rectangles, const vector<int>& required) {\n    for (size_t i = 0; i < rectangles.size(); ++i) {\n        int current_area = rectangles[i].area();\n        int required_area = required[i];\n        \n        if (current_area < required_area) {\n            // Expand rectangle to meet requirement\n            int shortage = required_area - current_area;\n            \n            int width = rectangles[i].j1 - rectangles[i].j0;\n            int height = rectangles[i].i1 - rectangles[i].i0;\n            \n            // Try to expand width first\n            if (rectangles[i].j1 < W) {\n                int expand_w = min(shortage / height + 1, W - rectangles[i].j1);\n                rectangles[i].j1 = min(W, rectangles[i].j1 + expand_w);\n            }\n            // Then try to expand height\n            else if (rectangles[i].i1 < W) {\n                int expand_h = min(shortage / width + 1, W - rectangles[i].i1);\n                rectangles[i].i1 = min(W, rectangles[i].i1 + expand_h);\n            }\n        }\n    }\n    return rectangles;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int W_input, D, N;\n    cin >> W_input >> D >> N;\n    \n    vector<vector<int>> required(D, vector<int>(N));\n    for (int d = 0; d < D; ++d) {\n        for (int k = 0; k < N; ++k) {\n            cin >> required[d][k];\n        }\n    }\n    \n    vector<vector<Rectangle>> solutions(D);\n    \n    // Day 0: Generate initial solution\n    solutions[0] = pack_rectangles_improved(required[0]);\n    solutions[0] = optimize_rectangles(solutions[0], required[0]);\n    \n    // Days 1 to D-1: Try to maintain consistency to reduce partition costs\n    for (int d = 1; d < D; ++d) {\n        // Start with previous day's solution\n        solutions[d] = solutions[d-1];\n        \n        // Adjust to meet new requirements\n        solutions[d] = optimize_rectangles(solutions[d], required[d]);\n        \n        // If still not good enough, generate fresh solution\n        bool need_fresh = false;\n        for (int k = 0; k < N; ++k) {\n            if (solutions[d][k].area() < required[d][k]) {\n                need_fresh = true;\n                break;\n            }\n        }\n        \n        if (need_fresh) {\n            solutions[d] = pack_rectangles_improved(required[d]);\n            solutions[d] = optimize_rectangles(solutions[d], required[d]);\n        }\n    }\n    \n    // Output results\n    for (int d = 0; d < D; ++d) {\n        for (int k = 0; k < N; ++k) {\n            cout << solutions[d][k].i0 << \" \" << solutions[d][k].j0 << \" \" \n                 << solutions[d][k].i1 << \" \" << solutions[d][k].j1 << \"\\n\";\n        }\n    }\n    \n    return 0;\n}","ahc032":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 9;\nconst int M = 20;\nconst int K = 81;\nconst long long MOD = 998244353;\n\nlong long a[N][N];\nlong long s[M][3][3];\n\nstruct Operation {\n    int stamp, p, q;\n};\n\n// Calculate current score of board\nlong long calculate_score(long long board[N][N]) {\n    long long score = 0;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            score = (score + (board[i][j] % MOD + MOD) % MOD) % MOD;\n        }\n    }\n    return score;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    // Read input\n    int dummy_N, dummy_M, dummy_K;\n    cin >> dummy_N >> dummy_M >> dummy_K;\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    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 board\n    long long board[N][N];\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            board[i][j] = a[i][j];\n        }\n    }\n    \n    vector<Operation> operations;\n    \n    // Greedy selection\n    for (int k = 0; k < K; k++) {\n        Operation best_op = {-1, -1, -1};\n        long long best_gain = -1;\n        long long current_score = calculate_score(board);\n        \n        // Try all possible stamp placements\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                    // Create temporary board\n                    long long temp_board[N][N];\n                    for (int i = 0; i < N; i++) {\n                        for (int j = 0; j < N; j++) {\n                            temp_board[i][j] = board[i][j];\n                        }\n                    }\n                    \n                    // Apply stamp\n                    for (int i = 0; i < 3; i++) {\n                        for (int j = 0; j < 3; j++) {\n                            temp_board[p + i][q + j] += s[m][i][j];\n                        }\n                    }\n                    \n                    long long new_score = calculate_score(temp_board);\n                    long long gain = new_score - current_score;\n                    \n                    if (gain > best_gain) {\n                        best_gain = gain;\n                        best_op = {m, p, q};\n                    }\n                }\n            }\n        }\n        \n        // If no positive gain, stop\n        if (best_gain <= 0) {\n            break;\n        }\n        \n        // Apply the best operation\n        operations.push_back(best_op);\n        \n        for (int i = 0; i < 3; i++) {\n            for (int j = 0; j < 3; j++) {\n                board[best_op.p + i][best_op.q + j] += s[best_op.stamp][i][j];\n            }\n        }\n    }\n    \n    // Output\n    cout << operations.size() << \"\\n\";\n    for (const auto& op : operations) {\n        cout << op.stamp << \" \" << op.p << \" \" << op.q << \"\\n\";\n    }\n    \n    return 0;\n}","ahc033":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 5;\nconst int MAX_TURNS = 10000;\n\n// Container information\nint A[N][N];\nint container_to_gate[N*N]; // which gate each container should go to\n\n// Crane positions and states\nstruct Crane {\n    int row, col;\n    bool holding;\n    int container;\n    bool bombed;\n};\n\n// Grid state\nint grid[N][N]; // -1 = empty, >= 0 = container number\nCrane cranes[N];\n\n// Output actions\nvector<string> actions(N);\n\n// Initialize problem\nvoid init() {\n    // Calculate which gate each container should go to\n    for (int i = 0; i < N*N; i++) {\n        container_to_gate[i] = i / N;\n    }\n    \n    // Initialize grid\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            grid[i][j] = -1;\n        }\n    }\n    \n    // Initialize cranes\n    for (int i = 0; i < N; i++) {\n        cranes[i] = {i, 0, false, -1, false};\n        grid[i][0] = A[i][0]; // First container is already placed\n    }\n}\n\n// Check if position is valid\nbool is_valid(int r, int c) {\n    return r >= 0 && r < N && c >= 0 && c < N;\n}\n\n// Check if crane can move to position\nbool can_move_to(int crane_id, int r, int c) {\n    if (!is_valid(r, c)) return false;\n    \n    Crane& crane = cranes[crane_id];\n    \n    // Large crane (id=0) can move through occupied squares even when holding\n    if (crane_id == 0) {\n        return true;\n    }\n    \n    // Small cranes cannot move to occupied squares when holding container\n    if (crane.holding && grid[r][c] != -1) {\n        return false;\n    }\n    \n    return true;\n}\n\n// Find path for crane to target position\nvector<pair<int, int>> find_path(int start_r, int start_c, int target_r, int target_c) {\n    // Simple pathfinding - move horizontally then vertically\n    vector<pair<int, int>> path;\n    int r = start_r, c = start_c;\n    \n    // Move horizontally first\n    while (c < target_c) {\n        c++;\n        path.push_back({r, c});\n    }\n    while (c > target_c) {\n        c--;\n        path.push_back({r, c});\n    }\n    \n    // Then vertically\n    while (r < target_r) {\n        r++;\n        path.push_back({r, c});\n    }\n    while (r > target_r) {\n        r--;\n        path.push_back({r, c});\n    }\n    \n    return path;\n}\n\n// Get action character for movement\nchar get_move_action(int from_r, int from_c, int to_r, int to_c) {\n    if (to_r == from_r - 1) return 'U';\n    if (to_r == from_r + 1) return 'D';\n    if (to_c == from_c - 1) return 'L';\n    if (to_c == from_c + 1) return 'R';\n    return '.';\n}\n\n// Main simulation logic\nvoid simulate() {\n    int turn = 0;\n    int received[N] = {0}; // Count of containers received by each gate\n    bool dispatched[N*N] = {false}; // Track which containers have been dispatched\n    \n    while (turn < MAX_TURNS) {\n        bool all_done = true;\n        for (int i = 0; i < N*N; i++) {\n            if (!dispatched[i]) {\n                all_done = false;\n                break;\n            }\n        }\n        if (all_done) break;\n        \n        // Step 1: Place new containers at receiving gates\n        for (int i = 0; i < N; i++) {\n            if (received[i] < N && grid[i][0] == -1) {\n                grid[i][0] = A[i][received[i]];\n                received[i]++;\n            }\n        }\n        \n        // Step 2: Plan crane actions (simplified greedy approach)\n        vector<char> turn_actions(N, '.');\n        \n        // Prioritize dispatching containers that are already at dispatch gates\n        for (int i = 0; i < N; i++) {\n            if (grid[i][N-1] != -1) {\n                int container = grid[i][N-1];\n                if (container / N == i) { // Correct gate\n                    turn_actions[i] = '.'; // Do nothing, container will be dispatched\n                    dispatched[container] = true;\n                    grid[i][N-1] = -1;\n                } else {\n                    // Wrong gate, need to move it\n                    // For simplicity, we'll just leave it for now\n                    turn_actions[i] = '.';\n                }\n            }\n        }\n        \n        // Move containers toward their target gates\n        // Use greedy approach: move containers right when possible\n        for (int i = 0; i < N; i++) {\n            if (turn_actions[i] != '.') continue; // Already decided\n            \n            Crane& crane = cranes[i];\n            \n            // If crane is at dispatch gate with wrong container, try to move it\n            if (crane.col == N-1 && grid[crane.row][crane.col] != -1) {\n                int container = grid[crane.row][crane.col];\n                if (container / N != crane.row) {\n                    // Wrong container at dispatch gate\n                    if (!crane.holding) {\n                        turn_actions[i] = 'P'; // Pick it up\n                        crane.holding = true;\n                        crane.container = container;\n                        grid[crane.row][crane.col] = -1;\n                    } else {\n                        // Try to move up or down to get out of the way\n                        if (crane.row > 0 && grid[crane.row-1][crane.col] == -1) {\n                            turn_actions[i] = 'U';\n                            crane.row--;\n                        } else if (crane.row < N-1 && grid[crane.row+1][crane.col] == -1) {\n                            turn_actions[i] = 'D';\n                            crane.row++;\n                        } else {\n                            turn_actions[i] = '.';\n                        }\n                    }\n                } else {\n                    turn_actions[i] = '.';\n                }\n                continue;\n            }\n            \n            // If crane is holding a container, move toward correct row\n            if (crane.holding) {\n                int target_gate = crane.container / N;\n                if (crane.row < target_gate) {\n                    if (can_move_to(i, crane.row+1, crane.col)) {\n                        turn_actions[i] = 'D';\n                        crane.row++;\n                    } else {\n                        turn_actions[i] = '.';\n                    }\n                } else if (crane.row > target_gate) {\n                    if (can_move_to(i, crane.row-1, crane.col)) {\n                        turn_actions[i] = 'U';\n                        crane.row--;\n                    } else {\n                        turn_actions[i] = '.';\n                    }\n                } else {\n                    // Correct row, move right if possible\n                    if (crane.col < N-1) {\n                        if (can_move_to(i, crane.row, crane.col+1)) {\n                            turn_actions[i] = 'R';\n                            crane.col++;\n                        } else {\n                            turn_actions[i] = '.';\n                        }\n                    } else {\n                        // At dispatch gate, release\n                        turn_actions[i] = 'Q';\n                        crane.holding = false;\n                        grid[crane.row][crane.col] = crane.container;\n                        dispatched[crane.container] = true;\n                        crane.container = -1;\n                    }\n                }\n            } else {\n                // Not holding container, look for work\n                if (grid[crane.row][crane.col] != -1) {\n                    int container = grid[crane.row][crane.col];\n                    // Pick up if it's not at the right gate or needs to be moved\n                    if (crane.col < N-1 || (crane.col == N-1 && container / N != crane.row)) {\n                        turn_actions[i] = 'P';\n                        crane.holding = true;\n                        crane.container = container;\n                        grid[crane.row][crane.col] = -1;\n                    } else {\n                        turn_actions[i] = '.';\n                    }\n                } else {\n                    // Move to find work\n                    if (crane.col < N-1) {\n                        if (can_move_to(i, crane.row, crane.col+1)) {\n                            turn_actions[i] = 'R';\n                            crane.col++;\n                        } else {\n                            turn_actions[i] = '.';\n                        }\n                    } else {\n                        turn_actions[i] = '.';\n                    }\n                }\n            }\n        }\n        \n        // Apply actions\n        for (int i = 0; i < N; i++) {\n            actions[i] += turn_actions[i];\n        }\n        \n        turn++;\n    }\n    \n    // Pad all action strings to same length\n    int max_len = 0;\n    for (int i = 0; i < N; i++) {\n        max_len = max(max_len, (int)actions[i].length());\n    }\n    \n    for (int i = 0; i < N; i++) {\n        while ((int)actions[i].length() < max_len) {\n            actions[i] += '.';\n        }\n    }\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    // Read input\n    cin >> 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\n    init();\n    \n    // Run simulation\n    simulate();\n    \n    // Output results\n    for (int i = 0; i < N; i++) {\n        cout << actions[i] << \"\\n\";\n    }\n    \n    return 0;\n}","ahc034":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 20;\nconst int INF = 1e9;\n\nint grid[N][N];\nint final_grid[N][N];\nvector<pair<int, int>> pos_cells, neg_cells;\nvector<pair<pair<int, int>, pair<int, int>>> transport_pairs;\n\n// Directions: U, D, L, R\nconst int dx[4] = {-1, 1, 0, 0};\nconst int dy[4] = {0, 0, -1, 1};\nconst char dir_chars[4] = {'U', 'D', 'L', 'R'};\n\nstruct State {\n    int x, y, dist;\n    vector<pair<int, int>> path;\n};\n\n// BFS to find shortest path between two points\nvector<pair<int, int>> find_path(int sx, int sy, int tx, int ty) {\n    if (sx == tx && sy == ty) return {};\n    \n    bool visited[N][N];\n    pair<int, int> parent[N][N];\n    memset(visited, 0, sizeof(visited));\n    \n    queue<pair<int, int>> q;\n    q.push({sx, sy});\n    visited[sx][sy] = true;\n    parent[sx][sy] = {-1, -1};\n    \n    while (!q.empty()) {\n        auto [x, y] = q.front(); q.pop();\n        \n        for (int i = 0; i < 4; i++) {\n            int nx = x + dx[i];\n            int ny = y + dy[i];\n            \n            if (nx >= 0 && nx < N && ny >= 0 && ny < N && !visited[nx][ny]) {\n                visited[nx][ny] = true;\n                parent[nx][ny] = {x, y};\n                if (nx == tx && ny == ty) {\n                    // Reconstruct path\n                    vector<pair<int, int>> path;\n                    int cx = tx, cy = ty;\n                    while (parent[cx][cy].first != -1) {\n                        path.push_back({cx, cy});\n                        auto p = parent[cx][cy];\n                        cx = p.first;\n                        cy = p.second;\n                    }\n                    reverse(path.begin(), path.end());\n                    return path;\n                }\n                q.push({nx, ny});\n            }\n        }\n    }\n    \n    return {}; // Should not happen\n}\n\n// Convert path to directions\nvector<char> path_to_directions(int sx, int sy, const vector<pair<int, int>>& path) {\n    vector<char> directions;\n    int cx = sx, cy = sy;\n    \n    for (auto [nx, ny] : path) {\n        if (nx == cx - 1 && ny == cy) directions.push_back('U');\n        else if (nx == cx + 1 && ny == cy) directions.push_back('D');\n        else if (nx == cx && ny == cy - 1) directions.push_back('L');\n        else if (nx == cx && ny == cy + 1) directions.push_back('R');\n        cx = nx;\n        cy = ny;\n    }\n    \n    return directions;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    cin >> N;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cin >> grid[i][j];\n            final_grid[i][j] = grid[i][j];\n        }\n    }\n    \n    // Create list of sources and sinks\n    vector<tuple<int, int, int>> sources, sinks; // {x, y, amount}\n    \n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (grid[i][j] > 0) {\n                sources.push_back({i, j, grid[i][j]});\n            } else if (grid[i][j] < 0) {\n                sinks.push_back({i, j, -grid[i][j]});\n            }\n        }\n    }\n    \n    vector<string> operations;\n    int truck_x = 0, truck_y = 0;\n    int load = 0;\n    \n    // Greedy matching: match sources to sinks\n    int source_idx = 0, sink_idx = 0;\n    \n    while (source_idx < sources.size() || sink_idx < sinks.size()) {\n        // Find next source that has remaining soil\n        while (source_idx < sources.size() && get<2>(sources[source_idx]) == 0) {\n            source_idx++;\n        }\n        \n        if (source_idx >= sources.size()) break;\n        \n        auto& source = sources[source_idx];\n        int sx = get<0>(source), sy = get<1>(source);\n        int& source_amount = get<2>(source);\n        \n        // Move to source\n        if (truck_x != sx || truck_y != sy) {\n            auto path = find_path(truck_x, truck_y, sx, sy);\n            auto directions = path_to_directions(truck_x, truck_y, path);\n            \n            for (char dir : directions) {\n                operations.push_back(string(1, dir));\n            }\n            truck_x = sx;\n            truck_y = sy;\n        }\n        \n        // Load soil\n        int load_amount = min(source_amount, 1000000);\n        operations.push_back(\"+\" + to_string(load_amount));\n        load += load_amount;\n        source_amount -= load_amount;\n        \n        // Now find a sink to unload\n        while (sink_idx < sinks.size() && get<2>(sinks[sink_idx]) == 0) {\n            sink_idx++;\n        }\n        \n        if (sink_idx >= sinks.size()) {\n            // If no sinks left but we have soil, we need to find the closest sink\n            int best_sink = -1;\n            int min_dist = INF;\n            for (int i = 0; i < sinks.size(); i++) {\n                if (get<2>(sinks[i]) > 0) {\n                    int dist = abs(truck_x - get<0>(sinks[i])) + abs(truck_y - get<1>(sinks[i]));\n                    if (dist < min_dist) {\n                        min_dist = dist;\n                        best_sink = i;\n                    }\n                }\n            }\n            if (best_sink != -1) sink_idx = best_sink;\n            else break;\n        }\n        \n        auto& sink = sinks[sink_idx];\n        int tx = get<0>(sink), ty = get<1>(sink);\n        int& sink_amount = get<2>(sink);\n        \n        // Move to sink\n        if (truck_x != tx || truck_y != ty) {\n            auto path = find_path(truck_x, truck_y, tx, ty);\n            auto directions = path_to_directions(truck_x, truck_y, path);\n            \n            for (char dir : directions) {\n                operations.push_back(string(1, dir));\n            }\n            truck_x = tx;\n            truck_y = ty;\n        }\n        \n        // Unload soil\n        int unload_amount = min({load, sink_amount, 1000000});\n        operations.push_back(\"-\" + to_string(unload_amount));\n        load -= unload_amount;\n        sink_amount -= unload_amount;\n        \n        // If source is depleted, move to next source\n        if (source_amount == 0) {\n            source_idx++;\n        }\n    }\n    \n    // Output operations\n    for (const string& op : operations) {\n        cout << op << \"\\n\";\n    }\n    \n    return 0;\n}","ahc035":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <numeric>\n\nusing namespace std;\n\nconst int N = 6;\nconst int SEED_COUNT = 2 * N * (N - 1); // 60 seeds\n\n// Calculate total value of a seed\nint calculateValue(const vector<int>& seed) {\n    return accumulate(seed.begin(), seed.end(), 0);\n}\n\n// Generate placement strategy\nvector<vector<int>> generatePlacement(const vector<vector<int>>& seeds) {\n    vector<pair<int, int>> seedValues;\n    for (int i = 0; i < seeds.size(); i++) {\n        seedValues.push_back({calculateValue(seeds[i]), i});\n    }\n    \n    // Sort by value descending\n    sort(seedValues.begin(), seedValues.end(), greater<pair<int, int>>());\n    \n    vector<vector<int>> placement(N, vector<int>(N));\n    vector<bool> used(SEED_COUNT, false);\n    \n    // Define positions in order of importance (center first)\n    vector<pair<int, int>> positions = {\n        // Center positions (most neighbors)\n        {2, 2}, {2, 3}, {3, 2}, {3, 3},\n        // Next tier\n        {1, 2}, {1, 3}, {2, 1}, {3, 1}, {2, 4}, {3, 4}, {4, 2}, {4, 3},\n        // Edges\n        {0, 2}, {0, 3}, {2, 0}, {3, 0}, {2, 5}, {3, 5}, {5, 2}, {5, 3},\n        // Diagonals to center\n        {1, 1}, {1, 4}, {4, 1}, {4, 4},\n        // Edge corners\n        {0, 1}, {0, 4}, {1, 0}, {4, 0}, {1, 5}, {4, 5}, {5, 1}, {5, 4},\n        // Corners\n        {0, 0}, {0, 5}, {5, 0}, {5, 5}\n    };\n    \n    // Assign best seeds to best positions\n    for (int i = 0; i < min((int)positions.size(), SEED_COUNT); i++) {\n        int seedIdx = seedValues[i].second;\n        auto pos = positions[i];\n        placement[pos.first][pos.second] = seedIdx;\n        used[seedIdx] = true;\n    }\n    \n    // Fill remaining positions with unused seeds\n    int nextSeed = 0;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (placement[i][j] == 0 && (i != 0 || j != 0)) { // If not set (but not the default 0,0)\n                while (nextSeed < SEED_COUNT && used[nextSeed]) nextSeed++;\n                if (nextSeed < SEED_COUNT) {\n                    placement[i][j] = nextSeed;\n                    used[nextSeed] = true;\n                }\n            }\n        }\n    }\n    \n    // Final check to ensure all positions have valid seeds\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (placement[i][j] >= SEED_COUNT) {\n                int k = 0;\n                while (k < SEED_COUNT && used[k]) k++;\n                if (k < SEED_COUNT) {\n                    placement[i][j] = k;\n                    used[k] = true;\n                }\n            }\n        }\n    }\n    \n    return placement;\n}\n\nint main() {\n    int N_input, M, T;\n    cin >> N_input >> M >> T;\n    \n    vector<vector<int>> seeds(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 >> seeds[i][j];\n        }\n    }\n    \n    // Process T turns\n    for (int turn = 0; turn < T; turn++) {\n        // Generate placement\n        auto placement = generatePlacement(seeds);\n        \n        // Output placement\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                cout << placement[i][j];\n                if (j < N - 1) cout << \" \";\n            }\n            cout << endl;\n        }\n        cout.flush();\n        \n        // Read new seeds for next iteration\n        if (turn < T - 1) {\n            for (int i = 0; i < SEED_COUNT; i++) {\n                for (int j = 0; j < M; j++) {\n                    cin >> seeds[i][j];\n                }\n            }\n        }\n    }\n    \n    return 0;\n}","ahc038":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MAX_N = 30;\nconst int MAX_V = 15;\nconst int INF = 1e9;\n\nstruct Point {\n    int x, y;\n    Point() : x(0), y(0) {}\n    Point(int x, int y) : x(x), y(y) {}\n    Point operator+(const Point& p) const { return Point(x + p.x, y + p.y); }\n    Point operator-(const Point& p) const { return Point(x - p.x, y - p.y); }\n    bool operator==(const Point& p) const { return x == p.x && y == p.y; }\n    bool operator!=(const Point& p) const { return !(*this == p); }\n};\n\nconst Point DIRS[4] = {Point(0, 1), Point(1, 0), Point(0, -1), Point(-1, 0)}; // R, D, L, U\nconst char DIR_CHARS[4] = {'R', 'D', 'L', 'U'};\n\nclass RobotArm {\npublic:\n    int V;\n    vector<pair<int, int>> tree; // {parent, length}\n    Point root_pos;\n    \n    RobotArm(int v) : V(v) {\n        tree.resize(V);\n    }\n};\n\nclass Solution {\nprivate:\n    int N, M, V;\n    vector<vector<int>> initial, target;\n    vector<Point> initial_pos, target_pos;\n    \npublic:\n    void readInput() {\n        cin >> N >> M >> V;\n        initial.assign(N, vector<int>(N, 0));\n        target.assign(N, vector<int>(N, 0));\n        \n        for (int i = 0; i < N; i++) {\n            string line;\n            cin >> line;\n            for (int j = 0; j < N; j++) {\n                initial[i][j] = line[j] - '0';\n                if (initial[i][j]) {\n                    initial_pos.push_back(Point(i, j));\n                }\n            }\n        }\n        \n        for (int i = 0; i < N; i++) {\n            string line;\n            cin >> line;\n            for (int j = 0; j < N; j++) {\n                target[i][j] = line[j] - '0';\n                if (target[i][j]) {\n                    target_pos.push_back(Point(i, j));\n                }\n            }\n        }\n    }\n    \n    RobotArm designArm() {\n        RobotArm arm(V);\n        \n        // Create a star-like structure with root at center\n        // Root is vertex 0, others are connected directly to root\n        for (int i = 1; i < V; i++) {\n            arm.tree[i] = {0, min(3, N-1)}; // Parent is 0, length 3\n        }\n        \n        // Initial position at center of grid\n        arm.root_pos = Point(N/2, N/2);\n        \n        return arm;\n    }\n    \n    string createMoveString(char move, const vector<char>& rotations, const vector<char>& actions) {\n        string result;\n        result += move;\n        for (int i = 1; i < rotations.size(); i++) {\n            result += rotations[i];\n        }\n        for (int i = 0; i < actions.size(); i++) {\n            result += actions[i];\n        }\n        return result;\n    }\n    \n    vector<string> solve(RobotArm& arm) {\n        vector<string> operations;\n        Point current_pos = arm.root_pos;\n        \n        // Simple greedy approach - move towards takoyaki and grab them\n        int turns = 0;\n        int max_turns = min(1000, (int)(1e5));\n        \n        // Track which takoyaki are taken and which targets are filled\n        vector<bool> taken(initial_pos.size(), false);\n        vector<bool> filled(target_pos.size(), false);\n        \n        // Directions for each finger (assuming 4 fingers pointing in different directions)\n        vector<Point> finger_dirs = {Point(0, 1), Point(1, 0), Point(0, -1), Point(-1, 0)};\n        \n        // Extend finger directions if we have more vertices\n        while (finger_dirs.size() < V - 1) {\n            finger_dirs.push_back(finger_dirs[finger_dirs.size() - 4]);\n        }\n        \n        random_device rd;\n        mt19937 gen(rd());\n        uniform_int_distribution<> dis(0, 3);\n        \n        while (turns < max_turns) {\n            // Random movement for exploration\n            int dir = dis(gen);\n            Point new_pos = current_pos + DIRS[dir];\n            \n            // Check bounds\n            if (new_pos.x >= 0 && new_pos.x < N && new_pos.y >= 0 && new_pos.y < N) {\n                current_pos = new_pos;\n                \n                // Create operation string\n                vector<char> rotations(V, '.');\n                vector<char> actions(V, '.');\n                \n                // For simplicity, let's just have one finger try to grab/release\n                if (V > 1) {\n                    Point finger_pos = current_pos + finger_dirs[0];\n                    if (finger_pos.x >= 0 && finger_pos.x < N && finger_pos.y >= 0 && finger_pos.y < N) {\n                        // Try to grab or release\n                        if (initial[finger_pos.x][finger_pos.y] && !target[finger_pos.x][finger_pos.y]) {\n                            actions[1] = 'P'; // Try to grab\n                        } else if (!initial[finger_pos.x][finger_pos.y] && target[finger_pos.x][finger_pos.y]) {\n                            actions[1] = 'P'; // Try to release\n                        }\n                    }\n                }\n                \n                operations.push_back(createMoveString(DIR_CHARS[dir], rotations, actions));\n                turns++;\n            } else {\n                // Can't move in that direction, stay put\n                vector<char> rotations(V, '.');\n                vector<char> actions(V, '.');\n                operations.push_back(createMoveString('.', rotations, actions));\n                turns++;\n            }\n            \n            // Early termination if we've done enough\n            if (turns % 100 == 0) {\n                // Check if we should continue based on some condition\n                // For now, just continue with random moves\n            }\n        }\n        \n        return operations;\n    }\n    \n    void outputSolution(RobotArm& arm, const vector<string>& operations) {\n        // Output arm design\n        cout << arm.V << endl;\n        for (int i = 1; i < arm.V; i++) {\n            cout << arm.tree[i].first << \" \" << arm.tree[i].second << endl;\n        }\n        cout << arm.root_pos.x << \" \" << arm.root_pos.y << endl;\n        \n        // Output operations\n        for (const string& op : operations) {\n            cout << op << endl;\n        }\n    }\n    \n    void run() {\n        readInput();\n        RobotArm arm = designArm();\n        vector<string> operations = solve(arm);\n        outputSolution(arm, operations);\n    }\n};\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    Solution solution;\n    solution.run();\n    \n    return 0;\n}","ahc039":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MAX_N = 5000;\nconst int GRID_SIZE = 100;\nconst int FIELD_SIZE = 100000;\n\nstruct Point {\n    int x, y;\n    Point() : x(0), y(0) {}\n    Point(int x, int y) : x(x), y(y) {}\n};\n\nstruct Rectangle {\n    int x1, y1, x2, y2;\n    int mackerels, sardines;\n    int score;\n    \n    Rectangle() : x1(0), y1(0), x2(0), y2(0), mackerels(0), sardines(0), score(0) {}\n    Rectangle(int x1, int y1, int x2, int y2) : x1(x1), y1(y1), x2(x2), y2(y2), mackerels(0), sardines(0), score(0) {}\n    \n    bool isValid() const {\n        return x1 >= 0 && x2 <= FIELD_SIZE && y1 >= 0 && y2 <= FIELD_SIZE && \n               x1 < x2 && y1 < y2 && (x2 - x1 + y2 - y1) * 2 <= 400000;\n    }\n    \n    int perimeter() const {\n        return 2 * (x2 - x1 + y2 - y1);\n    }\n};\n\n// Grid for density calculation\nint mackerel_grid[GRID_SIZE + 1][GRID_SIZE + 1];\nint sardine_grid[GRID_SIZE + 1][GRID_SIZE + 1];\n\nvector<Point> mackerels(MAX_N);\nvector<Point> sardines(MAX_N);\n\n// Convert coordinate to grid index\nint toGrid(int coord) {\n    return min(GRID_SIZE, max(0, coord * GRID_SIZE / FIELD_SIZE));\n}\n\n// Count points in rectangle\npair<int, int> countPoints(const Rectangle& rect, const vector<Point>& mackerels, const vector<Point>& sardines) {\n    int m_count = 0, s_count = 0;\n    \n    for (const auto& p : mackerels) {\n        if (p.x >= rect.x1 && p.x <= rect.x2 && p.y >= rect.y1 && p.y <= rect.y2) {\n            m_count++;\n        }\n    }\n    \n    for (const auto& p : sardines) {\n        if (p.x >= rect.x1 && p.x <= rect.x2 && p.y >= rect.y1 && p.y <= rect.y2) {\n            s_count++;\n        }\n    }\n    \n    return {m_count, s_count};\n}\n\n// Generate rectangles around high-density areas\nvector<Rectangle> generateRectangles(const vector<Point>& mackerels, const vector<Point>& sardines) {\n    vector<Rectangle> rectangles;\n    \n    // Initialize grids\n    memset(mackerel_grid, 0, sizeof(mackerel_grid));\n    memset(sardine_grid, 0, sizeof(sardine_grid));\n    \n    // Fill grids\n    for (const auto& p : mackerels) {\n        int gx = toGrid(p.x);\n        int gy = toGrid(p.y);\n        mackerel_grid[gx][gy]++;\n    }\n    \n    for (const auto& p : sardines) {\n        int gx = toGrid(p.x);\n        int gy = toGrid(p.y);\n        sardine_grid[gx][gy]++;\n    }\n    \n    // Generate rectangles around high mackerel density areas\n    for (int i = 0; i < GRID_SIZE; i++) {\n        for (int j = 0; j < GRID_SIZE; j++) {\n            if (mackerel_grid[i][j] > 0) {\n                // Try different rectangle sizes around this grid cell\n                for (int size = 1; size <= 20 && size <= GRID_SIZE; size++) {\n                    int x1_grid = max(0, i - size/2);\n                    int y1_grid = max(0, j - size/2);\n                    int x2_grid = min(GRID_SIZE, i + size/2);\n                    int y2_grid = min(GRID_SIZE, j + size/2);\n                    \n                    int x1 = x1_grid * FIELD_SIZE / GRID_SIZE;\n                    int y1 = y1_grid * FIELD_SIZE / GRID_SIZE;\n                    int x2 = min(FIELD_SIZE, (x2_grid + 1) * FIELD_SIZE / GRID_SIZE);\n                    int y2 = min(FIELD_SIZE, (y2_grid + 1) * FIELD_SIZE / GRID_SIZE);\n                    \n                    Rectangle rect(x1, y1, x2, y2);\n                    if (rect.isValid()) {\n                        auto counts = countPoints(rect, mackerels, sardines);\n                        rect.mackerels = counts.first;\n                        rect.sardines = counts.second;\n                        rect.score = rect.mackerels - rect.sardines;\n                        \n                        if (rect.score > 0) {\n                            rectangles.push_back(rect);\n                        }\n                    }\n                }\n            }\n        }\n    }\n    \n    // Sort by score\n    sort(rectangles.begin(), rectangles.end(), [](const Rectangle& a, const Rectangle& b) {\n        return a.score > b.score;\n    });\n    \n    return rectangles;\n}\n\n// Check if two rectangles overlap\nbool overlaps(const Rectangle& a, const Rectangle& b) {\n    return !(a.x2 < b.x1 || b.x2 < a.x1 || a.y2 < b.y1 || b.y2 < a.y1);\n}\n\n// Merge overlapping rectangles\nvector<Rectangle> mergeRectangles(vector<Rectangle>& rectangles) {\n    if (rectangles.empty()) return rectangles;\n    \n    vector<bool> used(rectangles.size(), false);\n    vector<Rectangle> result;\n    \n    for (int i = 0; i < rectangles.size(); i++) {\n        if (used[i]) continue;\n        \n        Rectangle current = rectangles[i];\n        bool merged = true;\n        \n        while (merged) {\n            merged = false;\n            for (int j = i + 1; j < rectangles.size(); j++) {\n                if (used[j]) continue;\n                \n                // Check if we can merge\n                Rectangle test(current.x1, current.y1, current.x2, current.y2);\n                test.x1 = min(test.x1, rectangles[j].x1);\n                test.y1 = min(test.y1, rectangles[j].y1);\n                test.x2 = max(test.x2, rectangles[j].x2);\n                test.y2 = max(test.y2, rectangles[j].y2);\n                \n                if (test.isValid() && test.perimeter() <= 400000) {\n                    auto counts = countPoints(test, mackerels, sardines);\n                    int new_score = counts.first - counts.second;\n                    \n                    // Simple heuristic: if combined score is better, merge\n                    if (new_score > current.score + rectangles[j].score - 5) {\n                        current = test;\n                        current.mackerels = counts.first;\n                        current.sardines = counts.second;\n                        current.score = new_score;\n                        used[j] = true;\n                        merged = true;\n                    }\n                }\n            }\n        }\n        \n        result.push_back(current);\n    }\n    \n    return result;\n}\n\n// Create rectilinear polygon from rectangles\nvector<Point> createPolygon(const vector<Rectangle>& rectangles) {\n    vector<Point> vertices;\n    \n    if (rectangles.empty()) {\n        // Fallback: create a simple rectangle around good area\n        vertices.push_back(Point(10000, 10000));\n        vertices.push_back(Point(90000, 10000));\n        vertices.push_back(Point(90000, 90000));\n        vertices.push_back(Point(10000, 90000));\n        return vertices;\n    }\n    \n    // For simplicity, use just the first rectangle\n    if (!rectangles.empty()) {\n        const Rectangle& rect = rectangles[0];\n        vertices.push_back(Point(rect.x1, rect.y1));\n        vertices.push_back(Point(rect.x2, rect.y1));\n        vertices.push_back(Point(rect.x2, rect.y2));\n        vertices.push_back(Point(rect.x1, rect.y2));\n    }\n    \n    return vertices;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    int N;\n    cin >> N;\n    \n    for (int i = 0; i < N; i++) {\n        cin >> mackerels[i].x >> mackerels[i].y;\n    }\n    \n    for (int i = 0; i < N; i++) {\n        cin >> sardines[i].x >> sardines[i].y;\n    }\n    \n    // Generate candidate rectangles\n    vector<Rectangle> rectangles = generateRectangles(mackerels, sardines);\n    \n    // Keep only top rectangles to avoid complexity\n    if (rectangles.size() > 100) {\n        rectangles.resize(100);\n    }\n    \n    // Merge overlapping rectangles\n    vector<Rectangle> merged = mergeRectangles(rectangles);\n    \n    // Create polygon\n    vector<Point> vertices = createPolygon(merged);\n    \n    // Output\n    cout << vertices.size() << \"\\n\";\n    for (const auto& vertex : vertices) {\n        cout << vertex.x << \" \" << vertex.y << \"\\n\";\n    }\n    \n    return 0;\n}","ahc040":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Rectangle {\n    int w, h;\n    int idx;\n    \n    Rectangle(int _w, int _h, int _idx) : w(_w), h(_h), idx(_idx) {}\n    \n    void rotate() {\n        swap(w, h);\n    }\n};\n\nstruct Placement {\n    int p, r, d, b;\n    \n    Placement(int _p, int _r, char _d, int _b) : p(_p), r(_r), d(_d), b(_b) {}\n};\n\n// Simple greedy placement algorithm\nvector<Placement> greedyPlace(const vector<pair<int,int>>& rectangles, mt19937& rng) {\n    vector<Placement> placements;\n    int n = rectangles.size();\n    \n    // For each rectangle, decide placement\n    for (int i = 0; i < n; i++) {\n        // Random rotation\n        int rotate = uniform_int_distribution<int>(0, 1)(rng);\n        \n        // Random direction\n        char direction = (uniform_int_distribution<int>(0, 1)(rng) == 0) ? 'U' : 'L';\n        \n        // Random reference (but must be valid)\n        int reference = -1;\n        if (i > 0) {\n            reference = uniform_int_distribution<int>(-1, i-1)(rng);\n        }\n        \n        placements.emplace_back(i, rotate, direction, reference);\n    }\n    \n    return placements;\n}\n\n// Skyline-based placement\nvector<Placement> skylinePlace(const vector<pair<int,int>>& rectangles, mt19937& rng) {\n    vector<Placement> placements;\n    int n = rectangles.size();\n    \n    if (n == 0) return placements;\n    \n    // Place first rectangle at origin\n    int rotate_first = uniform_int_distribution<int>(0, 1)(rng);\n    placements.emplace_back(0, rotate_first, 'U', -1);\n    \n    // For remaining rectangles, try to place them optimally\n    for (int i = 1; i < n; i++) {\n        // Try different strategies\n        int rotate = uniform_int_distribution<int>(0, 1)(rng);\n        char direction = (uniform_int_distribution<int>(0, 1)(rng) == 0) ? 'U' : 'L';\n        int reference = uniform_int_distribution<int>(-1, i-1)(rng);\n        \n        placements.emplace_back(i, rotate, direction, reference);\n    }\n    \n    return placements;\n}\n\n// Try to improve by placing fewer rectangles strategically\nvector<Placement> strategicPlace(const vector<pair<int,int>>& rectangles, mt19937& rng) {\n    vector<Placement> placements;\n    int n = rectangles.size();\n    \n    if (n == 0) return placements;\n    \n    // Place rectangles in order but with strategic choices\n    for (int i = 0; i < n; i++) {\n        if (uniform_int_distribution<int>(0, 4)(rng) == 0 && i > 0) {\n            // Skip some rectangles randomly to reduce unused penalty\n            continue;\n        }\n        \n        // Make strategic decisions\n        int rotate = 0; // Prefer original orientation\n        if (rectangles[i].second > rectangles[i].first) {\n            rotate = 1; // Rotate if height > width\n        }\n        \n        char direction = 'U';\n        int reference = -1;\n        \n        if (i > 0) {\n            // Try to place relative to previous rectangles\n            direction = (uniform_int_distribution<int>(0, 2)(rng) == 0) ? 'L' : 'U';\n            reference = max(-1, (int)i - uniform_int_distribution<int>(1, min(3, i))(rng));\n        }\n        \n        placements.emplace_back(i, rotate, direction, reference);\n    }\n    \n    return placements;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    int N, T, sigma;\n    cin >> N >> T >> sigma;\n    \n    vector<pair<int,int>> measured_rectangles(N);\n    for (int i = 0; i < N; i++) {\n        cin >> measured_rectangles[i].first >> measured_rectangles[i].second;\n    }\n    \n    // Initialize random number generator\n    random_device rd;\n    mt19937 rng(rd());\n    \n    // Try different strategies for each turn\n    for (int turn = 0; turn < T; turn++) {\n        vector<Placement> placements;\n        \n        // Choose strategy based on turn\n        if (turn < T/3) {\n            // Random placement\n            placements = greedyPlace(measured_rectangles, rng);\n        } else if (turn < 2*T/3) {\n            // Skyline-based placement\n            placements = skylinePlace(measured_rectangles, rng);\n        } else {\n            // Strategic placement\n            placements = strategicPlace(measured_rectangles, rng);\n        }\n        \n        // Output placements\n        cout << placements.size() << \"\\n\";\n        for (const auto& p : placements) {\n            cout << p.p << \" \" << p.r << \" \" << (char)p.d << \" \" << p.b << \"\\n\";\n        }\n        cout << flush;\n        \n        // Read measurement result\n        int W_meas, H_meas;\n        cin >> W_meas >> H_meas;\n    }\n    \n    return 0;\n}","ahc041":"#include <bits/stdc++.h>\n#include <atcoder/all>\nusing namespace std;\nusing namespace atcoder;\n\nconst int MAXN = 1000;\nconst int MAXH = 10;\n\nint N, M, H;\nvector<int> A;\nvector<vector<int>> adj;\nvector<pair<int, int>> coords;\nvector<int> parent;\nvector<bool> assigned;\n\n// Build trees greedily\nvoid buildTrees() {\n    parent.assign(N, -1);\n    assigned.assign(N, false);\n    \n    // Create list of vertices with their beauty values\n    vector<pair<int, int>> beauty_order;\n    for (int i = 0; i < N; i++) {\n        beauty_order.push_back({A[i], i});\n    }\n    \n    // Sort by beauty value descending\n    sort(beauty_order.rbegin(), beauty_order.rend());\n    \n    for (auto& [beauty, start_v] : beauty_order) {\n        if (assigned[start_v]) continue;\n        \n        // Start a new tree with this vertex as root\n        queue<pair<int, int>> q; // {vertex, depth}\n        q.push({start_v, 0});\n        assigned[start_v] = true;\n        \n        while (!q.empty()) {\n            auto [v, depth] = q.front();\n            q.pop();\n            \n            if (depth >= H) continue;\n            \n            // Try to add unassigned neighbors\n            for (int u : adj[v]) {\n                if (!assigned[u]) {\n                    parent[u] = v;\n                    assigned[u] = true;\n                    q.push({u, depth + 1});\n                }\n            }\n        }\n    }\n}\n\n// Improved approach: try to optimize by reassigning vertices\nvoid optimize() {\n    // For each vertex, if moving it to a better parent increases score, do it\n    bool improved = true;\n    while (improved) {\n        improved = false;\n        vector<pair<int, int>> beauty_order;\n        for (int i = 0; i < N; i++) {\n            beauty_order.push_back({A[i], i});\n        }\n        sort(beauty_order.rbegin(), beauty_order.rend());\n        \n        for (auto& [beauty, v] : beauty_order) {\n            if (parent[v] == -1) continue; // root\n            \n            int current_parent = parent[v];\n            int current_depth = 0;\n            int temp = v;\n            while (parent[temp] != -1) {\n                current_depth++;\n                temp = parent[temp];\n            }\n            \n            // Try to find a better parent (higher up in some tree)\n            for (int u : adj[v]) {\n                if (u == current_parent) continue;\n                \n                // Calculate depth of u in its tree\n                int u_depth = 0;\n                int temp_u = u;\n                while (parent[temp_u] != -1) {\n                    u_depth++;\n                    temp_u = parent[temp_u];\n                }\n                \n                // If we can place v higher (closer to root) and within height limit\n                if (u_depth < H) {\n                    // Check if this improves attractiveness\n                    int new_depth = u_depth + 1;\n                    if (new_depth < current_depth) {\n                        parent[v] = u;\n                        improved = true;\n                        break;\n                    }\n                }\n            }\n            if (improved) break;\n        }\n    }\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    cin >> N >> M >> H;\n    A.resize(N);\n    for (int i = 0; i < N; i++) {\n        cin >> A[i];\n    }\n    \n    adj.resize(N);\n    for (int i = 0; i < M; i++) {\n        int u, v;\n        cin >> u >> v;\n        adj[u].push_back(v);\n        adj[v].push_back(u);\n    }\n    \n    coords.resize(N);\n    for (int i = 0; i < N; i++) {\n        cin >> coords[i].first >> coords[i].second;\n    }\n    \n    buildTrees();\n    optimize();\n    \n    for (int i = 0; i < N; i++) {\n        cout << parent[i];\n        if (i < N - 1) cout << \" \";\n    }\n    cout << \"\\n\";\n    \n    return 0;\n}","ahc042":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MAX_N = 20;\nchar board[MAX_N][MAX_N];\nint N;\n\nstruct Position {\n    int row, col;\n};\n\nvector<Position> oni_list;\nvector<Position> fuku_list;\n\nstruct Move {\n    char direction;\n    int index;\n};\n\nvector<Move> moves;\n\n// Check if column j has any Fukunokami above row r\nbool hasFukuAbove(int r, int j) {\n    for (int i = 0; i < r; i++) {\n        if (board[i][j] == 'o') return true;\n    }\n    return false;\n}\n\n// Check if column j has any Fukunokami below row r\nbool hasFukuBelow(int r, int j) {\n    for (int i = r + 1; i < N; i++) {\n        if (board[i][j] == 'o') return true;\n    }\n    return false;\n}\n\n// Check if row i has any Fukunokami to the left of column c\nbool hasFukuLeft(int i, int c) {\n    for (int j = 0; j < c; j++) {\n        if (board[i][j] == 'o') return true;\n    }\n    return false;\n}\n\n// Check if row i has any Fukunokami to the right of column c\nbool hasFukuRight(int i, int c) {\n    for (int j = c + 1; j < N; j++) {\n        if (board[i][j] == 'o') return true;\n    }\n    return false;\n}\n\nvoid addMove(char dir, int idx) {\n    moves.push_back({dir, idx});\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    cin >> N;\n    for (int i = 0; i < N; i++) {\n        string row;\n        cin >> row;\n        for (int j = 0; j < N; j++) {\n            board[i][j] = row[j];\n            if (row[j] == 'x') {\n                oni_list.push_back({i, j});\n            } else if (row[j] == 'o') {\n                fuku_list.push_back({i, j});\n            }\n        }\n    }\n    \n    // Process each Oni\n    for (const auto& oni : oni_list) {\n        int r = oni.row;\n        int c = oni.col;\n        \n        // Try to remove this Oni in the most efficient way\n        // Prefer directions that require fewer moves\n        \n        // Up direction: shift column down\n        if (!hasFukuAbove(r, c)) {\n            // Shift down r+1 times to move Oni out, then shift back\n            for (int i = 0; i < r + 1; i++) addMove('D', c);\n            for (int i = 0; i < r + 1; i++) addMove('U', c);\n        }\n        // Down direction: shift column up\n        else if (!hasFukuBelow(r, c)) {\n            // Shift up N-r times to move Oni out, then shift back\n            for (int i = 0; i < N - r; i++) addMove('U', c);\n            for (int i = 0; i < N - r; i++) addMove('D', c);\n        }\n        // Left direction: shift row right\n        else if (!hasFukuLeft(r, c)) {\n            // Shift right c+1 times to move Oni out, then shift back\n            for (int i = 0; i < c + 1; i++) addMove('R', r);\n            for (int i = 0; i < c + 1; i++) addMove('L', r);\n        }\n        // Right direction: shift row left\n        else if (!hasFukuRight(r, c)) {\n            // Shift left N-c times to move Oni out, then shift back\n            for (int i = 0; i < N - c; i++) addMove('L', r);\n            for (int i = 0; i < N - c; i++) addMove('R', r);\n        }\n    }\n    \n    // Output the moves\n    for (const auto& move : moves) {\n        cout << move.direction << \" \" << move.index << \"\\n\";\n    }\n    \n    return 0;\n}","ahc044":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 100;\nconst int L = 500000;\n\nint T[N];\n\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\n\n// Simulate the sequence and return the error\nlong long simulate(int a[], int b[]) {\n    vector<int> count(N, 0);\n    vector<long long> actual(N, 0);\n    \n    int current = 0;\n    actual[0]++;\n    count[0]++;\n    \n    // Run for L weeks\n    for (long long step = 1; step < L; step++) {\n        int next;\n        if (count[current] % 2 == 1) {\n            next = a[current];\n        } else {\n            next = b[current];\n        }\n        current = next;\n        actual[current]++;\n        count[current]++;\n    }\n    \n    long long error = 0;\n    for (int i = 0; i < N; i++) {\n        error += abs(actual[i] - (long long)T[i]);\n    }\n    \n    return error;\n}\n\n// Generate a reasonable assignment based on targets\nvoid generate_assignment(int a[], int b[], int T[]) {\n    // Create a list of employees sorted by target (descending)\n    vector<pair<int, int>> targets;\n    for (int i = 0; i < N; i++) {\n        targets.push_back({T[i], i});\n    }\n    sort(targets.rbegin(), targets.rend());\n    \n    // For employees with high targets, make them more reachable\n    // For employees with low targets, make them less reachable but still possible\n    \n    for (int i = 0; i < N; i++) {\n        // Point a_i and b_i towards employees that help achieve target distribution\n        // Simple strategy: point more often to higher target employees\n        \n        // Use a weighted approach\n        uniform_int_distribution<int> dist(0, N-1);\n        \n        // For high target employees, point to other high targets\n        // For low target employees, distribute more evenly\n        \n        if (T[i] > L / N) {\n            // High target - point to other high targets\n            int idx1 = dist(rng) % min(20, N);\n            int idx2 = dist(rng) % min(20, N);\n            a[i] = targets[idx1].second;\n            b[i] = targets[idx2].second;\n        } else if (T[i] < L / (2 * N)) {\n            // Very low target - point more randomly but avoid high targets too much\n            a[i] = dist(rng);\n            b[i] = dist(rng);\n        } else {\n            // Medium target - balanced approach\n            a[i] = targets[dist(rng) % N].second;\n            b[i] = targets[dist(rng) % N].second;\n        }\n        \n        // Ensure valid indices\n        a[i] = max(0, min(N-1, a[i]));\n        b[i] = max(0, min(N-1, b[i]));\n    }\n    \n    // Make sure employee 0 appears in some transitions (since we start there)\n    // This helps ensure connectivity\n    for (int i = 0; i < min(5, N); i++) {\n        if (rng() % 2) {\n            a[targets[i].second] = 0;\n        } else {\n            b[targets[i].second] = 0;\n        }\n    }\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    int n, l;\n    cin >> n >> l;\n    for (int i = 0; i < n; i++) {\n        cin >> T[i];\n    }\n    \n    int a[N], b[N];\n    \n    // Generate initial assignment\n    generate_assignment(a, b, T);\n    \n    // Simple improvement: make sure high-demand employees are reachable\n    vector<pair<int, int>> targets;\n    for (int i = 0; i < N; i++) {\n        targets.push_back({T[i], i});\n    }\n    sort(targets.rbegin(), targets.rend());\n    \n    // Ensure top targets are well-connected\n    for (int i = 0; i < min(10, N); i++) {\n        int emp = targets[i].second;\n        // Make sure someone points to this employee\n        int from = (emp + 1) % N;\n        if (rng() % 2) {\n            a[from] = emp;\n        } else {\n            b[from] = emp;\n        }\n    }\n    \n    // Output result\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>\n#include <atcoder/all>\nusing namespace std;\nusing namespace atcoder;\n\nconst int N = 800;\nconst int MAX_L = 15;\n\nint n, m, q, L, W;\nvector<int> G;\nvector<int> lx, rx, ly, ry;\nvector<pair<int, int>> centers;\nvector<vector<pair<int, int>>> mst_edges;\n\n// Union-Find for MST construction\nclass UnionFind {\nprivate:\n    vector<int> parent, rank;\npublic:\n    UnionFind(int n) : parent(n), rank(n, 0) {\n        for (int i = 0; i < n; i++) parent[i] = i;\n    }\n    \n    int find(int x) {\n        if (parent[x] != x) parent[x] = find(parent[x]);\n        return parent[x];\n    }\n    \n    bool unite(int x, int y) {\n        int root_x = find(x);\n        int root_y = find(y);\n        if (root_x == root_y) return false;\n        if (rank[root_x] < rank[root_y]) swap(root_x, root_y);\n        parent[root_y] = root_x;\n        if (rank[root_x] == rank[root_y]) rank[root_x]++;\n        return true;\n    }\n};\n\n// Query function\nvector<pair<int, int>> query(const vector<int>& cities) {\n    if (cities.size() < 2) return {};\n    \n    cout << \"? \" << cities.size();\n    for (int city : cities) cout << \" \" << city;\n    cout << endl;\n    cout.flush();\n    \n    vector<pair<int, int>> result(cities.size() - 1);\n    for (int i = 0; i < cities.size() - 1; i++) {\n        int u, v;\n        cin >> u >> v;\n        result[i] = {u, v};\n    }\n    return result;\n}\n\n// Calculate estimated distance using center points\ndouble estimated_distance(int i, int j) {\n    double cx1 = (lx[i] + rx[i]) / 2.0;\n    double cy1 = (ly[i] + ry[i]) / 2.0;\n    double cx2 = (lx[j] + rx[j]) / 2.0;\n    double cy2 = (ly[j] + ry[j]) / 2.0;\n    return sqrt((cx1 - cx2) * (cx1 - cx2) + (cy1 - cy2) * (cy1 - cy2));\n}\n\n// Group cities based on proximity\nvector<vector<int>> create_groups() {\n    vector<pair<double, int>> city_positions(n);\n    for (int i = 0; i < n; i++) {\n        city_positions[i] = {estimated_distance(0, i), i};\n    }\n    \n    // Sort by estimated position from first city\n    sort(city_positions.begin(), city_positions.end());\n    \n    vector<vector<int>> groups;\n    int idx = 0;\n    for (int group_size : G) {\n        vector<int> group;\n        for (int i = 0; i < group_size; i++) {\n            group.push_back(city_positions[idx++].second);\n        }\n        groups.push_back(group);\n    }\n    \n    return groups;\n}\n\n// Improved grouping using k-means style approach\nvector<vector<int>> create_groups_improved() {\n    // Use center coordinates for sorting\n    vector<pair<pair<int, int>, int>> city_centers(n);\n    for (int i = 0; i < n; i++) {\n        int cx = (lx[i] + rx[i]) / 2;\n        int cy = (ly[i] + ry[i]) / 2;\n        city_centers[i] = {{cx, cy}, i};\n    }\n    \n    // Sort by x-coordinate then y-coordinate\n    sort(city_centers.begin(), city_centers.end());\n    \n    vector<vector<int>> groups;\n    int idx = 0;\n    for (int group_size : G) {\n        vector<int> group;\n        for (int i = 0; i < group_size; i++) {\n            group.push_back(city_centers[idx++].second);\n        }\n        groups.push_back(group);\n    }\n    \n    return groups;\n}\n\n// Use queries to get MST information\nvector<vector<pair<int, int>>> get_mst_edges(const vector<vector<int>>& groups) {\n    vector<vector<pair<int, int>>> all_edges(m);\n    \n    int queries_used = 0;\n    \n    for (int g = 0; g < m && queries_used < q; g++) {\n        const vector<int>& group = groups[g];\n        int group_size = group.size();\n        \n        if (group_size <= 1) continue;\n        \n        // For small groups, query the entire group\n        if (group_size <= L && queries_used < q) {\n            auto edges = query(group);\n            all_edges[g] = edges;\n            queries_used++;\n            continue;\n        }\n        \n        // For larger groups, use sliding window approach\n        set<pair<int, int>> edge_set;\n        \n        // Query overlapping triplets\n        for (int i = 0; i < min(group_size - 2, (q - queries_used) * 2) && queries_used < q; i++) {\n            vector<int> subset = {group[i], group[i + 1], group[i + 2]};\n            auto edges = query(subset);\n            for (auto edge : edges) {\n                edge_set.insert(edge);\n            }\n            queries_used++;\n        }\n        \n        // Convert set to vector\n        all_edges[g] = vector<pair<int, int>>(edge_set.begin(), edge_set.end());\n    }\n    \n    return all_edges;\n}\n\n// Build MST for each group using collected edges\nvector<vector<pair<int, int>>> build_mst(const vector<vector<int>>& groups, \n                                        const vector<vector<pair<int, int>>>& queried_edges) {\n    vector<vector<pair<int, int>>> result(m);\n    \n    for (int g = 0; g < m; g++) {\n        const vector<int>& group = groups[g];\n        if (group.size() <= 1) continue;\n        \n        // If we have queried edges for this group, use them\n        if (!queried_edges[g].empty()) {\n            // Use Union-Find to extract a spanning tree from the queried edges\n            UnionFind uf(n);\n            vector<pair<int, int>> mst_edges;\n            \n            for (auto edge : queried_edges[g]) {\n                if (uf.unite(edge.first, edge.second)) {\n                    mst_edges.push_back(edge);\n                }\n            }\n            \n            // If we don't have enough edges, add some heuristically\n            if (mst_edges.size() < group.size() - 1) {\n                set<pair<int, int>> existing_edges(mst_edges.begin(), mst_edges.end());\n                \n                // Add direct connections to complete the tree\n                for (int i = 0; i < group.size() - 1; i++) {\n                    pair<int, int> edge = {min(group[i], group[i+1]), max(group[i], group[i+1])};\n                    if (existing_edges.find(edge) == existing_edges.end() && \n                        mst_edges.size() < group.size() - 1) {\n                        mst_edges.push_back(edge);\n                    }\n                }\n            }\n            \n            result[g] = mst_edges;\n        } else {\n            // Fallback: connect consecutive cities\n            vector<pair<int, int>> edges;\n            for (int i = 0; i < group.size() - 1; i++) {\n                edges.push_back({group[i], group[i+1]});\n            }\n            result[g] = edges;\n        }\n    }\n    \n    return result;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    // Read input\n    cin >> n >> m >> q >> L >> W;\n    G.resize(m);\n    for (int i = 0; i < m; i++) cin >> G[i];\n    \n    lx.resize(n); rx.resize(n); ly.resize(n); ry.resize(n);\n    for (int i = 0; i < n; i++) {\n        cin >> lx[i] >> rx[i] >> ly[i] >> ry[i];\n    }\n    \n    // Create groups\n    vector<vector<int>> groups = create_groups_improved();\n    \n    // Use queries to get information\n    vector<vector<pair<int, int>>> queried_edges = get_mst_edges(groups);\n    \n    // Build final MST for each group\n    vector<vector<pair<int, int>>> final_edges = build_mst(groups, queried_edges);\n    \n    // Output answer\n    cout << \"!\" << endl;\n    for (int g = 0; g < m; g++) {\n        // Output group cities\n        for (int i = 0; i < groups[g].size(); i++) {\n            if (i > 0) cout << \" \";\n            cout << groups[g][i];\n        }\n        cout << endl;\n        \n        // Output edges\n        for (auto edge : final_edges[g]) {\n            cout << edge.first << \" \" << edge.second << endl;\n        }\n    }\n    \n    return 0;\n}","ahc046":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 20;\nconst int M = 40;\n\nstruct Point {\n    int r, c;\n    Point() : r(0), c(0) {}\n    Point(int _r, int _c) : r(_r), c(_c) {}\n    bool operator==(const Point& other) const {\n        return r == other.r && c == other.c;\n    }\n};\n\n// Directions\nconst int DR[] = {-1, 1, 0, 0};  // Up, Down, Left, Right\nconst int DC[] = {0, 0, -1, 1};\nconst char DIR_CHARS[] = {'U', 'D', 'L', 'R'};\n\nPoint targets[M];\nbool blocked[N][N];\n\n// Check if position is valid\nbool isValid(int r, int c) {\n    return r >= 0 && r < N && c >= 0 && c < N;\n}\n\n// Simple Manhattan pathfinding\nvector<pair<char, char>> findPath(Point from, Point to) {\n    vector<pair<char, char>> path;\n    int r = from.r, c = from.c;\n    \n    // Move vertically first\n    while (r < to.r) {\n        if (isValid(r + 1, c) && !blocked[r + 1][c]) {\n            path.push_back({'M', 'D'});\n            r++;\n        } else {\n            break;\n        }\n    }\n    while (r > to.r) {\n        if (isValid(r - 1, c) && !blocked[r - 1][c]) {\n            path.push_back({'M', 'U'});\n            r--;\n        } else {\n            break;\n        }\n    }\n    \n    // Move horizontally\n    while (c < to.c) {\n        if (isValid(r, c + 1) && !blocked[r][c + 1]) {\n            path.push_back({'M', 'R'});\n            c++;\n        } else {\n            break;\n        }\n    }\n    while (c > to.c) {\n        if (isValid(r, c - 1) && !blocked[r][c - 1]) {\n            path.push_back({'M', 'L'});\n            c--;\n        } else {\n            break;\n        }\n    }\n    \n    return path;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int n, m;\n    cin >> n >> m;\n    \n    Point start;\n    cin >> start.r >> start.c;\n    \n    for (int i = 0; i < m; i++) {\n        cin >> targets[i].r >> targets[i].c;\n    }\n    \n    vector<pair<char, char>> actions;\n    Point current = start;\n    \n    // Visit each target in order\n    for (int i = 1; i < m; i++) {\n        Point target = targets[i];\n        auto path = findPath(current, target);\n        actions.insert(actions.end(), path.begin(), path.end());\n        current = target;\n    }\n    \n    // Output results\n    for (auto& action : actions) {\n        cout << action.first << \" \" << action.second << \"\\n\";\n    }\n    \n    return 0;\n}"},"2":{"ahc001":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Rectangle {\n    int a, b, c, d; // (a,b) to (c,d)\n    Rectangle() : a(0), b(0), c(0), d(0) {}\n    Rectangle(int _a, int _b, int _c, int _d) : a(_a), b(_b), c(_c), d(_d) {}\n    bool isValid() const { return a < c && b < d; }\n    long long area() const { return 1LL * (c - a) * (d - b); }\n};\n\nstruct Company {\n    int x, y, r;\n    int id;\n    Company() : x(0), y(0), r(0), id(0) {}\n    Company(int _x, int _y, int _r, int _id) : x(_x), y(_y), r(_r), id(_id) {}\n};\n\nint n;\nvector<Company> companies;\nvector<Rectangle> rectangles;\n\n// Check if two rectangles overlap\nbool overlap(const Rectangle& r1, const Rectangle& r2) {\n    return !(r1.c <= r2.a || r2.c <= r1.a || r1.d <= r2.b || r2.d <= r1.b);\n}\n\n// Check if rectangle overlaps with any existing rectangle\nbool hasOverlap(const Rectangle& rect, int skip_id = -1) {\n    for (int i = 0; i < n; i++) {\n        if (i == skip_id) continue;\n        if (rectangles[i].isValid() && overlap(rect, rectangles[i])) {\n            return true;\n        }\n    }\n    return false;\n}\n\n// Try to place a rectangle for company at index idx\nbool placeRectangle(int idx) {\n    const Company& company = companies[idx];\n    int x = company.x;\n    int y = company.y;\n    int r = company.r;\n    \n    // Try different rectangle dimensions\n    vector<pair<int, int>> dimensions;\n    for (int w = 1; w <= min(r, 10000); w++) {\n        if (r % w == 0) {\n            int h = r / w;\n            if (h <= 10000) {\n                dimensions.push_back({w, h});\n            }\n        }\n    }\n    \n    // Sort by perimeter (prefer more square-like rectangles)\n    sort(dimensions.begin(), dimensions.end(), [](const pair<int,int>& a, const pair<int,int>& b) {\n        return abs(a.first - a.second) < abs(b.first - b.second);\n    });\n    \n    // Try to place rectangle containing (x, y) \n    for (auto& dim : dimensions) {\n        int w = dim.first;\n        int h = dim.second;\n        \n        // Try different positions where (x, y) is inside the rectangle\n        for (int dx = 0; dx < w; dx++) {\n            for (int dy = 0; dy < h; dy++) {\n                int a = x - dx;\n                int b = y - dy;\n                int c = a + w;\n                int d = b + h;\n                \n                // Check bounds\n                if (a >= 0 && b >= 0 && c <= 10000 && d <= 10000) {\n                    Rectangle rect(a, b, c, d);\n                    // Check if (x, y) is inside (including boundary)\n                    if (a <= x && x < c && b <= y && y < d) {\n                        if (!hasOverlap(rect, idx)) {\n                            rectangles[idx] = rect;\n                            return true;\n                        }\n                    }\n                }\n            }\n        }\n    }\n    \n    // If we can't place exact area, try approximate placement\n    // Try to place any rectangle containing the point\n    for (int size = max(1, (int)sqrt(r)); size >= 1; size--) {\n        int w = min(size, 10000);\n        int h = min(max(1, r / w), 10000);\n        \n        // Center around the point\n        int a = max(0, min(x - w/2, 10000 - w));\n        int b = max(0, min(y - h/2, 10000 - h));\n        int c = a + w;\n        int d = b + h;\n        \n        if (c <= 10000 && d <= 10000) {\n            Rectangle rect(a, b, c, d);\n            // Check if point is inside\n            if (a <= x && x < c && b <= y && y < d) {\n                if (!hasOverlap(rect, idx)) {\n                    rectangles[idx] = rect;\n                    return true;\n                }\n            }\n        }\n    }\n    \n    // Last resort: place smallest possible rectangle\n    Rectangle rect(max(0, x), max(0, y), min(10000, x + 1), min(10000, y + 1));\n    if (!hasOverlap(rect, idx)) {\n        rectangles[idx] = rect;\n        return true;\n    }\n    \n    return false;\n}\n\n// Improved placement trying multiple strategies\nbool placeRectangleImproved(int idx) {\n    const Company& company = companies[idx];\n    int x = company.x;\n    int y = company.y;\n    int r = company.r;\n    \n    // Strategy 1: Try exact area placement\n    for (int w = 1; w <= min(r, 10000); w++) {\n        if (r % w == 0) {\n            int h = r / w;\n            if (h <= 10000) {\n                // Try to center around the point\n                for (int offset_x = min(w, 20); offset_x >= 0; offset_x--) {\n                    for (int offset_y = min(h, 20); offset_y >= 0; offset_y--) {\n                        int a = max(0, min(x - offset_x, 10000 - w));\n                        int b = max(0, min(y - offset_y, 10000 - h));\n                        int c = a + w;\n                        int d = b + h;\n                        \n                        if (c <= 10000 && d <= 10000) {\n                            Rectangle rect(a, b, c, d);\n                            if (a <= x && x < c && b <= y && y < d) {\n                                if (!hasOverlap(rect, idx)) {\n                                    rectangles[idx] = rect;\n                                    return true;\n                                }\n                            }\n                        }\n                    }\n                }\n            }\n        }\n    }\n    \n    // Strategy 2: Place any valid rectangle containing the point\n    int best_area = 0;\n    Rectangle best_rect;\n    \n    for (int w = 1; w <= 100 && w <= 10000; w++) {\n        for (int h = 1; h <= 100 && h <= 10000; h++) {\n            // Try different positions\n            for (int dx = 0; dx < min(w, 20); dx++) {\n                for (int dy = 0; dy < min(h, 20); dy++) {\n                    int a = max(0, min(x - dx, 10000 - w));\n                    int b = max(0, min(y - dy, 10000 - h));\n                    int c = a + w;\n                    int d = b + h;\n                    \n                    if (c <= 10000 && d <= 10000) {\n                        Rectangle rect(a, b, c, d);\n                        if (a <= x && x < c && b <= y && y < d) {\n                            if (!hasOverlap(rect, idx)) {\n                                int area = rect.area();\n                                if (area > best_area) {\n                                    best_area = area;\n                                    best_rect = rect;\n                                }\n                            }\n                        }\n                    }\n                }\n            }\n        }\n    }\n    \n    if (best_area > 0) {\n        rectangles[idx] = best_rect;\n        return true;\n    }\n    \n    // Strategy 3: Last resort - minimal rectangle\n    for (int a = max(0, x - 5); a <= min(x + 5, 9999); a++) {\n        for (int b = max(0, y - 5); b <= min(y + 5, 9999); b++) {\n            Rectangle rect(a, b, a + 1, b + 1);\n            if (a <= x && x < a + 1 && b <= y && y < b + 1) {\n                if (!hasOverlap(rect, idx)) {\n                    rectangles[idx] = rect;\n                    return true;\n                }\n            }\n        }\n    }\n    \n    return false;\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    cin >> n;\n    companies.resize(n);\n    rectangles.resize(n);\n    \n    for (int i = 0; i < n; i++) {\n        int x, y, r;\n        cin >> x >> y >> r;\n        companies[i] = Company(x, y, r, i);\n    }\n    \n    // Sort by area descending (place larger rectangles first)\n    sort(companies.begin(), companies.end(), [](const Company& a, const Company& b) {\n        return a.r > b.r;\n    });\n    \n    // Place rectangles\n    for (int i = 0; i < n; i++) {\n        placeRectangleImproved(companies[i].id);\n    }\n    \n    // Output results\n    vector<pair<int, Rectangle>> result(n);\n    for (int i = 0; i < n; i++) {\n        result[i] = {companies[i].id, rectangles[companies[i].id]};\n    }\n    \n    sort(result.begin(), result.end());\n    \n    for (int i = 0; i < n; i++) {\n        cout << result[i].second.a << \" \" << result[i].second.b << \" \" \n             << result[i].second.c << \" \" << result[i].second.d << \"\\n\";\n    }\n    \n    return 0;\n}","ahc002":"#include <bits/stdc++.h>\n#include <atcoder/all>\nusing namespace std;\nusing namespace atcoder;\n\nconst int SIZE = 50;\nint si, sj;\nint tile[SIZE][SIZE];\nint value[SIZE][SIZE];\nbool visited_tile[2500]; // max 2500 tiles\nvector<pair<int, int>> tile_positions[2500];\nint tile_values[2500];\n\n// Directions: U, D, L, R\nconst int dx[4] = {-1, 1, 0, 0};\nconst int dy[4] = {0, 0, -1, 1};\nconst char dir_chars[4] = {'U', 'D', 'L', 'R'};\n\n// Check if position is valid\nbool is_valid(int x, int y) {\n    return x >= 0 && x < SIZE && y >= 0 && y < SIZE;\n}\n\n// Get tile ID at position\nint get_tile_id(int x, int y) {\n    return tile[x][y];\n}\n\n// Calculate total value of a tile\nint calculate_tile_value(int tile_id) {\n    int total = 0;\n    for (auto& pos : tile_positions[tile_id]) {\n        total += value[pos.first][pos.second];\n    }\n    return total;\n}\n\n// Initialize tile information\nvoid init_tiles() {\n    set<int> tile_ids;\n    for (int i = 0; i < SIZE; i++) {\n        for (int j = 0; j < SIZE; j++) {\n            tile_ids.insert(tile[i][j]);\n        }\n    }\n    \n    for (int i = 0; i < SIZE; i++) {\n        for (int j = 0; j < SIZE; j++) {\n            tile_positions[tile[i][j]].push_back({i, j});\n        }\n    }\n    \n    for (int i = 0; i < 2500; i++) {\n        if (!tile_positions[i].empty()) {\n            tile_values[i] = calculate_tile_value(i);\n        }\n    }\n}\n\n// Check if we can move to a position\nbool can_move(int x, int y) {\n    if (!is_valid(x, y)) return false;\n    int tile_id = get_tile_id(x, y);\n    return !visited_tile[tile_id];\n}\n\n// Greedy path finding with beam search\nstring find_path() {\n    string best_path = \"\";\n    int best_score = -1;\n    \n    // Try multiple strategies\n    for (int strategy = 0; strategy < 3; strategy++) {\n        // Reset visited tiles\n        memset(visited_tile, false, sizeof(visited_tile));\n        \n        string current_path = \"\";\n        int x = si, y = sj;\n        int current_score = value[x][y];\n        int tile_id = get_tile_id(x, y);\n        visited_tile[tile_id] = true;\n        \n        // Maximum steps to prevent infinite loops\n        for (int step = 0; step < 200; step++) {\n            vector<pair<int, pair<int, int>>> candidates; // {score, {nx, ny}}\n            \n            // Evaluate all possible moves\n            for (int d = 0; d < 4; d++) {\n                int nx = x + dx[d];\n                int ny = y + dy[d];\n                \n                if (can_move(nx, ny)) {\n                    int next_tile_id = get_tile_id(nx, ny);\n                    int potential_score = tile_values[next_tile_id];\n                    \n                    // Add some randomness for different strategies\n                    if (strategy == 1) {\n                        potential_score += rand() % 100;\n                    } else if (strategy == 2) {\n                        potential_score -= rand() % 50;\n                    }\n                    \n                    candidates.push_back({potential_score, {nx, ny}});\n                }\n            }\n            \n            if (candidates.empty()) break;\n            \n            // Sort by score (descending)\n            sort(candidates.rbegin(), candidates.rend());\n            \n            // Choose best candidate (with some randomness for strategy 0)\n            int chosen_idx = 0;\n            if (strategy == 0 && candidates.size() > 1) {\n                // 70% chance to pick best, 30% chance to pick second best\n                if (rand() % 100 < 30 && candidates.size() > 1) {\n                    chosen_idx = 1;\n                }\n            }\n            \n            auto chosen = candidates[chosen_idx];\n            int nx = chosen.second.first;\n            int ny = chosen.second.second;\n            \n            // Move to the new position\n            for (int d = 0; d < 4; d++) {\n                if (x + dx[d] == nx && y + dy[d] == ny) {\n                    current_path += dir_chars[d];\n                    break;\n                }\n            }\n            \n            x = nx;\n            y = ny;\n            int new_tile_id = get_tile_id(x, y);\n            current_score += tile_values[new_tile_id];\n            visited_tile[new_tile_id] = true;\n        }\n        \n        if (current_score > best_score) {\n            best_score = current_score;\n            best_path = current_path;\n        }\n    }\n    \n    return best_path;\n}\n\n// Enhanced path finding with look-ahead\nstring find_path_enhanced() {\n    // Reset visited tiles\n    memset(visited_tile, false, sizeof(visited_tile));\n    \n    string path = \"\";\n    int x = si, y = sj;\n    int tile_id = get_tile_id(x, y);\n    visited_tile[tile_id] = true;\n    \n    // Use a more sophisticated approach with look-ahead\n    for (int step = 0; step < 250; step++) {\n        vector<tuple<int, int, int, int>> candidates; // {score, future_score, nx, ny}\n        \n        // Evaluate moves\n        for (int d = 0; d < 4; d++) {\n            int nx = x + dx[d];\n            int ny = y + dy[d];\n            \n            if (can_move(nx, ny)) {\n                int next_tile_id = get_tile_id(nx, ny);\n                int immediate_score = tile_values[next_tile_id];\n                \n                // Look ahead one step\n                int future_score = 0;\n                for (int d2 = 0; d2 < 4; d2++) {\n                    int nnx = nx + dx[d2];\n                    int nny = ny + dy[d2];\n                    \n                    if (is_valid(nnx, nny) && !visited_tile[get_tile_id(nnx, nny)]) {\n                        future_score = max(future_score, tile_values[get_tile_id(nnx, nny)]);\n                    }\n                }\n                \n                candidates.push_back({immediate_score, future_score, nx, ny});\n            }\n        }\n        \n        if (candidates.empty()) break;\n        \n        // Sort by immediate score, then by future score\n        sort(candidates.rbegin(), candidates.rend());\n        \n        // Choose the best move\n        auto best = candidates[0];\n        int nx = get<2>(best);\n        int ny = get<3>(best);\n        \n        // Find direction\n        for (int d = 0; d < 4; d++) {\n            if (x + dx[d] == nx && y + dy[d] == ny) {\n                path += dir_chars[d];\n                break;\n            }\n        }\n        \n        x = nx;\n        y = ny;\n        visited_tile[get_tile_id(x, y)] = true;\n    }\n    \n    return path;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    // Read input\n    cin >> si >> sj;\n    \n    for (int i = 0; i < SIZE; i++) {\n        for (int j = 0; j < SIZE; j++) {\n            cin >> tile[i][j];\n        }\n    }\n    \n    for (int i = 0; i < SIZE; i++) {\n        for (int j = 0; j < SIZE; j++) {\n            cin >> value[i][j];\n        }\n    }\n    \n    // Initialize tile information\n    init_tiles();\n    \n    // Find and output path\n    string result = find_path_enhanced();\n    cout << result << endl;\n    \n    return 0;\n}","ahc003":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int GRID_SIZE = 30;\nconst int MAX_QUERIES = 1000;\n\n// Direction vectors: U, D, L, R\nconst int di[4] = {-1, 1, 0, 0};\nconst int dj[4] = {0, 0, -1, 1};\nconst char dir_chars[4] = {'U', 'D', 'L', 'R'};\n\n// Estimated edge weights\ndouble h[GRID_SIZE][GRID_SIZE-1]; // horizontal edges (i, j) -> (i, j+1)\ndouble v[GRID_SIZE-1][GRID_SIZE]; // vertical edges (i, j) -> (i+1, j)\n\n// Count of observations for each edge (for confidence)\nint h_count[GRID_SIZE][GRID_SIZE-1];\nint v_count[GRID_SIZE-1][GRID_SIZE];\n\n// Sum of weighted observations for better averaging\ndouble h_sum[GRID_SIZE][GRID_SIZE-1];\ndouble v_sum[GRID_SIZE-1][GRID_SIZE];\n\nvoid init_weights() {\n    // Initialize with lower estimates to encourage exploration early on\n    for (int i = 0; i < GRID_SIZE; i++) {\n        for (int j = 0; j < GRID_SIZE-1; j++) {\n            h[i][j] = 3000.0;\n            h_count[i][j] = 0;\n            h_sum[i][j] = 0.0;\n        }\n    }\n    for (int i = 0; i < GRID_SIZE-1; i++) {\n        for (int j = 0; j < GRID_SIZE; j++) {\n            v[i][j] = 3000.0;\n            v_count[i][j] = 0;\n            v_sum[i][j] = 0.0;\n        }\n    }\n}\n\nstruct State {\n    int i, j;\n    double cost;\n    string path;\n    bool operator>(const State& other) const {\n        return cost > other.cost;\n    }\n};\n\nstring find_path(int si, int sj, int ti, int tj) {\n    // Dijkstra's algorithm\n    vector<vector<double>> dist(GRID_SIZE, vector<double>(GRID_SIZE, 1e18));\n    vector<vector<pair<int, int>>> prev(GRID_SIZE, vector<pair<int, int>>(GRID_SIZE, make_pair(-1, -1)));\n    vector<vector<char>> prev_dir(GRID_SIZE, vector<char>(GRID_SIZE, ' '));\n    \n    priority_queue<State, vector<State>, greater<State>> pq;\n    dist[si][sj] = 0;\n    pq.push({si, sj, 0, \"\"});\n    \n    while (!pq.empty()) {\n        auto [i, j, cost, path] = pq.top(); pq.pop();\n        if (cost > dist[i][j]) continue;\n        \n        // Try all 4 directions\n        for (int d = 0; d < 4; d++) {\n            int ni = i + di[d];\n            int nj = j + dj[d];\n            \n            if (ni >= 0 && ni < GRID_SIZE && nj >= 0 && nj < GRID_SIZE) {\n                double edge_cost = 0;\n                if (d == 0) { // Up: (i,j) -> (i-1,j)\n                    if (ni < i) edge_cost = v[ni][nj];\n                } else if (d == 1) { // Down: (i,j) -> (i+1,j)\n                    if (i < ni) edge_cost = v[i][j];\n                } else if (d == 2) { // Left: (i,j) -> (i,j-1)\n                    if (nj < j) edge_cost = h[ni][nj];\n                } else if (d == 3) { // Right: (i,j) -> (i,j+1)\n                    if (j < nj) edge_cost = h[i][j];\n                }\n                \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                    prev_dir[ni][nj] = dir_chars[d];\n                    pq.push({ni, nj, new_cost, \"\"});\n                }\n            }\n        }\n    }\n    \n    // Reconstruct path\n    string path;\n    int ci = ti, cj = tj;\n    while (ci != si || cj != sj) {\n        path += prev_dir[ci][cj];\n        auto p = prev[ci][cj];\n        ci = p.first;\n        cj = p.second;\n    }\n    reverse(path.begin(), path.end());\n    \n    return path;\n}\n\n// Store the last path for updating weights\nvector<pair<int, int>> last_path_coords;\nstring last_path;\n\nvoid store_path_info(int si, int sj, const string& path) {\n    last_path_coords.clear();\n    last_path_coords.push_back({si, sj});\n    \n    int ci = si, cj = sj;\n    for (char c : path) {\n        if (c == 'U') ci--;\n        else if (c == 'D') ci++;\n        else if (c == 'L') cj--;\n        else if (c == 'R') cj++;\n        last_path_coords.push_back({ci, cj});\n    }\n    last_path = path;\n}\n\nvoid update_weights(int actual_cost) {\n    // Get the edges used in the path\n    vector<pair<pair<int, int>, bool>> edges_used; // {{i,j}, is_vertical}\n    \n    for (int k = 0; k < (int)last_path.length(); k++) {\n        int i1 = last_path_coords[k].first;\n        int j1 = last_path_coords[k].second;\n        int i2 = last_path_coords[k+1].first;\n        int j2 = last_path_coords[k+1].second;\n        \n        if (i1 == i2) {\n            // Horizontal move\n            int j = min(j1, j2);\n            edges_used.push_back({{i1, j}, false});\n        } else {\n            // Vertical move\n            int i = min(i1, i2);\n            edges_used.push_back({{i, j1}, true});\n        }\n    }\n    \n    if (edges_used.empty()) return;\n    \n    // Calculate estimated cost\n    double estimated_cost = 0;\n    for (auto& edge : edges_used) {\n        auto [pos, is_vertical] = edge;\n        int i = pos.first, j = pos.second;\n        if (is_vertical) {\n            estimated_cost += v[i][j];\n        } else {\n            estimated_cost += h[i][j];\n        }\n    }\n    \n    if (estimated_cost <= 0) return;\n    \n    // Calculate error\n    double error = (double)actual_cost - estimated_cost;\n    \n    // Distribute error proportionally to edge weights\n    for (auto& edge : edges_used) {\n        auto [pos, is_vertical] = edge;\n        int i = pos.first, j = pos.second;\n        \n        double edge_weight = is_vertical ? v[i][j] : h[i][j];\n        double proportion = edge_weight / estimated_cost;\n        double adjustment = error * proportion;\n        \n        // Adaptive learning rate - higher confidence = slower learning\n        double confidence = is_vertical ? min(1.0, (double)v_count[i][j] / 10.0) : min(1.0, (double)h_count[i][j] / 10.0);\n        double learning_rate = 0.3 / (1.0 + confidence);\n        learning_rate = max(0.01, learning_rate); // Minimum learning rate\n        \n        double new_weight = edge_weight + adjustment * learning_rate;\n        \n        // Keep weights in reasonable bounds\n        new_weight = max(500.0, min(15000.0, new_weight));\n        \n        if (is_vertical) {\n            v_sum[i][j] += new_weight;\n            v_count[i][j]++;\n            // Use running average for more stable estimates\n            v[i][j] = v_sum[i][j] / v_count[i][j];\n        } else {\n            h_sum[i][j] += new_weight;\n            h_count[i][j]++;\n            // Use running average for more stable estimates\n            h[i][j] = h_sum[i][j] / h_count[i][j];\n        }\n    }\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    init_weights();\n    \n    for (int query = 0; query < MAX_QUERIES; query++) {\n        int si, sj, ti, tj;\n        cin >> si >> sj >> ti >> tj;\n        \n        string path = find_path(si, sj, ti, tj);\n        store_path_info(si, sj, path);\n        \n        cout << path << endl;\n        cout.flush();\n        \n        int result;\n        cin >> result;\n        \n        update_weights(result);\n    }\n    \n    return 0;\n}","ahc004":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 20;\nconst string CHARS = \"ABCDEFGH\";\n\nstruct StringInfo {\n    string s;\n    int id;\n    int length;\n};\n\n// Matrix representation\nvector<vector<char>> matrix(N, vector<char>(N, '.'));\nvector<string> strings;\n\n// Count how many strings are satisfied\nint countSatisfied(const vector<vector<char>>& mat, const vector<string>& strs) {\n    int count = 0;\n    for (const string& s : strs) {\n        bool found = false;\n        // Check horizontal\n        for (int i = 0; i < N && !found; i++) {\n            for (int j = 0; j < N && !found; j++) {\n                bool match = true;\n                for (int k = 0; k < (int)s.length(); k++) {\n                    int col = (j + k) % N;\n                    if (mat[i][col] != '.' && mat[i][col] != s[k]) {\n                        match = false;\n                        break;\n                    }\n                }\n                if (match) {\n                    found = true;\n                }\n            }\n        }\n        // Check vertical\n        for (int j = 0; j < N && !found; j++) {\n            for (int i = 0; i < N && !found; i++) {\n                bool match = true;\n                for (int k = 0; k < (int)s.length(); k++) {\n                    int row = (i + k) % N;\n                    if (mat[row][j] != '.' && mat[row][j] != s[k]) {\n                        match = false;\n                        break;\n                    }\n                }\n                if (match) {\n                    found = true;\n                }\n            }\n        }\n        if (found) count++;\n    }\n    return count;\n}\n\n// Check if a string can be placed at a specific position and direction\nbool canPlace(const vector<vector<char>>& mat, const string& s, int row, int col, bool vertical) {\n    if (vertical) {\n        for (int i = 0; i < (int)s.length(); i++) {\n            int r = (row + i) % N;\n            if (mat[r][col] != '.' && mat[r][col] != s[i]) {\n                return false;\n            }\n        }\n    } else {\n        for (int j = 0; j < (int)s.length(); j++) {\n            int c = (col + j) % N;\n            if (mat[row][c] != '.' && mat[row][c] != s[j]) {\n                return false;\n            }\n        }\n    }\n    return true;\n}\n\n// Place a string (modifies matrix)\nvoid placeString(vector<vector<char>>& mat, const string& s, int row, int col, bool vertical) {\n    if (vertical) {\n        for (int i = 0; i < (int)s.length(); i++) {\n            int r = (row + i) % N;\n            mat[r][col] = s[i];\n        }\n    } else {\n        for (int j = 0; j < (int)s.length(); j++) {\n            int c = (col + j) % N;\n            mat[row][c] = s[j];\n        }\n    }\n}\n\n// Try to place strings greedily\nvoid greedyPlacement() {\n    // Sort strings by length (longer first) and then by some heuristic\n    vector<pair<int, int>> stringScores;\n    for (int i = 0; i < (int)strings.size(); i++) {\n        stringScores.push_back({-strings[i].length(), i}); // negative for descending order\n    }\n    sort(stringScores.begin(), stringScores.end());\n    \n    vector<int> stringOrder;\n    for (auto& p : stringScores) {\n        stringOrder.push_back(p.second);\n    }\n    \n    // Try to place each string\n    for (int idx : stringOrder) {\n        const string& s = strings[idx];\n        bool placed = false;\n        \n        // Try all positions and directions\n        for (int dir = 0; dir < 2 && !placed; dir++) {\n            bool vertical = (dir == 1);\n            for (int i = 0; i < N && !placed; i++) {\n                for (int j = 0; j < N && !placed; j++) {\n                    if (canPlace(matrix, s, i, j, vertical)) {\n                        placeString(matrix, s, i, j, vertical);\n                        placed = true;\n                    }\n                }\n            }\n        }\n    }\n}\n\n// Local search improvement\nvoid localSearch() {\n    // Simple local search: try random changes and keep improvements\n    random_device rd;\n    mt19937 gen(rd());\n    uniform_int_distribution<> dis(0, N-1);\n    uniform_int_distribution<> char_dis(0, 7);\n    \n    int current_score = countSatisfied(matrix, strings);\n    \n    for (int iter = 0; iter < 5000; iter++) {\n        // Pick a random cell to modify\n        int i = dis(gen);\n        int j = dis(gen);\n        char original = matrix[i][j];\n        \n        // Try changing it to a random character or '.'\n        if (uniform_int_distribution<>(0, 4)(gen) == 0) {\n            matrix[i][j] = '.';\n        } else {\n            matrix[i][j] = CHARS[char_dis(gen)];\n        }\n        \n        int new_score = countSatisfied(matrix, strings);\n        if (new_score < current_score) {\n            // Revert change\n            matrix[i][j] = original;\n        } else {\n            current_score = new_score;\n        }\n    }\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    int M;\n    cin >> N >> M;\n    \n    strings.resize(M);\n    for (int i = 0; i < M; i++) {\n        cin >> strings[i];\n    }\n    \n    // Initialize matrix\n    matrix.assign(N, vector<char>(N, '.'));\n    \n    // Greedy placement\n    greedyPlacement();\n    \n    // Local search improvement\n    localSearch();\n    \n    // Output result\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cout << matrix[i][j];\n        }\n        cout << \"\\n\";\n    }\n    \n    return 0;\n}","ahc005":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MAX_N = 70;\nint N, si, sj;\nchar grid[MAX_N][MAX_N];\nbool is_road[MAX_N][MAX_N];\nint cost[MAX_N][MAX_N];\nvector<pair<int,int>> roads;\n\n// Directions: U, D, L, R\nconst int dx[4] = {-1, 1, 0, 0};\nconst int dy[4] = {0, 0, -1, 1};\nconst char dir_char[4] = {'U', 'D', 'L', 'R'};\n\n// Calculate which roads are visible from position (x, y)\nset<pair<int,int>> get_visible_roads(int x, int y) {\n    set<pair<int,int>> visible;\n    \n    if (!is_road[x][y]) return visible;\n    \n    // Horizontal visibility (same row)\n    // Look left\n    for (int j = y; j >= 0; j--) {\n        if (!is_road[x][j]) break;\n        visible.insert({x, j});\n    }\n    // Look right\n    for (int j = y; j < N; j++) {\n        if (!is_road[x][j]) break;\n        visible.insert({x, j});\n    }\n    \n    // Vertical visibility (same column)\n    // Look up\n    for (int i = x; i >= 0; i--) {\n        if (!is_road[i][y]) break;\n        visible.insert({i, y});\n    }\n    // Look down\n    for (int i = x; i < N; i++) {\n        if (!is_road[i][y]) break;\n        visible.insert({i, y});\n    }\n    \n    return visible;\n}\n\nbool is_valid(int x, int y) {\n    return x >= 0 && x < N && y >= 0 && y < N && is_road[x][y];\n}\n\n// BFS to find shortest path between two points\nvector<pair<int,int>> bfs_path(int sx, int sy, int tx, int ty) {\n    if (sx == tx && sy == ty) return {{sx, sy}};\n    if (!is_valid(sx, sy) || !is_valid(tx, ty)) return {{sx, sy}};\n    \n    queue<pair<int,int>> q;\n    bool visited[MAX_N][MAX_N] = {false};\n    pair<int,int> parent[MAX_N][MAX_N];\n    \n    q.push({sx, sy});\n    visited[sx][sy] = true;\n    \n    while (!q.empty()) {\n        auto [x, y] = q.front(); q.pop();\n        \n        for (int d = 0; d < 4; d++) {\n            int nx = x + dx[d];\n            int ny = y + dy[d];\n            \n            if (is_valid(nx, ny) && !visited[nx][ny]) {\n                visited[nx][ny] = true;\n                parent[nx][ny] = {x, y};\n                q.push({nx, ny});\n                \n                if (nx == tx && ny == ty) {\n                    // Reconstruct path\n                    vector<pair<int,int>> path;\n                    pair<int,int> current = {tx, ty};\n                    while (current.first != sx || current.second != sy) {\n                        path.push_back(current);\n                        current = parent[current.first][current.second];\n                    }\n                    path.push_back({sx, sy});\n                    reverse(path.begin(), path.end());\n                    return path;\n                }\n            }\n        }\n    }\n    \n    return {{sx, sy}}; // Fallback\n}\n\n// Convert path to direction string\nstring path_to_directions(const vector<pair<int,int>>& path) {\n    string result;\n    for (int i = 1; i < path.size(); i++) {\n        auto [prev_x, prev_y] = path[i-1];\n        auto [curr_x, curr_y] = path[i];\n        \n        if (curr_x == prev_x - 1 && curr_y == prev_y) result += 'U';\n        else if (curr_x == prev_x + 1 && curr_y == prev_y) result += 'D';\n        else if (curr_x == prev_x && curr_y == prev_y - 1) result += 'L';\n        else if (curr_x == prev_x && curr_y == prev_y + 1) result += 'R';\n    }\n    return result;\n}\n\n// Improved greedy approach with better coverage tracking\nstring solve_improved() {\n    // Track which roads need to be covered\n    set<pair<int,int>> uncovered_roads;\n    for (auto& road : roads) {\n        uncovered_roads.insert(road);\n    }\n    \n    vector<pair<int,int>> path_positions = {{si, sj}};\n    int current_x = si, current_y = sj;\n    \n    // Mark initially visible roads as covered\n    auto initial_visible = get_visible_roads(current_x, current_y);\n    for (auto& road : initial_visible) {\n        uncovered_roads.erase(road);\n    }\n    \n    const int MAX_ITERATIONS = 1500;\n    int iterations = 0;\n    \n    // Greedy selection: at each step, choose the position that covers most uncovered roads\n    while (!uncovered_roads.empty() && iterations < MAX_ITERATIONS) {\n        pair<int,int> best_next = {-1, -1};\n        int best_coverage = -1;\n        vector<pair<int,int>> best_path;\n        \n        // Consider moving to nearby uncovered roads\n        vector<pair<int,int>> candidates;\n        for (auto& road : uncovered_roads) {\n            candidates.push_back(road);\n            if (candidates.size() >= 50) break; // Limit candidates for performance\n        }\n        \n        // Also consider some random road positions for exploration\n        for (int i = 0; i < min(20, (int)roads.size()); i++) {\n            candidates.push_back(roads[rand() % roads.size()]);\n        }\n        \n        for (auto& candidate : candidates) {\n            // Find path to candidate\n            auto path_to_candidate = bfs_path(current_x, current_y, candidate.first, candidate.second);\n            if (path_to_candidate.size() <= 1) continue;\n            \n            // Calculate coverage from candidate position\n            auto visible_from_candidate = get_visible_roads(candidate.first, candidate.second);\n            int coverage_count = 0;\n            for (auto& visible : visible_from_candidate) {\n                if (uncovered_roads.count(visible)) {\n                    coverage_count++;\n                }\n            }\n            \n            // Prefer moves that cover more new roads, but penalize long paths\n            int score = coverage_count * 100 - (int)path_to_candidate.size();\n            if (score > best_coverage) {\n                best_coverage = score;\n                best_next = candidate;\n                best_path = path_to_candidate;\n            }\n        }\n        \n        if (best_next.first == -1 || best_path.size() <= 1) {\n            // No good move found, try to go to any uncovered road\n            if (!uncovered_roads.empty()) {\n                auto target = *uncovered_roads.begin();\n                auto path_to_target = bfs_path(current_x, current_y, target.first, target.second);\n                best_path = path_to_target;\n                best_next = target;\n            } else {\n                break;\n            }\n        }\n        \n        // Execute the move\n        for (int i = 1; i < best_path.size(); i++) {\n            path_positions.push_back(best_path[i]);\n        }\n        current_x = best_path.back().first;\n        current_y = best_path.back().second;\n        \n        // Update uncovered roads\n        auto visible_now = get_visible_roads(current_x, current_y);\n        for (auto& visible : visible_now) {\n            uncovered_roads.erase(visible);\n        }\n        \n        iterations++;\n    }\n    \n    // Return to start\n    auto return_path = bfs_path(current_x, current_y, si, sj);\n    for (int i = 1; i < return_path.size(); i++) {\n        path_positions.push_back(return_path[i]);\n    }\n    \n    return path_to_directions(path_positions);\n}\n\n// Spiral approach for better coverage\nstring solve_spiral() {\n    vector<pair<int,int>> path_positions = {{si, sj}};\n    int current_x = si, current_y = sj;\n    \n    set<pair<int,int>> visited;\n    visited.insert({si, sj});\n    \n    // Simple spiral-like movement\n    int steps = 0;\n    const int MAX_STEPS = 1000;\n    \n    while (steps < MAX_STEPS) {\n        // Try moves in all directions\n        bool moved = false;\n        for (int d = 0; d < 4; d++) {\n            int nx = current_x + dx[d];\n            int ny = current_y + dy[d];\n            \n            if (is_valid(nx, ny) && !visited.count({nx, ny})) {\n                current_x = nx;\n                current_y = ny;\n                path_positions.push_back({current_x, current_y});\n                visited.insert({current_x, current_y});\n                moved = true;\n                break;\n            }\n        }\n        \n        // If can't move to unvisited, move anyway\n        if (!moved) {\n            for (int d = 0; d < 4; d++) {\n                int nx = current_x + dx[d];\n                int ny = current_y + dy[d];\n                if (is_valid(nx, ny)) {\n                    current_x = nx;\n                    current_y = ny;\n                    path_positions.push_back({current_x, current_y});\n                    moved = true;\n                    break;\n                }\n            }\n        }\n        \n        if (!moved) break;\n        steps++;\n    }\n    \n    // Return to start\n    auto return_path = bfs_path(current_x, current_y, si, sj);\n    for (int i = 1; i < return_path.size(); i++) {\n        path_positions.push_back(return_path[i]);\n    }\n    \n    return path_to_directions(path_positions);\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    srand(time(NULL));\n    \n    cin >> N >> si >> sj;\n    \n    for (int i = 0; i < N; i++) {\n        string line;\n        cin >> line;\n        for (int j = 0; j < N; j++) {\n            grid[i][j] = line[j];\n            if (line[j] >= '5' && line[j] <= '9') {\n                is_road[i][j] = true;\n                cost[i][j] = line[j] - '0';\n                roads.push_back({i, j});\n            } else {\n                is_road[i][j] = false;\n            }\n        }\n    }\n    \n    // Try different approaches\n    string result1 = solve_improved();\n    string result2 = solve_spiral();\n    \n    // Output the longer one (likely to have better coverage)\n    if (result1.length() > result2.length()) {\n        cout << result1 << endl;\n    } else {\n        cout << result2 << endl;\n    }\n    \n    return 0;\n}","future-contest-2022-qual":"#include <bits/stdc++.h>\n#include <atcoder/all>\nusing namespace std;\nusing namespace atcoder;\n\n// Constants\nconst int MAX_N = 1000;\nconst int MAX_M = 20;\nconst int MAX_DAYS = 2000;\n\n// Global variables\nint N, M, K, R;\nvector<vector<int>> task_difficulty;  // d[i][k]\nvector<vector<int>> task_dependencies; // dependencies for each task\nvector<vector<int>> reverse_dependencies; // which tasks depend on this task\nvector<vector<int>> completion_times; // actual completion times (hidden)\nvector<vector<int>> skill_estimates;  // s[j][k] - our estimates\nvector<int> task_status;  // -1: not started, 0: in progress, 1: completed\nvector<int> member_current_task; // which task each member is working on\nvector<int> task_start_day; // when each task started\nvector<int> in_degree; // in-degree for topological sorting\nvector<bool> task_available; // whether task can be started (dependencies done)\npriority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> task_queue; // priority queue for tasks\n\n// Initialize data structures\nvoid initialize() {\n    task_difficulty.assign(N, vector<int>(K));\n    task_dependencies.assign(N, vector<int>());\n    reverse_dependencies.assign(N, vector<int>());\n    completion_times.assign(N, vector<int>(M));\n    skill_estimates.assign(M, vector<int>(K, 10)); // initial guess\n    task_status.assign(N, -1);\n    member_current_task.assign(M, -1);\n    task_start_day.assign(N, -1);\n    in_degree.assign(N, 0);\n    task_available.assign(N, false);\n}\n\n// Calculate estimated time for member j to complete task i\nint estimate_time(int task_id, int member_id) {\n    int w = 0;\n    for (int k = 0; k < K; k++) {\n        w += max(0, task_difficulty[task_id][k] - skill_estimates[member_id][k]);\n    }\n    if (w == 0) return 1;\n    return max(1, w + 1); // conservative estimate (middle of random range)\n}\n\n// Update skill estimates based on actual performance\nvoid update_skills(int task_id, int member_id, int actual_time, int days_taken) {\n    // Only update if we have meaningful data\n    if (days_taken <= 0) return;\n    \n    int w = 0;\n    for (int k = 0; k < K; k++) {\n        w += max(0, task_difficulty[task_id][k] - skill_estimates[member_id][k]);\n    }\n    \n    // Estimate actual w based on observed time\n    int estimated_w = (actual_time == 1) ? 0 : (actual_time - 1); // reverse engineer w\n    \n    // Adjust skills based on difference\n    if (w > estimated_w) {\n        // Member is better than we thought - increase skill estimates\n        for (int k = 0; k < K; k++) {\n            if (task_difficulty[task_id][k] > skill_estimates[member_id][k]) {\n                skill_estimates[member_id][k] = min(skill_estimates[member_id][k] + 1, \n                                                   task_difficulty[task_id][k] + 5);\n            }\n        }\n    } else if (w < estimated_w) {\n        // Member is worse than we thought - but be conservative\n        // Since we don't know exact breakdown, make small adjustments\n    }\n}\n\n// Check which tasks are available to start (all dependencies completed)\nvoid update_available_tasks() {\n    for (int i = 0; i < N; i++) {\n        if (task_status[i] == -1 && !task_available[i]) {\n            bool can_start = true;\n            for (int dep : task_dependencies[i]) {\n                if (task_status[dep] != 1) {\n                    can_start = false;\n                    break;\n                }\n            }\n            if (can_start) {\n                task_available[i] = true;\n                // Priority: tasks that take longer (to get them started early)\n                int max_time = 0;\n                for (int j = 0; j < M; j++) {\n                    max_time = max(max_time, estimate_time(i, j));\n                }\n                task_queue.push({-max_time, i}); // negative for max-heap behavior\n            }\n        }\n    }\n}\n\nint main() {\n    // Read initial information\n    cin >> N >> M >> K >> R;\n    initialize();\n    \n    // Read task difficulties\n    for (int i = 0; i < N; i++) {\n        for (int k = 0; k < K; k++) {\n            cin >> task_difficulty[i][k];\n        }\n    }\n    \n    // Read dependencies\n    for (int i = 0; i < R; i++) {\n        int u, v;\n        cin >> u >> v;\n        u--; v--; // Convert to 0-indexed\n        task_dependencies[v].push_back(u);\n        reverse_dependencies[u].push_back(v);\n        in_degree[v]++;\n    }\n    \n    // Initialize available tasks (those with no dependencies)\n    update_available_tasks();\n    \n    int day = 0;\n    while (day < MAX_DAYS) {\n        day++;\n        \n        // Prepare assignments for this day\n        vector<pair<int, int>> assignments; // {member, task}\n        vector<bool> member_busy(M, false);\n        \n        // Assign tasks to free members\n        for (int member = 0; member < M; member++) {\n            if (member_current_task[member] == -1) { // Member is free\n                // Find best task to assign\n                vector<pair<int, int>> candidates; // {estimated_time, task_id}\n                \n                // Look through available tasks\n                priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> temp_queue = task_queue;\n                int checked = 0;\n                while (!task_queue.empty() && checked < 20) { // Limit search\n                    auto [neg_priority, task_id] = task_queue.top();\n                    task_queue.pop();\n                    if (task_status[task_id] == -1 && task_available[task_id]) {\n                        int est_time = estimate_time(task_id, member);\n                        candidates.push_back({est_time, task_id});\n                    }\n                    checked++;\n                }\n                \n                if (!candidates.empty()) {\n                    // Sort by estimated time (prefer shorter tasks)\n                    sort(candidates.begin(), candidates.end());\n                    int best_task = candidates[0].second;\n                    \n                    assignments.push_back({member, best_task});\n                    member_current_task[member] = best_task;\n                    task_status[best_task] = 0;\n                    task_start_day[best_task] = day;\n                }\n            }\n        }\n        \n        // Output assignments\n        cout << assignments.size();\n        for (auto& assignment : assignments) {\n            cout << \" \" << (assignment.first + 1) << \" \" << (assignment.second + 1);\n        }\n        cout << endl;\n        cout.flush();\n        \n        // Read completion information\n        int completion_count;\n        cin >> completion_count;\n        \n        if (completion_count == -1) {\n            // All tasks completed or time limit reached\n            break;\n        }\n        \n        if (completion_count == 0) {\n            // No completions, continue to next day\n            continue;\n        }\n        \n        // Process completed tasks\n        for (int i = 0; i < completion_count; i++) {\n            int member_id;\n            cin >> member_id;\n            member_id--; // Convert to 0-indexed\n            \n            int task_id = member_current_task[member_id];\n            if (task_id != -1) {\n                task_status[task_id] = 1;\n                member_current_task[member_id] = -1;\n                \n                // Update skills based on performance\n                int days_taken = day - task_start_day[task_id];\n                // We don't know actual time, but we can make educated guesses\n                update_skills(task_id, member_id, completion_times[task_id][member_id], days_taken);\n                \n                // Make dependent tasks available\n                for (int dependent : reverse_dependencies[task_id]) {\n                    in_degree[dependent]--;\n                    if (in_degree[dependent] == 0) {\n                        task_available[dependent] = true;\n                        int max_time = 0;\n                        for (int j = 0; j < M; j++) {\n                            max_time = max(max_time, estimate_time(dependent, j));\n                        }\n                        task_queue.push({-max_time, dependent});\n                    }\n                }\n            }\n        }\n        \n        // Update available tasks for next assignments\n        update_available_tasks();\n    }\n    \n    return 0;\n}","ahc006":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int DEPOT_X = 400;\nconst int DEPOT_Y = 400;\nconst int NUM_ORDERS = 1000;\nconst int SELECT_COUNT = 50;\n\nstruct Order {\n    int id, a, b, c, d;\n    \n    Order(int _id, int _a, int _b, int _c, int _d) \n        : id(_id), a(_a), b(_b), c(_c), d(_d) {}\n    \n    int distance() const {\n        return abs(a - c) + abs(b - d);\n    }\n};\n\nstruct Point {\n    int x, y;\n    Point() : x(0), y(0) {}\n    Point(int _x, int _y) : x(_x), y(_y) {}\n    \n    int distance(const Point& other) const {\n        return abs(x - other.x) + abs(y - other.y);\n    }\n};\n\n// Calculate total distance of a route\nlong long calculateRouteDistance(const vector<Point>& route) {\n    long long total = 0;\n    for (size_t i = 0; i + 1 < route.size(); i++) {\n        total += route[i].distance(route[i + 1]);\n    }\n    return total;\n}\n\n// Select orders based on a combination of distance and centrality\nvector<int> selectOrders(const vector<Order>& orders) {\n    vector<pair<int, int>> scores; // {score, id}\n    \n    for (int i = 0; i < NUM_ORDERS; i++) {\n        const Order& order = orders[i];\n        \n        // Direct distance\n        int order_dist = order.distance();\n        \n        // Distance from depot to the \"center\" of the order\n        int center_x = (order.a + order.c) / 2;\n        int center_y = (order.b + order.d) / 2;\n        int depot_dist = abs(DEPOT_X - center_x) + abs(DEPOT_Y - center_y);\n        \n        // Score: prefer orders that are not too far from depot but have reasonable distance\n        // Lower score is better\n        int score = order_dist + depot_dist / 2;\n        \n        scores.push_back({score, i});\n    }\n    \n    sort(scores.begin(), scores.end());\n    \n    vector<int> selected;\n    for (int i = 0; i < SELECT_COUNT; i++) {\n        selected.push_back(scores[i].second);\n    }\n    \n    return selected;\n}\n\n// Simple insertion heuristic for TSP with pickup-delivery constraints\nvector<Point> buildRoute(const vector<Order>& orders, const vector<int>& selected_ids) {\n    vector<Point> route = {Point(DEPOT_X, DEPOT_Y)};\n    \n    // For each selected order, we need to insert both pickup and delivery\n    vector<pair<Point, Point>> order_points; // {pickup, delivery}\n    for (int id : selected_ids) {\n        order_points.push_back({Point(orders[id].a, orders[id].b), Point(orders[id].c, orders[id].d)});\n    }\n    \n    // Insert orders one by one using cheapest insertion\n    vector<bool> inserted(selected_ids.size(), false);\n    vector<int> order_sequence; // sequence of inserted order indices\n    \n    for (int i = 0; i < (int)selected_ids.size(); i++) {\n        int best_order = -1;\n        int best_pos = -1;\n        int best_cost = 1e9;\n        \n        // Try inserting each remaining order\n        for (size_t j = 0; j < selected_ids.size(); j++) {\n            if (inserted[j]) continue;\n            \n            // Try all positions to insert this order (pickup followed by delivery)\n            for (size_t pos = 0; pos < route.size(); pos++) {\n                // Insert pickup at pos, delivery at some position after pos\n                for (size_t delivery_pos = pos + 1; delivery_pos <= route.size(); delivery_pos++) {\n                    Point pickup = order_points[j].first;\n                    Point delivery = order_points[j].second;\n                    \n                    // Calculate insertion cost\n                    int cost = 0;\n                    \n                    // Cost to insert pickup\n                    if (pos == 0) {\n                        cost += route[0].distance(pickup);\n                    } else {\n                        cost -= route[pos-1].distance(route[pos]);\n                        cost += route[pos-1].distance(pickup);\n                        cost += pickup.distance(route[pos]);\n                    }\n                    \n                    // Cost to insert delivery\n                    if (delivery_pos == route.size()) {\n                        cost += route.back().distance(delivery);\n                    } else {\n                        cost -= route[delivery_pos-1].distance(route[delivery_pos]);\n                        cost += route[delivery_pos-1].distance(delivery);\n                        cost += delivery.distance(route[delivery_pos]);\n                    }\n                    \n                    if (cost < best_cost) {\n                        best_cost = cost;\n                        best_order = j;\n                        best_pos = pos;\n                    }\n                }\n            }\n        }\n        \n        if (best_order != -1) {\n            // Actually insert the order\n            Point pickup = order_points[best_order].first;\n            Point delivery = order_points[best_order].second;\n            \n            // Simple approach: insert pickup at beginning and delivery at end\n            // This is faster and still reasonable\n            route.insert(route.begin() + 1, pickup);\n            route.insert(route.end() - 1, delivery);\n            \n            inserted[best_order] = true;\n            order_sequence.push_back(best_order);\n        }\n    }\n    \n    // Much simpler approach: just append all pickups, then all deliveries\n    route.clear();\n    route.push_back(Point(DEPOT_X, DEPOT_Y));\n    \n    // Add all pickups first\n    for (size_t i = 0; i < selected_ids.size(); i++) {\n        route.push_back(order_points[i].first);\n    }\n    \n    // Add all deliveries\n    for (size_t i = 0; i < selected_ids.size(); i++) {\n        route.push_back(order_points[i].second);\n    }\n    \n    route.push_back(Point(DEPOT_X, DEPOT_Y));\n    \n    return route;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    vector<Order> orders;\n    \n    // Read input\n    for (int i = 0; i < NUM_ORDERS; i++) {\n        int a, b, c, d;\n        cin >> a >> b >> c >> d;\n        orders.emplace_back(i, a, b, c, d);\n    }\n    \n    // Select orders\n    vector<int> selected_ids = selectOrders(orders);\n    \n    // Build route\n    vector<Point> route = buildRoute(orders, selected_ids);\n    \n    // Output\n    cout << SELECT_COUNT;\n    for (int id : selected_ids) {\n        cout << \" \" << (id + 1); // 1-indexed\n    }\n    cout << \"\\n\";\n    \n    cout << route.size();\n    for (const Point& p : route) {\n        cout << \" \" << p.x << \" \" << p.y;\n    }\n    cout << \"\\n\";\n    \n    return 0;\n}","ahc007":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 400;\nconst int M = 1995;\nconst int TARGET_EDGES = N - 1;  // 399 edges needed for spanning tree\n\nclass UnionFind {\nprivate:\n    vector<int> parent, rank, size;\npublic:\n    UnionFind(int n) : parent(n), rank(n, 0), size(n, 1) {\n        for (int i = 0; i < n; i++) {\n            parent[i] = i;\n        }\n    }\n    \n    int find(int x) {\n        if (parent[x] != x) {\n            parent[x] = find(parent[x]);\n        }\n        return parent[x];\n    }\n    \n    bool unite(int x, int y) {\n        x = find(x);\n        y = find(y);\n        if (x == y) return false;\n        if (rank[x] < rank[y]) swap(x, y);\n        parent[y] = x;\n        size[x] += size[y];\n        if (rank[x] == rank[y]) rank[x]++;\n        return true;\n    }\n    \n    bool same(int x, int y) {\n        return find(x) == find(y);\n    }\n    \n    int get_size(int x) {\n        return size[find(x)];\n    }\n    \n    int count_components() {\n        int count = 0;\n        for (int i = 0; i < N; i++) {\n            if (find(i) == i) count++;\n        }\n        return count;\n    }\n};\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    // Read coordinates\n    vector<pair<int, int>> coords(N);\n    for (int i = 0; i < N; i++) {\n        cin >> coords[i].first >> coords[i].second;\n    }\n    \n    // Read edges\n    vector<pair<int, int>> edges(M);\n    for (int i = 0; i < M; i++) {\n        cin >> edges[i].first >> edges[i].second;\n    }\n    \n    // Precompute minimum distances\n    vector<int> min_distances(M);\n    for (int i = 0; i < M; i++) {\n        int u = edges[i].first;\n        int v = edges[i].second;\n        long long dx = (long long)(coords[u].first - coords[v].first);\n        long long dy = (long long)(coords[u].second - coords[v].second);\n        long long dist_sq = dx * dx + dy * dy;\n        double dist = sqrt((double)dist_sq);\n        min_distances[i] = (int)round(dist);\n    }\n    \n    // Initialize Union-Find\n    UnionFind uf(N);\n    int edges_accepted = 0;\n    \n    // Process edges as they arrive\n    for (int i = 0; i < M; i++) {\n        int length;\n        cin >> length;\n        \n        int u = edges[i].first;\n        int v = edges[i].second;\n        int min_dist = min_distances[i];\n        \n        // Check if this edge connects two different components\n        if (!uf.same(u, v)) {\n            // This edge reduces the number of components - accept it\n            uf.unite(u, v);\n            edges_accepted++;\n            cout << \"1\" << endl;\n            cout.flush();\n        } else {\n            // This edge creates a cycle - be more selective\n            double ratio = (double)length / min_dist;\n            \n            // Calculate how many more edges we can afford to take\n            int components_remaining = uf.count_components() - 1;\n            int edges_needed = components_remaining;\n            int edges_left = M - i - 1;\n            \n            // If we're close to the end and still need edges, be more aggressive\n            if (edges_needed > 0 && edges_left <= edges_needed + 50) {\n                // We're running out of edges - accept reasonable ones\n                if (ratio <= 1.5) {\n                    cout << \"1\" << endl;\n                    cout.flush();\n                } else if (ratio <= 2.0 && edges_left <= edges_needed + 20) {\n                    cout << \"1\" << endl;\n                    cout.flush();\n                } else {\n                    cout << \"0\" << endl;\n                    cout.flush();\n                }\n            } else {\n                // Early/mid game - be more selective\n                if (ratio <= 1.2) {\n                    cout << \"1\" << endl;\n                    cout.flush();\n                } else if (ratio <= 1.5 && (min_dist < 20)) {\n                    // Short edges are valuable even if ratio isn't perfect\n                    cout << \"1\" << endl;\n                    cout.flush();\n                } else {\n                    cout << \"0\" << endl;\n                    cout.flush();\n                }\n            }\n        }\n    }\n    \n    return 0;\n}","ahc008":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int GRID_SIZE = 30;\nconst int MAX_TURNS = 300;\nconst int dx[4] = {-1, 1, 0, 0}; // U, D, L, R\nconst int dy[4] = {0, 0, -1, 1};\nconst char dir_chars[4] = {'u', 'd', 'l', 'r'};\nconst char move_chars[4] = {'U', 'D', 'L', 'R'};\n\nstruct Position {\n    int x, y;\n    Position() : x(0), y(0) {}\n    Position(int _x, int _y) : x(_x), y(_y) {}\n    bool operator==(const Position& other) const {\n        return x == other.x && y == other.y;\n    }\n};\n\nstruct Pet {\n    Position pos;\n    int type;\n    \n    Pet() : pos(0, 0), type(0) {}\n    Pet(int x, int y, int t) : pos(x, y), type(t) {}\n};\n\nstruct Human {\n    Position pos;\n    \n    Human() : pos(0, 0) {}\n    Human(int x, int y) : pos(x, y) {}\n};\n\nclass Grid {\npublic:\n    bool blocked[GRID_SIZE][GRID_SIZE];\n    bool pet_present[GRID_SIZE][GRID_SIZE];\n    bool human_present[GRID_SIZE][GRID_SIZE];\n    \n    Grid() {\n        memset(blocked, false, sizeof(blocked));\n        memset(pet_present, false, sizeof(pet_present));\n        memset(human_present, false, sizeof(human_present));\n    }\n    \n    void reset_presence() {\n        memset(pet_present, false, sizeof(pet_present));\n        memset(human_present, false, sizeof(human_present));\n    }\n    \n    bool is_valid(int x, int y) {\n        return x >= 0 && x < GRID_SIZE && y >= 0 && y < GRID_SIZE;\n    }\n    \n    bool is_passable(int x, int y) {\n        return is_valid(x, y) && !blocked[x][y];\n    }\n    \n    void block_square(int x, int y) {\n        if (is_valid(x, y)) {\n            blocked[x][y] = true;\n        }\n    }\n};\n\nclass Solver {\nprivate:\n    int N, M;\n    vector<Pet> pets;\n    vector<Human> humans;\n    Grid grid;\n    mt19937 rng;\n    \npublic:\n    Solver() : rng(chrono::steady_clock::now().time_since_epoch().count()) {}\n    \n    void read_input() {\n        cin >> N;\n        pets.resize(N);\n        for (int i = 0; i < N; i++) {\n            int x, y, t;\n            cin >> x >> y >> t;\n            pets[i] = Pet(x-1, y-1, t); // Convert to 0-indexed\n        }\n        \n        cin >> M;\n        humans.resize(M);\n        for (int i = 0; i < M; i++) {\n            int x, y;\n            cin >> x >> y;\n            humans[i] = Human(x-1, y-1); // Convert to 0-indexed\n        }\n    }\n    \n    bool is_adjacent_to_pet(int x, int y) {\n        for (int d = 0; d < 4; d++) {\n            int nx = x + dx[d];\n            int ny = y + dy[d];\n            if (grid.is_valid(nx, ny)) {\n                for (const auto& pet : pets) {\n                    if (pet.pos.x == nx && pet.pos.y == ny) {\n                        return true;\n                    }\n                }\n            }\n        }\n        return false;\n    }\n    \n    bool is_adjacent_to_any_pet(int x, int y) {\n        if (!grid.is_valid(x, y)) return true;\n        \n        for (const auto& pet : pets) {\n            if (abs(pet.pos.x - x) + abs(pet.pos.y - y) <= 1) {\n                return true;\n            }\n        }\n        return false;\n    }\n    \n    string get_action(int human_id) {\n        Human& human = humans[human_id];\n        \n        // Check if we're adjacent to any pet - if so, better not block\n        if (is_adjacent_to_any_pet(human.pos.x, human.pos.y)) {\n            // Try to move away from pets\n            for (int d = 0; d < 4; d++) {\n                int nx = human.pos.x + dx[d];\n                int ny = human.pos.y + dy[d];\n                \n                if (grid.is_passable(nx, ny) && !is_adjacent_to_any_pet(nx, ny)) {\n                    return string(1, move_chars[d]);\n                }\n            }\n            return \".\"; // Stay put if no good move\n        }\n        \n        // Try to block squares that are adjacent to pets but not adjacent to any pet currently\n        for (int d = 0; d < 4; d++) {\n            int nx = human.pos.x + dx[d];\n            int ny = human.pos.y + dy[d];\n            \n            if (grid.is_valid(nx, ny) && !grid.blocked[nx][ny]) {\n                // Check if this square is adjacent to any pet\n                bool adjacent_to_pet = false;\n                for (const auto& pet : pets) {\n                    if (abs(pet.pos.x - nx) + abs(pet.pos.y - ny) <= 1) {\n                        adjacent_to_pet = true;\n                        break;\n                    }\n                }\n                \n                // But make sure it's not adjacent to the pet we're blocking (to avoid blocking pet itself)\n                if (adjacent_to_pet && !is_adjacent_to_any_pet(nx, ny)) {\n                    return string(1, dir_chars[d]); // Block this square\n                }\n            }\n        }\n        \n        // If no good blocking move, try to move to a strategic position\n        // Prefer moving towards areas with more pets but not too close\n        vector<pair<int, int>> candidates;\n        for (int d = 0; d < 4; d++) {\n            int nx = human.pos.x + dx[d];\n            int ny = human.pos.y + dy[d];\n            \n            if (grid.is_passable(nx, ny) && !is_adjacent_to_any_pet(nx, ny)) {\n                // Count nearby pets\n                int nearby_pets = 0;\n                for (const auto& pet : pets) {\n                    if (abs(pet.pos.x - nx) + abs(pet.pos.y - ny) <= 3) {\n                        nearby_pets++;\n                    }\n                }\n                candidates.push_back({nearby_pets, d});\n            }\n        }\n        \n        if (!candidates.empty()) {\n            sort(candidates.begin(), candidates.end(), greater<pair<int, int>>());\n            return string(1, move_chars[candidates[0].second]);\n        }\n        \n        // Default: do nothing\n        return \".\";\n    }\n    \n    void update_grid_state(const string& actions) {\n        // Process blocking actions first\n        for (int i = 0; i < M; i++) {\n            char action = actions[i];\n            if (action == 'u' || action == 'd' || action == 'l' || action == 'r') {\n                int d = -1;\n                for (int j = 0; j < 4; j++) {\n                    if (dir_chars[j] == action) {\n                        d = j;\n                        break;\n                    }\n                }\n                if (d != -1) {\n                    int nx = humans[i].pos.x + dx[d];\n                    int ny = humans[i].pos.y + dy[d];\n                    if (grid.is_valid(nx, ny)) {\n                        grid.block_square(nx, ny);\n                    }\n                }\n            }\n        }\n        \n        // Then process movement actions\n        for (int i = 0; i < M; i++) {\n            char action = actions[i];\n            if (action == 'U' || action == 'D' || action == 'L' || action == 'R') {\n                int d = -1;\n                for (int j = 0; j < 4; j++) {\n                    if (move_chars[j] == action) {\n                        d = j;\n                        break;\n                    }\n                }\n                if (d != -1) {\n                    int nx = humans[i].pos.x + dx[d];\n                    int ny = humans[i].pos.y + dy[d];\n                    if (grid.is_passable(nx, ny)) {\n                        humans[i].pos.x = nx;\n                        humans[i].pos.y = ny;\n                    }\n                }\n            }\n        }\n    }\n    \n    void update_positions(const vector<string>& pet_moves) {\n        // Update pet positions based on their moves\n        for (int i = 0; i < N; i++) {\n            const string& moves = pet_moves[i];\n            for (char move : moves) {\n                if (move == 'U' && pets[i].pos.x > 0) pets[i].pos.x--;\n                else if (move == 'D' && pets[i].pos.x < GRID_SIZE - 1) pets[i].pos.x++;\n                else if (move == 'L' && pets[i].pos.y > 0) pets[i].pos.y--;\n                else if (move == 'R' && pets[i].pos.y < GRID_SIZE - 1) pets[i].pos.y++;\n            }\n        }\n    }\n    \n    void update_grid_presence() {\n        grid.reset_presence();\n        for (const auto& pet : pets) {\n            if (grid.is_valid(pet.pos.x, pet.pos.y)) {\n                grid.pet_present[pet.pos.x][pet.pos.y] = true;\n            }\n        }\n        for (const auto& human : humans) {\n            if (grid.is_valid(human.pos.x, human.pos.y)) {\n                grid.human_present[human.pos.x][human.pos.y] = true;\n            }\n        }\n    }\n    \n    void solve() {\n        read_input();\n        \n        // Initialize grid presence\n        update_grid_presence();\n        \n        for (int turn = 0; turn < MAX_TURNS; turn++) {\n            string actions = \"\";\n            for (int i = 0; i < M; i++) {\n                actions += get_action(i);\n            }\n            cout << actions << endl;\n            cout.flush();\n            \n            // Update grid based on human actions\n            update_grid_state(actions);\n            \n            // Read pet moves\n            vector<string> pet_moves(N);\n            for (int i = 0; i < N; i++) {\n                cin >> pet_moves[i];\n            }\n            \n            // Update positions\n            update_positions(pet_moves);\n            \n            // Update grid presence\n            update_grid_presence();\n        }\n    }\n};\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    Solver solver;\n    solver.solve();\n    \n    return 0;\n}","ahc009":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int SIZE = 20;\nconst int MAX_LENGTH = 200;\n\n// Directions: U, D, L, R\nconst int dr[] = {-1, 1, 0, 0};\nconst int dc[] = {0, 0, -1, 1};\nconst char dir_chars[] = {'U', 'D', 'L', 'R'};\n\nint si, sj, ti, tj;\ndouble p;\nbool h_walls[SIZE][SIZE-1]; // horizontal walls\nbool v_walls[SIZE-1][SIZE]; // vertical walls\n\nbool can_move(int r, int c, int dir) {\n    if (dir == 0) { // Up\n        if (r == 0) return false;\n        return !v_walls[r-1][c];\n    } else if (dir == 1) { // Down\n        if (r == SIZE-1) return false;\n        return !v_walls[r][c];\n    } else if (dir == 2) { // Left\n        if (c == 0) return false;\n        return !h_walls[r][c-1];\n    } else { // Right\n        if (c == SIZE-1) return false;\n        return !h_walls[r][c];\n    }\n}\n\n// Find shortest path using BFS\nstring find_shortest_path() {\n    queue<tuple<int, int, string>> q;\n    bool visited[SIZE][SIZE] = {false};\n    \n    q.push({si, sj, \"\"});\n    visited[si][sj] = true;\n    \n    while (!q.empty()) {\n        auto [r, c, path] = q.front();\n        q.pop();\n        \n        if (r == ti && c == tj) {\n            return path;\n        }\n        \n        if (path.length() > MAX_LENGTH) continue;\n        \n        // Try all 4 directions\n        for (int dir = 0; dir < 4; dir++) {\n            int nr = r + dr[dir];\n            int nc = c + dc[dir];\n            \n            if (nr >= 0 && nr < SIZE && nc >= 0 && nc < SIZE && !visited[nr][nc]) {\n                if (can_move(r, c, dir)) {\n                    visited[nr][nc] = true;\n                    string new_path = path + dir_chars[dir];\n                    if (new_path.length() <= MAX_LENGTH) {\n                        q.push({nr, nc, new_path});\n                    }\n                }\n            }\n        }\n    }\n    \n    return \"\"; // Should not happen\n}\n\n// Create a robust path by repeating segments\nstring create_robust_path() {\n    string base_path = find_shortest_path();\n    \n    // If base path is already quite long, return as is\n    if (base_path.length() > 150) {\n        return base_path;\n    }\n    \n    // Create a more robust version by repeating the path\n    string robust_path = \"\";\n    \n    // First, add the base path\n    robust_path += base_path;\n    \n    // Then add it again for redundancy (but be smart about it)\n    if (robust_path.length() + base_path.length() <= MAX_LENGTH) {\n        robust_path += base_path;\n    }\n    \n    // If we still have room, add a simple pattern\n    while (robust_path.length() + 4 <= MAX_LENGTH) {\n        // Add a simple back-and-forth pattern that should help with robustness\n        if (ti > si && tj > sj) {\n            // If target is southeast, add some DR patterns\n            if (robust_path.length() + 4 <= MAX_LENGTH) {\n                robust_path += \"DRRD\";\n            }\n        } else if (ti > si && tj < sj) {\n            // If target is southwest, add some DL patterns\n            if (robust_path.length() + 4 <= MAX_LENGTH) {\n                robust_path += \"DLLD\";\n            }\n        } else if (ti < si && tj > sj) {\n            // If target is northeast, add some UR patterns\n            if (robust_path.length() + 4 <= MAX_LENGTH) {\n                robust_path += \"URRU\";\n            }\n        } else if (ti < si && tj < sj) {\n            // If target is northwest, add some UL patterns\n            if (robust_path.length() + 4 <= MAX_LENGTH) {\n                robust_path += \"ULLU\";\n            }\n        } else {\n            // Default pattern\n            if (robust_path.length() + 4 <= MAX_LENGTH) {\n                robust_path += \"DRRD\";\n            }\n        }\n        \n        // Break if getting too long\n        if (robust_path.length() >= MAX_LENGTH - 10) break;\n    }\n    \n    // Ensure we don't exceed limit\n    if (robust_path.length() > MAX_LENGTH) {\n        robust_path = robust_path.substr(0, MAX_LENGTH);\n    }\n    \n    return robust_path;\n}\n\n// Create a path based on grid movement pattern\nstring create_grid_path() {\n    string path = \"\";\n    \n    // Move towards target with redundancy\n    int dr_target = ti - si;\n    int dc_target = tj - sj;\n    \n    // Create a path that goes in the general direction but with redundancy\n    int r = si, c = sj;\n    \n    // Move step by step towards target with some redundancy\n    while ((r != ti || c != tj) && path.length() < MAX_LENGTH) {\n        int dr_current = ti - r;\n        int dc_current = tj - c;\n        \n        // Prefer moving in the direction that gets us closer\n        if (abs(dr_current) >= abs(dc_current) && dr_current != 0) {\n            // Move vertically\n            if (dr_current > 0) {\n                // Move down\n                if (can_move(r, c, 1)) {\n                    path += 'D';\n                    r++;\n                } else {\n                    // Try horizontal movement instead\n                    if (dc_current > 0 && can_move(r, c, 3)) {\n                        path += 'R';\n                        c++;\n                    } else if (dc_current < 0 && can_move(r, c, 2)) {\n                        path += 'L';\n                        c--;\n                    } else {\n                        path += 'D'; // Still try, even if blocked\n                    }\n                }\n            } else {\n                // Move up\n                if (can_move(r, c, 0)) {\n                    path += 'U';\n                    r--;\n                } else {\n                    // Try horizontal movement instead\n                    if (dc_current > 0 && can_move(r, c, 3)) {\n                        path += 'R';\n                        c++;\n                    } else if (dc_current < 0 && can_move(r, c, 2)) {\n                        path += 'L';\n                        c--;\n                    } else {\n                        path += 'U'; // Still try, even if blocked\n                    }\n                }\n            }\n        } else if (dc_current != 0) {\n            // Move horizontally\n            if (dc_current > 0) {\n                // Move right\n                if (can_move(r, c, 3)) {\n                    path += 'R';\n                    c++;\n                } else {\n                    // Try vertical movement instead\n                    if (dr_current > 0 && can_move(r, c, 1)) {\n                        path += 'D';\n                        r++;\n                    } else if (dr_current < 0 && can_move(r, c, 0)) {\n                        path += 'U';\n                        r--;\n                    } else {\n                        path += 'R'; // Still try, even if blocked\n                    }\n                }\n            } else {\n                // Move left\n                if (can_move(r, c, 2)) {\n                    path += 'L';\n                    c--;\n                } else {\n                    // Try vertical movement instead\n                    if (dr_current > 0 && can_move(r, c, 1)) {\n                        path += 'D';\n                        r++;\n                    } else if (dr_current < 0 && can_move(r, c, 0)) {\n                        path += 'U';\n                        r--;\n                    } else {\n                        path += 'L'; // Still try, even if blocked\n                    }\n                }\n            }\n        } else {\n            // We're at target\n            break;\n        }\n        \n        // Add some redundancy every few steps\n        if (path.length() % 10 == 7 && path.length() + 4 <= MAX_LENGTH) {\n            // Add a small redundant pattern\n            if (path.back() == 'R' && path.length() + 2 <= MAX_LENGTH) {\n                path += \"LR\"; // Right then left (should cancel)\n            } else if (path.back() == 'L' && path.length() + 2 <= MAX_LENGTH) {\n                path += \"RL\";\n            } else if (path.back() == 'U' && path.length() + 2 <= MAX_LENGTH) {\n                path += \"DU\";\n            } else if (path.back() == 'D' && path.length() + 2 <= MAX_LENGTH) {\n                path += \"UD\";\n            }\n        }\n    }\n    \n    // If we still have room, add the shortest path from current position to target\n    if (path.length() < MAX_LENGTH) {\n        // Simple approach: just fill with moves towards target\n        while (r != ti && path.length() < MAX_LENGTH) {\n            if (r < ti && path.length() < MAX_LENGTH) {\n                path += 'D';\n                r++;\n            } else if (r > ti && path.length() < MAX_LENGTH) {\n                path += 'U';\n                r--;\n            }\n        }\n        while (c != tj && path.length() < MAX_LENGTH) {\n            if (c < tj && path.length() < MAX_LENGTH) {\n                path += 'R';\n                c++;\n            } else if (c > tj && path.length() < MAX_LENGTH) {\n                path += 'L';\n                c--;\n            }\n        }\n    }\n    \n    if (path.length() > MAX_LENGTH) {\n        path = path.substr(0, MAX_LENGTH);\n    }\n    \n    return path;\n}\n\n// Create a very robust path by using repetitive patterns\nstring create_repetitive_path() {\n    string path = \"\";\n    \n    // Create a path with lots of redundancy\n    int dr_target = ti - si;\n    int dc_target = tj - sj;\n    \n    // Use a snake-like pattern for robustness\n    bool horizontal_first = (abs(dc_target) >= abs(dr_target));\n    \n    if (horizontal_first) {\n        // Move horizontally first with redundancy\n        int c = sj;\n        while (c != tj && path.length() < MAX_LENGTH - 5) {\n            if (c < tj) {\n                path += \"RR\";\n                c += 2;\n                if (c > tj) c = tj;\n            } else {\n                path += \"LL\";\n                c -= 2;\n                if (c < tj) c = tj;\n            }\n            \n            // Add some vertical redundancy\n            if (path.length() < MAX_LENGTH - 5) {\n                path += \"DU\"; // Up then down (should cancel)\n            }\n        }\n        \n        // Then move vertically\n        int r = si;\n        while (r != ti && path.length() < MAX_LENGTH - 2) {\n            if (r < ti) {\n                path += \"DD\";\n                r += 2;\n                if (r > ti) r = ti;\n            } else {\n                path += \"UU\";\n                r -= 2;\n                if (r < ti) r = ti;\n            }\n        }\n    } else {\n        // Move vertically first\n        int r = si;\n        while (r != ti && path.length() < MAX_LENGTH - 5) {\n            if (r < ti) {\n                path += \"DD\";\n                r += 2;\n                if (r > ti) r = ti;\n            } else {\n                path += \"UU\";\n                r -= 2;\n                if (r < ti) r = ti;\n            }\n            \n            // Add some horizontal redundancy\n            if (path.length() < MAX_LENGTH - 5) {\n                path += \"LR\"; // Left then right (should cancel)\n            }\n        }\n        \n        // Then move horizontally\n        int c = sj;\n        while (c != tj && path.length() < MAX_LENGTH - 2) {\n            if (c < tj) {\n                path += \"RR\";\n                c += 2;\n                if (c > tj) c = tj;\n            } else {\n                path += \"LL\";\n                c -= 2;\n                if (c < tj) c = tj;\n            }\n        }\n    }\n    \n    if (path.length() > MAX_LENGTH) {\n        path = path.substr(0, MAX_LENGTH);\n    }\n    \n    return path;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    // Read input\n    cin >> si >> sj >> ti >> tj >> p;\n    \n    // Read horizontal walls\n    for (int i = 0; i < SIZE; i++) {\n        string line;\n        cin >> line;\n        for (int j = 0; j < SIZE-1; j++) {\n            h_walls[i][j] = (line[j] == '1');\n        }\n    }\n    \n    // Read vertical walls\n    for (int i = 0; i < SIZE-1; i++) {\n        string line;\n        cin >> line;\n        for (int j = 0; j < SIZE; j++) {\n            v_walls[i][j] = (line[j] == '1');\n        }\n    }\n    \n    // Try different approaches and pick the best one\n    string path1 = find_shortest_path();\n    string path2 = create_robust_path();\n    string path3 = create_grid_path();\n    string path4 = create_repetitive_path();\n    \n    // Choose the longest one that's under the limit (more robust)\n    string best_path = path1;\n    if (path2.length() > best_path.length() && path2.length() <= MAX_LENGTH) {\n        best_path = path2;\n    }\n    if (path3.length() > best_path.length() && path3.length() <= MAX_LENGTH) {\n        best_path = path3;\n    }\n    if (path4.length() > best_path.length() && path4.length() <= MAX_LENGTH) {\n        best_path = path4;\n    }\n    \n    // Ensure we don't exceed the limit\n    if (best_path.length() > MAX_LENGTH) {\n        best_path = best_path.substr(0, MAX_LENGTH);\n    }\n    \n    cout << best_path << endl;\n    \n    return 0;\n}","ahc010":"#include <bits/stdc++.h>\nusing namespace std;\n\n// Direction: 0=left, 1=up, 2=right, 3=down\nconst int di[4] = {0, -1, 0, 1};\nconst int dj[4] = {-1, 0, 1, 0};\n\n// to[tile_type][direction_entering] = direction_exiting (-1 if invalid)\nconst int to[8][4] = {\n    {1, 0, -1, -1}, // tile 0\n    {3, -1, -1, 0}, // tile 1\n    {-1, -1, 3, 2}, // tile 2\n    {-1, 2, 1, -1}, // tile 3\n    {1, 0, 3, 2},   // tile 4\n    {3, 2, 1, 0},   // tile 5\n    {2, -1, 0, -1}, // tile 6\n    {-1, 3, -1, 1}  // tile 7\n};\n\nconst int N = 30;\nint original_grid[N][N];\nint rotations[N][N];\n\n// Random number generator\nmt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n\n// Get effective tile type after rotation\nint getEffectiveTile(int baseTile, int rotation) {\n    // For tiles 0-3 (single curve): 0->1->2->3->0\n    // For tiles 4-5 (double curve): 4->5->4->5->4 (alternating)\n    // For tiles 6-7 (straight): 6->7->6->7->6 (alternating)\n    \n    if (baseTile >= 0 && baseTile <= 3) {\n        return (baseTile + rotation) % 4;\n    } else if (baseTile == 4 || baseTile == 5) {\n        if (rotation % 2 == 0) return baseTile;\n        else return (baseTile == 4) ? 5 : 4;\n    } else { // baseTile == 6 or 7\n        if (rotation % 2 == 0) return baseTile;\n        else return (baseTile == 6) ? 7 : 6;\n    }\n}\n\n// Trace a loop starting from position (i,j) entering from direction d\nint traceLoop(vector<vector<int>>& effectiveGrid, int start_i, int start_j, int start_d) {\n    int i = start_i, j = start_j, d = start_d;\n    int length = 0;\n    \n    // Check if we can enter this tile from this direction\n    int d2 = to[effectiveGrid[i][j]][d];\n    if (d2 == -1) return 0;\n    \n    do {\n        d2 = to[effectiveGrid[i][j]][d];\n        if (d2 == -1) return 0;\n        \n        // Move to next tile\n        i += di[d2];\n        j += dj[d2];\n        \n        // Check bounds\n        if (i < 0 || i >= N || j < 0 || j >= N) return 0;\n        \n        // Update direction (opposite of exit direction)\n        d = (d2 + 2) % 4;\n        length++;\n        \n        // Check if we've returned to start\n        if (i == start_i && j == start_j && d == start_d) break;\n        \n        // Safety check to avoid infinite loops\n        if (length > 1000) return 0;\n        \n    } while (true);\n    \n    return length;\n}\n\n// Find all loops and return their lengths\nvector<int> findLoops(vector<vector<int>>& effectiveGrid) {\n    vector<int> loops;\n    bool visited[N][N][4] = {false};\n    \n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            for (int d = 0; d < 4; d++) {\n                if (!visited[i][j][d]) {\n                    int length = traceLoop(effectiveGrid, i, j, d);\n                    if (length > 0) {\n                        // Mark all positions in this loop as visited\n                        int ci = i, cj = j, cd = d;\n                        do {\n                            visited[ci][cj][cd] = true;\n                            int d2 = to[effectiveGrid[ci][cj]][cd];\n                            if (d2 == -1) break;\n                            ci += di[d2];\n                            cj += dj[d2];\n                            if (ci < 0 || ci >= N || cj < 0 || cj >= N) break;\n                            cd = (d2 + 2) % 4;\n                        } while (!(ci == i && cj == j && cd == d) && ci >= 0 && ci < N && cj >= 0 && cj < N);\n                        \n                        loops.push_back(length);\n                    }\n                }\n            }\n        }\n    }\n    \n    sort(loops.begin(), loops.end(), greater<int>());\n    return loops;\n}\n\n// Calculate score for current rotation configuration\nlong long calculateScore() {\n    // Create effective grid with rotations applied\n    vector<vector<int>> effectiveGrid(N, vector<int>(N));\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            effectiveGrid[i][j] = getEffectiveTile(original_grid[i][j], rotations[i][j]);\n        }\n    }\n    \n    vector<int> loops = findLoops(effectiveGrid);\n    \n    if (loops.size() < 2) return 0;\n    \n    return (long long)loops[0] * loops[1];\n}\n\nint main() {\n    // Read input\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cin >> original_grid[i][j];\n            rotations[i][j] = 0;\n        }\n    }\n    \n    // Simple greedy approach - try random rotations\n    long long bestScore = calculateScore();\n    \n    // Try some random improvements\n    for (int iter = 0; iter < 5000; iter++) {\n        // Make a random change\n        int i = rng() % N;\n        int j = rng() % N;\n        int oldRot = rotations[i][j];\n        rotations[i][j] = rng() % 4;\n        \n        long long score = calculateScore();\n        if (score > bestScore) {\n            bestScore = score;\n        } else {\n            rotations[i][j] = oldRot; // Revert\n        }\n    }\n    \n    // Output result\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cout << rotations[i][j];\n        }\n    }\n    cout << endl;\n    \n    return 0;\n}","ahc011":"#include <bits/stdc++.h>\nusing namespace std;\n\n// Direction constants\nconst int UP = 0, DOWN = 1, LEFT = 2, RIGHT = 3;\nconst int dr[4] = {-1, 1, 0, 0};\nconst int dc[4] = {0, 0, -1, 1};\nconst char move_chars[4] = {'U', 'D', 'L', 'R'};\n\nstruct State {\n    vector<vector<int>> board;\n    int empty_r, empty_c;\n    string moves;\n    \n    State(vector<vector<int>> b, int er, int ec, string m = \"\") \n        : board(b), empty_r(er), empty_c(ec), moves(m) {}\n};\n\n// Check if two adjacent tiles can connect\nbool canConnect(int tile1, int tile2, int direction) {\n    // direction: 0=up, 1=down, 2=left, 3=right\n    // tile1 is at (r,c), tile2 is at (r+dr[direction], c+dc[direction])\n    if (direction == 0) { // up\n        return (tile1 & 2) && (tile2 & 8);\n    } else if (direction == 1) { // down\n        return (tile1 & 8) && (tile2 & 2);\n    } else if (direction == 2) { // left\n        return (tile1 & 1) && (tile2 & 4);\n    } else { // right\n        return (tile1 & 4) && (tile2 & 1);\n    }\n}\n\n// Find connected components in the current board\nvector<vector<bool>> findComponents(const vector<vector<int>>& board, int empty_r, int empty_c) {\n    int N = board.size();\n    vector<vector<bool>> visited(N, vector<bool>(N, false));\n    vector<vector<bool>> is_tile(N, vector<bool>(N, true));\n    is_tile[empty_r][empty_c] = false;\n    \n    // BFS to find connected components\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (!visited[i][j] && is_tile[i][j]) {\n                queue<pair<int,int>> q;\n                q.push({i, j});\n                visited[i][j] = true;\n                \n                while (!q.empty()) {\n                    auto [r, c] = q.front(); q.pop();\n                    \n                    // Check 4 directions\n                    for (int d = 0; d < 4; d++) {\n                        int nr = r + dr[d];\n                        int nc = c + dc[d];\n                        \n                        if (nr >= 0 && nr < N && nc >= 0 && nc < N && \n                            is_tile[nr][nc] && !visited[nr][nc]) {\n                            if (canConnect(board[r][c], board[nr][nc], d)) {\n                                visited[nr][nc] = true;\n                                q.push({nr, nc});\n                            }\n                        }\n                    }\n                }\n            }\n        }\n    }\n    \n    return visited;\n}\n\n// Count size of largest connected component\nint largestComponentSize(const vector<vector<int>>& board, int empty_r, int empty_c) {\n    auto components = findComponents(board, empty_r, empty_c);\n    int max_size = 0;\n    int current_size = 0;\n    \n    for (const auto& row : components) {\n        for (bool cell : row) {\n            if (cell) current_size++;\n        }\n    }\n    \n    return current_size;\n}\n\n// Heuristic function - how good is this state?\nint evaluate(const vector<vector<int>>& board, int empty_r, int empty_c) {\n    return largestComponentSize(board, empty_r, empty_c);\n}\n\n// Try to solve by building a path to maximize tree size\nstring solve(vector<vector<int>> initial_board, int N, int T) {\n    // Find initial empty position\n    int empty_r = 0, empty_c = 0;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (initial_board[i][j] == 0) {\n                empty_r = i;\n                empty_c = j;\n                break;\n            }\n        }\n    }\n    \n    // Simple greedy approach - try to improve connectivity\n    string best_moves = \"\";\n    vector<vector<int>> current_board = initial_board;\n    int current_empty_r = empty_r;\n    int current_empty_c = empty_c;\n    \n    // Try random moves to improve connectivity\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n    uniform_int_distribution<int> dir_dist(0, 3);\n    \n    int best_score = evaluate(current_board, current_empty_r, current_empty_c);\n    \n    for (int step = 0; step < min(T, 5000); step++) {\n        // Try a few random moves\n        vector<pair<int, string>> candidates;\n        \n        for (int attempt = 0; attempt < 20; attempt++) {\n            int dir = dir_dist(rng);\n            int nr = current_empty_r + dr[dir];\n            int nc = current_empty_c + dc[dir];\n            \n            if (nr >= 0 && nr < N && nc >= 0 && nc < N) {\n                // Make the move\n                vector<vector<int>> new_board = current_board;\n                swap(new_board[current_empty_r][current_empty_c], new_board[nr][nc]);\n                \n                string new_moves = best_moves + move_chars[dir];\n                int score = evaluate(new_board, nr, nc);\n                \n                candidates.push_back({score, new_moves});\n            }\n        }\n        \n        if (!candidates.empty()) {\n            sort(candidates.rbegin(), candidates.rend());\n            if (candidates[0].first > best_score) {\n                best_score = candidates[0].first;\n                best_moves = candidates[0].second;\n                // Update current state\n                int last_move = best_moves.back() - 'A';\n                int dir = -1;\n                if (last_move == 'U' - 'A') dir = 0;\n                else if (last_move == 'D' - 'A') dir = 1;\n                else if (last_move == 'L' - 'A') dir = 2;\n                else if (last_move == 'R' - 'A') dir = 3;\n                \n                int nr = current_empty_r + dr[dir];\n                int nc = current_empty_c + dc[dir];\n                swap(current_board[current_empty_r][current_empty_c], current_board[nr][nc]);\n                current_empty_r = nr;\n                current_empty_c = nc;\n            }\n        }\n    }\n    \n    // If we still have moves, try to improve with local search\n    if (best_moves.length() < T) {\n        // Try to optimize the current position\n        for (int extra = best_moves.length(); extra < T && extra < best_moves.length() + 1000; extra++) {\n            int dir = dir_dist(rng);\n            int nr = current_empty_r + dr[dir];\n            int nc = current_empty_c + dc[dir];\n            \n            if (nr >= 0 && nr < N && nc >= 0 && nc < N) {\n                best_moves += move_chars[dir];\n                swap(current_board[current_empty_r][current_empty_c], current_board[nr][nc]);\n                current_empty_r = nr;\n                current_empty_c = nc;\n            }\n        }\n    }\n    \n    return best_moves.substr(0, T);\n}\n\nint main() {\n    int N, T;\n    cin >> N >> T;\n    \n    vector<vector<int>> board(N, vector<int>(N));\n    for (int i = 0; i < N; i++) {\n        string line;\n        cin >> line;\n        for (int j = 0; j < N; j++) {\n            char c = line[j];\n            if (c >= '0' && c <= '9') {\n                board[i][j] = c - '0';\n            } else {\n                board[i][j] = c - 'a' + 10;\n            }\n        }\n    }\n    \n    string result = solve(board, N, T);\n    cout << result << endl;\n    \n    return 0;\n}","ahc012":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst double PI = acos(-1);\nconst long long BOUND = 1000000000LL;\nconst double CAKE_RADIUS = 10000.0;\n\nstruct Strawberry {\n    long long x, y;\n    int id;\n    double angle;\n    \n    bool operator<(const Strawberry& other) const {\n        return angle < other.angle;\n    }\n};\n\ndouble calculate_angle(long long x, long long y) {\n    double angle = atan2((double)y, (double)x);\n    if (angle < 0) angle += 2 * PI;\n    return angle;\n}\n\n// Create a line passing through two points\npair<pair<long long, long long>, pair<long long, long long>> create_line_from_points(pair<double, double> p1, pair<double, double> p2) {\n    long long x1 = (long long)round(p1.first);\n    long long y1 = (long long)round(p1.second);\n    long long x2 = (long long)round(p2.first);\n    long long y2 = (long long)round(p2.second);\n    \n    // Ensure they're different points\n    if (x1 == x2 && y1 == y2) {\n        x2 += 1;\n    }\n    \n    // Bound checking\n    x1 = max(-BOUND, min(BOUND, x1));\n    y1 = max(-BOUND, min(BOUND, y1));\n    x2 = max(-BOUND, min(BOUND, x2));\n    y2 = max(-BOUND, min(BOUND, y2));\n    \n    return {{x1, y1}, {x2, y2}};\n}\n\n// Create a line that goes through a point with a specific angle\npair<pair<long long, long long>, pair<long long, long long>> create_line_at_angle(pair<double, double> point, double angle) {\n    double dx = cos(angle) * 100000;\n    double dy = sin(angle) * 100000;\n    \n    pair<double, double> p1 = {point.first + dx, point.second + dy};\n    pair<double, double> p2 = {point.first - dx, point.second - dy};\n    \n    return create_line_from_points(p1, p2);\n}\n\n// Distance between two points\ndouble distance(long long x1, long long y1, long long x2, long long y2) {\n    return sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    int N, K;\n    cin >> N >> K;\n    \n    vector<int> a(11); // a[1] to a[10]\n    int total_attendees = 0;\n    for (int i = 1; i <= 10; i++) {\n        cin >> a[i];\n        total_attendees += a[i];\n    }\n    \n    vector<Strawberry> strawberries(N);\n    for (int i = 0; i < N; i++) {\n        cin >> strawberries[i].x >> strawberries[i].y;\n        strawberries[i].id = i;\n        strawberries[i].angle = calculate_angle(strawberries[i].x, strawberries[i].y);\n    }\n    \n    vector<pair<pair<long long, long long>, pair<long long, long long>>> lines;\n    \n    // Strategy: Try to create clusters of strawberries\n    // Sort by distance from origin first, then by angle\n    vector<pair<double, int>> strawberry_info(N);\n    for (int i = 0; i < N; i++) {\n        double dist = sqrt(strawberries[i].x * strawberries[i].x + strawberries[i].y * strawberries[i].y);\n        strawberry_info[i] = {dist, i};\n    }\n    sort(strawberry_info.begin(), strawberry_info.end());\n    \n    // Simple clustering approach: group nearby strawberries\n    vector<vector<int>> clusters;\n    vector<bool> used(N, false);\n    \n    // Try to form clusters of sizes 1 to 10 based on a_d values\n    for (int cluster_size = 10; cluster_size >= 1 && lines.size() < K; cluster_size--) {\n        int target_clusters = min(a[cluster_size], N / cluster_size);\n        \n        for (int c = 0; c < target_clusters && lines.size() < K; c++) {\n            // Find cluster_size unused strawberries that are close to each other\n            vector<int> current_cluster;\n            int start_idx = -1;\n            \n            // Find first unused strawberry\n            for (int i = 0; i < N; i++) {\n                int orig_idx = strawberry_info[i].second;\n                if (!used[orig_idx]) {\n                    start_idx = i;\n                    break;\n                }\n            }\n            \n            if (start_idx == -1) break;\n            \n            int start_orig = strawberry_info[start_idx].second;\n            current_cluster.push_back(start_orig);\n            used[start_orig] = true;\n            \n            // Try to find nearby strawberries\n            for (int i = 1; i < cluster_size && current_cluster.size() < cluster_size; i++) {\n                int best_idx = -1;\n                double best_dist = 1e18;\n                \n                for (int j = 0; j < N; j++) {\n                    int orig_idx = strawberry_info[j].second;\n                    if (used[orig_idx]) continue;\n                    \n                    double dist = distance(\n                        strawberries[start_orig].x, strawberries[start_orig].y,\n                        strawberries[orig_idx].x, strawberries[orig_idx].y\n                    );\n                    \n                    if (dist < best_dist) {\n                        best_dist = dist;\n                        best_idx = orig_idx;\n                    }\n                }\n                \n                if (best_idx != -1) {\n                    current_cluster.push_back(best_idx);\n                    used[best_idx] = true;\n                }\n            }\n            \n            // If we have a good cluster, try to separate it from others\n            if (current_cluster.size() >= 2 && lines.size() + (current_cluster.size() - 1) <= K) {\n                // Create separating lines between consecutive strawberries in cluster\n                for (int i = 0; i < (int)current_cluster.size() - 1 && lines.size() < K; i++) {\n                    int idx1 = current_cluster[i];\n                    int idx2 = current_cluster[i+1];\n                    \n                    // Create perpendicular bisector of the line connecting these two points\n                    double mid_x = (strawberries[idx1].x + strawberries[idx2].x) / 2.0;\n                    double mid_y = (strawberries[idx1].y + strawberries[idx2].y) / 2.0;\n                    \n                    // Perpendicular direction\n                    double dx = strawberries[idx2].x - strawberries[idx1].x;\n                    double dy = strawberries[idx2].y - strawberries[idx1].y;\n                    double perp_angle = atan2(-dx, dy);\n                    \n                    auto line = create_line_at_angle({mid_x, mid_y}, perp_angle);\n                    lines.push_back(line);\n                }\n            } else {\n                // Undo the clustering if we can't separate properly\n                for (int idx : current_cluster) {\n                    used[idx] = false;\n                }\n            }\n        }\n    }\n    \n    // Fallback: if we don't have enough lines, add some radial lines\n    if (lines.size() < K / 3) {\n        int remaining = min(K - (int)lines.size(), 20);\n        for (int i = 0; i < remaining; i++) {\n            double angle = 2.0 * PI * i / remaining;\n            // Create a line far from origin in this direction\n            double dist = CAKE_RADIUS * 2;\n            pair<double, double> p1 = {cos(angle) * dist, sin(angle) * dist};\n            pair<double, double> p2 = {cos(angle + 0.1) * dist, sin(angle + 0.1) * dist};\n            lines.push_back(create_line_from_points(p1, p2));\n        }\n    }\n    \n    // Ensure we don't exceed K lines\n    while (lines.size() > K) {\n        lines.pop_back();\n    }\n    \n    // Output result\n    cout << lines.size() << \"\\n\";\n    for (const auto& line : lines) {\n        cout << line.first.first << \" \" << line.first.second << \" \" \n             << line.second.first << \" \" << line.second.second << \"\\n\";\n    }\n    \n    return 0;\n}","ahc014":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MAX_N = 65;\n\nint N, M;\nbool grid[MAX_N][MAX_N];\nvector<pair<int, int>> initial_dots;\nvector<tuple<int, int, int, int, int, int, int, int>> operations;\n\n// Center of the grid\nint center;\n\n// Calculate weight of a position\ninline int weight(int x, int y) {\n    int dx = x - center;\n    int dy = y - center;\n    return dx * dx + dy * dy + 1;\n}\n\n// Check if position is valid (within bounds)\ninline bool valid_pos(int x, int y) {\n    return x >= 0 && x < N && y >= 0 && y < N;\n}\n\n// Check if a rectangle is axis-aligned\nbool is_axis_aligned(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) {\n    return (x1 == x2 && x3 == x4 && y1 == y4 && y2 == y3) ||\n           (x1 == x4 && x2 == x3 && y1 == y2 && y3 == y4);\n}\n\n// Check if a rectangle is 45-degree rotated\nbool is_45_rotated(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) {\n    int dx12 = abs(x1 - x2);\n    int dy12 = abs(y1 - y2);\n    int dx23 = abs(x2 - x3);\n    int dy23 = abs(y2 - y3);\n    int dx34 = abs(x3 - x4);\n    int dy34 = abs(y3 - y4);\n    int dx41 = abs(x4 - x1);\n    int dy41 = abs(y4 - y1);\n    \n    return (dx12 == dy12 && dx23 == dy23 && dx34 == dy34 && dx41 == dy41) &&\n           (dx12 == dx23 && dx23 == dx34 && dx34 == dx41);\n}\n\n// Check if four points form a valid rectangle (axis-aligned or 45\u00b0 rotated)\nbool is_valid_rectangle(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) {\n    // Check if it's a valid rectangle\n    if (!valid_pos(x1, y1) || !valid_pos(x2, y2) || !valid_pos(x3, y3) || !valid_pos(x4, y4))\n        return false;\n        \n    // Check if it's axis-aligned or 45-degree rotated\n    return is_axis_aligned(x1, y1, x2, y2, x3, y3, x4, y4) || \n           is_45_rotated(x1, y1, x2, y2, x3, y3, x4, y4);\n}\n\n// Check if there are no dots on the perimeter except the three given points\nbool no_dots_on_perimeter(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) {\n    // This is a simplified check - we'd need to implement proper perimeter checking\n    // For now, let's just check the four corners\n    int corners[4][2] = {{x1, y1}, {x2, y2}, {x3, y3}, {x4, y4}};\n    \n    for (int i = 0; i < 4; i++) {\n        int x = corners[i][0];\n        int y = corners[i][1];\n        // Skip the three points that should have dots\n        if ((x == x2 && y == y2) || (x == x3 && y == y3) || (x == x4 && y == y4))\n            continue;\n        // If it's the new point, it should not have a dot\n        if (x == x1 && y == y1) {\n            if (grid[x][y]) return false;\n        }\n        // For other perimeter points, they should not have dots\n        // This is simplified - we need a better perimeter check\n    }\n    return true;\n}\n\n// Find valid rectangles for a given position\nvector<tuple<int, int, int, int, int, int>> find_valid_rectangles(int new_x, int new_y) {\n    vector<tuple<int, int, int, int, int, int>> result;\n    \n    // Look for axis-aligned rectangles\n    for (int dx = 1; dx < N; dx++) {\n        for (int dy = 1; dy < N; dy++) {\n            // Try different orientations\n            vector<pair<int, int>> corners = {\n                {new_x, new_y},\n                {new_x + dx, new_y},\n                {new_x + dx, new_y + dy},\n                {new_x, new_y + dy}\n            };\n            \n            // Check all 4 rotations\n            for (int rot = 0; rot < 4; rot++) {\n                int x1 = corners[0].first, y1 = corners[0].second;\n                int x2 = corners[1].first, y2 = corners[1].second;\n                int x3 = corners[2].first, y3 = corners[2].second;\n                int x4 = corners[3].first, y4 = corners[3].second;\n                \n                if (valid_pos(x1, y1) && valid_pos(x2, y2) && valid_pos(x3, y3) && valid_pos(x4, y4)) {\n                    // Check if the three points (excluding new position) have dots\n                    if (grid[x2][y2] && grid[x3][y3] && grid[x4][y4]) {\n                        // Simple check for now - need better perimeter checking\n                        result.push_back(make_tuple(x1, y1, x2, y2, x3, y3, x4, y4));\n                    }\n                }\n                \n                // Rotate corners\n                auto temp = corners[0];\n                for (int i = 0; i < 3; i++) {\n                    corners[i] = corners[i+1];\n                }\n                corners[3] = temp;\n            }\n        }\n    }\n    \n    // Look for 45-degree rotated rectangles\n    for (int d = 1; d < N; d++) {\n        // Diamond shape with side length d\n        vector<pair<int, int>> corners = {\n            {new_x, new_y + d},\n            {new_x + d, new_y},\n            {new_x, new_y - d},\n            {new_x - d, new_y}\n        };\n        \n        if (valid_pos(corners[0].first, corners[0].second) &&\n            valid_pos(corners[1].first, corners[1].second) &&\n            valid_pos(corners[2].first, corners[2].second) &&\n            valid_pos(corners[3].first, corners[3].second)) {\n            \n            if (grid[corners[1].first][corners[1].second] &&\n                grid[corners[2].first][corners[2].second] &&\n                grid[corners[3].first][corners[3].second]) {\n                result.push_back(make_tuple(\n                    corners[0].first, corners[0].second,\n                    corners[1].first, corners[1].second,\n                    corners[2].first, corners[2].second,\n                    corners[3].first, corners[3].second\n                ));\n            }\n        }\n    }\n    \n    return result;\n}\n\n// Greedy approach: try to place dots with highest weight first\nvoid solve() {\n    // Priority queue to process positions by weight (descending)\n    priority_queue<pair<int, pair<int, int>>> pq;\n    \n    // Add all valid positions to priority queue\n    for (int x = 0; x < N; x++) {\n        for (int y = 0; y < N; y++) {\n            if (!grid[x][y]) { // Position doesn't have a dot yet\n                pq.push({weight(x, y), {x, y}});\n            }\n        }\n    }\n    \n    // Process positions\n    int attempts = 0;\n    const int MAX_ATTEMPTS = 10000; // Limit attempts for performance\n    \n    while (!pq.empty() && attempts < MAX_ATTEMPTS) {\n        auto current = pq.top();\n        pq.pop();\n        \n        int x = current.second.first;\n        int y = current.second.second;\n        \n        // If this position already has a dot, skip\n        if (grid[x][y]) continue;\n        \n        // Find valid rectangles for this position\n        auto rectangles = find_valid_rectangles(x, y);\n        \n        if (!rectangles.empty()) {\n            // Use the first valid rectangle (could be improved with better selection)\n            auto rect = rectangles[0];\n            int x1, y1, x2, y2, x3, y3, x4, y4;\n            tie(x1, y1, x2, y2, x3, y3, x4, y4) = rect;\n            \n            // Place the dot\n            grid[x1][y1] = true;\n            operations.push_back(make_tuple(x1, y1, x2, y2, x3, y3, x4, y4));\n            \n            attempts = 0; // Reset attempts since we made progress\n        }\n        \n        attempts++;\n    }\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    cin >> N >> M;\n    center = (N - 1) / 2;\n    \n    // Initialize grid\n    memset(grid, false, sizeof(grid));\n    \n    // Read initial dots\n    for (int i = 0; i < M; i++) {\n        int x, y;\n        cin >> x >> y;\n        grid[x][y] = true;\n        initial_dots.push_back({x, y});\n    }\n    \n    // Solve\n    solve();\n    \n    // Output results\n    cout << operations.size() << \"\\n\";\n    for (const auto& op : operations) {\n        cout << get<0>(op) << \" \" << get<1>(op) << \" \" \n             << get<2>(op) << \" \" << get<3>(op) << \" \"\n             << get<4>(op) << \" \" << get<5>(op) << \" \"\n             << get<6>(op) << \" \" << get<7>(op) << \"\\n\";\n    }\n    \n    return 0;\n}","ahc015":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int GRID_SIZE = 10;\nconst int TOTAL_CANDIES = 100;\nconst int LOOKAHEAD = 3; // Look ahead this many moves\n\n// Directions: F(forward/up), B(backward/down), L(left), R(right)\nconst char DIR_CHARS[] = {'F', 'B', 'L', 'R'};\nconst int DX[] = {-1, 1, 0, 0}; // F, B, L, R\nconst int DY[] = {0, 0, -1, 1};\n\nstruct Position {\n    int x, y;\n    Position() : x(0), y(0) {}\n    Position(int _x, int _y) : x(_x), y(_y) {}\n};\n\nclass CandyBox {\npublic:\n    int grid[GRID_SIZE][GRID_SIZE]; // 0 = empty, 1,2,3 = flavors\n    vector<int> flavor_sequence;\n    int candy_count[4]; // index 0 unused, 1-3 for flavors\n    \n    CandyBox() {\n        memset(grid, 0, sizeof(grid));\n        memset(candy_count, 0, sizeof(candy_count));\n    }\n    \n    void read_flavors() {\n        flavor_sequence.resize(TOTAL_CANDIES);\n        for (int i = 0; i < TOTAL_CANDIES; i++) {\n            cin >> flavor_sequence[i];\n        }\n    }\n    \n    // Convert empty cell index to position\n    Position index_to_position(int index) {\n        int count = 0;\n        for (int i = 0; i < GRID_SIZE; i++) {\n            for (int j = 0; j < GRID_SIZE; j++) {\n                if (grid[i][j] == 0) {\n                    count++;\n                    if (count == index) {\n                        return Position(i, j);\n                    }\n                }\n            }\n        }\n        return Position(-1, -1); // Error\n    }\n    \n    // Place candy at specified position\n    void place_candy(int x, int y, int flavor) {\n        grid[x][y] = flavor;\n        candy_count[flavor]++;\n    }\n    \n    // Simulate sliding in given direction\n    void slide_candies(int dir) {\n        if (dir == 0) { // Forward (up) - move rows up\n            for (int col = 0; col < GRID_SIZE; col++) {\n                int write_pos = 0;\n                for (int row = 0; row < GRID_SIZE; row++) {\n                    if (grid[row][col] != 0) {\n                        if (write_pos != row) {\n                            grid[write_pos][col] = grid[row][col];\n                            grid[row][col] = 0;\n                        }\n                        write_pos++;\n                    }\n                }\n            }\n        } else if (dir == 1) { // Backward (down) - move rows down\n            for (int col = 0; col < GRID_SIZE; col++) {\n                int write_pos = GRID_SIZE - 1;\n                for (int row = GRID_SIZE - 1; row >= 0; row--) {\n                    if (grid[row][col] != 0) {\n                        if (write_pos != row) {\n                            grid[write_pos][col] = grid[row][col];\n                            grid[row][col] = 0;\n                        }\n                        write_pos--;\n                    }\n                }\n            }\n        } else if (dir == 2) { // Left - move columns left\n            for (int row = 0; row < GRID_SIZE; row++) {\n                int write_pos = 0;\n                for (int col = 0; col < GRID_SIZE; col++) {\n                    if (grid[row][col] != 0) {\n                        if (write_pos != col) {\n                            grid[row][write_pos] = grid[row][col];\n                            grid[row][col] = 0;\n                        }\n                        write_pos++;\n                    }\n                }\n            }\n        } else if (dir == 3) { // Right - move columns right\n            for (int row = 0; row < GRID_SIZE; row++) {\n                int write_pos = GRID_SIZE - 1;\n                for (int col = GRID_SIZE - 1; col >= 0; col--) {\n                    if (grid[row][col] != 0) {\n                        if (write_pos != col) {\n                            grid[row][write_pos] = grid[row][col];\n                            grid[row][col] = 0;\n                        }\n                        write_pos--;\n                    }\n                }\n            }\n        }\n    }\n    \n    // Create a copy of current state\n    CandyBox copy() const {\n        CandyBox other;\n        for (int i = 0; i < GRID_SIZE; i++) {\n            for (int j = 0; j < GRID_SIZE; j++) {\n                other.grid[i][j] = grid[i][j];\n            }\n        }\n        for (int i = 0; i < 4; i++) {\n            other.candy_count[i] = candy_count[i];\n        }\n        other.flavor_sequence = flavor_sequence;\n        return other;\n    }\n    \n    // Calculate connected components score with additional metrics\n    pair<int, int> calculate_connectedness_detailed() {\n        bool visited[GRID_SIZE][GRID_SIZE];\n        memset(visited, false, sizeof(visited));\n        \n        int total_score = 0;\n        int max_component = 0;\n        int dx[] = {0, 0, -1, 1};\n        int dy[] = {-1, 1, 0, 0};\n        \n        for (int i = 0; i < GRID_SIZE; i++) {\n            for (int j = 0; j < GRID_SIZE; j++) {\n                if (grid[i][j] != 0 && !visited[i][j]) {\n                    // BFS to find connected component\n                    queue<Position> q;\n                    vector<Position> component;\n                    q.push(Position(i, j));\n                    visited[i][j] = true;\n                    component.push_back(Position(i, j));\n                    \n                    int flavor = grid[i][j];\n                    \n                    while (!q.empty()) {\n                        Position current = q.front();\n                        q.pop();\n                        \n                        for (int d = 0; d < 4; d++) {\n                            int nx = current.x + dx[d];\n                            int ny = current.y + dy[d];\n                            \n                            if (nx >= 0 && nx < GRID_SIZE && ny >= 0 && ny < GRID_SIZE) {\n                                if (!visited[nx][ny] && grid[nx][ny] == flavor) {\n                                    visited[nx][ny] = true;\n                                    q.push(Position(nx, ny));\n                                    component.push_back(Position(nx, ny));\n                                }\n                            }\n                        }\n                    }\n                    \n                    // Add square of component size to score\n                    int component_size = component.size();\n                    total_score += component_size * component_size;\n                    max_component = max(max_component, component_size);\n                }\n            }\n        }\n        \n        return {total_score, max_component};\n    }\n    \n    int calculate_connectedness() {\n        return calculate_connectedness_detailed().first;\n    }\n    \n    // Count empty cells\n    int count_empty() const {\n        int count = 0;\n        for (int i = 0; i < GRID_SIZE; i++) {\n            for (int j = 0; j < GRID_SIZE; j++) {\n                if (grid[i][j] == 0) count++;\n            }\n        }\n        return count;\n    }\n    \n    // Evaluate clustering potential for a specific flavor\n    int evaluate_flavor_clustering(int flavor) {\n        bool visited[GRID_SIZE][GRID_SIZE];\n        memset(visited, false, sizeof(visited));\n        \n        int total_score = 0;\n        int dx[] = {0, 0, -1, 1};\n        int dy[] = {-1, 1, 0, 0};\n        \n        for (int i = 0; i < GRID_SIZE; i++) {\n            for (int j = 0; j < GRID_SIZE; j++) {\n                if (grid[i][j] == flavor && !visited[i][j]) {\n                    // BFS to find connected component\n                    queue<Position> q;\n                    vector<Position> component;\n                    q.push(Position(i, j));\n                    visited[i][j] = true;\n                    component.push_back(Position(i, j));\n                    \n                    while (!q.empty()) {\n                        Position current = q.front();\n                        q.pop();\n                        \n                        for (int d = 0; d < 4; d++) {\n                            int nx = current.x + dx[d];\n                            int ny = current.y + dy[d];\n                            \n                            if (nx >= 0 && nx < GRID_SIZE && ny >= 0 && ny < GRID_SIZE) {\n                                if (!visited[nx][ny] && grid[nx][ny] == flavor) {\n                                    visited[nx][ny] = true;\n                                    q.push(Position(nx, ny));\n                                    component.push_back(Position(nx, ny));\n                                }\n                            }\n                        }\n                    }\n                    \n                    // Prefer larger components\n                    total_score += component.size() * component.size();\n                }\n            }\n        }\n        \n        return total_score;\n    }\n    \n    // Advanced evaluation considering multiple factors\n    double evaluate_move_advanced(int dir, int current_turn) {\n        // Create copy and simulate\n        CandyBox temp = this->copy();\n        temp.slide_candies(dir);\n        \n        auto [connected_score, max_component] = temp.calculate_connectedness_detailed();\n        \n        // Weight factors\n        double score = connected_score;\n        \n        // Bonus for larger maximum component\n        score += max_component * 10;\n        \n        // Evaluate clustering potential for each flavor\n        for (int flavor = 1; flavor <= 3; flavor++) {\n            if (temp.candy_count[flavor] > 0) {\n                score += temp.evaluate_flavor_clustering(flavor) * 0.5;\n            }\n        }\n        \n        // Prefer moves that don't isolate single candies\n        int isolated_candies = 0;\n        int dx[] = {0, 0, -1, 1};\n        int dy[] = {-1, 1, 0, 0};\n        for (int i = 0; i < GRID_SIZE; i++) {\n            for (int j = 0; j < GRID_SIZE; j++) {\n                if (temp.grid[i][j] != 0) {\n                    bool has_neighbor = false;\n                    int flavor = temp.grid[i][j];\n                    for (int d = 0; d < 4; d++) {\n                        int nx = i + dx[d];\n                        int ny = j + dy[d];\n                        if (nx >= 0 && nx < GRID_SIZE && ny >= 0 && ny < GRID_SIZE) {\n                            if (temp.grid[nx][ny] == flavor) {\n                                has_neighbor = true;\n                                break;\n                            }\n                        }\n                    }\n                    if (!has_neighbor) isolated_candies++;\n                }\n            }\n        }\n        score -= isolated_candies * 20; // Penalty for isolated candies\n        \n        return score;\n    }\n    \n    // Simulate lookahead\n    double simulate_lookahead(int dir, int current_turn) {\n        CandyBox temp = this->copy();\n        temp.slide_candies(dir);\n        \n        double total_score = temp.evaluate_move_advanced(dir, current_turn);\n        \n        // Simple lookahead - simulate next few placements randomly\n        int lookahead_depth = min(LOOKAHEAD, TOTAL_CANDIES - current_turn - 1);\n        CandyBox lookahead_state = temp.copy();\n        \n        for (int i = 0; i < lookahead_depth; i++) {\n            int future_turn = current_turn + 1 + i;\n            if (future_turn >= TOTAL_CANDIES) break;\n            \n            // Simulate a random placement (we don't know exact position, but we know flavor)\n            int flavor = lookahead_state.flavor_sequence[future_turn];\n            \n            // Find a reasonable position - prefer corners/edges for isolation control\n            vector<Position> empty_positions;\n            for (int x = 0; x < GRID_SIZE; x++) {\n                for (int y = 0; y < GRID_SIZE; y++) {\n                    if (lookahead_state.grid[x][y] == 0) {\n                        empty_positions.push_back(Position(x, y));\n                    }\n                }\n            }\n            \n            if (!empty_positions.empty()) {\n                // Place in a position that might help clustering\n                lookahead_state.place_candy(empty_positions[0].x, empty_positions[0].y, flavor);\n            }\n            \n            // Apply a reasonable tilt (prefer clustering directions)\n            int best_dir = 0;\n            double best_score = -1e9;\n            for (int d = 0; d < 4; d++) {\n                double score = lookahead_state.evaluate_move_advanced(d, future_turn);\n                if (score > best_score) {\n                    best_score = score;\n                    best_dir = d;\n                }\n            }\n            lookahead_state.slide_candies(best_dir);\n        }\n        \n        total_score += lookahead_state.calculate_connectedness() * 0.3;\n        return total_score;\n    }\n};\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    CandyBox box;\n    box.read_flavors();\n    \n    for (int turn = 0; turn < TOTAL_CANDIES; turn++) {\n        int p;\n        cin >> p;\n        \n        // Place the candy\n        Position pos = box.index_to_position(p);\n        box.place_candy(pos.x, pos.y, box.flavor_sequence[turn]);\n        \n        // Don't need to output direction for last candy\n        if (turn == TOTAL_CANDIES - 1) {\n            cout << \"F\" << endl;\n            cout.flush();\n            break;\n        }\n        \n        // Evaluate all four directions and choose best\n        double best_score = -1e18;\n        int best_direction = 0;\n        \n        for (int dir = 0; dir < 4; dir++) {\n            double score = box.simulate_lookahead(dir, turn);\n            if (score > best_score) {\n                best_score = score;\n                best_direction = dir;\n            }\n        }\n        \n        // Output the chosen direction\n        cout << DIR_CHARS[best_direction] << endl;\n        cout.flush();\n        \n        // Apply the chosen move\n        box.slide_candies(best_direction);\n    }\n    \n    return 0;\n}","ahc016":"#include <bits/stdc++.h>\nusing namespace std;\n\nmt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n\nconst int MAX_N = 100;\nconst int QUERY_COUNT = 100;\n\nint M;\ndouble epsilon;\nint N;\nvector<string> graphs;\nvector<vector<vector<int>>> graph_matrices;\n\n// Convert adjacency matrix to edge string\nstring matrixToEdgeString(const vector<vector<int>>& matrix) {\n    string result;\n    for (int i = 0; i < N; i++) {\n        for (int j = i + 1; j < N; j++) {\n            result += (matrix[i][j] ? '1' : '0');\n        }\n    }\n    return result;\n}\n\n// Convert edge string to adjacency matrix\nvector<vector<int>> edgeStringToMatrix(const string& edge_str) {\n    vector<vector<int>> matrix(N, vector<int>(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 (edge_str[idx] == '1') {\n                matrix[i][j] = 1;\n                matrix[j][i] = 1;\n            }\n            idx++;\n        }\n    }\n    return matrix;\n}\n\n// Calculate graph features for better discrimination\nvector<int> getDegreeSequence(const vector<vector<int>>& matrix) {\n    vector<int> degrees(N);\n    for (int i = 0; i < N; i++) {\n        degrees[i] = accumulate(matrix[i].begin(), matrix[i].end(), 0);\n    }\n    sort(degrees.begin(), degrees.end());\n    return degrees;\n}\n\n// Generate more distinguishable graphs\nvoid generateGraphs() {\n    // Adaptive N selection based on M and epsilon\n    if (M <= 15) N = 12;\n    else if (M <= 30) N = 16;\n    else if (M <= 50) N = 20;\n    else if (M <= 70) N = 24;\n    else N = 28;\n    \n    // For high error rates, use larger N\n    if (epsilon > 0.3) N = min(40, max(N, 20));\n    else if (epsilon > 0.2) N = min(35, max(N, 18));\n    \n    N = min(N, 100);\n    \n    graphs.clear();\n    graph_matrices.clear();\n    \n    // Generate graphs with different structures\n    for (int k = 0; k < M; k++) {\n        vector<vector<int>> matrix(N, vector<int>(N, 0));\n        \n        // Use multiple strategies based on k\n        if (k < M / 3) {\n            // Strategy 1: Regular graphs with different degrees\n            int target_degree = max(1, min(N-1, 1 + (k * (N-2)) / max(1, M/3 - 1)));\n            for (int i = 0; i < N; i++) {\n                int degree_count = 0;\n                for (int j = 0; j < N && degree_count < target_degree; j++) {\n                    if (i != j && uniform_int_distribution<int>(0, N-1)(rng) < target_degree) {\n                        matrix[i][j] = 1;\n                        matrix[j][i] = 1;\n                        degree_count++;\n                    }\n                }\n            }\n        } else if (k < 2 * M / 3) {\n            // Strategy 2: Cluster-based graphs\n            int clusters = max(2, min(N/2, 2 + (k * 3) / max(1, M/3)));\n            vector<int> cluster_assignment(N);\n            for (int i = 0; i < N; i++) {\n                cluster_assignment[i] = i % clusters;\n            }\n            \n            // Intra-cluster edges\n            for (int i = 0; i < N; i++) {\n                for (int j = i + 1; j < N; j++) {\n                    if (cluster_assignment[i] == cluster_assignment[j]) {\n                        if (uniform_int_distribution<int>(0, 2)(rng) > 0) {\n                            matrix[i][j] = 1;\n                            matrix[j][i] = 1;\n                        }\n                    } else {\n                        // Few inter-cluster edges\n                        if (uniform_int_distribution<int>(0, clusters*2)(rng) == 0) {\n                            matrix[i][j] = 1;\n                            matrix[j][i] = 1;\n                        }\n                    }\n                }\n            }\n        } else {\n            // Strategy 3: Random graphs with controlled edge count\n            int edge_count = max(N, min(N*(N-1)/4, (k * N * (N-1) / 2) / max(1, M - 2*M/3)));\n            for (int e = 0; e < edge_count; e++) {\n                int i, j;\n                do {\n                    i = uniform_int_distribution<int>(0, N-2)(rng);\n                    j = uniform_int_distribution<int>(i+1, N-1)(rng);\n                } while (matrix[i][j] == 1);\n                matrix[i][j] = 1;\n                matrix[j][i] = 1;\n            }\n        }\n        \n        graphs.push_back(matrixToEdgeString(matrix));\n        graph_matrices.push_back(matrix);\n    }\n}\n\n// Improved similarity calculation with better permutation handling\ndouble calculateSimilarity(const string& original, const string& received) {\n    auto orig_matrix = edgeStringToMatrix(original);\n    auto recv_matrix = edgeStringToMatrix(received);\n    \n    // Get degree sequences for quick filtering\n    auto orig_degrees = getDegreeSequence(orig_matrix);\n    auto recv_degrees = getDegreeSequence(recv_matrix);\n    \n    // Quick similarity check based on degree sequences\n    int degree_similarity = 0;\n    for (int i = 0; i < N; i++) {\n        degree_similarity += abs(orig_degrees[i] - recv_degrees[i]);\n    }\n    \n    // If degree sequences are too different, this is likely not a match\n    if (degree_similarity > N * epsilon * 4) {\n        return -1e9;\n    }\n    \n    // Try multiple random permutations\n    double best_score = -1e18;\n    \n    // Identity mapping\n    double identity_score = 0;\n    for (int i = 0; i < original.size(); i++) {\n        if (original[i] == received[i]) {\n            identity_score += log(1.0 - epsilon + 1e-12);\n        } else {\n            identity_score += log(epsilon + 1e-12);\n        }\n    }\n    best_score = identity_score;\n    \n    // Try random permutations\n    vector<int> perm(N);\n    for (int i = 0; i < N; i++) perm[i] = i;\n    \n    int trials = min(100, max(20, N * 2));\n    if (epsilon > 0.25) trials *= 2;\n    \n    for (int trial = 0; trial < trials; trial++) {\n        shuffle(perm.begin(), perm.end(), rng);\n        \n        double score = 0;\n        int idx = 0;\n        for (int i = 0; i < N; i++) {\n            for (int j = i + 1; j < N; j++) {\n                int pi = perm[i], pj = perm[j];\n                if (pi > pj) swap(pi, pj);\n                \n                // Calculate index in original graph\n                int orig_idx = 0;\n                for (int x = 0; x < pi; x++) {\n                    orig_idx += (N - 1 - x);\n                }\n                orig_idx += (pj - pi - 1);\n                \n                if (orig_idx >= 0 && orig_idx < original.size()) {\n                    char orig_edge = original[orig_idx];\n                    char recv_edge = received[idx];\n                    \n                    if (orig_edge == recv_edge) {\n                        score += log(1.0 - epsilon + 1e-12);\n                    } else {\n                        score += log(epsilon + 1e-12);\n                    }\n                }\n                idx++;\n            }\n        }\n        \n        best_score = max(best_score, score);\n    }\n    \n    return best_score;\n}\n\n// Enhanced decoding with multiple verification steps\nint decodeGraph(const string& received) {\n    vector<pair<double, int>> scores(M);\n    \n    for (int i = 0; i < M; i++) {\n        scores[i].second = i;\n        scores[i].first = calculateSimilarity(graphs[i], received);\n    }\n    \n    sort(scores.begin(), scores.end(), greater<pair<double, int>>());\n    \n    // If top score is significantly better, return it\n    if (scores.size() > 1 && scores[0].first > scores[1].first + 5.0) {\n        return scores[0].second;\n    }\n    \n    // Otherwise, use edge count as secondary criterion\n    int recv_edges = count(received.begin(), received.end(), '1');\n    vector<pair<int, int>> edge_diffs(M);\n    \n    for (int i = 0; i < M; i++) {\n        int orig_edges = count(graphs[i].begin(), graphs[i].end(), '1');\n        edge_diffs[i] = {abs(orig_edges - recv_edges), i};\n    }\n    \n    sort(edge_diffs.begin(), edge_diffs.end());\n    \n    // Blend both approaches\n    if (scores[0].first > -1e6) {\n        return scores[0].second;\n    }\n    return edge_diffs[0].second;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    cin >> M >> epsilon;\n    \n    // Generate graphs\n    generateGraphs();\n    \n    // Output N\n    cout << N << endl;\n    \n    // Output all graphs\n    for (const string& graph : graphs) {\n        cout << graph << endl;\n    }\n    cout.flush();\n    \n    // Process queries\n    for (int q = 0; q < QUERY_COUNT; q++) {\n        string received;\n        cin >> received;\n        \n        int prediction = decodeGraph(received);\n        cout << prediction << endl;\n        cout.flush();\n    }\n    \n    return 0;\n}","ahc017":"#include <bits/stdc++.h>\n#include <atcoder/all>\nusing namespace std;\nusing namespace atcoder;\n\nusing ll = long long;\nusing P = pair<int, int>;\nusing PL = pair<ll, ll>;\nusing PLL = pair<ll, ll>;\n\nconst ll INF = 1e15;\nconst int MAXN = 1005;\nconst int MAXM = 3005;\n\nint N, M, D, K;\nvector<tuple<int, int, int>> edges;\nvector<P> coords;\nvector<vector<P>> adj;\nvector<tuple<int, int, ll>> dists;\n\n// Calculate shortest distances using Floyd-Warshall for small graphs or Dijkstra for larger ones\nvector<vector<ll>> calculate_all_pairs_distances(const vector<vector<P>>& graph) {\n    vector<vector<ll>> dist(N, vector<ll>(N, INF));\n    \n    // For smaller graphs, use Floyd-Warshall\n    if (N <= 300) {\n        // Initialize distances\n        for (int i = 0; i < N; i++) {\n            dist[i][i] = 0;\n            for (auto& edge : graph[i]) {\n                dist[i][edge.first] = min(dist[i][edge.first], (ll)edge.second);\n            }\n        }\n        \n        // Floyd-Warshall\n        for (int k = 0; k < N; k++) {\n            for (int i = 0; i < N; i++) {\n                for (int j = 0; j < N; j++) {\n                    if (dist[i][k] < INF && dist[k][j] < INF) {\n                        dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);\n                    }\n                }\n            }\n        }\n    } else {\n        // For larger graphs, use Dijkstra from each node\n        for (int s = 0; s < N; s++) {\n            priority_queue<PL, vector<PL>, greater<PL>> pq;\n            vector<ll> d(N, INF);\n            d[s] = 0;\n            pq.push({0, s});\n            \n            while (!pq.empty()) {\n                auto [cur_dist, u] = pq.top(); pq.pop();\n                if (cur_dist > d[u]) continue;\n                \n                for (auto& edge : graph[u]) {\n                    int v = edge.first;\n                    ll w = edge.second;\n                    if (d[u] + w < d[v]) {\n                        d[v] = d[u] + w;\n                        pq.push({d[v], v});\n                    }\n                }\n            }\n            \n            for (int i = 0; i < N; i++) {\n                dist[s][i] = d[i];\n            }\n        }\n    }\n    \n    return dist;\n}\n\n// Calculate frustration for a given day (edges to remove)\nll calculate_frustration(const vector<vector<ll>>& original_dist, \n                        const vector<bool>& removed_edges,\n                        const vector<vector<P>>& graph) {\n    // Create graph without removed edges\n    vector<vector<P>> temp_graph(N);\n    for (int i = 0; i < M; i++) {\n        if (!removed_edges[i]) {\n            auto [u, v, w] = edges[i];\n            temp_graph[u-1].push_back({v-1, w});\n            temp_graph[v-1].push_back({u-1, w});\n        }\n    }\n    \n    auto new_dist = calculate_all_pairs_distances(temp_graph);\n    \n    ll total_increase = 0;\n    int count = 0;\n    \n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (i != j) {\n                ll increase = new_dist[i][j] - original_dist[i][j];\n                if (new_dist[i][j] >= 1000000000) increase = 1000000000; // Cap at 1e9\n                total_increase += max(0LL, increase);\n                count++;\n            }\n        }\n    }\n    \n    return total_increase;\n}\n\n// Heuristic approach to assign edges to days\nvector<int> solve() {\n    vector<int> result(M, 1);\n    \n    // Calculate original distances\n    vector<vector<P>> original_graph(N);\n    for (auto& edge : edges) {\n        auto [u, v, w] = edge;\n        original_graph[u-1].push_back({v-1, w});\n        original_graph[v-1].push_back({u-1, w});\n    }\n    \n    auto original_dist = calculate_all_pairs_distances(original_graph);\n    \n    // Simple greedy assignment - distribute edges evenly\n    vector<vector<int>> days(D + 1);\n    for (int i = 0; i < M; i++) {\n        int day = (i % D) + 1;\n        result[i] = day;\n        days[day].push_back(i);\n    }\n    \n    // Try to improve by swapping edges between days\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n    uniform_int_distribution<int> dist_day(1, D);\n    \n    for (int iter = 0; iter < 10000 && chrono::duration_cast<chrono::milliseconds>(chrono::steady_clock::now().time_since_epoch()).count() < 5500; iter++) {\n        int day1 = dist_day(rng);\n        int day2 = dist_day(rng);\n        if (day1 == day2) continue;\n        \n        if (!days[day1].empty() && days[day2].size() < K) {\n            uniform_int_distribution<int> dist_edge(0, (int)days[day1].size() - 1);\n            int idx = dist_edge(rng);\n            int edge_idx = days[day1][idx];\n            \n            // Check if moving this edge improves the solution\n            if (days[day1].size() > 1) {  // Don't empty a day\n                result[edge_idx] = day2;\n                days[day2].push_back(edge_idx);\n                days[day1].erase(days[day1].begin() + idx);\n            }\n        }\n    }\n    \n    return result;\n}\n\n// Alternative simpler approach focusing on edge betweenness\nvector<int> solve_simple() {\n    vector<int> result(M);\n    \n    // Assign edges to days in round-robin fashion\n    for (int i = 0; i < M; i++) {\n        result[i] = (i % D) + 1;\n    }\n    \n    return result;\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    cin >> N >> M >> D >> K;\n    edges.resize(M);\n    coords.resize(N);\n    \n    for (int i = 0; i < M; i++) {\n        int u, v, w;\n        cin >> u >> v >> w;\n        edges[i] = {u, v, w};\n    }\n    \n    for (int i = 0; i < N; i++) {\n        cin >> coords[i].first >> coords[i].second;\n    }\n    \n    // Build adjacency list\n    adj.assign(N, vector<P>());\n    for (auto& edge : edges) {\n        auto [u, v, w] = edge;\n        adj[u-1].push_back({v-1, w});\n        adj[v-1].push_back({u-1, w});\n    }\n    \n    auto result = solve_simple();\n    \n    for (int i = 0; i < M; i++) {\n        cout << result[i];\n        if (i < M - 1) cout << \" \";\n    }\n    cout << \"\\n\";\n    \n    return 0;\n}","ahc019":"#include <bits/stdc++.h>\n#include <atcoder/all>\nusing namespace std;\nusing namespace atcoder;\n\n// Direction vectors for connectivity checks\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// Generate small polycube blocks\nvector<vector<tuple<int, int, int>>> generateBlocks(int maxVolume) {\n    vector<vector<tuple<int, int, int>>> blocks;\n    \n    // Single cube\n    blocks.push_back({{0, 0, 0}});\n    \n    // 2 cubes\n    blocks.push_back({{0, 0, 0}, {1, 0, 0}});\n    blocks.push_back({{0, 0, 0}, {0, 1, 0}});\n    blocks.push_back({{0, 0, 0}, {0, 0, 1}});\n    \n    // 3 cubes - lines\n    blocks.push_back({{0, 0, 0}, {1, 0, 0}, {2, 0, 0}});\n    blocks.push_back({{0, 0, 0}, {0, 1, 0}, {0, 2, 0}});\n    blocks.push_back({{0, 0, 0}, {0, 0, 1}, {0, 0, 2}});\n    \n    // 3 cubes - L-shape\n    blocks.push_back({{0, 0, 0}, {1, 0, 0}, {0, 1, 0}});\n    blocks.push_back({{0, 0, 0}, {1, 0, 0}, {0, 0, 1}});\n    blocks.push_back({{0, 0, 0}, {0, 1, 0}, {0, 0, 1}});\n    \n    // 4 cubes - 2x2 square\n    blocks.push_back({{0, 0, 0}, {1, 0, 0}, {0, 1, 0}, {1, 1, 0}});\n    \n    // 4 cubes - line\n    blocks.push_back({{0, 0, 0}, {1, 0, 0}, {2, 0, 0}, {3, 0, 0}});\n    \n    // 4 cubes - T-shape\n    blocks.push_back({{0, 0, 0}, {1, 0, 0}, {2, 0, 0}, {1, 1, 0}});\n    \n    // 4 cubes - 3D shapes\n    blocks.push_back({{0, 0, 0}, {1, 0, 0}, {0, 1, 0}, {0, 0, 1}});\n    \n    return blocks;\n}\n\n// Check if a block is connected\nbool isConnected(const vector<tuple<int, int, int>>& block) {\n    if (block.size() <= 1) return true;\n    \n    set<tuple<int, int, int>> visited;\n    queue<tuple<int, int, int>> q;\n    q.push(block[0]);\n    visited.insert(block[0]);\n    \n    while (!q.empty()) {\n        auto [x, y, z] = q.front(); q.pop();\n        \n        for (int i = 0; i < 6; i++) {\n            int nx = x + dx[i], ny = y + dy[i], nz = z + dz[i];\n            tuple<int, int, int> neighbor(nx, ny, nz);\n            \n            if (find(block.begin(), block.end(), neighbor) != block.end() && \n                visited.find(neighbor) == visited.end()) {\n                visited.insert(neighbor);\n                q.push(neighbor);\n            }\n        }\n    }\n    \n    return visited.size() == block.size();\n}\n\n// Rotate a block 90 degrees around x-axis\nvector<tuple<int, int, int>> rotateX(const vector<tuple<int, int, int>>& block) {\n    vector<tuple<int, int, int>> rotated;\n    for (auto [x, y, z] : block) {\n        rotated.push_back({x, -z, y});\n    }\n    return rotated;\n}\n\n// Rotate a block 90 degrees around y-axis\nvector<tuple<int, int, int>> rotateY(const vector<tuple<int, int, int>>& block) {\n    vector<tuple<int, int, int>> rotated;\n    for (auto [x, y, z] : block) {\n        rotated.push_back({z, y, -x});\n    }\n    return rotated;\n}\n\n// Rotate a block 90 degrees around z-axis\nvector<tuple<int, int, int>> rotateZ(const vector<tuple<int, int, int>>& block) {\n    vector<tuple<int, int, int>> rotated;\n    for (auto [x, y, z] : block) {\n        rotated.push_back({-y, x, z});\n    }\n    return rotated;\n}\n\n// Normalize block position (translate to origin)\nvector<tuple<int, int, int>> normalize(const vector<tuple<int, int, int>>& block) {\n    if (block.empty()) return block;\n    \n    int min_x = get<0>(*min_element(block.begin(), block.end(), \n                                   [](const auto& a, const auto& b) { return get<0>(a) < get<0>(b); }));\n    int min_y = get<1>(*min_element(block.begin(), block.end(), \n                                   [](const auto& a, const auto& b) { return get<1>(a) < get<1>(b); }));\n    int min_z = get<2>(*min_element(block.begin(), block.end(), \n                                   [](const auto& a, const auto& b) { return get<2>(a) < get<2>(b); }));\n    \n    vector<tuple<int, int, int>> normalized;\n    for (auto [x, y, z] : block) {\n        normalized.push_back({x - min_x, y - min_y, z - min_z});\n    }\n    return normalized;\n}\n\n// Generate all rotations of a block\nset<vector<tuple<int, int, int>>> getAllRotations(const vector<tuple<int, int, int>>& block) {\n    set<vector<tuple<int, int, int>>> rotations;\n    \n    auto current = block;\n    for (int rx = 0; rx < 4; rx++) {\n        auto temp = current;\n        for (int ry = 0; ry < 4; ry++) {\n            auto temp2 = temp;\n            for (int rz = 0; rz < 4; rz++) {\n                rotations.insert(normalize(temp2));\n                temp2 = rotateZ(temp2);\n            }\n            temp = rotateY(temp);\n        }\n        current = rotateX(current);\n    }\n    \n    return rotations;\n}\n\n// Check if block fits in valid positions for silhouette\nbool canPlaceBlock(const vector<tuple<int, int, int>>& block, \n                   const vector<vector<int>>& front, \n                   const vector<vector<int>>& right,\n                   int x_offset, int y_offset, int z_offset) {\n    for (auto [x, y, z] : block) {\n        int nx = x + x_offset, ny = y + y_offset, nz = z + z_offset;\n        if (nx < 0 || nx >= front[0].size() || \n            ny < 0 || ny >= right[0].size() || \n            nz < 0 || nz >= front.size()) {\n            return false;\n        }\n        if (front[nz][nx] == 0 || right[nz][ny] == 0) {\n            return false;\n        }\n    }\n    return true;\n}\n\n// Try to place blocks to satisfy silhouettes\npair<vector<vector<vector<int>>>, vector<int>> solveSilhouette(\n    const vector<vector<int>>& front, \n    const vector<vector<int>>& right,\n    const vector<vector<tuple<int, int, int>>>& blocks) {\n    \n    int D = front.size();\n    vector<vector<vector<int>>> result(D, vector<vector<int>>(D, vector<int>(D, 0)));\n    vector<int> used_blocks;\n    \n    // Simple greedy approach - place blocks where both silhouettes are 1\n    vector<vector<vector<bool>>> occupied(D, vector<vector<bool>>(D, vector<bool>(D, false)));\n    \n    // Try to fill positions where both silhouettes are active\n    for (int z = 0; z < D; z++) {\n        for (int x = 0; x < D; x++) {\n            if (front[z][x] == 1) {\n                for (int y = 0; y < D; y++) {\n                    if (right[z][y] == 1 && !occupied[z][x][y]) {\n                        // Try to place a small block here\n                        result[z][x][y] = used_blocks.size() + 1;\n                        used_blocks.push_back(0); // Single cube block index\n                        occupied[z][x][y] = true;\n                    }\n                }\n            }\n        }\n    }\n    \n    return {result, used_blocks};\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int D;\n    cin >> D;\n    \n    vector<vector<int>> f1(D, vector<int>(D));\n    vector<vector<int>> r1(D, vector<int>(D));\n    vector<vector<int>> f2(D, vector<int>(D));\n    vector<vector<int>> r2(D, vector<int>(D));\n    \n    // Read first silhouette pair\n    for (int i = 0; i < D; i++) {\n        string line;\n        cin >> line;\n        for (int j = 0; j < D; j++) {\n            f1[i][j] = line[j] - '0';\n        }\n    }\n    for (int i = 0; i < D; i++) {\n        string line;\n        cin >> line;\n        for (int j = 0; j < D; j++) {\n            r1[i][j] = line[j] - '0';\n        }\n    }\n    \n    // Read second silhouette pair\n    for (int i = 0; i < D; i++) {\n        string line;\n        cin >> line;\n        for (int j = 0; j < D; j++) {\n            f2[i][j] = line[j] - '0';\n        }\n    }\n    for (int i = 0; i < D; i++) {\n        string line;\n        cin >> line;\n        for (int j = 0; j < D; j++) {\n            r2[i][j] = line[j] - '0';\n        }\n    }\n    \n    // Generate basic blocks\n    auto blocks = generateBlocks(4);\n    \n    // Solve for both silhouette pairs\n    auto [solution1, used1] = solveSilhouette(f1, r1, blocks);\n    auto [solution2, used2] = solveSilhouette(f2, r2, blocks);\n    \n    // Count total unique blocks needed\n    int max_block = 0;\n    for (int z = 0; z < D; z++) {\n        for (int x = 0; x < D; x++) {\n            for (int y = 0; y < D; y++) {\n                max_block = max({max_block, solution1[z][x][y], solution2[z][x][y]});\n            }\n        }\n    }\n    \n    // Output format\n    cout << max_block << \"\\n\";\n    \n    // Output first solution\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 (z > 0) cout << \" \";\n                cout << solution1[z][x][y];\n            }\n        }\n    }\n    cout << \"\\n\";\n    \n    // Output second solution\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 (z > 0) cout << \" \";\n                cout << solution2[z][x][y];\n            }\n        }\n    }\n    cout << \"\\n\";\n    \n    return 0;\n}","ahc020":"#include <bits/stdc++.h>\n#include <atcoder/all>\nusing namespace std;\nusing namespace atcoder;\n\nusing ll = long long;\nusing ld = long double;\n\nconst int MAXN = 105;\nconst int MAXM = 305;\nconst int MAXK = 5005;\n\nint N, M, K;\npair<int, int> vertices[MAXN];\ntuple<int, int, int> edges[MAXM];\npair<int, int> residents[MAXK];\nint P[MAXN];\nint B[MAXM];\nvector<vector<int>> adj[MAXN];\nvector<pair<int, int>> edge_list;\n\n// Distance calculation\nll dist_sq(int x1, int y1, int x2, int y2) {\n    return 1LL * (x1 - x2) * (x1 - x2) + 1LL * (y1 - y2) * (y1 - y2);\n}\n\nld dist(int x1, int y1, int x2, int y2) {\n    return sqrt(dist_sq(x1, y1, x2, y2));\n}\n\n// Find closest vertex to a resident\nint find_closest_vertex(int rx, int ry) {\n    int best = 0;\n    ll best_dist = dist_sq(rx, ry, vertices[0].first, vertices[0].second);\n    for (int i = 1; i < N; i++) {\n        ll d = dist_sq(rx, ry, vertices[i].first, vertices[i].second);\n        if (d < best_dist) {\n            best_dist = d;\n            best = i;\n        }\n    }\n    return best;\n}\n\n// Calculate required radius to cover residents from a vertex\nint calculate_radius(int vertex_idx, const vector<int>& assigned_residents) {\n    if (assigned_residents.empty()) return 0;\n    \n    int vx = vertices[vertex_idx].first;\n    int vy = vertices[vertex_idx].second;\n    int max_dist = 0;\n    \n    for (int res_idx : assigned_residents) {\n        int rx = residents[res_idx].first;\n        int ry = residents[res_idx].second;\n        int d = (int)round(dist(vx, vy, rx, ry));\n        max_dist = max(max_dist, d);\n    }\n    \n    return min(max_dist, 5000);\n}\n\n// Prim's algorithm for minimum spanning tree\nvector<bool> find_minimal_network(const vector<bool>& must_include) {\n    vector<bool> in_tree(N, false);\n    vector<ll> min_cost(N, 1e18);\n    vector<int> parent(N, -1);\n    \n    // Start from vertex 0 (AtCoder office)\n    in_tree[0] = true;\n    min_cost[0] = 0;\n    \n    // Initialize costs for adjacent vertices\n    vector<pair<int, int>> adj_edges[N];\n    for (int i = 0; i < M; i++) {\n        int u = get<0>(edges[i]) - 1;\n        int v = get<1>(edges[i]) - 1;\n        int w = get<2>(edges[i]);\n        adj_edges[u].push_back({v, w});\n        adj_edges[v].push_back({u, w});\n    }\n    \n    priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<>> pq;\n    \n    for (auto& e : adj_edges[0]) {\n        int to = e.first;\n        int weight = e.second;\n        if (min_cost[to] > weight) {\n            min_cost[to] = weight;\n            parent[to] = 0;\n            pq.push({min_cost[to], to});\n        }\n    }\n    \n    while (!pq.empty()) {\n        auto [cost, u] = pq.top(); pq.pop();\n        if (in_tree[u]) continue;\n        in_tree[u] = true;\n        \n        for (auto& e : adj_edges[u]) {\n            int to = e.first;\n            int weight = e.second;\n            if (!in_tree[to] && min_cost[to] > weight) {\n                min_cost[to] = weight;\n                parent[to] = u;\n                pq.push({min_cost[to], to});\n            }\n        }\n    }\n    \n    // Ensure all must_include vertices are connected\n    vector<bool> result(N, false);\n    for (int i = 0; i < N; i++) {\n        if (must_include[i]) {\n            int current = i;\n            while (current != -1 && !result[current]) {\n                result[current] = true;\n                current = parent[current];\n            }\n        }\n    }\n    \n    // Always include vertex 0\n    result[0] = true;\n    \n    // Connect components if needed\n    queue<int> q;\n    for (int i = 0; i < N; i++) {\n        if (result[i]) q.push(i);\n    }\n    \n    vector<bool> visited(N, false);\n    while (!q.empty()) {\n        int u = q.front(); q.pop();\n        if (visited[u]) continue;\n        visited[u] = true;\n        \n        for (auto& e : adj_edges[u]) {\n            int v = e.first;\n            if (!visited[v]) {\n                q.push(v);\n                result[v] = true;\n            }\n        }\n    }\n    \n    return result;\n}\n\nvoid solve() {\n    // Assign residents to closest vertices\n    vector<vector<int>> vertex_residents(N);\n    for (int i = 0; i < K; i++) {\n        int closest = find_closest_vertex(residents[i].first, residents[i].second);\n        vertex_residents[closest].push_back(i);\n    }\n    \n    // Determine which vertices we might want to include\n    vector<bool> should_include(N, false);\n    vector<int> required_radius(N, 0);\n    \n    for (int i = 0; i < N; i++) {\n        if (!vertex_residents[i].empty()) {\n            required_radius[i] = calculate_radius(i, vertex_residents[i]);\n            if (required_radius[i] > 0) {\n                should_include[i] = true;\n            }\n        }\n    }\n    \n    // Find minimal network to connect necessary vertices\n    vector<bool> network = find_minimal_network(should_include);\n    \n    // Calculate final output strengths\n    fill(P, P + N, 0);\n    for (int i = 0; i < N; i++) {\n        if (network[i]) {\n            P[i] = required_radius[i];\n        }\n    }\n    \n    // Set edge states\n    fill(B, B + M, 0);\n    vector<vector<pair<int, int>>> graph_adj(N);\n    for (int i = 0; i < M; i++) {\n        int u = get<0>(edges[i]) - 1;\n        int v = get<1>(edges[i]) - 1;\n        if (network[u] && network[v]) {\n            graph_adj[u].push_back({v, i});\n            graph_adj[v].push_back({u, i});\n        }\n    }\n    \n    // Build spanning tree on active vertices\n    vector<bool> visited(N, false);\n    queue<int> q;\n    q.push(0);\n    visited[0] = true;\n    \n    while (!q.empty()) {\n        int u = q.front(); q.pop();\n        for (auto& [v, edge_idx] : graph_adj[u]) {\n            if (!visited[v]) {\n                visited[v] = true;\n                B[edge_idx] = 1;\n                q.push(v);\n            }\n        }\n    }\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    cin >> N >> M >> K;\n    \n    for (int i = 0; i < N; i++) {\n        cin >> vertices[i].first >> vertices[i].second;\n    }\n    \n    for (int i = 0; i < M; i++) {\n        int u, v, w;\n        cin >> u >> v >> w;\n        edges[i] = {u, v, w};\n    }\n    \n    for (int i = 0; i < K; i++) {\n        cin >> residents[i].first >> residents[i].second;\n    }\n    \n    solve();\n    \n    for (int i = 0; i < N; i++) {\n        cout << P[i];\n        if (i + 1 < N) cout << \" \";\n    }\n    cout << \"\\n\";\n    \n    for (int i = 0; i < M; i++) {\n        cout << B[i];\n        if (i + 1 < M) cout << \" \";\n    }\n    cout << \"\\n\";\n    \n    return 0;\n}","ahc021":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 30;\nconst int MAX_OPS = 10000;\nconst int TOTAL_BALLS = 465;\n\nint pyramid[N][N];\nvector<tuple<int, int, int, int>> operations;\n\n// Check if coordinates are valid\nbool valid(int x, int y) {\n    return x >= 0 && x < N && y >= 0 && y <= x;\n}\n\n// Get adjacent positions in 6 directions\nvector<pair<int, int>> getAdjacent(int x, int y) {\n    vector<pair<int, int>> adj;\n    vector<pair<int, int>> directions = {\n        {-1, -1}, {-1, 0}, {0, -1}, {0, 1}, {1, 0}, {1, 1}\n    };\n    \n    for (auto& dir : directions) {\n        int nx = x + dir.first;\n        int ny = y + dir.second;\n        if (valid(nx, ny)) {\n            adj.push_back({nx, ny});\n        }\n    }\n    return adj;\n}\n\n// Count violations in current state\nint countViolations() {\n    int violations = 0;\n    for (int x = 0; x < N - 1; x++) {\n        for (int y = 0; y <= x; y++) {\n            if (pyramid[x][y] > pyramid[x+1][y]) violations++;\n            if (pyramid[x][y] > pyramid[x+1][y+1]) violations++;\n        }\n    }\n    return violations;\n}\n\n// Find shortest path between two positions using BFS\nvector<pair<int, int>> findPath(int startX, int startY, int endX, int endY) {\n    if (startX == endX && startY == endY) return {};\n    \n    queue<pair<int, int>> q;\n    map<pair<int, int>, pair<int, int>> parent;\n    set<pair<int, int>> visited;\n    \n    q.push({startX, startY});\n    visited.insert({startX, startY});\n    \n    vector<pair<int, int>> directions = {{-1, -1}, {-1, 0}, {0, -1}, {0, 1}, {1, 0}, {1, 1}};\n    \n    while (!q.empty()) {\n        auto [x, y] = q.front();\n        q.pop();\n        \n        if (x == endX && y == endY) {\n            // Reconstruct path\n            vector<pair<int, int>> path;\n            auto current = make_pair(endX, endY);\n            while (current != make_pair(startX, startY)) {\n                path.push_back(current);\n                current = parent[current];\n            }\n            reverse(path.begin(), path.end());\n            return path;\n        }\n        \n        for (auto& dir : directions) {\n            int nx = x + dir.first;\n            int ny = y + dir.second;\n            if (valid(nx, ny) && visited.find({nx, ny}) == visited.end()) {\n                visited.insert({nx, ny});\n                parent[{nx, ny}] = {x, y};\n                q.push({nx, ny});\n            }\n        }\n    }\n    \n    return {}; // No path found\n}\n\n// Perform a swap and record operation\nvoid performSwap(int x1, int y1, int x2, int y2) {\n    if (operations.size() >= MAX_OPS) return;\n    \n    swap(pyramid[x1][y1], pyramid[x2][y2]);\n    operations.push_back({x1, y1, x2, y2});\n}\n\n// Bubble down approach - move larger values toward bottom\nvoid bubbleSortApproach() {\n    // Multiple passes with different strategies\n    for (int pass = 0; pass < 5 && operations.size() < MAX_OPS - 50; pass++) {\n        // Pass 1: Bottom-up fixing\n        if (pass % 2 == 0) {\n            for (int x = N - 2; x >= 0; x--) {\n                for (int y = 0; y <= x && operations.size() < MAX_OPS - 10; y++) {\n                    if (x < N - 1) {\n                        // Check violations with children\n                        bool violation1 = (pyramid[x][y] > pyramid[x+1][y]);\n                        bool violation2 = (pyramid[x][y] > pyramid[x+1][y+1]);\n                        \n                        if (violation1 && operations.size() < MAX_OPS - 5) {\n                            performSwap(x, y, x+1, y);\n                        }\n                        if (violation2 && operations.size() < MAX_OPS - 5) {\n                            performSwap(x, y, x+1, y+1);\n                        }\n                    }\n                }\n            }\n        } \n        // Pass 2: Top-down fixing\n        else {\n            for (int x = 0; x < N - 1 && operations.size() < MAX_OPS - 10; x++) {\n                for (int y = 0; y <= x && operations.size() < MAX_OPS - 10; y++) {\n                    if (x < N - 1) {\n                        // Check violations with children\n                        bool violation1 = (pyramid[x][y] > pyramid[x+1][y]);\n                        bool violation2 = (pyramid[x][y] > pyramid[x+1][y+1]);\n                        \n                        if ((violation1 || violation2) && operations.size() < MAX_OPS - 10) {\n                            // Choose the smaller child to swap with\n                            if (violation1 && violation2) {\n                                if (pyramid[x+1][y] <= pyramid[x+1][y+1]) {\n                                    performSwap(x, y, x+1, y);\n                                } else {\n                                    performSwap(x, y, x+1, y+1);\n                                }\n                            } else if (violation1) {\n                                performSwap(x, y, x+1, y);\n                            } else if (violation2) {\n                                performSwap(x, y, x+1, y+1);\n                            }\n                        }\n                    }\n                }\n            }\n        }\n    }\n}\n\n// More aggressive approach - try to sort locally\nvoid localOptimization() {\n    // Focus on areas with most violations\n    for (int iteration = 0; iteration < 3 && operations.size() < MAX_OPS - 100; iteration++) {\n        // Process each row\n        for (int x = 0; x < N - 1 && operations.size() < MAX_OPS - 50; x++) {\n            // Create a list of violations in this row\n            vector<tuple<int, int, int, int>> rowViolations; // (parent_x, parent_y, child_x, child_y)\n            \n            for (int y = 0; y <= x; y++) {\n                if (x < N - 1) {\n                    if (pyramid[x][y] > pyramid[x+1][y]) {\n                        rowViolations.push_back({x, y, x+1, y});\n                    }\n                    if (pyramid[x][y] > pyramid[x+1][y+1]) {\n                        rowViolations.push_back({x, y, x+1, y+1});\n                    }\n                }\n            }\n            \n            // Process violations in this row\n            for (auto& violation : rowViolations) {\n                if (operations.size() >= MAX_OPS - 10) break;\n                \n                int px, py, cx, cy;\n                tie(px, py, cx, cy) = violation;\n                \n                // Just do the swap to fix this specific violation\n                performSwap(px, py, cx, cy);\n            }\n        }\n    }\n}\n\n// Final polishing pass\nvoid polishSolution() {\n    if (operations.size() < MAX_OPS - 100) {\n        // One final pass to clean up any remaining easy violations\n        for (int x = 0; x < N - 1 && operations.size() < MAX_OPS - 10; x++) {\n            for (int y = 0; y <= x && operations.size() < MAX_OPS - 10; y++) {\n                if (x < N - 1) {\n                    if (pyramid[x][y] > pyramid[x+1][y]) {\n                        performSwap(x, y, x+1, y);\n                    }\n                    if (pyramid[x][y] > pyramid[x+1][y+1]) {\n                        performSwap(x, y, x+1, y+1);\n                    }\n                }\n            }\n        }\n    }\n}\n\nint main() {\n    // Read input\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j <= i; j++) {\n            cin >> pyramid[i][j];\n        }\n    }\n    \n    // Apply multiple strategies\n    bubbleSortApproach();\n    \n    if (operations.size() < MAX_OPS - 200) {\n        localOptimization();\n    }\n    \n    if (operations.size() < MAX_OPS - 50) {\n        polishSolution();\n    }\n    \n    // Output results\n    cout << operations.size() << \"\\n\";\n    for (auto& op : operations) {\n        cout << get<0>(op) << \" \" << get<1>(op) << \" \" << get<2>(op) << \" \" << get<3>(op) << \"\\n\";\n    }\n    \n    return 0;\n}","toyota2023summer-final":"#include <bits/stdc++.h>\n#include <atcoder/all>\nusing namespace std;\nusing namespace atcoder;\n\nconst int D = 9;\nconst int INF = 100000000;\n\nint entrance_i = 0;\nint entrance_j = 4;\n\nvector<vector<bool>> is_obstacle(D, vector<bool>(D, false));\nvector<vector<int>> container_id(D, vector<int>(D, -1));\nvector<pair<int, int>> container_positions(D * D); // id -> position\nvector<int> container_numbers(D * D); // position index -> container number\n\n// Directions: up, down, left, right\nconst int di[4] = {-1, 1, 0, 0};\nconst int dj[4] = {0, 0, -1, 1};\n\n// Check if position is valid\nbool is_valid(int i, int j) {\n    return i >= 0 && i < D && j >= 0 && j < D;\n}\n\n// Check if a position is the entrance or adjacent to entrance\nbool is_entrance_area(int i, int j) {\n    if (i == 0 && j == 4) return true;\n    if (i == 0 && j == 3) return true;\n    if (i == 0 && j == 5) return true;\n    if (i == 1 && j == 4) return true;\n    return false;\n}\n\n// BFS to find all reachable positions from entrance\nvector<vector<int>> get_distances_from_entrance() {\n    vector<vector<int>> dist(D, vector<int>(D, INF));\n    queue<pair<int, int>> q;\n    \n    // Start from entrance\n    dist[0][4] = 0;\n    q.push({0, 4});\n    \n    while (!q.empty()) {\n        auto [ci, cj] = q.front(); q.pop();\n        \n        for (int d = 0; d < 4; d++) {\n            int ni = ci + di[d];\n            int nj = cj + dj[d];\n            \n            if (is_valid(ni, nj) && !is_obstacle[ni][nj] && container_id[ni][nj] == -1) {\n                if (dist[ni][nj] > dist[ci][cj] + 1) {\n                    dist[ni][nj] = dist[ci][cj] + 1;\n                    q.push({ni, nj});\n                }\n            }\n        }\n    }\n    \n    return dist;\n}\n\n// Find all reachable empty positions\nvector<pair<int, int>> get_reachable_empty_positions() {\n    vector<vector<bool>> visited(D, vector<bool>(D, false));\n    vector<pair<int, int>> result;\n    queue<pair<int, int>> q;\n    \n    // Start from entrance\n    visited[0][4] = true;\n    q.push({0, 4});\n    if (container_id[0][4] == -1 && !is_obstacle[0][4]) {\n        result.push_back({0, 4});\n    }\n    \n    while (!q.empty()) {\n        auto [ci, cj] = q.front(); q.pop();\n        \n        for (int d = 0; d < 4; d++) {\n            int ni = ci + di[d];\n            int nj = cj + dj[d];\n            \n            if (is_valid(ni, nj) && !visited[ni][nj] && !is_obstacle[ni][nj]) {\n                visited[ni][nj] = true;\n                q.push({ni, nj});\n                if (container_id[ni][nj] == -1) {\n                    result.push_back({ni, nj});\n                }\n            }\n        }\n    }\n    \n    return result;\n}\n\n// Calculate Manhattan distance from entrance\nint manhattan_distance(int i, int j) {\n    return abs(i - 0) + abs(j - 4);\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int N;\n    cin >> D >> N;\n    \n    for (int i = 0; i < N; i++) {\n        int ri, rj;\n        cin >> ri >> rj;\n        is_obstacle[ri][rj] = true;\n    }\n    \n    int total_containers = D * D - 1 - N;\n    vector<int> transport_order(total_containers);\n    \n    // Process each container as it arrives\n    for (int d = 0; d < total_containers; d++) {\n        int t_d;\n        cin >> t_d;\n        transport_order[d] = t_d;\n        \n        // Get all reachable empty positions\n        auto reachable_positions = get_reachable_empty_positions();\n        \n        // Remove entrance area from consideration (except if it's the only option)\n        vector<pair<int, int>> valid_positions;\n        for (auto& pos : reachable_positions) {\n            if (!is_entrance_area(pos.first, pos.second)) {\n                valid_positions.push_back(pos);\n            }\n        }\n        \n        // If no valid positions except entrance area, use entrance area\n        if (valid_positions.empty()) {\n            for (auto& pos : reachable_positions) {\n                if (pos.first != 0 || pos.second != 4) { // exclude exact entrance\n                    valid_positions.push_back(pos);\n                }\n            }\n        }\n        \n        // If still empty, just use entrance\n        if (valid_positions.empty()) {\n            valid_positions.push_back({0, 4});\n        }\n        \n        // Choose position based on container number\n        // Lower numbers should be placed closer to entrance for easier retrieval\n        pair<int, int> best_position = valid_positions[0];\n        int best_score = INF;\n        \n        for (auto& pos : valid_positions) {\n            int dist = manhattan_distance(pos.first, pos.second);\n            // Prefer positions closer to entrance for lower numbered containers\n            // But also add some randomness to avoid getting stuck\n            int score = dist * 1000 + (t_d * 10) + (rand() % 100);\n            \n            if (score < best_score) {\n                best_score = score;\n                best_position = pos;\n            }\n        }\n        \n        // Place the container\n        int pi = best_position.first;\n        int pj = best_position.second;\n        container_id[pi][pj] = d;\n        container_positions[d] = {pi, pj};\n        container_numbers[d] = t_d;\n        \n        // Output the position\n        cout << pi << \" \" << pj << endl;\n        cout.flush();\n    }\n    \n    // Now determine transport order\n    // We want to transport in order of assigned numbers (0, 1, 2, ...)\n    // But we can only transport reachable containers\n    \n    vector<bool> transported(total_containers, false);\n    vector<pair<int, int>> transport_sequence;\n    \n    for (int target_number = 0; target_number < total_containers; target_number++) {\n        // Find container with target_number\n        int container_idx = -1;\n        for (int i = 0; i < total_containers; i++) {\n            if (container_numbers[i] == target_number) {\n                container_idx = i;\n                break;\n            }\n        }\n        \n        if (container_idx == -1) continue;\n        \n        // Mark this container for transport when it becomes reachable\n        pair<int, int> target_pos = container_positions[container_idx];\n        \n        // For simplicity, we'll transport containers in the order they become reachable\n        // In a more sophisticated solution, we'd use a priority queue\n        transport_sequence.push_back(target_pos);\n    }\n    \n    // Actually, let's implement a better approach:\n    // Try to transport containers in order of their numbers, but only when reachable\n    vector<bool> container_transported(total_containers, false);\n    transport_sequence.clear();\n    \n    while (transport_sequence.size() < total_containers) {\n        // Get currently reachable containers\n        auto distances = get_distances_from_entrance();\n        vector<pair<int, int>> reachable_containers;\n        vector<int> reachable_indices;\n        \n        for (int i = 0; i < total_containers; i++) {\n            if (container_transported[i]) continue;\n            \n            auto pos = container_positions[i];\n            if (distances[pos.first][pos.second] < INF) {\n                reachable_containers.push_back(pos);\n                reachable_indices.push_back(i);\n            }\n        }\n        \n        if (reachable_containers.empty()) {\n            // This shouldn't happen if the problem is solvable\n            break;\n        }\n        \n        // Among reachable containers, pick the one with lowest assigned number\n        int best_idx = reachable_indices[0];\n        int best_number = container_numbers[best_idx];\n        \n        for (int idx : reachable_indices) {\n            if (container_numbers[idx] < best_number) {\n                best_number = container_numbers[idx];\n                best_idx = idx;\n            }\n        }\n        \n        // Transport this container\n        auto pos = container_positions[best_idx];\n        transport_sequence.push_back(pos);\n        container_transported[best_idx] = true;\n        \n        // Clear the position\n        container_id[pos.first][pos.second] = -1;\n    }\n    \n    // Output transport sequence\n    for (auto& pos : transport_sequence) {\n        cout << pos.first << \" \" << pos.second << \"\\n\";\n    }\n    \n    return 0;\n}","ahc024":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 50;\nconst int M = 100;\n\nint grid[N][N];\nint result[N][N];\nvector<pair<int,int>> positions[M+1]; // positions for each color\nvector<int> adj_list[M+1]; // adjacency list\nbool adj_matrix[M+1][M+1]; // adjacency matrix\n\n// Directions: up, right, down, left\nconst int dx[4] = {-1, 0, 1, 0};\nconst int dy[4] = {0, 1, 0, -1};\n\nvoid extract_adjacencies() {\n    // Initialize adjacency matrix\n    memset(adj_matrix, 0, sizeof(adj_matrix));\n    \n    // Check adjacencies in the original grid\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            int color = grid[i][j];\n            for (int d = 0; d < 4; d++) {\n                int ni = i + dx[d];\n                int nj = j + dy[d];\n                int neighbor_color = 0; // outside is color 0\n                if (ni >= 0 && ni < N && nj >= 0 && nj < N) {\n                    neighbor_color = grid[ni][nj];\n                }\n                if (color != neighbor_color) {\n                    adj_matrix[color][neighbor_color] = true;\n                    adj_matrix[neighbor_color][color] = true;\n                }\n            }\n        }\n    }\n    \n    // Build adjacency lists\n    for (int i = 0; i <= M; i++) {\n        adj_list[i].clear();\n        for (int j = 0; j <= M; j++) {\n            if (adj_matrix[i][j] && i != j) {\n                adj_list[i].push_back(j);\n            }\n        }\n    }\n}\n\n// Check if we can place a color at position (x,y)\nbool can_place(int x, int y, int color) {\n    if (x < 0 || x >= N || y < 0 || y >= N) return false;\n    return result[x][y] == 0;\n}\n\n// Check if placement maintains adjacency requirements\nbool check_adjacency(int x, int y, int color) {\n    // Check if this placement creates required adjacencies\n    for (int d = 0; d < 4; d++) {\n        int nx = x + dx[d];\n        int ny = y + dy[d];\n        int neighbor = 0;\n        if (nx >= 0 && nx < N && ny >= 0 && ny < N) {\n            neighbor = result[nx][ny];\n        }\n        \n        // If neighbor is a color that should be adjacent, that's good\n        // If neighbor is a color that shouldn't be adjacent, that's bad (but we can't know all adjacencies yet)\n    }\n    return true;\n}\n\nvoid place_color_compactly(int color) {\n    if (color == 0) return; // Don't place color 0, it's empty space\n    \n    // If this color is already placed, skip\n    if (!positions[color].empty()) return;\n    \n    // Find a good starting position - near already placed colors that it should be adjacent to\n    vector<pair<int,int>> candidates;\n    \n    // If no colors placed yet, start in the middle\n    if (positions[1].empty()) {\n        int cx = N/2, cy = N/2;\n        result[cx][cy] = color;\n        positions[color].push_back({cx, cy});\n        return;\n    }\n    \n    // Try to place near adjacencies\n    bool placed = false;\n    for (int adj_color : adj_list[color]) {\n        if (!positions[adj_color].empty()) {\n            // Try to place near this adjacent color\n            for (auto& pos : positions[adj_color]) {\n                int x = pos.first, y = pos.second;\n                for (int d = 0; d < 4; d++) {\n                    int nx = x + dx[d];\n                    int ny = y + dy[d];\n                    if (nx >= 0 && nx < N && ny >= 0 && ny < N && result[nx][ny] == 0) {\n                        result[nx][ny] = color;\n                        positions[color].push_back({nx, ny});\n                        placed = true;\n                        break;\n                    }\n                }\n                if (placed) break;\n            }\n            if (placed) break;\n        }\n    }\n    \n    // If couldn't place near adjacencies, place anywhere available\n    if (!placed) {\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                if (result[i][j] == 0) {\n                    result[i][j] = color;\n                    positions[color].push_back({i, j});\n                    placed = true;\n                    break;\n                }\n            }\n            if (placed) break;\n        }\n    }\n    \n    // Expand the color to make it connected\n    if (!positions[color].empty()) {\n        // Simple expansion to make it connected - just place one tile\n        // In a more sophisticated solution, we'd expand to match the required size\n        // But since we're optimizing for fewer empty tiles, smaller is better\n    }\n}\n\n// Greedy approach: place colors in order of how many adjacencies they have\nvoid solve() {\n    // Initialize result grid\n    memset(result, 0, sizeof(result));\n    for (int i = 0; i <= M; i++) {\n        positions[i].clear();\n    }\n    \n    // Create ordering based on adjacency count (place high-degree nodes first)\n    vector<pair<int, int>> order; // {adjacency_count, color}\n    for (int i = 1; i <= M; i++) {\n        order.push_back({(int)adj_list[i].size(), i});\n    }\n    sort(order.rbegin(), order.rend()); // descending order\n    \n    // Place colors\n    for (auto& p : order) {\n        place_color_compactly(p.second);\n    }\n    \n    // Ensure connectivity for each color\n    for (int color = 1; color <= M; color++) {\n        if (positions[color].empty()) {\n            // Place at least one tile\n            for (int i = 0; i < N; i++) {\n                for (int j = 0; j < N; j++) {\n                    if (result[i][j] == 0) {\n                        result[i][j] = color;\n                        positions[color].push_back({i, j});\n                        goto placed;\n                    }\n                }\n            }\n            placed:;\n        }\n    }\n}\n\n// Better approach: create a compact layout\nvoid solve_better() {\n    // Initialize result grid\n    memset(result, 0, sizeof(result));\n    for (int i = 0; i <= M; i++) {\n        positions[i].clear();\n    }\n    \n    // Start by placing color 1 in the center\n    int center_x = N/2, center_y = N/2;\n    result[center_x][center_y] = 1;\n    positions[1].push_back({center_x, center_y});\n    \n    // Place other colors in a spiral pattern to minimize empty space\n    vector<pair<int,int>> spiral_dirs = {{0,1}, {1,0}, {0,-1}, {-1,0}}; // right, down, left, up\n    int dir = 0;\n    int x = center_x, y = center_y;\n    int steps = 1;\n    \n    vector<int> colors_to_place;\n    for (int i = 2; i <= M; i++) {\n        colors_to_place.push_back(i);\n    }\n    \n    int color_idx = 0;\n    while (color_idx < (int)colors_to_place.size()) {\n        for (int step = 0; step < 2; step++) {\n            for (int i = 0; i < steps && color_idx < (int)colors_to_place.size(); i++) {\n                x += spiral_dirs[dir].first;\n                y += spiral_dirs[dir].second;\n                \n                if (x >= 0 && x < N && y >= 0 && y < N && result[x][y] == 0) {\n                    int color = colors_to_place[color_idx];\n                    result[x][y] = color;\n                    positions[color].push_back({x, y});\n                    color_idx++;\n                }\n                \n                if (color_idx >= (int)colors_to_place.size()) break;\n            }\n            dir = (dir + 1) % 4;\n            if (color_idx >= (int)colors_to_place.size()) break;\n        }\n        steps++;\n    }\n    \n    // This approach doesn't preserve adjacencies, so let's try a different approach\n}\n\nvoid solve_best() {\n    // Initialize result grid\n    memset(result, 0, sizeof(result));\n    for (int i = 0; i <= M; i++) {\n        positions[i].clear();\n    }\n    \n    // Simple but effective: place colors in a dense grid pattern\n    int idx = 0;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (idx < M) {\n                int color = idx + 1;\n                result[i][j] = color;\n                positions[color].push_back({i, j});\n                idx++;\n            } else {\n                // Fill remaining with a pattern to maintain some adjacencies\n                result[i][j] = 0;\n            }\n        }\n    }\n    \n    // This still doesn't preserve the original adjacencies\n    // Let's try a more sophisticated approach\n}\n\n// Let's go with a simple but effective approach\nvoid solve_simple() {\n    // Initialize result grid\n    memset(result, 0, sizeof(result));\n    for (int i = 0; i <= M; i++) {\n        positions[i].clear();\n    }\n    \n    // Place colors in a way that tries to preserve adjacencies\n    // Place first color in center\n    result[N/2][N/2] = 1;\n    positions[1].push_back({N/2, N/2});\n    \n    // For remaining colors, place them near colors they should be adjacent to\n    vector<bool> placed(M+1, false);\n    placed[0] = true; // color 0 is special\n    placed[1] = true;\n    \n    queue<int> to_process;\n    to_process.push(1);\n    \n    while (!to_process.empty()) {\n        int current_color = to_process.front();\n        to_process.pop();\n        \n        // Try to place adjacent colors\n        for (int adj_color : adj_list[current_color]) {\n            if (!placed[adj_color] && adj_color != 0) {\n                // Try to place adj_color near current_color\n                bool placed_adj = false;\n                for (auto& pos : positions[current_color]) {\n                    int x = pos.first, y = pos.second;\n                    // Try 8 directions (including diagonals for more flexibility)\n                    for (int dx_val = -1; dx_val <= 1; dx_val++) {\n                        for (int dy_val = -1; dy_val <= 1; dy_val++) {\n                            if (dx_val == 0 && dy_val == 0) continue;\n                            int nx = x + dx_val;\n                            int ny = y + dy_val;\n                            if (nx >= 0 && nx < N && ny >= 0 && ny < N && result[nx][ny] == 0) {\n                                result[nx][ny] = adj_color;\n                                positions[adj_color].push_back({nx, ny});\n                                placed[adj_color] = true;\n                                to_process.push(adj_color);\n                                placed_adj = true;\n                                break;\n                            }\n                        }\n                        if (placed_adj) break;\n                    }\n                    if (placed_adj) break;\n                }\n                \n                // If we couldn't place it near an adjacent color, place it anywhere\n                if (!placed_adj) {\n                    for (int i = 0; i < N; i++) {\n                        for (int j = 0; j < N; j++) {\n                            if (result[i][j] == 0) {\n                                result[i][j] = adj_color;\n                                positions[adj_color].push_back({i, j});\n                                placed[adj_color] = true;\n                                to_process.push(adj_color);\n                                placed_adj = true;\n                                break;\n                            }\n                        }\n                        if (placed_adj) break;\n                    }\n                }\n            }\n        }\n    }\n    \n    // Place any remaining unplaced colors\n    for (int color = 1; color <= M; color++) {\n        if (!placed[color]) {\n            for (int i = 0; i < N; i++) {\n                for (int j = 0; j < N; j++) {\n                    if (result[i][j] == 0) {\n                        result[i][j] = color;\n                        positions[color].push_back({i, j});\n                        placed[color] = true;\n                        break;\n                    }\n                }\n                if (placed[color]) break;\n            }\n        }\n    }\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    int n, m;\n    cin >> n >> m;\n    \n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            cin >> grid[i][j];\n        }\n    }\n    \n    extract_adjacencies();\n    solve_simple();\n    \n    // Output result\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            if (j > 0) cout << \" \";\n            cout << result[i][j];\n        }\n        cout << \"\\n\";\n    }\n    \n    return 0;\n}","ahc025":"#include <bits/stdc++.h>\n#include <Eigen/Dense>\n\nusing namespace std;\n\n// Global variables\nint N, D, Q;\nvector<double> weight_estimates;\nvector<vector<pair<int, string>>> comparison_history;\n\n// Compare two individual items\npair<vector<int>, vector<int>> make_item_comparison(int item1, int item2) {\n    return {{item1}, {item2}};\n}\n\n// Compare groups of items\npair<vector<int>, vector<int>> make_group_comparison(const vector<int>& group1, const vector<int>& group2) {\n    return {group1, group2};\n}\n\nvoid solve() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    // Read input\n    cin >> N >> D >> Q;\n    \n    // Initialize data structures\n    weight_estimates.assign(N, 1.0);\n    comparison_history.resize(N);\n    \n    // Store all queries for processing\n    vector<tuple<vector<int>, vector<int>, string>> all_queries;\n    \n    // Strategy: Use first half of queries for exploration, second half for refinement\n    int exploration_queries = Q * 2 / 3;\n    \n    // Phase 1: Exploration - systematic pairwise comparisons\n    vector<pair<int, int>> item_pairs;\n    for (int i = 0; i < N; i++) {\n        for (int j = i + 1; j < N; j++) {\n            item_pairs.push_back({i, j});\n        }\n    }\n    \n    // Shuffle to randomize\n    random_shuffle(item_pairs.begin(), item_pairs.end());\n    \n    int queries_used = 0;\n    \n    // Use some queries for pairwise comparisons\n    for (int i = 0; i < min((int)item_pairs.size(), exploration_queries / 2) && queries_used < exploration_queries; i++) {\n        int item1 = item_pairs[i].first;\n        int item2 = item_pairs[i].second;\n        \n        cout << \"1 1 \" << item1 << \" \" << item2 << endl;\n        cout.flush();\n        \n        string response;\n        cin >> response;\n        \n        all_queries.push_back({{item1}, {item2}, response});\n        queries_used++;\n        \n        // Update simple weight estimates\n        if (response == \">\") {\n            weight_estimates[item1] *= 1.05;\n            weight_estimates[item2] /= 1.05;\n        } else if (response == \"<\") {\n            weight_estimates[item1] /= 1.05;\n            weight_estimates[item2] *= 1.05;\n        }\n    }\n    \n    // Phase 2: Group comparisons for better estimates\n    vector<vector<int>> groups_for_comparison;\n    int group_size = max(1, N / 8);\n    \n    // Create groups for comparison\n    for (int g = 0; g < min(8, N); g++) {\n        vector<int> group;\n        for (int i = 0; i < group_size && g * group_size + i < N; i++) {\n            group.push_back(g * group_size + i);\n        }\n        if (!group.empty()) {\n            groups_for_comparison.push_back(group);\n        }\n    }\n    \n    // Compare groups if we have queries left\n    for (int i = 0; i < (int)groups_for_comparison.size() && queries_used < exploration_queries; i++) {\n        for (int j = i + 1; j < (int)groups_for_comparison.size() && queries_used < exploration_queries; j++) {\n            if (groups_for_comparison[i].empty() || groups_for_comparison[j].empty()) continue;\n            \n            cout << groups_for_comparison[i].size() << \" \" << groups_for_comparison[j].size();\n            for (int item : groups_for_comparison[i]) cout << \" \" << item;\n            for (int item : groups_for_comparison[j]) cout << \" \" << item;\n            cout << endl;\n            cout.flush();\n            \n            string response;\n            cin >> response;\n            \n            all_queries.push_back({groups_for_comparison[i], groups_for_comparison[j], response});\n            queries_used++;\n            \n            break; // Just do one group comparison to save queries\n        }\n        break;\n    }\n    \n    // Phase 3: Refinement queries with remaining budget\n    while (queries_used < Q) {\n        // Find items with highest uncertainty or that are critical for partitioning\n        vector<pair<double, int>> sorted_by_weight;\n        for (int i = 0; i < N; i++) {\n            sorted_by_weight.push_back({weight_estimates[i], i});\n        }\n        sort(sorted_by_weight.begin(), sorted_by_weight.end());\n        \n        // Compare lightest vs heaviest\n        if (sorted_by_weight.size() >= 2) {\n            int lightest = sorted_by_weight[0].second;\n            int heaviest = sorted_by_weight.back().second;\n            \n            cout << \"1 1 \" << lightest << \" \" << heaviest << endl;\n            cout.flush();\n            \n            string response;\n            cin >> response;\n            \n            all_queries.push_back({{lightest}, {heaviest}, response});\n            queries_used++;\n            \n            // Update weights\n            if (response == \">\") {\n                weight_estimates[lightest] *= 1.1;\n            } else if (response == \"<\") {\n                weight_estimates[heaviest] *= 1.1;\n            }\n        } else {\n            // Fallback: compare random items\n            int item1 = rand() % N;\n            int item2 = rand() % N;\n            if (item1 != item2) {\n                cout << \"1 1 \" << item1 << \" \" << item2 << endl;\n                cout.flush();\n                \n                string response;\n                cin >> response;\n                \n                all_queries.push_back({{item1}, {item2}, response});\n                queries_used++;\n            } else {\n                queries_used++; // consume query even if we skip it\n            }\n        }\n    }\n    \n    // Refine weight estimates using all comparison history\n    // Simple approach: reinforce consistent patterns\n    for (const auto& [left, right, response] : all_queries) {\n        double left_weight = 0, right_weight = 0;\n        for (int item : left) left_weight += weight_estimates[item];\n        for (int item : right) right_weight += weight_estimates[item];\n        \n        double total = left_weight + right_weight;\n        if (total > 0) {\n            double left_ratio = left_weight / total;\n            double right_ratio = right_weight / total;\n            \n            if (response == \"=\") {\n                // Move weights closer together\n                double avg = (left_weight + right_weight) / 2.0;\n                if (left_weight > 0) {\n                    double factor = avg / left_weight;\n                    for (int item : left) weight_estimates[item] *= factor;\n                }\n                if (right_weight > 0) {\n                    double factor = avg / right_weight;\n                    for (int item : right) weight_estimates[item] *= factor;\n                }\n            } else if (response == \">\") {\n                // Amplify the difference slightly\n                if (left_weight > 0 && right_weight > 0) {\n                    double factor = 1.05;\n                    for (int item : left) weight_estimates[item] *= factor;\n                    for (int item : right) weight_estimates[item] /= factor;\n                }\n            } else { // response == \"<\"\n                if (left_weight > 0 && right_weight > 0) {\n                    double factor = 1.05;\n                    for (int item : left) weight_estimates[item] /= factor;\n                    for (int item : right) weight_estimates[item] *= factor;\n                }\n            }\n        }\n    }\n    \n    // Normalize weights\n    double sum_weights = 0;\n    for (double w : weight_estimates) sum_weights += w;\n    if (sum_weights > 0) {\n        for (double& w : weight_estimates) w = w * N / sum_weights;\n    }\n    \n    // Improved grouping using Karmarkar-Karp-like approach\n    vector<pair<double, int>> items_with_indices;\n    for (int i = 0; i < N; i++) {\n        items_with_indices.push_back({weight_estimates[i], i});\n    }\n    \n    // Sort by weight (descending)\n    sort(items_with_indices.begin(), items_with_indices.end(), greater<pair<double, int>>());\n    \n    // Use priority queue approach for balanced partitioning\n    vector<double> group_sums(D, 0);\n    vector<vector<int>> group_items(D);\n    vector<int> result(N);\n    \n    // Assign items using improved greedy method\n    for (const auto& [weight, idx] : items_with_indices) {\n        // Find the group with minimum current sum\n        int best_group = 0;\n        for (int g = 1; g < D; g++) {\n            if (group_sums[g] < group_sums[best_group]) {\n                best_group = g;\n            }\n        }\n        \n        group_items[best_group].push_back(idx);\n        group_sums[best_group] += weight;\n        result[idx] = best_group;\n    }\n    \n    // Output result\n    for (int i = 0; i < N; i++) {\n        cout << result[i];\n        if (i < N - 1) cout << \" \";\n    }\n    cout << endl;\n}\n\nint main() {\n    solve();\n    return 0;\n}","ahc026":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 200;\nconst int M = 10;\nconst int STACK_SIZE = 20;\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    int n, m;\n    cin >> n >> m;\n    \n    vector<deque<int>> stacks(M);\n    vector<int> box_stack(N + 1);      // which stack each box is in\n    vector<int> box_position(N + 1);   // position in stack (0 = bottom)\n    \n    // Read initial configuration\n    for (int i = 0; i < m; i++) {\n        for (int j = 0; j < STACK_SIZE; j++) {\n            int box;\n            cin >> box;\n            stacks[i].push_back(box);\n            box_stack[box] = i;\n            box_position[box] = j;\n        }\n    }\n    \n    vector<pair<int, int>> operations;\n    \n    // Process boxes in order from 1 to 200\n    for (int target = 1; target <= n; target++) {\n        int stack_idx = box_stack[target];\n        int pos_in_stack = box_position[target];\n        int stack_size = stacks[stack_idx].size();\n        \n        // If box is not at the top, we need to move boxes above it\n        if (pos_in_stack != stack_size - 1) {\n            // Determine which boxes need to be moved\n            int boxes_above = stack_size - 1 - pos_in_stack;\n            \n            // Find a destination stack (prefer one that's not full)\n            int dest_stack = -1;\n            for (int s = 0; s < m; s++) {\n                if (s != stack_idx && (int)stacks[s].size() < STACK_SIZE) {\n                    dest_stack = s;\n                    break;\n                }\n            }\n            \n            // If all stacks are full, pick any other stack\n            if (dest_stack == -1) {\n                dest_stack = (stack_idx + 1) % m;\n            }\n            \n            // Move operation: move box 'target' and all above it to dest_stack\n            operations.push_back({target, dest_stack + 1}); // 1-indexed output\n            \n            // Actually perform the move\n            vector<int> moved_boxes;\n            for (int i = pos_in_stack; i < stack_size; i++) {\n                moved_boxes.push_back(stacks[stack_idx][i]);\n            }\n            \n            // Remove from source stack\n            stacks[stack_idx].erase(\n                stacks[stack_idx].begin() + pos_in_stack, \n                stacks[stack_idx].end()\n            );\n            \n            // Add to destination stack\n            for (int box : moved_boxes) {\n                stacks[dest_stack].push_back(box);\n            }\n            \n            // Update positions for all moved boxes\n            for (int i = 0; i < (int)stacks[dest_stack].size(); i++) {\n                box_stack[stacks[dest_stack][i]] = dest_stack;\n                box_position[stacks[dest_stack][i]] = i;\n            }\n        }\n        \n        // Now the target box should be at the top of its stack\n        // Remove it\n        int final_stack = box_stack[target];\n        stacks[final_stack].pop_back();\n        box_stack[target] = -1;\n        operations.push_back({target, 0});\n        \n        // Update positions of remaining boxes in the stack\n        for (int i = 0; i < (int)stacks[final_stack].size(); i++) {\n            box_position[stacks[final_stack][i]] = i;\n        }\n    }\n    \n    // Output operations\n    for (const auto& op : operations) {\n        cout << op.first << \" \" << op.second << \"\\n\";\n    }\n    \n    return 0;\n}","ahc027":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MAXN = 45;\nint N;\nchar h[MAXN][MAXN], v[MAXN][MAXN];\nint d[MAXN][MAXN];\nbool visited[MAXN][MAXN];\nvector<pair<int, int>> tree_edges;\nvector<char> directions = {'R', 'D', 'L', 'U'};\nvector<pair<int, int>> moves = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};\n\n// Build spanning tree using BFS\nvoid build_spanning_tree() {\n    memset(visited, 0, sizeof(visited));\n    queue<pair<int, int>> q;\n    q.push({0, 0});\n    visited[0][0] = true;\n    \n    while (!q.empty()) {\n        auto [x, y] = q.front(); q.pop();\n        \n        for (int dir = 0; dir < 4; dir++) {\n            int nx = x + moves[dir].first;\n            int ny = y + moves[dir].second;\n            \n            if (nx >= 0 && nx < N && ny >= 0 && ny < N && !visited[nx][ny]) {\n                // Check if there's no wall\n                bool can_move = false;\n                if (dir == 0) { // Right\n                    if (ny > 0) can_move = (v[x][min(y, ny-1)] == '0');\n                    else can_move = true;\n                } else if (dir == 1) { // Down\n                    if (nx > 0) can_move = (h[min(x, nx-1)][y] == '0');\n                    else can_move = true;\n                } else if (dir == 2) { // Left\n                    if (y > 0) can_move = (v[x][min(y-1, ny)] == '0');\n                    else can_move = true;\n                } else { // Up\n                    if (x > 0) can_move = (h[min(x-1, nx)][y] == '0');\n                    else can_move = true;\n                }\n                \n                if (can_move || (nx >= 0 && nx < N && ny >= 0 && ny < N)) {\n                    if ((dir == 0 && (y+1 < N) && v[x][y] == '0') ||\n                        (dir == 1 && (x+1 < N) && h[x][y] == '0') ||\n                        (dir == 2 && (y > 0) && v[x][y-1] == '0') ||\n                        (dir == 3 && (x > 0) && h[x-1][y] == '0')) {\n                        visited[nx][ny] = true;\n                        tree_edges.push_back({x*N + y, nx*N + ny});\n                        q.push({nx, ny});\n                    }\n                }\n            }\n        }\n    }\n}\n\n// DFS to create Eulerian tour\nstring result = \"\";\nvoid dfs_tour(int x, int y, int parent_x, int parent_y) {\n    visited[x][y] = true;\n    \n    // Try all directions\n    for (int dir = 0; dir < 4; dir++) {\n        int nx = x + moves[dir].first;\n        int ny = y + moves[dir].second;\n        \n        // Check bounds\n        if (nx < 0 || nx >= N || ny < 0 || ny >= N) continue;\n        \n        // Check walls\n        bool can_move = false;\n        if (dir == 0) { // Right\n            can_move = (v[x][y] == '0');\n        } else if (dir == 1) { // Down\n            can_move = (h[x][y] == '0');\n        } else if (dir == 2) { // Left\n            if (y > 0) can_move = (v[x][y-1] == '0');\n        } else { // Up\n            if (x > 0) can_move = (h[x-1][y] == '0');\n        }\n        \n        if (!can_move) continue;\n        \n        // Skip parent to avoid going back immediately\n        if (nx == parent_x && ny == parent_y) continue;\n        \n        if (!visited[nx][ny]) {\n            result += directions[dir];\n            dfs_tour(nx, ny, x, y);\n            // Return move\n            if (dir == 0) result += 'L';\n            else if (dir == 1) result += 'U';\n            else if (dir == 2) result += 'R';\n            else result += 'D';\n        }\n    }\n}\n\n// Better approach: use BFS to build proper spanning tree\nvoid build_proper_spanning_tree() {\n    memset(visited, 0, sizeof(visited));\n    queue<tuple<int, int, int, int>> q; // x, y, parent_x, parent_y\n    q.push({0, 0, -1, -1});\n    visited[0][0] = true;\n    vector<tuple<int, int, int, int>> edges;\n    \n    while (!q.empty()) {\n        auto [x, y, px, py] = q.front(); q.pop();\n        \n        for (int dir = 0; dir < 4; dir++) {\n            int nx = x + moves[dir].first;\n            int ny = y + moves[dir].second;\n            \n            if (nx >= 0 && nx < N && ny >= 0 && ny < N && !visited[nx][ny]) {\n                // Check if move is possible\n                bool can_move = false;\n                if (dir == 0 && y+1 < N) can_move = (v[x][y] == '0');\n                else if (dir == 1 && x+1 < N) can_move = (h[x][y] == '0');\n                else if (dir == 2 && y > 0) can_move = (v[x][y-1] == '0');\n                else if (dir == 3 && x > 0) can_move = (h[x-1][y] == '0');\n                \n                if (can_move) {\n                    visited[nx][ny] = true;\n                    edges.push_back({x, y, nx, ny});\n                    q.push({nx, ny, x, y});\n                }\n            }\n        }\n    }\n    \n    // Now create tour from edges\n    memset(visited, 0, sizeof(visited));\n    result = \"\";\n    dfs_tour(0, 0, -1, -1);\n}\n\n// Even simpler approach: just do DFS on the grid\nstring simple_dfs() {\n    memset(visited, 0, sizeof(visited));\n    string path = \"\";\n    \n    function<void(int, int)> dfs = [&](int x, int y) {\n        visited[x][y] = true;\n        \n        // Try directions in order that prioritizes higher d values\n        vector<pair<int, int>> next_moves;\n        for (int dir = 0; dir < 4; dir++) {\n            int nx = x + moves[dir].first;\n            int ny = y + moves[dir].second;\n            \n            if (nx >= 0 && nx < N && ny >= 0 && ny < N && !visited[nx][ny]) {\n                // Check walls\n                bool can_move = false;\n                if (dir == 0 && y+1 < N) can_move = (v[x][y] == '0');\n                else if (dir == 1 && x+1 < N) can_move = (h[x][y] == '0');\n                else if (dir == 2 && y > 0) can_move = (v[x][y-1] == '0');\n                else if (dir == 3 && x > 0) can_move = (h[x-1][y] == '0');\n                \n                if (can_move) {\n                    next_moves.push_back({nx, ny});\n                }\n            }\n        }\n        \n        // Sort by dirtiness (descending)\n        sort(next_moves.begin(), next_moves.end(), [&](const pair<int,int>& a, const pair<int,int>& b) {\n            return d[a.first][a.second] > d[b.first][b.second];\n        });\n        \n        for (auto& move : next_moves) {\n            int nx = move.first, ny = move.second;\n            // Find direction\n            int dir = -1;\n            for (int i = 0; i < 4; i++) {\n                if (x + moves[i].first == nx && y + moves[i].second == ny) {\n                    dir = i;\n                    break;\n                }\n            }\n            \n            if (dir != -1) {\n                path += directions[dir];\n                dfs(nx, ny);\n                // Return move\n                if (dir == 0) path += 'L';\n                else if (dir == 1) path += 'U';\n                else if (dir == 2) path += 'R';\n                else path += 'D';\n            }\n        }\n    };\n    \n    dfs(0, 0);\n    return path;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    cin >> N;\n    \n    // Read horizontal walls\n    for (int i = 0; i < N-1; i++) {\n        string line;\n        cin >> line;\n        for (int j = 0; j < N; j++) {\n            h[i][j] = line[j];\n        }\n    }\n    \n    // Read vertical walls\n    for (int i = 0; i < N; i++) {\n        string line;\n        cin >> line;\n        for (int j = 0; j < N-1; j++) {\n            v[i][j] = line[j];\n        }\n    }\n    \n    // Read dirtiness values\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cin >> d[i][j];\n        }\n    }\n    \n    // Generate path using simple DFS approach\n    string result = simple_dfs();\n    \n    // Make sure we don't exceed limit\n    if (result.length() > 100000) {\n        result = result.substr(0, 100000);\n    }\n    \n    cout << result << endl;\n    \n    return 0;\n}","ahc028":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 15;\nconst int M = 200;\nconst int MAX_OPS = 5000;\n\nint grid_size;\nint target_count;\nint start_row, start_col;\nchar grid[N][N];\nvector<string> targets;\nvector<pair<int,int>> char_positions[26]; // positions for each character A-Z\n\n// Precompute positions for each character\nvoid precompute_positions() {\n    for (int i = 0; i < 26; i++) {\n        char_positions[i].clear();\n    }\n    \n    for (int i = 0; i < grid_size; i++) {\n        for (int j = 0; j < grid_size; j++) {\n            int char_idx = grid[i][j] - 'A';\n            char_positions[char_idx].push_back({i, j});\n        }\n    }\n}\n\n// Find the best position considering future characters\npair<int, int> find_best_position_with_lookahead(const string& suffix, int cur_row, int cur_col, int lookahead) {\n    char c = suffix[0];\n    int char_idx = c - 'A';\n    \n    if (lookahead == 0 || suffix.length() == 1) {\n        // Just find closest position\n        pair<int, int> best_pos = char_positions[char_idx][0];\n        int best_dist = abs(cur_row - best_pos.first) + abs(cur_col - best_pos.second);\n        \n        for (auto& pos : char_positions[char_idx]) {\n            int dist = abs(cur_row - pos.first) + abs(cur_col - pos.second);\n            if (dist < best_dist) {\n                best_dist = dist;\n                best_pos = pos;\n            }\n        }\n        return best_pos;\n    }\n    \n    // Look ahead to make better decision\n    pair<int, int> best_pos = char_positions[char_idx][0];\n    int best_score = 1e9;\n    \n    for (auto& pos : char_positions[char_idx]) {\n        int move_cost = abs(cur_row - pos.first) + abs(cur_col - pos.second) + 1;\n        \n        // Estimate future cost\n        int future_cost = 0;\n        if (suffix.length() > 1) {\n            char next_char = suffix[1];\n            int next_char_idx = next_char - 'A';\n            pair<int, int> next_best = char_positions[next_char_idx][0];\n            int next_dist = abs(pos.first - next_best.first) + abs(pos.second - next_best.second);\n            \n            for (auto& next_pos : char_positions[next_char_idx]) {\n                int dist = abs(pos.first - next_pos.first) + abs(pos.second - next_pos.second);\n                if (dist < next_dist) {\n                    next_dist = dist;\n                }\n            }\n            future_cost = next_dist + 1;\n        }\n        \n        int total_score = move_cost + future_cost / 2; // Weight future cost less\n        \n        if (total_score < best_score) {\n            best_score = total_score;\n            best_pos = pos;\n        }\n    }\n    \n    return best_pos;\n}\n\n// Build superstring using greedy concatenation with overlap\nstring build_superstring_greedy() {\n    if (targets.empty()) return \"\";\n    \n    vector<bool> used(targets.size(), false);\n    string result = targets[0];\n    used[0] = true;\n    \n    // Try to merge remaining strings with maximum overlap\n    for (int iter = 1; iter < (int)targets.size(); iter++) {\n        int best_overlap = -1;\n        int best_idx = -1;\n        bool append = true;\n        string best_new_string = \"\";\n        \n        // Find string with maximum overlap\n        for (int i = 0; i < (int)targets.size(); i++) {\n            if (used[i]) continue;\n            \n            const string& s = targets[i];\n            \n            // Check overlap when appending\n            for (int overlap = min(4, (int)result.length()); overlap >= 0; overlap--) {\n                if (result.length() >= overlap && \n                    result.substr(result.length() - overlap) == s.substr(0, overlap)) {\n                    string new_result = result + s.substr(overlap);\n                    if (overlap > best_overlap || (overlap == best_overlap && new_result.length() < best_new_string.length())) {\n                        best_overlap = overlap;\n                        best_idx = i;\n                        append = true;\n                        best_new_string = new_result;\n                    }\n                    break;\n                }\n            }\n        }\n        \n        if (best_idx != -1) {\n            result = best_new_string;\n            used[best_idx] = true;\n        } else {\n            // Find first unused string and append it\n            for (int i = 0; i < (int)targets.size(); i++) {\n                if (!used[i]) {\n                    result += targets[i];\n                    used[i] = true;\n                    break;\n                }\n            }\n        }\n    }\n    \n    return result;\n}\n\n// Alternative superstring using de Bruijn-like approach for small overlaps\nstring build_superstring_alternative() {\n    string result = \"\";\n    \n    // Simple concatenation with small overlaps where possible\n    for (int i = 0; i < (int)targets.size(); i++) {\n        if (result.empty()) {\n            result = targets[i];\n        } else {\n            // Try to find 1-2 character overlap\n            bool merged = false;\n            for (int overlap = min(2, min(4, (int)result.length())); overlap >= 1; overlap--) {\n                if (result.length() >= overlap && \n                    result.substr(result.length() - overlap) == targets[i].substr(0, overlap)) {\n                    result += targets[i].substr(overlap);\n                    merged = true;\n                    break;\n                }\n            }\n            if (!merged) {\n                result += targets[i];\n            }\n        }\n    }\n    \n    return result;\n}\n\n// Generate path with optimization\nvector<pair<int, int>> generate_optimized_path(const string& superstring) {\n    vector<pair<int, int>> path;\n    int cur_row = start_row;\n    int cur_col = start_col;\n    \n    // Process in chunks to optimize movement\n    for (int i = 0; i < (int)superstring.length(); i++) {\n        // Look ahead a few characters to make better decisions\n        string lookahead_str = superstring.substr(i, min(3, (int)superstring.length() - i));\n        pair<int, int> next_pos = find_best_position_with_lookahead(lookahead_str, cur_row, cur_col, \n                                                                  min(2, (int)lookahead_str.length() - 1));\n        \n        path.push_back(next_pos);\n        cur_row = next_pos.first;\n        cur_col = next_pos.second;\n    }\n    \n    return path;\n}\n\n// Remove redundant moves\nvector<pair<int, int>> optimize_path(const vector<pair<int, int>>& path) {\n    if (path.empty()) return path;\n    \n    vector<pair<int, int>> optimized;\n    optimized.push_back(path[0]);\n    \n    for (int i = 1; i < (int)path.size(); i++) {\n        // Only add if it's different from the last position\n        if (path[i] != optimized.back()) {\n            optimized.push_back(path[i]);\n        }\n    }\n    \n    return optimized;\n}\n\n// Try multiple strategies and pick the best\nvector<pair<int, int>> find_best_path() {\n    // Strategy 1: Greedy overlap\n    string superstring1 = build_superstring_greedy();\n    vector<pair<int, int>> path1 = generate_optimized_path(superstring1);\n    path1 = optimize_path(path1);\n    \n    // Strategy 2: Simple concatenation with small overlaps\n    string superstring2 = build_superstring_alternative();\n    vector<pair<int, int>> path2 = generate_optimized_path(superstring2);\n    path2 = optimize_path(path2);\n    \n    // Pick the shorter path, but not too short (need to cover all strings)\n    if (path1.size() <= path2.size() && path1.size() <= MAX_OPS) {\n        return path1;\n    } else if (path2.size() <= MAX_OPS) {\n        return path2;\n    } else {\n        return path1.size() <= MAX_OPS ? path1 : vector<pair<int, int>>(path1.begin(), path1.begin() + min(MAX_OPS, (int)path1.size()));\n    }\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    // Read input\n    cin >> grid_size >> target_count;\n    cin >> start_row >> start_col;\n    \n    for (int i = 0; i < grid_size; i++) {\n        for (int j = 0; j < grid_size; j++) {\n            cin >> grid[i][j];\n        }\n    }\n    \n    targets.resize(target_count);\n    for (int i = 0; i < target_count; i++) {\n        cin >> targets[i];\n    }\n    \n    // Precompute character positions\n    precompute_positions();\n    \n    // Find best path using multiple strategies\n    vector<pair<int, int>> path = find_best_path();\n    \n    // Ensure we don't exceed limit\n    if (path.size() > MAX_OPS) {\n        path.resize(MAX_OPS);\n    }\n    \n    // Output path\n    for (const auto& pos : path) {\n        cout << pos.first << \" \" << pos.second << \"\\n\";\n    }\n    \n    return 0;\n}","ahc030":"#include <bits/stdc++.h>\n#include <boost/random.hpp>\n#include <boost/random/normal_distribution.hpp>\n\nusing namespace std;\n\n// Random number generator\nboost::random::mt19937 gen;\n\nstruct Point {\n    int i, j;\n    Point() : i(0), j(0) {}\n    Point(int _i, int _j) : i(_i), j(_j) {}\n    bool operator<(const Point& other) const {\n        if (i != other.i) return i < other.i;\n        return j < other.j;\n    }\n    bool operator==(const Point& other) const {\n        return i == other.i && j == other.j;\n    }\n};\n\nstruct Polyomino {\n    vector<Point> points;\n    int width, height;\n    \n    Polyomino() : width(0), height(0) {}\n    \n    void normalize() {\n        if (points.empty()) return;\n        int min_i = points[0].i, min_j = points[0].j;\n        int max_i = points[0].i, max_j = points[0].j;\n        \n        for (const auto& p : points) {\n            min_i = min(min_i, p.i);\n            min_j = min(min_j, p.j);\n            max_i = max(max_i, p.i);\n            max_j = max(max_j, p.j);\n        }\n        \n        for (auto& p : points) {\n            p.i -= min_i;\n            p.j -= min_j;\n        }\n        \n        width = max_j - min_j + 1;\n        height = max_i - min_i + 1;\n    }\n};\n\nint N, M;\ndouble epsilon;\nvector<Polyomino> fields;\nvector<vector<double>> prob_map;\nvector<vector<bool>> drilled;\nvector<vector<int>> known_values;\nint operations_used = 0;\nconst int MAX_OPERATIONS = 2 * 20 * 20; // 2 * N^2, using max N=20\n\n// Function to query the system\nint query_single(int i, int j) {\n    cout << \"q 1 \" << i << \" \" << j << endl;\n    cout.flush();\n    operations_used++;\n    int response;\n    cin >> response;\n    return response;\n}\n\nint query_group(const vector<Point>& points) {\n    cout << \"q \" << points.size();\n    for (const auto& p : points) {\n        cout << \" \" << p.i << \" \" << p.j;\n    }\n    cout << endl;\n    cout.flush();\n    operations_used++;\n    int response;\n    cin >> response;\n    return response;\n}\n\nbool submit_answer(const set<Point>& oil_squares) {\n    cout << \"a \" << oil_squares.size();\n    for (const auto& p : oil_squares) {\n        cout << \" \" << p.i << \" \" << p.j;\n    }\n    cout << endl;\n    cout.flush();\n    operations_used++;\n    int response;\n    cin >> response;\n    return response == 1;\n}\n\n// Check if a polyomino can be placed at position (di, dj)\nbool can_place(const Polyomino& poly, int di, int dj) {\n    for (const auto& p : poly.points) {\n        int ni = p.i + di;\n        int nj = p.j + dj;\n        if (ni < 0 || ni >= N || nj < 0 || nj >= N) {\n            return false;\n        }\n    }\n    return true;\n}\n\n// Get all possible positions for a polyomino\nvector<pair<int, int>> get_possible_positions(const Polyomino& poly) {\n    vector<pair<int, int>> positions;\n    for (int i = 0; i <= N - poly.height; i++) {\n        for (int j = 0; j <= N - poly.width; j++) {\n            if (can_place(poly, i, j)) {\n                positions.push_back({i, j});\n            }\n        }\n    }\n    return positions;\n}\n\n// Update probability map based on a group query result\nvoid update_probabilities(const vector<Point>& group, int result) {\n    double group_size = group.size();\n    double expected = result;\n    \n    // Simple update - distribute the result evenly among group members\n    for (const auto& p : group) {\n        prob_map[p.i][p.j] += expected / group_size;\n    }\n}\n\n// Get unexplored points for group querying\nvector<Point> get_unexplored_group(int size) {\n    vector<Point> candidates;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (!drilled[i][j]) {\n                candidates.push_back(Point(i, j));\n            }\n        }\n    }\n    \n    // Shuffle and take first 'size' elements\n    shuffle(candidates.begin(), candidates.end(), gen);\n    \n    vector<Point> result;\n    for (int i = 0; i < min(size, (int)candidates.size()); i++) {\n        result.push_back(candidates[i]);\n    }\n    \n    return result;\n}\n\n// Get high probability points that haven't been drilled\nset<Point> get_high_prob_points(double threshold) {\n    set<Point> result;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (!drilled[i][j] && prob_map[i][j] > threshold) {\n                result.insert(Point(i, j));\n            }\n        }\n    }\n    return result;\n}\n\n// Get all points we know have oil\nset<Point> get_known_oil_points() {\n    set<Point> result;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (drilled[i][j] && known_values[i][j] > 0) {\n                result.insert(Point(i, j));\n            }\n        }\n    }\n    return result;\n}\n\nint main() {\n    gen.seed(time(0));\n    \n    // Read input\n    cin >> N >> M >> epsilon;\n    fields.resize(M);\n    \n    for (int i = 0; i < M; i++) {\n        int d;\n        cin >> d;\n        fields[i].points.resize(d);\n        for (int j = 0; j < d; j++) {\n            cin >> fields[i].points[j].i >> fields[i].points[j].j;\n        }\n        fields[i].normalize();\n    }\n    \n    // Initialize data structures\n    prob_map.assign(N, vector<double>(N, 0.0));\n    drilled.assign(N, vector<bool>(N, false));\n    known_values.assign(N, vector<int>(N, 0));\n    \n    // Initial exploration phase - use group queries\n    int group_queries = min(50, MAX_OPERATIONS / 4);\n    for (int q = 0; q < group_queries && operations_used < MAX_OPERATIONS - N*N/2; q++) {\n        int group_size = min(20, max(5, N*N/20)); // Adaptive group size\n        auto group = get_unexplored_group(group_size);\n        \n        if (group.size() >= 2) {\n            int result = query_group(group);\n            update_probabilities(group, result);\n        }\n    }\n    \n    // Drilling phase - drill high probability squares\n    vector<pair<double, Point>> prob_list;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (!drilled[i][j]) {\n                prob_list.push_back({prob_map[i][j], Point(i, j)});\n            }\n        }\n    }\n    \n    sort(prob_list.rbegin(), prob_list.rend()); // Sort by probability descending\n    \n    int drill_attempts = min(100, MAX_OPERATIONS - operations_used - N*N/4);\n    for (int i = 0; i < min(drill_attempts, (int)prob_list.size()); i++) {\n        Point p = prob_list[i].second;\n        int result = query_single(p.i, p.j);\n        drilled[p.i][p.j] = true;\n        known_values[p.i][p.j] = result;\n        \n        // Update probabilities around this point\n        for (int di = -2; di <= 2; di++) {\n            for (int dj = -2; dj <= 2; dj++) {\n                int ni = p.i + di;\n                int nj = p.j + dj;\n                if (ni >= 0 && ni < N && nj >= 0 && nj < N) {\n                    if (result > 0) {\n                        prob_map[ni][nj] += 1.0 / (1 + abs(di) + abs(dj)); // Proximity bonus\n                    } else {\n                        prob_map[ni][nj] *= 0.8; // Reduce probability if no oil found nearby\n                    }\n                }\n            }\n        }\n    }\n    \n    // Final phase - collect all points that likely have oil\n    set<Point> answer = get_known_oil_points();\n    \n    // Add high probability points\n    auto high_prob = get_high_prob_points(0.5);\n    answer.insert(high_prob.begin(), high_prob.end());\n    \n    // Make sure we're not exceeding operation limits\n    if (operations_used < MAX_OPERATIONS) {\n        bool success = submit_answer(answer);\n        \n        // If failed and still have operations, try again with different threshold\n        if (!success && operations_used < MAX_OPERATIONS - 1) {\n            answer = get_known_oil_points();\n            auto medium_prob = get_high_prob_points(0.3);\n            answer.insert(medium_prob.begin(), medium_prob.end());\n            submit_answer(answer);\n        }\n    }\n    \n    return 0;\n}","ahc031":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int W = 1000;\n\nstruct Rectangle {\n    int i0, j0, i1, j1;\n    Rectangle() : i0(0), j0(0), i1(0), j1(0) {}\n    Rectangle(int _i0, int _j0, int _i1, int _j1) : i0(_i0), j0(_j0), i1(_i1), j1(_j1) {}\n    int area() const { return (i1 - i0) * (j1 - j0); }\n    bool valid() const { return i0 >= 0 && j0 >= 0 && i1 <= W && j1 <= W && i0 < i1 && j0 < j1; }\n};\n\n// Calculate partition cost between two days\nint calculate_partition_cost(const vector<Rectangle>& prev, const vector<Rectangle>& curr) {\n    // Simplified: just count rectangle changes\n    // In practice, we'd need to track actual partition lines\n    int cost = 0;\n    for (size_t i = 0; i < min(prev.size(), curr.size()); ++i) {\n        if (prev[i].i0 != curr[i].i0 || prev[i].j0 != curr[i].j0 || \n            prev[i].i1 != curr[i].i1 || prev[i].j1 != curr[i].j1) {\n            cost += 10; // penalty for moving rectangles\n        }\n    }\n    return cost;\n}\n\n// Calculate area shortage cost\nint calculate_area_cost(const vector<int>& required, const vector<Rectangle>& rectangles) {\n    int cost = 0;\n    for (size_t i = 0; i < required.size(); ++i) {\n        int shortage = max(0, required[i] - rectangles[i].area());\n        cost += 100 * shortage;\n    }\n    return cost;\n}\n\n// Simple strip packing algorithm\nvector<Rectangle> pack_rectangles_simple(const vector<int>& required) {\n    vector<Rectangle> rectangles(required.size());\n    int current_row = 0;\n    \n    for (size_t i = 0; i < required.size(); ++i) {\n        int area_needed = required[i];\n        \n        // Try to make rectangle as square as possible\n        int width = min(W, (int)sqrt(area_needed) + 1);\n        while (width > 0 && area_needed % width != 0) width--;\n        if (width == 0) width = 1;\n        \n        int height = (area_needed + width - 1) / width;\n        \n        // Adjust to fit in grid\n        if (width > W) {\n            width = W;\n            height = (area_needed + W - 1) / W;\n        }\n        if (height > W) {\n            height = W;\n            width = (area_needed + W - 1) / W;\n        }\n        \n        // Position the rectangle\n        if (current_row + height <= W) {\n            rectangles[i] = Rectangle(current_row, 0, current_row + height, width);\n            current_row += height;\n        } else {\n            // If we run out of space, just place it somewhere\n            rectangles[i] = Rectangle(0, 0, min(height, W), min(width, W));\n        }\n    }\n    \n    return rectangles;\n}\n\n// Improved packing with better area satisfaction\nvector<Rectangle> pack_rectangles_improved(const vector<int>& required) {\n    vector<Rectangle> rectangles(required.size());\n    \n    // Sort rectangles by required area (descending) for better packing\n    vector<pair<int, int>> area_index;\n    for (size_t i = 0; i < required.size(); ++i) {\n        area_index.push_back({required[i], i});\n    }\n    sort(area_index.rbegin(), area_index.rend());\n    \n    // Use a simple grid-based approach\n    int grid_size = (int)sqrt(required.size()) + 1;\n    int cell_width = W / grid_size;\n    int cell_height = W / grid_size;\n    \n    vector<vector<bool>> occupied(grid_size, vector<bool>(grid_size, false));\n    \n    for (const auto& [area, idx] : area_index) {\n        int area_needed = area;\n        \n        // Try to make rectangle fit requirements\n        int width = min(W, (int)sqrt(area_needed) + 50);  // Add buffer\n        int height = (area_needed + width - 1) / width;\n        \n        if (width > W) {\n            width = W;\n            height = (area_needed + W - 1) / W + 1;\n        }\n        if (height > W) {\n            height = W;\n            width = (area_needed + W - 1) / W + 1;\n        }\n        \n        // Ensure minimum area\n        if (width * height < area_needed) {\n            int diff = area_needed - width * height;\n            width += (diff + height - 1) / height;\n            if (width > W) width = W;\n            height += (diff + width - 1) / width;\n            if (height > W) height = W;\n        }\n        \n        // Place rectangle\n        bool placed = false;\n        for (int x = 0; x <= W - width && !placed; ++x) {\n            for (int y = 0; y <= W - height && !placed; ++y) {\n                rectangles[idx] = Rectangle(x, y, x + height, y + width);\n                if (rectangles[idx].valid()) {\n                    placed = true;\n                    break;\n                }\n            }\n        }\n        \n        // Fallback if not placed properly\n        if (!placed || rectangles[idx].area() < area_needed) {\n            int side = max(1, (int)sqrt(area_needed));\n            rectangles[idx] = Rectangle(0, 0, min(side, W), min((area_needed + side - 1) / side, W));\n        }\n    }\n    \n    return rectangles;\n}\n\n// Optimize rectangles to reduce area shortage\nvector<Rectangle> optimize_rectangles(vector<Rectangle> rectangles, const vector<int>& required) {\n    for (size_t i = 0; i < rectangles.size(); ++i) {\n        int current_area = rectangles[i].area();\n        int required_area = required[i];\n        \n        if (current_area < required_area) {\n            // Expand rectangle to meet requirement\n            int shortage = required_area - current_area;\n            \n            int width = rectangles[i].j1 - rectangles[i].j0;\n            int height = rectangles[i].i1 - rectangles[i].i0;\n            \n            // Try to expand width first\n            if (rectangles[i].j1 < W) {\n                int expand_w = min(shortage / height + 1, W - rectangles[i].j1);\n                rectangles[i].j1 = min(W, rectangles[i].j1 + expand_w);\n            }\n            // Then try to expand height\n            else if (rectangles[i].i1 < W) {\n                int expand_h = min(shortage / width + 1, W - rectangles[i].i1);\n                rectangles[i].i1 = min(W, rectangles[i].i1 + expand_h);\n            }\n        }\n    }\n    return rectangles;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int W_input, D, N;\n    cin >> W_input >> D >> N;\n    \n    vector<vector<int>> required(D, vector<int>(N));\n    for (int d = 0; d < D; ++d) {\n        for (int k = 0; k < N; ++k) {\n            cin >> required[d][k];\n        }\n    }\n    \n    vector<vector<Rectangle>> solutions(D);\n    \n    // Day 0: Generate initial solution\n    solutions[0] = pack_rectangles_improved(required[0]);\n    solutions[0] = optimize_rectangles(solutions[0], required[0]);\n    \n    // Days 1 to D-1: Try to maintain consistency to reduce partition costs\n    for (int d = 1; d < D; ++d) {\n        // Start with previous day's solution\n        solutions[d] = solutions[d-1];\n        \n        // Adjust to meet new requirements\n        solutions[d] = optimize_rectangles(solutions[d], required[d]);\n        \n        // If still not good enough, generate fresh solution\n        bool need_fresh = false;\n        for (int k = 0; k < N; ++k) {\n            if (solutions[d][k].area() < required[d][k]) {\n                need_fresh = true;\n                break;\n            }\n        }\n        \n        if (need_fresh) {\n            solutions[d] = pack_rectangles_improved(required[d]);\n            solutions[d] = optimize_rectangles(solutions[d], required[d]);\n        }\n    }\n    \n    // Output results\n    for (int d = 0; d < D; ++d) {\n        for (int k = 0; k < N; ++k) {\n            cout << solutions[d][k].i0 << \" \" << solutions[d][k].j0 << \" \" \n                 << solutions[d][k].i1 << \" \" << solutions[d][k].j1 << \"\\n\";\n        }\n    }\n    \n    return 0;\n}","ahc032":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 9;\nconst int M = 20;\nconst int K = 81;\nconst long long MOD = 998244353;\n\nlong long a[N][N];\nlong long s[M][3][3];\n\nstruct Operation {\n    int stamp, p, q;\n};\n\n// Calculate current score of board\nlong long calculate_score(long long board[N][N]) {\n    long long score = 0;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            score = (score + (board[i][j] % MOD + MOD) % MOD) % MOD;\n        }\n    }\n    return score;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    // Read input\n    int dummy_N, dummy_M, dummy_K;\n    cin >> dummy_N >> dummy_M >> dummy_K;\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    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 board\n    long long board[N][N];\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            board[i][j] = a[i][j];\n        }\n    }\n    \n    vector<Operation> operations;\n    \n    // Greedy selection\n    for (int k = 0; k < K; k++) {\n        Operation best_op = {-1, -1, -1};\n        long long best_gain = -1;\n        long long current_score = calculate_score(board);\n        \n        // Try all possible stamp placements\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                    // Create temporary board\n                    long long temp_board[N][N];\n                    for (int i = 0; i < N; i++) {\n                        for (int j = 0; j < N; j++) {\n                            temp_board[i][j] = board[i][j];\n                        }\n                    }\n                    \n                    // Apply stamp\n                    for (int i = 0; i < 3; i++) {\n                        for (int j = 0; j < 3; j++) {\n                            temp_board[p + i][q + j] += s[m][i][j];\n                        }\n                    }\n                    \n                    long long new_score = calculate_score(temp_board);\n                    long long gain = new_score - current_score;\n                    \n                    if (gain > best_gain) {\n                        best_gain = gain;\n                        best_op = {m, p, q};\n                    }\n                }\n            }\n        }\n        \n        // If no positive gain, stop\n        if (best_gain <= 0) {\n            break;\n        }\n        \n        // Apply the best operation\n        operations.push_back(best_op);\n        \n        for (int i = 0; i < 3; i++) {\n            for (int j = 0; j < 3; j++) {\n                board[best_op.p + i][best_op.q + j] += s[best_op.stamp][i][j];\n            }\n        }\n    }\n    \n    // Output\n    cout << operations.size() << \"\\n\";\n    for (const auto& op : operations) {\n        cout << op.stamp << \" \" << op.p << \" \" << op.q << \"\\n\";\n    }\n    \n    return 0;\n}","ahc033":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 5;\nconst int MAX_TURNS = 10000;\n\n// Container information\nint A[N][N];\nint container_to_gate[N*N]; // which gate each container should go to\n\n// Crane positions and states\nstruct Crane {\n    int row, col;\n    bool holding;\n    int container;\n    bool bombed;\n};\n\n// Grid state\nint grid[N][N]; // -1 = empty, >= 0 = container number\nCrane cranes[N];\n\n// Output actions\nvector<string> actions(N);\n\n// Initialize problem\nvoid init() {\n    // Calculate which gate each container should go to\n    for (int i = 0; i < N*N; i++) {\n        container_to_gate[i] = i / N;\n    }\n    \n    // Initialize grid\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            grid[i][j] = -1;\n        }\n    }\n    \n    // Initialize cranes\n    for (int i = 0; i < N; i++) {\n        cranes[i] = {i, 0, false, -1, false};\n    }\n}\n\n// Check if position is valid\nbool is_valid(int r, int c) {\n    return r >= 0 && r < N && c >= 0 && c < N;\n}\n\n// Main simulation logic\nvoid simulate() {\n    int turn = 0;\n    int received[N] = {0}; // Count of containers received by each gate\n    bool dispatched[N*N] = {false}; // Track which containers have been dispatched\n    \n    // Place initial containers\n    for (int i = 0; i < N; i++) {\n        if (received[i] < N) {\n            grid[i][0] = A[i][received[i]];\n            received[i]++;\n        }\n    }\n    \n    while (turn < MAX_TURNS) {\n        // Check if all containers are dispatched\n        bool all_done = true;\n        for (int i = 0; i < N*N; i++) {\n            if (!dispatched[i]) {\n                all_done = false;\n                break;\n            }\n        }\n        if (all_done) break;\n        \n        // Step 1: Place new containers at receiving gates\n        for (int i = 0; i < N; i++) {\n            if (received[i] < N && grid[i][0] == -1) {\n                grid[i][0] = A[i][received[i]];\n                received[i]++;\n            }\n        }\n        \n        // Step 2: Plan crane actions\n        vector<char> turn_actions(N, '.');\n        \n        // First, handle containers that are ready to be dispatched\n        for (int gate = 0; gate < N; gate++) {\n            if (grid[gate][N-1] != -1) {\n                int container = grid[gate][N-1];\n                if (container / N == gate && !dispatched[container]) {\n                    // Correct container at correct gate - dispatch it\n                    dispatched[container] = true;\n                    grid[gate][N-1] = -1;\n                }\n            }\n        }\n        \n        // Plan crane movements (simplified strategy)\n        for (int i = 0; i < N; i++) {\n            if (cranes[i].bombed) {\n                turn_actions[i] = '.';\n                continue;\n            }\n            \n            Crane& crane = cranes[i];\n            \n            // If at dispatch gate with correct container, dispatch it\n            if (crane.col == N-1 && grid[crane.row][crane.col] != -1) {\n                int container = grid[crane.row][crane.col];\n                if (container / N == crane.row && !dispatched[container]) {\n                    grid[crane.row][crane.col] = -1;\n                    dispatched[container] = true;\n                    continue;\n                }\n            }\n            \n            // Simple strategy: move right when possible\n            if (crane.col < N-1) {\n                // Try to move right\n                if (grid[crane.row][crane.col+1] == -1 || i == 0) { // Large crane can move through occupied\n                    turn_actions[i] = 'R';\n                    crane.col++;\n                } else {\n                    // Try to pick up container if at receiving gate or wrong position\n                    if (grid[crane.row][crane.col] != -1) {\n                        int container = grid[crane.row][crane.col];\n                        if (!crane.holding) {\n                            turn_actions[i] = 'P';\n                            crane.holding = true;\n                            crane.container = container;\n                            grid[crane.row][crane.col] = -1;\n                        } else {\n                            turn_actions[i] = '.';\n                        }\n                    } else {\n                        turn_actions[i] = '.';\n                    }\n                }\n            } else {\n                // At dispatch gate\n                if (grid[crane.row][crane.col] != -1) {\n                    int container = grid[crane.row][crane.col];\n                    if (container / N == crane.row && !dispatched[container]) {\n                        // Dispatch container\n                        grid[crane.row][crane.col] = -1;\n                        dispatched[container] = true;\n                    } else if (!crane.holding) {\n                        // Pick up wrong container to move it\n                        turn_actions[i] = 'P';\n                        crane.holding = true;\n                        crane.container = container;\n                        grid[crane.row][crane.col] = -1;\n                    } else {\n                        turn_actions[i] = '.';\n                    }\n                } else if (crane.holding) {\n                    // Place container\n                    int target_gate = crane.container / N;\n                    if (target_gate == crane.row) {\n                        turn_actions[i] = 'Q';\n                        crane.holding = false;\n                        grid[crane.row][crane.col] = crane.container;\n                        if (target_gate == crane.row) {\n                            dispatched[crane.container] = true;\n                        }\n                        crane.container = -1;\n                    } else {\n                        // Move to correct row\n                        if (crane.row < target_gate && (grid[crane.row+1][crane.col] == -1 || i == 0)) {\n                            turn_actions[i] = 'D';\n                            crane.row++;\n                        } else if (crane.row > target_gate && (grid[crane.row-1][crane.col] == -1 || i == 0)) {\n                            turn_actions[i] = 'U';\n                            crane.row--;\n                        } else {\n                            turn_actions[i] = '.';\n                        }\n                    }\n                } else {\n                    turn_actions[i] = '.';\n                }\n            }\n        }\n        \n        // Apply actions\n        for (int i = 0; i < N; i++) {\n            if (turn_actions[i] == 'R' && cranes[i].col < N-1) {\n                if (grid[cranes[i].row][cranes[i].col+1] == -1 || i == 0) {\n                    // Valid move\n                } else {\n                    turn_actions[i] = '.';\n                }\n            }\n            actions[i] += turn_actions[i];\n        }\n        \n        // Update grid based on movements and actions\n        for (int i = 0; i < N; i++) {\n            if (turn_actions[i] == 'P' && !cranes[i].holding) {\n                if (grid[cranes[i].row][cranes[i].col] != -1) {\n                    cranes[i].holding = true;\n                    cranes[i].container = grid[cranes[i].row][cranes[i].col];\n                    grid[cranes[i].row][cranes[i].col] = -1;\n                }\n            } else if (turn_actions[i] == 'Q' && cranes[i].holding) {\n                if (grid[cranes[i].row][cranes[i].col] == -1) {\n                    grid[cranes[i].row][cranes[i].col] = cranes[i].container;\n                    cranes[i].holding = false;\n                    cranes[i].container = -1;\n                }\n            }\n        }\n        \n        turn++;\n    }\n    \n    // Pad all action strings to same length\n    int max_len = 0;\n    for (int i = 0; i < N; i++) {\n        max_len = max(max_len, (int)actions[i].length());\n    }\n    \n    for (int i = 0; i < N; i++) {\n        while ((int)actions[i].length() < max_len) {\n            actions[i] += '.';\n        }\n    }\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    // Read input (N is fixed to 5)\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\n    init();\n    \n    // Run simulation\n    simulate();\n    \n    // Output results\n    for (int i = 0; i < N; i++) {\n        cout << actions[i] << \"\\n\";\n    }\n    \n    return 0;\n}","ahc034":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int GRID_SIZE = 20;\nint grid[GRID_SIZE][GRID_SIZE];\n\n// Directions: U, D, L, R\nconst int dx[4] = {-1, 1, 0, 0};\nconst int dy[4] = {0, 0, -1, 1};\nconst char dir_chars[4] = {'U', 'D', 'L', 'R'};\n\nstruct Position {\n    int x, y;\n    Position() : x(0), y(0) {}\n    Position(int _x, int _y) : x(_x), y(_y) {}\n};\n\nstruct TransportTask {\n    Position source;\n    Position target;\n    int amount;\n};\n\n// BFS to find shortest path between two points\nvector<pair<int, int>> find_shortest_path(Position start, Position end) {\n    if (start.x == end.x && start.y == end.y) return {};\n    \n    bool visited[GRID_SIZE][GRID_SIZE];\n    pair<int, int> parent[GRID_SIZE][GRID_SIZE];\n    memset(visited, 0, sizeof(visited));\n    \n    queue<Position> q;\n    q.push(start);\n    visited[start.x][start.y] = true;\n    parent[start.x][start.y] = {-1, -1};\n    \n    while (!q.empty()) {\n        Position pos = q.front(); q.pop();\n        \n        for (int i = 0; i < 4; i++) {\n            int nx = pos.x + dx[i];\n            int ny = pos.y + dy[i];\n            \n            if (nx >= 0 && nx < GRID_SIZE && ny >= 0 && ny < GRID_SIZE && !visited[nx][ny]) {\n                visited[nx][ny] = true;\n                parent[nx][ny] = {pos.x, pos.y};\n                \n                if (nx == end.x && ny == end.y) {\n                    // Reconstruct path\n                    vector<pair<int, int>> path;\n                    int cx = nx, cy = ny;\n                    while (parent[cx][cy].first != -1) {\n                        path.push_back({cx, cy});\n                        auto p = parent[cx][cy];\n                        cx = p.first;\n                        cy = p.second;\n                    }\n                    reverse(path.begin(), path.end());\n                    return path;\n                }\n                q.push(Position(nx, ny));\n            }\n        }\n    }\n    \n    return {}; // Should not happen in a connected grid\n}\n\n// Convert path to movement directions\nvector<char> path_to_directions(Position start, const vector<pair<int, int>>& path) {\n    vector<char> directions;\n    int cx = start.x, cy = start.y;\n    \n    for (auto [nx, ny] : path) {\n        if (nx == cx - 1 && ny == cy) directions.push_back('U');\n        else if (nx == cx + 1 && ny == cy) directions.push_back('D');\n        else if (nx == cx && ny == cy - 1) directions.push_back('L');\n        else if (nx == cx && ny == cy + 1) directions.push_back('R');\n        cx = nx;\n        cy = ny;\n    }\n    \n    return directions;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    int N = 20; // Fixed according to problem statement\n    \n    // Read grid\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cin >> grid[i][j];\n        }\n    }\n    \n    // Collect sources (positive) and sinks (negative)\n    vector<pair<Position, int>> sources, sinks;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (grid[i][j] > 0) {\n                sources.push_back({Position(i, j), grid[i][j]});\n            } else if (grid[i][j] < 0) {\n                sinks.push_back({Position(i, j), -grid[i][j]});\n            }\n        }\n    }\n    \n    vector<string> operations;\n    Position current_pos(0, 0);\n    int load = 0;\n    \n    // Simple greedy approach: process sources and sinks in order\n    int source_idx = 0, sink_idx = 0;\n    \n    while (source_idx < sources.size() && sink_idx < sinks.size()) {\n        auto& source = sources[source_idx];\n        auto& sink = sinks[sink_idx];\n        \n        // Determine amount to transport (minimum of source availability and sink demand)\n        int transport_amount = min(source.second, sink.second);\n        \n        // Move to source if needed\n        if (current_pos.x != source.first.x || current_pos.y != source.first.y) {\n            auto path = find_shortest_path(current_pos, source.first);\n            auto directions = path_to_directions(current_pos, path);\n            \n            for (char dir : directions) {\n                operations.push_back(string(1, dir));\n            }\n            current_pos = source.first;\n        }\n        \n        // Load soil\n        operations.push_back(\"+\" + to_string(transport_amount));\n        load += transport_amount;\n        source.second -= transport_amount;\n        \n        // Move to sink\n        if (current_pos.x != sink.first.x || current_pos.y != sink.first.y) {\n            auto path = find_shortest_path(current_pos, sink.first);\n            auto directions = path_to_directions(current_pos, path);\n            \n            for (char dir : directions) {\n                operations.push_back(string(1, dir));\n            }\n            current_pos = sink.first;\n        }\n        \n        // Unload soil\n        operations.push_back(\"-\" + to_string(transport_amount));\n        load -= transport_amount;\n        sink.second -= transport_amount;\n        \n        // Update indices\n        if (source.second == 0) source_idx++;\n        if (sink.second == 0) sink_idx++;\n    }\n    \n    // Output operations\n    for (const string& op : operations) {\n        cout << op << \"\\n\";\n    }\n    \n    return 0;\n}","ahc035":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <numeric>\n#include <cmath>\n\nusing namespace std;\n\nconst int N = 6;\nconst int SEED_COUNT = 2 * N * (N - 1); // 60 seeds\n\n// Calculate total value of a seed\nint calculateValue(const vector<int>& seed) {\n    return accumulate(seed.begin(), seed.end(), 0);\n}\n\n// Calculate similarity between two seeds (lower = more complementary)\nint calculateSimilarity(const vector<int>& seed1, const vector<int>& seed2) {\n    int similarity = 0;\n    for (int i = 0; i < seed1.size(); i++) {\n        similarity += abs(seed1[i] - seed2[i]);\n    }\n    return similarity;\n}\n\n// Find pairs of seeds that are complementary (high total but different distributions)\nvector<pair<int, int>> findComplementaryPairs(const vector<vector<int>>& seeds) {\n    vector<pair<int, int>> pairs;\n    vector<pair<int, int>> seedIndices;\n    \n    // Get top seeds by value\n    for (int i = 0; i < seeds.size(); i++) {\n        seedIndices.push_back({calculateValue(seeds[i]), i});\n    }\n    sort(seedIndices.begin(), seedIndices.end(), greater<pair<int, int>>());\n    \n    // Take top 30 seeds for pairing\n    int pairCount = min(30, (int)seedIndices.size());\n    for (int i = 0; i < pairCount; i++) {\n        int idx1 = seedIndices[i].second;\n        int bestComplement = -1;\n        int bestScore = -1;\n        \n        // Find the most complementary seed among top seeds\n        for (int j = 0; j < pairCount; j++) {\n            if (i == j) continue;\n            int idx2 = seedIndices[j].second;\n            \n            // Score based on total value and complementarity\n            int totalValue = calculateValue(seeds[idx1]) + calculateValue(seeds[idx2]);\n            int similarity = calculateSimilarity(seeds[idx1], seeds[idx2]);\n            int score = totalValue + (1000 - similarity); // Prefer dissimilar seeds\n            \n            if (score > bestScore) {\n                bestScore = score;\n                bestComplement = idx2;\n            }\n        }\n        \n        if (bestComplement != -1) {\n            pairs.push_back({idx1, bestComplement});\n        }\n    }\n    \n    return pairs;\n}\n\n// Generate placement strategy focusing on complementary pairing\nvector<vector<int>> generatePlacement(const vector<vector<int>>& seeds) {\n    vector<vector<int>> placement(N, vector<int>(N));\n    vector<bool> used(SEED_COUNT, false);\n    \n    // Priority positions for pairing (positions that are adjacent)\n    vector<vector<pair<int, int>>> positionGroups = {\n        // Horizontal pairs\n        {{0,0}, {0,1}}, {{0,1}, {0,2}}, {{0,2}, {0,3}}, {{0,3}, {0,4}}, {{0,4}, {0,5}},\n        {{1,0}, {1,1}}, {{1,1}, {1,2}}, {{1,2}, {1,3}}, {{1,3}, {1,4}}, {{1,4}, {1,5}},\n        {{2,0}, {2,1}}, {{2,1}, {2,2}}, {{2,2}, {2,3}}, {{2,3}, {2,4}}, {{2,4}, {2,5}},\n        {{3,0}, {3,1}}, {{3,1}, {3,2}}, {{3,2}, {3,3}}, {{3,3}, {3,4}}, {{3,4}, {3,5}},\n        {{4,0}, {4,1}}, {{4,1}, {4,2}}, {{4,2}, {4,3}}, {{4,3}, {4,4}}, {{4,4}, {4,5}},\n        {{5,0}, {5,1}}, {{5,1}, {5,2}}, {{5,2}, {5,3}}, {{5,3}, {5,4}}, {{5,4}, {5,5}},\n        // Vertical pairs\n        {{0,0}, {1,0}}, {{1,0}, {2,0}}, {{2,0}, {3,0}}, {{3,0}, {4,0}}, {{4,0}, {5,0}},\n        {{0,1}, {1,1}}, {{1,1}, {2,1}}, {{2,1}, {3,1}}, {{3,1}, {4,1}}, {{4,1}, {5,1}},\n        {{0,2}, {1,2}}, {{1,2}, {2,2}}, {{2,2}, {3,2}}, {{3,2}, {4,2}}, {{4,2}, {5,2}},\n        {{0,3}, {1,3}}, {{1,3}, {2,3}}, {{2,3}, {3,3}}, {{3,3}, {4,3}}, {{4,3}, {5,3}},\n        {{0,4}, {1,4}}, {{1,4}, {2,4}}, {{2,4}, {3,4}}, {{3,4}, {4,4}}, {{4,4}, {5,4}},\n        {{0,5}, {1,5}}, {{1,5}, {2,5}}, {{2,5}, {3,5}}, {{3,5}, {4,5}}, {{4,5}, {5,5}}\n    };\n    \n    // Find complementary pairs\n    auto pairs = findComplementaryPairs(seeds);\n    \n    // Place complementary pairs in adjacent positions\n    vector<pair<int, int>> placedPositions(N * N);\n    int posIdx = 0;\n    \n    // Fill the placement grid\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            placedPositions[posIdx++] = {i, j};\n        }\n    }\n    \n    // Place the best seeds first in strategic positions\n    vector<pair<int, int>> seedValues;\n    for (int i = 0; i < seeds.size(); i++) {\n        seedValues.push_back({calculateValue(seeds[i]), i});\n    }\n    sort(seedValues.begin(), seedValues.end(), greater<pair<int, int>>());\n    \n    // Place top seeds in center and well-connected positions\n    vector<pair<int, int>> priorityPositions = {\n        {2,2}, {2,3}, {3,2}, {3,3},  // center\n        {1,2}, {1,3}, {2,1}, {2,4}, {3,1}, {3,4}, {4,2}, {4,3},  // near center\n        {0,2}, {0,3}, {2,0}, {3,0}, {2,5}, {3,5}, {5,2}, {5,3},  // edges\n        {1,1}, {1,4}, {4,1}, {4,4},  // diagonals\n        {0,1}, {0,4}, {1,0}, {1,5}, {4,0}, {4,5}, {5,1}, {5,4},  // edge corners\n        {0,0}, {0,5}, {5,0}, {5,5}   // corners\n    };\n    \n    // Initialize placement\n    vector<bool> seedUsed(SEED_COUNT, false);\n    \n    // Place top seeds in priority positions\n    for (int i = 0; i < min((int)priorityPositions.size(), SEED_COUNT); i++) {\n        if (i >= seedValues.size()) break;\n        int seedIdx = seedValues[i].second;\n        auto pos = priorityPositions[i];\n        if (!seedUsed[seedIdx]) {\n            placement[pos.first][pos.second] = seedIdx;\n            seedUsed[seedIdx] = true;\n        }\n    }\n    \n    // Fill remaining positions\n    int seedIdx = 0;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (seedUsed[placement[i][j]]) continue; // Already placed\n            \n            // Find next unused seed\n            while (seedIdx < SEED_COUNT && seedUsed[seedIdx]) seedIdx++;\n            if (seedIdx < SEED_COUNT) {\n                placement[i][j] = seedIdx;\n                seedUsed[seedIdx] = true;\n                seedIdx++;\n            }\n        }\n    }\n    \n    // Final validation to ensure all positions have valid, unique seeds\n    vector<bool> positionUsed(SEED_COUNT, false);\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (placement[i][j] >= 0 && placement[i][j] < SEED_COUNT) {\n                positionUsed[placement[i][j]] = true;\n            }\n        }\n    }\n    \n    return placement;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    int N_input, M, T;\n    cin >> N_input >> M >> T;\n    \n    vector<vector<int>> seeds(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 >> seeds[i][j];\n        }\n    }\n    \n    // Process T turns\n    for (int turn = 0; turn < T; turn++) {\n        // Generate placement\n        auto placement = generatePlacement(seeds);\n        \n        // Output placement\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                cout << placement[i][j];\n                if (j < N - 1) cout << \" \";\n            }\n            cout << endl;\n        }\n        cout.flush();\n        \n        // Read new seeds for next iteration\n        if (turn < T - 1) {\n            for (int i = 0; i < SEED_COUNT; i++) {\n                for (int j = 0; j < M; j++) {\n                    cin >> seeds[i][j];\n                }\n            }\n        }\n    }\n    \n    return 0;\n}","ahc038":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MAX_N = 30;\nconst int MAX_V = 15;\nconst int INF = 1e9;\n\nstruct Point {\n    int x, y;\n    Point() : x(0), y(0) {}\n    Point(int x, int y) : x(x), y(y) {}\n    Point operator+(const Point& p) const { return Point(x + p.x, y + p.y); }\n    Point operator-(const Point& p) const { return Point(x - p.x, y - p.y); }\n    bool operator==(const Point& p) const { return x == p.x && y == p.y; }\n    bool operator!=(const Point& p) const { return !(*this == p); }\n};\n\nconst Point DIRS[4] = {Point(0, 1), Point(1, 0), Point(0, -1), Point(-1, 0)}; // R, D, L, U\nconst char DIR_CHARS[4] = {'R', 'D', 'L', 'U'};\n\nclass RobotArm {\npublic:\n    int V;\n    vector<pair<int, int>> tree; // {parent, length}\n    Point root_pos;\n    \n    RobotArm(int v) : V(v) {\n        tree.resize(V);\n    }\n};\n\nclass Solution {\nprivate:\n    int N, M, V;\n    vector<vector<int>> initial, target;\n    vector<Point> initial_pos, target_pos;\n    \npublic:\n    void readInput() {\n        cin >> N >> M >> V;\n        initial.assign(N, vector<int>(N, 0));\n        target.assign(N, vector<int>(N, 0));\n        \n        for (int i = 0; i < N; i++) {\n            string line;\n            cin >> line;\n            for (int j = 0; j < N; j++) {\n                initial[i][j] = line[j] - '0';\n                if (initial[i][j]) {\n                    initial_pos.push_back(Point(i, j));\n                }\n            }\n        }\n        \n        for (int i = 0; i < N; i++) {\n            string line;\n            cin >> line;\n            for (int j = 0; j < N; j++) {\n                target[i][j] = line[j] - '0';\n                if (target[i][j]) {\n                    target_pos.push_back(Point(i, j));\n                }\n            }\n        }\n    }\n    \n    RobotArm designArm() {\n        RobotArm arm(V);\n        \n        // Create a star-like structure with root at center\n        // Root is vertex 0, others are connected directly to root\n        for (int i = 1; i < V; i++) {\n            arm.tree[i] = {0, min(3, N-1)}; // Parent is 0, length 3\n        }\n        \n        // Initial position at center of grid\n        arm.root_pos = Point(N/2, N/2);\n        \n        return arm;\n    }\n    \n    string createMoveString(char move, const vector<char>& rotations, const vector<char>& actions) {\n        string result;\n        result += move;\n        for (int i = 1; i < rotations.size(); i++) {\n            result += rotations[i];\n        }\n        for (int i = 0; i < actions.size(); i++) {\n            result += actions[i];\n        }\n        return result;\n    }\n    \n    vector<string> solve(RobotArm& arm) {\n        vector<string> operations;\n        Point current_pos = arm.root_pos;\n        \n        // Simple greedy approach - move towards takoyaki and grab them\n        int turns = 0;\n        int max_turns = min(1000, (int)(1e5));\n        \n        // Track which takoyaki are taken and which targets are filled\n        vector<bool> taken(initial_pos.size(), false);\n        vector<bool> filled(target_pos.size(), false);\n        \n        // Directions for each finger (assuming 4 fingers pointing in different directions)\n        vector<Point> finger_dirs = {Point(0, 1), Point(1, 0), Point(0, -1), Point(-1, 0)};\n        \n        // Extend finger directions if we have more vertices\n        while (finger_dirs.size() < V - 1) {\n            finger_dirs.push_back(finger_dirs[finger_dirs.size() - 4]);\n        }\n        \n        random_device rd;\n        mt19937 gen(rd());\n        uniform_int_distribution<> dis(0, 3);\n        \n        while (turns < max_turns) {\n            // Random movement for exploration\n            int dir = dis(gen);\n            Point new_pos = current_pos + DIRS[dir];\n            \n            // Check bounds\n            if (new_pos.x >= 0 && new_pos.x < N && new_pos.y >= 0 && new_pos.y < N) {\n                current_pos = new_pos;\n                \n                // Create operation string\n                vector<char> rotations(V, '.');\n                vector<char> actions(V, '.');\n                \n                // For simplicity, let's just have one finger try to grab/release\n                if (V > 1) {\n                    Point finger_pos = current_pos + finger_dirs[0];\n                    if (finger_pos.x >= 0 && finger_pos.x < N && finger_pos.y >= 0 && finger_pos.y < N) {\n                        // Try to grab or release\n                        if (initial[finger_pos.x][finger_pos.y] && !target[finger_pos.x][finger_pos.y]) {\n                            actions[1] = 'P'; // Try to grab\n                        } else if (!initial[finger_pos.x][finger_pos.y] && target[finger_pos.x][finger_pos.y]) {\n                            actions[1] = 'P'; // Try to release\n                        }\n                    }\n                }\n                \n                operations.push_back(createMoveString(DIR_CHARS[dir], rotations, actions));\n                turns++;\n            } else {\n                // Can't move in that direction, stay put\n                vector<char> rotations(V, '.');\n                vector<char> actions(V, '.');\n                operations.push_back(createMoveString('.', rotations, actions));\n                turns++;\n            }\n            \n            // Early termination if we've done enough\n            if (turns % 100 == 0) {\n                // Check if we should continue based on some condition\n                // For now, just continue with random moves\n            }\n        }\n        \n        return operations;\n    }\n    \n    void outputSolution(RobotArm& arm, const vector<string>& operations) {\n        // Output arm design\n        cout << arm.V << endl;\n        for (int i = 1; i < arm.V; i++) {\n            cout << arm.tree[i].first << \" \" << arm.tree[i].second << endl;\n        }\n        cout << arm.root_pos.x << \" \" << arm.root_pos.y << endl;\n        \n        // Output operations\n        for (const string& op : operations) {\n            cout << op << endl;\n        }\n    }\n    \n    void run() {\n        readInput();\n        RobotArm arm = designArm();\n        vector<string> operations = solve(arm);\n        outputSolution(arm, operations);\n    }\n};\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    Solution solution;\n    solution.run();\n    \n    return 0;\n}","ahc039":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MAX_N = 5000;\nconst int FIELD_SIZE = 100000;\nconst int MAX_VERTICES = 1000;\nconst int MAX_PERIMETER = 400000;\n\nstruct Point {\n    int x, y;\n    Point() : x(0), y(0) {}\n    Point(int x, int y) : x(x), y(y) {}\n};\n\nstruct Rectangle {\n    int x1, y1, x2, y2;\n    int mackerels, sardines;\n    int score;\n    \n    Rectangle() : x1(0), y1(0), x2(0), y2(0), mackerels(0), sardines(0), score(0) {}\n    Rectangle(int x1, int y1, int x2, int y2) : x1(x1), y1(y1), x2(x2), y2(y2), mackerels(0), sardines(0), score(0) {}\n    \n    bool isValid() const {\n        return x1 >= 0 && x2 <= FIELD_SIZE && y1 >= 0 && y2 <= FIELD_SIZE && \n               x1 <= x2 && y1 <= y2 && perimeter() <= MAX_PERIMETER;\n    }\n    \n    int perimeter() const {\n        return 2 * (x2 - x1 + y2 - y1);\n    }\n    \n    long long area() const {\n        return 1LL * (x2 - x1) * (y2 - y1);\n    }\n};\n\nvector<Point> mackerels(MAX_N);\nvector<Point> sardines(MAX_N);\nvector<bool> mackerel_in_polygon(MAX_N, false);\nvector<bool> sardine_in_polygon(MAX_N, false);\n\n// Count points in rectangle efficiently\npair<int, int> countPoints(const Rectangle& rect, const vector<Point>& mackerels, const vector<Point>& sardines) {\n    int m_count = 0, s_count = 0;\n    \n    for (int i = 0; i < MAX_N; i++) {\n        const Point& m = mackerels[i];\n        if (m.x >= rect.x1 && m.x <= rect.x2 && m.y >= rect.y1 && m.y <= rect.y2) {\n            m_count++;\n        }\n    }\n    \n    for (int i = 0; i < MAX_N; i++) {\n        const Point& s = sardines[i];\n        if (s.x >= rect.x1 && s.x <= rect.x2 && s.y >= rect.y1 && s.y <= rect.y2) {\n            s_count++;\n        }\n    }\n    \n    return {m_count, s_count};\n}\n\n// Generate rectangles around mackerel clusters\nvector<Rectangle> generateRectangles(const vector<Point>& mackerels, const vector<Point>& sardines) {\n    vector<Rectangle> rectangles;\n    \n    // Sort mackerels by x and y coordinates for clustering\n    vector<Point> sorted_x = mackerels;\n    vector<Point> sorted_y = mackerels;\n    \n    sort(sorted_x.begin(), sorted_x.end(), [](const Point& a, const Point& b) { return a.x < b.x; });\n    sort(sorted_y.begin(), sorted_y.end(), [](const Point& a, const Point& b) { return a.y < b.y; });\n    \n    // Generate rectangles of different sizes around dense areas\n    for (int base_size = 5000; base_size <= 30000; base_size += 5000) {\n        // Try different positions\n        for (int i = 0; i < min(100, (int)mackerels.size()); i++) {\n            int center_x = mackerels[i].x;\n            int center_y = mackerels[i].y;\n            \n            int x1 = max(0, center_x - base_size/2);\n            int y1 = max(0, center_y - base_size/2);\n            int x2 = min(FIELD_SIZE, center_x + base_size/2);\n            int y2 = min(FIELD_SIZE, center_y + base_size/2);\n            \n            Rectangle rect(x1, y1, x2, y2);\n            if (rect.isValid()) {\n                auto counts = countPoints(rect, mackerels, sardines);\n                rect.mackerels = counts.first;\n                rect.sardines = counts.second;\n                rect.score = rect.mackerels - rect.sardines;\n                \n                if (rect.score > 0) {\n                    rectangles.push_back(rect);\n                }\n            }\n        }\n    }\n    \n    // Generate rectangles around individual high-value mackerels\n    for (int i = 0; i < min(200, (int)mackerels.size()); i++) {\n        for (int size = 2000; size <= 20000; size += 2000) {\n            int x1 = max(0, mackerels[i].x - size/2);\n            int y1 = max(0, mackerels[i].y - size/2);\n            int x2 = min(FIELD_SIZE, mackerels[i].x + size/2);\n            int y2 = min(FIELD_SIZE, mackerels[i].y + size/2);\n            \n            Rectangle rect(x1, y1, x2, y2);\n            if (rect.isValid()) {\n                auto counts = countPoints(rect, mackerels, sardines);\n                rect.mackerels = counts.first;\n                rect.sardines = counts.second;\n                rect.score = rect.mackerels - rect.sardines;\n                \n                if (rect.score > 0) {\n                    rectangles.push_back(rect);\n                }\n            }\n        }\n    }\n    \n    // Remove duplicates and sort by score\n    sort(rectangles.begin(), rectangles.end(), [](const Rectangle& a, const Rectangle& b) {\n        if (a.score != b.score) return a.score > b.score;\n        return a.area() < b.area();\n    });\n    \n    // Keep unique rectangles\n    rectangles.erase(unique(rectangles.begin(), rectangles.end(), \n                           [](const Rectangle& a, const Rectangle& b) {\n                               return a.x1 == b.x1 && a.y1 == b.y1 && a.x2 == b.x2 && a.y2 == b.y2;\n                           }), rectangles.end());\n    \n    return rectangles;\n}\n\n// Check if point is inside rectangle\nbool isInside(const Point& p, const Rectangle& rect) {\n    return p.x >= rect.x1 && p.x <= rect.x2 && p.y >= rect.y1 && p.y <= rect.y2;\n}\n\n// Create a rectilinear polygon that combines multiple rectangles\nvector<Point> createOptimizedPolygon(const vector<Rectangle>& rectangles, \n                                   const vector<Point>& mackerels, \n                                   const vector<Point>& sardines) {\n    if (rectangles.empty()) {\n        // Fallback polygon\n        return {{10000, 10000}, {90000, 10000}, {90000, 90000}, {10000, 90000}};\n    }\n    \n    // Use the best single rectangle for now\n    const Rectangle& best = rectangles[0];\n    \n    vector<Point> vertices;\n    vertices.push_back(Point(best.x1, best.y1));\n    vertices.push_back(Point(best.x2, best.y1));\n    vertices.push_back(Point(best.x2, best.y2));\n    vertices.push_back(Point(best.x1, best.y2));\n    \n    return vertices;\n}\n\n// Greedy selection of rectangles to maximize score\nvector<Rectangle> selectBestRectangles(const vector<Rectangle>& rectangles) {\n    if (rectangles.empty()) return {};\n    \n    // For now, just return the best rectangle\n    if (!rectangles.empty()) {\n        return {rectangles[0]};\n    }\n    \n    return {};\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    int N;\n    cin >> N;\n    \n    for (int i = 0; i < N; i++) {\n        cin >> mackerels[i].x >> mackerels[i].y;\n    }\n    \n    for (int i = 0; i < N; i++) {\n        cin >> sardines[i].x >> sardines[i].y;\n    }\n    \n    // Generate candidate rectangles\n    vector<Rectangle> rectangles = generateRectangles(mackerels, sardines);\n    \n    // Select best rectangles\n    vector<Rectangle> selected = selectBestRectangles(rectangles);\n    \n    // Create final polygon\n    vector<Point> vertices = createOptimizedPolygon(selected, mackerels, sardines);\n    \n    // Ensure we have at least 4 vertices\n    while (vertices.size() < 4) {\n        vertices.push_back(vertices.back());\n    }\n    \n    // Output\n    cout << vertices.size() << \"\\n\";\n    for (const auto& vertex : vertices) {\n        cout << vertex.x << \" \" << vertex.y << \"\\n\";\n    }\n    \n    return 0;\n}","ahc040":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Placement {\n    int p, r, d, b;\n    \n    Placement(int _p, int _r, char _d, int _b) : p(_p), r(_r), d(_d), b(_b) {}\n};\n\n// Simple greedy placement algorithm\nvector<Placement> greedyPlace(const vector<pair<int,int>>& rectangles, mt19937& rng) {\n    vector<Placement> placements;\n    int n = rectangles.size();\n    \n    // For each rectangle, decide placement\n    for (int i = 0; i < n; i++) {\n        // Random rotation\n        int rotate = uniform_int_distribution<int>(0, 1)(rng);\n        \n        // Random direction\n        char direction = (uniform_int_distribution<int>(0, 1)(rng) == 0) ? 'U' : 'L';\n        \n        // Random reference (but must be valid)\n        int reference = -1;\n        if (i > 0) {\n            reference = uniform_int_distribution<int>(-1, i-1)(rng);\n        }\n        \n        placements.emplace_back(i, rotate, direction, reference);\n    }\n    \n    return placements;\n}\n\n// Skyline-based placement\nvector<Placement> skylinePlace(const vector<pair<int,int>>& rectangles, mt19937& rng) {\n    vector<Placement> placements;\n    int n = rectangles.size();\n    \n    if (n == 0) return placements;\n    \n    // Place first rectangle at origin\n    int rotate_first = uniform_int_distribution<int>(0, 1)(rng);\n    placements.emplace_back(0, rotate_first, 'U', -1);\n    \n    // For remaining rectangles, try to place them optimally\n    for (int i = 1; i < n; i++) {\n        // Try different strategies\n        int rotate = uniform_int_distribution<int>(0, 1)(rng);\n        char direction = (uniform_int_distribution<int>(0, 1)(rng) == 0) ? 'U' : 'L';\n        int reference = uniform_int_distribution<int>(-1, i-1)(rng);\n        \n        placements.emplace_back(i, rotate, direction, reference);\n    }\n    \n    return placements;\n}\n\n// Try to improve by placing rectangles with better orientation\nvector<Placement> smartPlace(const vector<pair<int,int>>& rectangles, mt19937& rng) {\n    vector<Placement> placements;\n    int n = rectangles.size();\n    \n    if (n == 0) return placements;\n    \n    // Place all rectangles but make smart decisions about rotation and placement\n    for (int i = 0; i < n; i++) {\n        // Smart rotation: prefer orientation that minimizes one dimension\n        int rotate = 0;\n        if (rectangles[i].second > rectangles[i].first * 2) {\n            rotate = 1; // Rotate if height >> width\n        } else if (rectangles[i].first > rectangles[i].second * 2) {\n            rotate = 0; // Keep original if width >> height\n        } else {\n            rotate = uniform_int_distribution<int>(0, 1)(rng); // Random otherwise\n        }\n        \n        // Placement strategy\n        char direction = 'U';\n        int reference = -1;\n        \n        if (i > 0) {\n            // Most of the time place relative to previous, sometimes random\n            if (uniform_int_distribution<int>(0, 4)(rng) == 0) {\n                direction = 'L';\n            }\n            reference = uniform_int_distribution<int>(max(0, i-3), i-1)(rng);\n        }\n        \n        placements.emplace_back(i, rotate, direction, reference);\n    }\n    \n    return placements;\n}\n\n// Mixed strategy - sometimes use fewer rectangles\nvector<Placement> mixedPlace(const vector<pair<int,int>>& rectangles, mt19937& rng) {\n    vector<Placement> placements;\n    int n = rectangles.size();\n    \n    if (n == 0) return placements;\n    \n    // Always place rectangles in order (0,1,2,...) to satisfy constraint\n    // But sometimes make strategic decisions about rotation/placement\n    for (int i = 0; i < n; i++) {\n        // With small probability, we might skip optimization for this rectangle\n        // But we still include it to maintain sequence\n        \n        int rotate = uniform_int_distribution<int>(0, 1)(rng);\n        char direction = (uniform_int_distribution<int>(0, 1)(rng) == 0) ? 'U' : 'L';\n        int reference = -1;\n        \n        if (i > 0) {\n            reference = uniform_int_distribution<int>(-1, i-1)(rng);\n        }\n        \n        placements.emplace_back(i, rotate, direction, reference);\n    }\n    \n    return placements;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    int N, T, sigma;\n    cin >> N >> T >> sigma;\n    \n    vector<pair<int,int>> measured_rectangles(N);\n    for (int i = 0; i < N; i++) {\n        cin >> measured_rectangles[i].first >> measured_rectangles[i].second;\n    }\n    \n    // Initialize random number generator\n    random_device rd;\n    mt19937 rng(rd());\n    \n    // Try different strategies for each turn\n    for (int turn = 0; turn < T; turn++) {\n        vector<Placement> placements;\n        \n        // Choose strategy based on turn\n        if (turn % 4 == 0) {\n            // Random placement\n            placements = greedyPlace(measured_rectangles, rng);\n        } else if (turn % 4 == 1) {\n            // Skyline-based placement\n            placements = skylinePlace(measured_rectangles, rng);\n        } else if (turn % 4 == 2) {\n            // Smart placement\n            placements = smartPlace(measured_rectangles, rng);\n        } else {\n            // Mixed strategy\n            placements = mixedPlace(measured_rectangles, rng);\n        }\n        \n        // Output placements\n        cout << placements.size() << \"\\n\";\n        for (const auto& p : placements) {\n            cout << p.p << \" \" << p.r << \" \" << (char)p.d << \" \" << p.b << \"\\n\";\n        }\n        cout << flush;\n        \n        // Read measurement result\n        int W_meas, H_meas;\n        cin >> W_meas >> H_meas;\n    }\n    \n    return 0;\n}","ahc041":"#include <bits/stdc++.h>\n#include <atcoder/all>\nusing namespace std;\nusing namespace atcoder;\n\nconst int MAXN = 1000;\nconst int MAXH = 10;\n\nint N, M, H;\nvector<int> A;\nvector<vector<int>> adj;\nvector<pair<int, int>> coords;\nvector<int> parent;\nvector<bool> assigned;\n\n// Build trees greedily\nvoid buildTrees() {\n    parent.assign(N, -1);\n    assigned.assign(N, false);\n    \n    // Create list of vertices with their beauty values\n    vector<pair<int, int>> beauty_order;\n    for (int i = 0; i < N; i++) {\n        beauty_order.push_back({A[i], i});\n    }\n    \n    // Sort by beauty value descending\n    sort(beauty_order.rbegin(), beauty_order.rend());\n    \n    for (auto& [beauty, start_v] : beauty_order) {\n        if (assigned[start_v]) continue;\n        \n        // Start a new tree with this vertex as root\n        queue<pair<int, int>> q; // {vertex, depth}\n        q.push({start_v, 0});\n        assigned[start_v] = true;\n        \n        while (!q.empty()) {\n            auto [v, depth] = q.front();\n            q.pop();\n            \n            if (depth >= H) continue;\n            \n            // Try to add unassigned neighbors\n            for (int u : adj[v]) {\n                if (!assigned[u]) {\n                    parent[u] = v;\n                    assigned[u] = true;\n                    q.push({u, depth + 1});\n                }\n            }\n        }\n    }\n}\n\n// Improved approach: try to optimize by reassigning vertices\nvoid optimize() {\n    // For each vertex, if moving it to a better parent increases score, do it\n    bool improved = true;\n    while (improved) {\n        improved = false;\n        vector<pair<int, int>> beauty_order;\n        for (int i = 0; i < N; i++) {\n            beauty_order.push_back({A[i], i});\n        }\n        sort(beauty_order.rbegin(), beauty_order.rend());\n        \n        for (auto& [beauty, v] : beauty_order) {\n            if (parent[v] == -1) continue; // root\n            \n            int current_parent = parent[v];\n            int current_depth = 0;\n            int temp = v;\n            while (parent[temp] != -1) {\n                current_depth++;\n                temp = parent[temp];\n            }\n            \n            // Try to find a better parent (higher up in some tree)\n            for (int u : adj[v]) {\n                if (u == current_parent) continue;\n                \n                // Calculate depth of u in its tree\n                int u_depth = 0;\n                int temp_u = u;\n                while (parent[temp_u] != -1) {\n                    u_depth++;\n                    temp_u = parent[temp_u];\n                }\n                \n                // If we can place v higher (closer to root) and within height limit\n                if (u_depth < H) {\n                    // Check if this improves attractiveness\n                    int new_depth = u_depth + 1;\n                    if (new_depth < current_depth) {\n                        parent[v] = u;\n                        improved = true;\n                        break;\n                    }\n                }\n            }\n            if (improved) break;\n        }\n    }\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    cin >> N >> M >> H;\n    A.resize(N);\n    for (int i = 0; i < N; i++) {\n        cin >> A[i];\n    }\n    \n    adj.resize(N);\n    for (int i = 0; i < M; i++) {\n        int u, v;\n        cin >> u >> v;\n        adj[u].push_back(v);\n        adj[v].push_back(u);\n    }\n    \n    coords.resize(N);\n    for (int i = 0; i < N; i++) {\n        cin >> coords[i].first >> coords[i].second;\n    }\n    \n    buildTrees();\n    optimize();\n    \n    for (int i = 0; i < N; i++) {\n        cout << parent[i];\n        if (i < N - 1) cout << \" \";\n    }\n    cout << \"\\n\";\n    \n    return 0;\n}","ahc042":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MAX_N = 20;\nchar board[MAX_N][MAX_N];\nint N;\n\nstruct Position {\n    int row, col;\n};\n\nvector<Position> oni_list;\nvector<Position> fuku_list;\n\nstruct Move {\n    char direction;\n    int index;\n};\n\nvector<Move> moves;\n\n// Check if column j has any Fukunokami above row r\nbool hasFukuAbove(int r, int j) {\n    for (int i = 0; i < r; i++) {\n        if (board[i][j] == 'o') return true;\n    }\n    return false;\n}\n\n// Check if column j has any Fukunokami below row r\nbool hasFukuBelow(int r, int j) {\n    for (int i = r + 1; i < N; i++) {\n        if (board[i][j] == 'o') return true;\n    }\n    return false;\n}\n\n// Check if row i has any Fukunokami to the left of column c\nbool hasFukuLeft(int i, int c) {\n    for (int j = 0; j < c; j++) {\n        if (board[i][j] == 'o') return true;\n    }\n    return false;\n}\n\n// Check if row i has any Fukunokami to the right of column c\nbool hasFukuRight(int i, int c) {\n    for (int j = c + 1; j < N; j++) {\n        if (board[i][j] == 'o') return true;\n    }\n    return false;\n}\n\nvoid addMove(char dir, int idx) {\n    moves.push_back({dir, idx});\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    cin >> N;\n    for (int i = 0; i < N; i++) {\n        string row;\n        cin >> row;\n        for (int j = 0; j < N; j++) {\n            board[i][j] = row[j];\n            if (row[j] == 'x') {\n                oni_list.push_back({i, j});\n            } else if (row[j] == 'o') {\n                fuku_list.push_back({i, j});\n            }\n        }\n    }\n    \n    // Process each Oni\n    for (const auto& oni : oni_list) {\n        int r = oni.row;\n        int c = oni.col;\n        \n        // Try to remove this Oni in the most efficient way\n        // Prefer directions that require fewer moves\n        \n        // Up direction: shift column down\n        if (!hasFukuAbove(r, c)) {\n            // Shift down r+1 times to move Oni out, then shift back\n            for (int i = 0; i < r + 1; i++) addMove('D', c);\n            for (int i = 0; i < r + 1; i++) addMove('U', c);\n        }\n        // Down direction: shift column up\n        else if (!hasFukuBelow(r, c)) {\n            // Shift up N-r times to move Oni out, then shift back\n            for (int i = 0; i < N - r; i++) addMove('U', c);\n            for (int i = 0; i < N - r; i++) addMove('D', c);\n        }\n        // Left direction: shift row right\n        else if (!hasFukuLeft(r, c)) {\n            // Shift right c+1 times to move Oni out, then shift back\n            for (int i = 0; i < c + 1; i++) addMove('R', r);\n            for (int i = 0; i < c + 1; i++) addMove('L', r);\n        }\n        // Right direction: shift row left\n        else if (!hasFukuRight(r, c)) {\n            // Shift left N-c times to move Oni out, then shift back\n            for (int i = 0; i < N - c; i++) addMove('L', r);\n            for (int i = 0; i < N - c; i++) addMove('R', r);\n        }\n    }\n    \n    // Output the moves\n    for (const auto& move : moves) {\n        cout << move.direction << \" \" << move.index << \"\\n\";\n    }\n    \n    return 0;\n}","ahc044":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 100;\nconst int L = 500000;\n\nint T[N];\n\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\n\n// Simulate the sequence and return the error\nlong long simulate(int a[], int b[]) {\n    vector<int> count(N, 0);\n    vector<long long> actual(N, 0);\n    \n    int current = 0;\n    actual[0]++;\n    count[0]++;\n    \n    // Run for L weeks\n    for (long long step = 1; step < L; step++) {\n        int next;\n        if (count[current] % 2 == 1) {\n            next = a[current];\n        } else {\n            next = b[current];\n        }\n        current = next;\n        actual[current]++;\n        count[current]++;\n    }\n    \n    long long error = 0;\n    for (int i = 0; i < N; i++) {\n        error += abs(actual[i] - (long long)T[i]);\n    }\n    \n    return error;\n}\n\n// Generate a reasonable assignment based on targets\nvoid generate_assignment(int a[], int b[], int T[]) {\n    // Create a list of employees sorted by target (descending)\n    vector<pair<int, int>> targets;\n    for (int i = 0; i < N; i++) {\n        targets.push_back({T[i], i});\n    }\n    sort(targets.rbegin(), targets.rend());\n    \n    // For employees with high targets, make them more reachable\n    // For employees with low targets, make them less reachable but still possible\n    \n    for (int i = 0; i < N; i++) {\n        // Point a_i and b_i towards employees that help achieve target distribution\n        // Simple strategy: point more often to higher target employees\n        \n        // Use a weighted approach\n        uniform_int_distribution<int> dist(0, N-1);\n        \n        // For high target employees, point to other high targets\n        // For low target employees, distribute more evenly\n        \n        if (T[i] > L / N) {\n            // High target - point to other high targets\n            int idx1 = dist(rng) % min(20, N);\n            int idx2 = dist(rng) % min(20, N);\n            a[i] = targets[idx1].second;\n            b[i] = targets[idx2].second;\n        } else if (T[i] < L / (2 * N)) {\n            // Very low target - point more randomly but avoid high targets too much\n            a[i] = dist(rng);\n            b[i] = dist(rng);\n        } else {\n            // Medium target - balanced approach\n            a[i] = targets[dist(rng) % N].second;\n            b[i] = targets[dist(rng) % N].second;\n        }\n        \n        // Ensure valid indices\n        a[i] = max(0, min(N-1, a[i]));\n        b[i] = max(0, min(N-1, b[i]));\n    }\n    \n    // Make sure employee 0 appears in some transitions (since we start there)\n    // This helps ensure connectivity\n    for (int i = 0; i < min(5, N); i++) {\n        if (rng() % 2) {\n            a[targets[i].second] = 0;\n        } else {\n            b[targets[i].second] = 0;\n        }\n    }\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    int n, l;\n    cin >> n >> l;\n    for (int i = 0; i < n; i++) {\n        cin >> T[i];\n    }\n    \n    int a[N], b[N];\n    \n    // Generate initial assignment\n    generate_assignment(a, b, T);\n    \n    // Simple improvement: make sure high-demand employees are reachable\n    vector<pair<int, int>> targets;\n    for (int i = 0; i < N; i++) {\n        targets.push_back({T[i], i});\n    }\n    sort(targets.rbegin(), targets.rend());\n    \n    // Ensure top targets are well-connected\n    for (int i = 0; i < min(10, N); i++) {\n        int emp = targets[i].second;\n        // Make sure someone points to this employee\n        int from = (emp + 1) % N;\n        if (rng() % 2) {\n            a[from] = emp;\n        } else {\n            b[from] = emp;\n        }\n    }\n    \n    // Output result\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>\n#include <atcoder/all>\nusing namespace std;\nusing namespace atcoder;\n\nconst int N = 800;\nconst int MAX_L = 15;\n\nint n, m, q, L, W;\nvector<int> G;\nvector<int> lx, rx, ly, ry;\nvector<pair<int, int>> centers;\nvector<vector<pair<int, int>>> mst_edges;\n\n// Union-Find for MST construction\nclass UnionFind {\nprivate:\n    vector<int> parent, rank;\npublic:\n    UnionFind(int n) : parent(n), rank(n, 0) {\n        for (int i = 0; i < n; i++) parent[i] = i;\n    }\n    \n    int find(int x) {\n        if (parent[x] != x) parent[x] = find(parent[x]);\n        return parent[x];\n    }\n    \n    bool unite(int x, int y) {\n        int root_x = find(x);\n        int root_y = find(y);\n        if (root_x == root_y) return false;\n        if (rank[root_x] < rank[root_y]) swap(root_x, root_y);\n        parent[root_y] = root_x;\n        if (rank[root_x] == rank[root_y]) rank[root_x]++;\n        return true;\n    }\n};\n\n// Query function\nvector<pair<int, int>> query(const vector<int>& cities) {\n    if (cities.size() < 2) return {};\n    \n    cout << \"? \" << cities.size();\n    for (int city : cities) cout << \" \" << city;\n    cout << endl;\n    cout.flush();\n    \n    vector<pair<int, int>> result(cities.size() - 1);\n    for (int i = 0; i < cities.size() - 1; i++) {\n        int u, v;\n        cin >> u >> v;\n        result[i] = {u, v};\n    }\n    return result;\n}\n\n// Calculate estimated distance using center points\ndouble estimated_distance(int i, int j) {\n    double cx1 = (lx[i] + rx[i]) / 2.0;\n    double cy1 = (ly[i] + ry[i]) / 2.0;\n    double cx2 = (lx[j] + rx[j]) / 2.0;\n    double cy2 = (ly[j] + ry[j]) / 2.0;\n    return sqrt((cx1 - cx2) * (cx1 - cx2) + (cy1 - cy2) * (cy1 - cy2));\n}\n\n// Group cities based on proximity\nvector<vector<int>> create_groups() {\n    vector<pair<double, int>> city_positions(n);\n    for (int i = 0; i < n; i++) {\n        city_positions[i] = {estimated_distance(0, i), i};\n    }\n    \n    // Sort by estimated position from first city\n    sort(city_positions.begin(), city_positions.end());\n    \n    vector<vector<int>> groups;\n    int idx = 0;\n    for (int group_size : G) {\n        vector<int> group;\n        for (int i = 0; i < group_size; i++) {\n            group.push_back(city_positions[idx++].second);\n        }\n        groups.push_back(group);\n    }\n    \n    return groups;\n}\n\n// Improved grouping using k-means style approach\nvector<vector<int>> create_groups_improved() {\n    // Use center coordinates for sorting\n    vector<pair<pair<int, int>, int>> city_centers(n);\n    for (int i = 0; i < n; i++) {\n        int cx = (lx[i] + rx[i]) / 2;\n        int cy = (ly[i] + ry[i]) / 2;\n        city_centers[i] = {{cx, cy}, i};\n    }\n    \n    // Sort by x-coordinate then y-coordinate\n    sort(city_centers.begin(), city_centers.end());\n    \n    vector<vector<int>> groups;\n    int idx = 0;\n    for (int group_size : G) {\n        vector<int> group;\n        for (int i = 0; i < group_size; i++) {\n            group.push_back(city_centers[idx++].second);\n        }\n        groups.push_back(group);\n    }\n    \n    return groups;\n}\n\n// Use queries to get MST information\nvector<vector<pair<int, int>>> get_mst_edges(const vector<vector<int>>& groups) {\n    vector<vector<pair<int, int>>> all_edges(m);\n    \n    int queries_used = 0;\n    \n    for (int g = 0; g < m && queries_used < q; g++) {\n        const vector<int>& group = groups[g];\n        int group_size = group.size();\n        \n        if (group_size <= 1) continue;\n        \n        // For small groups, query the entire group\n        if (group_size <= L && queries_used < q) {\n            auto edges = query(group);\n            all_edges[g] = edges;\n            queries_used++;\n            continue;\n        }\n        \n        // For larger groups, use sliding window approach\n        set<pair<int, int>> edge_set;\n        \n        // Query overlapping triplets\n        for (int i = 0; i < min(group_size - 2, (q - queries_used) * 2) && queries_used < q; i++) {\n            vector<int> subset = {group[i], group[i + 1], group[i + 2]};\n            auto edges = query(subset);\n            for (auto edge : edges) {\n                edge_set.insert(edge);\n            }\n            queries_used++;\n        }\n        \n        // Convert set to vector\n        all_edges[g] = vector<pair<int, int>>(edge_set.begin(), edge_set.end());\n    }\n    \n    return all_edges;\n}\n\n// Build MST for each group using collected edges\nvector<vector<pair<int, int>>> build_mst(const vector<vector<int>>& groups, \n                                        const vector<vector<pair<int, int>>>& queried_edges) {\n    vector<vector<pair<int, int>>> result(m);\n    \n    for (int g = 0; g < m; g++) {\n        const vector<int>& group = groups[g];\n        if (group.size() <= 1) continue;\n        \n        // If we have queried edges for this group, use them\n        if (!queried_edges[g].empty()) {\n            // Use Union-Find to extract a spanning tree from the queried edges\n            UnionFind uf(n);\n            vector<pair<int, int>> mst_edges;\n            \n            for (auto edge : queried_edges[g]) {\n                if (uf.unite(edge.first, edge.second)) {\n                    mst_edges.push_back(edge);\n                }\n            }\n            \n            // If we don't have enough edges, add some heuristically\n            if (mst_edges.size() < group.size() - 1) {\n                set<pair<int, int>> existing_edges(mst_edges.begin(), mst_edges.end());\n                \n                // Add direct connections to complete the tree\n                for (int i = 0; i < group.size() - 1; i++) {\n                    pair<int, int> edge = {min(group[i], group[i+1]), max(group[i], group[i+1])};\n                    if (existing_edges.find(edge) == existing_edges.end() && \n                        mst_edges.size() < group.size() - 1) {\n                        mst_edges.push_back(edge);\n                    }\n                }\n            }\n            \n            result[g] = mst_edges;\n        } else {\n            // Fallback: connect consecutive cities\n            vector<pair<int, int>> edges;\n            for (int i = 0; i < group.size() - 1; i++) {\n                edges.push_back({group[i], group[i+1]});\n            }\n            result[g] = edges;\n        }\n    }\n    \n    return result;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    // Read input\n    cin >> n >> m >> q >> L >> W;\n    G.resize(m);\n    for (int i = 0; i < m; i++) cin >> G[i];\n    \n    lx.resize(n); rx.resize(n); ly.resize(n); ry.resize(n);\n    for (int i = 0; i < n; i++) {\n        cin >> lx[i] >> rx[i] >> ly[i] >> ry[i];\n    }\n    \n    // Create groups\n    vector<vector<int>> groups = create_groups_improved();\n    \n    // Use queries to get information\n    vector<vector<pair<int, int>>> queried_edges = get_mst_edges(groups);\n    \n    // Build final MST for each group\n    vector<vector<pair<int, int>>> final_edges = build_mst(groups, queried_edges);\n    \n    // Output answer\n    cout << \"!\" << endl;\n    for (int g = 0; g < m; g++) {\n        // Output group cities\n        for (int i = 0; i < groups[g].size(); i++) {\n            if (i > 0) cout << \" \";\n            cout << groups[g][i];\n        }\n        cout << endl;\n        \n        // Output edges\n        for (auto edge : final_edges[g]) {\n            cout << edge.first << \" \" << edge.second << endl;\n        }\n    }\n    \n    return 0;\n}","ahc046":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 20;\nconst int M = 40;\n\nstruct Point {\n    int r, c;\n    Point() : r(0), c(0) {}\n    Point(int _r, int _c) : r(_r), c(_c) {}\n    bool operator==(const Point& other) const {\n        return r == other.r && c == other.c;\n    }\n};\n\n// Directions\nconst int DR[] = {-1, 1, 0, 0};  // Up, Down, Left, Right\nconst int DC[] = {0, 0, -1, 1};\nconst char DIR_CHARS[] = {'U', 'D', 'L', 'R'};\n\nPoint targets[M];\nbool blocked[N][N];\n\n// Check if position is valid\nbool isValid(int r, int c) {\n    return r >= 0 && r < N && c >= 0 && c < N;\n}\n\n// Simple Manhattan pathfinding\nvector<pair<char, char>> findPath(Point from, Point to) {\n    vector<pair<char, char>> path;\n    int r = from.r, c = from.c;\n    \n    // Move vertically first\n    while (r < to.r) {\n        if (isValid(r + 1, c) && !blocked[r + 1][c]) {\n            path.push_back({'M', 'D'});\n            r++;\n        } else {\n            break;\n        }\n    }\n    while (r > to.r) {\n        if (isValid(r - 1, c) && !blocked[r - 1][c]) {\n            path.push_back({'M', 'U'});\n            r--;\n        } else {\n            break;\n        }\n    }\n    \n    // Move horizontally\n    while (c < to.c) {\n        if (isValid(r, c + 1) && !blocked[r][c + 1]) {\n            path.push_back({'M', 'R'});\n            c++;\n        } else {\n            break;\n        }\n    }\n    while (c > to.c) {\n        if (isValid(r, c - 1) && !blocked[r][c - 1]) {\n            path.push_back({'M', 'L'});\n            c--;\n        } else {\n            break;\n        }\n    }\n    \n    return path;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int n, m;\n    cin >> n >> m;\n    \n    Point start;\n    cin >> start.r >> start.c;\n    \n    for (int i = 0; i < m; i++) {\n        cin >> targets[i].r >> targets[i].c;\n    }\n    \n    vector<pair<char, char>> actions;\n    Point current = start;\n    \n    // Visit each target in order\n    for (int i = 1; i < m; i++) {\n        Point target = targets[i];\n        auto path = findPath(current, target);\n        actions.insert(actions.end(), path.begin(), path.end());\n        current = target;\n    }\n    \n    // Output results\n    for (auto& action : actions) {\n        cout << action.first << \" \" << action.second << \"\\n\";\n    }\n    \n    return 0;\n}"},"4":{"ahc001":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Rectangle {\n    int a, b, c, d; // (a,b) to (c,d)\n    Rectangle() : a(0), b(0), c(0), d(0) {}\n    Rectangle(int _a, int _b, int _c, int _d) : a(_a), b(_b), c(_c), d(_d) {}\n    bool isValid() const { return a < c && b < d; }\n    long long area() const { return 1LL * (c - a) * (d - b); }\n};\n\nstruct Company {\n    int x, y, r;\n    int id;\n    Company() : x(0), y(0), r(0), id(0) {}\n    Company(int _x, int _y, int _r, int _id) : x(_x), y(_y), r(_r), id(_id) {}\n};\n\nint n;\nvector<Company> companies;\nvector<Rectangle> rectangles;\n\n// Check if two rectangles overlap\nbool overlap(const Rectangle& r1, const Rectangle& r2) {\n    return !(r1.c <= r2.a || r2.c <= r1.a || r1.d <= r2.b || r2.d <= r1.b);\n}\n\n// Check if rectangle overlaps with any existing rectangle\nbool hasOverlap(const Rectangle& rect, int skip_id = -1) {\n    for (int i = 0; i < n; i++) {\n        if (i == skip_id) continue;\n        if (rectangles[i].isValid() && overlap(rect, rectangles[i])) {\n            return true;\n        }\n    }\n    return false;\n}\n\n// Try to place a rectangle for company at index idx\nbool placeRectangle(int idx) {\n    const Company& company = companies[idx];\n    int x = company.x;\n    int y = company.y;\n    int r = company.r;\n    \n    // Try different rectangle dimensions\n    vector<pair<int, int>> dimensions;\n    for (int w = 1; w <= min(r, 10000); w++) {\n        if (r % w == 0) {\n            int h = r / w;\n            if (h <= 10000) {\n                dimensions.push_back({w, h});\n            }\n        }\n    }\n    \n    // Sort by perimeter (prefer more square-like rectangles)\n    sort(dimensions.begin(), dimensions.end(), [](const pair<int,int>& a, const pair<int,int>& b) {\n        return abs(a.first - a.second) < abs(b.first - b.second);\n    });\n    \n    // Try to place rectangle containing (x, y) \n    for (auto& dim : dimensions) {\n        int w = dim.first;\n        int h = dim.second;\n        \n        // Try different positions where (x, y) is inside the rectangle\n        for (int dx = 0; dx < w; dx++) {\n            for (int dy = 0; dy < h; dy++) {\n                int a = x - dx;\n                int b = y - dy;\n                int c = a + w;\n                int d = b + h;\n                \n                // Check bounds\n                if (a >= 0 && b >= 0 && c <= 10000 && d <= 10000) {\n                    Rectangle rect(a, b, c, d);\n                    // Check if (x, y) is inside (including boundary)\n                    if (a <= x && x < c && b <= y && y < d) {\n                        if (!hasOverlap(rect, idx)) {\n                            rectangles[idx] = rect;\n                            return true;\n                        }\n                    }\n                }\n            }\n        }\n    }\n    \n    // If we can't place exact area, try approximate placement\n    // Try to place any rectangle containing the point\n    for (int size = max(1, (int)sqrt(r)); size >= 1; size--) {\n        int w = min(size, 10000);\n        int h = min(max(1, r / w), 10000);\n        \n        // Center around the point\n        int a = max(0, min(x - w/2, 10000 - w));\n        int b = max(0, min(y - h/2, 10000 - h));\n        int c = a + w;\n        int d = b + h;\n        \n        if (c <= 10000 && d <= 10000) {\n            Rectangle rect(a, b, c, d);\n            // Check if point is inside\n            if (a <= x && x < c && b <= y && y < d) {\n                if (!hasOverlap(rect, idx)) {\n                    rectangles[idx] = rect;\n                    return true;\n                }\n            }\n        }\n    }\n    \n    // Last resort: place smallest possible rectangle\n    Rectangle rect(max(0, x), max(0, y), min(10000, x + 1), min(10000, y + 1));\n    if (!hasOverlap(rect, idx)) {\n        rectangles[idx] = rect;\n        return true;\n    }\n    \n    return false;\n}\n\n// Improved placement trying multiple strategies\nbool placeRectangleImproved(int idx) {\n    const Company& company = companies[idx];\n    int x = company.x;\n    int y = company.y;\n    int r = company.r;\n    \n    // Strategy 1: Try exact area placement\n    for (int w = 1; w <= min(r, 10000); w++) {\n        if (r % w == 0) {\n            int h = r / w;\n            if (h <= 10000) {\n                // Try to center around the point\n                for (int offset_x = min(w, 20); offset_x >= 0; offset_x--) {\n                    for (int offset_y = min(h, 20); offset_y >= 0; offset_y--) {\n                        int a = max(0, min(x - offset_x, 10000 - w));\n                        int b = max(0, min(y - offset_y, 10000 - h));\n                        int c = a + w;\n                        int d = b + h;\n                        \n                        if (c <= 10000 && d <= 10000) {\n                            Rectangle rect(a, b, c, d);\n                            if (a <= x && x < c && b <= y && y < d) {\n                                if (!hasOverlap(rect, idx)) {\n                                    rectangles[idx] = rect;\n                                    return true;\n                                }\n                            }\n                        }\n                    }\n                }\n            }\n        }\n    }\n    \n    // Strategy 2: Place any valid rectangle containing the point\n    int best_area = 0;\n    Rectangle best_rect;\n    \n    for (int w = 1; w <= 100 && w <= 10000; w++) {\n        for (int h = 1; h <= 100 && h <= 10000; h++) {\n            // Try different positions\n            for (int dx = 0; dx < min(w, 20); dx++) {\n                for (int dy = 0; dy < min(h, 20); dy++) {\n                    int a = max(0, min(x - dx, 10000 - w));\n                    int b = max(0, min(y - dy, 10000 - h));\n                    int c = a + w;\n                    int d = b + h;\n                    \n                    if (c <= 10000 && d <= 10000) {\n                        Rectangle rect(a, b, c, d);\n                        if (a <= x && x < c && b <= y && y < d) {\n                            if (!hasOverlap(rect, idx)) {\n                                int area = rect.area();\n                                if (area > best_area) {\n                                    best_area = area;\n                                    best_rect = rect;\n                                }\n                            }\n                        }\n                    }\n                }\n            }\n        }\n    }\n    \n    if (best_area > 0) {\n        rectangles[idx] = best_rect;\n        return true;\n    }\n    \n    // Strategy 3: Last resort - minimal rectangle\n    for (int a = max(0, x - 5); a <= min(x + 5, 9999); a++) {\n        for (int b = max(0, y - 5); b <= min(y + 5, 9999); b++) {\n            Rectangle rect(a, b, a + 1, b + 1);\n            if (a <= x && x < a + 1 && b <= y && y < b + 1) {\n                if (!hasOverlap(rect, idx)) {\n                    rectangles[idx] = rect;\n                    return true;\n                }\n            }\n        }\n    }\n    \n    return false;\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    cin >> n;\n    companies.resize(n);\n    rectangles.resize(n);\n    \n    for (int i = 0; i < n; i++) {\n        int x, y, r;\n        cin >> x >> y >> r;\n        companies[i] = Company(x, y, r, i);\n    }\n    \n    // Sort by area descending (place larger rectangles first)\n    sort(companies.begin(), companies.end(), [](const Company& a, const Company& b) {\n        return a.r > b.r;\n    });\n    \n    // Place rectangles\n    for (int i = 0; i < n; i++) {\n        placeRectangleImproved(companies[i].id);\n    }\n    \n    // Output results\n    vector<pair<int, Rectangle>> result(n);\n    for (int i = 0; i < n; i++) {\n        result[i] = {companies[i].id, rectangles[companies[i].id]};\n    }\n    \n    sort(result.begin(), result.end());\n    \n    for (int i = 0; i < n; i++) {\n        cout << result[i].second.a << \" \" << result[i].second.b << \" \" \n             << result[i].second.c << \" \" << result[i].second.d << \"\\n\";\n    }\n    \n    return 0;\n}","ahc002":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int SIZE = 50;\nconst int MAX_TILES = 2500;\nint si, sj;\nint tile[SIZE][SIZE];\nint value[SIZE][SIZE];\nvector<pair<int, int>> tile_positions[MAX_TILES];\nint tile_values[MAX_TILES];\n\n// Directions: U, D, L, R\nconst int dx[4] = {-1, 1, 0, 0};\nconst int dy[4] = {0, 0, -1, 1};\nconst char dir_chars[4] = {'U', 'D', 'L', 'R'};\n\n// Check if position is valid\nbool is_valid(int x, int y) {\n    return x >= 0 && x < SIZE && y >= 0 && y < SIZE;\n}\n\n// Initialize tile information\nvoid init_tiles() {\n    for (int i = 0; i < MAX_TILES; i++) {\n        tile_positions[i].clear();\n    }\n    \n    for (int i = 0; i < SIZE; i++) {\n        for (int j = 0; j < SIZE; j++) {\n            tile_positions[tile[i][j]].push_back({i, j});\n        }\n    }\n    \n    for (int i = 0; i < MAX_TILES; i++) {\n        if (!tile_positions[i].empty()) {\n            int total = 0;\n            for (auto& pos : tile_positions[i]) {\n                total += value[pos.first][pos.second];\n            }\n            tile_values[i] = total;\n        }\n    }\n}\n\n// Enhanced beam search with better evaluation\nstring beam_search_enhanced(int beam_width, int max_steps, double time_limit_factor = 1.0) {\n    const int BEAM_WIDTH = beam_width;\n    const int MAX_STEPS = max_steps;\n    \n    struct State {\n        int score;\n        int x, y;\n        string path;\n        vector<bool> visited;\n        \n        bool operator<(const State& other) const {\n            if (score != other.score) return score < other.score;\n            return path.length() > other.path.length(); // Prefer shorter paths if scores equal\n        }\n    };\n    \n    priority_queue<State> beam;\n    \n    // Initialize\n    vector<bool> visited(MAX_TILES, false);\n    int start_tile_id = tile[si][sj];\n    visited[start_tile_id] = true;\n    int start_score = tile_values[start_tile_id];\n    \n    beam.push({start_score, si, sj, \"\", visited});\n    \n    string best_path = \"\";\n    int best_score = start_score;\n    \n    clock_t start_time = clock();\n    const double TIME_LIMIT = 1.9 * time_limit_factor;\n    \n    for (int step = 0; step < MAX_STEPS && !beam.empty(); step++) {\n        priority_queue<State> next_beam;\n        \n        // Process current beam\n        int processed = 0;\n        while (!beam.empty() && processed < BEAM_WIDTH) {\n            State current = beam.top();\n            beam.pop();\n            processed++;\n            \n            // Update best if needed\n            if (current.score > best_score) {\n                best_score = current.score;\n                best_path = current.path;\n            }\n            \n            // Check time limit\n            double elapsed = (double)(clock() - start_time) / CLOCKS_PER_SEC;\n            if (elapsed > TIME_LIMIT) {\n                return best_path;\n            }\n            \n            // Generate next moves\n            for (int d = 0; d < 4; d++) {\n                int nx = current.x + dx[d];\n                int ny = current.y + dy[d];\n                \n                if (is_valid(nx, ny)) {\n                    int next_tile_id = tile[nx][ny];\n                    if (!current.visited[next_tile_id]) {\n                        // Create new state\n                        vector<bool> new_visited = current.visited;\n                        new_visited[next_tile_id] = true;\n                        \n                        int new_score = current.score + tile_values[next_tile_id];\n                        string new_path = current.path + dir_chars[d];\n                        \n                        next_beam.push({new_score, nx, ny, new_path, new_visited});\n                    }\n                }\n            }\n        }\n        \n        // Keep only top beam_width states\n        int added = 0;\n        while (!next_beam.empty() && added < BEAM_WIDTH) {\n            beam.push(next_beam.top());\n            next_beam.pop();\n            added++;\n        }\n    }\n    \n    return best_path;\n}\n\n// Look-ahead greedy with better evaluation\nstring lookahead_greedy(int look_ahead_depth = 3) {\n    vector<bool> visited(MAX_TILES, false);\n    int x = si, y = sj;\n    visited[tile[x][y]] = true;\n    string path = \"\";\n    \n    for (int step = 0; step < 200; step++) {\n        vector<tuple<int, int, int, int>> candidates; // {score, future_score, direction, tile_id}\n        \n        for (int d = 0; d < 4; d++) {\n            int nx = x + dx[d];\n            int ny = y + dy[d];\n            \n            if (is_valid(nx, ny)) {\n                int tile_id = tile[nx][ny];\n                if (!visited[tile_id]) {\n                    int immediate_score = tile_values[tile_id];\n                    \n                    // Look ahead\n                    int future_score = 0;\n                    vector<bool> temp_visited = visited;\n                    temp_visited[tile_id] = true;\n                    \n                    // Simple 1-step look-ahead\n                    for (int d2 = 0; d2 < 4; d2++) {\n                        int nnx = nx + dx[d2];\n                        int nny = ny + dy[d2];\n                        \n                        if (is_valid(nnx, nny)) {\n                            int next_tile_id = tile[nnx][nny];\n                            if (!temp_visited[next_tile_id]) {\n                                future_score = max(future_score, tile_values[next_tile_id]);\n                            }\n                        }\n                    }\n                    \n                    candidates.push_back({immediate_score, future_score, d, tile_id});\n                }\n            }\n        }\n        \n        if (candidates.empty()) break;\n        \n        // Sort by immediate score + weighted future score\n        sort(candidates.begin(), candidates.end(), [](const auto& a, const auto& b) {\n            int score_a = get<0>(a) + get<1>(a) / 2;\n            int score_b = get<0>(b) + get<1>(b) / 2;\n            if (score_a != score_b) return score_a > score_b;\n            return get<0>(a) > get<0>(b); // Break ties by immediate score\n        });\n        \n        // Choose with some randomness\n        int chosen_idx = 0;\n        if (candidates.size() > 1 && rand() % 100 < 15) {\n            chosen_idx = min(1, (int)candidates.size() - 1);\n        }\n        \n        auto chosen = candidates[chosen_idx];\n        int chosen_dir = get<2>(chosen);\n        int nx = x + dx[chosen_dir];\n        int ny = y + dy[chosen_dir];\n        \n        path += dir_chars[chosen_dir];\n        x = nx;\n        y = ny;\n        visited[get<3>(chosen)] = true;\n    }\n    \n    return path;\n}\n\n// Multiple strategy approach\nstring multi_strategy_search() {\n    string best_path = \"\";\n    int best_score = 0;\n    \n    // Strategy 1: Wide beam search\n    string result1 = beam_search_enhanced(100, 150, 0.8);\n    \n    // Strategy 2: Deep beam search\n    string result2 = beam_search_enhanced(30, 250, 0.9);\n    \n    // Strategy 3: Look-ahead greedy\n    string result3 = lookahead_greedy(2);\n    \n    // Strategy 4: Another greedy variant\n    string result4 = lookahead_greedy(1);\n    \n    // Return the longest path (assuming longer paths collect more score)\n    vector<pair<int, string>> results = {\n        {result1.length(), result1},\n        {result2.length(), result2},\n        {result3.length(), result3},\n        {result4.length(), result4}\n    };\n    \n    sort(results.rbegin(), results.rend());\n    return results[0].second;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    // Read input\n    cin >> si >> sj;\n    \n    for (int i = 0; i < SIZE; i++) {\n        for (int j = 0; j < SIZE; j++) {\n            cin >> tile[i][j];\n        }\n    }\n    \n    for (int i = 0; i < SIZE; i++) {\n        for (int j = 0; j < SIZE; j++) {\n            cin >> value[i][j];\n        }\n    }\n    \n    // Initialize tile information\n    init_tiles();\n    \n    // Find and output path\n    string result = multi_strategy_search();\n    cout << result << endl;\n    \n    return 0;\n}","ahc003":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int GRID_SIZE = 30;\nconst int MAX_QUERIES = 1000;\n\n// Direction vectors: U, D, L, R\nconst int di[4] = {-1, 1, 0, 0};\nconst int dj[4] = {0, 0, -1, 1};\nconst char dir_chars[4] = {'U', 'D', 'L', 'R'};\n\n// Estimated edge weights\ndouble h[GRID_SIZE][GRID_SIZE-1]; // horizontal edges (i, j) -> (i, j+1)\ndouble v[GRID_SIZE-1][GRID_SIZE]; // vertical edges (i, j) -> (i+1, j)\n\n// Count of observations for each edge (for confidence)\nint h_count[GRID_SIZE][GRID_SIZE-1];\nint v_count[GRID_SIZE-1][GRID_SIZE];\n\n// Sum of weighted observations for better averaging\ndouble h_sum[GRID_SIZE][GRID_SIZE-1];\ndouble v_sum[GRID_SIZE-1][GRID_SIZE];\n\nvoid init_weights() {\n    // Initialize with lower estimates to encourage exploration early on\n    for (int i = 0; i < GRID_SIZE; i++) {\n        for (int j = 0; j < GRID_SIZE-1; j++) {\n            h[i][j] = 3000.0;\n            h_count[i][j] = 0;\n            h_sum[i][j] = 0.0;\n        }\n    }\n    for (int i = 0; i < GRID_SIZE-1; i++) {\n        for (int j = 0; j < GRID_SIZE; j++) {\n            v[i][j] = 3000.0;\n            v_count[i][j] = 0;\n            v_sum[i][j] = 0.0;\n        }\n    }\n}\n\nstruct State {\n    int i, j;\n    double cost;\n    string path;\n    bool operator>(const State& other) const {\n        return cost > other.cost;\n    }\n};\n\nstring find_path(int si, int sj, int ti, int tj) {\n    // Dijkstra's algorithm\n    vector<vector<double>> dist(GRID_SIZE, vector<double>(GRID_SIZE, 1e18));\n    vector<vector<pair<int, int>>> prev(GRID_SIZE, vector<pair<int, int>>(GRID_SIZE, make_pair(-1, -1)));\n    vector<vector<char>> prev_dir(GRID_SIZE, vector<char>(GRID_SIZE, ' '));\n    \n    priority_queue<State, vector<State>, greater<State>> pq;\n    dist[si][sj] = 0;\n    pq.push({si, sj, 0, \"\"});\n    \n    while (!pq.empty()) {\n        auto [i, j, cost, path] = pq.top(); pq.pop();\n        if (cost > dist[i][j]) continue;\n        \n        // Try all 4 directions\n        for (int d = 0; d < 4; d++) {\n            int ni = i + di[d];\n            int nj = j + dj[d];\n            \n            if (ni >= 0 && ni < GRID_SIZE && nj >= 0 && nj < GRID_SIZE) {\n                double edge_cost = 0;\n                if (d == 0) { // Up: (i,j) -> (i-1,j)\n                    if (ni < i) edge_cost = v[ni][nj];\n                } else if (d == 1) { // Down: (i,j) -> (i+1,j)\n                    if (i < ni) edge_cost = v[i][j];\n                } else if (d == 2) { // Left: (i,j) -> (i,j-1)\n                    if (nj < j) edge_cost = h[ni][nj];\n                } else if (d == 3) { // Right: (i,j) -> (i,j+1)\n                    if (j < nj) edge_cost = h[i][j];\n                }\n                \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                    prev_dir[ni][nj] = dir_chars[d];\n                    pq.push({ni, nj, new_cost, \"\"});\n                }\n            }\n        }\n    }\n    \n    // Reconstruct path\n    string path;\n    int ci = ti, cj = tj;\n    while (ci != si || cj != sj) {\n        path += prev_dir[ci][cj];\n        auto p = prev[ci][cj];\n        ci = p.first;\n        cj = p.second;\n    }\n    reverse(path.begin(), path.end());\n    \n    return path;\n}\n\n// Store the last path for updating weights\nvector<pair<int, int>> last_path_coords;\nstring last_path;\n\nvoid store_path_info(int si, int sj, const string& path) {\n    last_path_coords.clear();\n    last_path_coords.push_back({si, sj});\n    \n    int ci = si, cj = sj;\n    for (char c : path) {\n        if (c == 'U') ci--;\n        else if (c == 'D') ci++;\n        else if (c == 'L') cj--;\n        else if (c == 'R') cj++;\n        last_path_coords.push_back({ci, cj});\n    }\n    last_path = path;\n}\n\nvoid update_weights(int actual_cost) {\n    // Get the edges used in the path\n    vector<pair<pair<int, int>, bool>> edges_used; // {{i,j}, is_vertical}\n    \n    for (int k = 0; k < (int)last_path.length(); k++) {\n        int i1 = last_path_coords[k].first;\n        int j1 = last_path_coords[k].second;\n        int i2 = last_path_coords[k+1].first;\n        int j2 = last_path_coords[k+1].second;\n        \n        if (i1 == i2) {\n            // Horizontal move\n            int j = min(j1, j2);\n            edges_used.push_back({{i1, j}, false});\n        } else {\n            // Vertical move\n            int i = min(i1, i2);\n            edges_used.push_back({{i, j1}, true});\n        }\n    }\n    \n    if (edges_used.empty()) return;\n    \n    // Calculate estimated cost\n    double estimated_cost = 0;\n    for (auto& edge : edges_used) {\n        auto [pos, is_vertical] = edge;\n        int i = pos.first, j = pos.second;\n        if (is_vertical) {\n            estimated_cost += v[i][j];\n        } else {\n            estimated_cost += h[i][j];\n        }\n    }\n    \n    if (estimated_cost <= 0) return;\n    \n    // Calculate error\n    double error = (double)actual_cost - estimated_cost;\n    \n    // Distribute error proportionally to edge weights\n    for (auto& edge : edges_used) {\n        auto [pos, is_vertical] = edge;\n        int i = pos.first, j = pos.second;\n        \n        double edge_weight = is_vertical ? v[i][j] : h[i][j];\n        double proportion = edge_weight / estimated_cost;\n        double adjustment = error * proportion;\n        \n        // Adaptive learning rate - higher confidence = slower learning\n        double confidence = is_vertical ? min(1.0, (double)v_count[i][j] / 10.0) : min(1.0, (double)h_count[i][j] / 10.0);\n        double learning_rate = 0.3 / (1.0 + confidence);\n        learning_rate = max(0.01, learning_rate); // Minimum learning rate\n        \n        double new_weight = edge_weight + adjustment * learning_rate;\n        \n        // Keep weights in reasonable bounds\n        new_weight = max(500.0, min(15000.0, new_weight));\n        \n        if (is_vertical) {\n            v_sum[i][j] += new_weight;\n            v_count[i][j]++;\n            // Use running average for more stable estimates\n            v[i][j] = v_sum[i][j] / v_count[i][j];\n        } else {\n            h_sum[i][j] += new_weight;\n            h_count[i][j]++;\n            // Use running average for more stable estimates\n            h[i][j] = h_sum[i][j] / h_count[i][j];\n        }\n    }\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    init_weights();\n    \n    for (int query = 0; query < MAX_QUERIES; query++) {\n        int si, sj, ti, tj;\n        cin >> si >> sj >> ti >> tj;\n        \n        string path = find_path(si, sj, ti, tj);\n        store_path_info(si, sj, path);\n        \n        cout << path << endl;\n        cout.flush();\n        \n        int result;\n        cin >> result;\n        \n        update_weights(result);\n    }\n    \n    return 0;\n}","ahc004":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 20;\nconst string CHARS = \"ABCDEFGH\";\n\n// Matrix representation\nvector<vector<char>> matrix(N, vector<char>(N, '.'));\nvector<string> strings;\n\n// Count how many strings are satisfied\nint countSatisfied(const vector<vector<char>>& mat, const vector<string>& strs) {\n    int count = 0;\n    for (const string& s : strs) {\n        bool found = false;\n        // Check horizontal\n        for (int i = 0; i < N && !found; i++) {\n            for (int j = 0; j < N && !found; j++) {\n                bool match = true;\n                for (int k = 0; k < (int)s.length(); k++) {\n                    int col = (j + k) % N;\n                    if (mat[i][col] != '.' && mat[i][col] != s[k]) {\n                        match = false;\n                        break;\n                    }\n                }\n                if (match) {\n                    found = true;\n                    break;\n                }\n            }\n            if (found) break;\n        }\n        // Check vertical if not found horizontally\n        if (!found) {\n            for (int j = 0; j < N && !found; j++) {\n                for (int i = 0; i < N && !found; i++) {\n                    bool match = true;\n                    for (int k = 0; k < (int)s.length(); k++) {\n                        int row = (i + k) % N;\n                        if (mat[row][j] != '.' && mat[row][j] != s[k]) {\n                            match = false;\n                            break;\n                        }\n                    }\n                    if (match) {\n                        found = true;\n                        break;\n                    }\n                }\n                if (found) break;\n            }\n        }\n        if (found) count++;\n    }\n    return count;\n}\n\n// Check if a string can be placed at a specific position and direction\nbool canPlace(const vector<vector<char>>& mat, const string& s, int row, int col, bool vertical) {\n    if (vertical) {\n        for (int i = 0; i < (int)s.length(); i++) {\n            int r = (row + i) % N;\n            if (mat[r][col] != '.' && mat[r][col] != s[i]) {\n                return false;\n            }\n        }\n    } else {\n        for (int j = 0; j < (int)s.length(); j++) {\n            int c = (col + j) % N;\n            if (mat[row][c] != '.' && mat[row][c] != s[j]) {\n                return false;\n            }\n        }\n    }\n    return true;\n}\n\n// Place a string (modifies matrix)\nvoid placeString(vector<vector<char>>& mat, const string& s, int row, int col, bool vertical) {\n    if (vertical) {\n        for (int i = 0; i < (int)s.length(); i++) {\n            int r = (row + i) % N;\n            mat[r][col] = s[i];\n        }\n    } else {\n        for (int j = 0; j < (int)s.length(); j++) {\n            int c = (col + j) % N;\n            mat[row][c] = s[j];\n        }\n    }\n}\n\n// Try to place as many strings as possible\nvoid placeStrings() {\n    // Try to place each string optimally\n    for (int idx = 0; idx < (int)strings.size(); idx++) {\n        const string& s = strings[idx];\n        bool placed = false;\n        \n        // Try all positions and directions\n        for (int dir = 0; dir < 2 && !placed; dir++) {\n            bool vertical = (dir == 1);\n            for (int i = 0; i < N && !placed; i++) {\n                for (int j = 0; j < N && !placed; j++) {\n                    if (canPlace(matrix, s, i, j, vertical)) {\n                        placeString(matrix, s, i, j, vertical);\n                        placed = true;\n                    }\n                }\n            }\n        }\n    }\n}\n\n// Fill empty cells with valid characters\nvoid fillEmptyCells() {\n    random_device rd;\n    mt19937 gen(rd());\n    uniform_int_distribution<> char_dis(0, 7);\n    \n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (matrix[i][j] == '.') {\n                matrix[i][j] = CHARS[char_dis(gen)];\n            }\n        }\n    }\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    int M;\n    cin >> M;  // N is fixed to 20 according to problem statement\n    \n    strings.resize(M);\n    for (int i = 0; i < M; i++) {\n        cin >> strings[i];\n    }\n    \n    // Initialize matrix\n    matrix.assign(N, vector<char>(N, '.'));\n    \n    // Place strings\n    placeStrings();\n    \n    // Fill remaining empty cells\n    fillEmptyCells();\n    \n    // Output result - ensure only valid characters\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            // Validate character before output\n            if (matrix[i][j] == '.' || (matrix[i][j] >= 'A' && matrix[i][j] <= 'H')) {\n                cout << matrix[i][j];\n            } else {\n                // Fallback to a valid character if somehow invalid\n                cout << 'A';\n            }\n        }\n        cout << \"\\n\";\n    }\n    \n    return 0;\n}","ahc005":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MAX_N = 70;\nint N, si, sj;\nchar grid[MAX_N][MAX_N];\nbool is_road[MAX_N][MAX_N];\nint cost[MAX_N][MAX_N];\nvector<pair<int,int>> roads;\n\n// Directions: U, D, L, R\nconst int dx[4] = {-1, 1, 0, 0};\nconst int dy[4] = {0, 0, -1, 1};\nconst char dir_char[4] = {'U', 'D', 'L', 'R'};\n\n// Calculate which roads are visible from position (x, y)\nset<pair<int,int>> get_visible_roads(int x, int y) {\n    set<pair<int,int>> visible;\n    \n    if (!is_road[x][y]) return visible;\n    \n    // Horizontal visibility (same row)\n    // Look left\n    for (int j = y; j >= 0; j--) {\n        if (!is_road[x][j]) break;\n        visible.insert({x, j});\n    }\n    // Look right\n    for (int j = y; j < N; j++) {\n        if (!is_road[x][j]) break;\n        visible.insert({x, j});\n    }\n    \n    // Vertical visibility (same column)\n    // Look up\n    for (int i = x; i >= 0; i--) {\n        if (!is_road[i][y]) break;\n        visible.insert({i, y});\n    }\n    // Look down\n    for (int i = x; i < N; i++) {\n        if (!is_road[i][y]) break;\n        visible.insert({i, y});\n    }\n    \n    return visible;\n}\n\nbool is_valid(int x, int y) {\n    return x >= 0 && x < N && y >= 0 && y < N && is_road[x][y];\n}\n\n// BFS to find shortest path between two points\nvector<pair<int,int>> bfs_path(int sx, int sy, int tx, int ty) {\n    if (sx == tx && sy == ty) return {{sx, sy}};\n    if (!is_valid(sx, sy) || !is_valid(tx, ty)) return {{sx, sy}};\n    \n    queue<pair<int,int>> q;\n    bool visited[MAX_N][MAX_N] = {false};\n    pair<int,int> parent[MAX_N][MAX_N];\n    \n    q.push({sx, sy});\n    visited[sx][sy] = true;\n    \n    while (!q.empty()) {\n        auto [x, y] = q.front(); q.pop();\n        \n        for (int d = 0; d < 4; d++) {\n            int nx = x + dx[d];\n            int ny = y + dy[d];\n            \n            if (is_valid(nx, ny) && !visited[nx][ny]) {\n                visited[nx][ny] = true;\n                parent[nx][ny] = {x, y};\n                q.push({nx, ny});\n                \n                if (nx == tx && ny == ty) {\n                    // Reconstruct path\n                    vector<pair<int,int>> path;\n                    pair<int,int> current = {tx, ty};\n                    while (current.first != sx || current.second != sy) {\n                        path.push_back(current);\n                        current = parent[current.first][current.second];\n                    }\n                    path.push_back({sx, sy});\n                    reverse(path.begin(), path.end());\n                    return path;\n                }\n            }\n        }\n    }\n    \n    return {{sx, sy}}; // Fallback\n}\n\n// Convert path to direction string\nstring path_to_directions(const vector<pair<int,int>>& path) {\n    string result;\n    for (int i = 1; i < path.size(); i++) {\n        auto [prev_x, prev_y] = path[i-1];\n        auto [curr_x, curr_y] = path[i];\n        \n        if (curr_x == prev_x - 1 && curr_y == prev_y) result += 'U';\n        else if (curr_x == prev_x + 1 && curr_y == prev_y) result += 'D';\n        else if (curr_x == prev_x && curr_y == prev_y - 1) result += 'L';\n        else if (curr_x == prev_x && curr_y == prev_y + 1) result += 'R';\n    }\n    return result;\n}\n\n// Improved greedy approach with better coverage tracking\nstring solve_improved() {\n    // Track which roads need to be covered\n    set<pair<int,int>> uncovered_roads;\n    for (auto& road : roads) {\n        uncovered_roads.insert(road);\n    }\n    \n    vector<pair<int,int>> path_positions = {{si, sj}};\n    int current_x = si, current_y = sj;\n    \n    // Mark initially visible roads as covered\n    auto initial_visible = get_visible_roads(current_x, current_y);\n    for (auto& road : initial_visible) {\n        uncovered_roads.erase(road);\n    }\n    \n    const int MAX_ITERATIONS = 1500;\n    int iterations = 0;\n    \n    // Greedy selection: at each step, choose the position that covers most uncovered roads\n    while (!uncovered_roads.empty() && iterations < MAX_ITERATIONS) {\n        pair<int,int> best_next = {-1, -1};\n        int best_coverage = -1;\n        vector<pair<int,int>> best_path;\n        \n        // Consider moving to nearby uncovered roads\n        vector<pair<int,int>> candidates;\n        for (auto& road : uncovered_roads) {\n            candidates.push_back(road);\n            if (candidates.size() >= 50) break; // Limit candidates for performance\n        }\n        \n        // Also consider some random road positions for exploration\n        for (int i = 0; i < min(20, (int)roads.size()); i++) {\n            candidates.push_back(roads[rand() % roads.size()]);\n        }\n        \n        for (auto& candidate : candidates) {\n            // Find path to candidate\n            auto path_to_candidate = bfs_path(current_x, current_y, candidate.first, candidate.second);\n            if (path_to_candidate.size() <= 1) continue;\n            \n            // Calculate coverage from candidate position\n            auto visible_from_candidate = get_visible_roads(candidate.first, candidate.second);\n            int coverage_count = 0;\n            for (auto& visible : visible_from_candidate) {\n                if (uncovered_roads.count(visible)) {\n                    coverage_count++;\n                }\n            }\n            \n            // Prefer moves that cover more new roads, but penalize long paths\n            int score = coverage_count * 100 - (int)path_to_candidate.size();\n            if (score > best_coverage) {\n                best_coverage = score;\n                best_next = candidate;\n                best_path = path_to_candidate;\n            }\n        }\n        \n        if (best_next.first == -1 || best_path.size() <= 1) {\n            // No good move found, try to go to any uncovered road\n            if (!uncovered_roads.empty()) {\n                auto target = *uncovered_roads.begin();\n                auto path_to_target = bfs_path(current_x, current_y, target.first, target.second);\n                best_path = path_to_target;\n                best_next = target;\n            } else {\n                break;\n            }\n        }\n        \n        // Execute the move\n        for (int i = 1; i < best_path.size(); i++) {\n            path_positions.push_back(best_path[i]);\n        }\n        current_x = best_path.back().first;\n        current_y = best_path.back().second;\n        \n        // Update uncovered roads\n        auto visible_now = get_visible_roads(current_x, current_y);\n        for (auto& visible : visible_now) {\n            uncovered_roads.erase(visible);\n        }\n        \n        iterations++;\n    }\n    \n    // Return to start\n    auto return_path = bfs_path(current_x, current_y, si, sj);\n    for (int i = 1; i < return_path.size(); i++) {\n        path_positions.push_back(return_path[i]);\n    }\n    \n    return path_to_directions(path_positions);\n}\n\n// Spiral approach for better coverage\nstring solve_spiral() {\n    vector<pair<int,int>> path_positions = {{si, sj}};\n    int current_x = si, current_y = sj;\n    \n    set<pair<int,int>> visited;\n    visited.insert({si, sj});\n    \n    // Simple spiral-like movement\n    int steps = 0;\n    const int MAX_STEPS = 1000;\n    \n    while (steps < MAX_STEPS) {\n        // Try moves in all directions\n        bool moved = false;\n        for (int d = 0; d < 4; d++) {\n            int nx = current_x + dx[d];\n            int ny = current_y + dy[d];\n            \n            if (is_valid(nx, ny) && !visited.count({nx, ny})) {\n                current_x = nx;\n                current_y = ny;\n                path_positions.push_back({current_x, current_y});\n                visited.insert({current_x, current_y});\n                moved = true;\n                break;\n            }\n        }\n        \n        // If can't move to unvisited, move anyway\n        if (!moved) {\n            for (int d = 0; d < 4; d++) {\n                int nx = current_x + dx[d];\n                int ny = current_y + dy[d];\n                if (is_valid(nx, ny)) {\n                    current_x = nx;\n                    current_y = ny;\n                    path_positions.push_back({current_x, current_y});\n                    moved = true;\n                    break;\n                }\n            }\n        }\n        \n        if (!moved) break;\n        steps++;\n    }\n    \n    // Return to start\n    auto return_path = bfs_path(current_x, current_y, si, sj);\n    for (int i = 1; i < return_path.size(); i++) {\n        path_positions.push_back(return_path[i]);\n    }\n    \n    return path_to_directions(path_positions);\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    srand(time(NULL));\n    \n    cin >> N >> si >> sj;\n    \n    for (int i = 0; i < N; i++) {\n        string line;\n        cin >> line;\n        for (int j = 0; j < N; j++) {\n            grid[i][j] = line[j];\n            if (line[j] >= '5' && line[j] <= '9') {\n                is_road[i][j] = true;\n                cost[i][j] = line[j] - '0';\n                roads.push_back({i, j});\n            } else {\n                is_road[i][j] = false;\n            }\n        }\n    }\n    \n    // Try different approaches\n    string result1 = solve_improved();\n    string result2 = solve_spiral();\n    \n    // Output the longer one (likely to have better coverage)\n    if (result1.length() > result2.length()) {\n        cout << result1 << endl;\n    } else {\n        cout << result2 << endl;\n    }\n    \n    return 0;\n}","future-contest-2022-qual":"#include <bits/stdc++.h>\n#include <atcoder/all>\nusing namespace std;\nusing namespace atcoder;\n\n// Constants\nconst int MAX_N = 1000;\nconst int MAX_M = 20;\nconst int MAX_DAYS = 2000;\n\n// Global variables\nint N, M, K, R;\nvector<vector<int>> task_difficulty;  // d[i][k]\nvector<vector<int>> task_dependencies; // dependencies for each task\nvector<vector<int>> reverse_dependencies; // which tasks depend on this task\nvector<vector<int>> completion_times; // actual completion times (hidden)\nvector<vector<int>> skill_estimates;  // s[j][k] - our estimates\nvector<int> task_status;  // -1: not started, 0: in progress, 1: completed\nvector<int> member_current_task; // which task each member is working on\nvector<int> task_start_day; // when each task started\nvector<int> in_degree; // in-degree for topological sorting\nvector<bool> task_available; // whether task can be started (dependencies done)\npriority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> task_queue; // priority queue for tasks\n\n// Initialize data structures\nvoid initialize() {\n    task_difficulty.assign(N, vector<int>(K));\n    task_dependencies.assign(N, vector<int>());\n    reverse_dependencies.assign(N, vector<int>());\n    completion_times.assign(N, vector<int>(M));\n    skill_estimates.assign(M, vector<int>(K, 10)); // initial guess\n    task_status.assign(N, -1);\n    member_current_task.assign(M, -1);\n    task_start_day.assign(N, -1);\n    in_degree.assign(N, 0);\n    task_available.assign(N, false);\n}\n\n// Calculate estimated time for member j to complete task i\nint estimate_time(int task_id, int member_id) {\n    int w = 0;\n    for (int k = 0; k < K; k++) {\n        w += max(0, task_difficulty[task_id][k] - skill_estimates[member_id][k]);\n    }\n    if (w == 0) return 1;\n    return max(1, w + 1); // conservative estimate (middle of random range)\n}\n\n// Update skill estimates based on actual performance\nvoid update_skills(int task_id, int member_id, int actual_time, int days_taken) {\n    // Only update if we have meaningful data\n    if (days_taken <= 0) return;\n    \n    int w = 0;\n    for (int k = 0; k < K; k++) {\n        w += max(0, task_difficulty[task_id][k] - skill_estimates[member_id][k]);\n    }\n    \n    // Estimate actual w based on observed time\n    int estimated_w = (actual_time == 1) ? 0 : (actual_time - 1); // reverse engineer w\n    \n    // Adjust skills based on difference\n    if (w > estimated_w) {\n        // Member is better than we thought - increase skill estimates\n        for (int k = 0; k < K; k++) {\n            if (task_difficulty[task_id][k] > skill_estimates[member_id][k]) {\n                skill_estimates[member_id][k] = min(skill_estimates[member_id][k] + 1, \n                                                   task_difficulty[task_id][k] + 5);\n            }\n        }\n    } else if (w < estimated_w) {\n        // Member is worse than we thought - but be conservative\n        // Since we don't know exact breakdown, make small adjustments\n    }\n}\n\n// Check which tasks are available to start (all dependencies completed)\nvoid update_available_tasks() {\n    for (int i = 0; i < N; i++) {\n        if (task_status[i] == -1 && !task_available[i]) {\n            bool can_start = true;\n            for (int dep : task_dependencies[i]) {\n                if (task_status[dep] != 1) {\n                    can_start = false;\n                    break;\n                }\n            }\n            if (can_start) {\n                task_available[i] = true;\n                // Priority: tasks that take longer (to get them started early)\n                int max_time = 0;\n                for (int j = 0; j < M; j++) {\n                    max_time = max(max_time, estimate_time(i, j));\n                }\n                task_queue.push({-max_time, i}); // negative for max-heap behavior\n            }\n        }\n    }\n}\n\nint main() {\n    // Read initial information\n    cin >> N >> M >> K >> R;\n    initialize();\n    \n    // Read task difficulties\n    for (int i = 0; i < N; i++) {\n        for (int k = 0; k < K; k++) {\n            cin >> task_difficulty[i][k];\n        }\n    }\n    \n    // Read dependencies\n    for (int i = 0; i < R; i++) {\n        int u, v;\n        cin >> u >> v;\n        u--; v--; // Convert to 0-indexed\n        task_dependencies[v].push_back(u);\n        reverse_dependencies[u].push_back(v);\n        in_degree[v]++;\n    }\n    \n    // Initialize available tasks (those with no dependencies)\n    update_available_tasks();\n    \n    int day = 0;\n    while (day < MAX_DAYS) {\n        day++;\n        \n        // Prepare assignments for this day\n        vector<pair<int, int>> assignments; // {member, task}\n        vector<bool> member_busy(M, false);\n        \n        // Assign tasks to free members\n        for (int member = 0; member < M; member++) {\n            if (member_current_task[member] == -1) { // Member is free\n                // Find best task to assign\n                vector<pair<int, int>> candidates; // {estimated_time, task_id}\n                \n                // Look through available tasks\n                priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> temp_queue = task_queue;\n                int checked = 0;\n                while (!task_queue.empty() && checked < 20) { // Limit search\n                    auto [neg_priority, task_id] = task_queue.top();\n                    task_queue.pop();\n                    if (task_status[task_id] == -1 && task_available[task_id]) {\n                        int est_time = estimate_time(task_id, member);\n                        candidates.push_back({est_time, task_id});\n                    }\n                    checked++;\n                }\n                \n                if (!candidates.empty()) {\n                    // Sort by estimated time (prefer shorter tasks)\n                    sort(candidates.begin(), candidates.end());\n                    int best_task = candidates[0].second;\n                    \n                    assignments.push_back({member, best_task});\n                    member_current_task[member] = best_task;\n                    task_status[best_task] = 0;\n                    task_start_day[best_task] = day;\n                }\n            }\n        }\n        \n        // Output assignments\n        cout << assignments.size();\n        for (auto& assignment : assignments) {\n            cout << \" \" << (assignment.first + 1) << \" \" << (assignment.second + 1);\n        }\n        cout << endl;\n        cout.flush();\n        \n        // Read completion information\n        int completion_count;\n        cin >> completion_count;\n        \n        if (completion_count == -1) {\n            // All tasks completed or time limit reached\n            break;\n        }\n        \n        if (completion_count == 0) {\n            // No completions, continue to next day\n            continue;\n        }\n        \n        // Process completed tasks\n        for (int i = 0; i < completion_count; i++) {\n            int member_id;\n            cin >> member_id;\n            member_id--; // Convert to 0-indexed\n            \n            int task_id = member_current_task[member_id];\n            if (task_id != -1) {\n                task_status[task_id] = 1;\n                member_current_task[member_id] = -1;\n                \n                // Update skills based on performance\n                int days_taken = day - task_start_day[task_id];\n                // We don't know actual time, but we can make educated guesses\n                update_skills(task_id, member_id, completion_times[task_id][member_id], days_taken);\n                \n                // Make dependent tasks available\n                for (int dependent : reverse_dependencies[task_id]) {\n                    in_degree[dependent]--;\n                    if (in_degree[dependent] == 0) {\n                        task_available[dependent] = true;\n                        int max_time = 0;\n                        for (int j = 0; j < M; j++) {\n                            max_time = max(max_time, estimate_time(dependent, j));\n                        }\n                        task_queue.push({-max_time, dependent});\n                    }\n                }\n            }\n        }\n        \n        // Update available tasks for next assignments\n        update_available_tasks();\n    }\n    \n    return 0;\n}","ahc006":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int DEPOT_X = 400;\nconst int DEPOT_Y = 400;\nconst int NUM_ORDERS = 1000;\nconst int SELECT_COUNT = 50;\n\nstruct Order {\n    int id, a, b, c, d;\n    \n    Order(int _id, int _a, int _b, int _c, int _d) \n        : id(_id), a(_a), b(_b), c(_c), d(_d) {}\n    \n    int distance() const {\n        return abs(a - c) + abs(b - d);\n    }\n};\n\nstruct Point {\n    int x, y;\n    Point() : x(0), y(0) {}\n    Point(int _x, int _y) : x(_x), y(_y) {}\n    \n    int distance(const Point& other) const {\n        return abs(x - other.x) + abs(y - other.y);\n    }\n};\n\n// Calculate total distance of a route\nlong long calculateRouteDistance(const vector<Point>& route) {\n    long long total = 0;\n    for (size_t i = 0; i + 1 < route.size(); i++) {\n        total += route[i].distance(route[i + 1]);\n    }\n    return total;\n}\n\n// Select orders based on a combination of distance and centrality\nvector<int> selectOrders(const vector<Order>& orders) {\n    vector<pair<int, int>> scores; // {score, id}\n    \n    for (int i = 0; i < NUM_ORDERS; i++) {\n        const Order& order = orders[i];\n        \n        // Direct distance\n        int order_dist = order.distance();\n        \n        // Distance from depot to the \"center\" of the order\n        int center_x = (order.a + order.c) / 2;\n        int center_y = (order.b + order.d) / 2;\n        int depot_dist = abs(DEPOT_X - center_x) + abs(DEPOT_Y - center_y);\n        \n        // Score: prefer orders that are not too far from depot but have reasonable distance\n        // Lower score is better\n        int score = order_dist + depot_dist / 2;\n        \n        scores.push_back({score, i});\n    }\n    \n    sort(scores.begin(), scores.end());\n    \n    vector<int> selected;\n    for (int i = 0; i < SELECT_COUNT; i++) {\n        selected.push_back(scores[i].second);\n    }\n    \n    return selected;\n}\n\n// Simple insertion heuristic for TSP with pickup-delivery constraints\nvector<Point> buildRoute(const vector<Order>& orders, const vector<int>& selected_ids) {\n    vector<Point> route = {Point(DEPOT_X, DEPOT_Y)};\n    \n    // For each selected order, we need to insert both pickup and delivery\n    vector<pair<Point, Point>> order_points; // {pickup, delivery}\n    for (int id : selected_ids) {\n        order_points.push_back({Point(orders[id].a, orders[id].b), Point(orders[id].c, orders[id].d)});\n    }\n    \n    // Insert orders one by one using cheapest insertion\n    vector<bool> inserted(selected_ids.size(), false);\n    vector<int> order_sequence; // sequence of inserted order indices\n    \n    for (int i = 0; i < (int)selected_ids.size(); i++) {\n        int best_order = -1;\n        int best_pos = -1;\n        int best_cost = 1e9;\n        \n        // Try inserting each remaining order\n        for (size_t j = 0; j < selected_ids.size(); j++) {\n            if (inserted[j]) continue;\n            \n            // Try all positions to insert this order (pickup followed by delivery)\n            for (size_t pos = 0; pos < route.size(); pos++) {\n                // Insert pickup at pos, delivery at some position after pos\n                for (size_t delivery_pos = pos + 1; delivery_pos <= route.size(); delivery_pos++) {\n                    Point pickup = order_points[j].first;\n                    Point delivery = order_points[j].second;\n                    \n                    // Calculate insertion cost\n                    int cost = 0;\n                    \n                    // Cost to insert pickup\n                    if (pos == 0) {\n                        cost += route[0].distance(pickup);\n                    } else {\n                        cost -= route[pos-1].distance(route[pos]);\n                        cost += route[pos-1].distance(pickup);\n                        cost += pickup.distance(route[pos]);\n                    }\n                    \n                    // Cost to insert delivery\n                    if (delivery_pos == route.size()) {\n                        cost += route.back().distance(delivery);\n                    } else {\n                        cost -= route[delivery_pos-1].distance(route[delivery_pos]);\n                        cost += route[delivery_pos-1].distance(delivery);\n                        cost += delivery.distance(route[delivery_pos]);\n                    }\n                    \n                    if (cost < best_cost) {\n                        best_cost = cost;\n                        best_order = j;\n                        best_pos = pos;\n                    }\n                }\n            }\n        }\n        \n        if (best_order != -1) {\n            // Actually insert the order\n            Point pickup = order_points[best_order].first;\n            Point delivery = order_points[best_order].second;\n            \n            // Simple approach: insert pickup at beginning and delivery at end\n            // This is faster and still reasonable\n            route.insert(route.begin() + 1, pickup);\n            route.insert(route.end() - 1, delivery);\n            \n            inserted[best_order] = true;\n            order_sequence.push_back(best_order);\n        }\n    }\n    \n    // Much simpler approach: just append all pickups, then all deliveries\n    route.clear();\n    route.push_back(Point(DEPOT_X, DEPOT_Y));\n    \n    // Add all pickups first\n    for (size_t i = 0; i < selected_ids.size(); i++) {\n        route.push_back(order_points[i].first);\n    }\n    \n    // Add all deliveries\n    for (size_t i = 0; i < selected_ids.size(); i++) {\n        route.push_back(order_points[i].second);\n    }\n    \n    route.push_back(Point(DEPOT_X, DEPOT_Y));\n    \n    return route;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    vector<Order> orders;\n    \n    // Read input\n    for (int i = 0; i < NUM_ORDERS; i++) {\n        int a, b, c, d;\n        cin >> a >> b >> c >> d;\n        orders.emplace_back(i, a, b, c, d);\n    }\n    \n    // Select orders\n    vector<int> selected_ids = selectOrders(orders);\n    \n    // Build route\n    vector<Point> route = buildRoute(orders, selected_ids);\n    \n    // Output\n    cout << SELECT_COUNT;\n    for (int id : selected_ids) {\n        cout << \" \" << (id + 1); // 1-indexed\n    }\n    cout << \"\\n\";\n    \n    cout << route.size();\n    for (const Point& p : route) {\n        cout << \" \" << p.x << \" \" << p.y;\n    }\n    cout << \"\\n\";\n    \n    return 0;\n}","ahc007":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 400;\nconst int M = 1995;\n\nclass UnionFind {\nprivate:\n    vector<int> parent, rank;\n    vector<int> component_size;\npublic:\n    UnionFind(int n) : parent(n), rank(n, 0), component_size(n, 1) {\n        for (int i = 0; i < n; i++) {\n            parent[i] = i;\n        }\n    }\n    \n    int find(int x) {\n        if (parent[x] != x) {\n            parent[x] = find(parent[x]);\n        }\n        return parent[x];\n    }\n    \n    bool unite(int x, int y) {\n        x = find(x);\n        y = find(y);\n        if (x == y) return false;\n        if (rank[x] < rank[y]) swap(x, y);\n        parent[y] = x;\n        component_size[x] += component_size[y];\n        if (rank[x] == rank[y]) rank[x]++;\n        return true;\n    }\n    \n    bool same(int x, int y) {\n        return find(x) == find(y);\n    }\n    \n    int get_component_size(int x) {\n        return component_size[find(x)];\n    }\n    \n    int count_components() {\n        int count = 0;\n        for (int i = 0; i < N; i++) {\n            if (find(i) == i) count++;\n        }\n        return count;\n    }\n};\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    // Read coordinates\n    vector<pair<int, int>> coords(N);\n    for (int i = 0; i < N; i++) {\n        cin >> coords[i].first >> coords[i].second;\n    }\n    \n    // Read edges\n    vector<pair<int, int>> edges(M);\n    for (int i = 0; i < M; i++) {\n        cin >> edges[i].first >> edges[i].second;\n    }\n    \n    // Precompute minimum distances\n    vector<int> min_distances(M);\n    for (int i = 0; i < M; i++) {\n        int u = edges[i].first;\n        int v = edges[i].second;\n        long long dx = (long long)(coords[u].first - coords[v].first);\n        long long dy = (long long)(coords[u].second - coords[v].second);\n        long long dist_sq = dx * dx + dy * dy;\n        double dist = sqrt((double)dist_sq);\n        min_distances[i] = max(1, (int)round(dist));\n    }\n    \n    // Initialize Union-Find\n    UnionFind uf(N);\n    \n    // Track statistics\n    int total_accepted = 0;\n    \n    // Process edges as they arrive\n    for (int i = 0; i < M; i++) {\n        int length;\n        cin >> length;\n        \n        int u = edges[i].first;\n        int v = edges[i].second;\n        int min_dist = min_distances[i];\n        \n        // Check if this edge connects two different components\n        if (!uf.same(u, v)) {\n            // Essential for connectivity - accept\n            uf.unite(u, v);\n            total_accepted++;\n            cout << \"1\" << endl;\n            cout.flush();\n        } else {\n            // Creating a cycle - decide based on value\n            double ratio = (double)length / min_dist;\n            \n            // Get component information\n            int comp_size = uf.get_component_size(u); // Same for both u and v since they're connected\n            \n            // Calculate critical metrics\n            int current_components = uf.count_components();\n            int edges_remaining = M - i - 1;\n            int components_to_connect = current_components - 1;\n            \n            // Progress through the algorithm\n            double progress = (double)i / M;\n            \n            bool accept = false;\n            \n            // Strategy: Value-based decision making\n            if (progress < 0.4) {\n                // Early phase - be very selective\n                if (ratio <= 1.05) {\n                    accept = true;\n                } else if (ratio <= 1.15 && min_dist <= 15) {\n                    accept = true;\n                } else if (ratio <= 1.25 && min_dist <= 5) {\n                    accept = true;\n                }\n            } else if (progress < 0.7) {\n                // Mid phase - balanced approach\n                if (ratio <= 1.1) {\n                    accept = true;\n                } else if (ratio <= 1.25 && min_dist <= 20) {\n                    accept = true;\n                } else if (ratio <= 1.4 && min_dist <= 10) {\n                    accept = true;\n                } else if (ratio <= 1.6 && min_dist <= 5) {\n                    accept = true;\n                }\n            } else {\n                // Late phase - more aggressive if needed\n                // Check if we're running dangerously low on edges\n                bool edge_critical = (edges_remaining <= components_to_connect + 20);\n                \n                if (edge_critical) {\n                    // We need to be more aggressive\n                    if (ratio <= 1.5) {\n                        accept = true;\n                    } else if (ratio <= 1.8 && min_dist <= 15) {\n                        accept = true;\n                    } else if (ratio <= 2.0 && min_dist <= 8) {\n                        accept = true;\n                    }\n                } else {\n                    // Normal late game criteria\n                    if (ratio <= 1.2) {\n                        accept = true;\n                    } else if (ratio <= 1.4 && min_dist <= 25) {\n                        accept = true;\n                    } else if (ratio <= 1.6 && min_dist <= 12) {\n                        accept = true;\n                    } else if (ratio <= 1.9 && min_dist <= 6) {\n                        accept = true;\n                    }\n                }\n            }\n            \n            if (accept) {\n                cout << \"1\" << endl;\n                cout.flush();\n                total_accepted++;\n            } else {\n                cout << \"0\" << endl;\n                cout.flush();\n            }\n        }\n    }\n    \n    return 0;\n}","ahc008":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int GRID_SIZE = 30;\nconst int MAX_TURNS = 300;\nconst int dx[4] = {-1, 1, 0, 0}; // U, D, L, R\nconst int dy[4] = {0, 0, -1, 1};\nconst char dir_chars[4] = {'u', 'd', 'l', 'r'};\nconst char move_chars[4] = {'U', 'D', 'L', 'R'};\n\nstruct Position {\n    int x, y;\n    Position() : x(0), y(0) {}\n    Position(int _x, int _y) : x(_x), y(_y) {}\n    bool operator==(const Position& other) const {\n        return x == other.x && y == other.y;\n    }\n};\n\nstruct Pet {\n    Position pos;\n    int type;\n    \n    Pet() : pos(0, 0), type(0) {}\n    Pet(int x, int y, int t) : pos(x, y), type(t) {}\n};\n\nstruct Human {\n    Position pos;\n    \n    Human() : pos(0, 0) {}\n    Human(int x, int y) : pos(x, y) {}\n};\n\nclass Grid {\npublic:\n    bool blocked[GRID_SIZE][GRID_SIZE];\n    bool pet_present[GRID_SIZE][GRID_SIZE];\n    bool human_present[GRID_SIZE][GRID_SIZE];\n    \n    Grid() {\n        memset(blocked, false, sizeof(blocked));\n        memset(pet_present, false, sizeof(pet_present));\n        memset(human_present, false, sizeof(human_present));\n    }\n    \n    void reset_presence() {\n        memset(pet_present, false, sizeof(pet_present));\n        memset(human_present, false, sizeof(human_present));\n    }\n    \n    bool is_valid(int x, int y) {\n        return x >= 0 && x < GRID_SIZE && y >= 0 && y < GRID_SIZE;\n    }\n    \n    bool is_passable(int x, int y) {\n        return is_valid(x, y) && !blocked[x][y];\n    }\n    \n    void block_square(int x, int y) {\n        if (is_valid(x, y)) {\n            blocked[x][y] = true;\n        }\n    }\n};\n\nclass Solver {\nprivate:\n    int N, M;\n    vector<Pet> pets;\n    vector<Human> humans;\n    Grid grid;\n    mt19937 rng;\n    \npublic:\n    Solver() : rng(chrono::steady_clock::now().time_since_epoch().count()) {}\n    \n    void read_input() {\n        cin >> N;\n        pets.resize(N);\n        for (int i = 0; i < N; i++) {\n            int x, y, t;\n            cin >> x >> y >> t;\n            pets[i] = Pet(x-1, y-1, t); // Convert to 0-indexed\n        }\n        \n        cin >> M;\n        humans.resize(M);\n        for (int i = 0; i < M; i++) {\n            int x, y;\n            cin >> x >> y;\n            humans[i] = Human(x-1, y-1); // Convert to 0-indexed\n        }\n    }\n    \n    bool is_adjacent_to_pet(int x, int y) {\n        for (int d = 0; d < 4; d++) {\n            int nx = x + dx[d];\n            int ny = y + dy[d];\n            if (grid.is_valid(nx, ny)) {\n                for (const auto& pet : pets) {\n                    if (pet.pos.x == nx && pet.pos.y == ny) {\n                        return true;\n                    }\n                }\n            }\n        }\n        return false;\n    }\n    \n    bool is_adjacent_to_any_pet(int x, int y) {\n        if (!grid.is_valid(x, y)) return true;\n        \n        for (const auto& pet : pets) {\n            if (abs(pet.pos.x - x) + abs(pet.pos.y - y) <= 1) {\n                return true;\n            }\n        }\n        return false;\n    }\n    \n    string get_action(int human_id) {\n        Human& human = humans[human_id];\n        \n        // Check if we're adjacent to any pet - if so, better not block\n        if (is_adjacent_to_any_pet(human.pos.x, human.pos.y)) {\n            // Try to move away from pets\n            for (int d = 0; d < 4; d++) {\n                int nx = human.pos.x + dx[d];\n                int ny = human.pos.y + dy[d];\n                \n                if (grid.is_passable(nx, ny) && !is_adjacent_to_any_pet(nx, ny)) {\n                    return string(1, move_chars[d]);\n                }\n            }\n            return \".\"; // Stay put if no good move\n        }\n        \n        // Try to block squares that are adjacent to pets but not adjacent to any pet currently\n        for (int d = 0; d < 4; d++) {\n            int nx = human.pos.x + dx[d];\n            int ny = human.pos.y + dy[d];\n            \n            if (grid.is_valid(nx, ny) && !grid.blocked[nx][ny]) {\n                // Check if this square is adjacent to any pet\n                bool adjacent_to_pet = false;\n                for (const auto& pet : pets) {\n                    if (abs(pet.pos.x - nx) + abs(pet.pos.y - ny) <= 1) {\n                        adjacent_to_pet = true;\n                        break;\n                    }\n                }\n                \n                // But make sure it's not adjacent to the pet we're blocking (to avoid blocking pet itself)\n                if (adjacent_to_pet && !is_adjacent_to_any_pet(nx, ny)) {\n                    return string(1, dir_chars[d]); // Block this square\n                }\n            }\n        }\n        \n        // If no good blocking move, try to move to a strategic position\n        // Prefer moving towards areas with more pets but not too close\n        vector<pair<int, int>> candidates;\n        for (int d = 0; d < 4; d++) {\n            int nx = human.pos.x + dx[d];\n            int ny = human.pos.y + dy[d];\n            \n            if (grid.is_passable(nx, ny) && !is_adjacent_to_any_pet(nx, ny)) {\n                // Count nearby pets\n                int nearby_pets = 0;\n                for (const auto& pet : pets) {\n                    if (abs(pet.pos.x - nx) + abs(pet.pos.y - ny) <= 3) {\n                        nearby_pets++;\n                    }\n                }\n                candidates.push_back({nearby_pets, d});\n            }\n        }\n        \n        if (!candidates.empty()) {\n            sort(candidates.begin(), candidates.end(), greater<pair<int, int>>());\n            return string(1, move_chars[candidates[0].second]);\n        }\n        \n        // Default: do nothing\n        return \".\";\n    }\n    \n    void update_grid_state(const string& actions) {\n        // Process blocking actions first\n        for (int i = 0; i < M; i++) {\n            char action = actions[i];\n            if (action == 'u' || action == 'd' || action == 'l' || action == 'r') {\n                int d = -1;\n                for (int j = 0; j < 4; j++) {\n                    if (dir_chars[j] == action) {\n                        d = j;\n                        break;\n                    }\n                }\n                if (d != -1) {\n                    int nx = humans[i].pos.x + dx[d];\n                    int ny = humans[i].pos.y + dy[d];\n                    if (grid.is_valid(nx, ny)) {\n                        grid.block_square(nx, ny);\n                    }\n                }\n            }\n        }\n        \n        // Then process movement actions\n        for (int i = 0; i < M; i++) {\n            char action = actions[i];\n            if (action == 'U' || action == 'D' || action == 'L' || action == 'R') {\n                int d = -1;\n                for (int j = 0; j < 4; j++) {\n                    if (move_chars[j] == action) {\n                        d = j;\n                        break;\n                    }\n                }\n                if (d != -1) {\n                    int nx = humans[i].pos.x + dx[d];\n                    int ny = humans[i].pos.y + dy[d];\n                    if (grid.is_passable(nx, ny)) {\n                        humans[i].pos.x = nx;\n                        humans[i].pos.y = ny;\n                    }\n                }\n            }\n        }\n    }\n    \n    void update_positions(const vector<string>& pet_moves) {\n        // Update pet positions based on their moves\n        for (int i = 0; i < N; i++) {\n            const string& moves = pet_moves[i];\n            for (char move : moves) {\n                if (move == 'U' && pets[i].pos.x > 0) pets[i].pos.x--;\n                else if (move == 'D' && pets[i].pos.x < GRID_SIZE - 1) pets[i].pos.x++;\n                else if (move == 'L' && pets[i].pos.y > 0) pets[i].pos.y--;\n                else if (move == 'R' && pets[i].pos.y < GRID_SIZE - 1) pets[i].pos.y++;\n            }\n        }\n    }\n    \n    void update_grid_presence() {\n        grid.reset_presence();\n        for (const auto& pet : pets) {\n            if (grid.is_valid(pet.pos.x, pet.pos.y)) {\n                grid.pet_present[pet.pos.x][pet.pos.y] = true;\n            }\n        }\n        for (const auto& human : humans) {\n            if (grid.is_valid(human.pos.x, human.pos.y)) {\n                grid.human_present[human.pos.x][human.pos.y] = true;\n            }\n        }\n    }\n    \n    void solve() {\n        read_input();\n        \n        // Initialize grid presence\n        update_grid_presence();\n        \n        for (int turn = 0; turn < MAX_TURNS; turn++) {\n            string actions = \"\";\n            for (int i = 0; i < M; i++) {\n                actions += get_action(i);\n            }\n            cout << actions << endl;\n            cout.flush();\n            \n            // Update grid based on human actions\n            update_grid_state(actions);\n            \n            // Read pet moves\n            vector<string> pet_moves(N);\n            for (int i = 0; i < N; i++) {\n                cin >> pet_moves[i];\n            }\n            \n            // Update positions\n            update_positions(pet_moves);\n            \n            // Update grid presence\n            update_grid_presence();\n        }\n    }\n};\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    Solver solver;\n    solver.solve();\n    \n    return 0;\n}","ahc009":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int SIZE = 20;\nconst int MAX_LENGTH = 200;\n\n// Directions: U, D, L, R\nconst int dr[] = {-1, 1, 0, 0};\nconst int dc[] = {0, 0, -1, 1};\nconst char dir_chars[] = {'U', 'D', 'L', 'R'};\n\nint si, sj, ti, tj;\ndouble p;\nbool h_walls[SIZE][SIZE-1]; // horizontal walls\nbool v_walls[SIZE-1][SIZE]; // vertical walls\n\nbool can_move(int r, int c, int dir) {\n    if (dir == 0) { // Up\n        if (r == 0) return false;\n        return !v_walls[r-1][c];\n    } else if (dir == 1) { // Down\n        if (r == SIZE-1) return false;\n        return !v_walls[r][c];\n    } else if (dir == 2) { // Left\n        if (c == 0) return false;\n        return !h_walls[r][c-1];\n    } else { // Right\n        if (c == SIZE-1) return false;\n        return !h_walls[r][c];\n    }\n}\n\n// Find shortest path using BFS\nstring find_shortest_path() {\n    queue<tuple<int, int, string>> q;\n    bool visited[SIZE][SIZE] = {false};\n    \n    q.push({si, sj, \"\"});\n    visited[si][sj] = true;\n    \n    while (!q.empty()) {\n        auto [r, c, path] = q.front();\n        q.pop();\n        \n        if (r == ti && c == tj) {\n            return path;\n        }\n        \n        if (path.length() > MAX_LENGTH) continue;\n        \n        // Try all 4 directions\n        for (int dir = 0; dir < 4; dir++) {\n            int nr = r + dr[dir];\n            int nc = c + dc[dir];\n            \n            if (nr >= 0 && nr < SIZE && nc >= 0 && nc < SIZE && !visited[nr][nc]) {\n                if (can_move(r, c, dir)) {\n                    visited[nr][nc] = true;\n                    string new_path = path + dir_chars[dir];\n                    if (new_path.length() <= MAX_LENGTH) {\n                        q.push({nr, nc, new_path});\n                    }\n                }\n            }\n        }\n    }\n    \n    return \"\"; // Should not happen\n}\n\n// Create a robust path by repeating segments\nstring create_robust_path() {\n    string base_path = find_shortest_path();\n    \n    // If base path is already quite long, return as is\n    if (base_path.length() > 150) {\n        return base_path;\n    }\n    \n    // Create a more robust version by repeating the path\n    string robust_path = \"\";\n    \n    // First, add the base path\n    robust_path += base_path;\n    \n    // Then add it again for redundancy (but be smart about it)\n    if (robust_path.length() + base_path.length() <= MAX_LENGTH) {\n        robust_path += base_path;\n    }\n    \n    // If we still have room, add a simple pattern\n    while (robust_path.length() + 4 <= MAX_LENGTH) {\n        // Add a simple back-and-forth pattern that should help with robustness\n        if (ti > si && tj > sj) {\n            // If target is southeast, add some DR patterns\n            if (robust_path.length() + 4 <= MAX_LENGTH) {\n                robust_path += \"DRRD\";\n            }\n        } else if (ti > si && tj < sj) {\n            // If target is southwest, add some DL patterns\n            if (robust_path.length() + 4 <= MAX_LENGTH) {\n                robust_path += \"DLLD\";\n            }\n        } else if (ti < si && tj > sj) {\n            // If target is northeast, add some UR patterns\n            if (robust_path.length() + 4 <= MAX_LENGTH) {\n                robust_path += \"URRU\";\n            }\n        } else if (ti < si && tj < sj) {\n            // If target is northwest, add some UL patterns\n            if (robust_path.length() + 4 <= MAX_LENGTH) {\n                robust_path += \"ULLU\";\n            }\n        } else {\n            // Default pattern\n            if (robust_path.length() + 4 <= MAX_LENGTH) {\n                robust_path += \"DRRD\";\n            }\n        }\n        \n        // Break if getting too long\n        if (robust_path.length() >= MAX_LENGTH - 10) break;\n    }\n    \n    // Ensure we don't exceed limit\n    if (robust_path.length() > MAX_LENGTH) {\n        robust_path = robust_path.substr(0, MAX_LENGTH);\n    }\n    \n    return robust_path;\n}\n\n// Create a path based on grid movement pattern\nstring create_grid_path() {\n    string path = \"\";\n    \n    // Move towards target with redundancy\n    int dr_target = ti - si;\n    int dc_target = tj - sj;\n    \n    // Create a path that goes in the general direction but with redundancy\n    int r = si, c = sj;\n    \n    // Move step by step towards target with some redundancy\n    while ((r != ti || c != tj) && path.length() < MAX_LENGTH) {\n        int dr_current = ti - r;\n        int dc_current = tj - c;\n        \n        // Prefer moving in the direction that gets us closer\n        if (abs(dr_current) >= abs(dc_current) && dr_current != 0) {\n            // Move vertically\n            if (dr_current > 0) {\n                // Move down\n                if (can_move(r, c, 1)) {\n                    path += 'D';\n                    r++;\n                } else {\n                    // Try horizontal movement instead\n                    if (dc_current > 0 && can_move(r, c, 3)) {\n                        path += 'R';\n                        c++;\n                    } else if (dc_current < 0 && can_move(r, c, 2)) {\n                        path += 'L';\n                        c--;\n                    } else {\n                        path += 'D'; // Still try, even if blocked\n                    }\n                }\n            } else {\n                // Move up\n                if (can_move(r, c, 0)) {\n                    path += 'U';\n                    r--;\n                } else {\n                    // Try horizontal movement instead\n                    if (dc_current > 0 && can_move(r, c, 3)) {\n                        path += 'R';\n                        c++;\n                    } else if (dc_current < 0 && can_move(r, c, 2)) {\n                        path += 'L';\n                        c--;\n                    } else {\n                        path += 'U'; // Still try, even if blocked\n                    }\n                }\n            }\n        } else if (dc_current != 0) {\n            // Move horizontally\n            if (dc_current > 0) {\n                // Move right\n                if (can_move(r, c, 3)) {\n                    path += 'R';\n                    c++;\n                } else {\n                    // Try vertical movement instead\n                    if (dr_current > 0 && can_move(r, c, 1)) {\n                        path += 'D';\n                        r++;\n                    } else if (dr_current < 0 && can_move(r, c, 0)) {\n                        path += 'U';\n                        r--;\n                    } else {\n                        path += 'R'; // Still try, even if blocked\n                    }\n                }\n            } else {\n                // Move left\n                if (can_move(r, c, 2)) {\n                    path += 'L';\n                    c--;\n                } else {\n                    // Try vertical movement instead\n                    if (dr_current > 0 && can_move(r, c, 1)) {\n                        path += 'D';\n                        r++;\n                    } else if (dr_current < 0 && can_move(r, c, 0)) {\n                        path += 'U';\n                        r--;\n                    } else {\n                        path += 'L'; // Still try, even if blocked\n                    }\n                }\n            }\n        } else {\n            // We're at target\n            break;\n        }\n        \n        // Add some redundancy every few steps\n        if (path.length() % 10 == 7 && path.length() + 4 <= MAX_LENGTH) {\n            // Add a small redundant pattern\n            if (path.back() == 'R' && path.length() + 2 <= MAX_LENGTH) {\n                path += \"LR\"; // Right then left (should cancel)\n            } else if (path.back() == 'L' && path.length() + 2 <= MAX_LENGTH) {\n                path += \"RL\";\n            } else if (path.back() == 'U' && path.length() + 2 <= MAX_LENGTH) {\n                path += \"DU\";\n            } else if (path.back() == 'D' && path.length() + 2 <= MAX_LENGTH) {\n                path += \"UD\";\n            }\n        }\n    }\n    \n    // If we still have room, add the shortest path from current position to target\n    if (path.length() < MAX_LENGTH) {\n        // Simple approach: just fill with moves towards target\n        while (r != ti && path.length() < MAX_LENGTH) {\n            if (r < ti && path.length() < MAX_LENGTH) {\n                path += 'D';\n                r++;\n            } else if (r > ti && path.length() < MAX_LENGTH) {\n                path += 'U';\n                r--;\n            }\n        }\n        while (c != tj && path.length() < MAX_LENGTH) {\n            if (c < tj && path.length() < MAX_LENGTH) {\n                path += 'R';\n                c++;\n            } else if (c > tj && path.length() < MAX_LENGTH) {\n                path += 'L';\n                c--;\n            }\n        }\n    }\n    \n    if (path.length() > MAX_LENGTH) {\n        path = path.substr(0, MAX_LENGTH);\n    }\n    \n    return path;\n}\n\n// Create a very robust path by using repetitive patterns\nstring create_repetitive_path() {\n    string path = \"\";\n    \n    // Create a path with lots of redundancy\n    int dr_target = ti - si;\n    int dc_target = tj - sj;\n    \n    // Use a snake-like pattern for robustness\n    bool horizontal_first = (abs(dc_target) >= abs(dr_target));\n    \n    if (horizontal_first) {\n        // Move horizontally first with redundancy\n        int c = sj;\n        while (c != tj && path.length() < MAX_LENGTH - 5) {\n            if (c < tj) {\n                path += \"RR\";\n                c += 2;\n                if (c > tj) c = tj;\n            } else {\n                path += \"LL\";\n                c -= 2;\n                if (c < tj) c = tj;\n            }\n            \n            // Add some vertical redundancy\n            if (path.length() < MAX_LENGTH - 5) {\n                path += \"DU\"; // Up then down (should cancel)\n            }\n        }\n        \n        // Then move vertically\n        int r = si;\n        while (r != ti && path.length() < MAX_LENGTH - 2) {\n            if (r < ti) {\n                path += \"DD\";\n                r += 2;\n                if (r > ti) r = ti;\n            } else {\n                path += \"UU\";\n                r -= 2;\n                if (r < ti) r = ti;\n            }\n        }\n    } else {\n        // Move vertically first\n        int r = si;\n        while (r != ti && path.length() < MAX_LENGTH - 5) {\n            if (r < ti) {\n                path += \"DD\";\n                r += 2;\n                if (r > ti) r = ti;\n            } else {\n                path += \"UU\";\n                r -= 2;\n                if (r < ti) r = ti;\n            }\n            \n            // Add some horizontal redundancy\n            if (path.length() < MAX_LENGTH - 5) {\n                path += \"LR\"; // Left then right (should cancel)\n            }\n        }\n        \n        // Then move horizontally\n        int c = sj;\n        while (c != tj && path.length() < MAX_LENGTH - 2) {\n            if (c < tj) {\n                path += \"RR\";\n                c += 2;\n                if (c > tj) c = tj;\n            } else {\n                path += \"LL\";\n                c -= 2;\n                if (c < tj) c = tj;\n            }\n        }\n    }\n    \n    if (path.length() > MAX_LENGTH) {\n        path = path.substr(0, MAX_LENGTH);\n    }\n    \n    return path;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    // Read input\n    cin >> si >> sj >> ti >> tj >> p;\n    \n    // Read horizontal walls\n    for (int i = 0; i < SIZE; i++) {\n        string line;\n        cin >> line;\n        for (int j = 0; j < SIZE-1; j++) {\n            h_walls[i][j] = (line[j] == '1');\n        }\n    }\n    \n    // Read vertical walls\n    for (int i = 0; i < SIZE-1; i++) {\n        string line;\n        cin >> line;\n        for (int j = 0; j < SIZE; j++) {\n            v_walls[i][j] = (line[j] == '1');\n        }\n    }\n    \n    // Try different approaches and pick the best one\n    string path1 = find_shortest_path();\n    string path2 = create_robust_path();\n    string path3 = create_grid_path();\n    string path4 = create_repetitive_path();\n    \n    // Choose the longest one that's under the limit (more robust)\n    string best_path = path1;\n    if (path2.length() > best_path.length() && path2.length() <= MAX_LENGTH) {\n        best_path = path2;\n    }\n    if (path3.length() > best_path.length() && path3.length() <= MAX_LENGTH) {\n        best_path = path3;\n    }\n    if (path4.length() > best_path.length() && path4.length() <= MAX_LENGTH) {\n        best_path = path4;\n    }\n    \n    // Ensure we don't exceed the limit\n    if (best_path.length() > MAX_LENGTH) {\n        best_path = best_path.substr(0, MAX_LENGTH);\n    }\n    \n    cout << best_path << endl;\n    \n    return 0;\n}","ahc010":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 30;\n\n// Direction: 0=left, 1=up, 2=right, 3=down\nconst int di[4] = {0, -1, 0, 1};\nconst int dj[4] = {-1, 0, 1, 0};\n\n// to[tile_type][direction_entering] = direction_exiting (-1 if invalid)\nconst int to[8][4] = {\n    {1, 0, -1, -1}, // tile 0\n    {3, -1, -1, 0}, // tile 1\n    {-1, -1, 3, 2}, // tile 2\n    {-1, 2, 1, -1}, // tile 3\n    {1, 0, 3, 2},   // tile 4\n    {3, 2, 1, 0},   // tile 5\n    {2, -1, 0, -1}, // tile 6\n    {-1, 3, -1, 1}  // tile 7\n};\n\nint grid[N][N];\nint rotations[N][N];\n\n// Get effective tile after rotation\nint getEffectiveTile(int baseTile, int rot) {\n    // Tile 0: 0->1->2->3->0\n    // Tile 1: 1->2->3->0->1\n    // Tile 2: 2->3->0->1->2\n    // Tile 3: 3->0->1->2->3\n    // Tile 4: 4->5->4->5->4\n    // Tile 5: 5->4->5->4->5\n    // Tile 6: 6->7->6->7->6\n    // Tile 7: 7->6->7->6->7\n    \n    if (baseTile <= 3) {\n        return (baseTile + rot) % 4;\n    } else if (baseTile == 4) {\n        return (rot % 2 == 0) ? 4 : 5;\n    } else if (baseTile == 5) {\n        return (rot % 2 == 0) ? 5 : 4;\n    } else if (baseTile == 6) {\n        return (rot % 2 == 0) ? 6 : 7;\n    } else { // baseTile == 7\n        return (rot % 2 == 0) ? 7 : 6;\n    }\n}\n\n// Find cycle length starting from (i,j) with entry direction d\nint findCycle(vector<vector<int>>& effGrid, vector<vector<vector<bool>>>& visited, int si, int sj, int sd) {\n    int i = si, j = sj, d = sd;\n    int length = 0;\n    \n    // Check if initial move is valid\n    int d2 = to[effGrid[i][j]][d];\n    if (d2 == -1) return 0;\n    \n    do {\n        // If already visited this (pos,dir) in current trace, not a new cycle\n        if (visited[i][j][d]) return 0;\n        \n        d2 = to[effGrid[i][j]][d];\n        if (d2 == -1) return 0;\n        \n        visited[i][j][d] = true;\n        \n        // Move to next position\n        i += di[d2];\n        j += dj[d2];\n        length++;\n        \n        // Check bounds\n        if (i < 0 || i >= N || j < 0 || j >= N) return 0;\n        \n        // New direction is opposite of exit direction\n        d = (d2 + 2) % 4;\n        \n        // Prevent infinite loops\n        if (length > 10000) return 0;\n        \n    } while (!(i == si && j == sj && d == sd));\n    \n    return length;\n}\n\n// Find all cycles\nvector<int> findCycles() {\n    // Create effective grid\n    vector<vector<int>> effGrid(N, vector<int>(N));\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            effGrid[i][j] = getEffectiveTile(grid[i][j], rotations[i][j]);\n        }\n    }\n    \n    vector<vector<vector<bool>>> visited(N, vector<vector<bool>>(N, vector<bool>(4, false)));\n    vector<int> cycles;\n    \n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            for (int d = 0; d < 4; d++) {\n                if (!visited[i][j][d]) {\n                    int cycleLen = findCycle(effGrid, visited, i, j, d);\n                    if (cycleLen > 0) {\n                        cycles.push_back(cycleLen);\n                    }\n                }\n            }\n        }\n    }\n    \n    sort(cycles.begin(), cycles.end(), greater<int>());\n    return cycles;\n}\n\nlong long getScore() {\n    vector<int> cycles = findCycles();\n    if (cycles.size() < 2) return 0;\n    return 1LL * cycles[0] * cycles[1];\n}\n\nint main() {\n    // Read input\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cin >> grid[i][j];\n            rotations[i][j] = 0;\n        }\n    }\n    \n    // Simple greedy improvement\n    long long bestScore = getScore();\n    \n    // Try local search with small changes\n    for (int iter = 0; iter < 1000 && iter < N * N * 2; iter++) {\n        int i = iter / N;\n        int j = iter % N;\n        \n        if (i >= N) break;\n        \n        int oldRot = rotations[i][j];\n        long long bestLocalScore = bestScore;\n        int bestRot = oldRot;\n        \n        // Try all rotations for this position\n        for (int r = 0; r < 4; r++) {\n            rotations[i][j] = r;\n            long long score = getScore();\n            if (score > bestLocalScore) {\n                bestLocalScore = score;\n                bestRot = r;\n            }\n        }\n        \n        rotations[i][j] = bestRot;\n        if (bestLocalScore > bestScore) {\n            bestScore = bestLocalScore;\n        } else {\n            rotations[i][j] = oldRot;\n        }\n    }\n    \n    // Output\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cout << rotations[i][j];\n        }\n    }\n    cout << endl;\n    \n    return 0;\n}","ahc011":"#include <bits/stdc++.h>\nusing namespace std;\n\n// Direction constants: U=up, D=down, L=left, R=right\n// These represent the direction the EMPTY SPACE moves\nconst int UP = 0, DOWN = 1, LEFT = 2, RIGHT = 3;\nconst int dr[4] = {-1, 1, 0, 0};\nconst int dc[4] = {0, 0, -1, 1};\nconst char move_chars[4] = {'U', 'D', 'L', 'R'};\n\nstruct State {\n    vector<vector<int>> board;\n    int empty_r, empty_c;\n    string moves;\n    \n    State() {}\n    State(vector<vector<int>> b, int er, int ec, string m = \"\") \n        : board(b), empty_r(er), empty_c(ec), moves(m) {}\n};\n\n// Check if two adjacent tiles can connect\n// direction: 0=up, 1=down, 2=left, 3=right (direction from first tile to second)\nbool canConnect(int tile1, int tile2, int direction) {\n    if (direction == 0) { // up: tile1 is below tile2\n        return (tile1 & 2) && (tile2 & 8);  // tile1 has up, tile2 has down\n    } else if (direction == 1) { // down: tile1 is above tile2\n        return (tile1 & 8) && (tile2 & 2);  // tile1 has down, tile2 has up\n    } else if (direction == 2) { // left: tile1 is right of tile2\n        return (tile1 & 1) && (tile2 & 4);  // tile1 has left, tile2 has right\n    } else { // right: tile1 is left of tile2\n        return (tile1 & 4) && (tile2 & 1);  // tile1 has right, tile2 has left\n    }\n}\n\n// Find connected components and check if they form trees\npair<int, bool> findLargestTree(const vector<vector<int>>& board, int empty_r, int empty_c) {\n    int N = board.size();\n    vector<vector<bool>> visited(N, vector<bool>(N, false));\n    vector<vector<bool>> is_tile(N, vector<bool>(N, true));\n    is_tile[empty_r][empty_c] = false;\n    \n    int max_tree_size = 0;\n    bool has_complete_tree = false;\n    \n    int total_tiles = 0;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (is_tile[i][j]) total_tiles++;\n        }\n    }\n    \n    // For each unvisited tile, find its connected component\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (!visited[i][j] && is_tile[i][j]) {\n                // BFS to find connected component\n                queue<pair<int,int>> q;\n                vector<pair<int,int>> component;\n                vector<pair<pair<int,int>, pair<int,int>>> edges;\n                q.push({i, j});\n                visited[i][j] = true;\n                component.push_back({i, j});\n                \n                while (!q.empty()) {\n                    auto [r, c] = q.front(); q.pop();\n                    \n                    // Check 4 directions\n                    for (int d = 0; d < 4; d++) {\n                        int nr = r + dr[d];\n                        int nc = c + dc[d];\n                        \n                        if (nr >= 0 && nr < N && nc >= 0 && nc < N && \n                            is_tile[nr][nc]) {\n                            if (canConnect(board[r][c], board[nr][nc], d)) {\n                                edges.push_back({{r, c}, {nr, nc}});\n                                if (!visited[nr][nc]) {\n                                    visited[nr][nc] = true;\n                                    q.push({nr, nc});\n                                    component.push_back({nr, nc});\n                                }\n                            }\n                        }\n                    }\n                }\n                \n                int component_size = component.size();\n                max_tree_size = max(max_tree_size, component_size);\n                \n                // A component is a tree if it has exactly (n-1) edges for n vertices\n                if (component_size > 0 && edges.size() == component_size - 1) {\n                    // This component is a tree\n                    if (component_size == total_tiles) {\n                        has_complete_tree = true;\n                    }\n                }\n            }\n        }\n    }\n    \n    return {max_tree_size, has_complete_tree};\n}\n\n// Count total connections for heuristic\nint countConnections(const vector<vector<int>>& board, int empty_r, int empty_c) {\n    int N = board.size();\n    int connections = 0;\n    vector<vector<bool>> is_tile(N, vector<bool>(N, true));\n    is_tile[empty_r][empty_c] = false;\n    \n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (!is_tile[i][j]) continue;\n            \n            // Check connections in all 4 directions\n            for (int d = 0; d < 4; d++) {\n                int nr = i + dr[d];\n                int nc = j + dc[d];\n                \n                if (nr >= 0 && nr < N && nc >= 0 && nc < N && is_tile[nr][nc]) {\n                    if (canConnect(board[i][j], board[nr][nc], d)) {\n                        connections++;\n                    }\n                }\n            }\n        }\n    }\n    \n    return connections;\n}\n\n// Evaluate state quality with emphasis on complete trees\nint evaluate(const vector<vector<int>>& board, int empty_r, int empty_c) {\n    auto [max_tree, is_complete] = findLargestTree(board, empty_r, empty_c);\n    int connections = countConnections(board, empty_r, empty_c);\n    \n    if (is_complete) {\n        // High priority for complete trees\n        return 1000000 + max_tree;\n    }\n    \n    // Primary: largest tree size, Secondary: total connections\n    return max_tree * 1000 + connections;\n}\n\n// Generate all possible moves from current state\nvector<State> getNeighbors(const State& current, int N) {\n    vector<State> neighbors;\n    \n    // Empty space can move UP, DOWN, LEFT, RIGHT\n    for (int d = 0; d < 4; d++) {\n        int nr = current.empty_r + dr[d];\n        int nc = current.empty_c + dc[d];\n        \n        if (nr >= 0 && nr < N && nc >= 0 && nc < N) {\n            // Create new state by moving empty space in direction d\n            vector<vector<int>> new_board = current.board;\n            swap(new_board[current.empty_r][current.empty_c], new_board[nr][nc]);\n            \n            string new_moves = current.moves + move_chars[d];\n            neighbors.push_back(State(new_board, nr, nc, new_moves));\n        }\n    }\n    \n    return neighbors;\n}\n\n// Main solving function using beam search with tree focus\nstring solve(vector<vector<int>> initial_board, int N, int T) {\n    // Find initial empty position\n    int empty_r = 0, empty_c = 0;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (initial_board[i][j] == 0) {\n                empty_r = i;\n                empty_c = j;\n                break;\n            }\n        }\n    }\n    \n    // Use beam search with wider beam for better exploration\n    const int BEAM_WIDTH = 100;\n    vector<State> beam;\n    beam.push_back(State(initial_board, empty_r, empty_c, \"\"));\n    \n    string best_moves = \"\";\n    int best_score = 0;\n    \n    // Track if we found a complete tree\n    bool found_complete_tree = false;\n    \n    // Beam search for several iterations\n    for (int iter = 0; iter < min(50, T/10); iter++) {\n        vector<pair<int, State>> scored_states;\n        \n        // Expand each state in current beam\n        for (const State& state : beam) {\n            vector<State> neighbors = getNeighbors(state, N);\n            for (const State& neighbor : neighbors) {\n                int score = evaluate(neighbor.board, neighbor.empty_r, neighbor.empty_c);\n                scored_states.push_back({score, neighbor});\n            }\n        }\n        \n        // Sort by score (descending)\n        sort(scored_states.begin(), scored_states.end(), [](const pair<int,State>& a, const pair<int,State>& b) {\n            return a.first > b.first;\n        });\n        \n        // Update best solution\n        if (!scored_states.empty() && scored_states[0].first > best_score) {\n            best_score = scored_states[0].first;\n            best_moves = scored_states[0].second.moves;\n            \n            // Check if we have a complete tree\n            auto [max_tree, is_complete] = findLargestTree(\n                scored_states[0].second.board, \n                scored_states[0].second.empty_r, \n                scored_states[0].second.empty_c\n            );\n            if (is_complete && max_tree == N*N-1) {\n                found_complete_tree = true;\n            }\n        }\n        \n        // Keep only top BEAM_WIDTH states\n        beam.clear();\n        for (int i = 0; i < min(BEAM_WIDTH, (int)scored_states.size()); i++) {\n            beam.push_back(scored_states[i].second);\n        }\n        \n        if (beam.empty()) break;\n    }\n    \n    // If we haven't used all moves and didn't find complete tree, do local improvement\n    if (best_moves.length() < T && !found_complete_tree) {\n        if (!beam.empty()) {\n            State current = beam[0];\n            \n            // Continue with greedy local search\n            mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n            uniform_int_distribution<int> dir_dist(0, 3);\n            \n            int current_score = evaluate(current.board, current.empty_r, current.empty_c);\n            \n            for (int step = current.moves.length(); step < T; step++) {\n                // Try all directions\n                int best_dir = -1;\n                int best_eval = current_score - 1; // Only accept improvements\n                vector<vector<int>> best_board;\n                int best_nr = -1, best_nc = -1;\n                \n                for (int d = 0; d < 4; d++) {\n                    int nr = current.empty_r + dr[d];\n                    int nc = current.empty_c + dc[d];\n                    \n                    if (nr >= 0 && nr < N && nc >= 0 && nc < N) {\n                        vector<vector<int>> new_board = current.board;\n                        swap(new_board[current.empty_r][current.empty_c], new_board[nr][nc]);\n                        int eval = evaluate(new_board, nr, nc);\n                        \n                        if (eval > best_eval) {\n                            best_eval = eval;\n                            best_dir = d;\n                            best_board = new_board;\n                            best_nr = nr;\n                            best_nc = nc;\n                        }\n                    }\n                }\n                \n                if (best_dir != -1) {\n                    current.board = best_board;\n                    current.empty_r = best_nr;\n                    current.empty_c = best_nc;\n                    current.moves += move_chars[best_dir];\n                    current_score = best_eval;\n                    \n                    // Update best if this is better\n                    if (best_eval > best_score) {\n                        best_score = best_eval;\n                        best_moves = current.moves;\n                    }\n                } else {\n                    // No improvement, try random move to escape local optimum\n                    int dir = dir_dist(rng);\n                    int nr = current.empty_r + dr[dir];\n                    int nc = current.empty_c + dc[dir];\n                    \n                    if (nr >= 0 && nr < N && nc >= 0 && nc < N) {\n                        swap(current.board[current.empty_r][current.empty_c], current.board[nr][nc]);\n                        current.empty_r = nr;\n                        current.empty_c = nc;\n                        current.moves += move_chars[dir];\n                    }\n                }\n            }\n        }\n    }\n    \n    return best_moves.substr(0, T);\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    int N, T;\n    cin >> N >> T;\n    \n    vector<vector<int>> board(N, vector<int>(N));\n    for (int i = 0; i < N; i++) {\n        string line;\n        cin >> line;\n        for (int j = 0; j < N; j++) {\n            char c = line[j];\n            if (c >= '0' && c <= '9') {\n                board[i][j] = c - '0';\n            } else {\n                board[i][j] = c - 'a' + 10;\n            }\n        }\n    }\n    \n    string result = solve(board, N, T);\n    cout << result << endl;\n    \n    return 0;\n}","ahc012":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst double PI = acos(-1);\nconst long long BOUND = 1000000000LL;\nconst double CAKE_RADIUS = 10000.0;\n\nstruct Strawberry {\n    long long x, y;\n    int id;\n    double angle;\n    double distance;\n};\n\ndouble calculate_angle(long long x, long long y) {\n    double angle = atan2((double)y, (double)x);\n    if (angle < 0) angle += 2 * PI;\n    return angle;\n}\n\npair<pair<long long, long long>, pair<long long, long long>> create_line_from_points(pair<double, double> p1, pair<double, double> p2) {\n    long long x1 = (long long)round(p1.first);\n    long long y1 = (long long)round(p1.second);\n    long long x2 = (long long)round(p2.first);\n    long long y2 = (long long)round(p2.second);\n    \n    // Ensure they're different points\n    if (abs(x1 - x2) < 1 && abs(y1 - y2) < 1) {\n        x2 += 100;\n    }\n    \n    // Bound checking\n    x1 = max(-BOUND, min(BOUND, x1));\n    y1 = max(-BOUND, min(BOUND, y1));\n    x2 = max(-BOUND, min(BOUND, x2));\n    y2 = max(-BOUND, min(BOUND, y2));\n    \n    return {{x1, y1}, {x2, y2}};\n}\n\n// Distance between two points\ndouble distance_points(long long x1, long long y1, long long x2, long long y2) {\n    return sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    int N, K;\n    cin >> N >> K;\n    \n    vector<int> a(11); // a[1] to a[10]\n    for (int i = 1; i <= 10; i++) {\n        cin >> a[i];\n    }\n    \n    vector<Strawberry> strawberries(N);\n    for (int i = 0; i < N; i++) {\n        cin >> strawberries[i].x >> strawberries[i].y;\n        strawberries[i].id = i;\n        strawberries[i].angle = calculate_angle(strawberries[i].x, strawberries[i].y);\n        strawberries[i].distance = distance_points(strawberries[i].x, strawberries[i].y, 0, 0);\n    }\n    \n    vector<pair<pair<long long, long long>, pair<long long, long long>>> lines;\n    \n    // Sort strawberries by distance from center (to help with clustering)\n    vector<int> indices(N);\n    for (int i = 0; i < N; i++) {\n        indices[i] = i;\n    }\n    \n    sort(indices.begin(), indices.end(), [&](int i, int j) {\n        return strawberries[i].distance < strawberries[j].distance;\n    });\n    \n    // Simple but effective approach: create clusters by proximity\n    vector<bool> used(N, false);\n    vector<vector<int>> clusters;\n    \n    // Try to create good clusters first\n    for (int cluster_size = 10; cluster_size >= 1 && lines.size() < K; cluster_size--) {\n        int clusters_needed = min(a[cluster_size], 5); // Limit to prevent overfitting\n        \n        for (int c = 0; c < clusters_needed && lines.size() + cluster_size <= K && clusters.size() < 20; c++) {\n            // Find the first unused strawberry\n            int start_idx = -1;\n            for (int i = 0; i < N; i++) {\n                if (!used[indices[i]]) {\n                    start_idx = indices[i];\n                    break;\n                }\n            }\n            \n            if (start_idx == -1) break;\n            \n            vector<int> current_cluster;\n            current_cluster.push_back(start_idx);\n            used[start_idx] = true;\n            \n            // Add nearby strawberries to cluster\n            for (int i = 1; i < cluster_size && current_cluster.size() < cluster_size; i++) {\n                int best_idx = -1;\n                double best_dist = 1e18;\n                \n                // Find closest unused strawberry to any member of current cluster\n                for (int j = 0; j < N; j++) {\n                    if (used[j]) continue;\n                    \n                    double min_dist_to_cluster = 1e18;\n                    for (int member : current_cluster) {\n                        double dist = distance_points(\n                            strawberries[member].x, strawberries[member].y,\n                            strawberries[j].x, strawberries[j].y\n                        );\n                        min_dist_to_cluster = min(min_dist_to_cluster, dist);\n                    }\n                    \n                    if (min_dist_to_cluster < best_dist) {\n                        best_dist = min_dist_to_cluster;\n                        best_idx = j;\n                    }\n                }\n                \n                if (best_idx != -1) {\n                    current_cluster.push_back(best_idx);\n                    used[best_idx] = true;\n                }\n            }\n            \n            if (current_cluster.size() >= 1) {\n                clusters.push_back(current_cluster);\n            }\n        }\n    }\n    \n    // Create separating lines between clusters\n    for (int i = 0; i < (int)clusters.size() && lines.size() < K; i++) {\n        for (int j = i + 1; j < (int)clusters.size() && lines.size() < K; j++) {\n            if (lines.size() >= K) break;\n            \n            // Create a line that tries to separate these two clusters\n            // Find centroids\n            double cx1 = 0, cy1 = 0;\n            for (int idx : clusters[i]) {\n                cx1 += strawberries[idx].x;\n                cy1 += strawberries[idx].y;\n            }\n            cx1 /= clusters[i].size();\n            cy1 /= clusters[i].size();\n            \n            double cx2 = 0, cy2 = 0;\n            for (int idx : clusters[j]) {\n                cx2 += strawberries[idx].x;\n                cy2 += strawberries[idx].y;\n            }\n            cx2 /= clusters[j].size();\n            cy2 /= clusters[j].size();\n            \n            // Midpoint\n            double mid_x = (cx1 + cx2) / 2.0;\n            double mid_y = (cy1 + cy2) / 2.0;\n            \n            // Direction perpendicular to line connecting centroids\n            double dx = cx2 - cx1;\n            double dy = cy2 - cy1;\n            double angle = atan2(dy, dx) + PI/2;\n            \n            // Create line through midpoint\n            double len = 100000;\n            pair<double, double> p1 = {mid_x + cos(angle) * len, mid_y + sin(angle) * len};\n            pair<double, double> p2 = {mid_x - cos(angle) * len, mid_y - sin(angle) * len};\n            \n            lines.push_back(create_line_from_points(p1, p2));\n        }\n    }\n    \n    // Add some radial lines as fallback\n    if (lines.size() < K) {\n        int current_count = lines.size();\n        int to_add = min(K - current_count, 15);\n        for (int i = 0; i < to_add && lines.size() < K; i++) {\n            double angle = 2.0 * PI * i / max(to_add, 1);\n            double dist = CAKE_RADIUS * 2;\n            pair<double, double> p1 = {cos(angle) * dist, sin(angle) * dist};\n            pair<double, double> p2 = {cos(angle + 0.1) * dist, sin(angle + 0.1) * dist};\n            lines.push_back(create_line_from_points(p1, p2));\n        }\n    }\n    \n    // Ensure we don't exceed K lines\n    while (lines.size() > K) {\n        lines.pop_back();\n    }\n    \n    // Output result\n    cout << lines.size() << \"\\n\";\n    for (const auto& line : lines) {\n        cout << line.first.first << \" \" << line.first.second << \" \" \n             << line.second.first << \" \" << line.second.second << \"\\n\";\n    }\n    \n    return 0;\n}","ahc014":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MAX_N = 65;\n\nint N, M;\nbool grid[MAX_N][MAX_N];\nvector<pair<int, int>> initial_dots;\nvector<tuple<int, int, int, int, int, int, int, int>> operations;\n\n// Center of the grid\nint center;\n\n// Calculate weight of a position\ninline int weight(int x, int y) {\n    int dx = x - center;\n    int dy = y - center;\n    return dx * dx + dy * dy + 1;\n}\n\n// Check if position is valid (within bounds)\ninline bool valid_pos(int x, int y) {\n    return x >= 0 && x < N && y >= 0 && y < N;\n}\n\n// Check if a rectangle is axis-aligned\nbool is_axis_aligned(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) {\n    return (x1 == x2 && x3 == x4 && y1 == y4 && y2 == y3) ||\n           (x1 == x4 && x2 == x3 && y1 == y2 && y3 == y4);\n}\n\n// Check if a rectangle is 45-degree rotated\nbool is_45_rotated(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) {\n    int dx12 = abs(x1 - x2);\n    int dy12 = abs(y1 - y2);\n    int dx23 = abs(x2 - x3);\n    int dy23 = abs(y2 - y3);\n    int dx34 = abs(x3 - x4);\n    int dy34 = abs(y3 - y4);\n    int dx41 = abs(x4 - x1);\n    int dy41 = abs(y4 - y1);\n    \n    return (dx12 == dy12 && dx23 == dy23 && dx34 == dy34 && dx41 == dy41) &&\n           (dx12 == dx23 && dx23 == dx34 && dx34 == dx41);\n}\n\n// Check if four points form a valid rectangle (axis-aligned or 45\u00b0 rotated)\nbool is_valid_rectangle(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) {\n    // Check if it's a valid rectangle\n    if (!valid_pos(x1, y1) || !valid_pos(x2, y2) || !valid_pos(x3, y3) || !valid_pos(x4, y4))\n        return false;\n        \n    // Check if it's axis-aligned or 45-degree rotated\n    return is_axis_aligned(x1, y1, x2, y2, x3, y3, x4, y4) || \n           is_45_rotated(x1, y1, x2, y2, x3, y3, x4, y4);\n}\n\n// Check if there are no dots on the perimeter except the three given points\nbool no_dots_on_perimeter(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) {\n    // This is a simplified check - we'd need to implement proper perimeter checking\n    // For now, let's just check the four corners\n    int corners[4][2] = {{x1, y1}, {x2, y2}, {x3, y3}, {x4, y4}};\n    \n    for (int i = 0; i < 4; i++) {\n        int x = corners[i][0];\n        int y = corners[i][1];\n        // Skip the three points that should have dots\n        if ((x == x2 && y == y2) || (x == x3 && y == y3) || (x == x4 && y == y4))\n            continue;\n        // If it's the new point, it should not have a dot\n        if (x == x1 && y == y1) {\n            if (grid[x][y]) return false;\n        }\n        // For other perimeter points, they should not have dots\n        // This is simplified - we need a better perimeter check\n    }\n    return true;\n}\n\n// Find valid rectangles for a given position\nvector<tuple<int, int, int, int, int, int>> find_valid_rectangles(int new_x, int new_y) {\n    vector<tuple<int, int, int, int, int, int>> result;\n    \n    // Look for axis-aligned rectangles\n    for (int dx = 1; dx < N; dx++) {\n        for (int dy = 1; dy < N; dy++) {\n            // Try different orientations\n            vector<pair<int, int>> corners = {\n                {new_x, new_y},\n                {new_x + dx, new_y},\n                {new_x + dx, new_y + dy},\n                {new_x, new_y + dy}\n            };\n            \n            // Check all 4 rotations\n            for (int rot = 0; rot < 4; rot++) {\n                int x1 = corners[0].first, y1 = corners[0].second;\n                int x2 = corners[1].first, y2 = corners[1].second;\n                int x3 = corners[2].first, y3 = corners[2].second;\n                int x4 = corners[3].first, y4 = corners[3].second;\n                \n                if (valid_pos(x1, y1) && valid_pos(x2, y2) && valid_pos(x3, y3) && valid_pos(x4, y4)) {\n                    // Check if the three points (excluding new position) have dots\n                    if (grid[x2][y2] && grid[x3][y3] && grid[x4][y4]) {\n                        // Simple check for now - need better perimeter checking\n                        result.push_back(make_tuple(x1, y1, x2, y2, x3, y3, x4, y4));\n                    }\n                }\n                \n                // Rotate corners\n                auto temp = corners[0];\n                for (int i = 0; i < 3; i++) {\n                    corners[i] = corners[i+1];\n                }\n                corners[3] = temp;\n            }\n        }\n    }\n    \n    // Look for 45-degree rotated rectangles\n    for (int d = 1; d < N; d++) {\n        // Diamond shape with side length d\n        vector<pair<int, int>> corners = {\n            {new_x, new_y + d},\n            {new_x + d, new_y},\n            {new_x, new_y - d},\n            {new_x - d, new_y}\n        };\n        \n        if (valid_pos(corners[0].first, corners[0].second) &&\n            valid_pos(corners[1].first, corners[1].second) &&\n            valid_pos(corners[2].first, corners[2].second) &&\n            valid_pos(corners[3].first, corners[3].second)) {\n            \n            if (grid[corners[1].first][corners[1].second] &&\n                grid[corners[2].first][corners[2].second] &&\n                grid[corners[3].first][corners[3].second]) {\n                result.push_back(make_tuple(\n                    corners[0].first, corners[0].second,\n                    corners[1].first, corners[1].second,\n                    corners[2].first, corners[2].second,\n                    corners[3].first, corners[3].second\n                ));\n            }\n        }\n    }\n    \n    return result;\n}\n\n// Greedy approach: try to place dots with highest weight first\nvoid solve() {\n    // Priority queue to process positions by weight (descending)\n    priority_queue<pair<int, pair<int, int>>> pq;\n    \n    // Add all valid positions to priority queue\n    for (int x = 0; x < N; x++) {\n        for (int y = 0; y < N; y++) {\n            if (!grid[x][y]) { // Position doesn't have a dot yet\n                pq.push({weight(x, y), {x, y}});\n            }\n        }\n    }\n    \n    // Process positions\n    int attempts = 0;\n    const int MAX_ATTEMPTS = 10000; // Limit attempts for performance\n    \n    while (!pq.empty() && attempts < MAX_ATTEMPTS) {\n        auto current = pq.top();\n        pq.pop();\n        \n        int x = current.second.first;\n        int y = current.second.second;\n        \n        // If this position already has a dot, skip\n        if (grid[x][y]) continue;\n        \n        // Find valid rectangles for this position\n        auto rectangles = find_valid_rectangles(x, y);\n        \n        if (!rectangles.empty()) {\n            // Use the first valid rectangle (could be improved with better selection)\n            auto rect = rectangles[0];\n            int x1, y1, x2, y2, x3, y3, x4, y4;\n            tie(x1, y1, x2, y2, x3, y3, x4, y4) = rect;\n            \n            // Place the dot\n            grid[x1][y1] = true;\n            operations.push_back(make_tuple(x1, y1, x2, y2, x3, y3, x4, y4));\n            \n            attempts = 0; // Reset attempts since we made progress\n        }\n        \n        attempts++;\n    }\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    cin >> N >> M;\n    center = (N - 1) / 2;\n    \n    // Initialize grid\n    memset(grid, false, sizeof(grid));\n    \n    // Read initial dots\n    for (int i = 0; i < M; i++) {\n        int x, y;\n        cin >> x >> y;\n        grid[x][y] = true;\n        initial_dots.push_back({x, y});\n    }\n    \n    // Solve\n    solve();\n    \n    // Output results\n    cout << operations.size() << \"\\n\";\n    for (const auto& op : operations) {\n        cout << get<0>(op) << \" \" << get<1>(op) << \" \" \n             << get<2>(op) << \" \" << get<3>(op) << \" \"\n             << get<4>(op) << \" \" << get<5>(op) << \" \"\n             << get<6>(op) << \" \" << get<7>(op) << \"\\n\";\n    }\n    \n    return 0;\n}","ahc015":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int GRID_SIZE = 10;\nconst int TOTAL_CANDIES = 100;\nconst int LOOKAHEAD = 3; // Look ahead this many moves\n\n// Directions: F(forward/up), B(backward/down), L(left), R(right)\nconst char DIR_CHARS[] = {'F', 'B', 'L', 'R'};\nconst int DX[] = {-1, 1, 0, 0}; // F, B, L, R\nconst int DY[] = {0, 0, -1, 1};\n\nstruct Position {\n    int x, y;\n    Position() : x(0), y(0) {}\n    Position(int _x, int _y) : x(_x), y(_y) {}\n};\n\nclass CandyBox {\npublic:\n    int grid[GRID_SIZE][GRID_SIZE]; // 0 = empty, 1,2,3 = flavors\n    vector<int> flavor_sequence;\n    int candy_count[4]; // index 0 unused, 1-3 for flavors\n    \n    CandyBox() {\n        memset(grid, 0, sizeof(grid));\n        memset(candy_count, 0, sizeof(candy_count));\n    }\n    \n    void read_flavors() {\n        flavor_sequence.resize(TOTAL_CANDIES);\n        for (int i = 0; i < TOTAL_CANDIES; i++) {\n            cin >> flavor_sequence[i];\n        }\n    }\n    \n    // Convert empty cell index to position\n    Position index_to_position(int index) {\n        int count = 0;\n        for (int i = 0; i < GRID_SIZE; i++) {\n            for (int j = 0; j < GRID_SIZE; j++) {\n                if (grid[i][j] == 0) {\n                    count++;\n                    if (count == index) {\n                        return Position(i, j);\n                    }\n                }\n            }\n        }\n        return Position(-1, -1); // Error\n    }\n    \n    // Place candy at specified position\n    void place_candy(int x, int y, int flavor) {\n        grid[x][y] = flavor;\n        candy_count[flavor]++;\n    }\n    \n    // Simulate sliding in given direction\n    void slide_candies(int dir) {\n        if (dir == 0) { // Forward (up) - move rows up\n            for (int col = 0; col < GRID_SIZE; col++) {\n                int write_pos = 0;\n                for (int row = 0; row < GRID_SIZE; row++) {\n                    if (grid[row][col] != 0) {\n                        if (write_pos != row) {\n                            grid[write_pos][col] = grid[row][col];\n                            grid[row][col] = 0;\n                        }\n                        write_pos++;\n                    }\n                }\n            }\n        } else if (dir == 1) { // Backward (down) - move rows down\n            for (int col = 0; col < GRID_SIZE; col++) {\n                int write_pos = GRID_SIZE - 1;\n                for (int row = GRID_SIZE - 1; row >= 0; row--) {\n                    if (grid[row][col] != 0) {\n                        if (write_pos != row) {\n                            grid[write_pos][col] = grid[row][col];\n                            grid[row][col] = 0;\n                        }\n                        write_pos--;\n                    }\n                }\n            }\n        } else if (dir == 2) { // Left - move columns left\n            for (int row = 0; row < GRID_SIZE; row++) {\n                int write_pos = 0;\n                for (int col = 0; col < GRID_SIZE; col++) {\n                    if (grid[row][col] != 0) {\n                        if (write_pos != col) {\n                            grid[row][write_pos] = grid[row][col];\n                            grid[row][col] = 0;\n                        }\n                        write_pos++;\n                    }\n                }\n            }\n        } else if (dir == 3) { // Right - move columns right\n            for (int row = 0; row < GRID_SIZE; row++) {\n                int write_pos = GRID_SIZE - 1;\n                for (int col = GRID_SIZE - 1; col >= 0; col--) {\n                    if (grid[row][col] != 0) {\n                        if (write_pos != col) {\n                            grid[row][write_pos] = grid[row][col];\n                            grid[row][col] = 0;\n                        }\n                        write_pos--;\n                    }\n                }\n            }\n        }\n    }\n    \n    // Create a copy of current state\n    CandyBox copy() const {\n        CandyBox other;\n        for (int i = 0; i < GRID_SIZE; i++) {\n            for (int j = 0; j < GRID_SIZE; j++) {\n                other.grid[i][j] = grid[i][j];\n            }\n        }\n        for (int i = 0; i < 4; i++) {\n            other.candy_count[i] = candy_count[i];\n        }\n        other.flavor_sequence = flavor_sequence;\n        return other;\n    }\n    \n    // Calculate connected components score with additional metrics\n    pair<int, int> calculate_connectedness_detailed() {\n        bool visited[GRID_SIZE][GRID_SIZE];\n        memset(visited, false, sizeof(visited));\n        \n        int total_score = 0;\n        int max_component = 0;\n        int dx[] = {0, 0, -1, 1};\n        int dy[] = {-1, 1, 0, 0};\n        \n        for (int i = 0; i < GRID_SIZE; i++) {\n            for (int j = 0; j < GRID_SIZE; j++) {\n                if (grid[i][j] != 0 && !visited[i][j]) {\n                    // BFS to find connected component\n                    queue<Position> q;\n                    vector<Position> component;\n                    q.push(Position(i, j));\n                    visited[i][j] = true;\n                    component.push_back(Position(i, j));\n                    \n                    int flavor = grid[i][j];\n                    \n                    while (!q.empty()) {\n                        Position current = q.front();\n                        q.pop();\n                        \n                        for (int d = 0; d < 4; d++) {\n                            int nx = current.x + dx[d];\n                            int ny = current.y + dy[d];\n                            \n                            if (nx >= 0 && nx < GRID_SIZE && ny >= 0 && ny < GRID_SIZE) {\n                                if (!visited[nx][ny] && grid[nx][ny] == flavor) {\n                                    visited[nx][ny] = true;\n                                    q.push(Position(nx, ny));\n                                    component.push_back(Position(nx, ny));\n                                }\n                            }\n                        }\n                    }\n                    \n                    // Add square of component size to score\n                    int component_size = component.size();\n                    total_score += component_size * component_size;\n                    max_component = max(max_component, component_size);\n                }\n            }\n        }\n        \n        return {total_score, max_component};\n    }\n    \n    int calculate_connectedness() {\n        return calculate_connectedness_detailed().first;\n    }\n    \n    // Count empty cells\n    int count_empty() const {\n        int count = 0;\n        for (int i = 0; i < GRID_SIZE; i++) {\n            for (int j = 0; j < GRID_SIZE; j++) {\n                if (grid[i][j] == 0) count++;\n            }\n        }\n        return count;\n    }\n    \n    // Evaluate clustering potential for a specific flavor\n    int evaluate_flavor_clustering(int flavor) {\n        bool visited[GRID_SIZE][GRID_SIZE];\n        memset(visited, false, sizeof(visited));\n        \n        int total_score = 0;\n        int dx[] = {0, 0, -1, 1};\n        int dy[] = {-1, 1, 0, 0};\n        \n        for (int i = 0; i < GRID_SIZE; i++) {\n            for (int j = 0; j < GRID_SIZE; j++) {\n                if (grid[i][j] == flavor && !visited[i][j]) {\n                    // BFS to find connected component\n                    queue<Position> q;\n                    vector<Position> component;\n                    q.push(Position(i, j));\n                    visited[i][j] = true;\n                    component.push_back(Position(i, j));\n                    \n                    while (!q.empty()) {\n                        Position current = q.front();\n                        q.pop();\n                        \n                        for (int d = 0; d < 4; d++) {\n                            int nx = current.x + dx[d];\n                            int ny = current.y + dy[d];\n                            \n                            if (nx >= 0 && nx < GRID_SIZE && ny >= 0 && ny < GRID_SIZE) {\n                                if (!visited[nx][ny] && grid[nx][ny] == flavor) {\n                                    visited[nx][ny] = true;\n                                    q.push(Position(nx, ny));\n                                    component.push_back(Position(nx, ny));\n                                }\n                            }\n                        }\n                    }\n                    \n                    // Prefer larger components\n                    total_score += component.size() * component.size();\n                }\n            }\n        }\n        \n        return total_score;\n    }\n    \n    // Advanced evaluation considering multiple factors\n    double evaluate_move_advanced(int dir, int current_turn) {\n        // Create copy and simulate\n        CandyBox temp = this->copy();\n        temp.slide_candies(dir);\n        \n        auto [connected_score, max_component] = temp.calculate_connectedness_detailed();\n        \n        // Weight factors\n        double score = connected_score;\n        \n        // Bonus for larger maximum component\n        score += max_component * 10;\n        \n        // Evaluate clustering potential for each flavor\n        for (int flavor = 1; flavor <= 3; flavor++) {\n            if (temp.candy_count[flavor] > 0) {\n                score += temp.evaluate_flavor_clustering(flavor) * 0.5;\n            }\n        }\n        \n        // Prefer moves that don't isolate single candies\n        int isolated_candies = 0;\n        int dx[] = {0, 0, -1, 1};\n        int dy[] = {-1, 1, 0, 0};\n        for (int i = 0; i < GRID_SIZE; i++) {\n            for (int j = 0; j < GRID_SIZE; j++) {\n                if (temp.grid[i][j] != 0) {\n                    bool has_neighbor = false;\n                    int flavor = temp.grid[i][j];\n                    for (int d = 0; d < 4; d++) {\n                        int nx = i + dx[d];\n                        int ny = j + dy[d];\n                        if (nx >= 0 && nx < GRID_SIZE && ny >= 0 && ny < GRID_SIZE) {\n                            if (temp.grid[nx][ny] == flavor) {\n                                has_neighbor = true;\n                                break;\n                            }\n                        }\n                    }\n                    if (!has_neighbor) isolated_candies++;\n                }\n            }\n        }\n        score -= isolated_candies * 20; // Penalty for isolated candies\n        \n        return score;\n    }\n    \n    // Simulate lookahead\n    double simulate_lookahead(int dir, int current_turn) {\n        CandyBox temp = this->copy();\n        temp.slide_candies(dir);\n        \n        double total_score = temp.evaluate_move_advanced(dir, current_turn);\n        \n        // Simple lookahead - simulate next few placements randomly\n        int lookahead_depth = min(LOOKAHEAD, TOTAL_CANDIES - current_turn - 1);\n        CandyBox lookahead_state = temp.copy();\n        \n        for (int i = 0; i < lookahead_depth; i++) {\n            int future_turn = current_turn + 1 + i;\n            if (future_turn >= TOTAL_CANDIES) break;\n            \n            // Simulate a random placement (we don't know exact position, but we know flavor)\n            int flavor = lookahead_state.flavor_sequence[future_turn];\n            \n            // Find a reasonable position - prefer corners/edges for isolation control\n            vector<Position> empty_positions;\n            for (int x = 0; x < GRID_SIZE; x++) {\n                for (int y = 0; y < GRID_SIZE; y++) {\n                    if (lookahead_state.grid[x][y] == 0) {\n                        empty_positions.push_back(Position(x, y));\n                    }\n                }\n            }\n            \n            if (!empty_positions.empty()) {\n                // Place in a position that might help clustering\n                lookahead_state.place_candy(empty_positions[0].x, empty_positions[0].y, flavor);\n            }\n            \n            // Apply a reasonable tilt (prefer clustering directions)\n            int best_dir = 0;\n            double best_score = -1e9;\n            for (int d = 0; d < 4; d++) {\n                double score = lookahead_state.evaluate_move_advanced(d, future_turn);\n                if (score > best_score) {\n                    best_score = score;\n                    best_dir = d;\n                }\n            }\n            lookahead_state.slide_candies(best_dir);\n        }\n        \n        total_score += lookahead_state.calculate_connectedness() * 0.3;\n        return total_score;\n    }\n};\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    CandyBox box;\n    box.read_flavors();\n    \n    for (int turn = 0; turn < TOTAL_CANDIES; turn++) {\n        int p;\n        cin >> p;\n        \n        // Place the candy\n        Position pos = box.index_to_position(p);\n        box.place_candy(pos.x, pos.y, box.flavor_sequence[turn]);\n        \n        // Don't need to output direction for last candy\n        if (turn == TOTAL_CANDIES - 1) {\n            cout << \"F\" << endl;\n            cout.flush();\n            break;\n        }\n        \n        // Evaluate all four directions and choose best\n        double best_score = -1e18;\n        int best_direction = 0;\n        \n        for (int dir = 0; dir < 4; dir++) {\n            double score = box.simulate_lookahead(dir, turn);\n            if (score > best_score) {\n                best_score = score;\n                best_direction = dir;\n            }\n        }\n        \n        // Output the chosen direction\n        cout << DIR_CHARS[best_direction] << endl;\n        cout.flush();\n        \n        // Apply the chosen move\n        box.slide_candies(best_direction);\n    }\n    \n    return 0;\n}","ahc016":"#include <bits/stdc++.h>\nusing namespace std;\n\nmt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n\nconst int MAX_N = 100;\nconst int QUERY_COUNT = 100;\n\nint M;\ndouble epsilon;\nint N;\nvector<string> graphs;\nvector<vector<vector<int>>> graph_matrices;\n\n// Convert adjacency matrix to edge string\nstring matrixToEdgeString(const vector<vector<int>>& matrix) {\n    string result;\n    for (int i = 0; i < N; i++) {\n        for (int j = i + 1; j < N; j++) {\n            result += (matrix[i][j] ? '1' : '0');\n        }\n    }\n    return result;\n}\n\n// Convert edge string to adjacency matrix\nvector<vector<int>> edgeStringToMatrix(const string& edge_str) {\n    vector<vector<int>> matrix(N, vector<int>(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 (edge_str[idx] == '1') {\n                matrix[i][j] = 1;\n                matrix[j][i] = 1;\n            }\n            idx++;\n        }\n    }\n    return matrix;\n}\n\n// Calculate graph features for better discrimination\nvector<int> getDegreeSequence(const vector<vector<int>>& matrix) {\n    vector<int> degrees(N);\n    for (int i = 0; i < N; i++) {\n        degrees[i] = accumulate(matrix[i].begin(), matrix[i].end(), 0);\n    }\n    sort(degrees.begin(), degrees.end());\n    return degrees;\n}\n\n// Generate more distinguishable graphs\nvoid generateGraphs() {\n    // Adaptive N selection based on M and epsilon\n    if (M <= 15) N = 12;\n    else if (M <= 30) N = 16;\n    else if (M <= 50) N = 20;\n    else if (M <= 70) N = 24;\n    else N = 28;\n    \n    // For high error rates, use larger N\n    if (epsilon > 0.3) N = min(40, max(N, 20));\n    else if (epsilon > 0.2) N = min(35, max(N, 18));\n    \n    N = min(N, 100);\n    \n    graphs.clear();\n    graph_matrices.clear();\n    \n    // Generate graphs with different structures\n    for (int k = 0; k < M; k++) {\n        vector<vector<int>> matrix(N, vector<int>(N, 0));\n        \n        // Use multiple strategies based on k\n        if (k < M / 3) {\n            // Strategy 1: Regular graphs with different degrees\n            int target_degree = max(1, min(N-1, 1 + (k * (N-2)) / max(1, M/3 - 1)));\n            for (int i = 0; i < N; i++) {\n                int degree_count = 0;\n                for (int j = 0; j < N && degree_count < target_degree; j++) {\n                    if (i != j && uniform_int_distribution<int>(0, N-1)(rng) < target_degree) {\n                        matrix[i][j] = 1;\n                        matrix[j][i] = 1;\n                        degree_count++;\n                    }\n                }\n            }\n        } else if (k < 2 * M / 3) {\n            // Strategy 2: Cluster-based graphs\n            int clusters = max(2, min(N/2, 2 + (k * 3) / max(1, M/3)));\n            vector<int> cluster_assignment(N);\n            for (int i = 0; i < N; i++) {\n                cluster_assignment[i] = i % clusters;\n            }\n            \n            // Intra-cluster edges\n            for (int i = 0; i < N; i++) {\n                for (int j = i + 1; j < N; j++) {\n                    if (cluster_assignment[i] == cluster_assignment[j]) {\n                        if (uniform_int_distribution<int>(0, 2)(rng) > 0) {\n                            matrix[i][j] = 1;\n                            matrix[j][i] = 1;\n                        }\n                    } else {\n                        // Few inter-cluster edges\n                        if (uniform_int_distribution<int>(0, clusters*2)(rng) == 0) {\n                            matrix[i][j] = 1;\n                            matrix[j][i] = 1;\n                        }\n                    }\n                }\n            }\n        } else {\n            // Strategy 3: Random graphs with controlled edge count\n            int edge_count = max(N, min(N*(N-1)/4, (k * N * (N-1) / 2) / max(1, M - 2*M/3)));\n            for (int e = 0; e < edge_count; e++) {\n                int i, j;\n                do {\n                    i = uniform_int_distribution<int>(0, N-2)(rng);\n                    j = uniform_int_distribution<int>(i+1, N-1)(rng);\n                } while (matrix[i][j] == 1);\n                matrix[i][j] = 1;\n                matrix[j][i] = 1;\n            }\n        }\n        \n        graphs.push_back(matrixToEdgeString(matrix));\n        graph_matrices.push_back(matrix);\n    }\n}\n\n// Improved similarity calculation with better permutation handling\ndouble calculateSimilarity(const string& original, const string& received) {\n    auto orig_matrix = edgeStringToMatrix(original);\n    auto recv_matrix = edgeStringToMatrix(received);\n    \n    // Get degree sequences for quick filtering\n    auto orig_degrees = getDegreeSequence(orig_matrix);\n    auto recv_degrees = getDegreeSequence(recv_matrix);\n    \n    // Quick similarity check based on degree sequences\n    int degree_similarity = 0;\n    for (int i = 0; i < N; i++) {\n        degree_similarity += abs(orig_degrees[i] - recv_degrees[i]);\n    }\n    \n    // If degree sequences are too different, this is likely not a match\n    if (degree_similarity > N * epsilon * 4) {\n        return -1e9;\n    }\n    \n    // Try multiple random permutations\n    double best_score = -1e18;\n    \n    // Identity mapping\n    double identity_score = 0;\n    for (int i = 0; i < original.size(); i++) {\n        if (original[i] == received[i]) {\n            identity_score += log(1.0 - epsilon + 1e-12);\n        } else {\n            identity_score += log(epsilon + 1e-12);\n        }\n    }\n    best_score = identity_score;\n    \n    // Try random permutations\n    vector<int> perm(N);\n    for (int i = 0; i < N; i++) perm[i] = i;\n    \n    int trials = min(100, max(20, N * 2));\n    if (epsilon > 0.25) trials *= 2;\n    \n    for (int trial = 0; trial < trials; trial++) {\n        shuffle(perm.begin(), perm.end(), rng);\n        \n        double score = 0;\n        int idx = 0;\n        for (int i = 0; i < N; i++) {\n            for (int j = i + 1; j < N; j++) {\n                int pi = perm[i], pj = perm[j];\n                if (pi > pj) swap(pi, pj);\n                \n                // Calculate index in original graph\n                int orig_idx = 0;\n                for (int x = 0; x < pi; x++) {\n                    orig_idx += (N - 1 - x);\n                }\n                orig_idx += (pj - pi - 1);\n                \n                if (orig_idx >= 0 && orig_idx < original.size()) {\n                    char orig_edge = original[orig_idx];\n                    char recv_edge = received[idx];\n                    \n                    if (orig_edge == recv_edge) {\n                        score += log(1.0 - epsilon + 1e-12);\n                    } else {\n                        score += log(epsilon + 1e-12);\n                    }\n                }\n                idx++;\n            }\n        }\n        \n        best_score = max(best_score, score);\n    }\n    \n    return best_score;\n}\n\n// Enhanced decoding with multiple verification steps\nint decodeGraph(const string& received) {\n    vector<pair<double, int>> scores(M);\n    \n    for (int i = 0; i < M; i++) {\n        scores[i].second = i;\n        scores[i].first = calculateSimilarity(graphs[i], received);\n    }\n    \n    sort(scores.begin(), scores.end(), greater<pair<double, int>>());\n    \n    // If top score is significantly better, return it\n    if (scores.size() > 1 && scores[0].first > scores[1].first + 5.0) {\n        return scores[0].second;\n    }\n    \n    // Otherwise, use edge count as secondary criterion\n    int recv_edges = count(received.begin(), received.end(), '1');\n    vector<pair<int, int>> edge_diffs(M);\n    \n    for (int i = 0; i < M; i++) {\n        int orig_edges = count(graphs[i].begin(), graphs[i].end(), '1');\n        edge_diffs[i] = {abs(orig_edges - recv_edges), i};\n    }\n    \n    sort(edge_diffs.begin(), edge_diffs.end());\n    \n    // Blend both approaches\n    if (scores[0].first > -1e6) {\n        return scores[0].second;\n    }\n    return edge_diffs[0].second;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    cin >> M >> epsilon;\n    \n    // Generate graphs\n    generateGraphs();\n    \n    // Output N\n    cout << N << endl;\n    \n    // Output all graphs\n    for (const string& graph : graphs) {\n        cout << graph << endl;\n    }\n    cout.flush();\n    \n    // Process queries\n    for (int q = 0; q < QUERY_COUNT; q++) {\n        string received;\n        cin >> received;\n        \n        int prediction = decodeGraph(received);\n        cout << prediction << endl;\n        cout.flush();\n    }\n    \n    return 0;\n}","ahc017":"#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing P = pair<int, int>;\nusing PL = pair<ll, int>;\n\nconst ll INF = 1e15;\nconst int MAXN = 1005;\nconst int MAXM = 3005;\n\nint N, M, D, K;\nvector<tuple<int, int, int>> edges;\nvector<pair<int, int>> coords;\nvector<vector<P>> adj;\n\n// Find bridges using Tarjan's algorithm\nvector<bool> find_bridges() {\n    vector<bool> is_bridge(M, false);\n    vector<int> disc(N, -1), low(N, -1);\n    vector<bool> visited(N, false);\n    int time = 0;\n    \n    function<void(int, int, int)> dfs = [&](int u, int parent, int edge_idx) {\n        visited[u] = true;\n        disc[u] = low[u] = time++;\n        \n        for (int i = 0; i < adj[u].size(); i++) {\n            auto [v, w] = adj[u][i];\n            int current_edge_idx = -1;\n            \n            // Find the edge index\n            for (int j = 0; j < M; j++) {\n                auto [eu, ev, ew] = edges[j];\n                if ((eu-1 == u && ev-1 == v) || (eu-1 == v && ev-1 == u)) {\n                    current_edge_idx = j;\n                    break;\n                }\n            }\n            \n            if (!visited[v]) {\n                dfs(v, u, current_edge_idx);\n                low[u] = min(low[u], low[v]);\n                \n                if (low[v] > disc[u]) {\n                    if (current_edge_idx != -1) {\n                        is_bridge[current_edge_idx] = true;\n                    }\n                }\n            } else if (v != parent) {\n                low[u] = min(low[u], disc[v]);\n            }\n        }\n    };\n    \n    for (int i = 0; i < N; i++) {\n        if (!visited[i]) {\n            dfs(i, -1, -1);\n        }\n    }\n    \n    return is_bridge;\n}\n\n// Calculate multiple importance metrics\ntuple<vector<ll>, vector<ll>, vector<ll>> calculate_importance_metrics() {\n    vector<ll> degree_importance(M, 0);\n    vector<ll> weight_importance(M, 0);\n    vector<ll> spatial_importance(M, 0);\n    \n    // Degree-based importance\n    vector<int> degree(N, 0);\n    for (int i = 0; i < M; i++) {\n        auto [u, v, w] = edges[i];\n        degree[u-1]++;\n        degree[v-1]++;\n    }\n    \n    for (int i = 0; i < M; i++) {\n        auto [u, v, w] = edges[i];\n        // Edges connecting high-degree vertices are more important\n        degree_importance[i] = (ll)(degree[u-1] + degree[v-1]) * 1000000LL;\n        // Lower weight edges are more critical for shortest paths\n        weight_importance[i] = 1000000000LL / (w + 1);\n    }\n    \n    // Spatial importance - edges closer to graph center\n    ll sum_x = 0, sum_y = 0;\n    for (int i = 0; i < N; i++) {\n        sum_x += coords[i].first;\n        sum_y += coords[i].second;\n    }\n    double center_x = (double)sum_x / N;\n    double center_y = (double)sum_y / N;\n    \n    for (int i = 0; i < M; i++) {\n        auto [u, v, w] = edges[i];\n        auto [x1, y1] = coords[u-1];\n        auto [x2, y2] = coords[v-1];\n        \n        // Midpoint of edge\n        double mid_x = (x1 + x2) / 2.0;\n        double mid_y = (y1 + y2) / 2.0;\n        \n        // Distance from center\n        double dist_from_center = sqrt((mid_x - center_x) * (mid_x - center_x) + \n                                      (mid_y - center_y) * (mid_y - center_y));\n        \n        // Closer to center = more important\n        spatial_importance[i] = (ll)(10000.0 / (dist_from_center + 1.0)) * 100000LL;\n    }\n    \n    return {degree_importance, weight_importance, spatial_importance};\n}\n\n// Main solving function\nvector<int> solve() {\n    // Build adjacency list\n    adj.assign(N, vector<P>());\n    for (int i = 0; i < M; i++) {\n        auto [u, v, w] = edges[i];\n        adj[u-1].push_back({v-1, w});\n        adj[v-1].push_back({u-1, w});\n    }\n    \n    // Find bridges (critical edges)\n    auto is_bridge = find_bridges();\n    \n    // Calculate importance metrics\n    auto [degree_imp, weight_imp, spatial_imp] = calculate_importance_metrics();\n    \n    // Combine importance scores\n    vector<pair<ll, int>> combined_importance(M);\n    for (int i = 0; i < M; i++) {\n        ll bridge_bonus = is_bridge[i] ? 10000000000LL : 0;\n        ll total_importance = degree_imp[i] + weight_imp[i] + spatial_imp[i] + bridge_bonus;\n        combined_importance[i] = {total_importance, i};\n    }\n    \n    sort(combined_importance.rbegin(), combined_importance.rend());\n    \n    // Assign edges to days with better strategy\n    vector<int> result(M, 1);\n    vector<int> day_count(D + 1, 0);\n    \n    // First, assign bridges and very high importance edges to separate days\n    int critical_count = 0;\n    for (int i = 0; i < min(M, D * 3) && critical_count < D; i++) {\n        int edge_idx = combined_importance[i].second;\n        if (is_bridge[edge_idx] || combined_importance[i].first > 5000000000LL) {\n            // Assign to next available day\n            if (critical_count + 1 <= D && day_count[critical_count + 1] < K) {\n                result[edge_idx] = critical_count + 1;\n                day_count[critical_count + 1]++;\n                critical_count++;\n            }\n        }\n    }\n    \n    // Then assign remaining high importance edges\n    for (int i = 0; i < min(M, D * 5); i++) {\n        int edge_idx = combined_importance[i].second;\n        if (result[edge_idx] != 1) continue; // Already assigned\n        \n        // Find day with minimum load\n        int best_day = 1;\n        for (int day = 2; day <= D; day++) {\n            if (day_count[day] < day_count[best_day]) {\n                best_day = day;\n            }\n        }\n        \n        if (day_count[best_day] < K) {\n            result[edge_idx] = best_day;\n            day_count[best_day]++;\n        }\n    }\n    \n    // Assign remaining edges\n    for (int i = 0; i < M; i++) {\n        if (result[i] != 1) continue; // Already assigned\n        \n        // Find day with minimum load\n        int best_day = 1;\n        for (int day = 2; day <= D; day++) {\n            if (day_count[day] < day_count[best_day]) {\n                best_day = day;\n            }\n        }\n        \n        if (day_count[best_day] < K) {\n            result[i] = best_day;\n            day_count[best_day]++;\n        } else {\n            // Find any day with space\n            for (int day = 1; day <= D; day++) {\n                if (day_count[day] < K) {\n                    result[i] = day;\n                    day_count[day]++;\n                    break;\n                }\n            }\n        }\n    }\n    \n    // Lightweight local search\n    auto start_time = chrono::steady_clock::now();\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n    \n    uniform_int_distribution<int> edge_dist(0, M-1);\n    uniform_int_distribution<int> day_dist(1, D);\n    \n    // Very limited and fast local search\n    for (int iter = 0; iter < 5000; iter++) {\n        auto current_time = chrono::steady_clock::now();\n        if (chrono::duration_cast<chrono::milliseconds>(current_time - start_time).count() > 5500) {\n            break;\n        }\n        \n        int edge_idx = edge_dist(rng);\n        int current_day = result[edge_idx];\n        int new_day = day_dist(rng);\n        \n        if (new_day != current_day) {\n            // Quick constraint check\n            vector<int> day_load(D + 1, 0);\n            for (int i = 0; i < M; i++) {\n                day_load[result[i]]++;\n            }\n            \n            if (day_load[new_day] < K) {\n                // Simple swap acceptance - accept if it spreads out high importance edges\n                ll old_score = 0, new_score = 0;\n                \n                // Very rough approximation - just check if we're balancing important edges\n                result[edge_idx] = new_day;\n                // Accept most moves in the time limit since exact evaluation is too expensive\n            }\n        }\n    }\n    \n    return result;\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    cin >> N >> M >> D >> K;\n    edges.resize(M);\n    coords.resize(N);\n    \n    for (int i = 0; i < M; i++) {\n        int u, v, w;\n        cin >> u >> v >> w;\n        edges[i] = {u, v, w};\n    }\n    \n    for (int i = 0; i < N; i++) {\n        cin >> coords[i].first >> coords[i].second;\n    }\n    \n    auto result = solve();\n    \n    for (int i = 0; i < M; i++) {\n        cout << result[i];\n        if (i < M - 1) cout << \" \";\n    }\n    cout << \"\\n\";\n    \n    return 0;\n}","ahc019":"#include <bits/stdc++.h>\nusing namespace std;\n\n// Direction vectors for connectivity (6 directions: +/- x, y, z)\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// Check if a set of cubes forms a connected component\nbool isConnected(const vector<tuple<int,int,int>>& cubes) {\n    if (cubes.empty()) return true;\n    if (cubes.size() == 1) return true;\n    \n    set<tuple<int,int,int>> cube_set(cubes.begin(), cubes.end());\n    set<tuple<int,int,int>> visited;\n    queue<tuple<int,int,int>> q;\n    \n    q.push(cubes[0]);\n    visited.insert(cubes[0]);\n    \n    while (!q.empty()) {\n        auto [x, y, z] = q.front();\n        q.pop();\n        \n        for (int i = 0; i < 6; i++) {\n            int nx = x + dx[i];\n            int ny = y + dy[i];\n            int nz = z + dz[i];\n            tuple<int,int,int> neighbor(nx, ny, nz);\n            \n            if (cube_set.count(neighbor) && !visited.count(neighbor)) {\n                visited.insert(neighbor);\n                q.push(neighbor);\n            }\n        }\n    }\n    \n    return visited.size() == cubes.size();\n}\n\n// Generate simple connected blocks\nvector<vector<tuple<int,int,int>>> generateSimpleBlocks(int max_size) {\n    vector<vector<tuple<int,int,int>>> blocks;\n    \n    // Block 1: Single cube\n    blocks.push_back({{0, 0, 0}});\n    \n    // Block 2: 1x2 line\n    blocks.push_back({{0, 0, 0}, {1, 0, 0}});\n    \n    // Block 3: 1x3 line\n    blocks.push_back({{0, 0, 0}, {1, 0, 0}, {2, 0, 0}});\n    \n    // Block 4: 2x2 square\n    blocks.push_back({{0, 0, 0}, {1, 0, 0}, {0, 1, 0}, {1, 1, 0}});\n    \n    // Block 5: L-shape\n    blocks.push_back({{0, 0, 0}, {1, 0, 0}, {0, 1, 0}});\n    \n    // Block 6: 1x1x2 tower\n    blocks.push_back({{0, 0, 0}, {0, 0, 1}});\n    \n    return blocks;\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int D;\n    cin >> D;\n    \n    vector<vector<int>> f1(D, vector<int>(D));\n    vector<vector<int>> r1(D, vector<int>(D));\n    vector<vector<int>> f2(D, vector<int>(D));\n    vector<vector<int>> r2(D, vector<int>(D));\n    \n    // Read first silhouette pair\n    for (int i = 0; i < D; i++) {\n        string line;\n        cin >> line;\n        for (int j = 0; j < D; j++) {\n            f1[i][j] = line[j] - '0';\n        }\n    }\n    for (int i = 0; i < D; i++) {\n        string line;\n        cin >> line;\n        for (int j = 0; j < D; j++) {\n            r1[i][j] = line[j] - '0';\n        }\n    }\n    \n    // Read second silhouette pair\n    for (int i = 0; i < D; i++) {\n        string line;\n        cin >> line;\n        for (int j = 0; j < D; j++) {\n            f2[i][j] = line[j] - '0';\n        }\n    }\n    for (int i = 0; i < D; i++) {\n        string line;\n        cin >> line;\n        for (int j = 0; j < D; j++) {\n            r2[i][j] = line[j] - '0';\n        }\n    }\n    \n    // Generate some simple connected blocks\n    auto blocks = generateSimpleBlocks(4);\n    \n    // Create 3D solution arrays\n    vector<vector<vector<int>>> sol1(D, vector<vector<int>>(D, vector<int>(D, 0)));\n    vector<vector<vector<int>>> sol2(D, vector<vector<int>>(D, vector<int>(D, 0)));\n    \n    // Simple approach: place single cube blocks at required positions\n    int block_id = 1;\n    \n    // For solution 1\n    for (int z = 0; z < D; z++) {\n        for (int x = 0; x < D; x++) {\n            if (f1[z][x] == 1) {\n                for (int y = 0; y < D; y++) {\n                    if (r1[z][y] == 1) {\n                        sol1[z][x][y] = block_id;\n                        block_id++;\n                    }\n                }\n            }\n        }\n    }\n    \n    int max_block_id = block_id - 1;\n    \n    // For solution 2, reuse some blocks where possible\n    block_id = 1;\n    for (int z = 0; z < D; z++) {\n        for (int x = 0; x < D; x++) {\n            if (f2[z][x] == 1) {\n                for (int y = 0; y < D; y++) {\n                    if (r2[z][y] == 1) {\n                        sol2[z][x][y] = block_id;\n                        block_id++;\n                    }\n                }\n            }\n        }\n    }\n    \n    // Take maximum of both\n    int total_blocks = max(max_block_id, block_id - 1);\n    \n    // But let's be more sophisticated - use actual connected blocks\n    // Reset and do it properly\n    for (int i = 0; i < D; i++) {\n        for (int j = 0; j < D; j++) {\n            for (int k = 0; k < D; k++) {\n                sol1[i][j][k] = 0;\n                sol2[i][j][k] = 0;\n            }\n        }\n    }\n    \n    // Use single cubes for simplicity (each is a valid connected block)\n    block_id = 1;\n    \n    // Solution 1: place single cube blocks\n    for (int z = 0; z < D; z++) {\n        for (int x = 0; x < D; x++) {\n            if (f1[z][x] == 1) {\n                for (int y = 0; y < D; y++) {\n                    if (r1[z][y] == 1) {\n                        sol1[z][x][y] = block_id;\n                        block_id++;\n                    }\n                }\n            }\n        }\n    }\n    \n    int blocks_for_sol1 = block_id - 1;\n    \n    // Solution 2: place single cube blocks\n    int start_id = block_id;\n    for (int z = 0; z < D; z++) {\n        for (int x = 0; x < D; x++) {\n            if (f2[z][x] == 1) {\n                for (int y = 0; y < D; y++) {\n                    if (r2[z][y] == 1) {\n                        sol2[z][x][y] = block_id;\n                        block_id++;\n                    }\n                }\n            }\n        }\n    }\n    \n    total_blocks = block_id - 1;\n    \n    cout << total_blocks << \"\\n\";\n    \n    // Output first solution\n    bool first = true;\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 (!first) cout << \" \";\n                cout << sol1[z][x][y];\n                first = false;\n            }\n        }\n    }\n    cout << \"\\n\";\n    \n    // Output second solution\n    first = true;\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 (!first) cout << \" \";\n                cout << sol2[z][x][y];\n                first = false;\n            }\n        }\n    }\n    cout << \"\\n\";\n    \n    return 0;\n}","ahc020":"#include <bits/stdc++.h>\n#include <atcoder/all>\nusing namespace std;\nusing namespace atcoder;\n\nusing ll = long long;\nusing ld = long double;\n\nconst int MAXN = 105;\nconst int MAXM = 305;\nconst int MAXK = 5005;\n\nint N, M, K;\npair<int, int> vertices[MAXN];\ntuple<int, int, int> edges[MAXM];\npair<int, int> residents[MAXK];\nint P[MAXN];\nint B[MAXM];\nvector<vector<int>> adj[MAXN];\nvector<pair<int, int>> edge_list;\n\n// Distance calculation\nll dist_sq(int x1, int y1, int x2, int y2) {\n    return 1LL * (x1 - x2) * (x1 - x2) + 1LL * (y1 - y2) * (y1 - y2);\n}\n\nld dist(int x1, int y1, int x2, int y2) {\n    return sqrt(dist_sq(x1, y1, x2, y2));\n}\n\n// Find closest vertex to a resident\nint find_closest_vertex(int rx, int ry) {\n    int best = 0;\n    ll best_dist = dist_sq(rx, ry, vertices[0].first, vertices[0].second);\n    for (int i = 1; i < N; i++) {\n        ll d = dist_sq(rx, ry, vertices[i].first, vertices[i].second);\n        if (d < best_dist) {\n            best_dist = d;\n            best = i;\n        }\n    }\n    return best;\n}\n\n// Calculate required radius to cover residents from a vertex\nint calculate_radius(int vertex_idx, const vector<int>& assigned_residents) {\n    if (assigned_residents.empty()) return 0;\n    \n    int vx = vertices[vertex_idx].first;\n    int vy = vertices[vertex_idx].second;\n    int max_dist = 0;\n    \n    for (int res_idx : assigned_residents) {\n        int rx = residents[res_idx].first;\n        int ry = residents[res_idx].second;\n        int d = (int)round(dist(vx, vy, rx, ry));\n        max_dist = max(max_dist, d);\n    }\n    \n    return min(max_dist, 5000);\n}\n\n// Prim's algorithm for minimum spanning tree\nvector<bool> find_minimal_network(const vector<bool>& must_include) {\n    vector<bool> in_tree(N, false);\n    vector<ll> min_cost(N, 1e18);\n    vector<int> parent(N, -1);\n    \n    // Start from vertex 0 (AtCoder office)\n    in_tree[0] = true;\n    min_cost[0] = 0;\n    \n    // Initialize costs for adjacent vertices\n    vector<pair<int, int>> adj_edges[N];\n    for (int i = 0; i < M; i++) {\n        int u = get<0>(edges[i]) - 1;\n        int v = get<1>(edges[i]) - 1;\n        int w = get<2>(edges[i]);\n        adj_edges[u].push_back({v, w});\n        adj_edges[v].push_back({u, w});\n    }\n    \n    priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<>> pq;\n    \n    for (auto& e : adj_edges[0]) {\n        int to = e.first;\n        int weight = e.second;\n        if (min_cost[to] > weight) {\n            min_cost[to] = weight;\n            parent[to] = 0;\n            pq.push({min_cost[to], to});\n        }\n    }\n    \n    while (!pq.empty()) {\n        auto [cost, u] = pq.top(); pq.pop();\n        if (in_tree[u]) continue;\n        in_tree[u] = true;\n        \n        for (auto& e : adj_edges[u]) {\n            int to = e.first;\n            int weight = e.second;\n            if (!in_tree[to] && min_cost[to] > weight) {\n                min_cost[to] = weight;\n                parent[to] = u;\n                pq.push({min_cost[to], to});\n            }\n        }\n    }\n    \n    // Ensure all must_include vertices are connected\n    vector<bool> result(N, false);\n    for (int i = 0; i < N; i++) {\n        if (must_include[i]) {\n            int current = i;\n            while (current != -1 && !result[current]) {\n                result[current] = true;\n                current = parent[current];\n            }\n        }\n    }\n    \n    // Always include vertex 0\n    result[0] = true;\n    \n    // Connect components if needed\n    queue<int> q;\n    for (int i = 0; i < N; i++) {\n        if (result[i]) q.push(i);\n    }\n    \n    vector<bool> visited(N, false);\n    while (!q.empty()) {\n        int u = q.front(); q.pop();\n        if (visited[u]) continue;\n        visited[u] = true;\n        \n        for (auto& e : adj_edges[u]) {\n            int v = e.first;\n            if (!visited[v]) {\n                q.push(v);\n                result[v] = true;\n            }\n        }\n    }\n    \n    return result;\n}\n\nvoid solve() {\n    // Assign residents to closest vertices\n    vector<vector<int>> vertex_residents(N);\n    for (int i = 0; i < K; i++) {\n        int closest = find_closest_vertex(residents[i].first, residents[i].second);\n        vertex_residents[closest].push_back(i);\n    }\n    \n    // Determine which vertices we might want to include\n    vector<bool> should_include(N, false);\n    vector<int> required_radius(N, 0);\n    \n    for (int i = 0; i < N; i++) {\n        if (!vertex_residents[i].empty()) {\n            required_radius[i] = calculate_radius(i, vertex_residents[i]);\n            if (required_radius[i] > 0) {\n                should_include[i] = true;\n            }\n        }\n    }\n    \n    // Find minimal network to connect necessary vertices\n    vector<bool> network = find_minimal_network(should_include);\n    \n    // Calculate final output strengths\n    fill(P, P + N, 0);\n    for (int i = 0; i < N; i++) {\n        if (network[i]) {\n            P[i] = required_radius[i];\n        }\n    }\n    \n    // Set edge states\n    fill(B, B + M, 0);\n    vector<vector<pair<int, int>>> graph_adj(N);\n    for (int i = 0; i < M; i++) {\n        int u = get<0>(edges[i]) - 1;\n        int v = get<1>(edges[i]) - 1;\n        if (network[u] && network[v]) {\n            graph_adj[u].push_back({v, i});\n            graph_adj[v].push_back({u, i});\n        }\n    }\n    \n    // Build spanning tree on active vertices\n    vector<bool> visited(N, false);\n    queue<int> q;\n    q.push(0);\n    visited[0] = true;\n    \n    while (!q.empty()) {\n        int u = q.front(); q.pop();\n        for (auto& [v, edge_idx] : graph_adj[u]) {\n            if (!visited[v]) {\n                visited[v] = true;\n                B[edge_idx] = 1;\n                q.push(v);\n            }\n        }\n    }\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    cin >> N >> M >> K;\n    \n    for (int i = 0; i < N; i++) {\n        cin >> vertices[i].first >> vertices[i].second;\n    }\n    \n    for (int i = 0; i < M; i++) {\n        int u, v, w;\n        cin >> u >> v >> w;\n        edges[i] = {u, v, w};\n    }\n    \n    for (int i = 0; i < K; i++) {\n        cin >> residents[i].first >> residents[i].second;\n    }\n    \n    solve();\n    \n    for (int i = 0; i < N; i++) {\n        cout << P[i];\n        if (i + 1 < N) cout << \" \";\n    }\n    cout << \"\\n\";\n    \n    for (int i = 0; i < M; i++) {\n        cout << B[i];\n        if (i + 1 < M) cout << \" \";\n    }\n    cout << \"\\n\";\n    \n    return 0;\n}","ahc021":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 30;\nconst int MAX_OPS = 10000;\n\nint pyramid[N][N];\nvector<tuple<int, int, int, int>> operations;\n\n// Check if coordinates are valid\nbool valid(int x, int y) {\n    return x >= 0 && x < N && y >= 0 && y <= x;\n}\n\n// Perform a swap and record operation\nvoid performSwap(int x1, int y1, int x2, int y2) {\n    if (operations.size() >= MAX_OPS) return;\n    \n    swap(pyramid[x1][y1], pyramid[x2][y2]);\n    operations.push_back({x1, y1, x2, y2});\n}\n\n// Count violations in current state\nint countViolations() {\n    int violations = 0;\n    for (int x = 0; x < N - 1; x++) {\n        for (int y = 0; y <= x; y++) {\n            if (pyramid[x][y] > pyramid[x+1][y]) violations++;\n            if (pyramid[x][y] > pyramid[x+1][y+1]) violations++;\n        }\n    }\n    return violations;\n}\n\n// Simple but effective approach: multiple passes of heap fixing\nvoid fixHeapMultiplePasses() {\n    // Do multiple passes, each getting more aggressive\n    for (int pass = 0; pass < 8 && operations.size() < MAX_OPS - 100; pass++) {\n        bool madeProgress = false;\n        \n        // Bottom-up pass\n        for (int x = N - 2; x >= 0; x--) {\n            for (int y = 0; y <= x && operations.size() < MAX_OPS - 10; y++) {\n                if (x < N - 1) {\n                    // Check if current node violates heap property\n                    bool violation1 = pyramid[x][y] > pyramid[x+1][y];\n                    bool violation2 = pyramid[x][y] > pyramid[x+1][y+1];\n                    \n                    if (violation1 || violation2) {\n                        madeProgress = true;\n                        // Swap with the smaller child\n                        if (violation1 && violation2) {\n                            if (pyramid[x+1][y] <= pyramid[x+1][y+1]) {\n                                performSwap(x, y, x+1, y);\n                            } else {\n                                performSwap(x, y, x+1, y+1);\n                            }\n                        } else if (violation1) {\n                            performSwap(x, y, x+1, y);\n                        } else if (violation2) {\n                            performSwap(x, y, x+1, y+1);\n                        }\n                    }\n                }\n            }\n        }\n        \n        // If no progress was made, we might be stuck - try a different approach\n        if (!madeProgress && pass > 3) {\n            // Try top-down pass to break local minima\n            for (int x = 0; x < N - 1 && operations.size() < MAX_OPS - 10; x++) {\n                for (int y = 0; y <= x && operations.size() < MAX_OPS - 10; y++) {\n                    if (x < N - 1) {\n                        if (pyramid[x][y] > pyramid[x+1][y]) {\n                            performSwap(x, y, x+1, y);\n                        }\n                        if (pyramid[x][y] > pyramid[x+1][y+1]) {\n                            performSwap(x, y, x+1, y+1);\n                        }\n                    }\n                }\n            }\n        }\n    }\n}\n\n// Aggressive optimization for remaining violations\nvoid aggressiveOptimization() {\n    if (operations.size() < MAX_OPS - 500) {\n        // Collect all current violations\n        vector<pair<int, int>> violationParents;\n        for (int x = 0; x < N - 1; x++) {\n            for (int y = 0; y <= x; y++) {\n                if (pyramid[x][y] > pyramid[x+1][y] || pyramid[x][y] > pyramid[x+1][y+1]) {\n                    violationParents.push_back({x, y});\n                }\n            }\n        }\n        \n        // Try to fix each violation multiple times\n        for (auto& parent : violationParents) {\n            if (operations.size() >= MAX_OPS - 20) break;\n            \n            int x = parent.first, y = parent.second;\n            \n            // Try up to 5 times to fix this violation\n            for (int attempt = 0; attempt < 5 && operations.size() < MAX_OPS - 5; attempt++) {\n                bool violation1 = (x + 1 < N && y <= x && pyramid[x][y] > pyramid[x+1][y]);\n                bool violation2 = (x + 1 < N && y + 1 <= x + 1 && pyramid[x][y] > pyramid[x+1][y+1]);\n                \n                if (!violation1 && !violation2) break; // Fixed\n                \n                if (violation1 && violation2) {\n                    if (pyramid[x+1][y] <= pyramid[x+1][y+1]) {\n                        performSwap(x, y, x+1, y);\n                    } else {\n                        performSwap(x, y, x+1, y+1);\n                    }\n                } else if (violation1) {\n                    performSwap(x, y, x+1, y);\n                } else if (violation2) {\n                    performSwap(x, y, x+1, y+1);\n                }\n            }\n        }\n    }\n}\n\n// Final cleanup pass\nvoid finalCleanup() {\n    if (operations.size() < MAX_OPS - 50) {\n        // One final thorough pass\n        for (int x = 0; x < N - 1 && operations.size() < MAX_OPS - 5; x++) {\n            for (int y = 0; y <= x && operations.size() < MAX_OPS - 5; y++) {\n                if (x < N - 1) {\n                    if (pyramid[x][y] > pyramid[x+1][y]) {\n                        performSwap(x, y, x+1, y);\n                    }\n                    if (pyramid[x][y] > pyramid[x+1][y+1]) {\n                        performSwap(x, y, x+1, y+1);\n                    }\n                }\n            }\n        }\n    }\n}\n\nint main() {\n    // Read input\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j <= i; j++) {\n            cin >> pyramid[i][j];\n        }\n    }\n    \n    // Apply main optimization strategy\n    fixHeapMultiplePasses();\n    \n    // Aggressive optimization if we have room\n    if (operations.size() < MAX_OPS - 300) {\n        aggressiveOptimization();\n    }\n    \n    // Final cleanup\n    if (operations.size() < MAX_OPS - 20) {\n        finalCleanup();\n    }\n    \n    // Output results\n    cout << operations.size() << \"\\n\";\n    for (auto& op : operations) {\n        cout << get<0>(op) << \" \" << get<1>(op) << \" \" << get<2>(op) << \" \" << get<3>(op) << \"\\n\";\n    }\n    \n    return 0;\n}","toyota2023summer-final":"#include <bits/stdc++.h>\n#include <atcoder/all>\nusing namespace std;\nusing namespace atcoder;\n\nconst int D = 9;\nconst int INF = 100000000;\n\nint entrance_i = 0;\nint entrance_j = 4;\n\nvector<vector<bool>> is_obstacle(D, vector<bool>(D, false));\nvector<vector<int>> container_id(D, vector<int>(D, -1));\nvector<pair<int, int>> container_positions(D * D); // id -> position\nvector<int> container_numbers(D * D); // position index -> container number\n\n// Directions: up, down, left, right\nconst int di[4] = {-1, 1, 0, 0};\nconst int dj[4] = {0, 0, -1, 1};\n\n// Check if position is valid\nbool is_valid(int i, int j) {\n    return i >= 0 && i < D && j >= 0 && j < D;\n}\n\n// Check if a position is the entrance or adjacent to entrance\nbool is_entrance_area(int i, int j) {\n    if (i == 0 && j == 4) return true;\n    if (i == 0 && j == 3) return true;\n    if (i == 0 && j == 5) return true;\n    if (i == 1 && j == 4) return true;\n    return false;\n}\n\n// BFS to find all reachable positions from entrance\nvector<vector<int>> get_distances_from_entrance() {\n    vector<vector<int>> dist(D, vector<int>(D, INF));\n    queue<pair<int, int>> q;\n    \n    // Start from entrance\n    dist[0][4] = 0;\n    q.push({0, 4});\n    \n    while (!q.empty()) {\n        auto [ci, cj] = q.front(); q.pop();\n        \n        for (int d = 0; d < 4; d++) {\n            int ni = ci + di[d];\n            int nj = cj + dj[d];\n            \n            if (is_valid(ni, nj) && !is_obstacle[ni][nj] && container_id[ni][nj] == -1) {\n                if (dist[ni][nj] > dist[ci][cj] + 1) {\n                    dist[ni][nj] = dist[ci][cj] + 1;\n                    q.push({ni, nj});\n                }\n            }\n        }\n    }\n    \n    return dist;\n}\n\n// Find all reachable empty positions\nvector<pair<int, int>> get_reachable_empty_positions() {\n    vector<vector<bool>> visited(D, vector<bool>(D, false));\n    vector<pair<int, int>> result;\n    queue<pair<int, int>> q;\n    \n    // Start from entrance\n    visited[0][4] = true;\n    q.push({0, 4});\n    if (container_id[0][4] == -1 && !is_obstacle[0][4]) {\n        result.push_back({0, 4});\n    }\n    \n    while (!q.empty()) {\n        auto [ci, cj] = q.front(); q.pop();\n        \n        for (int d = 0; d < 4; d++) {\n            int ni = ci + di[d];\n            int nj = cj + dj[d];\n            \n            if (is_valid(ni, nj) && !visited[ni][nj] && !is_obstacle[ni][nj]) {\n                visited[ni][nj] = true;\n                q.push({ni, nj});\n                if (container_id[ni][nj] == -1) {\n                    result.push_back({ni, nj});\n                }\n            }\n        }\n    }\n    \n    return result;\n}\n\n// Calculate Manhattan distance from entrance\nint manhattan_distance(int i, int j) {\n    return abs(i - 0) + abs(j - 4);\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int N;\n    cin >> D >> N;\n    \n    for (int i = 0; i < N; i++) {\n        int ri, rj;\n        cin >> ri >> rj;\n        is_obstacle[ri][rj] = true;\n    }\n    \n    int total_containers = D * D - 1 - N;\n    vector<int> transport_order(total_containers);\n    \n    // Process each container as it arrives\n    for (int d = 0; d < total_containers; d++) {\n        int t_d;\n        cin >> t_d;\n        transport_order[d] = t_d;\n        \n        // Get all reachable empty positions\n        auto reachable_positions = get_reachable_empty_positions();\n        \n        // Remove entrance area from consideration (except if it's the only option)\n        vector<pair<int, int>> valid_positions;\n        for (auto& pos : reachable_positions) {\n            if (!is_entrance_area(pos.first, pos.second)) {\n                valid_positions.push_back(pos);\n            }\n        }\n        \n        // If no valid positions except entrance area, use entrance area\n        if (valid_positions.empty()) {\n            for (auto& pos : reachable_positions) {\n                if (pos.first != 0 || pos.second != 4) { // exclude exact entrance\n                    valid_positions.push_back(pos);\n                }\n            }\n        }\n        \n        // If still empty, just use entrance\n        if (valid_positions.empty()) {\n            valid_positions.push_back({0, 4});\n        }\n        \n        // Choose position based on container number\n        // Lower numbers should be placed closer to entrance for easier retrieval\n        pair<int, int> best_position = valid_positions[0];\n        int best_score = INF;\n        \n        for (auto& pos : valid_positions) {\n            int dist = manhattan_distance(pos.first, pos.second);\n            // Prefer positions closer to entrance for lower numbered containers\n            // But also add some randomness to avoid getting stuck\n            int score = dist * 1000 + (t_d * 10) + (rand() % 100);\n            \n            if (score < best_score) {\n                best_score = score;\n                best_position = pos;\n            }\n        }\n        \n        // Place the container\n        int pi = best_position.first;\n        int pj = best_position.second;\n        container_id[pi][pj] = d;\n        container_positions[d] = {pi, pj};\n        container_numbers[d] = t_d;\n        \n        // Output the position\n        cout << pi << \" \" << pj << endl;\n        cout.flush();\n    }\n    \n    // Now determine transport order\n    // We want to transport in order of assigned numbers (0, 1, 2, ...)\n    // But we can only transport reachable containers\n    \n    vector<bool> transported(total_containers, false);\n    vector<pair<int, int>> transport_sequence;\n    \n    for (int target_number = 0; target_number < total_containers; target_number++) {\n        // Find container with target_number\n        int container_idx = -1;\n        for (int i = 0; i < total_containers; i++) {\n            if (container_numbers[i] == target_number) {\n                container_idx = i;\n                break;\n            }\n        }\n        \n        if (container_idx == -1) continue;\n        \n        // Mark this container for transport when it becomes reachable\n        pair<int, int> target_pos = container_positions[container_idx];\n        \n        // For simplicity, we'll transport containers in the order they become reachable\n        // In a more sophisticated solution, we'd use a priority queue\n        transport_sequence.push_back(target_pos);\n    }\n    \n    // Actually, let's implement a better approach:\n    // Try to transport containers in order of their numbers, but only when reachable\n    vector<bool> container_transported(total_containers, false);\n    transport_sequence.clear();\n    \n    while (transport_sequence.size() < total_containers) {\n        // Get currently reachable containers\n        auto distances = get_distances_from_entrance();\n        vector<pair<int, int>> reachable_containers;\n        vector<int> reachable_indices;\n        \n        for (int i = 0; i < total_containers; i++) {\n            if (container_transported[i]) continue;\n            \n            auto pos = container_positions[i];\n            if (distances[pos.first][pos.second] < INF) {\n                reachable_containers.push_back(pos);\n                reachable_indices.push_back(i);\n            }\n        }\n        \n        if (reachable_containers.empty()) {\n            // This shouldn't happen if the problem is solvable\n            break;\n        }\n        \n        // Among reachable containers, pick the one with lowest assigned number\n        int best_idx = reachable_indices[0];\n        int best_number = container_numbers[best_idx];\n        \n        for (int idx : reachable_indices) {\n            if (container_numbers[idx] < best_number) {\n                best_number = container_numbers[idx];\n                best_idx = idx;\n            }\n        }\n        \n        // Transport this container\n        auto pos = container_positions[best_idx];\n        transport_sequence.push_back(pos);\n        container_transported[best_idx] = true;\n        \n        // Clear the position\n        container_id[pos.first][pos.second] = -1;\n    }\n    \n    // Output transport sequence\n    for (auto& pos : transport_sequence) {\n        cout << pos.first << \" \" << pos.second << \"\\n\";\n    }\n    \n    return 0;\n}","ahc024":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 50;\nconst int M = 100;\n\nint grid[N][N];\nint result[N][N];\nvector<pair<int,int>> positions[M+1]; // positions for each color\nvector<int> adj_list[M+1]; // adjacency list\nbool adj_matrix[M+1][M+1]; // adjacency matrix\n\n// Directions: up, right, down, left\nconst int dx[4] = {-1, 0, 1, 0};\nconst int dy[4] = {0, 1, 0, -1};\n\nvoid extract_adjacencies() {\n    // Initialize adjacency matrix\n    memset(adj_matrix, 0, sizeof(adj_matrix));\n    \n    // Check adjacencies in the original grid\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            int color = grid[i][j];\n            for (int d = 0; d < 4; d++) {\n                int ni = i + dx[d];\n                int nj = j + dy[d];\n                int neighbor_color = 0; // outside is color 0\n                if (ni >= 0 && ni < N && nj >= 0 && nj < N) {\n                    neighbor_color = grid[ni][nj];\n                }\n                if (color != neighbor_color) {\n                    adj_matrix[color][neighbor_color] = true;\n                    adj_matrix[neighbor_color][color] = true;\n                }\n            }\n        }\n    }\n    \n    // Build adjacency lists\n    for (int i = 0; i <= M; i++) {\n        adj_list[i].clear();\n        for (int j = 0; j <= M; j++) {\n            if (adj_matrix[i][j] && i != j) {\n                adj_list[i].push_back(j);\n            }\n        }\n    }\n}\n\n// Check if we can place a color at position (x,y)\nbool can_place(int x, int y, int color) {\n    if (x < 0 || x >= N || y < 0 || y >= N) return false;\n    return result[x][y] == 0;\n}\n\n// Check if placement maintains adjacency requirements\nbool check_adjacency(int x, int y, int color) {\n    // Check if this placement creates required adjacencies\n    for (int d = 0; d < 4; d++) {\n        int nx = x + dx[d];\n        int ny = y + dy[d];\n        int neighbor = 0;\n        if (nx >= 0 && nx < N && ny >= 0 && ny < N) {\n            neighbor = result[nx][ny];\n        }\n        \n        // If neighbor is a color that should be adjacent, that's good\n        // If neighbor is a color that shouldn't be adjacent, that's bad (but we can't know all adjacencies yet)\n    }\n    return true;\n}\n\nvoid place_color_compactly(int color) {\n    if (color == 0) return; // Don't place color 0, it's empty space\n    \n    // If this color is already placed, skip\n    if (!positions[color].empty()) return;\n    \n    // Find a good starting position - near already placed colors that it should be adjacent to\n    vector<pair<int,int>> candidates;\n    \n    // If no colors placed yet, start in the middle\n    if (positions[1].empty()) {\n        int cx = N/2, cy = N/2;\n        result[cx][cy] = color;\n        positions[color].push_back({cx, cy});\n        return;\n    }\n    \n    // Try to place near adjacencies\n    bool placed = false;\n    for (int adj_color : adj_list[color]) {\n        if (!positions[adj_color].empty()) {\n            // Try to place near this adjacent color\n            for (auto& pos : positions[adj_color]) {\n                int x = pos.first, y = pos.second;\n                for (int d = 0; d < 4; d++) {\n                    int nx = x + dx[d];\n                    int ny = y + dy[d];\n                    if (nx >= 0 && nx < N && ny >= 0 && ny < N && result[nx][ny] == 0) {\n                        result[nx][ny] = color;\n                        positions[color].push_back({nx, ny});\n                        placed = true;\n                        break;\n                    }\n                }\n                if (placed) break;\n            }\n            if (placed) break;\n        }\n    }\n    \n    // If couldn't place near adjacencies, place anywhere available\n    if (!placed) {\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                if (result[i][j] == 0) {\n                    result[i][j] = color;\n                    positions[color].push_back({i, j});\n                    placed = true;\n                    break;\n                }\n            }\n            if (placed) break;\n        }\n    }\n    \n    // Expand the color to make it connected\n    if (!positions[color].empty()) {\n        // Simple expansion to make it connected - just place one tile\n        // In a more sophisticated solution, we'd expand to match the required size\n        // But since we're optimizing for fewer empty tiles, smaller is better\n    }\n}\n\n// Greedy approach: place colors in order of how many adjacencies they have\nvoid solve() {\n    // Initialize result grid\n    memset(result, 0, sizeof(result));\n    for (int i = 0; i <= M; i++) {\n        positions[i].clear();\n    }\n    \n    // Create ordering based on adjacency count (place high-degree nodes first)\n    vector<pair<int, int>> order; // {adjacency_count, color}\n    for (int i = 1; i <= M; i++) {\n        order.push_back({(int)adj_list[i].size(), i});\n    }\n    sort(order.rbegin(), order.rend()); // descending order\n    \n    // Place colors\n    for (auto& p : order) {\n        place_color_compactly(p.second);\n    }\n    \n    // Ensure connectivity for each color\n    for (int color = 1; color <= M; color++) {\n        if (positions[color].empty()) {\n            // Place at least one tile\n            for (int i = 0; i < N; i++) {\n                for (int j = 0; j < N; j++) {\n                    if (result[i][j] == 0) {\n                        result[i][j] = color;\n                        positions[color].push_back({i, j});\n                        goto placed;\n                    }\n                }\n            }\n            placed:;\n        }\n    }\n}\n\n// Better approach: create a compact layout\nvoid solve_better() {\n    // Initialize result grid\n    memset(result, 0, sizeof(result));\n    for (int i = 0; i <= M; i++) {\n        positions[i].clear();\n    }\n    \n    // Start by placing color 1 in the center\n    int center_x = N/2, center_y = N/2;\n    result[center_x][center_y] = 1;\n    positions[1].push_back({center_x, center_y});\n    \n    // Place other colors in a spiral pattern to minimize empty space\n    vector<pair<int,int>> spiral_dirs = {{0,1}, {1,0}, {0,-1}, {-1,0}}; // right, down, left, up\n    int dir = 0;\n    int x = center_x, y = center_y;\n    int steps = 1;\n    \n    vector<int> colors_to_place;\n    for (int i = 2; i <= M; i++) {\n        colors_to_place.push_back(i);\n    }\n    \n    int color_idx = 0;\n    while (color_idx < (int)colors_to_place.size()) {\n        for (int step = 0; step < 2; step++) {\n            for (int i = 0; i < steps && color_idx < (int)colors_to_place.size(); i++) {\n                x += spiral_dirs[dir].first;\n                y += spiral_dirs[dir].second;\n                \n                if (x >= 0 && x < N && y >= 0 && y < N && result[x][y] == 0) {\n                    int color = colors_to_place[color_idx];\n                    result[x][y] = color;\n                    positions[color].push_back({x, y});\n                    color_idx++;\n                }\n                \n                if (color_idx >= (int)colors_to_place.size()) break;\n            }\n            dir = (dir + 1) % 4;\n            if (color_idx >= (int)colors_to_place.size()) break;\n        }\n        steps++;\n    }\n    \n    // This approach doesn't preserve adjacencies, so let's try a different approach\n}\n\nvoid solve_best() {\n    // Initialize result grid\n    memset(result, 0, sizeof(result));\n    for (int i = 0; i <= M; i++) {\n        positions[i].clear();\n    }\n    \n    // Simple but effective: place colors in a dense grid pattern\n    int idx = 0;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (idx < M) {\n                int color = idx + 1;\n                result[i][j] = color;\n                positions[color].push_back({i, j});\n                idx++;\n            } else {\n                // Fill remaining with a pattern to maintain some adjacencies\n                result[i][j] = 0;\n            }\n        }\n    }\n    \n    // This still doesn't preserve the original adjacencies\n    // Let's try a more sophisticated approach\n}\n\n// Let's go with a simple but effective approach\nvoid solve_simple() {\n    // Initialize result grid\n    memset(result, 0, sizeof(result));\n    for (int i = 0; i <= M; i++) {\n        positions[i].clear();\n    }\n    \n    // Place colors in a way that tries to preserve adjacencies\n    // Place first color in center\n    result[N/2][N/2] = 1;\n    positions[1].push_back({N/2, N/2});\n    \n    // For remaining colors, place them near colors they should be adjacent to\n    vector<bool> placed(M+1, false);\n    placed[0] = true; // color 0 is special\n    placed[1] = true;\n    \n    queue<int> to_process;\n    to_process.push(1);\n    \n    while (!to_process.empty()) {\n        int current_color = to_process.front();\n        to_process.pop();\n        \n        // Try to place adjacent colors\n        for (int adj_color : adj_list[current_color]) {\n            if (!placed[adj_color] && adj_color != 0) {\n                // Try to place adj_color near current_color\n                bool placed_adj = false;\n                for (auto& pos : positions[current_color]) {\n                    int x = pos.first, y = pos.second;\n                    // Try 8 directions (including diagonals for more flexibility)\n                    for (int dx_val = -1; dx_val <= 1; dx_val++) {\n                        for (int dy_val = -1; dy_val <= 1; dy_val++) {\n                            if (dx_val == 0 && dy_val == 0) continue;\n                            int nx = x + dx_val;\n                            int ny = y + dy_val;\n                            if (nx >= 0 && nx < N && ny >= 0 && ny < N && result[nx][ny] == 0) {\n                                result[nx][ny] = adj_color;\n                                positions[adj_color].push_back({nx, ny});\n                                placed[adj_color] = true;\n                                to_process.push(adj_color);\n                                placed_adj = true;\n                                break;\n                            }\n                        }\n                        if (placed_adj) break;\n                    }\n                    if (placed_adj) break;\n                }\n                \n                // If we couldn't place it near an adjacent color, place it anywhere\n                if (!placed_adj) {\n                    for (int i = 0; i < N; i++) {\n                        for (int j = 0; j < N; j++) {\n                            if (result[i][j] == 0) {\n                                result[i][j] = adj_color;\n                                positions[adj_color].push_back({i, j});\n                                placed[adj_color] = true;\n                                to_process.push(adj_color);\n                                placed_adj = true;\n                                break;\n                            }\n                        }\n                        if (placed_adj) break;\n                    }\n                }\n            }\n        }\n    }\n    \n    // Place any remaining unplaced colors\n    for (int color = 1; color <= M; color++) {\n        if (!placed[color]) {\n            for (int i = 0; i < N; i++) {\n                for (int j = 0; j < N; j++) {\n                    if (result[i][j] == 0) {\n                        result[i][j] = color;\n                        positions[color].push_back({i, j});\n                        placed[color] = true;\n                        break;\n                    }\n                }\n                if (placed[color]) break;\n            }\n        }\n    }\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    int n, m;\n    cin >> n >> m;\n    \n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            cin >> grid[i][j];\n        }\n    }\n    \n    extract_adjacencies();\n    solve_simple();\n    \n    // Output result\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            if (j > 0) cout << \" \";\n            cout << result[i][j];\n        }\n        cout << \"\\n\";\n    }\n    \n    return 0;\n}","ahc025":"#include <bits/stdc++.h>\n#include <Eigen/Dense>\n\nusing namespace std;\n\n// Global variables\nint N, D, Q;\nvector<double> weight_estimates;\nvector<vector<pair<int, string>>> comparison_history;\n\n// Compare two individual items\npair<vector<int>, vector<int>> make_item_comparison(int item1, int item2) {\n    return {{item1}, {item2}};\n}\n\n// Compare groups of items\npair<vector<int>, vector<int>> make_group_comparison(const vector<int>& group1, const vector<int>& group2) {\n    return {group1, group2};\n}\n\nvoid solve() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    // Read input\n    cin >> N >> D >> Q;\n    \n    // Initialize data structures\n    weight_estimates.assign(N, 1.0);\n    comparison_history.resize(N);\n    \n    // Store all queries for processing\n    vector<tuple<vector<int>, vector<int>, string>> all_queries;\n    \n    // Strategy: Use first half of queries for exploration, second half for refinement\n    int exploration_queries = Q * 2 / 3;\n    \n    // Phase 1: Exploration - systematic pairwise comparisons\n    vector<pair<int, int>> item_pairs;\n    for (int i = 0; i < N; i++) {\n        for (int j = i + 1; j < N; j++) {\n            item_pairs.push_back({i, j});\n        }\n    }\n    \n    // Shuffle to randomize\n    random_shuffle(item_pairs.begin(), item_pairs.end());\n    \n    int queries_used = 0;\n    \n    // Use some queries for pairwise comparisons\n    for (int i = 0; i < min((int)item_pairs.size(), exploration_queries / 2) && queries_used < exploration_queries; i++) {\n        int item1 = item_pairs[i].first;\n        int item2 = item_pairs[i].second;\n        \n        cout << \"1 1 \" << item1 << \" \" << item2 << endl;\n        cout.flush();\n        \n        string response;\n        cin >> response;\n        \n        all_queries.push_back({{item1}, {item2}, response});\n        queries_used++;\n        \n        // Update simple weight estimates\n        if (response == \">\") {\n            weight_estimates[item1] *= 1.05;\n            weight_estimates[item2] /= 1.05;\n        } else if (response == \"<\") {\n            weight_estimates[item1] /= 1.05;\n            weight_estimates[item2] *= 1.05;\n        }\n    }\n    \n    // Phase 2: Group comparisons for better estimates\n    vector<vector<int>> groups_for_comparison;\n    int group_size = max(1, N / 8);\n    \n    // Create groups for comparison\n    for (int g = 0; g < min(8, N); g++) {\n        vector<int> group;\n        for (int i = 0; i < group_size && g * group_size + i < N; i++) {\n            group.push_back(g * group_size + i);\n        }\n        if (!group.empty()) {\n            groups_for_comparison.push_back(group);\n        }\n    }\n    \n    // Compare groups if we have queries left\n    for (int i = 0; i < (int)groups_for_comparison.size() && queries_used < exploration_queries; i++) {\n        for (int j = i + 1; j < (int)groups_for_comparison.size() && queries_used < exploration_queries; j++) {\n            if (groups_for_comparison[i].empty() || groups_for_comparison[j].empty()) continue;\n            \n            cout << groups_for_comparison[i].size() << \" \" << groups_for_comparison[j].size();\n            for (int item : groups_for_comparison[i]) cout << \" \" << item;\n            for (int item : groups_for_comparison[j]) cout << \" \" << item;\n            cout << endl;\n            cout.flush();\n            \n            string response;\n            cin >> response;\n            \n            all_queries.push_back({groups_for_comparison[i], groups_for_comparison[j], response});\n            queries_used++;\n            \n            break; // Just do one group comparison to save queries\n        }\n        break;\n    }\n    \n    // Phase 3: Refinement queries with remaining budget\n    while (queries_used < Q) {\n        // Find items with highest uncertainty or that are critical for partitioning\n        vector<pair<double, int>> sorted_by_weight;\n        for (int i = 0; i < N; i++) {\n            sorted_by_weight.push_back({weight_estimates[i], i});\n        }\n        sort(sorted_by_weight.begin(), sorted_by_weight.end());\n        \n        // Compare lightest vs heaviest\n        if (sorted_by_weight.size() >= 2) {\n            int lightest = sorted_by_weight[0].second;\n            int heaviest = sorted_by_weight.back().second;\n            \n            cout << \"1 1 \" << lightest << \" \" << heaviest << endl;\n            cout.flush();\n            \n            string response;\n            cin >> response;\n            \n            all_queries.push_back({{lightest}, {heaviest}, response});\n            queries_used++;\n            \n            // Update weights\n            if (response == \">\") {\n                weight_estimates[lightest] *= 1.1;\n            } else if (response == \"<\") {\n                weight_estimates[heaviest] *= 1.1;\n            }\n        } else {\n            // Fallback: compare random items\n            int item1 = rand() % N;\n            int item2 = rand() % N;\n            if (item1 != item2) {\n                cout << \"1 1 \" << item1 << \" \" << item2 << endl;\n                cout.flush();\n                \n                string response;\n                cin >> response;\n                \n                all_queries.push_back({{item1}, {item2}, response});\n                queries_used++;\n            } else {\n                queries_used++; // consume query even if we skip it\n            }\n        }\n    }\n    \n    // Refine weight estimates using all comparison history\n    // Simple approach: reinforce consistent patterns\n    for (const auto& [left, right, response] : all_queries) {\n        double left_weight = 0, right_weight = 0;\n        for (int item : left) left_weight += weight_estimates[item];\n        for (int item : right) right_weight += weight_estimates[item];\n        \n        double total = left_weight + right_weight;\n        if (total > 0) {\n            double left_ratio = left_weight / total;\n            double right_ratio = right_weight / total;\n            \n            if (response == \"=\") {\n                // Move weights closer together\n                double avg = (left_weight + right_weight) / 2.0;\n                if (left_weight > 0) {\n                    double factor = avg / left_weight;\n                    for (int item : left) weight_estimates[item] *= factor;\n                }\n                if (right_weight > 0) {\n                    double factor = avg / right_weight;\n                    for (int item : right) weight_estimates[item] *= factor;\n                }\n            } else if (response == \">\") {\n                // Amplify the difference slightly\n                if (left_weight > 0 && right_weight > 0) {\n                    double factor = 1.05;\n                    for (int item : left) weight_estimates[item] *= factor;\n                    for (int item : right) weight_estimates[item] /= factor;\n                }\n            } else { // response == \"<\"\n                if (left_weight > 0 && right_weight > 0) {\n                    double factor = 1.05;\n                    for (int item : left) weight_estimates[item] /= factor;\n                    for (int item : right) weight_estimates[item] *= factor;\n                }\n            }\n        }\n    }\n    \n    // Normalize weights\n    double sum_weights = 0;\n    for (double w : weight_estimates) sum_weights += w;\n    if (sum_weights > 0) {\n        for (double& w : weight_estimates) w = w * N / sum_weights;\n    }\n    \n    // Improved grouping using Karmarkar-Karp-like approach\n    vector<pair<double, int>> items_with_indices;\n    for (int i = 0; i < N; i++) {\n        items_with_indices.push_back({weight_estimates[i], i});\n    }\n    \n    // Sort by weight (descending)\n    sort(items_with_indices.begin(), items_with_indices.end(), greater<pair<double, int>>());\n    \n    // Use priority queue approach for balanced partitioning\n    vector<double> group_sums(D, 0);\n    vector<vector<int>> group_items(D);\n    vector<int> result(N);\n    \n    // Assign items using improved greedy method\n    for (const auto& [weight, idx] : items_with_indices) {\n        // Find the group with minimum current sum\n        int best_group = 0;\n        for (int g = 1; g < D; g++) {\n            if (group_sums[g] < group_sums[best_group]) {\n                best_group = g;\n            }\n        }\n        \n        group_items[best_group].push_back(idx);\n        group_sums[best_group] += weight;\n        result[idx] = best_group;\n    }\n    \n    // Output result\n    for (int i = 0; i < N; i++) {\n        cout << result[i];\n        if (i < N - 1) cout << \" \";\n    }\n    cout << endl;\n}\n\nint main() {\n    solve();\n    return 0;\n}","ahc026":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 200;\nconst int M = 10;\nconst int STACK_SIZE = 20;\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    int n, m;\n    cin >> n >> m;\n    \n    vector<deque<int>> stacks(M);\n    vector<int> box_stack(N + 1);      // which stack each box is in\n    vector<int> box_position(N + 1);   // position in stack (0 = bottom)\n    \n    // Read initial configuration\n    for (int i = 0; i < m; i++) {\n        for (int j = 0; j < STACK_SIZE; j++) {\n            int box;\n            cin >> box;\n            stacks[i].push_back(box);\n            box_stack[box] = i;\n            box_position[box] = j;\n        }\n    }\n    \n    vector<pair<int, int>> operations;\n    \n    // Process boxes in order from 1 to 200\n    for (int target = 1; target <= n; target++) {\n        int stack_idx = box_stack[target];\n        int pos_in_stack = box_position[target];\n        int stack_size = stacks[stack_idx].size();\n        \n        // If box is not at the top, we need to move boxes above it\n        if (pos_in_stack != stack_size - 1) {\n            // Determine which boxes need to be moved\n            int boxes_above = stack_size - 1 - pos_in_stack;\n            \n            // Find a destination stack (prefer one that's not full)\n            int dest_stack = -1;\n            for (int s = 0; s < m; s++) {\n                if (s != stack_idx && (int)stacks[s].size() < STACK_SIZE) {\n                    dest_stack = s;\n                    break;\n                }\n            }\n            \n            // If all stacks are full, pick any other stack\n            if (dest_stack == -1) {\n                dest_stack = (stack_idx + 1) % m;\n            }\n            \n            // Move operation: move box 'target' and all above it to dest_stack\n            operations.push_back({target, dest_stack + 1}); // 1-indexed output\n            \n            // Actually perform the move\n            vector<int> moved_boxes;\n            for (int i = pos_in_stack; i < stack_size; i++) {\n                moved_boxes.push_back(stacks[stack_idx][i]);\n            }\n            \n            // Remove from source stack\n            stacks[stack_idx].erase(\n                stacks[stack_idx].begin() + pos_in_stack, \n                stacks[stack_idx].end()\n            );\n            \n            // Add to destination stack\n            for (int box : moved_boxes) {\n                stacks[dest_stack].push_back(box);\n            }\n            \n            // Update positions for all moved boxes\n            for (int i = 0; i < (int)stacks[dest_stack].size(); i++) {\n                box_stack[stacks[dest_stack][i]] = dest_stack;\n                box_position[stacks[dest_stack][i]] = i;\n            }\n        }\n        \n        // Now the target box should be at the top of its stack\n        // Remove it\n        int final_stack = box_stack[target];\n        stacks[final_stack].pop_back();\n        box_stack[target] = -1;\n        operations.push_back({target, 0});\n        \n        // Update positions of remaining boxes in the stack\n        for (int i = 0; i < (int)stacks[final_stack].size(); i++) {\n            box_position[stacks[final_stack][i]] = i;\n        }\n    }\n    \n    // Output operations\n    for (const auto& op : operations) {\n        cout << op.first << \" \" << op.second << \"\\n\";\n    }\n    \n    return 0;\n}","ahc027":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MAXN = 45;\nint N;\nchar h[MAXN][MAXN], v[MAXN][MAXN];\nint d[MAXN][MAXN];\nbool visited[MAXN][MAXN];\nvector<pair<int, int>> tree_edges;\nvector<char> directions = {'R', 'D', 'L', 'U'};\nvector<pair<int, int>> moves = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};\n\n// Build spanning tree using BFS\nvoid build_spanning_tree() {\n    memset(visited, 0, sizeof(visited));\n    queue<pair<int, int>> q;\n    q.push({0, 0});\n    visited[0][0] = true;\n    \n    while (!q.empty()) {\n        auto [x, y] = q.front(); q.pop();\n        \n        for (int dir = 0; dir < 4; dir++) {\n            int nx = x + moves[dir].first;\n            int ny = y + moves[dir].second;\n            \n            if (nx >= 0 && nx < N && ny >= 0 && ny < N && !visited[nx][ny]) {\n                // Check if there's no wall\n                bool can_move = false;\n                if (dir == 0) { // Right\n                    if (ny > 0) can_move = (v[x][min(y, ny-1)] == '0');\n                    else can_move = true;\n                } else if (dir == 1) { // Down\n                    if (nx > 0) can_move = (h[min(x, nx-1)][y] == '0');\n                    else can_move = true;\n                } else if (dir == 2) { // Left\n                    if (y > 0) can_move = (v[x][min(y-1, ny)] == '0');\n                    else can_move = true;\n                } else { // Up\n                    if (x > 0) can_move = (h[min(x-1, nx)][y] == '0');\n                    else can_move = true;\n                }\n                \n                if (can_move || (nx >= 0 && nx < N && ny >= 0 && ny < N)) {\n                    if ((dir == 0 && (y+1 < N) && v[x][y] == '0') ||\n                        (dir == 1 && (x+1 < N) && h[x][y] == '0') ||\n                        (dir == 2 && (y > 0) && v[x][y-1] == '0') ||\n                        (dir == 3 && (x > 0) && h[x-1][y] == '0')) {\n                        visited[nx][ny] = true;\n                        tree_edges.push_back({x*N + y, nx*N + ny});\n                        q.push({nx, ny});\n                    }\n                }\n            }\n        }\n    }\n}\n\n// DFS to create Eulerian tour\nstring result = \"\";\nvoid dfs_tour(int x, int y, int parent_x, int parent_y) {\n    visited[x][y] = true;\n    \n    // Try all directions\n    for (int dir = 0; dir < 4; dir++) {\n        int nx = x + moves[dir].first;\n        int ny = y + moves[dir].second;\n        \n        // Check bounds\n        if (nx < 0 || nx >= N || ny < 0 || ny >= N) continue;\n        \n        // Check walls\n        bool can_move = false;\n        if (dir == 0) { // Right\n            can_move = (v[x][y] == '0');\n        } else if (dir == 1) { // Down\n            can_move = (h[x][y] == '0');\n        } else if (dir == 2) { // Left\n            if (y > 0) can_move = (v[x][y-1] == '0');\n        } else { // Up\n            if (x > 0) can_move = (h[x-1][y] == '0');\n        }\n        \n        if (!can_move) continue;\n        \n        // Skip parent to avoid going back immediately\n        if (nx == parent_x && ny == parent_y) continue;\n        \n        if (!visited[nx][ny]) {\n            result += directions[dir];\n            dfs_tour(nx, ny, x, y);\n            // Return move\n            if (dir == 0) result += 'L';\n            else if (dir == 1) result += 'U';\n            else if (dir == 2) result += 'R';\n            else result += 'D';\n        }\n    }\n}\n\n// Better approach: use BFS to build proper spanning tree\nvoid build_proper_spanning_tree() {\n    memset(visited, 0, sizeof(visited));\n    queue<tuple<int, int, int, int>> q; // x, y, parent_x, parent_y\n    q.push({0, 0, -1, -1});\n    visited[0][0] = true;\n    vector<tuple<int, int, int, int>> edges;\n    \n    while (!q.empty()) {\n        auto [x, y, px, py] = q.front(); q.pop();\n        \n        for (int dir = 0; dir < 4; dir++) {\n            int nx = x + moves[dir].first;\n            int ny = y + moves[dir].second;\n            \n            if (nx >= 0 && nx < N && ny >= 0 && ny < N && !visited[nx][ny]) {\n                // Check if move is possible\n                bool can_move = false;\n                if (dir == 0 && y+1 < N) can_move = (v[x][y] == '0');\n                else if (dir == 1 && x+1 < N) can_move = (h[x][y] == '0');\n                else if (dir == 2 && y > 0) can_move = (v[x][y-1] == '0');\n                else if (dir == 3 && x > 0) can_move = (h[x-1][y] == '0');\n                \n                if (can_move) {\n                    visited[nx][ny] = true;\n                    edges.push_back({x, y, nx, ny});\n                    q.push({nx, ny, x, y});\n                }\n            }\n        }\n    }\n    \n    // Now create tour from edges\n    memset(visited, 0, sizeof(visited));\n    result = \"\";\n    dfs_tour(0, 0, -1, -1);\n}\n\n// Even simpler approach: just do DFS on the grid\nstring simple_dfs() {\n    memset(visited, 0, sizeof(visited));\n    string path = \"\";\n    \n    function<void(int, int)> dfs = [&](int x, int y) {\n        visited[x][y] = true;\n        \n        // Try directions in order that prioritizes higher d values\n        vector<pair<int, int>> next_moves;\n        for (int dir = 0; dir < 4; dir++) {\n            int nx = x + moves[dir].first;\n            int ny = y + moves[dir].second;\n            \n            if (nx >= 0 && nx < N && ny >= 0 && ny < N && !visited[nx][ny]) {\n                // Check walls\n                bool can_move = false;\n                if (dir == 0 && y+1 < N) can_move = (v[x][y] == '0');\n                else if (dir == 1 && x+1 < N) can_move = (h[x][y] == '0');\n                else if (dir == 2 && y > 0) can_move = (v[x][y-1] == '0');\n                else if (dir == 3 && x > 0) can_move = (h[x-1][y] == '0');\n                \n                if (can_move) {\n                    next_moves.push_back({nx, ny});\n                }\n            }\n        }\n        \n        // Sort by dirtiness (descending)\n        sort(next_moves.begin(), next_moves.end(), [&](const pair<int,int>& a, const pair<int,int>& b) {\n            return d[a.first][a.second] > d[b.first][b.second];\n        });\n        \n        for (auto& move : next_moves) {\n            int nx = move.first, ny = move.second;\n            // Find direction\n            int dir = -1;\n            for (int i = 0; i < 4; i++) {\n                if (x + moves[i].first == nx && y + moves[i].second == ny) {\n                    dir = i;\n                    break;\n                }\n            }\n            \n            if (dir != -1) {\n                path += directions[dir];\n                dfs(nx, ny);\n                // Return move\n                if (dir == 0) path += 'L';\n                else if (dir == 1) path += 'U';\n                else if (dir == 2) path += 'R';\n                else path += 'D';\n            }\n        }\n    };\n    \n    dfs(0, 0);\n    return path;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    cin >> N;\n    \n    // Read horizontal walls\n    for (int i = 0; i < N-1; i++) {\n        string line;\n        cin >> line;\n        for (int j = 0; j < N; j++) {\n            h[i][j] = line[j];\n        }\n    }\n    \n    // Read vertical walls\n    for (int i = 0; i < N; i++) {\n        string line;\n        cin >> line;\n        for (int j = 0; j < N-1; j++) {\n            v[i][j] = line[j];\n        }\n    }\n    \n    // Read dirtiness values\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cin >> d[i][j];\n        }\n    }\n    \n    // Generate path using simple DFS approach\n    string result = simple_dfs();\n    \n    // Make sure we don't exceed limit\n    if (result.length() > 100000) {\n        result = result.substr(0, 100000);\n    }\n    \n    cout << result << endl;\n    \n    return 0;\n}","ahc028":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 15;\nconst int M = 200;\nconst int MAX_OPS = 5000;\n\nint grid_size;\nint target_count;\nint start_row, start_col;\nchar grid[N][N];\nvector<string> targets;\nvector<pair<int,int>> char_positions[26]; // positions for each character A-Z\n\n// Precompute positions for each character\nvoid precompute_positions() {\n    for (int i = 0; i < 26; i++) {\n        char_positions[i].clear();\n    }\n    \n    for (int i = 0; i < grid_size; i++) {\n        for (int j = 0; j < grid_size; j++) {\n            int char_idx = grid[i][j] - 'A';\n            char_positions[char_idx].push_back({i, j});\n        }\n    }\n}\n\n// Find the best position considering future characters\npair<int, int> find_best_position_with_lookahead(const string& suffix, int cur_row, int cur_col, int lookahead) {\n    char c = suffix[0];\n    int char_idx = c - 'A';\n    \n    if (lookahead == 0 || suffix.length() == 1) {\n        // Just find closest position\n        pair<int, int> best_pos = char_positions[char_idx][0];\n        int best_dist = abs(cur_row - best_pos.first) + abs(cur_col - best_pos.second);\n        \n        for (auto& pos : char_positions[char_idx]) {\n            int dist = abs(cur_row - pos.first) + abs(cur_col - pos.second);\n            if (dist < best_dist) {\n                best_dist = dist;\n                best_pos = pos;\n            }\n        }\n        return best_pos;\n    }\n    \n    // Look ahead to make better decision\n    pair<int, int> best_pos = char_positions[char_idx][0];\n    int best_score = 1e9;\n    \n    for (auto& pos : char_positions[char_idx]) {\n        int move_cost = abs(cur_row - pos.first) + abs(cur_col - pos.second) + 1;\n        \n        // Estimate future cost\n        int future_cost = 0;\n        if (suffix.length() > 1) {\n            char next_char = suffix[1];\n            int next_char_idx = next_char - 'A';\n            pair<int, int> next_best = char_positions[next_char_idx][0];\n            int next_dist = abs(pos.first - next_best.first) + abs(pos.second - next_best.second);\n            \n            for (auto& next_pos : char_positions[next_char_idx]) {\n                int dist = abs(pos.first - next_pos.first) + abs(pos.second - next_pos.second);\n                if (dist < next_dist) {\n                    next_dist = dist;\n                }\n            }\n            future_cost = next_dist + 1;\n        }\n        \n        int total_score = move_cost + future_cost / 2; // Weight future cost less\n        \n        if (total_score < best_score) {\n            best_score = total_score;\n            best_pos = pos;\n        }\n    }\n    \n    return best_pos;\n}\n\n// Build superstring using greedy concatenation with overlap\nstring build_superstring_greedy() {\n    if (targets.empty()) return \"\";\n    \n    vector<bool> used(targets.size(), false);\n    string result = targets[0];\n    used[0] = true;\n    \n    // Try to merge remaining strings with maximum overlap\n    for (int iter = 1; iter < (int)targets.size(); iter++) {\n        int best_overlap = -1;\n        int best_idx = -1;\n        bool append = true;\n        string best_new_string = \"\";\n        \n        // Find string with maximum overlap\n        for (int i = 0; i < (int)targets.size(); i++) {\n            if (used[i]) continue;\n            \n            const string& s = targets[i];\n            \n            // Check overlap when appending\n            for (int overlap = min(4, (int)result.length()); overlap >= 0; overlap--) {\n                if (result.length() >= overlap && \n                    result.substr(result.length() - overlap) == s.substr(0, overlap)) {\n                    string new_result = result + s.substr(overlap);\n                    if (overlap > best_overlap || (overlap == best_overlap && new_result.length() < best_new_string.length())) {\n                        best_overlap = overlap;\n                        best_idx = i;\n                        append = true;\n                        best_new_string = new_result;\n                    }\n                    break;\n                }\n            }\n        }\n        \n        if (best_idx != -1) {\n            result = best_new_string;\n            used[best_idx] = true;\n        } else {\n            // Find first unused string and append it\n            for (int i = 0; i < (int)targets.size(); i++) {\n                if (!used[i]) {\n                    result += targets[i];\n                    used[i] = true;\n                    break;\n                }\n            }\n        }\n    }\n    \n    return result;\n}\n\n// Alternative superstring using de Bruijn-like approach for small overlaps\nstring build_superstring_alternative() {\n    string result = \"\";\n    \n    // Simple concatenation with small overlaps where possible\n    for (int i = 0; i < (int)targets.size(); i++) {\n        if (result.empty()) {\n            result = targets[i];\n        } else {\n            // Try to find 1-2 character overlap\n            bool merged = false;\n            for (int overlap = min(2, min(4, (int)result.length())); overlap >= 1; overlap--) {\n                if (result.length() >= overlap && \n                    result.substr(result.length() - overlap) == targets[i].substr(0, overlap)) {\n                    result += targets[i].substr(overlap);\n                    merged = true;\n                    break;\n                }\n            }\n            if (!merged) {\n                result += targets[i];\n            }\n        }\n    }\n    \n    return result;\n}\n\n// Generate path with optimization\nvector<pair<int, int>> generate_optimized_path(const string& superstring) {\n    vector<pair<int, int>> path;\n    int cur_row = start_row;\n    int cur_col = start_col;\n    \n    // Process in chunks to optimize movement\n    for (int i = 0; i < (int)superstring.length(); i++) {\n        // Look ahead a few characters to make better decisions\n        string lookahead_str = superstring.substr(i, min(3, (int)superstring.length() - i));\n        pair<int, int> next_pos = find_best_position_with_lookahead(lookahead_str, cur_row, cur_col, \n                                                                  min(2, (int)lookahead_str.length() - 1));\n        \n        path.push_back(next_pos);\n        cur_row = next_pos.first;\n        cur_col = next_pos.second;\n    }\n    \n    return path;\n}\n\n// Remove redundant moves\nvector<pair<int, int>> optimize_path(const vector<pair<int, int>>& path) {\n    if (path.empty()) return path;\n    \n    vector<pair<int, int>> optimized;\n    optimized.push_back(path[0]);\n    \n    for (int i = 1; i < (int)path.size(); i++) {\n        // Only add if it's different from the last position\n        if (path[i] != optimized.back()) {\n            optimized.push_back(path[i]);\n        }\n    }\n    \n    return optimized;\n}\n\n// Try multiple strategies and pick the best\nvector<pair<int, int>> find_best_path() {\n    // Strategy 1: Greedy overlap\n    string superstring1 = build_superstring_greedy();\n    vector<pair<int, int>> path1 = generate_optimized_path(superstring1);\n    path1 = optimize_path(path1);\n    \n    // Strategy 2: Simple concatenation with small overlaps\n    string superstring2 = build_superstring_alternative();\n    vector<pair<int, int>> path2 = generate_optimized_path(superstring2);\n    path2 = optimize_path(path2);\n    \n    // Pick the shorter path, but not too short (need to cover all strings)\n    if (path1.size() <= path2.size() && path1.size() <= MAX_OPS) {\n        return path1;\n    } else if (path2.size() <= MAX_OPS) {\n        return path2;\n    } else {\n        return path1.size() <= MAX_OPS ? path1 : vector<pair<int, int>>(path1.begin(), path1.begin() + min(MAX_OPS, (int)path1.size()));\n    }\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    // Read input\n    cin >> grid_size >> target_count;\n    cin >> start_row >> start_col;\n    \n    for (int i = 0; i < grid_size; i++) {\n        for (int j = 0; j < grid_size; j++) {\n            cin >> grid[i][j];\n        }\n    }\n    \n    targets.resize(target_count);\n    for (int i = 0; i < target_count; i++) {\n        cin >> targets[i];\n    }\n    \n    // Precompute character positions\n    precompute_positions();\n    \n    // Find best path using multiple strategies\n    vector<pair<int, int>> path = find_best_path();\n    \n    // Ensure we don't exceed limit\n    if (path.size() > MAX_OPS) {\n        path.resize(MAX_OPS);\n    }\n    \n    // Output path\n    for (const auto& pos : path) {\n        cout << pos.first << \" \" << pos.second << \"\\n\";\n    }\n    \n    return 0;\n}","ahc030":"#include <bits/stdc++.h>\n\nusing namespace std;\n\nstruct Point {\n    int i, j;\n    Point() : i(0), j(0) {}\n    Point(int _i, int _j) : i(_i), j(_j) {}\n    bool operator<(const Point& other) const {\n        if (i != other.i) return i < other.i;\n        return j < other.j;\n    }\n};\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    int N, M;\n    double epsilon;\n    \n    // Read initial parameters\n    if (!(cin >> N >> M >> epsilon)) {\n        return 1;\n    }\n    \n    // Read polyomino data (skip it since we don't need it for basic approach)\n    for (int i = 0; i < M; i++) {\n        int d;\n        if (!(cin >> d)) return 1;\n        for (int j = 0; j < 2 * d; j++) {\n            int dummy;\n            if (!(cin >> dummy)) return 1;\n        }\n    }\n    \n    // Track which squares we know have oil\n    vector<vector<bool>> has_oil(N, vector<bool>(N, false));\n    vector<vector<bool>> drilled(N, vector<bool>(N, false));\n    set<Point> oil_squares;\n    \n    // Simple strategy: drill all squares\n    // This guarantees we find all oil squares but might not be optimal cost-wise\n    int operations = 0;\n    const int max_operations = 2 * N * N;\n    \n    for (int i = 0; i < N && operations < max_operations - N*N/2; i++) {\n        for (int j = 0; j < N && operations < max_operations - N*N/2; j++) {\n            // Query this square\n            cout << \"q 1 \" << i << \" \" << j << endl;\n            cout.flush();\n            operations++;\n            \n            int response;\n            if (!(cin >> response)) {\n                return 1;\n            }\n            \n            drilled[i][j] = true;\n            if (response > 0) {\n                has_oil[i][j] = true;\n                oil_squares.insert(Point(i, j));\n            }\n        }\n    }\n    \n    // Submit our answer\n    cout << \"a \" << oil_squares.size();\n    for (const auto& p : oil_squares) {\n        cout << \" \" << p.i << \" \" << p.j;\n    }\n    cout << endl;\n    cout.flush();\n    \n    // Read the response (should be 1 for correct answer)\n    int result;\n    if (cin >> result) {\n        // We don't need to do anything with the result since we're done\n    }\n    \n    return 0;\n}","ahc031":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int W = 1000;\n\nstruct Rectangle {\n    int i0, j0, i1, j1;\n    Rectangle() : i0(0), j0(0), i1(0), j1(0) {}\n    Rectangle(int _i0, int _j0, int _i1, int _j1) : i0(_i0), j0(_j0), i1(_i1), j1(_j1) {}\n    int area() const { return (i1 - i0) * (j1 - j0); }\n    bool valid() const { return i0 >= 0 && j0 >= 0 && i1 <= W && j1 <= W && i0 < i1 && j0 < j1; }\n};\n\n// Calculate partition cost between two days\nint calculate_partition_cost(const vector<Rectangle>& prev, const vector<Rectangle>& curr) {\n    // Simplified: just count rectangle changes\n    // In practice, we'd need to track actual partition lines\n    int cost = 0;\n    for (size_t i = 0; i < min(prev.size(), curr.size()); ++i) {\n        if (prev[i].i0 != curr[i].i0 || prev[i].j0 != curr[i].j0 || \n            prev[i].i1 != curr[i].i1 || prev[i].j1 != curr[i].j1) {\n            cost += 10; // penalty for moving rectangles\n        }\n    }\n    return cost;\n}\n\n// Calculate area shortage cost\nint calculate_area_cost(const vector<int>& required, const vector<Rectangle>& rectangles) {\n    int cost = 0;\n    for (size_t i = 0; i < required.size(); ++i) {\n        int shortage = max(0, required[i] - rectangles[i].area());\n        cost += 100 * shortage;\n    }\n    return cost;\n}\n\n// Simple strip packing algorithm\nvector<Rectangle> pack_rectangles_simple(const vector<int>& required) {\n    vector<Rectangle> rectangles(required.size());\n    int current_row = 0;\n    \n    for (size_t i = 0; i < required.size(); ++i) {\n        int area_needed = required[i];\n        \n        // Try to make rectangle as square as possible\n        int width = min(W, (int)sqrt(area_needed) + 1);\n        while (width > 0 && area_needed % width != 0) width--;\n        if (width == 0) width = 1;\n        \n        int height = (area_needed + width - 1) / width;\n        \n        // Adjust to fit in grid\n        if (width > W) {\n            width = W;\n            height = (area_needed + W - 1) / W;\n        }\n        if (height > W) {\n            height = W;\n            width = (area_needed + W - 1) / W;\n        }\n        \n        // Position the rectangle\n        if (current_row + height <= W) {\n            rectangles[i] = Rectangle(current_row, 0, current_row + height, width);\n            current_row += height;\n        } else {\n            // If we run out of space, just place it somewhere\n            rectangles[i] = Rectangle(0, 0, min(height, W), min(width, W));\n        }\n    }\n    \n    return rectangles;\n}\n\n// Improved packing with better area satisfaction\nvector<Rectangle> pack_rectangles_improved(const vector<int>& required) {\n    vector<Rectangle> rectangles(required.size());\n    \n    // Sort rectangles by required area (descending) for better packing\n    vector<pair<int, int>> area_index;\n    for (size_t i = 0; i < required.size(); ++i) {\n        area_index.push_back({required[i], i});\n    }\n    sort(area_index.rbegin(), area_index.rend());\n    \n    // Use a simple grid-based approach\n    int grid_size = (int)sqrt(required.size()) + 1;\n    int cell_width = W / grid_size;\n    int cell_height = W / grid_size;\n    \n    vector<vector<bool>> occupied(grid_size, vector<bool>(grid_size, false));\n    \n    for (const auto& [area, idx] : area_index) {\n        int area_needed = area;\n        \n        // Try to make rectangle fit requirements\n        int width = min(W, (int)sqrt(area_needed) + 50);  // Add buffer\n        int height = (area_needed + width - 1) / width;\n        \n        if (width > W) {\n            width = W;\n            height = (area_needed + W - 1) / W + 1;\n        }\n        if (height > W) {\n            height = W;\n            width = (area_needed + W - 1) / W + 1;\n        }\n        \n        // Ensure minimum area\n        if (width * height < area_needed) {\n            int diff = area_needed - width * height;\n            width += (diff + height - 1) / height;\n            if (width > W) width = W;\n            height += (diff + width - 1) / width;\n            if (height > W) height = W;\n        }\n        \n        // Place rectangle\n        bool placed = false;\n        for (int x = 0; x <= W - width && !placed; ++x) {\n            for (int y = 0; y <= W - height && !placed; ++y) {\n                rectangles[idx] = Rectangle(x, y, x + height, y + width);\n                if (rectangles[idx].valid()) {\n                    placed = true;\n                    break;\n                }\n            }\n        }\n        \n        // Fallback if not placed properly\n        if (!placed || rectangles[idx].area() < area_needed) {\n            int side = max(1, (int)sqrt(area_needed));\n            rectangles[idx] = Rectangle(0, 0, min(side, W), min((area_needed + side - 1) / side, W));\n        }\n    }\n    \n    return rectangles;\n}\n\n// Optimize rectangles to reduce area shortage\nvector<Rectangle> optimize_rectangles(vector<Rectangle> rectangles, const vector<int>& required) {\n    for (size_t i = 0; i < rectangles.size(); ++i) {\n        int current_area = rectangles[i].area();\n        int required_area = required[i];\n        \n        if (current_area < required_area) {\n            // Expand rectangle to meet requirement\n            int shortage = required_area - current_area;\n            \n            int width = rectangles[i].j1 - rectangles[i].j0;\n            int height = rectangles[i].i1 - rectangles[i].i0;\n            \n            // Try to expand width first\n            if (rectangles[i].j1 < W) {\n                int expand_w = min(shortage / height + 1, W - rectangles[i].j1);\n                rectangles[i].j1 = min(W, rectangles[i].j1 + expand_w);\n            }\n            // Then try to expand height\n            else if (rectangles[i].i1 < W) {\n                int expand_h = min(shortage / width + 1, W - rectangles[i].i1);\n                rectangles[i].i1 = min(W, rectangles[i].i1 + expand_h);\n            }\n        }\n    }\n    return rectangles;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int W_input, D, N;\n    cin >> W_input >> D >> N;\n    \n    vector<vector<int>> required(D, vector<int>(N));\n    for (int d = 0; d < D; ++d) {\n        for (int k = 0; k < N; ++k) {\n            cin >> required[d][k];\n        }\n    }\n    \n    vector<vector<Rectangle>> solutions(D);\n    \n    // Day 0: Generate initial solution\n    solutions[0] = pack_rectangles_improved(required[0]);\n    solutions[0] = optimize_rectangles(solutions[0], required[0]);\n    \n    // Days 1 to D-1: Try to maintain consistency to reduce partition costs\n    for (int d = 1; d < D; ++d) {\n        // Start with previous day's solution\n        solutions[d] = solutions[d-1];\n        \n        // Adjust to meet new requirements\n        solutions[d] = optimize_rectangles(solutions[d], required[d]);\n        \n        // If still not good enough, generate fresh solution\n        bool need_fresh = false;\n        for (int k = 0; k < N; ++k) {\n            if (solutions[d][k].area() < required[d][k]) {\n                need_fresh = true;\n                break;\n            }\n        }\n        \n        if (need_fresh) {\n            solutions[d] = pack_rectangles_improved(required[d]);\n            solutions[d] = optimize_rectangles(solutions[d], required[d]);\n        }\n    }\n    \n    // Output results\n    for (int d = 0; d < D; ++d) {\n        for (int k = 0; k < N; ++k) {\n            cout << solutions[d][k].i0 << \" \" << solutions[d][k].j0 << \" \" \n                 << solutions[d][k].i1 << \" \" << solutions[d][k].j1 << \"\\n\";\n        }\n    }\n    \n    return 0;\n}","ahc032":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 9;\nconst int M = 20;\nconst int K = 81;\nconst long long MOD = 998244353;\n\nlong long a[N][N];\nlong long s[M][3][3];\n\nstruct Operation {\n    int stamp, p, q;\n};\n\n// Calculate current score of board\nlong long calculate_score(long long board[N][N]) {\n    long long score = 0;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            score = (score + (board[i][j] % MOD + MOD) % MOD) % MOD;\n        }\n    }\n    return score;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    // Read input\n    int dummy_N, dummy_M, dummy_K;\n    cin >> dummy_N >> dummy_M >> dummy_K;\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    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 board\n    long long board[N][N];\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            board[i][j] = a[i][j];\n        }\n    }\n    \n    vector<Operation> operations;\n    \n    // Greedy selection\n    for (int k = 0; k < K; k++) {\n        Operation best_op = {-1, -1, -1};\n        long long best_gain = -1;\n        long long current_score = calculate_score(board);\n        \n        // Try all possible stamp placements\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                    // Create temporary board\n                    long long temp_board[N][N];\n                    for (int i = 0; i < N; i++) {\n                        for (int j = 0; j < N; j++) {\n                            temp_board[i][j] = board[i][j];\n                        }\n                    }\n                    \n                    // Apply stamp\n                    for (int i = 0; i < 3; i++) {\n                        for (int j = 0; j < 3; j++) {\n                            temp_board[p + i][q + j] += s[m][i][j];\n                        }\n                    }\n                    \n                    long long new_score = calculate_score(temp_board);\n                    long long gain = new_score - current_score;\n                    \n                    if (gain > best_gain) {\n                        best_gain = gain;\n                        best_op = {m, p, q};\n                    }\n                }\n            }\n        }\n        \n        // If no positive gain, stop\n        if (best_gain <= 0) {\n            break;\n        }\n        \n        // Apply the best operation\n        operations.push_back(best_op);\n        \n        for (int i = 0; i < 3; i++) {\n            for (int j = 0; j < 3; j++) {\n                board[best_op.p + i][best_op.q + j] += s[best_op.stamp][i][j];\n            }\n        }\n    }\n    \n    // Output\n    cout << operations.size() << \"\\n\";\n    for (const auto& op : operations) {\n        cout << op.stamp << \" \" << op.p << \" \" << op.q << \"\\n\";\n    }\n    \n    return 0;\n}","ahc033":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 5;\nconst int MAX_TURNS = 10000;\n\n// Container information\nint A[N][N];\nint container_to_gate[N*N]; // which gate each container should go to\n\n// Crane positions and states\nstruct Crane {\n    int row, col;\n    bool holding;\n    int container;\n    bool bombed;\n};\n\n// Grid state\nint grid[N][N]; // -1 = empty, >= 0 = container number\nCrane cranes[N];\n\n// Output actions\nvector<string> actions(N);\n\n// Initialize problem\nvoid init() {\n    // Calculate which gate each container should go to\n    for (int i = 0; i < N*N; i++) {\n        container_to_gate[i] = i / N;\n    }\n    \n    // Initialize grid\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            grid[i][j] = -1;\n        }\n    }\n    \n    // Initialize cranes\n    for (int i = 0; i < N; i++) {\n        cranes[i] = {i, 0, false, -1, false};\n    }\n}\n\n// Check if position is valid\nbool is_valid(int r, int c) {\n    return r >= 0 && r < N && c >= 0 && c < N;\n}\n\n// Main simulation logic\nvoid simulate() {\n    int turn = 0;\n    int received[N] = {0}; // Count of containers received by each gate\n    bool dispatched[N*N] = {false}; // Track which containers have been dispatched\n    \n    // Place initial containers\n    for (int i = 0; i < N; i++) {\n        if (received[i] < N) {\n            grid[i][0] = A[i][received[i]];\n            received[i]++;\n        }\n    }\n    \n    while (turn < MAX_TURNS) {\n        // Check if all containers are dispatched\n        bool all_done = true;\n        for (int i = 0; i < N*N; i++) {\n            if (!dispatched[i]) {\n                all_done = false;\n                break;\n            }\n        }\n        if (all_done) break;\n        \n        // Step 1: Place new containers at receiving gates\n        for (int i = 0; i < N; i++) {\n            if (received[i] < N && grid[i][0] == -1) {\n                grid[i][0] = A[i][received[i]];\n                received[i]++;\n            }\n        }\n        \n        // Step 2: Plan crane actions\n        vector<char> turn_actions(N, '.');\n        \n        // First, handle containers that are ready to be dispatched\n        for (int gate = 0; gate < N; gate++) {\n            if (grid[gate][N-1] != -1) {\n                int container = grid[gate][N-1];\n                if (container / N == gate && !dispatched[container]) {\n                    // Correct container at correct gate - dispatch it\n                    dispatched[container] = true;\n                    grid[gate][N-1] = -1;\n                }\n            }\n        }\n        \n        // Plan crane movements (simplified strategy)\n        for (int i = 0; i < N; i++) {\n            if (cranes[i].bombed) {\n                turn_actions[i] = '.';\n                continue;\n            }\n            \n            Crane& crane = cranes[i];\n            \n            // If at dispatch gate with correct container, dispatch it\n            if (crane.col == N-1 && grid[crane.row][crane.col] != -1) {\n                int container = grid[crane.row][crane.col];\n                if (container / N == crane.row && !dispatched[container]) {\n                    grid[crane.row][crane.col] = -1;\n                    dispatched[container] = true;\n                    continue;\n                }\n            }\n            \n            // Simple strategy: move right when possible\n            if (crane.col < N-1) {\n                // Try to move right\n                if (grid[crane.row][crane.col+1] == -1 || i == 0) { // Large crane can move through occupied\n                    turn_actions[i] = 'R';\n                    crane.col++;\n                } else {\n                    // Try to pick up container if at receiving gate or wrong position\n                    if (grid[crane.row][crane.col] != -1) {\n                        int container = grid[crane.row][crane.col];\n                        if (!crane.holding) {\n                            turn_actions[i] = 'P';\n                            crane.holding = true;\n                            crane.container = container;\n                            grid[crane.row][crane.col] = -1;\n                        } else {\n                            turn_actions[i] = '.';\n                        }\n                    } else {\n                        turn_actions[i] = '.';\n                    }\n                }\n            } else {\n                // At dispatch gate\n                if (grid[crane.row][crane.col] != -1) {\n                    int container = grid[crane.row][crane.col];\n                    if (container / N == crane.row && !dispatched[container]) {\n                        // Dispatch container\n                        grid[crane.row][crane.col] = -1;\n                        dispatched[container] = true;\n                    } else if (!crane.holding) {\n                        // Pick up wrong container to move it\n                        turn_actions[i] = 'P';\n                        crane.holding = true;\n                        crane.container = container;\n                        grid[crane.row][crane.col] = -1;\n                    } else {\n                        turn_actions[i] = '.';\n                    }\n                } else if (crane.holding) {\n                    // Place container\n                    int target_gate = crane.container / N;\n                    if (target_gate == crane.row) {\n                        turn_actions[i] = 'Q';\n                        crane.holding = false;\n                        grid[crane.row][crane.col] = crane.container;\n                        if (target_gate == crane.row) {\n                            dispatched[crane.container] = true;\n                        }\n                        crane.container = -1;\n                    } else {\n                        // Move to correct row\n                        if (crane.row < target_gate && (grid[crane.row+1][crane.col] == -1 || i == 0)) {\n                            turn_actions[i] = 'D';\n                            crane.row++;\n                        } else if (crane.row > target_gate && (grid[crane.row-1][crane.col] == -1 || i == 0)) {\n                            turn_actions[i] = 'U';\n                            crane.row--;\n                        } else {\n                            turn_actions[i] = '.';\n                        }\n                    }\n                } else {\n                    turn_actions[i] = '.';\n                }\n            }\n        }\n        \n        // Apply actions\n        for (int i = 0; i < N; i++) {\n            if (turn_actions[i] == 'R' && cranes[i].col < N-1) {\n                if (grid[cranes[i].row][cranes[i].col+1] == -1 || i == 0) {\n                    // Valid move\n                } else {\n                    turn_actions[i] = '.';\n                }\n            }\n            actions[i] += turn_actions[i];\n        }\n        \n        // Update grid based on movements and actions\n        for (int i = 0; i < N; i++) {\n            if (turn_actions[i] == 'P' && !cranes[i].holding) {\n                if (grid[cranes[i].row][cranes[i].col] != -1) {\n                    cranes[i].holding = true;\n                    cranes[i].container = grid[cranes[i].row][cranes[i].col];\n                    grid[cranes[i].row][cranes[i].col] = -1;\n                }\n            } else if (turn_actions[i] == 'Q' && cranes[i].holding) {\n                if (grid[cranes[i].row][cranes[i].col] == -1) {\n                    grid[cranes[i].row][cranes[i].col] = cranes[i].container;\n                    cranes[i].holding = false;\n                    cranes[i].container = -1;\n                }\n            }\n        }\n        \n        turn++;\n    }\n    \n    // Pad all action strings to same length\n    int max_len = 0;\n    for (int i = 0; i < N; i++) {\n        max_len = max(max_len, (int)actions[i].length());\n    }\n    \n    for (int i = 0; i < N; i++) {\n        while ((int)actions[i].length() < max_len) {\n            actions[i] += '.';\n        }\n    }\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    // Read input (N is fixed to 5)\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\n    init();\n    \n    // Run simulation\n    simulate();\n    \n    // Output results\n    for (int i = 0; i < N; i++) {\n        cout << actions[i] << \"\\n\";\n    }\n    \n    return 0;\n}","ahc034":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int GRID_SIZE = 20;\nint grid[GRID_SIZE][GRID_SIZE];\n\n// Directions: U, D, L, R\nconst int dx[4] = {-1, 1, 0, 0};\nconst int dy[4] = {0, 0, -1, 1};\n\nstruct Position {\n    int x, y;\n    Position() : x(0), y(0) {}\n    Position(int _x, int _y) : x(_x), y(_y) {}\n    bool operator==(const Position& other) const {\n        return x == other.x && y == other.y;\n    }\n};\n\n// BFS to find shortest path between two points\nvector<pair<int, int>> find_shortest_path(Position start, Position end) {\n    if (start == end) return {};\n    \n    bool visited[GRID_SIZE][GRID_SIZE];\n    pair<int, int> parent[GRID_SIZE][GRID_SIZE];\n    memset(visited, 0, sizeof(visited));\n    \n    queue<Position> q;\n    q.push(start);\n    visited[start.x][start.y] = true;\n    parent[start.x][start.y] = {-1, -1};\n    \n    while (!q.empty()) {\n        Position pos = q.front(); q.pop();\n        \n        if (pos.x == end.x && pos.y == end.y) {\n            // Reconstruct path\n            vector<pair<int, int>> path;\n            int cx = end.x, cy = end.y;\n            while (parent[cx][cy].first != -1) {\n                path.push_back({cx, cy});\n                auto p = parent[cx][cy];\n                cx = p.first;\n                cy = p.second;\n            }\n            reverse(path.begin(), path.end());\n            return path;\n        }\n        \n        for (int i = 0; i < 4; i++) {\n            int nx = pos.x + dx[i];\n            int ny = pos.y + dy[i];\n            \n            if (nx >= 0 && nx < GRID_SIZE && ny >= 0 && ny < GRID_SIZE && !visited[nx][ny]) {\n                visited[nx][ny] = true;\n                parent[nx][ny] = {pos.x, pos.y};\n                q.push(Position(nx, ny));\n            }\n        }\n    }\n    \n    return {};\n}\n\n// Convert path to movement directions\nvector<char> path_to_directions(Position start, const vector<pair<int, int>>& path) {\n    vector<char> directions;\n    int cx = start.x, cy = start.y;\n    \n    for (auto [nx, ny] : path) {\n        if (nx == cx - 1 && ny == cy) directions.push_back('U');\n        else if (nx == cx + 1 && ny == cy) directions.push_back('D');\n        else if (nx == cx && ny == cy - 1) directions.push_back('L');\n        else if (nx == cx && ny == cy + 1) directions.push_back('R');\n        cx = nx;\n        cy = ny;\n    }\n    \n    return directions;\n}\n\n// Find nearest sink for a given source\npair<Position, int> find_nearest_sink(Position source, vector<pair<Position, int>>& sinks) {\n    int best_idx = -1;\n    int min_distance = GRID_SIZE * GRID_SIZE;\n    Position best_pos;\n    \n    for (int i = 0; i < sinks.size(); i++) {\n        if (sinks[i].second > 0) {\n            int dist = abs(source.x - sinks[i].first.x) + abs(source.y - sinks[i].first.y);\n            if (dist < min_distance) {\n                min_distance = dist;\n                best_idx = i;\n                best_pos = sinks[i].first;\n            }\n        }\n    }\n    \n    if (best_idx != -1) {\n        return {best_pos, best_idx};\n    }\n    \n    return {Position(-1, -1), -1};\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    // Read grid\n    for (int i = 0; i < GRID_SIZE; i++) {\n        for (int j = 0; j < GRID_SIZE; j++) {\n            cin >> grid[i][j];\n        }\n    }\n    \n    // Collect sources (positive) and sinks (negative)\n    vector<pair<Position, int>> sources, sinks;\n    for (int i = 0; i < GRID_SIZE; i++) {\n        for (int j = 0; j < GRID_SIZE; j++) {\n            if (grid[i][j] > 0) {\n                sources.push_back({Position(i, j), grid[i][j]});\n            } else if (grid[i][j] < 0) {\n                sinks.push_back({Position(i, j), -grid[i][j]});\n            }\n        }\n    }\n    \n    vector<string> operations;\n    Position current_pos(0, 0);\n    int load = 0;\n    \n    // Process sources one by one, always going to the nearest available sink\n    for (int src_idx = 0; src_idx < sources.size(); src_idx++) {\n        auto& source = sources[src_idx];\n        \n        while (source.second > 0) {\n            // Find the nearest sink with demand\n            auto nearest = find_nearest_sink(source.first, sinks);\n            if (nearest.second == -1) break; // No more sinks\n            \n            int sink_idx = nearest.second;\n            auto& sink = sinks[sink_idx];\n            \n            if (sink.second <= 0) continue;\n            \n            int transport_amount = min(source.second, sink.second);\n            if (transport_amount <= 0) break;\n            \n            // Move to source if needed\n            if (!(current_pos == source.first)) {\n                auto path = find_shortest_path(current_pos, source.first);\n                auto directions = path_to_directions(current_pos, path);\n                \n                for (char dir : directions) {\n                    operations.push_back(string(1, dir));\n                }\n                current_pos = source.first;\n            }\n            \n            // Load soil\n            operations.push_back(\"+\" + to_string(transport_amount));\n            load += transport_amount;\n            source.second -= transport_amount;\n            \n            // Move to sink\n            if (!(current_pos == sink.first)) {\n                auto path = find_shortest_path(current_pos, sink.first);\n                auto directions = path_to_directions(current_pos, path);\n                \n                for (char dir : directions) {\n                    operations.push_back(string(1, dir));\n                }\n                current_pos = sink.first;\n            }\n            \n            // Unload soil\n            operations.push_back(\"-\" + to_string(transport_amount));\n            load -= transport_amount;\n            sink.second -= transport_amount;\n        }\n    }\n    \n    // Output operations\n    for (const string& op : operations) {\n        cout << op << \"\\n\";\n    }\n    \n    return 0;\n}","ahc035":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <numeric>\n\nusing namespace std;\n\nconst int N = 6;\nconst int SEED_COUNT = 2 * N * (N - 1); // 60 seeds\n\n// Calculate total value of a seed\nint calculateValue(const vector<int>& seed) {\n    return accumulate(seed.begin(), seed.end(), 0);\n}\n\n// Generate placement strategy with better positioning\nvector<vector<int>> generatePlacement(const vector<vector<int>>& seeds) {\n    vector<vector<int>> placement(N, vector<int>(N));\n    \n    // Rank seeds by value\n    vector<pair<int, int>> seedRanking;\n    for (int i = 0; i < SEED_COUNT; i++) {\n        seedRanking.push_back({calculateValue(seeds[i]), i});\n    }\n    sort(seedRanking.begin(), seedRanking.end(), greater<pair<int, int>>());\n    \n    // Create position priority based on number of neighbors (influence)\n    // Format: {neighbors, row, col}\n    vector<pair<int, pair<int, int>>> positionPriority;\n    \n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            int neighbors = 0;\n            if (i > 0) neighbors++;     // up\n            if (i < N-1) neighbors++;   // down\n            if (j > 0) neighbors++;     // left\n            if (j < N-1) neighbors++;   // right\n            positionPriority.push_back({neighbors, {i, j}});\n        }\n    }\n    \n    // Sort positions by number of neighbors (descending)\n    sort(positionPriority.begin(), positionPriority.end(), greater<pair<int, pair<int, int>>>());\n    \n    // Assign best seeds to most influential positions\n    vector<bool> seedUsed(SEED_COUNT, false);\n    \n    for (int posIdx = 0; posIdx < min((int)positionPriority.size(), SEED_COUNT); posIdx++) {\n        int neighbors = positionPriority[posIdx].first;\n        int row = positionPriority[posIdx].second.first;\n        int col = positionPriority[posIdx].second.second;\n        \n        // Find the next best unused seed\n        int seedIdx = 0;\n        while (seedIdx < SEED_COUNT && seedUsed[seedIdx]) {\n            seedIdx++;\n        }\n        \n        if (seedIdx < SEED_COUNT) {\n            placement[row][col] = seedIdx;\n            seedUsed[seedIdx] = true;\n        }\n    }\n    \n    // Fill any remaining positions (shouldn't happen but just in case)\n    int nextSeed = 0;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (!seedUsed[placement[i][j]]) {\n                while (nextSeed < SEED_COUNT && seedUsed[nextSeed]) nextSeed++;\n                if (nextSeed < SEED_COUNT) {\n                    placement[i][j] = nextSeed;\n                    seedUsed[nextSeed] = true;\n                }\n            }\n        }\n    }\n    \n    return placement;\n}\n\n// Alternative strategy: focus on clustering high-value seeds\nvector<vector<int>> generatePlacementClustered(const vector<vector<int>>& seeds) {\n    vector<vector<int>> placement(N, vector<int>(N));\n    \n    // Rank seeds by value\n    vector<pair<int, int>> seedRanking;\n    for (int i = 0; i < SEED_COUNT; i++) {\n        seedRanking.push_back({calculateValue(seeds[i]), i});\n    }\n    sort(seedRanking.begin(), seedRanking.end(), greater<pair<int, int>>());\n    \n    // Define a good placement pattern that clusters high-value seeds\n    // Place top seeds in center area to maximize their influence\n    vector<pair<int, int>> placementOrder = {\n        // Center positions (4 neighbors)\n        {2,2}, {2,3}, {3,2}, {3,3},\n        // Near-center positions (3 neighbors)\n        {1,2}, {1,3}, {2,1}, {2,4}, {3,1}, {3,4}, {4,2}, {4,3},\n        // Edge positions (2 neighbors)\n        {0,2}, {0,3}, {2,0}, {3,0}, {2,5}, {3,5}, {5,2}, {5,3},\n        {1,1}, {1,4}, {4,1}, {4,4},\n        // Corner positions (2 neighbors)\n        {0,1}, {0,4}, {1,0}, {1,5}, {4,0}, {4,5}, {5,1}, {5,4},\n        {0,0}, {0,5}, {5,0}, {5,5}\n    };\n    \n    vector<bool> seedUsed(SEED_COUNT, false);\n    \n    for (int i = 0; i < min((int)placementOrder.size(), SEED_COUNT); i++) {\n        int row = placementOrder[i].first;\n        int col = placementOrder[i].second;\n        \n        // Assign the i-th best seed\n        if (i < seedRanking.size()) {\n            int seedIdx = seedRanking[i].second;\n            placement[row][col] = seedIdx;\n            seedUsed[seedIdx] = true;\n        }\n    }\n    \n    // Fill remaining positions\n    int seedIdx = 0;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (placement[i][j] == 0 && (i != 0 || j != 0)) { // If not properly set\n                while (seedIdx < SEED_COUNT && seedUsed[seedIdx]) seedIdx++;\n                if (seedIdx < SEED_COUNT) {\n                    placement[i][j] = seedIdx;\n                    seedUsed[seedIdx] = true;\n                }\n            }\n        }\n    }\n    \n    return placement;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    int N_input, M, T;\n    cin >> N_input >> M >> T;\n    \n    vector<vector<int>> seeds(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 >> seeds[i][j];\n        }\n    }\n    \n    // Process T turns\n    for (int turn = 0; turn < T; turn++) {\n        // Use the clustered approach as it's simpler and likely more effective\n        auto placement = generatePlacementClustered(seeds);\n        \n        // Output placement\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                cout << placement[i][j];\n                if (j < N - 1) cout << \" \";\n            }\n            cout << endl;\n        }\n        cout.flush();\n        \n        // Read new seeds for next iteration\n        if (turn < T - 1) {\n            for (int i = 0; i < SEED_COUNT; i++) {\n                for (int j = 0; j < M; j++) {\n                    cin >> seeds[i][j];\n                }\n            }\n        }\n    }\n    \n    return 0;\n}","ahc038":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MAX_N = 30;\nconst int MAX_V = 15;\n\nstruct Point {\n    int x, y;\n    Point() : x(0), y(0) {}\n    Point(int x, int y) : x(x), y(y) {}\n    Point operator+(const Point& p) const { return Point(x + p.x, y + p.y); }\n    Point operator-(const Point& p) const { return Point(x - p.x, y - p.y); }\n    bool operator==(const Point& p) const { return x == p.x && y == p.y; }\n    bool operator!=(const Point& p) const { return !(*this == p); }\n};\n\nconst Point DIRS[4] = {Point(0, 1), Point(1, 0), Point(0, -1), Point(-1, 0)}; // R, D, L, U\nconst char DIR_CHARS[4] = {'R', 'D', 'L', 'U'};\n\nclass RobotArm {\npublic:\n    int V;\n    vector<pair<int, int>> tree; // {parent, length}\n    Point root_pos;\n    \n    RobotArm(int v) : V(v) {\n        tree.resize(V);\n    }\n};\n\nclass Solution {\nprivate:\n    int N, M, V;\n    vector<vector<int>> initial, target;\n    vector<Point> initial_pos, target_pos;\n    \npublic:\n    void readInput() {\n        cin >> N >> M >> V;\n        initial.assign(N, vector<int>(N, 0));\n        target.assign(N, vector<int>(N, 0));\n        \n        for (int i = 0; i < N; i++) {\n            string line;\n            cin >> line;\n            for (int j = 0; j < N; j++) {\n                initial[i][j] = line[j] - '0';\n                if (initial[i][j]) {\n                    initial_pos.push_back(Point(i, j));\n                }\n            }\n        }\n        \n        for (int i = 0; i < N; i++) {\n            string line;\n            cin >> line;\n            for (int j = 0; j < N; j++) {\n                target[i][j] = line[j] - '0';\n                if (target[i][j]) {\n                    target_pos.push_back(Point(i, j));\n                }\n            }\n        }\n    }\n    \n    RobotArm designArm() {\n        RobotArm arm(V);\n        \n        // Create a star structure: root (0) with all other vertices as direct children\n        for (int i = 1; i < V; i++) {\n            arm.tree[i] = {0, 1}; // Parent is 0 (root), length 1\n        }\n        \n        // Initial position at (0, 0)\n        arm.root_pos = Point(0, 0);\n        \n        return arm;\n    }\n    \n    // Find all leaf vertices (fingertips)\n    vector<int> findLeaves(const RobotArm& arm) {\n        vector<int> is_child(arm.V, 0);\n        for (int i = 1; i < arm.V; i++) {\n            is_child[arm.tree[i].first] = 1; // Mark parents as having children\n        }\n        \n        vector<int> leaves;\n        for (int i = 0; i < arm.V; i++) {\n            if (!is_child[i]) { // Vertices that are not parents are leaves\n                leaves.push_back(i);\n            }\n        }\n        return leaves;\n    }\n    \n    string createMoveString(char move, const vector<char>& rotations, const vector<char>& actions) {\n        string result;\n        result += move;\n        for (int i = 1; i < rotations.size(); i++) {\n            result += rotations[i];\n        }\n        for (int i = 0; i < actions.size(); i++) {\n            result += actions[i];\n        }\n        return result;\n    }\n    \n    // Calculate positions of all vertices based on current configuration\n    vector<Point> calculateVertexPositions(const Point& root_pos, const vector<int>& directions, const RobotArm& arm) {\n        vector<Point> positions(arm.V);\n        positions[0] = root_pos;\n        \n        // For star structure, each leaf extends from root in its direction\n        for (int i = 1; i < arm.V; i++) {\n            Point offset;\n            int dir = directions[i]; // 0=R, 1=D, 2=L, 3=U\n            if (dir == 0) offset = Point(0, 1);        // Right\n            else if (dir == 1) offset = Point(1, 0);   // Down\n            else if (dir == 2) offset = Point(0, -1);  // Left\n            else offset = Point(-1, 0);                // Up\n            \n            positions[i] = positions[arm.tree[i].first] + offset;\n        }\n        \n        return positions;\n    }\n    \n    vector<string> solve(RobotArm& arm) {\n        vector<string> operations;\n        Point current_pos = arm.root_pos;\n        vector<vector<int>> current_grid = initial; // Track current state\n        \n        // Track direction of each edge from parent (0=R, 1=D, 2=L, 3=U)\n        vector<int> edge_directions(V, 0); // Initially all pointing right\n        vector<int> holding(V, 0); // Track which vertices are holding takoyaki\n        \n        // Find leaf vertices\n        vector<int> leaves = findLeaves(arm);\n        \n        // Simple strategy\n        int turns = 0;\n        int max_turns = min(1000, (int)(1e5));\n        \n        random_device rd;\n        mt19937 gen(rd());\n        uniform_int_distribution<> dis(0, 3);\n        \n        while (turns < max_turns) {\n            // Move randomly but try to stay in bounds\n            char move_char = '.';\n            Point new_pos = current_pos;\n            \n            int move_dir = dis(gen) % 5;\n            if (move_dir < 4) {\n                new_pos = current_pos + DIRS[move_dir];\n                if (new_pos.x >= 0 && new_pos.x < N && new_pos.y >= 0 && new_pos.y < N) {\n                    current_pos = new_pos;\n                    move_char = DIR_CHARS[move_dir];\n                } else {\n                    move_char = '.';\n                }\n            }\n            \n            // Random rotations\n            vector<char> rotations(V, '.');\n            for (int i = 1; i < V; i++) {\n                if (dis(gen) % 4 == 0) {\n                    if (dis(gen) % 2 == 0) {\n                        rotations[i] = 'R';\n                        edge_directions[i] = (edge_directions[i] + 1) % 4;\n                    } else {\n                        rotations[i] = 'L';\n                        edge_directions[i] = (edge_directions[i] + 3) % 4;\n                    }\n                }\n            }\n            \n            // Calculate vertex positions\n            vector<Point> vertex_positions = calculateVertexPositions(current_pos, edge_directions, arm);\n            \n            // Actions for leaves only\n            vector<char> actions(V, '.');\n            for (int leaf : leaves) {\n                if (leaf < V) { // Safety check\n                    Point pos = vertex_positions[leaf];\n                    // Check bounds\n                    if (pos.x >= 0 && pos.x < N && pos.y >= 0 && pos.y < N) {\n                        if (holding[leaf] == 0 && current_grid[pos.x][pos.y] == 1 && target[pos.x][pos.y] == 0) {\n                            // Grab takoyaki\n                            actions[leaf] = 'P';\n                            holding[leaf] = 1;\n                            current_grid[pos.x][pos.y] = 0;\n                        } else if (holding[leaf] == 1 && current_grid[pos.x][pos.y] == 0 && target[pos.x][pos.y] == 1) {\n                            // Place takoyaki\n                            actions[leaf] = 'P';\n                            holding[leaf] = 0;\n                            current_grid[pos.x][pos.y] = 1;\n                        }\n                    }\n                }\n            }\n            \n            operations.push_back(createMoveString(move_char, rotations, actions));\n            turns++;\n        }\n        \n        return operations;\n    }\n    \n    void outputSolution(RobotArm& arm, const vector<string>& operations) {\n        // Output arm design\n        cout << arm.V << endl;\n        for (int i = 1; i < arm.V; i++) {\n            cout << arm.tree[i].first << \" \" << arm.tree[i].second << endl;\n        }\n        cout << arm.root_pos.x << \" \" << arm.root_pos.y << endl;\n        \n        // Output operations\n        for (const string& op : operations) {\n            cout << op << endl;\n        }\n    }\n    \n    void run() {\n        readInput();\n        RobotArm arm = designArm();\n        vector<string> operations = solve(arm);\n        outputSolution(arm, operations);\n    }\n};\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    Solution solution;\n    solution.run();\n    \n    return 0;\n}","ahc039":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MAX_N = 5000;\nconst int FIELD_SIZE = 100000;\n\nstruct Point {\n    int x, y;\n    Point() : x(0), y(0) {}\n    Point(int x, int y) : x(x), y(y) {}\n};\n\nstruct Rectangle {\n    int x1, y1, x2, y2;\n    int mackerels, sardines;\n    int score;\n    \n    Rectangle() : x1(0), y1(0), x2(0), y2(0), mackerels(0), sardines(0), score(0) {}\n    Rectangle(int x1, int y1, int x2, int y2) : x1(x1), y1(y1), x2(x2), y2(y2), mackerels(0), sardines(0), score(0) {}\n    \n    bool isValid() const {\n        return x1 >= 0 && x2 <= FIELD_SIZE && y1 >= 0 && y2 <= FIELD_SIZE && \n               x1 < x2 && y1 < y2 && perimeter() <= 400000;\n    }\n    \n    int perimeter() const {\n        return 2 * (x2 - x1 + y2 - y1);\n    }\n};\n\nvector<Point> mackerels(MAX_N);\nvector<Point> sardines(MAX_N);\n\n// Count points in rectangle\npair<int, int> countPoints(const Rectangle& rect) {\n    int m_count = 0, s_count = 0;\n    \n    for (const auto& p : mackerels) {\n        if (p.x >= rect.x1 && p.x <= rect.x2 && p.y >= rect.y1 && p.y <= rect.y2) {\n            m_count++;\n        }\n    }\n    \n    for (const auto& p : sardines) {\n        if (p.x >= rect.x1 && p.x <= rect.x2 && p.y >= rect.y1 && p.y <= rect.y2) {\n            s_count++;\n        }\n    }\n    \n    return {m_count, s_count};\n}\n\n// Generate rectangles around mackerel points with adaptive sizing\nvector<Rectangle> generateRectangles() {\n    vector<Rectangle> rectangles;\n    \n    // Try different rectangle sizes centered around mackerels\n    vector<int> sizes = {3000, 5000, 8000, 12000, 15000, 20000, 25000, 30000};\n    \n    for (int i = 0; i < min(200, MAX_N); i++) {\n        const Point& center = mackerels[i];\n        \n        for (int size : sizes) {\n            int x1 = max(0, center.x - size/2);\n            int y1 = max(0, center.y - size/2);\n            int x2 = min(FIELD_SIZE, center.x + size/2);\n            int y2 = min(FIELD_SIZE, center.y + size/2);\n            \n            Rectangle rect(x1, y1, x2, y2);\n            if (rect.isValid()) {\n                auto counts = countPoints(rect);\n                rect.mackerels = counts.first;\n                rect.sardines = counts.second;\n                rect.score = rect.mackerels - rect.sardines;\n                \n                if (rect.score > 0) {\n                    rectangles.push_back(rect);\n                }\n            }\n        }\n    }\n    \n    // Also try rectangles around clusters using simple grid approach\n    const int GRID_SIZE = 20;\n    vector<vector<pair<int, int>>> grid(GRID_SIZE, vector<pair<int, int>>(GRID_SIZE, {0, 0}));\n    \n    // Count mackerels and sardines in each grid cell\n    for (const auto& p : mackerels) {\n        int gx = min(GRID_SIZE - 1, p.x * GRID_SIZE / (FIELD_SIZE + 1));\n        int gy = min(GRID_SIZE - 1, p.y * GRID_SIZE / (FIELD_SIZE + 1));\n        grid[gx][gy].first++;\n    }\n    \n    for (const auto& p : sardines) {\n        int gx = min(GRID_SIZE - 1, p.x * GRID_SIZE / (FIELD_SIZE + 1));\n        int gy = min(GRID_SIZE - 1, p.y * GRID_SIZE / (FIELD_SIZE + 1));\n        grid[gx][gy].second++;\n    }\n    \n    // Generate rectangles around high mackerel, low sardine areas\n    for (int i = 0; i < GRID_SIZE; i++) {\n        for (int j = 0; j < GRID_SIZE; j++) {\n            if (grid[i][j].first > grid[i][j].second) {  // More mackerels than sardines\n                // Try different window sizes\n                for (int window = 2; window <= 6; window++) {\n                    int x1_grid = max(0, i - window/2);\n                    int y1_grid = max(0, j - window/2);\n                    int x2_grid = min(GRID_SIZE - 1, i + window/2);\n                    int y2_grid = min(GRID_SIZE - 1, j + window/2);\n                    \n                    int x1 = x1_grid * FIELD_SIZE / GRID_SIZE;\n                    int y1 = y1_grid * FIELD_SIZE / GRID_SIZE;\n                    int x2 = min(FIELD_SIZE, (x2_grid + 1) * FIELD_SIZE / GRID_SIZE);\n                    int y2 = min(FIELD_SIZE, (y2_grid + 1) * FIELD_SIZE / GRID_SIZE);\n                    \n                    Rectangle rect(x1, y1, x2, y2);\n                    if (rect.isValid()) {\n                        auto counts = countPoints(rect);\n                        rect.mackerels = counts.first;\n                        rect.sardines = counts.second;\n                        rect.score = rect.mackerels - rect.sardines;\n                        \n                        if (rect.score > 0) {\n                            rectangles.push_back(rect);\n                        }\n                    }\n                }\n            }\n        }\n    }\n    \n    // Sort by score\n    sort(rectangles.begin(), rectangles.end(), [](const Rectangle& a, const Rectangle& b) {\n        return a.score > b.score;\n    });\n    \n    // Remove duplicates\n    rectangles.erase(unique(rectangles.begin(), rectangles.end(), \n                           [](const Rectangle& a, const Rectangle& b) {\n                               return a.x1 == b.x1 && a.y1 == b.y1 && a.x2 == b.x2 && a.y2 == b.y2;\n                           }), rectangles.end());\n    \n    return rectangles;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    int N;\n    cin >> N;\n    \n    for (int i = 0; i < N; i++) {\n        cin >> mackerels[i].x >> mackerels[i].y;\n    }\n    \n    for (int i = 0; i < N; i++) {\n        cin >> sardines[i].x >> sardines[i].y;\n    }\n    \n    // Generate rectangles\n    vector<Rectangle> rectangles = generateRectangles();\n    \n    // Find the best rectangle\n    Rectangle best_rect(10000, 10000, 90000, 90000);  // Default fallback\n    int best_score = -1;\n    \n    if (!rectangles.empty()) {\n        best_rect = rectangles[0];\n        best_score = best_rect.score;\n    }\n    \n    // Output the rectangle as a polygon\n    cout << \"4\\n\";\n    cout << best_rect.x1 << \" \" << best_rect.y1 << \"\\n\";\n    cout << best_rect.x2 << \" \" << best_rect.y1 << \"\\n\";\n    cout << best_rect.x2 << \" \" << best_rect.y2 << \"\\n\";\n    cout << best_rect.x1 << \" \" << best_rect.y2 << \"\\n\";\n    \n    return 0;\n}","ahc040":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Rectangle {\n    int w, h;\n    int idx;\n    \n    Rectangle(int _w, int _h, int _idx) : w(_w), h(_h), idx(_idx) {}\n};\n\nstruct Placement {\n    int p, r, d, b;\n    \n    Placement(int _p, int _r, char _d, int _b) : p(_p), r(_r), d(_d), b(_b) {}\n};\n\nclass SkylinePacker {\nprivate:\n    struct SkylineNode {\n        int x, y, width;\n        SkylineNode(int _x, int _y, int _width) : x(_x), y(_y), width(_width) {}\n    };\n    \n    vector<SkylineNode> skyline;\n    \npublic:\n    SkylinePacker() {\n        // Initialize with base skyline\n        skyline.emplace_back(0, 0, 1000000000);\n    }\n    \n    pair<int, int> findPosition(int width, int height) {\n        // Find best position using skyline algorithm\n        int best_x = 0;\n        int best_y = 0;\n        int best_area = numeric_limits<int>::max();\n        int best_idx = 0;\n        \n        for (size_t i = 0; i < skyline.size(); i++) {\n            int y = skyline[i].y;\n            int x = skyline[i].x;\n            \n            // Check if rectangle fits at this position\n            int current_width = 0;\n            for (size_t j = i; j < skyline.size() && skyline[j].y <= y; j++) {\n                if (skyline[j].x >= x + width) break;\n                current_width += skyline[j].width;\n                if (current_width >= width) {\n                    // Found a valid position\n                    if (y + height < best_area) {\n                        best_area = y + height;\n                        best_x = x;\n                        best_y = y;\n                        best_idx = i;\n                    }\n                    break;\n                }\n            }\n        }\n        \n        return {best_x, best_y};\n    }\n};\n\n// Strategy 1: Greedy skyline-based packing\nvector<Placement> skylineGreedy(const vector<pair<int,int>>& rectangles, mt19937& rng) {\n    vector<Placement> placements;\n    int n = rectangles.size();\n    \n    if (n == 0) return placements;\n    \n    // Simple sequential placement with smart rotation\n    for (int i = 0; i < n; i++) {\n        // Smart rotation based on aspect ratio\n        int rotate = 0;\n        double aspect_ratio = (double)rectangles[i].first / rectangles[i].second;\n        \n        // Rotate if it makes the rectangle more \"square-like\" or if one dimension is much larger\n        if (aspect_ratio > 3.0) {\n            rotate = 1; // Width >> height, rotate\n        } else if (aspect_ratio < 0.33) {\n            rotate = 1; // Height >> width, rotate\n        } else {\n            rotate = 0; // Keep original orientation\n        }\n        \n        // Placement strategy\n        char direction = 'U';\n        int reference = -1;\n        \n        if (i > 0) {\n            // Most recent rectangle as reference, sometimes random\n            if (uniform_int_distribution<int>(0, 2)(rng) == 0 && i > 1) {\n                reference = uniform_int_distribution<int>(0, i-2)(rng);\n            } else {\n                reference = i - 1;\n            }\n            direction = (uniform_int_distribution<int>(0, 3)(rng) == 0) ? 'L' : 'U';\n        }\n        \n        placements.emplace_back(i, rotate, direction, reference);\n    }\n    \n    return placements;\n}\n\n// Strategy 2: Try to minimize perimeter by smart orientation\nvector<Placement> perimeterOptimized(const vector<pair<int,int>>& rectangles, mt19937& rng) {\n    vector<Placement> placements;\n    int n = rectangles.size();\n    \n    // Calculate total area to estimate optimal aspect ratio\n    long long total_area = 0;\n    for (const auto& rect : rectangles) {\n        total_area += (long long)rect.first * rect.second;\n    }\n    \n    // Place all rectangles with orientation optimization\n    for (int i = 0; i < n; i++) {\n        int rotate = 0;\n        \n        // Orientation decision based on global strategy\n        if (i % 3 == 0) {\n            // Every 3rd rectangle, try to minimize height\n            rotate = (rectangles[i].first > rectangles[i].second) ? 1 : 0;\n        } else if (i % 3 == 1) {\n            // Every 3rd rectangle starting at 1, try to minimize width\n            rotate = (rectangles[i].second > rectangles[i].first) ? 1 : 0;\n        } else {\n            // Random for others\n            rotate = uniform_int_distribution<int>(0, 1)(rng);\n        }\n        \n        char direction = (uniform_int_distribution<int>(0, 1)(rng) == 0) ? 'U' : 'L';\n        int reference = -1;\n        \n        if (i > 0) {\n            reference = max(0, i - uniform_int_distribution<int>(1, min(3, i))(rng));\n        }\n        \n        placements.emplace_back(i, rotate, direction, reference);\n    }\n    \n    return placements;\n}\n\n// Strategy 3: Adaptive based on rectangle sizes\nvector<Placement> adaptivePlace(const vector<pair<int,int>>& rectangles, mt19937& rng) {\n    vector<Placement> placements;\n    int n = rectangles.size();\n    \n    // Sort rectangles by area to place larger ones first (but maintain index order in output)\n    vector<pair<long long, int>> area_indices(n);\n    for (int i = 0; i < n; i++) {\n        area_indices[i] = {(long long)rectangles[i].first * rectangles[i].second, i};\n    }\n    \n    // But we still need to output in index order, so we'll use a different approach\n    for (int i = 0; i < n; i++) {\n        int rotate = 0;\n        long long area = (long long)rectangles[i].first * rectangles[i].second;\n        \n        // For large rectangles, be more careful about orientation\n        if (area > 1e10) {\n            // Large rectangle - try to make it more compact\n            double ratio = max(rectangles[i].first, rectangles[i].second) / \n                          (double)min(rectangles[i].first, rectangles[i].second);\n            if (ratio > 2.0) {\n                rotate = 1; // Rotate to make more square\n            }\n        } else {\n            // Small rectangle - random orientation\n            rotate = uniform_int_distribution<int>(0, 1)(rng);\n        }\n        \n        char direction = 'U';\n        int reference = -1;\n        \n        if (i > 0) {\n            // Prefer recent placements but sometimes vary\n            if (uniform_int_distribution<int>(0, 4)(rng) == 0) {\n                reference = uniform_int_distribution<int>(0, i-1)(rng);\n            } else {\n                reference = i - 1; // Most recent\n            }\n            direction = (rectangles[i].first > rectangles[i].second) ? 'L' : 'U';\n        }\n        \n        placements.emplace_back(i, rotate, direction, reference);\n    }\n    \n    return placements;\n}\n\n// Strategy 4: Focus on minimizing unused rectangle penalty\nvector<Placement> minimizeUnused(const vector<pair<int,int>>& rectangles, mt19937& rng) {\n    vector<Placement> placements;\n    int n = rectangles.size();\n    \n    // When we have many rectangles, we might want to skip small ones\n    // But we still need to maintain order, so we place all but make better choices\n    \n    for (int i = 0; i < n; i++) {\n        int rotate = 0;\n        \n        // Rotate small rectangles to minimize their contribution to unused penalty\n        long long area = (long long)rectangles[i].first * rectangles[i].second;\n        if (area < 1e8) { // Small rectangle\n            // Rotate to make the larger dimension vertical (to minimize width impact)\n            rotate = (rectangles[i].first > rectangles[i].second) ? 1 : 0;\n        } else {\n            // Large rectangle - place normally\n            rotate = uniform_int_distribution<int>(0, 1)(rng);\n        }\n        \n        char direction = (i % 2 == 0) ? 'U' : 'L'; // Alternate directions\n        int reference = -1;\n        \n        if (i > 0) {\n            reference = i - 1; // Always reference previous\n        }\n        \n        placements.emplace_back(i, rotate, direction, reference);\n    }\n    \n    return placements;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    int N, T, sigma;\n    cin >> N >> T >> sigma;\n    \n    vector<pair<int,int>> measured_rectangles(N);\n    for (int i = 0; i < N; i++) {\n        cin >> measured_rectangles[i].first >> measured_rectangles[i].second;\n    }\n    \n    // Store results to adapt strategies\n    vector<pair<int,int>> results;\n    \n    // Initialize random number generator\n    random_device rd;\n    mt19937 rng(rd());\n    \n    // Try different strategies for each turn\n    for (int turn = 0; turn < T; turn++) {\n        vector<Placement> placements;\n        \n        // Choose strategy based on turn and previous results\n        if (turn == 0) {\n            // First turn - try skyline greedy\n            placements = skylineGreedy(measured_rectangles, rng);\n        } else if (turn == 1) {\n            // Second turn - perimeter optimized\n            placements = perimeterOptimized(measured_rectangles, rng);\n        } else if (turn == 2) {\n            // Third turn - adaptive placement\n            placements = adaptivePlace(measured_rectangles, rng);\n        } else if (turn == 3) {\n            // Fourth turn - minimize unused\n            placements = minimizeUnused(measured_rectangles, rng);\n        } else if (turn % 4 == 0) {\n            // Every 4th turn - try skyline with variation\n            placements = skylineGreedy(measured_rectangles, rng);\n        } else if (turn % 4 == 1) {\n            // Try perimeter optimization\n            placements = perimeterOptimized(measured_rectangles, rng);\n        } else if (turn % 4 == 2) {\n            // Try adaptive placement\n            placements = adaptivePlace(measured_rectangles, rng);\n        } else {\n            // Minimize unused strategy\n            placements = minimizeUnused(measured_rectangles, rng);\n        }\n        \n        // Output placements\n        cout << placements.size() << \"\\n\";\n        for (const auto& p : placements) {\n            cout << p.p << \" \" << p.r << \" \" << (char)p.d << \" \" << p.b << \"\\n\";\n        }\n        cout << flush;\n        \n        // Read measurement result\n        int W_meas, H_meas;\n        cin >> W_meas >> H_meas;\n        \n        // Store results for adaptation (not used yet but could be useful)\n        results.emplace_back(W_meas, H_meas);\n    }\n    \n    return 0;\n}","ahc041":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MAXN = 1000;\nconst int MAXH = 10;\n\nint N, M, H;\nvector<int> A;\nvector<vector<int>> adj;\nvector<pair<int, int>> coords;\nvector<int> parent;\n\n// BFS-based tree building with priority\nvoid buildTreeFromRoot(int root, vector<bool>& used) {\n    queue<pair<int, int>> q; // {vertex, depth}\n    q.push({root, 0});\n    used[root] = true;\n    \n    while (!q.empty()) {\n        auto [v, depth] = q.front();\n        q.pop();\n        \n        if (depth >= H) continue;\n        \n        // Collect unassigned neighbors and sort by beauty value\n        vector<pair<int, int>> candidates;\n        for (int u : adj[v]) {\n            if (!used[u]) {\n                candidates.push_back({A[u], u});\n            }\n        }\n        \n        // Sort by beauty value descending\n        sort(candidates.rbegin(), candidates.rend());\n        \n        // Add neighbors up to height limit\n        for (auto& [beauty, u] : candidates) {\n            if (!used[u]) {\n                parent[u] = v;\n                used[u] = true;\n                q.push({u, depth + 1});\n            }\n        }\n    }\n}\n\n// Build initial solution\nvoid buildInitialSolution() {\n    parent.assign(N, -1);\n    vector<bool> used(N, false);\n    \n    // Create list of potential roots (high degree + high beauty)\n    vector<pair<int, int>> root_candidates;\n    for (int i = 0; i < N; i++) {\n        root_candidates.push_back({A[i] * (adj[i].size() + 1), i});\n    }\n    sort(root_candidates.rbegin(), root_candidates.rend());\n    \n    // Build trees from best roots first\n    for (auto& [score, root] : root_candidates) {\n        if (!used[root]) {\n            buildTreeFromRoot(root, used);\n        }\n    }\n}\n\n// Calculate total attractiveness\nlong long calculateTotalAttractiveness() {\n    vector<int> depth(N, 0);\n    vector<int> tree_root(N, -1);\n    \n    // Find tree roots and initialize depths\n    for (int i = 0; i < N; i++) {\n        if (parent[i] == -1) {\n            tree_root[i] = i;\n        }\n    }\n    \n    // BFS to calculate depths for all vertices\n    queue<int> q;\n    for (int i = 0; i < N; i++) {\n        if (parent[i] == -1) {\n            q.push(i);\n            depth[i] = 0;\n        }\n    }\n    \n    while (!q.empty()) {\n        int v = q.front();\n        q.pop();\n        \n        for (int u : adj[v]) {\n            if (parent[u] == v && depth[u] == 0 && u != v) {\n                depth[u] = depth[v] + 1;\n                q.push(u);\n            }\n        }\n    }\n    \n    long long total = 0;\n    for (int i = 0; i < N; i++) {\n        total += (long long)(depth[i] + 1) * A[i];\n    }\n    return total;\n}\n\n// Find root of a vertex's tree\nint findRoot(int v) {\n    while (parent[v] != -1) {\n        v = parent[v];\n    }\n    return v;\n}\n\n// Calculate depth of a vertex\nint calculateDepth(int v) {\n    int d = 0;\n    while (parent[v] != -1) {\n        d++;\n        v = parent[v];\n    }\n    return d;\n}\n\n// Local search improvement\nvoid localSearch() {\n    const int MAX_ITERATIONS = 1000;\n    for (int iter = 0; iter < MAX_ITERATIONS; iter++) {\n        bool improved = false;\n        \n        // Try to move each vertex to a better position\n        for (int v = 0; v < N; v++) {\n            if (parent[v] == -1) continue; // Skip roots\n            \n            int old_parent = parent[v];\n            int current_depth = calculateDepth(v);\n            \n            // Try to attach to a neighbor with better position\n            for (int u : adj[v]) {\n                if (u == old_parent) continue;\n                \n                // Check if u is in a different tree or same tree\n                int u_depth = calculateDepth(u);\n                \n                // If we can place v higher up (closer to root)\n                if (u_depth < MAXH) {\n                    int new_depth = u_depth + 1;\n                    if (new_depth < current_depth) {\n                        // Temporarily make the change\n                        parent[v] = u;\n                        \n                        // Check if this creates a cycle or violates constraints\n                        int root_check = findRoot(v);\n                        int new_v_depth = calculateDepth(v);\n                        \n                        if (new_v_depth <= MAXH) {\n                            improved = true;\n                            break;\n                        } else {\n                            // Revert if invalid\n                            parent[v] = old_parent;\n                        }\n                    }\n                }\n            }\n            \n            if (improved) break;\n        }\n        \n        if (!improved) break;\n    }\n}\n\n// Alternative construction method\nvoid buildAlternativeSolution() {\n    parent.assign(N, -1);\n    vector<bool> used(N, false);\n    \n    // Sort vertices by beauty value descending\n    vector<pair<int, int>> beauty_order;\n    for (int i = 0; i < N; i++) {\n        beauty_order.push_back({A[i], i});\n    }\n    sort(beauty_order.rbegin(), beauty_order.rend());\n    \n    // For each vertex in beauty order, if not used, make it root of new tree\n    for (auto& [beauty, v] : beauty_order) {\n        if (!used[v]) {\n            buildTreeFromRoot(v, used);\n        }\n    }\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    cin >> N >> M >> H;\n    A.resize(N);\n    for (int i = 0; i < N; i++) {\n        cin >> A[i];\n    }\n    \n    adj.resize(N);\n    for (int i = 0; i < M; i++) {\n        int u, v;\n        cin >> u >> v;\n        adj[u].push_back(v);\n        adj[v].push_back(u);\n    }\n    \n    coords.resize(N);\n    for (int i = 0; i < N; i++) {\n        cin >> coords[i].first >> coords[i].second;\n    }\n    \n    // Try multiple approaches\n    vector<int> best_solution(N);\n    long long best_score = 0;\n    \n    // Approach 1: High degree + beauty roots\n    buildInitialSolution();\n    localSearch();\n    long long score1 = calculateTotalAttractiveness();\n    \n    if (score1 > best_score) {\n        best_score = score1;\n        best_solution = parent;\n    }\n    \n    // Approach 2: Pure beauty order\n    buildAlternativeSolution();\n    localSearch();\n    long long score2 = calculateTotalAttractiveness();\n    \n    if (score2 > best_score) {\n        best_score = score2;\n        best_solution = parent;\n    }\n    \n    // Output best solution\n    for (int i = 0; i < N; i++) {\n        cout << best_solution[i];\n        if (i < N - 1) cout << \" \";\n    }\n    cout << \"\\n\";\n    \n    return 0;\n}","ahc042":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MAX_N = 20;\nchar board[MAX_N][MAX_N];\nint N;\n\nstruct Position {\n    int row, col;\n};\n\nvector<Position> oni_list;\nvector<Position> fuku_list;\n\nstruct Move {\n    char direction;\n    int index;\n};\n\nvector<Move> moves;\n\n// Check if column j has any Fukunokami above row r\nbool hasFukuAbove(int r, int j) {\n    for (int i = 0; i < r; i++) {\n        if (board[i][j] == 'o') return true;\n    }\n    return false;\n}\n\n// Check if column j has any Fukunokami below row r\nbool hasFukuBelow(int r, int j) {\n    for (int i = r + 1; i < N; i++) {\n        if (board[i][j] == 'o') return true;\n    }\n    return false;\n}\n\n// Check if row i has any Fukunokami to the left of column c\nbool hasFukuLeft(int i, int c) {\n    for (int j = 0; j < c; j++) {\n        if (board[i][j] == 'o') return true;\n    }\n    return false;\n}\n\n// Check if row i has any Fukunokami to the right of column c\nbool hasFukuRight(int i, int c) {\n    for (int j = c + 1; j < N; j++) {\n        if (board[i][j] == 'o') return true;\n    }\n    return false;\n}\n\nvoid addMove(char dir, int idx) {\n    moves.push_back({dir, idx});\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    cin >> N;\n    for (int i = 0; i < N; i++) {\n        string row;\n        cin >> row;\n        for (int j = 0; j < N; j++) {\n            board[i][j] = row[j];\n            if (row[j] == 'x') {\n                oni_list.push_back({i, j});\n            } else if (row[j] == 'o') {\n                fuku_list.push_back({i, j});\n            }\n        }\n    }\n    \n    // Process each Oni\n    for (const auto& oni : oni_list) {\n        int r = oni.row;\n        int c = oni.col;\n        \n        // Try to remove this Oni in the most efficient way\n        // Prefer directions that require fewer moves\n        \n        // Up direction: shift column down\n        if (!hasFukuAbove(r, c)) {\n            // Shift down r+1 times to move Oni out, then shift back\n            for (int i = 0; i < r + 1; i++) addMove('D', c);\n            for (int i = 0; i < r + 1; i++) addMove('U', c);\n        }\n        // Down direction: shift column up\n        else if (!hasFukuBelow(r, c)) {\n            // Shift up N-r times to move Oni out, then shift back\n            for (int i = 0; i < N - r; i++) addMove('U', c);\n            for (int i = 0; i < N - r; i++) addMove('D', c);\n        }\n        // Left direction: shift row right\n        else if (!hasFukuLeft(r, c)) {\n            // Shift right c+1 times to move Oni out, then shift back\n            for (int i = 0; i < c + 1; i++) addMove('R', r);\n            for (int i = 0; i < c + 1; i++) addMove('L', r);\n        }\n        // Right direction: shift row left\n        else if (!hasFukuRight(r, c)) {\n            // Shift left N-c times to move Oni out, then shift back\n            for (int i = 0; i < N - c; i++) addMove('L', r);\n            for (int i = 0; i < N - c; i++) addMove('R', r);\n        }\n    }\n    \n    // Output the moves\n    for (const auto& move : moves) {\n        cout << move.direction << \" \" << move.index << \"\\n\";\n    }\n    \n    return 0;\n}","ahc044":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 100;\nconst int L = 500000;\nconst int SIM_STEPS = 1000000; // Reduced simulation steps\n\nint T[N];\nint a[N], b[N];\n\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\n\n// Fast simulation - simulate fewer steps but scale appropriately\nvector<long long> simulate_sequence(int a[], int b[], int steps = SIM_STEPS) {\n    vector<long long> actual(N, 0);\n    vector<long long> count(N, 0);\n    \n    int current = 0;\n    actual[0]++;\n    count[0]++;\n    \n    // Run simulation\n    for (int step = 1; step < steps; step++) {\n        int next;\n        if (count[current] % 2 == 1) {\n            next = a[current];\n        } else {\n            next = b[current];\n        }\n        current = next;\n        actual[current]++;\n        count[current]++;\n    }\n    \n    // Scale to match L weeks\n    for (int i = 0; i < N; i++) {\n        actual[i] = (1LL * actual[i] * L) / steps;\n    }\n    \n    return actual;\n}\n\n// Calculate error\nlong long calculate_error(const vector<long long>& actual) {\n    long long error = 0;\n    for (int i = 0; i < N; i++) {\n        error += abs(actual[i] - (long long)T[i]);\n    }\n    return error;\n}\n\n// Initialize with smart strategy based on target frequencies\nvoid initialize_assignment() {\n    // Sort employees by target frequency (descending)\n    vector<pair<int, int>> targets;\n    for (int i = 0; i < N; i++) {\n        targets.push_back({T[i], i});\n    }\n    sort(targets.rbegin(), targets.rend());\n    \n    // Create a balanced assignment strategy\n    for (int i = 0; i < N; i++) {\n        int emp = i;\n        \n        // Point to employees with somewhat balanced targets\n        // But favor higher target employees\n        int idx1 = (i * 7 + 17) % N;  // Pseudo-random but deterministic\n        int idx2 = (i * 13 + 23) % N;\n        \n        a[emp] = targets[idx1].second;\n        b[emp] = targets[idx2].second;\n    }\n    \n    // Ensure good connectivity for high-demand employees\n    for (int i = 0; i < min(15, N); i++) {\n        int high_emp = targets[i].second;\n        int from_emp = targets[(i + 10) % N].second;\n        \n        // Make sure some employees point to high-demand ones\n        if (i % 2 == 0) {\n            a[from_emp] = high_emp;\n        } else {\n            b[from_emp] = high_emp;\n        }\n    }\n    \n    // Make sure we can reach employee 0 (starting point)\n    a[targets[0].second] = 0;\n    b[targets[1].second] = 0;\n}\n\n// Simple local improvement\nvoid simple_improvement() {\n    auto actual = simulate_sequence(a, b);\n    long long current_error = calculate_error(actual);\n    \n    // Try a few random modifications\n    for (int iter = 0; iter < 50; iter++) {\n        // Pick a random employee to modify\n        int emp = rng() % N;\n        \n        // Save old values\n        int old_a = a[emp];\n        int old_b = b[emp];\n        \n        // Try new random assignments\n        a[emp] = rng() % N;\n        b[emp] = rng() % N;\n        \n        // Evaluate\n        auto new_actual = simulate_sequence(a, b);\n        long long new_error = calculate_error(new_actual);\n        \n        // Keep improvement, otherwise revert\n        if (new_error >= current_error) {\n            a[emp] = old_a;\n            b[emp] = old_b;\n        } else {\n            current_error = new_error;\n        }\n    }\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    int n, l;\n    cin >> n >> l;\n    for (int i = 0; i < n; i++) {\n        cin >> T[i];\n    }\n    \n    // Initialize with smart strategy\n    initialize_assignment();\n    \n    // Simple improvement\n    simple_improvement();\n    \n    // Output result\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\nconst int N = 800;\n\nint n, m, q, L, W;\nvector<int> G;\nvector<int> lx, rx, ly, ry;\n\n// Query function\nvector<pair<int, int>> query(const vector<int>& cities) {\n    if (cities.size() < 2) return {};\n    \n    cout << \"? \" << cities.size();\n    for (int city : cities) cout << \" \" << city;\n    cout << endl;\n    cout.flush();\n    \n    vector<pair<int, int>> result(cities.size() - 1);\n    for (int i = 0; i < cities.size() - 1; i++) {\n        int u, v;\n        cin >> u >> v;\n        if (u > v) swap(u, v);\n        result[i] = {u, v};\n    }\n    return result;\n}\n\n// Group cities based on center positions\nvector<vector<int>> create_groups() {\n    // Use center coordinates \n    vector<tuple<int, int, int>> city_centers(n);\n    for (int i = 0; i < n; i++) {\n        int cx = (lx[i] + rx[i]) / 2;\n        int cy = (ly[i] + ry[i]) / 2;\n        city_centers[i] = make_tuple(cx, cy, i);\n    }\n    \n    // Sort by center coordinates\n    sort(city_centers.begin(), city_centers.end());\n    \n    vector<vector<int>> groups;\n    int idx = 0;\n    for (int group_size : G) {\n        vector<int> group;\n        for (int i = 0; i < group_size; i++) {\n            group.push_back(get<2>(city_centers[idx++]));\n        }\n        groups.push_back(group);\n    }\n    \n    return groups;\n}\n\n// Simple approach: just connect consecutive cities in each group\nvector<pair<int, int>> build_simple_mst(const vector<int>& group) {\n    vector<pair<int, int>> edges;\n    for (int i = 0; i < (int)group.size() - 1; i++) {\n        int u = group[i];\n        int v = group[i + 1];\n        if (u > v) swap(u, v);\n        edges.push_back({u, v});\n    }\n    return edges;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    // Read input\n    cin >> n >> m >> q >> L >> W;\n    G.resize(m);\n    for (int i = 0; i < m; i++) cin >> G[i];\n    \n    lx.resize(n); rx.resize(n); ly.resize(n); ry.resize(n);\n    for (int i = 0; i < n; i++) {\n        cin >> lx[i] >> rx[i] >> ly[i] >> ry[i];\n    }\n    \n    // Create groups\n    vector<vector<int>> groups = create_groups();\n    \n    // Use some queries to get information (but don't overcomplicate)\n    int queries_used = 0;\n    vector<set<pair<int, int>>> group_edges(m);\n    \n    // For each group, if it's small enough and we have queries, get the real MST\n    for (int g = 0; g < m && queries_used < q; g++) {\n        if ((int)groups[g].size() <= L && (int)groups[g].size() >= 2) {\n            auto edges = query(groups[g]);\n            for (auto edge : edges) {\n                group_edges[g].insert(edge);\n            }\n            queries_used++;\n        }\n    }\n    \n    // Output answer\n    cout << \"!\" << endl;\n    for (int g = 0; g < m; g++) {\n        // Output group cities\n        for (int i = 0; i < (int)groups[g].size(); i++) {\n            if (i > 0) cout << \" \";\n            cout << groups[g][i];\n        }\n        cout << endl;\n        \n        // Output edges - prefer queried edges, fallback to simple chain\n        vector<pair<int, int>> edges_to_output;\n        if (!group_edges[g].empty()) {\n            edges_to_output = vector<pair<int, int>>(group_edges[g].begin(), group_edges[g].end());\n        } else {\n            edges_to_output = build_simple_mst(groups[g]);\n        }\n        \n        for (auto edge : edges_to_output) {\n            cout << edge.first << \" \" << edge.second << endl;\n        }\n    }\n    \n    return 0;\n}","ahc046":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 20;\nconst int M = 40;\n\nstruct Point {\n    int r, c;\n    Point() : r(0), c(0) {}\n    Point(int _r, int _c) : r(_r), c(_c) {}\n    bool operator==(const Point& other) const {\n        return r == other.r && c == other.c;\n    }\n};\n\n// Directions\nconst int DR[] = {-1, 1, 0, 0};  // Up, Down, Left, Right\nconst int DC[] = {0, 0, -1, 1};\nconst char DIR_CHARS[] = {'U', 'D', 'L', 'R'};\n\nPoint targets[M];\nbool blocked[N][N];\n\n// Check if position is valid\nbool isValid(int r, int c) {\n    return r >= 0 && r < N && c >= 0 && c < N;\n}\n\n// Simple Manhattan pathfinding\nvector<pair<char, char>> findPath(Point from, Point to) {\n    vector<pair<char, char>> path;\n    int r = from.r, c = from.c;\n    \n    // Move vertically first\n    while (r < to.r) {\n        if (isValid(r + 1, c) && !blocked[r + 1][c]) {\n            path.push_back({'M', 'D'});\n            r++;\n        } else {\n            break;\n        }\n    }\n    while (r > to.r) {\n        if (isValid(r - 1, c) && !blocked[r - 1][c]) {\n            path.push_back({'M', 'U'});\n            r--;\n        } else {\n            break;\n        }\n    }\n    \n    // Move horizontally\n    while (c < to.c) {\n        if (isValid(r, c + 1) && !blocked[r][c + 1]) {\n            path.push_back({'M', 'R'});\n            c++;\n        } else {\n            break;\n        }\n    }\n    while (c > to.c) {\n        if (isValid(r, c - 1) && !blocked[r][c - 1]) {\n            path.push_back({'M', 'L'});\n            c--;\n        } else {\n            break;\n        }\n    }\n    \n    return path;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int n, m;\n    cin >> n >> m;\n    \n    Point start;\n    cin >> start.r >> start.c;\n    \n    for (int i = 0; i < m; i++) {\n        cin >> targets[i].r >> targets[i].c;\n    }\n    \n    vector<pair<char, char>> actions;\n    Point current = start;\n    \n    // Visit each target in order\n    for (int i = 1; i < m; i++) {\n        Point target = targets[i];\n        auto path = findPath(current, target);\n        actions.insert(actions.end(), path.begin(), path.end());\n        current = target;\n    }\n    \n    // Output results\n    for (auto& action : actions) {\n        cout << action.first << \" \" << action.second << \"\\n\";\n    }\n    \n    return 0;\n}"},"8":{"ahc001":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Rectangle {\n    int a, b, c, d; // (a,b) to (c,d)\n    Rectangle() : a(0), b(0), c(0), d(0) {}\n    Rectangle(int _a, int _b, int _c, int _d) : a(_a), b(_b), c(_c), d(_d) {}\n    bool isValid() const { \n        return a >= 0 && b >= 0 && c <= 10000 && d <= 10000 && a < c && b < d; \n    }\n    long long area() const { \n        return 1LL * (c - a) * (d - b); \n    }\n};\n\nint n;\nvector<tuple<int, int, int>> companies; // {x, y, r}\nvector<Rectangle> rectangles;\n\n// Check if two rectangles overlap\nbool overlap(const Rectangle& r1, const Rectangle& r2) {\n    return r1.a < r2.c && r2.a < r1.c && r1.b < r2.d && r2.b < r1.d;\n}\n\n// Check if rectangle overlaps with any existing rectangles\nbool hasOverlap(int current_idx, const Rectangle& rect) {\n    if (!rect.isValid()) return true;\n    for (int i = 0; i < current_idx; i++) {\n        if (rectangles[i].isValid() && overlap(rect, rectangles[i])) {\n            return true;\n        }\n    }\n    return false;\n}\n\n// Try to place a rectangle for company\nbool placeRectangle(int idx) {\n    int x = get<0>(companies[idx]);\n    int y = get<1>(companies[idx]);\n    int r = get<2>(companies[idx]);\n    \n    // Try to place rectangle with exact area first\n    for (int w = 1; w <= min(1000, r); w++) {\n        if (r % w == 0) {\n            int h = r / w;\n            if (h <= 1000) {\n                // Try to center rectangle around the point\n                for (int offset_x = 0; offset_x < min(w, 50); offset_x++) {\n                    for (int offset_y = 0; offset_y < min(h, 50); offset_y++) {\n                        int a = max(0, min(x - offset_x, 10000 - w));\n                        int b = max(0, min(y - offset_y, 10000 - h));\n                        int c = a + w;\n                        int d = b + h;\n                        \n                        if (a >= 0 && b >= 0 && c <= 10000 && d <= 10000) {\n                            Rectangle rect(a, b, c, d);\n                            // Check if point is properly inside\n                            if (a <= x && x < c && b <= y && y < d) {\n                                if (!hasOverlap(idx, rect)) {\n                                    rectangles[idx] = rect;\n                                    return true;\n                                }\n                            }\n                        }\n                    }\n                }\n            }\n        }\n    }\n    \n    // Try smaller rectangles\n    for (int target_area = max(1, r/10); target_area <= min(r, 10000); target_area += max(1, r/100)) {\n        for (int w = 1; w <= min(100, target_area); w++) {\n            if (target_area % w == 0) {\n                int h = target_area / w;\n                if (h <= 100) {\n                    // Try different positions\n                    for (int dx = 0; dx <= min(w-1, 30); dx++) {\n                        for (int dy = 0; dy <= min(h-1, 30); dy++) {\n                            int a = max(0, min(x - dx, 10000 - w));\n                            int b = max(0, min(y - dy, 10000 - h));\n                            int c = a + w;\n                            int d = b + h;\n                            \n                            if (a >= 0 && b >= 0 && c <= 10000 && d <= 10000) {\n                                Rectangle rect(a, b, c, d);\n                                if (a <= x && x < c && b <= y && y < d) {\n                                    if (!hasOverlap(idx, rect)) {\n                                        rectangles[idx] = rect;\n                                        return true;\n                                    }\n                                }\n                            }\n                        }\n                    }\n                }\n            }\n        }\n    }\n    \n    // Last resort: place 1x1 rectangle at or near the point\n    for (int dx = -5; dx <= 5; dx++) {\n        for (int dy = -5; dy <= 5; dy++) {\n            int a = max(0, min(x + dx, 9999));\n            int b = max(0, min(y + dy, 9999));\n            Rectangle rect(a, b, min(10000, a + 1), min(10000, b + 1));\n            if (rect.isValid()) {\n                if (!hasOverlap(idx, rect)) {\n                    rectangles[idx] = rect;\n                    return true;\n                }\n            }\n        }\n    }\n    \n    // Absolute fallback\n    rectangles[idx] = Rectangle(0, 0, 1, 1);\n    return true;\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    cin >> n;\n    companies.resize(n);\n    rectangles.resize(n);\n    \n    for (int i = 0; i < n; i++) {\n        int x, y, r;\n        cin >> x >> y >> r;\n        companies[i] = make_tuple(x, y, r);\n    }\n    \n    // Place rectangles in input order\n    for (int i = 0; i < n; i++) {\n        placeRectangle(i);\n    }\n    \n    // Output results\n    for (int i = 0; i < n; i++) {\n        Rectangle& rect = rectangles[i];\n        \n        // Ensure rectangle validity\n        rect.a = max(0, min(rect.a, 9999));\n        rect.b = max(0, min(rect.b, 9999));\n        rect.c = max(rect.a + 1, min(rect.c, 10000));\n        rect.d = max(rect.b + 1, min(rect.d, 10000));\n        \n        // Final safety check\n        if (rect.a >= rect.c) rect.c = rect.a + 1;\n        if (rect.b >= rect.d) rect.d = rect.b + 1;\n        \n        cout << rect.a << \" \" << rect.b << \" \" << rect.c << \" \" << rect.d << \"\\n\";\n    }\n    \n    return 0;\n}","ahc002":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int SIZE = 50;\nconst int MAX_TILES = 2500;\nint si, sj;\nint tile[SIZE][SIZE];\nint value[SIZE][SIZE];\nvector<pair<int, int>> tile_positions[MAX_TILES];\nint tile_values[MAX_TILES];\n\n// Directions: U, D, L, R\nconst int dx[4] = {-1, 1, 0, 0};\nconst int dy[4] = {0, 0, -1, 1};\nconst char dir_chars[4] = {'U', 'D', 'L', 'R'};\n\n// Check if position is valid\nbool is_valid(int x, int y) {\n    return x >= 0 && x < SIZE && y >= 0 && y < SIZE;\n}\n\n// Initialize tile information\nvoid init_tiles() {\n    for (int i = 0; i < MAX_TILES; i++) {\n        tile_positions[i].clear();\n    }\n    \n    for (int i = 0; i < SIZE; i++) {\n        for (int j = 0; j < SIZE; j++) {\n            tile_positions[tile[i][j]].push_back({i, j});\n        }\n    }\n    \n    for (int i = 0; i < MAX_TILES; i++) {\n        if (!tile_positions[i].empty()) {\n            int total = 0;\n            for (auto& pos : tile_positions[i]) {\n                total += value[pos.first][pos.second];\n            }\n            tile_values[i] = total;\n        }\n    }\n}\n\n// Simulate path to get actual score\nint get_actual_score(const string& path) {\n    vector<bool> visited(MAX_TILES, false);\n    int x = si, y = sj;\n    int score = tile_values[tile[x][y]];\n    visited[tile[x][y]] = true;\n    \n    for (char c : path) {\n        int d = -1;\n        for (int i = 0; i < 4; i++) {\n            if (dir_chars[i] == c) {\n                d = i;\n                break;\n            }\n        }\n        \n        if (d == -1) break;\n        \n        int nx = x + dx[d];\n        int ny = y + dy[d];\n        \n        if (!is_valid(nx, ny)) break;\n        \n        int tile_id = tile[nx][ny];\n        if (visited[tile_id]) break;\n        \n        x = nx;\n        y = ny;\n        score += tile_values[tile_id];\n        visited[tile_id] = true;\n    }\n    \n    return score;\n}\n\n// Ultra beam search with maximum time utilization\nstring ultra_beam_search(int beam_width, int max_steps) {\n    struct State {\n        int score;\n        int x, y;\n        string path;\n        vector<bool> visited;\n        int steps;\n        \n        bool operator<(const State& other) const {\n            if (score != other.score) return score < other.score;\n            return steps > other.steps;\n        }\n    };\n    \n    priority_queue<State> beam;\n    \n    // Initialize\n    vector<bool> visited(MAX_TILES, false);\n    int start_tile_id = tile[si][sj];\n    visited[start_tile_id] = true;\n    int start_score = tile_values[start_tile_id];\n    \n    beam.push({start_score, si, sj, \"\", visited, 0});\n    \n    string best_path = \"\";\n    int best_score = start_score;\n    \n    clock_t start_time = clock();\n    const double TIME_LIMIT = 1.98; // Use almost all time\n    \n    for (int step = 0; step < max_steps && !beam.empty(); step++) {\n        priority_queue<State> next_beam;\n        \n        // Process current beam with adaptive processing\n        int processed = 0;\n        int max_process = beam_width;\n        \n        // Increase processing power in middle game\n        if (step > 30 && step < 100) {\n            max_process = min(beam_width * 3/2, 150);\n        }\n        \n        while (!beam.empty() && processed < max_process) {\n            State current = beam.top();\n            beam.pop();\n            processed++;\n            \n            // Update best if needed\n            if (current.score > best_score) {\n                best_score = current.score;\n                best_path = current.path;\n            }\n            \n            // Check time limit\n            double elapsed = (double)(clock() - start_time) / CLOCKS_PER_SEC;\n            if (elapsed > TIME_LIMIT) {\n                return best_path;\n            }\n            \n            // Generate next moves with enhanced evaluation\n            vector<tuple<int, int, int, int>> moves; // {enhanced_score, immediate, potential, direction}\n            \n            for (int d = 0; d < 4; d++) {\n                int nx = current.x + dx[d];\n                int ny = current.y + dy[d];\n                \n                if (is_valid(nx, ny)) {\n                    int next_tile_id = tile[nx][ny];\n                    if (!current.visited[next_tile_id]) {\n                        int immediate_value = tile_values[next_tile_id];\n                        \n                        // Enhanced future potential with 3-step lookahead\n                        int future_potential = 0;\n                        vector<bool> temp_visited = current.visited;\n                        temp_visited[next_tile_id] = true;\n                        \n                        // Level 1 lookahead\n                        vector<pair<int, int>> level1_moves; // {value, direction}\n                        for (int d1 = 0; d1 < 4; d1++) {\n                            int nnx = nx + dx[d1];\n                            int nny = ny + dy[d1];\n                            \n                            if (is_valid(nnx, nny)) {\n                                int next_next_tile_id = tile[nnx][nny];\n                                if (!temp_visited[next_next_tile_id]) {\n                                    level1_moves.push_back({tile_values[next_next_tile_id], d1});\n                                }\n                            }\n                        }\n                        \n                        sort(level1_moves.rbegin(), level1_moves.rend());\n                        \n                        if (!level1_moves.empty()) {\n                            future_potential += level1_moves[0].first;\n                            \n                            // Level 2 lookahead from best level 1 move\n                            if (!level1_moves.empty()) {\n                                int best_d1 = level1_moves[0].second;\n                                int nnx = nx + dx[best_d1];\n                                int nny = ny + dy[best_d1];\n                                \n                                vector<bool> temp_visited2 = temp_visited;\n                                temp_visited2[tile[nnx][nny]] = true;\n                                \n                                int level2_potential = 0;\n                                for (int d2 = 0; d2 < 4; d2++) {\n                                    int nnnx = nnx + dx[d2];\n                                    int nnny = nny + dy[d2];\n                                    \n                                    if (is_valid(nnnx, nnny)) {\n                                        int next_next_next_tile_id = tile[nnnx][nnny];\n                                        if (!temp_visited2[next_next_next_tile_id]) {\n                                            level2_potential = max(level2_potential, tile_values[next_next_next_tile_id]);\n                                        }\n                                    }\n                                }\n                                \n                                future_potential += level2_potential / 2;\n                            }\n                        }\n                        \n                        int enhanced_score = immediate_value + future_potential / 3;\n                        moves.push_back({enhanced_score, immediate_value, future_potential, d});\n                    }\n                }\n            }\n            \n            // Sort by enhanced score\n            sort(moves.begin(), moves.end(), [](const auto& a, const auto& b) {\n                if (get<0>(a) != get<0>(b)) return get<0>(a) > get<0>(b);\n                return get<1>(a) > get<1>(b);\n            });\n            \n            // Adaptive branching based on game stage\n            int branches = 3;\n            if (current.steps < 20) branches = 5; // Early exploration\n            else if (current.steps > 150) branches = 2; // Late focus\n            \n            branches = min(branches, (int)moves.size());\n            \n            for (int i = 0; i < branches; i++) {\n                int d = get<3>(moves[i]);\n                int nx = current.x + dx[d];\n                int ny = current.y + dy[d];\n                \n                int next_tile_id = tile[nx][ny];\n                vector<bool> new_visited = current.visited;\n                new_visited[next_tile_id] = true;\n                \n                int new_score = current.score + tile_values[next_tile_id];\n                string new_path = current.path + dir_chars[d];\n                \n                next_beam.push({new_score, nx, ny, new_path, new_visited, current.steps + 1});\n            }\n        }\n        \n        // Keep top states with diversity\n        vector<State> candidates;\n        while (!next_beam.empty() && candidates.size() < beam_width * 2) {\n            candidates.push_back(next_beam.top());\n            next_beam.pop();\n        }\n        \n        // Sort and keep best\n        sort(candidates.begin(), candidates.end());\n        reverse(candidates.begin(), candidates.end());\n        \n        int added = 0;\n        for (const auto& candidate : candidates) {\n            if (added >= beam_width) break;\n            beam.push(candidate);\n            added++;\n        }\n    }\n    \n    return best_path;\n}\n\n// High-performance greedy with advanced lookahead\nstring advanced_greedy() {\n    vector<bool> visited(MAX_TILES, false);\n    int x = si, y = sj;\n    visited[tile[x][y]] = true;\n    string path = \"\";\n    \n    for (int step = 0; step < 300; step++) {\n        vector<tuple<int, int, int, int>> candidates; // {total_score, immediate, potential, direction}\n        \n        for (int d = 0; d < 4; d++) {\n            int nx = x + dx[d];\n            int ny = y + dy[d];\n            \n            if (is_valid(nx, ny)) {\n                int tile_id = tile[nx][ny];\n                if (!visited[tile_id]) {\n                    int immediate_score = tile_values[tile_id];\n                    \n                    // Advanced potential calculation\n                    int potential = 0;\n                    vector<bool> temp_visited = visited;\n                    temp_visited[tile_id] = true;\n                    \n                    // Multi-level potential with decay\n                    int level = 1;\n                    int current_x = nx, current_y = ny;\n                    \n                    while (level <= 4) {\n                        vector<pair<int, int>> next_moves; // {value, direction}\n                        \n                        for (int d2 = 0; d2 < 4; d2++) {\n                            int next_x = current_x + dx[d2];\n                            int next_y = current_y + dy[d2];\n                            \n                            if (is_valid(next_x, next_y)) {\n                                int next_tile_id = tile[next_x][next_y];\n                                if (!temp_visited[next_tile_id]) {\n                                    next_moves.push_back({tile_values[next_tile_id], d2});\n                                }\n                            }\n                        }\n                        \n                        if (next_moves.empty()) break;\n                        \n                        sort(next_moves.rbegin(), next_moves.rend());\n                        int best_value = next_moves[0].first;\n                        \n                        // Add weighted potential\n                        potential += best_value / (1 << (level - 1)); // Exponential decay\n                        \n                        // Move to next position for next level\n                        int best_dir = next_moves[0].second;\n                        current_x += dx[best_dir];\n                        current_y += dy[best_dir];\n                        temp_visited[tile[current_x][current_y]] = true;\n                        level++;\n                    }\n                    \n                    int total_score = immediate_score + potential / 2;\n                    candidates.push_back({total_score, immediate_score, potential, d});\n                }\n            }\n        }\n        \n        if (candidates.empty()) break;\n        \n        // Sort by total score\n        sort(candidates.begin(), candidates.end(), [](const auto& a, const auto& b) {\n            if (get<0>(a) != get<0>(b)) return get<0>(a) > get<0>(b);\n            return get<1>(a) > get<1>(b);\n        });\n        \n        // Adaptive selection\n        int chosen_idx = 0;\n        if (candidates.size() > 1) {\n            // Variable exploration based on stage\n            int exploration_rate = 25;\n            if (step > 50) exploration_rate = 15;\n            if (step > 150) exploration_rate = 5;\n            \n            if (rand() % 100 < exploration_rate && candidates.size() > 1) {\n                chosen_idx = min(1, (int)candidates.size() - 1);\n            }\n        }\n        \n        auto chosen = candidates[chosen_idx];\n        int chosen_dir = get<3>(chosen);\n        \n        path += dir_chars[chosen_dir];\n        x = x + dx[chosen_dir];\n        y = y + dy[chosen_dir];\n        visited[tile[x][y]] = true;\n    }\n    \n    return path;\n}\n\n// Rescue strategy for problematic cases\nstring rescue_strategy() {\n    vector<bool> visited(MAX_TILES, false);\n    int x = si, y = sj;\n    visited[tile[x][y]] = true;\n    string path = \"\";\n    \n    // Try multiple patterns\n    int pattern = (si * 7 + sj * 11) % 6; // Different patterns based on start position\n    \n    for (int step = 0; step < 350; step++) {\n        vector<pair<int, int>> valid_moves; // {value, direction}\n        \n        // Pattern-based direction preference\n        vector<int> direction_preference;\n        switch (pattern) {\n            case 0: direction_preference = {3, 1, 2, 0}; break; // R, D, L, U\n            case 1: direction_preference = {1, 3, 0, 2}; break; // D, R, U, L\n            case 2: direction_preference = {0, 2, 1, 3}; break; // U, L, D, R\n            case 3: direction_preference = {2, 0, 3, 1}; break; // L, U, R, D\n            case 4: direction_preference = {1, 0, 3, 2}; break; // D, U, R, L\n            case 5: direction_preference = {3, 2, 1, 0}; break; // R, L, D, U\n            default: direction_preference = {0, 1, 2, 3}; break;\n        }\n        \n        for (int dir_idx : direction_preference) {\n            int nx = x + dx[dir_idx];\n            int ny = y + dy[dir_idx];\n            \n            if (is_valid(nx, ny)) {\n                int tile_id = tile[nx][ny];\n                if (!visited[tile_id]) {\n                    valid_moves.push_back({tile_values[tile_id], dir_idx});\n                }\n            }\n        }\n        \n        if (valid_moves.empty()) break;\n        \n        // Sort by value but with threshold selection\n        sort(valid_moves.rbegin(), valid_moves.rend());\n        \n        int best_value = valid_moves[0].first;\n        vector<int> acceptable_moves;\n        \n        // Accept moves within 80% of best\n        for (int i = 0; i < valid_moves.size(); i++) {\n            if (valid_moves[i].first >= best_value * 0.8) {\n                acceptable_moves.push_back(i);\n            }\n        }\n        \n        // Choose among acceptable moves\n        int chosen_idx = acceptable_moves[rand() % acceptable_moves.size()];\n        int chosen_dir = valid_moves[chosen_idx].second;\n        \n        path += dir_chars[chosen_dir];\n        x = x + dx[chosen_dir];\n        y = y + dy[chosen_dir];\n        visited[tile[x][y]] = true;\n    }\n    \n    return path;\n}\n\n// Main strategy selector\nstring find_best_path() {\n    vector<pair<string, int>> results;\n    \n    // Strategy 1: Ultra wide beam search\n    string result1 = ultra_beam_search(120, 200);\n    results.push_back({result1, get_actual_score(result1)});\n    \n    // Strategy 2: Deep narrow beam search\n    string result2 = ultra_beam_search(35, 300);\n    results.push_back({result2, get_actual_score(result2)});\n    \n    // Strategy 3: Medium beam search\n    string result3 = ultra_beam_search(60, 250);\n    results.push_back({result3, get_actual_score(result3)});\n    \n    // Strategy 4: Advanced greedy\n    string result4 = advanced_greedy();\n    results.push_back({result4, get_actual_score(result4)});\n    \n    // Strategy 5: Rescue strategy\n    string result5 = rescue_strategy();\n    results.push_back({result5, get_actual_score(result5)});\n    \n    // Sort by actual score (descending)\n    sort(results.begin(), results.end(), [](const auto& a, const auto& b) {\n        return a.second > b.second;\n    });\n    \n    return results[0].first;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    // Set random seed for consistency\n    srand(12345);\n    \n    // Read input\n    cin >> si >> sj;\n    \n    for (int i = 0; i < SIZE; i++) {\n        for (int j = 0; j < SIZE; j++) {\n            cin >> tile[i][j];\n        }\n    }\n    \n    for (int i = 0; i < SIZE; i++) {\n        for (int j = 0; j < SIZE; j++) {\n            cin >> value[i][j];\n        }\n    }\n    \n    // Initialize tile information\n    init_tiles();\n    \n    // Find and output path\n    string result = find_best_path();\n    cout << result << endl;\n    \n    return 0;\n}","ahc003":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int GRID_SIZE = 30;\nconst int MAX_QUERIES = 1000;\n\n// Direction vectors: U, D, L, R\nconst int di[4] = {-1, 1, 0, 0};\nconst int dj[4] = {0, 0, -1, 1};\nconst char dir_chars[4] = {'U', 'D', 'L', 'R'};\n\n// Estimated edge weights\ndouble h[GRID_SIZE][GRID_SIZE-1]; // horizontal edges (i, j) -> (i, j+1)\ndouble v[GRID_SIZE-1][GRID_SIZE]; // vertical edges (i, j) -> (i+1, j)\n\n// Count of observations for each edge\nint h_count[GRID_SIZE][GRID_SIZE-1];\nint v_count[GRID_SIZE-1][GRID_SIZE];\n\nvoid init_weights() {\n    // Slightly optimized initialization based on empirical testing\n    for (int i = 0; i < GRID_SIZE; i++) {\n        for (int j = 0; j < GRID_SIZE-1; j++) {\n            h[i][j] = 3800.0;  // Slightly lower to encourage early exploration\n            h_count[i][j] = 0;\n        }\n    }\n    for (int i = 0; i < GRID_SIZE-1; i++) {\n        for (int j = 0; j < GRID_SIZE; j++) {\n            v[i][j] = 3800.0;\n            v_count[i][j] = 0;\n        }\n    }\n}\n\nstruct State {\n    int i, j;\n    double cost;\n    bool operator>(const State& other) const {\n        return cost > other.cost;\n    }\n};\n\nstring find_path(int si, int sj, int ti, int tj) {\n    // Dijkstra's algorithm\n    vector<vector<double>> dist(GRID_SIZE, vector<double>(GRID_SIZE, 1e18));\n    vector<vector<pair<int, int>>> prev(GRID_SIZE, vector<pair<int, int>>(GRID_SIZE, make_pair(-1, -1)));\n    vector<vector<char>> prev_dir(GRID_SIZE, vector<char>(GRID_SIZE, ' '));\n    \n    priority_queue<State, vector<State>, greater<State>> pq;\n    dist[si][sj] = 0;\n    pq.push({si, sj, 0});\n    \n    while (!pq.empty()) {\n        auto [i, j, cost] = pq.top(); pq.pop();\n        if (cost > dist[i][j]) continue;\n        \n        if (i == ti && j == tj) break;\n        \n        // Try all 4 directions\n        for (int d = 0; d < 4; d++) {\n            int ni = i + di[d];\n            int nj = j + dj[d];\n            \n            if (ni >= 0 && ni < GRID_SIZE && nj >= 0 && nj < GRID_SIZE) {\n                double edge_cost = 0;\n                bool valid_move = true;\n                \n                if (d == 0) { // Up: (i,j) -> (i-1,j)\n                    if (ni < i) edge_cost = v[ni][nj];\n                    else valid_move = false;\n                } else if (d == 1) { // Down: (i,j) -> (i+1,j)\n                    if (i < ni) edge_cost = v[i][j];\n                    else valid_move = false;\n                } else if (d == 2) { // Left: (i,j) -> (i,j-1)\n                    if (nj < j) edge_cost = h[ni][nj];\n                    else valid_move = false;\n                } else if (d == 3) { // Right: (i,j) -> (i,j+1)\n                    if (j < nj) edge_cost = h[i][j];\n                    else valid_move = false;\n                }\n                \n                if (!valid_move) continue;\n                \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                    prev_dir[ni][nj] = dir_chars[d];\n                    pq.push({ni, nj, new_cost});\n                }\n            }\n        }\n    }\n    \n    // Reconstruct path\n    string path;\n    int ci = ti, cj = tj;\n    while (ci != si || cj != sj) {\n        path += prev_dir[ci][cj];\n        auto p = prev[ci][cj];\n        ci = p.first;\n        cj = p.second;\n    }\n    reverse(path.begin(), path.end());\n    \n    return path;\n}\n\n// Store the last path for updating weights\nvector<pair<int, int>> last_path_coords;\n\nvoid store_path_info(int si, int sj, const string& path) {\n    last_path_coords.clear();\n    last_path_coords.push_back({si, sj});\n    \n    int ci = si, cj = sj;\n    for (char c : path) {\n        if (c == 'U') ci--;\n        else if (c == 'D') ci++;\n        else if (c == 'L') cj--;\n        else if (c == 'R') cj++;\n        last_path_coords.push_back({ci, cj});\n    }\n}\n\nvoid update_weights(int actual_cost, int query_num) {\n    // Get edges used in the path\n    vector<tuple<int, int, bool, double>> edges_info;\n    double total_estimated = 0;\n    \n    for (int k = 0; k < (int)last_path_coords.size() - 1; k++) {\n        int i1 = last_path_coords[k].first;\n        int j1 = last_path_coords[k].second;\n        int i2 = last_path_coords[k+1].first;\n        int j2 = last_path_coords[k+1].second;\n        \n        double cost = 0;\n        int i, j;\n        bool is_vertical;\n        \n        if (i1 == i2) {\n            // Horizontal move\n            j = min(j1, j2);\n            i = i1;\n            is_vertical = false;\n            cost = h[i][j];\n        } else {\n            // Vertical move\n            i = min(i1, i2);\n            j = j1;\n            is_vertical = true;\n            cost = v[i][j];\n        }\n        \n        edges_info.push_back({i, j, is_vertical, cost});\n        total_estimated += cost;\n    }\n    \n    if (edges_info.empty() || total_estimated <= 0) return;\n    \n    // Calculate error\n    double error = (double)actual_cost - total_estimated;\n    \n    // Improved update strategy with better learning rate scheduling\n    for (auto& [i, j, is_vertical, edge_cost] : edges_info) {\n        double proportion = edge_cost / total_estimated;\n        double adjustment = error * proportion;\n        \n        // Enhanced adaptive learning rate\n        int count = is_vertical ? v_count[i][j] : h_count[i][j];\n        \n        // Optimized learning rate schedule\n        double learning_rate;\n        if (count == 0) {\n            learning_rate = 0.25;  // Higher initial learning rate\n        } else if (count < 3) {\n            learning_rate = 0.18;  // Still aggressive for new edges\n        } else if (count < 10) {\n            learning_rate = 0.12;  // Moderate learning\n        } else {\n            learning_rate = 0.08 / (1.0 + (double)count / 25.0);  // Slow decay\n        }\n        \n        // Time-based optimization based on scoring function (0.998^(1000-k))\n        if (query_num < 200) {\n            learning_rate *= 1.3;  // Extra boost for very early queries\n        } else if (query_num < 500) {\n            learning_rate *= 1.1;  // Moderate boost for early queries\n        } else if (query_num > 800) {\n            learning_rate *= 0.7;  // Reduced learning for late queries\n        }\n        \n        learning_rate = max(0.003, learning_rate);  // Very small minimum\n        \n        double current_weight = is_vertical ? v[i][j] : h[i][j];\n        double new_weight = current_weight + adjustment * learning_rate;\n        \n        // Refined bounds based on problem constraints\n        new_weight = max(700.0, min(11000.0, new_weight));\n        \n        // Improved exponential moving average\n        double alpha = 2.0 / (2.0 + count);  // Better weighting formula\n        if (is_vertical) {\n            v[i][j] = v[i][j] * (1 - alpha) + new_weight * alpha;\n            v_count[i][j]++;\n        } else {\n            h[i][j] = h[i][j] * (1 - alpha) + new_weight * alpha;\n            h_count[i][j]++;\n        }\n    }\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    init_weights();\n    \n    for (int query = 0; query < MAX_QUERIES; query++) {\n        int si, sj, ti, tj;\n        cin >> si >> sj >> ti >> tj;\n        \n        string path = find_path(si, sj, ti, tj);\n        store_path_info(si, sj, path);\n        \n        cout << path << endl;\n        cout.flush();\n        \n        int result;\n        cin >> result;\n        \n        update_weights(result, query);\n    }\n    \n    return 0;\n}","ahc004":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 20;\nconst string CHARS = \"ABCDEFGH\";\n\n// Matrix representation\nvector<vector<char>> matrix(N, vector<char>(N, '.'));\nvector<string> strings;\n\n// Count how many strings are satisfied\nint countSatisfied(const vector<vector<char>>& mat, const vector<string>& strs) {\n    int count = 0;\n    for (const string& s : strs) {\n        bool found = false;\n        // Check horizontal\n        for (int i = 0; i < N && !found; i++) {\n            for (int j = 0; j < N && !found; j++) {\n                bool match = true;\n                for (int k = 0; k < (int)s.length(); k++) {\n                    int col = (j + k) % N;\n                    if (mat[i][col] != '.' && mat[i][col] != s[k]) {\n                        match = false;\n                        break;\n                    }\n                }\n                if (match) {\n                    found = true;\n                    break;\n                }\n            }\n            if (found) break;\n        }\n        // Check vertical if not found horizontally\n        if (!found) {\n            for (int j = 0; j < N && !found; j++) {\n                for (int i = 0; i < N && !found; i++) {\n                    bool match = true;\n                    for (int k = 0; k < (int)s.length(); k++) {\n                        int row = (i + k) % N;\n                        if (mat[row][j] != '.' && mat[row][j] != s[k]) {\n                            match = false;\n                            break;\n                        }\n                    }\n                    if (match) {\n                        found = true;\n                        break;\n                    }\n                }\n                if (found) break;\n            }\n        }\n        if (found) count++;\n    }\n    return count;\n}\n\n// Check if a string can be placed at a specific position and direction\nbool canPlace(const vector<vector<char>>& mat, const string& s, int row, int col, bool vertical) {\n    if (vertical) {\n        for (int i = 0; i < (int)s.length(); i++) {\n            int r = (row + i) % N;\n            if (mat[r][col] != '.' && mat[r][col] != s[i]) {\n                return false;\n            }\n        }\n    } else {\n        for (int j = 0; j < (int)s.length(); j++) {\n            int c = (col + j) % N;\n            if (mat[row][c] != '.' && mat[row][c] != s[j]) {\n                return false;\n            }\n        }\n    }\n    return true;\n}\n\n// Place a string (modifies matrix)\nvoid placeString(vector<vector<char>>& mat, const string& s, int row, int col, bool vertical) {\n    if (vertical) {\n        for (int i = 0; i < (int)s.length(); i++) {\n            int r = (row + i) % N;\n            mat[r][col] = s[i];\n        }\n    } else {\n        for (int j = 0; j < (int)s.length(); j++) {\n            int c = (col + j) % N;\n            mat[row][c] = s[j];\n        }\n    }\n}\n\n// Try to place as many strings as possible\nvoid placeStrings() {\n    // Try to place each string optimally\n    for (int idx = 0; idx < (int)strings.size(); idx++) {\n        const string& s = strings[idx];\n        bool placed = false;\n        \n        // Try all positions and directions\n        for (int dir = 0; dir < 2 && !placed; dir++) {\n            bool vertical = (dir == 1);\n            for (int i = 0; i < N && !placed; i++) {\n                for (int j = 0; j < N && !placed; j++) {\n                    if (canPlace(matrix, s, i, j, vertical)) {\n                        placeString(matrix, s, i, j, vertical);\n                        placed = true;\n                    }\n                }\n            }\n        }\n    }\n}\n\n// Fill empty cells with valid characters\nvoid fillEmptyCells() {\n    random_device rd;\n    mt19937 gen(rd());\n    uniform_int_distribution<> char_dis(0, 7);\n    \n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (matrix[i][j] == '.') {\n                matrix[i][j] = CHARS[char_dis(gen)];\n            }\n        }\n    }\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    int M;\n    cin >> M;  // N is fixed to 20 according to problem statement\n    \n    strings.resize(M);\n    for (int i = 0; i < M; i++) {\n        cin >> strings[i];\n    }\n    \n    // Initialize matrix\n    matrix.assign(N, vector<char>(N, '.'));\n    \n    // Place strings\n    placeStrings();\n    \n    // Fill remaining empty cells\n    fillEmptyCells();\n    \n    // Output result - ensure only valid characters\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            // Validate character before output\n            if (matrix[i][j] == '.' || (matrix[i][j] >= 'A' && matrix[i][j] <= 'H')) {\n                cout << matrix[i][j];\n            } else {\n                // Fallback to a valid character if somehow invalid\n                cout << 'A';\n            }\n        }\n        cout << \"\\n\";\n    }\n    \n    return 0;\n}","ahc005":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MAX_N = 70;\nint N, si, sj;\nchar grid[MAX_N][MAX_N];\nbool is_road[MAX_N][MAX_N];\nvector<pair<int,int>> roads;\n\n// Directions: U, D, L, R\nconst int dx[4] = {-1, 1, 0, 0};\nconst int dy[4] = {0, 0, -1, 1};\n\n// Calculate which roads are visible from position (x, y)\nset<pair<int,int>> get_visible_roads(int x, int y) {\n    set<pair<int,int>> visible;\n    \n    if (!is_road[x][y]) return visible;\n    \n    // Horizontal visibility (same row)\n    // Look left\n    for (int j = y; j >= 0; j--) {\n        if (!is_road[x][j]) break;\n        visible.insert({x, j});\n    }\n    // Look right\n    for (int j = y; j < N; j++) {\n        if (!is_road[x][j]) break;\n        visible.insert({x, j});\n    }\n    \n    // Vertical visibility (same column)\n    // Look up\n    for (int i = x; i >= 0; i--) {\n        if (!is_road[i][y]) break;\n        visible.insert({i, y});\n    }\n    // Look down\n    for (int i = x; i < N; i++) {\n        if (!is_road[i][y]) break;\n        visible.insert({i, y});\n    }\n    \n    return visible;\n}\n\nbool is_valid(int x, int y) {\n    return x >= 0 && x < N && y >= 0 && y < N && is_road[x][y];\n}\n\n// BFS to find shortest path between two points\nvector<pair<int,int>> bfs_path(int sx, int sy, int tx, int ty) {\n    if (sx == tx && sy == ty) return {{sx, sy}};\n    if (!is_valid(sx, sy) || !is_valid(tx, ty)) return {{sx, sy}};\n    \n    queue<pair<int,int>> q;\n    bool visited[MAX_N][MAX_N] = {false};\n    pair<int,int> parent[MAX_N][MAX_N];\n    int dist[MAX_N][MAX_N];\n    \n    for (int i = 0; i < MAX_N; i++) {\n        for (int j = 0; j < MAX_N; j++) {\n            dist[i][j] = 1e9;\n        }\n    }\n    \n    q.push({sx, sy});\n    visited[sx][sy] = true;\n    dist[sx][sy] = 0;\n    \n    while (!q.empty()) {\n        auto [x, y] = q.front(); q.pop();\n        \n        if (x == tx && y == ty) {\n            // Reconstruct path\n            vector<pair<int,int>> path;\n            pair<int,int> current = {tx, ty};\n            while (current.first != sx || current.second != sy) {\n                path.push_back(current);\n                current = parent[current.first][current.second];\n            }\n            path.push_back({sx, sy});\n            reverse(path.begin(), path.end());\n            return path;\n        }\n        \n        for (int d = 0; d < 4; d++) {\n            int nx = x + dx[d];\n            int ny = y + dy[d];\n            \n            if (is_valid(nx, ny) && !visited[nx][ny]) {\n                visited[nx][ny] = true;\n                parent[nx][ny] = {x, y};\n                dist[nx][ny] = dist[x][y] + 1;\n                q.push({nx, ny});\n            }\n        }\n    }\n    \n    return {{sx, sy}}; // Fallback\n}\n\n// Convert path to direction string\nstring path_to_directions(const vector<pair<int,int>>& path) {\n    string result;\n    for (int i = 1; i < path.size(); i++) {\n        auto [prev_x, prev_y] = path[i-1];\n        auto [curr_x, curr_y] = path[i];\n        \n        if (curr_x == prev_x - 1 && curr_y == prev_y) result += 'U';\n        else if (curr_x == prev_x + 1 && curr_y == prev_y) result += 'D';\n        else if (curr_x == prev_x && curr_y == prev_y - 1) result += 'L';\n        else if (curr_x == prev_x && curr_y == prev_y + 1) result += 'R';\n    }\n    return result;\n}\n\n// Ultimate coverage strategy with multiple fallbacks\nstring solve_ultimate_coverage() {\n    set<pair<int,int>> all_roads(roads.begin(), roads.end());\n    set<pair<int,int>> uncovered_roads = all_roads;\n    \n    vector<pair<int,int>> path_positions = {{si, sj}};\n    int current_x = si, current_y = sj;\n    \n    // Initial coverage\n    auto initial_visible = get_visible_roads(current_x, current_y);\n    for (auto& road : initial_visible) {\n        uncovered_roads.erase(road);\n    }\n    \n    const int MAX_MOVES = 4500;\n    int moves = 0;\n    \n    // Ultimate strategy: multiple layers of optimization\n    while (!uncovered_roads.empty() && moves < MAX_MOVES) {\n        pair<int,int> best_target = {-1, -1};\n        double best_score = -1e9;\n        vector<pair<int,int>> best_path;\n        \n        // Multi-level candidate selection:\n        \n        // Level 1: Critical uncovered roads (those that are hard to cover)\n        vector<pair<int,int>> critical_roads;\n        map<pair<int,int>, int> coverage_potential;\n        \n        // Calculate coverage potential for each uncovered road\n        for (auto& road : uncovered_roads) {\n            auto visible = get_visible_roads(road.first, road.second);\n            int potential = 0;\n            for (auto& v : visible) {\n                if (uncovered_roads.count(v)) potential++;\n            }\n            coverage_potential[road] = potential;\n            \n            // Roads with low potential are critical (hard to cover indirectly)\n            if (potential <= 3) {\n                critical_roads.push_back(road);\n            }\n        }\n        \n        // Level 2: Roads with high local isolation\n        vector<pair<int,int>> isolated_roads;\n        for (auto& road : uncovered_roads) {\n            int isolated_neighbors = 0;\n            // Check how many neighbors are also uncovered\n            for (int d = 0; d < 4; d++) {\n                int nx = road.first + dx[d];\n                int ny = road.second + dy[d];\n                if (is_valid(nx, ny) && uncovered_roads.count({nx, ny})) {\n                    isolated_neighbors++;\n                }\n            }\n            // Roads with few uncovered neighbors are more isolated\n            if (isolated_neighbors <= 2) {\n                isolated_roads.push_back(road);\n            }\n        }\n        \n        // Build candidate list with priorities\n        vector<pair<int,int>> candidates;\n        \n        // Add critical roads first (highest priority)\n        for (auto& road : critical_roads) {\n            if (candidates.size() < 15) {\n                candidates.push_back(road);\n            }\n        }\n        \n        // Add isolated roads\n        for (auto& road : isolated_roads) {\n            if (candidates.size() < 30 && find(candidates.begin(), candidates.end(), road) == candidates.end()) {\n                candidates.push_back(road);\n            }\n        }\n        \n        // Add high potential roads\n        vector<pair<int, pair<int,int>>> sorted_by_potential;\n        for (auto& road : uncovered_roads) {\n            sorted_by_potential.push_back({coverage_potential[road], road});\n        }\n        sort(sorted_by_potential.begin(), sorted_by_potential.end());\n        \n        // Add roads with high potential (but not already added)\n        for (int i = (int)sorted_by_potential.size() - 1; i >= 0 && candidates.size() < 40; i--) {\n            auto road = sorted_by_potential[i].second;\n            if (find(candidates.begin(), candidates.end(), road) == candidates.end()) {\n                candidates.push_back(road);\n            }\n        }\n        \n        // Evaluate candidates with sophisticated scoring\n        for (auto& target : candidates) {\n            auto path_to_target = bfs_path(current_x, current_y, target.first, target.second);\n            if (path_to_target.size() <= 1) continue;\n            \n            // Calculate comprehensive metrics\n            auto visible_from_target = get_visible_roads(target.first, target.second);\n            int new_coverage = 0;\n            int critical_coverage = 0;\n            int isolated_coverage = 0;\n            \n            for (auto& visible : visible_from_target) {\n                if (uncovered_roads.count(visible)) {\n                    new_coverage++;\n                    // Bonus for covering critical roads\n                    if (coverage_potential[visible] <= 3) critical_coverage++;\n                    // Bonus for covering isolated roads\n                    int isolated_neighbors = 0;\n                    for (int d = 0; d < 4; d++) {\n                        int nx = visible.first + dx[d];\n                        int ny = visible.second + dy[d];\n                        if (is_valid(nx, ny) && uncovered_roads.count({nx, ny})) {\n                            isolated_neighbors++;\n                        }\n                    }\n                    if (isolated_neighbors <= 2) isolated_coverage++;\n                }\n            }\n            \n            int distance = path_to_target.size() - 1;\n            double score = 0;\n            \n            // Base efficiency score\n            if (distance > 0) {\n                score = (double)new_coverage / distance;\n            } else {\n                score = new_coverage;\n            }\n            \n            // Multipliers for strategic importance\n            score *= (1.0 + critical_coverage * 0.3 + isolated_coverage * 0.2);\n            \n            // Bonus for targeting critical/isolated positions\n            if (find(critical_roads.begin(), critical_roads.end(), target) != critical_roads.end()) {\n                score *= 1.4;\n            }\n            if (find(isolated_roads.begin(), isolated_roads.end(), target) != isolated_roads.end()) {\n                score *= 1.2;\n            }\n            \n            if (score > best_score) {\n                best_score = score;\n                best_target = target;\n                best_path = path_to_target;\n            }\n        }\n        \n        // Fallback strategies if no good target found\n        if (best_target.first == -1 || best_score <= 0) {\n            // Fallback 1: Greedy closest uncovered road\n            int min_dist = 1e9;\n            pair<int,int> closest_road = {-1, -1};\n            vector<pair<int,int>> closest_path;\n            \n            // Sample a subset for performance\n            int sample_count = 0;\n            for (auto& road : uncovered_roads) {\n                if (sample_count++ >= 50) break;\n                \n                auto path = bfs_path(current_x, current_y, road.first, road.second);\n                int dist = path.size() - 1;\n                if (dist > 0 && dist < min_dist) {\n                    min_dist = dist;\n                    closest_road = road;\n                    closest_path = path;\n                }\n            }\n            \n            if (closest_road.first != -1) {\n                best_target = closest_road;\n                best_path = closest_path;\n            } else {\n                // Fallback 2: Any valid move\n                for (int d = 0; d < 4; d++) {\n                    int nx = current_x + dx[d];\n                    int ny = current_y + dy[d];\n                    if (is_valid(nx, ny)) {\n                        best_target = {nx, ny};\n                        best_path = {{current_x, current_y}, {nx, ny}};\n                        break;\n                    }\n                }\n            }\n        }\n        \n        // If still no target, break\n        if (best_target.first == -1 || best_path.size() <= 1) {\n            // Emergency fallback: visit remaining uncovered roads directly\n            if (!uncovered_roads.empty()) {\n                auto it = uncovered_roads.begin();\n                if (it != uncovered_roads.end()) {\n                    auto path_to_emergency = bfs_path(current_x, current_y, it->first, it->second);\n                    if (path_to_emergency.size() > 1) {\n                        best_path = path_to_emergency;\n                    }\n                }\n            }\n            if (best_path.size() <= 1) break;\n        }\n        \n        // Execute the move with move limit checking\n        int segment_length = best_path.size() - 1;\n        if (moves + segment_length <= MAX_MOVES) {\n            for (int i = 1; i < best_path.size(); i++) {\n                path_positions.push_back(best_path[i]);\n                current_x = best_path[i].first;\n                current_y = best_path[i].second;\n            }\n            \n            // Update coverage\n            auto visible_now = get_visible_roads(current_x, current_y);\n            for (auto& visible : visible_now) {\n                uncovered_roads.erase(visible);\n            }\n            \n            moves += segment_length;\n        } else {\n            break;\n        }\n    }\n    \n    // Final emergency coverage for any remaining roads\n    const int EMERGENCY_MOVES = 200;\n    int emergency_moves = 0;\n    while (!uncovered_roads.empty() && emergency_moves < EMERGENCY_MOVES) {\n        // Pick any uncovered road and go there\n        auto it = uncovered_roads.begin();\n        if (it != uncovered_roads.end()) {\n            auto path_to_last = bfs_path(current_x, current_y, it->first, it->second);\n            if (path_to_last.size() > 1) {\n                for (int i = 1; i < path_to_last.size(); i++) {\n                    path_positions.push_back(path_to_last[i]);\n                    current_x = path_to_last[i].first;\n                    current_y = path_to_last[i].second;\n                    emergency_moves++;\n                }\n                \n                // Update coverage one final time\n                auto visible_final = get_visible_roads(current_x, current_y);\n                for (auto& visible : visible_final) {\n                    uncovered_roads.erase(visible);\n                }\n            }\n        }\n        break; // Only one emergency attempt\n    }\n    \n    // Return home efficiently\n    auto return_path = bfs_path(current_x, current_y, si, sj);\n    for (int i = 1; i < return_path.size(); i++) {\n        path_positions.push_back(return_path[i]);\n    }\n    \n    return path_to_directions(path_positions);\n}\n\n// Focused exploration strategy as backup\nstring solve_focused_exploration() {\n    vector<pair<int,int>> path_positions = {{si, sj}};\n    int current_x = si, current_y = sj;\n    \n    set<pair<int,int>> all_roads_set(roads.begin(), roads.end());\n    set<pair<int,int>> uncovered_roads = all_roads_set;\n    \n    // Initial coverage\n    auto initial_visible = get_visible_roads(current_x, current_y);\n    for (auto& road : initial_visible) {\n        uncovered_roads.erase(road);\n    }\n    \n    const int MAX_STEPS = 4000;\n    int steps = 0;\n    \n    // Focused strategy: prioritize coverage over efficiency in early stages\n    while (!uncovered_roads.empty() && steps < MAX_STEPS) {\n        // Strategy phases:\n        // Phase 1 (0-2000 steps): Aggressive coverage\n        // Phase 2 (2000+ steps): Efficient return\n        \n        pair<int,int> best_move = {-1, -1};\n        int best_coverage = -1;\n        vector<pair<int,int>> best_path_segment;\n        \n        if (steps < 3000) {\n            // Aggressive coverage phase\n            // Find move that maximizes new coverage\n            \n            // Check adjacent positions\n            for (int d = 0; d < 4; d++) {\n                int nx = current_x + dx[d];\n                int ny = current_y + dy[d];\n                \n                if (is_valid(nx, ny)) {\n                    auto visible = get_visible_roads(nx, ny);\n                    int coverage = 0;\n                    for (auto& v : visible) {\n                        if (uncovered_roads.count(v)) coverage++;\n                    }\n                    \n                    if (coverage > best_coverage) {\n                        best_coverage = coverage;\n                        best_move = {nx, ny};\n                        best_path_segment = {{current_x, current_y}, {nx, ny}};\n                    }\n                }\n            }\n            \n            // If adjacent moves aren't good enough, plan longer paths\n            if (best_coverage < 2) {\n                // Target uncovered roads with high potential\n                pair<int,int> best_target = {-1, -1};\n                int best_target_coverage = -1;\n                \n                // Sample uncovered roads\n                int sample_count = 0;\n                for (auto& road : uncovered_roads) {\n                    if (sample_count++ >= 25) break;\n                    \n                    auto visible = get_visible_roads(road.first, road.second);\n                    int coverage = 0;\n                    for (auto& v : visible) {\n                        if (uncovered_roads.count(v)) coverage++;\n                    }\n                    \n                    if (coverage > best_target_coverage) {\n                        best_target_coverage = coverage;\n                        best_target = road;\n                    }\n                }\n                \n                if (best_target.first != -1) {\n                    auto path_to_target = bfs_path(current_x, current_y, best_target.first, best_target.second);\n                    if (path_to_target.size() > 1) {\n                        best_path_segment = path_to_target;\n                        best_move = best_path_segment.back();\n                    }\n                }\n            }\n        } else {\n            // Efficient phase - just try to cover what's left\n            // Find closest uncovered road\n            int min_dist = 1e9;\n            pair<int,int> closest_road = {-1, -1};\n            vector<pair<int,int>> closest_path;\n            \n            for (auto& road : uncovered_roads) {\n                auto path = bfs_path(current_x, current_y, road.first, road.second);\n                int dist = path.size() - 1;\n                if (dist > 0 && dist < min_dist) {\n                    min_dist = dist;\n                    closest_road = road;\n                    closest_path = path;\n                }\n            }\n            \n            if (closest_road.first != -1) {\n                best_path_segment = closest_path;\n                best_move = closest_path.back();\n            }\n        }\n        \n        // Execute the move\n        if (best_move.first != -1 && best_path_segment.size() > 1) {\n            int segment_length = best_path_segment.size() - 1;\n            if (steps + segment_length <= MAX_STEPS) {\n                for (int i = 1; i < best_path_segment.size(); i++) {\n                    path_positions.push_back(best_path_segment[i]);\n                    current_x = best_path_segment[i].first;\n                    current_y = best_path_segment[i].second;\n                    steps++;\n                }\n                \n                // Update coverage\n                auto visible_now = get_visible_roads(current_x, current_y);\n                for (auto& visible : visible_now) {\n                    uncovered_roads.erase(visible);\n                }\n            } else {\n                break;\n            }\n        } else {\n            // No good move, try random valid move\n            bool moved = false;\n            for (int d = 0; d < 4; d++) {\n                int nx = current_x + dx[d];\n                int ny = current_y + dy[d];\n                if (is_valid(nx, ny)) {\n                    current_x = nx;\n                    current_y = ny;\n                    path_positions.push_back({current_x, current_y});\n                    steps++;\n                    moved = true;\n                    \n                    // Update coverage for random moves\n                    auto visible_now = get_visible_roads(current_x, current_y);\n                    for (auto& visible : visible_now) {\n                        uncovered_roads.erase(visible);\n                    }\n                    break;\n                }\n            }\n            if (!moved) break;\n        }\n    }\n    \n    // Return home\n    auto return_path = bfs_path(current_x, current_y, si, sj);\n    for (int i = 1; i < return_path.size(); i++) {\n        path_positions.push_back(return_path[i]);\n    }\n    \n    return path_to_directions(path_positions);\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    srand(time(NULL));\n    \n    cin >> N >> si >> sj;\n    \n    for (int i = 0; i < N; i++) {\n        string line;\n        cin >> line;\n        for (int j = 0; j < N; j++) {\n            grid[i][j] = line[j];\n            if (line[j] >= '5' && line[j] <= '9') {\n                is_road[i][j] = true;\n                roads.push_back({i, j});\n            } else {\n                is_road[i][j] = false;\n            }\n        }\n    }\n    \n    // Generate solutions with our best strategies\n    string result1 = solve_ultimate_coverage();\n    string result2 = solve_focused_exploration();\n    \n    // Choose based on expected quality - ultimate coverage strategy is more robust\n    if (result1.length() > result2.length() * 0.8) {\n        cout << result1 << endl;\n    } else {\n        cout << result2 << endl;\n    }\n    \n    return 0;\n}","future-contest-2022-qual":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MAX_N = 1000;\nconst int MAX_M = 20;\n\nint N, M, K, R;\nvector<vector<int>> task_difficulty;\nvector<vector<int>> task_dependencies;\nvector<vector<int>> reverse_dependencies;\nvector<int> task_status;  // -1: not started, 0: in progress, 1: completed\nvector<int> member_current_task;\nvector<bool> task_ready;\nvector<int> in_degree;\n\nvoid update_ready_tasks() {\n    for (int i = 0; i < N; i++) {\n        if (task_status[i] == -1 && !task_ready[i]) {\n            bool all_deps_met = true;\n            for (int dep : task_dependencies[i]) {\n                if (task_status[dep] != 1) {\n                    all_deps_met = false;\n                    break;\n                }\n            }\n            if (all_deps_met) {\n                task_ready[i] = true;\n            }\n        }\n    }\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    cin >> N >> M >> K >> R;\n    \n    task_difficulty.assign(N, vector<int>(K));\n    task_dependencies.assign(N, vector<int>());\n    reverse_dependencies.assign(N, vector<int>());\n    task_status.assign(N, -1);\n    member_current_task.assign(M, -1);\n    task_ready.assign(N, false);\n    in_degree.assign(N, 0);\n    \n    // Read task difficulties\n    for (int i = 0; i < N; i++) {\n        for (int k = 0; k < K; k++) {\n            cin >> task_difficulty[i][k];\n        }\n    }\n    \n    // Read dependencies\n    for (int i = 0; i < R; i++) {\n        int u, v;\n        cin >> u >> v;\n        u--; v--;\n        task_dependencies[v].push_back(u);\n        reverse_dependencies[u].push_back(v);\n        in_degree[v]++;\n    }\n    \n    // Initialize ready tasks (those with no dependencies)\n    update_ready_tasks();\n    \n    vector<bool> task_assigned_today(N, false);\n    vector<bool> member_busy_today(M, false);\n    \n    int day = 0;\n    while (day < 2000) {\n        day++;\n        \n        // Reset daily tracking\n        fill(task_assigned_today.begin(), task_assigned_today.end(), false);\n        fill(member_busy_today.begin(), member_busy_today.end(), false);\n        \n        // Update which tasks are ready\n        update_ready_tasks();\n        \n        // Prepare assignments for today\n        vector<pair<int, int>> assignments; // {member, task}\n        \n        // Simple assignment: assign ready tasks to free members\n        for (int task = 0; task < N; task++) {\n            if (task_ready[task] && task_status[task] == -1 && !task_assigned_today[task]) {\n                // Find first available member\n                for (int member = 0; member < M; member++) {\n                    if (member_current_task[member] == -1 && !member_busy_today[member]) {\n                        assignments.push_back({member, task});\n                        member_busy_today[member] = true;\n                        task_assigned_today[task] = true;\n                        member_current_task[member] = task;\n                        task_status[task] = 0; // Mark as in progress\n                        break;\n                    }\n                }\n            }\n        }\n        \n        // Output assignments\n        cout << assignments.size();\n        for (auto& assignment : assignments) {\n            cout << \" \" << (assignment.first + 1) << \" \" << (assignment.second + 1);\n        }\n        cout << \"\\n\";\n        cout.flush();\n        \n        // Read completion information\n        int completion_count;\n        cin >> completion_count;\n        \n        if (completion_count == -1) {\n            break;\n        }\n        \n        if (completion_count == 0) {\n            continue;\n        }\n        \n        // Process completed tasks\n        for (int i = 0; i < completion_count; i++) {\n            int member_id;\n            cin >> member_id;\n            member_id--;\n            \n            int task_id = member_current_task[member_id];\n            if (task_id != -1) {\n                task_status[task_id] = 1; // Mark as completed\n                member_current_task[member_id] = -1; // Member is now free\n                \n                // Make dependent tasks ready if possible\n                for (int dependent : reverse_dependencies[task_id]) {\n                    bool all_deps_done = true;\n                    for (int dep : task_dependencies[dependent]) {\n                        if (task_status[dep] != 1) {\n                            all_deps_done = false;\n                            break;\n                        }\n                    }\n                    if (all_deps_done) {\n                        task_ready[dependent] = true;\n                    }\n                }\n            }\n        }\n    }\n    \n    return 0;\n}","ahc006":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int DEPOT_X = 400;\nconst int DEPOT_Y = 400;\nconst int NUM_ORDERS = 1000;\nconst int SELECT_COUNT = 50;\n\nstruct Order {\n    int id, a, b, c, d;\n    \n    Order(int _id, int _a, int _b, int _c, int _d) \n        : id(_id), a(_a), b(_b), c(_c), d(_d) {}\n    \n    int distance() const {\n        return abs(a - c) + abs(b - d);\n    }\n};\n\nstruct Point {\n    int x, y;\n    Point() : x(0), y(0) {}\n    Point(int _x, int _y) : x(_x), y(_y) {}\n    \n    int distance(const Point& other) const {\n        return abs(x - other.x) + abs(y - other.y);\n    }\n};\n\n// Select orders based on clustering and efficiency\nvector<int> selectOrders(const vector<Order>& orders) {\n    vector<pair<double, int>> scores;\n    \n    // Calculate approximate center of all orders\n    long long sum_a = 0, sum_b = 0, sum_c = 0, sum_d = 0;\n    for (const auto& order : orders) {\n        sum_a += order.a;\n        sum_b += order.b;\n        sum_c += order.c;\n        sum_d += order.d;\n    }\n    \n    int center_a_x = sum_a / NUM_ORDERS;\n    int center_a_y = sum_b / NUM_ORDERS;\n    int center_c_x = sum_c / NUM_ORDERS;\n    int center_c_y = sum_d / NUM_ORDERS;\n    \n    for (int i = 0; i < NUM_ORDERS; i++) {\n        const Order& order = orders[i];\n        \n        // Direct distance\n        int order_dist = order.distance();\n        \n        // Distance from centers\n        int dist_to_center_a = abs(order.a - center_a_x) + abs(order.b - center_a_y);\n        int dist_to_center_c = abs(order.c - center_c_x) + abs(order.d - center_c_y);\n        \n        // Distance-based score: prefer orders that are efficient and clustered\n        double score = (double)order_dist / (1.0 + dist_to_center_a * 0.2 + dist_to_center_c * 0.2);\n        \n        scores.push_back({score, i});\n    }\n    \n    sort(scores.begin(), scores.end(), greater<pair<double, int>>());\n    \n    vector<int> selected;\n    for (int i = 0; i < SELECT_COUNT; i++) {\n        selected.push_back(scores[i].second);\n    }\n    \n    return selected;\n}\n\n// Build initial route with pickups followed by deliveries\nvector<Point> buildInitialRoute(const vector<Order>& orders, const vector<int>& selected_ids) {\n    vector<Point> route;\n    route.push_back(Point(DEPOT_X, DEPOT_Y));\n    \n    // Add all pickups\n    for (int id : selected_ids) {\n        route.push_back(Point(orders[id].a, orders[id].b));\n    }\n    \n    // Add all deliveries\n    for (int id : selected_ids) {\n        route.push_back(Point(orders[id].c, orders[id].d));\n    }\n    \n    route.push_back(Point(DEPOT_X, DEPOT_Y));\n    \n    return route;\n}\n\n// Safe 2-opt that respects pickup-delivery constraints\nvoid safeTwoOpt(vector<Point>& route, const vector<int>& selected_ids) {\n    int n = route.size();\n    if (n <= 4) return;\n    \n    // Limit iterations for performance\n    for (int iter = 0; iter < 50; iter++) {\n        bool improved = false;\n        \n        // Only consider swaps that don't violate pickup-before-delivery\n        // Pickups are at indices 1 to SELECT_COUNT\n        // Deliveries are at indices SELECT_COUNT+1 to 2*SELECT_COUNT\n        \n        for (int i = 1; i < n - 2 && i < 30; i++) {\n            for (int j = i + 1; j < min(n - 1, i + 40); j++) {\n                // Check if this swap is safe (doesn't mix pickups and deliveries incorrectly)\n                bool safe_swap = true;\n                \n                // For simplicity, only allow swaps within pickup segment or within delivery segment\n                bool in_pickup_segment = (i >= 1 && j <= SELECT_COUNT);\n                bool in_delivery_segment = (i >= SELECT_COUNT + 1 && j <= 2 * SELECT_COUNT + 1);\n                \n                if (!in_pickup_segment && !in_delivery_segment) {\n                    // Cross-segment swaps are complex, skip for now\n                    continue;\n                }\n                \n                long long current_dist = (long long)route[i-1].distance(route[i]) + route[j].distance(route[j+1]);\n                long long new_dist = (long long)route[i-1].distance(route[j]) + route[i].distance(route[j+1]);\n                \n                if (new_dist + 2 < current_dist) {\n                    reverse(route.begin() + i, route.begin() + j + 1);\n                    improved = true;\n                    break;\n                }\n            }\n            if (improved) break;\n        }\n        \n        if (!improved) break;\n    }\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    vector<Order> orders;\n    \n    // Read input\n    for (int i = 0; i < NUM_ORDERS; i++) {\n        int a, b, c, d;\n        cin >> a >> b >> c >> d;\n        orders.emplace_back(i, a, b, c, d);\n    }\n    \n    // Select orders\n    vector<int> selected_ids = selectOrders(orders);\n    \n    // Build initial route\n    vector<Point> route = buildInitialRoute(orders, selected_ids);\n    \n    // Improve route with safe local search\n    safeTwoOpt(route, selected_ids);\n    \n    // Output\n    cout << SELECT_COUNT;\n    for (int id : selected_ids) {\n        cout << \" \" << (id + 1); // 1-indexed\n    }\n    cout << \"\\n\";\n    \n    cout << route.size();\n    for (const Point& p : route) {\n        cout << \" \" << p.x << \" \" << p.y;\n    }\n    cout << \"\\n\";\n    \n    return 0;\n}","ahc007":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 400;\nconst int M = 1995;\n\nclass UnionFind {\nprivate:\n    vector<int> parent, rank;\n    vector<int> component_size;\npublic:\n    UnionFind(int n) : parent(n), rank(n, 0), component_size(n, 1) {\n        for (int i = 0; i < n; i++) {\n            parent[i] = i;\n        }\n    }\n    \n    int find(int x) {\n        if (parent[x] != x) {\n            parent[x] = find(parent[x]);\n        }\n        return parent[x];\n    }\n    \n    bool unite(int x, int y) {\n        x = find(x);\n        y = find(y);\n        if (x == y) return false;\n        if (rank[x] < rank[y]) swap(x, y);\n        parent[y] = x;\n        component_size[x] += component_size[y];\n        if (rank[x] == rank[y]) rank[x]++;\n        return true;\n    }\n    \n    bool same(int x, int y) {\n        return find(x) == find(y);\n    }\n    \n    int get_component_size(int x) {\n        return component_size[find(x)];\n    }\n    \n    int count_components() {\n        int count = 0;\n        for (int i = 0; i < N; i++) {\n            if (find(i) == i) count++;\n        }\n        return count;\n    }\n};\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    // Read coordinates\n    vector<pair<int, int>> coords(N);\n    for (int i = 0; i < N; i++) {\n        cin >> coords[i].first >> coords[i].second;\n    }\n    \n    // Read edges\n    vector<pair<int, int>> edges(M);\n    for (int i = 0; i < M; i++) {\n        cin >> edges[i].first >> edges[i].second;\n    }\n    \n    // Precompute minimum distances\n    vector<int> min_distances(M);\n    for (int i = 0; i < M; i++) {\n        int u = edges[i].first;\n        int v = edges[i].second;\n        long long dx = (long long)(coords[u].first - coords[v].first);\n        long long dy = (long long)(coords[u].second - coords[v].second);\n        long long dist_sq = dx * dx + dy * dy;\n        double dist = sqrt((double)dist_sq);\n        min_distances[i] = max(1, (int)round(dist));\n    }\n    \n    // Initialize Union-Find\n    UnionFind uf(N);\n    \n    // Process edges as they arrive\n    for (int i = 0; i < M; i++) {\n        int length;\n        cin >> length;\n        \n        int u = edges[i].first;\n        int v = edges[i].second;\n        int min_dist = min_distances[i];\n        \n        // Check if this edge connects two different components\n        if (!uf.same(u, v)) {\n            // Essential for connectivity - accept\n            uf.unite(u, v);\n            cout << \"1\" << endl;\n            cout.flush();\n        } else {\n            // Creating a cycle - decide based on value\n            double ratio = (double)length / min_dist;\n            \n            // Calculate critical metrics\n            int current_components = uf.count_components();\n            int edges_remaining = M - i - 1;\n            int components_to_connect = current_components - 1;\n            \n            // Progress through the algorithm\n            double progress = (double)i / M;\n            \n            bool accept = false;\n            \n            // Refined thresholds with better balance\n            if (progress < 0.35) {\n                // Early phase - ultra selective\n                if (ratio <= 1.03) {\n                    accept = true;\n                } else if (ratio <= 1.12 && min_dist <= 12) {\n                    accept = true;\n                } else if (ratio <= 1.2 && min_dist <= 4) {\n                    accept = true;\n                }\n            } else if (progress < 0.65) {\n                // Mid phase - balanced approach\n                if (ratio <= 1.08) {\n                    accept = true;\n                } else if (ratio <= 1.2 && min_dist <= 18) {\n                    accept = true;\n                } else if (ratio <= 1.35 && min_dist <= 9) {\n                    accept = true;\n                } else if (ratio <= 1.5 && min_dist <= 4) {\n                    accept = true;\n                }\n            } else {\n                // Late phase - more aggressive when needed\n                bool edge_critical = (edges_remaining <= components_to_connect + 15);\n                \n                if (edge_critical) {\n                    // Emergency mode\n                    if (ratio <= 1.45) {\n                        accept = true;\n                    } else if (ratio <= 1.7 && min_dist <= 14) {\n                        accept = true;\n                    } else if (ratio <= 2.0 && min_dist <= 7) {\n                        accept = true;\n                    } else if (ratio <= 2.5 && min_dist <= 3) {\n                        accept = true;\n                    }\n                } else {\n                    // Normal late game\n                    if (ratio <= 1.15) {\n                        accept = true;\n                    } else if (ratio <= 1.3 && min_dist <= 22) {\n                        accept = true;\n                    } else if (ratio <= 1.5 && min_dist <= 11) {\n                        accept = true;\n                    } else if (ratio <= 1.8 && min_dist <= 5) {\n                        accept = true;\n                    } else if (ratio <= 2.1 && min_dist <= 2) {\n                        accept = true;\n                    }\n                }\n            }\n            \n            if (accept) {\n                cout << \"1\" << endl;\n                cout.flush();\n            } else {\n                cout << \"0\" << endl;\n                cout.flush();\n            }\n        }\n    }\n    \n    return 0;\n}","ahc008":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int GRID_SIZE = 30;\nconst int MAX_TURNS = 300;\nconst int dx[4] = {-1, 1, 0, 0}; // U, D, L, R\nconst int dy[4] = {0, 0, -1, 1};\nconst char dir_chars[4] = {'u', 'd', 'l', 'r'};\nconst char move_chars[4] = {'U', 'D', 'L', 'R'};\n\nstruct Position {\n    int x, y;\n    Position() : x(0), y(0) {}\n    Position(int _x, int _y) : x(_x), y(_y) {}\n    bool operator==(const Position& other) const {\n        return x == other.x && y == other.y;\n    }\n    bool operator!=(const Position& other) const {\n        return !(*this == other);\n    }\n    // Add comparison operator for use in std::set\n    bool operator<(const Position& other) const {\n        if (x != other.x) return x < other.x;\n        return y < other.y;\n    }\n};\n\nstruct Pet {\n    Position pos;\n    int type;\n    \n    Pet() : pos(0, 0), type(0) {}\n    Pet(int x, int y, int t) : pos(x, y), type(t) {}\n};\n\nstruct Human {\n    Position pos;\n    \n    Human() : pos(0, 0) {}\n    Human(int x, int y) : pos(x, y) {}\n};\n\nclass Grid {\npublic:\n    bool blocked[GRID_SIZE][GRID_SIZE];\n    bool pet_present[GRID_SIZE][GRID_SIZE];\n    bool human_present[GRID_SIZE][GRID_SIZE];\n    \n    Grid() {\n        memset(blocked, false, sizeof(blocked));\n        memset(pet_present, false, sizeof(pet_present));\n        memset(human_present, false, sizeof(human_present));\n    }\n    \n    void reset_presence() {\n        memset(pet_present, false, sizeof(pet_present));\n        memset(human_present, false, sizeof(human_present));\n    }\n    \n    bool is_valid(int x, int y) {\n        return x >= 0 && x < GRID_SIZE && y >= 0 && y < GRID_SIZE;\n    }\n    \n    bool is_passable(int x, int y) {\n        return is_valid(x, y) && !blocked[x][y];\n    }\n    \n    void block_square(int x, int y) {\n        if (is_valid(x, y)) {\n            blocked[x][y] = true;\n        }\n    }\n};\n\nclass Solver {\nprivate:\n    int N, M;\n    vector<Pet> pets;\n    vector<Human> humans;\n    Grid grid;\n    mt19937 rng;\n    \npublic:\n    Solver() : rng(chrono::steady_clock::now().time_since_epoch().count()) {}\n    \n    void read_input() {\n        cin >> N;\n        pets.resize(N);\n        for (int i = 0; i < N; i++) {\n            int x, y, t;\n            cin >> x >> y >> t;\n            pets[i] = Pet(x-1, y-1, t); // Convert to 0-indexed\n        }\n        \n        cin >> M;\n        humans.resize(M);\n        for (int i = 0; i < M; i++) {\n            int x, y;\n            cin >> x >> y;\n            humans[i] = Human(x-1, y-1); // Convert to 0-indexed\n        }\n    }\n    \n    // Check if a position is adjacent to any pet\n    bool is_adjacent_to_any_pet(int x, int y) {\n        if (!grid.is_valid(x, y)) return true;\n        \n        for (const auto& pet : pets) {\n            if (abs(pet.pos.x - x) + abs(pet.pos.y - y) <= 1) {\n                return true;\n            }\n        }\n        return false;\n    }\n    \n    // Check if moving in a direction is safe\n    bool is_safe_move(const Human& human, int direction) {\n        int nx = human.pos.x + dx[direction];\n        int ny = human.pos.y + dy[direction];\n        return grid.is_passable(nx, ny) && !is_adjacent_to_any_pet(nx, ny);\n    }\n    \n    // Find the closest pet to a human\n    int find_closest_pet(int human_id) {\n        Human& human = humans[human_id];\n        int closest_pet = -1;\n        int min_dist = 10000;\n        \n        for (int i = 0; i < N; i++) {\n            int dist = abs(human.pos.x - pets[i].pos.x) + abs(human.pos.y - pets[i].pos.y);\n            if (dist < min_dist) {\n                min_dist = dist;\n                closest_pet = i;\n            }\n        }\n        \n        return closest_pet;\n    }\n    \n    // Try to block a square that limits pet movement\n    string try_block_strategy(int human_id) {\n        Human& human = humans[human_id];\n        \n        // Don't block if we're adjacent to a pet\n        if (is_adjacent_to_any_pet(human.pos.x, human.pos.y)) {\n            return \"\";\n        }\n        \n        // Try to block squares that would limit pet movement\n        for (int d = 0; d < 4; d++) {\n            int nx = human.pos.x + dx[d];\n            int ny = human.pos.y + dy[d];\n            \n            if (grid.is_valid(nx, ny) && !grid.blocked[nx][ny]) {\n                // Check if this square is occupied by pet or human\n                bool occupied = false;\n                \n                // Check for pets\n                for (const auto& pet : pets) {\n                    if (pet.pos.x == nx && pet.pos.y == ny) {\n                        occupied = true;\n                        break;\n                    }\n                }\n                \n                // Check for humans\n                for (int i = 0; i < M; i++) {\n                    if (i != human_id && humans[i].pos.x == nx && humans[i].pos.y == ny) {\n                        occupied = true;\n                        break;\n                    }\n                }\n                \n                // Check if adjacent to any pet (rule constraint)\n                if (!occupied && !is_adjacent_to_any_pet(nx, ny)) {\n                    return string(1, dir_chars[d]);\n                }\n            }\n        }\n        \n        return \"\";\n    }\n    \n    // Try to move towards a strategic position\n    string try_move_strategy(int human_id) {\n        Human& human = humans[human_id];\n        \n        // Try to move away from pets if too close\n        if (is_adjacent_to_any_pet(human.pos.x, human.pos.y)) {\n            for (int d = 0; d < 4; d++) {\n                if (is_safe_move(human, d)) {\n                    return string(1, move_chars[d]);\n                }\n            }\n            return \".\";\n        }\n        \n        // Try to move towards closest pet but maintain safe distance\n        int closest_pet = find_closest_pet(human_id);\n        if (closest_pet != -1) {\n            Position target = pets[closest_pet].pos;\n            \n            // Try moves that get us closer but maintain safety\n            vector<pair<int, int>> candidates;\n            for (int d = 0; d < 4; d++) {\n                if (is_safe_move(human, d)) {\n                    int nx = human.pos.x + dx[d];\n                    int ny = human.pos.y + dy[d];\n                    int new_dist = abs(nx - target.x) + abs(ny - target.y);\n                    int old_dist = abs(human.pos.x - target.x) + abs(human.pos.y - target.y);\n                    \n                    if (new_dist < old_dist) {\n                        candidates.push_back({new_dist, d});\n                    }\n                }\n            }\n            \n            if (!candidates.empty()) {\n                sort(candidates.begin(), candidates.end());\n                return string(1, move_chars[candidates[0].second]);\n            }\n        }\n        \n        // If no strategic move, try any safe move\n        for (int d = 0; d < 4; d++) {\n            if (is_safe_move(human, d)) {\n                return string(1, move_chars[d]);\n            }\n        }\n        \n        return \".\";\n    }\n    \n    string get_action(int human_id) {\n        // First try blocking strategy\n        string block_action = try_block_strategy(human_id);\n        if (!block_action.empty()) {\n            return block_action;\n        }\n        \n        // If no good blocking, try moving\n        return try_move_strategy(human_id);\n    }\n    \n    void update_grid_state(const string& actions) {\n        // First, collect all intended blocking positions\n        vector<pair<int, Position>> block_intentions; // {human_id, position}\n        for (int i = 0; i < M; i++) {\n            char action = actions[i];\n            if (action == 'u' || action == 'd' || action == 'l' || action == 'r') {\n                int d = -1;\n                for (int j = 0; j < 4; j++) {\n                    if (dir_chars[j] == action) {\n                        d = j;\n                        break;\n                    }\n                }\n                if (d != -1) {\n                    int nx = humans[i].pos.x + dx[d];\n                    int ny = humans[i].pos.y + dy[d];\n                    if (grid.is_valid(nx, ny)) {\n                        block_intentions.push_back({i, Position(nx, ny)});\n                    }\n                }\n            }\n        }\n        \n        // Resolve conflicts: only allow one human to block each square\n        set<Position> blocked_this_turn;\n        vector<Position> final_blocked_positions;\n        \n        for (const auto& intention : block_intentions) {\n            // int human_id = intention.first; // Removed unused variable\n            Position pos = intention.second;\n            \n            // Check if position is already blocked this turn\n            if (blocked_this_turn.find(pos) != blocked_this_turn.end()) {\n                continue;\n            }\n            \n            // Check if position contains a pet\n            bool has_pet = false;\n            for (const auto& pet : pets) {\n                if (pet.pos.x == pos.x && pet.pos.y == pos.y) {\n                    has_pet = true;\n                    break;\n                }\n            }\n            \n            // Check if position contains a human\n            bool has_human = false;\n            for (int i = 0; i < M; i++) {\n                if (humans[i].pos.x == pos.x && humans[i].pos.y == pos.y) {\n                    has_human = true;\n                    break;\n                }\n            }\n            \n            // Check if adjacent to any pet (rule constraint)\n            bool adjacent_to_pet = is_adjacent_to_any_pet(pos.x, pos.y);\n            \n            // If all constraints are satisfied, block the square\n            if (!has_pet && !has_human && !adjacent_to_pet) {\n                blocked_this_turn.insert(pos);\n                final_blocked_positions.push_back(pos);\n            }\n        }\n        \n        // Apply all blocking\n        for (const auto& pos : final_blocked_positions) {\n            grid.block_square(pos.x, pos.y);\n        }\n        \n        // Then process movement actions\n        for (int i = 0; i < M; i++) {\n            char action = actions[i];\n            if (action == 'U' || action == 'D' || action == 'L' || action == 'R') {\n                int d = -1;\n                for (int j = 0; j < 4; j++) {\n                    if (move_chars[j] == action) {\n                        d = j;\n                        break;\n                    }\n                }\n                if (d != -1) {\n                    int nx = humans[i].pos.x + dx[d];\n                    int ny = humans[i].pos.y + dy[d];\n                    if (grid.is_passable(nx, ny)) {\n                        humans[i].pos.x = nx;\n                        humans[i].pos.y = ny;\n                    }\n                }\n            }\n        }\n    }\n    \n    void update_positions(const vector<string>& pet_moves) {\n        // Update pet positions based on their moves\n        for (int i = 0; i < N; i++) {\n            const string& moves = pet_moves[i];\n            for (char move : moves) {\n                if (move == 'U' && pets[i].pos.x > 0) pets[i].pos.x--;\n                else if (move == 'D' && pets[i].pos.x < GRID_SIZE - 1) pets[i].pos.x++;\n                else if (move == 'L' && pets[i].pos.y > 0) pets[i].pos.y--;\n                else if (move == 'R' && pets[i].pos.y < GRID_SIZE - 1) pets[i].pos.y++;\n            }\n        }\n    }\n    \n    void update_grid_presence() {\n        grid.reset_presence();\n        for (const auto& pet : pets) {\n            if (grid.is_valid(pet.pos.x, pet.pos.y)) {\n                grid.pet_present[pet.pos.x][pet.pos.y] = true;\n            }\n        }\n        for (const auto& human : humans) {\n            if (grid.is_valid(human.pos.x, human.pos.y)) {\n                grid.human_present[human.pos.x][human.pos.y] = true;\n            }\n        }\n    }\n    \n    void solve() {\n        read_input();\n        \n        // Initialize grid presence\n        update_grid_presence();\n        \n        for (int turn = 0; turn < MAX_TURNS; turn++) {\n            string actions = \"\";\n            for (int i = 0; i < M; i++) {\n                actions += get_action(i);\n            }\n            cout << actions << endl;\n            cout.flush();\n            \n            // Update grid based on human actions\n            update_grid_state(actions);\n            \n            // Read pet moves\n            vector<string> pet_moves(N);\n            for (int i = 0; i < N; i++) {\n                cin >> pet_moves[i];\n            }\n            \n            // Update positions\n            update_positions(pet_moves);\n            \n            // Update grid presence\n            update_grid_presence();\n        }\n    }\n};\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    Solver solver;\n    solver.solve();\n    \n    return 0;\n}","ahc009":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int SIZE = 20;\nconst int MAX_LENGTH = 200;\n\n// Directions: U, D, L, R\nconst int dr[] = {-1, 1, 0, 0};\nconst int dc[] = {0, 0, -1, 1};\nconst char dir_chars[] = {'U', 'D', 'L', 'R'};\n\nint si, sj, ti, tj;\ndouble p;\nbool h_walls[SIZE][SIZE-1]; // horizontal walls\nbool v_walls[SIZE-1][SIZE]; // vertical walls\n\nbool can_move(int r, int c, int dir) {\n    if (dir == 0) { // Up\n        if (r == 0) return false;\n        return !v_walls[r-1][c];\n    } else if (dir == 1) { // Down\n        if (r == SIZE-1) return false;\n        return !v_walls[r][c];\n    } else if (dir == 2) { // Left\n        if (c == 0) return false;\n        return !h_walls[r][c-1];\n    } else { // Right\n        if (c == SIZE-1) return false;\n        return !h_walls[r][c];\n    }\n}\n\n// Find shortest path using BFS\nstring find_shortest_path() {\n    if (si == ti && sj == tj) {\n        return \"\";\n    }\n    \n    queue<tuple<int, int, string>> q;\n    bool visited[SIZE][SIZE] = {};\n    \n    q.push({si, sj, \"\"});\n    visited[si][sj] = true;\n    \n    while (!q.empty()) {\n        auto [r, c, path] = q.front();\n        q.pop();\n        \n        if (r == ti && c == tj) {\n            return path;\n        }\n        \n        if (path.length() > MAX_LENGTH) continue;\n        \n        // Try all 4 directions\n        for (int dir = 0; dir < 4; dir++) {\n            int nr = r + dr[dir];\n            int nc = c + dc[dir];\n            \n            if (nr >= 0 && nr < SIZE && nc >= 0 && nc < SIZE && !visited[nr][nc]) {\n                if (can_move(r, c, dir)) {\n                    visited[nr][nc] = true;\n                    string new_path = path + dir_chars[dir];\n                    if (new_path.length() <= MAX_LENGTH) {\n                        q.push({nr, nc, new_path});\n                    }\n                }\n            }\n        }\n    }\n    \n    return \"\"; // No path found\n}\n\n// Generate a guaranteed valid path to target\nstring generate_guaranteed_path() {\n    string path = \"\";\n    int r = si, c = sj;\n    \n    // Use a smarter approach that tries to make consistent progress\n    while ((r != ti || c != tj) && path.length() < MAX_LENGTH) {\n        // Calculate direction to target\n        int dr_target = ti - r;\n        int dc_target = tj - c;\n        \n        bool moved = false;\n        \n        // Use a preference order based on which direction gives more progress\n        vector<pair<int, int>> move_prefs; // {priority, direction}\n        \n        // Add moves with priorities\n        if (dr_target != 0) {\n            move_prefs.push_back({abs(dr_target) * 1000 + abs(dc_target), dr_target > 0 ? 1 : 0});\n        }\n        if (dc_target != 0) {\n            move_prefs.push_back({abs(dc_target) * 1000 + abs(dr_target), dc_target > 0 ? 3 : 2});\n        }\n        \n        // Sort by priority (higher priority first)\n        sort(move_prefs.rbegin(), move_prefs.rend());\n        \n        // Try moves in priority order\n        for (auto& pref : move_prefs) {\n            int dir = pref.second;\n            if (can_move(r, c, dir)) {\n                path += dir_chars[dir];\n                r += dr[dir];\n                c += dc[dir];\n                moved = true;\n                break;\n            }\n        }\n        \n        // If no preferred move works, try any valid move\n        if (!moved) {\n            for (int dir = 0; dir < 4; dir++) {\n                if (can_move(r, c, dir)) {\n                    path += dir_chars[dir];\n                    r += dr[dir];\n                    c += dc[dir];\n                    moved = true;\n                    break;\n                }\n            }\n        }\n        \n        // If completely stuck, add a move anyway (will just stay in place)\n        if (!moved && path.length() < MAX_LENGTH) {\n            // Add move in general direction of target\n            if (dr_target > 0) {\n                path += 'D';\n            } else if (dr_target < 0) {\n                path += 'U';\n            } else if (dc_target > 0) {\n                path += 'R';\n            } else if (dc_target < 0) {\n                path += 'L';\n            } else {\n                path += 'D'; // Default\n            }\n        }\n        \n        // Prevent infinite loops\n        if (path.length() >= MAX_LENGTH - 5) {\n            break;\n        }\n    }\n    \n    // Final approach to target\n    int safety = 0;\n    while ((r != ti || c != tj) && path.length() < MAX_LENGTH && safety < 20) {\n        safety++;\n        \n        if (r != ti) {\n            if (r < ti && can_move(r, c, 1)) {\n                path += 'D';\n                r++;\n            } else if (r > ti && can_move(r, c, 0)) {\n                path += 'U';\n                r--;\n            }\n        }\n        \n        if (c != tj) {\n            if (c < tj && can_move(r, c, 3)) {\n                path += 'R';\n                c++;\n            } else if (c > tj && can_move(r, c, 2)) {\n                path += 'L';\n                c--;\n            }\n        }\n    }\n    \n    if (path.length() > MAX_LENGTH) {\n        path = path.substr(0, MAX_LENGTH);\n    }\n    \n    return path;\n}\n\n// Create an advanced robust path with optimized redundancy\nstring create_advanced_robust_path() {\n    // Get base path\n    string base_path = find_shortest_path();\n    \n    if (base_path.empty()) {\n        base_path = generate_guaranteed_path();\n    }\n    \n    // Handle empty path case\n    if (base_path.empty()) {\n        // Create a robust default path\n        string fallback = \"\";\n        int r = si, c = sj;\n        \n        // Snake pattern that covers area and is robust\n        bool going_down = true;\n        while (fallback.length() < MAX_LENGTH - 10) {\n            if (going_down) {\n                if (r < ti && r < SIZE-1) {\n                    if (can_move(r, c, 1)) {\n                        fallback += 'D';\n                        r++;\n                    } else {\n                        going_down = false;\n                    }\n                } else {\n                    going_down = false;\n                }\n            } else {\n                if (c < tj && c < SIZE-1) {\n                    if (can_move(r, c, 3)) {\n                        fallback += 'R';\n                        c++;\n                    } else if (r > 0 && can_move(r, c, 0)) {\n                        fallback += 'U';\n                        r--;\n                        going_down = true;\n                    } else {\n                        break;\n                    }\n                } else if (c > tj && c > 0) {\n                    if (can_move(r, c, 2)) {\n                        fallback += 'L';\n                        c--;\n                    } else if (r > 0 && can_move(r, c, 0)) {\n                        fallback += 'U';\n                        r--;\n                        going_down = true;\n                    } else {\n                        break;\n                    }\n                } else {\n                    break;\n                }\n            }\n            if (fallback.length() >= MAX_LENGTH - 15) break;\n        }\n        base_path = fallback;\n    }\n    \n    // Create robust path with advanced strategy\n    string robust_path = \"\";\n    \n    // More sophisticated redundancy calculation based on p and path length\n    double base_redundancy = 1.0;\n    if (p <= 0.15) {\n        base_redundancy = 1.2;\n    } else if (p <= 0.25) {\n        base_redundancy = 1.5;\n    } else if (p <= 0.35) {\n        base_redundancy = 2.0;\n    } else if (p <= 0.45) {\n        base_redundancy = 2.5;\n    } else {\n        base_redundancy = 3.0;\n    }\n    \n    // Calculate target length\n    int target_length = min(MAX_LENGTH, (int)(base_path.length() * base_redundancy));\n    \n    if (p <= 0.15) {\n        // Very low forget probability - minimal but smart redundancy\n        robust_path = base_path;\n        if (robust_path.length() + 15 <= MAX_LENGTH) {\n            // Add a smart pattern that helps with robustness\n            robust_path += \"DRURDLULDRURDL\";\n        }\n    } else if (p <= 0.25) {\n        // Low forget probability - moderate redundancy\n        robust_path = base_path;\n        if (robust_path.length() + min(25, (int)(base_path.length() * 0.8)) <= MAX_LENGTH) {\n            robust_path += base_path.substr(0, min(25, (int)(base_path.length() * 0.8)));\n        }\n        if (robust_path.length() + 15 <= MAX_LENGTH) {\n            robust_path += \"DRURDLULDRURDL\";\n        }\n    } else if (p <= 0.35) {\n        // Medium forget probability - good redundancy\n        robust_path = base_path;\n        while (robust_path.length() < target_length - 20 && \n               robust_path.length() + base_path.length() <= MAX_LENGTH - 20) {\n            robust_path += base_path;\n        }\n        // Add robust pattern\n        if (robust_path.length() + 20 <= MAX_LENGTH) {\n            robust_path += \"DRDRDRDRDRDRDRDRDRDR\";\n        }\n    } else {\n        // High forget probability - maximum redundancy\n        robust_path = base_path;\n        while (robust_path.length() < target_length - 30 && \n               robust_path.length() + min(30, (int)base_path.length()) <= MAX_LENGTH - 30) {\n            robust_path += base_path.substr(0, min(30, (int)base_path.length()));\n        }\n        // Add very robust patterns\n        if (robust_path.length() + 30 <= MAX_LENGTH) {\n            robust_path += \"DRDRDRDRDRDRDRDRDRDRDRDRDRDRDRDRDRDRDRDR\";\n        }\n    }\n    \n    // Ensure we don't exceed limit\n    if (robust_path.length() > MAX_LENGTH) {\n        robust_path = robust_path.substr(0, MAX_LENGTH);\n    }\n    \n    // Final optimization - if path is too short, add robust patterns\n    if (robust_path.length() < MAX_LENGTH - 20) {\n        string fill_pattern = \"DRURDLUL\";\n        while (robust_path.length() + fill_pattern.length() <= MAX_LENGTH) {\n            robust_path += fill_pattern;\n        }\n    }\n    \n    // Last resort safety check\n    if (robust_path.empty() || robust_path.length() > MAX_LENGTH) {\n        robust_path = \"\";\n        // Create a highly robust pattern\n        string core = \"DRURDLULDR\";\n        while (robust_path.length() + core.length() <= MAX_LENGTH) {\n            robust_path += core;\n        }\n        // Fill remaining with simple pattern\n        while (robust_path.length() < MAX_LENGTH) {\n            robust_path += (robust_path.length() % 2 == 0) ? 'D' : 'R';\n        }\n    }\n    \n    // Final truncation\n    if (robust_path.length() > MAX_LENGTH) {\n        robust_path = robust_path.substr(0, MAX_LENGTH);\n    }\n    \n    return robust_path;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    // Read input\n    cin >> si >> sj >> ti >> tj >> p;\n    \n    // Read horizontal walls\n    for (int i = 0; i < SIZE; i++) {\n        string line;\n        cin >> line;\n        for (int j = 0; j < SIZE-1; j++) {\n            h_walls[i][j] = (line[j] == '1');\n        }\n    }\n    \n    // Read vertical walls\n    for (int i = 0; i < SIZE-1; i++) {\n        string line;\n        cin >> line;\n        for (int j = 0; j < SIZE; j++) {\n            v_walls[i][j] = (line[j] == '1');\n        }\n    }\n    \n    // Generate the advanced robust path\n    string result = create_advanced_robust_path();\n    \n    // Final safety check\n    if (result.length() > MAX_LENGTH) {\n        result = result.substr(0, MAX_LENGTH);\n    }\n    \n    if (result.empty()) {\n        // Ultimate fallback\n        for (int i = 0; i < MAX_LENGTH; i++) {\n            result += (i % 4 < 2) ? 'D' : 'R';\n        }\n    }\n    \n    cout << result << endl;\n    \n    return 0;\n}","ahc010":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 30;\n\n// Direction: 0=left, 1=up, 2=right, 3=down\nconst int di[4] = {0, -1, 0, 1};\nconst int dj[4] = {-1, 0, 1, 0};\n\n// to[tile_type][direction_entering] = direction_exiting (-1 if invalid)\nconst int to[8][4] = {\n    {1, 0, -1, -1}, // tile 0\n    {3, -1, -1, 0}, // tile 1\n    {-1, -1, 3, 2}, // tile 2\n    {-1, 2, 1, -1}, // tile 3\n    {1, 0, 3, 2},   // tile 4\n    {3, 2, 1, 0},   // tile 5\n    {2, -1, 0, -1}, // tile 6\n    {-1, 3, -1, 1}  // tile 7\n};\n\nint grid[N][N];\nint rotations[N][N];\n\n// Get effective tile after rotation\nint getEffectiveTile(int baseTile, int rot) {\n    // Tile 0: 0->1->2->3->0\n    // Tile 1: 1->2->3->0->1\n    // Tile 2: 2->3->0->1->2\n    // Tile 3: 3->0->1->2->3\n    // Tile 4: 4->5->4->5->4\n    // Tile 5: 5->4->5->4->5\n    // Tile 6: 6->7->6->7->6\n    // Tile 7: 7->6->7->6->7\n    \n    if (baseTile <= 3) {\n        return (baseTile + rot) % 4;\n    } else if (baseTile == 4) {\n        return (rot % 2 == 0) ? 4 : 5;\n    } else if (baseTile == 5) {\n        return (rot % 2 == 0) ? 5 : 4;\n    } else if (baseTile == 6) {\n        return (rot % 2 == 0) ? 6 : 7;\n    } else { // baseTile == 7\n        return (rot % 2 == 0) ? 7 : 6;\n    }\n}\n\n// Find cycle length starting from (i,j) with entry direction d\nint findCycle(vector<vector<int>>& effGrid, vector<vector<vector<bool>>>& visited, int si, int sj, int sd) {\n    int i = si, j = sj, d = sd;\n    int length = 0;\n    \n    // Check if initial move is valid\n    int d2 = to[effGrid[i][j]][d];\n    if (d2 == -1) return 0;\n    \n    do {\n        // If already visited this (pos,dir) in current trace, not a new cycle\n        if (visited[i][j][d]) return 0;\n        \n        d2 = to[effGrid[i][j]][d];\n        if (d2 == -1) return 0;\n        \n        visited[i][j][d] = true;\n        \n        // Move to next position\n        i += di[d2];\n        j += dj[d2];\n        length++;\n        \n        // Check bounds\n        if (i < 0 || i >= N || j < 0 || j >= N) return 0;\n        \n        // New direction is opposite of exit direction\n        d = (d2 + 2) % 4;\n        \n        // Prevent infinite loops\n        if (length > 10000) return 0;\n        \n    } while (!(i == si && j == sj && d == sd));\n    \n    return length;\n}\n\n// Find all cycles\nvector<int> findCycles() {\n    // Create effective grid\n    vector<vector<int>> effGrid(N, vector<int>(N));\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            effGrid[i][j] = getEffectiveTile(grid[i][j], rotations[i][j]);\n        }\n    }\n    \n    vector<vector<vector<bool>>> visited(N, vector<vector<bool>>(N, vector<bool>(4, false)));\n    vector<int> cycles;\n    \n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            for (int d = 0; d < 4; d++) {\n                if (!visited[i][j][d]) {\n                    int cycleLen = findCycle(effGrid, visited, i, j, d);\n                    if (cycleLen > 0) {\n                        cycles.push_back(cycleLen);\n                    }\n                }\n            }\n        }\n    }\n    \n    sort(cycles.begin(), cycles.end(), greater<int>());\n    return cycles;\n}\n\nlong long getScore() {\n    vector<int> cycles = findCycles();\n    if (cycles.size() < 2) return 0;\n    return 1LL * cycles[0] * cycles[1];\n}\n\nint main() {\n    // Read input\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cin >> grid[i][j];\n            rotations[i][j] = 0;\n        }\n    }\n    \n    // Simple greedy improvement\n    long long bestScore = getScore();\n    \n    // Try local search with small changes\n    for (int iter = 0; iter < 1000 && iter < N * N * 2; iter++) {\n        int i = iter / N;\n        int j = iter % N;\n        \n        if (i >= N) break;\n        \n        int oldRot = rotations[i][j];\n        long long bestLocalScore = bestScore;\n        int bestRot = oldRot;\n        \n        // Try all rotations for this position\n        for (int r = 0; r < 4; r++) {\n            rotations[i][j] = r;\n            long long score = getScore();\n            if (score > bestLocalScore) {\n                bestLocalScore = score;\n                bestRot = r;\n            }\n        }\n        \n        rotations[i][j] = bestRot;\n        if (bestLocalScore > bestScore) {\n            bestScore = bestLocalScore;\n        } else {\n            rotations[i][j] = oldRot;\n        }\n    }\n    \n    // Output\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cout << rotations[i][j];\n        }\n    }\n    cout << endl;\n    \n    return 0;\n}","ahc011":"#include <bits/stdc++.h>\nusing namespace std;\n\n// Direction constants: U=up, D=down, L=left, R=right\n// These represent the direction the EMPTY SPACE moves\nconst int UP = 0, DOWN = 1, LEFT = 2, RIGHT = 3;\nconst int dr[4] = {-1, 1, 0, 0};\nconst int dc[4] = {0, 0, -1, 1};\nconst char move_chars[4] = {'U', 'D', 'L', 'R'};\n\nstruct State {\n    vector<vector<int>> board;\n    int empty_r, empty_c;\n    string moves;\n    \n    State() {}\n    State(vector<vector<int>> b, int er, int ec, string m = \"\") \n        : board(b), empty_r(er), empty_c(ec), moves(m) {}\n};\n\n// Check if two adjacent tiles can connect\n// direction: 0=up, 1=down, 2=left, 3=right (direction from first tile to second)\nbool canConnect(int tile1, int tile2, int direction) {\n    if (direction == 0) { // up: tile1 is below tile2\n        return (tile1 & 2) && (tile2 & 8);  // tile1 has up, tile2 has down\n    } else if (direction == 1) { // down: tile1 is above tile2\n        return (tile1 & 8) && (tile2 & 2);  // tile1 has down, tile2 has up\n    } else if (direction == 2) { // left: tile1 is right of tile2\n        return (tile1 & 1) && (tile2 & 4);  // tile1 has left, tile2 has right\n    } else { // right: tile1 is left of tile2\n        return (tile1 & 4) && (tile2 & 1);  // tile1 has right, tile2 has left\n    }\n}\n\n// Find connected components and check if they form trees\npair<int, bool> findLargestTree(const vector<vector<int>>& board, int empty_r, int empty_c) {\n    int N = board.size();\n    vector<vector<bool>> visited(N, vector<bool>(N, false));\n    vector<vector<bool>> is_tile(N, vector<bool>(N, true));\n    is_tile[empty_r][empty_c] = false;\n    \n    int max_tree_size = 0;\n    bool has_complete_tree = false;\n    \n    int total_tiles = 0;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (is_tile[i][j]) total_tiles++;\n        }\n    }\n    \n    // For each unvisited tile, find its connected component\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (!visited[i][j] && is_tile[i][j]) {\n                // BFS to find connected component\n                queue<pair<int,int>> q;\n                vector<pair<int,int>> component;\n                vector<pair<pair<int,int>, pair<int,int>>> edges;\n                q.push({i, j});\n                visited[i][j] = true;\n                component.push_back({i, j});\n                \n                while (!q.empty()) {\n                    auto [r, c] = q.front(); q.pop();\n                    \n                    // Check 4 directions\n                    for (int d = 0; d < 4; d++) {\n                        int nr = r + dr[d];\n                        int nc = c + dc[d];\n                        \n                        if (nr >= 0 && nr < N && nc >= 0 && nc < N && \n                            is_tile[nr][nc]) {\n                            if (canConnect(board[r][c], board[nr][nc], d)) {\n                                edges.push_back({{r, c}, {nr, nc}});\n                                if (!visited[nr][nc]) {\n                                    visited[nr][nc] = true;\n                                    q.push({nr, nc});\n                                    component.push_back({nr, nc});\n                                }\n                            }\n                        }\n                    }\n                }\n                \n                int component_size = component.size();\n                max_tree_size = max(max_tree_size, component_size);\n                \n                // A component is a tree if it has exactly (n-1) edges for n vertices\n                if (component_size > 0 && edges.size() == component_size - 1) {\n                    // This component is a tree\n                    if (component_size == total_tiles) {\n                        has_complete_tree = true;\n                    }\n                }\n            }\n        }\n    }\n    \n    return {max_tree_size, has_complete_tree};\n}\n\n// Count total connections for heuristic\nint countConnections(const vector<vector<int>>& board, int empty_r, int empty_c) {\n    int N = board.size();\n    int connections = 0;\n    vector<vector<bool>> is_tile(N, vector<bool>(N, true));\n    is_tile[empty_r][empty_c] = false;\n    \n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (!is_tile[i][j]) continue;\n            \n            // Check connections in all 4 directions\n            for (int d = 0; d < 4; d++) {\n                int nr = i + dr[d];\n                int nc = j + dc[d];\n                \n                if (nr >= 0 && nr < N && nc >= 0 && nc < N && is_tile[nr][nc]) {\n                    if (canConnect(board[i][j], board[nr][nc], d)) {\n                        connections++;\n                    }\n                }\n            }\n        }\n    }\n    \n    return connections;\n}\n\n// Evaluate state quality with emphasis on complete trees\nint evaluate(const vector<vector<int>>& board, int empty_r, int empty_c) {\n    auto [max_tree, is_complete] = findLargestTree(board, empty_r, empty_c);\n    int connections = countConnections(board, empty_r, empty_c);\n    \n    if (is_complete) {\n        // High priority for complete trees\n        return 1000000 + max_tree;\n    }\n    \n    // Primary: largest tree size, Secondary: total connections\n    return max_tree * 1000 + connections;\n}\n\n// Generate all possible moves from current state\nvector<State> getNeighbors(const State& current, int N) {\n    vector<State> neighbors;\n    \n    // Empty space can move UP, DOWN, LEFT, RIGHT\n    for (int d = 0; d < 4; d++) {\n        int nr = current.empty_r + dr[d];\n        int nc = current.empty_c + dc[d];\n        \n        if (nr >= 0 && nr < N && nc >= 0 && nc < N) {\n            // Create new state by moving empty space in direction d\n            vector<vector<int>> new_board = current.board;\n            swap(new_board[current.empty_r][current.empty_c], new_board[nr][nc]);\n            \n            string new_moves = current.moves + move_chars[d];\n            neighbors.push_back(State(new_board, nr, nc, new_moves));\n        }\n    }\n    \n    return neighbors;\n}\n\n// Main solving function using beam search with tree focus\nstring solve(vector<vector<int>> initial_board, int N, int T) {\n    // Find initial empty position\n    int empty_r = 0, empty_c = 0;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (initial_board[i][j] == 0) {\n                empty_r = i;\n                empty_c = j;\n                break;\n            }\n        }\n    }\n    \n    // Use beam search with wider beam for better exploration\n    const int BEAM_WIDTH = 100;\n    vector<State> beam;\n    beam.push_back(State(initial_board, empty_r, empty_c, \"\"));\n    \n    string best_moves = \"\";\n    int best_score = 0;\n    \n    // Track if we found a complete tree\n    bool found_complete_tree = false;\n    \n    // Beam search for several iterations\n    for (int iter = 0; iter < min(50, T/10); iter++) {\n        vector<pair<int, State>> scored_states;\n        \n        // Expand each state in current beam\n        for (const State& state : beam) {\n            vector<State> neighbors = getNeighbors(state, N);\n            for (const State& neighbor : neighbors) {\n                int score = evaluate(neighbor.board, neighbor.empty_r, neighbor.empty_c);\n                scored_states.push_back({score, neighbor});\n            }\n        }\n        \n        // Sort by score (descending)\n        sort(scored_states.begin(), scored_states.end(), [](const pair<int,State>& a, const pair<int,State>& b) {\n            return a.first > b.first;\n        });\n        \n        // Update best solution\n        if (!scored_states.empty() && scored_states[0].first > best_score) {\n            best_score = scored_states[0].first;\n            best_moves = scored_states[0].second.moves;\n            \n            // Check if we have a complete tree\n            auto [max_tree, is_complete] = findLargestTree(\n                scored_states[0].second.board, \n                scored_states[0].second.empty_r, \n                scored_states[0].second.empty_c\n            );\n            if (is_complete && max_tree == N*N-1) {\n                found_complete_tree = true;\n            }\n        }\n        \n        // Keep only top BEAM_WIDTH states\n        beam.clear();\n        for (int i = 0; i < min(BEAM_WIDTH, (int)scored_states.size()); i++) {\n            beam.push_back(scored_states[i].second);\n        }\n        \n        if (beam.empty()) break;\n    }\n    \n    // If we haven't used all moves and didn't find complete tree, do local improvement\n    if (best_moves.length() < T && !found_complete_tree) {\n        if (!beam.empty()) {\n            State current = beam[0];\n            \n            // Continue with greedy local search\n            mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n            uniform_int_distribution<int> dir_dist(0, 3);\n            \n            int current_score = evaluate(current.board, current.empty_r, current.empty_c);\n            \n            for (int step = current.moves.length(); step < T; step++) {\n                // Try all directions\n                int best_dir = -1;\n                int best_eval = current_score - 1; // Only accept improvements\n                vector<vector<int>> best_board;\n                int best_nr = -1, best_nc = -1;\n                \n                for (int d = 0; d < 4; d++) {\n                    int nr = current.empty_r + dr[d];\n                    int nc = current.empty_c + dc[d];\n                    \n                    if (nr >= 0 && nr < N && nc >= 0 && nc < N) {\n                        vector<vector<int>> new_board = current.board;\n                        swap(new_board[current.empty_r][current.empty_c], new_board[nr][nc]);\n                        int eval = evaluate(new_board, nr, nc);\n                        \n                        if (eval > best_eval) {\n                            best_eval = eval;\n                            best_dir = d;\n                            best_board = new_board;\n                            best_nr = nr;\n                            best_nc = nc;\n                        }\n                    }\n                }\n                \n                if (best_dir != -1) {\n                    current.board = best_board;\n                    current.empty_r = best_nr;\n                    current.empty_c = best_nc;\n                    current.moves += move_chars[best_dir];\n                    current_score = best_eval;\n                    \n                    // Update best if this is better\n                    if (best_eval > best_score) {\n                        best_score = best_eval;\n                        best_moves = current.moves;\n                    }\n                } else {\n                    // No improvement, try random move to escape local optimum\n                    int dir = dir_dist(rng);\n                    int nr = current.empty_r + dr[dir];\n                    int nc = current.empty_c + dc[dir];\n                    \n                    if (nr >= 0 && nr < N && nc >= 0 && nc < N) {\n                        swap(current.board[current.empty_r][current.empty_c], current.board[nr][nc]);\n                        current.empty_r = nr;\n                        current.empty_c = nc;\n                        current.moves += move_chars[dir];\n                    }\n                }\n            }\n        }\n    }\n    \n    return best_moves.substr(0, T);\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    int N, T;\n    cin >> N >> T;\n    \n    vector<vector<int>> board(N, vector<int>(N));\n    for (int i = 0; i < N; i++) {\n        string line;\n        cin >> line;\n        for (int j = 0; j < N; j++) {\n            char c = line[j];\n            if (c >= '0' && c <= '9') {\n                board[i][j] = c - '0';\n            } else {\n                board[i][j] = c - 'a' + 10;\n            }\n        }\n    }\n    \n    string result = solve(board, N, T);\n    cout << result << endl;\n    \n    return 0;\n}","ahc012":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst double PI = acos(-1);\nconst long long BOUND = 1000000000LL;\nconst double CAKE_RADIUS = 10000.0;\n\nstruct Strawberry {\n    long long x, y;\n    int id;\n    double angle;\n    double distance;\n};\n\ndouble calculate_angle(long long x, long long y) {\n    double angle = atan2((double)y, (double)x);\n    if (angle < 0) angle += 2 * PI;\n    return angle;\n}\n\npair<pair<long long, long long>, pair<long long, long long>> create_line_from_points(pair<double, double> p1, pair<double, double> p2) {\n    long long x1 = (long long)round(p1.first);\n    long long y1 = (long long)round(p1.second);\n    long long x2 = (long long)round(p2.first);\n    long long y2 = (long long)round(p2.second);\n    \n    // Ensure they're significantly different points\n    if (abs(x1 - x2) < 100 && abs(y1 - y2) < 100) {\n        x2 += 20000;\n        y1 += 20000;\n    }\n    \n    // Bound checking with safety margin\n    x1 = max(-BOUND + 100000LL, min(BOUND - 100000LL, x1));\n    y1 = max(-BOUND + 100000LL, min(BOUND - 100000LL, y1));\n    x2 = max(-BOUND + 100000LL, min(BOUND - 100000LL, x2));\n    y2 = max(-BOUND + 100000LL, min(BOUND - 100000LL, y2));\n    \n    return {{x1, y1}, {x2, y2}};\n}\n\n// Distance between two points\ndouble distance_points(long long x1, long long y1, long long x2, long long y2) {\n    return sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    int N, K;\n    cin >> N >> K;\n    \n    vector<int> a(11); // a[1] to a[10]\n    for (int i = 1; i <= 10; i++) {\n        cin >> a[i];\n    }\n    \n    vector<Strawberry> strawberries(N);\n    for (int i = 0; i < N; i++) {\n        cin >> strawberries[i].x >> strawberries[i].y;\n        strawberries[i].id = i;\n        strawberries[i].angle = calculate_angle(strawberries[i].x, strawberries[i].y);\n        strawberries[i].distance = distance_points(strawberries[i].x, strawberries[i].y, 0, 0);\n    }\n    \n    vector<pair<pair<long long, long long>, pair<long long, long long>>> lines;\n    \n    // Clustering: group nearby strawberries\n    vector<bool> used(N, false);\n    vector<vector<int>> clusters;\n    \n    // Create clusters prioritizing larger sizes first (they're more valuable)\n    for (int cluster_size = 10; cluster_size >= 1 && lines.size() < K; cluster_size--) {\n        int clusters_to_create = min(a[cluster_size], 8); // Cap to prevent overfitting\n        \n        for (int c = 0; c < clusters_to_create && clusters.size() < 25; c++) {\n            // Find first unused strawberry\n            int start_idx = -1;\n            for (int i = 0; i < N; i++) {\n                if (!used[i]) {\n                    start_idx = i;\n                    break;\n                }\n            }\n            \n            if (start_idx == -1) break;\n            \n            vector<int> current_cluster;\n            current_cluster.push_back(start_idx);\n            used[start_idx] = true;\n            \n            // Add nearby strawberries to cluster\n            for (int i = 1; i < cluster_size && current_cluster.size() < cluster_size; i++) {\n                int best_idx = -1;\n                double best_dist = 1e18;\n                \n                // Find strawberry closest to current cluster\n                for (int j = 0; j < N; j++) {\n                    if (used[j]) continue;\n                    \n                    // Distance to nearest member of cluster\n                    double min_dist = 1e18;\n                    for (int member : current_cluster) {\n                        double dist = distance_points(\n                            strawberries[member].x, strawberries[member].y,\n                            strawberries[j].x, strawberries[j].y\n                        );\n                        min_dist = min(min_dist, dist);\n                    }\n                    \n                    if (min_dist < best_dist) {\n                        best_dist = min_dist;\n                        best_idx = j;\n                    }\n                }\n                \n                if (best_idx != -1) {\n                    current_cluster.push_back(best_idx);\n                    used[best_idx] = true;\n                } else {\n                    break;\n                }\n            }\n            \n            if (!current_cluster.empty()) {\n                clusters.push_back(current_cluster);\n            }\n        }\n    }\n    \n    // Create separating lines between clusters\n    // But be smart about it - only create useful lines\n    for (int i = 0; i < (int)clusters.size() && lines.size() < K - 3; i++) {\n        for (int j = i + 1; j < (int)clusters.size() && lines.size() < K - 3; j++) {\n            if (lines.size() >= K - 3) break;\n            \n            // Only create line if clusters are reasonably separated\n            // Find centroids\n            double cx1 = 0, cy1 = 0;\n            for (int idx : clusters[i]) {\n                cx1 += strawberries[idx].x;\n                cy1 += strawberries[idx].y;\n            }\n            cx1 /= clusters[i].size();\n            cy1 /= clusters[i].size();\n            \n            double cx2 = 0, cy2 = 0;\n            for (int idx : clusters[j]) {\n                cx2 += strawberries[idx].x;\n                cy2 += strawberries[idx].y;\n            }\n            cx2 /= clusters[j].size();\n            cy2 /= clusters[j].size();\n            \n            // If clusters are far enough apart, create separating line\n            double dist = distance_points((long long)cx1, (long long)cy1, (long long)cx2, (long long)cy2);\n            if (dist > 500) { // Only separate if they're not too close\n                // Midpoint between centroids\n                double mid_x = (cx1 + cx2) / 2.0;\n                double mid_y = (cy1 + cy2) / 2.0;\n                \n                // Perpendicular to line connecting centroids\n                double dx = cx2 - cx1;\n                double dy = cy2 - cy1;\n                double angle = atan2(dy, dx) + PI/2;\n                \n                // Create separating line\n                double len = 300000;\n                pair<double, double> p1 = {mid_x + cos(angle) * len, mid_y + sin(angle) * len};\n                pair<double, double> p2 = {mid_x - cos(angle) * len, mid_y - sin(angle) * len};\n                \n                lines.push_back(create_line_from_points(p1, p2));\n            }\n        }\n    }\n    \n    // Add some radial lines for additional separation\n    if (lines.size() < K) {\n        int current_count = (int)lines.size();\n        int to_add = min(K - current_count, 8);\n        for (int i = 0; i < to_add && (int)lines.size() < K; i++) {\n            double angle = 2.0 * PI * i / max(to_add, 1);\n            double dist = CAKE_RADIUS * 2.5;\n            // Make sure points are well outside the cake\n            pair<double, double> p1 = {cos(angle) * dist, sin(angle) * dist};\n            pair<double, double> p2 = {cos(angle + 0.15) * dist, sin(angle + 0.15) * dist};\n            lines.push_back(create_line_from_points(p1, p2));\n        }\n    }\n    \n    // Ensure we don't exceed K lines\n    while ((int)lines.size() > K) {\n        lines.pop_back();\n    }\n    \n    // Output result\n    cout << lines.size() << \"\\n\";\n    for (const auto& line : lines) {\n        cout << line.first.first << \" \" << line.first.second << \" \" \n             << line.second.first << \" \" << line.second.second << \"\\n\";\n    }\n    \n    return 0;\n}","ahc014":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MAX_N = 65;\n\nint N, M;\nbool grid[MAX_N][MAX_N];\nvector<tuple<int, int, int, int, int, int, int, int>> operations;\n\n// Center of the grid\nint center;\n\n// Calculate weight of a position\ninline int weight(int x, int y) {\n    int dx = x - center;\n    int dy = y - center;\n    return dx * dx + dy * dy + 1;\n}\n\n// Check if position is valid (within bounds)\ninline bool valid_pos(int x, int y) {\n    return x >= 0 && x < N && y >= 0 && y < N;\n}\n\n// Try to find a valid rectangle by checking existing dots\nbool find_rectangle(int new_x, int new_y, int &x2, int &y2, int &x3, int &y3, int &x4, int &y4) {\n    // Collect existing dots\n    vector<pair<int, int>> dots;\n    for (int x = 0; x < N; x++) {\n        for (int y = 0; y < N; y++) {\n            if (grid[x][y]) {\n                dots.push_back({x, y});\n            }\n        }\n    }\n    \n    if (dots.size() < 3) return false;\n    \n    // Try all combinations of 3 existing dots\n    for (size_t i = 0; i < dots.size() && i < 50; i++) { // Limit for performance\n        for (size_t j = i + 1; j < dots.size() && j < 50; j++) {\n            for (size_t k = j + 1; k < dots.size() && k < 50; k++) {\n                int a_x = dots[i].first, a_y = dots[i].second;\n                int b_x = dots[j].first, b_y = dots[j].second;\n                int c_x = dots[k].first, c_y = dots[k].second;\n                \n                // Try different orderings to form a rectangle\n                // Check if new_x,new_y,a,b,c forms a valid rectangle in some order\n                \n                // Try order: new-a-b-c\n                int dx1 = a_x - new_x, dy1 = a_y - new_y;\n                int dx2 = b_x - a_x, dy2 = b_y - a_y;\n                int dx3 = c_x - b_x, dy3 = c_y - b_y;\n                int dx4 = new_x - c_x, dy4 = new_y - c_y;\n                \n                // Check if it forms a rectangle (opposite sides equal and parallel)\n                if (dx1 == -dx3 && dy1 == -dy3 && dx2 == -dx4 && dy2 == -dy4) {\n                    // Check if adjacent sides are perpendicular\n                    if (dx1 * dx2 + dy1 * dy2 == 0) {\n                        x2 = a_x; y2 = a_y; x3 = b_x; y3 = b_y; x4 = c_x; y4 = c_y;\n                        return true;\n                    }\n                }\n                \n                // Try order: new-a-c-b\n                dx1 = a_x - new_x, dy1 = a_y - new_y;\n                dx2 = c_x - a_x, dy2 = c_y - a_y;\n                dx3 = b_x - c_x, dy3 = b_y - c_y;\n                dx4 = new_x - b_x, dy4 = new_y - b_y;\n                \n                if (dx1 == -dx3 && dy1 == -dy3 && dx2 == -dx4 && dy2 == -dy4) {\n                    if (dx1 * dx2 + dy1 * dy2 == 0) {\n                        x2 = a_x; y2 = a_y; x3 = c_x; y3 = c_y; x4 = b_x; y4 = b_y;\n                        return true;\n                    }\n                }\n            }\n        }\n    }\n    \n    // Try diamond shapes\n    for (int d = 1; d <= min(N/2, 8); d++) {\n        vector<pair<int, int>> corners = {\n            {new_x, new_y + d}, {new_x + d, new_y}, \n            {new_x, new_y - d}, {new_x - d, new_y}\n        };\n        \n        int dot_count = 0;\n        int dot_indices[3];\n        int idx = 0;\n        \n        for (int i = 0; i < 4; i++) {\n            int cx = corners[i].first;\n            int cy = corners[i].second;\n            if (valid_pos(cx, cy) && grid[cx][cy]) {\n                if (idx < 3) {\n                    dot_indices[idx] = i;\n                    idx++;\n                }\n                dot_count++;\n            }\n        }\n        \n        if (dot_count == 3) {\n            x2 = corners[dot_indices[0]].first; y2 = corners[dot_indices[0]].second;\n            x3 = corners[dot_indices[1]].first; y3 = corners[dot_indices[1]].second;\n            x4 = corners[dot_indices[2]].first; y4 = corners[dot_indices[2]].second;\n            return true;\n        }\n    }\n    \n    return false;\n}\n\nvoid solve() {\n    // Create list of empty positions sorted by weight (descending)\n    vector<tuple<int, int, int>> candidates;\n    \n    for (int x = 1; x < N-1; x++) {\n        for (int y = 1; y < N-1; y++) {\n            if (!grid[x][y]) { // Only consider positions without dots\n                candidates.push_back({weight(x, y), x, y});\n            }\n        }\n    }\n    \n    sort(candidates.rbegin(), candidates.rend());\n    \n    // Try to place dots\n    const int MAX_ATTEMPTS = min(200, (int)candidates.size());\n    \n    for (int i = 0; i < MAX_ATTEMPTS; i++) {\n        int x = get<1>(candidates[i]);\n        int y = get<2>(candidates[i]);\n        \n        // Double-check that position doesn't have a dot\n        if (grid[x][y]) continue;\n        \n        int x2, y2, x3, y3, x4, y4;\n        \n        // Try to find a valid rectangle\n        if (find_rectangle(x, y, x2, y2, x3, y3, x4, y4)) {\n            operations.push_back(make_tuple(x, y, x2, y2, x3, y3, x4, y4));\n            grid[x][y] = true;\n        }\n    }\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    cin >> N >> M;\n    center = (N - 1) / 2;\n    \n    // Initialize grid\n    memset(grid, false, sizeof(grid));\n    \n    // Read initial dots\n    for (int i = 0; i < M; i++) {\n        int x, y;\n        cin >> x >> y;\n        if (valid_pos(x, y)) {\n            grid[x][y] = true;\n        }\n    }\n    \n    // Solve\n    solve();\n    \n    // Output results\n    cout << operations.size() << \"\\n\";\n    for (const auto& op : operations) {\n        cout << get<0>(op) << \" \" << get<1>(op) << \" \" \n             << get<2>(op) << \" \" << get<3>(op) << \" \"\n             << get<4>(op) << \" \" << get<5>(op) << \" \"\n             << get<6>(op) << \" \" << get<7>(op) << \"\\n\";\n    }\n    \n    return 0;\n}","ahc015":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int GRID_SIZE = 10;\nconst int TOTAL_CANDIES = 100;\nconst int LOOKAHEAD = 3; // Look ahead this many moves\n\n// Directions: F(forward/up), B(backward/down), L(left), R(right)\nconst char DIR_CHARS[] = {'F', 'B', 'L', 'R'};\nconst int DX[] = {-1, 1, 0, 0}; // F, B, L, R\nconst int DY[] = {0, 0, -1, 1};\n\nstruct Position {\n    int x, y;\n    Position() : x(0), y(0) {}\n    Position(int _x, int _y) : x(_x), y(_y) {}\n};\n\nclass CandyBox {\npublic:\n    int grid[GRID_SIZE][GRID_SIZE]; // 0 = empty, 1,2,3 = flavors\n    vector<int> flavor_sequence;\n    int candy_count[4]; // index 0 unused, 1-3 for flavors\n    \n    CandyBox() {\n        memset(grid, 0, sizeof(grid));\n        memset(candy_count, 0, sizeof(candy_count));\n    }\n    \n    void read_flavors() {\n        flavor_sequence.resize(TOTAL_CANDIES);\n        for (int i = 0; i < TOTAL_CANDIES; i++) {\n            cin >> flavor_sequence[i];\n        }\n    }\n    \n    // Convert empty cell index to position\n    Position index_to_position(int index) {\n        int count = 0;\n        for (int i = 0; i < GRID_SIZE; i++) {\n            for (int j = 0; j < GRID_SIZE; j++) {\n                if (grid[i][j] == 0) {\n                    count++;\n                    if (count == index) {\n                        return Position(i, j);\n                    }\n                }\n            }\n        }\n        return Position(-1, -1); // Error\n    }\n    \n    // Place candy at specified position\n    void place_candy(int x, int y, int flavor) {\n        grid[x][y] = flavor;\n        candy_count[flavor]++;\n    }\n    \n    // Simulate sliding in given direction\n    void slide_candies(int dir) {\n        if (dir == 0) { // Forward (up) - move rows up\n            for (int col = 0; col < GRID_SIZE; col++) {\n                int write_pos = 0;\n                for (int row = 0; row < GRID_SIZE; row++) {\n                    if (grid[row][col] != 0) {\n                        if (write_pos != row) {\n                            grid[write_pos][col] = grid[row][col];\n                            grid[row][col] = 0;\n                        }\n                        write_pos++;\n                    }\n                }\n            }\n        } else if (dir == 1) { // Backward (down) - move rows down\n            for (int col = 0; col < GRID_SIZE; col++) {\n                int write_pos = GRID_SIZE - 1;\n                for (int row = GRID_SIZE - 1; row >= 0; row--) {\n                    if (grid[row][col] != 0) {\n                        if (write_pos != row) {\n                            grid[write_pos][col] = grid[row][col];\n                            grid[row][col] = 0;\n                        }\n                        write_pos--;\n                    }\n                }\n            }\n        } else if (dir == 2) { // Left - move columns left\n            for (int row = 0; row < GRID_SIZE; row++) {\n                int write_pos = 0;\n                for (int col = 0; col < GRID_SIZE; col++) {\n                    if (grid[row][col] != 0) {\n                        if (write_pos != col) {\n                            grid[row][write_pos] = grid[row][col];\n                            grid[row][col] = 0;\n                        }\n                        write_pos++;\n                    }\n                }\n            }\n        } else if (dir == 3) { // Right - move columns right\n            for (int row = 0; row < GRID_SIZE; row++) {\n                int write_pos = GRID_SIZE - 1;\n                for (int col = GRID_SIZE - 1; col >= 0; col--) {\n                    if (grid[row][col] != 0) {\n                        if (write_pos != col) {\n                            grid[row][write_pos] = grid[row][col];\n                            grid[row][col] = 0;\n                        }\n                        write_pos--;\n                    }\n                }\n            }\n        }\n    }\n    \n    // Create a copy of current state\n    CandyBox copy() const {\n        CandyBox other;\n        for (int i = 0; i < GRID_SIZE; i++) {\n            for (int j = 0; j < GRID_SIZE; j++) {\n                other.grid[i][j] = grid[i][j];\n            }\n        }\n        for (int i = 0; i < 4; i++) {\n            other.candy_count[i] = candy_count[i];\n        }\n        other.flavor_sequence = flavor_sequence;\n        return other;\n    }\n    \n    // Calculate connected components score with additional metrics\n    pair<int, int> calculate_connectedness_detailed() {\n        bool visited[GRID_SIZE][GRID_SIZE];\n        memset(visited, false, sizeof(visited));\n        \n        int total_score = 0;\n        int max_component = 0;\n        int dx[] = {0, 0, -1, 1};\n        int dy[] = {-1, 1, 0, 0};\n        \n        for (int i = 0; i < GRID_SIZE; i++) {\n            for (int j = 0; j < GRID_SIZE; j++) {\n                if (grid[i][j] != 0 && !visited[i][j]) {\n                    // BFS to find connected component\n                    queue<Position> q;\n                    vector<Position> component;\n                    q.push(Position(i, j));\n                    visited[i][j] = true;\n                    component.push_back(Position(i, j));\n                    \n                    int flavor = grid[i][j];\n                    \n                    while (!q.empty()) {\n                        Position current = q.front();\n                        q.pop();\n                        \n                        for (int d = 0; d < 4; d++) {\n                            int nx = current.x + dx[d];\n                            int ny = current.y + dy[d];\n                            \n                            if (nx >= 0 && nx < GRID_SIZE && ny >= 0 && ny < GRID_SIZE) {\n                                if (!visited[nx][ny] && grid[nx][ny] == flavor) {\n                                    visited[nx][ny] = true;\n                                    q.push(Position(nx, ny));\n                                    component.push_back(Position(nx, ny));\n                                }\n                            }\n                        }\n                    }\n                    \n                    // Add square of component size to score\n                    int component_size = component.size();\n                    total_score += component_size * component_size;\n                    max_component = max(max_component, component_size);\n                }\n            }\n        }\n        \n        return {total_score, max_component};\n    }\n    \n    int calculate_connectedness() {\n        return calculate_connectedness_detailed().first;\n    }\n    \n    // Count empty cells\n    int count_empty() const {\n        int count = 0;\n        for (int i = 0; i < GRID_SIZE; i++) {\n            for (int j = 0; j < GRID_SIZE; j++) {\n                if (grid[i][j] == 0) count++;\n            }\n        }\n        return count;\n    }\n    \n    // Evaluate clustering potential for a specific flavor\n    int evaluate_flavor_clustering(int flavor) {\n        bool visited[GRID_SIZE][GRID_SIZE];\n        memset(visited, false, sizeof(visited));\n        \n        int total_score = 0;\n        int dx[] = {0, 0, -1, 1};\n        int dy[] = {-1, 1, 0, 0};\n        \n        for (int i = 0; i < GRID_SIZE; i++) {\n            for (int j = 0; j < GRID_SIZE; j++) {\n                if (grid[i][j] == flavor && !visited[i][j]) {\n                    // BFS to find connected component\n                    queue<Position> q;\n                    vector<Position> component;\n                    q.push(Position(i, j));\n                    visited[i][j] = true;\n                    component.push_back(Position(i, j));\n                    \n                    while (!q.empty()) {\n                        Position current = q.front();\n                        q.pop();\n                        \n                        for (int d = 0; d < 4; d++) {\n                            int nx = current.x + dx[d];\n                            int ny = current.y + dy[d];\n                            \n                            if (nx >= 0 && nx < GRID_SIZE && ny >= 0 && ny < GRID_SIZE) {\n                                if (!visited[nx][ny] && grid[nx][ny] == flavor) {\n                                    visited[nx][ny] = true;\n                                    q.push(Position(nx, ny));\n                                    component.push_back(Position(nx, ny));\n                                }\n                            }\n                        }\n                    }\n                    \n                    // Prefer larger components\n                    total_score += component.size() * component.size();\n                }\n            }\n        }\n        \n        return total_score;\n    }\n    \n    // Advanced evaluation considering multiple factors\n    double evaluate_move_advanced(int dir, int current_turn) {\n        // Create copy and simulate\n        CandyBox temp = this->copy();\n        temp.slide_candies(dir);\n        \n        auto [connected_score, max_component] = temp.calculate_connectedness_detailed();\n        \n        // Weight factors\n        double score = connected_score;\n        \n        // Bonus for larger maximum component\n        score += max_component * 10;\n        \n        // Evaluate clustering potential for each flavor\n        for (int flavor = 1; flavor <= 3; flavor++) {\n            if (temp.candy_count[flavor] > 0) {\n                score += temp.evaluate_flavor_clustering(flavor) * 0.5;\n            }\n        }\n        \n        // Prefer moves that don't isolate single candies\n        int isolated_candies = 0;\n        int dx[] = {0, 0, -1, 1};\n        int dy[] = {-1, 1, 0, 0};\n        for (int i = 0; i < GRID_SIZE; i++) {\n            for (int j = 0; j < GRID_SIZE; j++) {\n                if (temp.grid[i][j] != 0) {\n                    bool has_neighbor = false;\n                    int flavor = temp.grid[i][j];\n                    for (int d = 0; d < 4; d++) {\n                        int nx = i + dx[d];\n                        int ny = j + dy[d];\n                        if (nx >= 0 && nx < GRID_SIZE && ny >= 0 && ny < GRID_SIZE) {\n                            if (temp.grid[nx][ny] == flavor) {\n                                has_neighbor = true;\n                                break;\n                            }\n                        }\n                    }\n                    if (!has_neighbor) isolated_candies++;\n                }\n            }\n        }\n        score -= isolated_candies * 20; // Penalty for isolated candies\n        \n        return score;\n    }\n    \n    // Simulate lookahead\n    double simulate_lookahead(int dir, int current_turn) {\n        CandyBox temp = this->copy();\n        temp.slide_candies(dir);\n        \n        double total_score = temp.evaluate_move_advanced(dir, current_turn);\n        \n        // Simple lookahead - simulate next few placements randomly\n        int lookahead_depth = min(LOOKAHEAD, TOTAL_CANDIES - current_turn - 1);\n        CandyBox lookahead_state = temp.copy();\n        \n        for (int i = 0; i < lookahead_depth; i++) {\n            int future_turn = current_turn + 1 + i;\n            if (future_turn >= TOTAL_CANDIES) break;\n            \n            // Simulate a random placement (we don't know exact position, but we know flavor)\n            int flavor = lookahead_state.flavor_sequence[future_turn];\n            \n            // Find a reasonable position - prefer corners/edges for isolation control\n            vector<Position> empty_positions;\n            for (int x = 0; x < GRID_SIZE; x++) {\n                for (int y = 0; y < GRID_SIZE; y++) {\n                    if (lookahead_state.grid[x][y] == 0) {\n                        empty_positions.push_back(Position(x, y));\n                    }\n                }\n            }\n            \n            if (!empty_positions.empty()) {\n                // Place in a position that might help clustering\n                lookahead_state.place_candy(empty_positions[0].x, empty_positions[0].y, flavor);\n            }\n            \n            // Apply a reasonable tilt (prefer clustering directions)\n            int best_dir = 0;\n            double best_score = -1e9;\n            for (int d = 0; d < 4; d++) {\n                double score = lookahead_state.evaluate_move_advanced(d, future_turn);\n                if (score > best_score) {\n                    best_score = score;\n                    best_dir = d;\n                }\n            }\n            lookahead_state.slide_candies(best_dir);\n        }\n        \n        total_score += lookahead_state.calculate_connectedness() * 0.3;\n        return total_score;\n    }\n};\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    CandyBox box;\n    box.read_flavors();\n    \n    for (int turn = 0; turn < TOTAL_CANDIES; turn++) {\n        int p;\n        cin >> p;\n        \n        // Place the candy\n        Position pos = box.index_to_position(p);\n        box.place_candy(pos.x, pos.y, box.flavor_sequence[turn]);\n        \n        // Don't need to output direction for last candy\n        if (turn == TOTAL_CANDIES - 1) {\n            cout << \"F\" << endl;\n            cout.flush();\n            break;\n        }\n        \n        // Evaluate all four directions and choose best\n        double best_score = -1e18;\n        int best_direction = 0;\n        \n        for (int dir = 0; dir < 4; dir++) {\n            double score = box.simulate_lookahead(dir, turn);\n            if (score > best_score) {\n                best_score = score;\n                best_direction = dir;\n            }\n        }\n        \n        // Output the chosen direction\n        cout << DIR_CHARS[best_direction] << endl;\n        cout.flush();\n        \n        // Apply the chosen move\n        box.slide_candies(best_direction);\n    }\n    \n    return 0;\n}","ahc016":"#include <bits/stdc++.h>\nusing namespace std;\n\nmt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n\nconst int MAX_N = 100;\nconst int QUERY_COUNT = 100;\n\nint M;\ndouble epsilon;\nint N;\nvector<string> graphs;\nvector<vector<vector<int>>> graph_matrices;\n\n// Convert adjacency matrix to edge string\nstring matrixToEdgeString(const vector<vector<int>>& matrix) {\n    string result;\n    for (int i = 0; i < N; i++) {\n        for (int j = i + 1; j < N; j++) {\n            result += (matrix[i][j] ? '1' : '0');\n        }\n    }\n    return result;\n}\n\n// Convert edge string to adjacency matrix\nvector<vector<int>> edgeStringToMatrix(const string& edge_str) {\n    vector<vector<int>> matrix(N, vector<int>(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 (edge_str[idx] == '1') {\n                matrix[i][j] = 1;\n                matrix[j][i] = 1;\n            }\n            idx++;\n        }\n    }\n    return matrix;\n}\n\n// Calculate graph features for better discrimination\nvector<int> getDegreeSequence(const vector<vector<int>>& matrix) {\n    vector<int> degrees(N);\n    for (int i = 0; i < N; i++) {\n        degrees[i] = accumulate(matrix[i].begin(), matrix[i].end(), 0);\n    }\n    sort(degrees.begin(), degrees.end());\n    return degrees;\n}\n\n// Generate more distinguishable graphs\nvoid generateGraphs() {\n    // Adaptive N selection based on M and epsilon\n    if (M <= 15) N = 12;\n    else if (M <= 30) N = 16;\n    else if (M <= 50) N = 20;\n    else if (M <= 70) N = 24;\n    else N = 28;\n    \n    // For high error rates, use larger N\n    if (epsilon > 0.3) N = min(40, max(N, 20));\n    else if (epsilon > 0.2) N = min(35, max(N, 18));\n    \n    N = min(N, 100);\n    \n    graphs.clear();\n    graph_matrices.clear();\n    \n    // Generate graphs with different structures\n    for (int k = 0; k < M; k++) {\n        vector<vector<int>> matrix(N, vector<int>(N, 0));\n        \n        // Use multiple strategies based on k\n        if (k < M / 3) {\n            // Strategy 1: Regular graphs with different degrees\n            int target_degree = max(1, min(N-1, 1 + (k * (N-2)) / max(1, M/3 - 1)));\n            for (int i = 0; i < N; i++) {\n                int degree_count = 0;\n                for (int j = 0; j < N && degree_count < target_degree; j++) {\n                    if (i != j && uniform_int_distribution<int>(0, N-1)(rng) < target_degree) {\n                        matrix[i][j] = 1;\n                        matrix[j][i] = 1;\n                        degree_count++;\n                    }\n                }\n            }\n        } else if (k < 2 * M / 3) {\n            // Strategy 2: Cluster-based graphs\n            int clusters = max(2, min(N/2, 2 + (k * 3) / max(1, M/3)));\n            vector<int> cluster_assignment(N);\n            for (int i = 0; i < N; i++) {\n                cluster_assignment[i] = i % clusters;\n            }\n            \n            // Intra-cluster edges\n            for (int i = 0; i < N; i++) {\n                for (int j = i + 1; j < N; j++) {\n                    if (cluster_assignment[i] == cluster_assignment[j]) {\n                        if (uniform_int_distribution<int>(0, 2)(rng) > 0) {\n                            matrix[i][j] = 1;\n                            matrix[j][i] = 1;\n                        }\n                    } else {\n                        // Few inter-cluster edges\n                        if (uniform_int_distribution<int>(0, clusters*2)(rng) == 0) {\n                            matrix[i][j] = 1;\n                            matrix[j][i] = 1;\n                        }\n                    }\n                }\n            }\n        } else {\n            // Strategy 3: Random graphs with controlled edge count\n            int edge_count = max(N, min(N*(N-1)/4, (k * N * (N-1) / 2) / max(1, M - 2*M/3)));\n            for (int e = 0; e < edge_count; e++) {\n                int i, j;\n                do {\n                    i = uniform_int_distribution<int>(0, N-2)(rng);\n                    j = uniform_int_distribution<int>(i+1, N-1)(rng);\n                } while (matrix[i][j] == 1);\n                matrix[i][j] = 1;\n                matrix[j][i] = 1;\n            }\n        }\n        \n        graphs.push_back(matrixToEdgeString(matrix));\n        graph_matrices.push_back(matrix);\n    }\n}\n\n// Improved similarity calculation with better permutation handling\ndouble calculateSimilarity(const string& original, const string& received) {\n    auto orig_matrix = edgeStringToMatrix(original);\n    auto recv_matrix = edgeStringToMatrix(received);\n    \n    // Get degree sequences for quick filtering\n    auto orig_degrees = getDegreeSequence(orig_matrix);\n    auto recv_degrees = getDegreeSequence(recv_matrix);\n    \n    // Quick similarity check based on degree sequences\n    int degree_similarity = 0;\n    for (int i = 0; i < N; i++) {\n        degree_similarity += abs(orig_degrees[i] - recv_degrees[i]);\n    }\n    \n    // If degree sequences are too different, this is likely not a match\n    if (degree_similarity > N * epsilon * 4) {\n        return -1e9;\n    }\n    \n    // Try multiple random permutations\n    double best_score = -1e18;\n    \n    // Identity mapping\n    double identity_score = 0;\n    for (int i = 0; i < original.size(); i++) {\n        if (original[i] == received[i]) {\n            identity_score += log(1.0 - epsilon + 1e-12);\n        } else {\n            identity_score += log(epsilon + 1e-12);\n        }\n    }\n    best_score = identity_score;\n    \n    // Try random permutations\n    vector<int> perm(N);\n    for (int i = 0; i < N; i++) perm[i] = i;\n    \n    int trials = min(100, max(20, N * 2));\n    if (epsilon > 0.25) trials *= 2;\n    \n    for (int trial = 0; trial < trials; trial++) {\n        shuffle(perm.begin(), perm.end(), rng);\n        \n        double score = 0;\n        int idx = 0;\n        for (int i = 0; i < N; i++) {\n            for (int j = i + 1; j < N; j++) {\n                int pi = perm[i], pj = perm[j];\n                if (pi > pj) swap(pi, pj);\n                \n                // Calculate index in original graph\n                int orig_idx = 0;\n                for (int x = 0; x < pi; x++) {\n                    orig_idx += (N - 1 - x);\n                }\n                orig_idx += (pj - pi - 1);\n                \n                if (orig_idx >= 0 && orig_idx < original.size()) {\n                    char orig_edge = original[orig_idx];\n                    char recv_edge = received[idx];\n                    \n                    if (orig_edge == recv_edge) {\n                        score += log(1.0 - epsilon + 1e-12);\n                    } else {\n                        score += log(epsilon + 1e-12);\n                    }\n                }\n                idx++;\n            }\n        }\n        \n        best_score = max(best_score, score);\n    }\n    \n    return best_score;\n}\n\n// Enhanced decoding with multiple verification steps\nint decodeGraph(const string& received) {\n    vector<pair<double, int>> scores(M);\n    \n    for (int i = 0; i < M; i++) {\n        scores[i].second = i;\n        scores[i].first = calculateSimilarity(graphs[i], received);\n    }\n    \n    sort(scores.begin(), scores.end(), greater<pair<double, int>>());\n    \n    // If top score is significantly better, return it\n    if (scores.size() > 1 && scores[0].first > scores[1].first + 5.0) {\n        return scores[0].second;\n    }\n    \n    // Otherwise, use edge count as secondary criterion\n    int recv_edges = count(received.begin(), received.end(), '1');\n    vector<pair<int, int>> edge_diffs(M);\n    \n    for (int i = 0; i < M; i++) {\n        int orig_edges = count(graphs[i].begin(), graphs[i].end(), '1');\n        edge_diffs[i] = {abs(orig_edges - recv_edges), i};\n    }\n    \n    sort(edge_diffs.begin(), edge_diffs.end());\n    \n    // Blend both approaches\n    if (scores[0].first > -1e6) {\n        return scores[0].second;\n    }\n    return edge_diffs[0].second;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    cin >> M >> epsilon;\n    \n    // Generate graphs\n    generateGraphs();\n    \n    // Output N\n    cout << N << endl;\n    \n    // Output all graphs\n    for (const string& graph : graphs) {\n        cout << graph << endl;\n    }\n    cout.flush();\n    \n    // Process queries\n    for (int q = 0; q < QUERY_COUNT; q++) {\n        string received;\n        cin >> received;\n        \n        int prediction = decodeGraph(received);\n        cout << prediction << endl;\n        cout.flush();\n    }\n    \n    return 0;\n}","ahc017":"#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing P = pair<int, int>;\nusing PL = pair<ll, int>;\n\nconst ll INF = 1e15;\nconst int MAXN = 1005;\nconst int MAXM = 3005;\n\nint N, M, D, K;\nvector<tuple<int, int, int>> edges;\nvector<pair<int, int>> coords;\nvector<vector<P>> adj;\n\n// Find bridges using Tarjan's algorithm\nvector<bool> find_bridges() {\n    vector<bool> is_bridge(M, false);\n    vector<int> disc(N, -1), low(N, -1);\n    vector<bool> visited(N, false);\n    int time = 0;\n    \n    function<void(int, int, int)> dfs = [&](int u, int parent, int edge_idx) {\n        visited[u] = true;\n        disc[u] = low[u] = time++;\n        \n        for (int i = 0; i < adj[u].size(); i++) {\n            auto [v, w] = adj[u][i];\n            int current_edge_idx = -1;\n            \n            // Find the edge index\n            for (int j = 0; j < M; j++) {\n                auto [eu, ev, ew] = edges[j];\n                if ((eu-1 == u && ev-1 == v) || (eu-1 == v && ev-1 == u)) {\n                    current_edge_idx = j;\n                    break;\n                }\n            }\n            \n            if (!visited[v]) {\n                dfs(v, u, current_edge_idx);\n                low[u] = min(low[u], low[v]);\n                \n                if (low[v] > disc[u]) {\n                    if (current_edge_idx != -1) {\n                        is_bridge[current_edge_idx] = true;\n                    }\n                }\n            } else if (v != parent) {\n                low[u] = min(low[u], disc[v]);\n            }\n        }\n    };\n    \n    for (int i = 0; i < N; i++) {\n        if (!visited[i]) {\n            dfs(i, -1, -1);\n        }\n    }\n    \n    return is_bridge;\n}\n\n// Calculate multiple importance metrics\ntuple<vector<ll>, vector<ll>, vector<ll>> calculate_importance_metrics() {\n    vector<ll> degree_importance(M, 0);\n    vector<ll> weight_importance(M, 0);\n    vector<ll> spatial_importance(M, 0);\n    \n    // Degree-based importance\n    vector<int> degree(N, 0);\n    for (int i = 0; i < M; i++) {\n        auto [u, v, w] = edges[i];\n        degree[u-1]++;\n        degree[v-1]++;\n    }\n    \n    for (int i = 0; i < M; i++) {\n        auto [u, v, w] = edges[i];\n        // Edges connecting high-degree vertices are more important\n        degree_importance[i] = (ll)(degree[u-1] + degree[v-1]) * 1000000LL;\n        // Lower weight edges are more critical for shortest paths\n        weight_importance[i] = 1000000000LL / (w + 1);\n    }\n    \n    // Spatial importance - edges closer to graph center\n    ll sum_x = 0, sum_y = 0;\n    for (int i = 0; i < N; i++) {\n        sum_x += coords[i].first;\n        sum_y += coords[i].second;\n    }\n    double center_x = (double)sum_x / N;\n    double center_y = (double)sum_y / N;\n    \n    for (int i = 0; i < M; i++) {\n        auto [u, v, w] = edges[i];\n        auto [x1, y1] = coords[u-1];\n        auto [x2, y2] = coords[v-1];\n        \n        // Midpoint of edge\n        double mid_x = (x1 + x2) / 2.0;\n        double mid_y = (y1 + y2) / 2.0;\n        \n        // Distance from center\n        double dist_from_center = sqrt((mid_x - center_x) * (mid_x - center_x) + \n                                      (mid_y - center_y) * (mid_y - center_y));\n        \n        // Closer to center = more important\n        spatial_importance[i] = (ll)(10000.0 / (dist_from_center + 1.0)) * 100000LL;\n    }\n    \n    return {degree_importance, weight_importance, spatial_importance};\n}\n\n// Main solving function\nvector<int> solve() {\n    // Build adjacency list\n    adj.assign(N, vector<P>());\n    for (int i = 0; i < M; i++) {\n        auto [u, v, w] = edges[i];\n        adj[u-1].push_back({v-1, w});\n        adj[v-1].push_back({u-1, w});\n    }\n    \n    // Find bridges (critical edges)\n    auto is_bridge = find_bridges();\n    \n    // Calculate importance metrics\n    auto [degree_imp, weight_imp, spatial_imp] = calculate_importance_metrics();\n    \n    // Combine importance scores\n    vector<pair<ll, int>> combined_importance(M);\n    for (int i = 0; i < M; i++) {\n        ll bridge_bonus = is_bridge[i] ? 10000000000LL : 0;\n        ll total_importance = degree_imp[i] + weight_imp[i] + spatial_imp[i] + bridge_bonus;\n        combined_importance[i] = {total_importance, i};\n    }\n    \n    sort(combined_importance.rbegin(), combined_importance.rend());\n    \n    // Assign edges to days with better strategy\n    vector<int> result(M, 1);\n    vector<int> day_count(D + 1, 0);\n    \n    // First, assign bridges and very high importance edges to separate days\n    int critical_count = 0;\n    for (int i = 0; i < min(M, D * 3) && critical_count < D; i++) {\n        int edge_idx = combined_importance[i].second;\n        if (is_bridge[edge_idx] || combined_importance[i].first > 5000000000LL) {\n            // Assign to next available day\n            if (critical_count + 1 <= D && day_count[critical_count + 1] < K) {\n                result[edge_idx] = critical_count + 1;\n                day_count[critical_count + 1]++;\n                critical_count++;\n            }\n        }\n    }\n    \n    // Then assign remaining high importance edges\n    for (int i = 0; i < min(M, D * 5); i++) {\n        int edge_idx = combined_importance[i].second;\n        if (result[edge_idx] != 1) continue; // Already assigned\n        \n        // Find day with minimum load\n        int best_day = 1;\n        for (int day = 2; day <= D; day++) {\n            if (day_count[day] < day_count[best_day]) {\n                best_day = day;\n            }\n        }\n        \n        if (day_count[best_day] < K) {\n            result[edge_idx] = best_day;\n            day_count[best_day]++;\n        }\n    }\n    \n    // Assign remaining edges\n    for (int i = 0; i < M; i++) {\n        if (result[i] != 1) continue; // Already assigned\n        \n        // Find day with minimum load\n        int best_day = 1;\n        for (int day = 2; day <= D; day++) {\n            if (day_count[day] < day_count[best_day]) {\n                best_day = day;\n            }\n        }\n        \n        if (day_count[best_day] < K) {\n            result[i] = best_day;\n            day_count[best_day]++;\n        } else {\n            // Find any day with space\n            for (int day = 1; day <= D; day++) {\n                if (day_count[day] < K) {\n                    result[i] = day;\n                    day_count[day]++;\n                    break;\n                }\n            }\n        }\n    }\n    \n    // Lightweight local search\n    auto start_time = chrono::steady_clock::now();\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n    \n    uniform_int_distribution<int> edge_dist(0, M-1);\n    uniform_int_distribution<int> day_dist(1, D);\n    \n    // Very limited and fast local search\n    for (int iter = 0; iter < 5000; iter++) {\n        auto current_time = chrono::steady_clock::now();\n        if (chrono::duration_cast<chrono::milliseconds>(current_time - start_time).count() > 5500) {\n            break;\n        }\n        \n        int edge_idx = edge_dist(rng);\n        int current_day = result[edge_idx];\n        int new_day = day_dist(rng);\n        \n        if (new_day != current_day) {\n            // Quick constraint check\n            vector<int> day_load(D + 1, 0);\n            for (int i = 0; i < M; i++) {\n                day_load[result[i]]++;\n            }\n            \n            if (day_load[new_day] < K) {\n                // Simple swap acceptance - accept if it spreads out high importance edges\n                ll old_score = 0, new_score = 0;\n                \n                // Very rough approximation - just check if we're balancing important edges\n                result[edge_idx] = new_day;\n                // Accept most moves in the time limit since exact evaluation is too expensive\n            }\n        }\n    }\n    \n    return result;\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    cin >> N >> M >> D >> K;\n    edges.resize(M);\n    coords.resize(N);\n    \n    for (int i = 0; i < M; i++) {\n        int u, v, w;\n        cin >> u >> v >> w;\n        edges[i] = {u, v, w};\n    }\n    \n    for (int i = 0; i < N; i++) {\n        cin >> coords[i].first >> coords[i].second;\n    }\n    \n    auto result = solve();\n    \n    for (int i = 0; i < M; i++) {\n        cout << result[i];\n        if (i < M - 1) cout << \" \";\n    }\n    cout << \"\\n\";\n    \n    return 0;\n}","ahc019":"#include <bits/stdc++.h>\nusing namespace std;\n\n// Generate some simple connected blocks\nvector<vector<pair<int,int>>> generate2DBlocks() {\n    vector<vector<pair<int,int>>> blocks;\n    \n    // Single cube\n    blocks.push_back({{0, 0}});\n    \n    // 1x2 lines\n    blocks.push_back({{0, 0}, {1, 0}});\n    blocks.push_back({{0, 0}, {0, 1}});\n    \n    // 1x3 lines\n    blocks.push_back({{0, 0}, {1, 0}, {2, 0}});\n    blocks.push_back({{0, 0}, {0, 1}, {0, 2}});\n    \n    // 2x2 square\n    blocks.push_back({{0, 0}, {1, 0}, {0, 1}, {1, 1}});\n    \n    // L-shape\n    blocks.push_back({{0, 0}, {1, 0}, {0, 1}});\n    \n    return blocks;\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int D;\n    cin >> D;\n    \n    vector<vector<int>> f1(D, vector<int>(D));\n    vector<vector<int>> r1(D, vector<int>(D));\n    vector<vector<int>> f2(D, vector<int>(D));\n    vector<vector<int>> r2(D, vector<int>(D));\n    \n    // Read input\n    for (int i = 0; i < D; i++) {\n        string line; cin >> line;\n        for (int j = 0; j < D; j++) f1[i][j] = line[j] - '0';\n    }\n    for (int i = 0; i < D; i++) {\n        string line; cin >> line;\n        for (int j = 0; j < D; j++) r1[i][j] = line[j] - '0';\n    }\n    for (int i = 0; i < D; i++) {\n        string line; cin >> line;\n        for (int j = 0; j < D; j++) f2[i][j] = line[j] - '0';\n    }\n    for (int i = 0; i < D; i++) {\n        string line; cin >> line;\n        for (int j = 0; j < D; j++) r2[i][j] = line[j] - '0';\n    }\n    \n    // Create solution arrays\n    vector<vector<vector<int>>> sol1(D, vector<vector<int>>(D, vector<int>(D, 0)));\n    vector<vector<vector<int>>> sol2(D, vector<vector<int>>(D, vector<int>(D, 0)));\n    \n    // Identify required positions for each solution\n    vector<tuple<int,int,int>> req1, req2;\n    \n    for (int z = 0; z < D; z++) {\n        for (int x = 0; x < D; x++) {\n            if (f1[z][x] == 1) {\n                for (int y = 0; y < D; y++) {\n                    if (r1[z][y] == 1) {\n                        req1.push_back({z, x, y});\n                    }\n                }\n            }\n        }\n    }\n    \n    for (int z = 0; z < D; z++) {\n        for (int x = 0; x < D; x++) {\n            if (f2[z][x] == 1) {\n                for (int y = 0; y < D; y++) {\n                    if (r2[z][y] == 1) {\n                        req2.push_back({z, x, y});\n                    }\n                }\n            }\n        }\n    }\n    \n    // Better approach: share blocks between solutions where positions match exactly\n    int block_id = 1;\n    map<tuple<int,int,int>, int> position_to_block;\n    \n    // First, find positions that are required in both solutions\n    set<tuple<int,int,int>> set1(req1.begin(), req1.end());\n    set<tuple<int,int,int>> set2(req2.begin(), req2.end());\n    \n    // Assign blocks to positions that are in both solutions\n    for (const auto& pos : set1) {\n        if (set2.count(pos)) {\n            position_to_block[pos] = block_id++;\n        }\n    }\n    \n    // Assign remaining positions in solution 1\n    for (const auto& pos : req1) {\n        if (position_to_block.find(pos) == position_to_block.end()) {\n            position_to_block[pos] = block_id++;\n        }\n    }\n    \n    // Assign remaining positions in solution 2\n    for (const auto& pos : req2) {\n        if (position_to_block.find(pos) == position_to_block.end()) {\n            position_to_block[pos] = block_id++;\n        }\n    }\n    \n    int total_blocks = block_id - 1;\n    \n    // Fill solution arrays\n    for (const auto& pos : req1) {\n        auto [z, x, y] = pos;\n        sol1[z][x][y] = position_to_block[pos];\n    }\n    \n    for (const auto& pos : req2) {\n        auto [z, x, y] = pos;\n        sol2[z][x][y] = position_to_block[pos];\n    }\n    \n    cout << total_blocks << \"\\n\";\n    \n    // Output solutions\n    bool first = true;\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 (!first) cout << \" \";\n                cout << sol1[z][x][y];\n                first = false;\n            }\n        }\n    }\n    cout << \"\\n\";\n    \n    first = true;\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 (!first) cout << \" \";\n                cout << sol2[z][x][y];\n                first = false;\n            }\n        }\n    }\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\nconst int MAXN = 105;\nconst int MAXM = 305;\nconst int MAXK = 5005;\n\nint N, M, K;\npair<int, int> vertices[MAXN];\ntuple<int, int, int> edges[MAXM];\npair<int, int> residents[MAXK];\nint P[MAXN];\nint B[MAXM];\n\n// Distance calculations\nll dist_sq(int x1, int y1, int x2, int y2) {\n    return 1LL * (x1 - x2) * (x1 - x2) + 1LL * (y1 - y2) * (y1 - y2);\n}\n\nld dist(int x1, int y1, int x2, int y2) {\n    return sqrt((ld)dist_sq(x1, y1, x2, y2));\n}\n\nint dist_rounded(int x1, int y1, int x2, int y2) {\n    return (int)round(dist(x1, y1, x2, y2));\n}\n\n// Find close vertices to a resident\nvector<pair<int, int>> find_close_vertices(int rx, int ry, int max_dist = 5000) {\n    vector<pair<int, int>> result;\n    for (int i = 0; i < N; i++) {\n        int d = dist_rounded(rx, ry, vertices[i].first, vertices[i].second);\n        if (d <= max_dist) {\n            result.push_back({i, d});\n        }\n    }\n    sort(result.begin(), result.end(), [](const pair<int, int>& a, const pair<int, int>& b) {\n        return a.second < b.second;\n    });\n    return result;\n}\n\n// Union-Find data structure\nclass UnionFind {\nprivate:\n    vector<int> parent, rank;\npublic:\n    UnionFind(int n) : parent(n), rank(n, 0) {\n        for (int i = 0; i < n; i++) parent[i] = i;\n    }\n    \n    int find(int x) {\n        if (parent[x] != x) parent[x] = find(parent[x]);\n        return parent[x];\n    }\n    \n    bool unite(int x, int y) {\n        int root_x = find(x);\n        int root_y = find(y);\n        if (root_x == root_y) return false;\n        if (rank[root_x] < rank[root_y]) swap(root_x, root_y);\n        parent[root_y] = root_x;\n        if (rank[root_x] == rank[root_y]) rank[root_x]++;\n        return true;\n    }\n};\n\n// Calculate total cost for a solution\nll calculate_total_cost(const vector<int>& P_values, const vector<bool>& B_values) {\n    ll cost = 0;\n    for (int i = 0; i < N; i++) {\n        cost += (ll)P_values[i] * P_values[i];\n    }\n    for (int i = 0; i < M; i++) {\n        if (B_values[i]) {\n            cost += get<2>(edges[i]);\n        }\n    }\n    return cost;\n}\n\n// Check if all residents are covered\nbool check_coverage(const vector<int>& P_values, const vector<bool>& vertex_active) {\n    // Build connectivity graph\n    UnionFind uf(N);\n    for (int i = 0; i < M; i++) {\n        if (B[i]) {\n            int u = get<0>(edges[i]) - 1;\n            int v = get<1>(edges[i]) - 1;\n            uf.unite(u, v);\n        }\n    }\n    \n    // Check if vertex 0 is connected to active vertices\n    int root_0 = uf.find(0);\n    \n    // For each resident, check if it can be covered\n    for (int r = 0; r < K; r++) {\n        bool covered = false;\n        auto options = find_close_vertices(residents[r].first, residents[r].second, 5000);\n        for (auto& [vertex, distance] : options) {\n            if (vertex_active[vertex] && uf.find(vertex) == root_0 && distance <= P_values[vertex]) {\n                covered = true;\n                break;\n            }\n        }\n        if (!covered) return false;\n    }\n    return true;\n}\n\n// Improved clustering approach\nvoid solve() {\n    // Initialize\n    fill(P, P + N, 0);\n    fill(B, B + M, 0);\n    \n    // Build adjacency information\n    vector<vector<pair<int, int>>> adj(N); // {neighbor, edge_weight}\n    for (int i = 0; i < M; i++) {\n        int u = get<0>(edges[i]) - 1;\n        int v = get<1>(edges[i]) - 1;\n        int w = get<2>(edges[i]);\n        adj[u].push_back({v, w});\n        adj[v].push_back({u, w});\n    }\n    \n    // Strategy: Cluster residents and assign to optimal vertices\n    \n    // Step 1: For each resident, find close vertices\n    vector<vector<pair<int, int>>> resident_options(K); // {vertex, distance}\n    for (int r = 0; r < K; r++) {\n        resident_options[r] = find_close_vertices(residents[r].first, residents[r].second, 3000);\n    }\n    \n    // Step 2: Assign residents to vertices (greedy approach)\n    vector<vector<int>> vertex_residents(N);\n    vector<int> resident_assignment(K, -1);\n    vector<bool> assigned(K, false);\n    \n    // Priority queue: {benefit/cost_ratio, resident_index, vertex_index}\n    priority_queue<tuple<ld, int, int>> assignment_queue;\n    \n    for (int r = 0; r < K; r++) {\n        for (auto& [vertex, distance] : resident_options[r]) {\n            if (distance <= 3000) {\n                ld benefit = 1.0;\n                ld cost = (ld)(distance * distance); // P^2 cost approximation\n                ld ratio = benefit / (cost + 1e-9);\n                assignment_queue.push({ratio, r, vertex});\n            }\n        }\n    }\n    \n    // Greedy assignment\n    while (!assignment_queue.empty()) {\n        auto [ratio, r, v] = assignment_queue.top();\n        assignment_queue.pop();\n        \n        if (!assigned[r]) {\n            resident_assignment[r] = v;\n            vertex_residents[v].push_back(r);\n            assigned[r] = true;\n        }\n    }\n    \n    // Handle unassigned residents\n    for (int r = 0; r < K; r++) {\n        if (!assigned[r]) {\n            if (!resident_options[r].empty()) {\n                resident_assignment[r] = resident_options[r][0].first;\n                vertex_residents[resident_options[r][0].first].push_back(r);\n            } else {\n                // Assign to closest vertex regardless of distance\n                int best_vertex = 0;\n                int min_dist = dist_rounded(residents[r].first, residents[r].second, \n                                          vertices[0].first, vertices[0].second);\n                for (int i = 1; i < N; i++) {\n                    int d = dist_rounded(residents[r].first, residents[r].second,\n                                       vertices[i].first, vertices[i].second);\n                    if (d < min_dist) {\n                        min_dist = d;\n                        best_vertex = i;\n                    }\n                }\n                resident_assignment[r] = best_vertex;\n                vertex_residents[best_vertex].push_back(r);\n            }\n        }\n    }\n    \n    // Step 3: Determine which vertices to activate\n    vector<bool> vertex_active(N, false);\n    for (int i = 0; i < N; i++) {\n        if (!vertex_residents[i].empty()) {\n            vertex_active[i] = true;\n        }\n    }\n    \n    // Always activate vertex 0 (AtCoder office)\n    vertex_active[0] = true;\n    \n    // Step 4: Calculate required output strengths\n    vector<int> P_temp(N, 0);\n    for (int i = 0; i < N; i++) {\n        if (vertex_active[i]) {\n            int max_radius = 0;\n            for (int res_idx : vertex_residents[i]) {\n                int d = dist_rounded(vertices[i].first, vertices[i].second,\n                                   residents[res_idx].first, residents[res_idx].second);\n                max_radius = max(max_radius, d);\n            }\n            P_temp[i] = min(max_radius, 5000);\n        }\n    }\n    \n    // Step 5: Build minimum cost network using Prim's algorithm\n    vector<bool> in_tree(N, false);\n    vector<ll> min_edge_cost(N, 1e18);\n    vector<int> parent(N, -1);\n    \n    // Start from vertex 0\n    in_tree[0] = true;\n    min_edge_cost[0] = 0;\n    \n    priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;\n    \n    // Initialize edges from vertex 0\n    for (auto& [v, w] : adj[0]) {\n        if (vertex_active[v]) {\n            if (w < min_edge_cost[v]) {\n                min_edge_cost[v] = w;\n                parent[v] = 0;\n                pq.push({w, v});\n            }\n        }\n    }\n    \n    // Prim's algorithm\n    while (!pq.empty()) {\n        auto [cost, u] = pq.top(); pq.pop();\n        if (in_tree[u]) continue;\n        \n        in_tree[u] = true;\n        \n        // Add edges from u\n        for (auto& [v, w] : adj[u]) {\n            if (!in_tree[v] && vertex_active[v]) {\n                if (w < min_edge_cost[v]) {\n                    min_edge_cost[v] = w;\n                    parent[v] = u;\n                    pq.push({w, v});\n                }\n            }\n        }\n    }\n    \n    // Mark edges in MST\n    vector<bool> B_temp(M, false);\n    for (int i = 0; i < N; i++) {\n        if (parent[i] != -1) {\n            // Find the edge between i and parent[i]\n            for (int e = 0; e < M; e++) {\n                int u = get<0>(edges[e]) - 1;\n                int v = get<1>(edges[e]) - 1;\n                if ((u == i && v == parent[i]) || (u == parent[i] && v == i)) {\n                    B_temp[e] = true;\n                    break;\n                }\n            }\n        }\n    }\n    \n    // Apply results\n    for (int i = 0; i < N; i++) {\n        P[i] = P_temp[i];\n    }\n    for (int i = 0; i < M; i++) {\n        B[i] = B_temp[i] ? 1 : 0;\n    }\n    \n    // Final validation and adjustment\n    // Ensure vertex 0 has connectivity\n    bool vertex_0_connected = false;\n    for (int i = 0; i < M; i++) {\n        if (B[i]) {\n            int u = get<0>(edges[i]) - 1;\n            int v = get<1>(edges[i]) - 1;\n            if (u == 0 || v == 0) {\n                vertex_0_connected = true;\n                break;\n            }\n        }\n    }\n    \n    // If vertex 0 is isolated but has residents assigned, connect it\n    if (!vertex_0_connected && P[0] > 0) {\n        // Connect to the closest active vertex with smallest edge weight\n        ll min_edge_weight = 1e18;\n        int best_edge = -1;\n        for (int i = 0; i < M; i++) {\n            int u = get<0>(edges[i]) - 1;\n            int v = get<1>(edges[i]) - 1;\n            int w = get<2>(edges[i]);\n            if ((u == 0 && vertex_active[v]) || (v == 0 && vertex_active[u])) {\n                if (w < min_edge_weight) {\n                    min_edge_weight = w;\n                    best_edge = i;\n                }\n            }\n        }\n        if (best_edge != -1) {\n            B[best_edge] = 1;\n        }\n    }\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    cin >> N >> M >> K;\n    \n    for (int i = 0; i < N; i++) {\n        cin >> vertices[i].first >> vertices[i].second;\n    }\n    \n    for (int i = 0; i < M; i++) {\n        int u, v, w;\n        cin >> u >> v >> w;\n        edges[i] = {u, v, w};\n    }\n    \n    for (int i = 0; i < K; i++) {\n        cin >> residents[i].first >> residents[i].second;\n    }\n    \n    solve();\n    \n    for (int i = 0; i < N; i++) {\n        cout << P[i];\n        if (i + 1 < N) cout << \" \";\n    }\n    cout << \"\\n\";\n    \n    for (int i = 0; i < M; i++) {\n        cout << B[i];\n        if (i + 1 < M) cout << \" \";\n    }\n    cout << \"\\n\";\n    \n    return 0;\n}","ahc021":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 30;\nconst int MAX_OPS = 10000;\n\nint pyramid[N][N];\nvector<tuple<int, int, int, int>> operations;\n\n// Check if coordinates are valid\nbool valid(int x, int y) {\n    return x >= 0 && x < N && y >= 0 && y <= x;\n}\n\n// Perform a swap and record operation\nvoid performSwap(int x1, int y1, int x2, int y2) {\n    if (operations.size() >= MAX_OPS) return;\n    \n    swap(pyramid[x1][y1], pyramid[x2][y2]);\n    operations.push_back({x1, y1, x2, y2});\n}\n\n// Count violations in current state\nint countViolations() {\n    int violations = 0;\n    for (int x = 0; x < N - 1; x++) {\n        for (int y = 0; y <= x; y++) {\n            if (pyramid[x][y] > pyramid[x+1][y]) violations++;\n            if (pyramid[x][y] > pyramid[x+1][y+1]) violations++;\n        }\n    }\n    return violations;\n}\n\n// Get list of all violations with details\nvector<tuple<int, int, int, int>> getAllViolations() {\n    vector<tuple<int, int, int, int>> violations; // (parent_x, parent_y, child_x, child_y)\n    for (int x = 0; x < N - 1; x++) {\n        for (int y = 0; y <= x; y++) {\n            if (pyramid[x][y] > pyramid[x+1][y]) {\n                violations.push_back({x, y, x+1, y});\n            }\n            if (pyramid[x][y] > pyramid[x+1][y+1]) {\n                violations.push_back({x, y, x+1, y+1});\n            }\n        }\n    }\n    return violations;\n}\n\n// Ultra-smart heap optimization\nvoid ultraSmartHeapOptimization() {\n    // Multiple rounds with different strategies\n    for (int round = 0; round < 12 && operations.size() < MAX_OPS - 50; round++) {\n        int violationsAtStart = countViolations();\n        if (violationsAtStart == 0) return; // Already perfect!\n        \n        // Strategy 1: Bottom-up fixing (levels N-2 down to 0)\n        if (round % 3 != 2) { // Do this in 2 out of 3 rounds\n            for (int level = N - 2; level >= 0 && operations.size() < MAX_OPS - 10; level--) {\n                for (int pos = 0; pos <= level && operations.size() < MAX_OPS - 10; pos++) {\n                    if (level + 1 < N) {\n                        // Check and fix violations\n                        bool violation1 = (pos <= level && pyramid[level][pos] > pyramid[level+1][pos]);\n                        bool violation2 = (pos + 1 <= level + 1 && pyramid[level][pos] > pyramid[level+1][pos+1]);\n                        \n                        // Fix with preference for smaller child\n                        if (violation1 && violation2) {\n                            if (pyramid[level+1][pos] <= pyramid[level+1][pos+1]) {\n                                performSwap(level, pos, level+1, pos);\n                            } else {\n                                performSwap(level, pos, level+1, pos+1);\n                            }\n                        } else if (violation1) {\n                            performSwap(level, pos, level+1, pos);\n                        } else if (violation2) {\n                            performSwap(level, pos, level+1, pos+1);\n                        }\n                    }\n                }\n            }\n        }\n        \n        // Strategy 2: Severity-based fixing\n        else {\n            auto violations = getAllViolations();\n            if (!violations.empty()) {\n                // Sort by severity (value difference)\n                sort(violations.begin(), violations.end(), [&](const auto& a, const auto& b) {\n                    int ax, ay, cx, cy;\n                    tie(ax, ay, cx, cy) = a;\n                    int diffA = pyramid[ax][ay] - pyramid[cx][cy];\n                    \n                    tie(ax, ay, cx, cy) = b;\n                    int diffB = pyramid[ax][ay] - pyramid[cx][cy];\n                    \n                    return diffA > diffB;\n                });\n                \n                // Fix the worst violations\n                for (int i = 0; i < min(100, (int)violations.size()) && operations.size() < MAX_OPS - 5; i++) {\n                    int px, py, cx, cy;\n                    tie(px, py, cx, cy) = violations[i];\n                    if (px < N && py <= px && cx < N && cy <= cx) {\n                        if (pyramid[px][py] > pyramid[cx][cy]) {\n                            performSwap(px, py, cx, cy);\n                        }\n                    }\n                }\n            }\n        }\n        \n        // Check progress\n        int violationsAtEnd = countViolations();\n        if (violationsAtEnd == 0) return; // Perfect!\n        \n        // If no progress, try more aggressive approach\n        if (violationsAtEnd >= violationsAtStart && round > 5) {\n            // Randomize some positions to escape local minima\n            if (operations.size() < MAX_OPS - 100) {\n                for (int randomAttempt = 0; randomAttempt < 20 && operations.size() < MAX_OPS - 5; randomAttempt++) {\n                    int x = rand() % (N - 1);\n                    int y = rand() % (x + 1);\n                    if (x + 1 < N) {\n                        // Swap with a random child\n                        if (rand() % 2 == 0) {\n                            performSwap(x, y, x+1, y);\n                        } else {\n                            performSwap(x, y, x+1, y+1);\n                        }\n                    }\n                }\n            }\n        }\n    }\n}\n\n// Systematic perfection drive\nvoid systematicPerfectionDrive() {\n    if (operations.size() < MAX_OPS - 200) {\n        // Multiple systematic passes\n        for (int pass = 0; pass < 6 && operations.size() < MAX_OPS - 50; pass++) {\n            int violations = countViolations();\n            if (violations == 0) return; // Already perfect!\n            \n            // Different scanning patterns\n            if (pass % 2 == 0) {\n                // Normal order\n                for (int x = 0; x < N - 1 && operations.size() < MAX_OPS - 10; x++) {\n                    for (int y = 0; y <= x && operations.size() < MAX_OPS - 10; y++) {\n                        if (x < N - 1) {\n                            // Aggressive fixing - multiple attempts per position\n                            for (int attempt = 0; attempt < 3 && operations.size() < MAX_OPS - 3; attempt++) {\n                                bool violation1 = pyramid[x][y] > pyramid[x+1][y];\n                                bool violation2 = pyramid[x][y] > pyramid[x+1][y+1];\n                                \n                                if (!violation1 && !violation2) break;\n                                \n                                if (violation1 && violation2) {\n                                    if (pyramid[x+1][y] <= pyramid[x+1][y+1]) {\n                                        performSwap(x, y, x+1, y);\n                                    } else {\n                                        performSwap(x, y, x+1, y+1);\n                                    }\n                                } else if (violation1) {\n                                    performSwap(x, y, x+1, y);\n                                } else if (violation2) {\n                                    performSwap(x, y, x+1, y+1);\n                                }\n                            }\n                        }\n                    }\n                }\n            } else {\n                // Reverse order\n                for (int x = N - 2; x >= 0 && operations.size() < MAX_OPS - 10; x--) {\n                    for (int y = x; y >= 0 && operations.size() < MAX_OPS - 10; y--) {\n                        if (x < N - 1) {\n                            for (int attempt = 0; attempt < 3 && operations.size() < MAX_OPS - 3; attempt++) {\n                                bool violation1 = pyramid[x][y] > pyramid[x+1][y];\n                                bool violation2 = pyramid[x][y] > pyramid[x+1][y+1];\n                                \n                                if (!violation1 && !violation2) break;\n                                \n                                if (violation1 && violation2) {\n                                    if (pyramid[x+1][y] <= pyramid[x+1][y+1]) {\n                                        performSwap(x, y, x+1, y);\n                                    } else {\n                                        performSwap(x, y, x+1, y+1);\n                                    }\n                                } else if (violation1) {\n                                    performSwap(x, y, x+1, y);\n                                } else if (violation2) {\n                                    performSwap(x, y, x+1, y+1);\n                                }\n                            }\n                        }\n                    }\n                }\n            }\n        }\n    }\n}\n\n// Final perfection push - go all out to get E=0\nvoid finalPerfectionPush() {\n    if (operations.size() < MAX_OPS - 30) {\n        int currentViolations = countViolations();\n        if (currentViolations == 0) return; // Already perfect!\n        \n        // Ultra-aggressive approach - don't stop until perfect or out of operations\n        for (int megaRound = 0; megaRound < 4 && operations.size() < MAX_OPS - 10 && currentViolations > 0; megaRound++) {\n            // Get all current violations\n            auto violations = getAllViolations();\n            \n            if (violations.empty()) return; // Perfect!\n            \n            // Fix them all, multiple times if needed\n            for (int i = 0; i < min(50, (int)violations.size()) && operations.size() < MAX_OPS - 5; i++) {\n                int px, py, cx, cy;\n                tie(px, py, cx, cy) = violations[i];\n                \n                if (px < N && py <= px && cx < N && cy <= cx) {\n                    if (pyramid[px][py] > pyramid[cx][cy]) {\n                        performSwap(px, py, cx, cy);\n                    }\n                }\n            }\n            \n            // Check if we improved\n            int newViolations = countViolations();\n            if (newViolations == 0) return; // Perfect!\n            if (newViolations >= currentViolations) {\n                // Try random swaps to escape local minima\n                if (operations.size() < MAX_OPS - 50) {\n                    for (int randAttempt = 0; randAttempt < 10 && operations.size() < MAX_OPS - 3; randAttempt++) {\n                        int x = rand() % (N - 1);\n                        int y = rand() % (x + 1);\n                        if (x + 1 < N) {\n                            if (rand() % 2 == 0 && y <= x + 1) {\n                                performSwap(x, y, x+1, y);\n                            } else if (y + 1 <= x + 2) {\n                                performSwap(x, y, x+1, y+1);\n                            }\n                        }\n                    }\n                }\n            }\n            currentViolations = newViolations;\n        }\n    }\n}\n\nint main() {\n    // Initialize random seed\n    srand(time(nullptr));\n    \n    // Read input\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j <= i; j++) {\n            cin >> pyramid[i][j];\n        }\n    }\n    \n    // Get initial state\n    int initialViolations = countViolations();\n    \n    // Apply ultra-smart optimization\n    ultraSmartHeapOptimization();\n    \n    // Systematic perfection drive\n    if (operations.size() < MAX_OPS - 100) {\n        systematicPerfectionDrive();\n    }\n    \n    // Final perfection push\n    if (operations.size() < MAX_OPS - 20) {\n        finalPerfectionPush();\n    }\n    \n    // Output results\n    cout << operations.size() << \"\\n\";\n    for (auto& op : operations) {\n        cout << get<0>(op) << \" \" << get<1>(op) << \" \" << get<2>(op) << \" \" << get<3>(op) << \"\\n\";\n    }\n    \n    return 0;\n}","toyota2023summer-final":"#include <bits/stdc++.h>\n#include <atcoder/all>\nusing namespace std;\nusing namespace atcoder;\n\nconst int D = 9;\nconst int INF = 100000000;\n\nint entrance_i = 0;\nint entrance_j = 4;\n\nvector<vector<bool>> is_obstacle(D, vector<bool>(D, false));\nvector<vector<int>> container_id(D, vector<int>(D, -1));\nvector<pair<int, int>> container_positions(D * D); // id -> position\nvector<int> container_numbers(D * D); // position index -> container number\n\n// Directions: up, down, left, right\nconst int di[4] = {-1, 1, 0, 0};\nconst int dj[4] = {0, 0, -1, 1};\n\n// Check if position is valid\nbool is_valid(int i, int j) {\n    return i >= 0 && i < D && j >= 0 && j < D;\n}\n\n// Check if a position is the entrance or adjacent to entrance\nbool is_entrance_area(int i, int j) {\n    if (i == 0 && j == 4) return true;\n    if (i == 0 && j == 3) return true;\n    if (i == 0 && j == 5) return true;\n    if (i == 1 && j == 4) return true;\n    return false;\n}\n\n// BFS to find all reachable positions from entrance\nvector<vector<int>> get_distances_from_entrance() {\n    vector<vector<int>> dist(D, vector<int>(D, INF));\n    queue<pair<int, int>> q;\n    \n    // Start from entrance\n    dist[0][4] = 0;\n    q.push({0, 4});\n    \n    while (!q.empty()) {\n        auto [ci, cj] = q.front(); q.pop();\n        \n        for (int d = 0; d < 4; d++) {\n            int ni = ci + di[d];\n            int nj = cj + dj[d];\n            \n            if (is_valid(ni, nj) && !is_obstacle[ni][nj] && container_id[ni][nj] == -1) {\n                if (dist[ni][nj] > dist[ci][cj] + 1) {\n                    dist[ni][nj] = dist[ci][cj] + 1;\n                    q.push({ni, nj});\n                }\n            }\n        }\n    }\n    \n    return dist;\n}\n\n// Find all reachable empty positions\nvector<pair<int, int>> get_reachable_empty_positions() {\n    vector<vector<bool>> visited(D, vector<bool>(D, false));\n    vector<pair<int, int>> result;\n    queue<pair<int, int>> q;\n    \n    // Start from entrance\n    visited[0][4] = true;\n    q.push({0, 4});\n    if (container_id[0][4] == -1 && !is_obstacle[0][4]) {\n        result.push_back({0, 4});\n    }\n    \n    while (!q.empty()) {\n        auto [ci, cj] = q.front(); q.pop();\n        \n        for (int d = 0; d < 4; d++) {\n            int ni = ci + di[d];\n            int nj = cj + dj[d];\n            \n            if (is_valid(ni, nj) && !visited[ni][nj] && !is_obstacle[ni][nj]) {\n                visited[ni][nj] = true;\n                q.push({ni, nj});\n                if (container_id[ni][nj] == -1) {\n                    result.push_back({ni, nj});\n                }\n            }\n        }\n    }\n    \n    return result;\n}\n\n// Calculate Manhattan distance from entrance\nint manhattan_distance(int i, int j) {\n    return abs(i - 0) + abs(j - 4);\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int N;\n    cin >> D >> N;\n    \n    for (int i = 0; i < N; i++) {\n        int ri, rj;\n        cin >> ri >> rj;\n        is_obstacle[ri][rj] = true;\n    }\n    \n    int total_containers = D * D - 1 - N;\n    vector<int> transport_order(total_containers);\n    \n    // Process each container as it arrives\n    for (int d = 0; d < total_containers; d++) {\n        int t_d;\n        cin >> t_d;\n        transport_order[d] = t_d;\n        \n        // Get all reachable empty positions\n        auto reachable_positions = get_reachable_empty_positions();\n        \n        // Remove entrance area from consideration (except if it's the only option)\n        vector<pair<int, int>> valid_positions;\n        for (auto& pos : reachable_positions) {\n            if (!is_entrance_area(pos.first, pos.second)) {\n                valid_positions.push_back(pos);\n            }\n        }\n        \n        // If no valid positions except entrance area, use entrance area\n        if (valid_positions.empty()) {\n            for (auto& pos : reachable_positions) {\n                if (pos.first != 0 || pos.second != 4) { // exclude exact entrance\n                    valid_positions.push_back(pos);\n                }\n            }\n        }\n        \n        // If still empty, just use entrance\n        if (valid_positions.empty()) {\n            valid_positions.push_back({0, 4});\n        }\n        \n        // Choose position based on container number\n        // Lower numbers should be placed closer to entrance for easier retrieval\n        pair<int, int> best_position = valid_positions[0];\n        int best_score = INF;\n        \n        for (auto& pos : valid_positions) {\n            int dist = manhattan_distance(pos.first, pos.second);\n            // Prefer positions closer to entrance for lower numbered containers\n            // But also add some randomness to avoid getting stuck\n            int score = dist * 1000 + (t_d * 10) + (rand() % 100);\n            \n            if (score < best_score) {\n                best_score = score;\n                best_position = pos;\n            }\n        }\n        \n        // Place the container\n        int pi = best_position.first;\n        int pj = best_position.second;\n        container_id[pi][pj] = d;\n        container_positions[d] = {pi, pj};\n        container_numbers[d] = t_d;\n        \n        // Output the position\n        cout << pi << \" \" << pj << endl;\n        cout.flush();\n    }\n    \n    // Now determine transport order\n    // We want to transport in order of assigned numbers (0, 1, 2, ...)\n    // But we can only transport reachable containers\n    \n    vector<bool> transported(total_containers, false);\n    vector<pair<int, int>> transport_sequence;\n    \n    for (int target_number = 0; target_number < total_containers; target_number++) {\n        // Find container with target_number\n        int container_idx = -1;\n        for (int i = 0; i < total_containers; i++) {\n            if (container_numbers[i] == target_number) {\n                container_idx = i;\n                break;\n            }\n        }\n        \n        if (container_idx == -1) continue;\n        \n        // Mark this container for transport when it becomes reachable\n        pair<int, int> target_pos = container_positions[container_idx];\n        \n        // For simplicity, we'll transport containers in the order they become reachable\n        // In a more sophisticated solution, we'd use a priority queue\n        transport_sequence.push_back(target_pos);\n    }\n    \n    // Actually, let's implement a better approach:\n    // Try to transport containers in order of their numbers, but only when reachable\n    vector<bool> container_transported(total_containers, false);\n    transport_sequence.clear();\n    \n    while (transport_sequence.size() < total_containers) {\n        // Get currently reachable containers\n        auto distances = get_distances_from_entrance();\n        vector<pair<int, int>> reachable_containers;\n        vector<int> reachable_indices;\n        \n        for (int i = 0; i < total_containers; i++) {\n            if (container_transported[i]) continue;\n            \n            auto pos = container_positions[i];\n            if (distances[pos.first][pos.second] < INF) {\n                reachable_containers.push_back(pos);\n                reachable_indices.push_back(i);\n            }\n        }\n        \n        if (reachable_containers.empty()) {\n            // This shouldn't happen if the problem is solvable\n            break;\n        }\n        \n        // Among reachable containers, pick the one with lowest assigned number\n        int best_idx = reachable_indices[0];\n        int best_number = container_numbers[best_idx];\n        \n        for (int idx : reachable_indices) {\n            if (container_numbers[idx] < best_number) {\n                best_number = container_numbers[idx];\n                best_idx = idx;\n            }\n        }\n        \n        // Transport this container\n        auto pos = container_positions[best_idx];\n        transport_sequence.push_back(pos);\n        container_transported[best_idx] = true;\n        \n        // Clear the position\n        container_id[pos.first][pos.second] = -1;\n    }\n    \n    // Output transport sequence\n    for (auto& pos : transport_sequence) {\n        cout << pos.first << \" \" << pos.second << \"\\n\";\n    }\n    \n    return 0;\n}","ahc024":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 50;\nconst int M = 100;\n\nint grid[N][N];\nint result[N][N];\nvector<pair<int,int>> positions[M+1]; // positions for each color\nvector<int> adj_list[M+1]; // adjacency list\nbool adj_matrix[M+1][M+1]; // adjacency matrix\n\n// Directions: up, right, down, left\nconst int dx[4] = {-1, 0, 1, 0};\nconst int dy[4] = {0, 1, 0, -1};\n\nvoid extract_adjacencies() {\n    // Initialize adjacency matrix\n    memset(adj_matrix, 0, sizeof(adj_matrix));\n    \n    // Check adjacencies in the original grid\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            int color = grid[i][j];\n            for (int d = 0; d < 4; d++) {\n                int ni = i + dx[d];\n                int nj = j + dy[d];\n                int neighbor_color = 0; // outside is color 0\n                if (ni >= 0 && ni < N && nj >= 0 && nj < N) {\n                    neighbor_color = grid[ni][nj];\n                }\n                if (color != neighbor_color) {\n                    adj_matrix[color][neighbor_color] = true;\n                    adj_matrix[neighbor_color][color] = true;\n                }\n            }\n        }\n    }\n    \n    // Build adjacency lists\n    for (int i = 0; i <= M; i++) {\n        adj_list[i].clear();\n        for (int j = 0; j <= M; j++) {\n            if (adj_matrix[i][j] && i != j) {\n                adj_list[i].push_back(j);\n            }\n        }\n    }\n}\n\n// Check if we can place a color at position (x,y)\nbool can_place(int x, int y, int color) {\n    if (x < 0 || x >= N || y < 0 || y >= N) return false;\n    return result[x][y] == 0;\n}\n\n// Check if placement maintains adjacency requirements\nbool check_adjacency(int x, int y, int color) {\n    // Check if this placement creates required adjacencies\n    for (int d = 0; d < 4; d++) {\n        int nx = x + dx[d];\n        int ny = y + dy[d];\n        int neighbor = 0;\n        if (nx >= 0 && nx < N && ny >= 0 && ny < N) {\n            neighbor = result[nx][ny];\n        }\n        \n        // If neighbor is a color that should be adjacent, that's good\n        // If neighbor is a color that shouldn't be adjacent, that's bad (but we can't know all adjacencies yet)\n    }\n    return true;\n}\n\nvoid place_color_compactly(int color) {\n    if (color == 0) return; // Don't place color 0, it's empty space\n    \n    // If this color is already placed, skip\n    if (!positions[color].empty()) return;\n    \n    // Find a good starting position - near already placed colors that it should be adjacent to\n    vector<pair<int,int>> candidates;\n    \n    // If no colors placed yet, start in the middle\n    if (positions[1].empty()) {\n        int cx = N/2, cy = N/2;\n        result[cx][cy] = color;\n        positions[color].push_back({cx, cy});\n        return;\n    }\n    \n    // Try to place near adjacencies\n    bool placed = false;\n    for (int adj_color : adj_list[color]) {\n        if (!positions[adj_color].empty()) {\n            // Try to place near this adjacent color\n            for (auto& pos : positions[adj_color]) {\n                int x = pos.first, y = pos.second;\n                for (int d = 0; d < 4; d++) {\n                    int nx = x + dx[d];\n                    int ny = y + dy[d];\n                    if (nx >= 0 && nx < N && ny >= 0 && ny < N && result[nx][ny] == 0) {\n                        result[nx][ny] = color;\n                        positions[color].push_back({nx, ny});\n                        placed = true;\n                        break;\n                    }\n                }\n                if (placed) break;\n            }\n            if (placed) break;\n        }\n    }\n    \n    // If couldn't place near adjacencies, place anywhere available\n    if (!placed) {\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                if (result[i][j] == 0) {\n                    result[i][j] = color;\n                    positions[color].push_back({i, j});\n                    placed = true;\n                    break;\n                }\n            }\n            if (placed) break;\n        }\n    }\n    \n    // Expand the color to make it connected\n    if (!positions[color].empty()) {\n        // Simple expansion to make it connected - just place one tile\n        // In a more sophisticated solution, we'd expand to match the required size\n        // But since we're optimizing for fewer empty tiles, smaller is better\n    }\n}\n\n// Greedy approach: place colors in order of how many adjacencies they have\nvoid solve() {\n    // Initialize result grid\n    memset(result, 0, sizeof(result));\n    for (int i = 0; i <= M; i++) {\n        positions[i].clear();\n    }\n    \n    // Create ordering based on adjacency count (place high-degree nodes first)\n    vector<pair<int, int>> order; // {adjacency_count, color}\n    for (int i = 1; i <= M; i++) {\n        order.push_back({(int)adj_list[i].size(), i});\n    }\n    sort(order.rbegin(), order.rend()); // descending order\n    \n    // Place colors\n    for (auto& p : order) {\n        place_color_compactly(p.second);\n    }\n    \n    // Ensure connectivity for each color\n    for (int color = 1; color <= M; color++) {\n        if (positions[color].empty()) {\n            // Place at least one tile\n            for (int i = 0; i < N; i++) {\n                for (int j = 0; j < N; j++) {\n                    if (result[i][j] == 0) {\n                        result[i][j] = color;\n                        positions[color].push_back({i, j});\n                        goto placed;\n                    }\n                }\n            }\n            placed:;\n        }\n    }\n}\n\n// Better approach: create a compact layout\nvoid solve_better() {\n    // Initialize result grid\n    memset(result, 0, sizeof(result));\n    for (int i = 0; i <= M; i++) {\n        positions[i].clear();\n    }\n    \n    // Start by placing color 1 in the center\n    int center_x = N/2, center_y = N/2;\n    result[center_x][center_y] = 1;\n    positions[1].push_back({center_x, center_y});\n    \n    // Place other colors in a spiral pattern to minimize empty space\n    vector<pair<int,int>> spiral_dirs = {{0,1}, {1,0}, {0,-1}, {-1,0}}; // right, down, left, up\n    int dir = 0;\n    int x = center_x, y = center_y;\n    int steps = 1;\n    \n    vector<int> colors_to_place;\n    for (int i = 2; i <= M; i++) {\n        colors_to_place.push_back(i);\n    }\n    \n    int color_idx = 0;\n    while (color_idx < (int)colors_to_place.size()) {\n        for (int step = 0; step < 2; step++) {\n            for (int i = 0; i < steps && color_idx < (int)colors_to_place.size(); i++) {\n                x += spiral_dirs[dir].first;\n                y += spiral_dirs[dir].second;\n                \n                if (x >= 0 && x < N && y >= 0 && y < N && result[x][y] == 0) {\n                    int color = colors_to_place[color_idx];\n                    result[x][y] = color;\n                    positions[color].push_back({x, y});\n                    color_idx++;\n                }\n                \n                if (color_idx >= (int)colors_to_place.size()) break;\n            }\n            dir = (dir + 1) % 4;\n            if (color_idx >= (int)colors_to_place.size()) break;\n        }\n        steps++;\n    }\n    \n    // This approach doesn't preserve adjacencies, so let's try a different approach\n}\n\nvoid solve_best() {\n    // Initialize result grid\n    memset(result, 0, sizeof(result));\n    for (int i = 0; i <= M; i++) {\n        positions[i].clear();\n    }\n    \n    // Simple but effective: place colors in a dense grid pattern\n    int idx = 0;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (idx < M) {\n                int color = idx + 1;\n                result[i][j] = color;\n                positions[color].push_back({i, j});\n                idx++;\n            } else {\n                // Fill remaining with a pattern to maintain some adjacencies\n                result[i][j] = 0;\n            }\n        }\n    }\n    \n    // This still doesn't preserve the original adjacencies\n    // Let's try a more sophisticated approach\n}\n\n// Let's go with a simple but effective approach\nvoid solve_simple() {\n    // Initialize result grid\n    memset(result, 0, sizeof(result));\n    for (int i = 0; i <= M; i++) {\n        positions[i].clear();\n    }\n    \n    // Place colors in a way that tries to preserve adjacencies\n    // Place first color in center\n    result[N/2][N/2] = 1;\n    positions[1].push_back({N/2, N/2});\n    \n    // For remaining colors, place them near colors they should be adjacent to\n    vector<bool> placed(M+1, false);\n    placed[0] = true; // color 0 is special\n    placed[1] = true;\n    \n    queue<int> to_process;\n    to_process.push(1);\n    \n    while (!to_process.empty()) {\n        int current_color = to_process.front();\n        to_process.pop();\n        \n        // Try to place adjacent colors\n        for (int adj_color : adj_list[current_color]) {\n            if (!placed[adj_color] && adj_color != 0) {\n                // Try to place adj_color near current_color\n                bool placed_adj = false;\n                for (auto& pos : positions[current_color]) {\n                    int x = pos.first, y = pos.second;\n                    // Try 8 directions (including diagonals for more flexibility)\n                    for (int dx_val = -1; dx_val <= 1; dx_val++) {\n                        for (int dy_val = -1; dy_val <= 1; dy_val++) {\n                            if (dx_val == 0 && dy_val == 0) continue;\n                            int nx = x + dx_val;\n                            int ny = y + dy_val;\n                            if (nx >= 0 && nx < N && ny >= 0 && ny < N && result[nx][ny] == 0) {\n                                result[nx][ny] = adj_color;\n                                positions[adj_color].push_back({nx, ny});\n                                placed[adj_color] = true;\n                                to_process.push(adj_color);\n                                placed_adj = true;\n                                break;\n                            }\n                        }\n                        if (placed_adj) break;\n                    }\n                    if (placed_adj) break;\n                }\n                \n                // If we couldn't place it near an adjacent color, place it anywhere\n                if (!placed_adj) {\n                    for (int i = 0; i < N; i++) {\n                        for (int j = 0; j < N; j++) {\n                            if (result[i][j] == 0) {\n                                result[i][j] = adj_color;\n                                positions[adj_color].push_back({i, j});\n                                placed[adj_color] = true;\n                                to_process.push(adj_color);\n                                placed_adj = true;\n                                break;\n                            }\n                        }\n                        if (placed_adj) break;\n                    }\n                }\n            }\n        }\n    }\n    \n    // Place any remaining unplaced colors\n    for (int color = 1; color <= M; color++) {\n        if (!placed[color]) {\n            for (int i = 0; i < N; i++) {\n                for (int j = 0; j < N; j++) {\n                    if (result[i][j] == 0) {\n                        result[i][j] = color;\n                        positions[color].push_back({i, j});\n                        placed[color] = true;\n                        break;\n                    }\n                }\n                if (placed[color]) break;\n            }\n        }\n    }\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    int n, m;\n    cin >> n >> m;\n    \n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            cin >> grid[i][j];\n        }\n    }\n    \n    extract_adjacencies();\n    solve_simple();\n    \n    // Output result\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            if (j > 0) cout << \" \";\n            cout << result[i][j];\n        }\n        cout << \"\\n\";\n    }\n    \n    return 0;\n}","ahc025":"#include <bits/stdc++.h>\n\nusing namespace std;\n\nvoid solve() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    // Read input\n    int N, D, Q;\n    cin >> N >> D >> Q;\n    \n    // Initialize data structures\n    vector<double> weight_estimates(N, 1.0);\n    vector<vector<int>> comparison_count(N, vector<int>(N, 0)); // How many times i compared with j\n    vector<vector<int>> win_count(N, vector<int>(N, 0)); // How many times i won against j\n    \n    // Store queries for processing\n    vector<tuple<int, int, string>> single_comparisons; // {item1, item2, result}\n    \n    // Query strategy: systematic exploration\n    int queries_used = 0;\n    \n    // First, do round-robin comparisons for initial estimates\n    for (int i = 0; i < N && queries_used < Q/3; i++) {\n        for (int j = i + 1; j < N && queries_used < Q/3; j++) {\n            if (queries_used >= Q) break;\n            \n            cout << \"1 1 \" << i << \" \" << j << endl;\n            cout.flush();\n            \n            string response;\n            cin >> response;\n            \n            single_comparisons.push_back({i, j, response});\n            queries_used++;\n            \n            // Update comparison records\n            comparison_count[i][j]++;\n            comparison_count[j][i]++;\n            \n            if (response == \">\") {\n                win_count[i][j]++;\n            } else if (response == \"<\") {\n                win_count[j][i]++;\n            }\n        }\n        if (queries_used >= Q) break;\n    }\n    \n    // Then do strategic comparisons based on what we know\n    while (queries_used < Q) {\n        // Find the most informative pair to compare\n        int best_i = -1, best_j = -1;\n        int min_comparisons = 1000000;\n        \n        // Look for pairs we haven't compared much\n        for (int i = 0; i < N; i++) {\n            for (int j = i + 1; j < N; j++) {\n                if (comparison_count[i][j] < min_comparisons) {\n                    min_comparisons = comparison_count[i][j];\n                    best_i = i;\n                    best_j = j;\n                }\n            }\n        }\n        \n        // If we found a pair to compare\n        if (best_i != -1 && best_j != -1) {\n            cout << \"1 1 \" << best_i << \" \" << best_j << endl;\n            cout.flush();\n            \n            string response;\n            cin >> response;\n            \n            single_comparisons.push_back({best_i, best_j, response});\n            queries_used++;\n            \n            // Update records\n            comparison_count[best_i][best_j]++;\n            comparison_count[best_j][best_i]++;\n            \n            if (response == \">\") {\n                win_count[best_i][best_j]++;\n            } else if (response == \"<\") {\n                win_count[best_j][best_i]++;\n            }\n        } else {\n            // Fallback: compare random pair\n            int i = rand() % N;\n            int j = rand() % N;\n            if (i != j) {\n                // Ensure i < j for consistency\n                if (i > j) swap(i, j);\n                \n                cout << \"1 1 \" << i << \" \" << j << endl;\n                cout.flush();\n                \n                string response;\n                cin >> response;\n                \n                single_comparisons.push_back({i, j, response});\n                queries_used++;\n                \n                comparison_count[i][j]++;\n                comparison_count[j][i]++;\n                \n                if (response == \">\") {\n                    win_count[i][j]++;\n                } else if (response == \"<\") {\n                    win_count[j][i]++;\n                }\n            } else {\n                queries_used++; // consume query to avoid infinite loop\n            }\n        }\n    }\n    \n    // Estimate weights based on comparison results\n    vector<pair<double, int>> item_scores(N);\n    for (int i = 0; i < N; i++) {\n        item_scores[i].second = i;\n        double total_score = 0;\n        int total_comparisons = 0;\n        \n        for (int j = 0; j < N; j++) {\n            if (i != j && comparison_count[i][j] > 0) {\n                double win_rate = (double)win_count[i][j] / comparison_count[i][j];\n                total_score += win_rate * comparison_count[i][j];\n                total_comparisons += comparison_count[i][j];\n            }\n        }\n        \n        if (total_comparisons > 0) {\n            item_scores[i].first = total_score / total_comparisons;\n        } else {\n            item_scores[i].first = 0.5; // neutral score\n        }\n    }\n    \n    // Convert scores to weights\n    double min_score = 1e9, max_score = -1e9;\n    for (const auto& score_pair : item_scores) {\n        min_score = min(min_score, score_pair.first);\n        max_score = max(max_score, score_pair.first);\n    }\n    \n    double score_range = max_score - min_score;\n    if (score_range > 1e-9) {\n        for (int i = 0; i < N; i++) {\n            weight_estimates[i] = (item_scores[i].first - min_score) / score_range;\n        }\n    } else {\n        for (int i = 0; i < N; i++) {\n            weight_estimates[i] = 0.5;\n        }\n    }\n    \n    // Ensure no zero weights\n    for (int i = 0; i < N; i++) {\n        if (weight_estimates[i] < 1e-9) weight_estimates[i] = 1e-9;\n    }\n    \n    // Normalize weights\n    double sum_weights = 0;\n    for (double w : weight_estimates) sum_weights += w;\n    if (sum_weights > 1e-9) {\n        for (double& w : weight_estimates) w = w * N / sum_weights;\n    }\n    \n    // Simple greedy partitioning - each item assigned exactly once\n    vector<int> assignment(N, 0);\n    vector<double> group_sums(D, 0);\n    \n    // Sort items by weight (descending) for better initial assignment\n    vector<pair<double, int>> sorted_items;\n    for (int i = 0; i < N; i++) {\n        sorted_items.push_back({weight_estimates[i], i});\n    }\n    sort(sorted_items.begin(), sorted_items.end(), greater<pair<double, int>>());\n    \n    // Assign each item to the group with minimum current sum\n    for (const auto& [weight, item_idx] : sorted_items) {\n        // Find group with minimum sum\n        int best_group = 0;\n        for (int g = 1; g < D; g++) {\n            if (group_sums[g] < group_sums[best_group]) {\n                best_group = g;\n            }\n        }\n        \n        assignment[item_idx] = best_group;\n        group_sums[best_group] += weight;\n    }\n    \n    // Simple improvement: try pairwise swaps\n    bool improved = true;\n    int iterations = 0;\n    while (improved && iterations < 10) {\n        improved = false;\n        iterations++;\n        \n        // Try swapping each pair of items from different groups\n        for (int i = 0; i < N && !improved; i++) {\n            for (int j = i + 1; j < N && !improved; j++) {\n                int group_i = assignment[i];\n                int group_j = assignment[j];\n                \n                // Only consider if they're in different groups\n                if (group_i != group_j) {\n                    double weight_i = weight_estimates[i];\n                    double weight_j = weight_estimates[j];\n                    \n                    // Calculate variance before swap\n                    double mean_before = 0;\n                    for (int g = 0; g < D; g++) mean_before += group_sums[g];\n                    mean_before /= D;\n                    \n                    double variance_before = 0;\n                    for (int g = 0; g < D; g++) {\n                        variance_before += (group_sums[g] - mean_before) * (group_sums[g] - mean_before);\n                    }\n                    \n                    // Calculate variance after swap\n                    vector<double> temp_sums = group_sums;\n                    temp_sums[group_i] = group_sums[group_i] - weight_i + weight_j;\n                    temp_sums[group_j] = group_sums[group_j] - weight_j + weight_i;\n                    \n                    double mean_after = 0;\n                    for (int g = 0; g < D; g++) mean_after += temp_sums[g];\n                    mean_after /= D;\n                    \n                    double variance_after = 0;\n                    for (int g = 0; g < D; g++) {\n                        variance_after += (temp_sums[g] - mean_after) * (temp_sums[g] - mean_after);\n                    }\n                    \n                    // If swap improves variance, do it\n                    if (variance_after < variance_before - 1e-9) {\n                        // Actually perform the swap\n                        assignment[i] = group_j;\n                        assignment[j] = group_i;\n                        group_sums[group_i] = temp_sums[group_i];\n                        group_sums[group_j] = temp_sums[group_j];\n                        improved = true;\n                    }\n                }\n            }\n        }\n    }\n    \n    // Output result - verify each item appears exactly once\n    vector<int> count_check(N, 0);\n    for (int i = 0; i < N; i++) {\n        cout << assignment[i];\n        if (i < N - 1) cout << \" \";\n        count_check[assignment[i]]++;\n    }\n    cout << endl;\n}\n\nint main() {\n    solve();\n    return 0;\n}","ahc026":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 200;\nconst int M = 10;\nconst int STACK_SIZE = 20;\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    int n, m;\n    cin >> n >> m;\n    \n    vector<deque<int>> stacks(M);\n    vector<int> box_stack(N + 1);      // which stack each box is in\n    vector<int> box_position(N + 1);   // position in stack (0 = bottom)\n    \n    // Read initial configuration\n    for (int i = 0; i < m; i++) {\n        for (int j = 0; j < STACK_SIZE; j++) {\n            int box;\n            cin >> box;\n            stacks[i].push_back(box);\n            box_stack[box] = i;\n            box_position[box] = j;\n        }\n    }\n    \n    vector<pair<int, int>> operations;\n    \n    // Process boxes in order from 1 to 200\n    for (int target = 1; target <= n; target++) {\n        int stack_idx = box_stack[target];\n        int pos_in_stack = box_position[target];\n        int stack_size = stacks[stack_idx].size();\n        \n        // If box is not at the top, we need to move boxes above it\n        if (pos_in_stack != stack_size - 1) {\n            // Determine which boxes need to be moved\n            int boxes_above = stack_size - 1 - pos_in_stack;\n            \n            // Find a destination stack (prefer one that's not full)\n            int dest_stack = -1;\n            for (int s = 0; s < m; s++) {\n                if (s != stack_idx && (int)stacks[s].size() < STACK_SIZE) {\n                    dest_stack = s;\n                    break;\n                }\n            }\n            \n            // If all stacks are full, pick any other stack\n            if (dest_stack == -1) {\n                dest_stack = (stack_idx + 1) % m;\n            }\n            \n            // Move operation: move box 'target' and all above it to dest_stack\n            operations.push_back({target, dest_stack + 1}); // 1-indexed output\n            \n            // Actually perform the move\n            vector<int> moved_boxes;\n            for (int i = pos_in_stack; i < stack_size; i++) {\n                moved_boxes.push_back(stacks[stack_idx][i]);\n            }\n            \n            // Remove from source stack\n            stacks[stack_idx].erase(\n                stacks[stack_idx].begin() + pos_in_stack, \n                stacks[stack_idx].end()\n            );\n            \n            // Add to destination stack\n            for (int box : moved_boxes) {\n                stacks[dest_stack].push_back(box);\n            }\n            \n            // Update positions for all moved boxes\n            for (int i = 0; i < (int)stacks[dest_stack].size(); i++) {\n                box_stack[stacks[dest_stack][i]] = dest_stack;\n                box_position[stacks[dest_stack][i]] = i;\n            }\n        }\n        \n        // Now the target box should be at the top of its stack\n        // Remove it\n        int final_stack = box_stack[target];\n        stacks[final_stack].pop_back();\n        box_stack[target] = -1;\n        operations.push_back({target, 0});\n        \n        // Update positions of remaining boxes in the stack\n        for (int i = 0; i < (int)stacks[final_stack].size(); i++) {\n            box_position[stacks[final_stack][i]] = i;\n        }\n    }\n    \n    // Output operations\n    for (const auto& op : operations) {\n        cout << op.first << \" \" << op.second << \"\\n\";\n    }\n    \n    return 0;\n}","ahc027":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MAXN = 45;\nint N;\nchar h[MAXN][MAXN], v[MAXN][MAXN];\nint d[MAXN][MAXN];\nbool visited[MAXN][MAXN];\nvector<pair<int, int>> tree_edges;\nvector<char> directions = {'R', 'D', 'L', 'U'};\nvector<pair<int, int>> moves = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};\n\n// Build spanning tree using BFS\nvoid build_spanning_tree() {\n    memset(visited, 0, sizeof(visited));\n    queue<pair<int, int>> q;\n    q.push({0, 0});\n    visited[0][0] = true;\n    \n    while (!q.empty()) {\n        auto [x, y] = q.front(); q.pop();\n        \n        for (int dir = 0; dir < 4; dir++) {\n            int nx = x + moves[dir].first;\n            int ny = y + moves[dir].second;\n            \n            if (nx >= 0 && nx < N && ny >= 0 && ny < N && !visited[nx][ny]) {\n                // Check if there's no wall\n                bool can_move = false;\n                if (dir == 0) { // Right\n                    if (ny > 0) can_move = (v[x][min(y, ny-1)] == '0');\n                    else can_move = true;\n                } else if (dir == 1) { // Down\n                    if (nx > 0) can_move = (h[min(x, nx-1)][y] == '0');\n                    else can_move = true;\n                } else if (dir == 2) { // Left\n                    if (y > 0) can_move = (v[x][min(y-1, ny)] == '0');\n                    else can_move = true;\n                } else { // Up\n                    if (x > 0) can_move = (h[min(x-1, nx)][y] == '0');\n                    else can_move = true;\n                }\n                \n                if (can_move || (nx >= 0 && nx < N && ny >= 0 && ny < N)) {\n                    if ((dir == 0 && (y+1 < N) && v[x][y] == '0') ||\n                        (dir == 1 && (x+1 < N) && h[x][y] == '0') ||\n                        (dir == 2 && (y > 0) && v[x][y-1] == '0') ||\n                        (dir == 3 && (x > 0) && h[x-1][y] == '0')) {\n                        visited[nx][ny] = true;\n                        tree_edges.push_back({x*N + y, nx*N + ny});\n                        q.push({nx, ny});\n                    }\n                }\n            }\n        }\n    }\n}\n\n// DFS to create Eulerian tour\nstring result = \"\";\nvoid dfs_tour(int x, int y, int parent_x, int parent_y) {\n    visited[x][y] = true;\n    \n    // Try all directions\n    for (int dir = 0; dir < 4; dir++) {\n        int nx = x + moves[dir].first;\n        int ny = y + moves[dir].second;\n        \n        // Check bounds\n        if (nx < 0 || nx >= N || ny < 0 || ny >= N) continue;\n        \n        // Check walls\n        bool can_move = false;\n        if (dir == 0) { // Right\n            can_move = (v[x][y] == '0');\n        } else if (dir == 1) { // Down\n            can_move = (h[x][y] == '0');\n        } else if (dir == 2) { // Left\n            if (y > 0) can_move = (v[x][y-1] == '0');\n        } else { // Up\n            if (x > 0) can_move = (h[x-1][y] == '0');\n        }\n        \n        if (!can_move) continue;\n        \n        // Skip parent to avoid going back immediately\n        if (nx == parent_x && ny == parent_y) continue;\n        \n        if (!visited[nx][ny]) {\n            result += directions[dir];\n            dfs_tour(nx, ny, x, y);\n            // Return move\n            if (dir == 0) result += 'L';\n            else if (dir == 1) result += 'U';\n            else if (dir == 2) result += 'R';\n            else result += 'D';\n        }\n    }\n}\n\n// Better approach: use BFS to build proper spanning tree\nvoid build_proper_spanning_tree() {\n    memset(visited, 0, sizeof(visited));\n    queue<tuple<int, int, int, int>> q; // x, y, parent_x, parent_y\n    q.push({0, 0, -1, -1});\n    visited[0][0] = true;\n    vector<tuple<int, int, int, int>> edges;\n    \n    while (!q.empty()) {\n        auto [x, y, px, py] = q.front(); q.pop();\n        \n        for (int dir = 0; dir < 4; dir++) {\n            int nx = x + moves[dir].first;\n            int ny = y + moves[dir].second;\n            \n            if (nx >= 0 && nx < N && ny >= 0 && ny < N && !visited[nx][ny]) {\n                // Check if move is possible\n                bool can_move = false;\n                if (dir == 0 && y+1 < N) can_move = (v[x][y] == '0');\n                else if (dir == 1 && x+1 < N) can_move = (h[x][y] == '0');\n                else if (dir == 2 && y > 0) can_move = (v[x][y-1] == '0');\n                else if (dir == 3 && x > 0) can_move = (h[x-1][y] == '0');\n                \n                if (can_move) {\n                    visited[nx][ny] = true;\n                    edges.push_back({x, y, nx, ny});\n                    q.push({nx, ny, x, y});\n                }\n            }\n        }\n    }\n    \n    // Now create tour from edges\n    memset(visited, 0, sizeof(visited));\n    result = \"\";\n    dfs_tour(0, 0, -1, -1);\n}\n\n// Even simpler approach: just do DFS on the grid\nstring simple_dfs() {\n    memset(visited, 0, sizeof(visited));\n    string path = \"\";\n    \n    function<void(int, int)> dfs = [&](int x, int y) {\n        visited[x][y] = true;\n        \n        // Try directions in order that prioritizes higher d values\n        vector<pair<int, int>> next_moves;\n        for (int dir = 0; dir < 4; dir++) {\n            int nx = x + moves[dir].first;\n            int ny = y + moves[dir].second;\n            \n            if (nx >= 0 && nx < N && ny >= 0 && ny < N && !visited[nx][ny]) {\n                // Check walls\n                bool can_move = false;\n                if (dir == 0 && y+1 < N) can_move = (v[x][y] == '0');\n                else if (dir == 1 && x+1 < N) can_move = (h[x][y] == '0');\n                else if (dir == 2 && y > 0) can_move = (v[x][y-1] == '0');\n                else if (dir == 3 && x > 0) can_move = (h[x-1][y] == '0');\n                \n                if (can_move) {\n                    next_moves.push_back({nx, ny});\n                }\n            }\n        }\n        \n        // Sort by dirtiness (descending)\n        sort(next_moves.begin(), next_moves.end(), [&](const pair<int,int>& a, const pair<int,int>& b) {\n            return d[a.first][a.second] > d[b.first][b.second];\n        });\n        \n        for (auto& move : next_moves) {\n            int nx = move.first, ny = move.second;\n            // Find direction\n            int dir = -1;\n            for (int i = 0; i < 4; i++) {\n                if (x + moves[i].first == nx && y + moves[i].second == ny) {\n                    dir = i;\n                    break;\n                }\n            }\n            \n            if (dir != -1) {\n                path += directions[dir];\n                dfs(nx, ny);\n                // Return move\n                if (dir == 0) path += 'L';\n                else if (dir == 1) path += 'U';\n                else if (dir == 2) path += 'R';\n                else path += 'D';\n            }\n        }\n    };\n    \n    dfs(0, 0);\n    return path;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    cin >> N;\n    \n    // Read horizontal walls\n    for (int i = 0; i < N-1; i++) {\n        string line;\n        cin >> line;\n        for (int j = 0; j < N; j++) {\n            h[i][j] = line[j];\n        }\n    }\n    \n    // Read vertical walls\n    for (int i = 0; i < N; i++) {\n        string line;\n        cin >> line;\n        for (int j = 0; j < N-1; j++) {\n            v[i][j] = line[j];\n        }\n    }\n    \n    // Read dirtiness values\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cin >> d[i][j];\n        }\n    }\n    \n    // Generate path using simple DFS approach\n    string result = simple_dfs();\n    \n    // Make sure we don't exceed limit\n    if (result.length() > 100000) {\n        result = result.substr(0, 100000);\n    }\n    \n    cout << result << endl;\n    \n    return 0;\n}","ahc028":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 15;\nconst int M = 200;\nconst int MAX_OPS = 5000;\n\nint grid_size;\nint target_count;\nint start_row, start_col;\nchar grid[N][N];\nvector<string> targets;\nvector<pair<int,int>> char_positions[26];\n\n// Precompute positions for each character\nvoid precompute_positions() {\n    for (int i = 0; i < 26; i++) {\n        char_positions[i].clear();\n    }\n    \n    for (int i = 0; i < grid_size; i++) {\n        for (int j = 0; j < grid_size; j++) {\n            int char_idx = grid[i][j] - 'A';\n            char_positions[char_idx].push_back({i, j});\n        }\n    }\n}\n\n// Find the best position considering local context\npair<int, int> find_optimal_position(char c, int cur_row, int cur_col, char next_c = 0) {\n    int char_idx = c - 'A';\n    pair<int, int> best_pos = char_positions[char_idx][0];\n    int best_score = abs(cur_row - best_pos.first) + abs(cur_col - best_pos.second);\n    \n    // If we have a next character, consider it in our decision\n    if (next_c != 0) {\n        int next_char_idx = next_c - 'A';\n        best_score = 1e9;\n        \n        for (auto& pos : char_positions[char_idx]) {\n            int move_cost = abs(cur_row - pos.first) + abs(cur_col - pos.second);\n            \n            // Find closest next character position from this position\n            int min_next_dist = 1e9;\n            for (auto& next_pos : char_positions[next_char_idx]) {\n                int dist = abs(pos.first - next_pos.first) + abs(pos.second - next_pos.second);\n                min_next_dist = min(min_next_dist, dist);\n            }\n            \n            int total_cost = move_cost + min_next_dist;\n            if (total_cost < best_score) {\n                best_score = total_cost;\n                best_pos = pos;\n            }\n        }\n    } else {\n        // Simple closest position\n        for (auto& pos : char_positions[char_idx]) {\n            int dist = abs(cur_row - pos.first) + abs(cur_col - pos.second);\n            if (dist < best_score) {\n                best_score = dist;\n                best_pos = pos;\n            }\n        }\n    }\n    \n    return best_pos;\n}\n\n// Build superstring using greedy overlap with better strategy\nstring build_efficient_superstring() {\n    vector<string> remaining = targets;\n    if (remaining.empty()) return \"\";\n    \n    string result = remaining[0];\n    remaining.erase(remaining.begin());\n    \n    while (!remaining.empty() && result.length() < 3000) {\n        int best_overlap = -1;\n        int best_idx = -1;\n        string best_extension = \"\";\n        \n        // Try to find string with maximum overlap\n        for (int i = 0; i < (int)remaining.size(); i++) {\n            const string& s = remaining[i];\n            \n            // Check for overlaps of length 1 to 4\n            for (int overlap = min(4, min((int)result.length(), (int)s.length())); overlap >= 1; overlap--) {\n                if (result.length() >= overlap && \n                    s.length() >= overlap &&\n                    result.substr(result.length() - overlap) == s.substr(0, overlap)) {\n                    string candidate = result + s.substr(overlap);\n                    if (overlap > best_overlap || \n                        (overlap == best_overlap && candidate.length() < best_extension.length())) {\n                        best_overlap = overlap;\n                        best_idx = i;\n                        best_extension = candidate;\n                    }\n                    break;\n                }\n            }\n        }\n        \n        if (best_idx != -1) {\n            result = best_extension;\n            remaining.erase(remaining.begin() + best_idx);\n        } else {\n            // No overlap found, append first available string\n            result += remaining[0];\n            remaining.erase(remaining.begin());\n        }\n    }\n    \n    return result;\n}\n\n// Generate optimized path\nvector<pair<int, int>> generate_optimized_path(const string& superstring) {\n    vector<pair<int, int>> path;\n    int cur_row = start_row;\n    int cur_col = start_col;\n    \n    for (int i = 0; i < (int)superstring.length() && path.size() < MAX_OPS; i++) {\n        char current_char = superstring[i];\n        char next_char = (i + 1 < (int)superstring.length()) ? superstring[i + 1] : 0;\n        \n        pair<int, int> next_pos = find_optimal_position(current_char, cur_row, cur_col, next_char);\n        path.push_back(next_pos);\n        cur_row = next_pos.first;\n        cur_col = next_pos.second;\n    }\n    \n    return path;\n}\n\n// Remove consecutive duplicate positions\nvector<pair<int, int>> compress_path(const vector<pair<int, int>>& path) {\n    if (path.empty()) return path;\n    \n    vector<pair<int, int>> compressed;\n    compressed.push_back(path[0]);\n    \n    for (int i = 1; i < (int)path.size(); i++) {\n        if (path[i] != compressed.back()) {\n            compressed.push_back(path[i]);\n        }\n    }\n    \n    // Ensure we don't exceed limit\n    if (compressed.size() > MAX_OPS) {\n        compressed.resize(MAX_OPS);\n    }\n    \n    return compressed;\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    // Read input\n    cin >> grid_size >> target_count;\n    cin >> start_row >> start_col;\n    \n    for (int i = 0; i < grid_size; i++) {\n        string line;\n        cin >> line;\n        for (int j = 0; j < grid_size; j++) {\n            grid[i][j] = line[j];\n        }\n    }\n    \n    targets.resize(target_count);\n    for (int i = 0; i < target_count; i++) {\n        cin >> targets[i];\n    }\n    \n    // Preprocessing\n    precompute_positions();\n    \n    // Build superstring\n    string superstring = build_efficient_superstring();\n    \n    // Generate and optimize path\n    vector<pair<int, int>> path = generate_optimized_path(superstring);\n    path = compress_path(path);\n    \n    // Output path\n    for (const auto& pos : path) {\n        cout << pos.first << \" \" << pos.second << \"\\n\";\n    }\n    \n    return 0;\n}","ahc030":"#include <bits/stdc++.h>\n\nusing namespace std;\n\nstruct Point {\n    int i, j;\n    Point() : i(0), j(0) {}\n    Point(int _i, int _j) : i(_i), j(_j) {}\n    bool operator<(const Point& other) const {\n        if (i != other.i) return i < other.i;\n        return j < other.j;\n    }\n};\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    int N, M;\n    double epsilon;\n    \n    // Read initial parameters\n    if (!(cin >> N >> M >> epsilon)) {\n        return 1;\n    }\n    \n    // Read polyomino data (skip it since we don't need it for basic approach)\n    for (int i = 0; i < M; i++) {\n        int d;\n        if (!(cin >> d)) return 1;\n        for (int j = 0; j < 2 * d; j++) {\n            int dummy;\n            if (!(cin >> dummy)) return 1;\n        }\n    }\n    \n    // Track which squares we know have oil\n    vector<vector<bool>> has_oil(N, vector<bool>(N, false));\n    vector<vector<bool>> drilled(N, vector<bool>(N, false));\n    set<Point> oil_squares;\n    \n    // Simple strategy: drill all squares\n    // This guarantees we find all oil squares but might not be optimal cost-wise\n    int operations = 0;\n    const int max_operations = 2 * N * N;\n    \n    for (int i = 0; i < N && operations < max_operations - N*N/2; i++) {\n        for (int j = 0; j < N && operations < max_operations - N*N/2; j++) {\n            // Query this square\n            cout << \"q 1 \" << i << \" \" << j << endl;\n            cout.flush();\n            operations++;\n            \n            int response;\n            if (!(cin >> response)) {\n                return 1;\n            }\n            \n            drilled[i][j] = true;\n            if (response > 0) {\n                has_oil[i][j] = true;\n                oil_squares.insert(Point(i, j));\n            }\n        }\n    }\n    \n    // Submit our answer\n    cout << \"a \" << oil_squares.size();\n    for (const auto& p : oil_squares) {\n        cout << \" \" << p.i << \" \" << p.j;\n    }\n    cout << endl;\n    cout.flush();\n    \n    // Read the response (should be 1 for correct answer)\n    int result;\n    if (cin >> result) {\n        // We don't need to do anything with the result since we're done\n    }\n    \n    return 0;\n}","ahc031":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int W = 1000;\n\nstruct Rectangle {\n    int i0, j0, i1, j1;\n    Rectangle() : i0(0), j0(0), i1(0), j1(0) {}\n    Rectangle(int _i0, int _j0, int _i1, int _j1) : i0(_i0), j0(_j0), i1(_i1), j1(_j1) {}\n    int area() const { return (i1 - i0) * (j1 - j0); }\n    bool valid() const { return i0 >= 0 && j0 >= 0 && i1 <= W && j1 <= W && i0 < i1 && j0 < j1; }\n};\n\n// Calculate partition cost between two days\nint calculate_partition_cost(const vector<Rectangle>& prev, const vector<Rectangle>& curr) {\n    // Simplified: just count rectangle changes\n    // In practice, we'd need to track actual partition lines\n    int cost = 0;\n    for (size_t i = 0; i < min(prev.size(), curr.size()); ++i) {\n        if (prev[i].i0 != curr[i].i0 || prev[i].j0 != curr[i].j0 || \n            prev[i].i1 != curr[i].i1 || prev[i].j1 != curr[i].j1) {\n            cost += 10; // penalty for moving rectangles\n        }\n    }\n    return cost;\n}\n\n// Calculate area shortage cost\nint calculate_area_cost(const vector<int>& required, const vector<Rectangle>& rectangles) {\n    int cost = 0;\n    for (size_t i = 0; i < required.size(); ++i) {\n        int shortage = max(0, required[i] - rectangles[i].area());\n        cost += 100 * shortage;\n    }\n    return cost;\n}\n\n// Simple strip packing algorithm\nvector<Rectangle> pack_rectangles_simple(const vector<int>& required) {\n    vector<Rectangle> rectangles(required.size());\n    int current_row = 0;\n    \n    for (size_t i = 0; i < required.size(); ++i) {\n        int area_needed = required[i];\n        \n        // Try to make rectangle as square as possible\n        int width = min(W, (int)sqrt(area_needed) + 1);\n        while (width > 0 && area_needed % width != 0) width--;\n        if (width == 0) width = 1;\n        \n        int height = (area_needed + width - 1) / width;\n        \n        // Adjust to fit in grid\n        if (width > W) {\n            width = W;\n            height = (area_needed + W - 1) / W;\n        }\n        if (height > W) {\n            height = W;\n            width = (area_needed + W - 1) / W;\n        }\n        \n        // Position the rectangle\n        if (current_row + height <= W) {\n            rectangles[i] = Rectangle(current_row, 0, current_row + height, width);\n            current_row += height;\n        } else {\n            // If we run out of space, just place it somewhere\n            rectangles[i] = Rectangle(0, 0, min(height, W), min(width, W));\n        }\n    }\n    \n    return rectangles;\n}\n\n// Improved packing with better area satisfaction\nvector<Rectangle> pack_rectangles_improved(const vector<int>& required) {\n    vector<Rectangle> rectangles(required.size());\n    \n    // Sort rectangles by required area (descending) for better packing\n    vector<pair<int, int>> area_index;\n    for (size_t i = 0; i < required.size(); ++i) {\n        area_index.push_back({required[i], i});\n    }\n    sort(area_index.rbegin(), area_index.rend());\n    \n    // Use a simple grid-based approach\n    int grid_size = (int)sqrt(required.size()) + 1;\n    int cell_width = W / grid_size;\n    int cell_height = W / grid_size;\n    \n    vector<vector<bool>> occupied(grid_size, vector<bool>(grid_size, false));\n    \n    for (const auto& [area, idx] : area_index) {\n        int area_needed = area;\n        \n        // Try to make rectangle fit requirements\n        int width = min(W, (int)sqrt(area_needed) + 50);  // Add buffer\n        int height = (area_needed + width - 1) / width;\n        \n        if (width > W) {\n            width = W;\n            height = (area_needed + W - 1) / W + 1;\n        }\n        if (height > W) {\n            height = W;\n            width = (area_needed + W - 1) / W + 1;\n        }\n        \n        // Ensure minimum area\n        if (width * height < area_needed) {\n            int diff = area_needed - width * height;\n            width += (diff + height - 1) / height;\n            if (width > W) width = W;\n            height += (diff + width - 1) / width;\n            if (height > W) height = W;\n        }\n        \n        // Place rectangle\n        bool placed = false;\n        for (int x = 0; x <= W - width && !placed; ++x) {\n            for (int y = 0; y <= W - height && !placed; ++y) {\n                rectangles[idx] = Rectangle(x, y, x + height, y + width);\n                if (rectangles[idx].valid()) {\n                    placed = true;\n                    break;\n                }\n            }\n        }\n        \n        // Fallback if not placed properly\n        if (!placed || rectangles[idx].area() < area_needed) {\n            int side = max(1, (int)sqrt(area_needed));\n            rectangles[idx] = Rectangle(0, 0, min(side, W), min((area_needed + side - 1) / side, W));\n        }\n    }\n    \n    return rectangles;\n}\n\n// Optimize rectangles to reduce area shortage\nvector<Rectangle> optimize_rectangles(vector<Rectangle> rectangles, const vector<int>& required) {\n    for (size_t i = 0; i < rectangles.size(); ++i) {\n        int current_area = rectangles[i].area();\n        int required_area = required[i];\n        \n        if (current_area < required_area) {\n            // Expand rectangle to meet requirement\n            int shortage = required_area - current_area;\n            \n            int width = rectangles[i].j1 - rectangles[i].j0;\n            int height = rectangles[i].i1 - rectangles[i].i0;\n            \n            // Try to expand width first\n            if (rectangles[i].j1 < W) {\n                int expand_w = min(shortage / height + 1, W - rectangles[i].j1);\n                rectangles[i].j1 = min(W, rectangles[i].j1 + expand_w);\n            }\n            // Then try to expand height\n            else if (rectangles[i].i1 < W) {\n                int expand_h = min(shortage / width + 1, W - rectangles[i].i1);\n                rectangles[i].i1 = min(W, rectangles[i].i1 + expand_h);\n            }\n        }\n    }\n    return rectangles;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int W_input, D, N;\n    cin >> W_input >> D >> N;\n    \n    vector<vector<int>> required(D, vector<int>(N));\n    for (int d = 0; d < D; ++d) {\n        for (int k = 0; k < N; ++k) {\n            cin >> required[d][k];\n        }\n    }\n    \n    vector<vector<Rectangle>> solutions(D);\n    \n    // Day 0: Generate initial solution\n    solutions[0] = pack_rectangles_improved(required[0]);\n    solutions[0] = optimize_rectangles(solutions[0], required[0]);\n    \n    // Days 1 to D-1: Try to maintain consistency to reduce partition costs\n    for (int d = 1; d < D; ++d) {\n        // Start with previous day's solution\n        solutions[d] = solutions[d-1];\n        \n        // Adjust to meet new requirements\n        solutions[d] = optimize_rectangles(solutions[d], required[d]);\n        \n        // If still not good enough, generate fresh solution\n        bool need_fresh = false;\n        for (int k = 0; k < N; ++k) {\n            if (solutions[d][k].area() < required[d][k]) {\n                need_fresh = true;\n                break;\n            }\n        }\n        \n        if (need_fresh) {\n            solutions[d] = pack_rectangles_improved(required[d]);\n            solutions[d] = optimize_rectangles(solutions[d], required[d]);\n        }\n    }\n    \n    // Output results\n    for (int d = 0; d < D; ++d) {\n        for (int k = 0; k < N; ++k) {\n            cout << solutions[d][k].i0 << \" \" << solutions[d][k].j0 << \" \" \n                 << solutions[d][k].i1 << \" \" << solutions[d][k].j1 << \"\\n\";\n        }\n    }\n    \n    return 0;\n}","ahc032":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 9;\nconst int M = 20;\nconst int K = 81;\nconst long long MOD = 998244353;\n\nlong long a[N][N];\nlong long s[M][3][3];\n\nstruct Operation {\n    int stamp, p, q;\n};\n\n// Calculate current score of board\nlong long calculate_score(long long board[N][N]) {\n    long long score = 0;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            score = (score + (board[i][j] % MOD + MOD) % MOD) % MOD;\n        }\n    }\n    return score;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    // Read input\n    int dummy_N, dummy_M, dummy_K;\n    cin >> dummy_N >> dummy_M >> dummy_K;\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    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 board\n    long long board[N][N];\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            board[i][j] = a[i][j];\n        }\n    }\n    \n    vector<Operation> operations;\n    \n    // Greedy selection\n    for (int k = 0; k < K; k++) {\n        Operation best_op = {-1, -1, -1};\n        long long best_gain = -1;\n        long long current_score = calculate_score(board);\n        \n        // Try all possible stamp placements\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                    // Create temporary board\n                    long long temp_board[N][N];\n                    for (int i = 0; i < N; i++) {\n                        for (int j = 0; j < N; j++) {\n                            temp_board[i][j] = board[i][j];\n                        }\n                    }\n                    \n                    // Apply stamp\n                    for (int i = 0; i < 3; i++) {\n                        for (int j = 0; j < 3; j++) {\n                            temp_board[p + i][q + j] += s[m][i][j];\n                        }\n                    }\n                    \n                    long long new_score = calculate_score(temp_board);\n                    long long gain = new_score - current_score;\n                    \n                    if (gain > best_gain) {\n                        best_gain = gain;\n                        best_op = {m, p, q};\n                    }\n                }\n            }\n        }\n        \n        // If no positive gain, stop\n        if (best_gain <= 0) {\n            break;\n        }\n        \n        // Apply the best operation\n        operations.push_back(best_op);\n        \n        for (int i = 0; i < 3; i++) {\n            for (int j = 0; j < 3; j++) {\n                board[best_op.p + i][best_op.q + j] += s[best_op.stamp][i][j];\n            }\n        }\n    }\n    \n    // Output\n    cout << operations.size() << \"\\n\";\n    for (const auto& op : operations) {\n        cout << op.stamp << \" \" << op.p << \" \" << op.q << \"\\n\";\n    }\n    \n    return 0;\n}","ahc033":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 5;\nconst int MAX_TURNS = 10000;\n\n// Container information\nint A[N][N];\nint container_to_gate[N*N]; // which gate each container should go to\n\n// Crane positions and states\nstruct Crane {\n    int row, col;\n    bool holding;\n    int container;\n    bool bombed;\n};\n\n// Grid state\nint grid[N][N]; // -1 = empty, >= 0 = container number\nCrane cranes[N];\n\n// Output actions\nvector<string> actions(N);\n\n// Initialize problem\nvoid init() {\n    // Calculate which gate each container should go to\n    for (int i = 0; i < N*N; i++) {\n        container_to_gate[i] = i / N;\n    }\n    \n    // Initialize grid\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            grid[i][j] = -1;\n        }\n    }\n    \n    // Initialize cranes\n    for (int i = 0; i < N; i++) {\n        cranes[i] = {i, 0, false, -1, false};\n    }\n}\n\n// Check if a position is occupied by another crane\nbool is_position_occupied(int row, int col, int exclude_crane) {\n    for (int i = 0; i < N; i++) {\n        if (i != exclude_crane && !cranes[i].bombed && cranes[i].row == row && cranes[i].col == col) {\n            return true;\n        }\n    }\n    return false;\n}\n\n// Main simulation logic\nvoid simulate() {\n    int turn = 0;\n    int received[N] = {0}; // Count of containers received by each gate\n    bool dispatched[N*N] = {false}; // Track which containers have been dispatched\n    \n    // Place initial containers\n    for (int i = 0; i < N; i++) {\n        if (received[i] < N && grid[i][0] == -1) {\n            grid[i][0] = A[i][received[i]];\n            received[i]++;\n        }\n    }\n    \n    while (turn < MAX_TURNS) {\n        // Check if all containers are dispatched\n        bool all_done = true;\n        for (int i = 0; i < N*N; i++) {\n            if (!dispatched[i]) {\n                all_done = false;\n                break;\n            }\n        }\n        if (all_done) break;\n        \n        // Step 1: Place new containers at receiving gates\n        for (int i = 0; i < N; i++) {\n            if (received[i] < N && grid[i][0] == -1) {\n                grid[i][0] = A[i][received[i]];\n                received[i]++;\n            }\n        }\n        \n        // Step 2: Plan crane actions\n        char turn_actions[N];\n        for (int i = 0; i < N; i++) {\n            turn_actions[i] = '.';\n        }\n        \n        // Calculate next positions for collision detection\n        int next_positions[N][2]; // [row, col] for each crane\n        bool position_valid[N];   // whether the move is valid\n        \n        // Initialize next positions to current positions\n        for (int i = 0; i < N; i++) {\n            if (cranes[i].bombed) {\n                position_valid[i] = false;\n            } else {\n                next_positions[i][0] = cranes[i].row;\n                next_positions[i][1] = cranes[i].col;\n                position_valid[i] = true;\n            }\n        }\n        \n        // Plan actions and next positions\n        for (int crane_id = 0; crane_id < N; crane_id++) {\n            if (cranes[crane_id].bombed) {\n                turn_actions[crane_id] = '.';\n                position_valid[crane_id] = false;\n                continue;\n            }\n            \n            Crane& crane = cranes[crane_id];\n            \n            // If at dispatch gate, check for automatic dispatch\n            if (crane.col == N-1 && !crane.holding && grid[crane.row][crane.col] != -1) {\n                int container = grid[crane.row][crane.col];\n                if (container / N == crane.row) {\n                    dispatched[container] = true;\n                    grid[crane.row][crane.col] = -1;\n                }\n            }\n            \n            // Simple strategy: prioritize dispatching containers that are ready\n            if (crane.holding) {\n                int target_gate = crane.container / N;\n                \n                // If at dispatch gate\n                if (crane.col == N-1) {\n                    // If correct gate, place container\n                    if (crane.row == target_gate) {\n                        // Check if square is empty\n                        if (grid[crane.row][crane.col] == -1) {\n                            turn_actions[crane_id] = 'Q';\n                            position_valid[crane_id] = true; // Q doesn't change position\n                        } else {\n                            turn_actions[crane_id] = '.';\n                            position_valid[crane_id] = true;\n                        }\n                    } else {\n                        // Wrong gate, move vertically if possible\n                        if (crane.row < target_gate) {\n                            if (crane.row + 1 < N) {\n                                next_positions[crane_id][0] = crane.row + 1;\n                                next_positions[crane_id][1] = crane.col;\n                                // Check if destination valid\n                                if (!is_position_occupied(crane.row + 1, crane.col, crane_id)) {\n                                    if (crane_id == 0 || grid[crane.row+1][crane.col] == -1) {\n                                        turn_actions[crane_id] = 'D';\n                                        position_valid[crane_id] = true;\n                                    } else {\n                                        turn_actions[crane_id] = '.';\n                                        next_positions[crane_id][0] = crane.row;\n                                        position_valid[crane_id] = true;\n                                    }\n                                } else {\n                                    turn_actions[crane_id] = '.';\n                                    next_positions[crane_id][0] = crane.row;\n                                    position_valid[crane_id] = true;\n                                }\n                            } else {\n                                turn_actions[crane_id] = '.';\n                                position_valid[crane_id] = true;\n                            }\n                        } else if (crane.row > target_gate) {\n                            if (crane.row - 1 >= 0) {\n                                next_positions[crane_id][0] = crane.row - 1;\n                                next_positions[crane_id][1] = crane.col;\n                                // Check if destination valid\n                                if (!is_position_occupied(crane.row - 1, crane.col, crane_id)) {\n                                    if (crane_id == 0 || grid[crane.row-1][crane.col] == -1) {\n                                        turn_actions[crane_id] = 'U';\n                                        position_valid[crane_id] = true;\n                                    } else {\n                                        turn_actions[crane_id] = '.';\n                                        next_positions[crane_id][0] = crane.row;\n                                        position_valid[crane_id] = true;\n                                    }\n                                } else {\n                                    turn_actions[crane_id] = '.';\n                                    next_positions[crane_id][0] = crane.row;\n                                    position_valid[crane_id] = true;\n                                }\n                            } else {\n                                turn_actions[crane_id] = '.';\n                                position_valid[crane_id] = true;\n                            }\n                        } else {\n                            turn_actions[crane_id] = '.';\n                            position_valid[crane_id] = true;\n                        }\n                    }\n                } else {\n                    // Move right toward dispatch gate\n                    if (crane.col + 1 < N) {\n                        next_positions[crane_id][0] = crane.row;\n                        next_positions[crane_id][1] = crane.col + 1;\n                        // Check if destination valid\n                        if (!is_position_occupied(crane.row, crane.col + 1, crane_id)) {\n                            if (crane_id == 0) {\n                                // Large crane can move through occupied squares\n                                turn_actions[crane_id] = 'R';\n                                position_valid[crane_id] = true;\n                            } else {\n                                // Small crane needs clear path\n                                if (grid[crane.row][crane.col+1] == -1) {\n                                    turn_actions[crane_id] = 'R';\n                                    position_valid[crane_id] = true;\n                                } else {\n                                    turn_actions[crane_id] = '.';\n                                    next_positions[crane_id][1] = crane.col;\n                                    position_valid[crane_id] = true;\n                                }\n                            }\n                        } else {\n                            turn_actions[crane_id] = '.';\n                            next_positions[crane_id][1] = crane.col;\n                            position_valid[crane_id] = true;\n                        }\n                    } else {\n                        turn_actions[crane_id] = '.';\n                        position_valid[crane_id] = true;\n                    }\n                }\n            } else {\n                // Not holding container\n                // Check if container at current position\n                if (grid[crane.row][crane.col] != -1) {\n                    int container = grid[crane.row][crane.col];\n                    turn_actions[crane_id] = 'P';\n                    position_valid[crane_id] = true; // P doesn't change position\n                } else {\n                    // Move to find work - move right if possible\n                    if (crane.col + 1 < N) {\n                        next_positions[crane_id][0] = crane.row;\n                        next_positions[crane_id][1] = crane.col + 1;\n                        // Check if destination valid\n                        if (!is_position_occupied(crane.row, crane.col + 1, crane_id)) {\n                            if (crane_id == 0) {\n                                // Large crane can move through occupied squares\n                                turn_actions[crane_id] = 'R';\n                                position_valid[crane_id] = true;\n                            } else {\n                                // Small crane needs clear path\n                                if (grid[crane.row][crane.col+1] == -1) {\n                                    turn_actions[crane_id] = 'R';\n                                    position_valid[crane_id] = true;\n                                } else {\n                                    turn_actions[crane_id] = '.';\n                                    next_positions[crane_id][1] = crane.col;\n                                    position_valid[crane_id] = true;\n                                }\n                            }\n                        } else {\n                            turn_actions[crane_id] = '.';\n                            next_positions[crane_id][1] = crane.col;\n                            position_valid[crane_id] = true;\n                        }\n                    } else if (crane.col == N-1 && grid[crane.row][crane.col] != -1) {\n                        // At dispatch gate with container\n                        int container = grid[crane.row][crane.col];\n                        if (container / N != crane.row) {\n                            // Wrong container, pick it up\n                            turn_actions[crane_id] = 'P';\n                            position_valid[crane_id] = true;\n                        } else {\n                            turn_actions[crane_id] = '.';\n                            position_valid[crane_id] = true;\n                        }\n                    } else {\n                        turn_actions[crane_id] = '.';\n                        position_valid[crane_id] = true;\n                    }\n                }\n            }\n        }\n        \n        // Check for position conflicts and resolve them\n        bool conflict_found = true;\n        while (conflict_found) {\n            conflict_found = false;\n            for (int i = 0; i < N; i++) {\n                if (!position_valid[i] || cranes[i].bombed) continue;\n                \n                for (int j = i + 1; j < N; j++) {\n                    if (!position_valid[j] || cranes[j].bombed) continue;\n                    \n                    // Check if both cranes want to go to the same position\n                    if (next_positions[i][0] == next_positions[j][0] && \n                        next_positions[i][1] == next_positions[j][1]) {\n                        // Conflict found - make one of them stay\n                        turn_actions[i] = '.';\n                        next_positions[i][0] = cranes[i].row;\n                        next_positions[i][1] = cranes[i].col;\n                        conflict_found = true;\n                        break;\n                    }\n                }\n                if (conflict_found) break;\n            }\n        }\n        \n        // Apply actions and update states\n        for (int i = 0; i < N; i++) {\n            actions[i] += turn_actions[i];\n            \n            if (turn_actions[i] == 'P') {\n                if (grid[cranes[i].row][cranes[i].col] != -1 && !cranes[i].holding) {\n                    cranes[i].holding = true;\n                    cranes[i].container = grid[cranes[i].row][cranes[i].col];\n                    grid[cranes[i].row][cranes[i].col] = -1;\n                }\n            } else if (turn_actions[i] == 'Q') {\n                if (cranes[i].holding && grid[cranes[i].row][cranes[i].col] == -1) {\n                    grid[cranes[i].row][cranes[i].col] = cranes[i].container;\n                    int container = cranes[i].container;\n                    cranes[i].holding = false;\n                    cranes[i].container = -1;\n                    // Check if this container should be dispatched\n                    if (cranes[i].col == N-1 && container / N == cranes[i].row) {\n                        dispatched[container] = true;\n                    }\n                }\n            } else if (turn_actions[i] == 'U') {\n                if (cranes[i].row > 0) {\n                    if (i == 0 || grid[cranes[i].row-1][cranes[i].col] == -1 || !cranes[i].holding) {\n                        cranes[i].row--;\n                    }\n                }\n            } else if (turn_actions[i] == 'D') {\n                if (cranes[i].row < N-1) {\n                    if (i == 0 || grid[cranes[i].row+1][cranes[i].col] == -1 || !cranes[i].holding) {\n                        cranes[i].row++;\n                    }\n                }\n            } else if (turn_actions[i] == 'L') {\n                if (cranes[i].col > 0) {\n                    if (i == 0 || grid[cranes[i].row][cranes[i].col-1] == -1 || !cranes[i].holding) {\n                        cranes[i].col--;\n                    }\n                }\n            } else if (turn_actions[i] == 'R') {\n                if (cranes[i].col < N-1) {\n                    if (i == 0 || grid[cranes[i].row][cranes[i].col+1] == -1 || !cranes[i].holding) {\n                        cranes[i].col++;\n                    }\n                }\n            }\n        }\n        \n        turn++;\n    }\n    \n    // Pad all action strings to same length\n    int max_len = 0;\n    for (int i = 0; i < N; i++) {\n        max_len = max(max_len, (int)actions[i].length());\n    }\n    \n    for (int i = 0; i < N; i++) {\n        while ((int)actions[i].length() < max_len) {\n            actions[i] += '.';\n        }\n    }\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    // Read input (N is fixed to 5)\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\n    init();\n    \n    // Run simulation\n    simulate();\n    \n    // Output results\n    for (int i = 0; i < N; i++) {\n        cout << actions[i] << \"\\n\";\n    }\n    \n    return 0;\n}","ahc034":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int GRID_SIZE = 20;\nint grid[GRID_SIZE][GRID_SIZE];\n\n// Directions: U, D, L, R\nconst int dx[4] = {-1, 1, 0, 0};\nconst int dy[4] = {0, 0, -1, 1};\n\nstruct Position {\n    int x, y;\n    Position() : x(0), y(0) {}\n    Position(int _x, int _y) : x(_x), y(_y) {}\n    bool operator==(const Position& other) const {\n        return x == other.x && y == other.y;\n    }\n};\n\n// BFS to find shortest path between two points\nvector<pair<int, int>> find_shortest_path(Position start, Position end) {\n    if (start == end) return {};\n    \n    bool visited[GRID_SIZE][GRID_SIZE];\n    pair<int, int> parent[GRID_SIZE][GRID_SIZE];\n    memset(visited, 0, sizeof(visited));\n    \n    queue<Position> q;\n    q.push(start);\n    visited[start.x][start.y] = true;\n    parent[start.x][start.y] = {-1, -1};\n    \n    while (!q.empty()) {\n        Position pos = q.front(); q.pop();\n        \n        if (pos.x == end.x && pos.y == end.y) {\n            // Reconstruct path\n            vector<pair<int, int>> path;\n            int cx = end.x, cy = end.y;\n            while (parent[cx][cy].first != -1) {\n                path.push_back({cx, cy});\n                auto p = parent[cx][cy];\n                cx = p.first;\n                cy = p.second;\n            }\n            reverse(path.begin(), path.end());\n            return path;\n        }\n        \n        for (int i = 0; i < 4; i++) {\n            int nx = pos.x + dx[i];\n            int ny = pos.y + dy[i];\n            \n            if (nx >= 0 && nx < GRID_SIZE && ny >= 0 && ny < GRID_SIZE && !visited[nx][ny]) {\n                visited[nx][ny] = true;\n                parent[nx][ny] = {pos.x, pos.y};\n                q.push(Position(nx, ny));\n            }\n        }\n    }\n    \n    return {};\n}\n\n// Convert path to movement directions\nvector<char> path_to_directions(Position start, const vector<pair<int, int>>& path) {\n    vector<char> directions;\n    int cx = start.x, cy = start.y;\n    \n    for (auto [nx, ny] : path) {\n        if (nx == cx - 1 && ny == cy) directions.push_back('U');\n        else if (nx == cx + 1 && ny == cy) directions.push_back('D');\n        else if (nx == cx && ny == cy - 1) directions.push_back('L');\n        else if (nx == cx && ny == cy + 1) directions.push_back('R');\n        cx = nx;\n        cy = ny;\n    }\n    \n    return directions;\n}\n\n// Find nearest sink for a given source\npair<int, int> find_nearest_sink(Position source, vector<pair<Position, int>>& sinks) {\n    int best_idx = -1;\n    int min_distance = GRID_SIZE * GRID_SIZE;\n    \n    for (int i = 0; i < sinks.size(); i++) {\n        if (sinks[i].second > 0) {\n            int dist = abs(source.x - sinks[i].first.x) + abs(source.y - sinks[i].first.y);\n            if (dist < min_distance) {\n                min_distance = dist;\n                best_idx = i;\n            }\n        }\n    }\n    \n    return {best_idx, min_distance};\n}\n\n// Find best source to start with (closest to initial position or most beneficial)\nint find_best_initial_source(Position start_pos, vector<pair<Position, int>>& sources, vector<pair<Position, int>>& sinks) {\n    int best_idx = 0;\n    int best_score = -1;\n    \n    for (int i = 0; i < sources.size(); i++) {\n        if (sources[i].second > 0) {\n            auto nearest = find_nearest_sink(sources[i].first, sinks);\n            if (nearest.first != -1) {\n                int distance_to_source = abs(start_pos.x - sources[i].first.x) + abs(start_pos.y - sources[i].first.y);\n                int distance_source_to_sink = nearest.second;\n                int amount = min(sources[i].second, sinks[nearest.first].second);\n                \n                // Score based on amount vs distance\n                int score = (amount * 1000) / (distance_to_source + distance_source_to_sink + 1);\n                if (score > best_score) {\n                    best_score = score;\n                    best_idx = i;\n                }\n            }\n        }\n    }\n    \n    return best_idx;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    // Read grid\n    for (int i = 0; i < GRID_SIZE; i++) {\n        for (int j = 0; j < GRID_SIZE; j++) {\n            cin >> grid[i][j];\n        }\n    }\n    \n    // Collect sources (positive) and sinks (negative)\n    vector<pair<Position, int>> sources, sinks;\n    for (int i = 0; i < GRID_SIZE; i++) {\n        for (int j = 0; j < GRID_SIZE; j++) {\n            if (grid[i][j] > 0) {\n                sources.push_back({Position(i, j), grid[i][j]});\n            } else if (grid[i][j] < 0) {\n                sinks.push_back({Position(i, j), -grid[i][j]});\n            }\n        }\n    }\n    \n    vector<string> operations;\n    Position current_pos(0, 0);\n    int load = 0;\n    \n    // Process sources, but start with the most beneficial one\n    vector<bool> source_processed(sources.size(), false);\n    \n    // Find a good starting source\n    int current_source_idx = find_best_initial_source(current_pos, sources, sinks);\n    \n    int processed_sources = 0;\n    while (processed_sources < sources.size()) {\n        // Find next unprocessed source\n        if (source_processed[current_source_idx]) {\n            // Find next available source\n            current_source_idx = 0;\n            while (current_source_idx < sources.size() && \n                   (source_processed[current_source_idx] || sources[current_source_idx].second <= 0)) {\n                current_source_idx++;\n            }\n            \n            if (current_source_idx >= sources.size()) break;\n        }\n        \n        auto& source = sources[current_source_idx];\n        \n        if (source.second <= 0) {\n            source_processed[current_source_idx] = true;\n            processed_sources++;\n            continue;\n        }\n        \n        // Process this source completely\n        while (source.second > 0) {\n            // Find the nearest sink with demand\n            auto nearest = find_nearest_sink(source.first, sinks);\n            if (nearest.first == -1) break; // No more sinks\n            \n            int sink_idx = nearest.first;\n            auto& sink = sinks[sink_idx];\n            \n            if (sink.second <= 0) {\n                // This sink is depleted, try again to find another\n                continue;\n            }\n            \n            int transport_amount = min(source.second, sink.second);\n            if (transport_amount <= 0) break;\n            \n            // Move to source if needed\n            if (!(current_pos == source.first)) {\n                auto path = find_shortest_path(current_pos, source.first);\n                auto directions = path_to_directions(current_pos, path);\n                \n                for (char dir : directions) {\n                    operations.push_back(string(1, dir));\n                }\n                current_pos = source.first;\n            }\n            \n            // Load soil\n            operations.push_back(\"+\" + to_string(transport_amount));\n            load += transport_amount;\n            source.second -= transport_amount;\n            \n            // Move to sink\n            if (!(current_pos == sink.first)) {\n                auto path = find_shortest_path(current_pos, sink.first);\n                auto directions = path_to_directions(current_pos, path);\n                \n                for (char dir : directions) {\n                    operations.push_back(string(1, dir));\n                }\n                current_pos = sink.first;\n            }\n            \n            // Unload soil\n            operations.push_back(\"-\" + to_string(transport_amount));\n            load -= transport_amount;\n            sink.second -= transport_amount;\n        }\n        \n        source_processed[current_source_idx] = true;\n        processed_sources++;\n        \n        // Find next source to process (the one closest to current position)\n        int next_source_idx = -1;\n        int min_distance = GRID_SIZE * GRID_SIZE;\n        \n        for (int i = 0; i < sources.size(); i++) {\n            if (!source_processed[i] && sources[i].second > 0) {\n                int dist = abs(current_pos.x - sources[i].first.x) + abs(current_pos.y - sources[i].first.y);\n                if (dist < min_distance) {\n                    min_distance = dist;\n                    next_source_idx = i;\n                }\n            }\n        }\n        \n        if (next_source_idx != -1) {\n            current_source_idx = next_source_idx;\n        }\n    }\n    \n    // Output operations\n    for (const string& op : operations) {\n        cout << op << \"\\n\";\n    }\n    \n    return 0;\n}","ahc035":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <numeric>\n\nusing namespace std;\n\nconst int N = 6;\nconst int SEED_COUNT = 2 * N * (N - 1); // 60 seeds\n\n// Calculate total value of a seed\nint calculateValue(const vector<int>& seed) {\n    return accumulate(seed.begin(), seed.end(), 0);\n}\n\n// Generate placement with focus on high-value seed propagation\nvector<vector<int>> generatePlacement(const vector<vector<int>>& seeds) {\n    vector<vector<int>> placement(N, vector<int>(N));\n    vector<bool> seedUsed(SEED_COUNT, false);\n    \n    // Rank seeds by value (highest first)\n    vector<pair<int, int>> seedRanking;\n    for (int i = 0; i < SEED_COUNT; i++) {\n        seedRanking.push_back({calculateValue(seeds[i]), i});\n    }\n    sort(seedRanking.begin(), seedRanking.end(), greater<pair<int, int>>());\n    \n    // Position priority based on influence (number of adjacent cells)\n    vector<pair<int, pair<int, int>>> positions;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            int neighbors = 0;\n            if (i > 0) neighbors++;\n            if (i < N-1) neighbors++;\n            if (j > 0) neighbors++;\n            if (j < N-1) neighbors++;\n            positions.push_back({neighbors, {i, j}});\n        }\n    }\n    \n    // Sort positions by influence (more neighbors first)\n    sort(positions.begin(), positions.end(), greater<pair<int, pair<int, int>>>());\n    \n    // Assign seeds to positions\n    for (int posIdx = 0; posIdx < min((int)positions.size(), SEED_COUNT); posIdx++) {\n        int row = positions[posIdx].second.first;\n        int col = positions[posIdx].second.second;\n        int seedRankIdx = posIdx;\n        \n        // Make sure we don't go out of bounds\n        if (seedRankIdx >= SEED_COUNT) seedRankIdx = SEED_COUNT - 1;\n        \n        // Find the seed at this rank that hasn't been used\n        int actualSeedIdx = 0;\n        int count = 0;\n        for (int i = 0; i < SEED_COUNT && count <= seedRankIdx; i++) {\n            if (!seedUsed[seedRanking[i].second]) {\n                if (count == seedRankIdx) {\n                    actualSeedIdx = seedRanking[i].second;\n                    break;\n                }\n                count++;\n            }\n        }\n        \n        // If we couldn't find by rank, just find next available\n        if (seedUsed[actualSeedIdx]) {\n            for (int i = 0; i < SEED_COUNT; i++) {\n                if (!seedUsed[i]) {\n                    actualSeedIdx = i;\n                    break;\n                }\n            }\n        }\n        \n        placement[row][col] = actualSeedIdx;\n        seedUsed[actualSeedIdx] = true;\n    }\n    \n    // Simple fill for any remaining positions\n    int nextSeed = 0;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (!seedUsed[placement[i][j]]) {\n                while (nextSeed < SEED_COUNT && seedUsed[nextSeed]) nextSeed++;\n                if (nextSeed < SEED_COUNT) {\n                    placement[i][j] = nextSeed;\n                    seedUsed[nextSeed] = true;\n                }\n            }\n        }\n    }\n    \n    return placement;\n}\n\n// Alternative simpler approach\nvector<vector<int>> generateSimplePlacement(const vector<vector<int>>& seeds) {\n    vector<vector<int>> placement(N, vector<int>(N));\n    vector<bool> seedUsed(SEED_COUNT, false);\n    \n    // Simple strategy: place highest value seeds in a pattern that maximizes their influence\n    vector<pair<int, int>> pattern = {\n        {2,2}, {2,3}, {3,2}, {3,3},  // center\n        {1,2}, {1,3}, {2,1}, {2,4}, {3,1}, {3,4}, {4,2}, {4,3},  // ring 1\n        {0,2}, {0,3}, {2,0}, {3,0}, {2,5}, {3,5}, {5,2}, {5,3},  // ring 2\n        {1,1}, {1,4}, {4,1}, {4,4},  // diagonals\n        {0,1}, {0,4}, {1,0}, {1,5}, {4,0}, {4,5}, {5,1}, {5,4},  // edges\n        {0,0}, {0,5}, {5,0}, {5,5}   // corners\n    };\n    \n    // Rank all seeds\n    vector<int> seedOrder(SEED_COUNT);\n    for (int i = 0; i < SEED_COUNT; i++) {\n        seedOrder[i] = i;\n    }\n    \n    // Sort by value descending\n    sort(seedOrder.begin(), seedOrder.end(), [&](int a, int b) {\n        return calculateValue(seeds[a]) > calculateValue(seeds[b]);\n    });\n    \n    // Place seeds according to pattern\n    for (int i = 0; i < min((int)pattern.size(), SEED_COUNT); i++) {\n        int row = pattern[i].first;\n        int col = pattern[i].second;\n        int seedId = seedOrder[i];\n        placement[row][col] = seedId;\n    }\n    \n    return placement;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    int N_input, M, T;\n    cin >> N_input >> M >> T;\n    \n    vector<vector<int>> seeds(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 >> seeds[i][j];\n        }\n    }\n    \n    // Process T turns\n    for (int turn = 0; turn < T; turn++) {\n        // Use the simple but effective approach\n        auto placement = generateSimplePlacement(seeds);\n        \n        // Output placement\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                cout << placement[i][j];\n                if (j < N - 1) cout << \" \";\n            }\n            cout << endl;\n        }\n        cout.flush();\n        \n        // Read new seeds for next iteration\n        if (turn < T - 1) {\n            for (int i = 0; i < SEED_COUNT; i++) {\n                for (int j = 0; j < M; j++) {\n                    cin >> seeds[i][j];\n                }\n            }\n        }\n    }\n    \n    return 0;\n}","ahc038":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MAX_N = 30;\nconst int MAX_V = 15;\n\nstruct Point {\n    int x, y;\n    Point() : x(0), y(0) {}\n    Point(int x, int y) : x(x), y(y) {}\n    Point operator+(const Point& p) const { return Point(x + p.x, y + p.y); }\n    Point operator-(const Point& p) const { return Point(x - p.x, y - p.y); }\n    bool operator==(const Point& p) const { return x == p.x && y == p.y; }\n    bool operator!=(const Point& p) const { return !(*this == p); }\n};\n\nconst Point DIRS[4] = {Point(0, 1), Point(1, 0), Point(0, -1), Point(-1, 0)}; // R, D, L, U\nconst char DIR_CHARS[4] = {'R', 'D', 'L', 'U'};\n\nclass RobotArm {\npublic:\n    int V;\n    vector<pair<int, int>> tree; // {parent, length}\n    Point root_pos;\n    \n    RobotArm(int v) : V(v) {\n        tree.resize(V);\n    }\n};\n\nclass Solution {\nprivate:\n    int N, M, V;\n    vector<vector<int>> initial, target;\n    vector<Point> initial_pos, target_pos;\n    \npublic:\n    void readInput() {\n        cin >> N >> M >> V;\n        initial.assign(N, vector<int>(N, 0));\n        target.assign(N, vector<int>(N, 0));\n        \n        for (int i = 0; i < N; i++) {\n            string line;\n            cin >> line;\n            for (int j = 0; j < N; j++) {\n                initial[i][j] = line[j] - '0';\n                if (initial[i][j]) {\n                    initial_pos.push_back(Point(i, j));\n                }\n            }\n        }\n        \n        for (int i = 0; i < N; i++) {\n            string line;\n            cin >> line;\n            for (int j = 0; j < N; j++) {\n                target[i][j] = line[j] - '0';\n                if (target[i][j]) {\n                    target_pos.push_back(Point(i, j));\n                }\n            }\n        }\n    }\n    \n    RobotArm designArm() {\n        RobotArm arm(V);\n        \n        // Simple star structure: root with fingers of varying lengths\n        for (int i = 1; i < V; i++) {\n            // Alternate between longer and shorter fingers\n            int length = (i % 3 == 0) ? min(3, N-1) : ((i % 3 == 1) ? min(2, N-1) : 1);\n            arm.tree[i] = {0, length}; // All connected to root\n        }\n        \n        // Initial position at (0, 0)\n        arm.root_pos = Point(0, 0);\n        \n        return arm;\n    }\n    \n    // Find all leaf vertices (fingertips)\n    vector<int> findLeaves(const RobotArm& arm) {\n        vector<int> is_child(arm.V, 0);\n        for (int i = 1; i < arm.V; i++) {\n            if (arm.tree[i].first < arm.V) {\n                is_child[arm.tree[i].first] = 1;\n            }\n        }\n        \n        vector<int> leaves;\n        for (int i = 0; i < arm.V; i++) {\n            if (!is_child[i]) {\n                leaves.push_back(i);\n            }\n        }\n        return leaves;\n    }\n    \n    string createMoveString(char move, const vector<char>& rotations, const vector<char>& actions) {\n        string result;\n        result += move;\n        for (int i = 1; i < (int)rotations.size(); i++) {\n            result += rotations[i];\n        }\n        for (int i = 0; i < (int)actions.size(); i++) {\n            result += actions[i];\n        }\n        return result;\n    }\n    \n    // Simple position calculation - each vertex extends in fixed directions\n    vector<Point> calculateVertexPositions(const Point& root_pos, const vector<int>& directions, const RobotArm& arm) {\n        vector<Point> positions(arm.V);\n        positions[0] = root_pos;\n        \n        // Simple model: vertex i extends from its parent in a direction based on rotation\n        for (int i = 1; i < arm.V; i++) {\n            int parent = arm.tree[i].first;\n            int length = arm.tree[i].second;\n            \n            // Direction based on rotation state (0=right, 1=down, 2=left, 3=up)\n            int dir = directions[i];\n            Point offset;\n            if (dir == 0) offset = Point(0, length);      // Right\n            else if (dir == 1) offset = Point(length, 0); // Down\n            else if (dir == 2) offset = Point(0, -length); // Left\n            else offset = Point(-length, 0);              // Up\n            \n            positions[i] = positions[parent] + offset;\n        }\n        \n        return positions;\n    }\n    \n    vector<string> solve(RobotArm& arm) {\n        vector<string> operations;\n        Point current_pos = arm.root_pos;\n        vector<vector<int>> current_grid = initial;\n        \n        // Track rotation state of each edge (0=right, 1=down, 2=left, 3=up)\n        vector<int> edge_directions(V, 0);\n        vector<int> holding(V, 0);\n        \n        // Find leaf vertices\n        vector<int> leaves = findLeaves(arm);\n        \n        // Simple but more robust approach\n        int turns = 0;\n        int max_turns = min(2000, (int)(1e5));\n        \n        random_device rd;\n        mt19937 gen(rd());\n        uniform_int_distribution<> dis(0, 3);\n        \n        while (turns < max_turns) {\n            // Random but valid movement\n            char move_char = '.';\n            Point new_pos = current_pos;\n            \n            int move_choice = dis(gen) % 5;\n            if (move_choice < 4) {\n                new_pos = current_pos + DIRS[move_choice];\n                if (new_pos.x >= 0 && new_pos.x < N && new_pos.y >= 0 && new_pos.y < N) {\n                    current_pos = new_pos;\n                    move_char = DIR_CHARS[move_choice];\n                }\n            }\n            \n            // Occasional rotations\n            vector<char> rotations(V, '.');\n            for (int i = 1; i < V; i++) {\n                if (dis(gen) % 10 == 0) { // Rare rotations\n                    if (dis(gen) % 2 == 0) {\n                        rotations[i] = 'R';\n                        edge_directions[i] = (edge_directions[i] + 1) % 4;\n                    } else {\n                        rotations[i] = 'L';\n                        edge_directions[i] = (edge_directions[i] + 3) % 4;\n                    }\n                }\n            }\n            \n            // Calculate vertex positions\n            vector<Point> vertex_positions = calculateVertexPositions(current_pos, edge_directions, arm);\n            \n            // Actions for leaves only\n            vector<char> actions(V, '.');\n            for (int leaf : leaves) {\n                if (leaf < V && leaf > 0) { // Safety check and only for actual leaves (not root)\n                    Point pos = vertex_positions[leaf];\n                    // Check bounds carefully\n                    if (pos.x >= 0 && pos.x < N && pos.y >= 0 && pos.y < N) {\n                        // Double check that we're actually at a leaf position\n                        bool is_leaf = true;\n                        for (int j = 1; j < V; j++) {\n                            if (arm.tree[j].first == leaf) {\n                                is_leaf = false;\n                                break;\n                            }\n                        }\n                        \n                        if (is_leaf) {\n                            if (holding[leaf] == 0 && current_grid[pos.x][pos.y] == 1 && target[pos.x][pos.y] == 0) {\n                                // Grab takoyaki\n                                actions[leaf] = 'P';\n                                holding[leaf] = 1;\n                                current_grid[pos.x][pos.y] = 0;\n                            } else if (holding[leaf] == 1 && current_grid[pos.x][pos.y] == 0 && target[pos.x][pos.y] == 1) {\n                                // Place takoyaki\n                                actions[leaf] = 'P';\n                                holding[leaf] = 0;\n                                current_grid[pos.x][pos.y] = 1;\n                            }\n                        }\n                    }\n                }\n            }\n            \n            operations.push_back(createMoveString(move_char, rotations, actions));\n            turns++;\n        }\n        \n        return operations;\n    }\n    \n    void outputSolution(RobotArm& arm, const vector<string>& operations) {\n        // Output arm design\n        cout << arm.V << endl;\n        for (int i = 1; i < arm.V; i++) {\n            cout << arm.tree[i].first << \" \" << arm.tree[i].second << endl;\n        }\n        cout << arm.root_pos.x << \" \" << arm.root_pos.y << endl;\n        \n        // Output operations\n        for (const string& op : operations) {\n            cout << op << endl;\n        }\n    }\n    \n    void run() {\n        readInput();\n        RobotArm arm = designArm();\n        vector<string> operations = solve(arm);\n        outputSolution(arm, operations);\n    }\n};\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    Solution solution;\n    solution.run();\n    \n    return 0;\n}","ahc039":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MAX_N = 5000;\nconst int FIELD_SIZE = 100000;\n\nstruct Point {\n    int x, y;\n    Point() : x(0), y(0) {}\n    Point(int x, int y) : x(x), y(y) {}\n};\n\nstruct Rectangle {\n    int x1, y1, x2, y2;\n    int mackerels, sardines;\n    int score;\n    \n    Rectangle() : x1(0), y1(0), x2(0), y2(0), mackerels(0), sardines(0), score(0) {}\n    Rectangle(int x1, int y1, int x2, int y2) : x1(x1), y1(y1), x2(x2), y2(y2), mackerels(0), sardines(0), score(0) {}\n    \n    bool isValid() const {\n        return x1 >= 0 && x2 <= FIELD_SIZE && y1 >= 0 && y2 <= FIELD_SIZE && \n               x1 < x2 && y1 < y2 && perimeter() <= 400000;\n    }\n    \n    int perimeter() const {\n        return 2 * (x2 - x1 + y2 - y1);\n    }\n};\n\nvector<Point> mackerels(MAX_N);\nvector<Point> sardines(MAX_N);\n\n// Count points in rectangle\npair<int, int> countPoints(const Rectangle& rect) {\n    int m_count = 0, s_count = 0;\n    \n    for (const auto& p : mackerels) {\n        if (p.x >= rect.x1 && p.x <= rect.x2 && p.y >= rect.y1 && p.y <= rect.y2) {\n            m_count++;\n        }\n    }\n    \n    for (const auto& p : sardines) {\n        if (p.x >= rect.x1 && p.x <= rect.x2 && p.y >= rect.y1 && p.y <= rect.y2) {\n            s_count++;\n        }\n    }\n    \n    return {m_count, s_count};\n}\n\n// Generate rectangles around mackerel points with adaptive sizing\nvector<Rectangle> generateRectangles() {\n    vector<Rectangle> rectangles;\n    \n    // Try different rectangle sizes centered around mackerels\n    vector<int> sizes = {3000, 5000, 8000, 12000, 15000, 20000, 25000, 30000};\n    \n    for (int i = 0; i < min(200, MAX_N); i++) {\n        const Point& center = mackerels[i];\n        \n        for (int size : sizes) {\n            int x1 = max(0, center.x - size/2);\n            int y1 = max(0, center.y - size/2);\n            int x2 = min(FIELD_SIZE, center.x + size/2);\n            int y2 = min(FIELD_SIZE, center.y + size/2);\n            \n            Rectangle rect(x1, y1, x2, y2);\n            if (rect.isValid()) {\n                auto counts = countPoints(rect);\n                rect.mackerels = counts.first;\n                rect.sardines = counts.second;\n                rect.score = rect.mackerels - rect.sardines;\n                \n                if (rect.score > 0) {\n                    rectangles.push_back(rect);\n                }\n            }\n        }\n    }\n    \n    // Also try rectangles around clusters using simple grid approach\n    const int GRID_SIZE = 20;\n    vector<vector<pair<int, int>>> grid(GRID_SIZE, vector<pair<int, int>>(GRID_SIZE, {0, 0}));\n    \n    // Count mackerels and sardines in each grid cell\n    for (const auto& p : mackerels) {\n        int gx = min(GRID_SIZE - 1, p.x * GRID_SIZE / (FIELD_SIZE + 1));\n        int gy = min(GRID_SIZE - 1, p.y * GRID_SIZE / (FIELD_SIZE + 1));\n        grid[gx][gy].first++;\n    }\n    \n    for (const auto& p : sardines) {\n        int gx = min(GRID_SIZE - 1, p.x * GRID_SIZE / (FIELD_SIZE + 1));\n        int gy = min(GRID_SIZE - 1, p.y * GRID_SIZE / (FIELD_SIZE + 1));\n        grid[gx][gy].second++;\n    }\n    \n    // Generate rectangles around high mackerel, low sardine areas\n    for (int i = 0; i < GRID_SIZE; i++) {\n        for (int j = 0; j < GRID_SIZE; j++) {\n            if (grid[i][j].first > grid[i][j].second) {  // More mackerels than sardines\n                // Try different window sizes\n                for (int window = 2; window <= 6; window++) {\n                    int x1_grid = max(0, i - window/2);\n                    int y1_grid = max(0, j - window/2);\n                    int x2_grid = min(GRID_SIZE - 1, i + window/2);\n                    int y2_grid = min(GRID_SIZE - 1, j + window/2);\n                    \n                    int x1 = x1_grid * FIELD_SIZE / GRID_SIZE;\n                    int y1 = y1_grid * FIELD_SIZE / GRID_SIZE;\n                    int x2 = min(FIELD_SIZE, (x2_grid + 1) * FIELD_SIZE / GRID_SIZE);\n                    int y2 = min(FIELD_SIZE, (y2_grid + 1) * FIELD_SIZE / GRID_SIZE);\n                    \n                    Rectangle rect(x1, y1, x2, y2);\n                    if (rect.isValid()) {\n                        auto counts = countPoints(rect);\n                        rect.mackerels = counts.first;\n                        rect.sardines = counts.second;\n                        rect.score = rect.mackerels - rect.sardines;\n                        \n                        if (rect.score > 0) {\n                            rectangles.push_back(rect);\n                        }\n                    }\n                }\n            }\n        }\n    }\n    \n    // Sort by score\n    sort(rectangles.begin(), rectangles.end(), [](const Rectangle& a, const Rectangle& b) {\n        return a.score > b.score;\n    });\n    \n    // Remove duplicates\n    rectangles.erase(unique(rectangles.begin(), rectangles.end(), \n                           [](const Rectangle& a, const Rectangle& b) {\n                               return a.x1 == b.x1 && a.y1 == b.y1 && a.x2 == b.x2 && a.y2 == b.y2;\n                           }), rectangles.end());\n    \n    return rectangles;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    int N;\n    cin >> N;\n    \n    for (int i = 0; i < N; i++) {\n        cin >> mackerels[i].x >> mackerels[i].y;\n    }\n    \n    for (int i = 0; i < N; i++) {\n        cin >> sardines[i].x >> sardines[i].y;\n    }\n    \n    // Generate rectangles\n    vector<Rectangle> rectangles = generateRectangles();\n    \n    // Find the best rectangle\n    Rectangle best_rect(10000, 10000, 90000, 90000);  // Default fallback\n    int best_score = -1;\n    \n    if (!rectangles.empty()) {\n        best_rect = rectangles[0];\n        best_score = best_rect.score;\n    }\n    \n    // Output the rectangle as a polygon\n    cout << \"4\\n\";\n    cout << best_rect.x1 << \" \" << best_rect.y1 << \"\\n\";\n    cout << best_rect.x2 << \" \" << best_rect.y1 << \"\\n\";\n    cout << best_rect.x2 << \" \" << best_rect.y2 << \"\\n\";\n    cout << best_rect.x1 << \" \" << best_rect.y2 << \"\\n\";\n    \n    return 0;\n}","ahc040":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Placement {\n    int p, r, d, b;\n    \n    Placement(int _p, int _r, char _d, int _b) : p(_p), r(_r), d(_d), b(_b) {}\n};\n\n// Strategy that focuses on efficient packing without skipping rectangles\nvector<Placement> efficientPacking(const vector<pair<int,int>>& rectangles, mt19937& rng, int attempt_num) {\n    vector<Placement> placements;\n    int n = rectangles.size();\n    \n    for (int i = 0; i < n; i++) {\n        int rotate = 0;\n        \n        // Smart rotation strategy\n        if (attempt_num % 4 == 0) {\n            // Try to minimize the larger dimension\n            rotate = (max(rectangles[i].first, rectangles[i].second) == rectangles[i].first) ? 1 : 0;\n        } else if (attempt_num % 4 == 1) {\n            // Aspect ratio based rotation\n            double ratio = (double)max(rectangles[i].first, rectangles[i].second) / \n                          min(rectangles[i].first, rectangles[i].second);\n            if (ratio > 2.5) {\n                rotate = 1; // Rotate very long rectangles\n            } else if (ratio < 0.4) {\n                rotate = 1; // Rotate very tall rectangles\n            }\n        } else if (attempt_num % 4 == 2) {\n            // Position and size based\n            long long area = (long long)rectangles[i].first * rectangles[i].second;\n            if (area > 1e10) {\n                // Large rectangles - rotate to be more compact\n                rotate = (rectangles[i].first > rectangles[i].second) ? 1 : 0;\n            } else {\n                rotate = (i + attempt_num) % 2;\n            }\n        } else {\n            // Simple alternating\n            rotate = (i % 2 == 0) ? 0 : 1;\n        }\n        \n        char direction = 'U';\n        int reference = -1;\n        \n        if (i > 0) {\n            // Strategic placement based on rectangle properties\n            if (attempt_num % 5 == 0) {\n                // Always build up\n                direction = 'U';\n                reference = i - 1;\n            } else if (attempt_num % 5 == 1) {\n                // Always build left\n                direction = 'L';\n                reference = i - 1;\n            } else if (attempt_num % 5 == 2) {\n                // Based on rectangle dimensions\n                if (rectangles[i].first > rectangles[i].second * 1.5) {\n                    direction = 'L'; // Wide rectangles go left\n                } else if (rectangles[i].second > rectangles[i].first * 1.5) {\n                    direction = 'U'; // Tall rectangles go up\n                } else {\n                    direction = (uniform_int_distribution<int>(0, 1)(rng) == 0) ? 'U' : 'L';\n                }\n                reference = i - 1;\n            } else if (attempt_num % 5 == 3 && i > 1) {\n                // Reference earlier rectangles sometimes\n                int ref_choice = uniform_int_distribution<int>(0, 2)(rng);\n                if (ref_choice == 0) {\n                    reference = max(0, i - 2);\n                } else if (ref_choice == 1) {\n                    reference = max(0, i - 1 - (int)(rng() % min(3, i)));\n                } else {\n                    reference = i - 1;\n                }\n                direction = (uniform_int_distribution<int>(0, 1)(rng) == 0) ? 'U' : 'L';\n            } else {\n                // Default smart strategy\n                reference = i - 1;\n                direction = (i % 3 == 0) ? 'U' : ((i % 3 == 1) ? 'L' : ((i + attempt_num) % 2 ? 'U' : 'L'));\n            }\n        }\n        \n        placements.emplace_back(i, rotate, direction, reference);\n    }\n    \n    return placements;\n}\n\n// Perimeter-focused strategy\nvector<Placement> perimeterFocused(const vector<pair<int,int>>& rectangles, mt19937& rng, int attempt_num) {\n    vector<Placement> placements;\n    int n = rectangles.size();\n    \n    for (int i = 0; i < n; i++) {\n        int rotate = 0;\n        \n        // Rotation to minimize perimeter impact\n        long long perimeter = rectangles[i].first + rectangles[i].second;\n        \n        if (attempt_num % 3 == 0) {\n            // Try to minimize the dimension that contributes more to perimeter\n            if (rectangles[i].first > rectangles[i].second * 1.3) {\n                rotate = 1; // Wide rectangle - rotate to reduce width impact\n            }\n        } else if (attempt_num % 3 == 1) {\n            // Smart aspect ratio handling\n            double ratio = (double)rectangles[i].first / rectangles[i].second;\n            if (ratio > 3.0 || ratio < 0.33) {\n                rotate = 1; // Rotate extreme aspect ratios\n            }\n        } else {\n            // Position-based rotation\n            rotate = (i + attempt_num) % 2;\n        }\n        \n        char direction = 'U';\n        int reference = -1;\n        \n        if (i > 0) {\n            if (attempt_num % 4 == 0) {\n                // Try to build a more compact shape\n                if (perimeter > 100000) {\n                    // Large perimeter rectangle - place more carefully\n                    direction = (rectangles[i].first > rectangles[i].second) ? 'L' : 'U';\n                } else {\n                    direction = (uniform_int_distribution<int>(0, 1)(rng) == 0) ? 'U' : 'L';\n                }\n                reference = i - 1;\n            } else if (attempt_num % 4 == 1) {\n                // Alternate placement patterns\n                direction = (i % 2 == 0) ? 'U' : 'L';\n                reference = max(0, i - 1 - (int)(rng() % min(2, i)));\n            } else if (attempt_num % 4 == 2) {\n                // Try to minimize maximum dimension\n                long long max_dim = max(rectangles[i].first, rectangles[i].second);\n                if (max_dim > 50000) {\n                    direction = (rectangles[i].first > rectangles[i].second) ? 'L' : 'U';\n                } else {\n                    direction = (uniform_int_distribution<int>(0, 1)(rng) == 0) ? 'U' : 'L';\n                }\n                reference = i - 1;\n            } else {\n                // Default strategy\n                reference = i - 1;\n                direction = (i % 3 == 0) ? 'U' : ((i % 3 == 1) ? 'L' : 'U');\n            }\n        }\n        \n        placements.emplace_back(i, rotate, direction, reference);\n    }\n    \n    return placements;\n}\n\n// Compact arrangement strategy\nvector<Placement> compactArrangement(const vector<pair<int,int>>& rectangles, mt19937& rng, int attempt_num) {\n    vector<Placement> placements;\n    int n = rectangles.size();\n    \n    // Try to create a more square-like final arrangement\n    for (int i = 0; i < n; i++) {\n        int rotate = 0;\n        \n        // Rotation to promote compactness\n        double ratio = (double)rectangles[i].first / rectangles[i].second;\n        if (ratio > 2.0) {\n            rotate = 1; // Very wide - rotate to be more compact\n        } else if (ratio < 0.5) {\n            rotate = 1; // Very tall - rotate to be more compact\n        } else if (attempt_num % 3 != 0) {\n            // Sometimes rotate for variation\n            rotate = uniform_int_distribution<int>(0, 1)(rng);\n        }\n        \n        char direction = 'U';\n        int reference = -1;\n        \n        if (i > 0) {\n            // Try to build compactly by alternating strategies\n            if (i < n/3) {\n                // First third - build up primarily\n                direction = (uniform_int_distribution<int>(0, 3)(rng) == 0) ? 'L' : 'U';\n                reference = i - 1;\n            } else if (i < 2*n/3) {\n                // Middle third - mix strategies\n                direction = (i % 2 == 0) ? 'U' : 'L';\n                reference = max(0, i - 1 - (int)(rng() % min(2, i)));\n            } else {\n                // Last third - try to fill gaps\n                if (uniform_int_distribution<int>(0, 2)(rng) == 0) {\n                    direction = 'L';\n                    reference = max(0, i - 1 - (int)(rng() % min(3, i)));\n                } else {\n                    direction = 'U';\n                    reference = i - 1;\n                }\n            }\n        }\n        \n        placements.emplace_back(i, rotate, direction, reference);\n    }\n    \n    return placements;\n}\n\n// Simple but effective baseline strategy\nvector<Placement> baselineStrategy(const vector<pair<int,int>>& rectangles, mt19937& rng, int attempt_num) {\n    vector<Placement> placements;\n    int n = rectangles.size();\n    \n    for (int i = 0; i < n; i++) {\n        // Simple heuristic: rotate wide rectangles\n        int rotate = (rectangles[i].first > rectangles[i].second * 1.5) ? 1 : 0;\n        \n        // Add some variation based on attempt\n        if (attempt_num > 0 && (i + attempt_num) % 7 == 0) {\n            rotate = 1 - rotate; // Invert rotation sometimes\n        }\n        \n        char direction = 'U';\n        int reference = -1;\n        \n        if (i > 0) {\n            // Vary placement by attempt\n            if (attempt_num % 3 == 0) {\n                direction = 'U';\n                reference = i - 1;\n            } else if (attempt_num % 3 == 1) {\n                direction = 'L';\n                reference = i - 1;\n            } else {\n                direction = (uniform_int_distribution<int>(0, 1)(rng) == 0) ? 'U' : 'L';\n                reference = i - 1;\n            }\n        }\n        \n        placements.emplace_back(i, rotate, direction, reference);\n    }\n    \n    return placements;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    int N, T, sigma;\n    cin >> N >> T >> sigma;\n    \n    vector<pair<int,int>> measured_rectangles(N);\n    for (int i = 0; i < N; i++) {\n        cin >> measured_rectangles[i].first >> measured_rectangles[i].second;\n    }\n    \n    // Initialize random number generator\n    random_device rd;\n    mt19937 rng(rd());\n    rng.discard(3000); // Ensure good randomness\n    \n    // Try different strategies for each turn\n    for (int turn = 0; turn < T; turn++) {\n        vector<Placement> placements;\n        \n        // Strategy selection based on turn distribution\n        if (turn % 4 == 0) {\n            placements = efficientPacking(measured_rectangles, rng, turn);\n        } else if (turn % 4 == 1) {\n            placements = perimeterFocused(measured_rectangles, rng, turn);\n        } else if (turn % 4 == 2) {\n            placements = compactArrangement(measured_rectangles, rng, turn);\n        } else {\n            placements = baselineStrategy(measured_rectangles, rng, turn);\n        }\n        \n        // Output placements\n        cout << placements.size() << \"\\n\";\n        for (const auto& p : placements) {\n            cout << p.p << \" \" << p.r << \" \" << (char)p.d << \" \" << p.b << \"\\n\";\n        }\n        cout << flush;\n        \n        // Read measurement result\n        int W_meas, H_meas;\n        cin >> W_meas >> H_meas;\n    }\n    \n    return 0;\n}","ahc041":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MAXN = 1000;\nconst int MAXH = 10;\n\nint N, M, H;\nvector<int> A;\nvector<vector<int>> adj;\n\n// Ultra-simple approach without expensive validation\nvector<int> buildSimpleSolution() {\n    vector<int> parent(N, -1);\n    vector<bool> used(N, false);\n    \n    // Root selection by beauty * degree\n    vector<pair<int, int>> roots;\n    for (int i = 0; i < N; i++) {\n        roots.push_back({A[i] * (int)adj[i].size(), i});\n    }\n    sort(roots.rbegin(), roots.rend());\n    \n    // Build trees\n    for (auto& [score, root] : roots) {\n        if (used[root]) continue;\n        \n        queue<pair<int, int>> q; // {vertex, depth}\n        q.push({root, 0});\n        used[root] = true;\n        \n        while (!q.empty()) {\n            auto [v, depth] = q.front();\n            q.pop();\n            \n            if (depth >= H) continue;\n            \n            // Simple greedy: take neighbors in order they appear\n            vector<pair<int, int>> candidates;\n            for (int u : adj[v]) {\n                if (!used[u]) {\n                    candidates.push_back({A[u], u});\n                }\n            }\n            sort(candidates.rbegin(), candidates.rend());\n            \n            for (auto& [beauty, u] : candidates) {\n                if (!used[u]) {\n                    parent[u] = v;\n                    used[u] = true;\n                    q.push({u, depth + 1});\n                }\n            }\n        }\n    }\n    \n    return parent;\n}\n\n// Even simpler approach\nvector<int> buildAlternativeSolution() {\n    vector<int> parent(N, -1);\n    vector<bool> used(N, false);\n    \n    // Just use vertices in beauty order as roots\n    vector<pair<int, int>> order;\n    for (int i = 0; i < N; i++) {\n        order.push_back({A[i], i});\n    }\n    sort(order.rbegin(), order.rend());\n    \n    for (auto& [beauty, root] : order) {\n        if (used[root]) continue;\n        \n        queue<pair<int, int>> q;\n        q.push({root, 0});\n        used[root] = true;\n        \n        while (!q.empty()) {\n            auto [v, depth] = q.front();\n            q.pop();\n            \n            if (depth >= H) continue;\n            \n            // Simple greedy assignment\n            for (int u : adj[v]) {\n                if (!used[u]) {\n                    parent[u] = v;\n                    used[u] = true;\n                    q.push({u, depth + 1});\n                }\n            }\n        }\n    }\n    \n    return parent;\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    cin >> N >> M >> H;\n    A.resize(N);\n    for (int i = 0; i < N; i++) {\n        cin >> A[i];\n    }\n    \n    adj.resize(N);\n    for (int i = 0; i < M; i++) {\n        int u, v;\n        cin >> u >> v;\n        adj[u].push_back(v);\n        adj[v].push_back(u);\n    }\n    \n    // Read coordinates\n    for (int i = 0; i < N; i++) {\n        int x, y;\n        cin >> x >> y;\n    }\n    \n    // Build solution - just one fast approach\n    vector<int> solution = buildSimpleSolution();\n    \n    // Output\n    for (int i = 0; i < N; i++) {\n        cout << solution[i];\n        if (i < N - 1) cout << \" \";\n    }\n    cout << \"\\n\";\n    \n    return 0;\n}","ahc042":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MAX_N = 20;\nchar board[MAX_N][MAX_N];\nint N;\n\nstruct Position {\n    int row, col;\n};\n\nvector<Position> oni_list;\nvector<Position> fuku_list;\n\nstruct Move {\n    char direction;\n    int index;\n};\n\nvector<Move> moves;\n\n// Check if column j has any Fukunokami above row r\nbool hasFukuAbove(int r, int j) {\n    for (int i = 0; i < r; i++) {\n        if (board[i][j] == 'o') return true;\n    }\n    return false;\n}\n\n// Check if column j has any Fukunokami below row r\nbool hasFukuBelow(int r, int j) {\n    for (int i = r + 1; i < N; i++) {\n        if (board[i][j] == 'o') return true;\n    }\n    return false;\n}\n\n// Check if row i has any Fukunokami to the left of column c\nbool hasFukuLeft(int i, int c) {\n    for (int j = 0; j < c; j++) {\n        if (board[i][j] == 'o') return true;\n    }\n    return false;\n}\n\n// Check if row i has any Fukunokami to the right of column c\nbool hasFukuRight(int i, int c) {\n    for (int j = c + 1; j < N; j++) {\n        if (board[i][j] == 'o') return true;\n    }\n    return false;\n}\n\nvoid addMove(char dir, int idx) {\n    moves.push_back({dir, idx});\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    cin >> N;\n    for (int i = 0; i < N; i++) {\n        string row;\n        cin >> row;\n        for (int j = 0; j < N; j++) {\n            board[i][j] = row[j];\n            if (row[j] == 'x') {\n                oni_list.push_back({i, j});\n            } else if (row[j] == 'o') {\n                fuku_list.push_back({i, j});\n            }\n        }\n    }\n    \n    // Process each Oni\n    for (const auto& oni : oni_list) {\n        int r = oni.row;\n        int c = oni.col;\n        \n        // Try to remove this Oni in the most efficient way\n        // Prefer directions that require fewer moves\n        \n        // Up direction: shift column down\n        if (!hasFukuAbove(r, c)) {\n            // Shift down r+1 times to move Oni out, then shift back\n            for (int i = 0; i < r + 1; i++) addMove('D', c);\n            for (int i = 0; i < r + 1; i++) addMove('U', c);\n        }\n        // Down direction: shift column up\n        else if (!hasFukuBelow(r, c)) {\n            // Shift up N-r times to move Oni out, then shift back\n            for (int i = 0; i < N - r; i++) addMove('U', c);\n            for (int i = 0; i < N - r; i++) addMove('D', c);\n        }\n        // Left direction: shift row right\n        else if (!hasFukuLeft(r, c)) {\n            // Shift right c+1 times to move Oni out, then shift back\n            for (int i = 0; i < c + 1; i++) addMove('R', r);\n            for (int i = 0; i < c + 1; i++) addMove('L', r);\n        }\n        // Right direction: shift row left\n        else if (!hasFukuRight(r, c)) {\n            // Shift left N-c times to move Oni out, then shift back\n            for (int i = 0; i < N - c; i++) addMove('L', r);\n            for (int i = 0; i < N - c; i++) addMove('R', r);\n        }\n    }\n    \n    // Output the moves\n    for (const auto& move : moves) {\n        cout << move.direction << \" \" << move.index << \"\\n\";\n    }\n    \n    return 0;\n}","ahc044":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 100;\nconst int L = 500000;\nconst int SIM_STEPS = 1000000;\n\nint T[N];\nint a[N], b[N];\n\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\n\n// Fast simulation\nvector<long long> simulate_sequence(int a[], int b[], int steps = SIM_STEPS) {\n    vector<long long> actual(N, 0);\n    vector<int> count(N, 0);\n    \n    int current = 0;\n    actual[0]++;\n    count[0]++;\n    \n    // Run simulation\n    for (int step = 1; step < steps; step++) {\n        int next;\n        if (count[current] % 2 == 1) {\n            next = a[current];\n        } else {\n            next = b[current];\n        }\n        current = next;\n        actual[current]++;\n        count[current]++;\n    }\n    \n    // Scale to match L weeks\n    for (int i = 0; i < N; i++) {\n        actual[i] = (1LL * actual[i] * L) / steps;\n    }\n    \n    return actual;\n}\n\n// Calculate error\nlong long calculate_error(const vector<long long>& actual) {\n    long long error = 0;\n    for (int i = 0; i < N; i++) {\n        error += abs(actual[i] - (long long)T[i]);\n    }\n    return error;\n}\n\n// Effective initialization\nvoid initialize_assignment() {\n    // Sort employees by target frequency (descending)\n    vector<pair<int, int>> targets;\n    for (int i = 0; i < N; i++) {\n        targets.push_back({T[i], i});\n    }\n    sort(targets.rbegin(), targets.rend());\n    \n    // Create assignment with good distribution properties\n    for (int i = 0; i < N; i++) {\n        int emp = targets[i].second;\n        \n        // Use prime-based deterministic assignment for good distribution\n        a[emp] = targets[(i * 11 + 13) % N].second;\n        b[emp] = targets[(i * 17 + 23) % N].second;\n    }\n    \n    // Strategic adjustments for high-target employees\n    for (int i = 0; i < min(20, N); i++) {\n        int high_emp = targets[i].second;\n        // Make sure this high-target employee is accessible\n        int from_emp = targets[(i + 7) % N].second;\n        if (i % 3 == 0) {\n            a[from_emp] = high_emp;\n        } else if (i % 3 == 1) {\n            b[from_emp] = high_emp;\n        }\n        // else do nothing for variety\n    }\n}\n\n// Efficient improvement with early stopping\nvoid efficient_improvement() {\n    auto actual = simulate_sequence(a, b);\n    long long current_error = calculate_error(actual);\n    \n    // Quick improvement with early stopping\n    int no_improvement_count = 0;\n    const int MAX_NO_IMPROVEMENT = 15;\n    \n    for (int iter = 0; iter < 30 && no_improvement_count < MAX_NO_IMPROVEMENT; iter++) {\n        // Pick random employee\n        int emp = rng() % N;\n        \n        // Save current state\n        int old_a = a[emp];\n        int old_b = b[emp];\n        \n        // Smart modification based on target\n        uniform_int_distribution<int> dist(0, N-1);\n        \n        // Higher probability of pointing to employees with appropriate targets\n        if (T[emp] > L / (2 * N)) {\n            // High target - modify more conservatively\n            a[emp] = dist(rng);\n            b[emp] = dist(rng);\n        } else {\n            // Lower target - more flexible modifications\n            a[emp] = dist(rng);\n            b[emp] = dist(rng);\n        }\n        \n        // Fast evaluation\n        auto new_actual = simulate_sequence(a, b, 600000);\n        long long new_error = calculate_error(new_actual);\n        \n        // Keep if better, otherwise revert\n        if (new_error < current_error) {\n            current_error = new_error;\n            no_improvement_count = 0; // Reset counter\n            actual = new_actual; // Update for next iteration\n        } else {\n            a[emp] = old_a;\n            b[emp] = old_b;\n            no_improvement_count++;\n        }\n    }\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    int n, l;\n    cin >> n >> l;\n    for (int i = 0; i < n; i++) {\n        cin >> T[i];\n    }\n    \n    // Initialize with good strategy\n    initialize_assignment();\n    \n    // Efficient improvement\n    efficient_improvement();\n    \n    // Output result\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\nconst int N = 800;\n\nint n, m, q, L, W;\nvector<int> G;\nvector<int> lx, rx, ly, ry;\n\n// Query function\nvector<pair<int, int>> query(const vector<int>& cities) {\n    if (cities.size() < 2) return {};\n    \n    cout << \"? \" << cities.size();\n    for (int city : cities) cout << \" \" << city;\n    cout << endl;\n    cout.flush();\n    \n    vector<pair<int, int>> result(cities.size() - 1);\n    for (int i = 0; i < cities.size() - 1; i++) {\n        int u, v;\n        cin >> u >> v;\n        if (u > v) swap(u, v);\n        result[i] = {u, v};\n    }\n    return result;\n}\n\n// Calculate center of a city's rectangle\npair<int, int> get_center(int city) {\n    return {(lx[city] + rx[city]) / 2, (ly[city] + ry[city]) / 2};\n}\n\n// Group cities using simple but effective sorting\nvector<vector<int>> create_groups() {\n    vector<int> cities(n);\n    for (int i = 0; i < n; i++) cities[i] = i;\n    \n    // Sort by center coordinates\n    sort(cities.begin(), cities.end(), [&](int a, int b) {\n        auto center_a = get_center(a);\n        auto center_b = get_center(b);\n        if (center_a.first != center_b.first) \n            return center_a.first < center_b.first;\n        return center_a.second < center_b.second;\n    });\n    \n    vector<vector<int>> groups;\n    int idx = 0;\n    for (int group_size : G) {\n        vector<int> group;\n        for (int i = 0; i < group_size; i++) {\n            group.push_back(cities[idx++]);\n        }\n        groups.push_back(group);\n    }\n    \n    return groups;\n}\n\n// Use sliding window queries for larger groups\nvector<pair<int, int>> get_enhanced_mst(const vector<int>& group, int& queries_remaining) {\n    if (group.size() <= 1) return {};\n    if (queries_remaining <= 0) return {};\n    \n    set<pair<int, int>> all_edges;\n    \n    // If group is small enough, query it directly\n    if ((int)group.size() <= L) {\n        queries_remaining--;\n        return query(group);\n    }\n    \n    // Use sliding window for larger groups\n    int window_size = min(L, (int)group.size());\n    int queries_used = 0;\n    int step = max(1, window_size / 2);\n    \n    for (int start = 0; start < (int)group.size() - 1 && queries_remaining > 0 && queries_used < 10; start += step) {\n        int end = min(start + window_size, (int)group.size());\n        if (end - start >= 2) {\n            vector<int> window(group.begin() + start, group.begin() + end);\n            auto edges = query(window);\n            for (auto& edge : edges) {\n                all_edges.insert(edge);\n            }\n            queries_remaining--;\n            queries_used++;\n        }\n        if (end == (int)group.size()) break;\n    }\n    \n    return vector<pair<int, int>>(all_edges.begin(), all_edges.end());\n}\n\n// Build MST using Kruskal's algorithm with available edges\nvector<pair<int, int>> build_mst_with_edges(const vector<int>& group, const vector<pair<int, int>>& available_edges) {\n    if (group.size() <= 1) return {};\n    \n    // Map city IDs to local indices\n    unordered_map<int, int> city_to_index;\n    for (int i = 0; i < (int)group.size(); i++) {\n        city_to_index[group[i]] = i;\n    }\n    \n    // Union-Find for MST\n    vector<int> parent(group.size());\n    iota(parent.begin(), parent.end(), 0);\n    \n    function<int(int)> find = [&](int x) {\n        return parent[x] == x ? x : parent[x] = find(parent[x]);\n    };\n    \n    auto unite = [&](int x, int y) {\n        int root_x = find(x);\n        int root_y = find(y);\n        if (root_x == root_y) return false;\n        parent[root_y] = root_x;\n        return true;\n    };\n    \n    // Collect valid edges (connecting cities in this group)\n    vector<tuple<double, int, int>> edges;\n    set<int> group_set(group.begin(), group.end());\n    \n    for (const auto& edge : available_edges) {\n        if (group_set.count(edge.first) && group_set.count(edge.second)) {\n            int local_u = city_to_index[edge.first];\n            int local_v = city_to_index[edge.second];\n            double dist = sqrt(pow(get_center(edge.first).first - get_center(edge.second).first, 2) + \n                              pow(get_center(edge.first).second - get_center(edge.second).second, 2));\n            edges.push_back({dist, local_u, local_v});\n        }\n    }\n    \n    // Add fallback edges (consecutive in group)\n    for (int i = 0; i < (int)group.size() - 1; i++) {\n        int local_u = i;\n        int local_v = i + 1;\n        int global_u = group[i];\n        int global_v = group[i + 1];\n        double dist = sqrt(pow(get_center(global_u).first - get_center(global_v).first, 2) + \n                          pow(get_center(global_u).second - get_center(global_v).second, 2)) + 100000; // High cost fallback\n        edges.push_back({dist, local_u, local_v});\n    }\n    \n    // Sort edges by distance\n    sort(edges.begin(), edges.end());\n    \n    // Kruskal's algorithm\n    vector<pair<int, int>> mst;\n    for (const auto& edge : edges) {\n        double dist = get<0>(edge);\n        int u = get<1>(edge);\n        int v = get<2>(edge);\n        \n        if (unite(u, v)) {\n            int global_u = group[u];\n            int global_v = group[v];\n            if (global_u > global_v) swap(global_u, global_v);\n            mst.push_back({global_u, global_v});\n            if ((int)mst.size() == (int)group.size() - 1) break;\n        }\n    }\n    \n    return mst;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    // Read input\n    cin >> n >> m >> q >> L >> W;\n    G.resize(m);\n    for (int i = 0; i < m; i++) cin >> G[i];\n    \n    lx.resize(n); rx.resize(n); ly.resize(n); ry.resize(n);\n    for (int i = 0; i < n; i++) {\n        cin >> lx[i] >> rx[i] >> ly[i] >> ry[i];\n    }\n    \n    // Create groups\n    vector<vector<int>> groups = create_groups();\n    \n    // Process each group with enhanced MST building\n    vector<vector<pair<int, int>>> final_edges(m);\n    int queries_remaining = q;\n    \n    for (int g = 0; g < m; g++) {\n        // Get enhanced MST information\n        vector<pair<int, int>> available_edges = get_enhanced_mst(groups[g], queries_remaining);\n        \n        // Build final MST\n        final_edges[g] = build_mst_with_edges(groups[g], available_edges);\n    }\n    \n    // Output answer\n    cout << \"!\" << endl;\n    for (int g = 0; g < m; g++) {\n        // Output group cities\n        for (int i = 0; i < (int)groups[g].size(); i++) {\n            if (i > 0) cout << \" \";\n            cout << groups[g][i];\n        }\n        cout << endl;\n        \n        // Output edges\n        for (const auto& edge : final_edges[g]) {\n            cout << edge.first << \" \" << edge.second << endl;\n        }\n    }\n    \n    return 0;\n}","ahc046":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 20;\nconst int M = 40;\n\nstruct Point {\n    int r, c;\n    Point() : r(0), c(0) {}\n    Point(int _r, int _c) : r(_r), c(_c) {}\n    bool operator==(const Point& other) const {\n        return r == other.r && c == other.c;\n    }\n};\n\n// Directions\nconst int DR[] = {-1, 1, 0, 0};  // Up, Down, Left, Right\nconst int DC[] = {0, 0, -1, 1};\nconst char DIR_CHARS[] = {'U', 'D', 'L', 'R'};\n\nPoint targets[M];\nbool blocked[N][N];\n\n// Check if position is valid\nbool isValid(int r, int c) {\n    return r >= 0 && r < N && c >= 0 && c < N;\n}\n\n// Simple Manhattan pathfinding\nvector<pair<char, char>> findPath(Point from, Point to) {\n    vector<pair<char, char>> path;\n    int r = from.r, c = from.c;\n    \n    // Move vertically first\n    while (r < to.r) {\n        if (isValid(r + 1, c) && !blocked[r + 1][c]) {\n            path.push_back({'M', 'D'});\n            r++;\n        } else {\n            break;\n        }\n    }\n    while (r > to.r) {\n        if (isValid(r - 1, c) && !blocked[r - 1][c]) {\n            path.push_back({'M', 'U'});\n            r--;\n        } else {\n            break;\n        }\n    }\n    \n    // Move horizontally\n    while (c < to.c) {\n        if (isValid(r, c + 1) && !blocked[r][c + 1]) {\n            path.push_back({'M', 'R'});\n            c++;\n        } else {\n            break;\n        }\n    }\n    while (c > to.c) {\n        if (isValid(r, c - 1) && !blocked[r][c - 1]) {\n            path.push_back({'M', 'L'});\n            c--;\n        } else {\n            break;\n        }\n    }\n    \n    return path;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int n, m;\n    cin >> n >> m;\n    \n    Point start;\n    cin >> start.r >> start.c;\n    \n    for (int i = 0; i < m; i++) {\n        cin >> targets[i].r >> targets[i].c;\n    }\n    \n    vector<pair<char, char>> actions;\n    Point current = start;\n    \n    // Visit each target in order\n    for (int i = 1; i < m; i++) {\n        Point target = targets[i];\n        auto path = findPath(current, target);\n        actions.insert(actions.end(), path.begin(), path.end());\n        current = target;\n    }\n    \n    // Output results\n    for (auto& action : actions) {\n        cout << action.first << \" \" << action.second << \"\\n\";\n    }\n    \n    return 0;\n}"},"16":{"ahc001":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Rectangle {\n    int a, b, c, d; // (a,b) to (c,d)\n    Rectangle() : a(0), b(0), c(0), d(0) {}\n    Rectangle(int _a, int _b, int _c, int _d) : a(_a), b(_b), c(_c), d(_d) {}\n    bool isValid() const { \n        return a >= 0 && b >= 0 && c <= 10000 && d <= 10000 && a < c && b < d; \n    }\n    long long area() const { \n        return 1LL * (c - a) * (d - b); \n    }\n};\n\nint n;\nvector<tuple<int, int, int>> companies; // {x, y, r}\nvector<Rectangle> rectangles;\n\n// Check if two rectangles overlap\nbool overlap(const Rectangle& r1, const Rectangle& r2) {\n    return r1.a < r2.c && r2.a < r1.c && r1.b < r2.d && r2.b < r1.d;\n}\n\n// Check if rectangle overlaps with any existing rectangles\nbool hasOverlap(int current_idx, const Rectangle& rect) {\n    if (!rect.isValid()) return true;\n    for (int i = 0; i < current_idx; i++) {\n        if (rectangles[i].isValid() && overlap(rect, rectangles[i])) {\n            return true;\n        }\n    }\n    return false;\n}\n\n// Try to place a rectangle for company\nbool placeRectangle(int idx) {\n    int x = get<0>(companies[idx]);\n    int y = get<1>(companies[idx]);\n    int r = get<2>(companies[idx]);\n    \n    // Try to place rectangle with exact area first\n    for (int w = 1; w <= min(1000, r); w++) {\n        if (r % w == 0) {\n            int h = r / w;\n            if (h <= 1000) {\n                // Try to center rectangle around the point\n                for (int offset_x = 0; offset_x < min(w, 50); offset_x++) {\n                    for (int offset_y = 0; offset_y < min(h, 50); offset_y++) {\n                        int a = max(0, min(x - offset_x, 10000 - w));\n                        int b = max(0, min(y - offset_y, 10000 - h));\n                        int c = a + w;\n                        int d = b + h;\n                        \n                        if (a >= 0 && b >= 0 && c <= 10000 && d <= 10000) {\n                            Rectangle rect(a, b, c, d);\n                            // Check if point is properly inside\n                            if (a <= x && x < c && b <= y && y < d) {\n                                if (!hasOverlap(idx, rect)) {\n                                    rectangles[idx] = rect;\n                                    return true;\n                                }\n                            }\n                        }\n                    }\n                }\n            }\n        }\n    }\n    \n    // Try smaller rectangles\n    for (int target_area = max(1, r/10); target_area <= min(r, 10000); target_area += max(1, r/100)) {\n        for (int w = 1; w <= min(100, target_area); w++) {\n            if (target_area % w == 0) {\n                int h = target_area / w;\n                if (h <= 100) {\n                    // Try different positions\n                    for (int dx = 0; dx <= min(w-1, 30); dx++) {\n                        for (int dy = 0; dy <= min(h-1, 30); dy++) {\n                            int a = max(0, min(x - dx, 10000 - w));\n                            int b = max(0, min(y - dy, 10000 - h));\n                            int c = a + w;\n                            int d = b + h;\n                            \n                            if (a >= 0 && b >= 0 && c <= 10000 && d <= 10000) {\n                                Rectangle rect(a, b, c, d);\n                                if (a <= x && x < c && b <= y && y < d) {\n                                    if (!hasOverlap(idx, rect)) {\n                                        rectangles[idx] = rect;\n                                        return true;\n                                    }\n                                }\n                            }\n                        }\n                    }\n                }\n            }\n        }\n    }\n    \n    // Last resort: place 1x1 rectangle at or near the point\n    for (int dx = -5; dx <= 5; dx++) {\n        for (int dy = -5; dy <= 5; dy++) {\n            int a = max(0, min(x + dx, 9999));\n            int b = max(0, min(y + dy, 9999));\n            Rectangle rect(a, b, min(10000, a + 1), min(10000, b + 1));\n            if (rect.isValid()) {\n                if (!hasOverlap(idx, rect)) {\n                    rectangles[idx] = rect;\n                    return true;\n                }\n            }\n        }\n    }\n    \n    // Absolute fallback\n    rectangles[idx] = Rectangle(0, 0, 1, 1);\n    return true;\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    cin >> n;\n    companies.resize(n);\n    rectangles.resize(n);\n    \n    for (int i = 0; i < n; i++) {\n        int x, y, r;\n        cin >> x >> y >> r;\n        companies[i] = make_tuple(x, y, r);\n    }\n    \n    // Place rectangles in input order\n    for (int i = 0; i < n; i++) {\n        placeRectangle(i);\n    }\n    \n    // Output results\n    for (int i = 0; i < n; i++) {\n        Rectangle& rect = rectangles[i];\n        \n        // Ensure rectangle validity\n        rect.a = max(0, min(rect.a, 9999));\n        rect.b = max(0, min(rect.b, 9999));\n        rect.c = max(rect.a + 1, min(rect.c, 10000));\n        rect.d = max(rect.b + 1, min(rect.d, 10000));\n        \n        // Final safety check\n        if (rect.a >= rect.c) rect.c = rect.a + 1;\n        if (rect.b >= rect.d) rect.d = rect.b + 1;\n        \n        cout << rect.a << \" \" << rect.b << \" \" << rect.c << \" \" << rect.d << \"\\n\";\n    }\n    \n    return 0;\n}","ahc002":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int SIZE = 50;\nconst int MAX_TILES = 2500;\nint si, sj;\nint tile[SIZE][SIZE];\nint value[SIZE][SIZE];\nvector<pair<int, int>> tile_positions[MAX_TILES];\nint tile_values[MAX_TILES];\n\n// Directions: U, D, L, R\nconst int dx[4] = {-1, 1, 0, 0};\nconst int dy[4] = {0, 0, -1, 1};\nconst char dir_chars[4] = {'U', 'D', 'L', 'R'};\n\n// Check if position is valid\nbool is_valid(int x, int y) {\n    return x >= 0 && x < SIZE && y >= 0 && y < SIZE;\n}\n\n// Initialize tile information\nvoid init_tiles() {\n    for (int i = 0; i < MAX_TILES; i++) {\n        tile_positions[i].clear();\n    }\n    \n    for (int i = 0; i < SIZE; i++) {\n        for (int j = 0; j < SIZE; j++) {\n            tile_positions[tile[i][j]].push_back({i, j});\n        }\n    }\n    \n    for (int i = 0; i < MAX_TILES; i++) {\n        if (!tile_positions[i].empty()) {\n            int total = 0;\n            for (auto& pos : tile_positions[i]) {\n                total += value[pos.first][pos.second];\n            }\n            tile_values[i] = total;\n        }\n    }\n}\n\n// Simulate path to get actual score\nint get_actual_score(const string& path) {\n    vector<bool> visited(MAX_TILES, false);\n    int x = si, y = sj;\n    int score = tile_values[tile[x][y]];\n    visited[tile[x][y]] = true;\n    \n    for (char c : path) {\n        int d = -1;\n        for (int i = 0; i < 4; i++) {\n            if (dir_chars[i] == c) {\n                d = i;\n                break;\n            }\n        }\n        \n        if (d == -1) break;\n        \n        int nx = x + dx[d];\n        int ny = y + dy[d];\n        \n        if (!is_valid(nx, ny)) break;\n        \n        int tile_id = tile[nx][ny];\n        if (visited[tile_id]) break;\n        \n        x = nx;\n        y = ny;\n        score += tile_values[tile_id];\n        visited[tile_id] = true;\n    }\n    \n    return score;\n}\n\n// Optimized beam search with maximum efficiency\nstring optimized_beam_search(int beam_width, int max_steps, double time_multiplier) {\n    struct State {\n        int score;\n        int x, y;\n        string path;\n        vector<bool> visited;\n        int steps;\n        \n        bool operator<(const State& other) const {\n            if (score != other.score) return score < other.score;\n            return steps > other.steps;\n        }\n    };\n    \n    priority_queue<State> beam;\n    \n    // Initialize\n    vector<bool> visited(MAX_TILES, false);\n    int start_tile_id = tile[si][sj];\n    visited[start_tile_id] = true;\n    int start_score = tile_values[start_tile_id];\n    \n    beam.push({start_score, si, sj, \"\", visited, 0});\n    \n    string best_path = \"\";\n    int best_score = start_score;\n    \n    clock_t start_time = clock();\n    const double TIME_LIMIT = 1.999 * time_multiplier; // Use maximum time\n    \n    for (int step = 0; step < max_steps && !beam.empty(); step++) {\n        priority_queue<State> next_beam;\n        \n        // Adaptive processing based on performance\n        int max_process = beam_width;\n        if (step < 3) max_process = beam_width * 5; // Maximum expansion start\n        else if (step < 10) max_process = beam_width * 3; // High expansion\n        else if (step < 30) max_process = beam_width * 2; // Moderate expansion\n        else if (step > 400 && best_score > 150000) max_process = beam_width / 4; // Ultra-focus\n        else if (step > 500) max_process = beam_width / 2; // Late game focus\n        \n        int processed = 0;\n        while (!beam.empty() && processed < max_process) {\n            State current = beam.top();\n            beam.pop();\n            processed++;\n            \n            // Track best improvement\n            if (current.score > best_score) {\n                best_score = current.score;\n                best_path = current.path;\n            }\n            \n            // Time management\n            double elapsed = (double)(clock() - start_time) / CLOCKS_PER_SEC;\n            if (elapsed > TIME_LIMIT) {\n                return best_path;\n            }\n            \n            // Generate moves with advanced evaluation\n            vector<tuple<long long, int, long long, int>> moves;\n            \n            for (int d = 0; d < 4; d++) {\n                int nx = current.x + dx[d];\n                int ny = current.y + dy[d];\n                \n                if (is_valid(nx, ny)) {\n                    int next_tile_id = tile[nx][ny];\n                    if (!current.visited[next_tile_id]) {\n                        int immediate_value = tile_values[next_tile_id];\n                        \n                        // Advanced potential calculation\n                        long long future_potential = 0;\n                        vector<bool> temp_visited = current.visited;\n                        temp_visited[next_tile_id] = true;\n                        \n                        // Adaptive depth calculation\n                        int adaptive_depth = 15;\n                        if (current.steps < 20) adaptive_depth = 18; // Deeper exploration\n                        if (current.steps > 300) adaptive_depth = 10; // Shallow focus\n                        if (best_score > 300000) adaptive_depth = 8; // Focused search\n                        \n                        int current_x = nx, current_y = ny;\n                        vector<bool> potential_visited = temp_visited;\n                        \n                        for (int level = 0; level < adaptive_depth; level++) {\n                            vector<tuple<int, int, int>> next_moves;\n                            \n                            for (int d2 = 0; d2 < 4; d2++) {\n                                int next_x = current_x + dx[d2];\n                                int next_y = current_y + dy[d2];\n                                \n                                if (is_valid(next_x, next_y)) {\n                                    int next_tile_id2 = tile[next_x][next_y];\n                                    if (!potential_visited[next_tile_id2]) {\n                                        int base_value = tile_values[next_tile_id2];\n                                        \n                                        // Local clustering bonus\n                                        int cluster_bonus = 0;\n                                        for (int cd = 0; cd < 4; cd++) {\n                                            int cx = next_x + dx[cd];\n                                            int cy = next_y + dy[cd];\n                                            if (is_valid(cx, cy) && !potential_visited[tile[cx][cy]]) {\n                                                cluster_bonus += tile_values[tile[cx][cy]] / 40;\n                                            }\n                                        }\n                                        \n                                        next_moves.push_back({base_value, cluster_bonus, d2});\n                                    }\n                                }\n                            }\n                            \n                            if (next_moves.empty()) break;\n                            \n                            // Sort by combined score\n                            sort(next_moves.begin(), next_moves.end(), [](const auto& a, const auto& b) {\n                                int score_a = get<0>(a) + get<1>(a);\n                                int score_b = get<0>(b) + get<1>(b);\n                                if (score_a != score_b) return score_a > score_b;\n                                return get<0>(a) > get<0>(b);\n                            });\n                            \n                            int best_value = get<0>(next_moves[0]);\n                            int best_cluster = get<1>(next_moves[0]);\n                            \n                            // Weighted potential calculation\n                            double weight = 1.0 / pow(2.3, level * 0.9);\n                            future_potential += (long long)((best_value + best_cluster * 0.6) * weight);\n                            \n                            // Move to best next position\n                            int best_dir = get<2>(next_moves[0]);\n                            current_x += dx[best_dir];\n                            current_y += dy[best_dir];\n                            if (is_valid(current_x, current_y)) {\n                                potential_visited[tile[current_x][current_y]] = true;\n                            }\n                        }\n                        \n                        // Final scoring with optimal weights\n                        long long path_bonus = current.path.length() * 8LL;\n                        long long clustering_bonus = 0;\n                        \n                        // Immediate clustering analysis\n                        for (int cd = 0; cd < 4; cd++) {\n                            int cx = nx + dx[cd];\n                            int cy = ny + dy[cd];\n                            if (is_valid(cx, cy) && !temp_visited[tile[cx][cy]]) {\n                                clustering_bonus += tile_values[tile[cx][cy]] / 60;\n                            }\n                        }\n                        \n                        long long final_score = immediate_value + \n                                               (long long)(future_potential * 0.45) + \n                                               path_bonus + \n                                               clustering_bonus * 3LL;\n                        \n                        moves.push_back({final_score, immediate_value, future_potential, d});\n                    }\n                }\n            }\n            \n            // Sort by final score\n            sort(moves.begin(), moves.end(), [](const auto& a, const auto& b) {\n                if (get<0>(a) != get<0>(b)) return get<0>(a) > get<0>(b);\n                if (get<1>(a) != get<1>(b)) return get<1>(a) > get<1>(b);\n                return get<2>(a) > get<2>(b);\n            });\n            \n            // Adaptive branching\n            int branches = 6;\n            if (current.steps < 2) branches = 12; // Maximum start\n            else if (current.steps < 5) branches = 10; // High expansion\n            else if (current.steps < 20) branches = 8; // Good expansion\n            else if (current.steps < 80) branches = 7; // Moderate expansion\n            else if (current.steps > 500) branches = 2; // Minimal focus\n            else if (best_score > 400000) branches = 3; // Focused search\n            branches = min(branches, (int)moves.size());\n            \n            for (int i = 0; i < branches; i++) {\n                int d = get<3>(moves[i]);\n                int nx = current.x + dx[d];\n                int ny = current.y + dy[d];\n                \n                int next_tile_id = tile[nx][ny];\n                vector<bool> new_visited = current.visited;\n                new_visited[next_tile_id] = true;\n                \n                int new_score = current.score + tile_values[next_tile_id];\n                string new_path = current.path + dir_chars[d];\n                \n                next_beam.push({new_score, nx, ny, new_path, new_visited, current.steps + 1});\n            }\n        }\n        \n        // Enhanced candidate selection\n        vector<State> candidates;\n        int candidate_limit = beam_width * 6;\n        \n        while (!next_beam.empty() && candidates.size() < candidate_limit) {\n            candidates.push_back(next_beam.top());\n            next_beam.pop();\n        }\n        \n        // Sort candidates\n        sort(candidates.begin(), candidates.end());\n        reverse(candidates.begin(), candidates.end());\n        \n        // Diversity filtering\n        vector<State> diverse_candidates;\n        set<pair<int, int>> position_signatures;\n        set<string> path_signatures;\n        \n        for (const auto& candidate : candidates) {\n            if (diverse_candidates.size() >= beam_width) break;\n            \n            pair<int, int> pos_sig = {candidate.x / 2, candidate.y / 2};\n            string path_sig = candidate.path.substr(0, min(25, (int)candidate.path.length()));\n            \n            bool accept = false;\n            if (diverse_candidates.size() < beam_width / 5) {\n                accept = true;\n            } else if (position_signatures.find(pos_sig) == position_signatures.end()) {\n                accept = true;\n            } else if (path_signatures.find(path_sig) == path_signatures.end()) {\n                accept = true;\n            } else if (diverse_candidates.size() < beam_width * 0.95 && rand() % 1000 < 2) {\n                accept = true;\n            }\n            \n            if (accept) {\n                position_signatures.insert(pos_sig);\n                path_signatures.insert(path_sig);\n                diverse_candidates.push_back(candidate);\n            }\n        }\n        \n        // Ensure minimum candidates\n        if (diverse_candidates.size() < beam_width / 4) {\n            for (const auto& candidate : candidates) {\n                if (diverse_candidates.size() >= beam_width) break;\n                bool found = false;\n                for (const auto& existing : diverse_candidates) {\n                    if (existing.path == candidate.path) {\n                        found = true;\n                        break;\n                    }\n                }\n                if (!found) {\n                    diverse_candidates.push_back(candidate);\n                }\n            }\n        }\n        \n        // Fill beam\n        for (const auto& candidate : diverse_candidates) {\n            beam.push(candidate);\n        }\n    }\n    \n    return best_path;\n}\n\n// Enhanced greedy with maximum optimization\nstring enhanced_greedy() {\n    vector<bool> visited(MAX_TILES, false);\n    int x = si, y = sj;\n    visited[tile[x][y]] = true;\n    string path = \"\";\n    \n    for (int step = 0; step < 800; step++) {\n        vector<tuple<long long, int, long long, int>> candidates;\n        \n        for (int d = 0; d < 4; d++) {\n            int nx = x + dx[d];\n            int ny = y + dy[d];\n            \n            if (is_valid(nx, ny)) {\n                int tile_id = tile[nx][ny];\n                if (!visited[tile_id]) {\n                    int immediate_score = tile_values[tile_id];\n                    \n                    // Enhanced potential calculation\n                    long long potential = 0;\n                    vector<bool> temp_visited = visited;\n                    temp_visited[tile_id] = true;\n                    \n                    // Clustering analysis\n                    long long cluster_potential = 0;\n                    long long density_potential = 0;\n                    long long flow_potential = 0;\n                    \n                    // Immediate clustering\n                    for (int cd = 0; cd < 4; cd++) {\n                        int cx = nx + dx[cd];\n                        int cy = ny + dy[cd];\n                        if (is_valid(cx, cy) && !temp_visited[tile[cx][cy]]) {\n                            cluster_potential += tile_values[tile[cx][cy]];\n                        }\n                    }\n                    cluster_potential /= 10;\n                    \n                    // Multi-level lookahead\n                    int level = 1;\n                    int current_x = nx, current_y = ny;\n                    vector<bool> lookahead_visited = temp_visited;\n                    \n                    while (level <= 18) {\n                        vector<tuple<int, int, int>> next_moves;\n                        \n                        for (int d2 = 0; d2 < 4; d2++) {\n                            int next_x = current_x + dx[d2];\n                            int next_y = current_y + dy[d2];\n                            \n                            if (is_valid(next_x, next_y)) {\n                                int next_tile_id = tile[next_x][next_y];\n                                if (!lookahead_visited[next_tile_id]) {\n                                    int base_value = tile_values[next_tile_id];\n                                    \n                                    // Local bonus\n                                    int local_bonus = 0;\n                                    for (int cd = 0; cd < 4; cd++) {\n                                        int cx = next_x + dx[cd];\n                                        int cy = next_y + dy[cd];\n                                        if (is_valid(cx, cy) && !lookahead_visited[tile[cx][cy]]) {\n                                            local_bonus += tile_values[tile[cx][cy]] / 50;\n                                        }\n                                    }\n                                    \n                                    next_moves.push_back({base_value, local_bonus, d2});\n                                }\n                            }\n                        }\n                        \n                        if (next_moves.empty()) break;\n                        \n                        // Sort moves\n                        sort(next_moves.begin(), next_moves.end(), [](const auto& a, const auto& b) {\n                            int score_a = get<0>(a) + get<1>(a);\n                            int score_b = get<0>(b) + get<1>(b);\n                            if (score_a != score_b) return score_a > score_b;\n                            return get<0>(a) > get<0>(b);\n                        });\n                        \n                        int best_value = get<0>(next_moves[0]);\n                        int best_bonus = get<1>(next_moves[0]);\n                        \n                        // Weighted potential\n                        double weight = 1.0 / pow(2.7, (level - 1) * 0.85);\n                        long long level_contribution = (long long)((best_value + best_bonus * 0.8) * weight);\n                        potential += level_contribution;\n                        flow_potential += level_contribution / (level + 2);\n                        density_potential += best_value / (level * 3);\n                        \n                        // Move to best position\n                        int best_dir = get<2>(next_moves[0]);\n                        current_x += dx[best_dir];\n                        current_y += dy[best_dir];\n                        if (is_valid(current_x, current_y)) {\n                            lookahead_visited[tile[current_x][current_y]] = true;\n                        }\n                        level++;\n                    }\n                    \n                    // Final scoring\n                    long long path_bonus = path.length() * 10LL;\n                    long long cluster_bonus = cluster_potential * 4LL;\n                    long long temporal_bonus = flow_potential / 2;\n                    long long density_bonus = density_potential / 4;\n                    \n                    long long final_score = immediate_score + \n                                           (potential / 2) + \n                                           path_bonus + \n                                           cluster_bonus + \n                                           temporal_bonus + \n                                           density_bonus;\n                    \n                    candidates.push_back({final_score, immediate_score, potential, d});\n                }\n            }\n        }\n        \n        if (candidates.empty()) break;\n        \n        // Sort candidates\n        sort(candidates.begin(), candidates.end(), [](const auto& a, const auto& b) {\n            if (get<0>(a) != get<0>(b)) return get<0>(a) > get<0>(b);\n            if (get<1>(a) != get<1>(b)) return get<1>(a) > get<1>(b);\n            return get<2>(a) > get<2>(b);\n        });\n        \n        // Adaptive selection\n        int chosen_idx = 0;\n        if (candidates.size() > 1) {\n            int exploration_rate = 60; // High exploration\n            if (step > 5) exploration_rate = 40;\n            if (step > 20) exploration_rate = 25;\n            if (step > 50) exploration_rate = 15;\n            if (step > 100) exploration_rate = 8;\n            if (step > 200) exploration_rate = 3;\n            if (step > 400) exploration_rate = 1;\n            if (path.length() > 600) exploration_rate = 0;\n            \n            if (rand() % 100 < exploration_rate && candidates.size() > 1) {\n                int selection_pool = min(5, (int)candidates.size());\n                chosen_idx = rand() % selection_pool;\n            }\n        }\n        \n        auto chosen = candidates[chosen_idx];\n        int chosen_dir = get<3>(chosen);\n        \n        path += dir_chars[chosen_dir];\n        x = x + dx[chosen_dir];\n        y = y + dy[chosen_dir];\n        visited[tile[x][y]] = true;\n    }\n    \n    return path;\n}\n\n// Specialized rescue for edge cases\nstring specialized_rescue() {\n    vector<bool> visited(MAX_TILES, false);\n    int x = si, y = sj;\n    visited[tile[x][y]] = true;\n    string path = \"\";\n    \n    // Pattern based on start position\n    int pattern = (si * 41 + sj * 43 + si * sj * 13) % 16;\n    \n    for (int step = 0; step < 1000; step++) {\n        vector<tuple<int, int, int, int>> valid_moves;\n        \n        // Pattern-based directions\n        vector<int> pattern_directions;\n        switch (pattern % 8) {\n            case 0: pattern_directions = {3, 1, 2, 0}; break;\n            case 1: pattern_directions = {1, 3, 0, 2}; break;\n            case 2: pattern_directions = {0, 2, 1, 3}; break;\n            case 3: pattern_directions = {2, 0, 3, 1}; break;\n            case 4: pattern_directions = {1, 0, 3, 2}; break;\n            case 5: pattern_directions = {3, 2, 1, 0}; break;\n            case 6: pattern_directions = {0, 1, 2, 3}; break;\n            case 7: pattern_directions = {2, 3, 0, 1}; break;\n            default: pattern_directions = {0, 1, 2, 3}; break;\n        }\n        \n        // Enhanced move evaluation\n        for (int dir_idx : pattern_directions) {\n            int nx = x + dx[dir_idx];\n            int ny = y + dy[dir_idx];\n            \n            if (is_valid(nx, ny)) {\n                int tile_id = tile[nx][ny];\n                if (!visited[tile_id]) {\n                    int tile_value = tile_values[tile_id];\n                    \n                    // Enhanced clustering analysis\n                    int cluster_value = 0;\n                    int entropy_value = 0;\n                    \n                    // Local analysis\n                    for (int cd = 0; cd < 4; cd++) {\n                        int cx = nx + dx[cd];\n                        int cy = ny + dy[cd];\n                        if (is_valid(cx, cy) && !visited[tile[cx][cy]]) {\n                            cluster_value += tile_values[tile[cx][cy]];\n                            entropy_value += 1;\n                        }\n                    }\n                    cluster_value /= 12;\n                    \n                    valid_moves.push_back({tile_value, cluster_value, entropy_value, dir_idx});\n                }\n            }\n        }\n        \n        if (valid_moves.empty()) break;\n        \n        // Enhanced sorting\n        sort(valid_moves.begin(), valid_moves.end(), [](const auto& a, const auto& b) {\n            if (get<0>(a) != get<0>(b)) return get<0>(a) > get<0>(b);\n            if (get<1>(a) != get<1>(b)) return get<1>(a) > get<1>(b);\n            return get<2>(a) > get<2>(b);\n        });\n        \n        int best_value = get<0>(valid_moves[0]);\n        vector<int> high_value_moves;\n        \n        // Inclusive threshold\n        for (int i = 0; i < valid_moves.size(); i++) {\n            int current_value = get<0>(valid_moves[i]);\n            if (current_value >= best_value * 0.85) {\n                high_value_moves.push_back(i);\n            }\n        }\n        \n        // Selection logic\n        int chosen_idx;\n        if (high_value_moves.size() == 1) {\n            chosen_idx = high_value_moves[0];\n        } else {\n            if (step < 20) {\n                // High exploration\n                chosen_idx = high_value_moves[rand() % high_value_moves.size()];\n            } else if (step < 100) {\n                // Moderate exploration\n                if (rand() % 100 < 80 && high_value_moves.size() > 1) {\n                    chosen_idx = high_value_moves[1 % high_value_moves.size()];\n                } else {\n                    chosen_idx = high_value_moves[0];\n                }\n            } else {\n                // Low exploration\n                int top_choices = max(1, (int)(high_value_moves.size() / 5));\n                chosen_idx = high_value_moves[rand() % top_choices];\n            }\n        }\n        \n        auto chosen = valid_moves[chosen_idx];\n        int chosen_dir = get<3>(chosen);\n        \n        path += dir_chars[chosen_dir];\n        x = x + dx[chosen_dir];\n        y = y + dy[chosen_dir];\n        visited[tile[x][y]] = true;\n    }\n    \n    return path;\n}\n\n// Main strategy selector\nstring find_best_path() {\n    vector<pair<string, int>> results;\n    \n    // Strategy 1: Wide beam search\n    string result1 = optimized_beam_search(300, 500, 1.0);\n    results.push_back({result1, get_actual_score(result1)});\n    \n    // Strategy 2: Deep focused beam search\n    string result2 = optimized_beam_search(70, 700, 0.95);\n    results.push_back({result2, get_actual_score(result2)});\n    \n    // Strategy 3: Balanced beam search\n    string result3 = optimized_beam_search(200, 600, 1.05);\n    results.push_back({result3, get_actual_score(result3)});\n    \n    // Strategy 4: Enhanced greedy\n    string result4 = enhanced_greedy();\n    results.push_back({result4, get_actual_score(result4)});\n    \n    // Strategy 5: Specialized rescue\n    string result5 = specialized_rescue();\n    results.push_back({result5, get_actual_score(result5)});\n    \n    // Sort by score\n    sort(results.begin(), results.end(), [](const auto& a, const auto& b) {\n        if (a.second != b.second) return a.second > b.second;\n        return a.first.length() > b.first.length();\n    });\n    \n    // Final verification\n    if (results.size() > 1) {\n        int best_score = results[0].second;\n        int second_score = results[1].second;\n        \n        if (second_score > best_score * 0.99) {\n            int best_length = results[0].first.length();\n            int second_length = results[1].first.length();\n            \n            if (second_length > best_length * 1.4 && second_score > best_score * 0.995) {\n                return results[1].first;\n            }\n        }\n    }\n    \n    return results[0].first;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    // Set random seed\n    srand(24680);\n    \n    // Read input\n    cin >> si >> sj;\n    \n    for (int i = 0; i < SIZE; i++) {\n        for (int j = 0; j < SIZE; j++) {\n            cin >> tile[i][j];\n        }\n    }\n    \n    for (int i = 0; i < SIZE; i++) {\n        for (int j = 0; j < SIZE; j++) {\n            cin >> value[i][j];\n        }\n    }\n    \n    // Initialize tile information\n    init_tiles();\n    \n    // Find and output path\n    string result = find_best_path();\n    cout << result << endl;\n    \n    return 0;\n}","ahc003":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int GRID_SIZE = 30;\nconst int MAX_QUERIES = 1000;\n\n// Direction vectors: U, D, L, R\nconst int di[4] = {-1, 1, 0, 0};\nconst int dj[4] = {0, 0, -1, 1};\nconst char dir_chars[4] = {'U', 'D', 'L', 'R'};\n\n// Estimated edge weights\ndouble h[GRID_SIZE][GRID_SIZE-1]; // horizontal edges (i, j) -> (i, j+1)\ndouble v[GRID_SIZE-1][GRID_SIZE]; // vertical edges (i, j) -> (i+1, j)\n\n// Count of observations for each edge\nint h_count[GRID_SIZE][GRID_SIZE-1];\nint v_count[GRID_SIZE-1][GRID_SIZE];\n\nvoid init_weights() {\n    // Slightly optimized initialization\n    for (int i = 0; i < GRID_SIZE; i++) {\n        for (int j = 0; j < GRID_SIZE-1; j++) {\n            h[i][j] = 3700.0;  // Compromise between exploration and exploitation\n            h_count[i][j] = 0;\n        }\n    }\n    for (int i = 0; i < GRID_SIZE-1; i++) {\n        for (int j = 0; j < GRID_SIZE; j++) {\n            v[i][j] = 3700.0;\n            v_count[i][j] = 0;\n        }\n    }\n}\n\nstruct State {\n    int i, j;\n    double cost;\n    bool operator>(const State& other) const {\n        return cost > other.cost;\n    }\n};\n\nstring find_path(int si, int sj, int ti, int tj) {\n    // Dijkstra's algorithm\n    vector<vector<double>> dist(GRID_SIZE, vector<double>(GRID_SIZE, 1e18));\n    vector<vector<pair<int, int>>> prev(GRID_SIZE, vector<pair<int, int>>(GRID_SIZE, make_pair(-1, -1)));\n    vector<vector<char>> prev_dir(GRID_SIZE, vector<char>(GRID_SIZE, ' '));\n    \n    priority_queue<State, vector<State>, greater<State>> pq;\n    dist[si][sj] = 0;\n    pq.push({si, sj, 0});\n    \n    while (!pq.empty()) {\n        auto [i, j, cost] = pq.top(); pq.pop();\n        if (cost > dist[i][j]) continue;\n        \n        if (i == ti && j == tj) break;\n        \n        // Try all 4 directions\n        for (int d = 0; d < 4; d++) {\n            int ni = i + di[d];\n            int nj = j + dj[d];\n            \n            if (ni >= 0 && ni < GRID_SIZE && nj >= 0 && nj < GRID_SIZE) {\n                double edge_cost = 0;\n                bool valid_move = true;\n                \n                if (d == 0) { // Up: (i,j) -> (i-1,j)\n                    if (ni < i) edge_cost = v[ni][nj];\n                    else valid_move = false;\n                } else if (d == 1) { // Down: (i,j) -> (i+1,j)\n                    if (i < ni) edge_cost = v[i][j];\n                    else valid_move = false;\n                } else if (d == 2) { // Left: (i,j) -> (i,j-1)\n                    if (nj < j) edge_cost = h[ni][nj];\n                    else valid_move = false;\n                } else if (d == 3) { // Right: (i,j) -> (i,j+1)\n                    if (j < nj) edge_cost = h[i][j];\n                    else valid_move = false;\n                }\n                \n                if (!valid_move) continue;\n                \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                    prev_dir[ni][nj] = dir_chars[d];\n                    pq.push({ni, nj, new_cost});\n                }\n            }\n        }\n    }\n    \n    // Reconstruct path\n    string path;\n    int ci = ti, cj = tj;\n    while (ci != si || cj != sj) {\n        path += prev_dir[ci][cj];\n        auto p = prev[ci][cj];\n        ci = p.first;\n        cj = p.second;\n    }\n    reverse(path.begin(), path.end());\n    \n    return path;\n}\n\n// Store the last path for updating weights\nvector<pair<int, int>> last_path_coords;\n\nvoid store_path_info(int si, int sj, const string& path) {\n    last_path_coords.clear();\n    last_path_coords.push_back({si, sj});\n    \n    int ci = si, cj = sj;\n    for (char c : path) {\n        if (c == 'U') ci--;\n        else if (c == 'D') ci++;\n        else if (c == 'L') cj--;\n        else if (c == 'R') cj++;\n        last_path_coords.push_back({ci, cj});\n    }\n}\n\nvoid update_weights(int actual_cost, int query_num) {\n    // Get edges used in the path\n    vector<tuple<int, int, bool, double>> edges_info;\n    double total_estimated = 0;\n    \n    for (int k = 0; k < (int)last_path_coords.size() - 1; k++) {\n        int i1 = last_path_coords[k].first;\n        int j1 = last_path_coords[k].second;\n        int i2 = last_path_coords[k+1].first;\n        int j2 = last_path_coords[k+1].second;\n        \n        double cost = 0;\n        int i, j;\n        bool is_vertical;\n        \n        if (i1 == i2) {\n            // Horizontal move\n            j = min(j1, j2);\n            i = i1;\n            is_vertical = false;\n            cost = h[i][j];\n        } else {\n            // Vertical move\n            i = min(i1, i2);\n            j = j1;\n            is_vertical = true;\n            cost = v[i][j];\n        }\n        \n        edges_info.push_back({i, j, is_vertical, cost});\n        total_estimated += cost;\n    }\n    \n    if (edges_info.empty() || total_estimated <= 0) return;\n    \n    // Calculate error\n    double error = (double)actual_cost - total_estimated;\n    \n    // Optimized update strategy\n    for (auto& [i, j, is_vertical, edge_cost] : edges_info) {\n        double proportion = edge_cost / total_estimated;\n        double adjustment = error * proportion;\n        \n        // Optimized learning rate with proven effectiveness\n        int count = is_vertical ? v_count[i][j] : h_count[i][j];\n        \n        double learning_rate;\n        if (count == 0) {\n            learning_rate = 0.26;  // Slightly higher initial learning\n        } else if (count < 3) {\n            learning_rate = 0.19;\n        } else if (count < 10) {\n            learning_rate = 0.13;\n        } else {\n            learning_rate = 0.09 / (1.0 + (double)count / 22.0);  // Slightly faster decay\n        }\n        \n        // Optimized time-based adjustments\n        if (query_num < 180) {\n            learning_rate *= 1.28;  // Slightly more aggressive early boost\n        } else if (query_num < 450) {\n            learning_rate *= 1.12;\n        } else if (query_num > 820) {\n            learning_rate *= 0.72;\n        }\n        \n        learning_rate = max(0.0025, learning_rate);  // Slightly lower floor\n        \n        double current_weight = is_vertical ? v[i][j] : h[i][j];\n        double new_weight = current_weight + adjustment * learning_rate;\n        \n        // Optimized bounds\n        new_weight = max(650.0, min(11500.0, new_weight));  // Slightly wider range\n        \n        // Improved exponential moving average\n        double alpha = 2.0 / (2.5 + count);  // Slightly different weighting\n        if (is_vertical) {\n            v[i][j] = v[i][j] * (1 - alpha) + new_weight * alpha;\n            v_count[i][j]++;\n        } else {\n            h[i][j] = h[i][j] * (1 - alpha) + new_weight * alpha;\n            h_count[i][j]++;\n        }\n    }\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    init_weights();\n    \n    for (int query = 0; query < MAX_QUERIES; query++) {\n        int si, sj, ti, tj;\n        cin >> si >> sj >> ti >> tj;\n        \n        string path = find_path(si, sj, ti, tj);\n        store_path_info(si, sj, path);\n        \n        cout << path << endl;\n        cout.flush();\n        \n        int result;\n        cin >> result;\n        \n        update_weights(result, query);\n    }\n    \n    return 0;\n}","ahc004":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 20;\nconst char VALID_CHARS[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'};\n\nvector<string> strings;\nvector<vector<char>> matrix(N, vector<char>(N, '.'));\n\n// Check if a string can be placed at a specific position and direction\nbool canPlace(const string& s, int row, int col, bool vertical) {\n    if (vertical) {\n        for (int i = 0; i < (int)s.length(); i++) {\n            int r = (row + i) % N;\n            if (matrix[r][col] != '.' && matrix[r][col] != s[i]) {\n                return false;\n            }\n        }\n    } else {\n        for (int j = 0; j < (int)s.length(); j++) {\n            int c = (col + j) % N;\n            if (matrix[row][c] != '.' && matrix[row][c] != s[j]) {\n                return false;\n            }\n        }\n    }\n    return true;\n}\n\n// Count how many existing characters would be matched if we place this string\nint countExistingMatches(const string& s, int row, int col, bool vertical) {\n    int matches = 0;\n    if (vertical) {\n        for (int i = 0; i < (int)s.length(); i++) {\n            int r = (row + i) % N;\n            if (matrix[r][col] != '.' && matrix[r][col] == s[i]) {\n                matches++;\n            }\n        }\n    } else {\n        for (int j = 0; j < (int)s.length(); j++) {\n            int c = (col + j) % N;\n            if (matrix[row][c] != '.' && matrix[row][c] == s[j]) {\n                matches++;\n            }\n        }\n    }\n    return matches;\n}\n\n// Place a string (modifies matrix)\nvoid placeString(const string& s, int row, int col, bool vertical) {\n    if (vertical) {\n        for (int i = 0; i < (int)s.length(); i++) {\n            int r = (row + i) % N;\n            matrix[r][col] = s[i];\n        }\n    } else {\n        for (int j = 0; j < (int)s.length(); j++) {\n            int c = (col + j) % N;\n            matrix[row][c] = s[j];\n        }\n    }\n}\n\n// Try to place strings with better strategy - prioritize placements that match more existing chars\nvoid placeStrings() {\n    // Create priority list: longer strings first\n    vector<pair<int, int>> stringPriority;\n    for (int i = 0; i < (int)strings.size(); i++) {\n        // Priority: longer strings first (negative for descending sort)\n        stringPriority.push_back({-strings[i].length(), i});\n    }\n    sort(stringPriority.begin(), stringPriority.end());\n    \n    // Try to place each string - prioritize placements that match more existing chars\n    for (const auto& p : stringPriority) {\n        int idx = p.second;\n        const string& s = strings[idx];\n        \n        // Find the best placement (most existing matches)\n        int best_matches = -1;\n        int best_i = -1, best_j = -1, best_dir = -1;\n        \n        for (int dir = 0; dir < 2; dir++) {\n            bool vertical = (dir == 1);\n            for (int i = 0; i < N; i++) {\n                for (int j = 0; j < N; j++) {\n                    if (canPlace(s, i, j, vertical)) {\n                        int matches = countExistingMatches(s, i, j, vertical);\n                        if (matches > best_matches) {\n                            best_matches = matches;\n                            best_i = i;\n                            best_j = j;\n                            best_dir = dir;\n                        }\n                    }\n                }\n            }\n        }\n        \n        // Place at the best position if found\n        if (best_matches >= 0) {\n            placeString(s, best_i, best_j, best_dir == 1);\n        }\n    }\n}\n\n// Fill remaining empty cells with valid characters only\nvoid fillEmptyCells() {\n    random_device rd;\n    mt19937 gen(rd());\n    uniform_int_distribution<> char_dis(0, 7);\n    \n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (matrix[i][j] == '.') {\n                int idx = char_dis(gen);\n                // Safe bounds check\n                idx = idx % 8;\n                if (idx < 0) idx = -idx;\n                if (idx >= 8) idx = idx % 8;\n                matrix[i][j] = VALID_CHARS[idx];\n            }\n        }\n    }\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    int M;\n    cin >> M;\n    \n    strings.resize(M);\n    for (int i = 0; i < M; i++) {\n        cin >> strings[i];\n    }\n    \n    // Initialize matrix\n    matrix.assign(N, vector<char>(N, '.'));\n    \n    // Place strings strategically\n    placeStrings();\n    \n    // Fill any remaining empty cells\n    fillEmptyCells();\n    \n    // Output result - ensure only valid characters\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            char c = matrix[i][j];\n            // Validate character before output\n            bool valid = false;\n            for (int k = 0; k < 8; k++) {\n                if (c == VALID_CHARS[k]) {\n                    valid = true;\n                    break;\n                }\n            }\n            if (valid) {\n                cout << c;\n            } else {\n                // Fallback to a valid character\n                cout << 'A';\n            }\n        }\n        cout << \"\\n\";\n    }\n    \n    return 0;\n}","ahc005":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MAX_N = 70;\nint N, si, sj;\nchar grid[MAX_N][MAX_N];\nbool is_road[MAX_N][MAX_N];\nvector<pair<int,int>> roads;\n\n// Directions: U, D, L, R\nconst int dx[4] = {-1, 1, 0, 0};\nconst int dy[4] = {0, 0, -1, 1};\n\n// Calculate which roads are visible from position (x, y)\nset<pair<int,int>> get_visible_roads(int x, int y) {\n    set<pair<int,int>> visible;\n    \n    if (!is_road[x][y]) return visible;\n    \n    // Horizontal visibility (same row)\n    // Look left\n    for (int j = y; j >= 0; j--) {\n        if (!is_road[x][j]) break;\n        visible.insert({x, j});\n    }\n    // Look right\n    for (int j = y; j < N; j++) {\n        if (!is_road[x][j]) break;\n        visible.insert({x, j});\n    }\n    \n    // Vertical visibility (same column)\n    // Look up\n    for (int i = x; i >= 0; i--) {\n        if (!is_road[i][y]) break;\n        visible.insert({i, y});\n    }\n    // Look down\n    for (int i = x; i < N; i++) {\n        if (!is_road[i][y]) break;\n        visible.insert({i, y});\n    }\n    \n    return visible;\n}\n\nbool is_valid(int x, int y) {\n    return x >= 0 && x < N && y >= 0 && y < N && is_road[x][y];\n}\n\n// BFS to find shortest path between two points\nvector<pair<int,int>> bfs_path(int sx, int sy, int tx, int ty) {\n    if (sx == tx && sy == ty) return {{sx, sy}};\n    if (!is_valid(sx, sy) || !is_valid(tx, ty)) return {{sx, sy}};\n    \n    queue<pair<int,int>> q;\n    bool visited[MAX_N][MAX_N] = {false};\n    pair<int,int> parent[MAX_N][MAX_N];\n    int dist[MAX_N][MAX_N];\n    \n    for (int i = 0; i < MAX_N; i++) {\n        for (int j = 0; j < MAX_N; j++) {\n            dist[i][j] = 1e9;\n        }\n    }\n    \n    q.push({sx, sy});\n    visited[sx][sy] = true;\n    dist[sx][sy] = 0;\n    \n    while (!q.empty()) {\n        auto [x, y] = q.front(); q.pop();\n        \n        if (x == tx && y == ty) {\n            // Reconstruct path\n            vector<pair<int,int>> path;\n            pair<int,int> current = {tx, ty};\n            while (current.first != sx || current.second != sy) {\n                path.push_back(current);\n                current = parent[current.first][current.second];\n            }\n            path.push_back({sx, sy});\n            reverse(path.begin(), path.end());\n            return path;\n        }\n        \n        for (int d = 0; d < 4; d++) {\n            int nx = x + dx[d];\n            int ny = y + dy[d];\n            \n            if (is_valid(nx, ny) && !visited[nx][ny]) {\n                visited[nx][ny] = true;\n                parent[nx][ny] = {x, y};\n                dist[nx][ny] = dist[x][y] + 1;\n                q.push({nx, ny});\n            }\n        }\n    }\n    \n    return {{sx, sy}}; // Fallback\n}\n\n// Convert path to direction string\nstring path_to_directions(const vector<pair<int,int>>& path) {\n    string result;\n    for (int i = 1; i < path.size(); i++) {\n        auto [prev_x, prev_y] = path[i-1];\n        auto [curr_x, curr_y] = path[i];\n        \n        if (curr_x == prev_x - 1 && curr_y == prev_y) result += 'U';\n        else if (curr_x == prev_x + 1 && curr_y == prev_y) result += 'D';\n        else if (curr_x == prev_x && curr_y == prev_y - 1) result += 'L';\n        else if (curr_x == prev_x && curr_y == prev_y + 1) result += 'R';\n    }\n    return result;\n}\n\n// High-performing strategy from the 3.66M version with minor improvements\nstring solve_high_performance() {\n    set<pair<int,int>> all_roads(roads.begin(), roads.end());\n    set<pair<int,int>> uncovered_roads = all_roads;\n    \n    vector<pair<int,int>> path_positions = {{si, sj}};\n    int current_x = si, current_y = sj;\n    \n    // Initial coverage\n    auto initial_visible = get_visible_roads(current_x, current_y);\n    for (auto& road : initial_visible) {\n        uncovered_roads.erase(road);\n    }\n    \n    const int MAX_MOVES = 5000;\n    int moves = 0;\n    \n    // High-performance strategy - refined version of what worked well\n    while (!uncovered_roads.empty() && moves < MAX_MOVES) {\n        pair<int,int> best_target = {-1, -1};\n        double best_score = -1e9;\n        vector<pair<int,int>> best_path;\n        \n        // Intelligent candidate selection\n        vector<pair<int,int>> candidates;\n        \n        // Priority 1: Critical roads (lowest coverage potential)\n        map<pair<int,int>, int> coverage_potential;\n        for (auto& road : uncovered_roads) {\n            auto visible = get_visible_roads(road.first, road.second);\n            int potential = 0;\n            for (auto& v : visible) {\n                if (uncovered_roads.count(v)) potential++;\n            }\n            coverage_potential[road] = potential;\n        }\n        \n        // Add critical roads (potential <= 2)\n        vector<pair<int,int>> critical_roads;\n        for (auto& road : uncovered_roads) {\n            if (coverage_potential[road] <= 2) {\n                critical_roads.push_back(road);\n            }\n        }\n        \n        // Add isolated roads (few neighbors)\n        vector<pair<int,int>> isolated_roads;\n        for (auto& road : uncovered_roads) {\n            int neighbors = 0;\n            for (int d = 0; d < 4; d++) {\n                int nx = road.first + dx[d];\n                int ny = road.second + dy[d];\n                if (is_valid(nx, ny) && uncovered_roads.count({nx, ny})) {\n                    neighbors++;\n                }\n            }\n            if (neighbors <= 1) {\n                isolated_roads.push_back(road);\n            }\n        }\n        \n        // Build candidate list with good balance\n        // Priority 1: Critical roads\n        for (auto& road : critical_roads) {\n            if (candidates.size() < 25) {\n                candidates.push_back(road);\n            }\n        }\n        \n        // Priority 2: Isolated roads\n        for (auto& road : isolated_roads) {\n            if (candidates.size() < 40 && find(candidates.begin(), candidates.end(), road) == candidates.end()) {\n                candidates.push_back(road);\n            }\n        }\n        \n        // Priority 3: High potential roads\n        vector<pair<int, pair<int,int>>> sorted_potential;\n        for (auto& road : uncovered_roads) {\n            sorted_potential.push_back({coverage_potential[road], road});\n        }\n        sort(sorted_potential.begin(), sorted_potential.end());\n        \n        // Add roads with medium-high potential\n        for (int i = (int)sorted_potential.size() - 1; i >= 0 && candidates.size() < 50; i--) {\n            auto road = sorted_potential[i].second;\n            if (find(candidates.begin(), candidates.end(), road) == candidates.end()) {\n                candidates.push_back(road);\n            }\n        }\n        \n        // Evaluation with proven scoring function\n        for (auto& target : candidates) {\n            auto path_to_target = bfs_path(current_x, current_y, target.first, target.second);\n            if (path_to_target.size() <= 1) continue;\n            \n            // Calculate metrics\n            auto visible_from_target = get_visible_roads(target.first, target.second);\n            int new_coverage = 0;\n            int critical_covered = 0;\n            int isolated_covered = 0;\n            \n            for (auto& visible : visible_from_target) {\n                if (uncovered_roads.count(visible)) {\n                    new_coverage++;\n                    \n                    // Count critical roads covered\n                    if (coverage_potential[visible] <= 2) critical_covered++;\n                    \n                    // Count isolated roads covered\n                    int neighbors = 0;\n                    for (int d = 0; d < 4; d++) {\n                        int nx = visible.first + dx[d];\n                        int ny = visible.second + dy[d];\n                        if (is_valid(nx, ny) && uncovered_roads.count({nx, ny})) {\n                            neighbors++;\n                        }\n                    }\n                    if (neighbors <= 1) isolated_covered++;\n                }\n            }\n            \n            int distance = path_to_target.size() - 1;\n            double efficiency = (distance > 0) ? (double)new_coverage / distance : new_coverage;\n            \n            // Proven scoring function\n            double score = efficiency;\n            \n            // Strategic bonuses\n            score *= (1.0 + critical_covered * 0.5 + isolated_covered * 0.4);\n            \n            // Position-based bonuses\n            if (coverage_potential[target] <= 2) score *= 1.6;\n            if (find(isolated_roads.begin(), isolated_roads.end(), target) != isolated_roads.end()) {\n                score *= 1.4;\n            }\n            \n            // Distance considerations\n            if (distance > 25) {\n                score *= 0.9;\n            } else if (distance > 15) {\n                score *= 0.95;\n            }\n            \n            // Bonus for covering many roads\n            if (new_coverage >= 5) {\n                score *= (1.0 + (new_coverage - 4) * 0.1);\n            }\n            \n            if (score > best_score) {\n                best_score = score;\n                best_target = target;\n                best_path = path_to_target;\n            }\n        }\n        \n        // Robust fallback mechanisms\n        if (best_target.first == -1 || best_score <= 0.2) {\n            // Fallback 1: Find closest uncovered road\n            int min_dist = 1e9;\n            pair<int,int> closest_road = {-1, -1};\n            vector<pair<int,int>> closest_path;\n            \n            // Comprehensive search\n            int search_count = 0;\n            for (auto& road : uncovered_roads) {\n                if (search_count++ >= 150) break;\n                \n                auto path = bfs_path(current_x, current_y, road.first, road.second);\n                int dist = path.size() - 1;\n                if (dist > 0 && dist < min_dist) {\n                    min_dist = dist;\n                    closest_road = road;\n                    closest_path = path;\n                }\n            }\n            \n            if (closest_road.first != -1) {\n                best_target = closest_road;\n                best_path = closest_path;\n            } else {\n                // Fallback 2: Any valid adjacent move\n                for (int d = 0; d < 4; d++) {\n                    int nx = current_x + dx[d];\n                    int ny = current_y + dy[d];\n                    if (is_valid(nx, ny)) {\n                        best_target = {nx, ny};\n                        best_path = {{current_x, current_y}, {nx, ny}};\n                        break;\n                    }\n                }\n            }\n        }\n        \n        // Execute the move\n        if (best_target.first == -1 || best_path.size() <= 1) {\n            break;\n        }\n        \n        int segment_length = best_path.size() - 1;\n        if (moves + segment_length <= MAX_MOVES) {\n            for (int i = 1; i < best_path.size(); i++) {\n                path_positions.push_back(best_path[i]);\n                current_x = best_path[i].first;\n                current_y = best_path[i].second;\n            }\n            \n            // Update coverage\n            auto visible_now = get_visible_roads(current_x, current_y);\n            for (auto& visible : visible_now) {\n                uncovered_roads.erase(visible);\n            }\n            \n            moves += segment_length;\n        } else {\n            break;\n        }\n    }\n    \n    // Final comprehensive coverage\n    const int FINAL_ATTEMPTS = 500;\n    int final_attempts = 0;\n    while (!uncovered_roads.empty() && final_attempts < FINAL_ATTEMPTS) {\n        // Go directly to remaining uncovered roads\n        vector<pair<int,int>> remaining_roads(uncovered_roads.begin(), uncovered_roads.end());\n        \n        for (auto& road : remaining_roads) {\n            if (final_attempts >= FINAL_ATTEMPTS) break;\n            \n            auto path_to_final = bfs_path(current_x, current_y, road.first, road.second);\n            if (path_to_final.size() > 1) {\n                int path_length = path_to_final.size() - 1;\n                if (final_attempts + path_length <= FINAL_ATTEMPTS) {\n                    for (int i = 1; i < path_to_final.size(); i++) {\n                        path_positions.push_back(path_to_final[i]);\n                        current_x = path_to_final[i].first;\n                        current_y = path_to_final[i].second;\n                        final_attempts++;\n                    }\n                    \n                    // Final coverage update\n                    auto visible_final = get_visible_roads(current_x, current_y);\n                    for (auto& visible : visible_final) {\n                        uncovered_roads.erase(visible);\n                    }\n                } else {\n                    break;\n                }\n            }\n        }\n        break;\n    }\n    \n    // Return home\n    auto return_path = bfs_path(current_x, current_y, si, sj);\n    for (int i = 1; i < return_path.size(); i++) {\n        path_positions.push_back(return_path[i]);\n    }\n    \n    return path_to_directions(path_positions);\n}\n\n// Secondary strategy as backup\nstring solve_secondary_backup() {\n    vector<pair<int,int>> path_positions = {{si, sj}};\n    int current_x = si, current_y = sj;\n    \n    set<pair<int,int>> all_roads_set(roads.begin(), roads.end());\n    set<pair<int,int>> uncovered_roads = all_roads_set;\n    \n    // Initial coverage\n    auto initial_visible = get_visible_roads(current_x, current_y);\n    for (auto& road : initial_visible) {\n        uncovered_roads.erase(road);\n    }\n    \n    const int MAX_STEPS = 4800;\n    int steps = 0;\n    \n    // Secondary strategy - balanced approach\n    while (!uncovered_roads.empty() && steps < MAX_STEPS) {\n        pair<int,int> best_move = {-1, -1};\n        double best_score = -1e9;\n        vector<pair<int,int>> best_path_segment;\n        \n        // Calculate progress ratio\n        double progress_ratio = 1.0 - (double)uncovered_roads.size() / all_roads_set.size();\n        \n        // Adaptive strategy\n        if (progress_ratio < 0.7) {\n            // Early phase: coverage focus\n            vector<pair<int,int>> candidates;\n            \n            // Add adjacent positions\n            for (int d = 0; d < 4; d++) {\n                int nx = current_x + dx[d];\n                int ny = current_y + dy[d];\n                if (is_valid(nx, ny)) {\n                    candidates.push_back({nx, ny});\n                }\n            }\n            \n            // Add some uncovered roads\n            int sample_count = 0;\n            for (auto& road : uncovered_roads) {\n                if (sample_count++ >= 30) break;\n                candidates.push_back(road);\n            }\n            \n            // Evaluate candidates\n            for (auto& candidate : candidates) {\n                auto path_to_candidate = bfs_path(current_x, current_y, candidate.first, candidate.second);\n                if (path_to_candidate.size() <= 1) continue;\n                \n                auto visible_from_candidate = get_visible_roads(candidate.first, candidate.second);\n                int new_coverage = 0;\n                int critical_coverage = 0;\n                \n                for (auto& visible : visible_from_candidate) {\n                    if (uncovered_roads.count(visible)) {\n                        new_coverage++;\n                        // Check if critical\n                        auto visible_from_road = get_visible_roads(visible.first, visible.second);\n                        int potential = 0;\n                        for (auto& v : visible_from_road) {\n                            if (uncovered_roads.count(v)) potential++;\n                        }\n                        if (potential <= 2) critical_coverage++;\n                    }\n                }\n                \n                int distance = path_to_candidate.size() - 1;\n                double efficiency = (distance > 0) ? (double)new_coverage / distance : new_coverage;\n                \n                // Scoring\n                double score = efficiency;\n                score *= (1.0 + critical_coverage * 0.6);\n                \n                if (new_coverage >= 3) {\n                    score *= 1.2;\n                }\n                \n                if (score > best_score) {\n                    best_score = score;\n                    best_move = candidate;\n                    best_path_segment = path_to_candidate;\n                }\n            }\n        } else {\n            // Late phase: efficiency focus\n            int min_distance = 1e9;\n            pair<int,int> closest_remaining = {-1, -1};\n            vector<pair<int,int>> path_to_closest;\n            \n            int search_limit = min(100, (int)uncovered_roads.size());\n            int searched = 0;\n            for (auto& road : uncovered_roads) {\n                if (searched++ >= search_limit) break;\n                \n                auto path = bfs_path(current_x, current_y, road.first, road.second);\n                int distance = path.size() - 1;\n                if (distance > 0 && distance < min_distance) {\n                    min_distance = distance;\n                    closest_remaining = road;\n                    path_to_closest = path;\n                }\n            }\n            \n            if (closest_remaining.first != -1) {\n                best_path_segment = path_to_closest;\n                best_move = closest_remaining;\n                best_score = 1.0;\n            }\n        }\n        \n        // Execute the move\n        if (best_move.first != -1 && best_path_segment.size() > 1) {\n            int segment_length = best_path_segment.size() - 1;\n            if (steps + segment_length <= MAX_STEPS) {\n                for (int i = 1; i < best_path_segment.size(); i++) {\n                    path_positions.push_back(best_path_segment[i]);\n                    current_x = best_path_segment[i].first;\n                    current_y = best_path_segment[i].second;\n                    steps++;\n                }\n                \n                // Update coverage\n                auto visible_now = get_visible_roads(current_x, current_y);\n                for (auto& visible : visible_now) {\n                    uncovered_roads.erase(visible);\n                }\n            } else {\n                break;\n            }\n        } else {\n            // No good move, make any valid move\n            bool moved = false;\n            for (int d = 0; d < 4; d++) {\n                int nx = current_x + dx[d];\n                int ny = current_y + dy[d];\n                if (is_valid(nx, ny)) {\n                    current_x = nx;\n                    current_y = ny;\n                    path_positions.push_back({current_x, current_y});\n                    steps++;\n                    moved = true;\n                    \n                    // Update coverage\n                    auto visible_now = get_visible_roads(current_x, current_y);\n                    for (auto& visible : visible_now) {\n                        uncovered_roads.erase(visible);\n                    }\n                    break;\n                }\n            }\n            if (!moved) break;\n        }\n    }\n    \n    // Return home\n    auto return_path = bfs_path(current_x, current_y, si, sj);\n    for (int i = 1; i < return_path.size(); i++) {\n        path_positions.push_back(return_path[i]);\n    }\n    \n    return path_to_directions(path_positions);\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    srand(time(NULL));\n    \n    cin >> N >> si >> sj;\n    \n    for (int i = 0; i < N; i++) {\n        string line;\n        cin >> line;\n        for (int j = 0; j < N; j++) {\n            grid[i][j] = line[j];\n            if (line[j] >= '5' && line[j] <= '9') {\n                is_road[i][j] = true;\n                roads.push_back({i, j});\n            } else {\n                is_road[i][j] = false;\n            }\n        }\n    }\n    \n    // Generate our two proven strategies\n    string result1 = solve_high_performance();\n    string result2 = solve_secondary_backup();\n    \n    // Choose based on length - high performance should be better\n    if (result1.length() >= result2.length() * 0.95) {\n        cout << result1 << endl;\n    } else {\n        cout << result2 << endl;\n    }\n    \n    return 0;\n}","future-contest-2022-qual":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MAX_N = 1000;\nconst int MAX_M = 20;\n\nint N, M, K, R;\nvector<vector<int>> task_difficulty;\nvector<vector<int>> task_dependencies;\nvector<vector<int>> reverse_dependencies;\nvector<int> task_status;  // -1: not started, 0: in progress, 1: completed\nvector<int> member_current_task;\nvector<bool> task_ready;\nvector<int> in_degree;\n\nvoid update_ready_tasks() {\n    for (int i = 0; i < N; i++) {\n        if (task_status[i] == -1 && !task_ready[i]) {\n            bool all_deps_met = true;\n            for (int dep : task_dependencies[i]) {\n                if (task_status[dep] != 1) {\n                    all_deps_met = false;\n                    break;\n                }\n            }\n            if (all_deps_met) {\n                task_ready[i] = true;\n            }\n        }\n    }\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    cin >> N >> M >> K >> R;\n    \n    task_difficulty.assign(N, vector<int>(K));\n    task_dependencies.assign(N, vector<int>());\n    reverse_dependencies.assign(N, vector<int>());\n    task_status.assign(N, -1);\n    member_current_task.assign(M, -1);\n    task_ready.assign(N, false);\n    in_degree.assign(N, 0);\n    \n    // Read task difficulties\n    for (int i = 0; i < N; i++) {\n        for (int k = 0; k < K; k++) {\n            cin >> task_difficulty[i][k];\n        }\n    }\n    \n    // Read dependencies\n    for (int i = 0; i < R; i++) {\n        int u, v;\n        cin >> u >> v;\n        u--; v--;\n        task_dependencies[v].push_back(u);\n        reverse_dependencies[u].push_back(v);\n        in_degree[v]++;\n    }\n    \n    // Initialize ready tasks (those with no dependencies)\n    update_ready_tasks();\n    \n    vector<bool> task_assigned_today(N, false);\n    vector<bool> member_busy_today(M, false);\n    \n    int day = 0;\n    while (day < 2000) {\n        day++;\n        \n        // Reset daily tracking\n        fill(task_assigned_today.begin(), task_assigned_today.end(), false);\n        fill(member_busy_today.begin(), member_busy_today.end(), false);\n        \n        // Update which tasks are ready\n        update_ready_tasks();\n        \n        // Prepare assignments for today\n        vector<pair<int, int>> assignments; // {member, task}\n        \n        // Simple assignment: assign ready tasks to free members\n        for (int task = 0; task < N; task++) {\n            if (task_ready[task] && task_status[task] == -1 && !task_assigned_today[task]) {\n                // Find first available member\n                for (int member = 0; member < M; member++) {\n                    if (member_current_task[member] == -1 && !member_busy_today[member]) {\n                        assignments.push_back({member, task});\n                        member_busy_today[member] = true;\n                        task_assigned_today[task] = true;\n                        member_current_task[member] = task;\n                        task_status[task] = 0; // Mark as in progress\n                        break;\n                    }\n                }\n            }\n        }\n        \n        // Output assignments\n        cout << assignments.size();\n        for (auto& assignment : assignments) {\n            cout << \" \" << (assignment.first + 1) << \" \" << (assignment.second + 1);\n        }\n        cout << \"\\n\";\n        cout.flush();\n        \n        // Read completion information\n        int completion_count;\n        cin >> completion_count;\n        \n        if (completion_count == -1) {\n            break;\n        }\n        \n        if (completion_count == 0) {\n            continue;\n        }\n        \n        // Process completed tasks\n        for (int i = 0; i < completion_count; i++) {\n            int member_id;\n            cin >> member_id;\n            member_id--;\n            \n            int task_id = member_current_task[member_id];\n            if (task_id != -1) {\n                task_status[task_id] = 1; // Mark as completed\n                member_current_task[member_id] = -1; // Member is now free\n                \n                // Make dependent tasks ready if possible\n                for (int dependent : reverse_dependencies[task_id]) {\n                    bool all_deps_done = true;\n                    for (int dep : task_dependencies[dependent]) {\n                        if (task_status[dep] != 1) {\n                            all_deps_done = false;\n                            break;\n                        }\n                    }\n                    if (all_deps_done) {\n                        task_ready[dependent] = true;\n                    }\n                }\n            }\n        }\n    }\n    \n    return 0;\n}","ahc006":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int DEPOT_X = 400;\nconst int DEPOT_Y = 400;\nconst int NUM_ORDERS = 1000;\nconst int SELECT_COUNT = 50;\n\nstruct Order {\n    int id, a, b, c, d;\n    \n    Order(int _id, int _a, int _b, int _c, int _d) \n        : id(_id), a(_a), b(_b), c(_c), d(_d) {}\n    \n    int distance() const {\n        return abs(a - c) + abs(b - d);\n    }\n};\n\nstruct Point {\n    int x, y;\n    Point() : x(0), y(0) {}\n    Point(int _x, int _y) : x(_x), y(_y) {}\n    \n    int distance(const Point& other) const {\n        return abs(x - other.x) + abs(y - other.y);\n    }\n};\n\n// Select orders based on clustering and efficiency\nvector<int> selectOrders(const vector<Order>& orders) {\n    vector<pair<double, int>> scores;\n    \n    // Calculate approximate center of all orders\n    long long sum_a = 0, sum_b = 0, sum_c = 0, sum_d = 0;\n    for (const auto& order : orders) {\n        sum_a += order.a;\n        sum_b += order.b;\n        sum_c += order.c;\n        sum_d += order.d;\n    }\n    \n    int center_a_x = sum_a / NUM_ORDERS;\n    int center_a_y = sum_b / NUM_ORDERS;\n    int center_c_x = sum_c / NUM_ORDERS;\n    int center_c_y = sum_d / NUM_ORDERS;\n    \n    for (int i = 0; i < NUM_ORDERS; i++) {\n        const Order& order = orders[i];\n        \n        // Direct distance\n        int order_dist = order.distance();\n        \n        // Distance from centers\n        int dist_to_center_a = abs(order.a - center_a_x) + abs(order.b - center_a_y);\n        int dist_to_center_c = abs(order.c - center_c_x) + abs(order.d - center_c_y);\n        \n        // Distance-based score: prefer orders that are efficient and clustered\n        double score = (double)order_dist / (1.0 + dist_to_center_a * 0.2 + dist_to_center_c * 0.2);\n        \n        scores.push_back({score, i});\n    }\n    \n    sort(scores.begin(), scores.end(), greater<pair<double, int>>());\n    \n    vector<int> selected;\n    for (int i = 0; i < SELECT_COUNT; i++) {\n        selected.push_back(scores[i].second);\n    }\n    \n    return selected;\n}\n\n// Build initial route with pickups followed by deliveries\nvector<Point> buildInitialRoute(const vector<Order>& orders, const vector<int>& selected_ids) {\n    vector<Point> route;\n    route.push_back(Point(DEPOT_X, DEPOT_Y));\n    \n    // Add all pickups\n    for (int id : selected_ids) {\n        route.push_back(Point(orders[id].a, orders[id].b));\n    }\n    \n    // Add all deliveries\n    for (int id : selected_ids) {\n        route.push_back(Point(orders[id].c, orders[id].d));\n    }\n    \n    route.push_back(Point(DEPOT_X, DEPOT_Y));\n    \n    return route;\n}\n\n// Safe 2-opt that respects pickup-delivery constraints\nvoid safeTwoOpt(vector<Point>& route, const vector<int>& selected_ids) {\n    int n = route.size();\n    if (n <= 4) return;\n    \n    // Limit iterations for performance\n    for (int iter = 0; iter < 50; iter++) {\n        bool improved = false;\n        \n        // Only consider swaps that don't violate pickup-before-delivery\n        // Pickups are at indices 1 to SELECT_COUNT\n        // Deliveries are at indices SELECT_COUNT+1 to 2*SELECT_COUNT\n        \n        for (int i = 1; i < n - 2 && i < 30; i++) {\n            for (int j = i + 1; j < min(n - 1, i + 40); j++) {\n                // Check if this swap is safe (doesn't mix pickups and deliveries incorrectly)\n                bool safe_swap = true;\n                \n                // For simplicity, only allow swaps within pickup segment or within delivery segment\n                bool in_pickup_segment = (i >= 1 && j <= SELECT_COUNT);\n                bool in_delivery_segment = (i >= SELECT_COUNT + 1 && j <= 2 * SELECT_COUNT + 1);\n                \n                if (!in_pickup_segment && !in_delivery_segment) {\n                    // Cross-segment swaps are complex, skip for now\n                    continue;\n                }\n                \n                long long current_dist = (long long)route[i-1].distance(route[i]) + route[j].distance(route[j+1]);\n                long long new_dist = (long long)route[i-1].distance(route[j]) + route[i].distance(route[j+1]);\n                \n                if (new_dist + 2 < current_dist) {\n                    reverse(route.begin() + i, route.begin() + j + 1);\n                    improved = true;\n                    break;\n                }\n            }\n            if (improved) break;\n        }\n        \n        if (!improved) break;\n    }\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    vector<Order> orders;\n    \n    // Read input\n    for (int i = 0; i < NUM_ORDERS; i++) {\n        int a, b, c, d;\n        cin >> a >> b >> c >> d;\n        orders.emplace_back(i, a, b, c, d);\n    }\n    \n    // Select orders\n    vector<int> selected_ids = selectOrders(orders);\n    \n    // Build initial route\n    vector<Point> route = buildInitialRoute(orders, selected_ids);\n    \n    // Improve route with safe local search\n    safeTwoOpt(route, selected_ids);\n    \n    // Output\n    cout << SELECT_COUNT;\n    for (int id : selected_ids) {\n        cout << \" \" << (id + 1); // 1-indexed\n    }\n    cout << \"\\n\";\n    \n    cout << route.size();\n    for (const Point& p : route) {\n        cout << \" \" << p.x << \" \" << p.y;\n    }\n    cout << \"\\n\";\n    \n    return 0;\n}","ahc007":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 400;\nconst int M = 1995;\n\nclass UnionFind {\nprivate:\n    vector<int> parent, rank;\n    vector<int> component_size;\npublic:\n    UnionFind(int n) : parent(n), rank(n, 0), component_size(n, 1) {\n        for (int i = 0; i < n; i++) {\n            parent[i] = i;\n        }\n    }\n    \n    int find(int x) {\n        if (parent[x] != x) {\n            parent[x] = find(parent[x]);\n        }\n        return parent[x];\n    }\n    \n    bool unite(int x, int y) {\n        x = find(x);\n        y = find(y);\n        if (x == y) return false;\n        if (rank[x] < rank[y]) swap(x, y);\n        parent[y] = x;\n        component_size[x] += component_size[y];\n        if (rank[x] == rank[y]) rank[x]++;\n        return true;\n    }\n    \n    bool same(int x, int y) {\n        return find(x) == find(y);\n    }\n    \n    int get_component_size(int x) {\n        return component_size[find(x)];\n    }\n    \n    int count_components() {\n        int count = 0;\n        for (int i = 0; i < N; i++) {\n            if (find(i) == i) count++;\n        }\n        return count;\n    }\n};\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    // Read coordinates\n    vector<pair<int, int>> coords(N);\n    for (int i = 0; i < N; i++) {\n        cin >> coords[i].first >> coords[i].second;\n    }\n    \n    // Read edges\n    vector<pair<int, int>> edges(M);\n    for (int i = 0; i < M; i++) {\n        cin >> edges[i].first >> edges[i].second;\n    }\n    \n    // Precompute minimum distances\n    vector<int> min_distances(M);\n    for (int i = 0; i < M; i++) {\n        int u = edges[i].first;\n        int v = edges[i].second;\n        long long dx = (long long)(coords[u].first - coords[v].first);\n        long long dy = (long long)(coords[u].second - coords[v].second);\n        long long dist_sq = dx * dx + dy * dy;\n        double dist = sqrt((double)dist_sq);\n        min_distances[i] = max(1, (int)round(dist));\n    }\n    \n    // Initialize Union-Find\n    UnionFind uf(N);\n    \n    // Process edges as they arrive\n    for (int i = 0; i < M; i++) {\n        int length;\n        cin >> length;\n        \n        int u = edges[i].first;\n        int v = edges[i].second;\n        int min_dist = min_distances[i];\n        \n        // Check if this edge connects two different components\n        if (!uf.same(u, v)) {\n            // Essential for connectivity - accept\n            uf.unite(u, v);\n            cout << \"1\" << endl;\n            cout.flush();\n        } else {\n            // Creating a cycle - decide based on value\n            double ratio = (double)length / min_dist;\n            \n            // Calculate critical metrics\n            int current_components = uf.count_components();\n            int edges_remaining = M - i - 1;\n            int components_to_connect = current_components - 1;\n            \n            // Progress through the algorithm\n            double progress = (double)i / M;\n            \n            bool accept = false;\n            \n            // Final optimized thresholds\n            if (progress < 0.305) {\n                // Very early - extremely selective\n                if (ratio <= 1.012) {\n                    accept = true;\n                } else if (ratio <= 1.085 && min_dist <= 8) {\n                    accept = true;\n                } else if (ratio <= 1.165 && min_dist <= 2) {\n                    accept = true;\n                }\n            } else if (progress < 0.605) {\n                // Early-mid - selective but reasonable\n                if (ratio <= 1.058) {\n                    accept = true;\n                } else if (ratio <= 1.165 && min_dist <= 14) {\n                    accept = true;\n                } else if (ratio <= 1.305 && min_dist <= 7) {\n                    accept = true;\n                } else if (ratio <= 1.455 && min_dist <= 3) {\n                    accept = true;\n                }\n            } else if (progress < 0.785) {\n                // Mid-late - balanced approach\n                if (ratio <= 1.095) {\n                    accept = true;\n                } else if (ratio <= 1.225 && min_dist <= 17) {\n                    accept = true;\n                } else if (ratio <= 1.405 && min_dist <= 8) {\n                    accept = true;\n                } else if (ratio <= 1.635 && min_dist <= 4) {\n                    accept = true;\n                } else if (ratio <= 1.935 && min_dist <= 2) {\n                    accept = true;\n                }\n            } else {\n                // End game - sophisticated handling\n                bool edge_critical = (edges_remaining <= components_to_connect + 11);\n                \n                if (edge_critical) {\n                    // Critical situation handling\n                    if (ratio <= 1.445) {\n                        accept = true;\n                    } else if (ratio <= 1.745 && min_dist <= 14) {\n                        accept = true;\n                    } else if (ratio <= 2.005 && min_dist <= 6) {\n                        accept = true;\n                    } else if (ratio <= 2.505 && min_dist <= 3) {\n                        accept = true;\n                    }\n                } else {\n                    // Normal end game\n                    if (ratio <= 1.145) {\n                        accept = true;\n                    } else if (ratio <= 1.305 && min_dist <= 21) {\n                        accept = true;\n                    } else if (ratio <= 1.505 && min_dist <= 10) {\n                        accept = true;\n                    } else if (ratio <= 1.845 && min_dist <= 5) {\n                        accept = true;\n                    } else if (ratio <= 2.145 && min_dist <= 2) {\n                        accept = true;\n                    }\n                }\n            }\n            \n            if (accept) {\n                cout << \"1\" << endl;\n                cout.flush();\n            } else {\n                cout << \"0\" << endl;\n                cout.flush();\n            }\n        }\n    }\n    \n    return 0;\n}","ahc008":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int GRID_SIZE = 30;\nconst int MAX_TURNS = 300;\nconst int dx[4] = {-1, 1, 0, 0}; // U, D, L, R\nconst int dy[4] = {0, 0, -1, 1};\nconst char dir_chars[4] = {'u', 'd', 'l', 'r'};\nconst char move_chars[4] = {'U', 'D', 'L', 'R'};\n\nstruct Position {\n    int x, y;\n    Position() : x(0), y(0) {}\n    Position(int _x, int _y) : x(_x), y(_y) {}\n    bool operator==(const Position& other) const {\n        return x == other.x && y == other.y;\n    }\n    bool operator!=(const Position& other) const {\n        return !(*this == other);\n    }\n    // Add comparison operator for use in std::set\n    bool operator<(const Position& other) const {\n        if (x != other.x) return x < other.x;\n        return y < other.y;\n    }\n};\n\nstruct Pet {\n    Position pos;\n    int type;\n    \n    Pet() : pos(0, 0), type(0) {}\n    Pet(int x, int y, int t) : pos(x, y), type(t) {}\n};\n\nstruct Human {\n    Position pos;\n    \n    Human() : pos(0, 0) {}\n    Human(int x, int y) : pos(x, y) {}\n};\n\nclass Grid {\npublic:\n    bool blocked[GRID_SIZE][GRID_SIZE];\n    bool pet_present[GRID_SIZE][GRID_SIZE];\n    bool human_present[GRID_SIZE][GRID_SIZE];\n    \n    Grid() {\n        memset(blocked, false, sizeof(blocked));\n        memset(pet_present, false, sizeof(pet_present));\n        memset(human_present, false, sizeof(human_present));\n    }\n    \n    void reset_presence() {\n        memset(pet_present, false, sizeof(pet_present));\n        memset(human_present, false, sizeof(human_present));\n    }\n    \n    bool is_valid(int x, int y) {\n        return x >= 0 && x < GRID_SIZE && y >= 0 && y < GRID_SIZE;\n    }\n    \n    bool is_passable(int x, int y) {\n        return is_valid(x, y) && !blocked[x][y];\n    }\n    \n    void block_square(int x, int y) {\n        if (is_valid(x, y)) {\n            blocked[x][y] = true;\n        }\n    }\n};\n\nclass Solver {\nprivate:\n    int N, M;\n    vector<Pet> pets;\n    vector<Human> humans;\n    Grid grid;\n    mt19937 rng;\n    \npublic:\n    Solver() : rng(chrono::steady_clock::now().time_since_epoch().count()) {}\n    \n    void read_input() {\n        cin >> N;\n        pets.resize(N);\n        for (int i = 0; i < N; i++) {\n            int x, y, t;\n            cin >> x >> y >> t;\n            pets[i] = Pet(x-1, y-1, t); // Convert to 0-indexed\n        }\n        \n        cin >> M;\n        humans.resize(M);\n        for (int i = 0; i < M; i++) {\n            int x, y;\n            cin >> x >> y;\n            humans[i] = Human(x-1, y-1); // Convert to 0-indexed\n        }\n    }\n    \n    // Check if a position is adjacent to any pet\n    bool is_adjacent_to_any_pet(int x, int y) {\n        if (!grid.is_valid(x, y)) return true;\n        \n        for (const auto& pet : pets) {\n            if (abs(pet.pos.x - x) + abs(pet.pos.y - y) <= 1) {\n                return true;\n            }\n        }\n        return false;\n    }\n    \n    // Check if moving in a direction is safe\n    bool is_safe_move(const Human& human, int direction) {\n        int nx = human.pos.x + dx[direction];\n        int ny = human.pos.y + dy[direction];\n        return grid.is_passable(nx, ny) && !is_adjacent_to_any_pet(nx, ny);\n    }\n    \n    // Find the closest pet to a human\n    int find_closest_pet(int human_id) {\n        Human& human = humans[human_id];\n        int closest_pet = -1;\n        int min_dist = 10000;\n        \n        for (int i = 0; i < N; i++) {\n            int dist = abs(human.pos.x - pets[i].pos.x) + abs(human.pos.y - pets[i].pos.y);\n            if (dist < min_dist) {\n                min_dist = dist;\n                closest_pet = i;\n            }\n        }\n        \n        return closest_pet;\n    }\n    \n    // Try to block a square that limits pet movement\n    string try_block_strategy(int human_id) {\n        Human& human = humans[human_id];\n        \n        // Don't block if we're adjacent to a pet\n        if (is_adjacent_to_any_pet(human.pos.x, human.pos.y)) {\n            return \"\";\n        }\n        \n        // Try to block squares that would limit pet movement\n        for (int d = 0; d < 4; d++) {\n            int nx = human.pos.x + dx[d];\n            int ny = human.pos.y + dy[d];\n            \n            if (grid.is_valid(nx, ny) && !grid.blocked[nx][ny]) {\n                // Check if this square is occupied by pet or human\n                bool occupied = false;\n                \n                // Check for pets\n                for (const auto& pet : pets) {\n                    if (pet.pos.x == nx && pet.pos.y == ny) {\n                        occupied = true;\n                        break;\n                    }\n                }\n                \n                // Check for humans\n                for (int i = 0; i < M; i++) {\n                    if (i != human_id && humans[i].pos.x == nx && humans[i].pos.y == ny) {\n                        occupied = true;\n                        break;\n                    }\n                }\n                \n                // Check if adjacent to any pet (rule constraint)\n                if (!occupied && !is_adjacent_to_any_pet(nx, ny)) {\n                    return string(1, dir_chars[d]);\n                }\n            }\n        }\n        \n        return \"\";\n    }\n    \n    // Try to move towards a strategic position\n    string try_move_strategy(int human_id) {\n        Human& human = humans[human_id];\n        \n        // Try to move away from pets if too close\n        if (is_adjacent_to_any_pet(human.pos.x, human.pos.y)) {\n            for (int d = 0; d < 4; d++) {\n                if (is_safe_move(human, d)) {\n                    return string(1, move_chars[d]);\n                }\n            }\n            return \".\";\n        }\n        \n        // Try to move towards closest pet but maintain safe distance\n        int closest_pet = find_closest_pet(human_id);\n        if (closest_pet != -1) {\n            Position target = pets[closest_pet].pos;\n            \n            // Try moves that get us closer but maintain safety\n            vector<pair<int, int>> candidates;\n            for (int d = 0; d < 4; d++) {\n                if (is_safe_move(human, d)) {\n                    int nx = human.pos.x + dx[d];\n                    int ny = human.pos.y + dy[d];\n                    int new_dist = abs(nx - target.x) + abs(ny - target.y);\n                    int old_dist = abs(human.pos.x - target.x) + abs(human.pos.y - target.y);\n                    \n                    if (new_dist < old_dist) {\n                        candidates.push_back({new_dist, d});\n                    }\n                }\n            }\n            \n            if (!candidates.empty()) {\n                sort(candidates.begin(), candidates.end());\n                return string(1, move_chars[candidates[0].second]);\n            }\n        }\n        \n        // If no strategic move, try any safe move\n        for (int d = 0; d < 4; d++) {\n            if (is_safe_move(human, d)) {\n                return string(1, move_chars[d]);\n            }\n        }\n        \n        return \".\";\n    }\n    \n    string get_action(int human_id) {\n        // First try blocking strategy\n        string block_action = try_block_strategy(human_id);\n        if (!block_action.empty()) {\n            return block_action;\n        }\n        \n        // If no good blocking, try moving\n        return try_move_strategy(human_id);\n    }\n    \n    void update_grid_state(const string& actions) {\n        // First, collect all intended blocking positions\n        vector<pair<int, Position>> block_intentions; // {human_id, position}\n        for (int i = 0; i < M; i++) {\n            char action = actions[i];\n            if (action == 'u' || action == 'd' || action == 'l' || action == 'r') {\n                int d = -1;\n                for (int j = 0; j < 4; j++) {\n                    if (dir_chars[j] == action) {\n                        d = j;\n                        break;\n                    }\n                }\n                if (d != -1) {\n                    int nx = humans[i].pos.x + dx[d];\n                    int ny = humans[i].pos.y + dy[d];\n                    if (grid.is_valid(nx, ny)) {\n                        block_intentions.push_back({i, Position(nx, ny)});\n                    }\n                }\n            }\n        }\n        \n        // Resolve conflicts: only allow one human to block each square\n        set<Position> blocked_this_turn;\n        vector<Position> final_blocked_positions;\n        \n        for (const auto& intention : block_intentions) {\n            // int human_id = intention.first; // Removed unused variable\n            Position pos = intention.second;\n            \n            // Check if position is already blocked this turn\n            if (blocked_this_turn.find(pos) != blocked_this_turn.end()) {\n                continue;\n            }\n            \n            // Check if position contains a pet\n            bool has_pet = false;\n            for (const auto& pet : pets) {\n                if (pet.pos.x == pos.x && pet.pos.y == pos.y) {\n                    has_pet = true;\n                    break;\n                }\n            }\n            \n            // Check if position contains a human\n            bool has_human = false;\n            for (int i = 0; i < M; i++) {\n                if (humans[i].pos.x == pos.x && humans[i].pos.y == pos.y) {\n                    has_human = true;\n                    break;\n                }\n            }\n            \n            // Check if adjacent to any pet (rule constraint)\n            bool adjacent_to_pet = is_adjacent_to_any_pet(pos.x, pos.y);\n            \n            // If all constraints are satisfied, block the square\n            if (!has_pet && !has_human && !adjacent_to_pet) {\n                blocked_this_turn.insert(pos);\n                final_blocked_positions.push_back(pos);\n            }\n        }\n        \n        // Apply all blocking\n        for (const auto& pos : final_blocked_positions) {\n            grid.block_square(pos.x, pos.y);\n        }\n        \n        // Then process movement actions\n        for (int i = 0; i < M; i++) {\n            char action = actions[i];\n            if (action == 'U' || action == 'D' || action == 'L' || action == 'R') {\n                int d = -1;\n                for (int j = 0; j < 4; j++) {\n                    if (move_chars[j] == action) {\n                        d = j;\n                        break;\n                    }\n                }\n                if (d != -1) {\n                    int nx = humans[i].pos.x + dx[d];\n                    int ny = humans[i].pos.y + dy[d];\n                    if (grid.is_passable(nx, ny)) {\n                        humans[i].pos.x = nx;\n                        humans[i].pos.y = ny;\n                    }\n                }\n            }\n        }\n    }\n    \n    void update_positions(const vector<string>& pet_moves) {\n        // Update pet positions based on their moves\n        for (int i = 0; i < N; i++) {\n            const string& moves = pet_moves[i];\n            for (char move : moves) {\n                if (move == 'U' && pets[i].pos.x > 0) pets[i].pos.x--;\n                else if (move == 'D' && pets[i].pos.x < GRID_SIZE - 1) pets[i].pos.x++;\n                else if (move == 'L' && pets[i].pos.y > 0) pets[i].pos.y--;\n                else if (move == 'R' && pets[i].pos.y < GRID_SIZE - 1) pets[i].pos.y++;\n            }\n        }\n    }\n    \n    void update_grid_presence() {\n        grid.reset_presence();\n        for (const auto& pet : pets) {\n            if (grid.is_valid(pet.pos.x, pet.pos.y)) {\n                grid.pet_present[pet.pos.x][pet.pos.y] = true;\n            }\n        }\n        for (const auto& human : humans) {\n            if (grid.is_valid(human.pos.x, human.pos.y)) {\n                grid.human_present[human.pos.x][human.pos.y] = true;\n            }\n        }\n    }\n    \n    void solve() {\n        read_input();\n        \n        // Initialize grid presence\n        update_grid_presence();\n        \n        for (int turn = 0; turn < MAX_TURNS; turn++) {\n            string actions = \"\";\n            for (int i = 0; i < M; i++) {\n                actions += get_action(i);\n            }\n            cout << actions << endl;\n            cout.flush();\n            \n            // Update grid based on human actions\n            update_grid_state(actions);\n            \n            // Read pet moves\n            vector<string> pet_moves(N);\n            for (int i = 0; i < N; i++) {\n                cin >> pet_moves[i];\n            }\n            \n            // Update positions\n            update_positions(pet_moves);\n            \n            // Update grid presence\n            update_grid_presence();\n        }\n    }\n};\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    Solver solver;\n    solver.solve();\n    \n    return 0;\n}","ahc009":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int SIZE = 20;\nconst int MAX_LENGTH = 200;\n\n// Directions: U, D, L, R\nconst int dr[] = {-1, 1, 0, 0};\nconst int dc[] = {0, 0, -1, 1};\nconst char dir_chars[] = {'U', 'D', 'L', 'R'};\n\nint si, sj, ti, tj;\ndouble p;\nbool h_walls[SIZE][SIZE-1]; // horizontal walls\nbool v_walls[SIZE-1][SIZE]; // vertical walls\n\nbool can_move(int r, int c, int dir) {\n    if (dir == 0) { // Up\n        if (r == 0) return false;\n        return !v_walls[r-1][c];\n    } else if (dir == 1) { // Down\n        if (r == SIZE-1) return false;\n        return !v_walls[r][c];\n    } else if (dir == 2) { // Left\n        if (c == 0) return false;\n        return !h_walls[r][c-1];\n    } else { // Right\n        if (c == SIZE-1) return false;\n        return !h_walls[r][c];\n    }\n}\n\n// Find shortest path using BFS\nstring find_shortest_path() {\n    if (si == ti && sj == tj) {\n        return \"\";\n    }\n    \n    queue<tuple<int, int, string>> q;\n    bool visited[SIZE][SIZE] = {};\n    \n    q.push({si, sj, \"\"});\n    visited[si][sj] = true;\n    \n    while (!q.empty()) {\n        auto [r, c, path] = q.front();\n        q.pop();\n        \n        if (r == ti && c == tj) {\n            return path;\n        }\n        \n        if (path.length() > MAX_LENGTH) continue;\n        \n        // Try all 4 directions\n        for (int dir = 0; dir < 4; dir++) {\n            int nr = r + dr[dir];\n            int nc = c + dc[dir];\n            \n            if (nr >= 0 && nr < SIZE && nc >= 0 && nc < SIZE && !visited[nr][nc]) {\n                if (can_move(r, c, dir)) {\n                    visited[nr][nc] = true;\n                    string new_path = path + dir_chars[dir];\n                    if (new_path.length() <= MAX_LENGTH) {\n                        q.push({nr, nc, new_path});\n                    }\n                }\n            }\n        }\n    }\n    \n    return \"\"; // No path found\n}\n\n// Generate a guaranteed valid path to target\nstring generate_guaranteed_path() {\n    string path = \"\";\n    int r = si, c = sj;\n    \n    // Use a smarter approach that tries to make consistent progress\n    while ((r != ti || c != tj) && path.length() < MAX_LENGTH) {\n        // Calculate direction to target\n        int dr_target = ti - r;\n        int dc_target = tj - c;\n        \n        bool moved = false;\n        \n        // Use a preference order based on which direction gives more progress\n        vector<pair<int, int>> move_prefs; // {priority, direction}\n        \n        // Add moves with priorities\n        if (dr_target != 0) {\n            move_prefs.push_back({abs(dr_target) * 1000 + abs(dc_target), dr_target > 0 ? 1 : 0});\n        }\n        if (dc_target != 0) {\n            move_prefs.push_back({abs(dc_target) * 1000 + abs(dr_target), dc_target > 0 ? 3 : 2});\n        }\n        \n        // Sort by priority (higher priority first)\n        sort(move_prefs.rbegin(), move_prefs.rend());\n        \n        // Try moves in priority order\n        for (auto& pref : move_prefs) {\n            int dir = pref.second;\n            if (can_move(r, c, dir)) {\n                path += dir_chars[dir];\n                r += dr[dir];\n                c += dc[dir];\n                moved = true;\n                break;\n            }\n        }\n        \n        // If no preferred move works, try any valid move\n        if (!moved) {\n            for (int dir = 0; dir < 4; dir++) {\n                if (can_move(r, c, dir)) {\n                    path += dir_chars[dir];\n                    r += dr[dir];\n                    c += dc[dir];\n                    moved = true;\n                    break;\n                }\n            }\n        }\n        \n        // If completely stuck, add a move anyway (will just stay in place)\n        if (!moved && path.length() < MAX_LENGTH) {\n            // Add move in general direction of target\n            if (dr_target > 0) {\n                path += 'D';\n            } else if (dr_target < 0) {\n                path += 'U';\n            } else if (dc_target > 0) {\n                path += 'R';\n            } else if (dc_target < 0) {\n                path += 'L';\n            } else {\n                path += 'D'; // Default\n            }\n        }\n        \n        // Prevent infinite loops\n        if (path.length() >= MAX_LENGTH - 5) {\n            break;\n        }\n    }\n    \n    // Final approach to target\n    int safety = 0;\n    while ((r != ti || c != tj) && path.length() < MAX_LENGTH && safety < 20) {\n        safety++;\n        \n        if (r != ti) {\n            if (r < ti && can_move(r, c, 1)) {\n                path += 'D';\n                r++;\n            } else if (r > ti && can_move(r, c, 0)) {\n                path += 'U';\n                r--;\n            }\n        }\n        \n        if (c != tj) {\n            if (c < tj && can_move(r, c, 3)) {\n                path += 'R';\n                c++;\n            } else if (c > tj && can_move(r, c, 2)) {\n                path += 'L';\n                c--;\n            }\n        }\n    }\n    \n    if (path.length() > MAX_LENGTH) {\n        path = path.substr(0, MAX_LENGTH);\n    }\n    \n    return path;\n}\n\n// Create an advanced robust path with OPTIMIZED redundancy\nstring create_advanced_robust_path() {\n    // Get base path\n    string base_path = find_shortest_path();\n    \n    if (base_path.empty()) {\n        base_path = generate_guaranteed_path();\n    }\n    \n    // Handle empty path case\n    if (base_path.empty()) {\n        // Create a robust default path\n        string fallback = \"\";\n        int r = si, c = sj;\n        \n        // Snake pattern that covers area and is robust\n        bool going_down = true;\n        while (fallback.length() < MAX_LENGTH - 10) {\n            if (going_down) {\n                if (r < ti && r < SIZE-1) {\n                    if (can_move(r, c, 1)) {\n                        fallback += 'D';\n                        r++;\n                    } else {\n                        going_down = false;\n                    }\n                } else {\n                    going_down = false;\n                }\n            } else {\n                if (c < tj && c < SIZE-1) {\n                    if (can_move(r, c, 3)) {\n                        fallback += 'R';\n                        c++;\n                    } else if (r > 0 && can_move(r, c, 0)) {\n                        fallback += 'U';\n                        r--;\n                        going_down = true;\n                    } else {\n                        break;\n                    }\n                } else if (c > tj && c > 0) {\n                    if (can_move(r, c, 2)) {\n                        fallback += 'L';\n                        c--;\n                    } else if (r > 0 && can_move(r, c, 0)) {\n                        fallback += 'U';\n                        r--;\n                        going_down = true;\n                    } else {\n                        break;\n                    }\n                } else {\n                    break;\n                }\n            }\n            if (fallback.length() >= MAX_LENGTH - 15) break;\n        }\n        base_path = fallback;\n    }\n    \n    // Create robust path with **OPTIMIZED** strategy\n    string robust_path = \"\";\n    \n    // **OPTIMIZED** redundancy calculation based on extensive testing\n    double base_redundancy = 1.0;\n    if (p <= 0.13) {\n        base_redundancy = 1.15;  // **TUNED**\n    } else if (p <= 0.22) {\n        base_redundancy = 1.45;  // **TUNED**\n    } else if (p <= 0.32) {\n        base_redundancy = 1.9;   // **TUNED**\n    } else if (p <= 0.42) {\n        base_redundancy = 2.4;   // **TUNED**\n    } else {\n        base_redundancy = 2.9;   // **TUNED**\n    }\n    \n    // Calculate target length\n    int target_length = min(MAX_LENGTH, (int)(base_path.length() * base_redundancy));\n    \n    if (p <= 0.15) {\n        // Very low forget probability - minimal but smart redundancy\n        robust_path = base_path;\n        if (robust_path.length() + 18 <= MAX_LENGTH) {  // **INCREASED** from 15 to 18\n            // Add a smart pattern that helps with robustness\n            robust_path += \"DRURDLULDRURDLULDR\";  // **EXTENDED** pattern\n        }\n    } else if (p <= 0.25) {\n        // Low forget probability - moderate redundancy\n        robust_path = base_path;\n        if (robust_path.length() + min(30, (int)(base_path.length() * 0.9)) <= MAX_LENGTH) {  // **TUNED**\n            robust_path += base_path.substr(0, min(30, (int)(base_path.length() * 0.9)));  // **TUNED**\n        }\n        if (robust_path.length() + 18 <= MAX_LENGTH) {  // **INCREASED**\n            robust_path += \"DRURDLULDRURDLULDR\";  // **EXTENDED**\n        }\n    } else if (p <= 0.35) {\n        // Medium forget probability - good redundancy\n        robust_path = base_path;\n        while (robust_path.length() < target_length - 25 &&  // **TUNED** from 20 to 25\n               robust_path.length() + base_path.length() <= MAX_LENGTH - 25) {  // **TUNED**\n            robust_path += base_path;\n        }\n        // Add robust pattern\n        if (robust_path.length() + 25 <= MAX_LENGTH) {  // **INCREASED** from 20 to 25\n            robust_path += \"DRDRDRDRDRDRDRDRDRDRDRDRDRDRDR\";  // **EXTENDED**\n        }\n    } else {\n        // High forget probability - maximum redundancy\n        robust_path = base_path;\n        while (robust_path.length() < target_length - 35 &&  // **TUNED** from 30 to 35\n               robust_path.length() + min(35, (int)base_path.length()) <= MAX_LENGTH - 35) {  // **TUNED**\n            robust_path += base_path.substr(0, min(35, (int)base_path.length()));  // **TUNED**\n        }\n        // Add very robust patterns\n        if (robust_path.length() + 35 <= MAX_LENGTH) {  // **INCREASED** from 30 to 35\n            robust_path += \"DRDRDRDRDRDRDRDRDRDRDRDRDRDRDRDRDRDRDRDRDRDRDRDRDRDRDRDRDRDR\";  // **EXTENDED**\n        }\n    }\n    \n    // Ensure we don't exceed limit\n    if (robust_path.length() > MAX_LENGTH) {\n        robust_path = robust_path.substr(0, MAX_LENGTH);\n    }\n    \n    // Final optimization - if path is too short, add robust patterns\n    if (robust_path.length() < MAX_LENGTH - 25) {  // **TUNED** from 20 to 25\n        string fill_pattern = \"DRURDLULDR\";  // **EXTENDED** pattern\n        while (robust_path.length() + fill_pattern.length() <= MAX_LENGTH) {\n            robust_path += fill_pattern;\n        }\n    }\n    \n    // Last resort safety check\n    if (robust_path.empty() || robust_path.length() > MAX_LENGTH) {\n        robust_path = \"\";\n        // Create a highly robust pattern\n        string core = \"DRURDLULDRURDLUL\";  // **EXTENDED** pattern\n        while (robust_path.length() + core.length() <= MAX_LENGTH) {\n            robust_path += core;\n        }\n        // Fill remaining with simple pattern\n        while (robust_path.length() < MAX_LENGTH) {\n            robust_path += (robust_path.length() % 3 == 0) ? 'D' : (robust_path.length() % 3 == 1) ? 'R' : 'U';  // **IMPROVED** pattern\n        }\n    }\n    \n    // Final truncation\n    if (robust_path.length() > MAX_LENGTH) {\n        robust_path = robust_path.substr(0, MAX_LENGTH);\n    }\n    \n    return robust_path;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    // Read input\n    cin >> si >> sj >> ti >> tj >> p;\n    \n    // Read horizontal walls\n    for (int i = 0; i < SIZE; i++) {\n        string line;\n        cin >> line;\n        for (int j = 0; j < SIZE-1; j++) {\n            h_walls[i][j] = (line[j] == '1');\n        }\n    }\n    \n    // Read vertical walls\n    for (int i = 0; i < SIZE-1; i++) {\n        string line;\n        cin >> line;\n        for (int j = 0; j < SIZE; j++) {\n            v_walls[i][j] = (line[j] == '1');\n        }\n    }\n    \n    // Generate the advanced robust path\n    string result = create_advanced_robust_path();\n    \n    // Final safety check\n    if (result.length() > MAX_LENGTH) {\n        result = result.substr(0, MAX_LENGTH);\n    }\n    \n    if (result.empty()) {\n        // Ultimate fallback\n        for (int i = 0; i < MAX_LENGTH; i++) {\n            result += (i % 5 < 2) ? 'D' : (i % 5 < 4) ? 'R' : 'U';  // **IMPROVED** pattern\n        }\n    }\n    \n    cout << result << endl;\n    \n    return 0;\n}","ahc010":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 30;\n\n// Direction: 0=left, 1=up, 2=right, 3=down\nconst int di[4] = {0, -1, 0, 1};\nconst int dj[4] = {-1, 0, 1, 0};\n\n// to[tile_type][direction_entering] = direction_exiting (-1 if invalid)\nconst int to[8][4] = {\n    {1, 0, -1, -1}, // tile 0\n    {3, -1, -1, 0}, // tile 1\n    {-1, -1, 3, 2}, // tile 2\n    {-1, 2, 1, -1}, // tile 3\n    {1, 0, 3, 2},   // tile 4\n    {3, 2, 1, 0},   // tile 5\n    {2, -1, 0, -1}, // tile 6\n    {-1, 3, -1, 1}  // tile 7\n};\n\nint grid[N][N];\nint rotations[N][N];\n\n// Get effective tile after rotation\nint getEffectiveTile(int baseTile, int rot) {\n    // Tile 0: 0->1->2->3->0\n    // Tile 1: 1->2->3->0->1\n    // Tile 2: 2->3->0->1->2\n    // Tile 3: 3->0->1->2->3\n    // Tile 4: 4->5->4->5->4\n    // Tile 5: 5->4->5->4->5\n    // Tile 6: 6->7->6->7->6\n    // Tile 7: 7->6->7->6->7\n    \n    if (baseTile <= 3) {\n        return (baseTile + rot) % 4;\n    } else if (baseTile == 4) {\n        return (rot % 2 == 0) ? 4 : 5;\n    } else if (baseTile == 5) {\n        return (rot % 2 == 0) ? 5 : 4;\n    } else if (baseTile == 6) {\n        return (rot % 2 == 0) ? 6 : 7;\n    } else { // baseTile == 7\n        return (rot % 2 == 0) ? 7 : 6;\n    }\n}\n\n// Find cycle length starting from (i,j) with entry direction d\nint findCycle(vector<vector<int>>& effGrid, vector<vector<vector<bool>>>& visited, int si, int sj, int sd) {\n    int i = si, j = sj, d = sd;\n    int length = 0;\n    \n    // Check if initial move is valid\n    int d2 = to[effGrid[i][j]][d];\n    if (d2 == -1) return 0;\n    \n    do {\n        // If already visited this (pos,dir) in current trace, not a new cycle\n        if (visited[i][j][d]) return 0;\n        \n        d2 = to[effGrid[i][j]][d];\n        if (d2 == -1) return 0;\n        \n        visited[i][j][d] = true;\n        \n        // Move to next position\n        i += di[d2];\n        j += dj[d2];\n        length++;\n        \n        // Check bounds\n        if (i < 0 || i >= N || j < 0 || j >= N) return 0;\n        \n        // New direction is opposite of exit direction\n        d = (d2 + 2) % 4;\n        \n        // Prevent infinite loops\n        if (length > 10000) return 0;\n        \n    } while (!(i == si && j == sj && d == sd));\n    \n    return length;\n}\n\n// Find all cycles\nvector<int> findCycles() {\n    // Create effective grid\n    vector<vector<int>> effGrid(N, vector<int>(N));\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            effGrid[i][j] = getEffectiveTile(grid[i][j], rotations[i][j]);\n        }\n    }\n    \n    vector<vector<vector<bool>>> visited(N, vector<vector<bool>>(N, vector<bool>(4, false)));\n    vector<int> cycles;\n    \n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            for (int d = 0; d < 4; d++) {\n                if (!visited[i][j][d]) {\n                    int cycleLen = findCycle(effGrid, visited, i, j, d);\n                    if (cycleLen > 0) {\n                        cycles.push_back(cycleLen);\n                    }\n                }\n            }\n        }\n    }\n    \n    sort(cycles.begin(), cycles.end(), greater<int>());\n    return cycles;\n}\n\nlong long getScore() {\n    vector<int> cycles = findCycles();\n    if (cycles.size() < 2) return 0;\n    return 1LL * cycles[0] * cycles[1];\n}\n\nint main() {\n    // Read input\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cin >> grid[i][j];\n            rotations[i][j] = 0;\n        }\n    }\n    \n    // Simple greedy improvement\n    long long bestScore = getScore();\n    \n    // Try local search with small changes\n    for (int iter = 0; iter < 1000 && iter < N * N * 2; iter++) {\n        int i = iter / N;\n        int j = iter % N;\n        \n        if (i >= N) break;\n        \n        int oldRot = rotations[i][j];\n        long long bestLocalScore = bestScore;\n        int bestRot = oldRot;\n        \n        // Try all rotations for this position\n        for (int r = 0; r < 4; r++) {\n            rotations[i][j] = r;\n            long long score = getScore();\n            if (score > bestLocalScore) {\n                bestLocalScore = score;\n                bestRot = r;\n            }\n        }\n        \n        rotations[i][j] = bestRot;\n        if (bestLocalScore > bestScore) {\n            bestScore = bestLocalScore;\n        } else {\n            rotations[i][j] = oldRot;\n        }\n    }\n    \n    // Output\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cout << rotations[i][j];\n        }\n    }\n    cout << endl;\n    \n    return 0;\n}","ahc011":"#include <bits/stdc++.h>\nusing namespace std;\n\n// Direction constants\nconst int UP = 0, DOWN = 1, LEFT = 2, RIGHT = 3;\nconst int dr[4] = {-1, 1, 0, 0};\nconst int dc[4] = {0, 0, -1, 1};\nconst char move_chars[4] = {'U', 'D', 'L', 'R'};\n\n// Check if two adjacent tiles can connect\nbool canConnect(int tile1, int tile2, int direction) {\n    if (direction == 0) return (tile1 & 2) && (tile2 & 8);  // up\n    if (direction == 1) return (tile1 & 8) && (tile2 & 2);  // down\n    if (direction == 2) return (tile1 & 1) && (tile2 & 4);  // left\n    return (tile1 & 4) && (tile2 & 1);                      // right\n}\n\n// Find largest tree and check for complete tree\npair<int, bool> analyzeTree(const vector<vector<int>>& board, int empty_r, int empty_c) {\n    int N = board.size();\n    vector<vector<bool>> visited(N, vector<bool>(N, false));\n    vector<vector<bool>> is_tile(N, vector<bool>(N, true));\n    is_tile[empty_r][empty_c] = false;\n    \n    int max_tree_size = 0;\n    bool has_complete_tree = false;\n    int total_tiles = N * N - 1;\n    \n    // Find connected components\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (!visited[i][j] && is_tile[i][j]) {\n                queue<pair<int,int>> q;\n                vector<pair<int,int>> component;\n                int edge_count = 0;\n                q.push({i, j});\n                visited[i][j] = true;\n                component.push_back({i, j});\n                \n                while (!q.empty()) {\n                    auto [r, c] = q.front(); q.pop();\n                    \n                    for (int d = 0; d < 4; d++) {\n                        int nr = r + dr[d];\n                        int nc = c + dc[d];\n                        \n                        if (nr >= 0 && nr < N && nc >= 0 && nc < N && is_tile[nr][nc]) {\n                            if (canConnect(board[r][c], board[nr][nc], d)) {\n                                edge_count++;\n                                if (!visited[nr][nc]) {\n                                    visited[nr][nc] = true;\n                                    q.push({nr, nc});\n                                    component.push_back({nr, nc});\n                                }\n                            }\n                        }\n                    }\n                }\n                \n                int component_size = component.size();\n                max_tree_size = max(max_tree_size, component_size);\n                \n                // Check if this component is a tree (n vertices, n-1 edges)\n                if (component_size > 0 && edge_count == component_size - 1) {\n                    if (component_size == total_tiles) {\n                        has_complete_tree = true;\n                    }\n                }\n            }\n        }\n    }\n    \n    return {max_tree_size, has_complete_tree};\n}\n\n// Fast evaluation function\nint fastEvaluate(const vector<vector<int>>& board, int empty_r, int empty_c) {\n    auto [max_tree, is_complete] = analyzeTree(board, empty_r, empty_c);\n    \n    if (is_complete) {\n        return 10000000 + max_tree; // Huge bonus for complete tree\n    }\n    \n    // Count direct connections\n    int connections = 0;\n    int N = board.size();\n    vector<vector<bool>> is_tile(N, vector<bool>(N, true));\n    is_tile[empty_r][empty_c] = false;\n    \n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (!is_tile[i][j]) continue;\n            \n            for (int d = 0; d < 4; d++) {\n                int nr = i + dr[d];\n                int nc = j + dc[d];\n                \n                if (nr >= 0 && nr < N && nc >= 0 && nc < N && is_tile[nr][nc]) {\n                    if (canConnect(board[i][j], board[nr][nc], d)) {\n                        connections++;\n                    }\n                }\n            }\n        }\n    }\n    \n    return max_tree * 10000 + connections;\n}\n\n// Main solver - simplified and optimized version that worked well\nstring solve(vector<vector<int>> initial_board, int N, int T) {\n    // Find initial empty position\n    int empty_r = 0, empty_c = 0;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (initial_board[i][j] == 0) {\n                empty_r = i;\n                empty_c = j;\n                break;\n            }\n        }\n    }\n    \n    string best_moves = \"\";\n    int best_score = fastEvaluate(initial_board, empty_r, empty_c);\n    \n    // Simple but effective beam search using tuples\n    const int BEAM_WIDTH = 50;\n    vector<tuple<vector<vector<int>>, int, int, string>> beam;\n    beam.push_back(make_tuple(initial_board, empty_r, empty_c, \"\"));\n    \n    // Beam search iterations - increased iterations for better exploration\n    for (int iter = 0; iter < min(50, T/2) && !beam.empty(); iter++) {\n        vector<pair<int, tuple<vector<vector<int>>, int, int, string>>> candidates;\n        \n        // Expand each state in current beam\n        for (const auto& state : beam) {\n            auto [board, er, ec, moves] = state;\n            \n            // Try all 4 moves\n            for (int d = 0; d < 4; d++) {\n                int nr = er + dr[d];\n                int nc = ec + dc[d];\n                \n                if (nr >= 0 && nr < N && nc >= 0 && nc < N) {\n                    vector<vector<int>> new_board = board;\n                    swap(new_board[er][ec], new_board[nr][nc]);\n                    string new_moves = moves + move_chars[d];\n                    \n                    if (new_moves.length() <= T) {\n                        int score = fastEvaluate(new_board, nr, nc);\n                        candidates.push_back({score, make_tuple(new_board, nr, nc, new_moves)});\n                    }\n                }\n            }\n        }\n        \n        // Sort by score descending\n        sort(candidates.begin(), candidates.end(), [](const auto& a, const auto& b) {\n            return a.first > b.first;\n        });\n        \n        // Update best solution\n        if (!candidates.empty() && candidates[0].first > best_score) {\n            best_score = candidates[0].first;\n            best_moves = get<3>(candidates[0].second);\n        }\n        \n        // Keep top states for next iteration - slightly wider beam\n        beam.clear();\n        for (int i = 0; i < min(BEAM_WIDTH, (int)candidates.size()); i++) {\n            if (get<3>(candidates[i].second).length() <= T - 2) {\n                beam.push_back(candidates[i].second);\n            }\n        }\n    }\n    \n    // Enhanced local improvement phase\n    if (!beam.empty()) {\n        auto [current_board, current_empty_r, current_empty_c, current_moves] = beam[0];\n        \n        // Continue improving with more sophisticated local search\n        mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n        \n        for (int step = current_moves.length(); step < T; step++) {\n            // Try all directions for immediate improvement\n            int best_dir = -1;\n            int best_eval = -1;\n            vector<vector<int>> best_board;\n            int best_nr, best_nc;\n            \n            // Try all directions\n            for (int d = 0; d < 4; d++) {\n                int nr = current_empty_r + dr[d];\n                int nc = current_empty_c + dc[d];\n                \n                if (nr >= 0 && nr < N && nc >= 0 && nc < N) {\n                    vector<vector<int>> new_board = current_board;\n                    swap(new_board[current_empty_r][current_empty_c], new_board[nr][nc]);\n                    int new_score = fastEvaluate(new_board, nr, nc);\n                    \n                    if (new_score > best_eval) {\n                        best_eval = new_score;\n                        best_dir = d;\n                        best_board = new_board;\n                        best_nr = nr;\n                        best_nc = nc;\n                    }\n                }\n            }\n            \n            // If we found an improvement, take it\n            if (best_dir != -1 && best_eval > best_score) {\n                current_board = best_board;\n                current_empty_r = best_nr;\n                current_empty_c = best_nc;\n                current_moves += move_chars[best_dir];\n                best_score = best_eval;\n                best_moves = current_moves;\n            } else if (best_dir != -1) {\n                // Even if no improvement, sometimes accept to avoid local optima\n                // More exploration early, less later\n                int progress = (step * 100) / T;\n                if (progress < 70 || rng() % 10 < 2) {\n                    current_board = best_board;\n                    current_empty_r = best_nr;\n                    current_empty_c = best_nc;\n                    current_moves += move_chars[best_dir];\n                } else {\n                    // Try a random valid move\n                    vector<int> valid_dirs;\n                    for (int d = 0; d < 4; d++) {\n                        int nr = current_empty_r + dr[d];\n                        int nc = current_empty_c + dc[d];\n                        if (nr >= 0 && nr < N && nc >= 0 && nc < N) {\n                            valid_dirs.push_back(d);\n                        }\n                    }\n                    \n                    if (!valid_dirs.empty()) {\n                        int random_dir = valid_dirs[rng() % valid_dirs.size()];\n                        int nr = current_empty_r + dr[random_dir];\n                        int nc = current_empty_c + dc[random_dir];\n                        swap(current_board[current_empty_r][current_empty_c], current_board[nr][nc]);\n                        current_empty_r = nr;\n                        current_empty_c = nc;\n                        current_moves += move_chars[random_dir];\n                    }\n                }\n            } else {\n                break; // No valid moves\n            }\n        }\n    }\n    \n    return best_moves.substr(0, T);\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    int N, T;\n    cin >> N >> T;\n    \n    vector<vector<int>> board(N, vector<int>(N));\n    for (int i = 0; i < N; i++) {\n        string line;\n        cin >> line;\n        for (int j = 0; j < N; j++) {\n            char c = line[j];\n            if (c >= '0' && c <= '9') {\n                board[i][j] = c - '0';\n            } else {\n                board[i][j] = c - 'a' + 10;\n            }\n        }\n    }\n    \n    string result = solve(board, N, T);\n    cout << result << endl;\n    \n    return 0;\n}","ahc012":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst double PI = acos(-1);\nconst long long BOUND = 1000000000LL;\nconst double CAKE_RADIUS = 10000.0;\n\nstruct Strawberry {\n    long long x, y;\n    int id;\n    double angle;\n    double distance;\n};\n\ndouble calculate_angle(long long x, long long y) {\n    double angle = atan2((double)y, (double)x);\n    if (angle < 0) angle += 2 * PI;\n    return angle;\n}\n\npair<pair<long long, long long>, pair<long long, long long>> create_line_from_points(pair<double, double> p1, pair<double, double> p2) {\n    long long x1 = (long long)round(p1.first);\n    long long y1 = (long long)round(p1.second);\n    long long x2 = (long long)round(p2.first);\n    long long y2 = (long long)round(p2.second);\n    \n    // Ensure they're significantly different points\n    if (abs(x1 - x2) < 100 && abs(y1 - y2) < 100) {\n        x2 += 30000;\n        y1 += 30000;\n    }\n    \n    // Bound checking with safety margin\n    x1 = max(-BOUND + 100000LL, min(BOUND - 100000LL, x1));\n    y1 = max(-BOUND + 100000LL, min(BOUND - 100000LL, y1));\n    x2 = max(-BOUND + 100000LL, min(BOUND - 100000LL, x2));\n    y2 = max(-BOUND + 100000LL, min(BOUND - 100000LL, y2));\n    \n    return {{x1, y1}, {x2, y2}};\n}\n\n// Distance between two points\ndouble distance_points(long long x1, long long y1, long long x2, long long y2) {\n    return sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    int N, K;\n    cin >> N >> K;\n    \n    vector<int> a(11); // a[1] to a[10]\n    for (int i = 1; i <= 10; i++) {\n        cin >> a[i];\n    }\n    \n    vector<Strawberry> strawberries(N);\n    for (int i = 0; i < N; i++) {\n        cin >> strawberries[i].x >> strawberries[i].y;\n        strawberries[i].id = i;\n        strawberries[i].angle = calculate_angle(strawberries[i].x, strawberries[i].y);\n        strawberries[i].distance = distance_points(strawberries[i].x, strawberries[i].y, 0, 0);\n    }\n    \n    vector<pair<pair<long long, long long>, pair<long long, long long>>> lines;\n    \n    // Clustering: group nearby strawberries\n    vector<bool> used(N, false);\n    vector<vector<int>> clusters;\n    \n    // Create clusters prioritizing larger sizes first (they're more valuable)\n    for (int cluster_size = 10; cluster_size >= 1 && (int)lines.size() < K; cluster_size--) {\n        // Adjust cluster creation based on available lines and demand\n        int clusters_to_create = min(a[cluster_size], max(1, (K - (int)lines.size()) / max(cluster_size, 1)));\n        clusters_to_create = min(clusters_to_create, 8); // Cap to prevent overfitting\n        \n        for (int c = 0; c < clusters_to_create && (int)clusters.size() < 25; c++) {\n            // Find first unused strawberry\n            int start_idx = -1;\n            for (int i = 0; i < N; i++) {\n                if (!used[i]) {\n                    start_idx = i;\n                    break;\n                }\n            }\n            \n            if (start_idx == -1) break;\n            \n            vector<int> current_cluster;\n            current_cluster.push_back(start_idx);\n            used[start_idx] = true;\n            \n            // Add nearby strawberries to cluster\n            for (int i = 1; i < cluster_size && (int)current_cluster.size() < cluster_size; i++) {\n                int best_idx = -1;\n                double best_dist = 1e18;\n                \n                // Find strawberry closest to current cluster\n                for (int j = 0; j < N; j++) {\n                    if (used[j]) continue;\n                    \n                    // Distance to nearest member of cluster\n                    double min_dist = 1e18;\n                    for (int member : current_cluster) {\n                        double dist = distance_points(\n                            strawberries[member].x, strawberries[member].y,\n                            strawberries[j].x, strawberries[j].y\n                        );\n                        min_dist = min(min_dist, dist);\n                    }\n                    \n                    if (min_dist < best_dist) {\n                        best_dist = min_dist;\n                        best_idx = j;\n                    }\n                }\n                \n                if (best_idx != -1) {\n                    current_cluster.push_back(best_idx);\n                    used[best_idx] = true;\n                } else {\n                    break;\n                }\n            }\n            \n            if (!current_cluster.empty()) {\n                clusters.push_back(current_cluster);\n            }\n        }\n    }\n    \n    // Create separating lines between clusters more efficiently\n    for (int i = 0; i < (int)clusters.size() && (int)lines.size() < K - 2; i++) {\n        for (int j = i + 1; j < (int)clusters.size() && (int)lines.size() < K - 2; j++) {\n            if ((int)lines.size() >= K - 2) break;\n            \n            // Find centroids\n            double cx1 = 0, cy1 = 0;\n            for (int idx : clusters[i]) {\n                cx1 += strawberries[idx].x;\n                cy1 += strawberries[idx].y;\n            }\n            cx1 /= clusters[i].size();\n            cy1 /= clusters[i].size();\n            \n            double cx2 = 0, cy2 = 0;\n            for (int idx : clusters[j]) {\n                cx2 += strawberries[idx].x;\n                cy2 += strawberries[idx].y;\n            }\n            cx2 /= clusters[j].size();\n            cy2 /= clusters[j].size();\n            \n            // If clusters are reasonably separated, create separating line\n            double dist = distance_points((long long)cx1, (long long)cy1, (long long)cx2, (long long)cy2);\n            if (dist > 350) { // Lower threshold for better separation\n                // Midpoint between centroids\n                double mid_x = (cx1 + cx2) / 2.0;\n                double mid_y = (cy1 + cy2) / 2.0;\n                \n                // Perpendicular to line connecting centroids\n                double dx = cx2 - cx1;\n                double dy = cy2 - cy1;\n                double angle = atan2(dy, dx) + PI/2;\n                \n                // Create separating line with good length\n                double len = 380000;\n                pair<double, double> p1 = {mid_x + cos(angle) * len, mid_y + sin(angle) * len};\n                pair<double, double> p2 = {mid_x - cos(angle) * len, mid_y - sin(angle) * len};\n                \n                lines.push_back(create_line_from_points(p1, p2));\n            }\n        }\n    }\n    \n    // Add strategic radial lines for additional separation\n    if ((int)lines.size() < K) {\n        int current_count = (int)lines.size();\n        int to_add = min(K - current_count, 12);\n        for (int i = 0; i < to_add && (int)lines.size() < K; i++) {\n            double angle = 2.0 * PI * i / max(to_add, 1) + PI/24; // Small offset for better distribution\n            double dist = CAKE_RADIUS * 3.0;\n            // Make sure points are well outside the cake\n            pair<double, double> p1 = {cos(angle) * dist, sin(angle) * dist};\n            pair<double, double> p2 = {cos(angle + 0.2) * dist, sin(angle + 0.2) * dist};\n            lines.push_back(create_line_from_points(p1, p2));\n        }\n    }\n    \n    // Ensure we don't exceed K lines\n    while ((int)lines.size() > K) {\n        lines.pop_back();\n    }\n    \n    // Output result\n    cout << lines.size() << \"\\n\";\n    for (const auto& line : lines) {\n        cout << line.first.first << \" \" << line.first.second << \" \" \n             << line.second.first << \" \" << line.second.second << \"\\n\";\n    }\n    \n    return 0;\n}","ahc014":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MAX_N = 65;\n\nint N, M;\nbool grid[MAX_N][MAX_N];\nvector<tuple<int, int, int, int, int, int, int, int>> operations;\n\n// Center of the grid\nint center;\n\n// Calculate weight of a position\ninline int weight(int x, int y) {\n    int dx = x - center;\n    int dy = y - center;\n    return dx * dx + dy * dy + 1;\n}\n\n// Check if position is valid (within bounds)\ninline bool valid_pos(int x, int y) {\n    return x >= 0 && x < N && y >= 0 && y < N;\n}\n\n// Try to find a valid rectangle by checking existing dots\nbool find_rectangle(int new_x, int new_y, int &x2, int &y2, int &x3, int &y3, int &x4, int &y4) {\n    // Collect existing dots\n    vector<pair<int, int>> dots;\n    for (int x = 0; x < N; x++) {\n        for (int y = 0; y < N; y++) {\n            if (grid[x][y]) {\n                dots.push_back({x, y});\n            }\n        }\n    }\n    \n    if (dots.size() < 3) return false;\n    \n    // Try all combinations of 3 existing dots\n    for (size_t i = 0; i < dots.size() && i < 50; i++) { // Limit for performance\n        for (size_t j = i + 1; j < dots.size() && j < 50; j++) {\n            for (size_t k = j + 1; k < dots.size() && k < 50; k++) {\n                int a_x = dots[i].first, a_y = dots[i].second;\n                int b_x = dots[j].first, b_y = dots[j].second;\n                int c_x = dots[k].first, c_y = dots[k].second;\n                \n                // Try different orderings to form a rectangle\n                // Check if new_x,new_y,a,b,c forms a valid rectangle in some order\n                \n                // Try order: new-a-b-c\n                int dx1 = a_x - new_x, dy1 = a_y - new_y;\n                int dx2 = b_x - a_x, dy2 = b_y - a_y;\n                int dx3 = c_x - b_x, dy3 = c_y - b_y;\n                int dx4 = new_x - c_x, dy4 = new_y - c_y;\n                \n                // Check if it forms a rectangle (opposite sides equal and parallel)\n                if (dx1 == -dx3 && dy1 == -dy3 && dx2 == -dx4 && dy2 == -dy4) {\n                    // Check if adjacent sides are perpendicular\n                    if (dx1 * dx2 + dy1 * dy2 == 0) {\n                        x2 = a_x; y2 = a_y; x3 = b_x; y3 = b_y; x4 = c_x; y4 = c_y;\n                        return true;\n                    }\n                }\n                \n                // Try order: new-a-c-b\n                dx1 = a_x - new_x, dy1 = a_y - new_y;\n                dx2 = c_x - a_x, dy2 = c_y - a_y;\n                dx3 = b_x - c_x, dy3 = b_y - c_y;\n                dx4 = new_x - b_x, dy4 = new_y - b_y;\n                \n                if (dx1 == -dx3 && dy1 == -dy3 && dx2 == -dx4 && dy2 == -dy4) {\n                    if (dx1 * dx2 + dy1 * dy2 == 0) {\n                        x2 = a_x; y2 = a_y; x3 = c_x; y3 = c_y; x4 = b_x; y4 = b_y;\n                        return true;\n                    }\n                }\n            }\n        }\n    }\n    \n    // Try diamond shapes\n    for (int d = 1; d <= min(N/2, 8); d++) {\n        vector<pair<int, int>> corners = {\n            {new_x, new_y + d}, {new_x + d, new_y}, \n            {new_x, new_y - d}, {new_x - d, new_y}\n        };\n        \n        int dot_count = 0;\n        int dot_indices[3];\n        int idx = 0;\n        \n        for (int i = 0; i < 4; i++) {\n            int cx = corners[i].first;\n            int cy = corners[i].second;\n            if (valid_pos(cx, cy) && grid[cx][cy]) {\n                if (idx < 3) {\n                    dot_indices[idx] = i;\n                    idx++;\n                }\n                dot_count++;\n            }\n        }\n        \n        if (dot_count == 3) {\n            x2 = corners[dot_indices[0]].first; y2 = corners[dot_indices[0]].second;\n            x3 = corners[dot_indices[1]].first; y3 = corners[dot_indices[1]].second;\n            x4 = corners[dot_indices[2]].first; y4 = corners[dot_indices[2]].second;\n            return true;\n        }\n    }\n    \n    return false;\n}\n\nvoid solve() {\n    // Create list of empty positions sorted by weight (descending)\n    vector<tuple<int, int, int>> candidates;\n    \n    for (int x = 1; x < N-1; x++) {\n        for (int y = 1; y < N-1; y++) {\n            if (!grid[x][y]) { // Only consider positions without dots\n                candidates.push_back({weight(x, y), x, y});\n            }\n        }\n    }\n    \n    sort(candidates.rbegin(), candidates.rend());\n    \n    // Try to place dots\n    const int MAX_ATTEMPTS = min(200, (int)candidates.size());\n    \n    for (int i = 0; i < MAX_ATTEMPTS; i++) {\n        int x = get<1>(candidates[i]);\n        int y = get<2>(candidates[i]);\n        \n        // Double-check that position doesn't have a dot\n        if (grid[x][y]) continue;\n        \n        int x2, y2, x3, y3, x4, y4;\n        \n        // Try to find a valid rectangle\n        if (find_rectangle(x, y, x2, y2, x3, y3, x4, y4)) {\n            operations.push_back(make_tuple(x, y, x2, y2, x3, y3, x4, y4));\n            grid[x][y] = true;\n        }\n    }\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    cin >> N >> M;\n    center = (N - 1) / 2;\n    \n    // Initialize grid\n    memset(grid, false, sizeof(grid));\n    \n    // Read initial dots\n    for (int i = 0; i < M; i++) {\n        int x, y;\n        cin >> x >> y;\n        if (valid_pos(x, y)) {\n            grid[x][y] = true;\n        }\n    }\n    \n    // Solve\n    solve();\n    \n    // Output results\n    cout << operations.size() << \"\\n\";\n    for (const auto& op : operations) {\n        cout << get<0>(op) << \" \" << get<1>(op) << \" \" \n             << get<2>(op) << \" \" << get<3>(op) << \" \"\n             << get<4>(op) << \" \" << get<5>(op) << \" \"\n             << get<6>(op) << \" \" << get<7>(op) << \"\\n\";\n    }\n    \n    return 0;\n}","ahc015":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int GRID_SIZE = 10;\nconst int TOTAL_CANDIES = 100;\nconst int LOOKAHEAD = 3; // Look ahead this many moves\n\n// Directions: F(forward/up), B(backward/down), L(left), R(right)\nconst char DIR_CHARS[] = {'F', 'B', 'L', 'R'};\nconst int DX[] = {-1, 1, 0, 0}; // F, B, L, R\nconst int DY[] = {0, 0, -1, 1};\n\nstruct Position {\n    int x, y;\n    Position() : x(0), y(0) {}\n    Position(int _x, int _y) : x(_x), y(_y) {}\n};\n\nclass CandyBox {\npublic:\n    int grid[GRID_SIZE][GRID_SIZE]; // 0 = empty, 1,2,3 = flavors\n    vector<int> flavor_sequence;\n    int candy_count[4]; // index 0 unused, 1-3 for flavors\n    \n    CandyBox() {\n        memset(grid, 0, sizeof(grid));\n        memset(candy_count, 0, sizeof(candy_count));\n    }\n    \n    void read_flavors() {\n        flavor_sequence.resize(TOTAL_CANDIES);\n        for (int i = 0; i < TOTAL_CANDIES; i++) {\n            cin >> flavor_sequence[i];\n        }\n    }\n    \n    // Convert empty cell index to position\n    Position index_to_position(int index) {\n        int count = 0;\n        for (int i = 0; i < GRID_SIZE; i++) {\n            for (int j = 0; j < GRID_SIZE; j++) {\n                if (grid[i][j] == 0) {\n                    count++;\n                    if (count == index) {\n                        return Position(i, j);\n                    }\n                }\n            }\n        }\n        return Position(-1, -1); // Error\n    }\n    \n    // Place candy at specified position\n    void place_candy(int x, int y, int flavor) {\n        grid[x][y] = flavor;\n        candy_count[flavor]++;\n    }\n    \n    // Simulate sliding in given direction\n    void slide_candies(int dir) {\n        if (dir == 0) { // Forward (up) - move rows up\n            for (int col = 0; col < GRID_SIZE; col++) {\n                int write_pos = 0;\n                for (int row = 0; row < GRID_SIZE; row++) {\n                    if (grid[row][col] != 0) {\n                        if (write_pos != row) {\n                            grid[write_pos][col] = grid[row][col];\n                            grid[row][col] = 0;\n                        }\n                        write_pos++;\n                    }\n                }\n            }\n        } else if (dir == 1) { // Backward (down) - move rows down\n            for (int col = 0; col < GRID_SIZE; col++) {\n                int write_pos = GRID_SIZE - 1;\n                for (int row = GRID_SIZE - 1; row >= 0; row--) {\n                    if (grid[row][col] != 0) {\n                        if (write_pos != row) {\n                            grid[write_pos][col] = grid[row][col];\n                            grid[row][col] = 0;\n                        }\n                        write_pos--;\n                    }\n                }\n            }\n        } else if (dir == 2) { // Left - move columns left\n            for (int row = 0; row < GRID_SIZE; row++) {\n                int write_pos = 0;\n                for (int col = 0; col < GRID_SIZE; col++) {\n                    if (grid[row][col] != 0) {\n                        if (write_pos != col) {\n                            grid[row][write_pos] = grid[row][col];\n                            grid[row][col] = 0;\n                        }\n                        write_pos++;\n                    }\n                }\n            }\n        } else if (dir == 3) { // Right - move columns right\n            for (int row = 0; row < GRID_SIZE; row++) {\n                int write_pos = GRID_SIZE - 1;\n                for (int col = GRID_SIZE - 1; col >= 0; col--) {\n                    if (grid[row][col] != 0) {\n                        if (write_pos != col) {\n                            grid[row][write_pos] = grid[row][col];\n                            grid[row][col] = 0;\n                        }\n                        write_pos--;\n                    }\n                }\n            }\n        }\n    }\n    \n    // Create a copy of current state\n    CandyBox copy() const {\n        CandyBox other;\n        for (int i = 0; i < GRID_SIZE; i++) {\n            for (int j = 0; j < GRID_SIZE; j++) {\n                other.grid[i][j] = grid[i][j];\n            }\n        }\n        for (int i = 0; i < 4; i++) {\n            other.candy_count[i] = candy_count[i];\n        }\n        other.flavor_sequence = flavor_sequence;\n        return other;\n    }\n    \n    // Calculate connected components score with additional metrics\n    pair<int, int> calculate_connectedness_detailed() {\n        bool visited[GRID_SIZE][GRID_SIZE];\n        memset(visited, false, sizeof(visited));\n        \n        int total_score = 0;\n        int max_component = 0;\n        int dx[] = {0, 0, -1, 1};\n        int dy[] = {-1, 1, 0, 0};\n        \n        for (int i = 0; i < GRID_SIZE; i++) {\n            for (int j = 0; j < GRID_SIZE; j++) {\n                if (grid[i][j] != 0 && !visited[i][j]) {\n                    // BFS to find connected component\n                    queue<Position> q;\n                    vector<Position> component;\n                    q.push(Position(i, j));\n                    visited[i][j] = true;\n                    component.push_back(Position(i, j));\n                    \n                    int flavor = grid[i][j];\n                    \n                    while (!q.empty()) {\n                        Position current = q.front();\n                        q.pop();\n                        \n                        for (int d = 0; d < 4; d++) {\n                            int nx = current.x + dx[d];\n                            int ny = current.y + dy[d];\n                            \n                            if (nx >= 0 && nx < GRID_SIZE && ny >= 0 && ny < GRID_SIZE) {\n                                if (!visited[nx][ny] && grid[nx][ny] == flavor) {\n                                    visited[nx][ny] = true;\n                                    q.push(Position(nx, ny));\n                                    component.push_back(Position(nx, ny));\n                                }\n                            }\n                        }\n                    }\n                    \n                    // Add square of component size to score\n                    int component_size = component.size();\n                    total_score += component_size * component_size;\n                    max_component = max(max_component, component_size);\n                }\n            }\n        }\n        \n        return {total_score, max_component};\n    }\n    \n    int calculate_connectedness() {\n        return calculate_connectedness_detailed().first;\n    }\n    \n    // Count empty cells\n    int count_empty() const {\n        int count = 0;\n        for (int i = 0; i < GRID_SIZE; i++) {\n            for (int j = 0; j < GRID_SIZE; j++) {\n                if (grid[i][j] == 0) count++;\n            }\n        }\n        return count;\n    }\n    \n    // Evaluate clustering potential for a specific flavor\n    int evaluate_flavor_clustering(int flavor) {\n        bool visited[GRID_SIZE][GRID_SIZE];\n        memset(visited, false, sizeof(visited));\n        \n        int total_score = 0;\n        int dx[] = {0, 0, -1, 1};\n        int dy[] = {-1, 1, 0, 0};\n        \n        for (int i = 0; i < GRID_SIZE; i++) {\n            for (int j = 0; j < GRID_SIZE; j++) {\n                if (grid[i][j] == flavor && !visited[i][j]) {\n                    // BFS to find connected component\n                    queue<Position> q;\n                    vector<Position> component;\n                    q.push(Position(i, j));\n                    visited[i][j] = true;\n                    component.push_back(Position(i, j));\n                    \n                    while (!q.empty()) {\n                        Position current = q.front();\n                        q.pop();\n                        \n                        for (int d = 0; d < 4; d++) {\n                            int nx = current.x + dx[d];\n                            int ny = current.y + dy[d];\n                            \n                            if (nx >= 0 && nx < GRID_SIZE && ny >= 0 && ny < GRID_SIZE) {\n                                if (!visited[nx][ny] && grid[nx][ny] == flavor) {\n                                    visited[nx][ny] = true;\n                                    q.push(Position(nx, ny));\n                                    component.push_back(Position(nx, ny));\n                                }\n                            }\n                        }\n                    }\n                    \n                    // Prefer larger components\n                    total_score += component.size() * component.size();\n                }\n            }\n        }\n        \n        return total_score;\n    }\n    \n    // Advanced evaluation considering multiple factors\n    double evaluate_move_advanced(int dir, int current_turn) {\n        // Create copy and simulate\n        CandyBox temp = this->copy();\n        temp.slide_candies(dir);\n        \n        auto [connected_score, max_component] = temp.calculate_connectedness_detailed();\n        \n        // Weight factors\n        double score = connected_score;\n        \n        // Bonus for larger maximum component\n        score += max_component * 10;\n        \n        // Evaluate clustering potential for each flavor\n        for (int flavor = 1; flavor <= 3; flavor++) {\n            if (temp.candy_count[flavor] > 0) {\n                score += temp.evaluate_flavor_clustering(flavor) * 0.5;\n            }\n        }\n        \n        // Prefer moves that don't isolate single candies\n        int isolated_candies = 0;\n        int dx[] = {0, 0, -1, 1};\n        int dy[] = {-1, 1, 0, 0};\n        for (int i = 0; i < GRID_SIZE; i++) {\n            for (int j = 0; j < GRID_SIZE; j++) {\n                if (temp.grid[i][j] != 0) {\n                    bool has_neighbor = false;\n                    int flavor = temp.grid[i][j];\n                    for (int d = 0; d < 4; d++) {\n                        int nx = i + dx[d];\n                        int ny = j + dy[d];\n                        if (nx >= 0 && nx < GRID_SIZE && ny >= 0 && ny < GRID_SIZE) {\n                            if (temp.grid[nx][ny] == flavor) {\n                                has_neighbor = true;\n                                break;\n                            }\n                        }\n                    }\n                    if (!has_neighbor) isolated_candies++;\n                }\n            }\n        }\n        score -= isolated_candies * 20; // Penalty for isolated candies\n        \n        return score;\n    }\n    \n    // Simulate lookahead\n    double simulate_lookahead(int dir, int current_turn) {\n        CandyBox temp = this->copy();\n        temp.slide_candies(dir);\n        \n        double total_score = temp.evaluate_move_advanced(dir, current_turn);\n        \n        // Simple lookahead - simulate next few placements randomly\n        int lookahead_depth = min(LOOKAHEAD, TOTAL_CANDIES - current_turn - 1);\n        CandyBox lookahead_state = temp.copy();\n        \n        for (int i = 0; i < lookahead_depth; i++) {\n            int future_turn = current_turn + 1 + i;\n            if (future_turn >= TOTAL_CANDIES) break;\n            \n            // Simulate a random placement (we don't know exact position, but we know flavor)\n            int flavor = lookahead_state.flavor_sequence[future_turn];\n            \n            // Find a reasonable position - prefer corners/edges for isolation control\n            vector<Position> empty_positions;\n            for (int x = 0; x < GRID_SIZE; x++) {\n                for (int y = 0; y < GRID_SIZE; y++) {\n                    if (lookahead_state.grid[x][y] == 0) {\n                        empty_positions.push_back(Position(x, y));\n                    }\n                }\n            }\n            \n            if (!empty_positions.empty()) {\n                // Place in a position that might help clustering\n                lookahead_state.place_candy(empty_positions[0].x, empty_positions[0].y, flavor);\n            }\n            \n            // Apply a reasonable tilt (prefer clustering directions)\n            int best_dir = 0;\n            double best_score = -1e9;\n            for (int d = 0; d < 4; d++) {\n                double score = lookahead_state.evaluate_move_advanced(d, future_turn);\n                if (score > best_score) {\n                    best_score = score;\n                    best_dir = d;\n                }\n            }\n            lookahead_state.slide_candies(best_dir);\n        }\n        \n        total_score += lookahead_state.calculate_connectedness() * 0.3;\n        return total_score;\n    }\n};\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    CandyBox box;\n    box.read_flavors();\n    \n    for (int turn = 0; turn < TOTAL_CANDIES; turn++) {\n        int p;\n        cin >> p;\n        \n        // Place the candy\n        Position pos = box.index_to_position(p);\n        box.place_candy(pos.x, pos.y, box.flavor_sequence[turn]);\n        \n        // Don't need to output direction for last candy\n        if (turn == TOTAL_CANDIES - 1) {\n            cout << \"F\" << endl;\n            cout.flush();\n            break;\n        }\n        \n        // Evaluate all four directions and choose best\n        double best_score = -1e18;\n        int best_direction = 0;\n        \n        for (int dir = 0; dir < 4; dir++) {\n            double score = box.simulate_lookahead(dir, turn);\n            if (score > best_score) {\n                best_score = score;\n                best_direction = dir;\n            }\n        }\n        \n        // Output the chosen direction\n        cout << DIR_CHARS[best_direction] << endl;\n        cout.flush();\n        \n        // Apply the chosen move\n        box.slide_candies(best_direction);\n    }\n    \n    return 0;\n}","ahc016":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int QUERY_COUNT = 100;\n\nint M;\ndouble epsilon;\nint N;\nvector<string> graphs;\n\n// Fine-tuned graph generation\nvoid generateGraphs() {\n    // Slightly optimized parameters\n    if (M <= 15) N = 10;\n    else if (M <= 30) N = 14;\n    else if (M <= 50) N = 18;\n    else if (M <= 70) N = 22;\n    else N = 26;\n    \n    // Error rate adjustments that work well\n    if (epsilon > 0.3) N = min(32, N + 5);\n    else if (epsilon > 0.2) N = min(28, N + 2);\n    \n    N = min(N, 32); // Conservative but effective\n    \n    graphs.clear();\n    \n    // Generation that achieved high score\n    for (int k = 0; k < M; k++) {\n        string graph_str(N * (N - 1) / 2, '0');\n        \n        // Optimized edge distribution\n        int min_edges = max(1, N/2);\n        int max_edges = min(N * (N - 1) / 3, N * (N - 1) / 2 - 1);\n        int target_edges = min_edges + (k * (max_edges - min_edges)) / max(1, M - 1);\n        \n        // Effective deterministic placement\n        for (int edge = 0; edge < target_edges; edge++) {\n            // Hash function that provides good distribution\n            int hash = (edge * 19 + k * 37) % (N * (N - 1) / 2);\n            if (hash < (int)graph_str.size()) {\n                graph_str[hash] = '1';\n            }\n        }\n        \n        graphs.push_back(graph_str);\n    }\n}\n\n// Simple and effective decoding\nint decodeGraph(const string& received) {\n    int recv_edges = count(received.begin(), received.end(), '1');\n    \n    // Edge count matching approach that works\n    int best_diff = 1000000;\n    int best_idx = 0;\n    \n    for (int i = 0; i < M; i++) {\n        int orig_edges = count(graphs[i].begin(), graphs[i].end(), '1');\n        int diff = abs(orig_edges - recv_edges);\n        \n        if (diff < best_diff) {\n            best_diff = diff;\n            best_idx = i;\n        }\n    }\n    \n    return best_idx;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    cin >> M >> epsilon;\n    \n    generateGraphs();\n    \n    cout << N << endl;\n    \n    for (const string& graph : graphs) {\n        cout << graph << endl;\n    }\n    cout.flush();\n    \n    for (int q = 0; q < QUERY_COUNT; q++) {\n        string received;\n        cin >> received;\n        \n        int prediction = decodeGraph(received);\n        cout << prediction << endl;\n        cout.flush();\n    }\n    \n    return 0;\n}","ahc017":"#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing P = pair<int, int>;\nusing PL = pair<ll, int>;\n\nconst ll INF = 1e15;\nconst int MAXN = 1005;\nconst int MAXM = 3005;\n\nint N, M, D, K;\nvector<tuple<int, int, int>> edges;\nvector<pair<int, int>> coords;\nvector<vector<P>> adj;\n\n// Find bridges using Tarjan's algorithm\nvector<bool> find_bridges() {\n    vector<bool> is_bridge(M, false);\n    vector<int> disc(N, -1), low(N, -1);\n    vector<bool> visited(N, false);\n    int time = 0;\n    \n    function<void(int, int, int)> dfs = [&](int u, int parent, int edge_idx) {\n        visited[u] = true;\n        disc[u] = low[u] = time++;\n        \n        for (int i = 0; i < adj[u].size(); i++) {\n            auto [v, w] = adj[u][i];\n            int current_edge_idx = -1;\n            \n            // Find the edge index\n            for (int j = 0; j < M; j++) {\n                auto [eu, ev, ew] = edges[j];\n                if ((eu-1 == u && ev-1 == v) || (eu-1 == v && ev-1 == u)) {\n                    current_edge_idx = j;\n                    break;\n                }\n            }\n            \n            if (!visited[v]) {\n                dfs(v, u, current_edge_idx);\n                low[u] = min(low[u], low[v]);\n                \n                if (low[v] > disc[u]) {\n                    if (current_edge_idx != -1) {\n                        is_bridge[current_edge_idx] = true;\n                    }\n                }\n            } else if (v != parent) {\n                low[u] = min(low[u], disc[v]);\n            }\n        }\n    };\n    \n    for (int i = 0; i < N; i++) {\n        if (!visited[i]) {\n            dfs(i, -1, -1);\n        }\n    }\n    \n    return is_bridge;\n}\n\n// Calculate multiple importance metrics\ntuple<vector<ll>, vector<ll>, vector<ll>> calculate_importance_metrics() {\n    vector<ll> degree_importance(M, 0);\n    vector<ll> weight_importance(M, 0);\n    vector<ll> spatial_importance(M, 0);\n    \n    // Degree-based importance\n    vector<int> degree(N, 0);\n    for (int i = 0; i < M; i++) {\n        auto [u, v, w] = edges[i];\n        degree[u-1]++;\n        degree[v-1]++;\n    }\n    \n    for (int i = 0; i < M; i++) {\n        auto [u, v, w] = edges[i];\n        // Edges connecting high-degree vertices are more important\n        degree_importance[i] = (ll)(degree[u-1] + degree[v-1]) * 1000000LL;\n        // Lower weight edges are more critical for shortest paths\n        weight_importance[i] = 1000000000LL / (w + 1);\n    }\n    \n    // Spatial importance - edges closer to graph center\n    ll sum_x = 0, sum_y = 0;\n    for (int i = 0; i < N; i++) {\n        sum_x += coords[i].first;\n        sum_y += coords[i].second;\n    }\n    double center_x = (double)sum_x / N;\n    double center_y = (double)sum_y / N;\n    \n    for (int i = 0; i < M; i++) {\n        auto [u, v, w] = edges[i];\n        auto [x1, y1] = coords[u-1];\n        auto [x2, y2] = coords[v-1];\n        \n        // Midpoint of edge\n        double mid_x = (x1 + x2) / 2.0;\n        double mid_y = (y1 + y2) / 2.0;\n        \n        // Distance from center\n        double dist_from_center = sqrt((mid_x - center_x) * (mid_x - center_x) + \n                                      (mid_y - center_y) * (mid_y - center_y));\n        \n        // Closer to center = more important\n        spatial_importance[i] = (ll)(10000.0 / (dist_from_center + 1.0)) * 100000LL;\n    }\n    \n    return {degree_importance, weight_importance, spatial_importance};\n}\n\n// Main solving function\nvector<int> solve() {\n    // Build adjacency list\n    adj.assign(N, vector<P>());\n    for (int i = 0; i < M; i++) {\n        auto [u, v, w] = edges[i];\n        adj[u-1].push_back({v-1, w});\n        adj[v-1].push_back({u-1, w});\n    }\n    \n    // Find bridges (critical edges)\n    auto is_bridge = find_bridges();\n    \n    // Calculate importance metrics\n    auto [degree_imp, weight_imp, spatial_imp] = calculate_importance_metrics();\n    \n    // Combine importance scores\n    vector<pair<ll, int>> combined_importance(M);\n    for (int i = 0; i < M; i++) {\n        ll bridge_bonus = is_bridge[i] ? 10000000000LL : 0;\n        ll total_importance = degree_imp[i] + weight_imp[i] + spatial_imp[i] + bridge_bonus;\n        combined_importance[i] = {total_importance, i};\n    }\n    \n    sort(combined_importance.rbegin(), combined_importance.rend());\n    \n    // Assign edges to days with better strategy\n    vector<int> result(M, 1);\n    vector<int> day_count(D + 1, 0);\n    \n    // First, assign bridges and very high importance edges to separate days\n    int critical_count = 0;\n    for (int i = 0; i < min(M, D * 3) && critical_count < D; i++) {\n        int edge_idx = combined_importance[i].second;\n        if (is_bridge[edge_idx] || combined_importance[i].first > 5000000000LL) {\n            // Assign to next available day\n            if (critical_count + 1 <= D && day_count[critical_count + 1] < K) {\n                result[edge_idx] = critical_count + 1;\n                day_count[critical_count + 1]++;\n                critical_count++;\n            }\n        }\n    }\n    \n    // Then assign remaining high importance edges\n    for (int i = 0; i < min(M, D * 5); i++) {\n        int edge_idx = combined_importance[i].second;\n        if (result[edge_idx] != 1) continue; // Already assigned\n        \n        // Find day with minimum load\n        int best_day = 1;\n        for (int day = 2; day <= D; day++) {\n            if (day_count[day] < day_count[best_day]) {\n                best_day = day;\n            }\n        }\n        \n        if (day_count[best_day] < K) {\n            result[edge_idx] = best_day;\n            day_count[best_day]++;\n        }\n    }\n    \n    // Assign remaining edges\n    for (int i = 0; i < M; i++) {\n        if (result[i] != 1) continue; // Already assigned\n        \n        // Find day with minimum load\n        int best_day = 1;\n        for (int day = 2; day <= D; day++) {\n            if (day_count[day] < day_count[best_day]) {\n                best_day = day;\n            }\n        }\n        \n        if (day_count[best_day] < K) {\n            result[i] = best_day;\n            day_count[best_day]++;\n        } else {\n            // Find any day with space\n            for (int day = 1; day <= D; day++) {\n                if (day_count[day] < K) {\n                    result[i] = day;\n                    day_count[day]++;\n                    break;\n                }\n            }\n        }\n    }\n    \n    // Lightweight local search\n    auto start_time = chrono::steady_clock::now();\n    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n    \n    uniform_int_distribution<int> edge_dist(0, M-1);\n    uniform_int_distribution<int> day_dist(1, D);\n    \n    // Very limited and fast local search\n    for (int iter = 0; iter < 5000; iter++) {\n        auto current_time = chrono::steady_clock::now();\n        if (chrono::duration_cast<chrono::milliseconds>(current_time - start_time).count() > 5500) {\n            break;\n        }\n        \n        int edge_idx = edge_dist(rng);\n        int current_day = result[edge_idx];\n        int new_day = day_dist(rng);\n        \n        if (new_day != current_day) {\n            // Quick constraint check\n            vector<int> day_load(D + 1, 0);\n            for (int i = 0; i < M; i++) {\n                day_load[result[i]]++;\n            }\n            \n            if (day_load[new_day] < K) {\n                // Simple swap acceptance - accept if it spreads out high importance edges\n                ll old_score = 0, new_score = 0;\n                \n                // Very rough approximation - just check if we're balancing important edges\n                result[edge_idx] = new_day;\n                // Accept most moves in the time limit since exact evaluation is too expensive\n            }\n        }\n    }\n    \n    return result;\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    cin >> N >> M >> D >> K;\n    edges.resize(M);\n    coords.resize(N);\n    \n    for (int i = 0; i < M; i++) {\n        int u, v, w;\n        cin >> u >> v >> w;\n        edges[i] = {u, v, w};\n    }\n    \n    for (int i = 0; i < N; i++) {\n        cin >> coords[i].first >> coords[i].second;\n    }\n    \n    auto result = solve();\n    \n    for (int i = 0; i < M; i++) {\n        cout << result[i];\n        if (i < M - 1) cout << \" \";\n    }\n    cout << \"\\n\";\n    \n    return 0;\n}","ahc019":"#include <bits/stdc++.h>\nusing namespace std;\n\n// Generate some simple connected blocks\nvector<vector<pair<int,int>>> generate2DBlocks() {\n    vector<vector<pair<int,int>>> blocks;\n    \n    // Single cube\n    blocks.push_back({{0, 0}});\n    \n    // 1x2 lines\n    blocks.push_back({{0, 0}, {1, 0}});\n    blocks.push_back({{0, 0}, {0, 1}});\n    \n    // 1x3 lines\n    blocks.push_back({{0, 0}, {1, 0}, {2, 0}});\n    blocks.push_back({{0, 0}, {0, 1}, {0, 2}});\n    \n    // 2x2 square\n    blocks.push_back({{0, 0}, {1, 0}, {0, 1}, {1, 1}});\n    \n    // L-shape\n    blocks.push_back({{0, 0}, {1, 0}, {0, 1}});\n    \n    return blocks;\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int D;\n    cin >> D;\n    \n    vector<vector<int>> f1(D, vector<int>(D));\n    vector<vector<int>> r1(D, vector<int>(D));\n    vector<vector<int>> f2(D, vector<int>(D));\n    vector<vector<int>> r2(D, vector<int>(D));\n    \n    // Read input\n    for (int i = 0; i < D; i++) {\n        string line; cin >> line;\n        for (int j = 0; j < D; j++) f1[i][j] = line[j] - '0';\n    }\n    for (int i = 0; i < D; i++) {\n        string line; cin >> line;\n        for (int j = 0; j < D; j++) r1[i][j] = line[j] - '0';\n    }\n    for (int i = 0; i < D; i++) {\n        string line; cin >> line;\n        for (int j = 0; j < D; j++) f2[i][j] = line[j] - '0';\n    }\n    for (int i = 0; i < D; i++) {\n        string line; cin >> line;\n        for (int j = 0; j < D; j++) r2[i][j] = line[j] - '0';\n    }\n    \n    // Create solution arrays\n    vector<vector<vector<int>>> sol1(D, vector<vector<int>>(D, vector<int>(D, 0)));\n    vector<vector<vector<int>>> sol2(D, vector<vector<int>>(D, vector<int>(D, 0)));\n    \n    // Identify required positions for each solution\n    vector<tuple<int,int,int>> req1, req2;\n    \n    for (int z = 0; z < D; z++) {\n        for (int x = 0; x < D; x++) {\n            if (f1[z][x] == 1) {\n                for (int y = 0; y < D; y++) {\n                    if (r1[z][y] == 1) {\n                        req1.push_back({z, x, y});\n                    }\n                }\n            }\n        }\n    }\n    \n    for (int z = 0; z < D; z++) {\n        for (int x = 0; x < D; x++) {\n            if (f2[z][x] == 1) {\n                for (int y = 0; y < D; y++) {\n                    if (r2[z][y] == 1) {\n                        req2.push_back({z, x, y});\n                    }\n                }\n            }\n        }\n    }\n    \n    // Better approach: share blocks between solutions where positions match exactly\n    int block_id = 1;\n    map<tuple<int,int,int>, int> position_to_block;\n    \n    // First, find positions that are required in both solutions\n    set<tuple<int,int,int>> set1(req1.begin(), req1.end());\n    set<tuple<int,int,int>> set2(req2.begin(), req2.end());\n    \n    // Assign blocks to positions that are in both solutions\n    for (const auto& pos : set1) {\n        if (set2.count(pos)) {\n            position_to_block[pos] = block_id++;\n        }\n    }\n    \n    // Assign remaining positions in solution 1\n    for (const auto& pos : req1) {\n        if (position_to_block.find(pos) == position_to_block.end()) {\n            position_to_block[pos] = block_id++;\n        }\n    }\n    \n    // Assign remaining positions in solution 2\n    for (const auto& pos : req2) {\n        if (position_to_block.find(pos) == position_to_block.end()) {\n            position_to_block[pos] = block_id++;\n        }\n    }\n    \n    int total_blocks = block_id - 1;\n    \n    // Fill solution arrays\n    for (const auto& pos : req1) {\n        auto [z, x, y] = pos;\n        sol1[z][x][y] = position_to_block[pos];\n    }\n    \n    for (const auto& pos : req2) {\n        auto [z, x, y] = pos;\n        sol2[z][x][y] = position_to_block[pos];\n    }\n    \n    cout << total_blocks << \"\\n\";\n    \n    // Output solutions\n    bool first = true;\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 (!first) cout << \" \";\n                cout << sol1[z][x][y];\n                first = false;\n            }\n        }\n    }\n    cout << \"\\n\";\n    \n    first = true;\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 (!first) cout << \" \";\n                cout << sol2[z][x][y];\n                first = false;\n            }\n        }\n    }\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\nconst int MAXN = 105;\nconst int MAXM = 305;\nconst int MAXK = 5005;\n\nint N, M, K;\npair<int, int> vertices[MAXN];\ntuple<int, int, int> edges[MAXM];\npair<int, int> residents[MAXK];\nint P[MAXN];\nint B[MAXM];\n\n// Distance calculations\nll dist_sq(int x1, int y1, int x2, int y2) {\n    return 1LL * (x1 - x2) * (x1 - x2) + 1LL * (y1 - y2) * (y1 - y2);\n}\n\nld dist(int x1, int y1, int x2, int y2) {\n    return sqrt((ld)dist_sq(x1, y1, x2, y2));\n}\n\nint dist_rounded(int x1, int y1, int x2, int y2) {\n    return (int)round(dist(x1, y1, x2, y2));\n}\n\n// Find close vertices to a resident\nvector<pair<int, int>> find_close_vertices(int rx, int ry, int max_dist = 5000) {\n    vector<pair<int, int>> result;\n    for (int i = 0; i < N; i++) {\n        int d = dist_rounded(rx, ry, vertices[i].first, vertices[i].second);\n        if (d <= max_dist) {\n            result.push_back({i, d});\n        }\n    }\n    sort(result.begin(), result.end(), [](const pair<int, int>& a, const pair<int, int>& b) {\n        return a.second < b.second;\n    });\n    return result;\n}\n\n// Union-Find data structure\nclass UnionFind {\nprivate:\n    vector<int> parent, rank;\npublic:\n    UnionFind(int n) : parent(n), rank(n, 0) {\n        for (int i = 0; i < n; i++) parent[i] = i;\n    }\n    \n    int find(int x) {\n        if (parent[x] != x) parent[x] = find(parent[x]);\n        return parent[x];\n    }\n    \n    bool unite(int x, int y) {\n        int root_x = find(x);\n        int root_y = find(y);\n        if (root_x == root_y) return false;\n        if (rank[root_x] < rank[root_y]) swap(root_x, root_y);\n        parent[root_y] = root_x;\n        if (rank[root_x] == rank[root_y]) rank[root_x]++;\n        return true;\n    }\n};\n\n// Calculate total cost for a solution\nll calculate_total_cost(const vector<int>& P_values, const vector<bool>& B_values) {\n    ll cost = 0;\n    for (int i = 0; i < N; i++) {\n        cost += (ll)P_values[i] * P_values[i];\n    }\n    for (int i = 0; i < M; i++) {\n        if (B_values[i]) {\n            cost += get<2>(edges[i]);\n        }\n    }\n    return cost;\n}\n\n// Check if all residents are covered\nbool check_coverage(const vector<int>& P_values, const vector<bool>& vertex_active) {\n    // Build connectivity graph\n    UnionFind uf(N);\n    for (int i = 0; i < M; i++) {\n        if (B[i]) {\n            int u = get<0>(edges[i]) - 1;\n            int v = get<1>(edges[i]) - 1;\n            uf.unite(u, v);\n        }\n    }\n    \n    // Check if vertex 0 is connected to active vertices\n    int root_0 = uf.find(0);\n    \n    // For each resident, check if it can be covered\n    for (int r = 0; r < K; r++) {\n        bool covered = false;\n        auto options = find_close_vertices(residents[r].first, residents[r].second, 5000);\n        for (auto& [vertex, distance] : options) {\n            if (vertex_active[vertex] && uf.find(vertex) == root_0 && distance <= P_values[vertex]) {\n                covered = true;\n                break;\n            }\n        }\n        if (!covered) return false;\n    }\n    return true;\n}\n\n// Improved clustering approach\nvoid solve() {\n    // Initialize\n    fill(P, P + N, 0);\n    fill(B, B + M, 0);\n    \n    // Build adjacency information\n    vector<vector<pair<int, int>>> adj(N); // {neighbor, edge_weight}\n    for (int i = 0; i < M; i++) {\n        int u = get<0>(edges[i]) - 1;\n        int v = get<1>(edges[i]) - 1;\n        int w = get<2>(edges[i]);\n        adj[u].push_back({v, w});\n        adj[v].push_back({u, w});\n    }\n    \n    // Strategy: Cluster residents and assign to optimal vertices\n    \n    // Step 1: For each resident, find close vertices\n    vector<vector<pair<int, int>>> resident_options(K); // {vertex, distance}\n    for (int r = 0; r < K; r++) {\n        resident_options[r] = find_close_vertices(residents[r].first, residents[r].second, 3000);\n    }\n    \n    // Step 2: Assign residents to vertices (greedy approach)\n    vector<vector<int>> vertex_residents(N);\n    vector<int> resident_assignment(K, -1);\n    vector<bool> assigned(K, false);\n    \n    // Priority queue: {benefit/cost_ratio, resident_index, vertex_index}\n    priority_queue<tuple<ld, int, int>> assignment_queue;\n    \n    for (int r = 0; r < K; r++) {\n        for (auto& [vertex, distance] : resident_options[r]) {\n            if (distance <= 3000) {\n                ld benefit = 1.0;\n                ld cost = (ld)(distance * distance); // P^2 cost approximation\n                ld ratio = benefit / (cost + 1e-9);\n                assignment_queue.push({ratio, r, vertex});\n            }\n        }\n    }\n    \n    // Greedy assignment\n    while (!assignment_queue.empty()) {\n        auto [ratio, r, v] = assignment_queue.top();\n        assignment_queue.pop();\n        \n        if (!assigned[r]) {\n            resident_assignment[r] = v;\n            vertex_residents[v].push_back(r);\n            assigned[r] = true;\n        }\n    }\n    \n    // Handle unassigned residents\n    for (int r = 0; r < K; r++) {\n        if (!assigned[r]) {\n            if (!resident_options[r].empty()) {\n                resident_assignment[r] = resident_options[r][0].first;\n                vertex_residents[resident_options[r][0].first].push_back(r);\n            } else {\n                // Assign to closest vertex regardless of distance\n                int best_vertex = 0;\n                int min_dist = dist_rounded(residents[r].first, residents[r].second, \n                                          vertices[0].first, vertices[0].second);\n                for (int i = 1; i < N; i++) {\n                    int d = dist_rounded(residents[r].first, residents[r].second,\n                                       vertices[i].first, vertices[i].second);\n                    if (d < min_dist) {\n                        min_dist = d;\n                        best_vertex = i;\n                    }\n                }\n                resident_assignment[r] = best_vertex;\n                vertex_residents[best_vertex].push_back(r);\n            }\n        }\n    }\n    \n    // Step 3: Determine which vertices to activate\n    vector<bool> vertex_active(N, false);\n    for (int i = 0; i < N; i++) {\n        if (!vertex_residents[i].empty()) {\n            vertex_active[i] = true;\n        }\n    }\n    \n    // Always activate vertex 0 (AtCoder office)\n    vertex_active[0] = true;\n    \n    // Step 4: Calculate required output strengths\n    vector<int> P_temp(N, 0);\n    for (int i = 0; i < N; i++) {\n        if (vertex_active[i]) {\n            int max_radius = 0;\n            for (int res_idx : vertex_residents[i]) {\n                int d = dist_rounded(vertices[i].first, vertices[i].second,\n                                   residents[res_idx].first, residents[res_idx].second);\n                max_radius = max(max_radius, d);\n            }\n            P_temp[i] = min(max_radius, 5000);\n        }\n    }\n    \n    // Step 5: Build minimum cost network using Prim's algorithm\n    vector<bool> in_tree(N, false);\n    vector<ll> min_edge_cost(N, 1e18);\n    vector<int> parent(N, -1);\n    \n    // Start from vertex 0\n    in_tree[0] = true;\n    min_edge_cost[0] = 0;\n    \n    priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;\n    \n    // Initialize edges from vertex 0\n    for (auto& [v, w] : adj[0]) {\n        if (vertex_active[v]) {\n            if (w < min_edge_cost[v]) {\n                min_edge_cost[v] = w;\n                parent[v] = 0;\n                pq.push({w, v});\n            }\n        }\n    }\n    \n    // Prim's algorithm\n    while (!pq.empty()) {\n        auto [cost, u] = pq.top(); pq.pop();\n        if (in_tree[u]) continue;\n        \n        in_tree[u] = true;\n        \n        // Add edges from u\n        for (auto& [v, w] : adj[u]) {\n            if (!in_tree[v] && vertex_active[v]) {\n                if (w < min_edge_cost[v]) {\n                    min_edge_cost[v] = w;\n                    parent[v] = u;\n                    pq.push({w, v});\n                }\n            }\n        }\n    }\n    \n    // Mark edges in MST\n    vector<bool> B_temp(M, false);\n    for (int i = 0; i < N; i++) {\n        if (parent[i] != -1) {\n            // Find the edge between i and parent[i]\n            for (int e = 0; e < M; e++) {\n                int u = get<0>(edges[e]) - 1;\n                int v = get<1>(edges[e]) - 1;\n                if ((u == i && v == parent[i]) || (u == parent[i] && v == i)) {\n                    B_temp[e] = true;\n                    break;\n                }\n            }\n        }\n    }\n    \n    // Apply results\n    for (int i = 0; i < N; i++) {\n        P[i] = P_temp[i];\n    }\n    for (int i = 0; i < M; i++) {\n        B[i] = B_temp[i] ? 1 : 0;\n    }\n    \n    // Final validation and adjustment\n    // Ensure vertex 0 has connectivity\n    bool vertex_0_connected = false;\n    for (int i = 0; i < M; i++) {\n        if (B[i]) {\n            int u = get<0>(edges[i]) - 1;\n            int v = get<1>(edges[i]) - 1;\n            if (u == 0 || v == 0) {\n                vertex_0_connected = true;\n                break;\n            }\n        }\n    }\n    \n    // If vertex 0 is isolated but has residents assigned, connect it\n    if (!vertex_0_connected && P[0] > 0) {\n        // Connect to the closest active vertex with smallest edge weight\n        ll min_edge_weight = 1e18;\n        int best_edge = -1;\n        for (int i = 0; i < M; i++) {\n            int u = get<0>(edges[i]) - 1;\n            int v = get<1>(edges[i]) - 1;\n            int w = get<2>(edges[i]);\n            if ((u == 0 && vertex_active[v]) || (v == 0 && vertex_active[u])) {\n                if (w < min_edge_weight) {\n                    min_edge_weight = w;\n                    best_edge = i;\n                }\n            }\n        }\n        if (best_edge != -1) {\n            B[best_edge] = 1;\n        }\n    }\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    cin >> N >> M >> K;\n    \n    for (int i = 0; i < N; i++) {\n        cin >> vertices[i].first >> vertices[i].second;\n    }\n    \n    for (int i = 0; i < M; i++) {\n        int u, v, w;\n        cin >> u >> v >> w;\n        edges[i] = {u, v, w};\n    }\n    \n    for (int i = 0; i < K; i++) {\n        cin >> residents[i].first >> residents[i].second;\n    }\n    \n    solve();\n    \n    for (int i = 0; i < N; i++) {\n        cout << P[i];\n        if (i + 1 < N) cout << \" \";\n    }\n    cout << \"\\n\";\n    \n    for (int i = 0; i < M; i++) {\n        cout << B[i];\n        if (i + 1 < M) cout << \" \";\n    }\n    cout << \"\\n\";\n    \n    return 0;\n}","ahc021":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 30;\nconst int MAX_OPS = 10000;\n\nint pyramid[N][N];\nvector<tuple<int, int, int, int>> operations;\n\n// Check if coordinates are valid\nbool valid(int x, int y) {\n    return x >= 0 && x < N && y >= 0 && y <= x;\n}\n\n// Perform a swap and record operation\nvoid performSwap(int x1, int y1, int x2, int y2) {\n    if (operations.size() >= MAX_OPS) return;\n    \n    swap(pyramid[x1][y1], pyramid[x2][y2]);\n    operations.push_back({x1, y1, x2, y2});\n}\n\n// Count violations in current state\nint countViolations() {\n    int violations = 0;\n    for (int x = 0; x < N - 1; x++) {\n        for (int y = 0; y <= x; y++) {\n            if (pyramid[x][y] > pyramid[x+1][y]) violations++;\n            if (pyramid[x][y] > pyramid[x+1][y+1]) violations++;\n        }\n    }\n    return violations;\n}\n\n// Ultra-smart heap optimization with refined strategies\nvoid ultraSmartHeapOptimization() {\n    // Multiple rounds with intelligent strategy selection\n    for (int round = 0; round < 15 && operations.size() < MAX_OPS - 40; round++) {\n        int violationsAtStart = countViolations();\n        if (violationsAtStart == 0) return; // Already perfect!\n        \n        // Strategy selection based on round number and progress\n        if (round < 6) {\n            // Early rounds: systematic bottom-up approach (most effective)\n            for (int level = N - 2; level >= 0 && operations.size() < MAX_OPS - 8; level--) {\n                for (int pos = 0; pos <= level && operations.size() < MAX_OPS - 8; pos++) {\n                    if (level + 1 < N) {\n                        // Smart fixing with preference for smaller child\n                        bool violation1 = (pos <= level && pyramid[level][pos] > pyramid[level+1][pos]);\n                        bool violation2 = (pos + 1 <= level + 1 && pyramid[level][pos] > pyramid[level+1][pos+1]);\n                        \n                        if (violation1 || violation2) {\n                            if (violation1 && violation2) {\n                                if (pyramid[level+1][pos] <= pyramid[level+1][pos+1]) {\n                                    performSwap(level, pos, level+1, pos);\n                                } else {\n                                    performSwap(level, pos, level+1, pos+1);\n                                }\n                            } else if (violation1) {\n                                performSwap(level, pos, level+1, pos);\n                            } else if (violation2) {\n                                performSwap(level, pos, level+1, pos+1);\n                            }\n                        }\n                    }\n                }\n            }\n        } else if (round < 11) {\n            // Middle rounds: severity-based targeted fixing\n            // Collect all violations with severity information\n            vector<tuple<int, int, int, int, int>> violations; // (severity, parent_x, parent_y, child_x, child_y)\n            \n            for (int x = 0; x < N - 1; x++) {\n                for (int y = 0; y <= x; y++) {\n                    if (pyramid[x][y] > pyramid[x+1][y]) {\n                        int severity = pyramid[x][y] - pyramid[x+1][y];\n                        violations.push_back({severity, x, y, x+1, y});\n                    }\n                    if (pyramid[x][y] > pyramid[x+1][y+1]) {\n                        int severity = pyramid[x][y] - pyramid[x+1][y+1];\n                        violations.push_back({severity, x, y, x+1, y+1});\n                    }\n                }\n            }\n            \n            if (!violations.empty()) {\n                // Sort by severity (worst first)\n                sort(violations.begin(), violations.end(), greater<tuple<int, int, int, int, int>>());\n                \n                // Fix the most severe violations first\n                for (int i = 0; i < min(80, (int)violations.size()) && operations.size() < MAX_OPS - 5; i++) {\n                    int severity, px, py, cx, cy;\n                    tie(severity, px, py, cx, cy) = violations[i];\n                    if (px < N && py <= px && cx < N && cy <= cx) {\n                        if (pyramid[px][py] > pyramid[cx][cy]) {\n                            performSwap(px, py, cx, cy);\n                        }\n                    }\n                }\n            }\n        } else {\n            // Late rounds: aggressive fixing + intelligent randomization\n            auto violations = countViolations();\n            if (violations > 0) {\n                // Get all current violations\n                vector<pair<int, int>> violationParents;\n                for (int x = 0; x < N - 1; x++) {\n                    for (int y = 0; y <= x; y++) {\n                        if (pyramid[x][y] > pyramid[x+1][y] || pyramid[x][y] > pyramid[x+1][y+1]) {\n                            violationParents.push_back({x, y});\n                        }\n                    }\n                }\n                \n                // Fix violations aggressively\n                for (auto& parent : violationParents) {\n                    if (operations.size() >= MAX_OPS - 8) break;\n                    \n                    int x = parent.first, y = parent.second;\n                    \n                    // Try multiple fixes per problematic position\n                    for (int attempt = 0; attempt < 4 && operations.size() < MAX_OPS - 3; attempt++) {\n                        bool violation1 = pyramid[x][y] > pyramid[x+1][y];\n                        bool violation2 = pyramid[x][y] > pyramid[x+1][y+1];\n                        \n                        if (!violation1 && !violation2) break;\n                        \n                        if (violation1 && violation2) {\n                            if (pyramid[x+1][y] <= pyramid[x+1][y+1]) {\n                                performSwap(x, y, x+1, y);\n                            } else {\n                                performSwap(x, y, x+1, y+1);\n                            }\n                        } else if (violation1) {\n                            performSwap(x, y, x+1, y);\n                        } else if (violation2) {\n                            performSwap(x, y, x+1, y+1);\n                        }\n                    }\n                }\n            }\n            \n            // If still stuck, add intelligent randomization\n            if (violationsAtStart > 0 && round > 12 && operations.size() < MAX_OPS - 150) {\n                // Add strategic random swaps to escape local minima\n                for (int randAttempt = 0; randAttempt < 12 && operations.size() < MAX_OPS - 4; randAttempt++) {\n                    int x = rand() % (N - 1);\n                    int y = rand() % (x + 1);\n                    if (x + 1 < N) {\n                        // Smart random swap - prefer fixing violations\n                        if (rand() % 2 == 0) {\n                            performSwap(x, y, x+1, y);\n                        } else {\n                            performSwap(x, y, x+1, y+1);\n                        }\n                    }\n                }\n            }\n        }\n        \n        // Check progress\n        int violationsAtEnd = countViolations();\n        if (violationsAtEnd == 0) return; // Perfect!\n    }\n}\n\n// Systematic perfection optimization\nvoid systematicPerfectionOptimization() {\n    if (operations.size() < MAX_OPS - 150) {\n        // Multiple passes with different scanning strategies\n        for (int pass = 0; pass < 5 && operations.size() < MAX_OPS - 80; pass++) {\n            int violations = countViolations();\n            if (violations == 0) return; // Already perfect!\n            \n            // Alternate between different scanning patterns\n            if (pass % 3 == 0) {\n                // Normal sweep (top to bottom)\n                for (int x = 0; x < N - 1 && operations.size() < MAX_OPS - 6; x++) {\n                    for (int y = 0; y <= x && operations.size() < MAX_OPS - 6; y++) {\n                        if (x < N - 1) {\n                            // Aggressive fixing with multiple attempts\n                            for (int attempt = 0; attempt < 3 && operations.size() < MAX_OPS - 3; attempt++) {\n                                bool violation1 = pyramid[x][y] > pyramid[x+1][y];\n                                bool violation2 = pyramid[x][y] > pyramid[x+1][y+1];\n                                \n                                if (!violation1 && !violation2) break;\n                                \n                                if (violation1 && violation2) {\n                                    if (pyramid[x+1][y] <= pyramid[x+1][y+1]) {\n                                        performSwap(x, y, x+1, y);\n                                    } else {\n                                        performSwap(x, y, x+1, y+1);\n                                    }\n                                } else if (violation1) {\n                                    performSwap(x, y, x+1, y);\n                                } else if (violation2) {\n                                    performSwap(x, y, x+1, y+1);\n                                }\n                            }\n                        }\n                    }\n                }\n            } else if (pass % 3 == 1) {\n                // Reverse sweep (bottom to top)\n                for (int x = N - 2; x >= 0 && operations.size() < MAX_OPS - 6; x--) {\n                    for (int y = x; y >= 0 && operations.size() < MAX_OPS - 6; y--) {\n                        if (x < N - 1) {\n                            for (int attempt = 0; attempt < 3 && operations.size() < MAX_OPS - 3; attempt++) {\n                                bool violation1 = pyramid[x][y] > pyramid[x+1][y];\n                                bool violation2 = pyramid[x][y] > pyramid[x+1][y+1];\n                                \n                                if (!violation1 && !violation2) break;\n                                \n                                if (violation1 && violation2) {\n                                    if (pyramid[x+1][y] <= pyramid[x+1][y+1]) {\n                                        performSwap(x, y, x+1, y);\n                                    } else {\n                                        performSwap(x, y, x+1, y+1);\n                                    }\n                                } else if (violation1) {\n                                    performSwap(x, y, x+1, y);\n                                } else if (violation2) {\n                                    performSwap(x, y, x+1, y+1);\n                                }\n                            }\n                        }\n                    }\n                }\n            } else {\n                // Randomized approach for stubborn cases\n                vector<pair<int, int>> violationParents;\n                for (int x = 0; x < N - 1; x++) {\n                    for (int y = 0; y <= x; y++) {\n                        if (pyramid[x][y] > pyramid[x+1][y] || pyramid[x][y] > pyramid[x+1][y+1]) {\n                            violationParents.push_back({x, y});\n                        }\n                    }\n                }\n                \n                if (!violationParents.empty()) {\n                    random_shuffle(violationParents.begin(), violationParents.end());\n                    for (int i = 0; i < min(30, (int)violationParents.size()) && operations.size() < MAX_OPS - 4; i++) {\n                        int x = violationParents[i].first;\n                        int y = violationParents[i].second;\n                        if (x < N - 1) {\n                            if (pyramid[x][y] > pyramid[x+1][y]) {\n                                performSwap(x, y, x+1, y);\n                            } else if (pyramid[x][y] > pyramid[x+1][y+1]) {\n                                performSwap(x, y, x+1, y+1);\n                            }\n                        }\n                    }\n                }\n            }\n        }\n    }\n}\n\n// Ultra-aggressive final perfection push\nvoid ultraAggressiveFinalPush() {\n    if (operations.size() < MAX_OPS - 25) {\n        int currentViolations = countViolations();\n        if (currentViolations == 0) return; // Already perfect!\n        \n        // Go all-out to achieve perfection\n        for (int megaRound = 0; megaRound < 5 && operations.size() < MAX_OPS - 15 && currentViolations > 0; megaRound++) {\n            // Get all current violations with severity\n            vector<tuple<int, int, int, int>> violations; // (severity, parent_x, parent_y, child_type)\n            \n            for (int x = 0; x < N - 1; x++) {\n                for (int y = 0; y <= x; y++) {\n                    if (pyramid[x][y] > pyramid[x+1][y]) {\n                        int severity = pyramid[x][y] - pyramid[x+1][y];\n                        violations.push_back({severity, x, y, 0}); // 0 = left child\n                    }\n                    if (pyramid[x][y] > pyramid[x+1][y+1]) {\n                        int severity = pyramid[x][y] - pyramid[x+1][y+1];\n                        violations.push_back({severity, x, y, 1}); // 1 = right child\n                    }\n                }\n            }\n            \n            if (violations.empty()) return; // Perfect!\n            \n            // Sort by severity and fix the worst ones\n            sort(violations.begin(), violations.end(), greater<tuple<int, int, int, int>>());\n            \n            // Fix violations multiple times for maximum effectiveness\n            for (int repetition = 0; repetition < 2; repetition++) {\n                for (int i = 0; i < min(40, (int)violations.size()) && operations.size() < MAX_OPS - 3; i++) {\n                    int severity, px, py, childType;\n                    tie(severity, px, py, childType) = violations[i];\n                    \n                    if (px < N && py <= px && px + 1 < N) {\n                        if (childType == 0 && py <= px + 1 && pyramid[px][py] > pyramid[px+1][py]) {\n                            performSwap(px, py, px+1, py);\n                        } else if (childType == 1 && py + 1 <= px + 2 && pyramid[px][py] > pyramid[px+1][py+1]) {\n                            performSwap(px, py, px+1, py+1);\n                        }\n                    }\n                }\n            }\n            \n            // Check if we're making progress\n            int newViolations = countViolations();\n            if (newViolations == 0) return; // Perfect!\n            \n            // If not making progress, add more aggressive randomization\n            if (newViolations >= currentViolations && operations.size() < MAX_OPS - 200) {\n                // Add strategic random swaps\n                for (int randRound = 0; randRound < 3 && operations.size() < MAX_OPS - 8; randRound++) {\n                    for (int randAttempt = 0; randAttempt < 15; randAttempt++) {\n                        int x1 = rand() % N;\n                        int y1 = rand() % (x1 + 1);\n                        int direction = rand() % 6;\n                        \n                        // Define 6 possible directions\n                        int dx[] = {-1, -1, 0, 0, 1, 1};\n                        int dy[] = {-1, 0, -1, 1, 0, 1};\n                        \n                        int x2 = x1 + dx[direction];\n                        int y2 = y1 + dy[direction];\n                        \n                        if (valid(x1, y1) && valid(x2, y2)) {\n                            performSwap(x1, y1, x2, y2);\n                            break; // Just one strategic random swap per round\n                        }\n                    }\n                }\n            }\n            \n            currentViolations = newViolations;\n        }\n    }\n}\n\nint main() {\n    // Initialize random seed\n    srand(time(nullptr));\n    \n    // Read input\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j <= i; j++) {\n            cin >> pyramid[i][j];\n        }\n    }\n    \n    // Get initial state\n    int initialViolations = countViolations();\n    \n    // Apply ultra-smart optimization\n    ultraSmartHeapOptimization();\n    \n    // Systematic perfection optimization\n    if (operations.size() < MAX_OPS - 120) {\n        systematicPerfectionOptimization();\n    }\n    \n    // Ultra-aggressive final push\n    if (operations.size() < MAX_OPS - 15) {\n        ultraAggressiveFinalPush();\n    }\n    \n    // Output results\n    cout << operations.size() << \"\\n\";\n    for (auto& op : operations) {\n        cout << get<0>(op) << \" \" << get<1>(op) << \" \" << get<2>(op) << \" \" << get<3>(op) << \"\\n\";\n    }\n    \n    return 0;\n}","toyota2023summer-final":"#include <bits/stdc++.h>\n#include <atcoder/all>\nusing namespace std;\nusing namespace atcoder;\n\nconst int D = 9;\nconst int INF = 100000000;\n\nint entrance_i = 0;\nint entrance_j = 4;\n\nvector<vector<bool>> is_obstacle(D, vector<bool>(D, false));\nvector<vector<int>> container_id(D, vector<int>(D, -1));\nvector<pair<int, int>> container_positions(D * D); // id -> position\nvector<int> container_numbers(D * D); // position index -> container number\n\n// Directions: up, down, left, right\nconst int di[4] = {-1, 1, 0, 0};\nconst int dj[4] = {0, 0, -1, 1};\n\n// Check if position is valid\nbool is_valid(int i, int j) {\n    return i >= 0 && i < D && j >= 0 && j < D;\n}\n\n// Check if a position is the entrance or adjacent to entrance\nbool is_entrance_area(int i, int j) {\n    if (i == 0 && j == 4) return true;\n    if (i == 0 && j == 3) return true;\n    if (i == 0 && j == 5) return true;\n    if (i == 1 && j == 4) return true;\n    return false;\n}\n\n// BFS to find all reachable positions from entrance\nvector<vector<int>> get_distances_from_entrance() {\n    vector<vector<int>> dist(D, vector<int>(D, INF));\n    queue<pair<int, int>> q;\n    \n    // Start from entrance\n    dist[0][4] = 0;\n    q.push({0, 4});\n    \n    while (!q.empty()) {\n        auto [ci, cj] = q.front(); q.pop();\n        \n        for (int d = 0; d < 4; d++) {\n            int ni = ci + di[d];\n            int nj = cj + dj[d];\n            \n            if (is_valid(ni, nj) && !is_obstacle[ni][nj] && container_id[ni][nj] == -1) {\n                if (dist[ni][nj] > dist[ci][cj] + 1) {\n                    dist[ni][nj] = dist[ci][cj] + 1;\n                    q.push({ni, nj});\n                }\n            }\n        }\n    }\n    \n    return dist;\n}\n\n// Find all reachable empty positions\nvector<pair<int, int>> get_reachable_empty_positions() {\n    vector<vector<bool>> visited(D, vector<bool>(D, false));\n    vector<pair<int, int>> result;\n    queue<pair<int, int>> q;\n    \n    // Start from entrance\n    visited[0][4] = true;\n    q.push({0, 4});\n    if (container_id[0][4] == -1 && !is_obstacle[0][4]) {\n        result.push_back({0, 4});\n    }\n    \n    while (!q.empty()) {\n        auto [ci, cj] = q.front(); q.pop();\n        \n        for (int d = 0; d < 4; d++) {\n            int ni = ci + di[d];\n            int nj = cj + dj[d];\n            \n            if (is_valid(ni, nj) && !visited[ni][nj] && !is_obstacle[ni][nj]) {\n                visited[ni][nj] = true;\n                q.push({ni, nj});\n                if (container_id[ni][nj] == -1) {\n                    result.push_back({ni, nj});\n                }\n            }\n        }\n    }\n    \n    return result;\n}\n\n// Calculate Manhattan distance from entrance\nint manhattan_distance(int i, int j) {\n    return abs(i - 0) + abs(j - 4);\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int N;\n    cin >> D >> N;\n    \n    for (int i = 0; i < N; i++) {\n        int ri, rj;\n        cin >> ri >> rj;\n        is_obstacle[ri][rj] = true;\n    }\n    \n    int total_containers = D * D - 1 - N;\n    vector<int> transport_order(total_containers);\n    \n    // Process each container as it arrives\n    for (int d = 0; d < total_containers; d++) {\n        int t_d;\n        cin >> t_d;\n        transport_order[d] = t_d;\n        \n        // Get all reachable empty positions\n        auto reachable_positions = get_reachable_empty_positions();\n        \n        // Remove entrance area from consideration (except if it's the only option)\n        vector<pair<int, int>> valid_positions;\n        for (auto& pos : reachable_positions) {\n            if (!is_entrance_area(pos.first, pos.second)) {\n                valid_positions.push_back(pos);\n            }\n        }\n        \n        // If no valid positions except entrance area, use entrance area\n        if (valid_positions.empty()) {\n            for (auto& pos : reachable_positions) {\n                if (pos.first != 0 || pos.second != 4) { // exclude exact entrance\n                    valid_positions.push_back(pos);\n                }\n            }\n        }\n        \n        // If still empty, just use entrance\n        if (valid_positions.empty()) {\n            valid_positions.push_back({0, 4});\n        }\n        \n        // Choose position based on container number\n        // Lower numbers should be placed closer to entrance for easier retrieval\n        pair<int, int> best_position = valid_positions[0];\n        int best_score = INF;\n        \n        for (auto& pos : valid_positions) {\n            int dist = manhattan_distance(pos.first, pos.second);\n            // Prefer positions closer to entrance for lower numbered containers\n            // But also add some randomness to avoid getting stuck\n            int score = dist * 1000 + (t_d * 10) + (rand() % 100);\n            \n            if (score < best_score) {\n                best_score = score;\n                best_position = pos;\n            }\n        }\n        \n        // Place the container\n        int pi = best_position.first;\n        int pj = best_position.second;\n        container_id[pi][pj] = d;\n        container_positions[d] = {pi, pj};\n        container_numbers[d] = t_d;\n        \n        // Output the position\n        cout << pi << \" \" << pj << endl;\n        cout.flush();\n    }\n    \n    // Now determine transport order\n    // We want to transport in order of assigned numbers (0, 1, 2, ...)\n    // But we can only transport reachable containers\n    \n    vector<bool> transported(total_containers, false);\n    vector<pair<int, int>> transport_sequence;\n    \n    for (int target_number = 0; target_number < total_containers; target_number++) {\n        // Find container with target_number\n        int container_idx = -1;\n        for (int i = 0; i < total_containers; i++) {\n            if (container_numbers[i] == target_number) {\n                container_idx = i;\n                break;\n            }\n        }\n        \n        if (container_idx == -1) continue;\n        \n        // Mark this container for transport when it becomes reachable\n        pair<int, int> target_pos = container_positions[container_idx];\n        \n        // For simplicity, we'll transport containers in the order they become reachable\n        // In a more sophisticated solution, we'd use a priority queue\n        transport_sequence.push_back(target_pos);\n    }\n    \n    // Actually, let's implement a better approach:\n    // Try to transport containers in order of their numbers, but only when reachable\n    vector<bool> container_transported(total_containers, false);\n    transport_sequence.clear();\n    \n    while (transport_sequence.size() < total_containers) {\n        // Get currently reachable containers\n        auto distances = get_distances_from_entrance();\n        vector<pair<int, int>> reachable_containers;\n        vector<int> reachable_indices;\n        \n        for (int i = 0; i < total_containers; i++) {\n            if (container_transported[i]) continue;\n            \n            auto pos = container_positions[i];\n            if (distances[pos.first][pos.second] < INF) {\n                reachable_containers.push_back(pos);\n                reachable_indices.push_back(i);\n            }\n        }\n        \n        if (reachable_containers.empty()) {\n            // This shouldn't happen if the problem is solvable\n            break;\n        }\n        \n        // Among reachable containers, pick the one with lowest assigned number\n        int best_idx = reachable_indices[0];\n        int best_number = container_numbers[best_idx];\n        \n        for (int idx : reachable_indices) {\n            if (container_numbers[idx] < best_number) {\n                best_number = container_numbers[idx];\n                best_idx = idx;\n            }\n        }\n        \n        // Transport this container\n        auto pos = container_positions[best_idx];\n        transport_sequence.push_back(pos);\n        container_transported[best_idx] = true;\n        \n        // Clear the position\n        container_id[pos.first][pos.second] = -1;\n    }\n    \n    // Output transport sequence\n    for (auto& pos : transport_sequence) {\n        cout << pos.first << \" \" << pos.second << \"\\n\";\n    }\n    \n    return 0;\n}","ahc024":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 50;\nconst int M = 100;\n\nint grid[N][N];\nint result[N][N];\nvector<pair<int,int>> positions[M+1]; // positions for each color\nvector<int> adj_list[M+1]; // adjacency list\nbool adj_matrix[M+1][M+1]; // adjacency matrix\n\n// Directions: up, right, down, left\nconst int dx[4] = {-1, 0, 1, 0};\nconst int dy[4] = {0, 1, 0, -1};\n\nvoid extract_adjacencies() {\n    // Initialize adjacency matrix\n    memset(adj_matrix, 0, sizeof(adj_matrix));\n    \n    // Check adjacencies in the original grid\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            int color = grid[i][j];\n            for (int d = 0; d < 4; d++) {\n                int ni = i + dx[d];\n                int nj = j + dy[d];\n                int neighbor_color = 0; // outside is color 0\n                if (ni >= 0 && ni < N && nj >= 0 && nj < N) {\n                    neighbor_color = grid[ni][nj];\n                }\n                if (color != neighbor_color) {\n                    adj_matrix[color][neighbor_color] = true;\n                    adj_matrix[neighbor_color][color] = true;\n                }\n            }\n        }\n    }\n    \n    // Build adjacency lists\n    for (int i = 0; i <= M; i++) {\n        adj_list[i].clear();\n        for (int j = 0; j <= M; j++) {\n            if (adj_matrix[i][j] && i != j) {\n                adj_list[i].push_back(j);\n            }\n        }\n    }\n}\n\n// Check if we can place a color at position (x,y)\nbool can_place(int x, int y, int color) {\n    if (x < 0 || x >= N || y < 0 || y >= N) return false;\n    return result[x][y] == 0;\n}\n\n// Check if placement maintains adjacency requirements\nbool check_adjacency(int x, int y, int color) {\n    // Check if this placement creates required adjacencies\n    for (int d = 0; d < 4; d++) {\n        int nx = x + dx[d];\n        int ny = y + dy[d];\n        int neighbor = 0;\n        if (nx >= 0 && nx < N && ny >= 0 && ny < N) {\n            neighbor = result[nx][ny];\n        }\n        \n        // If neighbor is a color that should be adjacent, that's good\n        // If neighbor is a color that shouldn't be adjacent, that's bad (but we can't know all adjacencies yet)\n    }\n    return true;\n}\n\nvoid place_color_compactly(int color) {\n    if (color == 0) return; // Don't place color 0, it's empty space\n    \n    // If this color is already placed, skip\n    if (!positions[color].empty()) return;\n    \n    // Find a good starting position - near already placed colors that it should be adjacent to\n    vector<pair<int,int>> candidates;\n    \n    // If no colors placed yet, start in the middle\n    if (positions[1].empty()) {\n        int cx = N/2, cy = N/2;\n        result[cx][cy] = color;\n        positions[color].push_back({cx, cy});\n        return;\n    }\n    \n    // Try to place near adjacencies\n    bool placed = false;\n    for (int adj_color : adj_list[color]) {\n        if (!positions[adj_color].empty()) {\n            // Try to place near this adjacent color\n            for (auto& pos : positions[adj_color]) {\n                int x = pos.first, y = pos.second;\n                for (int d = 0; d < 4; d++) {\n                    int nx = x + dx[d];\n                    int ny = y + dy[d];\n                    if (nx >= 0 && nx < N && ny >= 0 && ny < N && result[nx][ny] == 0) {\n                        result[nx][ny] = color;\n                        positions[color].push_back({nx, ny});\n                        placed = true;\n                        break;\n                    }\n                }\n                if (placed) break;\n            }\n            if (placed) break;\n        }\n    }\n    \n    // If couldn't place near adjacencies, place anywhere available\n    if (!placed) {\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                if (result[i][j] == 0) {\n                    result[i][j] = color;\n                    positions[color].push_back({i, j});\n                    placed = true;\n                    break;\n                }\n            }\n            if (placed) break;\n        }\n    }\n    \n    // Expand the color to make it connected\n    if (!positions[color].empty()) {\n        // Simple expansion to make it connected - just place one tile\n        // In a more sophisticated solution, we'd expand to match the required size\n        // But since we're optimizing for fewer empty tiles, smaller is better\n    }\n}\n\n// Greedy approach: place colors in order of how many adjacencies they have\nvoid solve() {\n    // Initialize result grid\n    memset(result, 0, sizeof(result));\n    for (int i = 0; i <= M; i++) {\n        positions[i].clear();\n    }\n    \n    // Create ordering based on adjacency count (place high-degree nodes first)\n    vector<pair<int, int>> order; // {adjacency_count, color}\n    for (int i = 1; i <= M; i++) {\n        order.push_back({(int)adj_list[i].size(), i});\n    }\n    sort(order.rbegin(), order.rend()); // descending order\n    \n    // Place colors\n    for (auto& p : order) {\n        place_color_compactly(p.second);\n    }\n    \n    // Ensure connectivity for each color\n    for (int color = 1; color <= M; color++) {\n        if (positions[color].empty()) {\n            // Place at least one tile\n            for (int i = 0; i < N; i++) {\n                for (int j = 0; j < N; j++) {\n                    if (result[i][j] == 0) {\n                        result[i][j] = color;\n                        positions[color].push_back({i, j});\n                        goto placed;\n                    }\n                }\n            }\n            placed:;\n        }\n    }\n}\n\n// Better approach: create a compact layout\nvoid solve_better() {\n    // Initialize result grid\n    memset(result, 0, sizeof(result));\n    for (int i = 0; i <= M; i++) {\n        positions[i].clear();\n    }\n    \n    // Start by placing color 1 in the center\n    int center_x = N/2, center_y = N/2;\n    result[center_x][center_y] = 1;\n    positions[1].push_back({center_x, center_y});\n    \n    // Place other colors in a spiral pattern to minimize empty space\n    vector<pair<int,int>> spiral_dirs = {{0,1}, {1,0}, {0,-1}, {-1,0}}; // right, down, left, up\n    int dir = 0;\n    int x = center_x, y = center_y;\n    int steps = 1;\n    \n    vector<int> colors_to_place;\n    for (int i = 2; i <= M; i++) {\n        colors_to_place.push_back(i);\n    }\n    \n    int color_idx = 0;\n    while (color_idx < (int)colors_to_place.size()) {\n        for (int step = 0; step < 2; step++) {\n            for (int i = 0; i < steps && color_idx < (int)colors_to_place.size(); i++) {\n                x += spiral_dirs[dir].first;\n                y += spiral_dirs[dir].second;\n                \n                if (x >= 0 && x < N && y >= 0 && y < N && result[x][y] == 0) {\n                    int color = colors_to_place[color_idx];\n                    result[x][y] = color;\n                    positions[color].push_back({x, y});\n                    color_idx++;\n                }\n                \n                if (color_idx >= (int)colors_to_place.size()) break;\n            }\n            dir = (dir + 1) % 4;\n            if (color_idx >= (int)colors_to_place.size()) break;\n        }\n        steps++;\n    }\n    \n    // This approach doesn't preserve adjacencies, so let's try a different approach\n}\n\nvoid solve_best() {\n    // Initialize result grid\n    memset(result, 0, sizeof(result));\n    for (int i = 0; i <= M; i++) {\n        positions[i].clear();\n    }\n    \n    // Simple but effective: place colors in a dense grid pattern\n    int idx = 0;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (idx < M) {\n                int color = idx + 1;\n                result[i][j] = color;\n                positions[color].push_back({i, j});\n                idx++;\n            } else {\n                // Fill remaining with a pattern to maintain some adjacencies\n                result[i][j] = 0;\n            }\n        }\n    }\n    \n    // This still doesn't preserve the original adjacencies\n    // Let's try a more sophisticated approach\n}\n\n// Let's go with a simple but effective approach\nvoid solve_simple() {\n    // Initialize result grid\n    memset(result, 0, sizeof(result));\n    for (int i = 0; i <= M; i++) {\n        positions[i].clear();\n    }\n    \n    // Place colors in a way that tries to preserve adjacencies\n    // Place first color in center\n    result[N/2][N/2] = 1;\n    positions[1].push_back({N/2, N/2});\n    \n    // For remaining colors, place them near colors they should be adjacent to\n    vector<bool> placed(M+1, false);\n    placed[0] = true; // color 0 is special\n    placed[1] = true;\n    \n    queue<int> to_process;\n    to_process.push(1);\n    \n    while (!to_process.empty()) {\n        int current_color = to_process.front();\n        to_process.pop();\n        \n        // Try to place adjacent colors\n        for (int adj_color : adj_list[current_color]) {\n            if (!placed[adj_color] && adj_color != 0) {\n                // Try to place adj_color near current_color\n                bool placed_adj = false;\n                for (auto& pos : positions[current_color]) {\n                    int x = pos.first, y = pos.second;\n                    // Try 8 directions (including diagonals for more flexibility)\n                    for (int dx_val = -1; dx_val <= 1; dx_val++) {\n                        for (int dy_val = -1; dy_val <= 1; dy_val++) {\n                            if (dx_val == 0 && dy_val == 0) continue;\n                            int nx = x + dx_val;\n                            int ny = y + dy_val;\n                            if (nx >= 0 && nx < N && ny >= 0 && ny < N && result[nx][ny] == 0) {\n                                result[nx][ny] = adj_color;\n                                positions[adj_color].push_back({nx, ny});\n                                placed[adj_color] = true;\n                                to_process.push(adj_color);\n                                placed_adj = true;\n                                break;\n                            }\n                        }\n                        if (placed_adj) break;\n                    }\n                    if (placed_adj) break;\n                }\n                \n                // If we couldn't place it near an adjacent color, place it anywhere\n                if (!placed_adj) {\n                    for (int i = 0; i < N; i++) {\n                        for (int j = 0; j < N; j++) {\n                            if (result[i][j] == 0) {\n                                result[i][j] = adj_color;\n                                positions[adj_color].push_back({i, j});\n                                placed[adj_color] = true;\n                                to_process.push(adj_color);\n                                placed_adj = true;\n                                break;\n                            }\n                        }\n                        if (placed_adj) break;\n                    }\n                }\n            }\n        }\n    }\n    \n    // Place any remaining unplaced colors\n    for (int color = 1; color <= M; color++) {\n        if (!placed[color]) {\n            for (int i = 0; i < N; i++) {\n                for (int j = 0; j < N; j++) {\n                    if (result[i][j] == 0) {\n                        result[i][j] = color;\n                        positions[color].push_back({i, j});\n                        placed[color] = true;\n                        break;\n                    }\n                }\n                if (placed[color]) break;\n            }\n        }\n    }\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    int n, m;\n    cin >> n >> m;\n    \n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            cin >> grid[i][j];\n        }\n    }\n    \n    extract_adjacencies();\n    solve_simple();\n    \n    // Output result\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            if (j > 0) cout << \" \";\n            cout << result[i][j];\n        }\n        cout << \"\\n\";\n    }\n    \n    return 0;\n}","ahc025":"#include <bits/stdc++.h>\n\nusing namespace std;\n\nvoid solve() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    // Read input\n    int N, D, Q;\n    cin >> N >> D >> Q;\n    \n    // Initialize data structures\n    vector<double> weight_estimates(N, 1.0);\n    vector<vector<int>> comparison_count(N, vector<int>(N, 0));\n    vector<vector<int>> win_count(N, vector<int>(N, 0));\n    \n    // Query strategy with adaptive phases\n    int queries_used = 0;\n    \n    // Phase 1: Balanced exploration (40% of queries)\n    int exploration_queries = Q * 2 / 5;\n    \n    // Systematic initial comparisons\n    for (int i = 0; i < N && queries_used < exploration_queries; i++) {\n        // Compare each item with a few neighbors\n        int comparisons_for_this_item = min(3, max(1, (exploration_queries - queries_used) / max(1, N - i)));\n        \n        for (int k = 0; k < comparisons_for_this_item && queries_used < exploration_queries; k++) {\n            int j = (i + k + 1) % N;\n            if (i == j) continue;\n            \n            // Ensure consistent ordering\n            int item1 = min(i, j);\n            int item2 = max(i, j);\n            \n            cout << \"1 1 \" << item1 << \" \" << item2 << endl;\n            cout.flush();\n            \n            string response;\n            cin >> response;\n            \n            comparison_count[item1][item2]++;\n            comparison_count[item2][item1]++;\n            \n            if (response == \">\") {\n                win_count[item1][item2]++;\n            } else if (response == \"<\") {\n                win_count[item2][item1]++;\n            }\n            \n            queries_used++;\n        }\n    }\n    \n    // Phase 2: Intelligent refinement (40% of queries)\n    int refinement_queries = Q * 2 / 5;\n    while (queries_used < min(Q * 4 / 5, exploration_queries + refinement_queries)) {\n        // Periodically update weight estimates\n        if (queries_used % max(1, refinement_queries / 10) == 0) {\n            // Update weights based on current data\n            vector<double> scores(N, 0.0);\n            \n            for (int i = 0; i < N; i++) {\n                double total_score = 0.0;\n                int total_comparisons = 0;\n                \n                for (int j = 0; j < N; j++) {\n                    if (i != j && comparison_count[i][j] > 0) {\n                        double win_rate = (double)win_count[i][j] / comparison_count[i][j];\n                        total_score += win_rate * comparison_count[i][j];\n                        total_comparisons += comparison_count[i][j];\n                    }\n                }\n                \n                if (total_comparisons > 0) {\n                    scores[i] = total_score / total_comparisons;\n                } else {\n                    scores[i] = 0.5;\n                }\n            }\n            \n            // Convert to weights\n            double min_score = 1e9, max_score = -1e9;\n            for (double s : scores) {\n                min_score = min(min_score, s);\n                max_score = max(max_score, s);\n            }\n            \n            double score_range = max_score - min_score;\n            if (score_range > 1e-9) {\n                for (int i = 0; i < N; i++) {\n                    weight_estimates[i] = max(0.001, (scores[i] - min_score) / score_range);\n                }\n            }\n        }\n        \n        // Select most informative comparison\n        int item1 = -1, item2 = -1;\n        double best_value = -1;\n        \n        // Strategy: compare items with similar weights (most informative)\n        for (int i = 0; i < N; i++) {\n            for (int j = i + 1; j < N; j++) {\n                if (comparison_count[i][j] < 2) { // Not over-compared\n                    // Value based on similarity and information gain\n                    double weight_diff = abs(weight_estimates[i] - weight_estimates[j]);\n                    double similarity_value = 1.0 / (0.01 + weight_diff);\n                    double needed_value = 2.0 / (1.0 + comparison_count[i][j]);\n                    \n                    double total_value = similarity_value * needed_value;\n                    \n                    if (total_value > best_value) {\n                        best_value = total_value;\n                        item1 = i;\n                        item2 = j;\n                    }\n                }\n            }\n        }\n        \n        // Fallback if no good comparison found\n        if (item1 == -1 || item2 == -1) {\n            // Find items compared the least\n            vector<int> total_comparisons(N, 0);\n            for (int i = 0; i < N; i++) {\n                for (int j = 0; j < N; j++) {\n                    total_comparisons[i] += comparison_count[i][j];\n                }\n            }\n            \n            vector<pair<int, int>> item_indices;\n            for (int i = 0; i < N; i++) {\n                item_indices.push_back({total_comparisons[i], i});\n            }\n            sort(item_indices.begin(), item_indices.end());\n            \n            item1 = item_indices[0].second;\n            do {\n                item2 = rand() % N;\n            } while (item2 == item1);\n        }\n        \n        // Ensure consistent ordering\n        if (item1 > item2) swap(item1, item2);\n        \n        cout << \"1 1 \" << item1 << \" \" << item2 << endl;\n        cout.flush();\n        \n        string response;\n        cin >> response;\n        \n        comparison_count[item1][item2]++;\n        comparison_count[item2][item1]++;\n        \n        if (response == \">\") {\n            win_count[item1][item2]++;\n        } else if (response == \"<\") {\n            win_count[item2][item1]++;\n        }\n        \n        queries_used++;\n    }\n    \n    // Phase 3: Final optimization (20% of queries)\n    while (queries_used < Q) {\n        // Update final weight estimates\n        vector<double> scores(N, 0.0);\n        vector<int> confidence(N, 0);\n        \n        for (int i = 0; i < N; i++) {\n            double total_score = 0.0;\n            int total_comparisons = 0;\n            \n            for (int j = 0; j < N; j++) {\n                if (i != j && comparison_count[i][j] > 0) {\n                    double win_rate = (double)win_count[i][j] / comparison_count[i][j];\n                    total_score += win_rate * comparison_count[i][j];\n                    total_comparisons += comparison_count[i][j];\n                }\n            }\n            \n            if (total_comparisons > 0) {\n                scores[i] = total_score / total_comparisons;\n                confidence[i] = total_comparisons;\n            } else {\n                scores[i] = 0.5;\n                confidence[i] = 0;\n            }\n        }\n        \n        // Convert to robust weights\n        double min_score = 1e9, max_score = -1e9;\n        for (double s : scores) {\n            min_score = min(min_score, s);\n            max_score = max(max_score, s);\n        }\n        \n        double score_range = max_score - min_score;\n        if (score_range > 1e-9) {\n            for (int i = 0; i < N; i++) {\n                double conf_factor = min(1.0, (double)confidence[i] / max(1.0, 8.0));\n                double normalized_score = (scores[i] - min_score) / score_range;\n                weight_estimates[i] = normalized_score * conf_factor + 0.5 * (1.0 - conf_factor);\n                weight_estimates[i] = max(0.001, weight_estimates[i]);\n            }\n        } else {\n            for (int i = 0; i < N; i++) {\n                weight_estimates[i] = 0.5;\n            }\n        }\n        \n        // Focus on critical comparisons for final partitioning\n        int best_i = -1, best_j = -1;\n        double max_impact = -1;\n        \n        // Strategy: compare items that would most impact current partition quality\n        vector<pair<double, int>> sorted_items;\n        for (int i = 0; i < N; i++) {\n            sorted_items.push_back({weight_estimates[i], i});\n        }\n        sort(sorted_items.begin(), sorted_items.end());\n        \n        // Focus on middle-range items (most ambiguous for partitioning)\n        for (int i = N/3; i < 2*N/3; i++) {\n            for (int j = max(0, i-1); j < min(N, i+2); j++) {\n                if (i != j && comparison_count[sorted_items[i].second][sorted_items[j].second] < 2) {\n                    int idx1 = sorted_items[i].second;\n                    int idx2 = sorted_items[j].second;\n                    \n                    // Impact based on similar weights and partition sensitivity\n                    double weight_diff = abs(weight_estimates[idx1] - weight_estimates[idx2]);\n                    double impact = 1.0 / (0.001 + weight_diff) / (1.0 + comparison_count[idx1][idx2]);\n                    \n                    if (impact > max_impact) {\n                        max_impact = impact;\n                        best_i = idx1;\n                        best_j = idx2;\n                    }\n                }\n            }\n        }\n        \n        // Fallback if no high-impact comparison found\n        if (best_i == -1 || best_j == -1) {\n            best_i = rand() % N;\n            do {\n                best_j = rand() % N;\n            } while (best_j == best_i);\n        }\n        \n        // Ensure consistent ordering\n        if (best_i > best_j) swap(best_i, best_j);\n        \n        cout << \"1 1 \" << best_i << \" \" << best_j << endl;\n        cout.flush();\n        \n        string response;\n        cin >> response;\n        \n        comparison_count[best_i][best_j]++;\n        comparison_count[best_j][best_i]++;\n        \n        if (response == \">\") {\n            win_count[best_i][best_j]++;\n        } else if (response == \"<\") {\n            win_count[best_j][best_i]++;\n        }\n        \n        queries_used++;\n    }\n    \n    // Final comprehensive weight estimation with outlier robustness\n    vector<double> final_scores(N, 0.0);\n    vector<int> final_confidence(N, 0);\n    \n    for (int i = 0; i < N; i++) {\n        double total_score = 0.0;\n        int total_comparisons = 0;\n        vector<double> individual_scores;\n        \n        for (int j = 0; j < N; j++) {\n            if (i != j && comparison_count[i][j] > 0) {\n                double win_rate = (double)win_count[i][j] / comparison_count[i][j];\n                double score_contribution = win_rate * comparison_count[i][j];\n                total_score += score_contribution;\n                total_comparisons += comparison_count[i][j];\n                individual_scores.push_back(win_rate);\n            }\n        }\n        \n        if (total_comparisons > 0) {\n            final_scores[i] = total_score / total_comparisons;\n            final_confidence[i] = total_comparisons;\n        } else {\n            final_scores[i] = 0.5;\n            final_confidence[i] = 0;\n        }\n    }\n    \n    // Convert to final robust weights\n    double min_final_score = 1e9, max_final_score = -1e9;\n    for (double s : final_scores) {\n        min_final_score = min(min_final_score, s);\n        max_final_score = max(max_final_score, s);\n    }\n    \n    double final_score_range = max_final_score - min_final_score;\n    if (final_score_range > 1e-9) {\n        for (int i = 0; i < N; i++) {\n            // Robust estimation with confidence weighting\n            double confidence_factor = min(1.0, (double)final_confidence[i] / max(1.0, 10.0));\n            double normalized = (final_scores[i] - min_final_score) / final_score_range;\n            // Blend with median-like value for robustness\n            weight_estimates[i] = normalized * confidence_factor + 0.5 * (1.0 - confidence_factor);\n            weight_estimates[i] = max(0.001, weight_estimates[i]);\n        }\n    } else {\n        for (int i = 0; i < N; i++) {\n            weight_estimates[i] = 0.5;\n        }\n    }\n    \n    // Normalize weights\n    double sum_weights = 0;\n    for (double w : weight_estimates) sum_weights += w;\n    if (sum_weights > 1e-9) {\n        for (double& w : weight_estimates) w = w * N / sum_weights;\n    }\n    \n    // Multi-strategy partitioning with robust selection\n    vector<int> best_assignment(N);\n    double best_objective = 1e18;\n    \n    // Try different initialization strategies\n    for (int strategy = 0; strategy < 4; strategy++) {\n        vector<int> current_assignment(N, 0);\n        vector<double> group_sums(D, 0);\n        \n        // Different item ordering strategies\n        vector<pair<double, int>> items_ordered;\n        for (int i = 0; i < N; i++) {\n            items_ordered.push_back({weight_estimates[i], i});\n        }\n        \n        if (strategy == 0) {\n            // Heaviest first (standard greedy)\n            sort(items_ordered.begin(), items_ordered.end(), greater<pair<double, int>>());\n        } else if (strategy == 1) {\n            // Lightest first\n            sort(items_ordered.begin(), items_ordered.end());\n        } else if (strategy == 2) {\n            // Balanced approach: sort by weight then alternate assignment order\n            sort(items_ordered.begin(), items_ordered.end(), greater<pair<double, int>>());\n        } else {\n            // Random order for diversity\n            random_shuffle(items_ordered.begin(), items_ordered.end());\n        }\n        \n        // Reset group sums\n        fill(group_sums.begin(), group_sums.end(), 0);\n        \n        // Assignment with cyclic group selection for better balance\n        int group_offset = strategy % D;\n        for (int idx = 0; idx < N; idx++) {\n            const auto& [weight, item_idx] = items_ordered[idx];\n            \n            // For first few items, use standard greedy\n            // For later items, consider more options\n            int best_group = 0;\n            if (idx < min(5, N/2)) {\n                // Early items: standard greedy\n                for (int g = 1; g < D; g++) {\n                    if (group_sums[g] < group_sums[best_group]) {\n                        best_group = g;\n                    }\n                }\n            } else {\n                // Later items: try to balance better\n                for (int g = 0; g < D; g++) {\n                    if (group_sums[g] < group_sums[best_group]) {\n                        best_group = g;\n                    }\n                }\n            }\n            \n            current_assignment[item_idx] = best_group;\n            group_sums[best_group] += weight;\n        }\n        \n        // Calculate objective function (variance)\n        double mean = 0;\n        for (int g = 0; g < D; g++) mean += group_sums[g];\n        mean /= D;\n        \n        double variance = 0;\n        for (int g = 0; g < D; g++) {\n            variance += (group_sums[g] - mean) * (group_sums[g] - mean);\n        }\n        variance /= D;\n        \n        if (variance < best_objective) {\n            best_objective = variance;\n            best_assignment = current_assignment;\n        }\n    }\n    \n    // Use best assignment as starting point\n    vector<int> assignment = best_assignment;\n    \n    // Enhanced local search with multiple neighborhood structures\n    bool improved = true;\n    int iterations = 0;\n    const int max_iterations = 30;\n    \n    while (improved && iterations < max_iterations) {\n        improved = false;\n        iterations++;\n        \n        // Single item moves\n        for (int i = 0; i < N && !improved; i++) {\n            int current_group = assignment[i];\n            double item_weight = weight_estimates[i];\n            \n            // Try moving to other groups\n            for (int new_group = 0; new_group < D && !improved; new_group++) {\n                if (new_group != current_group) {\n                    // Calculate current state\n                    vector<double> group_sums(D, 0);\n                    for (int j = 0; j < N; j++) {\n                        group_sums[assignment[j]] += weight_estimates[j];\n                    }\n                    \n                    // Calculate variance before\n                    double mean_before = 0;\n                    for (int g = 0; g < D; g++) mean_before += group_sums[g];\n                    mean_before /= D;\n                    \n                    double variance_before = 0;\n                    for (int g = 0; g < D; g++) {\n                        variance_before += (group_sums[g] - mean_before) * (group_sums[g] - mean_before);\n                    }\n                    variance_before /= D;\n                    \n                    // Calculate variance after move\n                    vector<double> temp_sums = group_sums;\n                    temp_sums[current_group] -= item_weight;\n                    temp_sums[new_group] += item_weight;\n                    \n                    double mean_after = 0;\n                    for (int g = 0; g < D; g++) mean_after += temp_sums[g];\n                    mean_after /= D;\n                    \n                    double variance_after = 0;\n                    for (int g = 0; g < D; g++) {\n                        variance_after += (temp_sums[g] - mean_after) * (temp_sums[g] - mean_after);\n                    }\n                    variance_after /= D;\n                    \n                    // If significant improvement, make the move\n                    double improvement = variance_before - variance_after;\n                    if (improvement > 1e-12) {\n                        assignment[i] = new_group;\n                        improved = true;\n                    }\n                }\n            }\n        }\n        \n        // Pairwise swaps (only if single moves didn't help in later iterations)\n        if (!improved && iterations > 10) {\n            for (int i = 0; i < N && !improved; i++) {\n                for (int j = i + 1; j < N && !improved; j++) {\n                    int group_i = assignment[i];\n                    int group_j = assignment[j];\n                    \n                    // Only consider if in different groups\n                    if (group_i != group_j) {\n                        double weight_i = weight_estimates[i];\n                        double weight_j = weight_estimates[j];\n                        \n                        // Calculate current state\n                        vector<double> group_sums(D, 0);\n                        for (int k = 0; k < N; k++) {\n                            group_sums[assignment[k]] += weight_estimates[k];\n                        }\n                        \n                        // Calculate variance before\n                        double mean_before = 0;\n                        for (int g = 0; g < D; g++) mean_before += group_sums[g];\n                        mean_before /= D;\n                        \n                        double variance_before = 0;\n                        for (int g = 0; g < D; g++) {\n                            variance_before += (group_sums[g] - mean_before) * (group_sums[g] - mean_before);\n                        }\n                        variance_before /= D;\n                        \n                        // Calculate variance after swap\n                        vector<double> temp_sums = group_sums;\n                        temp_sums[group_i] = group_sums[group_i] - weight_i + weight_j;\n                        temp_sums[group_j] = group_sums[group_j] - weight_j + weight_i;\n                        \n                        double mean_after = 0;\n                        for (int g = 0; g < D; g++) mean_after += temp_sums[g];\n                        mean_after /= D;\n                        \n                        double variance_after = 0;\n                        for (int g = 0; g < D; g++) {\n                            variance_after += (temp_sums[g] - mean_after) * (temp_sums[g] - mean_after);\n                        }\n                        variance_after /= D;\n                        \n                        double improvement = variance_before - variance_after;\n                        if (improvement > 1e-10) {\n                            assignment[i] = group_j;\n                            assignment[j] = group_i;\n                            improved = true;\n                        }\n                    }\n                }\n            }\n        }\n    }\n    \n    // Final validation to ensure correct format\n    for (int i = 0; i < N; i++) {\n        if (assignment[i] < 0 || assignment[i] >= D) {\n            assignment[i] = 0;\n        }\n    }\n    \n    // Output final assignment\n    for (int i = 0; i < N; i++) {\n        cout << assignment[i];\n        if (i < N - 1) cout << \" \";\n    }\n    cout << endl;\n}\n\nint main() {\n    solve();\n    return 0;\n}","ahc026":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 200;\nconst int M = 10;\nconst int STACK_SIZE = 20;\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    int n, m;\n    cin >> n >> m;\n    \n    vector<deque<int>> stacks(M);\n    vector<int> box_stack(N + 1);      // which stack each box is in\n    vector<int> box_position(N + 1);   // position in stack (0 = bottom)\n    \n    // Read initial configuration\n    for (int i = 0; i < m; i++) {\n        for (int j = 0; j < STACK_SIZE; j++) {\n            int box;\n            cin >> box;\n            stacks[i].push_back(box);\n            box_stack[box] = i;\n            box_position[box] = j;\n        }\n    }\n    \n    vector<pair<int, int>> operations;\n    \n    // Process boxes in order from 1 to 200\n    for (int target = 1; target <= n; target++) {\n        int stack_idx = box_stack[target];\n        int pos_in_stack = box_position[target];\n        int stack_size = stacks[stack_idx].size();\n        \n        // If box is not at the top, we need to move boxes above it\n        if (pos_in_stack != stack_size - 1) {\n            // Determine which boxes need to be moved\n            int boxes_above = stack_size - 1 - pos_in_stack;\n            \n            // Find a destination stack (prefer one that's not full)\n            int dest_stack = -1;\n            for (int s = 0; s < m; s++) {\n                if (s != stack_idx && (int)stacks[s].size() < STACK_SIZE) {\n                    dest_stack = s;\n                    break;\n                }\n            }\n            \n            // If all stacks are full, pick any other stack\n            if (dest_stack == -1) {\n                dest_stack = (stack_idx + 1) % m;\n            }\n            \n            // Move operation: move box 'target' and all above it to dest_stack\n            operations.push_back({target, dest_stack + 1}); // 1-indexed output\n            \n            // Actually perform the move\n            vector<int> moved_boxes;\n            for (int i = pos_in_stack; i < stack_size; i++) {\n                moved_boxes.push_back(stacks[stack_idx][i]);\n            }\n            \n            // Remove from source stack\n            stacks[stack_idx].erase(\n                stacks[stack_idx].begin() + pos_in_stack, \n                stacks[stack_idx].end()\n            );\n            \n            // Add to destination stack\n            for (int box : moved_boxes) {\n                stacks[dest_stack].push_back(box);\n            }\n            \n            // Update positions for all moved boxes\n            for (int i = 0; i < (int)stacks[dest_stack].size(); i++) {\n                box_stack[stacks[dest_stack][i]] = dest_stack;\n                box_position[stacks[dest_stack][i]] = i;\n            }\n        }\n        \n        // Now the target box should be at the top of its stack\n        // Remove it\n        int final_stack = box_stack[target];\n        stacks[final_stack].pop_back();\n        box_stack[target] = -1;\n        operations.push_back({target, 0});\n        \n        // Update positions of remaining boxes in the stack\n        for (int i = 0; i < (int)stacks[final_stack].size(); i++) {\n            box_position[stacks[final_stack][i]] = i;\n        }\n    }\n    \n    // Output operations\n    for (const auto& op : operations) {\n        cout << op.first << \" \" << op.second << \"\\n\";\n    }\n    \n    return 0;\n}","ahc027":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MAXN = 45;\nint N;\nchar h[MAXN][MAXN], v[MAXN][MAXN];\nint d[MAXN][MAXN];\nbool visited[MAXN][MAXN];\nvector<pair<int, int>> moves = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; // R, D, L, U\nstring directions = \"RDLU\";\n\n// Check if we can move from (x,y) to (nx,ny)\nbool can_move(int x, int y, int nx, int ny) {\n    if (nx < 0 || nx >= N || ny < 0 || ny >= N) return false;\n    \n    // Determine direction\n    int dx = nx - x, dy = ny - y;\n    if (dx == 1 && dy == 0) { // Down\n        return (x < N-1) && (h[x][y] == '0');\n    } else if (dx == -1 && dy == 0) { // Up\n        return (x > 0) && (h[x-1][y] == '0');\n    } else if (dx == 0 && dy == 1) { // Right\n        return (y < N-1) && (v[x][y] == '0');\n    } else if (dx == 0 && dy == -1) { // Left\n        return (y > 0) && (v[x][y-1] == '0');\n    }\n    return false;\n}\n\n// Enhanced solution with better priority calculation\nstring enhanced_solution() {\n    memset(visited, 0, sizeof(visited));\n    string result = \"\";\n    \n    function<void(int, int)> dfs = [&](int x, int y) {\n        visited[x][y] = true;\n        \n        // Create list of valid neighbors\n        vector<pair<int, pair<int, int>>> neighbors; // priority, (nx, ny)\n        \n        for (int dir = 0; dir < 4; dir++) {\n            int nx = x + moves[dir].first;\n            int ny = y + moves[dir].second;\n            \n            if (nx >= 0 && nx < N && ny >= 0 && ny < N && !visited[nx][ny]) {\n                if (can_move(x, y, nx, ny)) {\n                    neighbors.push_back(make_pair(d[nx][ny], make_pair(nx, ny)));\n                }\n            }\n        }\n        \n        // Sort neighbors with enhanced priority calculation\n        sort(neighbors.begin(), neighbors.end(), [&](const pair<int, pair<int, int>>& a, const pair<int, pair<int, int>>& b) {\n            int a_val = a.first;\n            int b_val = b.first;\n            \n            auto a_pos = a.second;\n            auto b_pos = b.second;\n            int ax = a_pos.first, ay = a_pos.second;\n            int bx = b_pos.first, by = b_pos.second;\n            \n            // Bonus for edge positions (they're often high value and isolated)\n            bool a_is_edge = (ax == 0 || ax == N-1 || ay == 0 || ay == N-1);\n            bool b_is_edge = (bx == 0 || bx == N-1 || by == 0 || by == N-1);\n            \n            if (a_is_edge) a_val += 200;\n            if (b_is_edge) b_val += 200;\n            \n            // Bonus for corner-adjacent positions\n            int a_dist_to_edge = min({ax, N-1-ax, ay, N-1-ay});\n            int b_dist_to_edge = min({bx, N-1-bx, by, N-1-by});\n            \n            if (a_dist_to_edge <= 1) a_val += 100;\n            if (b_dist_to_edge <= 1) b_val += 100;\n            \n            return a_val > b_val;\n        });\n        \n        for (const auto& neighbor : neighbors) {\n            int nx = neighbor.second.first;\n            int ny = neighbor.second.second;\n            \n            // Find the direction\n            int dir = -1;\n            for (int i = 0; i < 4; i++) {\n                if (x + moves[i].first == nx && y + moves[i].second == ny) {\n                    dir = i;\n                    break;\n                }\n            }\n            \n            if (dir != -1) {\n                result += directions[dir];\n                dfs(nx, ny);\n                // Add return move\n                if (dir == 0) result += 'L';\n                else if (dir == 1) result += 'U';\n                else if (dir == 2) result += 'R';\n                else result += 'D';\n            }\n        }\n    };\n    \n    dfs(0, 0);\n    \n    // Ensure we don't exceed limit\n    if (result.length() > 100000) {\n        result = result.substr(0, 100000);\n    }\n    \n    return result;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    cin >> N;\n    \n    // Read horizontal walls\n    for (int i = 0; i < N-1; i++) {\n        string line;\n        cin >> line;\n        for (int j = 0; j < N; j++) {\n            h[i][j] = line[j];\n        }\n    }\n    \n    // Read vertical walls  \n    for (int i = 0; i < N; i++) {\n        string line;\n        cin >> line;\n        for (int j = 0; j < N-1; j++) {\n            v[i][j] = line[j];\n        }\n    }\n    \n    // Read dirtiness values\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            cin >> d[i][j];\n        }\n    }\n    \n    // Use the enhanced solution\n    string result = enhanced_solution();\n    \n    cout << result << endl;\n    \n    return 0;\n}","ahc028":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 15;\nconst int M = 200;\nconst int MAX_OPS = 5000;\n\nint grid_size;\nint target_count;\nint start_row, start_col;\nchar grid[N][N];\nvector<string> targets;\nvector<pair<int,int>> char_positions[26];\n\n// Precompute positions for each character\nvoid precompute_positions() {\n    for (int i = 0; i < 26; i++) {\n        char_positions[i].clear();\n    }\n    \n    for (int i = 0; i < grid_size; i++) {\n        for (int j = 0; j < grid_size; j++) {\n            int char_idx = grid[i][j] - 'A';\n            char_positions[char_idx].push_back({i, j});\n        }\n    }\n}\n\n// Find the best position considering local context\npair<int, int> find_optimal_position(char c, int cur_row, int cur_col, char next_c = 0) {\n    int char_idx = c - 'A';\n    pair<int, int> best_pos = char_positions[char_idx][0];\n    int best_score = abs(cur_row - best_pos.first) + abs(cur_col - best_pos.second);\n    \n    // If we have a next character, consider it in our decision\n    if (next_c != 0) {\n        int next_char_idx = next_c - 'A';\n        best_score = 1e9;\n        \n        for (auto& pos : char_positions[char_idx]) {\n            int move_cost = abs(cur_row - pos.first) + abs(cur_col - pos.second);\n            \n            // Find closest next character position from this position\n            int min_next_dist = 1e9;\n            for (auto& next_pos : char_positions[next_char_idx]) {\n                int dist = abs(pos.first - next_pos.first) + abs(pos.second - next_pos.second);\n                min_next_dist = min(min_next_dist, dist);\n            }\n            \n            int total_cost = move_cost + min_next_dist;\n            if (total_cost < best_score) {\n                best_score = total_cost;\n                best_pos = pos;\n            }\n        }\n    } else {\n        // Simple closest position\n        for (auto& pos : char_positions[char_idx]) {\n            int dist = abs(cur_row - pos.first) + abs(cur_col - pos.second);\n            if (dist < best_score) {\n                best_score = dist;\n                best_pos = pos;\n            }\n        }\n    }\n    \n    return best_pos;\n}\n\n// Build superstring using greedy overlap with better strategy\nstring build_efficient_superstring() {\n    vector<string> remaining = targets;\n    if (remaining.empty()) return \"\";\n    \n    string result = remaining[0];\n    remaining.erase(remaining.begin());\n    \n    while (!remaining.empty() && result.length() < 3000) {\n        int best_overlap = -1;\n        int best_idx = -1;\n        string best_extension = \"\";\n        \n        // Try to find string with maximum overlap\n        for (int i = 0; i < (int)remaining.size(); i++) {\n            const string& s = remaining[i];\n            \n            // Check for overlaps of length 1 to 4\n            for (int overlap = min(4, min((int)result.length(), (int)s.length())); overlap >= 1; overlap--) {\n                if (result.length() >= overlap && \n                    s.length() >= overlap &&\n                    result.substr(result.length() - overlap) == s.substr(0, overlap)) {\n                    string candidate = result + s.substr(overlap);\n                    if (overlap > best_overlap || \n                        (overlap == best_overlap && candidate.length() < best_extension.length())) {\n                        best_overlap = overlap;\n                        best_idx = i;\n                        best_extension = candidate;\n                    }\n                    break;\n                }\n            }\n        }\n        \n        if (best_idx != -1) {\n            result = best_extension;\n            remaining.erase(remaining.begin() + best_idx);\n        } else {\n            // No overlap found, append first available string\n            result += remaining[0];\n            remaining.erase(remaining.begin());\n        }\n    }\n    \n    return result;\n}\n\n// Generate optimized path\nvector<pair<int, int>> generate_optimized_path(const string& superstring) {\n    vector<pair<int, int>> path;\n    int cur_row = start_row;\n    int cur_col = start_col;\n    \n    for (int i = 0; i < (int)superstring.length() && path.size() < MAX_OPS; i++) {\n        char current_char = superstring[i];\n        char next_char = (i + 1 < (int)superstring.length()) ? superstring[i + 1] : 0;\n        \n        pair<int, int> next_pos = find_optimal_position(current_char, cur_row, cur_col, next_char);\n        path.push_back(next_pos);\n        cur_row = next_pos.first;\n        cur_col = next_pos.second;\n    }\n    \n    return path;\n}\n\n// Remove consecutive duplicate positions\nvector<pair<int, int>> compress_path(const vector<pair<int, int>>& path) {\n    if (path.empty()) return path;\n    \n    vector<pair<int, int>> compressed;\n    compressed.push_back(path[0]);\n    \n    for (int i = 1; i < (int)path.size(); i++) {\n        if (path[i] != compressed.back()) {\n            compressed.push_back(path[i]);\n        }\n    }\n    \n    // Ensure we don't exceed limit\n    if (compressed.size() > MAX_OPS) {\n        compressed.resize(MAX_OPS);\n    }\n    \n    return compressed;\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    // Read input\n    cin >> grid_size >> target_count;\n    cin >> start_row >> start_col;\n    \n    for (int i = 0; i < grid_size; i++) {\n        string line;\n        cin >> line;\n        for (int j = 0; j < grid_size; j++) {\n            grid[i][j] = line[j];\n        }\n    }\n    \n    targets.resize(target_count);\n    for (int i = 0; i < target_count; i++) {\n        cin >> targets[i];\n    }\n    \n    // Preprocessing\n    precompute_positions();\n    \n    // Build superstring\n    string superstring = build_efficient_superstring();\n    \n    // Generate and optimize path\n    vector<pair<int, int>> path = generate_optimized_path(superstring);\n    path = compress_path(path);\n    \n    // Output path\n    for (const auto& pos : path) {\n        cout << pos.first << \" \" << pos.second << \"\\n\";\n    }\n    \n    return 0;\n}","ahc030":"#include <bits/stdc++.h>\n\nusing namespace std;\n\nstruct Point {\n    int i, j;\n    Point() : i(0), j(0) {}\n    Point(int _i, int _j) : i(_i), j(_j) {}\n    bool operator<(const Point& other) const {\n        if (i != other.i) return i < other.i;\n        return j < other.j;\n    }\n};\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    int N, M;\n    double epsilon;\n    \n    // Read initial parameters\n    if (!(cin >> N >> M >> epsilon)) {\n        return 1;\n    }\n    \n    // Read polyomino data (skip it since we don't need it for basic approach)\n    for (int i = 0; i < M; i++) {\n        int d;\n        if (!(cin >> d)) return 1;\n        for (int j = 0; j < 2 * d; j++) {\n            int dummy;\n            if (!(cin >> dummy)) return 1;\n        }\n    }\n    \n    // Track which squares we know have oil\n    vector<vector<bool>> has_oil(N, vector<bool>(N, false));\n    vector<vector<bool>> drilled(N, vector<bool>(N, false));\n    set<Point> oil_squares;\n    \n    // Simple strategy: drill all squares\n    // This guarantees we find all oil squares but might not be optimal cost-wise\n    int operations = 0;\n    const int max_operations = 2 * N * N;\n    \n    for (int i = 0; i < N && operations < max_operations - N*N/2; i++) {\n        for (int j = 0; j < N && operations < max_operations - N*N/2; j++) {\n            // Query this square\n            cout << \"q 1 \" << i << \" \" << j << endl;\n            cout.flush();\n            operations++;\n            \n            int response;\n            if (!(cin >> response)) {\n                return 1;\n            }\n            \n            drilled[i][j] = true;\n            if (response > 0) {\n                has_oil[i][j] = true;\n                oil_squares.insert(Point(i, j));\n            }\n        }\n    }\n    \n    // Submit our answer\n    cout << \"a \" << oil_squares.size();\n    for (const auto& p : oil_squares) {\n        cout << \" \" << p.i << \" \" << p.j;\n    }\n    cout << endl;\n    cout.flush();\n    \n    // Read the response (should be 1 for correct answer)\n    int result;\n    if (cin >> result) {\n        // We don't need to do anything with the result since we're done\n    }\n    \n    return 0;\n}","ahc031":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int W = 1000;\n\nstruct Rectangle {\n    int i0, j0, i1, j1;\n    Rectangle() : i0(0), j0(0), i1(0), j1(0) {}\n    Rectangle(int _i0, int _j0, int _i1, int _j1) : i0(_i0), j0(_j0), i1(_i1), j1(_j1) {}\n    int area() const { return (i1 - i0) * (j1 - j0); }\n    bool valid() const { \n        return i0 >= 0 && j0 >= 0 && i1 <= W && j1 <= W && i0 < i1 && j0 < j1; \n    }\n};\n\n// Ultra-conservative horizontal strip packing\nvector<Rectangle> pack_rectangles_simple(const vector<int>& required) {\n    vector<Rectangle> rectangles(required.size());\n    \n    int n = (int)required.size();\n    // Give each rectangle plenty of vertical space to avoid any overlap\n    int strip_height = max(20, W / max(1, n)); // At least 20 pixels per strip\n    \n    for (int i = 0; i < n; ++i) {\n        int req_area = required[i];\n        \n        // Calculate vertical boundaries for this strip\n        int strip_start = i * strip_height;\n        int strip_end = min(W, strip_start + strip_height - 2); // Leave gap to ensure no overlap\n        \n        // Make sure we have valid boundaries\n        strip_start = max(0, strip_start);\n        strip_end = max(strip_start + 1, min(W, strip_end));\n        \n        // Calculate minimum width needed\n        int height_available = max(1, strip_end - strip_start);\n        int min_width = (req_area + height_available - 1) / height_available;\n        int width = min(W, max(1, min_width));\n        \n        // Place rectangle in the middle of the strip to be safe\n        int rect_top = strip_start + max(0, (strip_end - strip_start - height_available) / 2);\n        int rect_bottom = min(W, rect_top + height_available);\n        int rect_left = 0;\n        int rect_right = min(W, width);\n        \n        // Ensure valid rectangle\n        if (rect_top >= rect_bottom) {\n            rect_top = max(0, strip_start);\n            rect_bottom = min(W, rect_top + 10);\n        }\n        if (rect_left >= rect_right) {\n            rect_left = 0;\n            rect_right = min(W, max(1, width));\n        }\n        \n        rectangles[i] = Rectangle(rect_top, rect_left, rect_bottom, rect_right);\n    }\n    \n    return rectangles;\n}\n\n// Simple optimization that tries not to break the non-overlap guarantee\nvector<Rectangle> optimize_rectangles(vector<Rectangle> rectangles, const vector<int>& required) {\n    for (int i = 0; i < (int)rectangles.size(); ++i) {\n        int current_area = rectangles[i].area();\n        int required_area = required[i];\n        \n        if (current_area < required_area) {\n            // Only expand within the allocated strip space\n            int strip_height = rectangles[i].i1 - rectangles[i].i0;\n            int current_width = rectangles[i].j1 - rectangles[i].j0;\n            \n            // Calculate required width\n            int required_width = (required_area + strip_height - 1) / strip_height;\n            \n            // Expand width if possible\n            if (required_width > current_width && rectangles[i].j1 < W) {\n                int expand_by = min(W - rectangles[i].j1, required_width - current_width);\n                rectangles[i].j1 += expand_by;\n            }\n        }\n    }\n    return rectangles;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int W_input, D, N;\n    cin >> W_input >> D >> N;\n    \n    vector<vector<int>> required(D, vector<int>(N));\n    for (int d = 0; d < D; ++d) {\n        for (int k = 0; k < N; ++k) {\n            cin >> required[d][k];\n        }\n    }\n    \n    vector<vector<Rectangle>> solutions(D);\n    \n    // Day 0: Use conservative strip packing\n    solutions[0] = pack_rectangles_simple(required[0]);\n    \n    // Days 1 to D-1: Keep same layout, just optimize for area\n    for (int d = 1; d < D; ++d) {\n        solutions[d] = solutions[d-1];\n        solutions[d] = optimize_rectangles(solutions[d], required[d]);\n        \n        // If optimization didn't work, generate fresh solution\n        bool needs_fresh = false;\n        for (int k = 0; k < N; ++k) {\n            if (solutions[d][k].area() < required[d][k]) {\n                needs_fresh = true;\n                break;\n            }\n        }\n        \n        if (needs_fresh) {\n            solutions[d] = pack_rectangles_simple(required[d]);\n        }\n    }\n    \n    // Output results\n    for (int d = 0; d < D; ++d) {\n        for (int k = 0; k < N; ++k) {\n            cout << solutions[d][k].i0 << \" \" << solutions[d][k].j0 << \" \" \n                 << solutions[d][k].i1 << \" \" << solutions[d][k].j1 << \"\\n\";\n        }\n    }\n    \n    return 0;\n}","ahc032":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 9;\nconst int M = 20;\nconst int K = 81;\nconst long long MOD = 998244353;\n\nlong long a[N][N];\nlong long s[M][3][3];\n\nstruct Operation {\n    int stamp, p, q;\n};\n\n// Calculate current score of board\nlong long calculate_score(long long board[N][N]) {\n    long long score = 0;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            score = (score + (board[i][j] % MOD + MOD) % MOD) % MOD;\n        }\n    }\n    return score;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    // Read input\n    int dummy_N, dummy_M, dummy_K;\n    cin >> dummy_N >> dummy_M >> dummy_K;\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    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 board\n    long long board[N][N];\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            board[i][j] = a[i][j];\n        }\n    }\n    \n    vector<Operation> operations;\n    \n    // Greedy selection\n    for (int k = 0; k < K; k++) {\n        Operation best_op = {-1, -1, -1};\n        long long best_gain = -1;\n        long long current_score = calculate_score(board);\n        \n        // Try all possible stamp placements\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                    // Create temporary board\n                    long long temp_board[N][N];\n                    for (int i = 0; i < N; i++) {\n                        for (int j = 0; j < N; j++) {\n                            temp_board[i][j] = board[i][j];\n                        }\n                    }\n                    \n                    // Apply stamp\n                    for (int i = 0; i < 3; i++) {\n                        for (int j = 0; j < 3; j++) {\n                            temp_board[p + i][q + j] += s[m][i][j];\n                        }\n                    }\n                    \n                    long long new_score = calculate_score(temp_board);\n                    long long gain = new_score - current_score;\n                    \n                    if (gain > best_gain) {\n                        best_gain = gain;\n                        best_op = {m, p, q};\n                    }\n                }\n            }\n        }\n        \n        // If no positive gain, stop\n        if (best_gain <= 0) {\n            break;\n        }\n        \n        // Apply the best operation\n        operations.push_back(best_op);\n        \n        for (int i = 0; i < 3; i++) {\n            for (int j = 0; j < 3; j++) {\n                board[best_op.p + i][best_op.q + j] += s[best_op.stamp][i][j];\n            }\n        }\n    }\n    \n    // Output\n    cout << operations.size() << \"\\n\";\n    for (const auto& op : operations) {\n        cout << op.stamp << \" \" << op.p << \" \" << op.q << \"\\n\";\n    }\n    \n    return 0;\n}","ahc033":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 5;\nconst int MAX_TURNS = 10000;\n\n// Container information\nint A[N][N];\nint container_to_gate[N*N]; // which gate each container should go to\n\n// Crane positions and states\nstruct Crane {\n    int row, col;\n    bool holding;\n    int container;\n    bool bombed;\n};\n\n// Grid state\nint grid[N][N]; // -1 = empty, >= 0 = container number\nCrane cranes[N];\n\n// Output actions\nvector<string> actions(N);\n\n// Initialize problem\nvoid init() {\n    // Calculate which gate each container should go to\n    for (int i = 0; i < N*N; i++) {\n        container_to_gate[i] = i / N;\n    }\n    \n    // Initialize grid\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            grid[i][j] = -1;\n        }\n    }\n    \n    // Initialize cranes\n    for (int i = 0; i < N; i++) {\n        cranes[i] = {i, 0, false, -1, false};\n    }\n}\n\n// Check if a position is occupied by another crane\nbool is_position_occupied(int row, int col, int exclude_crane) {\n    for (int i = 0; i < N; i++) {\n        if (i != exclude_crane && !cranes[i].bombed && cranes[i].row == row && cranes[i].col == col) {\n            return true;\n        }\n    }\n    return false;\n}\n\n// Main simulation logic\nvoid simulate() {\n    int turn = 0;\n    int received[N] = {0}; // Count of containers received by each gate\n    bool dispatched[N*N] = {false}; // Track which containers have been dispatched\n    \n    // Place initial containers\n    for (int i = 0; i < N; i++) {\n        if (received[i] < N && grid[i][0] == -1) {\n            grid[i][0] = A[i][received[i]];\n            received[i]++;\n        }\n    }\n    \n    while (turn < MAX_TURNS) {\n        // Check if all containers are dispatched\n        bool all_done = true;\n        for (int i = 0; i < N*N; i++) {\n            if (!dispatched[i]) {\n                all_done = false;\n                break;\n            }\n        }\n        if (all_done) break;\n        \n        // Step 1: Place new containers at receiving gates\n        for (int i = 0; i < N; i++) {\n            if (received[i] < N && grid[i][0] == -1) {\n                grid[i][0] = A[i][received[i]];\n                received[i]++;\n            }\n        }\n        \n        // Step 2: Plan crane actions\n        char turn_actions[N];\n        for (int i = 0; i < N; i++) {\n            turn_actions[i] = '.';\n        }\n        \n        // Calculate next positions for collision detection\n        int next_positions[N][2]; // [row, col] for each crane\n        bool position_valid[N];   // whether the move is valid\n        \n        // Initialize next positions to current positions\n        for (int i = 0; i < N; i++) {\n            if (cranes[i].bombed) {\n                position_valid[i] = false;\n            } else {\n                next_positions[i][0] = cranes[i].row;\n                next_positions[i][1] = cranes[i].col;\n                position_valid[i] = true;\n            }\n        }\n        \n        // Plan actions and next positions\n        for (int crane_id = 0; crane_id < N; crane_id++) {\n            if (cranes[crane_id].bombed) {\n                turn_actions[crane_id] = '.';\n                position_valid[crane_id] = false;\n                continue;\n            }\n            \n            Crane& crane = cranes[crane_id];\n            \n            // If at dispatch gate, check for automatic dispatch\n            if (crane.col == N-1 && !crane.holding && grid[crane.row][crane.col] != -1) {\n                int container = grid[crane.row][crane.col];\n                if (container / N == crane.row) {\n                    dispatched[container] = true;\n                    grid[crane.row][crane.col] = -1;\n                }\n            }\n            \n            // Simple strategy: prioritize dispatching containers that are ready\n            if (crane.holding) {\n                int target_gate = crane.container / N;\n                \n                // If at dispatch gate\n                if (crane.col == N-1) {\n                    // If correct gate, place container\n                    if (crane.row == target_gate) {\n                        // Check if square is empty\n                        if (grid[crane.row][crane.col] == -1) {\n                            turn_actions[crane_id] = 'Q';\n                            position_valid[crane_id] = true; // Q doesn't change position\n                        } else {\n                            turn_actions[crane_id] = '.';\n                            position_valid[crane_id] = true;\n                        }\n                    } else {\n                        // Wrong gate, move vertically if possible\n                        if (crane.row < target_gate) {\n                            if (crane.row + 1 < N) {\n                                next_positions[crane_id][0] = crane.row + 1;\n                                next_positions[crane_id][1] = crane.col;\n                                // Check if destination valid\n                                if (!is_position_occupied(crane.row + 1, crane.col, crane_id)) {\n                                    if (crane_id == 0 || grid[crane.row+1][crane.col] == -1) {\n                                        turn_actions[crane_id] = 'D';\n                                        position_valid[crane_id] = true;\n                                    } else {\n                                        turn_actions[crane_id] = '.';\n                                        next_positions[crane_id][0] = crane.row;\n                                        position_valid[crane_id] = true;\n                                    }\n                                } else {\n                                    turn_actions[crane_id] = '.';\n                                    next_positions[crane_id][0] = crane.row;\n                                    position_valid[crane_id] = true;\n                                }\n                            } else {\n                                turn_actions[crane_id] = '.';\n                                position_valid[crane_id] = true;\n                            }\n                        } else if (crane.row > target_gate) {\n                            if (crane.row - 1 >= 0) {\n                                next_positions[crane_id][0] = crane.row - 1;\n                                next_positions[crane_id][1] = crane.col;\n                                // Check if destination valid\n                                if (!is_position_occupied(crane.row - 1, crane.col, crane_id)) {\n                                    if (crane_id == 0 || grid[crane.row-1][crane.col] == -1) {\n                                        turn_actions[crane_id] = 'U';\n                                        position_valid[crane_id] = true;\n                                    } else {\n                                        turn_actions[crane_id] = '.';\n                                        next_positions[crane_id][0] = crane.row;\n                                        position_valid[crane_id] = true;\n                                    }\n                                } else {\n                                    turn_actions[crane_id] = '.';\n                                    next_positions[crane_id][0] = crane.row;\n                                    position_valid[crane_id] = true;\n                                }\n                            } else {\n                                turn_actions[crane_id] = '.';\n                                position_valid[crane_id] = true;\n                            }\n                        } else {\n                            turn_actions[crane_id] = '.';\n                            position_valid[crane_id] = true;\n                        }\n                    }\n                } else {\n                    // Move right toward dispatch gate\n                    if (crane.col + 1 < N) {\n                        next_positions[crane_id][0] = crane.row;\n                        next_positions[crane_id][1] = crane.col + 1;\n                        // Check if destination valid\n                        if (!is_position_occupied(crane.row, crane.col + 1, crane_id)) {\n                            if (crane_id == 0) {\n                                // Large crane can move through occupied squares\n                                turn_actions[crane_id] = 'R';\n                                position_valid[crane_id] = true;\n                            } else {\n                                // Small crane needs clear path\n                                if (grid[crane.row][crane.col+1] == -1) {\n                                    turn_actions[crane_id] = 'R';\n                                    position_valid[crane_id] = true;\n                                } else {\n                                    turn_actions[crane_id] = '.';\n                                    next_positions[crane_id][1] = crane.col;\n                                    position_valid[crane_id] = true;\n                                }\n                            }\n                        } else {\n                            turn_actions[crane_id] = '.';\n                            next_positions[crane_id][1] = crane.col;\n                            position_valid[crane_id] = true;\n                        }\n                    } else {\n                        turn_actions[crane_id] = '.';\n                        position_valid[crane_id] = true;\n                    }\n                }\n            } else {\n                // Not holding container\n                // Check if container at current position\n                if (grid[crane.row][crane.col] != -1) {\n                    int container = grid[crane.row][crane.col];\n                    turn_actions[crane_id] = 'P';\n                    position_valid[crane_id] = true; // P doesn't change position\n                } else {\n                    // Move to find work - move right if possible\n                    if (crane.col + 1 < N) {\n                        next_positions[crane_id][0] = crane.row;\n                        next_positions[crane_id][1] = crane.col + 1;\n                        // Check if destination valid\n                        if (!is_position_occupied(crane.row, crane.col + 1, crane_id)) {\n                            if (crane_id == 0) {\n                                // Large crane can move through occupied squares\n                                turn_actions[crane_id] = 'R';\n                                position_valid[crane_id] = true;\n                            } else {\n                                // Small crane needs clear path\n                                if (grid[crane.row][crane.col+1] == -1) {\n                                    turn_actions[crane_id] = 'R';\n                                    position_valid[crane_id] = true;\n                                } else {\n                                    turn_actions[crane_id] = '.';\n                                    next_positions[crane_id][1] = crane.col;\n                                    position_valid[crane_id] = true;\n                                }\n                            }\n                        } else {\n                            turn_actions[crane_id] = '.';\n                            next_positions[crane_id][1] = crane.col;\n                            position_valid[crane_id] = true;\n                        }\n                    } else if (crane.col == N-1 && grid[crane.row][crane.col] != -1) {\n                        // At dispatch gate with container\n                        int container = grid[crane.row][crane.col];\n                        if (container / N != crane.row) {\n                            // Wrong container, pick it up\n                            turn_actions[crane_id] = 'P';\n                            position_valid[crane_id] = true;\n                        } else {\n                            turn_actions[crane_id] = '.';\n                            position_valid[crane_id] = true;\n                        }\n                    } else {\n                        turn_actions[crane_id] = '.';\n                        position_valid[crane_id] = true;\n                    }\n                }\n            }\n        }\n        \n        // Check for position conflicts and resolve them\n        bool conflict_found = true;\n        while (conflict_found) {\n            conflict_found = false;\n            for (int i = 0; i < N; i++) {\n                if (!position_valid[i] || cranes[i].bombed) continue;\n                \n                for (int j = i + 1; j < N; j++) {\n                    if (!position_valid[j] || cranes[j].bombed) continue;\n                    \n                    // Check if both cranes want to go to the same position\n                    if (next_positions[i][0] == next_positions[j][0] && \n                        next_positions[i][1] == next_positions[j][1]) {\n                        // Conflict found - make one of them stay\n                        turn_actions[i] = '.';\n                        next_positions[i][0] = cranes[i].row;\n                        next_positions[i][1] = cranes[i].col;\n                        conflict_found = true;\n                        break;\n                    }\n                }\n                if (conflict_found) break;\n            }\n        }\n        \n        // Apply actions and update states\n        for (int i = 0; i < N; i++) {\n            actions[i] += turn_actions[i];\n            \n            if (turn_actions[i] == 'P') {\n                if (grid[cranes[i].row][cranes[i].col] != -1 && !cranes[i].holding) {\n                    cranes[i].holding = true;\n                    cranes[i].container = grid[cranes[i].row][cranes[i].col];\n                    grid[cranes[i].row][cranes[i].col] = -1;\n                }\n            } else if (turn_actions[i] == 'Q') {\n                if (cranes[i].holding && grid[cranes[i].row][cranes[i].col] == -1) {\n                    grid[cranes[i].row][cranes[i].col] = cranes[i].container;\n                    int container = cranes[i].container;\n                    cranes[i].holding = false;\n                    cranes[i].container = -1;\n                    // Check if this container should be dispatched\n                    if (cranes[i].col == N-1 && container / N == cranes[i].row) {\n                        dispatched[container] = true;\n                    }\n                }\n            } else if (turn_actions[i] == 'U') {\n                if (cranes[i].row > 0) {\n                    if (i == 0 || grid[cranes[i].row-1][cranes[i].col] == -1 || !cranes[i].holding) {\n                        cranes[i].row--;\n                    }\n                }\n            } else if (turn_actions[i] == 'D') {\n                if (cranes[i].row < N-1) {\n                    if (i == 0 || grid[cranes[i].row+1][cranes[i].col] == -1 || !cranes[i].holding) {\n                        cranes[i].row++;\n                    }\n                }\n            } else if (turn_actions[i] == 'L') {\n                if (cranes[i].col > 0) {\n                    if (i == 0 || grid[cranes[i].row][cranes[i].col-1] == -1 || !cranes[i].holding) {\n                        cranes[i].col--;\n                    }\n                }\n            } else if (turn_actions[i] == 'R') {\n                if (cranes[i].col < N-1) {\n                    if (i == 0 || grid[cranes[i].row][cranes[i].col+1] == -1 || !cranes[i].holding) {\n                        cranes[i].col++;\n                    }\n                }\n            }\n        }\n        \n        turn++;\n    }\n    \n    // Pad all action strings to same length\n    int max_len = 0;\n    for (int i = 0; i < N; i++) {\n        max_len = max(max_len, (int)actions[i].length());\n    }\n    \n    for (int i = 0; i < N; i++) {\n        while ((int)actions[i].length() < max_len) {\n            actions[i] += '.';\n        }\n    }\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    // Read input (N is fixed to 5)\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\n    init();\n    \n    // Run simulation\n    simulate();\n    \n    // Output results\n    for (int i = 0; i < N; i++) {\n        cout << actions[i] << \"\\n\";\n    }\n    \n    return 0;\n}","ahc034":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int GRID_SIZE = 20;\nint grid[GRID_SIZE][GRID_SIZE];\n\n// Directions: U, D, L, R\nconst int dx[4] = {-1, 1, 0, 0};\nconst int dy[4] = {0, 0, -1, 1};\n\nstruct Position {\n    int x, y;\n    Position() : x(0), y(0) {}\n    Position(int _x, int _y) : x(_x), y(_y) {}\n    bool operator==(const Position& other) const {\n        return x == other.x && y == other.y;\n    }\n};\n\n// BFS to find shortest path between two points\nvector<pair<int, int>> find_shortest_path(Position start, Position end) {\n    if (start == end) return {};\n    \n    bool visited[GRID_SIZE][GRID_SIZE];\n    pair<int, int> parent[GRID_SIZE][GRID_SIZE];\n    memset(visited, 0, sizeof(visited));\n    \n    queue<Position> q;\n    q.push(start);\n    visited[start.x][start.y] = true;\n    parent[start.x][start.y] = {-1, -1};\n    \n    while (!q.empty()) {\n        Position pos = q.front(); q.pop();\n        \n        if (pos.x == end.x && pos.y == end.y) {\n            // Reconstruct path\n            vector<pair<int, int>> path;\n            int cx = end.x, cy = end.y;\n            while (parent[cx][cy].first != -1) {\n                path.push_back({cx, cy});\n                auto p = parent[cx][cy];\n                cx = p.first;\n                cy = p.second;\n            }\n            reverse(path.begin(), path.end());\n            return path;\n        }\n        \n        for (int i = 0; i < 4; i++) {\n            int nx = pos.x + dx[i];\n            int ny = pos.y + dy[i];\n            \n            if (nx >= 0 && nx < GRID_SIZE && ny >= 0 && ny < GRID_SIZE && !visited[nx][ny]) {\n                visited[nx][ny] = true;\n                parent[nx][ny] = {pos.x, pos.y};\n                q.push(Position(nx, ny));\n            }\n        }\n    }\n    \n    return {};\n}\n\n// Convert path to movement directions\nvector<char> path_to_directions(Position start, const vector<pair<int, int>>& path) {\n    vector<char> directions;\n    int cx = start.x, cy = start.y;\n    \n    for (auto [nx, ny] : path) {\n        if (nx == cx - 1 && ny == cy) directions.push_back('U');\n        else if (nx == cx + 1 && ny == cy) directions.push_back('D');\n        else if (nx == cx && ny == cy - 1) directions.push_back('L');\n        else if (nx == cx && ny == cy + 1) directions.push_back('R');\n        cx = nx;\n        cy = ny;\n    }\n    \n    return directions;\n}\n\n// Find nearest sink for a given source\npair<int, int> find_nearest_sink(Position source, vector<pair<Position, int>>& sinks) {\n    int best_idx = -1;\n    int min_distance = GRID_SIZE * GRID_SIZE;\n    \n    for (int i = 0; i < sinks.size(); i++) {\n        if (sinks[i].second > 0) {\n            int dist = abs(source.x - sinks[i].first.x) + abs(source.y - sinks[i].first.y);\n            if (dist < min_distance) {\n                min_distance = dist;\n                best_idx = i;\n            }\n        }\n    }\n    \n    return {best_idx, min_distance};\n}\n\n// Find best source to start with (closest to initial position or most beneficial)\nint find_best_initial_source(Position start_pos, vector<pair<Position, int>>& sources, vector<pair<Position, int>>& sinks) {\n    int best_idx = 0;\n    int best_score = -1;\n    \n    for (int i = 0; i < sources.size(); i++) {\n        if (sources[i].second > 0) {\n            auto nearest = find_nearest_sink(sources[i].first, sinks);\n            if (nearest.first != -1) {\n                int distance_to_source = abs(start_pos.x - sources[i].first.x) + abs(start_pos.y - sources[i].first.y);\n                int distance_source_to_sink = nearest.second;\n                int amount = min(sources[i].second, sinks[nearest.first].second);\n                \n                // Score based on amount vs distance\n                int score = (amount * 1000) / (distance_to_source + distance_source_to_sink + 1);\n                if (score > best_score) {\n                    best_score = score;\n                    best_idx = i;\n                }\n            }\n        }\n    }\n    \n    return best_idx;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    // Read grid\n    for (int i = 0; i < GRID_SIZE; i++) {\n        for (int j = 0; j < GRID_SIZE; j++) {\n            cin >> grid[i][j];\n        }\n    }\n    \n    // Collect sources (positive) and sinks (negative)\n    vector<pair<Position, int>> sources, sinks;\n    for (int i = 0; i < GRID_SIZE; i++) {\n        for (int j = 0; j < GRID_SIZE; j++) {\n            if (grid[i][j] > 0) {\n                sources.push_back({Position(i, j), grid[i][j]});\n            } else if (grid[i][j] < 0) {\n                sinks.push_back({Position(i, j), -grid[i][j]});\n            }\n        }\n    }\n    \n    vector<string> operations;\n    Position current_pos(0, 0);\n    int load = 0;\n    \n    // Process sources, but start with the most beneficial one\n    vector<bool> source_processed(sources.size(), false);\n    \n    // Find a good starting source\n    int current_source_idx = find_best_initial_source(current_pos, sources, sinks);\n    \n    int processed_sources = 0;\n    while (processed_sources < sources.size()) {\n        // Find next unprocessed source\n        if (source_processed[current_source_idx]) {\n            // Find next available source\n            current_source_idx = 0;\n            while (current_source_idx < sources.size() && \n                   (source_processed[current_source_idx] || sources[current_source_idx].second <= 0)) {\n                current_source_idx++;\n            }\n            \n            if (current_source_idx >= sources.size()) break;\n        }\n        \n        auto& source = sources[current_source_idx];\n        \n        if (source.second <= 0) {\n            source_processed[current_source_idx] = true;\n            processed_sources++;\n            continue;\n        }\n        \n        // Process this source completely\n        while (source.second > 0) {\n            // Find the nearest sink with demand\n            auto nearest = find_nearest_sink(source.first, sinks);\n            if (nearest.first == -1) break; // No more sinks\n            \n            int sink_idx = nearest.first;\n            auto& sink = sinks[sink_idx];\n            \n            if (sink.second <= 0) {\n                // This sink is depleted, try again to find another\n                continue;\n            }\n            \n            int transport_amount = min(source.second, sink.second);\n            if (transport_amount <= 0) break;\n            \n            // Move to source if needed\n            if (!(current_pos == source.first)) {\n                auto path = find_shortest_path(current_pos, source.first);\n                auto directions = path_to_directions(current_pos, path);\n                \n                for (char dir : directions) {\n                    operations.push_back(string(1, dir));\n                }\n                current_pos = source.first;\n            }\n            \n            // Load soil\n            operations.push_back(\"+\" + to_string(transport_amount));\n            load += transport_amount;\n            source.second -= transport_amount;\n            \n            // Move to sink\n            if (!(current_pos == sink.first)) {\n                auto path = find_shortest_path(current_pos, sink.first);\n                auto directions = path_to_directions(current_pos, path);\n                \n                for (char dir : directions) {\n                    operations.push_back(string(1, dir));\n                }\n                current_pos = sink.first;\n            }\n            \n            // Unload soil\n            operations.push_back(\"-\" + to_string(transport_amount));\n            load -= transport_amount;\n            sink.second -= transport_amount;\n        }\n        \n        source_processed[current_source_idx] = true;\n        processed_sources++;\n        \n        // Find next source to process (the one closest to current position)\n        int next_source_idx = -1;\n        int min_distance = GRID_SIZE * GRID_SIZE;\n        \n        for (int i = 0; i < sources.size(); i++) {\n            if (!source_processed[i] && sources[i].second > 0) {\n                int dist = abs(current_pos.x - sources[i].first.x) + abs(current_pos.y - sources[i].first.y);\n                if (dist < min_distance) {\n                    min_distance = dist;\n                    next_source_idx = i;\n                }\n            }\n        }\n        \n        if (next_source_idx != -1) {\n            current_source_idx = next_source_idx;\n        }\n    }\n    \n    // Output operations\n    for (const string& op : operations) {\n        cout << op << \"\\n\";\n    }\n    \n    return 0;\n}","ahc035":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <numeric>\n\nusing namespace std;\n\nconst int N = 6;\nconst int SEED_COUNT = 2 * N * (N - 1); // 60 seeds\n\n// Calculate total value of a seed\nint calculateValue(const vector<int>& seed) {\n    return accumulate(seed.begin(), seed.end(), 0);\n}\n\n// Generate placement with focus on high-value seed propagation\nvector<vector<int>> generatePlacement(const vector<vector<int>>& seeds) {\n    vector<vector<int>> placement(N, vector<int>(N));\n    vector<bool> seedUsed(SEED_COUNT, false);\n    \n    // Rank seeds by value (highest first)\n    vector<pair<int, int>> seedRanking;\n    for (int i = 0; i < SEED_COUNT; i++) {\n        seedRanking.push_back({calculateValue(seeds[i]), i});\n    }\n    sort(seedRanking.begin(), seedRanking.end(), greater<pair<int, int>>());\n    \n    // Position priority based on influence (number of adjacent cells)\n    vector<pair<int, pair<int, int>>> positions;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            int neighbors = 0;\n            if (i > 0) neighbors++;\n            if (i < N-1) neighbors++;\n            if (j > 0) neighbors++;\n            if (j < N-1) neighbors++;\n            positions.push_back({neighbors, {i, j}});\n        }\n    }\n    \n    // Sort positions by influence (more neighbors first)\n    sort(positions.begin(), positions.end(), greater<pair<int, pair<int, int>>>());\n    \n    // Assign seeds to positions\n    for (int posIdx = 0; posIdx < min((int)positions.size(), SEED_COUNT); posIdx++) {\n        int row = positions[posIdx].second.first;\n        int col = positions[posIdx].second.second;\n        int seedRankIdx = posIdx;\n        \n        // Make sure we don't go out of bounds\n        if (seedRankIdx >= SEED_COUNT) seedRankIdx = SEED_COUNT - 1;\n        \n        // Find the seed at this rank that hasn't been used\n        int actualSeedIdx = 0;\n        int count = 0;\n        for (int i = 0; i < SEED_COUNT && count <= seedRankIdx; i++) {\n            if (!seedUsed[seedRanking[i].second]) {\n                if (count == seedRankIdx) {\n                    actualSeedIdx = seedRanking[i].second;\n                    break;\n                }\n                count++;\n            }\n        }\n        \n        // If we couldn't find by rank, just find next available\n        if (seedUsed[actualSeedIdx]) {\n            for (int i = 0; i < SEED_COUNT; i++) {\n                if (!seedUsed[i]) {\n                    actualSeedIdx = i;\n                    break;\n                }\n            }\n        }\n        \n        placement[row][col] = actualSeedIdx;\n        seedUsed[actualSeedIdx] = true;\n    }\n    \n    // Simple fill for any remaining positions\n    int nextSeed = 0;\n    for (int i = 0; i < N; i++) {\n        for (int j = 0; j < N; j++) {\n            if (!seedUsed[placement[i][j]]) {\n                while (nextSeed < SEED_COUNT && seedUsed[nextSeed]) nextSeed++;\n                if (nextSeed < SEED_COUNT) {\n                    placement[i][j] = nextSeed;\n                    seedUsed[nextSeed] = true;\n                }\n            }\n        }\n    }\n    \n    return placement;\n}\n\n// Alternative simpler approach\nvector<vector<int>> generateSimplePlacement(const vector<vector<int>>& seeds) {\n    vector<vector<int>> placement(N, vector<int>(N));\n    vector<bool> seedUsed(SEED_COUNT, false);\n    \n    // Simple strategy: place highest value seeds in a pattern that maximizes their influence\n    vector<pair<int, int>> pattern = {\n        {2,2}, {2,3}, {3,2}, {3,3},  // center\n        {1,2}, {1,3}, {2,1}, {2,4}, {3,1}, {3,4}, {4,2}, {4,3},  // ring 1\n        {0,2}, {0,3}, {2,0}, {3,0}, {2,5}, {3,5}, {5,2}, {5,3},  // ring 2\n        {1,1}, {1,4}, {4,1}, {4,4},  // diagonals\n        {0,1}, {0,4}, {1,0}, {1,5}, {4,0}, {4,5}, {5,1}, {5,4},  // edges\n        {0,0}, {0,5}, {5,0}, {5,5}   // corners\n    };\n    \n    // Rank all seeds\n    vector<int> seedOrder(SEED_COUNT);\n    for (int i = 0; i < SEED_COUNT; i++) {\n        seedOrder[i] = i;\n    }\n    \n    // Sort by value descending\n    sort(seedOrder.begin(), seedOrder.end(), [&](int a, int b) {\n        return calculateValue(seeds[a]) > calculateValue(seeds[b]);\n    });\n    \n    // Place seeds according to pattern\n    for (int i = 0; i < min((int)pattern.size(), SEED_COUNT); i++) {\n        int row = pattern[i].first;\n        int col = pattern[i].second;\n        int seedId = seedOrder[i];\n        placement[row][col] = seedId;\n    }\n    \n    return placement;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    int N_input, M, T;\n    cin >> N_input >> M >> T;\n    \n    vector<vector<int>> seeds(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 >> seeds[i][j];\n        }\n    }\n    \n    // Process T turns\n    for (int turn = 0; turn < T; turn++) {\n        // Use the simple but effective approach\n        auto placement = generateSimplePlacement(seeds);\n        \n        // Output placement\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                cout << placement[i][j];\n                if (j < N - 1) cout << \" \";\n            }\n            cout << endl;\n        }\n        cout.flush();\n        \n        // Read new seeds for next iteration\n        if (turn < T - 1) {\n            for (int i = 0; i < SEED_COUNT; i++) {\n                for (int j = 0; j < M; j++) {\n                    cin >> seeds[i][j];\n                }\n            }\n        }\n    }\n    \n    return 0;\n}","ahc038":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MAX_N = 30;\nconst int MAX_V = 15;\n\nstruct Point {\n    int x, y;\n    Point() : x(0), y(0) {}\n    Point(int x, int y) : x(x), y(y) {}\n    Point operator+(const Point& p) const { return Point(x + p.x, y + p.y); }\n    Point operator-(const Point& p) const { return Point(x - p.x, y - p.y); }\n    bool operator==(const Point& p) const { return x == p.x && y == p.y; }\n    bool operator!=(const Point& p) const { return !(*this == p); }\n};\n\nconst Point DIRS[4] = {Point(0, 1), Point(1, 0), Point(0, -1), Point(-1, 0)}; // R, D, L, U\nconst char DIR_CHARS[4] = {'R', 'D', 'L', 'U'};\n\nclass RobotArm {\npublic:\n    int V;\n    vector<pair<int, int>> tree; // {parent, length}\n    Point root_pos;\n    \n    RobotArm(int v) : V(v) {\n        tree.resize(V);\n    }\n};\n\nclass Solution {\nprivate:\n    int N, M, V;\n    vector<vector<int>> initial, target;\n    vector<Point> initial_pos, target_pos;\n    \npublic:\n    void readInput() {\n        cin >> N >> M >> V;\n        initial.assign(N, vector<int>(N, 0));\n        target.assign(N, vector<int>(N, 0));\n        \n        for (int i = 0; i < N; i++) {\n            string line;\n            cin >> line;\n            for (int j = 0; j < N; j++) {\n                initial[i][j] = line[j] - '0';\n                if (initial[i][j]) {\n                    initial_pos.push_back(Point(i, j));\n                }\n            }\n        }\n        \n        for (int i = 0; i < N; i++) {\n            string line;\n            cin >> line;\n            for (int j = 0; j < N; j++) {\n                target[i][j] = line[j] - '0';\n                if (target[i][j]) {\n                    target_pos.push_back(Point(i, j));\n                }\n            }\n        }\n    }\n    \n    RobotArm designArm() {\n        RobotArm arm(V);\n        \n        // Good arm design with varied lengths for optimal reach\n        if (V >= 4) {\n            arm.tree[1] = {0, min(3, N-1)};  // Long finger for good reach\n            arm.tree[2] = {0, min(2, N-1)};  // Medium finger  \n            arm.tree[3] = {0, 1};            // Short finger\n            for (int i = 4; i < V; i++) {\n                arm.tree[i] = {0, 1};        // Additional short fingers\n            }\n        } else {\n            // For smaller V, optimize what we can\n            for (int i = 1; i < V; i++) {\n                int length = min(i + 1, N-1);\n                arm.tree[i] = {0, length};\n            }\n        }\n        \n        // Start at (0,0) for good baseline coverage\n        arm.root_pos = Point(0, 0);\n        \n        return arm;\n    }\n    \n    // Find leaf vertices correctly\n    vector<int> findLeaves(const RobotArm& arm) {\n        vector<bool> has_children(arm.V, false);\n        for (int i = 1; i < arm.V; i++) {\n            if (arm.tree[i].first >= 0 && arm.tree[i].first < arm.V) {\n                has_children[arm.tree[i].first] = true;\n            }\n        }\n        \n        vector<int> leaves;\n        for (int i = 1; i < arm.V; i++) {\n            if (!has_children[i]) {\n                leaves.push_back(i);\n            }\n        }\n        return leaves;\n    }\n    \n    string createMoveString(char move, const vector<char>& rotations, const vector<char>& actions) {\n        string result;\n        result += move;\n        for (int i = 1; i < (int)rotations.size(); i++) {\n            result += rotations[i];\n        }\n        for (int i = 0; i < (int)actions.size(); i++) {\n            result += actions[i];\n        }\n        return result;\n    }\n    \n    // Calculate vertex position correctly\n    Point getVertexPosition(const Point& root_pos, int vertex, const vector<int>& directions, const RobotArm& arm) {\n        if (vertex == 0) return root_pos;\n        \n        int parent = arm.tree[vertex].first;\n        int length = arm.tree[vertex].second;\n        \n        // Get parent position recursively\n        Point parent_pos = getVertexPosition(root_pos, parent, directions, arm);\n        \n        // Apply direction based on current rotation state\n        int dir = directions[vertex];\n        Point offset;\n        if (dir == 0) offset = Point(0, length);      // Right\n        else if (dir == 1) offset = Point(length, 0); // Down\n        else if (dir == 2) offset = Point(0, -length); // Left\n        else offset = Point(-length, 0);              // Up\n        \n        return parent_pos + offset;\n    }\n    \n    vector<string> solve(RobotArm& arm) {\n        vector<string> operations;\n        Point current_pos = arm.root_pos;\n        vector<vector<int>> current_grid = initial;\n        \n        // Track state\n        vector<int> edge_directions(V, 0); // 0=right, 1=down, 2=left, 3=up\n        vector<bool> holding(V, false);\n        \n        // Find leaves\n        vector<int> leaves = findLeaves(arm);\n        \n        // Efficient approach with optimized parameters\n        int turns = 0;\n        int max_turns = min(2500, (int)(1e5));\n        \n        random_device rd;\n        mt19937 gen(rd());\n        uniform_int_distribution<> dis(0, 3);\n        \n        while (turns < max_turns) {\n            // Movement strategy - balanced approach\n            char move_char = '.';\n            Point new_pos = current_pos;\n            \n            // Mostly random movement with occasional directed movement\n            if (dis(gen) % 5 == 0) {\n                // Occasionally try to move toward center\n                if (turns % 100 == 0) {\n                    int center_x = N/2;\n                    int center_y = N/2;\n                    \n                    if (current_pos.x < center_x && current_pos.x < N-1) {\n                        new_pos = current_pos + Point(1, 0);\n                        move_char = 'D';\n                    } else if (current_pos.x > center_x && current_pos.x > 0) {\n                        new_pos = current_pos + Point(-1, 0);\n                        move_char = 'U';\n                    } else if (current_pos.y < center_y && current_pos.y < N-1) {\n                        new_pos = current_pos + Point(0, 1);\n                        move_char = 'R';\n                    } else if (current_pos.y > center_y && current_pos.y > 0) {\n                        new_pos = current_pos + Point(0, -1);\n                        move_char = 'L';\n                    }\n                } else {\n                    // Regular random movement\n                    int rand_dir = dis(gen) % 4;\n                    new_pos = current_pos + DIRS[rand_dir];\n                    move_char = DIR_CHARS[rand_dir];\n                }\n            } else {\n                // Standard random movement\n                int rand_dir = dis(gen) % 4;\n                new_pos = current_pos + DIRS[rand_dir];\n                move_char = DIR_CHARS[rand_dir];\n            }\n            \n            // Check bounds and move if valid\n            if (new_pos.x >= 0 && new_pos.x < N && new_pos.y >= 0 && new_pos.y < N) {\n                current_pos = new_pos;\n            } else {\n                move_char = '.';\n            }\n            \n            // Very rare rotations to maintain efficiency\n            vector<char> rotations(V, '.');\n            for (int i = 1; i < V; i++) {\n                if (dis(gen) % 30 == 0) {\n                    if (dis(gen) % 2 == 0) {\n                        rotations[i] = 'R';\n                        edge_directions[i] = (edge_directions[i] + 1) % 4;\n                    } else {\n                        rotations[i] = 'L';\n                        edge_directions[i] = (edge_directions[i] + 3) % 4;\n                    }\n                }\n            }\n            \n            // Actions - validated carefully\n            vector<char> actions(V, '.');\n            for (int leaf : leaves) {\n                if (leaf >= 0 && leaf < V) {\n                    Point finger_pos = getVertexPosition(current_pos, leaf, edge_directions, arm);\n                    \n                    // Strict bounds checking\n                    if (finger_pos.x >= 0 && finger_pos.x < N && \n                        finger_pos.y >= 0 && finger_pos.y < N) {\n                        \n                        int x = finger_pos.x;\n                        int y = finger_pos.y;\n                        \n                        // Only act when there's something to do\n                        if (!holding[leaf] && current_grid[x][y] == 1 && target[x][y] == 0) {\n                            // Grab takoyaki\n                            actions[leaf] = 'P';\n                            holding[leaf] = true;\n                            current_grid[x][y] = 0;\n                        } else if (holding[leaf] && current_grid[x][y] == 0 && target[x][y] == 1) {\n                            // Place takoyaki\n                            actions[leaf] = 'P';\n                            holding[leaf] = false;\n                            current_grid[x][y] = 1;\n                        }\n                    }\n                }\n            }\n            \n            operations.push_back(createMoveString(move_char, rotations, actions));\n            turns++;\n        }\n        \n        return operations;\n    }\n    \n    void outputSolution(RobotArm& arm, const vector<string>& operations) {\n        // Output arm design\n        cout << arm.V << endl;\n        for (int i = 1; i < arm.V; i++) {\n            cout << arm.tree[i].first << \" \" << arm.tree[i].second << endl;\n        }\n        cout << arm.root_pos.x << \" \" << arm.root_pos.y << endl;\n        \n        // Output operations\n        for (const string& op : operations) {\n            cout << op << endl;\n        }\n    }\n    \n    void run() {\n        readInput();\n        RobotArm arm = designArm();\n        vector<string> operations = solve(arm);\n        outputSolution(arm, operations);\n    }\n};\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    Solution solution;\n    solution.run();\n    \n    return 0;\n}","ahc039":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MAX_N = 5000;\nconst int FIELD_SIZE = 100000;\n\nstruct Point {\n    int x, y;\n    Point() : x(0), y(0) {}\n    Point(int x, int y) : x(x), y(y) {}\n};\n\nstruct Rectangle {\n    int x1, y1, x2, y2;\n    int mackerels, sardines;\n    int score;\n    \n    Rectangle() : x1(0), y1(0), x2(0), y2(0), mackerels(0), sardines(0), score(0) {}\n    Rectangle(int x1, int y1, int x2, int y2) : x1(x1), y1(y1), x2(x2), y2(y2), mackerels(0), sardines(0), score(0) {}\n    \n    bool isValid() const {\n        return x1 >= 0 && x2 <= FIELD_SIZE && y1 >= 0 && y2 <= FIELD_SIZE && \n               x1 < x2 && y1 < y2 && perimeter() <= 400000;\n    }\n    \n    int perimeter() const {\n        return 2 * (x2 - x1 + y2 - y1);\n    }\n};\n\nvector<Point> mackerels(MAX_N);\nvector<Point> sardines(MAX_N);\n\n// Count points in rectangle\npair<int, int> countPoints(const Rectangle& rect) {\n    int m_count = 0, s_count = 0;\n    \n    for (const auto& p : mackerels) {\n        if (p.x >= rect.x1 && p.x <= rect.x2 && p.y >= rect.y1 && p.y <= rect.y2) {\n            m_count++;\n        }\n    }\n    \n    for (const auto& p : sardines) {\n        if (p.x >= rect.x1 && p.x <= rect.x2 && p.y >= rect.y1 && p.y <= rect.y2) {\n            s_count++;\n        }\n    }\n    \n    return {m_count, s_count};\n}\n\n// Generate rectangles with multiple strategies\nvector<Rectangle> generateRectangles() {\n    vector<Rectangle> rectangles;\n    \n    // Strategy 1: Individual mackerel focused rectangles\n    vector<int> sizes = {1500, 2000, 2500, 3000, 3500, 4000, 5000, 6000, 7000, 8000, 10000, 12000, 15000, 18000, 20000, 25000, 30000};\n    \n    for (int i = 0; i < min(250, MAX_N); i++) {\n        const Point& center = mackerels[i];\n        \n        for (int size : sizes) {\n            int x1 = max(0, center.x - size/2);\n            int y1 = max(0, center.y - size/2);\n            int x2 = min(FIELD_SIZE, center.x + size/2);\n            int y2 = min(FIELD_SIZE, center.y + size/2);\n            \n            Rectangle rect(x1, y1, x2, y2);\n            if (rect.isValid()) {\n                auto counts = countPoints(rect);\n                rect.mackerels = counts.first;\n                rect.sardines = counts.second;\n                rect.score = rect.mackerels - rect.sardines;\n                \n                if (rect.score > 0) {\n                    rectangles.push_back(rect);\n                }\n            }\n        }\n    }\n    \n    // Strategy 2: High density area detection\n    const int GRID_SIZE = 30;\n    vector<vector<pair<int, int>>> grid(GRID_SIZE, vector<pair<int, int>>(GRID_SIZE, {0, 0}));\n    \n    // Count mackerels and sardines in each grid cell\n    for (const auto& p : mackerels) {\n        int gx = min(GRID_SIZE - 1, p.x * GRID_SIZE / (FIELD_SIZE + 1));\n        int gy = min(GRID_SIZE - 1, p.y * GRID_SIZE / (FIELD_SIZE + 1));\n        grid[gx][gy].first++;\n    }\n    \n    for (const auto& p : sardines) {\n        int gx = min(GRID_SIZE - 1, p.x * GRID_SIZE / (FIELD_SIZE + 1));\n        int gy = min(GRID_SIZE - 1, p.y * GRID_SIZE / (FIELD_SIZE + 1));\n        grid[gx][gy].second++;\n    }\n    \n    // Generate rectangles around high mackerel, low sardine areas\n    for (int i = 0; i < GRID_SIZE; i++) {\n        for (int j = 0; j < GRID_SIZE; j++) {\n            if (grid[i][j].first > grid[i][j].second * 1.2) {  // More mackerels than sardines\n                // Try different window sizes\n                for (int window = 1; window <= 12; window++) {\n                    int x1_grid = max(0, i - window/2);\n                    int y1_grid = max(0, j - window/2);\n                    int x2_grid = min(GRID_SIZE - 1, i + window/2);\n                    int y2_grid = min(GRID_SIZE - 1, j + window/2);\n                    \n                    int x1 = x1_grid * FIELD_SIZE / GRID_SIZE;\n                    int y1 = y1_grid * FIELD_SIZE / GRID_SIZE;\n                    int x2 = min(FIELD_SIZE, (x2_grid + 1) * FIELD_SIZE / GRID_SIZE);\n                    int y2 = min(FIELD_SIZE, (y2_grid + 1) * FIELD_SIZE / GRID_SIZE);\n                    \n                    Rectangle rect(x1, y1, x2, y2);\n                    if (rect.isValid()) {\n                        auto counts = countPoints(rect);\n                        rect.mackerels = counts.first;\n                        rect.sardines = counts.second;\n                        rect.score = rect.mackerels - rect.sardines;\n                        \n                        if (rect.score > 0) {\n                            rectangles.push_back(rect);\n                        }\n                    }\n                }\n            }\n        }\n    }\n    \n    // Strategy 3: Mackerel cluster detection with neighborhood analysis\n    // Find points with high local mackerel density\n    for (int i = 0; i < min(150, MAX_N); i++) {\n        const Point& center = mackerels[i];\n        vector<int> cluster_sizes = {3000, 4000, 5000, 6000, 8000, 10000, 12000, 15000, 20000};\n        \n        for (int size : cluster_sizes) {\n            int x1 = max(0, center.x - size/2);\n            int y1 = max(0, center.y - size/2);\n            int x2 = min(FIELD_SIZE, center.x + size/2);\n            int y2 = min(FIELD_SIZE, center.y + size/2);\n            \n            Rectangle rect(x1, y1, x2, y2);\n            if (rect.isValid()) {\n                auto counts = countPoints(rect);\n                rect.mackerels = counts.first;\n                rect.sardines = counts.second;\n                rect.score = rect.mackerels - rect.sardines;\n                \n                if (rect.score > 0) {\n                    rectangles.push_back(rect);\n                }\n            }\n        }\n    }\n    \n    // Sort by score using manual sort\n    for (int i = 0; i < (int)rectangles.size(); i++) {\n        for (int j = i + 1; j < (int)rectangles.size(); j++) {\n            if (rectangles[i].score < rectangles[j].score) {\n                swap(rectangles[i], rectangles[j]);\n            }\n        }\n    }\n    \n    // Remove duplicates\n    vector<Rectangle> unique_rectangles;\n    set<pair<pair<int, int>, pair<int, int>>> seen;\n    \n    for (const auto& rect : rectangles) {\n        pair<pair<int, int>, pair<int, int>> key = {{rect.x1, rect.y1}, {rect.x2, rect.y2}};\n        if (seen.find(key) == seen.end()) {\n            seen.insert(key);\n            unique_rectangles.push_back(rect);\n        }\n    }\n    \n    return unique_rectangles;\n}\n\n// Try comprehensive optimization of rectangle boundaries\nRectangle optimizeRectangle(const Rectangle& rect) {\n    Rectangle best_rect = rect;\n    int best_score = rect.score;\n    \n    // Try boundary adjustments\n    vector<int> adjustments = {-3000, -2000, -1000, -500, 0, 500, 1000, 2000, 3000};\n    \n    for (int dx1 : adjustments) {\n        for (int dy1 : adjustments) {\n            for (int dx2 : adjustments) {\n                for (int dy2 : adjustments) {\n                    int new_x1 = max(0, rect.x1 + dx1);\n                    int new_y1 = max(0, rect.y1 + dy1);\n                    int new_x2 = min(FIELD_SIZE, rect.x2 + dx2);\n                    int new_y2 = min(FIELD_SIZE, rect.y2 + dy2);\n                    \n                    if (new_x1 < new_x2 && new_y1 < new_y2) {\n                        Rectangle test_rect(new_x1, new_y1, new_x2, new_y2);\n                        if (test_rect.isValid()) {\n                            auto counts = countPoints(test_rect);\n                            int score = counts.first - counts.second;\n                            if (score > best_score) {\n                                best_score = score;\n                                best_rect = test_rect;\n                                best_rect.mackerels = counts.first;\n                                best_rect.sardines = counts.second;\n                                best_rect.score = score;\n                            }\n                        }\n                    }\n                }\n            }\n        }\n    }\n    \n    return best_rect;\n}\n\n// Try expanding/shrinking rectangle to improve score\nRectangle expandRectangle(const Rectangle& rect) {\n    Rectangle best_rect = rect;\n    int best_score = rect.score;\n    \n    // Try expanding in all directions\n    for (int expand = 300; expand <= 4000; expand += 300) {\n        int new_x1 = max(0, rect.x1 - expand);\n        int new_y1 = max(0, rect.y1 - expand);\n        int new_x2 = min(FIELD_SIZE, rect.x2 + expand);\n        int new_y2 = min(FIELD_SIZE, rect.y2 + expand);\n        \n        if (new_x1 < new_x2 && new_y1 < new_y2) {\n            Rectangle test_rect(new_x1, new_y1, new_x2, new_y2);\n            if (test_rect.isValid()) {\n                auto counts = countPoints(test_rect);\n                int score = counts.first - counts.second;\n                if (score > best_score) {\n                    best_score = score;\n                    best_rect = test_rect;\n                    best_rect.mackerels = counts.first;\n                    best_rect.sardines = counts.second;\n                    best_rect.score = score;\n                }\n            }\n        }\n    }\n    \n    return best_rect;\n}\n\n// Try shifting rectangle to improve score\nRectangle shiftRectangle(const Rectangle& rect) {\n    Rectangle best_rect = rect;\n    int best_score = rect.score;\n    \n    // Try shifting in different directions\n    vector<int> shifts = {-2000, -1000, -500, 0, 500, 1000, 2000};\n    \n    for (int dx : shifts) {\n        for (int dy : shifts) {\n            int new_x1 = max(0, rect.x1 + dx);\n            int new_y1 = max(0, rect.y1 + dy);\n            int new_x2 = min(FIELD_SIZE, rect.x2 + dx);\n            int new_y2 = min(FIELD_SIZE, rect.y2 + dy);\n            \n            if (new_x1 < new_x2 && new_y1 < new_y2) {\n                Rectangle test_rect(new_x1, new_y1, new_x2, new_y2);\n                if (test_rect.isValid()) {\n                    auto counts = countPoints(test_rect);\n                    int score = counts.first - counts.second;\n                    if (score > best_score) {\n                        best_score = score;\n                        best_rect = test_rect;\n                        best_rect.mackerels = counts.first;\n                        best_rect.sardines = counts.second;\n                        best_rect.score = score;\n                    }\n                }\n            }\n        }\n    }\n    \n    return best_rect;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    int N;\n    cin >> N;\n    \n    for (int i = 0; i < N; i++) {\n        cin >> mackerels[i].x >> mackerels[i].y;\n    }\n    \n    for (int i = 0; i < N; i++) {\n        cin >> sardines[i].x >> sardines[i].y;\n    }\n    \n    // Generate rectangles\n    vector<Rectangle> rectangles = generateRectangles();\n    \n    // Try multiple strategies and pick the best\n    Rectangle best_rect(10000, 10000, 90000, 90000);  // Default fallback\n    int best_score = -1;\n    \n    // Strategy 1: Top rectangles as-is (try more of them)\n    for (int i = 0; i < min(8, (int)rectangles.size()); i++) {\n        if (rectangles[i].score > best_score) {\n            best_score = rectangles[i].score;\n            best_rect = rectangles[i];\n        }\n    }\n    \n    // Strategy 2: Try optimizations on top rectangles\n    for (int i = 0; i < min(5, (int)rectangles.size()); i++) {\n        Rectangle optimized = optimizeRectangle(rectangles[i]);\n        if (optimized.score > best_score) {\n            best_score = optimized.score;\n            best_rect = optimized;\n        }\n        \n        Rectangle expanded = expandRectangle(rectangles[i]);\n        if (expanded.score > best_score) {\n            best_score = expanded.score;\n            best_rect = expanded;\n        }\n        \n        Rectangle shifted = shiftRectangle(rectangles[i]);\n        if (shifted.score > best_score) {\n            best_score = shifted.score;\n            best_rect = shifted;\n        }\n    }\n    \n    // Strategy 3: Try optimizing the current best with more aggressive optimization\n    Rectangle final_optimized = optimizeRectangle(best_rect);\n    if (final_optimized.score > best_score) {\n        best_rect = final_optimized;\n        best_score = final_optimized.score;\n    }\n    \n    // Strategy 4: Try one more round of expansion on the best\n    Rectangle final_expanded = expandRectangle(best_rect);\n    if (final_expanded.score > best_score) {\n        best_rect = final_expanded;\n        best_score = final_expanded.score;\n    }\n    \n    // Output the rectangle as a polygon\n    cout << \"4\\n\";\n    cout << best_rect.x1 << \" \" << best_rect.y1 << \"\\n\";\n    cout << best_rect.x2 << \" \" << best_rect.y1 << \"\\n\";\n    cout << best_rect.x2 << \" \" << best_rect.y2 << \"\\n\";\n    cout << best_rect.x1 << \" \" << best_rect.y2 << \"\\n\";\n    \n    return 0;\n}","ahc040":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Placement {\n    int p, r, d, b;\n    \n    Placement(int _p, int _r, char _d, int _b) : p(_p), r(_r), d(_d), b(_b) {}\n};\n\n// Highly optimized packing strategy\nvector<Placement> optimizedPacking(const vector<pair<int,int>>& rectangles, mt19937& rng, int attempt_num, int T) {\n    vector<Placement> placements;\n    int n = rectangles.size();\n    \n    // Precompute statistics\n    long long total_area = 0;\n    long long total_perimeter = 0;\n    for (int i = 0; i < n; i++) {\n        total_area += (long long)rectangles[i].first * rectangles[i].second;\n        total_perimeter += rectangles[i].first + rectangles[i].second;\n    }\n    double avg_area = (double)total_area / n;\n    double avg_perimeter = (double)total_perimeter / n;\n    \n    for (int i = 0; i < n; i++) {\n        int rotate = 0;\n        \n        // Sophisticated rotation based on progression through attempts\n        long long area = (long long)rectangles[i].first * rectangles[i].second;\n        long long perimeter = rectangles[i].first + rectangles[i].second;\n        double ratio = (double)rectangles[i].first / rectangles[i].second;\n        \n        if (attempt_num < T/4) {\n            // Early attempts - aggressive optimization\n            if (area > avg_area * 2.0) {\n                // Very large rectangle - always try to rotate for compactness\n                rotate = (rectangles[i].first > rectangles[i].second) ? 1 : 0;\n            } else if (perimeter > avg_perimeter * 1.5) {\n                // High penalty rectangle - rotate if extreme aspect ratio\n                rotate = (ratio > 2.5 || ratio < 0.4) ? 1 : 0;\n            } else if (ratio > 3.0 || ratio < 0.33) {\n                // Extreme aspect ratio - rotate\n                rotate = 1;\n            } else {\n                // Normal rectangle - systematic rotation\n                rotate = (i + attempt_num) % 2;\n            }\n        } else if (attempt_num < T/2) {\n            // Middle attempts - balanced approach\n            if (ratio > 2.0) {\n                rotate = 1; // Wide rectangles\n            } else if (ratio < 0.5) {\n                rotate = 1; // Tall rectangles\n            } else if (area > avg_area * 1.5) {\n                rotate = (rectangles[i].first > rectangles[i].second * 1.2) ? 1 : 0;\n            } else {\n                rotate = uniform_int_distribution<int>(0, 1)(rng);\n            }\n        } else {\n            // Later attempts - refinement\n            if (attempt_num % 3 == 0) {\n                rotate = 0;\n            } else if (attempt_num % 3 == 1) {\n                rotate = 1;\n            } else {\n                rotate = (ratio > 1.5) ? 1 : 0;\n            }\n        }\n        \n        char direction = 'U';\n        int reference = -1;\n        \n        if (i > 0) {\n            // Progressive placement strategy\n            if (attempt_num < T/4) {\n                // Early - experimental approaches\n                if (attempt_num % 5 == 0) {\n                    // Build up strategy\n                    direction = 'U';\n                    reference = i - 1;\n                } else if (attempt_num % 5 == 1) {\n                    // Build left strategy\n                    direction = 'L';\n                    reference = i - 1;\n                } else if (attempt_num % 5 == 2) {\n                    // Mixed strategy\n                    direction = (rectangles[i].first > rectangles[i].second) ? 'L' : 'U';\n                    reference = max(0, i - 1 - (int)(rng() % min(3, i)));\n                } else if (attempt_num % 5 == 3 && i > 2) {\n                    // Reference earlier strategy\n                    reference = max(0, i - 2);\n                    direction = (uniform_int_distribution<int>(0, 1)(rng) == 0) ? 'U' : 'L';\n                } else {\n                    // Smart sizing strategy\n                    if (perimeter > avg_perimeter * 1.3) {\n                        direction = (rectangles[i].first > rectangles[i].second * 1.5) ? 'L' : 'U';\n                    } else {\n                        direction = (i % 2 == 0) ? 'U' : 'L';\n                    }\n                    reference = i - 1;\n                }\n            } else if (attempt_num < T/2) {\n                // Middle - proven strategies\n                if (i < n/3) {\n                    // Early placements - build foundation\n                    direction = 'U';\n                    reference = i - 1;\n                } else if (i < 2*n/3) {\n                    // Middle placements - expand\n                    direction = (rectangles[i].first > rectangles[i].second) ? 'L' : 'U';\n                    reference = max(0, i - 1 - (int)(rng() % min(2, i)));\n                } else {\n                    // Late placements - optimize\n                    direction = (uniform_int_distribution<int>(0, 1)(rng) == 0) ? 'U' : 'L';\n                    reference = max(0, i - 1 - (int)(rng() % min(3, i)));\n                }\n            } else {\n                // Late - refinement\n                if (attempt_num % 4 == 0) {\n                    direction = 'U';\n                    reference = i - 1;\n                } else if (attempt_num % 4 == 1) {\n                    direction = 'L';\n                    reference = i - 1;\n                } else if (attempt_num % 4 == 2) {\n                    direction = (uniform_int_distribution<int>(0, 1)(rng) == 0) ? 'U' : 'L';\n                    reference = max(0, (i - 1) % max(1, i));\n                } else {\n                    direction = (i % 3 == 0) ? 'U' : ((i % 3 == 1) ? 'L' : 'U');\n                    reference = i - 1;\n                }\n            }\n        }\n        \n        placements.emplace_back(i, rotate, direction, reference);\n    }\n    \n    return placements;\n}\n\n// Focused perimeter reduction strategy\nvector<Placement> perimeterReduction(const vector<pair<int,int>>& rectangles, mt19937& rng, int attempt_num) {\n    vector<Placement> placements;\n    int n = rectangles.size();\n    \n    for (int i = 0; i < n; i++) {\n        int rotate = 0;\n        \n        // Rotation focused on minimizing final perimeter\n        double ratio = (double)rectangles[i].first / rectangles[i].second;\n        \n        if (attempt_num % 4 == 0) {\n            // Minimize maximum dimension\n            rotate = (max(rectangles[i].first, rectangles[i].second) == rectangles[i].first) ? 1 : 0;\n        } else if (attempt_num % 4 == 1) {\n            // Aspect ratio optimization\n            if (ratio > 2.5) {\n                rotate = 1; // Very wide\n            } else if (ratio < 0.4) {\n                rotate = 1; // Very tall\n            }\n        } else if (attempt_num % 4 == 2) {\n            // Position-based\n            rotate = (i + attempt_num) % 2;\n        } else {\n            // Size-based\n            long long perimeter = rectangles[i].first + rectangles[i].second;\n            if (perimeter > 150000) {\n                rotate = (rectangles[i].first > rectangles[i].second * 1.3) ? 1 : 0;\n            } else {\n                rotate = uniform_int_distribution<int>(0, 1)(rng);\n            }\n        }\n        \n        char direction = 'U';\n        int reference = -1;\n        \n        if (i > 0) {\n            long long perimeter = rectangles[i].first + rectangles[i].second;\n            \n            if (attempt_num % 3 == 0) {\n                // Perimeter-aware placement\n                if (perimeter > 150000) {\n                    direction = (rectangles[i].first > rectangles[i].second) ? 'L' : 'U';\n                } else {\n                    direction = (uniform_int_distribution<int>(0, 1)(rng) == 0) ? 'U' : 'L';\n                }\n                reference = i - 1;\n            } else if (attempt_num % 3 == 1) {\n                // Reference variation\n                if (i > 3) {\n                    reference = max(0, i - 1 - (int)(rng() % 3));\n                } else {\n                    reference = i - 1;\n                }\n                direction = (uniform_int_distribution<int>(0, 1)(rng) == 0) ? 'U' : 'L';\n            } else {\n                // Systematic\n                reference = i - 1;\n                direction = (i % 2 == 0) ? 'U' : 'L';\n            }\n        }\n        \n        placements.emplace_back(i, rotate, direction, reference);\n    }\n    \n    return placements;\n}\n\n// Compact arrangement focused strategy\nvector<Placement> compactArrangement(const vector<pair<int,int>>& rectangles, mt19937& rng, int attempt_num) {\n    vector<Placement> placements;\n    int n = rectangles.size();\n    \n    for (int i = 0; i < n; i++) {\n        int rotate = 0;\n        \n        // Rotation to promote compactness\n        double ratio = (double)rectangles[i].first / rectangles[i].second;\n        if (ratio > 2.0) {\n            rotate = 1; // Wide rectangles\n        } else if (ratio < 0.5) {\n            rotate = 1; // Tall rectangles\n        } else {\n            rotate = (attempt_num + i) % 2; // Systematic variation\n        }\n        \n        char direction = 'U';\n        int reference = -1;\n        \n        if (i > 0) {\n            // Progressive compact building\n            if (i < n/4) {\n                // Start - build up\n                direction = (uniform_int_distribution<int>(0, 3)(rng) == 0) ? 'L' : 'U';\n                reference = i - 1;\n            } else if (i < n/2) {\n                // Middle - establish structure\n                direction = (rectangles[i].first > rectangles[i].second) ? 'L' : 'U';\n                reference = max(0, i - 1 - (int)(rng() % min(2, i)));\n            } else if (i < 3*n/4) {\n                // Later - fill strategically\n                if (uniform_int_distribution<int>(0, 2)(rng) == 0) {\n                    direction = 'L';\n                    reference = max(0, i - 1 - (int)(rng() % min(3, i)));\n                } else {\n                    direction = 'U';\n                    reference = i - 1;\n                }\n            } else {\n                // End - optimize\n                direction = (uniform_int_distribution<int>(0, 1)(rng) == 0) ? 'U' : 'L';\n                reference = max(0, i - 1 - (int)(rng() % min(4, i)));\n            }\n        }\n        \n        placements.emplace_back(i, rotate, direction, reference);\n    }\n    \n    return placements;\n}\n\n// High reliability baseline strategy\nvector<Placement> reliableBaseline(const vector<pair<int,int>>& rectangles, mt19937& rng, int attempt_num) {\n    vector<Placement> placements;\n    int n = rectangles.size();\n    \n    for (int i = 0; i < n; i++) {\n        // Conservative rotation strategy\n        int rotate = 0;\n        double ratio = (double)rectangles[i].first / rectangles[i].second;\n        \n        if (ratio > 1.8) {\n            rotate = 1; // Rotate wide rectangles\n        } else if (attempt_num % 5 == 0) {\n            // Occasionally try different rotation\n            rotate = uniform_int_distribution<int>(0, 1)(rng);\n        }\n        \n        char direction = 'U';\n        int reference = -1;\n        \n        if (i > 0) {\n            // Reliable placement strategy\n            if (attempt_num % 3 == 0) {\n                direction = 'U';\n                reference = i - 1;\n            } else if (attempt_num % 3 == 1) {\n                direction = 'L';\n                reference = i - 1;\n            } else {\n                direction = (uniform_int_distribution<int>(0, 1)(rng) == 0) ? 'U' : 'L';\n                reference = i - 1;\n            }\n        }\n        \n        placements.emplace_back(i, rotate, direction, reference);\n    }\n    \n    return placements;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    int N, T, sigma;\n    cin >> N >> T >> sigma;\n    \n    vector<pair<int,int>> measured_rectangles(N);\n    for (int i = 0; i < N; i++) {\n        cin >> measured_rectangles[i].first >> measured_rectangles[i].second;\n    }\n    \n    // Initialize random number generator\n    random_device rd;\n    mt19937 rng(rd());\n    rng.discard(10000); // High quality randomness\n    \n    // Use our best strategies early\n    for (int turn = 0; turn < T; turn++) {\n        vector<Placement> placements;\n        \n        // Strategy selection prioritizing our best approaches\n        if (turn == 0) {\n            // First attempt - our most reliable strategy\n            placements = reliableBaseline(measured_rectangles, rng, turn);\n        } else if (turn == 1) {\n            // Second attempt - optimized packing\n            placements = optimizedPacking(measured_rectangles, rng, turn, T);\n        } else if (turn == 2) {\n            // Third attempt - perimeter reduction\n            placements = perimeterReduction(measured_rectangles, rng, turn);\n        } else if (turn < min(6, T/3)) {\n            // Early attempts - our best strategies\n            if (turn % 3 == 0) {\n                placements = reliableBaseline(measured_rectangles, rng, turn);\n            } else if (turn % 3 == 1) {\n                placements = optimizedPacking(measured_rectangles, rng, turn, T);\n            } else {\n                placements = perimeterReduction(measured_rectangles, rng, turn);\n            }\n        } else if (turn < T/2) {\n            // Middle attempts - varied good strategies\n            if (turn % 4 == 0) {\n                placements = optimizedPacking(measured_rectangles, rng, turn, T);\n            } else if (turn % 4 == 1) {\n                placements = perimeterReduction(measured_rectangles, rng, turn);\n            } else if (turn % 4 == 2) {\n                placements = compactArrangement(measured_rectangles, rng, turn);\n            } else {\n                placements = reliableBaseline(measured_rectangles, rng, turn);\n            }\n        } else if (turn < 3*T/4) {\n            // Later attempts - refinement\n            if (turn % 3 == 0) {\n                placements = optimizedPacking(measured_rectangles, rng, turn, T);\n            } else if (turn % 3 == 1) {\n                placements = compactArrangement(measured_rectangles, rng, turn);\n            } else {\n                placements = reliableBaseline(measured_rectangles, rng, turn);\n            }\n        } else {\n            // Final attempts - our most reliable approach\n            placements = reliableBaseline(measured_rectangles, rng, turn);\n        }\n        \n        // Output placements\n        cout << placements.size() << \"\\n\";\n        for (const auto& p : placements) {\n            cout << p.p << \" \" << p.r << \" \" << (char)p.d << \" \" << p.b << \"\\n\";\n        }\n        cout << flush;\n        \n        // Read measurement result\n        int W_meas, H_meas;\n        cin >> W_meas >> H_meas;\n    }\n    \n    return 0;\n}","ahc041":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MAXN = 1000;\nconst int MAXH = 10;\n\nint N, M, H;\nvector<int> A;\nvector<vector<int>> adj;\n\n// Ultra-simple approach without expensive validation\nvector<int> buildSimpleSolution() {\n    vector<int> parent(N, -1);\n    vector<bool> used(N, false);\n    \n    // Root selection by beauty * degree\n    vector<pair<int, int>> roots;\n    for (int i = 0; i < N; i++) {\n        roots.push_back({A[i] * (int)adj[i].size(), i});\n    }\n    sort(roots.rbegin(), roots.rend());\n    \n    // Build trees\n    for (auto& [score, root] : roots) {\n        if (used[root]) continue;\n        \n        queue<pair<int, int>> q; // {vertex, depth}\n        q.push({root, 0});\n        used[root] = true;\n        \n        while (!q.empty()) {\n            auto [v, depth] = q.front();\n            q.pop();\n            \n            if (depth >= H) continue;\n            \n            // Simple greedy: take neighbors in order they appear\n            vector<pair<int, int>> candidates;\n            for (int u : adj[v]) {\n                if (!used[u]) {\n                    candidates.push_back({A[u], u});\n                }\n            }\n            sort(candidates.rbegin(), candidates.rend());\n            \n            for (auto& [beauty, u] : candidates) {\n                if (!used[u]) {\n                    parent[u] = v;\n                    used[u] = true;\n                    q.push({u, depth + 1});\n                }\n            }\n        }\n    }\n    \n    return parent;\n}\n\n// Even simpler approach\nvector<int> buildAlternativeSolution() {\n    vector<int> parent(N, -1);\n    vector<bool> used(N, false);\n    \n    // Just use vertices in beauty order as roots\n    vector<pair<int, int>> order;\n    for (int i = 0; i < N; i++) {\n        order.push_back({A[i], i});\n    }\n    sort(order.rbegin(), order.rend());\n    \n    for (auto& [beauty, root] : order) {\n        if (used[root]) continue;\n        \n        queue<pair<int, int>> q;\n        q.push({root, 0});\n        used[root] = true;\n        \n        while (!q.empty()) {\n            auto [v, depth] = q.front();\n            q.pop();\n            \n            if (depth >= H) continue;\n            \n            // Simple greedy assignment\n            for (int u : adj[v]) {\n                if (!used[u]) {\n                    parent[u] = v;\n                    used[u] = true;\n                    q.push({u, depth + 1});\n                }\n            }\n        }\n    }\n    \n    return parent;\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    cin >> N >> M >> H;\n    A.resize(N);\n    for (int i = 0; i < N; i++) {\n        cin >> A[i];\n    }\n    \n    adj.resize(N);\n    for (int i = 0; i < M; i++) {\n        int u, v;\n        cin >> u >> v;\n        adj[u].push_back(v);\n        adj[v].push_back(u);\n    }\n    \n    // Read coordinates\n    for (int i = 0; i < N; i++) {\n        int x, y;\n        cin >> x >> y;\n    }\n    \n    // Build solution - just one fast approach\n    vector<int> solution = buildSimpleSolution();\n    \n    // Output\n    for (int i = 0; i < N; i++) {\n        cout << solution[i];\n        if (i < N - 1) cout << \" \";\n    }\n    cout << \"\\n\";\n    \n    return 0;\n}","ahc042":"#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Move {\n    char direction;\n    int index;\n};\n\nvector<Move> moves;\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    int n;\n    cin >> n;\n    \n    vector<vector<char>> board(n, vector<char>(n));\n    vector<pair<int, int>> oni_positions;\n    \n    for (int i = 0; i < n; i++) {\n        string row;\n        cin >> row;\n        for (int j = 0; j < n; j++) {\n            board[i][j] = row[j];\n            if (row[j] == 'x') {\n                oni_positions.push_back({i, j});\n            }\n        }\n    }\n    \n    // For each Oni, find the best way to remove it - with slightly different priority\n    for (auto& pos : oni_positions) {\n        int row = pos.first;\n        int col = pos.second;\n        \n        // Check all directions\n        bool can_up = true;\n        for (int r = 0; r < row; r++) {\n            if (board[r][col] == 'o') {\n                can_up = false;\n                break;\n            }\n        }\n        \n        bool can_down = true;\n        for (int r = row + 1; r < n; r++) {\n            if (board[r][col] == 'o') {\n                can_down = false;\n                break;\n            }\n        }\n        \n        bool can_left = true;\n        for (int c = 0; c < col; c++) {\n            if (board[row][c] == 'o') {\n                can_left = false;\n                break;\n            }\n        }\n        \n        bool can_right = true;\n        for (int c = col + 1; c < n; c++) {\n            if (board[row][c] == 'o') {\n                can_right = false;\n                break;\n            }\n        }\n        \n        // Try a different priority order: up, left, right, down\n        if (can_up) {\n            // Move up: shift column down (row + 1) times, then back\n            for (int i = 0; i < row + 1; i++) moves.push_back({'D', col});\n            for (int i = 0; i < row + 1; i++) moves.push_back({'U', col});\n        } else if (can_left) {\n            // Move left: shift row right (col + 1) times, then back\n            for (int i = 0; i < col + 1; i++) moves.push_back({'R', row});\n            for (int i = 0; i < col + 1; i++) moves.push_back({'L', row});\n        } else if (can_right) {\n            // Move right: shift row left (N - col) times, then back\n            for (int i = 0; i < n - col; i++) moves.push_back({'L', row});\n            for (int i = 0; i < n - col; i++) moves.push_back({'R', row});\n        } else if (can_down) {\n            // Move down: shift column up (N - row) times, then back\n            for (int i = 0; i < n - row; i++) moves.push_back({'U', col});\n            for (int i = 0; i < n - row; i++) moves.push_back({'D', col});\n        }\n    }\n    \n    // Output moves\n    for (const auto& move : moves) {\n        cout << move.direction << \" \" << move.index << \"\\n\";\n    }\n    \n    return 0;\n}","ahc044":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 100;\nconst int L = 500000;\nconst int SIM_STEPS = 1000000;\n\nint T[N];\nint a[N], b[N];\n\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\n\n// Fast simulation\nvector<long long> simulate_sequence(int a[], int b[], int steps = SIM_STEPS) {\n    vector<long long> actual(N, 0);\n    vector<int> count(N, 0);\n    \n    int current = 0;\n    actual[0]++;\n    count[0]++;\n    \n    // Run simulation\n    for (int step = 1; step < steps; step++) {\n        int next;\n        if (count[current] % 2 == 1) {\n            next = a[current];\n        } else {\n            next = b[current];\n        }\n        current = next;\n        actual[current]++;\n        count[current]++;\n    }\n    \n    // Scale to match L weeks\n    for (int i = 0; i < N; i++) {\n        actual[i] = (1LL * actual[i] * L) / steps;\n    }\n    \n    return actual;\n}\n\n// Calculate error\nlong long calculate_error(const vector<long long>& actual) {\n    long long error = 0;\n    for (int i = 0; i < N; i++) {\n        error += abs(actual[i] - (long long)T[i]);\n    }\n    return error;\n}\n\n// Effective initialization\nvoid initialize_assignment() {\n    // Sort employees by target frequency (descending)\n    vector<pair<int, int>> targets;\n    for (int i = 0; i < N; i++) {\n        targets.push_back({T[i], i});\n    }\n    sort(targets.rbegin(), targets.rend());\n    \n    // Create assignment with good distribution properties\n    for (int i = 0; i < N; i++) {\n        int emp = targets[i].second;\n        \n        // Use prime-based deterministic assignment for good distribution\n        a[emp] = targets[(i * 11 + 13) % N].second;\n        b[emp] = targets[(i * 17 + 23) % N].second;\n    }\n    \n    // Strategic adjustments for high-target employees\n    for (int i = 0; i < min(20, N); i++) {\n        int high_emp = targets[i].second;\n        // Make sure this high-target employee is accessible\n        int from_emp = targets[(i + 7) % N].second;\n        if (i % 3 == 0) {\n            a[from_emp] = high_emp;\n        } else if (i % 3 == 1) {\n            b[from_emp] = high_emp;\n        }\n        // else do nothing for variety\n    }\n}\n\n// Efficient improvement with early stopping\nvoid efficient_improvement() {\n    auto actual = simulate_sequence(a, b);\n    long long current_error = calculate_error(actual);\n    \n    // Quick improvement with early stopping\n    int no_improvement_count = 0;\n    const int MAX_NO_IMPROVEMENT = 15;\n    \n    for (int iter = 0; iter < 30 && no_improvement_count < MAX_NO_IMPROVEMENT; iter++) {\n        // Pick random employee\n        int emp = rng() % N;\n        \n        // Save current state\n        int old_a = a[emp];\n        int old_b = b[emp];\n        \n        // Smart modification based on target\n        uniform_int_distribution<int> dist(0, N-1);\n        \n        // Higher probability of pointing to employees with appropriate targets\n        if (T[emp] > L / (2 * N)) {\n            // High target - modify more conservatively\n            a[emp] = dist(rng);\n            b[emp] = dist(rng);\n        } else {\n            // Lower target - more flexible modifications\n            a[emp] = dist(rng);\n            b[emp] = dist(rng);\n        }\n        \n        // Fast evaluation\n        auto new_actual = simulate_sequence(a, b, 600000);\n        long long new_error = calculate_error(new_actual);\n        \n        // Keep if better, otherwise revert\n        if (new_error < current_error) {\n            current_error = new_error;\n            no_improvement_count = 0; // Reset counter\n            actual = new_actual; // Update for next iteration\n        } else {\n            a[emp] = old_a;\n            b[emp] = old_b;\n            no_improvement_count++;\n        }\n    }\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    int n, l;\n    cin >> n >> l;\n    for (int i = 0; i < n; i++) {\n        cin >> T[i];\n    }\n    \n    // Initialize with good strategy\n    initialize_assignment();\n    \n    // Efficient improvement\n    efficient_improvement();\n    \n    // Output result\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\nconst int N = 800;\n\nint n, m, q, L, W;\nvector<int> G;\nvector<int> lx, rx, ly, ry;\n\n// Query function\nvector<pair<int, int>> query(const vector<int>& cities) {\n    if (cities.size() < 2) return {};\n    \n    cout << \"? \" << cities.size();\n    for (int city : cities) cout << \" \" << city;\n    cout << endl;\n    cout.flush();\n    \n    vector<pair<int, int>> result(cities.size() - 1);\n    for (int i = 0; i < cities.size() - 1; i++) {\n        int u, v;\n        cin >> u >> v;\n        if (u > v) swap(u, v);\n        result[i] = {u, v};\n    }\n    return result;\n}\n\n// Calculate center of a city's rectangle\npair<int, int> get_center(int city) {\n    return {(lx[city] + rx[city]) / 2, (ly[city] + ry[city]) / 2};\n}\n\n// Calculate distance between two cities using center points\ndouble city_distance(int city1, int city2) {\n    auto c1 = get_center(city1);\n    auto c2 = get_center(city2);\n    long long dx = (long long)c1.first - c2.first;\n    long long dy = (long long)c1.second - c2.second;\n    return sqrt(dx * dx + dy * dy);\n}\n\n// Group cities using simple but effective sorting\nvector<vector<int>> create_groups() {\n    vector<int> cities(n);\n    for (int i = 0; i < n; i++) cities[i] = i;\n    \n    // Sort by center coordinates\n    sort(cities.begin(), cities.end(), [&](int a, int b) {\n        auto center_a = get_center(a);\n        auto center_b = get_center(b);\n        if (center_a.first != center_b.first) \n            return center_a.first < center_b.first;\n        return center_a.second < center_b.second;\n    });\n    \n    vector<vector<int>> groups;\n    int idx = 0;\n    for (int group_size : G) {\n        vector<int> group;\n        for (int i = 0; i < group_size; i++) {\n            group.push_back(cities[idx++]);\n        }\n        groups.push_back(group);\n    }\n    \n    return groups;\n}\n\n// Use sliding window queries for larger groups - prioritizing larger groups\nvector<pair<int, int>> get_enhanced_mst(const vector<int>& group, int& queries_remaining) {\n    if (group.size() <= 1) return {};\n    if (queries_remaining <= 0) return {};\n    \n    set<pair<int, int>> all_edges;\n    \n    // If group is small enough, query it directly\n    if ((int)group.size() <= L) {\n        queries_remaining--;\n        return query(group);\n    }\n    \n    // Use sliding window for larger groups\n    int window_size = min(L, (int)group.size());\n    int queries_used = 0;\n    int step = max(1, window_size / 2);\n    \n    // For very large groups, use more queries\n    int max_queries = min(20, max(10, (int)(group.size() * 0.8))); // Adaptive query count\n    \n    for (int start = 0; start < (int)group.size() - 1 && queries_remaining > 0 && queries_used < max_queries; start += step) {\n        int end = min(start + window_size, (int)group.size());\n        if (end - start >= 2) {\n            vector<int> window(group.begin() + start, group.begin() + end);\n            auto edges = query(window);\n            for (auto& edge : edges) {\n                all_edges.insert(edge);\n            }\n            queries_remaining--;\n            queries_used++;\n        }\n        if (end == (int)group.size()) break;\n    }\n    \n    return vector<pair<int, int>>(all_edges.begin(), all_edges.end());\n}\n\n// Build MST using Kruskal's algorithm with available edges\nvector<pair<int, int>> build_mst_with_edges(const vector<int>& group, const vector<pair<int, int>>& available_edges) {\n    if (group.size() <= 1) return {};\n    \n    // Map city IDs to local indices\n    unordered_map<int, int> city_to_index;\n    for (int i = 0; i < (int)group.size(); i++) {\n        city_to_index[group[i]] = i;\n    }\n    \n    // Union-Find for MST\n    vector<int> parent(group.size());\n    iota(parent.begin(), parent.end(), 0);\n    \n    function<int(int)> find = [&](int x) {\n        return parent[x] == x ? x : parent[x] = find(parent[x]);\n    };\n    \n    auto unite = [&](int x, int y) {\n        int root_x = find(x);\n        int root_y = find(y);\n        if (root_x == root_y) return false;\n        parent[root_y] = root_x;\n        return true;\n    };\n    \n    // Collect valid edges (connecting cities in this group)\n    vector<tuple<double, int, int>> edges;\n    set<int> group_set(group.begin(), group.end());\n    \n    for (const auto& edge : available_edges) {\n        if (group_set.count(edge.first) && group_set.count(edge.second)) {\n            int local_u = city_to_index[edge.first];\n            int local_v = city_to_index[edge.second];\n            double dist = city_distance(edge.first, edge.second);\n            edges.push_back({dist, local_u, local_v});\n        }\n    }\n    \n    // Add fallback edges (consecutive in group) with high penalty to use only if needed\n    for (int i = 0; i < (int)group.size() - 1; i++) {\n        int local_u = i;\n        int local_v = i + 1;\n        int global_u = group[i];\n        int global_v = group[i + 1];\n        double dist = city_distance(global_u, global_v) + 100000; // High penalty\n        edges.push_back({dist, local_u, local_v});\n    }\n    \n    // Sort edges by distance\n    sort(edges.begin(), edges.end());\n    \n    // Kruskal's algorithm\n    vector<pair<int, int>> mst;\n    for (const auto& edge : edges) {\n        double dist = get<0>(edge);\n        int u = get<1>(edge);\n        int v = get<2>(edge);\n        \n        if (unite(u, v)) {\n            int global_u = group[u];\n            int global_v = group[v];\n            if (global_u > global_v) swap(global_u, global_v);\n            mst.push_back({global_u, global_v});\n            if ((int)mst.size() == (int)group.size() - 1) break;\n        }\n    }\n    \n    return mst;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    \n    // Read input\n    cin >> n >> m >> q >> L >> W;\n    G.resize(m);\n    for (int i = 0; i < m; i++) cin >> G[i];\n    \n    lx.resize(n); rx.resize(n); ly.resize(n); ry.resize(n);\n    for (int i = 0; i < n; i++) {\n        cin >> lx[i] >> rx[i] >> ly[i] >> ry[i];\n    }\n    \n    // Create groups\n    vector<vector<int>> groups = create_groups();\n    \n    // Process each group with enhanced MST building\n    vector<vector<pair<int, int>>> final_edges(m);\n    int queries_remaining = q;\n    \n    for (int g = 0; g < m; g++) {\n        // Get enhanced MST information\n        vector<pair<int, int>> available_edges = get_enhanced_mst(groups[g], queries_remaining);\n        \n        // Build final MST\n        final_edges[g] = build_mst_with_edges(groups[g], available_edges);\n    }\n    \n    // Output answer\n    cout << \"!\" << endl;\n    for (int g = 0; g < m; g++) {\n        // Output group cities\n        for (int i = 0; i < (int)groups[g].size(); i++) {\n            if (i > 0) cout << \" \";\n            cout << groups[g][i];\n        }\n        cout << endl;\n        \n        // Output edges\n        for (const auto& edge : final_edges[g]) {\n            cout << edge.first << \" \" << edge.second << endl;\n        }\n    }\n    \n    return 0;\n}","ahc046":"#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 20;\nconst int M = 40;\nconst int MAX_ACTIONS = 1600;\n\nstruct Point {\n    int r, c;\n    Point() : r(0), c(0) {}\n    Point(int _r, int _c) : r(_r), c(_c) {}\n    bool operator==(const Point& other) const {\n        return r == other.r && c == other.c;\n    }\n    bool operator!=(const Point& other) const {\n        return !(*this == other);\n    }\n};\n\nstruct State {\n    Point pos;\n    int actions_used;\n    bool blocked[N][N];\n    \n    State() : pos(0, 0), actions_used(0) {\n        memset(blocked, false, sizeof(blocked));\n    }\n    \n    State(Point p, int actions) : pos(p), actions_used(actions) {\n        memset(blocked, false, sizeof(blocked));\n    }\n};\n\n// Directions: Up, Down, Left, Right\nconst int DR[] = {-1, 1, 0, 0};\nconst int DC[] = {0, 0, -1, 1};\nconst char DIR_CHARS[] = {'U', 'D', 'L', 'R'};\n\nPoint targets[M];\n\nbool isValid(int r, int c) {\n    return r >= 0 && r < N && c >= 0 && c < N;\n}\n\n// BFS with all three actions considered\nvector<pair<char, char>> findBestPath(Point start, Point target, bool initial_blocked[N][N]) {\n    // For simplicity, let's use a greedy approach that's proven to work\n    vector<pair<char, char>> path;\n    Point current = start;\n    \n    // Copy blocked state\n    bool blocked[N][N];\n    memcpy(blocked, initial_blocked, sizeof(blocked));\n    \n    int maxSteps = 150;\n    int steps = 0;\n    \n    while (current != target && steps < maxSteps) {\n        steps++;\n        \n        // Calculate best direction to move\n        int dr = target.r - current.r;\n        int dc = target.c - current.c;\n        \n        // Try the most direct path first\n        bool moved = false;\n        \n        if (abs(dr) >= abs(dc)) {\n            // Try vertical movement\n            if (dr > 0) {\n                if (isValid(current.r + 1, current.c)) {\n                    if (!blocked[current.r + 1][current.c]) {\n                        path.push_back({'M', 'D'});\n                        current.r++;\n                        moved = true;\n                    } else {\n                        path.push_back({'A', 'D'});\n                        blocked[current.r + 1][current.c] = false;\n                        moved = true;\n                    }\n                }\n            } else if (dr < 0) {\n                if (isValid(current.r - 1, current.c)) {\n                    if (!blocked[current.r - 1][current.c]) {\n                        path.push_back({'M', 'U'});\n                        current.r--;\n                        moved = true;\n                    } else {\n                        path.push_back({'A', 'U'});\n                        blocked[current.r - 1][current.c] = false;\n                        moved = true;\n                    }\n                }\n            }\n            \n            // If vertical didn't work, try horizontal\n            if (!moved) {\n                if (dc > 0) {\n                    if (isValid(current.r, current.c + 1)) {\n                        if (!blocked[current.r][current.c + 1]) {\n                            path.push_back({'M', 'R'});\n                            current.c++;\n                            moved = true;\n                        } else {\n                            path.push_back({'A', 'R'});\n                            blocked[current.r][current.c + 1] = false;\n                            moved = true;\n                        }\n                    }\n                } else if (dc < 0) {\n                    if (isValid(current.r, current.c - 1)) {\n                        if (!blocked[current.r][current.c - 1]) {\n                            path.push_back({'M', 'L'});\n                            current.c--;\n                            moved = true;\n                        } else {\n                            path.push_back({'A', 'L'});\n                            blocked[current.r][current.c - 1] = false;\n                            moved = true;\n                        }\n                    }\n                }\n            }\n        } else {\n            // Try horizontal movement\n            if (dc > 0) {\n                if (isValid(current.r, current.c + 1)) {\n                    if (!blocked[current.r][current.c + 1]) {\n                        path.push_back({'M', 'R'});\n                        current.c++;\n                        moved = true;\n                    } else {\n                        path.push_back({'A', 'R'});\n                        blocked[current.r][current.c + 1] = false;\n                        moved = true;\n                    }\n                }\n            } else if (dc < 0) {\n                if (isValid(current.r, current.c - 1)) {\n                    if (!blocked[current.r][current.c - 1]) {\n                        path.push_back({'M', 'L'});\n                        current.c--;\n                        moved = true;\n                    } else {\n                        path.push_back({'A', 'L'});\n                        blocked[current.r][current.c - 1] = false;\n                        moved = true;\n                    }\n                }\n            }\n            \n            // If horizontal didn't work, try vertical\n            if (!moved) {\n                if (dr > 0) {\n                    if (isValid(current.r + 1, current.c)) {\n                        if (!blocked[current.r + 1][current.c]) {\n                            path.push_back({'M', 'D'});\n                            current.r++;\n                            moved = true;\n                        } else {\n                            path.push_back({'A', 'D'});\n                            blocked[current.r + 1][current.c] = false;\n                            moved = true;\n                        }\n                    }\n                } else if (dr < 0) {\n                    if (isValid(current.r - 1, current.c)) {\n                        if (!blocked[current.r - 1][current.c]) {\n                            path.push_back({'M', 'U'});\n                            current.r--;\n                            moved = true;\n                        } else {\n                            path.push_back({'A', 'U'});\n                            blocked[current.r - 1][current.c] = false;\n                            moved = true;\n                        }\n                    }\n                }\n            }\n        }\n        \n        if (!moved) break;\n    }\n    \n    return path;\n}\n\nint main() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n    \n    int n, m;\n    cin >> n >> m;\n    \n    Point start;\n    cin >> start.r >> start.c;\n    \n    for (int i = 0; i < m; i++) {\n        cin >> targets[i].r >> targets[i].c;\n    }\n    \n    vector<pair<char, char>> actions;\n    Point current = start;\n    bool blocked[N][N];\n    memset(blocked, false, sizeof(blocked));\n    \n    for (int i = 1; i < m; i++) {\n        Point target = targets[i];\n        auto path = findBestPath(current, target, blocked);\n        actions.insert(actions.end(), path.begin(), path.end());\n        current = target;\n    }\n    \n    for (auto& action : actions) {\n        cout << action.first << \" \" << action.second << \"\\n\";\n    }\n    \n    return 0;\n}"}}}