Advertisement
YEZAELP

SMMR-166: Subsegment Sum

Oct 15th, 2020
116
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. int ar[100010];
  5.  
  6. const int INF = 2e9;
  7.  
  8. int main(){
  9.  
  10.     int n, s;
  11.     scanf("%d%d", &n, &s);
  12.  
  13.     int start = 1, ending = 0, ans = INF, sum = 0;
  14.     for(int i=1;i<=n;i++){
  15.         scanf("%d", &ar[i]);
  16.         sum += ar[i];
  17.         while(sum >= s){
  18.             ans = min(ans, i - start + 1);
  19.             if(sum - ar[start] >= s) sum -= ar[start];
  20.             else break;
  21.             start ++;
  22.         }
  23.     }
  24.  
  25.     if(ans == INF) printf("-1");
  26.     else printf("%d", ans);
  27.  
  28.     return 0;
  29. }
  30.  
Advertisement
RAW Paste Data Copied
Advertisement