-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.js
34 lines (32 loc) · 930 Bytes
/
solution.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
const filePath = require('path').join(__dirname, 'input');
const [num, ...arr] = require('fs')
.readFileSync(filePath)
.toString()
.trim()
.split('\n');
function soluton (n, arr) {
const recursive = (depth, sum, maxPoint) => {
if (n <= depth) {
return sum;
}
for (let i = depth; i < n; i ++) {
if (i + arr[i][0] <= n) sum += arr[i][1];
maxPoint = Math.max(maxPoint, recursive(i + arr[i][0], sum, maxPoint));
if (i + arr[i][0] <= n) sum -= arr[i][1];
}
return maxPoint;
}
return recursive(0, 0, 0);
}
// console.log(soluton(+num, arr.map(r => r.split(' ').map(Number))));
function run (input) {
const [num, ...arr] = input
.toString()
.trim()
.split('\n')
.map(r => r.trim());
return soluton(+num, arr.map(r => r.split(' ').map(Number)));
}
module.exports = {
run
}