Bitcoin Profits
InterviewTime limit2sMemory limit256 MB
Given share difficulties, pick the integer k of at least 1 that maximizes k times the count of shares at or above k, breaking ties toward the smaller k.
- Level
Easy3 of 10
- Topics
- Sorting, Prefix sum
- Solved
- No attempts yet
Problem
Bitcoin is mined by computing a one-way pseudo-random function that outputs a large integer. The network fixes one global difficulty. A block is a claim that new bitcoins were generated, and other participants accept it only when the output of the pseudo-random function meets the difficulty requirement.
Dedicated mining hardware became cheaper, faster and more power efficient, and finding a block with a general-purpose computer soon stopped being feasible. Mining pools appeared instead. Several people donate processing power to a pool, and when the pool finds a block they split the reward in proportion to the work each one contributed.
To measure that proportion, miners work in an arena with a reduced difficulty, for example 1/1048576 of the real difficulty, and submit pool shares. A share that passes the difficulty requirement is assigned a share difficulty taken from the ratio.
To save network bandwidth, a pool member may request a higher base share difficulty, for example 1/524288 of the real difficulty. The reward rises linearly to pay for the extra computation: a share that is times as hard to find is worth times the proportional value. Every share with a difficulty below is discarded. The requested difficulty applies to all shares at once, and a miner cannot pick the best difficulty for each share separately.
You are given the difficulties of the shares a miner earned, as multiples of the base pool difficulty. Find the requested difficulty that would have maximized the reward.
Input
The input holds several test cases. Each test case begins with a line holding the number of accepted shares (). The next line holds integers (), the difficulty of each accepted share as a multiple of the base pool difficulty.
The input ends with a line holding a single zero, and that line is not a test case. The sum of over all test cases is at most .
Output
For each test case print, on its own line, the requested difficulty that gives the largest reward. Here is an integer with , and the reward for equals multiplied by the number of shares whose difficulty is at least .
When several values of reach the largest reward, print the smallest of them. When every share has difficulty 0, the reward is 0 for every , so print 1.