본문 바로가기
알고리즘 & 자료구조/백준

백준 2559_1

by 신재권 2022. 2. 4.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main2559_1 {

	public static void main(String[] args) throws IOException {
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		StringTokenizer st = new StringTokenizer(br.readLine());
		int N = Integer.parseInt(st.nextToken()); // 날짜의 수  N
		int K = Integer.parseInt(st.nextToken()); // 합을구하기위한 연속적인 날짜의 수 K
		
		int[] t = new int[N+1]; 
		st = new StringTokenizer(br.readLine());

		int[] sums = new int[N+1];
		
		for(int i=1; i<=N; i++) {
			t[i] = Integer.parseInt(st.nextToken());
			sums[i] = sums[i-1] + t[i];
		}
		
		int max = Integer.MIN_VALUE;
		
		for(int i=K; i<=N; i++) {
			//sums[i] - sums[i-K] = (i-K) ~ i 까지의 합 
			max = Math.max(max, sums[i] - sums[i-K]);
		}
		

		System.out.println(max);
		
		
	}
	
}

 

'알고리즘 & 자료구조 > 백준' 카테고리의 다른 글

백준 9375  (0) 2022.02.06
백준 1620  (0) 2022.02.05
백준 9996  (0) 2022.02.03
백준 11655  (0) 2022.02.02
백준 10988_1  (0) 2022.02.01