There are $n$ pieces of cake on a line. The $i$-th piece of cake has weight $a_i$ ($1 \leq i \leq n$).

The tastiness of the cake is the maximum total weight of two adjacent pieces of cake (i. e., $\max(a_1+a_2,\, a_2+a_3,\, \ldots,\, a_{n-1} + a_{n})$).

You want to maximize the tastiness of the cake. You are allowed to do the following operation at most once (doing more operations would ruin the cake):

Choose a contiguous subsegment $a[l, r]$ of pieces of cake ($1 \leq l \leq r \leq n$), and reverse it.

The subsegment $a[l, r]$ of the array $a$ is the sequence $a_l, a_{l+1}, \dots, a_r$.

If you reverse it, the array will become $a_1, a_2, \dots, a_{l-2}, a_{l-1}, \underline{a_r}, \underline{a_{r-1}}, \underline{\dots}, \underline{a_{l+1}}, \underline{a_l}, a_{r+1}, a_{r+2}, \dots, a_{n-1}, a_n$.

For example, if the weights are initially $[5, 2, 1, 4, 7, 3]$, you can reverse the subsegment $a[2, 5]$, getting $[5, \underline{7}, \underline{4}, \underline{1}, \underline{2}, 3]$. The tastiness of the cake is now $5 + 7 = 12$ (while before the operation the tastiness was $4+7=11$).

Find the maximum tastiness of the cake after doing the operation at most once.


-----Input-----

The first line contains a single integer $t$ ($1 \le t \le 50$) — the number of test cases.

The first line of each test case contains a single integer $n$ ($2 \le n \le 1000$) — the number of pieces of cake.

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) — $a_i$ is the weight of the $i$-th piece of cake.


-----Output-----

For each test case, print a single integer: the maximum tastiness of the cake after doing the operation at most once.


-----Examples-----

Input
5
6
5 2 1 4 7 3
3
32 78 78
3
69 54 91
8
999021 999021 999021 999021 999652 999021 999021 999021
2
1000000000 1000000000
Output
12
156
160
1998673
2000000000


-----Note-----

In the first test case, after reversing the subsegment $a[2, 5]$, you get a cake with weights $[5, \underline{7}, \underline{4}, \underline{1}, \underline{2}, 3]$. The tastiness of the cake is now $\max(5+7, 7+4, 4+1, 1+2, 2+3) = 12$. This is the maximum possible tastiness of the cake one can obtain by performing the operation at most once.

In the second test case, it's optimal not to do any operation. The tastiness is $78+78 = 156$.

In the third test case, after reversing the subsegment $a[1, 2]$, you get a cake with weights $[\underline{54}, \underline{69}, 91]$. The tastiness of the cake is now $\max(54+69, 69+91) = 160$. There is no way to make the tastiness of the cake greater than $160$ by performing at most one operation.

Difficulty: EASY
Tags: Complete search

Sorting

Implementation

Greedy algorithms
Skill types: Complete search

Sorting

Greedy algorithms

Starter code:

Solutions:
t = int(input())
for _ in range(t):
	n = int(input())
	nums = list(map(int, input().split()))
	nums.sort()
	print(nums[-1] + nums[-2])


for caseno in range(int(input())):
	n = int(input())
	a = map(int, input().split())
	a = sorted(a, reverse=True)
	print(sum(a[:2]))


T = int(input())
for _ in range(T):
	sum = 0
	N = int(input())
	nums = list(map(int, input().split()))
	sum += max(nums)
	nums.remove(sum)
	sum += max(nums)
	print(sum)


import sys
input = sys.stdin.readline

def solve(n, arr):
	arr.sort()
	print(arr[n - 1] + arr[n - 2])
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	solve(n, arr)


t = int(input())
while t:
	n = int(input())
	a = list(map(int, input().split()))
	a1 = max(a)
	a.remove(a1)
	a2 = max(a)
	print(a1 + a2)
	t -= 1


import math
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	max1 = max(arr)
	arr.remove(max1)
	max2 = max(arr)
	print(max1 + max2)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort(reverse=True)
	print(l[0] + l[1])


a = int(input())
for i in range(a):
	b = int(input())
	c = list(map(int, input().split()))
	c.sort()
	print(c[-1] + c[-2])


T = int(input())
for _ in range(T):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	print(l[-1] + l[-2])


mod = 10000000
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a1 = a
	m_1 = max(a)
	a1.remove(m_1)
	m_2 = max(a1)
	print(m_1 + m_2)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	mx1 = max(a)
	a.remove(mx1)
	mx2 = max(a)
	print(mx1 + mx2)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split(' ')))
	a.sort(reverse=True)
	print(a[0] + a[1])


t = int(input())
for i in range(t):
	n = int(input())
	l1 = [int(j) for j in input().split()]
	l1.sort()
	print(l1[-1] + l1[-2])


