Post

[BOJ] 2798. ๋ธ”๋ž™์žญ(python)

๐Ÿ“Œ๋ฌธ์ œ

Alt text

๐Ÿ’ช์•„์ด๋””์–ด

  1. ์นด๋“œ 3์žฅ ์„ ํƒํ•˜๊ธฐ
    • dfs๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜ ์ฐพ๊ธฐ
    • ์„ ํƒํ•œ ์นด๋“œ์˜ ์ˆ˜๊ฐ€ 3์ด๋ฉด ์นด๋“œ ๊ณ ๋ฅด๊ธฐ ๋ฉˆ์ถค
  2. ํ•ฉ์ด 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.