• Home
  • About
    • Ryureka Moment photo

      Ryureka

      Sin Prisa, Sin Pausa

    • About Me
    • Facebook
    • Github
    • Youtube
  • Projects
  • Posts
    • Posts
    • ProblemSolvings
    • Tags
    • Blog
    • Examples
  • ProblemSolving
    • ProblemSolving
    • BOJ
    • Programmers
    • SWEA
    • LeetCode
  • FrontEnd
    • FrontEnd
    • HTML
  • BackEnd
    • BackEnd
    • Server
      • Server
      • Spring
      • NodeJS
    • DataBase
      • DataBase
      • MySQL
      • MongoDB
  • Programming
    • Programming
    • Java
    • JS
    • Python
    • CleanCode
  • ComputerScience
    • DataStructure
    • Algorithm

[BOJ] 9465. 스티커

15 Apr 2026

Reading time ~1 minute

  • 풀이

풀이

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int T = sc.nextInt();
		int[][] arr;
		long[][] dp;
		for(int t = 0; t < T; t++) {
			int N = sc.nextInt();
			arr = new int [2][N];
			dp = new long[N+1][2];
			
			for(int i = 0; i < 2; i++) {
				for(int j = 0; j < N; j++) {
					arr[i][j] = sc.nextInt();
				}
			}
			
			dp[1][0] = arr[0][0];
			dp[1][1] = arr[1][0];
			
			if(N >= 2) {
				dp[2][0] = arr[1][0]+arr[0][1];
				dp[2][1] = arr[0][0]+arr[1][1];
			}
			
			for(int i = 3; i <= N; i++) {
				dp[i][0] = Math.max(dp[i-1][1], Math.max(dp[i-2][0],dp[i-2][1]))+arr[0][i-1];
				dp[i][1] = Math.max(dp[i-1][0], Math.max(dp[i-2][0],dp[i-2][1]))+arr[1][i-1];
			}
			
			System.out.println(Math.max(dp[N][0], dp[N][1]));			
		}
	}
}


DP Share