t = int(input())
for ele in range(t):
	n = int(input())
	foo = [int(i) for i in input().split()]
	foo.sort()
	print(foo[-1] + foo[-2])


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	maxi1 = max(a)
	a.remove(maxi1)
	maxi2 = max(a)
	print(maxi1 + maxi2)


t = int(input())
for i in range(t):
	a = int(input())
	l = list(map(int, input().split()))
	f1 = max(l)
	l.pop(l.index(f1))
	if len(l) == 0:
		print(f1)
	else:
		f2 = max(l)
		print(f1 + f2)


t = int(input())
while t:
	n = int(input())
	cake = list(map(int, input().split(' ')))
	max_1 = max(cake)
	cake.remove(max_1)
	max_2 = max(cake)
	print(max_1 + max_2)
	t -= 1


t = int(input())
while t:
	t -= 1
	l = int(input())
	cake = list(map(int, input().split()))
	ans = max(cake)
	cake.remove(ans)
	ans += max(cake)
	print(ans)


t = int(input())
while t:
	t -= 1
	n = int(input())
	l = list(map(int, input().split()))
	s = max(l)
	l.remove(s)
	s += max(l)
	print(s)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	x = max(a)
	a.remove(x)
	y = max(a)
	print(x + y)


T = int(input())
for i in range(T):
	n = int(input())
	A = list(map(int, input().split()))
	A.sort()
	print(A[-1] + A[-2])


def THE_TAST(n):
	Output = []
	for i in range(n):
		a = int(input())
		list_of_cakes = list(map(int, input().split()))
		max1 = max(list_of_cakes)
		list_of_cakes.pop(list_of_cakes.index(max1))
		max2 = max(list_of_cakes)
		Output.append(max1 + max2)
	for i in range(len(Output)):
		print(Output[i])
n = int(input())
THE_TAST(n)


t = int(input())
for _ in range(t):
	cakes = int(input())
	weight_cakes = list(map(int, input().split()))
	if cakes <= 2:
		print(sum(weight_cakes))
	else:
		first_max = max(weight_cakes)
		weight_cakes.remove(first_max)
		second_max = max(weight_cakes)
		print(first_max + second_max)


x = int(input().strip())
for i in range(x):
	y = int(input().strip())
	list1 = list(map(int, input().strip().split()))
	y = max(list1)
	list1.remove(y)
	x = max(list1)
	list1.remove(x)
	print(x + y)
	list1.clear()


def Tastiness(arr):
	arr.sort(reverse=True)
	return arr[0] + arr[1]
t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	print(Tastiness(arr))


t = int(input())
ml = []
for i in range(t):
	int(input())
	lst = list(map(int, input().rstrip().split()))
	ml.append(lst)
for j in ml:
	a = max(j)
	j.remove(a)
	b = max(j)
	print(a + b)


t = int(input())
for i in range(0, t):
	n = int(input())
	l = [int(j) for j in input().split()]
	l = sorted(l)
	s = l[n - 1] + l[n - 2]
	print(s)


def main():
	for _ in range(int(input())):
		n = int(input())
		nums = list(map(int, input().split()))
		nums.sort()
		print(nums[-1] + nums[-2])
main()


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	m1 = max(l)
	l.remove(m1)
	m2 = max(l)
	print(m1 + m2)


def rmt(lis):
	n = len(lis)
	if n == 2:
		return sum(lis)
	elif n >= 2:
		k = max(lis)
		if lis.count(k) >= 2:
			return 2 * k
		else:
			lis.remove(k)
			return max(lis) + k
n = int(input())
result = []
for i in range(n):
	noe = int(input())
	noel = list(map(int, input().split()))
	result.append(rmt(noel))
for el in result:
	print(el)


t = int(input())
while t:
	n = int(input())
	a = list(map(int, input().split()))
	a_max1 = max(a)
	a.remove(a_max1)
	a_max2 = max(a)
	print(a_max1 + a_max2)
	t -= 1


for i in range(int(input())):
	int(input())
	l1 = list(map(int, input().split()))
	sum = max(l1)
	l1.remove(max(l1))
	sum = sum + max(l1)
	print(sum)


t = int(input())
for i in range(t):
	n = int(input())
	entries = list(map(int, input().split()))
	if len(entries) == 1:
		print(entries[0])
		break
	max1 = max(entries)
	entries.remove(max1)
	max2 = max(entries)
	print(max2 + max1)


for _ in range(int(input())):
	n = int(input())
	a = sorted(list(map(int, input().split())), reverse=True)
	print(a[0] + a[1])


m = []
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()[:n]))
	c = 0
	i = max(a)
	c += i
	a.remove(i)
	ii = max(a)
	c += ii
	m.append(c)
for mo in m:
	print(mo)


