fork(1) download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. #define ll long long
  5. const int maxn = (int)1e6 + 1;
  6. int n, k;
  7. vector <int> a(maxn + 1);
  8. int main()
  9. {
  10. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  11.  
  12. cin >> n >> k;
  13. for (int i = 1; i <= n; i++)
  14. {
  15. cin >> a[i];
  16. }
  17. vector <ll> pref(n + 1, 0);
  18. for (int i = 1; i <= n; i++)
  19. {
  20. pref[i] = pref[i - 1] + (a[i] - k);
  21. }
  22. stack <ll> st;
  23. for (int i = 0; i <= n; i++)
  24. {
  25. if (st.empty() || pref[i] < pref[st.top()])
  26. {
  27. st.push(i);
  28. }
  29. }
  30.  
  31. ll ans = 0;
  32. for (int i = n; i >= 0; i--)
  33. {
  34. while (!st.empty() && pref[i] >= pref[st.top()])
  35. {
  36. ans = max(ans, i - st.top());
  37. st.pop();
  38. }
  39. }
  40. cout << ans;
  41. }
Success #stdin #stdout 0.01s 7020KB
stdin
7 3
1 5 2 3 1 4 1
stdout
5