fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long int ll;
  5. #define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  6.  
  7. #include <ext/pb_ds/assoc_container.hpp>
  8. #include <ext/pb_ds/tree_policy.hpp>
  9. using namespace __gnu_pbds;
  10.  
  11. typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>order_set;
  12. typedef pair<int, int>pr;
  13. #define all(i) i.begin() , i.end()
  14. #define ft first
  15. #define sn second
  16. #define pb push_back
  17. #define totalone(mask) __builtin_popcount(mask)
  18. #define chkbit(mask,bit) (mask&(1LL << bit))
  19.  
  20. // debug section start
  21. void __print(int x) {cerr << x;}
  22. void __print(long x) {cerr << x;}
  23. void __print(long long x) {cerr << x;}
  24. void __print(unsigned x) {cerr << x;}
  25. void __print(unsigned long x) {cerr << x;}
  26. void __print(unsigned long long x) {cerr << x;}
  27. void __print(float x) {cerr << x;}
  28. void __print(double x) {cerr << x;}
  29. void __print(long double x) {cerr << x;}
  30. void __print(char x) {cerr << '\'' << x << '\'';}
  31. void __print(const char *x) {cerr << '\"' << x << '\"';}
  32. void __print(const string &x) {cerr << '\"' << x << '\"';}
  33. void __print(bool x) {cerr << (x ? "true" : "false");}
  34.  
  35. template<typename T, typename V>
  36. void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
  37. template<typename T>
  38. void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
  39. void _print() {cerr << "]\n";}
  40. template <typename T, typename... V>
  41. void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
  42. #ifndef ONLINE_JUDGE
  43. #define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
  44. #else
  45. #define debug(x...)
  46. #endif
  47. // debug section closed
  48.  
  49. #define en "\n"
  50. #define sum(n) ((1LL*(n)*(n-1))/ 2LL)
  51. #define sqr(n) (1LL*(n)*(n))
  52. #define vag(a,b) ((a + b - 1)/b)
  53.  
  54. #define MAXN 100010
  55. #define inf 1e6+5
  56. const int mod = 1e9 + 7;
  57.  
  58. ll n, m;
  59. ll a[16], b[16], pre[16];
  60. ll mem[16][(1 << 15)];
  61.  
  62. ll f(ll i, ll mask)
  63. {
  64. if (i == m) {
  65. return 0;
  66. }
  67. if (mask == (1 << n) - 1) {
  68. return INT_MAX;
  69. }
  70.  
  71. if (mem[i][mask] != -1) return mem[i][mask];
  72.  
  73. ll sm = 0;
  74. for (ll j = 0; j < n; j++) {
  75. if (chkbit(mask, j)) {
  76. sm += a[j];
  77. }
  78. }
  79.  
  80. ll an = INT_MAX;
  81.  
  82. for (ll j = 0; j < n; j++) {
  83. if (!chkbit(mask, j) && pre[i] >= sm + a[j]) {
  84. if (sm + a[j] == pre[i]) {
  85. an = min(an, 1 + f(i + 1, mask | (1 << j)));
  86. }
  87. else {
  88. an = min(an, 1 + f(i, mask | (1 << j)));
  89. }
  90. }
  91. }
  92. return mem[i][mask] = an;
  93. }
  94.  
  95. void solve()
  96. {
  97. cin >> n;
  98. for (ll i = 0; i < n; i++) {
  99. cin >> a[i];
  100. }
  101. cin >> m;
  102. for (ll i = 0; i < m; i++) {
  103. cin >> b[i];
  104. pre[i] = (i == 0 ? b[i] : pre[i - 1] + b[i]);
  105. }
  106.  
  107. memset(mem, -1, sizeof(mem));
  108. ll an = f(0, 0);
  109. if (an >= INT_MAX) an = -1;
  110. cout << an << en;
  111.  
  112.  
  113. }
  114. int main()
  115. {
  116. IOS;
  117. ll t;
  118. t = 1;
  119. cin >> t;
  120.  
  121. int c = 0;
  122. while ( t-- )
  123. {
  124. cout << "Case " << ++c << ": ";
  125. solve();
  126. }
  127. return 0;
  128. }
Success #stdin #stdout 0.01s 7584KB
stdin
Standard input is empty
stdout
Case 1: 0