-
Notifications
You must be signed in to change notification settings - Fork 0
/
luck-balance.php
51 lines (38 loc) · 1.03 KB
/
luck-balance.php
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
<?php
/*
* 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)
{
$sum = 0;
$arr = [];
foreach ($contests as $contest) {
if ($contest[1] == 1) {
$arr[] = $contest[0];
} else {
$sum += $contest[0];
}
}
rsort($arr);
for ($x = 0, $l = count($arr); $x < $l; $x++) {
$sum += ($x < $k ? 1 : -1) * $arr[$x];
}
return $sum;
}
$fptr = fopen(getenv("OUTPUT_PATH"), "w");
$first_multiple_input = explode(' ', rtrim(fgets(STDIN)));
$n = intval($first_multiple_input[0]);
$k = intval($first_multiple_input[1]);
$contests = array();
for ($i = 0; $i < $n; $i++) {
$contests_temp = rtrim(fgets(STDIN));
$contests[] = array_map('intval', preg_split('/ /', $contests_temp, -1, PREG_SPLIT_NO_EMPTY));
}
$result = luckBalance($k, $contests);
fwrite($fptr, $result . "\n");
fclose($fptr);