My solution
\n\n# Approach\nMaintaining two things:\n1. multiset of 3 maximum elements\n2. multiset of 2 min elements (for cosidering case with 2 -ve elements and 1 positiv
\n\n# Approach\nMaintaining two things:\n1. multiset of 3 maximum elements\n2. multiset of 2 min elements (for cosidering case with 2 -ve elements and 1 positive element) \nres = max(product of all elements in 1st multiset,\n          product of all(2) elements of 2nd element and max element\n          in 1st multiset)\n\n\n# Code\n```\n#include<bits/stdc++.h>\ntypedef long long ll;\nclass Solution {\npublic:\n    vector<ll> res;\n    void solve(int root, int par, multiset<ll>& mn, multiset<ll>& mx, vector<vector<ll>>& graph, vector<int>& cost) {\n        multiset<ll> mnlocal, mxlocal;\n        mxlocal.insert(cost[root]); mnlocal.insert(cost[root]);\n\n        for(int i=0; i<graph[root].size(); i++) {\n            if(graph[root][i]!=par){\n                solve(graph[root][i], root, mnlocal, mxlocal, graph, cost);\n            }\n        }\n        \n        if(mxlocal.size()==3){\n            ll ans=1;\n            for(auto it=mxlocal.begin(); it!=mxlocal.end(); it++){\n                ans=ans*(*it);\n            }\n            ll ans2=1;\n            for(auto it=mnlocal.begin(); it!=mnlocal.end(); it++){\n                ans2=ans2*(*it);\n            }\n            ans2=ans2*(*mxlocal.rbegin());\n            ans=max(ans, ans2);\n            if(ans<=0){\n                res[root]=0;\n            } else \n            res[root]=ans;\n        } else {\n            res[root]=1;\n        }\n\n        for(auto it=mxlocal.begin(); it!=mxlocal.end(); it++){\n                mx.insert(*it);\n                if(mx.size()>3){\n                    mx.erase(mx.begin());\n                }\n            }\n            for(auto it=mnlocal.begin(); it!=mnlocal.end(); it++){\n                mn.insert(*it);\n                if(mn.size()>2){\n                    mn.erase(mn.find(*mn.rbegin()));\n                }\n            }\n        \n    }\n    vector<long long> placedCoins(vector<vector<int>>& edges, vector<int>& cost) {\n        vector<vector<ll>> graph(cost.size());\n        res.resize(cost.size());\n        for(ll i=0; i<edges.size(); i++) {\n            graph[edges[i][0]].push_back(edges[i][1]);\n            graph[edges[i][1]].push_back(edges[i][0]);\n        }\n        multiset<ll> mn, mx;\n        // mx.insert(cost[0]); mn.insert(cost[0]);\n        solve(0, -1, mn, mx, graph, cost);\n        if(mx.size()==3){\n            ll ans=1;\n            for(auto it=mx.begin(); it!=mx.end(); it++){\n                ans=ans*(*it);\n                // cout<<*it<<" ; ";\n            }\n            ll ans2=1;\n            for(auto it=mn.begin(); it!=mn.end(); it++){\n                ans2=ans2*(*it);\n            }\n            ans2=ans2*(*mx.rbegin());\n            ans=max(ans, ans2);\n            if(ans<=0){\n                res[0]=0;\n            } else \n            res[0]=ans;\n        } else {\n            res[0]=1;\n        }\n        return res;\n    }\n};\n```
['C++']