fork(2) download
  1. // Source by Gabriel Vasconcelos
  2. // Pay me 10$ per use or satan will curse your soul.
  3.  
  4. #include <bits/stdc++.h>
  5. using namespace std;
  6. #define endl '\n'
  7. #define min(x, y) ((x) < (y)) ? (x) : (y)
  8. #define max(x, y) ((x) > (y)) ? (x) : (y)
  9. #define abs(x) ((x) < 0) ? -(x) : (x)
  10.  
  11. typedef long long int64;
  12. typedef unsigned long long uint64;
  13. typedef unsigned uint;
  14.  
  15. int main() {
  16. ios_base::sync_with_stdio(false);
  17. uint n, h, k, buf;
  18. uint total = 0;
  19. uint current;
  20. vector<uint> potatoes;
  21. bool found, added;
  22.  
  23. cin >> n >> h >> k;
  24.  
  25. for (uint i = 0; i < n; ++i)
  26. {
  27. cin >> buf;
  28. potatoes.push_back(buf);
  29. }
  30.  
  31. cout << "Letf\n";
  32.  
  33. sort(potatoes.begin(), potatoes.end());
  34.  
  35. uint j = n - 1;
  36. while(potatoes[0] + potatoes[j] > h)
  37. {
  38. potatoes[j];
  39. j--;
  40. total++;
  41. }
  42.  
  43. current = potatoes[0] + potatoes[j];
  44.  
  45. while (true)
  46. {
  47. found = added = false;
  48. for (int i = j - 1; i >= 0; --i)
  49. {
  50. if (!found && potatoes[i] > 0) found = true;
  51. if (current + potatoes[i] < h && potatoes[i] > 0)
  52. {
  53. current += potatoes[i] - k;
  54. potatoes[i] = -1;
  55. total++;
  56. added = true;
  57. break;
  58. }
  59. }
  60. if (found && !added)
  61. {
  62. current -= k;
  63. total++;
  64. }
  65. else if (!found)
  66. {
  67. break;
  68. }
  69. }
  70.  
  71. cout << total;
  72.  
  73. return 0;
  74. }
Time limit exceeded #stdin #stdout 5s 3468KB
stdin
5 6 3
5 4 3 2 1
stdout
Standard output is empty