結果

問題 No.4 おもりと天秤
ユーザー DialBird
提出日時 2017-02-11 13:25:05
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 889 bytes
コンパイル時間 631 ms
コンパイル使用メモリ 75,588 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-26 10:09:43
合計ジャッジ時間 1,342 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <sstream>
#include <vector>
#include <array>
#include <string>
#include <algorithm>
#include <map>
#include <cmath>

using namespace std;

#define REP(i,first,last) for (int i=first;i<last;++i)
#define MAX(x,y) (x > y ? x : y)
#define MIN(x,y) (x < y ? x : y)

int N;
int half = 0;
vector<int> weights;
vector<bool> dp(10001, false);

int main(){
  cin >> N;
  int total = 0;
  REP(i,0,N){
    int val;
    cin >> val;
    weights.push_back(val);
    total += val;
  }
  if (total & 1) {
    cout << "impossible" << endl;
  } else {
    half = total >> 1;
    dp[0] = true;

    for (auto w: weights) {
      for (int i=10000-w;i>=0;--i) {
        if (dp[i]) dp[i+w] = true;
        if (dp[half]) break;
      }
      if (dp[half]) break;
    }

    if (dp[half]) {
      cout << "possible" << endl;
    } else {
      cout << "impossible" << endl;
    }
  }
}
0