-
Notifications
You must be signed in to change notification settings - Fork 0
/
18111.js
58 lines (51 loc) · 1.34 KB
/
18111.js
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
var readline = require("readline");
const { resourceLimits } = require("worker_threads");
var r = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let input = [];
r.on("line", function (line) {
input.push(line);
}).on("close", function () {
main(input);
process.exit();
});
function main(input) {
const [N, M, B] = input.shift().split(" ").map(Number);
const result = [Infinity];
let min = +Infinity,
max = -Infinity;
for (let i = 0; i < N; i++) {
input[i] = input[i].split(" ").map(Number);
for (let j = 0; j < M; j++) {
min = Math.min(min, input[i][j]);
max = Math.max(max, input[i][j]);
}
}
for (let floor = min; floor <= max; floor++) {
const cnt = setFloor(floor);
if (result[0] >= cnt) {
result[0] = cnt;
result[1] = floor;
}
}
console.log(result.join(" "));
function setFloor(floor) {
let cnt = 0;
let block = B;
for (let i = 0; i < N; i++) {
for (let j = 0; j < M; j++) {
const currentFloor = input[i][j];
if (currentFloor > floor) {
cnt += (currentFloor - floor) * 2;
block += currentFloor - floor;
} else if (currentFloor < floor) {
cnt += floor - currentFloor;
block -= floor - currentFloor;
}
}
}
return block >= 0 ? cnt : Infinity;
}
}