No.2423 Merge Stones
Problem Statement
$N$ magic stones are arranged on a circle, numbered $1, 2, …, N$ in counterclockwise order. The magic power of stone $i$ is $A_i$.
Each magic stone also has a color. The color of a magic stone is represented by an integer, and the color of stone $i$ is $C_i$.
You may perform the following operation any number of times.
- Choose $2$ adjacent magic stones whose color integers differ by at most $K$, and merge them. If the magic powers of the stones before merging are $x$ and $y$, the magic power of the merged stone is $x + y$. The color of the merged stone can be chosen to be either of the colors of the stones before merging.
The relative positions of the magic stones do not change before and after merging.
After performing all operations, you may choose $1$ of the arranged magic stones.
Find the maximum possible magic power of the stone you can choose.
Constraints
- $1 \leq N \leq 300$
- $0 \leq K \leq 5$
- $1 \leq A_i \leq 10^{9}$
- $1 \leq C_i \leq 50$
- All input values are integers.
Input
The input is given from Standard Input in the following format.
$N$ $K$ $A_1$ $A_2$ ... $A_N$ $C_1$ $C_2$ ... $C_N$
Output
Output the answer to the problem on a single line.
Sample
Sample 1
Input
6 1 10 10 5 10 38 5 3 4 1 3 6 2
Output
40
You can choose a magic stone with magic power $40$ by the following procedure.
From the $2$nd operation onward, the arrangement of the stones is considered counterclockwise, starting from the stone merged in the immediately preceding operation.
- Merge the $1$st and $2$nd stones in counterclockwise order, and set the color to $3$. Counterclockwise from the merged stone, the magic powers are $(20, 5, 10, 38, 5)$ and the colors are $(3, 1, 3, 6, 2)$.
- Merge the $1$st and $5$th stones in counterclockwise order, and set the color to $2$. Counterclockwise from the merged stone, the magic powers are $(25, 5, 10, 38)$ and the colors are $(2, 1, 3, 6)$.
- Merge the $1$st and $2$nd stones in counterclockwise order, and set the color to $2$. Counterclockwise from the merged stone, the magic powers are $(30, 10, 38)$ and the colors are $(2, 3, 6)$.
- Merge the $1$st and $2$nd stones in counterclockwise order, and set the color to $3$. Counterclockwise from the merged stone, the magic powers are $(40, 38)$ and the colors are $(3, 6)$.
- End the operations and choose the magic stone with magic power $40$.
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。
LaFolia13
sepa38
Seed57_cash