Hide

Problem A
Election in Valhem

Languages en sv

In a parallel universe, the Vikings have invented democracy in their land, Valhem. Now it is time for an election, and the three parties fighting for victory are the Right Party, Left Party, and Center Party. In Valhem, there are $X$ Viking villages, and to win the election, a party must get the most votes in the most villages. If two parties tie in a village, neither of them wins that village. If no party wins the election in the most villages, a re-election will be held since no winner can be declared. Harald Bluetooth is the leader of party $P$ and, of course, wants to win the election. He has conducted thorough opinion polls and found out what everyone will vote for, so now he has a master plan to win.

His idea is that if he withdraws from the election in certain villages, those who had planned to vote for his party will be forced to vote for someone else instead. If Harald is the leader of the Right Party or the Left Party, everyone who was going to vote for his party will instead vote for the Center Party. If he is the leader of the Center Party, half of the votes will go to the Right Party and half to the Left Party (if the number is odd, one person will abstain from voting). Can Harald win the election?

Input

The first line contains the integer $X$ $(1 \leq X \leq 10^5)$, which represents the number of Viking villages in Valhem. The next line contains the letter $P$, which is ’C’, ’H’, or ’V’.

Then, the next $X$ lines contain three integers $v_i$, $c_i$, and $h_i$, representing the number of votes for each party in the respective Viking villages.

Output

Print "Yes" if Harald can win, and "No" otherwise.

Scoring

Your solution will be tested on several different test groups. To earn points for a group, you must pass all test cases in the group.

Group

Points

Constraints

$1$

$18$

$P = C$

$2$

$23$

$P = V$

$3$

$29$

No parties will ever will tie in some elecetion, no matter which elections Harald chooses to opt out of.

$3$

$30$

No additional constraints.

Explanation

In the first example case, the Right Party wins one election, the Left Party wins one, and there is a tie in the third election. There is no way for us to withdraw from the elections so that the Center Party wins the most elections.

In the second example case, we win the election in the second village, and there is a tie in the election in the third village. If we withdraw from the election in the first village, the Center Party will get all the Left Party’s votes, creating another tie, and the Left Party wins the most elections.

In the fourth example case, we can withdraw from the election in the first, fifth, and sixth villages. This way, we win two elections, the Right Party wins one, the Left Party wins one, and there are two ties. This means we have won the most elections.

Sample Input 1 Sample Output 1
3
C
2 0 0
1 3 3
1 3 5
No
Sample Input 2 Sample Output 2
3
V
1 2 3
4 2 2
3 3 2
Yes
Sample Input 3 Sample Output 3
4
H
2 1 2
3 2 1
2 4 5
6 1 1
No
Sample Input 4 Sample Output 4
6
V
2 5 6
2 5 6
1 0 0
1 0 0
1 1 2
1 1 2
Yes

Please log in to submit a solution to this problem

Log in