编辑代码

public class TravelOptimization {

    public static void main(String[] args) {
        String[] attractions = {"故宫", "颐和园", "长城", "天坛"};
        int[] durations = {1, 2, 3, 1};
        int[] scores = {7, 8, 9, 6};

        int maxScore = getMaxScore(attractions, durations, scores, 4);
        System.out.println("最大累积评分:" + maxScore);
    }

    public static int getMaxScore(String[] attractions, int[] durations, int[] scores, int totalDays) {
        int n = attractions.length;
        int[][] dp = new int[totalDays + 1][n + 1];

        for (int i = 1; i <= totalDays; i++) {
            for (int j = 1; j <= n; j++) {
                dp[i][j] = dp[i][j - 1]; // 不选择当前景点

                if (i >= durations[j - 1]) {
                    int k = i - durations[j - 1];
                    dp[i][j] = Math.max(dp[i][j], dp[k][j - 1] + scores[j - 1]);
                }
            }
        }

        return dp[totalDays][n];
    }
}