結果

問題 No.4 おもりと天秤
ユーザー abinull
提出日時 2025-07-14 23:30:55
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,059 bytes
コンパイル時間 3,775 ms
コンパイル使用メモリ 100,840 KB
実行使用メモリ 7,844 KB
最終ジャッジ日時 2025-07-14 23:50:37
合計ジャッジ時間 5,520 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 21 WA * 2
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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;
    for (int a : w ) t += a;

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

    th = t/2;
    int ws = w[0];
    if (ws==th) {cout << "possible" << endl; return 0;}

    stack<int> s;
    for (int i=1;i<n;i++) {
        int m = w[i];
        ws = ws + m;
        for(int j=i+1;j<=n;j++) {
	  
	  if (ws>th) {
	    
	      while (!s.empty()) {
	      int a = s.top();s.pop();
	      ws = ws - a;
	    }
	    ws = ws -m;
	    break;}
	  
           else if (ws==th) {cout << "possible" << endl; return 0;}
           else {ws = ws +  w[j]; s.push(j);}
        }
    }
    cout << "impossible" << endl;
    return 0;
}

/*95 56 56 54 29 24 12 4
   0  1  2  3  4  5  6 7*/
0