D. Santa Claus and a Palindrome
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).

题意:

共有k个字符串,每个长度为n,每个字符串有权值,问用这些字符串能组成的权值最大的回文串,输出权值。可以一个都不用最后权值为0,一个相同的字符串可能对应多个不同的权值。

代码:

 //STL好强啊。共有两种情况,本身不是回文串的必须要有他的反串和他一起,本身是回文串的可以将一个放在中间,将一对
//放在两边。由于每个字符串可以由多个权值所以用的时候要尽量用权值大的。把他们放到优先队列中再map,用迭代器遍历,
//操作优先队列。当字符串是回文串并且多于一个时,有可能出现加入的第一个的权值>0,而第二个的权值<0,而此时又没有另
//一个正权值的回文串可以放在中间那么第二个权值<0的就不加入。
#include<bits\stdc++.h>
using namespace std;
char ch[];
struct cmp
{
bool operator()(int &a,int &b){
return a<b;
}
};
map<string,priority_queue<int,vector<int>,cmp> >mp;
int main()
{
int k,n,x;
scanf("%d%d",&k,&n);
for(int i=;i<k;i++){
scanf("%s %d",ch,&x);
string s=ch;
mp[s].push(x);
}
int ans=,maxn=,minn=;
for(map<string,priority_queue<int,vector<int>,cmp> >::iterator it=mp.begin();it!=mp.end();it++){
string s1=it->first;
string s2=s1;
reverse(s2.begin(),s2.end()); //翻转字符串
if(s1!=s2&&mp.find(s2)!=mp.end()){ //非回文串,并且存在相反串
while(!mp[s1].empty()&&!mp[s2].empty()){
int now=mp[s1].top()+mp[s2].top();
if(now>){
ans+=now;
mp[s1].pop();
mp[s2].pop();
}
else break;
}
}
else if(s1==s2){ //是回文串
while(mp[s1].size()>=){
int now1=mp[s1].top();
mp[s1].pop();
int now2=mp[s1].top();
mp[s1].pop();
if(now1+now2>){
ans+=(now1+now2);
minn=min(minn,now2); //记录
}
else{
mp[s1].push(now2);
mp[s1].push(now1);
break;
}
}
}
}
for(map<string,priority_queue<int,vector<int>,cmp> >::iterator it=mp.begin();it!=mp.end();it++){
string s1=it->first;
string s2=s1;
reverse(s2.begin(),s2.end());
if(s1==s2&&!mp[s1].empty()) //找一个放在中间的回文串
maxn=max(maxn,mp[s1].top());
}
printf("%d\n",max(ans-minn,ans+maxn));
return ;
}

CF2.D的更多相关文章

  1. 代码问题:【CF2】

    [CF2/CFCF/HCF]: C Ma, JB Huang, X Yang, et al. Hierarchical convolutional features for visual tracki ...

  2. CF2.E

    E. Comments time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...

  3. CF2.C

    C. Vladik and fractions time limit per test 1 second memory limit per test 256 megabytes input stand ...

  4. CF2.D 并查集+背包

    D. Arpa's weak amphitheater and Mehrdad's valuable Hoses time limit per test 1 second memory limit p ...

  5. CF2.BC

    B. Arpa's obvious problem and Mehrdad's terrible solution time limit per test 1 second memory limit ...

  6. CF2.C(二分贪心)

    C. Road to Cinema time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  7. *CF2.D(哥德巴赫猜想)

    D. Taxes time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...

  8. cf2.25

    T1 题意:判断给出的数中有多少不同的大于的数. content:傻逼题,5min手速 T2 题意:给出p.y,输出y~p+1中最大一个不是2-p的倍数的数. content:答案很简单,但是很难想到 ...

  9. BIRCH聚类算法原理

    在K-Means聚类算法原理中,我们讲到了K-Means和Mini Batch K-Means的聚类原理.这里我们再来看看另外一种常见的聚类算法BIRCH.BIRCH算法比较适合于数据量大,类别数K也 ...

随机推荐

  1. struts2 No result defined for action XXX and result input

    这种错误的原因一般是页面的属性和action里的属性个数.名称.类型不一致造成的 困扰了我一下午的问题,原来是表单中有两个input-text的name属性重复了,然后接受参数的时候就出现了这个错误 ...

  2. HDU 1166 敌兵布阵 线段树单点更新求和

    题目链接 中文题,线段树入门题,单点更新求和,建一棵树就可以了. #include <iostream> #include <cstdio> #include <cmat ...

  3. tp框架获取常量信息、方法、命名空间

    获取系统常量信息: public function ShowInFo() { var_dump(get_defined_constants(true)); //如果参数为true,则分类显示 } 在这 ...

  4. Python列表去除重复元素

    主要尝试了3种列表去除重复元素 #2.去除列表中的重复元素 #set方法 def removeDuplicates_set(nums): l2 = list(set(l1)) #用l1的顺序排序l2 ...

  5. Linux中文件颜色所代表的属性和颜色

    绿色文件: 可执行文件,可执行的程序    红色文件:压缩文件或者包文件   蓝色文件:目录   白色文件:一般性文件,如文本文件,配置文件,源码文件等    浅蓝色文件:链接文件,主要是使用ln命令 ...

  6. PROC 文件系统调节参数介绍(netstat -us)

    转自:http://www.cnblogs.com/super-king/p/3296333.html /proc/net/* snmp文件 Ip: ip项 Forwarding        : 是 ...

  7. Spring-boot 开发Web应用

    动态修改Freemarker模版: 设置模版属性:   spring.freemarker.cache=false 启动应用方式有两种: a. 运行main()函数启动应用:则修改完模版文件后,需要把 ...

  8. [译]:Xamarin.Android平台功能——位置服务

    返回索引目录 原文链接:Location Services. 译文链接:Xamarin.Android平台功能--位置服务 本部分介绍位置服务以及与如何使用位置提供商服务 Location Servi ...

  9. 责任链模式/chain of responsibility/行为型模式

    职责链模式 chain of responsibility 意图 使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系.将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处 ...

  10. GAMBIT、ICEM、HYPERMESH耦合面的处理方法

    前两天在论坛里碰到有朋友问关于使用fluent仿真流固耦合,使用hypermesh作为前处理时的耦合面的方法,刚好今天有点时间,借此机会总结一下GAMBIT.ICEM和HYPERMESH这三款软件作为 ...