結果

問題 No.4 おもりと天秤
ユーザー くわい
提出日時 2015-11-05 21:52:49
言語 Scala(Beta)
(3.6.2)
結果
AC  
実行時間 1,050 ms / 5,000 ms
コード長 635 bytes
コンパイル時間 8,618 ms
コンパイル使用メモリ 268,460 KB
実行使用メモリ 65,916 KB
最終ジャッジ日時 2024-06-29 05:05:38
合計ジャッジ時間 33,402 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

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