编辑代码

import java.util.ArrayList;
import java.util.List;

public class OptimizeTravel {

    static class TravelInfo {
        String name;
        int time;
        int score;

        public TravelInfo(String name, int time, int score) {
            this.name = name;
            this.time = time;
            this.score = score;
        }
    }

    public static int optimizeTravel(List<TravelInfo> schedule, int totalDays, int totalScore, List<String> selectedSites) {
        int n = schedule.size();
        int[][] dp = new int[n + 1][totalDays + 1];

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= totalDays; j++) {
                int time = schedule.get(i - 1).time;
                int score = schedule.get(i - 1).score;

                if (j >= time) {
                    dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - time] + score);
                } else {
                    dp[i][j] = dp[i - 1][j];
                }
            }
        }

        int i = n;
        int j = totalDays;
        while (i > 0 && j > 0) {
            if (dp[i][j] != dp[i - 1][j]) {
                selectedSites.add(schedule.get(i - 1).name);
                j -= schedule.get(i - 1).time;
            }
            i--;
        }

        return dp[n][totalDays];
    }

    public static void main(String[] args) {
        List<TravelInfo> schedule = new ArrayList<>();
        schedule.add(new TravelInfo("故宫", 1, 7));
        schedule.add(new TravelInfo("颐和园", 2, 8));
        schedule.add(new TravelInfo("长城", 3, 9));
        schedule.add(new TravelInfo("天坛", 1, 6));

        int totalDays = 4;
        int totalScore = 25;

        List<String> selectedSites = new ArrayList<>();
        int result = optimizeTravel(schedule, totalDays, totalScore, selectedSites);

        System.out.println("最大价值: " + result);
        System.out.println("选择的景点: " + selectedSites);
    }
}