Gerrymandering

Time limit: 1000 ms
Memory limit: 256 MB

Gerrymandering is a practice intended to establish a political advantage for a particular party or group by manipulating district boundaries.

An election between the political parties AA and BB has taken place in NN counties. Therefore, each county has chosen to represent either AA or BB.

Each county is either small or large. You are to partition them into districts so that:

  • Each county belongs to one district
  • If counties ii and jj (1i<jN1 \leq i < j \leq N) belong to the same district, then counties i+1,i+2,,j1i + 1, i + 2, \ldots, j - 1 must also belong here
  • Each district must contain one and only one large county

AA is majority in a district if the number of counties (belonging to this district) voting for it is greater or equal to the number of counties voting for the opposite party.

Find a split which maximizes the number of districts in which AA is majority. Print its number of districts.

Standard input

The first line contain an integer NN.

The next NN lines characterize the counties with two letters.

The first letter is either AA or BB, representing the elected party in this county.

The second letter is either SS or LL, where SS stands for small county and LL large county.

Standard output

Print the answer on the first line.

Constraints and notes

  • 1N1051 \leq N \leq 10^5
  • There is at least one large country

InputOutputExplanation
2
A L
B S
1

The counties that are belonging in the same district are inside the same [brackets][brackets]

The large counties are underlined.

[A B][\underline{A}\ B]

The district has even votes, so the winner is party AA.

3
A L
B S
B L
1

[A][B B][\underline{A}][B\ \underline{B}]

The first district is won by party AA while the second one goes to party BB.

13
A S
A L
B S
B S
B S
B S
A S
A L
B S
B L
B S
B S
A L
3

[A A B B][B B A A][B B B B][A][A\ \underline{A}\ B\ B][B\ B\ A\ \underline{A}][B\ \underline{B}\ B\ B][\underline{A}]

Party AA has a majority in districts 1,2,41, 2, 4 while party BB only has majority in district 33.

Authenticate to use the workspace

Log In
Sign Up
Forgot Password?
or connect with