atcoder#ARC126D. [ARC126D] Pure Straight
[ARC126D] Pure Straight
Score : points
Problem Statement
Given is a sequence of positive integers , where each is , , , or .
You can do the following operation on this sequence any number of times.
- Swap two adjacent elements, that is, choose and such that and swap and .
Find the minimum number of operations needed to make satisfy the following condition.
- contains as a contiguous subsequence, that is, there is a positive integer at most such that , , , and .
Constraints
- is , , , or .
- contains at least one occurrence of each of .
Input
Input is given from Standard Input in the following format:
Output
Print the minimum number of operations needed to make satisfy the condition.
4 3
3 1 2 1
2
One optimal sequence of operations is as follows.
- Swap and , changing to .
- Swap and , changing to .
- Now we have , , , satisfying the condition.
5 5
4 1 5 2 3
5
8 4
4 2 3 2 4 2 1 4
5