Skip to content

Latest commit

Β 

History

History

P1932

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Β 
Β 
Β 
Β 
Β 
Β 

[baekjoon-1932] μ •μˆ˜ μ‚Όκ°ν˜•

image

λ””λ²„κΉ…μš© μ½”λ“œ

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {

    static int N;
    static Node[] nums;

    public static void main(String[] args) throws Exception {
        System.setIn(new FileInputStream("src/DP/P1932/input.txt"));
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        N = Integer.parseInt(br.readLine());

        nums = new Node[N * (N + 1) / 2 + 1];
        nums[0] = new Node(0, 0);

        int count = 1;
        for (int i = 0; i < N; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            for (int j = 0; j <= i; j++) {
                int value = Integer.parseInt(st.nextToken());
                if (j == i) {
                    nums[count] = new Node(value, nums[count - i - 1].sum + value);
                } else if (j == 0) {
                    nums[count] = new Node(value, nums[count - i].sum + value);
                } else {
                    int parent_max = Math.max(nums[count - i - 1].sum, nums[count - i].sum);
                    nums[count] = new Node(value, parent_max + value);
                }
                count ++;
            }
        }
        System.out.println(Arrays.toString(nums));
    }
}

class Node {
    int value;
    int sum;

    public Node(int value, int sum) {
        this.value = value;
        this.sum = sum;
    }

    @Override
    public String toString() {
        return "Node{" +
                "value=" + value +
                ", sum=" + sum +
                '}';
    }
}