"Take 6" (also known as "6 Nimmt!") is a card game consisting of 104 cards, each with a number (ranging from 1 to 104) and a certain number of cattle head symbols. The number of cattle headson a card varies, with higher-numbered cards typically carrying more cattle heads. Players aim to finish the game with the fewest cattle heads, as each cattle head counts as a penalty point. The game is played in rounds.
Each card in the game has a certain number of "cattle heads" (penalty points). The distribution of cattle heads across the cards is as follows:
After seeing the homework problem, Temmie realized that he had no friends, so he decided to play the game "Take 6!" by himself.
The first line contains two integers, M and P, representing the number of cards Temmie holds and the number of rows on the table.
The next P lines each contain one integer Di , representing the initial card of row i .
The next line contains M integers, C1 , C2 , ... , CM , where Ci represents the card Temmie plays in the i -th round.
Hint: You only need to modify the code for the homework by setting N = 1, and you will be able to pass Testcases 1 ~ 6.
Output a single integer representing the total number of cattle heads that Temmie ends up with.
At the beginning, there are 2 rows of cards on the table, with 7 and 12 respectively.
In the first round, Temmie drew the card 6. Among the two rows, the number closest to 6 at the head or tail of each row is 7, and 6 < 7, so 6 is placed in front of 7.
In the second round, Temmie drew the card 5. Among the two rows, the number closest to 5 at the head or tail of each row is 6, and 5 < 6, so 5 is placed in front of 6.
In the third round, Temmie drew the card 4. Among the two rows, the number closest to 4 at the head or tail of each row is 5, and 4 < 5, so 4 is placed in front of 5.
In the fourth round, Temmie drew the card 8. Among the two rows, the number closest to 8 at the head or tail of each row is 7, and 8 > 7, so 8 is placed after 7.
In the fifth round, Temmie drew the card 11. Among the two rows, the number closest to 11 at the head or tail of each row is 12, and 11 < 12, so 11 is placed in front of 12.
In the sixth round, Temmie drew the card 3. Among the two rows, the number closest to 3 at the head or tail of each row is 4, and 3 < 4, so 3 is placed in front of 4.
However, since 3 is the 6th card placed in the first row, as a penalty, Temmie has to take away the first five cards placed in that row, and she gained 6 cattle heads. The 6th card placed, 3, becomes the new beginning of that row.
In the seventh round, Temmie drew the card 2. Among the two rows, the number closest to 2 at the head or tail of each row is 3, and 2 < 3, so 2 is placed in front of 3.
In the eighth round, Temmie drew the card 10. Among the two rows, the number closest to 10 at the head or tail of each row is 11, and 10 < 11, so 10 is placed in front of 11.
In the ninth round, Temmie drew the card 1. Among the two rows, the number closest to 1 at the head or tail of each row is 2, and 1 < 2, so 1 is placed in front of 2.
In the tenth round, Temmie drew the card 9. Among the two rows, the number closest to 9 at the head or tail of each row is 10, and 9 < 10, so 9 is placed in front of 10.
In the end, Temmie gained a total of 6 cattle heads.