Same Songs
Time limit2sMemory limit512 MB
Remove songs from a fixed playlist so that the remaining list has the maximum number of adjacent equal pairs, and output one such list.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Greedy, Implementation, Array
- Solved
- No attempts yet
Problem
Masha has a playlist of several songs in her music player. She listens to the songs in order. The playlist is not cyclic: when the last song ends, the player turns off. Masha likes repetitions, so she feels happy every time a song ends and the same one starts playing. When the same songs are separated by another one, Masha does not consider it a repetition.
The player has no option to add songs to the playlist or shuffle it. It can remove songs from the list. Your task is to remove some songs to maximize the number of repetitions. You may also remove everything or nothing from the list.
Input
The first line contains an integer : the number of songs in the playlist (). The second line contains space-separated numbers: the songs in the list. The songs are denoted by integer numbers from to .
Output
On the first line, print two space-separated numbers and : the number of songs in the edited playlist and the number of repetitions in it. On the second line, print the playlist: space-separated numbers.
If there are several possible answers, print any one of them. If the list is empty, you can either print or not print the second line, which would be empty.