Under construction

Still being built. Things may change

Bitcoin Profits

Interview

Time limit2sMemory limit256 MB

Summary
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 kk times as hard to find is worth kk times the proportional value. Every share with a difficulty below kk 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 NN (0<N<1060 < N < 10^6). The next line holds NN integers d1,…,dNd_1, \dots, d_N (0≤di≤1060 \le d_i \le 10^6), 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 NN over all test cases is at most 10610^6.

Output

For each test case print, on its own line, the requested difficulty kk that gives the largest reward. Here kk is an integer with k≥1k \ge 1, and the reward for kk equals kk multiplied by the number of shares whose difficulty is at least kk.

When several values of kk reach the largest reward, print the smallest of them. When every share has difficulty 0, the reward is 0 for every kk, so print 1.

Examples2

  1. Example 1

    Input
    5
    1 1 1 1 1
    2
    1 2
    3
    1 2 3
    5
    25 1 1 1 1
    0
    
    Expected output
    1
    1
    2
    25
    
  2. Example 2

    Input
    2
    2 4
    10
    1 2 3 4 5 6 7 8 9 10
    0
    
    Expected output
    2
    5