Skip to content

Instantly share code, notes, and snippets.

@completejavascript
Created September 15, 2018 10:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save completejavascript/960ce1d979d45eb851a6b1e29f37e69a to your computer and use it in GitHub Desktop.
Save completejavascript/960ce1d979d45eb851a6b1e29f37e69a to your computer and use it in GitHub Desktop.
#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