Under construction

Still being built. Things may change

All Survived?

Time limit8sMemory limit1024 MB

Summary
Given each player's team and hit probability, choose targets to maximize the chance that no ally is popped before the game ends.
Level

Medium7 of 10

Topics
Dynamic programming, Probability, Game theory
Solved
No attempts yet

Problem

You are playing a first-person shooter with your friends. In this game, nn players are divided into two teams that fight each other. Each player wears a balloon on top of the head, and players try to pop the balloons of the players on the opposing team.

Players are numbered from 1 through nn. These numbers are called the action orders. Each player also has a real number called the hit probability. The game consists of nn phases. In the ii-th phase (i=1,…,ni = 1, \dots, n), the player with action order ii (the attacker) takes the following action.

  • If the attacker's balloon has already been popped, the attacker is dead and the phase is skipped without any action.
  • If all opposing team members are dead, the phase is skipped as well.
  • Otherwise, the attacker chooses one living opposing player as the target. The choice may depend on the results of previous actions. The attack succeeds with the attacker's hit probability, regardless of the target. If the attack succeeds, the target's balloon is popped and the target is killed.

The game ends when all phases are completed.

You do not want any player on your team to be killed. Choose targets optimally and calculate the probability that all players on your team stay alive until the game ends. Assume that the opposing players choose their targets at random.

Input

The input consists of multiple datasets. The number of datasets does not exceed 50. Each dataset has the following format.

nn s1s_1 p1p_1 ⋮\vdots sns_n pnp_n

nn is the number of players, an integer from 2 to 300. For each i=1,…,ni = 1, \dots, n, sis_i is either "Ally" or "Enemy", meaning the player with action order ii is on your team or the opposing team. pip_i is the hit probability, a real number from 0 to 1 with 9 digits after the decimal point. Both teams have at least one player.

The input ends with a line containing 0.

Output

For each dataset, print one line containing the probability that all players on your team stay alive when targets are chosen optimally. The absolute or relative error must be at most 10−910^{-9}.

Examples1

  1. Example 1

    Input
    3
    Ally 0.800000000
    Enemy 0.300000000
    Enemy 0.600000000
    2
    Enemy 0.444444444
    Ally 0.111111111
    5
    Ally 0.500000000
    Ally 0.750000000
    Enemy 0.400000000
    Enemy 0.100000000
    Enemy 0.800000000
    6
    Ally 0.250000000
    Ally 0.300000000
    Enemy 0.200000000
    Ally 0.800000000
    Enemy 1.000000000
    Enemy 0.750000000
    10
    Enemy 0.032876598
    Ally 0.273941411
    Ally 0.560821701
    Ally 0.798896750
    Enemy 0.559524781
    Enemy 0.627232231
    Enemy 0.864686751
    Ally 0.991458997
    Enemy 0.699768375
    Enemy 0.675523871
    0
    
    Expected output
    0.616000000000
    0.555555556000
    0.621000000000
    0.419750000000
    0.142319776535