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. android studio 使用问题 解决方法

    1. Error:Execution failed for task ':app:transformClassesWithDexForDebug'. > com.android.build.ap ...

  2. Linux 网络编程详解七(并发僵尸进程处理)

    在上一篇程序框架中,解决了子进程退出,父进程继续存在的功能,但是多条客户端连接如果同一时间并行退出,导致服务器端多个子进程同一时间全部退出,而SIGCHLD是不可靠信号,同时来多条信号可能无法处理,导 ...

  3. Spring Security笔记:Remember Me(下次自动登录)

    前一节学习了如何限制登录尝试次数,今天在这个基础上再增加一点新功能:Remember Me. 很多网站,比如博客园,在登录页面就有这个选项,勾选“下次自动登录”后,在一定时间段内,只要不清空浏览器Co ...

  4. hadoop1.2.1伪分布模式配置

    1.修改core-site.xml,配置hdfs <configuration> <property> <name>fs.default.name</name ...

  5. VMware-Transport(VMDB) error -44:Message.The VMware Authorization Service is not running解决方案

    出现的错误如下: 原因:本机中有一个VMware服务未开启导致的. 解决方案: 1.打开“运行”->输入services.msc !!!文章转自浩瀚先森博客,转载请注明,谢谢.http://ww ...

  6. 基于DDD的.NET开发框架 - ABP工作单元(Unit of Work)

    返回ABP系列 ABP是“ASP.NET Boilerplate Project (ASP.NET样板项目)”的简称. ASP.NET Boilerplate是一个用最佳实践和流行技术开发现代WEB应 ...

  7. JQuery功能查询页

    JQuery在前端开发中已经是常用的不能再常用的库了.最近的项目中使用到了JQuery,我第一次接触它的时候为了学习,把常用的操作指令用比较小的字体写在一页word上,打印出来贴在桌子上,用来让自己时 ...

  8. DbEntry在Vs2012里的配置

    dbentry官方的版本还不支持vs2012,要再vs2012中使用,必须做下调整 1:新建类库项目,然后添加dbentry 的dll引用. 2:在建好的类库项目中.csproj 新添加了类库项目后, ...

  9. Redis的五种数据结构

    Redis支持持久化只是它的一件武器,它提供了多达5种数据存储方式: 一  string(字符串) string是最简单的类型,你可以理解成与Memcached一模一样的类型,一个key对应一个val ...

  10. [BZOJ1299]巧克力棒(博弈论)

    题目:http://hzwer.com/1976.html 分析:先Orz hzwer 对于盒子外面的巧克力棒,就是Nim游戏. 所以就很容易想到先手第一步最好从盒子中取出m根巧克力棒,使得这些巧克力 ...