DFS + python3
track the 3 biggest positive numbers and the 3 smallest negative numbers, what remains to be done is rather straightforward\n\n# Code\n\nclass Solution:\n    de
track the 3 biggest positive numbers and the 3 smallest negative numbers, what remains to be done is rather straightforward\n\n# Code\n```\nclass Solution:\n    def placedCoins(self, edges: List[List[int]], cost: List[int]) -> List[int]:\n        n = len(edges)+1\n        graph = {i:set([]) for i in range(n)}\n        for u,v in edges:\n            graph[u].add(v)\n            graph[v].add(u)\n        self.res = [1 for i in range(n)]\n        def dfs(node,parent):\n            pos = []\n            neg = []\n            if cost[node] >= 0:\n                pos.append(cost[node])\n            else:\n                neg.append(cost[node])\n            for child in graph[node]:\n                if child == parent:\n                    continue\n                else:\n                    posc,negc = dfs(child,node)\n                    pos += posc\n                    neg += negc\n                    if pos:pos.sort(reverse = True)\n                    if neg:neg.sort()\n                    pos = pos[:min(3,len(pos))]\n                    neg = neg[:min(3,len(neg))]\n            if len(pos) + len(neg) < 3:\n                self.res[node] = 1\n            else:\n                cres = 0\n                if len(pos) == 3:\n                    cres = max(cres,pos[0]*pos[1]*pos[2])\n                if len(pos)>=1 and len(neg)>=2:\n                    cres = max(cres,pos[0]*neg[0]*neg[1])\n                self.res[node] = cres\n            return pos,neg\n        dfs(0,None)\n        return self.res\n\n\n```
['Python3']