Under construction

Still being built. Things may change

Periodic Function (Hard)

Time limit1sMemory limit1024 MB

Summary
Given the integrals of a periodic function over each unit interval [i-1, i], print the integral over [a, b] using the function's period.
Level

Medium6 of 10

Topics
Prefix sum, String matching, Math
Solved
No attempts yet

Problem

This problem has different input constraints from Periodic Function (Easy).

A periodic function is a function whose values repeat at regular intervals. If there exists a positive number pp such that f(x+p)=f(x)f(x+p)=f(x) for all xx, then f(x)f(x) is called a periodic function, and pp is called a period. A function can have several periods. For example, if f(x)=sin⁡xf(x) = \sin x, then 2nπ2n\pi can be a period for every positive integer nn.

Periodic functions make definite integrals easier to compute, because shifting the integration interval by a period does not change the value. For example, the period of sin⁡\sin is 2π2\pi, so ∫0πsin⁡x dx=∫2π3πsin⁡x dx\int_0^\pi \sin x\,dx = \int_{2\pi}^{3\pi} \sin x\,dx.

Every day, Dooyoung brings Ian a periodic function that Ian does not know, gives him the integral over an arbitrary interval, and asks him to find the integral over another given interval. Tired of solving these by hand, Ian wants a program that computes the answers automatically.

Input

The first line contains the length LL of the interval array.

The second line contains LL integers separated by spaces. For each integer ii with 1≤i≤L1 \le i \le L, the ii-th number is ∫i−1if(x) dx\int_{i-1}^i f(x)\,dx.

The third line contains two integers aa and bb separated by a space, the endpoints of the interval to find.

Output

Print the value of ∫abf(x) dx\int_a^b f(x)\,dx on one line.

Constraints

  • 2≤L≤3×1052 \le L \le 3\times 10^5
  • −109≤a≤b≤109-10^9 \le a \le b \le 10^9
  • The absolute value of every integral value is at most 10910^9.
  • All input values are integers.
  • The length of the period is guaranteed to be an integer at most L2\frac{L}{2}.

Examples2

  1. Example 1

    Input
    10
    1 2 3 1 2 3 1 2 3 1
    2 5
    
    Expected output
    6
    
  2. Example 2

    Input
    10
    1 2 3 4 5 1 2 3 4 5
    -5 15
    
    Expected output
    60