結果

問題 No.4 おもりと天秤
ユーザー DialBird
提出日時 2016-12-10 06:17:01
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 1,139 bytes
コンパイル時間 689 ms
コンパイル使用メモリ 69,020 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-26 10:04:38
合計ジャッジ時間 1,402 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using namespace std;

#define REP(i,first,last) for (int i=first;i<last;i++)
#define FOR(it,con) for (auto it=con.begin();it!=con.end();it++)

int N;
int half;
vector<int> w_list;
int dp[101][10001] = {};

bool solve(int idx, int total){
  if (dp[idx][total] == 1) {
    return false;
  } else if (dp[idx][total] == 2) {
    return true;
  }
  if (idx >= N || total > half) {
    dp[idx][total] = 1;
    return false;
  }
  int new_total = total + w_list[idx];
  if (new_total == half) {
    dp[idx][total] = 2;
    return true;
  }
  if (solve(idx + 1, new_total) || solve(idx + 1, total)) {
    dp[idx][total] = 2;
    return true;
  }
  dp[idx][total] = 1;
  return false;
}

int main(){
  cin >> N;
  int val;
  int total = 0;
  REP(i,0,N){
    cin >> val;
    w_list.push_back(val);
    total += val;
  }
  sort(w_list.begin(), w_list.end());
  if (total & 1) {
    cout<<"impossible"<<endl;
  } else {
    half = total >> 1;
    if (solve(0, 0)) {
      cout<<"possible"<<endl;
    } else {
      cout<<"impossible"<<endl;
    }
  }
}
0