Solution of P10987 [蓝桥杯 2023 国 Python A] 火车运输
你可以浏览这个题解的中文版本。
Template Problem.
Consider the plain dp state
Note that the last two expressions are required to ensure that
The answer is then
Finding that space blows up doing this, consider using a rolling array to squash the first dimension.
Time complexity
line = list(map(int, input().split()))
# print(line)
n = line[0]
a = line[1]
b = line[2]
w = [0] + list(map(int, input().split()))
# print(w)
f = [[[0 for _ in range(b + 1)] for _ in range(a + 1)] for _ in range(2)]
# print(f)
for i in range(1, n + 1):
p = i % 2
q = not p
for x in range(0, a + 1):
for y in range(0, b + 1):
# print(i, x, y)
f[p][x][y] = f[q][x][y]
if(x >= w[i]):
f[p][x][y] = max(f[p][x][y], f[q][x - w[i]][y] + w[i])
if(y >= w[i]):
f[p][x][y] = max(f[p][x][y], f[q][x][y - w[i]] + w[i])
print(f[n % 2][a][b])