I found this problem by doing a sample test. The problem was that we have given him a tree that is not addressed. We can start from any node of our choice. Initially we have the power "P" and, when moving from one node to another, we lose some power "X" (what is considered as the cost of the trip) and we get some "Y" gains. So, we must say that what is the maximum gain we can obtain with a given power?

Example: the first line contains the number of nodes and the initial power

The following lines n-1 contain node-node-cost-benefit

5 4

1 2 1 2

1 3 2 3

1 4 2 4

4 5 2 2

Answer => 7. We can start from 4 and go to 1 and then to 3.

I have applied DFS in this to obtain the maximum benefit obtained when traveling each route.

But is there any way to reduce time?

```
from collections import defaultdict
class tree:
def __init__(self,nodes):
self.nodes = nodes
self.graph = defaultdict(list)
def add(self,a,b,charge,profit):
self.graph(a).append((b,charge,profit))
self.graph(b).append((a,charge,profit))
def start(self,power):
maxi = -1
visited = (False for i in range(self.nodes))
for i in range(1,self.nodes+1):
powers = power
visited(i-1) = True
for j in self.graph(i):
temp = self.dfs(j,powers,0,visited)
if temp > maxi:
maxi = temp
visited(i-1) = False
return maxi
def dfs(self,node,powers,profit,visited):
v,p,pro=node(0),node(1),node(2)
if powers-p < 0:
return 0
if powers-p == 0:
return profit + pro
profit += pro
powers = powers-p
visited(v-1) = True
tempo = profit
for k in self.graph(v):
if visited(k(0)-1) == False:
temp = self.dfs(k,powers,tempo,visited)
if temp > profit:
profit = temp
visited(v-1) = False
return profit
t = tree(5)
t.add(1,2,1,2)
t.add(1,3,2,3)
t.add(1,4,2,4)
t.add(4,5,2,2)
print(t.start(4))
```