LearnToCP
Sign in
Navigation
HomeRoadmapProblemsAbout Us
Theory
Contest Knowledge
Selecting an IDEInteractive TasksOutput-Only Tasks
Basics
Your First ProgramData types and IOC++ syntaxModuloFunctionsVectorsMatricesTime Complexity
Sorting
SortingCounting sortRadix Sort
Optimization Techniques
Two PointersSum of numbers 1 to nPrefix sumBinary SearchGreedyBinary Search FunctionsBinary Search by AnswerDivide and Conquer
Binary Numbers
Binary NumbersNumbers in codeBitwise OperationsBitmasks
Math
Binary ExponentiationPrime NumbersPrime FactorizationGCD and LCMSieve of EratosthenesModified Sieve
Data Structures
StringsStackQueueMapsSetsPriority QueueCustom Criteria for FunctionsSegment TreesFenwick TreesSparse TablesUnion FindSqrt Decomposition
Combinatorics
Addition PrincipleMultiplication PrincipleCombinatoric ObjectsInclusion Exclusion Principle
Geometry
Geometry BasicsVectorsCross and Dot ProductLinesPolygonsAnglesPoint in PolygonDistances and Intersection PointsConvex HullCircles
Recursion
PointersRecursionGenerating Combinatoric Objects
Dynamic Programming
About DPDP problemsTree DPBitmask DP
Graph Theory
GraphsDFS and BFSShortest PathsTreesTopological SortingDijkstra's AlgorithmMinimum Spanning TreesShortest Path Algorithms
Advanced Graph Theory
BiconnectivityStrongly Connected ComponentsBipartite GraphGraph FlowAugmenting PathsFlow - Minimum Cut DualityHeavy-Light DecompositionCentroid Decomposition
Advanced Data Structures
2D and 3D Segment TreesLazy PropagationImplicit Segment TreesPersistent Segment TreesLowest Common AncestorTrieBalanced Binary Search TreesMo's Algorithm

Woodcutter II

MediumProblem #58
Time LimitMemoryInputOutput
1 s64 MBstdinstdout

Same problem as Woodcutter, but counting the wood tree by tree is now too slow. The forest never changes between orders.

Milan the woodcutter has opened a business. His saw sits on a stand that can be set to any integer height in meters, and it cuts every tree in the forest at exactly that height. Only the part of a tree above the blade falls down - a tree that is not taller than the blade is left untouched.

Now q customers come in, one after another, and each one orders some amount of wood. Milan still cares about the forest, so for every order he wants to set the saw as high as possible while still getting at least the ordered amount.

The orders are independent - Milan plans each of them for the same untouched forest, so the tree heights never change.

Input

First line of input will be a single integer t - the number of testcases.
The first line of each testcase contains two integers n and q - the number of trees in the forest and the number of orders.
The second line contains n integers h1​,h2​,…,hn​ - the heights of the trees.
The third line contains q integers x1​,x2​,…,xq​ - the ordered amounts of wood.

Output

For every order print a single line with the highest height at which the saw can be set.

Example

Input
2
5 3
24 21 19 14 22
14 40 1
1 2
7
7 3
Output
18
12
23
0
4

The first forest has 100 meters of wood in total. For an order of 14 meters the saw goes to 18; for 40 meters it has to drop to 12, where all five trees together give exactly 40; and a single meter is enough to take off the tallest tree alone, with the blade at 23.

Constraints

1≤t≤5
1≤n≤105
1≤q≤105
1≤hi​≤109
1≤xj​≤h1​+h2​+⋯+hn​ - there is always enough wood in the forest


This problem was adapted, with permission, from Drva, authored by Društvo matematičara Srbije and Fondacija Petlja.

Submit your solution

Sign in to submit your solution and track your progress.

Sign in to submit