fork download
  1. #include <bits/stdc++.h> // NeOWami
  2. using namespace std;
  3.  
  4. #define ft first
  5. #define sc second
  6. #define int long long
  7. const int inf = 1e17;
  8. const int MSK = 262145;
  9. const int N = 10, K = 11;
  10. int a[N], k, ans = inf;
  11. int W[K][5];
  12. int POW[N];
  13.  
  14. bool ckmin(int &u, int v) {
  15. if (u > v) return u = v, 1;
  16. return 0;
  17. }
  18. int getMsk(int f[]) {
  19. int ans = 0;
  20. for (int i = 0; i <= 8; i++) ans += f[i] * POW[i];
  21. return ans;
  22. }
  23. int changeMsk(int msk, int i, int t) {
  24. int x = (msk % POW[i + 1]) / POW[i];
  25. msk -= POW[i] * x;
  26. msk += POW[i] * ((x + t) % 4);
  27. return msk;
  28. }
  29. void Try(int msk, int i, int val) {
  30. if (i > k) {
  31. if (msk == 0) ans = min(ans, val);
  32. return;
  33. }
  34. Try(msk, i + 1, val);
  35. for (int times = 1; times <= 3; times++) {
  36. for (int j = 0; j < 4; j++) msk = changeMsk(msk, W[i][j] - 1, 1);
  37. val += W[i][4];
  38. Try(msk, i + 1, val);
  39. }
  40. }
  41. signed main() {
  42. cin.tie(NULL)->sync_with_stdio(false);
  43. if(ifstream("CLOCKS.inp")) {
  44. freopen("CLOCKS.inp", "r", stdin);
  45. freopen("CLOCKS.out", "w", stdout);
  46. }
  47. POW[0] = 1; for (int i = 1; i < N; i++) POW[i] = POW[i - 1] * 4;
  48. for (int i = 0; i <= 8; i++) {
  49. cin >> a[i];
  50. if (a[i] == 12) a[i] = 0;
  51. else if (a[i] == 3) a[i] = 3;
  52. else if (a[i] == 6) a[i] = 2;
  53. else if (a[i] == 9) a[i] = 1;
  54. }
  55. cin >> k;
  56. for (int i = 1; i <= k; i++) {
  57. for (int j = 0; j < 5; j++) cin >> W[i][j];
  58. }
  59. Try(getMsk(a), 1, 0);
  60. if (ans == inf) cout << -1;
  61. else cout << ans;
  62. return 0;
  63. }
  64.  
  65.  
  66.  
  67.  
  68. //GEN
  69. /*
  70. #include <bits/stdc++.h>
  71. using namespace std;
  72.  
  73. #define int long long
  74.  
  75. mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  76.  
  77. int randint(int l, int r) {
  78.   return uniform_int_distribution<int> (l, r) (rng);
  79. }
  80.  
  81. signed main() {
  82.   for (int i = 1; i <= 9; i++) {
  83.   cout << randint(1, 4) * 3 << " ";
  84.   }
  85.   cout << "\n";
  86.   int k = randint(1, 10);
  87.   cout << k << "\n";
  88.   for (int i = 1; i <= k; i++) {
  89.   for (int j = 1; j <= 4; j++) cout << randint(1, 9) << " "; cout << randint(1, 10) << "\n";
  90.   }
  91. }
  92.  
  93. */
  94.  
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
Standard output is empty