Codeforces Round #389 Div.2 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).

 

map+堆+贪心

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

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

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

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

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

(但是这还没有结束)

然而WA掉了。

发现一个bug:

例如

aba 10

aba -1

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

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

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<string>
 6 #include<queue>
 7 #include<map>
 8 using namespace std;
 9 const int mxn=100010;
10 map<string,int>mp;
11 priority_queue<int>p[mxn];
12 int cnt=0;
13 bool hw[mxn];
14 string s[mxn],c;
15 int score[mxn];
16 int n,k;
17 int ans=0;
18 bool pd(string ch){
19     int l=n/2;
20     for(int i=0;i<l;i++){
21         if(ch[i]!=ch[n-i-1])return 0;
22     }
23     return 1;
24 }
25 int main(){
26     int i,j,w;
27     cin>>k>>n;
28     for(i=1;i<=k;i++){
29         cin>>s[i]>>w;
30         if(!mp[s[i]])mp[s[i]]=++cnt;
31         p[mp[s[i]]].push(w);
32         if(!hw[mp[s[i]]]){
33             if(pd(s[i]))hw[mp[s[i]]]=1;
34         }
35     }
36     int mx=0;
37     for(i=1;i<=k;i++){
38         if(p[mp[s[i]]].empty())continue;
39         c=s[i];
40         reverse(c.begin(),c.end());
41         int t=mp[c];
42         int x=mp[s[i]];
43         w=p[x].top();
44         p[x].pop();
45         if(t && !p[t].empty() && p[t].top()+w>0){
46             if(p[t].top()*w<0 && hw[x]){
47                 mx=max(mx,max(p[t].top(),w)-p[t].top()-w);
48             }
49             ans+=p[t].top()+w;
50             p[t].pop();
51         }
52         else p[x].push(w);
53     }
54 //    printf("%d %d\n",ans,mx);
55     for(i=1;i<=cnt;i++){
56         if(hw[i] && !p[i].empty()){
57             mx=max(mx,p[i].top());
58         }
59     }
60     printf("%d\n",ans+mx);
61     return 0;
62 }

 

posted @ 2016-12-26 23:11  SilverNebula  阅读(335)  评论(0编辑  收藏  举报
AmazingCounters.com