C++
\n# Code\n\nclass Solution {\npublic:\n    vector<long long> placedCoins(vector<vector<int>>& edges, vector<int>& cost) {\n        vector<long long> ans(cost.si
\n# Code\n```\nclass Solution {\npublic:\n    vector<long long> placedCoins(vector<vector<int>>& edges, vector<int>& cost) {\n        vector<long long> ans(cost.size(), 1);\n        vector<vector<int>> adj(cost.size());\n        for (auto edge: edges)\n        {\n            adj[edge[0]].push_back(edge[1]);\n            adj[edge[1]].push_back(edge[0]);\n        }\n        placeCoins(-1, 0, adj, cost, ans);\n        return ans;\n    }\n\n    vector<int> placeCoins(\n        int par,\n        int cur,\n        vector<vector<int>>& adj,\n        vector<int>& cost,\n        vector<long long>& res) {\n        vector<int> best {cost[cur]};\n        for (auto &it: adj[cur])\n        {\n            if (it == par) continue;\n            auto sub = placeCoins(cur, it, adj, cost, res);\n            best.insert(best.end(), sub.begin(), sub.end());\n        }\n\n        sort(best.begin(), best.end());\n        if (best.size() < 3)\n        {\n            res[cur] = 1;\n            return best;\n        }\n\n        int n = best.size();\n        res[cur] = max(\n            {\n                0LL,\n                1LL * best[n - 1] * best[n - 2] * best[n - 3],\n                1LL * best[0] * best[1] * best[n - 1] });\n        if (best.size() <= 5) return best;\n        else{\n            vector<int> toBeReturned;\n            toBeReturned.insert(toBeReturned.end(), best.begin(), best.begin() + 2);\n            toBeReturned.insert(toBeReturned.end(), best.end() - 3, best.end());\n            return toBeReturned;\n        }\n    }\n\n};\n```
['C++']