fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. long long DP[1000001];
  6. unordered_map <long long, int> M;
  7.  
  8. int main() {
  9. ios_base::sync_with_stdio(0);
  10. cin.tie(0); cout.tie(0);
  11.  
  12. int n, sum;
  13. cin >> n >> sum;
  14.  
  15. for (int i = 1; i <= n; i ++) {
  16. cin >> DP[i];
  17. DP[i] += DP[i - 1];
  18. }
  19.  
  20. M[0] = 0;
  21. for (int i = 1; i <= n; i ++)
  22. if (M.find(DP[i]) == M.end())
  23. M[DP[i]] = i;
  24.  
  25. int mx = 0;
  26. for (int i = 1; i <= n; i ++)
  27. if (M.find(DP[i] - sum) != M.end())
  28. mx = max(mx, i - M[DP[i] - sum]);
  29.  
  30. if (mx != 0)
  31. cout << mx;
  32. else
  33. cout << "BRAK";
  34. }
Success #stdin #stdout 0.01s 5384KB
stdin
6 4
3 -2 6 1 -1 5
stdout
4