-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy path小Q的最大总资产.js
55 lines (48 loc) · 1.28 KB
/
小Q的最大总资产.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
let input = ''
process.stdin.on('data', function (data) {
input += String(data)
})
process.stdin.on('end', function () {
main()
})
// 正解应该是 DP: dp[i][j]表示第i天持有j手的最大余额
function main () {
const s = input.split('\n')
const [n, m] = s[0].split(' ').map(Number)
const a = s[1].split(' ').map(Number)
const dp = Array(n + 1).fill(0).map(() => [])
dp[0][0] = m
if (m >= a[0]) dp[0][1] = m - a[0]
for (let i = 1; i < n; i++) {
for (let j = 0; ;j++) {
if (dp[i - 1][j - 1] == null || dp[i - 1][j - 1] - a[i] < 0) break
dp[i][j] = dp[i][j] || 0
// 第 i 天是买入
dp[i][j] = Math.max(dp[i][j], dp[i - 1][j - 1] - a[i])
}
for (let j = 0; ;j++) {
if (dp[i - 1][j + 1] == null || dp[i - 1][j + 1] - a[i] < 0) break
dp[i][j] = dp[i][j] || 0
// 第 i 天是卖出
dp[i][j] = Math.max(dp[i][j], dp[i - 1][j + 1] - a[i])
}
// 不买不卖
for (let j = 0; ;j++) {
if (dp[i - 1][j] == null) break
dp[i][j] = dp[i][j] || 0
dp[i][j] = Math.max(dp[i][j], dp[i - 1][j])
}
}
// console.log(dp)
let ans = 0
for (let j = 0; ;j++) {
if (dp[n - 1][j] == null) break
ans = Math.max(ans, dp[n - 1][j] + j * a[n - 1])
}
console.log(ans)
}
/*
3 100
10 9 8
100
*/