You are given two arrays A and B of length N. Let S be the set of integers from 1 to N. Can you find the maximum possible value of (A_{i1}+A_{i2}+...+A_{ik})^{2}+(B_{i1}+B_{i2}+...+B_{ik})^{2} where {i1,i2...ik} is a non-empty subset of S?

Input Format 

The first line contains a single integer T, denoting the number of test cases. 

T testcases follow, each test case given in following format.  

N  
A1 A2 ... AN  
B1 B2 ... BN  

Output Format 

For each test case, output the maximum possible value in one line.  

Constraints 

1 <= T <= 10 

1 <= N <= 1000 

-10^{6} <= A_{i}, B_{i} <= 10^{6}  

Sample Input  

1  
2  
-1 5  
4 -5  

Sample Output  

50

Explanation 

All possible non-empty subsets for N = 2 of S = {1,2} are {1}, {2} and {1,2}. The maximum possible values of the above equation now are 

(-1)^{2} + (4)^{2} = 17  
(5)^{2} + (-5)^{2} = 50  
(-1 + 5)^{2} + (4 - 5)^{2} = 17

hence 50. 

Timelimits

Timelimits for this challenge can be seen here

Source: hackerrank | Difficulty: HARD
Tags: Geometry


Solutions:
from math import *
from operator import itemgetter
nTests = int(input())
for t in range(nTests):
	n = int(input())
	a = [int(s) for s in input().split()]
	b = [int(s) for s in input().split()]
	vs = zip(a, b)
	vs = sorted(vs, key=lambda v: atan2(v[1], v[0]))
	best = 0
	for i in range(len(vs)):
		u = vs[i]
		l = u[0] * u[0] + u[1] * u[1]
		for v in (vs + vs)[i + 1:i + len(vs)]:
			tmpU = (u[0] + v[0], u[1] + v[1])
			tmpL = tmpU[0] * tmpU[0] + tmpU[1] * tmpU[1]
			if tmpL >= l:
				u = tmpU
				l = tmpL
		if l > best:
			best = l
		u = vs[i]
		l = u[0] * u[0] + u[1] * u[1]
		for v in reversed((vs + vs)[i + 1:i + len(vs)]):
			tmpU = (u[0] + v[0], u[1] + v[1])
			tmpL = tmpU[0] * tmpU[0] + tmpU[1] * tmpU[1]
			if tmpL >= l:
				u = tmpU
				l = tmpL
		if l > best:
			best = l
	print(best)


from math import *
from operator import itemgetter
nTests = int(input())
for t in range(nTests):
	n = int(input())
	a = [int(s) for s in input().split()]
	b = [int(s) for s in input().split()]
	vs = zip(a, b)
	vs = sorted(vs, key=lambda v: atan2(v[1], v[0]))
	best = 0
	for i in range(len(vs)):
		u = vs[i]
		l = u[0] * u[0] + u[1] * u[1]
		for v in (vs + vs)[i + 1:i + len(vs)]:
			tmpU = (u[0] + v[0], u[1] + v[1])
			tmpL = tmpU[0] * tmpU[0] + tmpU[1] * tmpU[1]
			if tmpL >= l:
				u = tmpU
				l = tmpL
		if l > best:
			best = l
		u = vs[i]
		l = u[0] * u[0] + u[1] * u[1]
		for v in reversed((vs + vs)[i + 1:i + len(vs)]):
			tmpU = (u[0] + v[0], u[1] + v[1])
			tmpL = tmpU[0] * tmpU[0] + tmpU[1] * tmpU[1]
			if tmpL >= l:
				u = tmpU
				l = tmpL
		if l > best:
			best = l
	print(best)


from math import atan2

def max_vec_sum(X, Y):
	P = [(x, y) for (x, y) in zip(X, Y) if not x == y == 0]
	N = len(P)
	P.sort(key=lambda p: atan2(p[1], p[0]))
	res = 0
	for i in range(N):
		(sx, sy) = P[i]
		j = (i + 1) % N
		res = max(res, sx * sx + sy * sy)
		while j != i:
			sx += P[j][0]
			sy += P[j][1]
			res = max(res, sx * sx + sy * sy)
			j = (j + 1) % N
	return res
T = int(input())
for _ in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	print(max_vec_sum(A, B))


