fork download
  1. /* Authors: Vu Hoang Bach from Phuoc Hoa Secondary School */
  2. /* Enjoy TheFatRat's music while reading my code. Link: https://w...content-available-to-author-only...e.com/watch?v=GCx9FU59FJc&t=669s */
  3.  
  4. //#pragma GCC optimize("O3", "unroll-loops")
  5. //#pragma GCC target("avx2", "bmi", "bmi2", "lzcnt", "popcnt")
  6.  
  7. #include <bits/stdc++.h>
  8. #define ldb long double
  9. //#define double ldb
  10. #define db double
  11. #define name "test"
  12. #define unomap unordered_map
  13. #define unoset unordered_set
  14. #define endl '\n'
  15. #define str string
  16. #define strstr stringstream
  17. #define sz(a) a.size()
  18. #define ll long long
  19. //#define int ll
  20. #define pii pair <int, int>
  21. #define pll pair <ll, ll>
  22. #define Unique(a) a.resize(unique(all(a)) - a.begin())
  23. #define ull unsigned ll
  24. #define fir first
  25. #define sec second
  26. #define idc cin.ignore()
  27. #define lb lower_bound
  28. #define ub upper_bound
  29. #define all(s) s.begin(), s.end()
  30. #define rev reverse
  31. #define sigma ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  32. #define skibidi int main
  33. #define rizz signed main
  34. #define gcd __gcd
  35. #define found(a, mp) mp.find(a) != mp.end()
  36. #define game_over exit(0);
  37. #define stfu return 0;
  38. #define stop break;
  39. #define odd(n) n & 1
  40. #define pushb push_back
  41. #define popb pop_back
  42. #define pushf push_front
  43. #define popf pop_front
  44. #define even(n) !odd(n)
  45. #define mul2x(a, x) a << x
  46. #define div2x(a, x) a >> x
  47. #define no_check continue;
  48. #define lcm(a, b) a / __gcd(a, b) * b
  49. #define log_base(x, base) log(x) / log(base)
  50. #define debug clog << "No errors!"; game_over;
  51. #define forw(i, a, b) for (int i = a; i <= b; ++i)
  52. #define forw2(i, a, b) for (ll i = a; i <= b; ++i)
  53. #define fors(i, a, b) for (int i = a; i >= b; --i)
  54. #define fors2(i, a, b) for (ll i = a; i >= b; --i)
  55. #define meme memset
  56. #define pqueue priority_queue
  57. #define want_digit(x) cout << fixed << setprecision(x);
  58. #define excuting_time 1000.0 * clock() / CLOCKS_PER_SEC
  59. using namespace std;
  60. const int MOD = 1e9 + 7; // 998244353
  61. const int inf = 1e9;
  62. const ll INF = 1e18;
  63. const int N = 4e5;
  64.  
  65. mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
  66. ll random(const ll &L, const ll &R)
  67. {
  68. return uniform_int_distribution<ll> (L, R) (rng);
  69. }
  70.  
  71. template <class X, class Y>
  72. bool maximize(X &x, const Y &y)
  73. {
  74. return x < y ? x = y, true : false;
  75. }
  76.  
  77. struct query
  78. {
  79. int last, start, finish;
  80. };
  81.  
  82. int n, k, a[N + 5];
  83. query res[N + 5];
  84. deque <ll> dq;
  85. ll dp[N + 5], ans, pre[N + 5];
  86.  
  87. ll get(int ind)
  88. {
  89. return dp[ind] - pre[ind + 1];
  90. }
  91.  
  92. void cook()
  93. {
  94. cin >> n >> k;
  95. forw (i, 1, n)
  96. cin >> a[i], pre[i] = pre[i - 1] + a[i];
  97.  
  98. forw (i, 1, n)
  99. {
  100. while (!dq.empty() && dq.front() < i - k) dq.popf();
  101. if (i < k)
  102. {
  103. dp[i] = pre[i];
  104. res[i].last = 0; res[i].start = 1; res[i].finish = i;
  105. }
  106. else
  107. {
  108. dp[i] = pre[i] + get(dq.front());
  109. res[i].last = dq.front(); res[i].start = dq.front() + 2; res[i].finish = i;
  110. }
  111. maximize(ans, dp[i]);
  112. while (!dq.empty() && get(dq.back()) <= get(i - 1)) dq.popb();
  113. dq.pushb(i - 1);
  114. }
  115.  
  116. ll ans = 0;
  117. int ind = 1;
  118. forw (i, 1, n)
  119. {
  120. if (maximize(ans, dp[i]))
  121. ind = i;
  122. }
  123.  
  124. vector <int> v;
  125. while (ind)
  126. {
  127. fors (i, res[ind].finish, res[ind].start) v.pushb(i);
  128. ind = res[ind].last;
  129. }
  130.  
  131. rev(all(v));
  132. cout << sz(v) << ' ' << ans << endl;
  133. forw (i, 0, sz(v) - 1) cout << v[i] << ' ';
  134. cout << endl;
  135. }
  136.  
  137. skibidi()
  138. //rizz()
  139. {
  140. srand(time(NULL));
  141. sigma;
  142. /*
  143.   if (fopen(name".INP", "r"))
  144.   {
  145.   freopen(name".INP", "r", stdin);
  146.   freopen(name".OUT", "w", stdout);
  147.   }
  148.   */
  149. int numTest = 1;
  150. // cin >> numTest;
  151. while (numTest--)
  152. {
  153. cook();
  154. }
  155. stfu;
  156. }
Success #stdin #stdout 0.01s 9680KB
stdin
6 3
6 10 10 13 10 10
stdout
4 40
2 3 5 6