#include #include #include using namespace std; int n1, n2; vector a; vector DP; int n0,sa,n_min; bool solve(int sum,int first); int main() { int d,m; cin >> d; for (int i = 0; i < d; i++) { cin >> n1 >> n2; cin >> m; a.resize(m); for (int j = 0; j < m; j++) { cin >> a[j]; } sort(a.begin(), a.end()); n0 = n1 + n2; n_min = min(n1, n2); int a_sum=0, ans=0; for (int j = 0; j < a.size(); j++) { if (a_sum + a[j] <= n0) { a_sum += a[j]; ans = j + 1; } else { a.erase(a.begin() + j); j--; } } sa = n0 - a_sum; DP.assign(100010, 100); cout << ans - !solve(0, 0) << endl; } return 0; } bool solve(int sum,int first) { for (int i = first; i < a.size(); i++) { int tmp; tmp = sum + a[i]; if ((tmp >= n1 - sa&&tmp <= n1) || (tmp >= n2 - sa&&tmp <= n2)) { return true; } else if (tmp > n_min) break; if (DP[tmp] < i) continue; else { DP[tmp] = i; if (solve(tmp,i+1)) return true; } } return false; }