Under construction

Still being built. Things may change

Hoof, Paper, Scissors (Gold)

Interview

Time limit2sMemory limit512 MB

Summary
Given John's sequence of N gestures and at most K gesture switches, find the maximum number of games Bessie can win.
Level

Medium6 of 10

Topics
Dynamic programming, Greedy, Prefix sum, Implementation
Solved
No attempts yet

Problem

You have probably heard of the game "Rock, Paper, Scissors". The cows like to play a similar game they call "Hoof, Paper, Scissors".

The rules of "Hoof, Paper, Scissors" are simple. Two cows play against each other. They both count to three, and then each one simultaneously makes a gesture that represents either a hoof, a piece of paper, or a pair of scissors. Hoof beats scissors (a hoof can smash a pair of scissors), scissors beats paper (scissors can cut paper), and paper beats hoof (the hoof can get a papercut). For example, if the first cow makes a "hoof" gesture and the second makes a "paper" gesture, the second cow wins. Both cows can also make the same gesture, in which case the game is a tie.

Farmer John wants to play NN games of "Hoof, Paper, Scissors" against his prize cow, Bessie (1≤N≤100 0001 \le N \le 100\,000). Bessie is an expert at the game and can predict each of John's gestures before he makes it. Unfortunately, Bessie is a cow, so she is also very lazy. She tends to play the same gesture many times in a row. In fact, she is only willing to switch gestures at most KK times over the entire set of games (0≤K≤200 \le K \le 20). For example, if K=2K=2, she might play "hoof" for the first few games, then switch to "paper" for a while, then finish the remaining games playing "hoof".

Given the sequence of gestures John will play, determine the maximum number of games that Bessie can win.

Input

The first line contains NN and KK.

Each of the next NN lines contains one of John's gestures, in order. Each gesture is H (hoof), P (paper), or S (scissors).

Output

Print the maximum number of games Bessie can win if she changes gestures at most KK times.

Examples1

  1. Example 1

    Input
    5 1
    P
    P
    H
    P
    S
    
    Expected output
    4