QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 256 MB Total points: 100 Hackable ✓

#18945. Please don't throw the steamed bun slices anymore.

Statistics

Asrit has $n$ packets of steamed bun slices, arranged in a row. The steamed bun slices come in $5$ different flavors: scallion, cumin, barbecue, numbing-spicy, and beef. For convenience, we denote the flavors by $1 \sim 5$, where the flavor of the $i$-th packet is $a_i$.

Every time Asrit eats a packet of steamed bun slices, he gains $1$ happiness point. However, always eating the same flavor is boring. Specifically, if Asrit first eats a packet with flavor $x$, and the next packet he eats immediately after has flavor $y$, he additionally gains $(y - x + 5) \bmod 5$ happiness points.

To make eating more enjoyable, Asrit decides to adjust the eating order. He processes each packet one by one according to the initial order. For the packet currently being processed, he chooses whether to move it to the end of the sequence. Note that a packet cannot be moved twice, otherwise it will break.

After processing all packets, Asrit will eat them one by one in the new order. What is the maximum happiness Asrit can obtain?

Input

The first line contains an integer $n$ $(1 \le n \le 10^5)$, representing the number of packets.

The second line contains $n$ integers $a_1, a_2, \dots, a_n$ $(1 \le a_i \le 5)$, representing the flavor of each packet.

Output

Output one line containing an integer, representing the maximum happiness Asrit can obtain.

Examples

Input 1

6
1 1 1 1 1 1

Output 1

6

Input 2

6
5 5 4 3 2 1

Output 2

26

Input 3

6
1 1 2 3 4 5

Output 3

15

Note

Explanation of Example $3$: Move the $1$-st and the $6$-th packets to the end; the final eating sequence is 1 2 3 4 1 5, which gives the maximum answer.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.