fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define endl '\n'
  4.  
  5. int main()
  6. {
  7. long long n, indprc, ind2prc, price, minprc, min2prc, d, cnt = 0;
  8. unsigned long long plat[10], harga[10] ;
  9. memset(plat,0,sizeof(plat));
  10. cin >> n;
  11. cin >> price;
  12. harga[0] = price;
  13. minprc = price;
  14. indprc = 0;
  15. for (long long i = 1;i <= n;i++) {
  16. cin >> price;
  17. harga[i] = price;
  18. if (price <= minprc) {
  19. minprc = price;
  20. indprc = i;
  21. }
  22. }
  23. cin >> d;
  24.  
  25. // kalau uangnya gacukup gajadi beli
  26. if (minprc > d) {
  27. cout << 0 << endl;
  28. return 0;
  29. }
  30. min2prc = harga[n];
  31. ind2prc = n;
  32.  
  33. // beli yang termurah dan bukan nol
  34. if (indprc == 0) {
  35. for (long long i = n-1;i >= 1 ;i--) {
  36. if (harga[i] < min2prc) {
  37. min2prc = harga[i];
  38. ind2prc = i;
  39. }
  40. }
  41. if (d - min2prc >= 0){
  42. d -= min2prc;
  43. plat[ind2prc]++;
  44. cnt++;
  45. }
  46. else ind2prc = 0;
  47. }
  48. if (ind2prc){
  49.  
  50. // beli yang termurah
  51. plat[indprc] += (d / minprc);
  52. cnt += (d / minprc);
  53. d %= minprc;
  54.  
  55. // kalau bisa tuker yang lebih besar ya tukar
  56. for (int i = indprc; i <= n ; i++) {
  57. for (int j = n; j >= i+1 ;j--) {
  58. if (plat[i] > 0 && d + harga[i] - harga[j] >= 0) {
  59. if (harga[j] == harga[i]) {
  60. plat[j] += plat[i];
  61. plat[i] = 0;
  62. }
  63. else {
  64. plat[j] += d / (harga[j] - harga[i]);
  65. plat[i] -= d / (harga[j] - harga[i]);
  66. d %= (harga[j] - harga[i]) ;
  67. }
  68. break;
  69. }
  70. }
  71. }
  72. }
  73. // bagian ngeprint
  74. if (ind2prc == 0) cnt = 1;
  75. cout << cnt << endl;
  76. if (ind2prc == 0) {
  77. cout << 0 << endl;
  78. cout << 0 << endl;
  79. }
  80. else {
  81. long long hit = 0;
  82. string dep;
  83. for (int i = n; i>=0; i--) {
  84. while (plat[i]>0){
  85. dep += to_string(i);
  86. cout << i;
  87. plat[i]--;
  88. hit++;
  89. if (dep.length() == 50) break;
  90. }
  91. if (dep.length() == 50) break;
  92. }
  93. cout << endl;
  94. if (cnt <= 50) cout << dep;
  95. else {
  96. hit = 0;
  97. for (int i = 0; i<=n; i++) {
  98. while (plat[i]>0){
  99. cout << i;
  100. plat[i]--;
  101. hit++;
  102. if (hit == 50) break;
  103. }
  104. if (hit == 50) break;
  105. }
  106. }
  107. cout << endl;
  108. }
  109. return 0;
  110. }
Success #stdin #stdout 0s 15240KB
stdin
1
100 101
1000
stdout
9
11111111111111111111111111111111111111111111111111
11111111111111111111111111111111111111111111111111