-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLuck-Balance.js
More file actions
136 lines (99 loc) · 3.88 KB
/
Luck-Balance.js
File metadata and controls
136 lines (99 loc) · 3.88 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
'use strict';
const fs = require('fs');
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 'luckBalance' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. INTEGER k
* 2. 2D_INTEGER_ARRAY contests
*/
function luckBalance(k, contests) {
// Write your code here
let i,total=0,j=0,n=contests.length,imp=[];
for( i=0;i<n;i++)
{
if(contests[i][1]==0)
total+=contests[i][0];
else
{
imp[j++]=contests[i][0];
}
}
imp.sort(function(a,b){return b-a});
//console.table(imp.sort(function(a,b){a-b}));
for(i=0;i<imp.length;i++){
if(i<k){
total +=imp[i];
}else{
total -=imp[i];
}
}
return total;
}
function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
const firstMultipleInput = readLine().replace(/\s+$/g, '').split(' ');
const n = parseInt(firstMultipleInput[0], 10);
const k = parseInt(firstMultipleInput[1], 10);
let contests = Array(n);
for (let i = 0; i < n; i++) {
contests[i] = readLine().replace(/\s+$/g, '').split(' ').map(contestsTemp => parseInt(contestsTemp, 10));
}
const result = luckBalance(k, contests);
ws.write(result + '\n');
ws.end();
}
/*
Lena is preparing for an important coding competition that is preceded by a number of sequential preliminary contests. Initially, her luck balance is 0. She believes in "saving luck", and wants to check her theory. Each contest is described by two integers, and :
is the amount of luck associated with a contest. If Lena wins the contest, her luck balance will decrease by ; if she loses it, her luck balance will increase by .
denotes the contest's importance rating. It's equal to if the contest is important, and it's equal to if it's unimportant.
If Lena loses no more than important contests, what is the maximum amount of luck she can have after competing in all the preliminary contests? This value may be negative.
Example
Contest L[i] T[i]
1 5 1
2 1 1
3 4 0
If Lena loses all of the contests, her will be . Since she is allowed to lose important contests, and there are only important contests, she can lose all three contests to maximize her luck at .
If , she has to win at least of the important contests. She would choose to win the lowest value important contest worth . Her final luck will be .
Function Description
Complete the luckBalance function in the editor below.
luckBalance has the following parameter(s):
int k: the number of important contests Lena can lose
int contests[n][2]: a 2D array of integers where each contains two integers that represent the luck balance and importance of the contest
Returns
int: the maximum luck balance achievable
Input Format
The first line contains two space-separated integers and , the number of preliminary contests and the maximum number of important contests Lena can lose.
Each of the next lines contains two space-separated integers, and , the contest's luck balance and its importance rating.
Constraints
Sample Input
STDIN Function
----- --------
6 3 n = 6, k = 3
5 1 contests = [[5, 1], [2, 1], [1, 1], [8, 1], [10, 0], [5, 0]]
2 1
1 1
8 1
10 0
5 0
Sample Output
29
Explanation
There are contests. Of these contests, are important and she cannot lose more than of them. Lena maximizes her
luck if she wins the important contest (where ) and loses all of the other five contests for a total luck balance of .
*/