Under construction

Still being built. Things may change

Secret of the Sequence (Easy)

Time limit1sMemory limit1024 MB

Summary
Given n, p, q, r, s, and a_1, the task is to compute the sum of the first n terms of a sequence defined by a_(2i) = p*a_i + q and a_(2i+1) = r*a_i + s.
Level

Easy2 of 10

Topics
Array, Simulation
Solved
No attempts yet

Problem

A sequence a_1,a_2,a_3,…,a_na\_1, a\_2, a\_3, \ldots, a\_n of length nn satisfies the following conditions.

  • a_2i=p⋅a_i+qa\_{2i} = p \cdot a\_i + q (2≤2i≤n)(2 \leq 2i \leq n)
  • a_2i+1=r⋅a_i+sa\_{2i+1} = r \cdot a\_{i} + s (3≤2i+1≤n)(3 \leq 2i+1 \leq n)

Let S_n=a_1+a_2+…+a_nS\_n = a\_1 + a\_2 + \ldots + a\_{n}. Print S_nS\_n.

Input

The first line contains nn.

The second line contains positive integers p,q,r,sp,q,r,s, separated by spaces.

The third line contains the positive integer a_1a\_1.

Output

Print the value of S_nS\_n on the first line.

Constraints

  • 1≤n≤201\leq n\leq 20
  • 1≤p,q,r,s≤101\leq p,q,r,s\leq 10
  • 1≤a_1≤1001\leq a\_1\leq 100

Examples2

  1. Example 1

    Input
    3
    2 1 1 2
    1
    
    Expected output
    7
    
  2. Example 2

    Input
    4
    10 10 10 10
    100
    
    Expected output
    12230