#P17018. [ROI 2026 Day1] 分布式系统

    ID: 19308 远端评测题 1000ms 1024MiB 尝试: 0 已通过: 0 显示难度普及+/提高− 上传者: 标签>图论2026双指针 two-pointerROI(俄罗斯)

[ROI 2026 Day1] 分布式系统

Problem Description

A company has nn servers, numbered from 11 to nn. Server ii is running aia_i services.

Servers may fail, so each server is assigned a backup server. The backup server of server ii is pip_i. If pi=ip_i = i, then this server is a high-reliability server and will never fail.

For any two different servers ii and jj, their backup server numbers pip_i and pjp_j are different. Therefore, pp is a permutation of length nn, meaning each number from 11 to nn appears exactly once among p1,,pnp_1, \ldots, p_n.

Failures are handled as follows: if server ii fails, all services running on it are transferred to server pip_i, and server ii is replaced by a brand-new server that runs no services. The server number and its backup server number remain unchanged. The service transfer and server replacement happen very quickly, and no new failure will occur during this period.

The company plans to test the system's capacity. For this, at most kk servers will be made to fail. Failures happen one by one, i.e., no two servers fail at the same time. Compute the maximum possible number of services that can appear on a single server after at most kk failures.

Input Format

The first line contains two integers nn and kk (1k<n1051 \le k < n \le 10^5), representing the total number of servers and the maximum number of servers that may fail.

The second line contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n (0ai1090 \le a_i \le 10^9), representing the initial number of services running on each server.

The third line contains nn integers p1,p2,,pnp_1, p_2, \ldots, p_n (1pin1 \le p_i \le n), representing the backup server number of each server.

Output Format

Output one integer, representing the answer.

4 2
6 10 7 9
2 3 4 1
26
3 1
1000000000 993 2010
1 3 2
1000000000
11 5
3 5 12 7 5 9 2 6 0 9 4
2 8 9 6 5 11 3 1 10 7 4
23

Hint

Explanation

Consider a failure order that can achieve the maximum answer in the first sample.

The table below shows the backup relationships of the servers:

Server 1 2 3 4
Backup 2 3 4 1

First, let server 2 fail. Its services are transferred to server 3, so server 3 now has 10+7=1710 + 7 = 17 services.

Next, let server 3 fail. Its services are transferred to server 4, so server 4 now has 9+17=269 + 17 = 26 services.

For easier understanding, refer to the table below, which records the number of services on each server during the process above.

Stage a1a_1 a2a_2 a3a_3 a4a_4
Before the first failure 6 10 7 9
After server 2 fails 0 17
After server 3 fails 0 26

If we first let server 3 fail, and then let server 2 fail, the process is as follows:

Stage a1a_1 a2a_2 a3a_3 a4a_4
Before the first failure 6 10 7 9
After server 3 fails 0 16
After server 2 fails 0 10

At this time, the maximum number of services on a single server is 16, which is not the optimal answer.

In the second sample, one possible plan is that no server fails. Then server 1 has 10000000001\,000\,000\,000 services, which is the answer. If server 2 or server 3 fails, the maximum number of services still appears on server 1.

Subtasks

Subtask Score nn Additional Constraints Dependencies
1 15 n1000n \le 1000 k=1k = 1 --
2 27 -- 1
3 21 -- pi=imodn+1p_i = i \bmod n + 1 --
4 37 -- 1, 2, 3

Translated by DeepSeek V4 Pro.

Translated by ChatGPT 5