fork download
  1. /*****************************************
  2. Creator: triclone_3, chinguyenylyd
  3. *****************************************/
  4. #include<bits/stdc++.h>
  5. using namespace std;
  6. #define endl "\n"
  7. #define onic ios_base::sync_with_stdio(false); cin.tie(NULL);
  8. #define kairi main()
  9. int main()
  10. {
  11. ios_base::sync_with_stdio(false);
  12. cin.tie(NULL);
  13. long long k, a, b, c, d;
  14. cin >> k >> a >> b >> c >> d;
  15. if ( a * b * c >=0 && a * b * c <= 1e6)
  16. {
  17. long long ma=-1;
  18. for(long long i = 0; i <= a; i++)
  19. {
  20. for(long long j = 0; j <= b; j++)
  21. {
  22. for(long long ij = 0; ij<= c; ij++)
  23. {
  24. long long cnt = k - i - j*5 - ij*10;
  25. if (cnt >= 0 and cnt % 25 == 0)
  26. {
  27. long long x = cnt/25;
  28. if (x <= d)
  29. ma=max(ma, i + j + ij + x);
  30. }
  31. }
  32. }
  33. }
  34. if (ma != -1)
  35. cout << ma;
  36. else
  37. cout << "Impossible";
  38. return 0;
  39. }
  40. if (a * b * c <= 1e27)
  41. {
  42. long long x;
  43. b*=5;
  44. c*=10;
  45. d*=25;
  46. if(a >= k%5)
  47. {
  48. x+=k%5;
  49. a-=k%5;
  50. k-=k%5;
  51. }
  52. if(a >= 5)
  53. {
  54. x+=min(k,a/5*5);
  55. k-=min(k,a/5*5);
  56. }
  57. while(k > b+c && d > 0 && k >= 25)
  58. {
  59. k-=25;
  60. d-=25;
  61. x++;
  62. }
  63. while(k > b && c > 0 && k >= 10)
  64. {
  65. k-=10;
  66. c-=10;
  67. x++;
  68. }
  69. while(k > 0 && b > 0 && k >= 5)
  70. {
  71. k-=5;
  72. b-=5;
  73. x++;
  74. }
  75. if(k > 0)
  76. cout<<"Impossible";
  77. else
  78. cout<<x;
  79. return 0;
  80. }
  81. else
  82. {
  83. long long ans = 0;
  84. if (a*1 + b*5 + c*10 + d*25 < k)
  85. {
  86. cout << "Impossible";
  87. return 0;
  88. }
  89. if (a*1 + b*5 + c*10 + d*25 == k)
  90. {
  91. cout << a + b + c + d;
  92. return 0;
  93. }
  94. while (a%5 != k%5)
  95. a--;
  96. if (a < 0)
  97. {
  98. cout << "Impossible";
  99. return 0;
  100. }
  101. k -= a;
  102. if (k <= 0)
  103. {
  104. cout << k;
  105. return 0;
  106. }
  107. ans += a;
  108. while (k%2 != b%2)
  109. b--;
  110. if (b < 0)
  111. {
  112. while (k%25 != c%5)
  113. c--;
  114. if (c < 0)
  115. {
  116. cout << "Impossible";
  117. return 0;
  118. }
  119. if (k <= c*10)
  120. {
  121. cout << ans + k/10;
  122. return 0;
  123. }
  124. k -= c*10;
  125. ans += c;
  126. if (k/25 <= d)
  127. {
  128. cout << ans + k/25;
  129. return 0;
  130. }
  131. }
  132. k -= b*5;
  133. if (k <= 0)
  134. {
  135. cout << ans + (k + b*5)/5;
  136. return 0;
  137. }
  138. ans += b;
  139. while (k%25 != c%5)
  140. c--;
  141. if (c < 0)
  142. {
  143. cout << "Impossible";
  144. return 0;
  145. }
  146. if (k <= c*10)
  147. {
  148. cout << ans + k/10;
  149. return 0;
  150. }
  151. k -= c*10;
  152. ans += c;
  153. if (k/25 <= d)
  154. {
  155. cout << ans + k/25;
  156. return 0;
  157. }
  158. cout << "Impossible";
  159. }
  160. }
  161. //K A 8 4 8
  162.  
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
93926837338120