[백준] 16198번 에너지 모으기 ★★

거북이·2023년 2월 9일
0

백준[실버1]

목록 보기
38/67
post-thumbnail

💡문제접근

  • 백트래킹의 개념을 몰랐고 재귀에 대해서 익숙하지 않아서 구글링하면서 공부했다.
  • 백트래킹과 재귀의 개념을 이해한 다음 다시 풀어봐야겠다.

💡코드(메모리 : 31256KB, 시간 : 88ms)

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

def backtracking(s):
    global Max_energy
    if len(li) == 2:
        if s > Max_energy:
            Max_energy = s
        return
    else:
        for i in range(1, len(li)-1):
            r = li[i-1] * li[i+1]
            temp = li[i]
            del li[i]
            backtracking(s + r)
            li.insert(i, temp)

N = int(input().strip())
li = list(map(int, input().strip().split()))
Max_energy = 0
backtracking(0)
print(Max_energy)

💡소요시간 : 1h

0개의 댓글