You are given a multiset S consisting of positive integers (initially empty). There are two kind of queries:   Add a positive integer to S, the newly added integer is not less than any number in it.  Find a subset s of the set S such that the value $\operatorname{max}(s) - \operatorname{mean}(s)$ is maximum possible. Here max(s) means maximum value of elements in s, $\operatorname{mean}(s)$ — the average value of numbers in s. Output this maximum possible value of $\operatorname{max}(s) - \operatorname{mean}(s)$. 


-----Input-----

The first line contains a single integer Q (1 ≤ Q ≤ 5·10^5) — the number of queries.

Each of the next Q lines contains a description of query. For queries of type 1 two integers 1 and x are given, where x (1 ≤ x ≤ 10^9) is a number that you should add to S. It's guaranteed that x is not less than any number in S. For queries of type 2, a single integer 2 is given.

It's guaranteed that the first query has type 1, i. e. S is not empty when a query of type 2 comes.


-----Output-----

Output the answer for each query of the second type in the order these queries are given in input. Each number should be printed in separate line.

Your answer is considered correct, if each of your answers has absolute or relative error not greater than 10^{ - 6}.

Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if $\frac{|a - b|}{\operatorname{max}(1,|b|)} \leq 10^{-6}$.


-----Examples-----
Input
6
1 3
2
1 4
2
1 8
2

Output
0.0000000000
0.5000000000
3.0000000000

Input
4
1 1
1 4
1 5
2

Output
2.0000000000

Source: codeforces | Difficulty: MEDIUM_HARD
Tags: Sorting

Amortized analysis

Greedy algorithms


Solutions:
import sys
import math
import collections
import heapq
q = int(input())
s = input().split()
a = [int(s[1])]
sum1 = a[0]
pos = -1
mean = sum1
fin = ''
for i in range(q - 1):
	n = len(a)
	s = input().split()
	if s[0] == '1':
		a.append(int(s[1]))
		sum1 += a[-1] - a[-2]
		mean = sum1 / (pos + 2)
		n = len(a)
		while pos < n - 2 and a[pos + 1] < mean:
			pos += 1
			sum1 += a[pos]
			mean = sum1 / (pos + 2)
	else:
		fin += str(a[-1] - mean) + '\n'
print(fin)


import sys
import math
from collections import defaultdict
arr = []
q = int(sys.stdin.readline())
(a, b) = list(map(int, sys.stdin.readline().split()))
(l, r) = (0, 0)
mean = b
num = 1
arr.append(b)
for _ in range(1, q):
	lis = list(map(int, sys.stdin.readline().split()))
	if lis[0] == 1:
		b = lis[1]
		arr.append(b)
		if len(arr) == 2:
			newmean = (arr[0] + arr[1]) / 2
			r += 1
			mean = newmean
			num = 2
			continue
		newmean = (mean * num - arr[r] + arr[r + 1]) / num
		r += 1
		mean = newmean
		while l + 1 < r and (mean * num + arr[l + 1]) / (num + 1) <= mean:
			mean = (mean * num + arr[l + 1]) / (num + 1)
			l += 1
			num += 1
	else:
		print(arr[r] - mean)


import sys
(s, left_index, sub_sum, subset) = ([], -1, 0, 1)
input()
for line in sys.stdin:
	inp = list(map(int, line.split()))
	if inp[0] == 2:
		sub_sum += s[-1]
		for j in range(left_index + 1, len(s)):
			if s[j] <= sub_sum / subset:
				sub_sum += s[j]
				subset += 1
				left_index += 1
			else:
				break
		print(s[-1] - sub_sum / subset)
		sub_sum -= s[-1]
	else:
		s.append(inp[1])


import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
q = int(input())
s = []
l = 0
sl = 0
for _ in range(q):
	a = [int(i) for i in input().split()]
	if len(a) == 2:
		s.append(a[1])
	res = (sl + s[-1]) / (l + 1)
	while l < len(s) and (sl + s[l] + s[-1]) / (l + 2) < res:
		sl += s[l]
		l += 1
		res = (sl + s[-1]) / (l + 1)
	if len(a) == 1:
		print('{0:.8f}'.format(s[-1] - res))


q = int(input())
s = input().split()
a = [int(s[1])]
sum1 = a[0]
pos = -1
mean = sum1
fin = ''
for i in range(q - 1):
	n = len(a)
	s = input().split()
	if s[0] == '1':
		a.append(int(s[1]))
		sum1 += a[-1] - a[-2]
		mean = sum1 / (pos + 2)
		n = len(a)
		while pos < n - 2 and a[pos + 1] < mean:
			pos += 1
			sum1 += a[pos]
			mean = sum1 / (pos + 2)
	else:
		fin += str(a[-1] - mean) + '\n'
print(fin)


import sys
input = sys.stdin.readline
Q = int(input())
a = []
sm = []

def best():
	mx = a[-1]
	n = len(a)
	(l, r) = (0, n - 2)
	ret = mx
	while l <= r:
		mid = (l + r) // 2
		s = sm[mid] + mx
		c = mid + 2
		avg = s / c
		if a[mid] > avg:
			r = mid - 1
		else:
			ret = avg
			l = mid + 1
	return ret
for _ in range(Q):
	inp = [int(i) for i in input().split()]
	if len(inp) == 2:
		x = inp[1]
		a.append(x)
		sm.append((sm[-1] if sm else 0) + x)
	else:
		mx = a[-1]
		avg = best()
		ans = mx - avg
		print(ans)


