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

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

 

13305๋ฒˆ: ์ฃผ์œ ์†Œ

ํ‘œ์ค€ ์ž…๋ ฅ์œผ๋กœ ๋‹ค์Œ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ฒซ ๋ฒˆ์งธ ์ค„์—๋Š” ๋„์‹œ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ •์ˆ˜ N(2 ≤ N ≤ 100,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ ์ค„์—๋Š” ์ธ์ ‘ํ•œ ๋‘ ๋„์‹œ๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ๋„๋กœ์˜ ๊ธธ์ด๊ฐ€ ์ œ์ผ ์™ผ์ชฝ ๋„๋กœ๋ถ€ํ„ฐ N-1

www.acmicpc.net


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

๐ŸŽจ Go

// https://www.acmicpc.net/problem/13305
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 distances = make([]int, n-1)
	var prices = make([]int, n)
	for i := 0; i < n-1; i++ {
		fmt.Fscanf(reader, "%d ", &distances[i])
	}
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d ", &prices[i])
	}

	result := 0
	for i := 0; i < n-1; i++ {
		cur := prices[i]
		dist := distances[i]
		for j := i + 1; j < n-1; j++ {
			next := prices[j]
			if cur < next {
				dist += distances[j]
				i++
			} else {
				break
			}
		}
		result += dist * cur
	}
	fmt.Fprintln(writer, result)
}

๐ŸŽจ Python3

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

if __name__ == "__main__":
    n = int(sys.stdin.readline())
    distances = list(map(int, sys.stdin.readline().split()))
    prices = list(map(int, sys.stdin.readline().split()))
    result = 0
    i = 0
    while i < n-1:
        cur = prices[i]
        dist = distances[i]
        for j in range(i+1, n-1):
            next = prices[j]
            if cur < next:
                dist += distances[j]
                i += 1
            else:
                break
        result += dist * cur
        i += 1
    print(result)
728x90
๋Œ“๊ธ€