QOJ.ac

QOJ

時間限制: 1.0 s 記憶體限制: 1024 MB 總分: 100 可 Hack ✓

#18602. 分布式系统

统计

一家公司有 $n$ 台服务器,编号从 $1$ 到 $n$。第 $i$ 台服务器上运行着 $a_i$ 个服务。

有时服务器可能会关闭,因此每台服务器都定义了一个备份服务器。对于编号为 $i$ 的服务器,其备份是编号为 $p_i$ 的服务器。如果第 $i$ 台服务器的 $p_i = i$,则它是一台高可靠性服务器,永远不会关闭。

对于任意两个不同的服务器 $i$ 和 $j$,它们的备份服务器编号 $p_i$ 和 $p_j$ 互不相同。因此,$p$ 是一个长度为 $n$ 的排列,意味着每个 $1$ 到 $n$ 之间的数在 $p_1, \dots, p_n$ 中恰好出现一次。

服务器关闭过程如下:如果服务器 $i$ 关闭,则其上运行的所有服务都被转移到编号为 $p_i$ 的服务器上,并且服务器 $i$ 被替换为一台没有运行任何服务的新服务器。该服务器的编号及其备份服务器编号保持不变。服务的转移和服务器的替换是一个极快的过程,在此期间不会发生新的关闭。

该公司计划进行一次系统性能测试。为此,最多将关闭 $k$ 台服务器。关闭是依次执行的,即没有两台服务器同时关闭。确定在关闭最多 $k$ 台服务器后,可能出现在同一台服务器上的最大服务数量。

输入格式

第一行包含两个整数 $n$ 和 $k$($1 \leqslant k < n \leqslant 10^5$)——服务器的数量和最多可以关闭的服务器数量。

第二行包含 $n$ 个整数 $a_1, a_2, \dots, a_n$($0 \leqslant a_i \leqslant 10^9$)——各服务器上运行的服务数量。

第三行包含 $n$ 个整数 $p_1, p_2, \dots, p_n$($1 \leqslant p_i \leqslant n$)——备份服务器的编号。

输出格式

输出一个整数——问题的答案。

子任务

子任务 分值 数据范围 所需子任务
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

样例

样例输入 1

4 2
6 10 7 9
2 3 4 1

样例输出 1

26

样例输入 2

3 1
1000000000 993 2010
1 3 2

样例输出 2

1000000000

样例输入 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

样例输出 3

23

说明

考虑第一个例子中能够达到最大答案的服务器关闭顺序。

回顾服务器关闭时服务的转移方式:

服务器 1 2 3 4
备份 2 3 4 1

首先关闭第二台服务器;它的服务转移到第三台服务器,因此现在第三台服务器上有 $10 + 7 = 17$ 个服务。

其次关闭第三台服务器;它的服务转移到第四台服务器,之后第四台服务器上有 $9 + 17 = 26$ 个服务。

为了更好地理解,请参见下表,该表记录了上述过程中每台服务器上的服务数量。

阶段 $a_1$ $a_2$ $a_3$ $a_4$
第一次关闭前 6 10 7 9
关闭服务器 2 后 6 0 17 9
关闭服务器 3 后 6 0 0 26

如果先关闭第三台服务器,再关闭第二台服务器,过程将如下所示:

阶段 $a_1$ $a_2$ $a_3$ $a_4$
第一次关闭前 6 10 7 9
关闭服务器 3 后 6 10 0 16
关闭服务器 2 后 6 0 10 16

在这种情况下,一台服务器上的最大服务数量将为 16,这不是最优答案。

在第二个例子中,一种可能的方案是不关闭任何服务器。此时第一台服务器上有 $1000000000$ 个服务,这就是问题的答案。如果关闭服务器 2 或 3,最大服务数量仍将在第一台服务器上。

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.