fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define mapll map<ll,ll>
  5. #define mapint map<int,int>
  6. #define pb push_back
  7. #define f(i, n) for (ll i = 0; i < n; i++)
  8. #define in(a, n) vector<ll> a(n); f(i, n) cin >> a[i];
  9. #define MOD 1000000007
  10. #define eod cout<<endl;return
  11.  
  12. bool isPrime(ll n);
  13. ll lg(ll a, ll b);
  14. ll power(ll a, ll b, ll mod = MOD);
  15. ll sumofdigits(ll x);
  16. ll factors(ll x);
  17.  
  18. void solve() {
  19. ll n,k;cin>>n>>k;
  20. ll a[n+1];
  21. for(int i=1;i<=n;i++){cin>>a[i];}
  22. mapll mp1,mp2;
  23. ll sum=0;
  24. mp1[0]=1;
  25. for(int i=1;i<=n;i++){
  26. if(!mp1[sum] && sum!=0){
  27. mp1[sum]=i;}
  28. sum+=a[i];
  29. }
  30. sum=0;
  31. mp2[0]=0;
  32. for(int i=n;i>=1;i--){
  33. if(!mp2[sum] && sum!=0){
  34. mp2[sum]=n-i;}
  35. sum+=a[i];
  36. }ll ans=0;
  37. for(auto [x,y]:mp1){
  38. if((sum-x-k)==0){ans=max(n-y+1,ans);}
  39. else if(mp2[sum-x-k]){ans=max(n-y+1-mp2[sum-x-k],ans);}
  40. }
  41. cout<<ans<<endl;
  42.  
  43.  
  44. }
  45.  
  46. int main() {
  47. std::ios::sync_with_stdio(false);
  48. std::cin.tie(nullptr); std::cout.tie(nullptr);
  49.  
  50. solve();
  51. return 0;
  52. }
  53.  
  54. bool isPrime(ll n) {
  55. if (n <= 1) return false;
  56. if (n == 2 || n == 3) return true;
  57. for (ll i = 2; i * i <= n; ++i) {
  58. if (n % i == 0) return false;
  59. }
  60. return true;
  61. }
  62.  
  63. ll lg(ll a, ll b) {
  64. if (b <= 1 || a < b) return 0;
  65. ll k = 0;
  66. while (a >= b) {
  67. a /= b;
  68. k++;
  69. }
  70. return k;
  71. }
  72.  
  73. ll power(ll a, ll b, ll mod) {
  74. ll res = 1;
  75. while (b) {
  76. if (b & 1) res = (res * a) % mod;
  77. a = (a * a) % mod;
  78. b >>= 1;
  79. }
  80. return res;
  81. }
  82.  
  83. ll sumofdigits(ll x) {
  84. ll k = 0;
  85. while (x > 0) {
  86. k += x % 10;
  87. x /= 10;
  88. }
  89. return k;
  90. }
  91.  
  92. ll factors(ll x) {
  93. ll f = 0;
  94. for (ll i = 1; i * i <= x; i++) {
  95. if (x % i == 0) {
  96. f += 2;
  97. if (x == i * i) f--;
  98. }
  99. }
  100. return f;
  101. }
Success #stdin #stdout 0.01s 5296KB
stdin
6 -5
-5 8 -14 2 4 12
stdout
5