fork download
  1. /* Authors: Vu Hoang Bach from Phuoc Hoa Secondary School */
  2.  
  3. //#pragma GCC optimize("O3", "unroll-loops")
  4. //#pragma GCC target("avx2", "bmi", "bmi2", "lzcnt", "popcnt")
  5.  
  6. #include <bits/stdc++.h>
  7. #define ldb long double
  8. //#define double ldb
  9. #define db double
  10. #define unomap unordered_map
  11. #define unoset unordered_set
  12. #define endl '\n'
  13. #define str string
  14. #define strstr stringstream
  15. #define sz(a) (int)a.size()
  16. #define ll long long
  17. //#define int ll
  18. #define pii pair <int, int>
  19. #define pll pair <ll, ll>
  20. #define Unique(a) a.resize(unique(all(a)) - a.begin())
  21. #define ull unsigned ll
  22. #define fir first
  23. #define sec second
  24. #define idc cin.ignore()
  25. #define lb lower_bound
  26. #define ub upper_bound
  27. #define all(s) s.begin(), s.end()
  28. #define rev reverse
  29. #define sigma ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  30. #define skibidi int main
  31. #define rizz signed main
  32. #define gcd __gcd
  33. #define found(a, mp) mp.find(a) != mp.end()
  34. #define game_over exit(0);
  35. #define stfu return 0;
  36. #define stop break;
  37. #define pushb push_back
  38. #define popb pop_back
  39. #define pushf push_front
  40. #define popf pop_front
  41. #define mul2x(a, x) a << x
  42. #define div2x(a, x) a >> x
  43. #define no_check continue;
  44. #define lcm(a, b) a / __gcd(a, b) * b
  45. #define log_base(x, base) log(x) / log(base)
  46. #define debug clog << "No errors!"; game_over;
  47. #define forw(i, a, b) for (int i = a; i <= b; ++i)
  48. #define forw2(i, a, b) for (ll i = a; i <= b; ++i)
  49. #define fors(i, a, b) for (int i = a; i >= b; --i)
  50. #define fors2(i, a, b) for (ll i = a; i >= b; --i)
  51. #define pqueue priority_queue
  52. #define sqrt sqrtl
  53. #define name "GDARTS"
  54. #define want_digit(x) cout << fixed << setprecision(x);
  55. #define excuting_time 1000.0 * clock() / CLOCKS_PER_SEC
  56. using namespace std;
  57. const int MOD = 1e9 + 7; // 998244353
  58. const int inf = 1e9;
  59. const ll INF = 1e18;
  60. const int N = 1e3;
  61.  
  62. mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
  63. ll random(const ll &L, const ll &R)
  64. {
  65. return uniform_int_distribution<ll> (L, R) (rng);
  66. }
  67.  
  68. template <class X, class Y>
  69. bool maximize(X &x, const Y &y)
  70. {
  71. return x < y ? x = y, true : false;
  72. }
  73.  
  74. int n, m, a[N + 5], ans;
  75. multiset <ll> se;
  76.  
  77. int get(int x)
  78. {
  79. multiset <ll>::iterator it = se.ub(m - x);
  80. return *(prev(it));
  81. }
  82.  
  83. void cook()
  84. {
  85. cin >> n >> m;
  86. forw (i, 1, n) cin >> a[i];
  87.  
  88. se.insert(0);
  89. se.insert(0);
  90. forw (i, 1, n) se.insert(a[i]);
  91.  
  92. forw (i, 1, n) forw (j, i, n)
  93. se.insert(a[i] + a[j]);
  94.  
  95. ans = get(m);
  96. forw (i, 1, n)
  97. {
  98. forw (j, i, n)
  99. {
  100. int curr = a[i] + a[j] + get(a[i] + a[j]);
  101. if (curr <= m)
  102. maximize(ans, curr);
  103. }
  104.  
  105. se.erase(se.find(a[i]));
  106. forw (j, 1, i - 1) se.erase(se.find(a[i] + a[j]));
  107. }
  108.  
  109. cout << ans << endl;
  110. }
  111.  
  112. skibidi()
  113. //rizz()
  114. {
  115. srand(time(NULL));
  116. sigma;
  117. if (fopen(name".INP", "r"))
  118. {
  119. freopen(name".INP", "r", stdin);
  120. freopen(name".OUT", "w", stdout);
  121. }
  122. int numTest = 1;
  123. // cin >> numTest;
  124. while (numTest--)
  125. {
  126. cook();
  127. }
  128. stfu;
  129. }
  130.  
Success #stdin #stdout 0.01s 5280KB
stdin
4 50
3 14 15 9
stdout
48