Skip to content

Instantly share code, notes, and snippets.

@avamsi
Created November 26, 2014 17:09
Show Gist options
  • Save avamsi/5a056de3807dfc6a08f0 to your computer and use it in GitHub Desktop.
Save avamsi/5a056de3807dfc6a08f0 to your computer and use it in GitHub Desktop.
ld = {'BC': [], 'CB': []}
nd = {'CC': 0, 'BB': 0}
for _ in xrange(int(raw_input())):
s = raw_input()
try:
ld[s[0] + s[-1]].append(len(s))
except KeyError:
nd[s[0] + s[-1]] += len(s)
if len(ld['BC']) < len(ld['CB']):
a = 'BC'
b = 'CB'
else:
a = 'CB'
b = 'BC'
if len(ld['BC']) == len(ld['CB']) == 0:
print max(nd.values())
else:
ans = sum(ld[a])
ans += sum(nd.values())
ans += sum(sorted(ld[b], reverse=True)[: len(ld[a]) + 1])
print ans
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment