Under construction

Still being built. Things may change

Active Preparation for the Battle

Time limit2sMemory limit1024 MB

Summary
Given n people with strength p and alert cost c, for each query x find the minimum total alert cost needed to reach total strength at least x, or -1 if impossible.
Level

Hard9 of 10

Topics
Dynamic programming, Divide and conquer, Greedy
Solved
No attempts yet

Problem

Carnage wants to fight Venom, but first he needs to build up his strength. He learned that eating certain people has a positive effect on symbiote abilities, and the stronger and more capable the person, the more useful they are to eat. However, people tend to resist. A person who is quick enough may raise an alarm before they are dealt with, drawing the attention of the police and Detective Patrick Mulligan.

Carnage's strength PP is currently 1, the police suspect nothing, and their alert level CC is 0. Carnage can reach nn people, and the ii-th person is described by a pair (pi,ci)(p_i, c_i). When Carnage attempts to eat the ii-th person, one of two things happens:

  • If P≥piP \geq p_i, Carnage is strong enough to deal with the victim silently, and his strength increases by pip_i without raising the police alert level.
  • Otherwise, the person is still eaten (and PP increases by pip_i), but the police alert level increases by cic_i.

Carnage can hunt only one target at a time, and each person can be eaten at most once. Carnage chooses the order in which he hunts the victims.

Carnage does not know how dangerous Venom is, so he makes qq assumptions about Venom's strength. The ii-th assumption is a potential strength xix_i for Venom. For each assumption, find the minimum police alert level that can result when Carnage reaches strength P≥xiP \geq x_i, or determine that such strength cannot be reached.

Input

The first line contains two integers nn and qq, the number of potential victims and the number of Carnage's assumptions about Venom's strength (1≤n≤1051 \leq n \leq 10^5; 1≤q≤1061 \leq q \leq 10^6).

Each of the next nn lines describes a person. The ii-th line contains two integers pip_i and cic_i separated by a space: the strength level of the ii-th person and the potential increase in police attention when hunting them (1≤pi≤1091 \leq p_i \leq 10^9; 1≤ci≤1091 \leq c_i \leq 10^9).

The next line contains qq integers x1,x2,…,xqx_1, x_2, \ldots, x_q separated by spaces. xix_i describes Carnage's ii-th assumption about Venom's strength (1≤xi≤1091 \leq x_i \leq 10^9). Carnage's memory sometimes fails him, so the same value may appear several times in this sequence.

Output

Print qq numbers separated by spaces. The ii-th number is the answer for the ii-th assumption. If it is impossible to gather strength at least xix_i, the answer is −1-1. Otherwise, the answer is the minimum police alert level after Carnage gathers that strength.

Examples2

  1. Example 1

    Input
    6 5
    8 10
    10 10
    4 4
    2 3
    8 8
    8 5
    3 7 32 36 36
    
    Expected output
    3 4 5 5 5
    
  2. Example 2

    Input
    9 10
    9 8
    3 1
    2 1
    8 9
    3 1
    5 2
    3 1
    4 1
    10 10
    75 53 82 22 6 27 21 86 14 35
    
    Expected output
    -1 -1 -1 1 1 1 1 -1 1 1