#P17018. [ROI 2026 Day1] 分布式系统
[ROI 2026 Day1] 分布式系统
Problem Description
A company has servers, numbered from to . Server is running services.
Servers may fail, so each server is assigned a backup server. The backup server of server is . If , then this server is a high-reliability server and will never fail.
For any two different servers and , their backup server numbers and are different. Therefore, is a permutation of length , meaning each number from to appears exactly once among .
Failures are handled as follows: if server fails, all services running on it are transferred to server , and server 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 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 failures.
Input Format
The first line contains two integers and (), representing the total number of servers and the maximum number of servers that may fail.
The second line contains integers (), representing the initial number of services running on each server.
The third line contains integers (), 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 services.
Next, let server 3 fail. Its services are transferred to server 4, so server 4 now has services.
For easier understanding, refer to the table below, which records the number of services on each server during the process above.
| Stage | ||||
|---|---|---|---|---|
| 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 | ||||
|---|---|---|---|---|
| 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 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 | Additional Constraints | Dependencies | |
|---|---|---|---|---|
| 1 | 15 | -- | ||
| 2 | 27 | -- | 1 | |
| 3 | 21 | -- | -- | |
| 4 | 37 | -- | 1, 2, 3 |
Translated by DeepSeek V4 Pro.
Translated by ChatGPT 5