알고리즘/삼성 SW expert Academy

[SWEA] 8673. 코딩 토너먼트1_JAVA

뇌장하드 2022. 5. 24. 23:03

https://swexpertacademy.com/main/code/problem/problemDetail.do?problemLevel=3&contestProbId=AW2Jldrqlo4DFASu&categoryId=AW2Jldrqlo4DFASu&categoryType=CODE&problemTitle=&orderBy=FIRST_REG_DATETIME&selectCodeLang=ALL&select-1=3&pageSize=10&pageIndex=5 

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

 

 

import java.util.Scanner;
class Solution{
    public static void main(String args[]) throws Exception{
        Scanner sc = new Scanner(System.in);
        int T;
        T=sc.nextInt();
        for(int test_case = 1; test_case <= T; test_case++) {
            int k=sc.nextInt();
            int players[] =new int[1<<k];
            int reult=0;
            //배열 저장
            for(int i=0;i<1<<k;i++) players[i]=sc.nextInt();

            while(k>0){
                int suv[]=new int[1<<(k-1)];

                for(int i=0;i<1<<k;i+=2){
                    suv[i/2]=Math.max(players[i],players[i+1]);
                    reult += Math.abs(players[i]-players[i+1]);
                }
                players=suv;
                k--;
            }
            System.out.println("#"+test_case+" "+reult);

        }
    }
}