Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.93 KB | None | 0 0
  1. #include <fstream>
  2. #include <cstring>
  3. #include <stack>
  4.  
  5. using namespace std;
  6.  
  7. ifstream inf("e_also_big.in");
  8. ofstream outf("e_also_big.out");
  9.  
  10. const int MMAX = 1000000000, NMAX = 10000;
  11.  
  12. int v[NMAX];
  13. short dp[MMAX + 1];
  14. stack<short> stk;
  15.  
  16. int main() {
  17.     int n, m, x;
  18.     inf >> m >> n;
  19.     memset(dp, 0x3f, sizeof(dp));
  20.     dp[0] = -1;
  21.     for(int i = 0; i < n; i++) {
  22.         inf >> x;
  23.         for(int j = x; j <= m; j++) {
  24.             if(dp[j] > n && dp[j - x] < i) {
  25.                 dp[j] = i;
  26.             }
  27.         }
  28.         v[i] = x;
  29.     }
  30.     int rez, cnt = 0;
  31.     for(int i = m; i >= 0; i--) {
  32.         if(dp[i] < n) {
  33.             rez = i;
  34.             break;
  35.         }
  36.     }
  37.     while(rez) {
  38.         cnt++;
  39.         stk.push(dp[rez]);
  40.         rez -= v[dp[rez]];
  41.     }
  42.     outf << cnt << '\n';
  43.     while(!stk.empty()) {
  44.         outf << stk.top() << ' ';
  45.         stk.pop();
  46.     }
  47.     return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement