50157. Pokemon's

I'm a slow walker, but I never walk backwards.

Task Description

We are given a series of Pokemon's. Each Pokemon has a combat point (CP) as a non-negative integers. You need to divied this series of Pokemon's into teams, select a deputy leader from each team, and compute the sum of the CP's of these deputy leaders.

We first describe the attribute of a Pokemon. If the CP of Pokemon is $3n$, then its attribute is wind. If the CP of Pokemon is $3n + 1$, then its attribute is fire. If the CP of Pokemon is $3n + 2$, then its attribute is water.

The teams are organized as follows. We look at the attribute of each Pokemon from start to finsh. Whenever we have at least $k$ Pokemon's of all three attributes, we have a team, and we start looking for the next team from the next Pokemon. Note that we will process all Pokemon's until EOF, so the last team may not have $k$ Pokemon's of all three attributes.

Now we select the deputy leader from each team. The deputy leader is the Pokemon that has the second highest CP in a team. Note that if the team has 34 56 56 2 0, then the second highest CP is 56, since we have two of them. Also note that if the last team has only one member, then the only member is the deputy leader.

The task is to output the sum of the CP's of all deputy leaders.

Let us conisder an example when $k$ is 1, and the CP's are 68 68 52 12 1. Note that the first team ends at 12 becuse at that time we have all three types of Pokemon's. Also note that the deputy leader of the first team has CP 68 because there are two 68's in the team. Finally note that the last team has only one member, so its deputy leader is the only member 1. As a result the sum of CP's of all deputy leaders is 69.

team 1 team 2
CP's 68 68 52 12 1
attribute 2 2 1 0 1
deputy leader 68 1

Input Format

There are two lines in the input. The first line has a positive integer $k$. The second line has a series of non-negative integers (CP's). You must process all CP's until EOF.

Output Format

The sum of CP's of all deputy leaders.

Sample Input

47 68 52 12 0 13 16 19 21 24 14

Sample Output