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: interview

Starter code:

Solutions:
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

#f = open('input', 'r')
f = sys.stdin

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]+0.00000001))
s = ''.join(x[0] for x in s)

cs = 0
ans = 0
for x in s:
  if x == 's':
    cs += 1
  else:
    ans += cs
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] + 0.0000001))
s = ''.join(x[0] for x in s)

for x in s:
  if x == 's': 
    od += 1
  else: 
    ans += od
print(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)

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]]
#print(fin)
p=0
q=0
ins=0
for i in fin:
    if(i=='s'):
        q+=1
    elif(i=='h'):
        p+=1
        ins+=q
print(ins)
    


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))


# Imports


##############################################
# Input
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))


##############################################
# Main code
sor = sorted(mas, key=lambda val:val[0], reverse=True)
str = ''

for item in sor:
    str += item[1]

#print(str)

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)






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)


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)

import functools

n = int(input())

t = [input() for i in range(n)]


def compare(s1, s2):
    if (s1.count('s') * len(s2)) < (s2.count('s') * len(s1)):
        return 1
    else:
        return -1


t = sorted(t, key=functools.cmp_to_key(compare))
s = ''.join(t)

c = 0
z = 0

for char in s:
    if char == 's':
        c += 1
    elif char == 'h':
        z += c
print(z)


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)

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-6))
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())
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]
#print(ss, hs2, hs)

for i in range(n):
    res += ss[i] * hs[i]
print(res)


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)


"""
Say I have two strings, u and v.
Let s(x) be the number of 's's in x.
Let h(x) be the number of 'h's in x.
Let sh(x) be the number of 'sh' subsequences in x.
Then
    sh(uv) = sh(u) + sh(v) + s(u)*h(v)
    sh(vu) = sh(v) + sh(u) + s(v)*h(u)
So we want uv if s(u)*h(v) > s(v)*h(u), else vu.
"""


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())
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)

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=1e18
	else:
		tmp=s/h
	a.append((int(s),int(h),tmp))
	#print(s,h)
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)


#input
n=int(input())

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

#calculation
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])
    else:
        if 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)
    
#output

print(noise)

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]+0.000001) )
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)


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))



# n = int(input())
# occurences = [''] * n
# t = [0] * n

# for i in range(n):
#     s = input()
#     occurences[i] = s
#     num_s = s.count('s')
#     num_h = s.count('h')
#     if num_h>0:
#         t[i]=num_s/num_h
#     else:
#         t[i]=10000000

# order = list(range(n))
# order.sort(reverse=True,key=lambda i:t[i])
# dum = ''
# for i in order:
#     dum += occurences[i]

# a = 0
# b = 0
# for c in dum:
#     if c=='s':
#         a+=1
#     else:
#         b+=a
# print(b)
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)

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)