-
Notifications
You must be signed in to change notification settings - Fork 63
/
bon-appetit.js
49 lines (36 loc) · 991 Bytes
/
bon-appetit.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
'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++];
}
// Complete the bonAppetit function below.
function bonAppetit(bill, k, b) {
let sum = 0;
bill.forEach((x) => {
sum = sum + x;
});
sum = (sum - bill[k])/2;
if (sum === b)
console.log("Bon Appetit");
else {
console.log(Math.abs(b - sum));
}
}
function main() {
const nk = readLine().replace(/\s+$/g, '').split(' ');
const n = parseInt(nk[0], 10);
const k = parseInt(nk[1], 10);
const bill = readLine().replace(/\s+$/g, '').split(' ').map(billTemp => parseInt(billTemp, 10));
const b = parseInt(readLine().trim(), 10);
bonAppetit(bill, k, b);
}