天天看點

HDU 6015 Skip the Class

Problem Description

Finally term begins. luras loves school so much as she could skip the class happily again.(wtf?)

Luras will take n lessons in sequence(in another word, to have a chance to skip xDDDD).

For every lesson, it has its own type and value to skip.

But the only thing to note here is that luras can't skip the same type lesson more than twice.

Which means if she have escaped the class type twice, she has to take all other lessons of this type.

Now please answer the highest value luras can earn if she choose in the best way.

Input

The first line is an integer T which indicates the case number.

And as for each case, the first line is an integer n which indicates the number of lessons luras will take in sequence.

Then there are n lines, for each line, there is a string consists of letters from 'a' to 'z' which is within the length of 10,

and there is also an integer which is the value of this lesson.

The string indicates the lesson type and the same string stands for the same lesson type.

It is guaranteed that——

T is about 1000

For 100% cases, 1 <= n <= 100,1 <= |s| <= 10, 1 <= v <= 1000

Output

As for each case, you need to output a single line.

there should be 1 integer in the line which represents the highest value luras can earn if she choose in the best way.

Sample Input

2
5
english 1
english 2
english 3
math 10
cook 100
2
a 1
a 2      

Sample Output

3

#include<map>  
#include<ctime>  
#include<cmath>      
#include<queue>   
#include<string>  
#include<vector>  
#include<cstdio>      
#include<cstring>    
#include<iostream>  
#include<algorithm>      
#include<functional>  
using namespace std;
#define ms(x,y) memset(x,y,sizeof(x))      
#define rep(i,j,k) for(int i=j;i<=k;i++)      
#define per(i,j,k) for(int i=j;i>=k;i--)      
#define loop(i,j,k) for (int i=j;i!=-1;i=k[i])      
#define inone(x) scanf("%d",&x)      
#define intwo(x,y) scanf("%d%d",&x,&y)      
#define inthr(x,y,z) scanf("%d%d%d",&x,&y,&z)    
#define infou(x,y,z,p) scanf("%d%d%d%d",&x,&y,&z,&p)   
#define lson x<<1,l,mid  
#define rson x<<1|1,mid+1,r  
#define mp(i,j) make_pair(i,j)  
#define ft first  
#define sd second  
typedef long long LL;
typedef pair<int, int> pii;
const int low(int x) { return x&-x; }
const int INF = 0x7FFFFFFF;
const int mod = 1e9 + 7;
const int N = 1e5 + 10;
const int M = 5e6;
const double eps = 1e-10;
int T, n, m, v[N], a[N];
char s[N][15];

bool cmp(int x, int y)
{
    return !strcmp(s[x], s[y]) ? v[x] > v[y]:strcmp(s[x], s[y]) < 0;
}

int main()
{
    for (inone(T); T--;)
    {
        inone(n);
        rep(i, 1, n)
        {
            scanf("%s%d", s[i], &v[i]);
            a[i] = i;
        }
        sort(a + 1, a + n + 1, cmp);
        int ans = 0;
        for (int i = 1, j; i <= n; i = j)
        {
            for (j = i; !strcmp(s[a[i]], s[a[j]]) && j <= n; j++)
            {
                if (j - i < 2) ans += v[a[j]];
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}