fork download
  1. // ~~ icebear love attttt ~~
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6. typedef pair<bool, ll> ii;
  7. typedef pair<int, ii> iii;
  8.  
  9. template<class T>
  10. bool minimize(T &a, const T &b) {
  11. if (a > b) return a = b, true;
  12. return false;
  13. }
  14.  
  15. template<class T>
  16. bool maximize(T &a, const T &b) {
  17. if (a < b) return a = b, true;
  18. return false;
  19. }
  20.  
  21. #define FOR(i,a,b) for(int i=(a); i<=(b); ++i)
  22. #define FORR(i,a,b) for(int i=(a); i>=(b); --i)
  23. #define REP(i, n) for(int i=0; i<(n); ++i)
  24. #define RED(i, n) for(int i=(n)-1; i>=0; --i)
  25. #define MASK(i) (1LL << (i))
  26. #define BIT(S, i) (((S) >> (i)) & 1)
  27. #define mp make_pair
  28. #define pb push_back
  29. #define fi first
  30. #define se second
  31. #define all(x) x.begin(), x.end()
  32. #define task "icebearat"
  33.  
  34. const int MOD = 1e9 + 7;
  35. const int inf = 1e9 + 27092008;
  36. const ll INF = 1e18 + 27092008;
  37. const int N = 5e5 + 5;
  38. int n, l, r, k, a[N], pref[N];
  39. vector<int> compress;
  40. ll cnt[N], sum[N];
  41. void update(ll ft[], int x, ll val) {
  42. for(; x <= n + 1; x += x & -x) ft[x] += val;
  43. }
  44. ll get(ll ft[], int x) {
  45. ll ans = 0;
  46. for(; x; x -= x & -x) ans += ft[x];
  47. return ans;
  48. }
  49.  
  50. int getLower(int x) {
  51. return upper_bound(all(compress), x) - compress.begin();
  52. }
  53.  
  54. bool check(int x) {
  55. memset(cnt, 0, sizeof cnt);
  56. ll great = 0;
  57. FOR(i, l, n) {
  58. update(cnt, pref[i - l], +1);
  59. int val = getLower(compress[pref[i] - 1] - x);
  60. int cnt_great = get(cnt, val);
  61. great += cnt_great;
  62. if (great >= k) return true;
  63. if (i >= r) update(cnt, pref[i - r], -1);
  64. }
  65. return false;
  66. }
  67.  
  68. ll getAns(int x) {
  69. memset(cnt, 0, sizeof cnt);
  70. ll great = 0;
  71. ll sum_great = 0;
  72. FOR(i, l, n) {
  73. update(cnt, pref[i - l], +1);
  74. update(sum, pref[i - l], +compress[pref[i - l] - 1]);
  75.  
  76. int val = getLower(compress[pref[i] - 1] - x);
  77. int cnt_great = get(cnt, val);
  78. // cerr << val << ' ' << pref[i - l] << ' ' << compress[pref[i] - 1] << ' ' << cnt_great << ' ' << get(sum, val) << '\n';
  79. great += cnt_great;
  80. sum_great += 1LL * cnt_great * compress[pref[i] - 1] - get(sum, val);
  81.  
  82. if (i >= r) {
  83. update(cnt, pref[i - r], -1);
  84. update(sum, pref[i - r], -compress[pref[i - r] - 1]);
  85. }
  86. }
  87. return sum_great - 1LL * max(great - k, 0ll) * x;
  88. }
  89.  
  90. void init(void) {
  91. cin >> n >> k >> l >> r;
  92. FOR(i, 1, n) cin >> a[i], pref[i] = pref[i - 1] + a[i];
  93. }
  94.  
  95. void process(void) {
  96. FOR(i, 0, n) compress.pb(pref[i]);
  97. sort(all(compress));
  98. compress.resize(unique(all(compress)) - compress.begin());
  99. FOR(i, 0, n) pref[i] = getLower(pref[i]);
  100. ll low = -1000 * n, high = 1000 * n;
  101. ll res = -low;
  102. while(low <= high) {
  103. ll mid = (low + high) >> 1;
  104. if (check(mid)) res = mid, low = mid + 1;
  105. else high = mid - 1;
  106. }
  107. cout << getAns(res);
  108. }
  109.  
  110. signed main() {
  111. ios_base::sync_with_stdio(0);
  112. cin.tie(0); cout.tie(0);
  113. if (fopen(task".inp", "r")) {
  114. freopen(task".inp", "r", stdin);
  115. freopen(task".out", "w", stdout);
  116. }
  117. int tc = 1;
  118. // cin >> tc;
  119. while(tc--) {
  120. init();
  121. process();
  122. }
  123. return 0;
  124. }
  125.  
Success #stdin #stdout 0.01s 9748KB
stdin
Standard input is empty
stdout
Standard output is empty