fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. inline int power(int a, int b) {
  6. int x = 1;
  7. while (b) {
  8. if (b & 1) x *= a;
  9. a *= a;
  10. b >>= 1;
  11. }
  12. return x;
  13. }
  14.  
  15.  
  16. const int M = 1000000007;
  17. const int N = 3e5+9;
  18. const int INF = 2e9+1;
  19. const int LINF = 2000000000000000001;
  20.  
  21. //_ ***************************** START Below *******************************
  22.  
  23.  
  24. vector<int> cost(3);
  25. vector<int> amount(3);
  26. vector<int> effect;
  27.  
  28. //* O(n^3)
  29. void bruteforce(int n) {
  30.  
  31. int minCost = INF;
  32. for(int i=0; i<=amount[0]; i++){
  33. for(int j=0; j<=amount[1]; j++){
  34. for(int k=0; k<=amount[2]; k++){
  35. int net = i*effect[0] + j*effect[1] + k*effect[2];
  36. if(net == n){
  37. int totalCost = i*cost[0] + j*cost[1] + k*cost[2];
  38. minCost = min(minCost, totalCost );
  39. }
  40. }
  41. }
  42. }
  43.  
  44. cout << minCost << endl;
  45. }
  46.  
  47. // O(n^2)
  48. void consistency(int n) {
  49.  
  50. int minCost = INF;
  51. for(int i=0; i<=amount[0]; i++){
  52. for(int j=0; j<=amount[1]; j++){
  53. int k = (n - i*effect[0] - j*effect[1] ) / effect[2];
  54.  
  55. if(k>=0 && k<= amount[2]){
  56. int net = i*effect[0] + j*effect[1] + k*effect[2];
  57. if(net == n){
  58.  
  59. int totalCost = i*cost[0] + j*cost[1] + k*cost[2];
  60. minCost = min(minCost, totalCost );
  61. }
  62. }
  63. }
  64. }
  65.  
  66. cout << minCost << endl;
  67. }
  68.  
  69. void solve() {
  70.  
  71. int n;
  72. cin >> n;
  73.  
  74. effect = {2, 3, 5};
  75.  
  76.  
  77. for(int i=0; i<3; i++) cin >> amount[i];
  78. for(int i=0; i<3; i++) cin >> cost[i];
  79.  
  80.  
  81. bruteforce(n);
  82. consistency(n) ;
  83.  
  84. }
  85.  
  86.  
  87.  
  88.  
  89.  
  90. int32_t main() {
  91. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  92.  
  93. int t = 1;
  94. while (t--) {
  95. solve();
  96. }
  97.  
  98. return 0;
  99. }
Success #stdin #stdout 0.01s 5268KB
stdin
10
2 2 1
5 5 20
stdout
20
20