编辑代码

public class TravelOptimization {
    public static void main(String[] args) {
        String[] places = {"故宫", "颐和园", "长城", "天坛"};
        int[] time = {1, 2, 3, 1};
        int[] score = {7, 8, 9, 6};
        int totalDays = 4;

        int maxScore = getMaxScore(places, time, score, totalDays);
        System.out.println("最大评分为:" + maxScore);
    }

    public static int getMaxScore(String[] places, int[] time, int[] score, int totalDays) {
        int n = places.length;
        int[][] dp = new int[n + 1][totalDays + 1];

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= totalDays; j++) {
                if (time[i - 1] <= j) {
                    dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - time[i - 1]] + score[i - 1]);
                } else {
                    dp[i][j] = dp[i - 1][j];
                }
            }
        }

        return dp[n][totalDays];
    }
}