1 solutions
-
0
C++ :
#include<bits/stdc++.h> using namespace std; int n,s,a[100005],w,tmp,ans=1e5+10,sum; int main(){ cin>>n>>s; for(int i=1;i<=n;i++) cin>>a[i],sum+=a[i]; if(sum<s) {cout<<-1;return 0;} if(sum==s) {cout<<0;return 0;} tmp=a[1]; for(int i=1,j=1;j<=n;) { if(tmp==s) { w=n-j+i-1; ans=min(ans,w); j++; tmp+=a[j]; } else if(tmp>s) tmp-=a[i],i++; else if(tmp<s) j++,tmp+=a[j]; } cout<<ans; return 0; }
- 1
Information
- ID
- 748
- Time
- 1000ms
- Memory
- 128MiB
- Difficulty
- 10
- Tags
- # Submissions
- 3
- Accepted
- 1
- Uploaded By