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

백준 10211

by 신재권 2021. 12. 14.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main10211 {
	
	static int[] D;

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int T = Integer.parseInt(br.readLine());
		while(T-- > 0) {
			int N = Integer.parseInt(br.readLine()); //배열의 크기 N
			int[] X = new int[N];
			StringTokenizer st = new StringTokenizer(br.readLine());
			for(int i=0; i<N; i++) {
				X[i] = Integer.parseInt(st.nextToken());
			}
			System.out.println(maximum(X));
			
		}
		

	}
	
	public static int maximum(int[] X) {
		D = new int[X.length]; 
		for(int i=0; i<X.length; i++) {
			D[i] = X[i]; //연속하지 않은경우 (새로운값으로 갱신) 
			if(i == 0) continue;
			if(D[i] < D[i-1] + X[i]) { //연속하는 경우 , 지금까지 합이랑 더했을때 더 크면 새로 갱신 
				D[i] = D[i-1] + X[i];
			}
		}
		int max = Integer.MIN_VALUE;
		for(int i=0; i<X.length; i++) {
			max = Math.max(max, D[i]);
		}
		
		return max;		
	}

}

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

백준 6236  (0) 2021.12.16
백준 4375  (0) 2021.12.15
백준 2615  (0) 2021.12.13
백준 1269  (0) 2021.12.12
백준 2491  (0) 2021.12.11