Animal Farm

시간 제한2초메모리 제한1024 MB

요약
돼지 종 하나를 고르고 그 돼지의 영향력보다 낮은 영향력을 가진 다른 종의 동물을 모두 포함해 총 영향력의 최댓값을 구한다.
난이도

보통10점 중 6점

유형
정렬, 그리디, 해시맵
정답자
아직 제출이 없습니다

문제

On Animal Farm, the animals have rebelled against their human owner and have taken over the management of the farm. To ensure equality and fairness among all the animals, they have decided to create a new set of rules. However, as the new leaders, the pigs have started making changes to the rules to favor themselves.

The farm maintains a hierarchy of animals based on their species, with each animal assigned a specifc infuence level. This infuence level, represented as a positive integer, determines the animal’s priority in decision-making. Within a group, an animal can make decisions if it has the highest influence level among the members.

The pigs have a plan to maximize their collective infuence in the leadership council by selecting a specific group of animals. Given a list of animals with their species and influence levels, you are tasked to form the most influential leadership council while adhering to the following rules:

  1. Only one pig species is allowed in the council to avoid power conflicts among the pigs.
  2. Every council member of non-pig species should have an influence level less than the influence level of the only pig’s in the council.

Determine the maximum total influence levels of the council that can be formed under these rules.

입력

The first line contains an integer nn, representing the number of animals. The next nn lines each contain a string species and a positive integer influence:

  • species is a string representing the species of the animal, e.g., “pig”, “horse”, “cow”, etc.
  • influence is an integer representing the influence level of the animal.

출력

Output a single integer, the maximum total influence levels of the leadership council that can be formed following the rules.

제한

  • 1≤n≤1051 ≤ n ≤ 10^5.
  • The length of species is at most 1010.
  • species consists of only English characters in lowercase.
  • At least one animal’s species is pig.
  • influence is at most 10810^8.

예제2

  1. 예제 1

    입력
    5
    pig 10
    horse 15
    pig 5
    cow 20
    sheep 25
    
    예상 출력
    10
    
  2. 예제 2

    입력
    5
    pig 10
    horse 15
    pig 15
    cow 15
    sheep 10
    
    예상 출력
    25