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

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

 

14888๋ฒˆ: ์—ฐ์‚ฐ์ž ๋ผ์›Œ๋„ฃ๊ธฐ

์ฒซ์งธ ์ค„์— ์ˆ˜์˜ ๊ฐœ์ˆ˜ N(2 ≤ N ≤ 11)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” A1, A2, ..., AN์ด ์ฃผ์–ด์ง„๋‹ค. (1 ≤ Ai ≤ 100) ์…‹์งธ ์ค„์—๋Š” ํ•ฉ์ด N-1์ธ 4๊ฐœ์˜ ์ •์ˆ˜๊ฐ€ ์ฃผ์–ด์ง€๋Š”๋ฐ, ์ฐจ๋ก€๋Œ€๋กœ ๋ง์…ˆ(+)์˜ ๊ฐœ์ˆ˜, ๋บ„์…ˆ(-)์˜ ๊ฐœ์ˆ˜, ๏ฟฝ๏ฟฝ

www.acmicpc.net


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

๐ŸŽจ Go

package main

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

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

	var n int
	fmt.Fscanln(reader, &n)

	var numbers = make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d ", &numbers[i])
	}

	var counts = make([]int, 4)
	for i := 0; i < 4; i++ {
		fmt.Fscanf(reader, "%d ", &counts[i])
	}

	var max, min, sum, idx = -1000000000, 1000000000, numbers[0], 0
	solve(numbers, counts, &max, &min, sum, idx)

	fmt.Fprintln(writer, max)
	fmt.Fprintln(writer, min)
}

func solve(numbers, counts []int, maxptr, minptr *int, sum, idx int) {
	if idx == len(numbers)-1 {
		if *maxptr < sum {
			*maxptr = sum
		}
		if *minptr > sum {
			*minptr = sum
		}
		return
	}

	if counts[0] > 0 {
		counts[0]--
		solve(numbers, counts, maxptr, minptr, sum+numbers[idx+1], idx+1)
		counts[0]++
	}

	if counts[1] > 0 {
		counts[1]--
		solve(numbers, counts, maxptr, minptr, sum-numbers[idx+1], idx+1)
		counts[1]++
	}

	if counts[2] > 0 {
		counts[2]--
		solve(numbers, counts, maxptr, minptr, sum*numbers[idx+1], idx+1)
		counts[2]++
	}

	if counts[3] > 0 {
		counts[3]--
		solve(numbers, counts, maxptr, minptr, sum/numbers[idx+1], idx+1)
		counts[3]++
	}
}
728x90
๋Œ“๊ธ€