250412 스티커

Jongleee·2025년 4월 12일
0

TIL

목록 보기
866/970
public static void main(String[] args) throws IOException {
	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	int testCaseCount = Integer.parseInt(br.readLine());

	for (int t = 0; t < testCaseCount; t++) {
		int columnCount = Integer.parseInt(br.readLine());
		int[][] stickers = readStickerArray(br, columnCount);
		int[][] dp = computeMaxStickerScore(stickers, columnCount);
		System.out.println(Math.max(dp[0][columnCount], dp[1][columnCount]));
	}
}

private static int[][] readStickerArray(BufferedReader br, int columnCount) throws IOException {
	int[][] stickers = new int[2][columnCount + 1];
	for (int row = 0; row < 2; row++) {
		StringTokenizer st = new StringTokenizer(br.readLine());
		for (int col = 1; col <= columnCount; col++) {
			stickers[row][col] = Integer.parseInt(st.nextToken());
		}
	}
	return stickers;
}

private static int[][] computeMaxStickerScore(int[][] stickers, int columnCount) {
	int[][] dp = new int[2][columnCount + 1];
	dp[0][1] = stickers[0][1];
	dp[1][1] = stickers[1][1];

	for (int col = 2; col <= columnCount; col++) {
		dp[0][col] = Math.max(dp[1][col - 1], dp[1][col - 2]) + stickers[0][col];
		dp[1][col] = Math.max(dp[0][col - 1], dp[0][col - 2]) + stickers[1][col];
	}

	return dp;
}

출처:https://www.acmicpc.net/problem/9465

0개의 댓글