t = int(input())
for item in range(0, t):
	n = int(input())
	arr = list(map(int, input().split()))
	max1 = max(arr)
	arr.remove(max1)
	max2 = max(arr)
	print(max1 + max2)


t = int(input())
for _ in range(0, t):
	n = int(input())
	brr = []
	arr = list(map(int, input().rstrip().split()))
	marr = max(arr)
	brr = arr
	brr.remove(marr)
	mbrr = max(brr)
	print(marr + mbrr)


a = int(input())
for i in range(a):
	b = int(input())
	c = list(map(int, input().split()))
	c1 = max(c)
	c3 = c.index(c1)
	if c3 == 0:
		c = c[c3 + 1:]
	elif c3 == b - 1:
		c = c[:c3]
	else:
		c = c[:c3] + c[c3 + 1:]
	c2 = max(c)
	print(c1 + c2)


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	print(l[-1] + l[-2])


for _ in range(int(input())):
	n = int(input())
	x = sorted(list(map(int, input().split())))
	print(sum(x[-2:]))


t = int(input())
for i in range(t):
	n = int(input())
	l = []
	s = input()
	l = [int(j) for j in s.split(' ')]
	if n == 2:
		print(l[0] + l[1])
		continue
	t1 = max(l)
	l.remove(t1)
	t2 = max(l)
	print(t1 + t2)


def find(W):
	W.sort()
	return W[-1] + W[-2]
num = int(input())
for n in range(num):
	p = input()
	W = [int(x) for x in input().split()]
	print(find(W))


for i in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	if n == 2:
		print(sum(arr))
	else:
		arr = sorted(arr)
		max1 = arr[n - 1]
		max2 = arr[n - 2]
		print(max1 + max2)


import collections
from math import ceil, gcd, sqrt, log
INF = float('inf')
mod = 1000000007

def solve():
	n = int(input())
	A = list(map(int, input().split()))
	A.sort()
	ans = A[-1] + A[-2]
	print(ans)
t = int(input())
while t != 0:
	solve()
	t -= 1


for s in [*open(0)][2::2]:
	a = list(map(int, s.split()))
	print(sum(sorted(a)[-2:]))


def work():
	n = int(input())
	x = [int(i) for i in input().split()]
	x.sort()
	return x[-1] + x[-2]
rez = list()
t = int(input())
for i in range(t):
	rez.append(work())
for i in rez:
	print(i)


for _ in range(int(input())):
	n = int(input())
	l = sorted(list(map(int, input().split())))
	print(l[-1] + l[-2])


import sys

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_ls():
	return list(map(int, sys.stdin.readline().strip().split()))

def main():
	for _ in range(int(input())):
		n = int(input())
		ls = get_ls()
		ls.sort()
		print(ls[n - 1] + ls[n - 2])
main()


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	arr.sort()
	if n > 1:
		print(arr[-1] + arr[-2])
	else:
		print(arr[-1])


for _ in ' ' * int(input()):
	n = int(input())
	l = list(map(int, input().split()))
	a = max(l)
	l.remove(a)
	a += max(l)
	print(a)


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())
for _ in range(inp()):
	input()
	lst = list(map(int, input().split()))
	lst.sort()
	print(sum(lst[-2:]))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	maxVal = int(a[n - 1])
	secMax = int(a[n - 2])
	print(maxVal + secMax)


import math
t = int(input())
while t > 0:
	n = int(input())
	q = input()
	a = list(map(int, q.split(' ')))
	a.sort()
	print(a[-1] + a[-2])
	t -= 1


tc = int(input())
for i in range(tc):
	n = input()
	t = input()
	a = tuple((int(x) for x in t.split()))
	res = sorted(a)
	print(res[-1] + res[-2])


def solve(nums):
	maxVal = max(nums)
	nums.remove(maxVal)
	return maxVal + max(nums)
while True:
	try:
		T = int(input())
		for _ in range(T):
			n = int(input())
			nums = list(map(int, input().split(' ')))
			print(solve(nums))
	except:
		break


def find(L):
	one = max(L)
	L.remove(one)
	two = max(L)
	return one + two
for _ in range(int(input())):
	n = int(input())
	L = input().split()
	intL = [int(elem) for elem in L]
	print(find(intL))


for i in range(int(input())):
	(n, nums) = (int(input()), list(map(int, input().split())))
	nums.sort()
	print(nums[len(nums) - 1] + nums[len(nums) - 2])


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	max_val = a[-1]
	second_max_val = a[len(a) - 2]
	print(max_val + second_max_val, end='\n')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	p = max(a)
	a.remove(p)
	print(p + max(a))


for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	a.sort(reverse=True)
	print(a[0] + a[1])


for i in range(int(input())):
	Len = int(input())
	cake = list(map(int, input().split()))
	cake.sort()
	Max = max(cake)
	cake.pop(-1)
	SMax = max(cake)
	print(Max + SMax)


