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

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

 

2217๋ฒˆ: ๋กœํ”„

N(1 ≤ N ≤ 100,000)๊ฐœ์˜ ๋กœํ”„๊ฐ€ ์žˆ๋‹ค. ์ด ๋กœํ”„๋ฅผ ์ด์šฉํ•˜์—ฌ ์ด๋Ÿฐ ์ €๋Ÿฐ ๋ฌผ์ฒด๋ฅผ ๋“ค์–ด์˜ฌ๋ฆด ์ˆ˜ ์žˆ๋‹ค. ๊ฐ๊ฐ์˜ ๋กœํ”„๋Š” ๊ทธ ๊ตต๊ธฐ๋‚˜ ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ๋“ค ์ˆ˜ ์žˆ๋Š” ๋ฌผ์ฒด์˜ ์ค‘๋Ÿ‰์ด ์„œ๋กœ ๋‹ค๋ฅผ ์ˆ˜๋„ ์žˆ๋‹ค. ํ•˜

www.acmicpc.net


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

๐ŸŽจ Go

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

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

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

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

	var ropes = make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanln(reader, &ropes[i])
	}
	sort.Ints(ropes)
	var maxWeight int
	for i := len(ropes) - 1; i >= 0; i-- {
		weight := ropes[i] * (len(ropes) - i)
		if weight > maxWeight {
			maxWeight = weight
		}
	}
	fmt.Fprintln(writer, maxWeight)
}

๐ŸŽจ Python3

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

if __name__ == "__main__":
    n = int(sys.stdin.readline())
    ropes = []
    for i in range(n):
        ropes.append(int(sys.stdin.readline()))
    ropes.sort()
    max_weight = 0
    for i in reversed(range(len(ropes))):
        weight = ropes[i] * (len(ropes) - i)
        max_weight = max(max_weight, weight)
    print(max_weight)
728x90
๋Œ“๊ธ€