Problem:
Score : 700 points

Problem Statement

We will define the median of a sequence b of length M, as follows:

Let b' be the sequence obtained by sorting b in non-decreasing order. Then, the value of the (M / 2 + 1)-th element of b' is the median of b. Here, / is integer division, rounding down.

For example, the median of (10, 30, 20) is 20; the median of (10, 30, 20, 40) is 30; the median of (10, 10, 10, 20, 30) is 10.

Snuke comes up with the following problem.

You are given a sequence a of length N.
For each pair (l, r) (1 \leq l \leq r \leq N), let m_{l, r} be the median of the contiguous subsequence (a_l, a_{l + 1}, ..., a_r) of a.
We will list m_{l, r} for all pairs (l, r) to create a new sequence m.
Find the median of m.

Constraints

1 \leq N \leq 10^5

a_i is an integer.

1 \leq a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the median of m.

Sample Input 1

3
10 30 20

Sample Output 1

30

The median of each contiguous subsequence of a is as follows:

The median of (10) is 10.

The median of (30) is 30.

The median of (20) is 20.

The median of (10, 30) is 30.

The median of (30, 20) is 30.

The median of (10, 30, 20) is 20.

Thus, m = (10, 30, 20, 30, 30, 20) and the median of m is 30.

Sample Input 2

1
10

Sample Output 2

10

Sample Input 3

10
5 9 5 9 8 9 3 5 4 3

Sample Output 3

8

Solution in C# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Competitive
{
    internal class Solution
    {
        public int N;
        public long[] A, AS;
        public List<long[]> H;
        public long MM = 1000000;
        public void Run()
        {
            N = Input.ReadInt();
            A = Input.ReadLongArray();
            AS = A.ToArray();
            Array.Sort(AS);

            var bs = new BinarySearch(AS, this.Cond);
            int ind = bs.Search();

            long ret = AS[ind];
            Console.WriteLine(ret);
        }

        // インデックスに対応する値が、答えとなる中央値以上ならtrue
        public bool Cond(int i)
        {
            // 答えとなる値 -> それ未満の値が半数以下
            // 答えとなる値より大きい -> それ未満の値が半数以下より大きい

            // 答えとなる値 -> それ以下の値が半数より大きい
            // 答えとなる値より大きい -> それ以下の値が半数以上より大きい

            // 対象値より小さいを-1, 対象値以上を+1にエンコードし累積和をとる。
            // その差がマイナスの組み合わせが、中央値が対応する値より小さい場合の数
            long x = AS[i];
            int[] E = A.Select(e => e <= x ? -1 : 1).ToArray();
            int[] ES = new int[N+1];
            for (int ii = 0; ii < N; ii++)
            {
                ES[ii + 1] = E[ii] + ES[ii];
            }

            // 「中央値が対応する値より小さい場合の数」が半数以上ならば、対象値は十分大きいのでtrue
            long count = new InversionCount().Calc(ES.ToArray());
          
            long comb = (N) * (N + 1) / 2;
            long threshold = comb / 2;
            // Console.WriteLine($"i:{i} x:{x} {count} {count >= threshold}");
            return count > threshold;
        }
    }

    // libs ----------
    // 転倒数の計算
    class InversionCount
    {
        public int[] A;

        public long Calc(int[] A)
        {
            this.A = A.ToArray(); // 一応コピーすることにする
            return dfs(0, A.Length);
        }

        long dfs(int L, int R)
        {
            // [L, M), [M, R) に分ける
            int n = R - L;
            if (n <= 1) return 0;
            int m = (L + R) / 2;

            long ret = 0;
            ret += dfs(L, m);
            ret += dfs(m, R);

            int r = m;
            for (int l = L; l < m; l++)
            {
                while (r < R && A[l] > A[r])
                {
                    r++;
                }
                ret += (r - m);
            }

            // 対象範囲をソートする
            Array.Sort(A, L, R - L);

            return ret;
        }
    }

    internal class BinarySearch
    {
        public long[] S;
        public int N { get { return S.Length; } }
        public Func<int, bool> Cond;
        public BinarySearch(long[] s, Func<int, bool> cond)
        {
            S = s;
            Cond = cond;
        }

        // 初めてCondがtrueを返すindexを返す
        // 全て条件を満たさない場合はNを返す
        public int Search()
        {
            int left = 0;
            int right = N;

            if (Cond(0)) return 0;
            if (!Cond(N - 1)) return N;

            while (left + 1 < right)
            {
                int mid = (left + right) / 2;
                if (!Cond(mid))
                {
                    left = mid;
                }
                else
                {
                    right = mid;
                }
            }
            return right;
        }
    }

    // common ----------

    internal static class Input
    {
        public static string ReadString()
        {
            string line = Console.ReadLine();
            return line;
        }

        public static int ReadInt()
        {
            string line = Console.ReadLine();
            return int.Parse(line);
        }

        public static int[] ReadIntArray()
        {
            string line = Console.ReadLine();
            return line.Split(' ').Select(int.Parse).ToArray();            
        }

        public static long ReadLong()
        {
            string line = Console.ReadLine();
            return long.Parse(line);
        }

        public static long[] ReadLongArray()
        {
            string line = Console.ReadLine();
            return line.Split(' ').Select(long.Parse).ToArray();
        }

        public static double[] ReadDoubleArray()
        {
            string line = Console.ReadLine();
            return line.Split(' ').Select(double.Parse).ToArray();
        }
    }
    
    internal class Program
    {
        public static void Main(string[] args)
        {
            var s = new Solution();
            s.Run();
        }
    }
}