#include <bits/stdc++.h>
using namespace std;
const int mx = 1e6 + 10;
int n, m;
int a[mx], b[mx];
long long pre[mx], cnt[mx];
vector<int> pri;
vector<long long> g;
bool vis[mx];
void init() {
  for (int i = 2; i < mx; i++) {
    if (!vis[i]) pri.push_back(i);
    for (int j : pri) {
      if (j * i >= mx) break;
      vis[j * i] = 1;
      if (i % j == 0) break;
    }
  }
}
long long get_ans(long long val) {
  long long sum = 0;
  for (int i = 1; i <= n; i++) {
    b[i] = a[i] % val;
    sum += b[i];
    pre[i] = pre[i - 1] + 1ll * b[i] * i;
    cnt[i] = cnt[i - 1] + b[i];
  }
  long long siz = sum / val;
  long long ans = 0, pv = 0, ret = 0;
  for (int i = 1; i <= siz; i++, ret += val) {
    int midk = lower_bound(cnt + 1, cnt + 1 + n, ret + (val + 1) / 2) - cnt;
    int k = lower_bound(cnt + midk, cnt + n + 1, ret + val) - cnt;
    long long mv = max(0ll, cnt[k] - (ret + val));
    long long lv = pre[midk] - pv;
    ans += midk * (cnt[midk] - ret) - lv;
    long long rv = pre[k] - mv * k - pre[midk];
    ans += rv - midk * (cnt[k] - cnt[midk] - mv);
    pv = pre[k] - mv * k;
  }
  return ans;
}
int main() {
  scanf("%d", &n);
  init();
  long long sum = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", a + i);
    sum += a[i];
  }
  long long tmp = sum, ans = 2e18;
  for (int i : pri) {
    if (tmp % i == 0) g.push_back(i);
    while (tmp % i == 0) tmp /= i;
  }
  if (tmp != 1) g.push_back(tmp);
  for (long long i : g) {
    ans = min(ans, get_ans(i));
  }
  printf("%lld\n", ans == 2e18 ? -1 : ans);
  return 0;
}

### Prompt

Please formulate a cpp solution to the following problem:

This is the harder version of the problem. In this version, 1 ≤ n ≤ 10^6 and 0 ≤ a_i ≤ 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 n boxes of chocolate, numbered from 1 to n. Initially, the i-th box contains a_i chocolate pieces.

Since Bob is a typical nice guy, he will not send Alice n empty boxes. In other words, at least one of a_1, a_2, …, a_n is positive. Since Alice dislikes coprime sets, she will be happy only if there exists some integer k > 1 such that the number of pieces in each box is divisible by k. 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 i and put it into either box i-1 or box i+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.

Input

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

The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^6) — the number of chocolate pieces in the i-th box.

It is guaranteed that at least one of a_1, a_2, …, a_n is positive.

Output

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

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

Examples

Input


3
4 8 5


Output


9


Input


5
3 10 2 1 5


Output


2


Input


4
0 5 15 10


Output


0


Input


1
1


Output


-1

Note

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

In the second example, Charlie can move a piece from box 2 to box 3 and a piece from box 4 to box 5. Each box will be divisible by 3.

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

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

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
const int mx = 1e6 + 10;
int n, m;
int a[mx], b[mx];
long long pre[mx], cnt[mx];
vector<int> pri;
vector<long long> g;
bool vis[mx];
void init() {
  for (int i = 2; i < mx; i++) {
    if (!vis[i]) pri.push_back(i);
    for (int j : pri) {
      if (j * i >= mx) break;
      vis[j * i] = 1;
      if (i % j == 0) break;
    }
  }
}
long long get_ans(long long val) {
  long long sum = 0;
  for (int i = 1; i <= n; i++) {
    b[i] = a[i] % val;
    sum += b[i];
    pre[i] = pre[i - 1] + 1ll * b[i] * i;
    cnt[i] = cnt[i - 1] + b[i];
  }
  long long siz = sum / val;
  long long ans = 0, pv = 0, ret = 0;
  for (int i = 1; i <= siz; i++, ret += val) {
    int midk = lower_bound(cnt + 1, cnt + 1 + n, ret + (val + 1) / 2) - cnt;
    int k = lower_bound(cnt + midk, cnt + n + 1, ret + val) - cnt;
    long long mv = max(0ll, cnt[k] - (ret + val));
    long long lv = pre[midk] - pv;
    ans += midk * (cnt[midk] - ret) - lv;
    long long rv = pre[k] - mv * k - pre[midk];
    ans += rv - midk * (cnt[k] - cnt[midk] - mv);
    pv = pre[k] - mv * k;
  }
  return ans;
}
int main() {
  scanf("%d", &n);
  init();
  long long sum = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", a + i);
    sum += a[i];
  }
  long long tmp = sum, ans = 2e18;
  for (int i : pri) {
    if (tmp % i == 0) g.push_back(i);
    while (tmp % i == 0) tmp /= i;
  }
  if (tmp != 1) g.push_back(tmp);
  for (long long i : g) {
    ans = min(ans, get_ans(i));
  }
  printf("%lld\n", ans == 2e18 ? -1 : ans);
  return 0;
}

```