Klee in Solitary Confinement
Time limit1sMemory limit1024 MB
Given an array and an integer k, add k to at most one contiguous interval so that the mode of the whole array appears as many times as possible.
- Level
Hard8 of 10
- Topics
- Sorting, Prefix sum, Segment tree, Hash map
- Solved
- No attempts yet
Problem
Since the traveler came, people in Monstadt have shown great interest in computer programming and algorithms, including Klee, the Spark Knight of the Knights of Favonius.

Source: Genshin Impact Official
Sent to solitary confinement by Jean again, Klee decides to spend her time learning Mo's algorithm, which computes answers to range query problems without modifications in time.
To check whether Klee has truly mastered the algorithm (or is secretly making more bombs), Jean gives her a problem. You are given an integer sequence and several queries . For each query, find the mode of the contiguous subsequence . The mode is the number that appears the most times in the subsequence.
With Mo's algorithm, Klee solves this problem easily, but another problem comes to mind. You are given an integer sequence of length and an integer . You may perform the following operation at most once: choose two integers and with , and add to every with . You may also skip the operation. Choose whether to perform the operation so as to maximize the number of occurrences of the mode of the whole sequence, and output that maximum.
Input
The first line contains two integers and (, ), the length of the sequence and the additive number.
The second line contains integers (), the original sequence.
There is only one test case in each test file.
Output
Output one line containing one integer, the maximum number of occurrences of the mode of the whole sequence after performing or skipping the operation.
Hint
For the first sample, choose and . The sequence becomes . The mode is clearly , which appears times.
For the second sample, choose and . The sequence becomes . The mode is , which appears times.
For the fourth sample, do not perform the operation. The modes are and , which both appear times.