Spectral::Cup 2026 Round 2 (Codeforces Round 1100, Div. 1 + Div. 2)
G
道路施工
Roadworks
Codeforces
3000
原题
← FH →

道路施工AI (CI翻译)

In an under-construction village, $n$ houses have been built in a row numbered from $1$ to $n$. House $i$ has hospitality $h_i$.

在一个正在建设中的村庄里,$n$ 栋房屋沿直线排列,编号为 $1$ 到 $n$。第 $i$ 栋房屋的舒适度为 $h_i$。AI (CI翻译)

The village has $n - 1$ roads, where road $i$ connects houses $i$ and $i + 1$ and will be built on day $d_i$. Initially, no roads are built.

村庄中有 $n - 1$ 条道路,其中道路 $i$ 连接房屋 $i$ 和 $i + 1$,并将在第 $d_i$ 天建成。起初,所有道路都尚未建成。AI (CI翻译)

You start at house $x$ and will stay in the village from day $1$ to day $k$, initially with a satisfaction of $0$. On day $s$, the following happens in order:

你从房屋 $x$ 出发,将在村庄中度过第 $1$ 天到第 $k$ 天,初始满意度为 $0$。第 $s$ 天会按以下顺序发生事件:AI (CI翻译)

  • All roads $i$ with $d_i = s$ are built;
  • You may move to an adjacent house, if the road to it has been built, or stay at your current house;
  • Your satisfaction increases by $h_j$, where $j$ is the house you are currently at.
  • 建成所有满足 $d_i = s$ 的道路 $i$;
  • 如果通往相邻房屋的道路已经建成,你可以移动到相邻房屋,也可以留在当前房屋;
  • 你的满意度增加 $h_j$,其中 $j$ 是你当前所在的房屋编号。
AI (CI翻译)

Find the maximum satisfaction you can achieve after $k$ days.

请计算在第 $k$ 天结束后,你能够获得的最大满意度。AI (CI翻译)

Input

输入格式

AI (CI翻译)

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

每个测试包含多个测试用例。第一行包含测试用例的数量 $t$($1 \le t \le 10^4$)。接下来是各测试用例的描述。AI (CI翻译)

The first line of each test case contains three integers $n$, $k$ and $x$ ($2 \le n \le 2 \cdot 10^5$, $1 \le k \le 10^9$, $1 \le x \le n$) — the number of houses, the number of days and the starting house, respectively.

每个测试用例的第一行包含三个整数 $n$、$k$ 和 $x$($2 \le n \le 2 \cdot 10^5$,$1 \le k \le 10^9$,$1 \le x \le n$),分别表示房屋数量、天数和起始房屋编号。AI (CI翻译)

The second line contains $n$ integers $h_1, h_2, \ldots, h_n$ ($0 \le h_i \le 10^9$) — the hospitality of each house.

第二行包含 $n$ 个整数 $h_1, h_2, \ldots, h_n$($0 \le h_i \le 10^9$),表示每栋房屋的舒适度。AI (CI翻译)

The third line contains $n - 1$ integers $d_1, d_2, \ldots, d_{n - 1}$ ($1 \le d_i \le k$) — the day each road is built.

第三行包含 $n - 1$ 个整数 $d_1, d_2, \ldots, d_{n - 1}$($1 \le d_i \le k$),表示每条道路建成的日期。AI (CI翻译)

It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.

保证所有测试用例的 $n$ 之和不超过 $2 \cdot 10^5$。AI (CI翻译)

Output

输出格式

AI (CI翻译)

For each test case, output a single integer — the maximum satisfaction you can achieve after $k$ days.

对于每个测试用例,输出一个整数,表示在第 $k$ 天结束后能够获得的最大满意度。AI (CI翻译)

Note

说明

AI (CI翻译)

In the first test case, the following is one optimal sequence of moves:

在第一个测试用例中,以下是一种最优的移动方案:AI (CI翻译)

  • You start at house $x = 3$ with a satisfaction of $0$.
  • On day $1$, no roads are built yet, so you must remain at house $3$. Your satisfaction becomes $3$.
  • On day $2$, road $3$ is built. You move to house $4$ and remain there on days $2$, $3$, $4$, $5$, $6$ and $7$. Your satisfaction becomes $33$. During this time, roads $2$ and $4$ are also built.
  • On day $8$, you move back to house $3$. Your satisfaction becomes $36$.
  • On day $9$, you move to house $2$. Your satisfaction becomes $38$.
  • On day $10$, road $1$ is built. You move to house $1$. Your satisfaction becomes $52$.
  • 你从房屋 $x = 3$ 出发,初始满意度为 $0$。
  • 第 $1$ 天还没有道路建成,因此你必须留在房屋 $3$。满意度变为 $3$。
  • 第 $2$ 天,道路 $3$ 建成。你移动到房屋 $4$,并在第 $2$、$3$、$4$、$5$、$6$ 和 $7$ 天一直留在那里。满意度变为 $33$。在此期间,道路 $2$ 和道路 $4$ 也建成了。
  • 第 $8$ 天,你移动回房屋 $3$。满意度变为 $36$。
  • 第 $9$ 天,你移动到房屋 $2$。满意度变为 $38$。
  • 第 $10$ 天,道路 $1$ 建成。你移动到房屋 $1$。满意度变为 $52$。
AI (CI翻译)

It can be shown that it is impossible to achieve a satisfaction greater than $52$.

可以证明,不可能获得超过 $52$ 的满意度。AI (CI翻译)

In the second test case, you cannot reach house $4$ within $8$ days, so the maximum achievable satisfaction is $0$.

在第二个测试用例中,你无法在 $8$ 天内到达房屋 $4$,因此能够获得的最大满意度为 $0$。AI (CI翻译)

In the third test case, you can immediately move to house $2$ and remain there for $1\,000\,000\,000$ days.

在第三个测试用例中,你可以立即移动到房屋 $2$,并在那里停留 $1\,000\,000\,000$ 天。AI (CI翻译)

样例

Input #1
4
5 10 3
14 2 3 5 6
10 6 2 7
4 8 1
0 0 0 1
7 1 2
2 1000000000 1
1 1000000000
1
9 27 6
17 13 5 8 14 3 4 17 20
10 1 2 13 3 15 6 23
Output #1
52
0
1000000000000000000
386
InputCopy OutputCopy
题目信息
题号
2229G
时间限制
3 s
内存限制
512 MB
标签
二分
数据结构
动态规划
贪心
双指针

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。