結果

問題 No.4 おもりと天秤
ユーザー abinull
提出日時 2025-08-03 21:49:43
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 875 bytes
コンパイル時間 908 ms
コンパイル使用メモリ 98,636 KB
実行使用メモリ 7,720 KB
最終ジャッジ日時 2025-08-03 21:49:45
合計ジャッジ時間 2,253 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 19 WA * 4
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>

using namespace std;

int main() {
    int n;
    cin >> n;

    vector<int> w(n);
    for (int i=0;i<n;i++) cin >> w[i];

    sort(w.begin(), w.end(), greater<int>());
    int t = 0;
    int th =0;
    for (int a : w ) t += a;

   if (t%2!=0) {
        cout << "impossible" << endl;
        return 0;
    }

    th = t/2;

    vector<vector<int>> m(n, vector<int>(th+1, 0));

    int i, j;

    for (j=0;j<=th;j++) {
        if (j<w[0]) continue;
        else m[0][j] = w[0];
    }

    for (i=1;i<n;i++) {
        for (j=0;j<=th;j++) {
            
            if (j<m[i-1][j] + w[i]) {m[i][j] = m[i-1][j];}
            else {m[i][j] = m[i-1][j] + w[i];}

            if (m[i][j]==th) {cout << "possible" << endl; return 0;}
        }
    }

    cout << "impossible" << endl;
    return 0;
}
0