Password Hacking
InterviewTime limit1sMemory limit512 MB
Given each password's probability of being correct, find the expected number of attempts when trying passwords in the order that minimizes it.
- Level
Easy3 of 10
- Topics
- Greedy, Sorting, Probability, Math
- Solved
- No attempts yet
Problem
Passwords are not very secure unless users are disciplined enough to pick ones that are hard to guess. Most users are not that careful and happily use passwords such as "123456". Lists of commonly used passwords circulate, attackers run those lists against systems, and the passwords on them work often enough.
You have done a lot of hacking with such a list, so you have a good idea of how likely each password on it is the correct one. The number of people using "123456" still surprises you. You have a new account to hack, and you decide to try the passwords on the list one at a time until the correct one is found. You are certain that the account uses a password from the given list.
Find the expected number of attempts needed to find the correct password, assuming you try the passwords in the best possible order.
Input
The first line contains a positive integer , the number of passwords on the list. Each of the next lines contains a password, one space, and the probability that this password is the correct one.
Each password is a string of length to made up only of letters and digits. Each probability is a real number given with exactly 4 decimal places. is at most , and the probabilities sum to . No password appears twice on the list.
Output
Print on one line the expected number of attempts needed to find the correct password when the attempts are made in the best possible order.
Every probability has exactly 4 decimal places, so the expected value is exact with 4 decimal places as well. Print it with exactly 4 digits after the decimal point, without rounding to fewer digits. If the answer is , print 1.0000.