Creative Accounting
Time limit2sMemory limit128 MB
Given daily balances, pick a contiguous period whose sum modulo m (with nonnegative remainder) is as large as possible, and report that maximum remainder.
- Level
Hard8 of 10
- Topics
- Prefix sum, Math, Sorting, Binary search
- Solved
- No attempts yet
Problem
Byteasar and his friends are spending their summer vacation at the well-known Byten-Byten resort. Where there are many tourists, prices are high, so from time to time they took on odd jobs to earn extra money for the trip. Byteasar served as the treasurer for the whole group.
When the vacation ends, the group settles up. The friends decided to split the entire surplus (or debt) evenly among the people in the group. If it cannot be divided exactly, then as a reward for his work as treasurer Byteasar takes the leftover amount from the common budget so that the remaining amount is divisible by the group size. In particular, if the total is a debt (negative), his reward still increases the debt.
Byteasar's reward is exactly the remainder of the chosen period's total balance when divided by , where the remainder is always taken as a value between and . In other words, his reward is the unique such that is a multiple of . For example, if and , then , so the reward is ; if and , then , so the reward is .
Byteasar has hatched a sneaky plan. He will show his friends only the part of his ledger covering days through (). He wants to choose this contiguous period so that the reward for its total balance is as large as possible.
Write a program that computes how much money Byteasar will receive as treasurer if he chooses the period optimally.
Input
The first line contains two integers and (, ): the length of the vacation in days and the size of the group (including Byteasar). The second line contains integers (), where is the financial result of day (in bythalers); a positive value means the income exceeded the spending.
Output
Print a single integer: the maximum number of bythalers Byteasar can receive for serving as treasurer.