結果

問題 No.4 おもりと天秤
ユーザー takeya_okinotakeya_okino
提出日時 2017-06-12 11:58:51
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 738 bytes
コンパイル時間 1,985 ms
コンパイル使用メモリ 77,272 KB
実行使用メモリ 66,764 KB
最終ジャッジ日時 2023-10-24 21:31:05
合計ジャッジ時間 6,014 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 126 ms
57,408 KB
testcase_02 AC 131 ms
59,532 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 112 ms
56,192 KB
testcase_07 WA -
testcase_08 AC 157 ms
66,628 KB
testcase_09 AC 154 ms
66,260 KB
testcase_10 WA -
testcase_11 AC 132 ms
57,680 KB
testcase_12 AC 124 ms
57,368 KB
testcase_13 AC 122 ms
57,448 KB
testcase_14 AC 123 ms
57,232 KB
testcase_15 AC 121 ms
57,204 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 152 ms
66,536 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int N = sc.nextInt();
    int[] w = new int[N];
    for(int i = 0; i < N; i++) w[i] = sc.nextInt();
    // dp[i][j]は重りiまでで和をj-10000と出来るかを表す
    int[][] dp = new int[N][20001];
    dp[0][w[0] + 10000] = 1;
    dp[0][-w[0] + 10000] = 1;
    for(int i = 1; i < N; i++) {
      for(int j = 0; j < 20001; j++) {
        if((j + w[i] < 20001) && (dp[i - 1][j + w[i]] == 1)) dp[i][j] = 1;
        if((j - w[i] >= 0) && (dp[i - 1][j - w[i]] == 1)) dp[i][j] = 1;
      }
    }
    String ans = "impossible";
    if(dp[N - 1][0] == 1) ans = "possible";
    System.out.println(ans);
  }
}
0