ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป ๋ฌธ์ œ

 

14501๋ฒˆ: ํ‡ด์‚ฌ

์ฒซ์งธ ์ค„์— ๋ฐฑ์ค€์ด๊ฐ€ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ์ด์ต์„ ์ถœ๋ ฅํ•œ๋‹ค.

www.acmicpc.net


โœ๐Ÿป ํ’€์ด

๐ŸŽจ Go

// https://www.acmicpc.net/problem/14501
package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	result int
	infos  []Info
	n      int
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	fmt.Fscanln(reader, &n)

	for i := 0; i < n; i++ {
		var time, profit int
		fmt.Fscanln(reader, &time, &profit)
		infos = append(infos, Info{time, profit})
	}
	getMax(0, 0)
	fmt.Fprintln(writer, result)
}

type Info struct {
	time   int
	profit int
}

func getMax(index, profit int) {
	if index == n {
		if result < profit {
			result = profit
		}
		return
	}

	if index > n {
		return
	}

	getMax(index+infos[index].time, profit+infos[index].profit) // ํ•ด๋‹น ๋‚ ์งœ์— ์ƒ๋‹ด์„ ํ•˜๋Š” ๊ฒฝ์šฐ
	getMax(index+1, profit)                                     // ํ•ด๋‹น ๋‚ ์งœ์— ์ƒ๋‹ด์„ ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ
}

๐ŸŽจ Python3

# https://www.acmicpc.net/problem/14501
import sys

result = 0
n = 0
infos = []

def get_max(index, profit):
    global result

    if index == n:
        if result < profit:
            result = profit
        return
    
    if index > n:
        return

    get_max(index+infos[index][0], profit+infos[index][1])
    get_max(index+1, profit)

if __name__ == "__main__":
    n = int(sys.stdin.readline())
    for i in range(0, n):
        time, profit = list(map(int, sys.stdin.readline().split()))
        infos.append((time, profit))
    get_max(0, 0)
    print(result)
728x90
๋Œ“๊ธ€