Paul the barista picks coffee beans
Time limit1.5sMemory limit64 MB
Pick the longest subsequence of the given row so that consecutive picked values are congruent mod k or differ by at most d in absolute value.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Segment tree, Array, Prefix sum
- Solved
- No attempts yet
Problem
Paul the barista has coffee beans laid out in a row. He picks some of them and brews them. The picked beans keep the order they were laid out in.
Each bean carries one integer that names its kind. Write the kinds of the picked beans in order as a sequence . The extraction has good quality when at least one of the two conditions below holds for every with .
Find the largest number of beans Paul can pick while the quality stays good.
Input
The first line contains , , and , separated by spaces. (, )
The second line contains a sequence of length giving the kind of each bean in the order the beans are laid out. ()
Output
Print the largest number of beans in a good extraction.