int n = in.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i ) { arr[i] = in.nextInt(); } if (arr.length == 1){ System.out.println(arr[0]); continue; } int k = in.nextInt(); int[] dp = new int[arr.length]; Deque<Integer> deque = new LinkedList<>(); for (int i = 0; i < arr.length; i ) { if (!deque.isEmpty() && i - deque.peekFirst() > k) { deque.pollFirst(); } dp[i] = (deque.isEmpty() ? 0 : dp[deque.peekFirst()]) arr[i]; while (!deque.isEmpty() && dp[i] >= dp[deque.peekLast()]) { deque.pollLast(); } deque.offerLast(i); } System.out.println(dp[dp.length-1]);
点赞 2

相关推荐

11-05 07:29
贵州大学 Java
点赞 评论 收藏
分享
牛客网
牛客企业服务