-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathb18111.js
More file actions
41 lines (34 loc) · 897 Bytes
/
b18111.js
File metadata and controls
41 lines (34 loc) · 897 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
var fs = require("fs");
var input = fs.readFileSync("/dev/stdin").toString().trim().split("\n");
let [N, M, B] = input[0].split(" ").map(Number);
const base = input.slice(1).map((r) => r.split(" ").map(Number));
const maxHeight = 256;
let currHeightBlocks = 0;
for (let x = 0; x < N; x++) {
for (let y = 0; y < M; y++) {
currHeightBlocks += base[x][y];
}
}
let minTime = Infinity;
let height = 0;
for (let i = 0; i <= maxHeight; i++) {
const targetHeightBlocks = i * N * M;
if (targetHeightBlocks - currHeightBlocks > B) {
continue;
}
let time = 0;
for (let x = 0; x < N; x++) {
for (let y = 0; y < M; y++) {
if (base[x][y] < i) {
time += i - base[x][y];
} else if (base[x][y] > i) {
time += (base[x][y] - i) * 2;
}
}
}
if (time <= minTime) {
minTime = time;
height = i;
}
}
console.log(`${minTime} ${height}`);