This page is still under construction.

Parts of this page are still being built. What you see may change.

Broken line

Time limit1sMemory limit512 MB

Summary
Assign each of the up to 16 distinct letters a fixed arrow (right or up) so the area under the resulting monotone staircase path is maximized.
Level

Hard8 of 10

Topics
Greedy, Bit manipulation, Implementation, Math
Solved
No attempts yet

Problem

Basia has a string ss, each character being one of the first 16 lowercase letters of the English alphabet.

Each character of this string will be replaced by an arrow to the right or up, but the same letters have to be replaced by the same arrow. For example, the string "banan" can be replaced to ↑↑→↑→\uparrow \uparrow \rightarrow \uparrow \rightarrow or ↑↑↑↑↑\uparrow \uparrow \uparrow \uparrow \uparrow, but you cannot obtain →→→↑→\rightarrow \rightarrow \rightarrow \uparrow \rightarrow because it would require replacing two letters 'a' with different arrows.

Basia will use the resulting sequence of arrows to draw a broken line. She will start with a pencil set at point (0,0)(0, 0), then nn times she will move it 11 unit right or up -- in the direction of the next arrow.

As a result of this drawing we will denote the area between the broken line and the OX axis. Formally, this area is a set of points (x,y)(x, y) such that y≥0y \geq 0 and there is a point (x,y′)(x, y') that belongs to the broken line and y′≥yy' \geq y occurs.

What is the largest possible result of Basia's drawing?

Input

The first and the only line of the standard input contains one string ss (1≤∣s∣≤300 0001 \leq |s| \leq 300\,000), consisting of lowercase letters of the English alphabet 'a'-'p' (16 possible characters).

Output

Output one integer -- the largest possible result of the drawing obtained after conversion from letters to arrows using given rules.

Hint

String "banan" should be replaced with ↑↑→↑→\uparrow \uparrow \rightarrow \uparrow \rightarrow. The area under the broken line is then 55:

For string "abcdefghijklmnopaaaa" there are two optimal solutions with the area 90:

Examples2

  1. Example 1

    Input
    banan
    
    Expected output
    5
    
  2. Example 2

    Input
    abcdefghijklmnopaaaa
    
    Expected output
    90