package main import ( "bufio" "fmt" "os" "sort" "strconv" ) var sc = bufio.NewScanner(os.Stdin) var wr = bufio.NewWriter(os.Stdout) func out(x ...interface{}) { fmt.Fprintln(wr, x...) } func getI() int { sc.Scan() i, e := strconv.Atoi(sc.Text()) if e != nil { panic(e) } return i } func getF() float64 { sc.Scan() i, e := strconv.ParseFloat(sc.Text(), 64) if e != nil { panic(e) } return i } func getInts(N int) []int { ret := make([]int, N) for i := 0; i < N; i++ { ret[i] = getI() } return ret } func getS() string { sc.Scan() return sc.Text() } // min, max, asub, absなど基本関数 func max(a, b int) int { if a > b { return a } return b } func min(a, b int) int { if a < b { return a } return b } func asub(a, b int) int { if a > b { return a - b } return b - a } func abs(a int) int { if a >= 0 { return a } return -a } func lowerBound(a []int, x int) int { idx := sort.Search(len(a), func(i int) bool { return a[i] >= x }) return idx } func upperBound(a []int, x int) int { idx := sort.Search(len(a), func(i int) bool { return a[i] > x }) return idx } const inf = 99 var a []int var m, n int var dp [][]int var r []int func rec(m, n int) { if m == 0 { return } x := dp[m][n] if n-a[m-1] >= 0 { y := dp[m-1][n-a[m-1]] if y+1 == x { r = append([]int{a[m-1]}, r...) rec(m-1, n-a[m-1]) return } } rec(m-1, n) } func g(N, N1 int) int { n = m + 1 dp = make([][]int, n) for i := 0; i < n; i++ { dp[i] = make([]int, 110000) } dp[0][0] = 0 for i := 1; i < n; i++ { for j := 0; j <= N; j++ { dp[i][j] = dp[i-1][j] if j-a[i-1] < 0 { continue } dp[i][j] = max(dp[i-1][j], dp[i-1][j-a[i-1]]+1) } // out(dp[i][:N+1], dp[i][N]) } r = make([]int, 0, m) rec(m, N) idx := 0 cnt := 0 tot := 0 // b := make([]int, 0) for _, e := range a { if idx < len(r) && e == r[idx] { idx++ continue } // b = append(b, e) if tot+e <= N1 { tot += e cnt++ } } return (len(r) + cnt) } func f() { n1, n2 := getI(), getI() m = getI() a = getInts(m) sort.Ints(a) ret := g(n1, n2) ret = max(g(n2, n1), ret) out(ret) } func main() { defer wr.Flush() sc.Split(bufio.ScanWords) sc.Buffer([]byte{}, 1000000) // this template is new version. // use getI(), getS(), getInts(), getF() d := getI() for i := 0; i < d; i++ { f() } }