Playlist (Codeforces 268E)
Tags: Math, probabilities

Manao's friends often send him new songs. He never listens to them right away. Instead, he compiles them into a playlist. When he feels that his mind is open to new music, he opens the playlist and starts to listen to the songs.Of course, there are some songs that Manao doesn't particuarly enjoy. To get more pleasure from the received songs, he invented the following procedure of listening to the playlist:  If after listening to some song Manao realizes that he liked it, then he remembers it and starts to listen to the next unlistened song.  If after listening to some song Manao realizes that he did not like it, he listens to all the songs he liked up to this point and then begins to listen to the next unlistened song. For example, if Manao has four songs in the playlist, A, B, C, D (in the corresponding order) and he is going to like songs A and C in the end, then the order of listening is the following:  Manao listens to A, he likes it, he remembers it.  Manao listens to B, he does not like it, so he listens to A, again.  Manao listens to C, he likes the song and he remembers it, too.  Manao listens to D, but does not enjoy it and re-listens to songs A and C. That is, in the end Manao listens to song A three times, to song C twice and songs B and D once. Note that if Manao once liked a song, he will never dislike it on a subsequent listening.Manao has received n songs: the i-th of them is li seconds long and Manao may like it with a probability of pi percents. The songs could get on Manao's playlist in any order, so Manao wants to know the maximum expected value of the number of seconds after which the listening process will be over, for all possible permutations of the songs in the playlist.

Input: The first line contains a single integer n (1 ≤ n ≤ 50000). The i-th of the following n lines contains two integers, separated by a single space — li and pi (15 ≤ li ≤ 1000, 0 ≤ pi ≤ 100) — the length of the i-th song in seconds and the probability that Manao will like the song, in percents.

Output: In a single line print a single real number — the maximum expected listening time over all permutations of songs. The answer will be considered valid if the absolute or relative error does not exceed 10 - 9.

Note: Consider the first test case. If Manao listens to the songs in the order in which they were originally compiled, the mathematical expectation will be equal to 467.5 seconds. The maximum expected value is obtained by putting the first song at the end of the playlist.Consider the second test case. The song which is 360 seconds long should be listened to first. The song 300 seconds long which Manao will dislike for sure should be put in the end.

Sample Inputs:
3
150 20
150 50
100 50

4
300 0
300 50
240 50
360 80

Sample Outputs:
537.500000000

2121.000000000

Solution (Python):
""""""
num_songs = int(input())
songs_meta = []
epsilon = 1e-07
for i in range(num_songs):
    (a, b) = map(int, input().split(' '))
    songs_meta.append([a, b / 100])
songs_meta = sorted(songs_meta, key=lambda x: x[0] * x[1] / (1 - x[1] + epsilon), reverse=True)
expected_value = 0
loved_songs = 0
for i in range(num_songs):
    expected_value += songs_meta[i][0]
    expected_value += (1 - songs_meta[i][1]) * loved_songs
    loved_songs += songs_meta[i][0] * songs_meta[i][1]
print(expected_value)

Solution (Python):
n = int(input())
arr = []
ans = 0
for i in range(n):
    (l, p) = map(int, input().split())
    arr.append([l, p / 100])
    ans += arr[i][0]
arr.sort(key=lambda x: x[0] * x[1] / (1 - x[1] + 1e-10), reverse=True)
tmp = 0
for i in range(n):
    ans += tmp * (1 - arr[i][1])
    tmp += arr[i][0] * arr[i][1]
print(ans)

Solution (Python):
import sys
from math import gcd
from collections import defaultdict, deque
import random
from datetime import datetime
random.seed(datetime.now().timestamp())
input = sys.stdin.readline
get_int = lambda : int(input().rstrip())
get_arr = lambda : [int(w) for w in input().split()]
get_str = lambda : input().rstrip()
n = get_int()
arr = []
for _ in range(n):
    (l, p) = get_arr()
    arr.append((l, p))

def kk(w):
    (l, p) = w
    if p == 100:
        return 10 ** 9
    return l * p / (100 - p)
arr.sort(key=kk, reverse=True)
s = 0
s2 = 0
exp = 0
for i in range(n):
    (l, p) = arr[i]
    exp += (1 - p / 100) * s
    s += l * p / 100
    s2 += l
print(exp + s2)