• Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017


    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).

    #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+10,M=1e6+10,inf=1e9+10;
    const ll INF=1e18+10,mod=2147493647;
    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=0;
        scanf("%d%d",&n,&m);
        for(int i=1;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=0,minn=inf;
        int ans=0;
        for(int i=1;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=0;j<v[i].size();j+=2)
                {
                    if(j+1<v[i].size()&&v[i][j]+v[i][j+1]>0)
                    {
                        ans+=v[i][j]+v[i][j+1];
                        minn=min(v[i][j+1],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]=0;
                mp[a]=0;
                for(int j=0;j<v[i].size()&&j<v[p].size();j++)
                {
                    if(v[i][j]+v[p][j]>0)
                        ans+=v[i][j]+v[p][j];
                    else break;
                }
            }
        }
        printf("%d
    ",max(ans+maxx,ans-minn));
        return 0;
    }
  • 相关阅读:
    samtools使用过程中出现的问题
    转移灶,原发灶,cfDNA的外显子测序得到的突变点的关系
    韦恩图的画法
    python的计算保留小数
    awk的输出格式控制:print 和printf
    awk遇到windows 的^M
    从引物序列出发查找pcr产物的内容和在基因组上的位置
    八.Windows内核保护机制--页保护3--PDE PTE属性
    九.Windows内核保护机制--TSS
    七.Windows内核保护机制--陷阱门
  • 原文地址:https://www.cnblogs.com/jhz033/p/6227296.html
Copyright © 2020-2023  润新知