第二题 能不能看一下为啥只通过一半 public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] A = new int[n]; int[] B = new int[n]; for(int i = 0; i < n; i++){ A[i] = sc.nextInt(); } for(int i = 0; i < n; i++){ B[i] = sc.nextInt(); } int[][] dp = new int[n][3]; dp[0][0] = 0; dp[0][1] = A[0]; dp[0][2] = B[0]; for(int i = 1; i < n; i++){ dp[i][0] = Math.max(dp[i - 1][1], dp[i - 1][2]); dp[i][1] = Math.max(dp[i - 1][0], dp[i - 1][2]) + A[i]; dp[i][2] = Math.max(dp[i - 1][0], dp[i - 1][1]) + B[i]; } int max = Math.max(dp[n - 1][1], dp[n - 1][2]); System.out.println(max); }
点赞 1

相关推荐

牛客网
牛客企业服务