CF1254B2.Send Boxes to Alice (Hard Version)

提高+/省选-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

This is the harder version of the problem. In this version, 1≤n≤1061 \le n \le 10^6 and 0≤ai≤1060 \leq a_i \leq 10^6. You can hack this problem if you locked it. But you can hack the previous problem only if you locked both problems

Christmas is coming, and our protagonist, Bob, is preparing a spectacular present for his long-time best friend Alice. This year, he decides to prepare nn boxes of chocolate, numbered from 11 to nn. Initially, the ii-th box contains aia_i chocolate pieces.

Since Bob is a typical nice guy, he will not send Alice nn empty boxes. In other words, at least one of a1,a2,…,ana_1, a_2, \ldots, a_n is positive. Since Alice dislikes coprime sets, she will be happy only if there exists some integer k>1k \gt 1 such that the number of pieces in each box is divisible by kk. Note that Alice won't mind if there exists some empty boxes.

Charlie, Alice's boyfriend, also is Bob's second best friend, so he decides to help Bob by rearranging the chocolate pieces. In one second, Charlie can pick up a piece in box ii and put it into either box i−1i-1 or box i+1i+1 (if such boxes exist). Of course, he wants to help his friend as quickly as possible. Therefore, he asks you to calculate the minimum number of seconds he would need to make Alice happy.

这是该问题的困难版本。在此版本中,1≤n≤1061 \le n \le 10^6 且 0≤ai≤1060 \leq a_i \leq 10^6。如果你锁定了本题,则可以对本题进行 Hack;但若你只想 Hack 上一题,则必须同时锁定本题和上一题。

圣诞节即将到来,我们的主角 Bob 正在为他多年以来最好的朋友 Alice 准备一份惊艳的礼物。今年,他决定准备 nn 盒巧克力,编号从 11 到 nn。初始时,第 ii 个盒子中有 aia_i 块巧克力。

由于 Bob 是一位典型的善良之人,他不会送给 Alice nn 个空盒子。换言之,a1,a2,…,ana_1, a_2, \ldots, a_n 中至少有一个是正数。又因 Alice 不喜欢互质的集合,她只有在存在某个整数 k>1k > 1,使得每个盒子中的巧克力块数均能被 kk 整除时,才会感到开心。注意:Alice 并不介意某些盒子为空。

Charlie 是 Alice 的男朋友,同时也是 Bob 的第二好朋友,因此他决定帮助 Bob 重新分配巧克力。每秒钟,Charlie 可以从第 ii 个盒子中取出一块巧克力,并将其放入第 i−1i-1 或第 i+1i+1 个盒子中(如果这样的盒子存在)。当然,他希望尽可能快地帮助自己的朋友。因此,他请你计算出使 Alice 开心所需的最少秒数。

输入格式

The first line contains a single integer nn (1≤n≤1061 \le n \le 10^6) — the number of chocolate boxes.

The second line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (0≤ai≤1060 \le a_i \le 10^6) — the number of chocolate pieces in the ii-th box.

It is guaranteed that at least one of a1,a2,…,ana_1, a_2, \ldots, a_n is positive.

第一行包含一个整数 nn(1≤n≤1061 \le n \le 10^6)—— 巧克力盒子的数量。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(0≤ai≤1060 \le a_i \le 10^6)—— 第 ii 个盒子中的巧克力块数量。

保证 a1,a2,…,ana_1, a_2, \ldots, a_n 中至少有一个为正数。

输出格式

If there is no way for Charlie to make Alice happy, print −1-1.

Otherwise, print a single integer xx — the minimum number of seconds for Charlie to help Bob make Alice happy.

如果查理无法让爱丽丝开心,则输出 −1-1。

否则,输出一个整数 xx —— 查理帮助鲍勃让爱丽丝开心所需的最少秒数。

输入输出样例

  • 输入#1

    3
    4 8 5

    输出#1

    9
  • 输入#2

    5
    3 10 2 1 5

    输出#2

    2
  • 输入#3

    4
    0 5 15 10

    输出#3

    0
  • 输入#4

    1
    1

    输出#4

    -1

说明/提示

In the first example, Charlie can move all chocolate pieces to the second box. Each box will be divisible by 1717.

In the second example, Charlie can move a piece from box 22 to box 33 and a piece from box 44 to box 55. Each box will be divisible by 33.

In the third example, each box is already divisible by 55.

In the fourth example, since Charlie has no available move, he cannot help Bob make Alice happy.

在第一个例子中,查理可以将所有巧克力块移动到第二个盒子中。每个盒子中的巧克力块数都将能被 1717 整除。

在第二个例子中,查理可以将一个巧克力块从第 22 个盒子移动到第 33 个盒子,并将一个巧克力块从第 44 个盒子移动到第 55 个盒子。每个盒子中的巧克力块数都将能被 33 整除。

在第三个例子中,每个盒子中的巧克力块数已经能被 55 整除。

在第四个例子中,由于查理没有任何可行的操作,他无法帮助鲍勃让爱丽丝开心。

输入解题思路,AI测评打分。不知道怎么写?

首页