結果

問題 No.519 アイドルユニット
ユーザー tentententen
提出日時 2023-08-10 16:24:29
言語 Java21
(openjdk 21)
結果
AC  
実行時間 154 ms / 1,000 ms
コード長 2,345 bytes
コンパイル時間 2,171 ms
コンパイル使用メモリ 91,876 KB
実行使用メモリ 105,988 KB
最終ジャッジ日時 2024-04-28 09:38:08
合計ジャッジ時間 5,692 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 134 ms
105,704 KB
testcase_01 AC 135 ms
105,920 KB
testcase_02 AC 93 ms
56,556 KB
testcase_03 AC 54 ms
38,248 KB
testcase_04 AC 46 ms
37,036 KB
testcase_05 AC 43 ms
36,968 KB
testcase_06 AC 42 ms
37,032 KB
testcase_07 AC 42 ms
37,196 KB
testcase_08 AC 43 ms
37,008 KB
testcase_09 AC 44 ms
36,708 KB
testcase_10 AC 46 ms
37,344 KB
testcase_11 AC 53 ms
37,976 KB
testcase_12 AC 44 ms
37,076 KB
testcase_13 AC 54 ms
38,060 KB
testcase_14 AC 56 ms
38,304 KB
testcase_15 AC 53 ms
38,084 KB
testcase_16 AC 53 ms
37,996 KB
testcase_17 AC 56 ms
38,076 KB
testcase_18 AC 53 ms
37,896 KB
testcase_19 AC 53 ms
38,232 KB
testcase_20 AC 56 ms
38,260 KB
testcase_21 AC 55 ms
38,164 KB
testcase_22 AC 54 ms
38,324 KB
testcase_23 AC 54 ms
38,292 KB
testcase_24 AC 85 ms
42,372 KB
testcase_25 AC 85 ms
43,004 KB
testcase_26 AC 81 ms
42,340 KB
testcase_27 AC 81 ms
42,444 KB
testcase_28 AC 84 ms
42,640 KB
testcase_29 AC 138 ms
105,792 KB
testcase_30 AC 154 ms
105,988 KB
testcase_31 AC 134 ms
105,828 KB
testcase_32 AC 137 ms
105,560 KB
testcase_33 AC 47 ms
37,196 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static int[] dp;
    static int[][] matrix;
    static int n;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        n = sc.nextInt();
        matrix = new int[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                matrix[i][j] = sc.nextInt();
            }
        }
        dp = new int[1 << n];
        Arrays.fill(dp, -1);
        dp[0] = 0;
        System.out.println(dfw((1 << n) - 1));
    }
    
    static int dfw(int x) {
        if (dp[x] < 0) {
            for (int i = 0; i < n; i++) {
                if ((x & (1 << i)) == 0) {
                    continue;
                }
                for (int j = i + 1; j < n; j++) {
                    if ((x & (1 << j)) == 0) {
                        continue;
                    }
                    dp[x] = Math.max(dp[x], dfw(x - (1 << i) ^ (1 << j)) + matrix[i][j]);
                }
                break;
            }
        }
        return dp[x];
    }
    
} 
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0