Created
September 15, 2018 10:48
-
-
Save completejavascript/960ce1d979d45eb851a6b1e29f37e69a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
int main() | |
{ | |
int n = 0, m = 0; | |
scanf("%d%d",&n,&m); | |
int *a = new int[n]; | |
for(int i = 0; i < n; i++) | |
scanf("%d",&a[i]); | |
int st = 0; | |
int i = st; | |
int sum = 0; | |
int max = 0; | |
while(i < n) | |
{ | |
sum += a[i]; | |
if(sum > m) | |
{ | |
sum -= a[i]; | |
if(sum > max) max = sum; | |
sum += a[i]; | |
while(sum > m) | |
sum -= a[st++]; | |
} | |
i++; | |
} | |
if(sum > max) max = sum; | |
printf("%d\n",max); | |
delete[] a; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment