File tree 1 file changed +12
-13
lines changed
1 file changed +12
-13
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ package main
3
3
import (
4
4
. "fmt"
5
5
"io"
6
+ "slices"
6
7
)
7
8
8
9
// https://space.bilibili.com/206214
@@ -23,26 +24,24 @@ func p160(in io.Reader, out io.Writer) {
23
24
Fscan (in , & a [i ].v )
24
25
}
25
26
26
- f := make ([][]int , 1 , n + 1 )
27
- f [0 ] = make ([]int , W + 1 )
28
- var dfs func (int ) int
29
- dfs = func (v int ) int {
27
+ var dfs func (int , []int ) ([]int , int )
28
+ dfs = func (v int , pre []int ) ([]int , int ) {
30
29
size := 1
30
+ t := pre
31
31
for _ , w := range g [v ] {
32
- size += dfs (w )
32
+ f , sz := dfs (w , t )
33
+ t = f
34
+ size += sz
33
35
}
34
- t := f [len (f )- size ]
35
- cur := append (t [:0 :0 ], t ... )
36
+ f := slices .Clone (pre )
36
37
p := a [v ]
37
- t = f [len (f )- 1 ]
38
38
for j := W ; j >= p .w ; j -- {
39
- cur [j ] = max (cur [j ], t [j - p .w ]+ p .v )
39
+ f [j ] = max (f [j ], t [j - p .w ]+ p .v )
40
40
}
41
- f = append (f , cur )
42
- return size
41
+ return f , size
43
42
}
44
- dfs (0 )
45
- Fprint (out , f [n ][ W ])
43
+ f , _ := dfs (0 , make ([] int , W + 1 ) )
44
+ Fprint (out , f [W ])
46
45
}
47
46
48
47
//func main() { p160(bufio.NewReader(os.Stdin), os.Stdout) }
You can’t perform that action at this time.
0 commit comments