結果

問題 No.4 おもりと天秤
ユーザー くわいくわい
提出日時 2015-11-05 21:52:49
言語 Scala(Beta)
(3.4.0)
結果
AC  
実行時間 1,095 ms / 5,000 ms
コード長 635 bytes
コンパイル時間 9,013 ms
コンパイル使用メモリ 259,292 KB
実行使用メモリ 63,692 KB
最終ジャッジ日時 2023-09-11 14:55:35
合計ジャッジ時間 33,649 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 984 ms
63,500 KB
testcase_01 AC 939 ms
62,956 KB
testcase_02 AC 995 ms
63,456 KB
testcase_03 AC 994 ms
63,196 KB
testcase_04 AC 994 ms
63,452 KB
testcase_05 AC 1,084 ms
63,552 KB
testcase_06 AC 993 ms
63,168 KB
testcase_07 AC 1,073 ms
63,532 KB
testcase_08 AC 971 ms
62,760 KB
testcase_09 AC 1,026 ms
63,360 KB
testcase_10 AC 1,067 ms
63,364 KB
testcase_11 AC 997 ms
63,352 KB
testcase_12 AC 1,001 ms
63,236 KB
testcase_13 AC 1,008 ms
63,356 KB
testcase_14 AC 988 ms
63,308 KB
testcase_15 AC 988 ms
63,320 KB
testcase_16 AC 989 ms
63,224 KB
testcase_17 AC 987 ms
63,340 KB
testcase_18 AC 1,047 ms
63,660 KB
testcase_19 AC 1,095 ms
63,536 KB
testcase_20 AC 1,092 ms
63,364 KB
testcase_21 AC 1,068 ms
63,692 KB
testcase_22 AC 1,074 ms
63,488 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner

object Problem004 {

  def proc(n: Int, weight: Seq[Int]): String = {
    if (weight.sum % 2 != 0) {
      return "impossible"
    }

    val target = weight.sum / 2
    val dp = Array.fill(target + 1)(false)
    dp(0) = true

    for (w <- weight) {
      for (d <- target - w to 0 by -1 if dp(d)) {
        dp(d + w) = true
      }
    }

    if (dp(target)) "possible" else "impossible"
  }

  def main(args: Array[String]): Unit = {
    val sc = new Scanner(System.in)
    val n = sc.nextInt()
    val weight = Seq.fill(n)(sc.nextInt())

    val result: String = proc(n, weight)
    println(result)
  }
}
0