-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path455A-Boredom.js
More file actions
54 lines (48 loc) · 1.09 KB
/
455A-Boredom.js
File metadata and controls
54 lines (48 loc) · 1.09 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
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const input = [];
rl.on("line", (line) => {
input.push(line);
});
rl.on("close", () => {
const str = input[1]
let pre = ''
const cnt = {}
let max = -Infinity
for(let i= 0, n = str.length; i < n; i++) {
if (str[i] === ' ') {
if (pre) {
cnt[pre] = (cnt[pre] || 0) + 1
if (+pre > max) max = +pre
pre = ''
}
} else {
pre += str[i]
}
}
if (pre) {
cnt[pre] = (cnt[pre] || 0) + 1
if (+pre > max) max = +pre
pre = ''
}
const v = []
for(let k in cnt) {
if (cnt.hasOwnProperty(k)) {
v.push([+k, cnt[k]])
}
}
v.sort((a, b) => a[0] - b[0])
const f = Array(111111).fill(0)
for (let i = 0; i < v.length; i++) {
let pr = i - 1;
if (pr >= 0 && v[pr][0] + 1 === v[i][0]) pr--;
if (pr === -1) f[i] = 1 * v[i][0] * v[i][1];
else f[i] = f[pr] + 1 * v[i][0] * v[i][1];
if (i !== 0) f[i] = Math.max(f[i], f[i - 1]);
}
const res = f[v.length - 1]
console.log(res)
});