Under construction

Still being built. Things may change

Peculiar Protocol

Time limit2sMemory limit2048 MB

Summary
Given banknotes and integers d and r, find the maximum total multiplier k when repeatedly removing a contiguous block whose sum is k*d+r.
Level

Medium7 of 10

Topics
Dynamic programming, Prefix sum, Math
Solved
No attempts yet

Problem

The Kingdom of Icpca has a peculiar protocol for wedding gifts. The gift amount must be a multiple of a fixed quantity plus a fixed extra. When the fixed quantity is dd and the fixed extra is rr, the courteous amounts are k×d+rk \times d + r for any non-negative integer multiplier kk.

You start with a pile of nn banknotes. Each time you attend a wedding ceremony, you draw a contiguous portion of the current pile whose sum is a courteous amount and give it as your gift. If no contiguous portion has such a sum, you cannot attend any more ceremonies. After drawing, the remaining banknotes are squeezed into a single pile, keeping their relative order. The new pile may contain a portion with a courteous sum, which lets you attend more ceremonies.

Your gifts raise your social reputation. Since the extra amount rr is mandatory, the multiplier kk is what matters. At each ceremony, your reputation rises in proportion to kk.

For example, assume d=5d = 5 and r=1r = 1, and you have banknotes worth 22, 22, 22, 44, and 44, in this order from the top. There are two ways to give a courteous gift.

  • Give the first three banknotes, totaling 2+2+2=6=1×d+r2 + 2 + 2 = 6 = 1 \times d + r. The remaining banknotes are 44 and 44. No contiguous portion of them sums to a courteous amount, so you cannot attend any more ceremonies.
  • Give the third and fourth banknotes, totaling 2+4=6=1×d+r2 + 4 = 6 = 1 \times d + r. The remaining banknotes are 22, 22, and 44, in this order. The second and third banknotes total 2+4=6=1×d+r2 + 4 = 6 = 1 \times d + r, so you can attend another ceremony.

In this example, the second way maximizes your reputation with two ceremonies, because the multipliers sum to 1+1=21 + 1 = 2, the largest possible total.

By contrast, if the first banknote is worth 1212, giving the first three banknotes stops you from attending more ceremonies. That method still maximizes your reputation, because its multipliers sum to 33.

Compute the maximum possible total of the multipliers from your wedding gifts. Assume you have enough unmarried relatives and friends to attend as many ceremonies as you can give courteous gifts to.

Input

The first line contains three integers nn, dd, and rr. The integer nn (1≤n≤5001 \le n \le 500) is the number of banknotes. The integers dd and rr (2≤d≤1082 \le d \le 10^8, 0≤r<d0 \le r < d) are the parameters of the protocol.

The second line contains nn integers a1,…,ana_1, \dots, a_n (0≤ai≤1080 \le a_i \le 10^8), where aia_i is the value of the banknote that is ii-th from the top.

Output

Print a single line containing the maximum possible total of the multipliers.

Examples4

  1. Example 1

    Input
    5 5 1
    2 2 2 4 4
    
    Expected output
    2
    
  2. Example 2

    Input
    5 5 1
    12 2 2 4 4
    
    Expected output
    3
    
  3. Example 3

    Input
    5 20000 10000
    5000 10000 15000 5000 25000
    
    Expected output
    2
    
  4. Example 4

    Input
    9 5 3
    4 2 2 1 1 4 3 2 1
    
    Expected output
    2