cswork / OAOpen practice workspace

MicrosoftOA #61

Shortest Compressed String After Removal

Estimated HardJudge-ready

Topics (AI-labelled)Two PointersStringsPrefix & Ranges

Delete exactly K consecutive characters, then compress each run of identical characters as "count+character"; a run of a single character is written as just the character. Return the shortest compressed length; K=0 and K=N are allowed. This site keeps the examples as curated in the catalog and computes them from the actual character counts.

This site uses a standard input/output protocol.

Input

The first line contains K, and the second line contains an uppercase string S. The catalog ranges are kept: 1≤N≤1000000, 0≤K≤min(100000,N).

Output

Output a single integer, the answer.

Example 1

Input:

3
ABBBCCDDCCC

Output:

5

Example 2

Input:

3
AAAAAAAAAABXXAAAAAAAAAA

Output:

3

Example 3

Input:

2
ABCDDDEFG

Output:

6

Example notes

Example 1: deleting DDC leaves ABBBCCCC, compressed to A3B4C with length 5. Example 2: there are 10 A on each side; deleting BXX gives 20A, with length 3. The catalog writes 21A here, which does not match its input; the original snapshot uses 11+10 A, for which its 21A is correct. Example 3: deleting EF gives ABC3DG, with length 6.

Editorial

Loading solution…