結果
| 問題 |
No.4 おもりと天秤
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2017-02-03 09:57:24 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 3 ms / 5,000 ms |
| コード長 | 1,025 bytes |
| コンパイル時間 | 567 ms |
| コンパイル使用メモリ | 69,024 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-06-26 10:08:59 |
| 合計ジャッジ時間 | 1,321 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 23 |
ソースコード
#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++)
int N;
int half = 0;
vector<int> weights;
int dp[101][10001] = {};
bool solve(int idx, int total){
if (dp[idx][total] == 1) return false;
if (idx >= N || total > half) {
//ここのtotalは10000を超えないため大丈夫
dp[idx][total] = 1;
return false;
}
int new_total = total + weights[idx];
if (new_total == half) return true;
if (solve(idx+1, total) || solve(idx+1, new_total)) return true;
dp[idx][total] = 1;
return false;
}
int main(){
cin >> N;
int val;
int total = 0;
REP(i,0,N) {
cin >> val;
weights.push_back(val);
total += val;
}
if (total & 1) {
cout << "impossible" << endl;
} else {
half = total >> 1;
sort(weights.begin(), weights.end());
if (solve(0,0)) {
cout << "possible" << endl;
} else {
cout << "impossible" << endl;
}
}
}