시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초 128 MB51031027961.589%

문제

An arithmetic series is defined as a series of numbers where each following number is an additive constant away from the previous number.

\[a_1+a_2+a_3+\dots+a_n = a_1 + (a_1+d)+(a_1+2d)+\dots+(a_1+nd)\]

The sum of all values from \(a_1\) to \(a_n\) is given by:

\[s_n = \dfrac{n(2a_1 + (n-1)d)}{2}\]

A geometric series is defined as a series of numbers where each following number is a multiplicative constant away from the previous number.

\[a_1+a_2+a_3+\dots+a_n = a_1 + (ra_1)+(r^2a_1)+\dots+(r^na_1)\]

The sum of all values from \(a_1\) to \(a_n\) is given by:

\[S_n = a_1\dfrac{r^n-1}{r-1}\]

Given the first 3 numbers of either a geometric or arithmetic series, determine the sum to N terms. The first term is index 1, the sum to N terms should include the Nth term in the calculation.

입력

The input will be given by 2 lines for each data set. The first line will be N, the term to which the sum should be computed. The next line of data is the series of numbers. They will be integers with a space separating each one. The series will be either geometric or arithmetic. The end of the input will be signaled by an N value of zero. You can assume r is integer if given series is gemoetric.

출력

For each test case output \(S_n\) on its own line.

제한

  • 1 ≤ N ≤ 100
  • 1 ≤ a1, a2, a3 ≤ 2,500

예제 입력 1

4
9 27 81
36
29 94 159
0

예제 출력 1

360
41994