@@ -150,25 +150,254 @@ tags:
150150#### Python3
151151
152152``` python
153-
153+ class Solution :
154+ def maxProfit (
155+ self ,
156+ n : int ,
157+ present : List[int ],
158+ future : List[int ],
159+ hierarchy : List[List[int ]],
160+ budget : int ,
161+ ) -> int :
162+ max = lambda a , b : a if a > b else b
163+ g = [[] for _ in range (n + 1 )]
164+ for u, v in hierarchy:
165+ g[u].append(v)
166+
167+ def dfs (u : int ):
168+ nxt = [[0 , 0 ] for _ in range (budget + 1 )]
169+ for v in g[u]:
170+ fv = dfs(v)
171+ for j in range (budget, - 1 , - 1 ):
172+ for jv in range (j + 1 ):
173+ for pre in (0 , 1 ):
174+ val = nxt[j - jv][pre] + fv[jv][pre]
175+ if val > nxt[j][pre]:
176+ nxt[j][pre] = val
177+
178+ f = [[0 , 0 ] for _ in range (budget + 1 )]
179+ price = future[u - 1 ]
180+
181+ for j in range (budget + 1 ):
182+ for pre in (0 , 1 ):
183+ cost = present[u - 1 ] // (pre + 1 )
184+ if j >= cost:
185+ f[j][pre] = max (nxt[j][0 ], nxt[j - cost][1 ] + (price - cost))
186+ else :
187+ f[j][pre] = nxt[j][0 ]
188+
189+ return f
190+
191+ return dfs(1 )[budget][0 ]
154192```
155193
156194#### Java
157195
158196``` java
159-
197+ class Solution {
198+ private List<Integer > [] g;
199+ private int [] present;
200+ private int [] future;
201+ private int budget;
202+
203+ public int maxProfit (int n , int [] present , int [] future , int [][] hierarchy , int budget ) {
204+ this . present = present;
205+ this . future = future;
206+ this . budget = budget;
207+
208+ g = new ArrayList [n + 1 ];
209+ Arrays . setAll(g, k - > new ArrayList<> ());
210+
211+ for (int [] e : hierarchy) {
212+ g[e[0 ]]. add(e[1 ]);
213+ }
214+
215+ return dfs(1 )[budget][0 ];
216+ }
217+
218+ private int [][] dfs (int u ) {
219+ int [][] nxt = new int [budget + 1 ][2 ];
220+
221+ for (int v : g[u]) {
222+ int [][] fv = dfs(v);
223+ for (int j = budget; j >= 0 ; j-- ) {
224+ for (int jv = 0 ; jv <= j; jv++ ) {
225+ for (int pre = 0 ; pre < 2 ; pre++ ) {
226+ int val = nxt[j - jv][pre] + fv[jv][pre];
227+ if (val > nxt[j][pre]) {
228+ nxt[j][pre] = val;
229+ }
230+ }
231+ }
232+ }
233+ }
234+
235+ int [][] f = new int [budget + 1 ][2 ];
236+ int price = future[u - 1 ];
237+
238+ for (int j = 0 ; j <= budget; j++ ) {
239+ for (int pre = 0 ; pre < 2 ; pre++ ) {
240+ int cost = present[u - 1 ] / (pre + 1 );
241+ if (j >= cost) {
242+ f[j][pre] = Math . max(nxt[j][0 ], nxt[j - cost][1 ] + (price - cost));
243+ } else {
244+ f[j][pre] = nxt[j][0 ];
245+ }
246+ }
247+ }
248+
249+ return f;
250+ }
251+ }
160252```
161253
162254#### C++
163255
164256``` cpp
165-
257+ class Solution {
258+ public:
259+ int maxProfit(int n, vector<int >& present, vector<int >& future, vector<vector<int >>& hierarchy, int budget) {
260+ vector<vector<int >> g(n + 1);
261+ for (auto& e : hierarchy) {
262+ g[ e[ 0]] .push_back(e[ 1] );
263+ }
264+
265+ auto dfs = [&](const auto& dfs, int u) -> vector<array<int, 2>> {
266+ vector<array<int, 2>> nxt(budget + 1);
267+ for (int j = 0; j <= budget; j++) nxt[j] = {0, 0};
268+
269+ for (int v : g[u]) {
270+ auto fv = dfs(dfs, v);
271+ for (int j = budget; j >= 0; j--) {
272+ for (int jv = 0; jv <= j; jv++) {
273+ for (int pre = 0; pre < 2; pre++) {
274+ int val = nxt[j - jv][pre] + fv[jv][pre];
275+ if (val > nxt[j][pre]) {
276+ nxt[j][pre] = val;
277+ }
278+ }
279+ }
280+ }
281+ }
282+
283+ vector<array<int , 2 >> f (budget + 1);
284+ int price = future[ u - 1] ;
285+
286+ for (int j = 0; j <= budget; j++) {
287+ for (int pre = 0; pre < 2; pre++) {
288+ int cost = present[u - 1] / (pre + 1);
289+ if (j >= cost) {
290+ f[j][pre] = max(nxt[j][0], nxt[j - cost][1] + (price - cost));
291+ } else {
292+ f[j][pre] = nxt[j][0];
293+ }
294+ }
295+ }
296+
297+ return f;
298+ };
299+
300+ return dfs(dfs, 1)[budget][0];
301+ }
302+ };
166303```
167304
168305#### Go
169306
170307```go
308+ func maxProfit(n int, present []int, future []int, hierarchy [][]int, budget int) int {
309+ g := make([][]int, n+1)
310+ for _, e := range hierarchy {
311+ u, v := e[0], e[1]
312+ g[u] = append(g[u], v)
313+ }
314+
315+ var dfs func(u int) [][2]int
316+ dfs = func(u int) [][2]int {
317+ nxt := make([][2]int, budget+1)
318+
319+ for _, v := range g[u] {
320+ fv := dfs(v)
321+ for j := budget; j >= 0; j-- {
322+ for jv := 0; jv <= j; jv++ {
323+ for pre := 0; pre < 2; pre++ {
324+ nxt[j][pre] = max(nxt[j][pre], nxt[j-jv][pre]+fv[jv][pre])
325+ }
326+ }
327+ }
328+ }
329+
330+ f := make([][2]int, budget+1)
331+ price := future[u-1]
332+
333+ for j := 0; j <= budget; j++ {
334+ for pre := 0; pre < 2; pre++ {
335+ cost := present[u-1] / (pre + 1)
336+ if j >= cost {
337+ buyProfit := nxt[j-cost][1] + (price - cost)
338+ f[j][pre] = max(nxt[j][0], buyProfit)
339+ } else {
340+ f[j][pre] = nxt[j][0]
341+ }
342+ }
343+ }
344+ return f
345+ }
346+
347+ return dfs(1)[budget][0]
348+ }
349+ ```
171350
351+ #### TypeScript
352+
353+ ``` ts
354+ function maxProfit(
355+ n : number ,
356+ present : number [],
357+ future : number [],
358+ hierarchy : number [][],
359+ budget : number ,
360+ ): number {
361+ const g: number [][] = Array .from ({ length: n + 1 }, () => []);
362+
363+ for (const [u, v] of hierarchy ) {
364+ g [u ].push (v );
365+ }
366+
367+ const dfs = (u : number ): number [][] => {
368+ const nxt: number [][] = Array .from ({ length: budget + 1 }, () => [0 , 0 ]);
369+
370+ for (const v of g [u ]) {
371+ const fv = dfs (v );
372+ for (let j = budget ; j >= 0 ; j -- ) {
373+ for (let jv = 0 ; jv <= j ; jv ++ ) {
374+ for (let pre = 0 ; pre < 2 ; pre ++ ) {
375+ nxt [j ][pre ] = Math .max (nxt [j ][pre ], nxt [j - jv ][pre ] + fv [jv ][pre ]);
376+ }
377+ }
378+ }
379+ }
380+
381+ const f: number [][] = Array .from ({ length: budget + 1 }, () => [0 , 0 ]);
382+ const price = future [u - 1 ];
383+
384+ for (let j = 0 ; j <= budget ; j ++ ) {
385+ for (let pre = 0 ; pre < 2 ; pre ++ ) {
386+ const cost = Math .floor (present [u - 1 ] / (pre + 1 ));
387+ if (j >= cost ) {
388+ const profitIfBuy = nxt [j - cost ][1 ] + (price - cost );
389+ f [j ][pre ] = Math .max (nxt [j ][0 ], profitIfBuy );
390+ } else {
391+ f [j ][pre ] = nxt [j ][0 ];
392+ }
393+ }
394+ }
395+
396+ return f ;
397+ };
398+
399+ return dfs (1 )[budget ][0 ];
400+ }
172401```
173402
174403<!-- tabs:end -->
0 commit comments