[백준] - 13305 주유소 (node.js)

밀루·2025년 1월 20일
1

BOJ

목록 보기
56/82

문제링크

코드

const fs = require("fs");
const filePath = process.platform === "linux" ? "/dev/stdin" : "./input.txt";
const arr = fs.readFileSync(filePath).toString().trim().split("\n");

const n = Number(arr[0]);
const di = arr[1].split(" ").map(BigInt);
let price = arr[2].split(" ").map(BigInt);
price.pop(); // 마지막껀 안씀

let total = 0n; // BigInt
let nowPrice = price[0];
for (let i = 0; i < n - 1; i++) {
  if (price[i] < nowPrice) {
    nowPrice = price[i];
  }
  total += di[i] * nowPrice;
}

console.log(String(total));

그냥 Number로 하니까 58점 밖에 받지 못했다. 문제 조건에서 각 거리와 리터당 가격이 최대 1,000,000,000 이하의 자연수라 Number의 범위를 벗어나는 게 문제였다. BigInt로 변경하니 100점이 나왔다.

profile
이밀루의 도전

0개의 댓글