Arithmetic and Geometric Sums

No attempts yetTime limit1sMemory limit128 MB

Problem

An arithmetic series adds a fixed constant dd to the previous term to get the next term.

a1+a2+a3++an=a1+(a1+d)+(a1+2d)++(a1+(n1)d)a_1 + a_2 + a_3 + \dots + a_n = a_1 + (a_1 + d) + (a_1 + 2d) + \dots + (a_1 + (n-1)d)

The sum of a1a_1 through ana_n is

Sn=n(2a1+(n1)d)2S_n = \frac{n(2a_1 + (n-1)d)}{2}

A geometric series multiplies the previous term by a fixed constant rr to get the next term.

a1+a2+a3++an=a1+ra1+r2a1++rn1a1a_1 + a_2 + a_3 + \dots + a_n = a_1 + ra_1 + r^2 a_1 + \dots + r^{n-1} a_1

The sum of a1a_1 through ana_n is

Sn=a1rn1r1S_n = a_1 \frac{r^n - 1}{r - 1}

You are given the first three terms of a series that is either arithmetic or geometric. Compute the sum of its first NN terms. The first term has index 1, and the sum includes the NN-th term.

Input

The input holds several data sets, two lines each. The first line is NN, the number of terms to sum. The second line is the first three terms of the series, integers separated by spaces. The series is either arithmetic or geometric. A line whose NN is zero ends the input, and no series line follows it. If the series is geometric, the ratio rr is an integer.

Output

For each data set print SnS_n on its own line. The sum can be larger than a 64-bit integer holds.

Constraints

  • 1N1001 \le N \le 100
  • 1a1,a2,a325001 \le a_1, a_2, a_3 \le 2500