結果
| 問題 | No.4 おもりと天秤 | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2023-02-10 18:02:39 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 4 ms / 5,000 ms | 
| コード長 | 699 bytes | 
| コンパイル時間 | 746 ms | 
| コンパイル使用メモリ | 75,528 KB | 
| 最終ジャッジ日時 | 2025-02-10 11:57:16 | 
| ジャッジサーバーID (参考情報) | judge2 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 23 | 
ソースコード
#include <iostream>
#include <vector>
bool solve(int N, std::vector<int> W) {
    int S = 0;
    for (int i = 0; i < N; i++) {
        S += W[i];
    }
    if (S % 2 == 1) {
        return false;
    }
    int M = S / 2;
    std::vector<bool> dp(M + 1, false);
    dp.at(0) = true;
    for (const auto &w: W) {
        for (int x = M - w; x >= 0; x--) {
            if (dp.at(x)) {
                dp.at(x + w) = true;
            }
        }
    }
    return dp.at(M);
}
int main() {
    int N;
    std::cin >> N;
    std::vector<int> W(N);
    for (int i = 0; i < N; i++) { std::cin >> W[i]; }
    std::string ans = solve(N, W) ? "possible" : "impossible";
    std::cout << ans << std::endl;
}
            
            
            
        