第二题记忆化搜索package 秋招笔试.小红书0806.第2题; import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); scanner.nextLine(); long[][] nums = new long[n][3]; int totalTime = scanner.nextInt(), totalEnergy = scanner.nextInt(); scanner.nextLine(); for (int i = 0; i < n; ++i) { nums[i] = new long[]{scanner.nextLong(), scanner.nextLong(), scanner.nextLong()}; scanner.nextLine(); } long[][][] dp = new long[501][501][nums.length]; for (long[][] d : dp) for (long[] arr : d) Arrays.fill(arr, -1); long res = dfs(nums, totalTime, totalEnergy, 0, dp); System.out.println(res); } private static long dfs(long[][] nums, int totalTime, int totalEnergy, int index, long[][][] dp) { if (index == nums.length) return 0; if (dp[totalTime][totalEnergy][index] != -1) return dp[totalTime][totalEnergy][index]; long pass = dfs(nums, totalTime, totalEnergy, index + 1, dp); long choose = 0; if (totalTime >= nums[index][0] && totalEnergy >= nums[index][1]) choose = dfs(nums, (int) (totalTime - nums[index][0]), (int) (totalEnergy - nums[index][1]), index + 1, dp) + nums[index][2]; return dp[totalTime][totalEnergy][index] = Math.max(pass, choose); } }
点赞 评论

相关推荐

牛客网
牛客企业服务