結果

問題 No.4 おもりと天秤
ユーザー kenji_shioyakenji_shioya
提出日時 2016-06-01 19:47:51
言語 Java21
(openjdk 21)
結果
AC  
実行時間 154 ms / 5,000 ms
コード長 907 bytes
コンパイル時間 5,346 ms
コンパイル使用メモリ 74,224 KB
実行使用メモリ 60,380 KB
最終ジャッジ日時 2023-09-08 16:44:59
合計ジャッジ時間 7,516 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 120 ms
55,352 KB
testcase_01 AC 122 ms
55,272 KB
testcase_02 AC 124 ms
55,660 KB
testcase_03 AC 120 ms
55,740 KB
testcase_04 AC 121 ms
55,840 KB
testcase_05 AC 145 ms
58,280 KB
testcase_06 AC 120 ms
55,732 KB
testcase_07 AC 149 ms
58,032 KB
testcase_08 AC 130 ms
55,340 KB
testcase_09 AC 154 ms
60,380 KB
testcase_10 AC 150 ms
58,336 KB
testcase_11 AC 131 ms
55,684 KB
testcase_12 AC 118 ms
55,432 KB
testcase_13 AC 117 ms
55,720 KB
testcase_14 AC 119 ms
55,652 KB
testcase_15 AC 120 ms
55,944 KB
testcase_16 AC 123 ms
55,728 KB
testcase_17 AC 122 ms
55,604 KB
testcase_18 AC 148 ms
57,800 KB
testcase_19 AC 148 ms
58,108 KB
testcase_20 AC 147 ms
58,000 KB
testcase_21 AC 147 ms
57,832 KB
testcase_22 AC 148 ms
58,216 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Exercise44{
  public static void main (String[] args){

    Scanner sc = new Scanner(System.in);

    int n = sc.nextInt();
    int[] weight = new int[n];
    int sum = 0;

    for (int i = 0; i < n; i++){
      int x = sc.nextInt();
      weight[i] = x;
      sum += x;
    }

    if(sum % 2 == 1){
      System.out.println("impossible");
      System.exit(0);
    }

    int[][] dp = new int[n + 1][sum + 1];

    for(int i = 0; i < n + 1; i++){
      for (int j = 1; j < sum + 1; j++){
        dp[i][j] = -1;
      }
    }

    for(int a = 0; a < n; a++){
      for(int b = 0; b < sum; b++){
        if(dp[a][b] >= 0){
          dp[a + 1][b] = dp[a][b];
          dp[a + 1][b + weight[a]] = dp[a][b] + weight[a];
        }
      }
    }

    if (dp[n][sum / 2] >= 0){
      System.out.println("possible");
	   }else{
       System.out.println("impossible");
     }
  }
}
0