goated solution frfr. The cleanest code you will ever witness
A very easy hard. Y\'all be laughing at my if condition spam but this beats like 95% KEKW\n\n# Code\n\nvector<int> place_coins(int node, int parent, vector<int>
A very easy hard. Y\'all be laughing at my if condition spam but this beats like 95% KEKW\n\n# Code\n```\nvector<int> place_coins(int node, int parent, vector<int>& cost, vector<vector<int>>& adj_list, vector<long long> &coins) {\n    vector<int> optimal_numbers = {-1, -1, -1, 1, 1, 1};\n    for (int neighbor : adj_list[node]) {\n        if (neighbor == parent) {\n            continue;\n        }\n        vector<int> result = place_coins(neighbor, node, cost, adj_list, coins);\n        if (result[0] > optimal_numbers[0]) {\n            optimal_numbers[2] = optimal_numbers[1];\n            optimal_numbers[1] = optimal_numbers[0];\n            optimal_numbers[0] = result[0];\n        } else if (result[0] > optimal_numbers[1]) {\n            optimal_numbers[2] = optimal_numbers[1];\n            optimal_numbers[1] = result[0];\n        } else if (result[0] > optimal_numbers[2]) {\n            optimal_numbers[2] = result[0];\n        }\n\n        if (result[1] > optimal_numbers[1]) {\n            optimal_numbers[2] = optimal_numbers[1];\n            optimal_numbers[1] = result[1];\n        } else if (result[1] > optimal_numbers[2]) {\n            optimal_numbers[2] = result[1];\n        }\n\n        if (result[2] > optimal_numbers[2]) {\n            optimal_numbers[2] = result[2];\n        }\n\n        if (result[3] < optimal_numbers[3]) {\n            optimal_numbers[4] = optimal_numbers[3];\n            optimal_numbers[3] = result[3];\n        } else if (result[3] < optimal_numbers[4]) {\n            optimal_numbers[4] = result[3];\n        }\n\n        if (result[4] < optimal_numbers[4]) {\n            optimal_numbers[4] = result[4];\n        }\n\n        optimal_numbers[5] += result[5];\n    }\n\n    int node_cost = cost[node];\n    if (node_cost >= 0) {\n        if (node_cost > optimal_numbers[0]) {\n            optimal_numbers[2] = optimal_numbers[1];\n            optimal_numbers[1] = optimal_numbers[0];\n            optimal_numbers[0] = node_cost;\n        } else if (node_cost > optimal_numbers[1]) {\n            optimal_numbers[2] = optimal_numbers[1];\n            optimal_numbers[1] = node_cost;\n        } else if (node_cost > optimal_numbers[2]) {\n            optimal_numbers[2] = node_cost;\n        }\n    } else {\n        if (node_cost < optimal_numbers[3]) {\n            optimal_numbers[4] = optimal_numbers[3];\n            optimal_numbers[3] = node_cost;\n        } else if (node_cost < optimal_numbers[4]) {\n            optimal_numbers[4] = node_cost;\n        }\n    }\n\n    // if subtree has size less than 3, place 1 coin\n    if (optimal_numbers[5] < 3) {\n        coins[node] = 1;\n    } else {\n        // option 1: 3 positives\n        long long three_positives = -1;\n        if (optimal_numbers[2] >= 0) {\n            three_positives = (long long)optimal_numbers[0] * (long long)optimal_numbers[1] * (long long)optimal_numbers[2];\n        }\n        // option 2: 1 positive 2 negatives\n        long long one_pos_two_neg = -1;\n        if (optimal_numbers[0] >= 0 && optimal_numbers[4] < 0) {\n            one_pos_two_neg = (long long)optimal_numbers[0] * (long long)optimal_numbers[3] * (long long)optimal_numbers[4];\n        }\n        long long highest = max(three_positives, one_pos_two_neg);\n        coins[node] = max(highest, (long long) 0);\n    }\n    return optimal_numbers;\n}\n\nclass Solution {\npublic:\n    vector<long long> placedCoins(vector<vector<int>>& edges, vector<int>& cost) {\n        vector<vector<int>> adj_list(cost.size());\n        for (vector<int> &edge : edges) {\n            adj_list[edge[0]].push_back(edge[1]);\n            adj_list[edge[1]].push_back(edge[0]);\n        }\n        vector<long long> coins(cost.size());\n        place_coins(0, -1, cost, adj_list, coins);\n        return coins;\n    }\n};\n```
['C++']