結果

問題 No.50 おもちゃ箱
コンテスト
ユーザー koba-e964
提出日時 2015-06-07 17:33:06
言語 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
結果
AC  
実行時間 6 ms / 5,000 ms
コード長 1,233 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 617 ms
コンパイル使用メモリ 112,220 KB
実行使用メモリ 7,720 KB
最終ジャッジ日時 2026-03-12 06:57:48
合計ジャッジ時間 1,665 ms
ジャッジサーバーID
(参考情報)
judge3_1 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)

using namespace std;
typedef long long int ll;
typedef vector<int> VI;
typedef pair<int, int> PI;
const double EPS=1e-9;

const int N = 10;

int n;
int a[N], b[N];

bool rec(int m, int *tbl, int k) {
  if (k >= n) {
    return true;
  }
  REP(i, 0, m) {
    if (tbl[i] >= a[k]) {
      tbl[i] -= a[k];
      bool res = rec(m, tbl, k + 1);
      tbl[i] += a[k];
      if (res) {
	return 1;
      }
    }
  }
  return 0;
}

bool solve(int m) {
  return rec(m, b, 0);
}
int main(void){
  int m;
  cin >> n;
  REP(i, 0, n) {
    cin >> a[i];
  }
  cin >> m;
  REP(i, 0, m) {
    cin >> b[i];
  }
  sort(b, b + m);
  reverse(b, b + m);
  REP (i, 1, m + 1) {
    if (solve(i)) {
      cout << i << endl;
      return 0;
    }
  }
  cout << -1 << endl;
  return 0;
}
0