编辑代码

def optimize_travel(time, score, days):
    n = len(score)
    dp = [[0] * (days+1) for _ in range(n+1)]

    for i in range(1, n+1):
        for j in range(1, days+1):
            if j >= time[i-1]:
                dp[i][j] = max(dp[i-1][j], dp[i-1][j-time[i-1]] + score[i-1])
            else:
                dp[i][j] = dp[i-1][j]

    # 找出能够获得最大价值的景点
    selected_spots = []
    j = days
    for i in range(n, 0, -1):
        if dp[i][j] > dp[i-1][j]:
            selected_spots.append(i)
            j -= time[i-1]

    return selected_spots

spots = ['故宫', '颐和园', '长城', '天坛']
time = [1, 2, 3, 1]
score = [7, 8, 9, 6]
days = 4

selected_spots = optimize_travel(time, score, days)

print("去哪些景点能够获得最大价值:")
for spot in selected_spots:
    print(spots[spot-1])