fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int maxn=1e6+14;
  4. int n, s, f[maxn];
  5. unordered_map < int , int > m;
  6. void solve(){
  7. cin >> n >> s;
  8. f[0] = 0;
  9. int ans = n + 1;
  10. for (int i = 1; i <= n; i++){
  11. int x;
  12. cin >> x;
  13. f[i] = f[i - 1] + x;
  14. m[f[i]] = i;
  15.  
  16. if (f[i] == s) {
  17. ans = min(ans, i - m[0]);
  18. }
  19. else {
  20. if (m[f[i] - s]) {
  21. ans = min(ans, i - m[f[i] - s]);
  22. }
  23. }
  24. }
  25. if (ans == n + 1) {
  26. cout << -1;
  27. }
  28. else cout << ans;
  29. }
  30. int main(){
  31. ios_base::sync_with_stdio(0);
  32. cin.tie(0); cout.tie(0);
  33. if (fopen("input.txt","r")){
  34. freopen("input.txt","r",stdin);
  35. freopen("output.txt","w",stdout);
  36. }
  37. solve();
  38. return 0;
  39. }
  40.  
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
-1