Skip to content

Commit fec5fb9

Browse files
committed
lucky
1 parent c17e9d7 commit fec5fb9

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

luck-balance.js

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
'use strict';
2+
3+
const fs = require('fs');
4+
5+
process.stdin.resume();
6+
process.stdin.setEncoding('utf-8');
7+
8+
let inputString = '';
9+
let currentLine = 0;
10+
11+
process.stdin.on('data', inputStdin => {
12+
inputString += inputStdin;
13+
});
14+
15+
process.stdin.on('end', function () {
16+
inputString = inputString.replace(/\s*$/, '')
17+
.split('\n')
18+
.map(str => str.replace(/\s*$/, ''));
19+
20+
main();
21+
});
22+
23+
function readLine() {
24+
return inputString[currentLine++];
25+
}
26+
27+
// Complete the luckBalance function below.
28+
function luckBalance(k, contests) {
29+
let sum = (x, y) => x + y;
30+
let important_contests = contests.filter(x => x[1] === 1).map(x => x[0]).sort((x, y) => x - y);
31+
let not_important_contests = contests.filter(x => x[1] === 0).map(x => x[0]).reduce(sum, 0);
32+
let count = not_important_contests;
33+
if (important_contests.length > k) {
34+
let i = important_contests.length - k;
35+
while (i--) {
36+
count -= important_contests.shift();
37+
}
38+
}
39+
count += important_contests.reduce(sum, 0)
40+
return count;
41+
42+
43+
}
44+
45+
function main() {
46+
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
47+
48+
const nk = readLine().split(' ');
49+
50+
const n = parseInt(nk[0], 10);
51+
52+
const k = parseInt(nk[1], 10);
53+
54+
let contests = Array(n);
55+
56+
for (let i = 0; i < n; i++) {
57+
contests[i] = readLine().split(' ').map(contestsTemp => parseInt(contestsTemp, 10));
58+
}
59+
60+
const result = luckBalance(k, contests);
61+
62+
ws.write(result + '\n');
63+
64+
ws.end();
65+
}

0 commit comments

Comments
 (0)