time limit per test

2 seconds
memory limit per test

256 megabytes

input

standard input

output

standard output

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 si having the same length n. We denote the beauty of the i-th string by ai. It can happen that ai 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 ai'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 si and its beauty ai ( - 10 000 ≤ ai ≤ 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.

Examples
input
7 3
abb 2
aaa -3
bba -1
zyz -4
abb 5
aaa 7
xyx 4
output
12
input
3 1
a 1
a 2
a 3
output
6
input
2 5
abcde 10000
abcde 10000
output
0
Note

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

map+堆+贪心

由于所有的字符串长度都相等,所以不必考虑不同字符串间的组合。

如果有两个字符串可以拼成回文字符串,且它们的价值和大于0,那么可以将这两个字符串一左一右添加进已有的串里。

↑字符串可能重复出现,为了贪心选取价值和最大的两个串加进已有串,可以先将它们的价值push进大根堆里(priority_queue)

为了建立字符串到堆下标的映射,再开一个map。codeforces评测机跑得飞快,不必担心时间。

之后在所有没有使用的回文串中,找到价值最大的,作为总串的中心。

(但是这还没有结束)

然而WA掉了。

发现一个bug:

例如

aba 10

aba -1

如果总共就这两个字符串,那么光添加一个aba显然比匹配一对aba收益大。

为此又加了一个“反悔”操作(第47行),AC

 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue>
#include<map>
using namespace std;
const int mxn=;
map<string,int>mp;
priority_queue<int>p[mxn];
int cnt=;
bool hw[mxn];
string s[mxn],c;
int score[mxn];
int n,k;
int ans=;
bool pd(string ch){
int l=n/;
for(int i=;i<l;i++){
if(ch[i]!=ch[n-i-])return ;
}
return ;
}
int main(){
int i,j,w;
cin>>k>>n;
for(i=;i<=k;i++){
cin>>s[i]>>w;
if(!mp[s[i]])mp[s[i]]=++cnt;
p[mp[s[i]]].push(w);
if(!hw[mp[s[i]]]){
if(pd(s[i]))hw[mp[s[i]]]=;
}
}
int mx=;
for(i=;i<=k;i++){
if(p[mp[s[i]]].empty())continue;
c=s[i];
reverse(c.begin(),c.end());
int t=mp[c];
int x=mp[s[i]];
w=p[x].top();
p[x].pop();
if(t && !p[t].empty() && p[t].top()+w>){
if(p[t].top()*w< && hw[x]){
mx=max(mx,max(p[t].top(),w)-p[t].top()-w);
}
ans+=p[t].top()+w;
p[t].pop();
}
else p[x].push(w);
}
// printf("%d %d\n",ans,mx);
for(i=;i<=cnt;i++){
if(hw[i] && !p[i].empty()){
mx=max(mx,p[i].top());
}
}
printf("%d\n",ans+mx);
return ;
}

Codeforces Round #389 Div.2 D. Santa Claus and a Palindrome的更多相关文章

  1. Codeforces Round #389 Div.2 E. Santa Claus and Tangerines

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  2. Codeforces Round #389 Div.2 C. Santa Claus and Robot

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  3. Codeforces Round #389 Div.2 B. Santa Claus and Keyboard Check

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  4. Codeforces Round #389 Div.2 A. Santa Claus and a Place in a Class

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  5. Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3) D. Santa Claus and a Palindrome STL

    D. Santa Claus and a Palindrome time limit per test 2 seconds memory limit per test 256 megabytes in ...

  6. Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3) E. Santa Claus and Tangerines

    E. Santa Claus and Tangerines time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  7. Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3) C

    Description Santa Claus has Robot which lives on the infinite grid and can move along its lines. He ...

  8. Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3) B

    Description Santa Claus decided to disassemble his keyboard to clean it. After he returned all the k ...

  9. Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3) A

    Description Santa Claus is the first who came to the Christmas Olympiad, and he is going to be the f ...

随机推荐

  1. 20款最佳用户体验的Sublime Text 2/3主题下载及安装方法

    20款最佳用户体验的Sublime Text 2/3主题下载及安装方法

  2. Bash on Windows 抢鲜测试 -- 介绍及安装

    前言 微软在上周的Windows BUILD大会上宣布,WIN10将引入原生Bash,并将很快在技术预览版中推出. 如此一来,windows的命令行工具就不再只有cmd和powershell了,我们可 ...

  3. SVN——配置和安装

    SVN安装步骤: 所有安装文件下载地址:http://pan.baidu.com/s/1bocNTDl 一.安装01----VisualSVN-Server-3.4.2-x64.msi 文件 直接下一 ...

  4. 跨浏览器事件EventUtil

    <div style="width: 150px; height: 150px; padding: 25px; border:1px solid blue; " id=&qu ...

  5. STL数组处理常用函数

    reverse(a,a+n)反转 sort(a,a+n,cmp)排序 unique(a,a+n,cmp)对于有序集合进行去重,返回新数组最后一个元素的指针 next_permutatoin(a,a+n ...

  6. C# EventHandler and Delegate(委托的使用)

    委托的声明 public delegate void MyDelegate(string str); 注 1.委托的定义和方法的定义类似,只是在前面加了一个delegate,但委托不是方法,它是一种特 ...

  7. Beta--项目冲刺第六天

    胜利在望-- 队伍:F4 成员:031302301 毕容甲 031302302 蔡逸轩 031302430 肖阳 031302418 黄彦宁 会议内容: 1.站立式会议照片: 2.项目燃尽图 3.冲刺 ...

  8. [转]JAVA设计模式之单例模式

    原文地址:http://blog.csdn.net/jason0539/article/details/23297037 概念: java中单例模式是一种常见的设计模式,单例模式的写法有好几种,这里主 ...

  9. ScrollView内部元素如何做到fill_parent 或者 match_parent?

    转  : http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/0704/1629.html ScrollView滚动视图是指当拥有很多 ...

  10. Swift开发小技巧--自定义转场动画

    自定义转场动画 个人理解为重写了被弹出控制器的modal样式,根据自己的样式来显示modal出来的控制器 例:presentViewController(aVC, animated: true, co ...