Advertisement
Guest User

Bshka

a guest
Oct 15th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int N = 1e6 + 5;
  5.  
  6. int dp[N], n, x, ar[105], ans = 2e9;
  7.  
  8. main()
  9. {
  10.     cin >> n >> x;
  11.     for (int i = 1; i <= n; i++)
  12.         scanf("%d", &ar[i]);
  13.  
  14.     memset(dp, 0x3f3f3f3f, sizeof(dp));
  15.  
  16.     dp[0] = 0;
  17.  
  18.     for (int i = 1; i <= x; i++)
  19.     {
  20.         int sum = 0;
  21.         for (int j = 1; j <= n; j++)
  22.         {
  23.             sum += ar[j];
  24.  
  25.             if (i >= sum)
  26.             {
  27.                 dp[i] = min( dp[i], dp[i - sum] + j + 1 );
  28.  
  29.                 if ( j == n && (x - i) % ar[n] == 0 )
  30.                     ans = min(ans, dp[i - sum] + j + 1 + (x - i) / ar[n]);
  31.             }
  32.         }
  33.     }
  34.     ans = min(ans, dp[x]);
  35.     if (ans > 1e9)
  36.         ans = 0;
  37.     cout << ans - 1 << endl;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement