Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3) D. Santa Claus and a Palindrome STL
2 seconds
256 megabytes
standard input
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.
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.
In the only line print the required maximum possible beauty.
7 3
abb 2
aaa -3
bba -1
zyz -4
abb 5
aaa 7
xyx 4
12
3 1
a 1
a 2
a 3
6
2 5
abcde 10000
abcde 10000
0
In the first example Santa can obtain abbaaaxyxaaabba by concatenating strings 5, 2, 7, 6 and 3 (in this order).
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
const int N=1e5+,M=1e6+,inf=1e9+;
const ll INF=1e18+,mod=;
char a[N];
map<string,int>mp;
vector<int>v[N];
string s[N];
int cmp(int x,int y)
{
return x>y;
}
int main()
{
int n,m;
int t=;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
{
string str;
int x;
cin>>str>>x;
if(mp[str])
v[mp[str]].push_back(x);
else
{
mp[str]=++t;
v[t].push_back(x);
s[t]=str;
}
}
int maxx=,minn=inf;
int ans=;
for(int i=;i<=t;i++)
{
string a=s[i];
reverse(s[i].begin(),s[i].end());
string b=s[i];
int p=mp[b];
if(a==b)
{
sort(v[i].begin(),v[i].end(),cmp);
for(int j=;j<v[i].size();j+=)
{
if(j+<v[i].size()&&v[i][j]+v[i][j+]>)
{
ans+=v[i][j]+v[i][j+];
minn=min(v[i][j+],min(minn,v[i][j]));
}
else
{
for(int k=j;k<v[i].size();k++)
maxx=max(maxx,v[i][k]);
break;
}
}
}
else if(p)
{
sort(v[i].begin(),v[i].end(),cmp);
sort(v[p].begin(),v[p].end(),cmp);
mp[b]=;
mp[a]=;
for(int j=;j<v[i].size()&&j<v[p].size();j++)
{
if(v[i][j]+v[p][j]>)
ans+=v[i][j]+v[p][j];
else break;
}
}
}
printf("%d\n",max(ans+maxx,ans-minn));
return ;
}
Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3) D. Santa Claus and a Palindrome STL的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3) 圣诞之夜!
A. Santa Claus and a Place in a Class 模拟题.(0:12) 题意:n列桌子,每列m张桌子,每张桌子一分为2,具体格式看题面描述.给出n,m及k.求编号为k的桌子在 ...
- Codeforces Round #380 (Div. 1, Rated, Based on Technocup 2017 - Elimination Round 2)
http://codeforces.com/contest/737 A: 题目大意: 有n辆车,每辆车有一个价钱ci和油箱容量vi.在x轴上,起点为0,终点为s,中途有k个加油站,坐标分别是pi,到每 ...
- codeforces Codeforces Round #380 (Div. 1, Rated, Based on Technocup 2017 - Elimination Round 2)// 二分的题目硬生生想出来ON的算法
A. Road to Cinema 很明显满足二分性质的题目. 题意:某人在起点处,到终点的距离为s. 汽车租赁公司提供n中车型,每种车型有属性ci(租车费用),vi(油箱容量). 车子有两种前进方式 ...
- Codeforces Round #380 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 2) E. Subordinates 贪心
E. Subordinates time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- Codeforces Round #380 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 2) D. Sea Battle 模拟
D. Sea Battle time limit per test 1 second memory limit per test 256 megabytes input standard input ...
随机推荐
- lnmp---------------lnmp1.3-full安装包安装lnmp环境,如何安装PHP扩展
1. 如果已经安装LNMP套件,请按以下步骤处理 a. 跳转到fileinfo源代码目录` cd /root/downloads/lnmp1.2-full/src/php-7.0.7/ext/file ...
- java代码打包成jar以及转换为exe
教你如何把java代码打包成jar文件以及转换为exe可执行文件 1.背景: 学习java时,教材中关于如题问题,只有一小节说明,而且要自己写麻烦的配置文件,最终结果却只能转换为jar文件.实在是心有 ...
- 200行代码搞定炸金花游戏(PHP版)
<?php/* * 游戏名称:炸金花(又名三张牌.扎金花) * 开发时间:2009.1.14 * 编 程:多菜鸟 * 来 源:http://blog.csdn.net/kingerq/archi ...
- svg gradient
SVG和canvas中是一样的,都是使用标准的HTML/CSS中的颜色表示方法,这些颜色都可以用于fill和stroke属性. 基本有下面这些定义颜色的方式: 1. 颜色名字: 直接使用颜色名字red ...
- 商品条形码(JBarcode)Java版(二)
下午开了一个下午的会议,其实开会我听不进去,因为今天妖都特别冷,下班在公司等待小媳妇一个钟头,然后带着她去吃饭,吃完饭回到家.她做运动,我就开始慢慢整理我自己的小博客. ——题记 先说一下,写这篇文章 ...
- PHP开发环境配置~Windows 7 IIS
1.配置IIS添加角色服务:CGI.ISAPI扩展.ISAPI筛选器 2.下载PHP安装包 http://windows.php.net/download/ 3.添加模块映射 4.配置php.ini ...
- Windows下USB磁盘开发系列二:枚举系统中所有USB设备
上篇 <Windows下USB磁盘开发系列一:枚举系统中U盘的盘符>介绍了很简单的获取系统U盘盘符的办法,现在介绍下如何枚举系统中所有USB设备(不光是U盘). 主要调用的API如下: 1 ...
- vim基本命令之剪切复制粘贴替换
首先是剪切(删除): 剪切其实也就顺带删除了所选择的内容,所以既可以当剪切命令用,也可以当删除命令使用. 1 首先,可以在命令模式下输入v进入自由选取模式,选择需要剪切的文字后,按下d就可以进行剪切了 ...
- gerrit session expired 怎么解决,搜索未果
gerrit session expired,怎么解决,搜索未果
- 有关docker的学习链接
本文是自己搜索的比较好的网上资源,便于有兴趣者查阅. 英文官网 https://docs.docker.com/engine/getstarted/ 中文入门手册 http://www.docker. ...