Maximal sum
Time limit0.2sMemory limit1024 MB
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 (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 .
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 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 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 , 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 , where is the maximum sum of carrots' sweetness under the j-th assumption.
Constraints
- for each carrot's index p
- 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 .
Bunny 2 collects total sweetness .
Bunny 3 collects total sweetness .
Bunny 4 collects total sweetness 2.
Bunny 5 collects total sweetness of -1.
Bunny 6 collects total sweetness 4.
Therefore .
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 .
The final answer is .