Advertisement
MiinaMagdy

624 - CD

May 12th, 2024
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.82 KB | None | 0 0
  1. /*
  2. +---------------------------------------------+
  3. |                                             |
  4. |       © 10/05/2024 (17:44) MinaMagdy        |
  5. |                                             |
  6. +---------------------------------------------+
  7. */
  8. #include <bits/stdc++.h>
  9. #include <ext/pb_ds/assoc_container.hpp>
  10. #include <ext/pb_ds/tree_policy.hpp>
  11.  
  12. using namespace std;
  13. using namespace __gnu_pbds;
  14. #define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
  15. #define multi_ordered_set tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update>
  16. #define endl "\n"
  17. #define MOD 1000000007
  18. #define INF 2000000000
  19. #define all(s) s.begin(), s.end()
  20. #define rall(s) s.rbegin(), s.rend()
  21. #define sz(x) int(x.size())
  22.  
  23. typedef long long ll;
  24. typedef long double ld;
  25. typedef unsigned long long ull;
  26.  
  27. void solve() {
  28.     int n, d;
  29.     while (cin >> n >> d) {
  30.         vector<int> v(d);
  31.         for (auto& I : v)
  32.             cin >> I;
  33.         vector<int> ans;
  34.         int diff = n;
  35.         for (int i = 0; i < (1 << d); ++i) {
  36.             int sum = 0;
  37.             for (int j = 0; j < d; ++j) {
  38.                 if (i & (1 << j))
  39.                     sum += v[j];
  40.             }
  41.             if (sum <= n && diff > n - sum) {
  42.                 diff = n - sum;
  43.                 ans.clear();
  44.                 for (int j = 0; j < d; ++j) {
  45.                     if (i & (1 << j))
  46.                         ans.push_back(v[j]);
  47.                 }
  48.             }
  49.         }
  50.         for (auto& I : ans)
  51.             cout << I << " ";
  52.         cout << "sum:" << n - diff << endl;
  53.     }
  54. }
  55.  
  56. int main(void)
  57. {
  58.     ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  59.     int testcase = 1;
  60.     // cin >> testcase;
  61.     while (testcase--)
  62.         solve();
  63.     return 0;
  64. }
Tags: UVA
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement