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. NOI2018准备 Day10

    效率低低低低低非常低!!!!!!!!!!!!!!!!!!!!!!! noi3.3 布尔表达式 做了2个晚上做不出来... 不过今晚上A了一道很水的找规律题

  2. redis 学习笔记(5)-Spring与Jedis的集成

    首先不得不服Spring这个宇宙无敌的开源框架,几乎整合了所有流行的其它框架,http://projects.spring.io/spring-data/从这上面看,当下流行的redis.solr.h ...

  3. JAVA CDI 学习(4) - @Alternative/@Default/@Any & Extension

    前面几节学习到的CDI内容,基本上都是hard-code,以硬编码的方式在代码里指定注入类型,这并非依赖注入的本意,依赖注入的优势之一在于“解耦”,这一节我们将学习如何利用配置来动态注入的类型及属性初 ...

  4. Webwork 学习之路【03】核心类 ServletDispatcher 的初始化

    1. Webwork 与 Xwork 搭建环境需要的的jar 为:webwork-core-1.0.jar,xwork-1.0.jar,搭建webwork 需要xwork 的jar呢?原因是这样的,W ...

  5. 读懂IL代码就这么简单(三)完结篇

    一 前言 写了两篇关于IL指令相关的文章,分别把值类型与引用类型在 堆与栈上的操作区别详细的写了一遍 这第三篇也是最后一篇,之所以到第三篇就结束了,是因为以我现在的层次,能理解到的都写完了,而且个人认 ...

  6. 更便捷的Android多渠道打包方式

    本文先回顾了以往流行的多渠道打包方式,随后引入的mcxiaoke的packer-ng-plugin项目,介绍该项目在实际应用(配合友盟统计)中如何解决更方便的Android多渠道打包问题 多渠道打包方 ...

  7. SNMP 原理与实战详解

    原文地址:http://freeloda.blog.51cto.com/2033581/1306743 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法 ...

  8. BroadcastReceive之ip拨号

    首先,新建一个类,继承于BroadcastReceive,然后去配置Manifest.xml <receiver android:name=".PhoneOnReceice" ...

  9. SQL-Server下载地址

    有同学费尽心思的找SQL server数据库各版本的下载地址,看到别人的求助贴就不自觉的想去帮助他们,但是一个一个去帮助又不太现实,毕竟个人精力有限,既然大家有需求,那么艾薇百科今天就本着乐于分享和奉 ...

  10. HashTable、HashMap、HashSet

    1. HashMap 1)  hashmap的数据结构 Hashmap是一个数组和链表的结合体(在数据结构称“链表散列“),如下图示: 当我们往hashmap中put元素的时候,先根据key的hash ...