A hillside road has n houses built in a row, numbered 1 to n starting from the base of the hill. Each house has a recorded elevation. A house is designated a checkpoint if its position number evenly divides the total number of houses n (that is, n modulo the position is exactly zero). For a road inspection report you need the sum of the squared elevations of all checkpoint houses.
Given the elevations of all n houses, compute the sum of the squares of the elevations at every checkpoint position.
Line 1: a single integer n, the number of houses. Line 2: n space-separated integers, where the i-th integer (1-indexed) is the elevation of the house at position i.
Print a single integer: the sum of the squares of the elevations of every position i (1 <= i <= n) for which n is divisible by i.
1 <= n <= 100 1 <= elevation <= 100 for every house All values are integers.
Example 1
Input
5 1 2 3 4 5
Expected
26
Explanation
n=5. Divisors of 5 in [1,5] are 1 and 5. Position 1 has elevation 1, position 5 has elevation 5. Sum of squares = 1^2 + 5^2 = 1 + 25 = 26.
Example 2
Input
6 2 3 4 10 5 7
Expected
78
Explanation
n=6. Divisors of 6 in [1,6] are 1, 2, 3, 6. Their elevations are 2, 3, 4, 7. Sum of squares = 2^2+3^2+4^2+7^2 = 4+9+16+49 = 78.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →