티스토리 뷰
👩🏻💻 문제
✍🏻 풀이
🎨 Go
// https://www.acmicpc.net/problem/11052
package main
import (
"bufio"
"fmt"
"math"
"os"
)
func main() {
reader := bufio.NewReader(os.Stdin)
writer := bufio.NewWriter(os.Stdout)
defer writer.Flush()
var n int
fmt.Fscanln(reader, &n)
var p = make([]int, n+1)
for i := 1; i < n+1; i++ {
fmt.Fscanf(reader, "%d ", &p[i])
}
fmt.Fprintln(writer, getMaxPrice(p))
}
func getMaxPrice(p []int) int {
n := len(p)
var dp = make([]int, n)
dp[1] = p[1]
for i := 2; i < n; i++ {
dp[i] = p[i]
for j := 1; j < i; j++ {
dp[i] = int(math.Max(float64(dp[i]), float64(dp[i-j]+p[j])))
}
}
return dp[n-1]
}
🎨 Python3
# https://www.acmicpc.net/problem/11052
import sys
def get_max_price(p):
n = len(p)
dp = [0 for _ in range(n)]
dp[1] = p[1]
for i in range(2, n):
dp[i] = p[i]
for j in range(1, i):
dp[i] = max(dp[i], dp[i-j]+p[j])
return dp[n-1]
if __name__ == "__main__":
n = int(sys.stdin.readline())
p = [0]
p.extend(list(map(int, sys.stdin.readline().split())))
print(get_max_price(p))
728x90
'dev > algorithm' 카테고리의 다른 글
BOJ / 9465번 / 스티커 [Go][Python3] (0) | 2021.03.20 |
---|---|
BOJ / 2294번 / 동전 2 [Go][Python3] (0) | 2021.03.19 |
BOJ / 1697번 / 숨바꼭질 [Go][Python3] (0) | 2021.03.17 |
BOJ / 7576번 / 토마토 [Go][Python3] (0) | 2021.03.16 |
BOJ / 11140번 / LOL [Go][Python3] (0) | 2021.03.15 |
댓글