Skip to content

Instantly share code, notes, and snippets.

@sukeesh
Created May 28, 2015 17:27
Show Gist options
  • Save sukeesh/c84cb8447a21fdee381c to your computer and use it in GitHub Desktop.
Save sukeesh/c84cb8447a21fdee381c to your computer and use it in GitHub Desktop.
//HOTELS - Hotels Along the Croatian Coast
/*
Author
-Sukeesh
*/
#include<iostream>
#include<cstdio>
#include<limits.h>
using namespace std;
int main(){
long n,m;
scanf("%ld%ld",&n,&m);
long a[n],i;
for(i=0;i<n;i++)
{
scanf("%ld", &a[i]);
}
long cur=0,j=0,sum=0;
for(i=0;i<n;i++)
{
if(cur+a[i]<m)
{
cur+=a[i];
}
else if(cur+a[i]==m)
{
cout<<m;
return 0;
}
else
{
while(cur+a[i]>m)
{
cur-=a[j];
j++;
}
if(cur+a[i]<m)
{
cur+=a[i];
}
else if(cur+a[i]==m)
{
printf("%ld",m);
return 0;
}
}
sum=max(sum,cur);
}
printf("%ld",sum);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment