编辑代码

def optimize_trip(days, time_limit, scores):
    n = len(days)
    dp = [[0] * (time_limit + 1) for _ in range(n + 1)]

    for i in range(1, n + 1):
        for j in range(1, time_limit + 1):
            if days[i - 1] <= j:
                dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - days[i - 1]] + scores[i - 1])
            else:
                dp[i][j] = dp[i - 1][j]

    result = []
    i, j = n, time_limit
    while i > 0 and j > 0:
        if dp[i][j] != dp[i - 1][j]:
            result.append(i - 1)
            j -= days[i - 1]
        i -= 1

    return result

def main():
    days = [1, 2, 3, 1]
    time_limit = 4
    scores = [7, 8, 9, 6]
    selected_indices = optimize_trip(days, time_limit, scores)
    print("Selected attractions to maximize value:")
    for index in selected_indices:
        print(f"Attraction {index + 1} - Time: {days[index]} day(s), Score: {scores[index]}")

if __name__ == "__main__":
    main()