코테용 문제풀이/백준

에너지 모으기 풀이

doctscoder 2023. 2. 16. 20:26

문제 링크: https://www.acmicpc.net/problem/16198

백준 알고리즘 중급 1/3 531에서 8번째 - 16198번 에너지 모으기를 풀어보았다.

 

풀이: https://lemonjade.tistory.com/m/11 를 참고했다.

 

C++

 

Python

def dfs(arr,tot):
	global res
	if len(arr)==2:
		res=max(res,tot)
		return
	for i in range(1,len(arr)-1):
		dfs(arr[:i]+arr[i+1:],tot+(arr[i-1]*arr[i+1]))
	
n=int(input())
nums=list(map(int,input().split()))
res=0
dfs(nums,res)
print(res)

Java