Alex_tz307

Fundamente XI - 6 / 219

Sep 21st, 2020 (edited)
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define INF 0x3f3f3f3f
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7.     ios_base::sync_with_stdio(false);
  8.     cin.tie(nullptr);
  9.     cout.tie(nullptr);
  10.     int N, S;
  11.     cin >> N >> S;
  12.     vector < int > a(N);
  13.     for(int& x : a)
  14.         cin >> x;
  15.     vector < int > dp(S + 1, INF), t(S + 1);
  16.     dp[a[0]] = 1;
  17.     t[a[0]] = a[0];
  18.     for(int i = 1; i < N; ++i) {
  19.         for(int j = S - a[i]; j >= 0; --j)
  20.             if(dp[j] && dp[j] + 1 < dp[j + a[i]]) {
  21.                 dp[j + a[i]] = dp[j] + 1;
  22.                 t[j + a[i]] = a[i];
  23.             }
  24.         if(dp[a[i]] == INF) {
  25.             dp[a[i]] = 1;
  26.             t[a[i]] = a[i];
  27.         }
  28.     }
  29.     cout << dp[S] << '\n';
  30.     vector < int > sol;
  31.     while(S > 0) {
  32.         sol.emplace_back(t[S]);
  33.         S -= t[S];
  34.     }
  35.     reverse(sol.begin(), sol.end());
  36.     for(int it : sol)
  37.         cout << it << ' ';
  38. }
  39.  
Advertisement
Add Comment
Please, Sign In to add comment