• Source
    1.  
    2. #include <bits/stdc++.h>
    3.  
    4. using namespace std;
    5.  
    6. #define int long long int
    7. #define F first
    8. #define S second
    9. #define pb push_back
    10. #define si set<int>
    11. #define vi vector<int>
    12. #define pii pair<int, int>
    13. #define vpi vector<pii>
    14. #define vpp vector<pair<int, pii>>
    15. #define mii map<int, int>
    16. #define mpi map<pii, int>
    17. #define spi set<pii>
    18. #define endl "\n"
    19. #define sz(x) ((int)x.size())
    20. #define all(p) p.begin(), p.end()
    21. #define double long double
    22. #define que_max priority_queue<int>
    23. #define que_min priority_queue<int, vi, greater<int>>
    24. #define bug(...) __f(#__VA_ARGS__, __VA_ARGS__)
    25. #define print(a) \
    26.   for (auto x : a) \
    27.   cout << x << " "; \
    28.   cout << endl
    29. #define print1(a) \
    30.   for (auto x : a) \
    31.   cout << x.F << " " << x.S << endl
    32. #define print2(a, x, y) \
    33.   for (int i = x; i < y; i++) \
    34.   cout << a[i] << " "; \
    35.   cout << endl
    36.  
    37. inline int power(int a, int b)
    38. {
    39. int x = 1;
    40. while (b)
    41. {
    42. if (b & 1)
    43. x *= a;
    44. a *= a;
    45. b >>= 1;
    46. }
    47. return x;
    48. }
    49.  
    50. template <typename Arg1>
    51. void __f(const char *name, Arg1 &&arg1) { cout << name << " : " << arg1 << endl; }
    52. template <typename Arg1, typename... Args>
    53. void __f(const char *names, Arg1 &&arg1, Args &&...args)
    54. {
    55. const char *comma = strchr(names + 1, ',');
    56. cout.write(names, comma - names) << " : " << arg1 << " | ";
    57. __f(comma + 1, args...);
    58. }
    59.  
    60. const int N = 200005;
    61.  
    62. struct node
    63. {
    64. public:
    65. int n, target;
    66. string path;
    67. node(int row, int tar, string psf)
    68. {
    69. n = row;
    70. target = tar;
    71. path = psf;
    72. }
    73. };
    74.  
    75. void solve()
    76. {
    77.  
    78. int n;
    79. cin >> n;
    80. vi v(n);
    81. for (int i = 0; i < n; i++)
    82. {
    83. cin >> v[i];
    84. }
    85. int k;
    86. cin >> k;
    87. vector<vector<bool>> dp(n + 1, vector<bool>(k + 1, false));
    88. dp[0][0] = true;
    89. for (int i = 1; i <= k; i++)
    90. dp[0][i] = false;
    91. for (int i = 1; i <= n; i++)
    92. dp[i][0] = true;
    93.  
    94. for (int i = 1; i <= n; i++)
    95. {
    96. for (int j = 1; j <= k; j++)
    97. {
    98. if (j < v[i - 1])
    99. dp[i][j] = dp[i - 1][j];
    100. else
    101. dp[i][j] = (dp[i - 1][j] | dp[i - 1][j - v[i - 1]]);
    102. }
    103. }
    104. if (dp[n][k])
    105. cout << "true\n";
    106. else
    107. cout << "false\n";
    108.  
    109. queue<node> q;
    110. q.push({n, k, ""});
    111.  
    112. while (!q.empty())
    113. {
    114. node f = q.front();
    115. q.pop();
    116.  
    117. if (f.n == 0 || f.target == 0)
    118. cout << f.path << endl;
    119. else
    120. {
    121.  
    122. if (f.target >= v[f.n - 1])
    123. {
    124. bool inc = dp[f.n - 1][f.target - v[f.n - 1]];
    125. if (inc)
    126. q.push({f.n - 1, f.target - v[f.n - 1], to_string(f.n - 1) + " " + f.path});
    127. }
    128. bool exc = dp[f.n - 1][f.target];
    129. if (exc)
    130. q.push({f.n - 1, f.target, f.path});
    131. }
    132. }
    133. }
    134.  
    135. int32_t main()
    136. {
    137. ios_base::sync_with_stdio(0);
    138. cin.tie(0);
    139. cout.tie(0);
    140.  
    141. clock_t z = clock();
    142.  
    143. int t = 1;
    144. // cin >> t;
    145. while (t--)
    146. solve();
    147.  
    148. return 0;
    149. }
    150.