結果

問題 No.50 おもちゃ箱
ユーザー togatoga
提出日時 2015-09-12 16:20:33
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 80 ms / 5,000 ms
コード長 714 bytes
コンパイル時間 1,584 ms
コンパイル使用メモリ 168,504 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-25 20:58:37
合計ジャッジ時間 3,108 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;


const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

int N,M;
vector<int> A,B;
int main(){
  cin >> N ;
  A.resize(N);
  for (int i = 0; i < N; i++){
    cin >> A[i];
  }
  cin >> M;
    B.resize(M);
  for (int i = 0; i < M; i++){
    cin >> B[i];
  }
  sort(A.begin(), A.end());
  sort(B.rbegin(), B.rend());
  int ans = 810;
  do{
    int pos = 0;
    for (int i = 0; i < M; i++){
      int rest = B[i];
      while (pos < N and rest >= A[pos]){
	rest -= A[pos];
	pos++;
      }
      if (pos >= N){
	ans = min(ans, (i + 1));
	break;
      }
    }
  }while(next_permutation(A.begin(), A.end()));
  
  if (ans >= 810){
    ans = -1;
  }
  cout << ans << endl;
}
0