Processing math: 100%

USACO 2023 January Contest, Platinum

Problem 2. Mana Collection


Contest has ended.

Log in to allow submissions in analysis mode


**Note: The time limit for this problem is 5s, 2.5 times the default. The memory limit for this problem is 512MB, twice the default.**

Bessie has recently taken an interest in magic and needs to collect mana for a very important spell. Bessie has N (1N18) mana pools, the ith of which accumulates mi mana per second (1mi108). The pools are linked by a collection of M (0MN(N1)) directed edges (ai,bi,ti), meaning that she can travel from ai to bi in ti seconds (1ai,biN, aibi, 1ti109). 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 (1Q2105) queries, each specified by two integers s and e (1s109, 1eN). 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 sth second.

INPUT FORMAT (input arrives from the terminal / stdin):

First line contains N and M.

Next line contains m1,m2,,mN.

Next M lines contain ai,bi,ti. No ordered pair (ai,bi) appears more than once in the input.

Next line contains Q.

Next Q lines contain two integers s and e.

OUTPUT FORMAT (print output to the terminal / stdout):

Q lines, one for each query.

SAMPLE INPUT:

2 1
1 10
1 2 10
4
5 1
5 2
100 1
100 2

SAMPLE OUTPUT:

5
50
100
1090

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.

SAMPLE INPUT:

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

SAMPLE OUTPUT:

160000000
239999988050000000
119992550000000

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

SCORING:

  • Inputs 3-4: N10,Q100
  • Inputs 5-9: N10
  • Inputs 10-14: Q100
  • Inputs 15-17: N=16
  • Inputs 18-20: N=17
  • Inputs 21-24: No additional constraints.

Problem credits: Benjamin Qi

Contest has ended. No further submissions allowed.