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 Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		FastScanner in = new FastScanner(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskD solver = new TaskD();
		solver.solve(1, in, out);
		out.close();
	}

	static class TaskD {
		public void solve(int testNumber, FastScanner in, PrintWriter out) {
			int n = in.nextInt();
			int[] a = new int[n];
			for (int i = 0; i < n; i++) {
				a[i] = in.nextInt();
			}

			int[] sorted = a.clone();
			Arrays.sort(sorted);
			int l = -1;
			int r = n - 1;
			long total = (long) n * (n + 1) / 2;
			while (r - l > 1) {
				int m = (l + r) / 2;
				if (countSubstringsWithMedianLessThanOrEqualTo(a, sorted[m]) > total / 2) {
					r = m;
				} else {
					l = m;
				}
			}
			out.println(sorted[r]);
		}

		private long countSubstringsWithMedianLessThanOrEqualTo(int[] a, int m) {
			int n = a.length;
//		long res = 0;
//		for (int i = 0; i < n; i++) {
//			List<Integer> cur = new ArrayList<>();
//			for (int j = i; j < n; j++) {
//				cur.add(a[j]);
//				List<Integer> b = new ArrayList<>(cur);
//				Collections.sort(b);
//				if (b.get(b.size() / 2) <= m) {
//					++res;
//				}
//			}
//		}
//		return res;

			int[] s = new int[n + 1];
			for (int i = 0; i < n; i++) {
				s[i + 1] = s[i] + (a[i] <= m ? 0 : 1);
			}

			for (int i = 0; i < n; i++) {
				s[i] = 2 * s[i + 1] - (i + 1);
			}

			return TaskD.InversionsCounter.countInversions(s);
		}

		static class InversionsCounter {
			public static long countInversions(int[] a) {
				int n = a.length;

				int[] b = sortUnique(a.clone());
				for (int i = 0; i < n; i++) {
					a[i] = Arrays.binarySearch(b, a[i]);
				}

				int[] tree = new int[n];
				long ans = 0;
				for (int i = n - 1; i >= 0; i--) {
					ans += sum(tree, a[i]);
					add(tree, a[i], 1);
				}
				return ans;
			}

			private static int[] sortUnique(int[] a) {
				Arrays.sort(a);
				int k = 0;
				for (int i = 0; i < a.length; i++) {
					if (i == 0 || a[i] != a[i - 1]) {
						a[k++] = a[i];
					}
				}
				return Arrays.copyOf(a, k);
			}

			private static void add(int[] tree, int pos, int val) {
				while (pos < tree.length) {
					tree[pos] += val;
					pos |= pos + 1;
				}
			}

			private static long sum(int[] tree, int r) {
				long res = 0;
				while (r >= 0) {
					res += tree[r];
					r = (r & (r + 1)) - 1;
				}
				return res;
			}

		}

	}

	static class FastScanner {
		private BufferedReader in;
		private StringTokenizer st;

		public FastScanner(InputStream stream) {
			in = new BufferedReader(new InputStreamReader(stream));
		}

		public String next() {
			while (st == null || !st.hasMoreTokens()) {
				try {
					String rl = in.readLine();
					if (rl == null) {
						return null;
					}
					st = new StringTokenizer(rl);
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
			return st.nextToken();
		}

		public int nextInt() {
			return Integer.parseInt(next());
		}

	}
}