def max_score_with_path(days, scores, names):
n = len(days) # 景点的数量
m = 4 # 总共的天数
dp = [[0] * (m+1) for _ in range(n+1)] # 创建二维数组dp
path = [[[] for _ in range(m+1)] for _ in range(n+1)] # 用于记录路径
for i in range(1, n+1):
for j in range(1, m+1):
if days[i-1] <= j: # 如果当前景点所需天数小于等于j
if dp[i-1][j] < dp[i-1][j-days[i-1]] + scores[i-1]:
dp[i][j] = dp[i-1][j-days[i-1]] + scores[i-1]
path[i][j] = path[i-1][j-days[i-1]] + [i] # 记录路径
else:
dp[i][j] = dp[i-1][j]
path[i][j] = path[i-1][j]
else: # 如果当前景点所需天数大于j
dp[i][j] = dp[i-1][j]
path[i][j] = path[i-1][j]
max_score = dp[n][m]
max_score_path = [names[i-1] for i in path[n][m]] # 将索引转换为景点名称
return max_score, max_score_path
# 主函数进行测试
def main():
days = [1, 2, 3, 1]
scores = [7, 8, 9, 6]
names = ["故宫", "颐和园", "长城", "天坛"]
maxScore, maxScorePath = max_score_with_path(days, scores, names)
print("能够获得的最大评分为:", maxScore)
print("选择的景点为:", maxScorePath)
if __name__ == "__main__":
main()