Pushok the dog has been chasing Imp for a few hours already. $48$ 

Fortunately, Imp knows that Pushok is afraid of a robot vacuum cleaner. 

While moving, the robot generates a string t consisting of letters 's' and 'h', that produces a lot of noise. We define noise of string t as the number of occurrences of string "sh" as a subsequence in it, in other words, the number of such pairs (i, j), that i < j and $t_{i} = s$ and $t_{j} = h$. 

The robot is off at the moment. Imp knows that it has a sequence of strings t_{i} in its memory, and he can arbitrary change their order. When the robot is started, it generates the string t as a concatenation of these strings in the given order. The noise of the resulting string equals the noise of this concatenation.

Help Imp to find the maximum noise he can achieve by changing the order of the strings.


-----Input-----

The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of strings in robot's memory.

Next n lines contain the strings t_1, t_2, ..., t_{n}, one per line. It is guaranteed that the strings are non-empty, contain only English letters 's' and 'h' and their total length does not exceed 10^5.


-----Output-----

Print a single integer — the maxumum possible noise Imp can achieve by changing the order of the strings.


-----Examples-----
Input
4
ssh
hs
s
hhhs

Output
18

Input
2
h
s

Output
1



-----Note-----

The optimal concatenation in the first sample is ssshhshhhs.

Difficulty: MEDIUM_HARD
Tags: Sorting

Greedy algorithms
Skill types: Sorting

Greedy algorithms

Starter code:

Solutions:
n = int(input())
a = []
for i in range(n):
	b = input()
	a.append([b.count('h') / len(b), b])
a.sort()
b = ''
for i in range(n):
	b = b + a[i][1]
c = 0
s = 0
for i in range(len(b)):
	if b[i] == 's':
		c = c + 1
	if b[i] == 'h':
		s = s + c
print(s)


import getpass
import sys
import math
import functools

def ria():
	return [int(i) for i in input().split()]
files = True
if getpass.getuser() == 'frohenk' and files:
	sys.stdin = open('test.in')
n = ria()[0]
ar = []
for i in range(n):
	st = input()
	ar.append((st.count('s'), st.count('h'), st))
ar.sort(key=functools.cmp_to_key(lambda x, y: -x[0] * y[1] + x[1] * y[0]))
fst = ''
for (d, a, b) in ar:
	fst += b
suma = 0
kek = 0
for i in fst:
	if i == 's':
		kek += 1
	else:
		suma += kek
print(suma)
sys.stdout.close()


import math
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
mod = 10 ** 9 + 7
mod1 = 998244353
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class TreeNode:

	def __init__(self, k, v):
		self.key = k
		self.value = v
		self.left = None
		self.right = None
		self.parent = None
		self.height = 1
		self.num_left = 1
		self.num_total = 1

