Drying Laundry

시간 제한3초메모리 제한1024 MB

요약
주어진 줄 길이 L마다 각 시트를 한 줄에만 걸어 느리게 말릴지 두 줄에 걸어 빠르게 말릴지 정하고, 말리는 시간의 최댓값을 최소로 만든다.
난이도

어려움10점 중 8점

유형
이분 탐색, 동적 계획법, 그리디, 수학
정답자
아직 제출이 없습니다

문제

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. On week jj, he has NN freshly washed bed sheets that he wants to dry by hanging them on two parallel clotheslines of length L_jL\_j each. The sheets can be hung next to each other but must not overlap. Each sheet is d_id\_i units wide and rather long, therefore he will always orient it so that it will take up d_id\_i units of the line when hung to dry. The sheets have different drying times that are not related to their sizes because of different materials. Thus, the ii-th sheet needs tslow_it^\text{slow}\_i minutes to dry. However, if it is hung over both lines at the same time, it dries quicker in tfast_it^\text{fast}\_i minutes, but also takes up space on the other line. To avoid smelly sheets, Harry the Beaver has to start drying all of them immediately after washing, i.e. all sheets have to be hung simultaneously.

Harry the Beaver wants to go to sleep as soon as possible on Sundays, therefore, he asks you to help him determine the minimal required drying time for each week jj, or inform him that it is impossible to finish drying the sheets that week.

입력

The first line contains an integer NN, the number of sheets, and an integer QQ, the number of weeks until the end of the tourist season. The next NN lines contain space-separated integers d_id\_i, tfast_it^\text{fast}\_i, and tslow_it^\text{slow}\_i, which correspond to the width, the shorter drying time, and the longer drying time of the ii-th sheet, respectively. The final QQ lines the the input contain integers L_jL\_j, jj-th of which represents the length of the clothesline for week jj.

출력

Print QQ lines, with jj-th of them containing the minimal required drying time for week jj, or "-1" (without the quotes) if it is impossible to finish drying the sheets that week. If the minimal required drying time for a particular week is longer than the number of minutes in a week, you should still output that drying time rather than -1.

제한

  • 1≤N≤3⋅1041 \leq N \leq 3 \cdot 10^4
  • 1≤Q≤3⋅1051 \leq Q \leq 3 \cdot 10^5
  • 1≤d_i≤3⋅1051 \leq d\_i \leq 3 \cdot 10^5 for all 1≤i≤N1 \leq i \leq N
  • 1≤tfast_i≤tslow_i≤1091 \leq t^\text{fast}\_i \leq t^\text{slow}\_i \leq 10^9 for all 1≤i≤N1 \leq i \leq N
  • 1≤L_j≤3⋅1051 \leq L\_j \leq 3 \cdot 10^5 for all 1≤j≤Q1 \leq j \leq Q

예제1

  1. 예제 1

    입력
    3 3
    1 2 2
    1 1 4
    2 3 100
    3
    1
    4
    
    예상 출력
    4
    -1
    3