結果

問題 No.4 おもりと天秤
ユーザー drymouse
提出日時 2024-02-14 23:06:02
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,003 bytes
コンパイル時間 740 ms
コンパイル使用メモリ 73,144 KB
最終ジャッジ日時 2025-02-19 13:12:47
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 10 WA * 13
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
using namespace std;

//動的計画法を用いた解法
int main(void) {
    int N;
    cin >> N;
    int W[N];
    int sum = 0;
    for (int i = 0; i < N; i++) {
        cin >> W[i];
        sum += W[i];
    }
    //cout << "sum: " << sum << endl;
    if (sum % 2) {
        cout << "impossible" << endl;
    } else {
        sort(W, W + N, greater<int>());
        int tar = sum / 2;
        bool possible[N+1][sum / 2 + 1] {false};
        possible[0][0] = true;
        for (int i = 1; i < N; i++) {
            for (int j = 0; j < tar + 1; j++) {
                if (possible[i-1][j]) {
                    possible[i][j] = true;
                    if (j + W[i-1] <= tar) {
                        possible[i][j+W[i-1]] = true;
                    }
                }
            }
        }
        if (possible[N][tar]) {
            cout << "possible" << endl;
        } else {
            cout << "impossible" << endl;
        }
    }
    return 0;
}
0