class AvlTree:

	def __init__(self):
		self._tree = None

	def add(self, k, v):
		if not self._tree:
			self._tree = TreeNode(k, v)
			return
		node = self._add(k, v)
		if node:
			self._rebalance(node)

	def _add(self, k, v):
		node = self._tree
		while node:
			if k < node.key:
				if node.left:
					node = node.left
				else:
					node.left = TreeNode(k, v)
					node.left.parent = node
					return node.left
			elif node.key < k:
				if node.right:
					node = node.right
				else:
					node.right = TreeNode(k, v)
					node.right.parent = node
					return node.right
			else:
				node.value = v
				return

	@staticmethod
	def get_height(x):
		return x.height if x else 0

	@staticmethod
	def get_num_total(x):
		return x.num_total if x else 0

	def _rebalance(self, node):
		n = node
		while n:
			lh = self.get_height(n.left)
			rh = self.get_height(n.right)
			n.height = max(lh, rh) + 1
			balance_factor = lh - rh
			n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right)
			n.num_left = 1 + self.get_num_total(n.left)
			if balance_factor > 1:
				if self.get_height(n.left.left) < self.get_height(n.left.right):
					self._rotate_left(n.left)
				self._rotate_right(n)
			elif balance_factor < -1:
				if self.get_height(n.right.right) < self.get_height(n.right.left):
					self._rotate_right(n.right)
				self._rotate_left(n)
			else:
				n = n.parent

	def _remove_one(self, node):
		replacement = node.left or node.right
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = replacement
			else:
				node.parent.right = replacement
			replacement.parent = node.parent
			node.parent = None
		else:
			self._tree = replacement
			replacement.parent = None
		node.left = None
		node.right = None
		node.parent = None
		self._rebalance(replacement)

	def _remove_leaf(self, node):
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = None
			else:
				node.parent.right = None
			self._rebalance(node.parent)
		else:
			self._tree = None
		node.parent = None
		node.left = None
		node.right = None

	def remove(self, k):
		node = self._get_node(k)
		if not node:
			return
		if AvlTree._is_leaf(node):
			self._remove_leaf(node)
			return
		if node.left and node.right:
			nxt = AvlTree._get_next(node)
			node.key = nxt.key
			node.value = nxt.value
			if self._is_leaf(nxt):
				self._remove_leaf(nxt)
			else:
				self._remove_one(nxt)
			self._rebalance(node)
		else:
			self._remove_one(node)

	def get(self, k):
		node = self._get_node(k)
		return node.value if node else -1

	def _get_node(self, k):
		if not self._tree:
			return None
		node = self._tree
		while node:
			if k < node.key:
				node = node.left
			elif node.key < k:
				node = node.right
			else:
				return node
		return None

	def get_at(self, pos):
		x = pos + 1
		node = self._tree
		while node:
			if x < node.num_left:
				node = node.left
			elif node.num_left < x:
				x -= node.num_left
				node = node.right
			else:
				return (node.key, node.value)
		raise IndexError('Out of ranges')

	@staticmethod
	def _is_left(node):
		return node.parent.left and node.parent.left == node

	@staticmethod
	def _is_leaf(node):
		return node.left is None and node.right is None

	def _rotate_right(self, node):
		if not node.parent:
			self._tree = node.left
			node.left.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.left
			node.left.parent = node.parent
		else:
			node.parent.right = node.left
			node.left.parent = node.parent
		bk = node.left.right
		node.left.right = node
		node.parent = node.left
		node.left = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	def _rotate_left(self, node):
		if not node.parent:
			self._tree = node.right
			node.right.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.right
			node.right.parent = node.parent
		else:
			node.parent.right = node.right
			node.right.parent = node.parent
		bk = node.right.left
		node.right.left = node
		node.parent = node.right
		node.right = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	@staticmethod
	def _get_next(node):
		if not node.right:
			return node.parent
		n = node.right
		while n.left:
			n = n.left
		return n
avl = AvlTree()

