|
| 1 | +import java.io.*; |
| 2 | +import java.util.*; |
| 3 | + |
| 4 | +public class JY_20181 { |
| 5 | + |
| 6 | + public static void main(String[] args) throws IOException{ |
| 7 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 8 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 9 | + |
| 10 | + int N = Integer.parseInt(st.nextToken()); |
| 11 | + int K = Integer.parseInt(st.nextToken()); |
| 12 | + |
| 13 | + long[] arr = new long[N+1]; |
| 14 | + st = new StringTokenizer(br.readLine()); |
| 15 | + for(int i=1; i<N+1; i++) { |
| 16 | + arr[i] = Long.parseLong(st.nextToken()); |
| 17 | + } |
| 18 | + |
| 19 | + long[] dp = new long[N+1]; |
| 20 | + int s = 1; |
| 21 | + int e = 1; |
| 22 | + int sum = 0; |
| 23 | + |
| 24 | + while(e <= N) { |
| 25 | + sum += arr[e]; |
| 26 | + dp[e] = dp[e-1]; // 이전 값을 최대값으로 가져옴 |
| 27 | + |
| 28 | + // K보다 작아질 떄까지 s증가 |
| 29 | + while(sum >= K) { |
| 30 | + // dp[s-1]+sum-K : 시작 위치 |
| 31 | + dp[e] = Math.max(dp[e], dp[s-1]+sum-K); |
| 32 | + sum -= arr[s]; |
| 33 | + s++; |
| 34 | + } |
| 35 | + e++; |
| 36 | + } |
| 37 | + System.out.println(dp[N]); |
| 38 | + |
| 39 | + } |
| 40 | + |
| 41 | +} |
0 commit comments