Technocup 2017 - Elimination Round 3
D
Santa Claus and a Palindrome
Codeforces
2100
原题
← CE →
翻译中…

Santa Claus likes palindromes very much. There was his birthday recently. $*k*$ of his friends came to him to congratulate him, and each of them presented to him a string $*s*_{*i*}$ having the same length $*n*$. We denote the beauty of the $*i*$-th string by $*a*_{*i*}$. It can happen that $*a*_{*i*}$ is negative — that means that Santa doesn't find this string beautiful at all.

翻译中…

Santa Claus is crazy about palindromes. He is thinking about the following question: what is the maximum possible total beauty of a palindrome which can be obtained by concatenating some (possibly all) of the strings he has? Each present can be used at most once. Note that all strings have the same length $*n*$.

翻译中…

Recall that a palindrome is a string that doesn't change after one reverses it.

翻译中…

Since the empty string is a palindrome too, the answer can't be negative. Even if all $*a*_{*i*}$'s are negative, Santa can obtain the empty string.

翻译中…

Input

翻译中…

The first line contains two positive integers $*k*$ and $*n*$ divided by space and denoting the number of Santa friends and the length of every string they've presented, respectively ($1 ≤ *k*, *n* ≤ 100 000$; $*n*·*k*  ≤ 100 000$).

翻译中…

$*k*$ lines follow. The $*i*$-th of them contains the string $*s*_{*i*}$ and its beauty $*a*_{*i*}$ ($- 10 000 ≤ *a*_{*i*} ≤ 10 000$). The string consists of $*n*$ lowercase English letters, and its beauty is integer. Some of strings may coincide. Also, equal strings can have different beauties.

翻译中…

Output

翻译中…

In the only line print the required maximum possible beauty.

翻译中…

Note

翻译中…

In the first example Santa can obtain abbaaaxyxaaabba by concatenating strings $5$, $2$, $7$, $6$ and $3$ (in this order).

翻译中…

样例

Input #1
7 3
abb 2
aaa -3
bba -1
zyz -4
abb 5
aaa 7
xyx 4
Output #1
12
InputCopy OutputCopy
Input #2
3 1
a 1
a 2
a 3
Output #2
6
InputCopy OutputCopy
Input #3
2 5
abcde 10000
abcde 10000
Output #3
0
InputCopy OutputCopy
题目信息
题号
748D
时间限制
2 s
内存限制
256 MB
标签
构造
数据结构
贪心

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。