33OJ
登录 注册

#P17846. 回天

    ID: 20522 远端评测题 2000ms 512MiB 尝试: 0 已通过: 0 显示难度提高+/省选− 上传者: 标签>贪心线段树平衡树上海2026高校校赛

回天

Problem Description

Through one "Kaiten" (turning back fate) after another, Homura Akemi keeps rewriting the causality of the world.

She holds a sequence of length nn. At each moment, Homura Akemi changes the value at some position; then Madoka Kaname observes the current sequence after the change and computes the chaos value of this sequence.

For a sequence, partition all of its elements into two non-empty multisets A,BA,B. Define the value of a multiset SS as the greatest common divisor of all its elements, that is, f(S)=gcd⁡x∈Sxf(S)=\gcd \limits_{x\in S}x.

The chaos value of the sequence is defined as the maximum of f(A)+f(B)f(A)+f(B) over all ways of partitioning.

Please help Madoka Kaname compute the chaos value of the current sequence after each of Homura Akemi's changes.

Input Format

The first line contains one integer nn (2≤n≤2×1052 \leq n \leq 2 \times 10^5), denoting the length of the sequence in Homura Akemi's hands.

The second line contains nn integers aia_i (1≤ai≤1091 \leq a_i \leq 10^9), denoting the initial state of the sequence.

The third line contains one integer qq (1≤q≤1051 \leq q \leq 10^5), denoting the number of changes.

The next qq lines each contain two integers ii (1≤i≤n1 \leq i \leq n) and vv (1≤v≤1091 \leq v \leq 10^9), meaning that the value of aia_i is changed to vv.

Output Format

Output q+1q+1 lines.

The first line outputs the chaos value of the sequence before any change.

For the ii-th change, the (i+1)(i+1)-th line outputs the chaos value of the sequence after that change is completed.

4
2 2 3 3
2
3 2
4 4
5
5
6

Hint

The sequence after each change in the sample and one optimal partition are as follows:

Current sequence Optimal partition
2,2,3,32,2,3,3 A={2,2}, B={3,3}A=\{2,2\},\ B=\{3,3\}, chaos value 2+3=52+3=5
2,2,2,32,2,2,3 A={2,2,2}, B={3}A=\{2,2,2\},\ B=\{3\}, chaos value 2+3=52+3=5
2,2,2,42,2,2,4 A={2,2,2}, B={4}A=\{2,2,2\},\ B=\{4\}, chaos value 2+4=62+4=6

Translated by QwenWork