-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirtydriving.js
45 lines (29 loc) · 889 Bytes
/
dirtydriving.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
'use strict';
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', function(inputStdin) {
inputString += inputStdin;
});
process.stdin.on('end', function() {
inputString = inputString.split('\n');
main();
});
function readLine() {
return inputString[currentLine++];
}
function main() {
let s = readLine().split(' ').map(Number);
let n = s[0]; // cars in front
let p = s[1]; // deceleration constant
const dists = readLine().split(' ')
.map(Number).sort((a, b) => a - b);
let min = dists[0];
let minDist = 0;
dists.forEach((dist, i) => {
// compare prev found min dist, to new using the: p(curr + 1)
minDist = Math.max(minDist, p * (i + 1) - dist + min)
});
console.log(minDist);
}