Silent Classroom
Contest: Codeforces Round 561 (Div. 2)
Difficulty rating: 900
Tags: combinatorics, greedy

There are $$$n$$$ students in the first grade of Nlogonia high school. The principal wishes to split the students into two classrooms (each student must be in exactly one of the classrooms). Two distinct students whose name starts with the same letter will be chatty if they are put in the same classroom (because they must have a lot in common). Let $$$x$$$ be the number of such pairs of students in a split. Pairs $$$(a, b)$$$ and $$$(b, a)$$$ are the same and counted only once.

For example, if there are $$$6$$$ students: "olivia", "jacob", "tanya", "jack", "oliver" and "jessica", then:

- splitting into two classrooms ("jack", "jacob", "jessica", "tanya") and ("olivia", "oliver") will give $$$x=4$$$ ($$$3$$$ chatting pairs in the first classroom, $$$1$$$ chatting pair in the second classroom),
- splitting into two classrooms ("jack", "tanya", "olivia") and ("jessica", "oliver", "jacob") will give $$$x=1$$$ ($$$0$$$ chatting pairs in the first classroom, $$$1$$$ chatting pair in the second classroom).

You are given the list of the $$$n$$$ names. What is the minimum $$$x$$$ we can obtain by splitting the students into classrooms?

Note that it is valid to place all of the students in one of the classrooms, leaving the other one empty.

Input format:
The first line contains a single integer $$$n$$$ ($$$1\leq n \leq 100$$$) — the number of students.

After this $$$n$$$ lines follow.

The $$$i$$$-th line contains the name of the $$$i$$$-th student.

It is guaranteed each name is a string of lowercase English letters of length at most $$$20$$$. Note that multiple students may share the same name.

Output format:
The output must consist of a single integer $$$x$$$ — the minimum possible number of chatty pairs.

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Note:
In the first sample the minimum number of pairs is $$$1$$$. This can be achieved, for example, by putting everyone except jose in one classroom, and jose in the other, so jorge and jerry form the only chatty pair.

In the second sample the minimum number of pairs is $$$2$$$. This can be achieved, for example, by putting kambei, gorobei, shichiroji and kyuzo in one room and putting heihachi, katsushiro and kikuchiyo in the other room. In this case the two pairs are kambei and kyuzo, and katsushiro and kikuchiyo.

In the third sample the minimum number of pairs is $$$4$$$. This can be achieved by placing three of the students named mike in one classroom and the other two students in another classroom. Thus there will be three chatty pairs in one classroom and one chatty pair in the other classroom.

Example input:
4
jorge
jose
oscar
jerry

Example output:
1

Example input:
7
kambei
gorobei
shichiroji
kyuzo
heihachi
katsushiro
kikuchiyo

Example output:
2

Editorial:
First, note that we can solve the problem for each starting letter independently, because two students whose name starts with a different letter never form a chatty pair.
How do we solve the problem when all the students' names start with the same letter? We claim that it's best to split as evenly as possible. If one classroom has $$$a$$$ students, and the other has $$$b$$$ students with $$$a > b + 1$$$, then by moving one of the students from the first classroom into the second we remove $$$a - 1$$$ chatty pairs from the first classroom and create $$$b$$$ new chatty pairs in the second, for a net result of $$$(a - 1) - b > 0$$$ chatty pairs removed. So in the best splitting we have $$$\vert a - b \vert \leq 1$$$, meaning we split as evenly as possible.
Then, if $$$cnt_a$$$ denotes the number of students whose name starts with $$$a$$$, we will split them into a classroom containing $$$\lfloor \frac{cnt_a}{2} \rfloor$$$ students and one containing $$$\lceil \frac{cnt_a}{2} \rceil$$$ students. So the total number of chatty pairs among students whose name starts with $$$a$$$ is
$$$$$$\binom{\lfloor \frac{cnt_a}{2} \rfloor}{2} + \binom{\lceil \frac{cnt_a}{2} \rceil}{2}$$$$$$
The expression is the same for the other letters in the alphabet, and adding them all up gives us our answer. We can also solve the problem without determining what the best splitting is. If, for each starting letter, we try all the possible number of students to send into the first classroom, and choose the one that gives the minimal $$$x$$$, then we will only have to do $$$\mathcal{O}(n)$$$ checks in total, which is more than fast enough to solve the problem.
Complexity: $$$\mathcal{O}(n)$$$.