Submission #70771363


Source Code Expand

import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int n = Integer.parseInt(st.nextToken());
        
        int[] weight = new int[n];
        long[] headHappiness = new long[n];
        long[] bodyHappiness = new long[n];
        
        for (int i = 0; i < n; i++) {
            st = new StringTokenizer(br.readLine());
            weight[i] = Integer.parseInt(st.nextToken());
            headHappiness[i] = Long.parseLong(st.nextToken());
            bodyHappiness[i] = Long.parseLong(st.nextToken());
        }
        
        System.out.println(solve(n, weight, headHappiness, bodyHappiness));
    }
    
    public static long solve(int n, int[] weight, long[] headHappiness, long[] bodyHappiness) {
        int totalWeight = 0;
        for (int w : weight) {
            totalWeight += w;
        }
        
        int maxHeadWeight = totalWeight / 2;
        
        long[] dp = new long[maxHeadWeight + 1];
        long[] newDp = new long[maxHeadWeight + 1];
        
        Arrays.fill(dp, Long.MIN_VALUE);
        dp[0] = 0;
        
        for (int i = 0; i < n; i++) {
            Arrays.fill(newDp, Long.MIN_VALUE);
            
            for (int j = 0; j <= maxHeadWeight; j++) {
                if (dp[j] == Long.MIN_VALUE) continue;
                
                // Option 1: Attach to body
                newDp[j] = Math.max(newDp[j], dp[j] + bodyHappiness[i]);
                
                if (j + weight[i] <= maxHeadWeight) {
                    newDp[j + weight[i]] = Math.max(
                        newDp[j + weight[i]], 
                        dp[j] + headHappiness[i]
                    );
                }
            }
            
            long[] temp = dp;
            dp = newDp;
            newDp = temp;
        }
        
        long maxHappiness = 0;
        for (int j = 0; j <= maxHeadWeight; j++) {
            if (dp[j] != Long.MIN_VALUE) {
                maxHappiness = Math.max(maxHappiness, dp[j]);
            }
        }
        
        return maxHappiness;
    }
}

Submission Info

Submission Time
Task D - Robot Customize
User addy
Language Java24 (OpenJDK 24.0.2)
Score 400
Code Size 2334 Byte
Status AC
Exec Time 134 ms
Memory 42048 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 4
AC × 54
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 01_random_24.txt, 01_random_25.txt, 01_random_26.txt, 01_random_27.txt, 01_random_28.txt, 01_random_29.txt, 01_random_30.txt, 01_random_31.txt, 01_random_32.txt, 01_random_33.txt, 01_random_34.txt, 01_random_35.txt, 01_random_36.txt, 01_random_37.txt, 01_random_38.txt, 01_random_39.txt, 01_random_40.txt, 01_random_41.txt, 01_random_42.txt, 01_random_43.txt, 01_random_44.txt, 01_random_45.txt, 01_random_46.txt, 01_random_47.txt, 01_random_48.txt, 01_random_49.txt, 01_random_50.txt, 01_random_51.txt, 01_random_52.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 40 ms 38120 KiB
00_sample_01.txt AC 35 ms 37964 KiB
00_sample_02.txt AC 33 ms 38052 KiB
00_sample_03.txt AC 37 ms 38028 KiB
01_random_03.txt AC 121 ms 41132 KiB
01_random_04.txt AC 119 ms 41080 KiB
01_random_05.txt AC 125 ms 41264 KiB
01_random_06.txt AC 126 ms 41144 KiB
01_random_07.txt AC 126 ms 41176 KiB
01_random_08.txt AC 126 ms 41216 KiB
01_random_09.txt AC 128 ms 41096 KiB
01_random_10.txt AC 118 ms 41008 KiB
01_random_11.txt AC 125 ms 40888 KiB
01_random_12.txt AC 107 ms 41008 KiB
01_random_13.txt AC 82 ms 40252 KiB
01_random_14.txt AC 79 ms 40352 KiB
01_random_15.txt AC 91 ms 40436 KiB
01_random_16.txt AC 76 ms 40216 KiB
01_random_17.txt AC 127 ms 41216 KiB
01_random_18.txt AC 131 ms 41260 KiB
01_random_19.txt AC 133 ms 41016 KiB
01_random_20.txt AC 131 ms 41104 KiB
01_random_21.txt AC 130 ms 41084 KiB
01_random_22.txt AC 83 ms 40320 KiB
01_random_23.txt AC 42 ms 38308 KiB
01_random_24.txt AC 38 ms 38004 KiB
01_random_25.txt AC 130 ms 41024 KiB
01_random_26.txt AC 130 ms 41016 KiB
01_random_27.txt AC 130 ms 41024 KiB
01_random_28.txt AC 129 ms 41144 KiB
01_random_29.txt AC 134 ms 41184 KiB
01_random_30.txt AC 114 ms 41260 KiB
01_random_31.txt AC 116 ms 40988 KiB
01_random_32.txt AC 92 ms 40576 KiB
01_random_33.txt AC 37 ms 37876 KiB
01_random_34.txt AC 37 ms 37952 KiB
01_random_35.txt AC 128 ms 41116 KiB
01_random_36.txt AC 133 ms 41120 KiB
01_random_37.txt AC 131 ms 41092 KiB
01_random_38.txt AC 115 ms 40876 KiB
01_random_39.txt AC 119 ms 41192 KiB
01_random_40.txt AC 125 ms 41116 KiB
01_random_41.txt AC 131 ms 41220 KiB
01_random_42.txt AC 128 ms 41108 KiB
01_random_43.txt AC 94 ms 40724 KiB
01_random_44.txt AC 109 ms 40832 KiB
01_random_45.txt AC 110 ms 40960 KiB
01_random_46.txt AC 72 ms 39776 KiB
01_random_47.txt AC 72 ms 40288 KiB
01_random_48.txt AC 116 ms 42016 KiB
01_random_49.txt AC 116 ms 42048 KiB
01_random_50.txt AC 127 ms 41996 KiB
01_random_51.txt AC 59 ms 39012 KiB
01_random_52.txt AC 83 ms 40692 KiB