시간 제한메모리 제한제출정답맞힌 사람정답 비율
0.1 초 1024 MB36141453.846%

문제

Nowadays your programming skills are amazing, and you regularly receive lots of money for your work. Unfortunately, your financial skills did not evolve the same way. So each time you earn some money, you simply invest it in a bank in a 30 days time deposit with an automatic renewal clause. This means that 30 days after you invest the money, it is invested for 30 additional days, over and over again, until you inform the bank that you want to stop the renewal and get your money back. Time deposits cannot be created nor renewed during weekends; if a 30 days period ends on a weekend, the renewal occurs on the immediately following Monday.

Since the bank holds almost all your money, you must wait until the closest renewal each time you want to buy anything but daily food. Today you decided to buy a new smartphone to replace your six-month-old device. Given the dates when you created each time deposit, you must determine the minimum number of days that you must wait to get some money from the bank.

As an example, suppose that today is Saturday and that you created five time deposits: a time deposit last Monday, another time deposit last Tuesday, yet another time deposit last Wednesday, and two time deposits yesterday. The first time deposit (Monday) would be renewed on a Wednesday after 25 days from today. The second time deposit (Tuesday) would be renewed on a Thursday after 26 days from today. The third time deposit (Wednesday) would be renewed on a Friday after 27 days from today. Finally, the last two time deposits (Friday) would be renewed on a Monday after 30 days from today, because the renewal on a Sunday is not allowed. Thus, in this case, you must wait 25 days to get some money from the bank.

입력

The first line contains a string T and an integer N (1 ≤ N ≤ 105), indicating respectively today’s day of the week and the number of time deposits. The string is either “Mon”, “Tue”, “Wed”, “Thu”, “Fri”, “Sat”, or “Sun”, representing respectively that today is Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, or Sunday. The second line contains N integers D1, D2, . . . , DN (0 ≤ Di ≤ 109 for i = 1, 2, . . . , N), indicating the number of days elapsed since each time deposit was created. It is guaranteed that the time deposits were not created during weekends.

출력

Output a single line with an integer indicating the minimum number of days that you must wait to get some money from the bank.

예제 입력 1

Sat 5
5 4 3 1 1

예제 출력 1

25

예제 입력 2

Sat 5
3 1 4 1 5

예제 출력 2

25

예제 입력 3

Thu 1
0

예제 출력 3

32

예제 입력 4

Thu 1
30

예제 출력 4

0

예제 입력 5

Fri 1
31

예제 출력 5

31

출처

ICPC > Regionals > Latin America > Latin America Regional Contests > Latin America Regional Contests 2021 I번

  • 문제를 만든 사람: Alejandro Strejilevich de Loma