QOJ.ac

QOJ

时间限制: 1.0 s 内存限制: 1024 MB 总分: 100 可 Hack ✓

#18602. Distributed Systems

统计

A company has $n$ servers, numbered from $1$ to $n$. The $i$-th server runs $a_i$ services.

Sometimes servers may shut down, so a backup server has been defined for each server. For a server with number $i$, the backup is the server with number $p_i$. If for the $i$-th server $p_i = i$, then it is a high-reliability server and it never shuts down.

For any two distinct servers $i$ and $j$, their backup server numbers $p_i$ and $p_j$ are distinct. Thus, $p$ is a permutation of length $n$, meaning each number from $1$ to $n$ appears exactly once among the values $p_1, \dots, p_n$.

The server shutdown process occurs as follows: if server $i$ shuts down, all services running on it are moved to the server with number $p_i$, and server $i$ is replaced by a new server on which no services are running. The number of this server and the number of its backup server remain unchanged. The transfer of services and the replacement of the server is a very fast process, during which no new shutdowns can occur.

The company plans to conduct a system performance test. For this, at most $k$ servers will be shut down. The shutdowns are performed sequentially, meaning no two servers are shut down simultaneously. Determine the maximum number of services that can end up on a single server after shutting down at most $k$ servers.

Input

The first line contains two integers $n$ and $k$ ($1 \leqslant k < n \leqslant 10^5$) — the number of servers and the maximum number of servers that can be shut down.

The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \leqslant a_i \leqslant 10^9$) — the number of services running on the servers.

The third line contains $n$ integers $p_1, p_2, \dots, p_n$ ($1 \leqslant p_i \leqslant n$) — the numbers of the backup servers.

Output

Output a single integer — the answer to the problem.

Subtasks

Subtask Points Constraints Required Subtasks
1 15 $n \leqslant 1000, k = 1$
2 27 $n \leqslant 1000$ 1
3 21 $p_i = i \pmod n + 1$
4 37 1, 2, 3

Examples

Input 1

4 2
6 10 7 9
2 3 4 1

Output 1

26

Input 2

3 1
1000000000 993 2010
1 3 2

Output 2

1000000000

Input 3

11 5
3 5 12 7 5 9 2 6 0 9 4
2 8 9 6 5 11 3 1 10 7 4

Output 3

23

Note

Consider the order of server shutdowns that allows achieving the maximum answer in the first example.

Recall how service transfers are carried out when servers shut down:

Server 1 2 3 4
Backup 2 3 4 1

The second server shuts down first; its services move to the third server, so now there are $10 + 7 = 17$ services on the third server.

The third server shuts down second; its services move to the fourth server, after which there are $9 + 17 = 26$ services on the fourth server.

For a better understanding, see the table below, which records the number of services on each server during the process described above.

Stage $a_1$ $a_2$ $a_3$ $a_4$
Before the first shutdown 6 10 7 9
After shutting down server 2 6 0 17 9
After shutting down server 3 6 0 0 26

If the third server had shut down first, and the second one second, the process would look like this:

Stage $a_1$ $a_2$ $a_3$ $a_4$
Before the first shutdown 6 10 7 9
After shutting down server 3 6 10 0 16
After shutting down server 2 6 0 10 16

In this case, the maximum number of services on a server would be 16, which is not the optimal answer.

In the second example, one of the possible options is that no server shuts down. Then there are $1000000000$ services on the first server, which is the answer to the problem. If server 2 or 3 shuts down, the maximum number of services will also be on the first server.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.