Peculiar Protocol
Time limit2sMemory limit2048 MB
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 and the fixed extra is , the courteous amounts are for any non-negative integer multiplier .
You start with a pile of 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 is mandatory, the multiplier is what matters. At each ceremony, your reputation rises in proportion to .
For example, assume and , and you have banknotes worth , , , , and , in this order from the top. There are two ways to give a courteous gift.
- Give the first three banknotes, totaling . The remaining banknotes are and . No contiguous portion of them sums to a courteous amount, so you cannot attend any more ceremonies.
- Give the third and fourth banknotes, totaling . The remaining banknotes are , , and , in this order. The second and third banknotes total , so you can attend another ceremony.
In this example, the second way maximizes your reputation with two ceremonies, because the multipliers sum to , the largest possible total.
By contrast, if the first banknote is worth , giving the first three banknotes stops you from attending more ceremonies. That method still maximizes your reputation, because its multipliers sum to .
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 , , and . The integer () is the number of banknotes. The integers and (, ) are the parameters of the protocol.
The second line contains integers (), where is the value of the banknote that is -th from the top.
Output
Print a single line containing the maximum possible total of the multipliers.