Longest Subsequence Queries

4.3

3 votes
, Algorithms, Binary Search
Problem

You are given a positive integer array A1,A2,...,AN of length N and Q queries. In each query, given an positive integer K find the length of the longest subsequence with sum of elements less than K?

INPUT FORMAT

The first line contains an integer, t - denoting the number of test cases.

The first line of each test case contains two integers n and q - denoting the length of the array A, and the number of queries.

The second line of each test case contains n positive integers — elements of A.

Then q lines follow, each line contains a single positive integer k for corresponding query.

OUTPUT FORMAT

For each test case, print the answer for each query in a new line.

 Constraints

1<=t<=1000

1<=n,q<=105, both sum of n  and  sum of q  across all test cases <= 105

0Ai1012

0K1018

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

For first query K=8, maximum length is 2 with subsequence [5,2].

For second query K=2, there is no subsequence with sum < 2.

For third query K=20, maximum length is 3 with subsequence [5, 7, 2].

Editor Image

?