t = int(input())
for zz in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	z = max(l)
	l.remove(z)
	p = max(l)
	print(z + p)


for i in range(int(input())):
	input()
	l = sorted(map(int, input().split()))
	print(l[-1] + l[-2])


import heapq as pq
t = int(input())
for _ in range(t):
	n = int(input())
	nums = list(map(int, input().split()))
	nums = [-num for num in nums]
	pq.heapify(nums)
	print(-(pq.heappop(nums) + pq.heappop(nums)))


def solve(nb, l):
	pg1 = -1
	pg2 = -1
	for v in l:
		if v > pg1:
			if pg1 > pg2:
				pg2 = pg1
			pg1 = v
		elif v > pg2:
			pg2 = v
	return pg1 + pg2
n = int(input())
for p in range(n):
	nb = int(input())
	l = [int(x) for x in input().split()]
	print(solve(nb, l))


def solve(l):
	pg1 = -1
	pg2 = -1
	for v in l:
		v = int(v)
		if v > pg2:
			if v > pg1:
				if pg1 > pg2:
					pg2 = pg1
				pg1 = v
			else:
				pg2 = v
	return pg1 + pg2
n = int(input())
for p in range(n):
	input()
	print(solve(input().split()))


for i in range(int(input())):
	s = int(input())
	l = map(int, input().split(' '))
	l = sorted(l)[::-1]
	print(l[0] + l[1])


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().strip().split()))
	arr = sorted(arr)
	print(arr[-1] + arr[-2])


for _ in range(int(input())):
	n = int(input())
	weights = list(map(int, input().split()))
	weights.sort()
	print(weights[-1] + weights[-2])


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	c = max(arr)
	arr.remove(c)
	c += max(arr)
	print(c)


t = int(input())
for i in range(t):
	n = int(input())
	l = [int(i) for i in input().split()]
	l.sort(reverse=True)
	print(l[0] + l[1])


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	ans = l[len(l) - 1] + l[len(l) - 2]
	print(ans)


n = int(input())
a = []
for _ in range(n):
	tp = input()
	arr = list(map(int, input().split()))
	arr.sort(reverse=True)
	a.append(arr)
for x in a:
	print(x[0] + x[1])


alpha = []
beta = []
gamma = []
t = int(input())
for i in range(t):
	n = int(input())
	a = [int(s) for s in input().split(' ')]
	alpha.append(n)
	beta.append(a)
for i in range(t):
	n = alpha[i]
	a = beta[i]
	a.sort()
	print(a[-1] + a[-2])


def solution(n, arr):
	(max1, max2) = (0, 0)
	for i in range(n):
		if arr[i] >= max1:
			max2 = max1
			max1 = arr[i]
		elif arr[i] > max2:
			max2 = arr[i]
	print(max1 + max2)

def main():
	t = int(input())
	while t:
		n = int(input())
		s = input().split(' ')
		arr = [int(x) for x in s]
		solution(n, arr)
		t -= 1
main()


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	print(a[-1] + a[-2])


t = int(input())
for i in range(t):
	n = int(input())
	num = list(map(int, input().split()))
	max1 = 0
	max2 = 0
	for x in num:
		if x > max1:
			max2 = max1
			max1 = x
		elif x > max2:
			max2 = x
	print(max1 + max2)


n = int(input())
for i in range(n):
	a = int(input())
	list = []
	list = [int(x) for x in input().split()]
	list.sort()
	print(list[a - 1] + list[a - 2])


from itertools import combinations
for _ in range(int(input())):
	l = int(input())
	arr = [int(i) for i in input().split()]
	arr = sorted(arr, reverse=True)
	sm = arr[0] + arr[1]
	print(sm)


test_cases = int(input())
for _ in range(test_cases):
	number_of_cases = int(input())
	weight_of_cakes = list(map(int, input().split()))
	weight_of_cakes.sort(reverse=True)
	print(weight_of_cakes[0] + weight_of_cakes[1])


a = int(input())
i = 0
while a > i:
	i = i + 1
	b = int(input())
	c = list(map(int, input().split()))
	d = sorted(c)
	print(d[b - 1] + d[b - 2])


a = int(input())
for i in range(a):
	b = int(input())
	c = list(map(int, input().split()))
	d = max(c)
	c.remove(d)
	e = max(c)
	print(d + e)


nb_test_case = int(input())
dico = {}
for i in range(nb_test_case):
	nb_pieces = int(input())
	liste = []
	weight = [int(x) for x in input().split()]
	dico[i] = weight
for test_case in dico.keys():
	maximum1 = max(dico[test_case])
	dico[test_case].remove(maximum1)
	maximum2 = max(dico[test_case])
	print(maximum1 + maximum2)