NKMINERS - IOI07 Miners

There are two coal mines, each employing a group of miners. Mining coal is hard work, so miners need food to keep at it. Every time a shipment of food arrives at their mine, the miners produce some amount of coal. There are three types of food shipments: meat shipments, fish shipments and bread shipments.

Miners like variety in their diet and they will be more productive if their food supply is kept varied. More precisely, every time a new shipment arrives to their mine, they will consider the new shipment and the previous two shipments (or fewer if there haven't been that many) and then:

  • If all shipments were of the same type, they will produce one unit of coal.
  • If there were two different types of food among the shipments, they will produce two units of coal.
  • If there were three different types of food, they will produce three units of coal.

We know in advance the types of food shipments and the order in which they will be sent. It is possible to influence the amount of coal that is produced by determining which shipment should go to which mine. Shipments cannot be divided; each shipment must be sent to one mine or the other in its entirety.

The two mines don't necessarily have to receive the same number of shipments (in fact, it is permitted to send all shipments to one mine).

Task

Your program will be given the types of food shipments, in the order in which they are to be sent. Write a program that finds the largest total amount of coal that can be produced (in both mines) by deciding which shipments should be sent to mine 1 and which shipments should be sent to mine 2.

Input

The first line of input contains an integer N (1 ≤ N ≤ 100 000), the number of food shipments.

The second line contains a string consisting of N characters, the types of shipments in the order in which they are to be distributed. Each character will be one of the uppercase letters 'M' (for meat), 'F' (for fish) or 'B' (for bread).

Output

Output a single integer, the largest total amount of coal that can be produced.

Grading

In test cases worth a total of 45 points, the number of shipments N will be at most 20.

Example

Input
6
MBMFFB

Output
12
Input
16
MMBMBBBBMMMMMBMB

Output
29

Được gửi lên bởi:Jimmy
Ngày:2007-12-28
Thời gian chạy:1s
Giới hạn mã nguồn:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Ngôn ngữ cho phép:Tất cả ngoại trừ: ERL GOSU JS-RHINO NODEJS PERL6 PYPY RUST SED VB.NET
Nguồn bài:IOI 2007 - Croatia

hide comments
2019-12-24 05:33:55
Bài này giải bằng quy hoạch động nha: https://loptelink.pro/NKMINERS-huongdan
2017-10-11 10:50:42
{dinh truong lam}

lời khuyên này, hãy dùng mảng khi còn có thể, ngoài ra sẽ tle :(
2016-11-26 15:10:39 hồ vãn tuấn
IT là gì
2015-08-06 14:58:17 [Nghien] Le Long
có khi nào là IT? :v
2015-08-06 03:37:03 there's no salvation for me...


Last edit: 2015-08-06 04:20:52
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.