Minimum subarray average

Find the starting index of a length-two-or-more subarray whose average is smallest, breaking ties by smallest start.

Medium6ArrayMathGreedyNo attempts yetTime limit1sMemory limit512 MB

Problem

You are given an array AA of length NN whose indices start at 0. For two integers PP and QQ with 0P<Q<N0 \le P < Q < N, the subarray average A(P,Q)A(P, Q) is defined as

A(P,Q)=i=PQA[i]QP+1A(P, Q) = \frac{\sum_{i=P}^{Q} A[i]}{Q - P + 1}

so a subarray average is the average of two or more consecutive elements.

For example, if N=3N = 3 with A[0]=3A[0] = 3, A[1]=1A[1] = 1, A[2]=2A[2] = 2, the possible subarray averages are A(0,1)=2A(0, 1) = 2, A(0,2)=2A(0, 2) = 2, and A(1,2)=1.5A(1, 2) = 1.5. The smallest of them is A(1,2)=1.5A(1, 2) = 1.5.

Given the array AA, find a pair (u,v)(u, v) whose subarray average A(u,v)A(u, v) is minimal and print uu. If several pairs reach the minimum, print the smallest uu.

Input

The first line contains the length NN of the array. The second line contains A[0]A[0], A[1]A[1], \dots, A[N1]A[N-1] separated by spaces.

  • 2N1,000,0002 \le N \le 1{,}000{,}000
  • 0A[i]7×1080 \le A[i] \le 7 \times 10^8

Output

Print uu on the first line.

Hint

If P+1<QP + 1 < Q, then there is an integer KK with P<K<QP < K < Q such that

i=PKA[i]KP+1i=PQA[i]QP+1i=K+1QA[i]QK\frac{\sum_{i=P}^{K} A[i]}{K - P + 1} \le \frac{\sum_{i=P}^{Q} A[i]}{Q - P + 1} \le \frac{\sum_{i=K+1}^{Q} A[i]}{Q - K}

or

i=K+1QA[i]QKi=PQA[i]QP+1i=PKA[i]KP+1\frac{\sum_{i=K+1}^{Q} A[i]}{Q - K} \le \frac{\sum_{i=P}^{Q} A[i]}{Q - P + 1} \le \frac{\sum_{i=P}^{K} A[i]}{K - P + 1}