From 8a2c33bb8187dff546aedfae7d646789afd53cbb Mon Sep 17 00:00:00 2001 From: wnsmir <163817041+wnsmir@users.noreply.github.com> Date: Wed, 6 Nov 2024 01:49:41 +0900 Subject: [PATCH] =?UTF-8?q?Create=209465=EC=8A=A4=ED=8B=B0=EC=BB=A4.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...65\354\212\244\355\213\260\354\273\244.py" | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 "wnsmir/DP/9465\354\212\244\355\213\260\354\273\244.py" diff --git "a/wnsmir/DP/9465\354\212\244\355\213\260\354\273\244.py" "b/wnsmir/DP/9465\354\212\244\355\213\260\354\273\244.py" new file mode 100644 index 0000000..5d50a89 --- /dev/null +++ "b/wnsmir/DP/9465\354\212\244\355\213\260\354\273\244.py" @@ -0,0 +1,46 @@ +import sys +input = sys.stdin.read + +def max_sticker_score(n, stickers): + # n이 1일 때는 간단히 큰 값을 반환 + if n == 1: + return max(stickers[0][0], stickers[1][0]) + + # DP 배열 초기화 + dp = [[0] * n for _ in range(2)] + + # 초기값 설정 + dp[0][0] = stickers[0][0] + dp[1][0] = stickers[1][0] + + # n이 2 이상인 경우 첫 두 열을 초기화 + if n > 1: + dp[0][1] = dp[1][0] + stickers[0][1] + dp[1][1] = dp[0][0] + stickers[1][1] + + # DP 배열 채우기 + for i in range(2, n): + dp[0][i] = max(dp[1][i-1], dp[1][i-2]) + stickers[0][i] # i-2까지 처리 (점화식) + dp[1][i] = max(dp[0][i-1], dp[0][i-2]) + stickers[1][i] + + # 최댓값 반환 + return max(dp[0][n-1], dp[1][n-1]) + +# 입력 처리 +data = input().strip().split() +T = int(data[0]) # 테스트 케이스 수 +index = 1 +results = [] + +for _ in range(T): + n = int(data[index]) + stickers = [ + list(map(int, data[index + 1:index + 1 + n])), + list(map(int, data[index + 1 + n:index + 1 + 2 * n])) + ] + results.append(max_sticker_score(n, stickers)) + index += 2 * n + 1 + +# 결과 출력 +for result in results: + print(result) \ No newline at end of file