This page is still under construction.

Parts of this page are still being built. What you see may change.

Creative Accounting

Time limit2sMemory limit128 MB

Summary
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 mm 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 SS when divided by mm, where the remainder is always taken as a value between 00 and m−1m-1. In other words, his reward is the unique r∈[0,m−1]r \in [0, m-1] such that S−rS - r is a multiple of mm. For example, if S=30S = 30 and m=13m = 13, then 30=2×13+430 = 2 \times 13 + 4, so the reward is 44; if S=−1S = -1 and m=13m = 13, then −1=(−1)×13+12-1 = (-1) \times 13 + 12, so the reward is 1212.

Byteasar has hatched a sneaky plan. He will show his friends only the part of his ledger covering days ll through rr (1≤l≤r≤n1 \le l \le r \le n). 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 nn and mm (1≤n≤200 0001 \le n \le 200\,000, 2≤m≤10182 \le m \le 10^{18}): the length of the vacation in days and the size of the group (including Byteasar). The second line contains nn integers aia_i (∣ai∣≤1018|a_i| \le 10^{18}), where aia_i is the financial result of day ii (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.

Examples3

  1. Example 1

    Input
    5 13
    10 9 5 -5 7
    
    Expected output
    11
    
  2. Example 2

    Input
    2 10
    1 9
    
    Expected output
    9
    
  3. Example 3

    Input
    1 5
    7
    
    Expected output
    2