5515.Mana Collection

Time Limit: 4s Memory Limit: 256MB

Bessie has recently taken an interest in magic and needs to collect mana for a very important spell. Bessie has $N (1≤N≤18)$ mana pools, the ith of which accumulates $m_i$ mana per second $(1≤m_i≤10^8)$. The pools are linked by a collection of $M (0≤M≤N(N−1))$ directed edges $(a_i,b_i,t_i)$, meaning that she can travel from $a_i$ to $b_i$ in $t_i$ seconds $(1≤a_i,b_i≤N, a_i \neq b_i, 1≤t_i≤10^9)$. Whenever Bessie is present at a pool, she can collect all the mana stored at that location, emptying it. At time $0$, all mana pools are empty, and Bessie can select any pool to start at.

Answer $Q (1≤Q≤2⋅10^5)$ queries, each specified by two integers $s$ and $e (1≤s≤10^9, 1≤e≤N)$. For each query, determine the maximum amount of mana Bessie can collect in $s$ seconds if she must be at mana pool $e$ at the end of the $s$th second.

Input Format(From the terminal/stdin)

First line contains $N$ and $M$.
Next line contains $m_1,m_2,…,m_N$.

Next $M$ lines contain $a_i,b_i,t_i$. No ordered pair $(a_i,b_i)$ appears more than once in the input.

Next line contains $Q$.

Next $Q$ lines contain two integers $s$ and $e$.

Output Format(To the terminal/stdout)

$Q$ lines, one for each query.

Sample Input 1

Copy
2 1
1 10
1 2 10
4
5 1
5 2
100 1
100 2
 · \n
 ·  \n
 · ·  \n
 \n
 · \n
 · \n
   · \n
   · \n

Sample Output 1

Copy
5
50
100
1090
 \n
  \n
   \n
    \n

Sample Input 2

Copy
4 8
50000000 100000000 20000000 70000000
1 2 20
2 1 50
2 3 90
1 3 40
3 1 10
4 1 25
1 4 5
4 3 70
3
8 3
1000000000 1
500000 4
 · \n
        ·         ·        ·        \n
 · ·  \n
 · ·  \n
 · ·  \n
 · ·  \n
 · ·  \n
 · ·  \n
 · · \n
 · ·  \n
 \n
 · \n
          · \n
      · \n

Sample Output 2

Copy
160000000
239999988050000000
119992550000000
         \n
                  \n
               \n

Hints

For sample input 1

First query: Bessie takes 5 mana from pool 1 after 5 seconds.

Second query: Bessie takes 50 mana from pool 2 after 5 seconds.

Third query: Bessie takes 100 mana from pool 1 after 100 seconds.

Fourth query: Bessie takes 90 mana from pool 1 after 90 seconds and 1000 mana from pool 2 after 100 seconds.

For sample input 2

An example where Bessie is able to collect much larger amounts of mana.

Source: USACO 2023 Jan, Platinum

Submit

请先 登录

© 2025 FAQs