結果

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

ソースコード

diff #

#include <iostream>
#include <cstring>
#include <vector>
#include <numeric>

using namespace std;

int main()
{
  int N, tmp, sum;
  vector<int> x;

  cin >> N;
  for(int i=0; i<N; ++i){
    cin >> tmp;
    x.push_back(tmp);
  }
  sum = accumulate(x.begin(), x.end(), 0);

  if(sum%2){
    cout << "impossible" << endl;
    return 0;
  }

  bool dp[N+10][sum+10];
  memset(dp, 0, sizeof(dp));
  dp[0][0] = true;

  for(int i=0; i<N; i++){
    for(int j=0; j<=sum; j++){
      if(dp[i][j] || (j>=x[i] && dp[i][j-x[i]])){
        dp[i+1][j] = true;
      }
    }
  }
  for(int i=0; i<=N; i++){
    if(dp[i][sum/2]){
      cout << "possible" << endl;
      return 0;
    }
  }
  cout << "impossible" << endl;
  return 0;
}
0