Simple & Understandable solutions.
Intuition\n Describe your first thoughts on how to solve this problem. \n1. Take a list of postitive and a other list of negative value.\n2. While calculating t
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n1. Take a list of postitive and a other list of negative value.\n2. While calculating the final cost do a brute force to check all combination.\n3. As we will take the list of only 3 numbers. \n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    vector<long long> ans;\n    vector<int> G[20003];\n    long long calculateMaxValue(pair<priority_queue<long long, vector<long long> , greater<long long>>, priority_queue<long long, vector<long long> , greater<long long>>> &costs) {\n        vector<long long> tempValue;\n        priority_queue<long long, vector<long long> , greater<long long>> positives = costs.first;\n        priority_queue<long long, vector<long long> , greater<long long>> negatives = costs.second;\n        while(positives.size()>0) {\n            tempValue.push_back(positives.top());\n            positives.pop();\n        }\n        while(negatives.size()>0) {\n            tempValue.push_back(-1*negatives.top());\n            negatives.pop();\n        }\n        if(tempValue.size()<3) {\n            return 1;\n        }\n        long long tAns = 0;\n        for(int i=0;i<tempValue.size();i++) {\n            for(int j=i+1;j<tempValue.size();j++) {\n                for(int k=j+1;k<tempValue.size();k++) {\n                    tAns = max(tAns, tempValue[i] * tempValue[j] * tempValue[k]);\n                }\n            }\n        }\n        return tAns;\n    }\n    pair<priority_queue<long long, vector<long long> , greater<long long>>, priority_queue<long long, vector<long long> , greater<long long>>> traverseTree(int node,vector<int>& cost, int prevNode) {\n        pair<priority_queue<long long, vector<long long> , greater<long long>>, priority_queue<long long, vector<long long> , greater<long long>>> costs;\n        for(int i=0;i<G[node].size();i++) {\n            int v = G[node][i];\n            if(v != prevNode) {\n                pair<priority_queue<long long, vector<long long> , greater<long long>>, priority_queue<long long, vector<long long> , greater<long long>>> child = traverseTree(v, cost, node);\n                while(!child.first.empty()) {\n                    if(costs.first.size()<3) {\n                        costs.first.push(child.first.top());\n                    } else {\n                        costs.first.push(child.first.top());\n                        costs.first.pop();\n                    }\n                    child.first.pop();\n                }\n                while(!child.second.empty()) {\n                    if(costs.second.size()<3) {\n                        costs.second.push(child.second.top());\n                    } else {\n                        costs.second.push(child.second.top());\n                        costs.second.pop();\n                    }\n                    child.second.pop();\n                }\n            }\n        }\n        if(cost[node]>=0) {\n            if(costs.first.size()<3) {\n                costs.first.push(cost[node]);\n            } else {\n                costs.first.push(cost[node]);\n                costs.first.pop();\n            }\n        } else {\n            if(costs.second.size()<3) {\n                costs.second.push(-1*cost[node]);\n            } else {\n                costs.second.push(-1*cost[node]);\n                costs.second.pop();\n            }\n        }\n        ans[node] = calculateMaxValue(costs);\n        return costs;\n    }\n    vector<long long> placedCoins(vector<vector<int>>& edges, vector<int>& cost) {\n        for(int i=0;i<=edges.size();i++) {\n            ans.push_back(0);\n            if(i<edges.size()) {\n                G[edges[i][0]].push_back(edges[i][1]);\n                G[edges[i][1]].push_back(edges[i][0]);\n            }\n        }\n        traverseTree(0, cost, 0);\n        return ans;\n    }\n};\n/*\n\n\n*/\n```
['Depth-First Search', 'Heap (Priority Queue)', 'C++']