Under construction

Still being built. Things may change

Drying Laundry

Time limit3sMemory limit1024 MB

Summary
Each sheet either hangs on one line (slow time) or spans both lines (fast time). For each line length, find the minimal time to dry all sheets, or report -1.
Level

Hard8 of 10

Topics
Dynamic programming, Sorting, Binary search
Solved
No attempts yet

Problem

Harry the Beaver runs a hotel and has to wash bed sheets every Sunday night for the next QQ weeks until the tourist season ends. In week jj, he has NN freshly washed bed sheets and wants to dry them by hanging them on two parallel clotheslines, each of length LjL_j. Sheets can hang next to each other but must not overlap. Each sheet is did_i units wide and quite long, so he always hangs it so that it takes up did_i units of a line. The sheets have different drying times because their materials differ, and those times do not depend on their sizes. Sheet ii needs tislowt^{\text{slow}}_i minutes when hung on one line. If it is hung over both lines at the same time, it dries in tifastt^{\text{fast}}_i minutes, but it also takes up space on the other line. Harry wants to go to sleep as early as possible on Sundays. All sheets must start drying right after washing, so all of them must be hung at the same time. Find the minimal drying time for each week jj, or report that the sheets cannot all be dried that week.

Input

The first line contains two integers NN and QQ, the number of sheets and the number of weeks until the end of the tourist season. Each of the next NN lines contains three space-separated integers did_i, tifastt^{\text{fast}}_i, and tislowt^{\text{slow}}_i, the width, the shorter drying time, and the longer drying time of sheet ii. Each of the final QQ lines contains one integer LjL_j, the length of the clotheslines in week jj.

Output

Print QQ lines. The jj-th line contains the minimal required drying time for week jj, or -1 if it is impossible to finish drying the sheets that week. If the minimal drying time is longer than the number of minutes in a week, print it anyway instead of -1.

Constraints

  • 1≤N≤3⋅1041 \leq N \leq 3 \cdot 10^4
  • 1≤Q≤3⋅1051 \leq Q \leq 3 \cdot 10^5
  • 1≤di≤3⋅1051 \leq d_i \leq 3 \cdot 10^5 for all 1≤i≤N1 \leq i \leq N
  • 1≤tifast≤tislow≤1091 \leq t^{\text{fast}}_i \leq t^{\text{slow}}_i \leq 10^9 for all 1≤i≤N1 \leq i \leq N
  • 1≤Lj≤3⋅1051 \leq L_j \leq 3 \cdot 10^5 for all 1≤j≤Q1 \leq j \leq Q

Examples1

  1. Example 1

    Input
    3 3
    1 2 2
    1 1 4
    2 3 100
    3
    1
    4
    
    Expected output
    4
    -1
    3