from math import pi, atan2
from itertools import chain, accumulate
for T in range(int(input())):
	n = int(input())
	a = map(int, input().split())
	b = map(int, input().split())
	phixy = sorted([(atan2(y, x), x, y) for (x, y) in zip(a, b) if x * x + y * y > 0])
	n = len(phixy)
	xacc = list(accumulate((x for (p, x, y) in phixy)))
	yacc = list(accumulate((y for (p, x, y) in phixy)))
	dmax = 0
	for lo in range(n):
		for hi in range(n):
			if hi >= lo:
				xsum = xacc[hi] if lo == 0 else xacc[hi] - xacc[lo - 1]
				ysum = yacc[hi] if lo == 0 else yacc[hi] - yacc[lo - 1]
			else:
				xsum = xacc[hi] + xacc[-1] - xacc[lo - 1]
				ysum = yacc[hi] + yacc[-1] - yacc[lo - 1]
			dmax = max(dmax, xsum * xsum + ysum * ysum)
	print(dmax)


from math import *
from operator import itemgetter
nTests = int(input())
for t in range(nTests):
	n = int(input())
	a = [int(s) for s in input().split()]
	b = [int(s) for s in input().split()]
	vs = zip(a, b)
	vs = sorted(vs, key=lambda v: atan2(v[1], v[0]))
	best = 0
	for i in range(len(vs)):
		u = vs[i]
		l = u[0] * u[0] + u[1] * u[1]
		for v in (vs + vs)[i + 1:i + len(vs)]:
			tmpU = (u[0] + v[0], u[1] + v[1])
			tmpL = tmpU[0] * tmpU[0] + tmpU[1] * tmpU[1]
			if tmpL >= l:
				u = tmpU
				l = tmpL
		if l > best:
			best = l
		u = vs[i]
		l = u[0] * u[0] + u[1] * u[1]
		for v in reversed((vs + vs)[i + 1:i + len(vs)]):
			tmpU = (u[0] + v[0], u[1] + v[1])
			tmpL = tmpU[0] * tmpU[0] + tmpU[1] * tmpU[1]
			if tmpL >= l:
				u = tmpU
				l = tmpL
		if l > best:
			best = l
	print(best)


from math import *
from operator import itemgetter
nTests = int(input())
for t in range(nTests):
	n = int(input())
	a = [int(s) for s in input().split()]
	b = [int(s) for s in input().split()]
	vs = zip(a, b)
	vs = sorted(vs, key=lambda v: atan2(v[1], v[0]))
	best = 0
	for i in range(len(vs)):
		u = vs[i]
		l = u[0] * u[0] + u[1] * u[1]
		for v in (vs + vs)[i + 1:i + len(vs)]:
			tmpU = (u[0] + v[0], u[1] + v[1])
			tmpL = tmpU[0] * tmpU[0] + tmpU[1] * tmpU[1]
			if tmpL >= l:
				u = tmpU
				l = tmpL
		if l > best:
			best = l
		u = vs[i]
		l = u[0] * u[0] + u[1] * u[1]
		for v in reversed((vs + vs)[i + 1:i + len(vs)]):
			tmpU = (u[0] + v[0], u[1] + v[1])
			tmpL = tmpU[0] * tmpU[0] + tmpU[1] * tmpU[1]
			if tmpL >= l:
				u = tmpU
				l = tmpL
		if l > best:
			best = l
	print(best)


from math import pi, atan2
from itertools import chain, accumulate
for T in range(int(input())):
	n = int(input())
	a = map(int, input().split())
	b = map(int, input().split())
	phixy = sorted([(atan2(y, x), x, y) for (x, y) in zip(a, b) if x * x + y * y > 0])
	n = len(phixy)
	xacc = list(accumulate((x for (p, x, y) in phixy)))
	yacc = list(accumulate((y for (p, x, y) in phixy)))
	dmax = 0
	for lo in range(n):
		for hi in range(n):
			if hi >= lo:
				xsum = xacc[hi] if lo == 0 else xacc[hi] - xacc[lo - 1]
				ysum = yacc[hi] if lo == 0 else yacc[hi] - yacc[lo - 1]
			else:
				xsum = xacc[hi] + xacc[-1] - xacc[lo - 1]
				ysum = yacc[hi] + yacc[-1] - yacc[lo - 1]
			dmax = max(dmax, xsum * xsum + ysum * ysum)
	print(dmax)