You are given a sequence of N$N$ powers of an integer k$k$; let's denote the i$i$-th of these powers by kAi$k^{A_i}$. You should partition this sequence into two non-empty contiguous subsequences; each element of the original sequence should appear in exactly one of these subsequences. In addition, the product of (the sum of elements of the left subsequence) and (the sum of elements of the right subsequence) should be maximum possible.
Find the smallest position at which you should split this sequence in such a way that this product is maximized.

-----Input-----
- The first line of the input contains a single integer T$T$ denoting the number of test cases. The description of T$T$ test cases follows.
- The first line of each test case contains two space-separated integers N$N$ and k$k$.
- The second line contains N$N$ space-separated integers A1,A2,…,AN$A_1, A_2, \dots, A_N$.

-----Output-----
For each test case, print a single line containing one integer — the size of the left subsequence. If there is more than one possible answer, print the smallest possible one.

-----Constraints-----
- 1≤T≤10$1 \le T \le 10$
- 2≤N≤105$2 \le N \le 10^5$
- 2≤k≤109$2 \le k \le 10^9$
- 0≤Ai≤105$0 \le A_i \le 10^5$ for each valid i$i$

-----Subtasks-----
Subtask #1 (30 points):
- 2≤N≤1,000$2 \le N \le 1,000$
- 0≤Ai≤1,000$0 \le A_i \le 1,000$ for each valid i$i$
Subtask #2 (70 points): original constraints

-----Example Input-----
1
5 2
1 1 3 3 5

-----Example Output-----
4

-----Explanation-----
Example case 1: The actual sequence of powers is [21,21,23,23,25]=[2,2,8,8,32]$[2^1, 2^1, 2^3, 2^3, 2^5] = [2, 2, 8, 8, 32]$. The maximum product is 20⋅32=640$20 \cdot 32 = 640$. In the optimal solution, the sequence is partitioned into [2,2,8,8]$[2, 2, 8, 8]$ and [32]$[32]$.

Difficulty: VERY_HARD
Tags: Sorting
Skill types: Sorting

Starter code:

Solutions:
def main():
	for _ in range(int(input())):
		(N, k) = [int(x) for x in input().split()]
		Powers = [k ** int(x) for x in input().split()]
		(s1, s2) = (0, sum(Powers))
		ans = (0, None)
		i = 0
		while i < N - 1:
			s1 += Powers[i]
			s2 -= Powers[i]
			z = s1 * s2
			if z > ans[0]:
				ans = (z, i)
			i += 1
		print(ans[1] + 1)
main()


for a in range(int(input())):
	(N, k) = map(int, input().split())
	c = list(map(lambda b: k ** b, list(map(int, input().split()))))
	total_sum = sum(c)
	i = 0
	j = sum(c[0:int(N / 2)])
	if j < total_sum - j:
		g = int(N / 2)
		i = j
	else:
		g = 0
	for e in range(g, N):
		i = i + c[e]
		if i > total_sum - i:
			f = e - 1
			break
	if sum(c[0:f + 2]) == sum(c[f + 2:]):
		print(f + 2)
	elif sum(c[0:f + 2]) >= sum(c[f + 1:]):
		print(f + 1)
	else:
		print(f + 2)


t = int(input())
for _ in range(t):
	(n, k) = [int(d) for d in input().split()]
	l = [int(d) for d in input().split()]
	a1 = min(l)
	l1 = []
	for i in range(n):
		l1.append(k ** (l[i] - a1))
	left_sum = 0
	right_sum = 0
	left = 0
	right = n - 1
	if k == 1:
		print(n // 2)
	elif n <= 3:
		if n == 2:
			print(1)
		elif l1[left] > l1[right]:
			print(1)
		elif l1[right] > l1[left]:
			print(2)
		else:
			print(1)


for _ in range(int(input())):
	(n, k) = [int(i) for i in input().strip().split(' ')]
	a = [int(i) for i in input().strip().split(' ')]
	sm = max(a) + 50
	ts = [0] * (sm + 1)
	ts[sm] = 1
	for i in range(n):
		ts[a[i]] += 1
		ii = a[i]
		while ts[ii] == k:
			ts[ii] = 0
			ii += 1
			ts[ii] += 1
	(tts, poss) = (ts[:], [])
	for i in range(n):
		tts[a[i]] -= 2
		ii = a[i]
		while tts[ii] < 0:
			tts[ii] += k
			ii += 1
			tts[ii] -= 1
			if ii == sm:
				poss += [i]
				if i:
					poss += [i - 1]
					ptts = tts[:]
					ptts[a[i]] += 1
					jj = a[i]
					while ptts[jj] >= k:
						ptts[jj] -= k
						jj += 1
						ptts[jj] += 1
					if ptts[sm] and max(ptts[:sm]):
						print(i + 1)
					else:
						print(i)
				else:
					print(i + 1)
		if len(poss):
			break


for i in range(int(input())):
	(n, k) = [int(i) for i in input().split()]
	arr = [k ** int(i) for i in input().split()]
	v1 = sum(arr)
	v1 = v1 // 2
	v2 = 0
	for i in range(len(arr)):
		v2 += arr[i]
		if v2 >= v1:
			break
	if i == 0:
		print(1)
	elif abs(v2 - v1) < abs(v2 - arr[i] - v1):
		print(i + 1)
	else:
		print(i)


q = int(input())
for i in range(q):
	(n, m) = list(map(int, input().split()))
	l = list(map(int, input().split()))
	po = [m ** i for i in l]
	r = sum(po)
	l = 0
	count = 0
	max = 1
	for i in range(n):
		l += po[i]
		r -= po[i]
		p = l * r
		if p > max:
			max = p
			count += 1
	print(count)


for _ in range(int(input())):
	(n, k) = [int(kk) for kk in input().strip().split(' ')]
	a = [int(kk) for kk in input().strip().split(' ')]
	sm = max(a) + 50
	ts = [0] * (sm + 1)
	ts[sm] = 1
	for i in range(n):
		ts[a[i]] += 1
		ii = a[i]
		while ts[ii] == k:
			ts[ii] = 0
			ii += 1
			ts[ii] += 1
	(tts, poss) = (ts[:], [])
	for i in range(n):
		tts[a[i]] -= 2
		ii = a[i]
		while tts[ii] < 0:
			tts[ii] += k
			ii += 1
			tts[ii] -= 1
			if ii == sm:
				poss += [i]
				if i:
					poss += [i - 1]
					ptts = tts[:]