[JS/백준]{그리디}(11399) ATM

202210월 01

백준 문제 링크

문제 설명

간단한 문제이다. 오름차순으로 정렬한다음에 하나씩 더해주면된다.

1 -> 1+/1+2 -> 1+2/+1+2+3 -> 누적값이 정답!


코드

const line = require("fs").readFileSync("./input.txt", "utf8");
let [n, gestList] = line.trim().split("\n");

gestList = gestList.split(" ").map(Number);

gestList.sort((a, b) => a - b);

let result = 0;

for (let i = 0; i < n; i++) {
  for (let j = 0; j < i + 1; j++) {
    result += gestList[j];
  }
}
console.log(result);