結果

問題 No.50 おもちゃ箱
ユーザー siman
提出日時 2021-06-24 03:29:44
言語 C++17(clang)
(17.0.6 + boost 1.87.0)
結果
AC  
実行時間 74 ms / 5,000 ms
コード長 1,051 bytes
コンパイル時間 3,944 ms
コンパイル使用メモリ 143,060 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-25 03:36:54
合計ジャッジ時間 5,498 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <string.h>
#include <vector>

using namespace std;
typedef long long ll;

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }

  int M;
  cin >> M;
  vector<int> B(M);
  for (int i = 0; i < M; ++i) {
    cin >> B[i];
  }

  sort(A.begin(), A.end());
  sort(B.begin(), B.end());
  reverse(B.begin(), B.end());

  int ans = INT_MAX;

  do {
    int cur = 0;
    int remain = B[0];
    bool ok = true;

    for (int i = 0; i < N; ++i) {
      while (remain < A[i] && cur < M) {
        ++cur;
        remain = B[cur];
      }

      if (cur >= M) {
        ok = false;
        break;
      }

      remain -= A[i];
    }

    if (ok) {
      ans = min(ans, cur + 1);
    }
  } while (next_permutation(A.begin(), A.end()));

  if (ans == INT_MAX) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }

  return 0;
}
0