-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path158-exploring-strings.js
More file actions
73 lines (55 loc) · 1.46 KB
/
158-exploring-strings.js
File metadata and controls
73 lines (55 loc) · 1.46 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
/**
* Exploring Strings
* Time Complexity: O(N^2)
* Space Complexity: O(N)
*/
function processData(input) {
const lines = input.trim().split("\n");
if (lines.length < 2) return;
const firstLine = lines[0].trim().split(/\s+/);
const N = parseInt(firstLine[0], 10);
const q = parseInt(firstLine[1], 10);
const queries = lines[1].trim().split(/\s+/).map(Number);
const max_p = new Array(N).fill(0n);
let prevRow = [1n];
let combinations = BigInt(N);
max_p[0] = combinations;
for (let n = 2; n <= N; n++) {
const currentRow = [];
combinations = (combinations * BigInt(N - n + 1)) / BigInt(n);
for (let m = 0; m < n; m++) {
let term1 = 0n;
let term2 = 0n;
if (m < prevRow.length) {
term1 = BigInt(m + 1) * prevRow[m];
}
if (m - 1 >= 0 && m - 1 < prevRow.length) {
term2 = BigInt(n - m) * prevRow[m - 1];
}
const Anm = term1 + term2;
currentRow.push(Anm);
const p_nm = combinations * Anm;
if (p_nm > max_p[m]) {
max_p[m] = p_nm;
}
}
prevRow = currentRow;
}
let totalSum = 0n;
for (let i = 0; i < q; i++) {
const m = queries[i];
if (m < max_p.length) {
totalSum += max_p[m];
}
}
console.log(totalSum.toString());
};
process.stdin.resume();
process.stdin.setEncoding("ascii");
let _input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});