class SegmentTree1:

	def __init__(self, data, default='z', func=lambda a, b: min(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class SegmentTree:

	def __init__(self, data, default=0, func=lambda a, b: a + b):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD

def powm(a, n, m):
	if a == 1 or n == 0:
		return 1
	if n % 2 == 0:
		s = powm(a, n // 2, m)
		return s * s % m
	else:
		return a * powm(a, n - 1, m) % m

def sort_list(list1, list2):
	zipped_pairs = zip(list2, list1)
	z = [x for (_, x) in sorted(zipped_pairs)]
	return z

def product(l):
	por = 1
	for i in range(len(l)):
		por *= l[i]
	return por

def binarySearchCount(arr, n, key):
	left = 0
	right = n - 1
	count = 0
	while left <= right:
		mid = int((right + left) / 2)
		if arr[mid] <= key:
			count = mid + 1
			left = mid + 1
		else:
			right = mid - 1
	return count

def countdig(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def countGreater(arr, n, k):
	l = 0
	r = n - 1
	leftGreater = n
	while l <= r:
		m = int(l + (r - l) / 2)
		if arr[m] >= k:
			leftGreater = m
			r = m - 1
		else:
			l = m + 1
	return n - leftGreater
n = int(input())
a = []
ind = defaultdict(list)
r = []
for i in range(n):
	a.append(input())
	r.append([a[-1].count('h') / len(a[-1]), a[-1]])
r.sort()
ans = ''
for i in range(n):
	for j in range(len(r[i][1])):
		ans += r[i][1][j]
tot = 0
sc = 0
for i in range(len(ans)):
	if ans[i] == 'h':
		tot += sc
	else:
		sc += 1
print(tot)


n = int(input())
s = [input() for i in range(n)]
s = [(x, x.count('s'), x.count('h')) for x in s]
s.sort(key=lambda x: x[2] / float(x[1] + 1e-06))
s = ''.join((x[0] for x in s))
(pre, ans) = (0, 0)
for x in s:
	if x == 's':
		pre += 1
	else:
		ans += pre
print(ans)


n = int(input())
mas = []
all_h = 0
for i in range(n):
	line = input()
	s = line.count('s')
	h = line.count('h')
	all_h += h
	if h == 0:
		k = 100000
	else:
		k = s / h
	mas.append((k, line))
sor = sorted(mas, key=lambda val: val[0], reverse=True)
str = ''
for item in sor:
	str += item[1]
count = 0
count_h = all_h
for i in range(len(str)):
	if str[i] == 'h':
		count_h -= 1
	else:
		count += count_h
print(count)


from sys import stdin
import functools

def read():
	return map(int, stdin.readline().split())

def compare(a, b):
	val = a[0] * b[1] - a[1] * b[0]
	return -1 if val < 0 else 1 if val > 0 else 0
(n,) = read()
arr = []
for i in range(n):
	s = input()
	hCnt = 0
	sCnt = 0
	total = 0
	for j in reversed(range(len(s))):
		if s[j] == 'h':
			hCnt += 1
		elif s[j] == 's':
			sCnt += 1
			total += hCnt
	arr.append((sCnt, hCnt, total))
arr = sorted(arr, key=functools.cmp_to_key(compare))
cntH = 0
ans = 0
for (a, b, c) in arr:
	ans += c + a * cntH
	cntH += b
print(ans)


n = int(input())
a = [input().strip() for i in range(n)]
a = [(z, z.count('s'), z.count('h')) for z in a]
a = sorted(a, key=lambda p: -p[1] / (p[2] + 1e-06))
s = ''.join((z[0] for z in a))
(ans, kols) = (0, 0)
for c in s:
	if c == 's':
		kols += 1
	else:
		ans += kols
print(ans)


import functools
li = []

def cmp(a, b):
	if a.count('s') / len(a) < b.count('s') / len(b):
		return 1
	else:
		return -1
for _ in range(int(input())):
	s = input()
	li.append(s)
li = sorted(li, key=functools.cmp_to_key(cmp))
li = ''.join(li)
c = ans = 0
for i in li:
	if i == 's':
		c += 1
	else:
		ans += c
print(ans)


import sys
f = sys.stdin
ans = 0
od = 0
n = int(f.readline())
s = [f.readline().strip() for _ in range(n)]
s = [(x, x.count('s'), x.count('h')) for x in s]
s = sorted(s, key=lambda x: x[2] / float(x[1] + 1e-07))
s = ''.join((x[0] for x in s))
for x in s:
	if x == 's':
		od += 1
	else:
		ans += od
print(ans)


from functools import cmp_to_key
n = int(input())
strings = [input() for _ in range(n)]
strings = [(s, s.count('s'), s.count('h')) for s in strings]

def compare(u, v):
	(_, su, hu) = u
	(_, sv, hv) = v
	suxhv = su * hv
	svxhu = sv * hu
	if suxhv > svxhu:
		return -1
	elif svxhu > suxhv:
		return 1
	else:
		return 0
strings.sort(key=cmp_to_key(compare))
noise = 0
ss = 0
for (string, _, _) in strings:
	for c in string:
		if c == 's':
			ss += 1
		else:
			noise += ss
print(noise)


n = int(input())
l = []
for i in range(n):
	l.append(input())
l.sort(key=lambda s: -(s.count('s') / (s.count('h') + 1e-07)))
s = ''.join(l)
count = 0
l = [0] * len(s)
for i in range(len(s)):
	if s[i] == 's':
		count += 1
	l[i] = count
count = 0
for i in range(1, len(s)):
	if s[i] == 'h':
		count += l[i - 1]
print(count)


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
import collections
from itertools import permutations
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import sys
from functools import cmp_to_key

def val(s):
	cnt = 0
	ret = 0
	for c in s:
		if c == 's':
			cnt += 1
		else:
			ret += cnt
	return ret

def cmp(a, b):
	v1 = val(a + b)
	v2 = val(b + a)
	if v1 < v2:
		return 1
	elif v1 > v2:
		return -1
	return 0
ans = ''
s = []
n = int(input())
for i in range(0, n):
	t = input()
	s.append(t)
s = sorted(s, key=cmp_to_key(cmp))
ans = ''.join(s)
print(val(ans))


n = int(input())

def insert(s, t, ret, string, c, scount, tcount):
	maxval = s * tcount
	val = maxval
	maxpos = 0
	for i in range(len(string)):
		(si, ti, ci) = string[i]
		val = val + si * t * ci - s * ti * ci
		if maxval < val:
			maxval = val
			maxpos = i + 1
	string = string[:maxpos] + [(s, t, c)] + string[maxpos:]
	return (ret + maxval * c, string)

def count2(m, memo):
	if m in memo:
		return memo[m]
	s = 0
	ret = 0
	for c in m:
		if c == 's':
			s += 1
		else:
			ret += s
	memo[m] = (ret, s)
	return (ret, s)
memo = {}
ret = 0
d = {}
for _ in range(n):
	m = input()
	(c, s) = count2(m, memo)
	ret += c
	t = len(m) - s
	if (s, t) not in d:
		d[s, t] = 0
	ret += s * t * d[s, t]
	d[s, t] += 1
string = []
scount = 0
tcount = 0
for (s, t) in d:
	c = d[s, t]
	(ret, string) = insert(s, t, ret, string, c, scount, tcount)
	scount += s * c
	tcount += t * c
print(ret)


import functools
n = int(input())
arr = [input() for i in range(n)]

def compare(s1, s2):
	a = s1.count('s')
	b = s2.count('s')
	if a * len(s2) < b * len(s1):
		return 1
	return -1
arr = sorted(arr, key=functools.cmp_to_key(compare))
s = ''.join(arr)
c = 0
t = 0
for char in s:
	if char == 's':
		c += 1
	elif char == 'h':
		t += c
print(t)


import sys
from functools import cmp_to_key

def val(s):
	cnt = 0
	ret = 0
	for c in s:
		if c == 's':
			cnt += 1
		else:
			ret += cnt
	return ret

def cmp(a, b):
	v1 = val(a + b)
	v2 = val(b + a)
	if v1 < v2:
		return 1
	elif v1 > v2:
		return -1
	return 0
ans = ''
s = []
n = int(input())
for i in range(0, n):
	t = input()
	s.append(t)
s = sorted(s, key=cmp_to_key(cmp))
ans = ''.join(s)
print(val(ans))


n = int(input())
ts = []
res = 0
for _ in range(n):
	i = input().strip()
	m_res = 0
	s_count = 0
	for c in i:
		if c == 's':
			s_count += 1
		else:
			m_res += s_count
	res += m_res
	ts.append((i.count('s'), i.count('h')))
ts = sorted(ts, key=lambda k: float(k[0]) / float(k[1]) if k[1] else float('inf'), reverse=True)
ss = [s for (s, h) in ts]
hs2 = [h for (s, h) in ts]
hs = [0 for _ in ts]
for i in range(n - 2, -1, -1):
	hs[i] = hs2[i + 1] + hs[i + 1]
for i in range(n):
	res += ss[i] * hs[i]
print(res)


def key(x):
	s = x.count('s')
	return s / len(x)

def sh_produce(x):
	x = str(x)
	sh = [0 for j in range(len(x))]
	if x[-1] == 'h':
		sh[-1] = 1
	for i in range(len(x) - 2, -1, -1):
		if x[i] == 'h':
			sh[i] = sh[i + 1] + 1
		else:
			sh[i] = sh[i + 1]
	sh_num = 0
	for i in range(len(x)):
		if x[i] == 's':
			sh_num += sh[i]
	return sh_num
n = int(input())
input_list = []
for i in range(n):
	tmp = input()
	input_list.append(tmp)
input_list = sorted(input_list, key=key, reverse=True)
res = ''.join(input_list)
print(sh_produce(res))


def count_s(s):
	ret = 0
	for k in s:
		if k == 's':
			ret += 1
	return ret

def count_sh(s):
	ret = 0
	ls = []
	for k in s:
		if k == 's':
			ls.append(0)
		else:
			ls.append(1)
	ls.reverse()
	for i in range(1, len(ls)):
		ls[i] += ls[i - 1]
	ls.reverse()
	for i in range(0, len(s) - 1):
		if s[i] == 's':
			ret += ls[i]
	return ret
n = int(input())
ls = []
ans = 0
for _ in range(n):
	s = input()
	cnt = count_s(s)
	ans += count_sh(s)
	if cnt == len(s):
		ls.append([s, 100000000000, cnt, 0])
	else:
		ls.append([s, cnt * 1.0 / (len(s) - cnt), cnt, len(s) - cnt])
ls.sort(key=lambda x: x[1], reverse=True)
ls.reverse()
for i in range(1, len(ls)):
	ls[i][3] += ls[i - 1][3]
ls.reverse()
for i in range(0, len(ls) - 1):
	ans += ls[i][2] * ls[i + 1][3]
print(ans)


import sys, os, io
from sys import stdin
from functools import cmp_to_key
from bisect import bisect_left, bisect_right

def ii():
	return int(input())

def li():
	return list(map(int, input().split()))
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
for _ in range(1):
	n = ii()
	l = []
	ans = ''
	last = ''
	for i in range(n):
		s = input()
		y = s.count('h')
		l.append([y / len(s), s])
	l.sort()
	ans = []
	for i in l:
		ans += i[1]
	ans = ''.join(ans)
	s = ans[:]
	pres = [0] * len(s)
	sufh = [0] * len(s)
	for i in range(len(s)):
		if i > 0:
			pres[i] += pres[i - 1]
		if s[i] == 's':
			pres[i] += 1
	for i in range(len(s) - 1, -1, -1):
		if i < len(s) - 1:
			sufh[i] += sufh[i + 1]
		if s[i] == 'h':
			sufh[i] += 1
	cnt = 0
	for i in range(len(s)):
		if s[i] == 's':
			cnt += sufh[i]
	print(cnt)


get = lambda : map(int, input().split())
f = next(get())
a = []
for i in range(f):
	s = input()
	a.append(s)

def key(y):
	t = y.count('s')
	if not t:
		return float('inf')
	return -(t - len(y)) / t
a.sort(key=key)
scnt = 0
ans = 0
s = ''.join(a)
for i in s:
	if i == 's':
		scnt += 1
	else:
		ans += scnt
print(ans)


class Node(object):

	def __init__(self, s, h, sh):
		self.s = s
		self.h = h
		self.sh = sh

	def __lt__(self, other):
		return self.s * other.h > other.s * self.h
M = int(100000.0 + 5)
c = []
n = int(input())
for i in range(n):
	ss = input()
	(sv, hv, shv) = (0, 0, 0)
	for e in ss:
		if e == 's':
			sv += 1
		else:
			shv += sv
			hv += 1
	c.append(Node(sv, hv, shv))
c = sorted(c)
(pre_s, ans) = (0, 0)
for e in c:
	ans += e.sh + pre_s * e.h
	pre_s += e.s
print(ans)


import functools

def cmp(a, b):
	if a.count('s') / len(a) < b.count('s') / len(b):
		return 1
	else:
		return -1
n = int(input())
a = [input() for i in range(n)]
a = sorted(a, key=functools.cmp_to_key(cmp))
a = ''.join(a)
c = 0
t = 0
for i in a:
	if i == 's':
		c += 1
	else:
		t += c
print(t)


import sys

def input():
	return sys.stdin.readline().rstrip()

class STR:

	def __init__(self, x, y):
		self.s = x
		self.h = y

	def __gt__(self, other):
		(ss, sh) = (self.s, self.h)
		(os, oh) = (other.s, other.h)
		return oh * ss <= os * sh
n = int(input())
ans = 0
que = []
for i in range(n):
	S = list(input())
	L = len(S)
	ccnt = 0
	hcnt = 0
	for j in range(L - 1, -1, -1):
		if S[j] == 'h':
			hcnt += 1
		else:
			ccnt += 1
			ans += hcnt
	que.append(STR(ccnt, hcnt))
que.sort(reverse=True)
(ccnt, hcnt) = (0, 0)
for v in que:
	(nows, nowh) = (v.s, v.h)
	ans += nows * hcnt
	hcnt += nowh
print(ans)


import sys
import functools

def sdiff(a, b):
	(nas, nah, nbs, nbh) = (0, 0, 0, 0)
	for c in a:
		if c == 's':
			nas += 1
		elif c == 'h':
			nah += 1
	for c in b:
		if c == 's':
			nbs += 1
		elif c == 'h':
			nbh += 1
	return nbs * nah - nas * nbh
n = int(sys.stdin.readline().strip())
s = sorted(sys.stdin.readlines(), key=functools.cmp_to_key(sdiff))
(r, rs) = (0, 0)
for ss in s:
	for c in ss:
		if c == 's':
			rs += 1
		elif c == 'h':
			r += rs
print(r)


import sys
f = sys.stdin
n = int(f.readline())
s = [f.readline().strip() for i in range(n)]
s = [(x, x.count('s'), x.count('h')) for x in s]
s = sorted(s, key=lambda x: -x[1] / (x[2] + 0.0001))
s = ''.join((x[0] for x in s))
(ans, tmp) = (0, 0)
for x in s:
	if x == 'h':
		ans += tmp
	else:
		tmp += 1
print(ans)


def key(x):
	try:
		return x.count('h') / x.count('s')
	except ZeroDivisionError:
		return 10 ** 9
n = int(input())
t = ''.join(sorted((input() for _ in range(n)), key=key))
(res, cnt) = (0, 0)
for ti in t:
	if ti == 's':
		cnt += 1
	if ti == 'h':
		res += cnt
print(res)


n = int(input())
a = []
for i in range(n):
	s = input()
	l = 0
	for c in s:
		if c == 's':
			l += 1
	a.append((s, l / len(s)))
a.sort(key=lambda x: x[1], reverse=True)
ns = 0
ans = 0
for (st, _) in a:
	for c in st:
		if c == 's':
			ns += 1
		else:
			ans += ns
print(ans)


n = int(input())
s = []
a = []
for i in range(n):
	s.append(input())
	p = 0
	q = 0
	for j in s[i]:
		if j == 's':
			p += 1
		elif j == 'h':
			q += 1
	val = (q - p) / (q + p)
	a.append([val, i])
a.sort()
fin = ''
for i in range(n):
	fin += s[a[i][1]]
p = 0
q = 0
ins = 0
for i in fin:
	if i == 's':
		q += 1
	elif i == 'h':
		p += 1
		ins += q
print(ins)


n = int(input())
a = []
for i in range(n):
	k = input()
	(s, t) = (0, 0)
	for j in k:
		if j == 's':
			s += 1
		else:
			t += 1
	if t > 0:
		a.append([k, s / t])
	else:
		a.append([k, 100000])
a.sort(key=lambda x: x[1])
a = list(reversed(a))
l = a[0][0][0]
x = 0
m = []
for i in a:
	for j in i[0]:
		if j != l:
			m.append(x)
			x = 1
			l = j
		else:
			x += 1
m.append(x)
if a[0][0][0] == 'h':
	m = m[1:]
(p, q) = ([], [])
e = 0
z = len(m)
for i in range(z):
	if i % 2 == 0:
		p.append(m[i])
m = list(reversed(m))
if z % 2 == 1:
	m = m[1:]
	z -= 1
for i in range(z):
	if i % 2 == 0:
		e += m[i]
		q.append(e)
q = list(reversed(q))
ans = 0
for i in range(z // 2):
	ans += p[i] * q[i]
print(ans)


def mycmp(a, b):
	if a[0] * b[1] > b[0] * a[1]:
		return -1
	return 1
n = int(input())
a = []
ans = 0
for i in range(1, n + 1):
	str = input()
	totlen = len(str)
	s = h = 0
	for j in range(0, totlen):
		if str[j] == 's':
			s = s + 1
		else:
			h = h + 1
			ans = ans + s
	tmp = 0
	if h == 0:
		tmp = 1e+18
	else:
		tmp = s / h
	a.append((int(s), int(h), tmp))
a.sort(key=lambda x: x[2], reverse=True)
sum = 0
for i in range(0, len(a)):
	ans = ans + sum * a[i][1]
	sum = sum + a[i][0]
print(ans)


n = int(input())
a = []
for i in range(0, n):
	a.append(input())
noise = 0

def calc_noise(str):
	result = 0
	struct = []
	struct.append(0)
	c = 's'
	j = 0
	str.lstrip('h')
	for i in range(0, len(str)):
		if str[i] == c:
			struct[j] += 1
		else:
			c = str[i]
			struct.append(1)
			j += 1
	s = 0
	for i in range(0, len(struct)):
		if i % 2 != 0:
			result += s * struct[i]
		else:
			s += struct[i]
	return result
v = []
for i in range(0, n):
	cs = a[i].count('s')
	ch = len(a[i]) - cs
	sound = calc_noise(a[i])
	if cs == 0:
		v.append([i, -100000, 0])
	elif ch == 0:
		v.append([i, 100000, 0])
	else:
		v.append([i, cs / ch, sound])
v.sort(key=lambda x: x[2], reverse=1)
v.sort(key=lambda x: x[1], reverse=1)
str = ''
for i in range(0, n):
	str += a[v[i][0]]
noise = calc_noise(str)
print(noise)


n = int(input())
li = []
for i in range(n):
	inp = input()
	li.append([inp.count('h') / len(inp), inp])
li.sort()
ans = ''
for i in range(n):
	ans += li[i][1]
count = 0
pri = 0
for i in range(1, len(ans) + 1):
	if ans[-i] == 'h':
		count += 1
	else:
		pri += count
print(pri)