結果

問題 No.4 おもりと天秤
コンテスト
ユーザー imulan
提出日時 2015-12-16 12:30:19
言語 C++11
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=gnu++11 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 757 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 517 ms
コンパイル使用メモリ 83,028 KB
実行使用メモリ 12,032 KB
最終ジャッジ日時 2026-04-05 10:57:29
合計ジャッジ時間 7,107 ms
ジャッジサーバーID
(参考情報)
judge1_1 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 3 WA * 2 TLE * 1 -- * 17
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;

int n;
const int N=100;
vector<int> w(N);
int sum=0;

int dp[101][5001];
//x番目に注目していて、現在の重さはy
int rec(int x, int y){
  if(x==n) return (y==sum/2);
  if(dp[x][y]>=0) return dp[x][y];

  int ret;
  if(y+w[x]>sum/2) ret = rec(x+1,y);
  else ret = rec(x+1,y) || rec(x+1, y+w[x]);
  return ret;
}

int main(int argc, char const *argv[]) {
  cin >>n;
  for(int i=0; i<n; ++i){
    scanf(" %d", &w[i]);
    sum += w[i];
  }

  memset(dp, -1, sizeof(dp));

  if(sum%2==0){
    int ret=rec(0, 0);
    if(ret==1) printf("possible\n");
    else printf("impposible\n");
  }
  else printf("impposible\n");
  return 0;
}
0