from sys import stdin

def mean(p):
	return (c[p] + a[-1]) / (p + 2)

def check(p):
	return mean(p) >= a[p]

def binary(a, b):
	(low, high) = (a - 1, b + 1)
	while high - low > 1:
		mid = (low + high) // 2
		if check(mid):
			low = mid
		else:
			high = mid
	return low
all = stdin.readlines()
q = int(all[0])
za = list(map(lambda x: tuple(map(int, x.split())), all[1:]))
(a, c) = ([za[0][1]], [za[0][1]])
ans = []
for i in za[1:]:
	if i[0] == 1:
		a.append(i[1])
		c.append(i[1] + c[-1])
	else:
		k = binary(0, len(a) - 1)
		ans.append(a[-1] - mean(k))
print('\n'.join(map(str, ans)))


import sys

def input():
	return sys.stdin.buffer.readline()
Q = int(input())
stack = []
pos = 0
query = [tuple(map(int, input().split())) for i in range(Q)]
S = 0
for que in query:
	command = que[0]
	if command == 1:
		stack.append(que[1])
		continue
	last_number = stack[-1]
	while pos < len(stack) and (pos + 2) * (last_number + S) > (pos + 1) * (last_number + S + stack[pos]):
		S += stack[pos]
		pos += 1
	print(last_number - (last_number + S) / (pos + 1))
	continue


from sys import stdin

def mean(p):
	return (c[p] + a[-1]) / (p + 2)

def check(p):
	return mean(p) >= a[p]

def binSearch(a, b):
	(left, right) = (a - 1, b + 1)
	while right - left > 1:
		mid = (left + right) // 2
		if check(mid):
			left = mid
		else:
			right = mid
	return left
all_in = stdin.readlines()
Q = int(all_in[0])
zapr = list(map(lambda x: tuple(map(int, x.split())), all_in[1:]))
(a, c) = ([zapr[0][1]], [zapr[0][1]])
ans = list()
for el in zapr[1:]:
	if el[0] == 1:
		a.append(el[1])
		c.append(c[-1] + el[1])
	elif el[0] == 2:
		b = binSearch(0, len(a) - 1)
		ans.append(a[-1] - mean(b))
	else:
		exit(100500)
print('\n'.join(map(str, ans)))


import math
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
mod = 10 ** 9 + 7
mod1 = 998244353
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class SegmentTree1:

	def __init__(self, data, default='z', func=lambda a, b: min(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class SegmentTree:

	def __init__(self, data, default=0, func=lambda a, b: a * a + b * b):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD

def powm(a, n, m):
	if a == 1 or n == 0:
		return 1
	if n % 2 == 0:
		s = powm(a, n // 2, m)
		return s * s % m
	else:
		return a * powm(a, n - 1, m) % m

def sort_list(list1, list2):
	zipped_pairs = zip(list2, list1)
	z = [x for (_, x) in sorted(zipped_pairs)]
	return z

def product(l):
	por = 1
	for i in range(len(l)):
		por *= l[i]
	return por

def binarySearchCount(arr, n, key, a1):
	left = 0
	right = n - 1
	count = 0
	while left <= right:
		mid = (right + left) // 2
		if Fraction(a1[mid] + l[-1], mid + 1) >= arr[mid]:
			count = mid
			left = mid + 1
		else:
			right = mid - 1
	return count

def countdig(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def countGreater(arr, n, k):
	l = 0
	r = n - 1
	leftGreater = n
	while l <= r:
		m = int(l + (r - l) / 2)
		if arr[m] > k:
			leftGreater = m
			r = m - 1
		else:
			l = m + 1
	return n - leftGreater
n = int(input())
l = []
a1 = []
a1.append(0)
for i in range(n):
	a = input()
	if len(a) != 1:
		l.append(int(a[2:]))
		a1.append(l[-1] + a1[-1])
	else:
		if len(l) == 1:
			print(0)
			continue
		elif len(l) == 2:
			print((l[-1] - l[0]) / 2)
			continue
		avg = Fraction(l[-1] + l[0], 2)
		t = binarySearchCount(l, len(l), avg, a1) + 1
		avg = Fraction(l[-1] + a1[t], t + 1)
		print(float(l[-1] - avg))


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
import collections
from itertools import permutations
from collections import defaultdict
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
n = int(input())
a = list()
c = 0
d = 0
j = 0
s = 0
for i in range(n):
	b = list(map(int, input().split()))
	if b[0] == 1:
		a.append(b[1])
		c += 1
		if c == 1:
			continue
		while a[j] <= (s + a[-1]) / (d + 1) and j < c - 1:
			s += a[j]
			j += 1
			d += 1
	else:
		m = a[-1]
		if c == 1:
			print(0)
		else:
			if d == 0:
				avg = (a[0] + a[-1]) / 2
			else:
				avg = (s + a[-1]) / (d + 1)
			print(m - avg)


import os
import sys
from io import BytesIO, IOBase

def main():
	a = []
	(j, su) = (0, 0)
	for _ in range(int(input())):
		b = list(map(int, input().split()))
		if b[0] == 1:
			a.append(b[1])
			while j < len(a) - 1 and (a[-1] + su) * (j + 2) > (a[-1] + su + a[j]) * (j + 1):
				su += a[j]
				j += 1
		else:
			print(a[-1] - (a[-1] + su) / (j + 1))
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()