Maximal Sum
Time limit2sMemory limit512 MB
For each query value b_j, find the maximum sum of a contiguous segment of a whose elements are all at least b_j, or 0 if none exists.
- Level
Medium7 of 10
- Topics
- Sorting, Divide and conquer, Dynamic programming, Prefix sum
- Solved
- No attempts yet
Problem
Marty wants to get back to the future from the past. The computer in his time machine is broken, so he has to work out the numbers himself and type them in.
Marty has two integer arrays: of length and of length . For each he needs the largest possible sum over the segments whose elements are all greater than or equal to .
A segment is never empty, so holds and the largest sum can be negative. If no element of is greater than or equal to , no segment satisfies the condition.
Input
The first line contains two integers and (), the sizes of the arrays and .
The second line contains integers ().
The third line contains integers ().
Output
Print integers on one line, separated by single spaces. The -th number is the largest segment sum for , or if no segment satisfies the condition.