ADAFRIEN - Ada and Friends


Ada the Ladybug has many friends. They always celebrate something and Ada has to buy them gifts. It is pretty costly so she have decided to unfriend some of them. What is the maximum of money she can spare?

Input

The first line of each test-case will contain two integers 1 ≤ Q K ≤ 105, the number of celebrations (Q) and the maximum number of friends Ada wants to unfriend (K).

The next Q lines will contain s (the name of friend to whom Ada will buy gift) and 1 ≤ E ≤ 109+1 (the expenses).

Names will contain at most 40 lowercase English letters.

Output

For each test-case, print the number of money Ada could spare.

Example Input

6 1
uvuvwevwevweonyetenyevweugwemubwemossas 10
ryuk 11
uvuvwevwevweonyetenyevweugwemubwemossas 5
fegla 3
tenshikanade 7
fegla 2

Example Output

15

Example Input

4 3
frodo 1
harrypotter 2
frodo 2
harrypotter 2

Example Output

7

Example Input

7 2
waynebot 7
lhic 4
petr 5
umnik 9
izrak 6
tourist 11
zlobobber 9

Example Output

20

Example Input

6 3
dufresne 5
gump 11
dufresne 3
mcmurphy 19
leon 10
dufresne 1

Example Output

40

hide comments
tarun_28: 2020-06-01 15:07:14

And obviously, using "unordered" map will be more efficient here.
It is .2 sec faster than normal map in this case.

Last edit: 2020-06-01 15:08:26
satish18: 2019-11-07 12:06:49

Beware when maximum number of friends Ada wants to unfriend (K) is greater than Ada friends.

aminetrabelsi: 2019-09-11 01:13:54

there can be 10^5 similar names each can be up to 10^9+1. if you use int you will get wrong answer

justasle: 2019-07-15 21:09:49

In the problem statement is it not guaranteed that K will always be <= the amount of unique friends. Be careful with this when solving the problem. Also, make sure to use long long instead of int to avoid overflow since the numbers can be quite large.

harsh_pandey: 2019-06-20 19:52:18

using map and sorting by second of element of map and then deleting k greatest elements from the map. wrong answer on 16 case. please help.

jodobear: 2019-03-02 16:30:23

Yeah, even i got NZEC in Python. @julkas

ajaytec227: 2018-11-17 07:46:42

Use 64 bit integer and not 32 bit type

akjol2049: 2018-11-07 20:00:23

waynebot
lhic
petr
umnik
izrak
tourist
zlobobber
//Some familiar names...:)

silverheart: 2018-10-06 23:21:55

@trishala_naman check your code your are accessing a value out of bound of an array that's why u are getting SIGSEGV

hercules_7: 2018-01-04 19:48:56

@trishala_naman what is the status of Runtime Error?


Added by:Morass
Date:2017-10-08
Time limit:2s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All
Resource:Tunisian Collegiate Training Contest - Round #01