結果

問題 No.4 おもりと天秤
ユーザー masaktmasakt
提出日時 2017-04-16 01:43:01
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 1,298 bytes
コンパイル時間 2,911 ms
コンパイル使用メモリ 104,424 KB
実行使用メモリ 25,608 KB
最終ジャッジ日時 2023-09-26 02:03:48
合計ジャッジ時間 5,447 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 58 ms
21,508 KB
testcase_01 AC 55 ms
21,508 KB
testcase_02 AC 58 ms
21,564 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 60 ms
21,640 KB
testcase_07 WA -
testcase_08 AC 56 ms
21,556 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 59 ms
23,560 KB
testcase_12 AC 59 ms
21,524 KB
testcase_13 AC 59 ms
21,400 KB
testcase_14 AC 59 ms
21,572 KB
testcase_15 AC 58 ms
21,432 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 59 ms
23,664 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections.Generic;
using System.Linq;

class P
{
    static int[] w;
    static bool[] dp;
    static bool dfs(int i, int sum, int goal)
    {
        if (dp[i])
        {
            return dp[i];
        }
        bool ret = false;
        if (goal == sum)
        {
            ret = true;
        }
        else if (i == 0)
        {
            ret = false;
        }
        else
        {
            ret = dfs(i - 1, sum, goal) || dfs(i - 1, sum + w[i], goal);
        }
        return dp[i] = ret;
    }
    static void Main(string[] _)
    {
        int n = int.Parse(Console.ReadLine());
        w = new int[n];
        dp = new bool[n];
        _ = Console.ReadLine().Split(' ');
        int sum = 0;
        string ans = "";
        for (int i = 0; i < n; i++)
        {
            w[i] = int.Parse(_[i]);
            sum += w[i];
        }

        if (sum % 2 == 0)
        {
            sum /= 2;
            Array.Sort(w);
            if (sum <= w.Last())
            {
                ans = dfs(n - 1, 0, sum) ? "possible" : "impossible";
            }
            else
            {
                ans = "impossible";
            }
        }
        else
        {
            ans = "impossible";
        }

        Console.WriteLine(ans);
    }
}
0