This page is still under construction.

Parts of this page are still being built. What you see may change.

Maximal sum

Time limit0.2sMemory limit1024 MB

Summary
For each of m queries that add s to a[p], find the maximum divisor-sum of the array and report the sum of these maxima.
Level

Medium7 of 10

Topics
Number theory, Math, Brute force, Prefix sum
Solved
No attempts yet

Problem

A group of n bunnies found a garden with n carrots, arranged in a row. Both the bunnies and the carrots are numbered with integers from 1 to n. The bunnies have made preliminary evaluation for the sweetness of the carrots, which are expressed with the integers a1,…,ana_1, \dots, a_n (some carrots may be spoiled and can have negative number for sweetness). The soil under only one carrot p is fertilized and this changes the sweetness of this carrot by an integer s. More precisely, the real sweetness of carrot p is ap+sa_p + s.

Unfortunately, p and s are unknown to the bunnies. However, they have assumptions about the pair of values (p, s).

Bunny number k makes jumps of length k, i.e. collects the carrots in positions that are multiples of k.

For each assumption j, they look for the maximum amount tjt_j of the total real sweetness of the carrots that can be collected by a bunny. Help them by writing program maxs that finds the sum of the values of tjt_j for all assumptions.

Input

From the first line of the standard input, your program reads an integer n, the number of carrots (and bunnies). From the next line read n integers a1,…,ana_1, \dots, a_n, the preliminary evaluation of the sweetness of the carrots. From the next line read an integer m, the number of assumptions. From each of the next m lines, read two integers p and s, carrot's index and its sweetness change for the corresponding assumption.

Output

On one line of the standard output, print a number equal to t1+⋯+tmt_1 + \dots + t_m, where tjt_j is the maximum sum of carrots' sweetness under the j-th assumption.

Constraints

  • 1≤n≤5×1041 \le n \le 5 \times 10^4
  • 1≤m≤5×1051 \le m \le 5 \times 10^5
  • −108≤ai≤108-10^8 \le a_i \le 10^8
  • 1≤p≤n1 \le p \le n for each carrot's index p
  • −1013≤s≤1013-10^{13} \le s \le 10^{13} for any change in sweetness s

Hint

According to the first assumption, carrots have sweetness 2, -5, -1, 2, -1, 4.

Bunny 1 collects total sweetness 2+(−5)+(−1)+2+(−1)+4=12 + (-5) + (-1) + 2 + (-1) + 4 = 1.

Bunny 2 collects total sweetness (−5)+2+4=1(-5) + 2 + 4 = 1.

Bunny 3 collects total sweetness (−1)+4=3(-1) + 4 = 3.

Bunny 4 collects total sweetness 2.

Bunny 5 collects total sweetness of -1.

Bunny 6 collects total sweetness 4.

Therefore t1=4t_1 = 4.

According to the second assumption, carrots have sweetness 2, -2, 3, 2, -1, 4. Bunnies collect sweetness 8, 4, 7, 2, -1, 4, respectively.

Therefore t2=8t_2 = 8.

The final answer is 4+8=124 + 8 = 12.

Examples1

  1. Example 1

    Input
    6
    2 -5 3 2 -1 4
    2
    3 -4
    2 3
    
    Expected output
    12