#P16573. [USACO26OPEN] Haybale Stacks

    ID: 16547 Type: RemoteJudge 2500ms 256MiB Tried: 0 Accepted: 0 Difficulty: 8 Uploaded By: Tags>动态规划 DP数学USACO2026

[USACO26OPEN] Haybale Stacks

题目描述

Note: The time limit for this problem is 2.5s2.5\text{s}.

Farmer John has NN stacks of haybales (1N51051 \le N \le 5 \cdot 10^5), where the ithi\text{th} stack contains aia_i haybales (1ai1091 \le a_i \le 10^9). He wants to remove all of these haybales and has MM (1M25001 \le M \le 2500) cows available to help him. If hired, the ithi\text{th} cow will repeat the following sis_i times (1si1001 \le s_i \le 100) for a cost of cic_i (1ci1091 \le c_i \le 10^9):

  • If the stack contains at least pip_i haybales (1pi1091 \le p_i \le 10^9), then the cow will remove one haybale.
  • If the stack contains less than pip_i haybales, the cow does nothing.

For each stack, FJ wants to remove all of the haybales in it. He will do this by hiring cows in sequence (possibly the same cow more than once) until the stack becomes empty. Help FJ determine for each stack the minimum cost to empty it.

输入格式

The first line contains TT (1T1001 \le T \le 100), the number of independent tests. Each test is formatted as follows:

The first line contains an integer NN. The second line contains NN integers, a1,a2,,aNa_1, a_2, \ldots, a_N.

The third line contains an integer MM. Then the next MM lines will contain pi,si,cip_i, s_i, c_i.

It is guaranteed that the cows will be able to remove all the haybales in every stack. Additionally, it is guaranteed that the sum of NN over all tests does not exceed 51055 \cdot 10^5, and the sum of MM over all tests does not exceed 25002500.

输出格式

For each test, print NN space-separated integers, the ithi\text{th} integer being the cost of removing all the haybales in the ithi\text{th} stack.

2
3
15 100 10
4
101 1 1
1 4 8
9 3 5
15 2 3
3
15 100 10
4
101 1 1
1 1 5
9 1 8
15 1 3
29 155 21
73 328 50

提示

First test: For the last stack of initial size 1010, we can hire cow 33 once, which costs 55 and will remove haybales twice (not thrice because the number of haybales turns to 88 after the second one is removed). Then we can hire cow 22 twice, removing the 88 haybales, resulting in no haybales left. The total cost is 5+8+8=215 + 8 + 8 = 21.

Second test: This satisfies max(s)=1\max(s) = 1.

SCORING

  • Inputs 22-33: ai100a_i \le 100
  • Inputs 44-55: max(s)=1\max(s) = 1
  • Inputs 66-99: max(s)4\max(s) \le 4
  • Inputs 1010-1515: max(s)20\max(s) \le 20
  • Inputs 1616-2121: No additional constraints.