[BOJ] 2798. ๋ธ๋์ญ(python)
๐๋ฌธ์
๐ช์์ด๋์ด
- ์นด๋ 3์ฅ ์ ํํ๊ธฐ
- dfs๋ฅผ ์ฌ์ฉํ์ฌ ๋ชจ๋ ๊ฒฝ์ฐ์ ์ ์ฐพ๊ธฐ
- ์ ํํ ์นด๋์ ์๊ฐ 3์ด๋ฉด ์นด๋ ๊ณ ๋ฅด๊ธฐ ๋ฉ์ถค
- ํฉ์ด m์ดํ์ด๋ฉด์ ๊ฐ์ฅ ํฐ ๊ฐ ๊ณ ๋ฅด๊ธฐ
๐ฅ์ฝ๋
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import sys
input=sys.stdin.readline
answer=0
def dfs(li,inx):
global answer
if len(li)==3:
li_sum = sum(li)
if li_sum <= m and answer < li_sum : answer = li_sum
return
for i in range(inx+1,n):
if not visited[i]:
visited[i]=True
li.append(numbers[i])
dfs(li,i)
li.pop()
visited[i]=False
n,m = map(int,input().split())
visited = [False]*n
numbers = list(map(int,input().split()))
for i,num in enumerate(numbers):
visited[i]=True
dfs([num],i)
print(answer)
This post is licensed under CC BY 4.0 by the author.
Comments powered by Disqus.