結果

問題 No.4 おもりと天秤
ユーザー nemunemu
提出日時 2024-01-13 16:11:22
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 5 ms / 5,000 ms
コード長 709 bytes
コンパイル時間 858 ms
コンパイル使用メモリ 75,892 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-09-28 01:32:26
合計ジャッジ時間 1,761 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

// No.4 おもりと天秤
#include <iostream>
#include <vector>
using namespace std;

int main() {
    int N;
    cin >> N;
    vector<int> W(N);
    int s = 0;
    for (int i = 0; i < N; ++i) cin >> W[i], s += W[i];
    if (s % 2) {
        cout << "impossible" << endl;
        return 0;
    }
    vector<vector<bool>> dp(N + 1, vector<bool>(s + 1, false));
    dp[0][0] = true;
    for (int i = 0; i < N; ++i) {
        for (int j = 0; j <= s; ++j) {
            dp[i + 1][j] = dp[i][j];
            if (j - W[i] >= 0) {
                dp[i + 1][j] = dp[i + 1][j] || dp[i][j - W[i]];
            }
        }
    }
    if (dp[N][s / 2]) cout << "possible" << endl;
    else cout << "impossible" << endl;
}
0